Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 08e574b

Browse files
committed
cmd/gomobile: replace go/build with go/packages in gomobile-build
This is a preparation to use Go modules at gomobile command. Updates golang/go#27234 Change-Id: I8ee47cb53f5b748592a0c8c9f383abab27a7fdad Reviewed-on: https://go-review.googlesource.com/c/mobile/+/208059 Run-TryBot: Hajime Hoshi <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Hyang-Ah Hana Kim <[email protected]> Reviewed-by: Daniel Martí <[email protected]>
1 parent 8715129 commit 08e574b

File tree

6 files changed

+46
-35
lines changed

6 files changed

+46
-35
lines changed

cmd/gomobile/bind.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func runBind(cmd *command) error {
116116
var pkgs []*packages.Package
117117
switch len(args) {
118118
case 0:
119-
pkgs, err = packages.Load(packagesConfig(targetOS), cwd)
119+
pkgs, err = packages.Load(packagesConfig(targetOS), ".")
120120
default:
121121
pkgs, err = importPackages(args, targetOS)
122122
}

cmd/gomobile/build.go

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ import (
1313
"io"
1414
"os"
1515
"os/exec"
16+
"path"
1617
"regexp"
1718
"strings"
19+
20+
"golang.org/x/tools/go/packages"
1821
)
1922

2023
var ctx = build.Default
@@ -72,7 +75,7 @@ func runBuild(cmd *command) (err error) {
7275

7376
// runBuildImpl builds a package for mobiles based on the given commands.
7477
// runBuildImpl returns a built package information and an error if exists.
75-
func runBuildImpl(cmd *command) (*build.Package, error) {
78+
func runBuildImpl(cmd *command) (*packages.Package, error) {
7679
cleanup, err := buildEnvInit()
7780
if err != nil {
7881
return nil, err
@@ -86,30 +89,37 @@ func runBuildImpl(cmd *command) (*build.Package, error) {
8689
return nil, fmt.Errorf(`invalid -target=%q: %v`, buildTarget, err)
8790
}
8891

92+
// TODO(hajimehoshi): ctx is now used only for recording build tags in build. Remove this.
8993
oldCtx := ctx
9094
defer func() {
9195
ctx = oldCtx
9296
}()
93-
ctx.GOARCH = targetArchs[0]
94-
ctx.GOOS = targetOS
9597

96-
if ctx.GOOS == "darwin" {
98+
if targetOS == "darwin" {
9799
ctx.BuildTags = append(ctx.BuildTags, "ios")
98100
}
99101

100-
var pkg *build.Package
102+
var buildPath string
101103
switch len(args) {
102104
case 0:
103-
pkg, err = ctx.ImportDir(cwd, build.ImportComment)
105+
buildPath = "."
104106
case 1:
105-
pkg, err = ctx.Import(args[0], cwd, build.ImportComment)
107+
buildPath = path.Clean(args[0])
106108
default:
107109
cmd.usage()
108110
os.Exit(1)
109111
}
112+
pkgs, err := packages.Load(packagesConfig(targetOS), buildPath)
110113
if err != nil {
111114
return nil, err
112115
}
116+
// len(pkgs) can be more than 1 e.g., when the specified path includes `...`.
117+
if len(pkgs) != 1 {
118+
cmd.usage()
119+
os.Exit(1)
120+
}
121+
122+
pkg := pkgs[0]
113123

114124
if pkg.Name != "main" && buildO != "" {
115125
return nil, fmt.Errorf("cannot set -o when building non-main package")
@@ -121,7 +131,7 @@ func runBuildImpl(cmd *command) (*build.Package, error) {
121131
if pkg.Name != "main" {
122132
for _, arch := range targetArchs {
123133
env := androidEnv[arch]
124-
if err := goBuild(pkg.ImportPath, env); err != nil {
134+
if err := goBuild(pkg.PkgPath, env); err != nil {
125135
return nil, err
126136
}
127137
}
@@ -138,7 +148,7 @@ func runBuildImpl(cmd *command) (*build.Package, error) {
138148
if pkg.Name != "main" {
139149
for _, arch := range targetArchs {
140150
env := darwinEnv[arch]
141-
if err := goBuild(pkg.ImportPath, env); err != nil {
151+
if err := goBuild(pkg.PkgPath, env); err != nil {
142152
return nil, err
143153
}
144154
}
@@ -154,7 +164,7 @@ func runBuildImpl(cmd *command) (*build.Package, error) {
154164
}
155165

156166
if !nmpkgs["golang.org/x/mobile/app"] {
157-
return nil, fmt.Errorf(`%s does not import "golang.org/x/mobile/app"`, pkg.ImportPath)
167+
return nil, fmt.Errorf(`%s does not import "golang.org/x/mobile/app"`, pkg.PkgPath)
158168
}
159169

160170
return pkg, nil

cmd/gomobile/build_androidapp.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"encoding/xml"
1313
"errors"
1414
"fmt"
15-
"go/build"
1615
"io"
1716
"io/ioutil"
1817
"log"
@@ -22,16 +21,22 @@ import (
2221
"strings"
2322

2423
"golang.org/x/mobile/internal/binres"
24+
"golang.org/x/tools/go/packages"
2525
)
2626

27-
func goAndroidBuild(pkg *build.Package, androidArchs []string) (map[string]bool, error) {
27+
func goAndroidBuild(pkg *packages.Package, androidArchs []string) (map[string]bool, error) {
2828
ndkRoot, err := ndkRoot()
2929
if err != nil {
3030
return nil, err
3131
}
32-
appName := path.Base(pkg.ImportPath)
32+
appName := path.Base(pkg.PkgPath)
3333
libName := androidPkgName(appName)
34-
manifestPath := filepath.Join(pkg.Dir, "AndroidManifest.xml")
34+
35+
// TODO(hajimehoshi): This works only with Go tools that assume all source files are in one directory.
36+
// Fix this to work with other Go tools.
37+
dir := filepath.Dir(pkg.GoFiles[0])
38+
39+
manifestPath := filepath.Join(dir, "AndroidManifest.xml")
3540
manifestData, err := ioutil.ReadFile(manifestPath)
3641
if err != nil {
3742
if !os.IsNotExist(err) {
@@ -72,7 +77,7 @@ func goAndroidBuild(pkg *build.Package, androidArchs []string) (map[string]bool,
7277
return nil, err
7378
}
7479
err = goBuild(
75-
pkg.ImportPath,
80+
pkg.PkgPath,
7681
env,
7782
"-buildmode=c-shared",
7883
"-o", libAbsPath,
@@ -97,7 +102,7 @@ func goAndroidBuild(pkg *build.Package, androidArchs []string) (map[string]bool,
97102
}
98103

99104
if buildO == "" {
100-
buildO = androidPkgName(filepath.Base(pkg.Dir)) + ".apk"
105+
buildO = androidPkgName(path.Base(pkg.PkgPath)) + ".apk"
101106
}
102107
if !strings.HasSuffix(buildO, ".apk") {
103108
return nil, fmt.Errorf("output file name %q does not end in '.apk'", buildO)
@@ -183,7 +188,7 @@ func goAndroidBuild(pkg *build.Package, androidArchs []string) (map[string]bool,
183188
var arsc struct {
184189
iconPath string
185190
}
186-
assetsDir := filepath.Join(pkg.Dir, "assets")
191+
assetsDir := filepath.Join(dir, "assets")
187192
assetsDirExists := true
188193
fi, err := os.Stat(assetsDir)
189194
if err != nil {

cmd/gomobile/build_iosapp.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,24 @@ import (
99
"crypto/x509"
1010
"encoding/pem"
1111
"fmt"
12-
"go/build"
1312
"io/ioutil"
1413
"os"
1514
"os/exec"
1615
"path"
1716
"path/filepath"
1817
"strings"
1918
"text/template"
19+
20+
"golang.org/x/tools/go/packages"
2021
)
2122

22-
func goIOSBuild(pkg *build.Package, bundleID string, archs []string) (map[string]bool, error) {
23-
src := pkg.ImportPath
23+
func goIOSBuild(pkg *packages.Package, bundleID string, archs []string) (map[string]bool, error) {
24+
src := pkg.PkgPath
2425
if buildO != "" && !strings.HasSuffix(buildO, ".app") {
2526
return nil, fmt.Errorf("-o must have an .app for -target=ios")
2627
}
2728

28-
productName := rfc1034Label(path.Base(pkg.ImportPath))
29+
productName := rfc1034Label(path.Base(pkg.PkgPath))
2930
if productName == "" {
3031
productName = "ProductName" // like xcode.
3132
}
@@ -34,7 +35,7 @@ func goIOSBuild(pkg *build.Package, bundleID string, archs []string) (map[string
3435
if err := infoplistTmpl.Execute(infoplist, infoplistTmplData{
3536
// TODO: better bundle id.
3637
BundleID: bundleID + "." + productName,
37-
Name: strings.Title(path.Base(pkg.ImportPath)),
38+
Name: strings.Title(path.Base(pkg.PkgPath)),
3839
}); err != nil {
3940
return nil, err
4041
}
@@ -116,7 +117,7 @@ func goIOSBuild(pkg *build.Package, bundleID string, archs []string) (map[string
116117

117118
// TODO(jbd): Fallback to copying if renaming fails.
118119
if buildO == "" {
119-
n := pkg.ImportPath
120+
n := pkg.PkgPath
120121
if n == "." {
121122
// use cwd name
122123
cwd, err := os.Getwd()
@@ -176,13 +177,15 @@ func detectTeamID() (string, error) {
176177
return cert.Subject.OrganizationalUnit[0], nil
177178
}
178179

179-
func iosCopyAssets(pkg *build.Package, xcodeProjDir string) error {
180+
func iosCopyAssets(pkg *packages.Package, xcodeProjDir string) error {
180181
dstAssets := xcodeProjDir + "/main/assets"
181182
if err := mkdir(dstAssets); err != nil {
182183
return err
183184
}
184185

185-
srcAssets := filepath.Join(pkg.Dir, "assets")
186+
// TODO(hajimehoshi): This works only with Go tools that assume all source files are in one directory.
187+
// Fix this to work with other Go tools.
188+
srcAssets := filepath.Join(filepath.Dir(pkg.GoFiles[0]), "assets")
186189
fi, err := os.Stat(srcAssets)
187190
if err != nil {
188191
if os.IsNotExist(err) {

cmd/gomobile/env.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313

1414
// General mobile build environment. Initialized by envInit.
1515
var (
16-
cwd string
1716
gomobilepath string // $GOPATH/pkg/gomobile
1817

1918
androidEnv map[string][]string // android arch -> []string
@@ -74,12 +73,6 @@ func buildEnvInit() (cleanup func(), err error) {
7473
}
7574

7675
func envInit() (err error) {
77-
// TODO(crawshaw): cwd only used by ctx.Import, which can take "."
78-
cwd, err = os.Getwd()
79-
if err != nil {
80-
return err
81-
}
82-
8376
// Setup the cross-compiler environments.
8477
if ndkRoot, err := ndkRoot(); err == nil {
8578
androidEnv = make(map[string][]string)

cmd/gomobile/install.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"fmt"
99
"os"
1010
"os/exec"
11-
"path/filepath"
11+
"path"
1212
"strings"
1313
)
1414

@@ -43,7 +43,7 @@ func runInstall(cmd *command) error {
4343
`adb`,
4444
`install`,
4545
`-r`,
46-
androidPkgName(filepath.Base(pkg.Dir))+`.apk`,
46+
androidPkgName(path.Base(pkg.PkgPath))+`.apk`,
4747
)
4848
c.Stdout = os.Stdout
4949
c.Stderr = os.Stderr

0 commit comments

Comments
 (0)