Skip to content

Commit 23a0503

Browse files
committed
cmd/gomobile: enable Cgo
This CL gives CGO_ENABLED=1 explicitly when executing gobind since Cgo is disabled by default when GOOS is given. This CL also adds importing C to the tests to confirm that Cgo works correctly. This CL also updates go.mod since this change requries the change in go/packages: golang.org/cl/214943 Updates golang/go#27234 Updates golang/go#36547 Change-Id: I66f9697f992f15b52fca7871e4e0ed64ca2b4965 Reviewed-on: https://go-review.googlesource.com/c/mobile/+/214498 Run-TryBot: Hajime Hoshi <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Hyang-Ah Hana Kim <[email protected]>
1 parent 1d4ecbb commit 23a0503

File tree

6 files changed

+15
-7
lines changed

6 files changed

+15
-7
lines changed

bind/testdata/cgopkg/cgopkg.go

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package cgopkg
22

3+
import "C"
4+
35
import (
46
_ "golang.org/x/mobile/gl"
57
)

cmd/gobind/gobind_test.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ func runGobind(t testing.TB, lang, pkg, goos string, exported *packagestest.Expo
9494
cmd.Dir = exported.Config.Dir
9595
cmd.Env = exported.Config.Env
9696
if goos != "" {
97-
cmd.Env = append(cmd.Env, "GOOS="+goos)
97+
// Add CGO_ENABLED=1 explicitly since Cgo is disabled when GOOS is different from host OS.
98+
cmd.Env = append(cmd.Env, "GOOS="+goos, "CGO_ENABLED=1")
9899
}
99100
if out, err := cmd.CombinedOutput(); err != nil {
100101
var cmd string

cmd/gobind/main.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ func run() {
5252
langs = []string{"go", "java", "objc"}
5353
}
5454

55+
// We need to give appropriate environment variables like CC or CXX so that the returned packages no longer have errors.
56+
// However, getting such environment variables is difficult or impossible so far.
57+
// Gomobile can obtain such environment variables in env.go, but this logic assumes some condiitons gobind doesn't assume.
5558
cfg := &packages.Config{
5659
Mode: packages.NeedName | packages.NeedFiles |
5760
packages.NeedImports | packages.NeedDeps |
@@ -143,10 +146,8 @@ func run() {
143146
typePkgs := make([]*types.Package, len(allPkg))
144147
astPkgs := make([][]*ast.File, len(allPkg))
145148
for i, pkg := range allPkg {
146-
if len(pkg.Errors) > 0 {
147-
errorf("%v", pkg.Errors)
148-
return
149-
}
149+
// Ignore pkg.Errors. pkg.Errors can exist when Cgo is used, but this should not affect the result.
150+
// See the discussion at golang/go#36547.
150151
typePkgs[i] = pkg.Types
151152
astPkgs[i] = pkg.Syntax
152153
}

cmd/gomobile/bind.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,8 @@ func writeFile(filename string, generate func(io.Writer) error) error {
214214

215215
func packagesConfig(targetOS string) *packages.Config {
216216
config := &packages.Config{}
217-
config.Env = append(os.Environ(), "GOARCH=arm", "GOOS="+targetOS)
217+
// Add CGO_ENABLED=1 explicitly since Cgo is disabled when GOOS is different from host OS.
218+
config.Env = append(os.Environ(), "GOARCH=arm", "GOOS="+targetOS, "CGO_ENABLED=1")
218219
tags := buildTags
219220
if targetOS == "darwin" {
220221
tags = append(tags, "ios")

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@ require (
66
golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56
77
golang.org/x/image v0.0.0-20190802002840-cff245a6509b
88
golang.org/x/mod v0.1.1-0.20191209134235-331c550502dd
9-
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e
9+
golang.org/x/tools v0.0.0-20200117012304-6edc0a871e69
1010
)

go.sum

+3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+o
1010
golang.org/x/mobile v0.0.0-20190312151609-d3739f865fa6/go.mod h1:z+o9i4GpDbdi3rU15maQ/Ox0txvL9dWGYEHz965HBQE=
1111
golang.org/x/mod v0.1.0 h1:sfUMP1Gu8qASkorDVjnMuvgJzwFbTZSeXFiGBYAVdl4=
1212
golang.org/x/mod v0.1.0/go.mod h1:0QHyrYULN0/3qlju5TqG8bIK38QM8yzMo5ekMj3DlcY=
13+
golang.org/x/mod v0.1.1-0.20191105210325-c90efee705ee/go.mod h1:QqPTAvyqsEbceGzBzNggFXnrqF1CaUcvgkdR5Ot7KZg=
1314
golang.org/x/mod v0.1.1-0.20191209134235-331c550502dd h1:ePuNC7PZ6O5BzgPn9bZayERXBdfZjUYoXEf5BTfDfh8=
1415
golang.org/x/mod v0.1.1-0.20191209134235-331c550502dd/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
1516
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
@@ -23,6 +24,8 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
2324
golang.org/x/tools v0.0.0-20190312151545-0bb0c0a6e846/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
2425
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e h1:aZzprAO9/8oim3qStq3wc1Xuxx4QmAGriC4VU4ojemQ=
2526
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
27+
golang.org/x/tools v0.0.0-20200117012304-6edc0a871e69 h1:yBHHx+XZqXJBm6Exke3N7V9gnlsyXxoCPEb1yVenjfk=
28+
golang.org/x/tools v0.0.0-20200117012304-6edc0a871e69/go.mod h1:TB2adYChydJhpapKDTa4BR/hXlZSLoq2Wpct/0txZ28=
2629
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
2730
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898 h1:/atklqdjdhuosWIl6AIbOeHJjicWYPqR9bpxqxYG2pA=
2831
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

0 commit comments

Comments
 (0)