Skip to content

Commit beab3f9

Browse files
committed
cmd/gomobile: disable bitcode on Go 1.13 or older
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]>
1 parent 5516773 commit beab3f9

File tree

5 files changed

+85
-27
lines changed

5 files changed

+85
-27
lines changed

cmd/gomobile/bind_test.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,16 @@ func TestBindAndroid(t *testing.T) {
6767
}
6868
got := filepath.ToSlash(buf.String())
6969

70+
output, err := defaultOutputData()
71+
if err != nil {
72+
t.Fatal(err)
73+
}
7074
data := struct {
7175
outputData
7276
AndroidPlatform string
7377
JavaPkg string
7478
}{
75-
outputData: defaultOutputData(),
79+
outputData: output,
7680
AndroidPlatform: platform,
7781
JavaPkg: tc.javaPkg,
7882
}
@@ -137,12 +141,19 @@ func TestBindIOS(t *testing.T) {
137141
}
138142
got := filepath.ToSlash(buf.String())
139143

144+
output, err := defaultOutputData()
145+
if err != nil {
146+
t.Fatal(err)
147+
}
148+
140149
data := struct {
141150
outputData
142-
Prefix string
151+
Prefix string
152+
BitcodeEnabled bool
143153
}{
144-
outputData: defaultOutputData(),
145-
Prefix: tc.prefix,
154+
outputData: output,
155+
Prefix: tc.prefix,
156+
BitcodeEnabled: bitcodeEnabled,
146157
}
147158

148159
wantBuf := new(bytes.Buffer)
@@ -175,7 +186,7 @@ var bindIOSTmpl = template.Must(template.New("output").Parse(`GOMOBILE={{.GOPATH
175186
WORK=$WORK
176187
GOOS=darwin CGO_ENABLED=1 gobind -lang=go,objc -outdir=$WORK -tags=ios{{if .Prefix}} -prefix={{.Prefix}}{{end}} golang.org/x/mobile/asset
177188
mkdir -p $WORK/src
178-
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
189+
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
179190
rm -r -f "Asset.framework"
180191
mkdir -p Asset.framework/Versions/A/Headers
181192
ln -s A Asset.framework/Versions/Current

cmd/gomobile/build_darwin_test.go

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,23 @@ func TestIOSBuild(t *testing.T) {
5555
t.Fatalf("detecting team ID failed: %v", err)
5656
}
5757

58+
output, err := defaultOutputData()
59+
if err != nil {
60+
t.Fatal(err)
61+
}
62+
5863
data := struct {
5964
outputData
60-
TeamID string
61-
Pkg string
62-
Main bool
65+
TeamID string
66+
Pkg string
67+
Main bool
68+
BitcodeEnabled bool
6369
}{
64-
outputData: defaultOutputData(),
65-
TeamID: teamID,
66-
Pkg: test.pkg,
67-
Main: test.main,
70+
outputData: output,
71+
TeamID: teamID,
72+
Pkg: test.pkg,
73+
Main: test.main,
74+
BitcodeEnabled: bitcodeEnabled,
6875
}
6976

7077
got := filepath.ToSlash(buf.String())
@@ -94,10 +101,10 @@ mkdir -p $WORK/main
94101
echo "{{template "infoplist" .Xinfo}}" > $WORK/main/Info.plist
95102
mkdir -p $WORK/main/Images.xcassets/AppIcon.appiconset
96103
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 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}}
104+
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}}
105+
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}}
106+
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}}
107+
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}}
101108
xcrun lipo -o $WORK/main/main -create $WORK/arm $WORK/arm64 $WORK/386 $WORK/amd64
102109
mkdir -p $WORK/main/assets
103110
xcrun xcodebuild -configuration Release -project $WORK/main.xcodeproj -allowProvisioningUpdates DEVELOPMENT_TEAM={{.TeamID}}

cmd/gomobile/build_iosapp.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ func goIOSBuild(pkg *packages.Package, bundleID string, archs []string) (map[str
3131
productName = "ProductName" // like xcode.
3232
}
3333

34+
projPbxproj := new(bytes.Buffer)
35+
if err := projPbxprojTmpl.Execute(projPbxproj, projPbxprojTmplData{
36+
BitcodeEnabled: bitcodeEnabled,
37+
}); err != nil {
38+
return nil, err
39+
}
40+
3441
infoplist := new(bytes.Buffer)
3542
if err := infoplistTmpl.Execute(infoplist, infoplistTmplData{
3643
// TODO: better bundle id.
@@ -44,7 +51,7 @@ func goIOSBuild(pkg *packages.Package, bundleID string, archs []string) (map[str
4451
name string
4552
contents []byte
4653
}{
47-
{tmpdir + "/main.xcodeproj/project.pbxproj", []byte(projPbxproj)},
54+
{tmpdir + "/main.xcodeproj/project.pbxproj", projPbxproj.Bytes()},
4855
{tmpdir + "/main/Info.plist", infoplist.Bytes()},
4956
{tmpdir + "/main/Images.xcassets/AppIcon.appiconset/Contents.json", []byte(contentsJSON)},
5057
}
@@ -271,7 +278,11 @@ var infoplistTmpl = template.Must(template.New("infoplist").Parse(`<?xml version
271278
</plist>
272279
`))
273280

274-
const projPbxproj = `// !$*UTF8*$!
281+
type projPbxprojTmplData struct {
282+
BitcodeEnabled bool
283+
}
284+
285+
var projPbxprojTmpl = template.Must(template.New("projPbxproj").Parse(`// !$*UTF8*$!
275286
{
276287
archiveVersion = 1;
277288
classes = {
@@ -429,6 +440,7 @@ const projPbxproj = `// !$*UTF8*$!
429440
SDKROOT = iphoneos;
430441
TARGETED_DEVICE_FAMILY = "1,2";
431442
VALIDATE_PRODUCT = YES;
443+
{{if not .BitcodeEnabled}}ENABLE_BITCODE = NO;{{end}}
432444
};
433445
name = Release;
434446
};
@@ -465,7 +477,7 @@ const projPbxproj = `// !$*UTF8*$!
465477
};
466478
rootObject = 254BB8361B1FD08900C56DE9 /* Project object */;
467479
}
468-
`
480+
`))
469481

470482
const contentsJSON = `{
471483
"images" : [

cmd/gomobile/env.go

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ var (
2323
darwinArmNM string
2424

2525
allArchs = []string{"arm", "arm64", "386", "amd64"}
26+
27+
bitcodeEnabled bool
2628
)
2729

2830
func buildEnvInit() (cleanup func(), err error) {
@@ -73,6 +75,15 @@ func buildEnvInit() (cleanup func(), err error) {
7375
}
7476

7577
func envInit() (err error) {
78+
// Check the current Go version by go-list.
79+
out, err := exec.Command("go", "list", "-e", "-f", `{{range context.ReleaseTags}}{{if eq . "go1.14"}}{{.}}{{end}}{{end}}`).Output()
80+
if err != nil {
81+
return err
82+
}
83+
if len(strings.TrimSpace(string(out))) > 0 {
84+
bitcodeEnabled = true
85+
}
86+
7687
// Setup the cross-compiler environments.
7788
if ndkRoot, err := ndkRoot(); err == nil {
7889
androidEnv = make(map[string][]string)
@@ -133,10 +144,13 @@ func envInit() (err error) {
133144
default:
134145
panic(fmt.Errorf("unknown GOARCH: %q", arch))
135146
}
136-
cflags += " -fembed-bitcode"
137147
if err != nil {
138148
return err
139149
}
150+
151+
if bitcodeEnabled {
152+
cflags += " -fembed-bitcode"
153+
}
140154
env = append(env,
141155
"GOOS=darwin",
142156
"GOARCH="+arch,

cmd/gomobile/init_test.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package main
66

77
import (
88
"bytes"
9+
"io/ioutil"
910
"os"
1011
"os/exec"
1112
"path/filepath"
@@ -33,7 +34,11 @@ func TestInit(t *testing.T) {
3334
buildX = true
3435

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

48-
err := runInit(cmdInit)
49-
if err != nil {
53+
if err := runInit(cmdInit); err != nil {
5054
t.Log(buf.String())
5155
t.Fatal(err)
5256
}
@@ -64,7 +68,10 @@ func diffOutput(got string, wantTmpl *template.Template) (string, error) {
6468
got = filepath.ToSlash(got)
6569

6670
wantBuf := new(bytes.Buffer)
67-
data := defaultOutputData()
71+
data, err := defaultOutputData()
72+
if err != nil {
73+
return "", err
74+
}
6875
if err := wantTmpl.Execute(wantBuf, data); err != nil {
6976
return "", err
7077
}
@@ -86,20 +93,27 @@ type outputData struct {
8693
Xinfo infoplistTmplData
8794
}
8895

89-
func defaultOutputData() outputData {
96+
func defaultOutputData() (outputData, error) {
97+
projPbxproj := new(bytes.Buffer)
98+
if err := projPbxprojTmpl.Execute(projPbxproj, projPbxprojTmplData{
99+
BitcodeEnabled: bitcodeEnabled,
100+
}); err != nil {
101+
return outputData{}, err
102+
}
103+
90104
data := outputData{
91105
GOOS: goos,
92106
GOARCH: goarch,
93107
GOPATH: gopath,
94108
NDKARCH: archNDK(),
95-
Xproj: projPbxproj,
109+
Xproj: string(projPbxproj.Bytes()),
96110
Xcontents: contentsJSON,
97111
Xinfo: infoplistTmplData{BundleID: "org.golang.todo.basic", Name: "Basic"},
98112
}
99113
if goos == "windows" {
100114
data.EXE = ".exe"
101115
}
102-
return data
116+
return data, nil
103117
}
104118

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

0 commit comments

Comments
 (0)