Skip to content

Commit 5516773

Browse files
committed
cmd/gomobile: enable Go modules at gomobile-build
This CL enables Go modules at gomobile-build command. This CL is a counterpart for the change gomobile-bind: https://golang.org/cl/210380 This CL also reformats the tests in bind_test.go Updates golang/go#27234 Change-Id: I9fb9612be6b08f5f61259879f563c8586fb1efef Reviewed-on: https://go-review.googlesource.com/c/mobile/+/214897 Run-TryBot: Hajime Hoshi <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Hyang-Ah Hana Kim <[email protected]> Reviewed-by: Daniel Martí <[email protected]>
1 parent 69413c2 commit 5516773

File tree

6 files changed

+121
-43
lines changed

6 files changed

+121
-43
lines changed

cmd/gomobile/bind_test.go

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ func TestBindWithGoModules(t *testing.T) {
201201
t.Skipf("gomobile and gobind are not available on %s", runtime.GOOS)
202202
}
203203

204-
dir, err := ioutil.TempDir("", "")
204+
dir, err := ioutil.TempDir("", "gomobile-test")
205205
if err != nil {
206206
t.Fatal(err)
207207
}
@@ -243,27 +243,32 @@ func TestBindWithGoModules(t *testing.T) {
243243
out = filepath.Join(dir, "Cgopkg.framework")
244244
}
245245

246-
// Absolute path
247-
{
248-
cmd := exec.Command(filepath.Join(dir, "gomobile"), "bind", "-target="+target, "-o="+out, "golang.org/x/mobile/bind/testdata/cgopkg")
249-
cmd.Env = append(os.Environ(), "PATH="+path, "GO111MODULE=on")
250-
var b bytes.Buffer
251-
cmd.Stderr = &b
252-
if err := cmd.Run(); err != nil {
253-
t.Errorf("%v: %s", err, string(b.Bytes()))
254-
}
246+
tests := []struct {
247+
Name string
248+
Path string
249+
Dir string
250+
}{
251+
{
252+
Name: "Absolute Path",
253+
Path: "golang.org/x/mobile/bind/testdata/cgopkg",
254+
},
255+
{
256+
Name: "Relative Path",
257+
Path: "./bind/testdata/cgopkg",
258+
Dir: filepath.Join("..", ".."),
259+
},
255260
}
256261

257-
// Relative path
258-
{
259-
cmd := exec.Command(filepath.Join(dir, "gomobile"), "bind", "-target="+target, "-o="+out, "./bind/testdata/cgopkg")
260-
cmd.Env = append(os.Environ(), "PATH="+path, "GO111MODULE=on")
261-
cmd.Dir = filepath.Join("..", "..")
262-
var b bytes.Buffer
263-
cmd.Stderr = &b
264-
if err := cmd.Run(); err != nil {
265-
t.Errorf("%v: %s", err, string(b.Bytes()))
266-
}
262+
for _, tc := range tests {
263+
tc := tc
264+
t.Run(tc.Name, func(t *testing.T) {
265+
cmd := exec.Command(filepath.Join(dir, "gomobile"), "bind", "-target="+target, "-o="+out, tc.Path)
266+
cmd.Env = append(os.Environ(), "PATH="+path, "GO111MODULE=on")
267+
cmd.Dir = tc.Dir
268+
if out, err := cmd.CombinedOutput(); err != nil {
269+
t.Errorf("gomobile bind failed: %v\n%s", err, string(out))
270+
}
271+
})
267272
}
268273
})
269274
}

cmd/gomobile/build.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"io"
1313
"os"
1414
"os/exec"
15-
"path"
1615
"regexp"
1716
"strings"
1817

@@ -92,7 +91,7 @@ func runBuildImpl(cmd *command) (*packages.Package, error) {
9291
case 0:
9392
buildPath = "."
9493
case 1:
95-
buildPath = path.Clean(args[0])
94+
buildPath = args[0]
9695
default:
9796
cmd.usage()
9897
os.Exit(1)
@@ -118,10 +117,7 @@ func runBuildImpl(cmd *command) (*packages.Package, error) {
118117
case "android":
119118
if pkg.Name != "main" {
120119
for _, arch := range targetArchs {
121-
env := androidEnv[arch]
122-
// gomobile-build does not support Go modules yet.
123-
env = append(env, "GO111MODULE=off")
124-
if err := goBuild(pkg.PkgPath, env); err != nil {
120+
if err := goBuild(pkg.PkgPath, androidEnv[arch]); err != nil {
125121
return nil, err
126122
}
127123
}
@@ -137,10 +133,7 @@ func runBuildImpl(cmd *command) (*packages.Package, error) {
137133
}
138134
if pkg.Name != "main" {
139135
for _, arch := range targetArchs {
140-
env := darwinEnv[arch]
141-
// gomobile-build does not support Go modules yet.
142-
env = append(env, "GO111MODULE=off")
143-
if err := goBuild(pkg.PkgPath, env); err != nil {
136+
if err := goBuild(pkg.PkgPath, darwinEnv[arch]); err != nil {
144137
return nil, err
145138
}
146139
}

cmd/gomobile/build_androidapp.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,6 @@ func goAndroidBuild(pkg *packages.Package, androidArchs []string) (map[string]bo
6969
nmpkgs := make(map[string]map[string]bool) // map: arch -> extractPkgs' output
7070

7171
for _, arch := range androidArchs {
72-
env := androidEnv[arch]
73-
// gomobile-build does not support Go modules yet.
74-
env = append(env, "GO111MODULE=off")
7572
toolchain := ndk.Toolchain(arch)
7673
libPath := "lib/" + toolchain.abi + "/lib" + libName + ".so"
7774
libAbsPath := filepath.Join(tmpdir, libPath)
@@ -80,7 +77,7 @@ func goAndroidBuild(pkg *packages.Package, androidArchs []string) (map[string]bo
8077
}
8178
err = goBuild(
8279
pkg.PkgPath,
83-
env,
80+
androidEnv[arch],
8481
"-buildmode=c-shared",
8582
"-o", libAbsPath,
8683
)

cmd/gomobile/build_darwin_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,10 +94,10 @@ mkdir -p $WORK/main
9494
echo "{{template "infoplist" .Xinfo}}" > $WORK/main/Info.plist
9595
mkdir -p $WORK/main/Images.xcassets/AppIcon.appiconset
9696
echo "{{.Xcontents}}" > $WORK/main/Images.xcassets/AppIcon.appiconset/Contents.json{{end}}
97-
GOARM=7 GOOS=darwin GOARCH=arm CC=iphoneos-clang CXX=iphoneos-clang++ CGO_CFLAGS=-isysroot=iphoneos -miphoneos-version-min=7.0 -fembed-bitcode -arch armv7 CGO_CXXFLAGS=-isysroot=iphoneos -miphoneos-version-min=7.0 -fembed-bitcode -arch armv7 CGO_LDFLAGS=-isysroot=iphoneos -miphoneos-version-min=7.0 -fembed-bitcode -arch armv7 CGO_ENABLED=1 GO111MODULE=off go build -tags tag1 ios -x {{if .Main}}-ldflags=-w -o=$WORK/arm {{end}}{{.Pkg}}
98-
GOOS=darwin GOARCH=arm64 CC=iphoneos-clang CXX=iphoneos-clang++ CGO_CFLAGS=-isysroot=iphoneos -miphoneos-version-min=7.0 -fembed-bitcode -arch arm64 CGO_CXXFLAGS=-isysroot=iphoneos -miphoneos-version-min=7.0 -fembed-bitcode -arch arm64 CGO_LDFLAGS=-isysroot=iphoneos -miphoneos-version-min=7.0 -fembed-bitcode -arch arm64 CGO_ENABLED=1 GO111MODULE=off go build -tags tag1 ios -x {{if .Main}}-ldflags=-w -o=$WORK/arm64 {{end}}{{.Pkg}}
99-
GOOS=darwin GOARCH=386 CC=iphonesimulator-clang CXX=iphonesimulator-clang++ CGO_CFLAGS=-isysroot=iphonesimulator -mios-simulator-version-min=7.0 -fembed-bitcode -arch i386 CGO_CXXFLAGS=-isysroot=iphonesimulator -mios-simulator-version-min=7.0 -fembed-bitcode -arch i386 CGO_LDFLAGS=-isysroot=iphonesimulator -mios-simulator-version-min=7.0 -fembed-bitcode -arch i386 CGO_ENABLED=1 GO111MODULE=off go build -tags tag1 ios -x {{if .Main}}-ldflags=-w -o=$WORK/386 {{end}}{{.Pkg}}
100-
GOOS=darwin GOARCH=amd64 CC=iphonesimulator-clang CXX=iphonesimulator-clang++ CGO_CFLAGS=-isysroot=iphonesimulator -mios-simulator-version-min=7.0 -fembed-bitcode -arch x86_64 CGO_CXXFLAGS=-isysroot=iphonesimulator -mios-simulator-version-min=7.0 -fembed-bitcode -arch x86_64 CGO_LDFLAGS=-isysroot=iphonesimulator -mios-simulator-version-min=7.0 -fembed-bitcode -arch x86_64 CGO_ENABLED=1 GO111MODULE=off go build -tags tag1 ios -x {{if .Main}}-ldflags=-w -o=$WORK/amd64 {{end}}{{.Pkg}}{{if .Main}}
97+
GOARM=7 GOOS=darwin GOARCH=arm CC=iphoneos-clang CXX=iphoneos-clang++ CGO_CFLAGS=-isysroot=iphoneos -miphoneos-version-min=7.0 -fembed-bitcode -arch armv7 CGO_CXXFLAGS=-isysroot=iphoneos -miphoneos-version-min=7.0 -fembed-bitcode -arch armv7 CGO_LDFLAGS=-isysroot=iphoneos -miphoneos-version-min=7.0 -fembed-bitcode -arch armv7 CGO_ENABLED=1 go build -tags tag1 ios -x {{if .Main}}-ldflags=-w -o=$WORK/arm {{end}}{{.Pkg}}
98+
GOOS=darwin GOARCH=arm64 CC=iphoneos-clang CXX=iphoneos-clang++ CGO_CFLAGS=-isysroot=iphoneos -miphoneos-version-min=7.0 -fembed-bitcode -arch arm64 CGO_CXXFLAGS=-isysroot=iphoneos -miphoneos-version-min=7.0 -fembed-bitcode -arch arm64 CGO_LDFLAGS=-isysroot=iphoneos -miphoneos-version-min=7.0 -fembed-bitcode -arch arm64 CGO_ENABLED=1 go build -tags tag1 ios -x {{if .Main}}-ldflags=-w -o=$WORK/arm64 {{end}}{{.Pkg}}
99+
GOOS=darwin GOARCH=386 CC=iphonesimulator-clang CXX=iphonesimulator-clang++ CGO_CFLAGS=-isysroot=iphonesimulator -mios-simulator-version-min=7.0 -fembed-bitcode -arch i386 CGO_CXXFLAGS=-isysroot=iphonesimulator -mios-simulator-version-min=7.0 -fembed-bitcode -arch i386 CGO_LDFLAGS=-isysroot=iphonesimulator -mios-simulator-version-min=7.0 -fembed-bitcode -arch i386 CGO_ENABLED=1 go build -tags tag1 ios -x {{if .Main}}-ldflags=-w -o=$WORK/386 {{end}}{{.Pkg}}
100+
GOOS=darwin GOARCH=amd64 CC=iphonesimulator-clang CXX=iphonesimulator-clang++ CGO_CFLAGS=-isysroot=iphonesimulator -mios-simulator-version-min=7.0 -fembed-bitcode -arch x86_64 CGO_CXXFLAGS=-isysroot=iphonesimulator -mios-simulator-version-min=7.0 -fembed-bitcode -arch x86_64 CGO_LDFLAGS=-isysroot=iphonesimulator -mios-simulator-version-min=7.0 -fembed-bitcode -arch x86_64 CGO_ENABLED=1 go build -tags tag1 ios -x {{if .Main}}-ldflags=-w -o=$WORK/amd64 {{end}}{{.Pkg}}{{if .Main}}
101101
xcrun lipo -o $WORK/main/main -create $WORK/arm $WORK/arm64 $WORK/386 $WORK/amd64
102102
mkdir -p $WORK/main/assets
103103
xcrun xcodebuild -configuration Release -project $WORK/main.xcodeproj -allowProvisioningUpdates DEVELOPMENT_TEAM={{.TeamID}}

cmd/gomobile/build_iosapp.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,7 @@ func goIOSBuild(pkg *packages.Package, bundleID string, archs []string) (map[str
7373
for _, arch := range archs {
7474
path := filepath.Join(tmpdir, arch)
7575
// Disable DWARF; see golang.org/issues/25148.
76-
env := darwinEnv[arch]
77-
// gomobile-build does not support Go modules yet.
78-
env = append(env, "GO111MODULE=off")
79-
if err := goBuild(src, env, "-ldflags=-w", "-o="+path); err != nil {
76+
if err := goBuild(src, darwinEnv[arch], "-ldflags=-w", "-o="+path); err != nil {
8077
return nil, err
8178
}
8279
if nmpkgs == nil {

cmd/gomobile/build_test.go

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ package main
66

77
import (
88
"bytes"
9+
"io/ioutil"
910
"os"
11+
"os/exec"
1012
"path/filepath"
1113
"runtime"
1214
"strings"
@@ -109,7 +111,7 @@ func TestAndroidBuild(t *testing.T) {
109111
var androidBuildTmpl = template.Must(template.New("output").Parse(`GOMOBILE={{.GOPATH}}/pkg/gomobile
110112
WORK=$WORK
111113
mkdir -p $WORK/lib/armeabi-v7a
112-
GOOS=android GOARCH=arm CC=$NDK_PATH/toolchains/llvm/prebuilt/{{.NDKARCH}}/bin/armv7a-linux-androideabi16-clang CXX=$NDK_PATH/toolchains/llvm/prebuilt/{{.NDKARCH}}/bin/armv7a-linux-androideabi16-clang++ CGO_ENABLED=1 GOARM=7 GO111MODULE=off go build -tags tag1 -x -buildmode=c-shared -o $WORK/lib/armeabi-v7a/libbasic.so golang.org/x/mobile/example/basic
114+
GOOS=android GOARCH=arm CC=$NDK_PATH/toolchains/llvm/prebuilt/{{.NDKARCH}}/bin/armv7a-linux-androideabi16-clang CXX=$NDK_PATH/toolchains/llvm/prebuilt/{{.NDKARCH}}/bin/armv7a-linux-androideabi16-clang++ CGO_ENABLED=1 GOARM=7 go build -tags tag1 -x -buildmode=c-shared -o $WORK/lib/armeabi-v7a/libbasic.so golang.org/x/mobile/example/basic
113115
`))
114116

115117
func TestParseBuildTargetFlag(t *testing.T) {
@@ -183,3 +185,87 @@ func TestRegexImportGolangXPackage(t *testing.T) {
183185
}
184186
}
185187
}
188+
189+
func TestBuildWithGoModules(t *testing.T) {
190+
if runtime.GOOS == "android" {
191+
t.Skipf("gomobile are not available on %s", runtime.GOOS)
192+
}
193+
194+
dir, err := ioutil.TempDir("", "gomobile-test")
195+
if err != nil {
196+
t.Fatal(err)
197+
}
198+
defer os.RemoveAll(dir)
199+
200+
if out, err := exec.Command("go", "build", "-o="+dir, "golang.org/x/mobile/cmd/gomobile").CombinedOutput(); err != nil {
201+
t.Fatalf("%v: %s", err, string(out))
202+
}
203+
path := dir
204+
if p := os.Getenv("PATH"); p != "" {
205+
path += string(filepath.ListSeparator) + p
206+
}
207+
208+
for _, target := range []string{"android", "ios"} {
209+
t.Run(target, func(t *testing.T) {
210+
if target == "ios" {
211+
t.Skip("gomobile-build doesn't work for iOS. see https://golang.org/issue/32963")
212+
}
213+
214+
switch target {
215+
case "android":
216+
androidHome := os.Getenv("ANDROID_HOME")
217+
if androidHome == "" {
218+
t.Skip("ANDROID_HOME not found, skipping bind")
219+
}
220+
if _, err := androidAPIPath(); err != nil {
221+
t.Skip("No android API platform found in $ANDROID_HOME, skipping bind")
222+
}
223+
case "ios":
224+
if !xcodeAvailable() {
225+
t.Skip("Xcode is missing")
226+
}
227+
}
228+
229+
var out string
230+
switch target {
231+
case "android":
232+
out = filepath.Join(dir, "basic.apk")
233+
case "ios":
234+
out = filepath.Join(dir, "Basic.app")
235+
}
236+
237+
tests := []struct {
238+
Name string
239+
Path string
240+
Dir string
241+
}{
242+
{
243+
Name: "Absolute Path",
244+
Path: "golang.org/x/mobile/example/basic",
245+
},
246+
{
247+
Name: "Relative Path",
248+
Path: "./example/basic",
249+
Dir: filepath.Join("..", ".."),
250+
},
251+
}
252+
253+
for _, tc := range tests {
254+
tc := tc
255+
t.Run(tc.Name, func(t *testing.T) {
256+
args := []string{"build", "-target=" + target, "-o=" + out}
257+
if target == "ios" {
258+
args = append(args, "-bundleid=org.golang.gomobiletest")
259+
}
260+
args = append(args, tc.Path)
261+
cmd := exec.Command(filepath.Join(dir, "gomobile"), args...)
262+
cmd.Env = append(os.Environ(), "PATH="+path, "GO111MODULE=on")
263+
cmd.Dir = tc.Dir
264+
if out, err := cmd.CombinedOutput(); err != nil {
265+
t.Errorf("gomobile build failed: %v\n%s", err, string(out))
266+
}
267+
})
268+
}
269+
})
270+
}
271+
}

0 commit comments

Comments
 (0)