From 1b63702b8efb4103a8f96cdec5835482542d0bcd Mon Sep 17 00:00:00 2001 From: Simone Basso Date: Tue, 18 Feb 2020 17:22:06 +0100 Subject: [PATCH] Allow compiling with CGO_ENABLED=1 and no measurement-kit (#344) I want to build an `.aar` file that only includes experiments written in Go such that I can link such `.aar` file into the OONI app along with measurement-kit/android-libs. To this end, I need to be able to compile this repository with CGO enabled, to cross compile for Android, but without linking with MK. This work is part of https://github.com/ooni/probe-engine/issues/339. --- .github/workflows/build.yml | 2 +- README.md | 7 +++---- measurementkit/{ => mkcgo}/force.cpp | 0 measurementkit/{task_cgo.go => mkcgo/mkcgo.go} | 12 ++++++------ measurementkit/task_mk.go | 15 +++++++++++++++ .../{task_otherwise.go => task_nomk.go} | 2 +- 6 files changed, 26 insertions(+), 12 deletions(-) rename measurementkit/{ => mkcgo}/force.cpp (100%) rename measurementkit/{task_cgo.go => mkcgo/mkcgo.go} (94%) create mode 100644 measurementkit/task_mk.go rename measurementkit/{task_otherwise.go => task_nomk.go} (92%) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 743da5d5..3f317340 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -13,7 +13,7 @@ jobs: with: go-version: ${{ matrix.go }} - uses: actions/checkout@v2 - - run: go test -v -coverprofile=probe-engine.cov -coverpkg=./... ./... + - run: go test -tags nomk -v -coverprofile=probe-engine.cov -coverpkg=./... ./... env: CGO_ENABLED: 0 - name: Send coverage diff --git a/README.md b/README.md index f2b2c72d..216413c1 100644 --- a/README.md +++ b/README.md @@ -28,10 +28,9 @@ and requires Go v1.13+. We also depend on [Measurement Kit]( https://github.com/measurement-kit/measurement-kit), a C++14 library implementing many OONI tests. -Note that `export CGO_ENABLED=0` will disable C/C++ extensions and -therefore will prevent Measurement Kit tests from being linked into -the resulting Go binaries. You may want that in some cases, e.g. when -you only want to use OONI tests written in Go. +Note that passing the `-tags nomk` flag to Go will disable linking +Measurement Kit into the resulting Go binaries. You may want that in +cases where you only want to use experiments written in Go. We plan on gradually rewriting all OONI tests in Go, therefore the dependency on Measurement Kit will eventually be removed. diff --git a/measurementkit/force.cpp b/measurementkit/mkcgo/force.cpp similarity index 100% rename from measurementkit/force.cpp rename to measurementkit/mkcgo/force.cpp diff --git a/measurementkit/task_cgo.go b/measurementkit/mkcgo/mkcgo.go similarity index 94% rename from measurementkit/task_cgo.go rename to measurementkit/mkcgo/mkcgo.go index 3ebf8b58..512532aa 100644 --- a/measurementkit/task_cgo.go +++ b/measurementkit/mkcgo/mkcgo.go @@ -1,4 +1,7 @@ -package measurementkit +// +build cgo + +// Package mkcgo contains CGO bindings to Measurement Kit. +package mkcgo import ( // #include @@ -75,7 +78,8 @@ func taskstart(settings []byte) *C.mk_task_t { return C.mk_task_start(settingsp) } -func start(settings []byte) (<-chan []byte, error) { +// Start starts a Measurement Kit task. +func Start(settings []byte) (<-chan []byte, error) { taskp := taskstart(settings) if taskp == nil { return nil, errors.New("C.mk_task_start failed") @@ -84,7 +88,3 @@ func start(settings []byte) (<-chan []byte, error) { go taskloop(taskp, out) return out, nil } - -func available() bool { - return true -} diff --git a/measurementkit/task_mk.go b/measurementkit/task_mk.go new file mode 100644 index 00000000..eb4cf52f --- /dev/null +++ b/measurementkit/task_mk.go @@ -0,0 +1,15 @@ +// +build !nomk + +package measurementkit + +import ( + "github.com/ooni/probe-engine/measurementkit/mkcgo" +) + +func start(settings []byte) (<-chan []byte, error) { + return mkcgo.Start(settings) +} + +func available() bool { + return true +} diff --git a/measurementkit/task_otherwise.go b/measurementkit/task_nomk.go similarity index 92% rename from measurementkit/task_otherwise.go rename to measurementkit/task_nomk.go index a095f719..090ac9e4 100644 --- a/measurementkit/task_otherwise.go +++ b/measurementkit/task_nomk.go @@ -1,4 +1,4 @@ -// +build !cgo +// +build nomk package measurementkit