2015-02-11 15:23:09 -05:00
|
|
|
// 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 (
|
2019-12-09 16:01:06 +09:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
cmd/gomobile: replace stripped NDK with external NDK
Gomobile has up until now used stripped NDKs hosted by Google. This
arrangement adds maintenance overhead and blocks the use of custom
NDKs or custom API levels. Also, as noted in issue 16211, the stripped
NDK is no longer tiny because Gomobile supports more platforms.
This CL removed the code for generating and packaging stripped NDKs and
adds support for using external NDKs to the gomobile tool.
gomobile init will now use the NDK installed by the Android SDK manager,
if present, or a user specified NDK if the -ndk flag is given. If no
NDK was found or specified, Android initialization is skipped. gomobile
will instruct the user to run init with a valid NDK if bind or build is
invoked without Android initialization.
gomobile init will also attempt to build OpenAL for Android if the -openal
flag specifies a source directory. It needs cmake and, on Windows, nmake
installed. If gomobile build is run on an app that requires
golang.org/x/mobile/exp/audio/al and OpenAL wasn't built by init, the user
is instructed to do so.
Tested on Linux, macOS, Windows.
Fixes golang/go#16211
Fixes golang/go#18522
Change-Id: Ia38f6e43e671a207dad562678c65225b426e7e3e
Reviewed-on: https://go-review.googlesource.com/35173
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-01-13 00:59:09 +01:00
|
|
|
"errors"
|
2015-02-11 15:23:09 -05:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2018-03-16 08:34:03 +01:00
|
|
|
"os/exec"
|
2015-02-11 15:23:09 -05:00
|
|
|
"path/filepath"
|
2019-11-12 21:44:24 +09:00
|
|
|
"strings"
|
|
|
|
|
2019-12-09 16:01:06 +09:00
|
|
|
"golang.org/x/mod/modfile"
|
2019-11-12 21:44:24 +09:00
|
|
|
"golang.org/x/tools/go/packages"
|
2015-02-11 15:23:09 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
var cmdBind = &command{
|
|
|
|
run: runBind,
|
|
|
|
Name: "bind",
|
2021-09-16 23:09:45 +00:00
|
|
|
Usage: "[-target android|" + strings.Join(applePlatforms, "|") + "] [-bootclasspath <path>] [-classpath <path>] [-o output] [build flags] [package]",
|
2015-08-29 16:27:39 -07:00
|
|
|
Short: "build a library for Android and iOS",
|
2015-02-11 15:23:09 -05:00
|
|
|
Long: `
|
2015-06-19 12:27:15 -04:00
|
|
|
Bind generates language bindings for the package named by the import
|
|
|
|
path, and compiles a library for the named target system.
|
|
|
|
|
2021-09-16 23:09:45 +00:00
|
|
|
The -target flag takes either android (the default), or one or more
|
|
|
|
comma-delimited Apple platforms (` + strings.Join(applePlatforms, ", ") + `).
|
2015-06-19 12:27:15 -04:00
|
|
|
|
|
|
|
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'
|
2016-07-07 11:36:51 -04:00
|
|
|
(version 1.7+) and Android SDK (API level 15 or newer) to build the
|
2015-06-19 12:27:15 -04:00
|
|
|
library for Android. The environment variable ANDROID_HOME must be set
|
2018-03-08 00:15:06 +01:00
|
|
|
to the path to Android SDK. Use the -javapkg flag to specify the Java
|
|
|
|
package prefix for the generated classes.
|
2015-06-19 12:27:15 -04:00
|
|
|
|
2016-06-14 10:43:25 -04:00
|
|
|
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.
|
|
|
|
|
2021-09-16 23:09:45 +00:00
|
|
|
For Apple -target platforms, gomobile must be run on an OS X machine with
|
|
|
|
Xcode installed. The generated Objective-C types can be prefixed with the
|
|
|
|
-prefix flag.
|
2015-02-11 15:23:09 -05:00
|
|
|
|
2016-09-30 12:39:25 +02:00
|
|
|
For -target android, the -bootclasspath and -classpath flags are used to
|
|
|
|
control the bootstrap classpath and the classpath for Go wrappers to Java
|
|
|
|
classes.
|
|
|
|
|
2015-02-11 15:23:09 -05:00
|
|
|
The -v flag provides verbose output, including the list of packages built.
|
|
|
|
|
2019-09-22 14:11:10 +00:00
|
|
|
The build flags -a, -n, -x, -gcflags, -ldflags, -tags, -trimpath, and -work
|
2015-07-27 16:33:08 -04:00
|
|
|
are shared with the build command. For documentation, see 'go help build'.
|
2015-02-11 15:23:09 -05:00
|
|
|
`,
|
|
|
|
}
|
|
|
|
|
|
|
|
func runBind(cmd *command) error {
|
2015-07-16 13:32:51 -04:00
|
|
|
cleanup, err := buildEnvInit()
|
2015-02-11 15:23:09 -05:00
|
|
|
if err != nil {
|
2015-07-10 16:47:46 -06:00
|
|
|
return err
|
2015-02-11 15:23:09 -05:00
|
|
|
}
|
2015-07-10 16:47:46 -06:00
|
|
|
defer cleanup()
|
|
|
|
|
2015-02-11 15:23:09 -05:00
|
|
|
args := cmd.flag.Args()
|
|
|
|
|
2021-09-16 23:09:45 +00:00
|
|
|
targets, err := parseBuildTarget(buildTarget)
|
2015-12-11 18:36:28 -05:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(`invalid -target=%q: %v`, buildTarget, err)
|
2015-07-24 07:04:40 -04:00
|
|
|
}
|
|
|
|
|
2021-09-16 23:09:45 +00:00
|
|
|
if isAndroidPlatform(targets[0].platform) {
|
|
|
|
if bindPrefix != "" {
|
|
|
|
return fmt.Errorf("-prefix is supported only for Apple targets")
|
|
|
|
}
|
2019-02-22 11:41:06 +01:00
|
|
|
if _, err := ndkRoot(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-09-16 23:09:45 +00:00
|
|
|
} else {
|
|
|
|
if bindJavaPkg != "" {
|
|
|
|
return fmt.Errorf("-javapkg is supported only for android target")
|
|
|
|
}
|
cmd/gomobile: replace stripped NDK with external NDK
Gomobile has up until now used stripped NDKs hosted by Google. This
arrangement adds maintenance overhead and blocks the use of custom
NDKs or custom API levels. Also, as noted in issue 16211, the stripped
NDK is no longer tiny because Gomobile supports more platforms.
This CL removed the code for generating and packaging stripped NDKs and
adds support for using external NDKs to the gomobile tool.
gomobile init will now use the NDK installed by the Android SDK manager,
if present, or a user specified NDK if the -ndk flag is given. If no
NDK was found or specified, Android initialization is skipped. gomobile
will instruct the user to run init with a valid NDK if bind or build is
invoked without Android initialization.
gomobile init will also attempt to build OpenAL for Android if the -openal
flag specifies a source directory. It needs cmake and, on Windows, nmake
installed. If gomobile build is run on an app that requires
golang.org/x/mobile/exp/audio/al and OpenAL wasn't built by init, the user
is instructed to do so.
Tested on Linux, macOS, Windows.
Fixes golang/go#16211
Fixes golang/go#18522
Change-Id: Ia38f6e43e671a207dad562678c65225b426e7e3e
Reviewed-on: https://go-review.googlesource.com/35173
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2017-01-13 00:59:09 +01:00
|
|
|
}
|
2018-03-09 10:32:43 +01:00
|
|
|
|
2018-03-16 08:34:03 +01:00
|
|
|
var gobind string
|
|
|
|
if !buildN {
|
|
|
|
gobind, err = exec.LookPath("gobind")
|
|
|
|
if err != nil {
|
2021-09-16 23:09:45 +00:00
|
|
|
return errors.New("gobind was not found. Please run gomobile init before trying again")
|
2018-03-16 08:34:03 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
gobind = "gobind"
|
|
|
|
}
|
|
|
|
|
2019-12-09 16:01:06 +09:00
|
|
|
if len(args) == 0 {
|
|
|
|
args = append(args, ".")
|
2015-02-11 15:23:09 -05:00
|
|
|
}
|
2021-09-16 23:09:45 +00:00
|
|
|
|
|
|
|
// TODO(ydnar): this should work, unless build tags affect loading a single package.
|
|
|
|
// Should we try to import packages with different build tags per platform?
|
|
|
|
pkgs, err := packages.Load(packagesConfig(targets[0]), args...)
|
2015-02-11 15:23:09 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-05-05 09:22:14 -07:00
|
|
|
// check if any of the package is main
|
|
|
|
for _, pkg := range pkgs {
|
|
|
|
if pkg.Name == "main" {
|
2021-09-16 23:09:45 +00:00
|
|
|
return fmt.Errorf(`binding "main" package (%s) is not supported`, pkg.PkgPath)
|
2016-05-05 09:22:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-16 23:09:45 +00:00
|
|
|
switch {
|
|
|
|
case isAndroidPlatform(targets[0].platform):
|
|
|
|
return goAndroidBind(gobind, pkgs, targets)
|
|
|
|
case isApplePlatform(targets[0].platform):
|
2018-03-28 12:39:35 +02:00
|
|
|
if !xcodeAvailable() {
|
2021-09-16 23:09:45 +00:00
|
|
|
return fmt.Errorf("-target=%q requires Xcode", buildTarget)
|
2018-03-28 12:39:35 +02:00
|
|
|
}
|
2021-09-16 23:09:45 +00:00
|
|
|
return goAppleBind(gobind, pkgs, targets)
|
2015-07-24 16:02:39 -04:00
|
|
|
default:
|
2015-12-11 18:36:28 -05:00
|
|
|
return fmt.Errorf(`invalid -target=%q`, buildTarget)
|
2015-06-19 12:27:15 -04:00
|
|
|
}
|
2015-02-11 15:23:09 -05:00
|
|
|
}
|
|
|
|
|
2015-08-28 13:39:30 -04:00
|
|
|
var (
|
2016-09-30 12:39:25 +02:00
|
|
|
bindPrefix string // -prefix
|
|
|
|
bindJavaPkg string // -javapkg
|
|
|
|
bindClasspath string // -classpath
|
|
|
|
bindBootClasspath string // -bootclasspath
|
2015-08-28 13:39:30 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
// bind command specific commands.
|
|
|
|
cmdBind.flag.StringVar(&bindJavaPkg, "javapkg", "",
|
2017-01-01 22:43:46 +01:00
|
|
|
"specifies custom Java package path prefix. Valid only with -target=android.")
|
2016-12-23 11:18:18 +01:00
|
|
|
cmdBind.flag.StringVar(&bindPrefix, "prefix", "",
|
2017-01-01 22:43:46 +01:00
|
|
|
"custom Objective-C name prefix. Valid only with -target=ios.")
|
2016-09-30 12:39:25 +02:00
|
|
|
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.")
|
2015-08-28 13:39:30 -04:00
|
|
|
}
|
|
|
|
|
2016-09-30 12:39:25 +02:00
|
|
|
func bootClasspath() (string, error) {
|
|
|
|
if bindBootClasspath != "" {
|
|
|
|
return bindBootClasspath, nil
|
|
|
|
}
|
2016-09-07 18:27:36 +02:00
|
|
|
apiPath, err := androidAPIPath()
|
|
|
|
if err != nil {
|
2016-09-30 12:39:25 +02:00
|
|
|
return "", err
|
2016-09-07 18:27:36 +02:00
|
|
|
}
|
2016-09-30 12:39:25 +02:00
|
|
|
return filepath.Join(apiPath, "android.jar"), nil
|
|
|
|
}
|
|
|
|
|
2015-07-14 18:09:25 -04:00
|
|
|
func copyFile(dst, src string) error {
|
|
|
|
if buildX {
|
|
|
|
printcmd("cp %s %s", src, dst)
|
|
|
|
}
|
|
|
|
return writeFile(dst, func(w io.Writer) error {
|
2015-07-19 21:29:35 -04:00
|
|
|
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
|
2015-07-14 18:09:25 -04:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-02-11 15:23:09 -05:00
|
|
|
func writeFile(filename string, generate func(io.Writer) error) error {
|
|
|
|
if buildV {
|
|
|
|
fmt.Fprintf(os.Stderr, "write %s\n", filename)
|
|
|
|
}
|
|
|
|
|
2019-12-09 16:01:06 +09:00
|
|
|
if err := mkdir(filepath.Dir(filename)); err != nil {
|
2015-02-11 15:23:09 -05:00
|
|
|
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)
|
|
|
|
}
|
2019-11-12 21:44:24 +09:00
|
|
|
|
2021-09-16 23:09:45 +00:00
|
|
|
func packagesConfig(t targetInfo) *packages.Config {
|
2019-11-12 21:44:24 +09:00
|
|
|
config := &packages.Config{}
|
2020-01-14 11:55:56 +09:00
|
|
|
// Add CGO_ENABLED=1 explicitly since Cgo is disabled when GOOS is different from host OS.
|
2021-09-16 23:09:45 +00:00
|
|
|
config.Env = append(os.Environ(), "GOARCH="+t.arch, "GOOS="+platformOS(t.platform), "CGO_ENABLED=1")
|
|
|
|
tags := append(buildTags[:], platformTags(t.platform)...)
|
|
|
|
|
2019-11-20 16:55:32 +09:00
|
|
|
if len(tags) > 0 {
|
|
|
|
config.BuildFlags = []string{"-tags=" + strings.Join(tags, ",")}
|
2019-11-12 21:44:24 +09:00
|
|
|
}
|
|
|
|
return config
|
|
|
|
}
|
2019-12-09 16:01:06 +09:00
|
|
|
|
|
|
|
// getModuleVersions returns a module information at the directory src.
|
2021-09-16 23:09:45 +00:00
|
|
|
func getModuleVersions(targetPlatform string, targetArch string, src string) (*modfile.File, error) {
|
2020-01-15 18:21:22 +09:00
|
|
|
cmd := exec.Command("go", "list")
|
2021-09-16 23:09:45 +00:00
|
|
|
cmd.Env = append(os.Environ(), "GOOS="+platformOS(targetPlatform), "GOARCH="+targetArch)
|
|
|
|
|
|
|
|
tags := append(buildTags[:], platformTags(targetPlatform)...)
|
2019-12-09 16:01:06 +09:00
|
|
|
|
cmd/gomobile: handle modules replaced by other versioned modules
Previously, gomobile bind's go.mod generation logic assumed
replacing module was always located in the disk, but is not
always true. It's valid to replace a module(version) with
another module&version.
For example,
replace golang.org/x/tools => ../
causes:
{
"Path": "golang.org/x/tools",
"Version": "v0.0.0-20191017151554-a3bc800455d5",
"Replace": {
"Path": "../",
"Dir": "/usr/local/google/home/hakim/go/src/golang.org/x/tools",
"GoMod": "/usr/local/google/home/hakim/go/src/golang.org/x/tools/go.mod",
"GoVersion": "1.11"
},
"Dir": "/usr/local/google/home/hakim/go/src/golang.org/x/tools",
"GoMod": "/usr/local/google/home/hakim/go/src/golang.org/x/tools/go.mod",
"GoVersion": "1.11"
}
replace github.com/anacrolix/torrent v1.13.0 => gitlab.com/axet/torrent v0.0.0-20200205141541-92b4b9e7387e
causes:
{
"Path": "github.com/anacrolix/torrent",
"Version": "v1.13.0",
"Replace": {
"Path": "gitlab.com/axet/torrent",
"Version": "v0.0.0-20200205141541-92b4b9e7387e",
"Time": "2020-02-05T14:15:41Z",
"Dir": "/usr/local/google/home/hakim/go/pkg/mod/gitlab.com/axet/torrent@v0.0.0-20200205141541-92b4b9e7387e",
"GoMod": "/usr/local/google/home/hakim/go/pkg/mod/cache/download/gitlab.com/axet/torrent/@v/v0.0.0-20200205141541-92b4b9e7387e.mod"
},
"Dir": "/usr/local/google/home/hakim/go/pkg/mod/gitlab.com/axet/torrent@v0.0.0-20200205141541-92b4b9e7387e",
"GoMod": "/usr/local/google/home/hakim/go/pkg/mod/cache/download/gitlab.com/axet/torrent/@v/v0.0.0-20200205141541-92b4b9e7387e.mod"
}
Also, while we are here, trim down the entries added to the generated
go.mod. We need the main module, and the replaced module info.
We may want to pin golang.org/x/mobile version if possible, but I don't
know a reliable way to achieve that yet.
Fixes golang/go#37048
Change-Id: Ibd7332338c0a3c4165a642c3e86852061f6ab13b
Reviewed-on: https://go-review.googlesource.com/c/mobile/+/218057
Run-TryBot: Hyang-Ah Hana Kim <hyangah@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Hajime Hoshi <hajimehoshi@gmail.com>
2020-02-05 18:37:23 -05:00
|
|
|
// TODO(hyangah): probably we don't need to add all the dependencies.
|
2019-12-09 16:01:06 +09:00
|
|
|
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 {
|
cmd/gomobile: handle modules replaced by other versioned modules
Previously, gomobile bind's go.mod generation logic assumed
replacing module was always located in the disk, but is not
always true. It's valid to replace a module(version) with
another module&version.
For example,
replace golang.org/x/tools => ../
causes:
{
"Path": "golang.org/x/tools",
"Version": "v0.0.0-20191017151554-a3bc800455d5",
"Replace": {
"Path": "../",
"Dir": "/usr/local/google/home/hakim/go/src/golang.org/x/tools",
"GoMod": "/usr/local/google/home/hakim/go/src/golang.org/x/tools/go.mod",
"GoVersion": "1.11"
},
"Dir": "/usr/local/google/home/hakim/go/src/golang.org/x/tools",
"GoMod": "/usr/local/google/home/hakim/go/src/golang.org/x/tools/go.mod",
"GoVersion": "1.11"
}
replace github.com/anacrolix/torrent v1.13.0 => gitlab.com/axet/torrent v0.0.0-20200205141541-92b4b9e7387e
causes:
{
"Path": "github.com/anacrolix/torrent",
"Version": "v1.13.0",
"Replace": {
"Path": "gitlab.com/axet/torrent",
"Version": "v0.0.0-20200205141541-92b4b9e7387e",
"Time": "2020-02-05T14:15:41Z",
"Dir": "/usr/local/google/home/hakim/go/pkg/mod/gitlab.com/axet/torrent@v0.0.0-20200205141541-92b4b9e7387e",
"GoMod": "/usr/local/google/home/hakim/go/pkg/mod/cache/download/gitlab.com/axet/torrent/@v/v0.0.0-20200205141541-92b4b9e7387e.mod"
},
"Dir": "/usr/local/google/home/hakim/go/pkg/mod/gitlab.com/axet/torrent@v0.0.0-20200205141541-92b4b9e7387e",
"GoMod": "/usr/local/google/home/hakim/go/pkg/mod/cache/download/gitlab.com/axet/torrent/@v/v0.0.0-20200205141541-92b4b9e7387e.mod"
}
Also, while we are here, trim down the entries added to the generated
go.mod. We need the main module, and the replaced module info.
We may want to pin golang.org/x/mobile version if possible, but I don't
know a reliable way to achieve that yet.
Fixes golang/go#37048
Change-Id: Ibd7332338c0a3c4165a642c3e86852061f6ab13b
Reviewed-on: https://go-review.googlesource.com/c/mobile/+/218057
Run-TryBot: Hyang-Ah Hana Kim <hyangah@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Hajime Hoshi <hajimehoshi@gmail.com>
2020-02-05 18:37:23 -05:00
|
|
|
Main bool
|
2019-12-09 16:01:06 +09:00
|
|
|
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 {
|
2020-03-29 14:35:15 +09:00
|
|
|
if mod.Replace != nil {
|
cmd/gomobile: handle modules replaced by other versioned modules
Previously, gomobile bind's go.mod generation logic assumed
replacing module was always located in the disk, but is not
always true. It's valid to replace a module(version) with
another module&version.
For example,
replace golang.org/x/tools => ../
causes:
{
"Path": "golang.org/x/tools",
"Version": "v0.0.0-20191017151554-a3bc800455d5",
"Replace": {
"Path": "../",
"Dir": "/usr/local/google/home/hakim/go/src/golang.org/x/tools",
"GoMod": "/usr/local/google/home/hakim/go/src/golang.org/x/tools/go.mod",
"GoVersion": "1.11"
},
"Dir": "/usr/local/google/home/hakim/go/src/golang.org/x/tools",
"GoMod": "/usr/local/google/home/hakim/go/src/golang.org/x/tools/go.mod",
"GoVersion": "1.11"
}
replace github.com/anacrolix/torrent v1.13.0 => gitlab.com/axet/torrent v0.0.0-20200205141541-92b4b9e7387e
causes:
{
"Path": "github.com/anacrolix/torrent",
"Version": "v1.13.0",
"Replace": {
"Path": "gitlab.com/axet/torrent",
"Version": "v0.0.0-20200205141541-92b4b9e7387e",
"Time": "2020-02-05T14:15:41Z",
"Dir": "/usr/local/google/home/hakim/go/pkg/mod/gitlab.com/axet/torrent@v0.0.0-20200205141541-92b4b9e7387e",
"GoMod": "/usr/local/google/home/hakim/go/pkg/mod/cache/download/gitlab.com/axet/torrent/@v/v0.0.0-20200205141541-92b4b9e7387e.mod"
},
"Dir": "/usr/local/google/home/hakim/go/pkg/mod/gitlab.com/axet/torrent@v0.0.0-20200205141541-92b4b9e7387e",
"GoMod": "/usr/local/google/home/hakim/go/pkg/mod/cache/download/gitlab.com/axet/torrent/@v/v0.0.0-20200205141541-92b4b9e7387e.mod"
}
Also, while we are here, trim down the entries added to the generated
go.mod. We need the main module, and the replaced module info.
We may want to pin golang.org/x/mobile version if possible, but I don't
know a reliable way to achieve that yet.
Fixes golang/go#37048
Change-Id: Ibd7332338c0a3c4165a642c3e86852061f6ab13b
Reviewed-on: https://go-review.googlesource.com/c/mobile/+/218057
Run-TryBot: Hyang-Ah Hana Kim <hyangah@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Hajime Hoshi <hajimehoshi@gmail.com>
2020-02-05 18:37:23 -05:00
|
|
|
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)
|
2020-03-29 14:35:15 +09:00
|
|
|
} else {
|
2019-12-09 16:01:06 +09:00
|
|
|
// When the version part is empty, the module is local and mod.Dir represents the location.
|
cmd/gomobile: handle modules replaced by other versioned modules
Previously, gomobile bind's go.mod generation logic assumed
replacing module was always located in the disk, but is not
always true. It's valid to replace a module(version) with
another module&version.
For example,
replace golang.org/x/tools => ../
causes:
{
"Path": "golang.org/x/tools",
"Version": "v0.0.0-20191017151554-a3bc800455d5",
"Replace": {
"Path": "../",
"Dir": "/usr/local/google/home/hakim/go/src/golang.org/x/tools",
"GoMod": "/usr/local/google/home/hakim/go/src/golang.org/x/tools/go.mod",
"GoVersion": "1.11"
},
"Dir": "/usr/local/google/home/hakim/go/src/golang.org/x/tools",
"GoMod": "/usr/local/google/home/hakim/go/src/golang.org/x/tools/go.mod",
"GoVersion": "1.11"
}
replace github.com/anacrolix/torrent v1.13.0 => gitlab.com/axet/torrent v0.0.0-20200205141541-92b4b9e7387e
causes:
{
"Path": "github.com/anacrolix/torrent",
"Version": "v1.13.0",
"Replace": {
"Path": "gitlab.com/axet/torrent",
"Version": "v0.0.0-20200205141541-92b4b9e7387e",
"Time": "2020-02-05T14:15:41Z",
"Dir": "/usr/local/google/home/hakim/go/pkg/mod/gitlab.com/axet/torrent@v0.0.0-20200205141541-92b4b9e7387e",
"GoMod": "/usr/local/google/home/hakim/go/pkg/mod/cache/download/gitlab.com/axet/torrent/@v/v0.0.0-20200205141541-92b4b9e7387e.mod"
},
"Dir": "/usr/local/google/home/hakim/go/pkg/mod/gitlab.com/axet/torrent@v0.0.0-20200205141541-92b4b9e7387e",
"GoMod": "/usr/local/google/home/hakim/go/pkg/mod/cache/download/gitlab.com/axet/torrent/@v/v0.0.0-20200205141541-92b4b9e7387e.mod"
}
Also, while we are here, trim down the entries added to the generated
go.mod. We need the main module, and the replaced module info.
We may want to pin golang.org/x/mobile version if possible, but I don't
know a reliable way to achieve that yet.
Fixes golang/go#37048
Change-Id: Ibd7332338c0a3c4165a642c3e86852061f6ab13b
Reviewed-on: https://go-review.googlesource.com/c/mobile/+/218057
Run-TryBot: Hyang-Ah Hana Kim <hyangah@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Hajime Hoshi <hajimehoshi@gmail.com>
2020-02-05 18:37:23 -05:00
|
|
|
if v := mod.Version; v == "" {
|
|
|
|
f.AddReplace(mod.Path, mod.Version, mod.Dir, "")
|
|
|
|
} else {
|
|
|
|
f.AddRequire(mod.Path, v)
|
|
|
|
}
|
2019-12-09 16:01:06 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if err == io.EOF {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return f, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// writeGoMod writes go.mod file at $WORK/src when Go modules are used.
|
2021-09-16 23:09:45 +00:00
|
|
|
func writeGoMod(dir, targetPlatform, targetArch string) error {
|
2019-12-09 16:01:06 +09:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2021-09-16 23:09:45 +00:00
|
|
|
return writeFile(filepath.Join(dir, "src", "go.mod"), func(w io.Writer) error {
|
|
|
|
f, err := getModuleVersions(targetPlatform, targetArch, ".")
|
2019-12-09 16:01:06 +09:00
|
|
|
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) {
|
2020-01-15 18:21:22 +09:00
|
|
|
out, err := exec.Command("go", "env", "GOMOD").Output()
|
2019-12-09 16:01:06 +09:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
outstr := strings.TrimSpace(string(out))
|
|
|
|
if outstr == "" {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
return true, nil
|
|
|
|
}
|