Skip to content

Commit 80cb72e

Browse files
changkunhajimehoshi
authored andcommitted
cmd/gomobile: use GOOS=ios for iOS builds
This change permits gomobile to build iOS applications by properly set GOOS=ios in the build process. The change is locally tested on darwin/arm64, and golang.org/x/mobile/example/basic can be build using the following commands: gomobile build -target=android -o=basic.apk \ golang.org/x/mobile/example/basic gomobile build -target=ios -bundleid=org.golang.gomobiletest \ -o=basic.app golang.org/x/mobile/example/basic The built binaries are also tested on iOS 15 beta7 and Android 12 API31. Updates golang/go#47952 Fixes golang/go#47238 Change-Id: Ibf40a77933ac957640c78d0dbc1af043477e4b3a Reviewed-on: https://go-review.googlesource.com/c/mobile/+/346150 Reviewed-by: Dmitri Shuralyov <[email protected]> Reviewed-by: Hajime Hoshi <[email protected]> Trust: Dmitri Shuralyov <[email protected]> Trust: Bryan C. Mills <[email protected]> Run-TryBot: Hajime Hoshi <[email protected]> TryBot-Result: Go Bot <[email protected]>
1 parent a0f9ae5 commit 80cb72e

File tree

13 files changed

+32
-54
lines changed

13 files changed

+32
-54
lines changed

cmd/gomobile/bind.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func runBind(cmd *command) error {
8484
if bindJavaPkg != "" && targetOS != "android" {
8585
return fmt.Errorf("-javapkg is supported only for android target")
8686
}
87-
if bindPrefix != "" && targetOS != "darwin" {
87+
if bindPrefix != "" && targetOS != "ios" {
8888
return fmt.Errorf("-prefix is supported only for ios target")
8989
}
9090

@@ -122,7 +122,7 @@ func runBind(cmd *command) error {
122122
switch targetOS {
123123
case "android":
124124
return goAndroidBind(gobind, pkgs, targetArchs)
125-
case "darwin":
125+
case "ios":
126126
if !xcodeAvailable() {
127127
return fmt.Errorf("-target=ios requires XCode")
128128
}
@@ -217,9 +217,6 @@ func packagesConfig(targetOS string) *packages.Config {
217217
// Add CGO_ENABLED=1 explicitly since Cgo is disabled when GOOS is different from host OS.
218218
config.Env = append(os.Environ(), "GOARCH=arm64", "GOOS="+targetOS, "CGO_ENABLED=1")
219219
tags := buildTags
220-
if targetOS == "darwin" {
221-
tags = append(tags, "ios")
222-
}
223220
if len(tags) > 0 {
224221
config.BuildFlags = []string{"-tags=" + strings.Join(tags, ",")}
225222
}
@@ -232,9 +229,6 @@ func getModuleVersions(targetOS string, targetArch string, src string) (*modfile
232229
cmd.Env = append(os.Environ(), "GOOS="+targetOS, "GOARCH="+targetArch)
233230

234231
tags := buildTags
235-
if targetOS == "darwin" {
236-
tags = append(tags, "ios")
237-
}
238232
// TODO(hyangah): probably we don't need to add all the dependencies.
239233
cmd.Args = append(cmd.Args, "-m", "-json", "-tags="+strings.Join(tags, ","), "all")
240234
cmd.Dir = src

cmd/gomobile/bind_iosapp.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ func goIOSBind(gobind string, pkgs []*packages.Package, archs []string) error {
6767
}
6868

6969
for _, arch := range archs {
70-
if err := writeGoMod("darwin", arch); err != nil {
70+
if err := writeGoMod("ios", arch); err != nil {
7171
return err
7272
}
7373

74-
env := darwinEnv[arch]
74+
env := iosEnv[arch]
7575
// Add the generated packages to GOPATH for reverse bindings.
7676
gopath := fmt.Sprintf("GOPATH=%s%c%s", tmpdir, filepath.ListSeparator, goEnv("GOPATH"))
7777
env = append(env, gopath)
@@ -86,7 +86,7 @@ func goIOSBind(gobind string, pkgs []*packages.Package, archs []string) error {
8686

8787
path, err := goIOSBindArchive(name, env, filepath.Join(tmpdir, "src"))
8888
if err != nil {
89-
return fmt.Errorf("darwin-%s: %v", arch, err)
89+
return fmt.Errorf("ios-%s: %v", arch, err)
9090
}
9191
cmd.Args = append(cmd.Args, "-arch", archClang(arch), path)
9292
}

cmd/gomobile/bind_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,8 @@ var bindIOSTmpl = template.Must(template.New("output").Parse(`GOMOBILE={{.GOPATH
196196
WORK=$WORK
197197
GOOS=darwin CGO_ENABLED=1 gobind -lang=go,objc -outdir=$WORK -tags=ios{{if .Prefix}} -prefix={{.Prefix}}{{end}} golang.org/x/mobile/asset
198198
mkdir -p $WORK/src
199-
PWD=$WORK/src 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 GOPATH=$WORK:$GOPATH go build -tags ios -x -buildmode=c-archive -o $WORK/{{.Output}}-arm64.a ./gobind
199+
PWD=$WORK/src GOOS=ios 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 GOPATH=$WORK:$GOPATH go mod tidy
200+
PWD=$WORK/src GOOS=ios 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 GOPATH=$WORK:$GOPATH go build -x -buildmode=c-archive -o $WORK/{{.Output}}-arm64.a ./gobind
200201
rm -r -f "{{.Output}}.framework"
201202
mkdir -p {{.Output}}.framework/Versions/A/Headers
202203
ln -s A {{.Output}}.framework/Versions/Current

cmd/gomobile/build.go

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -127,13 +127,13 @@ func runBuildImpl(cmd *command) (*packages.Package, error) {
127127
if err != nil {
128128
return nil, err
129129
}
130-
case "darwin":
130+
case "ios":
131131
if !xcodeAvailable() {
132132
return nil, fmt.Errorf("-target=ios requires XCode")
133133
}
134134
if pkg.Name != "main" {
135135
for _, arch := range targetArchs {
136-
if err := goBuild(pkg.PkgPath, darwinEnv[arch]); err != nil {
136+
if err := goBuild(pkg.PkgPath, iosEnv[arch]); err != nil {
137137
return nil, err
138138
}
139139
}
@@ -291,13 +291,6 @@ func goCmd(subcmd string, srcs []string, env []string, args ...string) error {
291291
func goCmdAt(at string, subcmd string, srcs []string, env []string, args ...string) error {
292292
cmd := exec.Command("go", subcmd)
293293
tags := buildTags
294-
targetOS, _, err := parseBuildTarget(buildTarget)
295-
if err != nil {
296-
return err
297-
}
298-
if targetOS == "darwin" {
299-
tags = append(tags, "ios")
300-
}
301294
if len(tags) > 0 {
302295
cmd.Args = append(cmd.Args, "-tags", strings.Join(tags, " "))
303296
}
@@ -378,10 +371,6 @@ func parseBuildTarget(buildTarget string) (os string, archs []string, _ error) {
378371
}
379372

380373
targetOS := os
381-
if os == "ios" {
382-
targetOS = "darwin"
383-
}
384-
385374
seen := map[string]bool{}
386375
for _, arch := range archNames {
387376
if _, ok := seen[arch]; ok {

cmd/gomobile/build_darwin_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,9 @@ mkdir -p $WORK/main
104104
echo "{{template "infoplist" .Xinfo}}" > $WORK/main/Info.plist
105105
mkdir -p $WORK/main/Images.xcassets/AppIcon.appiconset
106106
echo "{{.Xcontents}}" > $WORK/main/Images.xcassets/AppIcon.appiconset/Contents.json{{end}}
107-
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}}
108-
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}}
109-
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}}
110-
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}}
111-
xcrun lipo -o $WORK/main/main -create $WORK/arm $WORK/arm64 $WORK/386 $WORK/amd64
107+
GOOS=ios 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 -x {{if .Main}}-ldflags=-w -o=$WORK/arm64 {{end}}{{.Pkg}}
108+
GOOS=ios 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 -x {{if .Main}}-ldflags=-w -o=$WORK/amd64 {{end}}{{.Pkg}}{{if .Main}}
109+
xcrun lipo -o $WORK/main/main -create $WORK/arm64 $WORK/amd64
112110
mkdir -p $WORK/main/assets
113111
xcrun xcodebuild -configuration Release -project $WORK/main.xcodeproj -allowProvisioningUpdates DEVELOPMENT_TEAM={{.TeamID}}
114112
mv $WORK/build/Release-iphoneos/main.app basic.app{{end}}

cmd/gomobile/build_iosapp.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,12 +80,12 @@ func goIOSBuild(pkg *packages.Package, bundleID string, archs []string) (map[str
8080
for _, arch := range archs {
8181
path := filepath.Join(tmpdir, arch)
8282
// Disable DWARF; see golang.org/issues/25148.
83-
if err := goBuild(src, darwinEnv[arch], "-ldflags=-w", "-o="+path); err != nil {
83+
if err := goBuild(src, iosEnv[arch], "-ldflags=-w", "-o="+path); err != nil {
8484
return nil, err
8585
}
8686
if nmpkgs == nil {
8787
var err error
88-
nmpkgs, err = extractPkgs(darwinArmNM, path)
88+
nmpkgs, err = extractPkgs(iosArmNM, path)
8989
if err != nil {
9090
return nil, err
9191
}

cmd/gomobile/build_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,10 +128,10 @@ func TestParseBuildTargetFlag(t *testing.T) {
128128
{"android,android/arm", false, "android", androidArchs},
129129
{"android/arm", false, "android", "arm"},
130130

131-
{"ios", false, "darwin", iosArchs},
132-
{"ios,ios/arm64", false, "darwin", iosArchs},
133-
{"ios/arm64", false, "darwin", "arm64"},
134-
{"ios/amd64", false, "darwin", "amd64"},
131+
{"ios", false, "ios", iosArchs},
132+
{"ios,ios/arm64", false, "ios", iosArchs},
133+
{"ios/arm64", false, "ios", "arm64"},
134+
{"ios/amd64", false, "ios", "amd64"},
135135

136136
{"", true, "", ""},
137137
{"linux", true, "", ""},

cmd/gomobile/doc.go

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/gomobile/env.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ var (
1717

1818
androidEnv map[string][]string // android arch -> []string
1919

20-
darwinEnv map[string][]string
20+
iosEnv map[string][]string
2121

2222
androidArmNM string
23-
darwinArmNM string
23+
iosArmNM string
2424

2525
bitcodeEnabled bool
2626
)
@@ -139,8 +139,8 @@ func envInit() (err error) {
139139
return nil
140140
}
141141

142-
darwinArmNM = "nm"
143-
darwinEnv = make(map[string][]string)
142+
iosArmNM = "nm"
143+
iosEnv = make(map[string][]string)
144144
for _, arch := range allArchs("ios") {
145145
var env []string
146146
var err error
@@ -163,7 +163,7 @@ func envInit() (err error) {
163163
cflags += " -fembed-bitcode"
164164
}
165165
env = append(env,
166-
"GOOS=darwin",
166+
"GOOS=ios",
167167
"GOARCH="+arch,
168168
"CC="+clang,
169169
"CXX="+clang+"++",
@@ -172,7 +172,7 @@ func envInit() (err error) {
172172
"CGO_LDFLAGS="+cflags+" -arch "+archClang(arch),
173173
"CGO_ENABLED=1",
174174
)
175-
darwinEnv[arch] = env
175+
iosEnv[arch] = env
176176
}
177177

178178
return nil

cmd/gomobile/main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ func determineGoVersion() error {
9494
// Ignore unknown versions; it's probably a devel version.
9595
return nil
9696
}
97-
if minor < 10 {
98-
return errors.New("Go 1.10 or newer is required")
97+
if minor < 16 {
98+
return errors.New("Go 1.16 or newer is required")
9999
}
100100
return nil
101101
}
@@ -186,10 +186,10 @@ var usageTmpl = template.Must(template.New("usage").Parse(
186186
187187
To install:
188188
189-
$ go get golang.org/x/mobile/cmd/gomobile
189+
$ go install golang.org/x/mobile/cmd/gomobile@latest
190190
$ gomobile init
191191
192-
At least Go 1.10 is required.
192+
At least Go 1.16 is required.
193193
For detailed instructions, see https://golang.org/wiki/Mobile.
194194
195195
Usage:

example/basic/main.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@
77

88
// An app that draws a green triangle on a red background.
99
//
10-
// Note: This demo is an early preview of Go 1.5. In order to build this
11-
// program as an Android APK using the gomobile tool.
10+
// In order to build this program as an Android APK, using the gomobile tool.
1211
//
1312
// See http://godoc.org/golang.org/x/mobile/cmd/gomobile to install gomobile.
1413
//

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module golang.org/x/mobile
22

3-
go 1.11
3+
go 1.16
44

55
require (
66
golang.org/x/exp v0.0.0-20190731235908-ec7cb31e5a56

internal/mobileinit/mobileinit_ios.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22
// Use of this source code is governed by a BSD-style
33
// license that can be found in the LICENSE file.
44

5-
//go:build ios
6-
// +build ios
7-
85
package mobileinit
96

107
import (

0 commit comments

Comments
 (0)