Skip to content

Commit cafc553

Browse files
committed
cmd/gobind: fix build-tag, CGO and load trybot failures
* Correctly format build tags to pass into go/packages * Removes CGO_ENABLED=0 from a packages.Load configuration * Calls go/packages.Load twice to work around a build cache * staleness issue These bugs were introduced by CL 189597. Updates golang/go#27234. Updates golang/go#33687. Change-Id: I3ae6737bf53bbecda0c7e25885b9c6aea5779332 Reviewed-on: https://go-review.googlesource.com/c/mobile/+/190479 Reviewed-by: Hyang-Ah Hana Kim <[email protected]> Run-TryBot: Emmanuel Odeke <[email protected]>
1 parent 30c70e3 commit cafc553

File tree

6 files changed

+58
-23
lines changed

6 files changed

+58
-23
lines changed

bind/java/seq_test.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,11 @@ func testMain(m *testing.M) int {
4646
if out, err := exec.Command(gocmd, "build", "-o", gobindBin, "golang.org/x/mobile/cmd/gobind").CombinedOutput(); err != nil {
4747
log.Fatalf("gobind build failed: %v: %s", err, out)
4848
}
49-
PATH := os.Getenv("PATH")
50-
if PATH != "" {
51-
PATH += string(filepath.ListSeparator)
49+
path := binDir
50+
if oldPath := os.Getenv("PATH"); oldPath != "" {
51+
path += string(filepath.ListSeparator) + oldPath
5252
}
53-
PATH += binDir
54-
os.Setenv("PATH", PATH)
53+
os.Setenv("PATH", path)
5554
}
5655
return m.Run()
5756
}

bind/objc/seq_test.go

+43-7
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ import (
99
"fmt"
1010
"io"
1111
"io/ioutil"
12+
"log"
1213
"os"
1314
"os/exec"
1415
"path/filepath"
16+
"runtime"
1517
"strings"
1618
"testing"
1719
)
@@ -34,6 +36,43 @@ import (
3436

3537
var destination = flag.String("device", "platform=iOS Simulator,name=iPhone 6s Plus", "Specify the -destination flag to xcodebuild")
3638

39+
var gomobileBin string
40+
41+
func TestMain(m *testing.M) {
42+
os.Exit(testMain(m))
43+
}
44+
45+
func testMain(m *testing.M) int {
46+
binDir, err := ioutil.TempDir("", "bind-objc-test-")
47+
if err != nil {
48+
log.Fatal(err)
49+
}
50+
defer os.RemoveAll(binDir)
51+
52+
exe := ""
53+
if runtime.GOOS == "windows" {
54+
exe = ".exe"
55+
}
56+
if runtime.GOOS != "android" {
57+
gocmd := filepath.Join(runtime.GOROOT(), "bin", "go")
58+
gomobileBin = filepath.Join(binDir, "gomobile"+exe)
59+
gobindBin := filepath.Join(binDir, "gobind"+exe)
60+
if out, err := exec.Command(gocmd, "build", "-o", gomobileBin, "golang.org/x/mobile/cmd/gomobile").CombinedOutput(); err != nil {
61+
log.Fatalf("gomobile build failed: %v: %s", err, out)
62+
}
63+
if out, err := exec.Command(gocmd, "build", "-o", gobindBin, "golang.org/x/mobile/cmd/gobind").CombinedOutput(); err != nil {
64+
log.Fatalf("gobind build failed: %v: %s", err, out)
65+
}
66+
path := binDir
67+
if oldPath := os.Getenv("PATH"); oldPath != "" {
68+
path += string(filepath.ListSeparator) + oldPath
69+
}
70+
os.Setenv("PATH", path)
71+
}
72+
73+
return m.Run()
74+
}
75+
3776
// TestObjcSeqTest runs ObjC test SeqTest.m.
3877
func TestObjcSeqTest(t *testing.T) {
3978
runTest(t, []string{
@@ -62,15 +101,12 @@ func TestObjcCustomPkg(t *testing.T) {
62101
}
63102

64103
func runTest(t *testing.T, pkgNames []string, prefix, testfile, framework string, uitest, dumpOutput bool) {
104+
if gomobileBin == "" {
105+
t.Skipf("no gomobile on %s", runtime.GOOS)
106+
}
65107
if _, err := run("which xcodebuild"); err != nil {
66108
t.Skip("command xcodebuild not found, skipping")
67109
}
68-
if _, err := run("which gomobile"); err != nil {
69-
t.Log("go install gomobile")
70-
if _, err := run("go install golang.org/x/mobile/cmd/gomobile"); err != nil {
71-
t.Fatalf("gomobile install failed: %v", err)
72-
}
73-
}
74110

75111
tmpdir, err := ioutil.TempDir("", "bind-objc-seq-test-")
76112
if err != nil {
@@ -87,7 +123,7 @@ func runTest(t *testing.T, pkgNames []string, prefix, testfile, framework string
87123
t.Fatalf("failed to copy %s: %v", testfile, err)
88124
}
89125

90-
cmd := exec.Command("gomobile", "bind", "-target", "ios", "-tags", "aaa bbb")
126+
cmd := exec.Command(gomobileBin, "bind", "-target", "ios", "-tags", "aaa bbb")
91127
if prefix != "" {
92128
cmd.Args = append(cmd.Args, "-prefix", prefix)
93129
}

cmd/gobind/main.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -60,16 +60,16 @@ func run() {
6060
}
6161

6262
cfg := &packages.Config{
63-
Mode: packages.NeedName | packages.NeedFiles | packages.NeedCompiledGoFiles |
63+
Mode: packages.NeedName | packages.NeedFiles |
6464
packages.NeedImports | packages.NeedDeps |
6565
packages.NeedTypes | packages.NeedSyntax | packages.NeedTypesInfo,
66-
BuildFlags: []string{"-tags", *tags},
67-
68-
// packages.Load invokes `go list` command with `GOOS=android`, but in most cases
69-
// go-list cannot find the header files for Android. Suppress this error by
70-
// disabling Cgo.
71-
Env: append(os.Environ(), "CGO_ENABLED=0"),
66+
BuildFlags: []string{"-tags", strings.Join(strings.Split(*tags, ","), " ")},
7267
}
68+
69+
// Call Load twice to warm the cache. There is a known issue that the result of Load
70+
// depends on build cache state. See golang/go#33687.
71+
packages.Load(cfg, flag.Args()...)
72+
7373
allPkg, err := packages.Load(cfg, flag.Args()...)
7474
if err != nil {
7575
log.Fatal(err)

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ go 1.11
55
require (
66
golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56
77
golang.org/x/image v0.0.0-20190802002840-cff245a6509b
8-
golang.org/x/tools v0.0.0-20190808195139-e713427fea3f
8+
golang.org/x/tools v0.0.0-20190816200558-6889da9d5479
99
)

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5
1717
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
1818
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
1919
golang.org/x/tools v0.0.0-20190312151545-0bb0c0a6e846/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
20-
golang.org/x/tools v0.0.0-20190808195139-e713427fea3f h1:lSQQYboXWc71s9tnZRRBiMcc9Uc1BPWj3Bzvdk8UQ0Y=
21-
golang.org/x/tools v0.0.0-20190808195139-e713427fea3f/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
20+
golang.org/x/tools v0.0.0-20190816200558-6889da9d5479 h1:lfN2PY/jymfnxkNHlbBF5DwPsUvhqUnrdgfK01iH2s0=
21+
golang.org/x/tools v0.0.0-20190816200558-6889da9d5479/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
2222
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=

internal/importers/ast.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ func AnalyzePackages(pkgs []*packages.Package, pkgPrefix string) (*References, e
106106
fset := token.NewFileSet()
107107
for _, pkg := range pkgs {
108108
files := make(map[string]*ast.File)
109-
for i, name := range pkg.CompiledGoFiles {
109+
for i, name := range pkg.GoFiles {
110110
files[name] = pkg.Syntax[i]
111111
}
112112
// Ignore errors (from unknown packages)

0 commit comments

Comments
 (0)