-
Notifications
You must be signed in to change notification settings - Fork 769
/
bind.go
329 lines (289 loc) · 8.6 KB
/
bind.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"strings"
"golang.org/x/mod/modfile"
"golang.org/x/tools/go/packages"
)
var cmdBind = &command{
run: runBind,
Name: "bind",
Usage: "[-target android|ios] [-bootclasspath <path>] [-classpath <path>] [-o output] [build flags] [package]",
Short: "build a library for Android and iOS",
Long: `
Bind generates language bindings for the package named by the import
path, and compiles a library for the named target system.
The -target flag takes a target system name, either android (the
default) or ios.
For -target android, the bind command produces an AAR (Android ARchive)
file that archives the precompiled Java API stub classes, the compiled
shared libraries, and all asset files in the /assets subdirectory under
the package directory. The output is named '<package_name>.aar' by
default. This AAR file is commonly used for binary distribution of an
Android library project and most Android IDEs support AAR import. For
example, in Android Studio (1.2+), an AAR file can be imported using
the module import wizard (File > New > New Module > Import .JAR or
.AAR package), and setting it as a new dependency
(File > Project Structure > Dependencies). This requires 'javac'
(version 1.7+) and Android SDK (API level 15 or newer) to build the
library for Android. The environment variable ANDROID_HOME must be set
to the path to Android SDK. Use the -javapkg flag to specify the Java
package prefix for the generated classes.
By default, -target=android builds shared libraries for all supported
instruction sets (arm, arm64, 386, amd64). A subset of instruction sets
can be selected by specifying target type with the architecture name. E.g.,
-target=android/arm,android/386.
For -target ios, gomobile must be run on an OS X machine with Xcode
installed. The generated Objective-C types can be prefixed with the -prefix
flag.
For -target android, the -bootclasspath and -classpath flags are used to
control the bootstrap classpath and the classpath for Go wrappers to Java
classes.
The -v flag provides verbose output, including the list of packages built.
The build flags -a, -n, -x, -gcflags, -ldflags, -tags, -trimpath, and -work
are shared with the build command. For documentation, see 'go help build'.
`,
}
func runBind(cmd *command) error {
cleanup, err := buildEnvInit()
if err != nil {
return err
}
defer cleanup()
args := cmd.flag.Args()
targetOS, targetArchs, err := parseBuildTarget(buildTarget)
if err != nil {
return fmt.Errorf(`invalid -target=%q: %v`, buildTarget, err)
}
if bindJavaPkg != "" && targetOS != "android" {
return fmt.Errorf("-javapkg is supported only for android target")
}
if bindPrefix != "" && targetOS != "darwin" {
return fmt.Errorf("-prefix is supported only for ios target")
}
if targetOS == "android" {
if _, err := ndkRoot(); err != nil {
return err
}
}
var gobind string
if !buildN {
gobind, err = exec.LookPath("gobind")
if err != nil {
return errors.New("gobind was not found. Please run gomobile init before trying again.")
}
} else {
gobind = "gobind"
}
if len(args) == 0 {
args = append(args, ".")
}
pkgs, err := importPackages(args, targetOS)
if err != nil {
return err
}
// check if any of the package is main
for _, pkg := range pkgs {
if pkg.Name == "main" {
return fmt.Errorf("binding 'main' package (%s) is not supported", pkg.PkgPath)
}
}
switch targetOS {
case "android":
return goAndroidBind(gobind, pkgs, targetArchs)
case "darwin":
if !xcodeAvailable() {
return fmt.Errorf("-target=ios requires XCode")
}
return goIOSBind(gobind, pkgs, targetArchs)
default:
return fmt.Errorf(`invalid -target=%q`, buildTarget)
}
}
func importPackages(args []string, targetOS string) ([]*packages.Package, error) {
config := packagesConfig(targetOS)
return packages.Load(config, args...)
}
var (
bindPrefix string // -prefix
bindJavaPkg string // -javapkg
bindClasspath string // -classpath
bindBootClasspath string // -bootclasspath
)
func init() {
// bind command specific commands.
cmdBind.flag.StringVar(&bindJavaPkg, "javapkg", "",
"specifies custom Java package path prefix. Valid only with -target=android.")
cmdBind.flag.StringVar(&bindPrefix, "prefix", "",
"custom Objective-C name prefix. Valid only with -target=ios.")
cmdBind.flag.StringVar(&bindClasspath, "classpath", "", "The classpath for imported Java classes. Valid only with -target=android.")
cmdBind.flag.StringVar(&bindBootClasspath, "bootclasspath", "", "The bootstrap classpath for imported Java classes. Valid only with -target=android.")
}
func bootClasspath() (string, error) {
if bindBootClasspath != "" {
return bindBootClasspath, nil
}
apiPath, err := androidAPIPath()
if err != nil {
return "", err
}
return filepath.Join(apiPath, "android.jar"), nil
}
func copyFile(dst, src string) error {
if buildX {
printcmd("cp %s %s", src, dst)
}
return writeFile(dst, func(w io.Writer) error {
if buildN {
return nil
}
f, err := os.Open(src)
if err != nil {
return err
}
defer f.Close()
if _, err := io.Copy(w, f); err != nil {
return fmt.Errorf("cp %s %s failed: %v", src, dst, err)
}
return nil
})
}
func writeFile(filename string, generate func(io.Writer) error) error {
if buildV {
fmt.Fprintf(os.Stderr, "write %s\n", filename)
}
if err := mkdir(filepath.Dir(filename)); err != nil {
return err
}
if buildN {
return generate(ioutil.Discard)
}
f, err := os.Create(filename)
if err != nil {
return err
}
defer func() {
if cerr := f.Close(); err == nil {
err = cerr
}
}()
return generate(f)
}
func packagesConfig(targetOS string) *packages.Config {
config := &packages.Config{}
// Add CGO_ENABLED=1 explicitly since Cgo is disabled when GOOS is different from host OS.
config.Env = append(os.Environ(), "GOARCH=arm", "GOOS="+targetOS, "CGO_ENABLED=1")
tags := buildTags
if targetOS == "darwin" {
tags = append(tags, "ios")
}
if len(tags) > 0 {
config.BuildFlags = []string{"-tags=" + strings.Join(tags, ",")}
}
return config
}
// getModuleVersions returns a module information at the directory src.
func getModuleVersions(targetOS string, targetArch string, src string) (*modfile.File, error) {
cmd := exec.Command("go", "list")
cmd.Env = append(os.Environ(), "GOOS="+targetOS, "GOARCH="+targetArch)
tags := buildTags
if targetOS == "darwin" {
tags = append(tags, "ios")
}
// TODO(hyangah): probably we don't need to add all the dependencies.
cmd.Args = append(cmd.Args, "-m", "-json", "-tags="+strings.Join(tags, ","), "all")
cmd.Dir = src
output, err := cmd.Output()
if err != nil {
// Module information is not available at src.
return nil, nil
}
type Module struct {
Main bool
Path string
Version string
Dir string
Replace *Module
}
f := &modfile.File{}
f.AddModuleStmt("gobind")
e := json.NewDecoder(bytes.NewReader(output))
for {
var mod *Module
err := e.Decode(&mod)
if err != nil && err != io.EOF {
return nil, err
}
if mod != nil {
if mod.Replace != nil {
p, v := mod.Replace.Path, mod.Replace.Version
if modfile.IsDirectoryPath(p) {
// replaced by a local directory
p = mod.Replace.Dir
}
f.AddReplace(mod.Path, mod.Version, p, v)
} else {
// When the version part is empty, the module is local and mod.Dir represents the location.
if v := mod.Version; v == "" {
f.AddReplace(mod.Path, mod.Version, mod.Dir, "")
} else {
f.AddRequire(mod.Path, v)
}
}
}
if err == io.EOF {
break
}
}
return f, nil
}
// writeGoMod writes go.mod file at $WORK/src when Go modules are used.
func writeGoMod(targetOS string, targetArch string) error {
m, err := areGoModulesUsed()
if err != nil {
return err
}
// If Go modules are not used, go.mod should not be created because the dependencies might not be compatible with Go modules.
if !m {
return nil
}
return writeFile(filepath.Join(tmpdir, "src", "go.mod"), func(w io.Writer) error {
f, err := getModuleVersions(targetOS, targetArch, ".")
if err != nil {
return err
}
if f == nil {
return nil
}
bs, err := f.Format()
if err != nil {
return err
}
if _, err := w.Write(bs); err != nil {
return err
}
return nil
})
}
func areGoModulesUsed() (bool, error) {
out, err := exec.Command("go", "env", "GOMOD").Output()
if err != nil {
return false, err
}
outstr := strings.TrimSpace(string(out))
if outstr == "" {
return false, nil
}
return true, nil
}