2
0
mirror of synced 2025-02-23 06:48:15 +00:00

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 <hyangah@gmail.com>
Run-TryBot: Emmanuel Odeke <emm.odeke@gmail.com>
This commit is contained in:
Hajime Hoshi 2019-08-16 13:55:12 +09:00
parent 30c70e3810
commit cafc553e1a
6 changed files with 58 additions and 23 deletions

View File

@ -46,12 +46,11 @@ func testMain(m *testing.M) int {
if out, err := exec.Command(gocmd, "build", "-o", gobindBin, "golang.org/x/mobile/cmd/gobind").CombinedOutput(); err != nil { if out, err := exec.Command(gocmd, "build", "-o", gobindBin, "golang.org/x/mobile/cmd/gobind").CombinedOutput(); err != nil {
log.Fatalf("gobind build failed: %v: %s", err, out) log.Fatalf("gobind build failed: %v: %s", err, out)
} }
PATH := os.Getenv("PATH") path := binDir
if PATH != "" { if oldPath := os.Getenv("PATH"); oldPath != "" {
PATH += string(filepath.ListSeparator) path += string(filepath.ListSeparator) + oldPath
} }
PATH += binDir os.Setenv("PATH", path)
os.Setenv("PATH", PATH)
} }
return m.Run() return m.Run()
} }

View File

@ -9,9 +9,11 @@ import (
"fmt" "fmt"
"io" "io"
"io/ioutil" "io/ioutil"
"log"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
"runtime"
"strings" "strings"
"testing" "testing"
) )
@ -34,6 +36,43 @@ import (
var destination = flag.String("device", "platform=iOS Simulator,name=iPhone 6s Plus", "Specify the -destination flag to xcodebuild") var destination = flag.String("device", "platform=iOS Simulator,name=iPhone 6s Plus", "Specify the -destination flag to xcodebuild")
var gomobileBin string
func TestMain(m *testing.M) {
os.Exit(testMain(m))
}
func testMain(m *testing.M) int {
binDir, err := ioutil.TempDir("", "bind-objc-test-")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(binDir)
exe := ""
if runtime.GOOS == "windows" {
exe = ".exe"
}
if runtime.GOOS != "android" {
gocmd := filepath.Join(runtime.GOROOT(), "bin", "go")
gomobileBin = filepath.Join(binDir, "gomobile"+exe)
gobindBin := filepath.Join(binDir, "gobind"+exe)
if out, err := exec.Command(gocmd, "build", "-o", gomobileBin, "golang.org/x/mobile/cmd/gomobile").CombinedOutput(); err != nil {
log.Fatalf("gomobile build failed: %v: %s", err, out)
}
if out, err := exec.Command(gocmd, "build", "-o", gobindBin, "golang.org/x/mobile/cmd/gobind").CombinedOutput(); err != nil {
log.Fatalf("gobind build failed: %v: %s", err, out)
}
path := binDir
if oldPath := os.Getenv("PATH"); oldPath != "" {
path += string(filepath.ListSeparator) + oldPath
}
os.Setenv("PATH", path)
}
return m.Run()
}
// TestObjcSeqTest runs ObjC test SeqTest.m. // TestObjcSeqTest runs ObjC test SeqTest.m.
func TestObjcSeqTest(t *testing.T) { func TestObjcSeqTest(t *testing.T) {
runTest(t, []string{ runTest(t, []string{
@ -62,15 +101,12 @@ func TestObjcCustomPkg(t *testing.T) {
} }
func runTest(t *testing.T, pkgNames []string, prefix, testfile, framework string, uitest, dumpOutput bool) { func runTest(t *testing.T, pkgNames []string, prefix, testfile, framework string, uitest, dumpOutput bool) {
if gomobileBin == "" {
t.Skipf("no gomobile on %s", runtime.GOOS)
}
if _, err := run("which xcodebuild"); err != nil { if _, err := run("which xcodebuild"); err != nil {
t.Skip("command xcodebuild not found, skipping") t.Skip("command xcodebuild not found, skipping")
} }
if _, err := run("which gomobile"); err != nil {
t.Log("go install gomobile")
if _, err := run("go install golang.org/x/mobile/cmd/gomobile"); err != nil {
t.Fatalf("gomobile install failed: %v", err)
}
}
tmpdir, err := ioutil.TempDir("", "bind-objc-seq-test-") tmpdir, err := ioutil.TempDir("", "bind-objc-seq-test-")
if err != nil { if err != nil {
@ -87,7 +123,7 @@ func runTest(t *testing.T, pkgNames []string, prefix, testfile, framework string
t.Fatalf("failed to copy %s: %v", testfile, err) t.Fatalf("failed to copy %s: %v", testfile, err)
} }
cmd := exec.Command("gomobile", "bind", "-target", "ios", "-tags", "aaa bbb") cmd := exec.Command(gomobileBin, "bind", "-target", "ios", "-tags", "aaa bbb")
if prefix != "" { if prefix != "" {
cmd.Args = append(cmd.Args, "-prefix", prefix) cmd.Args = append(cmd.Args, "-prefix", prefix)
} }

View File

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

2
go.mod
View File

@ -5,5 +5,5 @@ go 1.11
require ( require (
golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56 golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56
golang.org/x/image v0.0.0-20190802002840-cff245a6509b golang.org/x/image v0.0.0-20190802002840-cff245a6509b
golang.org/x/tools v0.0.0-20190808195139-e713427fea3f golang.org/x/tools v0.0.0-20190816200558-6889da9d5479
) )

4
go.sum
View File

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

View File

@ -106,7 +106,7 @@ func AnalyzePackages(pkgs []*packages.Package, pkgPrefix string) (*References, e
fset := token.NewFileSet() fset := token.NewFileSet()
for _, pkg := range pkgs { for _, pkg := range pkgs {
files := make(map[string]*ast.File) files := make(map[string]*ast.File)
for i, name := range pkg.CompiledGoFiles { for i, name := range pkg.GoFiles {
files[name] = pkg.Syntax[i] files[name] = pkg.Syntax[i]
} }
// Ignore errors (from unknown packages) // Ignore errors (from unknown packages)