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 <hajimehoshi@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
This commit is contained in:
Hajime Hoshi 2020-01-15 17:12:52 +09:00
Родитель 56347cccba
Коммит af4a92906a
6 изменённых файлов: 121 добавлений и 43 удалений

Просмотреть файл

@ -201,7 +201,7 @@ func TestBindWithGoModules(t *testing.T) {
t.Skipf("gomobile and gobind are not available on %s", runtime.GOOS)
}
dir, err := ioutil.TempDir("", "")
dir, err := ioutil.TempDir("", "gomobile-test")
if err != nil {
t.Fatal(err)
}
@ -243,27 +243,32 @@ func TestBindWithGoModules(t *testing.T) {
out = filepath.Join(dir, "Cgopkg.framework")
}
// Absolute path
tests := []struct {
Name string
Path string
Dir string
}{
{
cmd := exec.Command(filepath.Join(dir, "gomobile"), "bind", "-target="+target, "-o="+out, "golang.org/x/mobile/bind/testdata/cgopkg")
cmd.Env = append(os.Environ(), "PATH="+path, "GO111MODULE=on")
var b bytes.Buffer
cmd.Stderr = &b
if err := cmd.Run(); err != nil {
t.Errorf("%v: %s", err, string(b.Bytes()))
}
Name: "Absolute Path",
Path: "golang.org/x/mobile/bind/testdata/cgopkg",
},
{
Name: "Relative Path",
Path: "./bind/testdata/cgopkg",
Dir: filepath.Join("..", ".."),
},
}
// Relative path
{
cmd := exec.Command(filepath.Join(dir, "gomobile"), "bind", "-target="+target, "-o="+out, "./bind/testdata/cgopkg")
for _, tc := range tests {
tc := tc
t.Run(tc.Name, func(t *testing.T) {
cmd := exec.Command(filepath.Join(dir, "gomobile"), "bind", "-target="+target, "-o="+out, tc.Path)
cmd.Env = append(os.Environ(), "PATH="+path, "GO111MODULE=on")
cmd.Dir = filepath.Join("..", "..")
var b bytes.Buffer
cmd.Stderr = &b
if err := cmd.Run(); err != nil {
t.Errorf("%v: %s", err, string(b.Bytes()))
cmd.Dir = tc.Dir
if out, err := cmd.CombinedOutput(); err != nil {
t.Errorf("gomobile bind failed: %v\n%s", err, string(out))
}
})
}
})
}

Просмотреть файл

@ -12,7 +12,6 @@ import (
"io"
"os"
"os/exec"
"path"
"regexp"
"strings"
@ -92,7 +91,7 @@ func runBuildImpl(cmd *command) (*packages.Package, error) {
case 0:
buildPath = "."
case 1:
buildPath = path.Clean(args[0])
buildPath = args[0]
default:
cmd.usage()
os.Exit(1)
@ -118,10 +117,7 @@ func runBuildImpl(cmd *command) (*packages.Package, error) {
case "android":
if pkg.Name != "main" {
for _, arch := range targetArchs {
env := androidEnv[arch]
// gomobile-build does not support Go modules yet.
env = append(env, "GO111MODULE=off")
if err := goBuild(pkg.PkgPath, env); err != nil {
if err := goBuild(pkg.PkgPath, androidEnv[arch]); err != nil {
return nil, err
}
}
@ -137,10 +133,7 @@ func runBuildImpl(cmd *command) (*packages.Package, error) {
}
if pkg.Name != "main" {
for _, arch := range targetArchs {
env := darwinEnv[arch]
// gomobile-build does not support Go modules yet.
env = append(env, "GO111MODULE=off")
if err := goBuild(pkg.PkgPath, env); err != nil {
if err := goBuild(pkg.PkgPath, darwinEnv[arch]); err != nil {
return nil, err
}
}

Просмотреть файл

@ -69,9 +69,6 @@ func goAndroidBuild(pkg *packages.Package, androidArchs []string) (map[string]bo
nmpkgs := make(map[string]map[string]bool) // map: arch -> extractPkgs' output
for _, arch := range androidArchs {
env := androidEnv[arch]
// gomobile-build does not support Go modules yet.
env = append(env, "GO111MODULE=off")
toolchain := ndk.Toolchain(arch)
libPath := "lib/" + toolchain.abi + "/lib" + libName + ".so"
libAbsPath := filepath.Join(tmpdir, libPath)
@ -80,7 +77,7 @@ func goAndroidBuild(pkg *packages.Package, androidArchs []string) (map[string]bo
}
err = goBuild(
pkg.PkgPath,
env,
androidEnv[arch],
"-buildmode=c-shared",
"-o", libAbsPath,
)

Просмотреть файл

@ -94,10 +94,10 @@ mkdir -p $WORK/main
echo "{{template "infoplist" .Xinfo}}" > $WORK/main/Info.plist
mkdir -p $WORK/main/Images.xcassets/AppIcon.appiconset
echo "{{.Xcontents}}" > $WORK/main/Images.xcassets/AppIcon.appiconset/Contents.json{{end}}
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}}
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}}
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}}
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}}
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}}
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}}
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}}
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}}
xcrun lipo -o $WORK/main/main -create $WORK/arm $WORK/arm64 $WORK/386 $WORK/amd64
mkdir -p $WORK/main/assets
xcrun xcodebuild -configuration Release -project $WORK/main.xcodeproj -allowProvisioningUpdates DEVELOPMENT_TEAM={{.TeamID}}

Просмотреть файл

@ -73,10 +73,7 @@ func goIOSBuild(pkg *packages.Package, bundleID string, archs []string) (map[str
for _, arch := range archs {
path := filepath.Join(tmpdir, arch)
// Disable DWARF; see golang.org/issues/25148.
env := darwinEnv[arch]
// gomobile-build does not support Go modules yet.
env = append(env, "GO111MODULE=off")
if err := goBuild(src, env, "-ldflags=-w", "-o="+path); err != nil {
if err := goBuild(src, darwinEnv[arch], "-ldflags=-w", "-o="+path); err != nil {
return nil, err
}
if nmpkgs == nil {

Просмотреть файл

@ -6,7 +6,9 @@ package main
import (
"bytes"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
@ -109,7 +111,7 @@ func TestAndroidBuild(t *testing.T) {
var androidBuildTmpl = template.Must(template.New("output").Parse(`GOMOBILE={{.GOPATH}}/pkg/gomobile
WORK=$WORK
mkdir -p $WORK/lib/armeabi-v7a
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
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
`))
func TestParseBuildTargetFlag(t *testing.T) {
@ -183,3 +185,87 @@ func TestRegexImportGolangXPackage(t *testing.T) {
}
}
}
func TestBuildWithGoModules(t *testing.T) {
if runtime.GOOS == "android" {
t.Skipf("gomobile are not available on %s", runtime.GOOS)
}
dir, err := ioutil.TempDir("", "gomobile-test")
if err != nil {
t.Fatal(err)
}
defer os.RemoveAll(dir)
if out, err := exec.Command("go", "build", "-o="+dir, "golang.org/x/mobile/cmd/gomobile").CombinedOutput(); err != nil {
t.Fatalf("%v: %s", err, string(out))
}
path := dir
if p := os.Getenv("PATH"); p != "" {
path += string(filepath.ListSeparator) + p
}
for _, target := range []string{"android", "ios"} {
t.Run(target, func(t *testing.T) {
if target == "ios" {
t.Skip("gomobile-build doesn't work for iOS. see https://golang.org/issue/32963")
}
switch target {
case "android":
androidHome := os.Getenv("ANDROID_HOME")
if androidHome == "" {
t.Skip("ANDROID_HOME not found, skipping bind")
}
if _, err := androidAPIPath(); err != nil {
t.Skip("No android API platform found in $ANDROID_HOME, skipping bind")
}
case "ios":
if !xcodeAvailable() {
t.Skip("Xcode is missing")
}
}
var out string
switch target {
case "android":
out = filepath.Join(dir, "basic.apk")
case "ios":
out = filepath.Join(dir, "Basic.app")
}
tests := []struct {
Name string
Path string
Dir string
}{
{
Name: "Absolute Path",
Path: "golang.org/x/mobile/example/basic",
},
{
Name: "Relative Path",
Path: "./example/basic",
Dir: filepath.Join("..", ".."),
},
}
for _, tc := range tests {
tc := tc
t.Run(tc.Name, func(t *testing.T) {
args := []string{"build", "-target=" + target, "-o=" + out}
if target == "ios" {
args = append(args, "-bundleid=org.golang.gomobiletest")
}
args = append(args, tc.Path)
cmd := exec.Command(filepath.Join(dir, "gomobile"), args...)
cmd.Env = append(os.Environ(), "PATH="+path, "GO111MODULE=on")
cmd.Dir = tc.Dir
if out, err := cmd.CombinedOutput(); err != nil {
t.Errorf("gomobile build failed: %v\n%s", err, string(out))
}
})
}
})
}
}