Skip to content

Commit

Permalink
cmd/gomobile: disable bitcode on Go 1.13 or older
Browse files Browse the repository at this point in the history
iOS's bitcode conflicts with headerpad on Go 1.13 or older. This
problem is fixed on Go 1.14.

This CL disables bitcode only on Go 1.13 or older.

Fixes golang/go#32963

Change-Id: Iac9edd56aaf7819288e8f46b9e29d310305acd9d
Reviewed-on: https://go-review.googlesource.com/c/mobile/+/214899
Run-TryBot: Hajime Hoshi <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
Reviewed-by: Hyang-Ah Hana Kim <[email protected]>
  • Loading branch information
hajimehoshi committed Jan 15, 2020
1 parent 3a67c91 commit 159caee
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 27 deletions.
21 changes: 16 additions & 5 deletions cmd/gomobile/bind_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,16 @@ func TestBindAndroid(t *testing.T) {
}
got := filepath.ToSlash(buf.String())

output, err := defaultOutputData()
if err != nil {
t.Fatal(err)
}
data := struct {
outputData
AndroidPlatform string
JavaPkg string
}{
outputData: defaultOutputData(),
outputData: output,
AndroidPlatform: platform,
JavaPkg: tc.javaPkg,
}
Expand Down Expand Up @@ -137,12 +141,19 @@ func TestBindIOS(t *testing.T) {
}
got := filepath.ToSlash(buf.String())

output, err := defaultOutputData()
if err != nil {
t.Fatal(err)
}

data := struct {
outputData
Prefix string
Prefix string
BitcodeEnabled bool
}{
outputData: defaultOutputData(),
Prefix: tc.prefix,
outputData: output,
Prefix: tc.prefix,
BitcodeEnabled: bitcodeEnabled,
}

wantBuf := new(bytes.Buffer)
Expand Down Expand Up @@ -175,7 +186,7 @@ var bindIOSTmpl = template.Must(template.New("output").Parse(`GOMOBILE={{.GOPATH
WORK=$WORK
GOOS=darwin CGO_ENABLED=1 gobind -lang=go,objc -outdir=$WORK -tags=ios{{if .Prefix}} -prefix={{.Prefix}}{{end}} golang.org/x/mobile/asset
mkdir -p $WORK/src
PWD=$WORK/src 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 GOPATH=$WORK:$GOPATH go build -tags ios -x -buildmode=c-archive -o $WORK/asset-arm.a ./gobind
PWD=$WORK/src GOARM=7 GOOS=darwin GOARCH=arm CC=iphoneos-clang CXX=iphoneos-clang++ CGO_CFLAGS=-isysroot=iphoneos -miphoneos-version-min=7.0 {{if .BitcodeEnabled}}-fembed-bitcode {{end}}-arch armv7 CGO_CXXFLAGS=-isysroot=iphoneos -miphoneos-version-min=7.0 {{if .BitcodeEnabled}}-fembed-bitcode {{end}}-arch armv7 CGO_LDFLAGS=-isysroot=iphoneos -miphoneos-version-min=7.0 {{if .BitcodeEnabled}}-fembed-bitcode {{end}}-arch armv7 CGO_ENABLED=1 GOPATH=$WORK:$GOPATH go build -tags ios -x -buildmode=c-archive -o $WORK/asset-arm.a ./gobind
rm -r -f "Asset.framework"
mkdir -p Asset.framework/Versions/A/Headers
ln -s A Asset.framework/Versions/Current
Expand Down
29 changes: 18 additions & 11 deletions cmd/gomobile/build_darwin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,16 +55,23 @@ func TestIOSBuild(t *testing.T) {
t.Fatalf("detecting team ID failed: %v", err)
}

output, err := defaultOutputData()
if err != nil {
t.Fatal(err)
}

data := struct {
outputData
TeamID string
Pkg string
Main bool
TeamID string
Pkg string
Main bool
BitcodeEnabled bool
}{
outputData: defaultOutputData(),
TeamID: teamID,
Pkg: test.pkg,
Main: test.main,
outputData: output,
TeamID: teamID,
Pkg: test.pkg,
Main: test.main,
BitcodeEnabled: bitcodeEnabled,
}

got := filepath.ToSlash(buf.String())
Expand Down Expand Up @@ -94,10 +101,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 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}}
GOARM=7 GOOS=darwin GOARCH=arm CC=iphoneos-clang CXX=iphoneos-clang++ CGO_CFLAGS=-isysroot=iphoneos -miphoneos-version-min=7.0 {{if .BitcodeEnabled}}-fembed-bitcode {{end}}-arch armv7 CGO_CXXFLAGS=-isysroot=iphoneos -miphoneos-version-min=7.0 {{if .BitcodeEnabled}}-fembed-bitcode {{end}}-arch armv7 CGO_LDFLAGS=-isysroot=iphoneos -miphoneos-version-min=7.0 {{if .BitcodeEnabled}}-fembed-bitcode {{end}}-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 {{if .BitcodeEnabled}}-fembed-bitcode {{end}}-arch arm64 CGO_CXXFLAGS=-isysroot=iphoneos -miphoneos-version-min=7.0 {{if .BitcodeEnabled}}-fembed-bitcode {{end}}-arch arm64 CGO_LDFLAGS=-isysroot=iphoneos -miphoneos-version-min=7.0 {{if .BitcodeEnabled}}-fembed-bitcode {{end}}-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 {{if .BitcodeEnabled}}-fembed-bitcode {{end}}-arch i386 CGO_CXXFLAGS=-isysroot=iphonesimulator -mios-simulator-version-min=7.0 {{if .BitcodeEnabled}}-fembed-bitcode {{end}}-arch i386 CGO_LDFLAGS=-isysroot=iphonesimulator -mios-simulator-version-min=7.0 {{if .BitcodeEnabled}}-fembed-bitcode {{end}}-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 {{if .BitcodeEnabled}}-fembed-bitcode {{end}}-arch x86_64 CGO_CXXFLAGS=-isysroot=iphonesimulator -mios-simulator-version-min=7.0 {{if .BitcodeEnabled}}-fembed-bitcode {{end}}-arch x86_64 CGO_LDFLAGS=-isysroot=iphonesimulator -mios-simulator-version-min=7.0 {{if .BitcodeEnabled}}-fembed-bitcode {{end}}-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}}
Expand Down
18 changes: 15 additions & 3 deletions cmd/gomobile/build_iosapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ func goIOSBuild(pkg *packages.Package, bundleID string, archs []string) (map[str
productName = "ProductName" // like xcode.
}

projPbxproj := new(bytes.Buffer)
if err := projPbxprojTmpl.Execute(projPbxproj, projPbxprojTmplData{
BitcodeEnabled: bitcodeEnabled,
}); err != nil {
return nil, err
}

infoplist := new(bytes.Buffer)
if err := infoplistTmpl.Execute(infoplist, infoplistTmplData{
// TODO: better bundle id.
Expand All @@ -44,7 +51,7 @@ func goIOSBuild(pkg *packages.Package, bundleID string, archs []string) (map[str
name string
contents []byte
}{
{tmpdir + "/main.xcodeproj/project.pbxproj", []byte(projPbxproj)},
{tmpdir + "/main.xcodeproj/project.pbxproj", projPbxproj.Bytes()},
{tmpdir + "/main/Info.plist", infoplist.Bytes()},
{tmpdir + "/main/Images.xcassets/AppIcon.appiconset/Contents.json", []byte(contentsJSON)},
}
Expand Down Expand Up @@ -271,7 +278,11 @@ var infoplistTmpl = template.Must(template.New("infoplist").Parse(`<?xml version
</plist>
`))

const projPbxproj = `// !$*UTF8*$!
type projPbxprojTmplData struct {
BitcodeEnabled bool
}

var projPbxprojTmpl = template.Must(template.New("projPbxproj").Parse(`// !$*UTF8*$!
{
archiveVersion = 1;
classes = {
Expand Down Expand Up @@ -429,6 +440,7 @@ const projPbxproj = `// !$*UTF8*$!
SDKROOT = iphoneos;
TARGETED_DEVICE_FAMILY = "1,2";
VALIDATE_PRODUCT = YES;
{{if not .BitcodeEnabled}}ENABLE_BITCODE = NO;{{end}}
};
name = Release;
};
Expand Down Expand Up @@ -465,7 +477,7 @@ const projPbxproj = `// !$*UTF8*$!
};
rootObject = 254BB8361B1FD08900C56DE9 /* Project object */;
}
`
`))

const contentsJSON = `{
"images" : [
Expand Down
16 changes: 15 additions & 1 deletion cmd/gomobile/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ var (
darwinArmNM string

allArchs = []string{"arm", "arm64", "386", "amd64"}

bitcodeEnabled bool
)

func buildEnvInit() (cleanup func(), err error) {
Expand Down Expand Up @@ -73,6 +75,15 @@ func buildEnvInit() (cleanup func(), err error) {
}

func envInit() (err error) {
// Check the current Go version by go-list.
out, err := exec.Command("go", "list", "-e", "-f", `{{range context.ReleaseTags}}{{if eq . "go1.14"}}{{.}}{{end}}{{end}}`).Output()
if err != nil {
return err
}
if len(strings.TrimSpace(string(out))) > 0 {
bitcodeEnabled = true
}

// Setup the cross-compiler environments.
if ndkRoot, err := ndkRoot(); err == nil {
androidEnv = make(map[string][]string)
Expand Down Expand Up @@ -133,10 +144,13 @@ func envInit() (err error) {
default:
panic(fmt.Errorf("unknown GOARCH: %q", arch))
}
cflags += " -fembed-bitcode"
if err != nil {
return err
}

if bitcodeEnabled {
cflags += " -fembed-bitcode"
}
env = append(env,
"GOOS=darwin",
"GOARCH="+arch,
Expand Down
28 changes: 21 additions & 7 deletions cmd/gomobile/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package main

import (
"bytes"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand Down Expand Up @@ -33,7 +34,11 @@ func TestInit(t *testing.T) {
buildX = true

// Test that first GOPATH element is chosen correctly.
gopath = "/GOPATH1"
var err error
gopath, err = ioutil.TempDir("", "gomobile-test")
if err != nil {
t.Fatal(err)
}
paths := []string{gopath, "/path2", "/path3"}
if goos == "windows" {
gopath = filepath.ToSlash(`C:\GOPATH1`)
Expand All @@ -45,8 +50,7 @@ func TestInit(t *testing.T) {
os.Setenv("HOMEDRIVE", "C:")
}

err := runInit(cmdInit)
if err != nil {
if err := runInit(cmdInit); err != nil {
t.Log(buf.String())
t.Fatal(err)
}
Expand All @@ -64,7 +68,10 @@ func diffOutput(got string, wantTmpl *template.Template) (string, error) {
got = filepath.ToSlash(got)

wantBuf := new(bytes.Buffer)
data := defaultOutputData()
data, err := defaultOutputData()
if err != nil {
return "", err
}
if err := wantTmpl.Execute(wantBuf, data); err != nil {
return "", err
}
Expand All @@ -86,20 +93,27 @@ type outputData struct {
Xinfo infoplistTmplData
}

func defaultOutputData() outputData {
func defaultOutputData() (outputData, error) {
projPbxproj := new(bytes.Buffer)
if err := projPbxprojTmpl.Execute(projPbxproj, projPbxprojTmplData{
BitcodeEnabled: bitcodeEnabled,
}); err != nil {
return outputData{}, err
}

data := outputData{
GOOS: goos,
GOARCH: goarch,
GOPATH: gopath,
NDKARCH: archNDK(),
Xproj: projPbxproj,
Xproj: string(projPbxproj.Bytes()),
Xcontents: contentsJSON,
Xinfo: infoplistTmplData{BundleID: "org.golang.todo.basic", Name: "Basic"},
}
if goos == "windows" {
data.EXE = ".exe"
}
return data
return data, nil
}

var initTmpl = template.Must(template.New("output").Parse(`GOMOBILE={{.GOPATH}}/pkg/gomobile
Expand Down

0 comments on commit 159caee

Please sign in to comment.