Skip to content

Commit 9070acd

Browse files
hajimehoshiandydotxyz
authored andcommitted
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 Cherry picked from github.com/golang/mobile
1 parent 618cd00 commit 9070acd

File tree

5 files changed

+51
-28
lines changed

5 files changed

+51
-28
lines changed

cmd/fyne/internal/mobile/bind.go

+12
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import (
1010
"io/ioutil"
1111
"os"
1212
"path/filepath"
13+
"strings"
14+
15+
"golang.org/x/tools/go/packages"
1316
)
1417

1518
// ctx, pkg, tmpdir in build.go
@@ -61,3 +64,12 @@ func writeFile(filename string, generate func(io.Writer) error) error {
6164

6265
return generate(f)
6366
}
67+
68+
func packagesConfig(targetOS string) *packages.Config {
69+
config := &packages.Config{}
70+
config.Env = append(os.Environ(), "GOARCH=arm", "GOOS="+targetOS)
71+
if len(ctx.BuildTags) > 0 {
72+
config.BuildFlags = []string{"-tags=" + strings.Join(ctx.BuildTags, ",")}
73+
}
74+
return config
75+
}

cmd/fyne/internal/mobile/build.go

+20-10
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ import (
1414
"io"
1515
"os"
1616
"os/exec"
17+
"path"
1718
"regexp"
1819
"strings"
20+
21+
"golang.org/x/tools/go/packages"
1922
)
2023

2124
var ctx = build.Default
@@ -77,7 +80,7 @@ func runBuild(cmd *command) (err error) {
7780

7881
// runBuildImpl builds a package for mobiles based on the given commands.
7982
// runBuildImpl returns a built package information and an error if exists.
80-
func runBuildImpl(cmd *command) (*build.Package, error) { cleanup, err := buildEnvInit()
83+
func runBuildImpl(cmd *command) (*packages.Package, error) { cleanup, err := buildEnvInit()
8184
if err != nil {
8285
return nil, err
8386
}
@@ -90,14 +93,13 @@ func runBuildImpl(cmd *command) (*build.Package, error) { cleanup, err := buildE
9093
return nil, fmt.Errorf(`invalid -target=%q: %v`, buildTarget, err)
9194
}
9295

96+
// TODO(hajimehoshi): ctx is now used only for recording build tags in build. Remove this.
9397
oldCtx := ctx
9498
defer func() {
9599
ctx = oldCtx
96100
}()
97-
ctx.GOARCH = targetArchs[0]
98-
ctx.GOOS = targetOS
99101

100-
if ctx.GOOS == "darwin" {
102+
if targetOS == "darwin" {
101103
ctx.BuildTags = append(ctx.BuildTags, "ios")
102104

103105
if buildRelease {
@@ -106,19 +108,27 @@ func runBuildImpl(cmd *command) (*build.Package, error) { cleanup, err := buildE
106108
}
107109
}
108110

109-
var pkg *build.Package
111+
var buildPath string
110112
switch len(args) {
111113
case 0:
112-
pkg, err = ctx.ImportDir(cwd, build.ImportComment)
114+
buildPath = "."
113115
case 1:
114-
pkg, err = ctx.Import(args[0], cwd, build.ImportComment)
116+
buildPath = path.Clean(args[0])
115117
default:
116118
cmd.usage()
117119
os.Exit(1)
118120
}
121+
pkgs, err := packages.Load(packagesConfig(targetOS), buildPath)
119122
if err != nil {
120123
return nil, err
121124
}
125+
// len(pkgs) can be more than 1 e.g., when the specified path includes `...`.
126+
if len(pkgs) != 1 {
127+
cmd.usage()
128+
os.Exit(1)
129+
}
130+
131+
pkg := pkgs[0]
122132

123133
if pkg.Name != "main" && buildO != "" {
124134
return nil, fmt.Errorf("cannot set -o when building non-main package")
@@ -133,7 +143,7 @@ func runBuildImpl(cmd *command) (*build.Package, error) { cleanup, err := buildE
133143
if pkg.Name != "main" {
134144
for _, arch := range targetArchs {
135145
env := androidEnv[arch]
136-
if err := goBuild(pkg.ImportPath, env); err != nil {
146+
if err := goBuild(pkg.PkgPath, env); err != nil {
137147
return nil, err
138148
}
139149
}
@@ -150,7 +160,7 @@ func runBuildImpl(cmd *command) (*build.Package, error) { cleanup, err := buildE
150160
if pkg.Name != "main" {
151161
for _, arch := range targetArchs {
152162
env := darwinEnv[arch]
153-
if err := goBuild(pkg.ImportPath, env); err != nil {
163+
if err := goBuild(pkg.PkgPath, env); err != nil {
154164
return nil, err
155165
}
156166
}
@@ -163,7 +173,7 @@ func runBuildImpl(cmd *command) (*build.Package, error) { cleanup, err := buildE
163173
}
164174

165175
if !nmpkgs["golang.org/x/mobile/app"] {
166-
return nil, fmt.Errorf(`%s does not import "golang.org/x/mobile/app"`, pkg.ImportPath)
176+
return nil, fmt.Errorf(`%s does not import "golang.org/x/mobile/app"`, pkg.PkgPath)
167177
}
168178

169179
return pkg, nil

cmd/fyne/internal/mobile/build_androidapp.go

+10-5
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"
@@ -21,15 +20,21 @@ import (
2120
"strings"
2221

2322
"fyne.io/fyne/cmd/fyne/internal/mobile/binres"
23+
"golang.org/x/tools/go/packages"
2424
)
2525

26-
func goAndroidBuild(pkg *build.Package, bundleID string, androidArchs []string, iconPath, appName string) (map[string]bool, error) {
26+
func goAndroidBuild(pkg *packages.Package, bundleID string, androidArchs []string, iconPath, appName string) (map[string]bool, error) {
2727
ndkRoot, err := ndkRoot()
2828
if err != nil {
2929
return nil, err
3030
}
3131
libName := androidPkgName(appName)
32-
manifestPath := filepath.Join(pkg.Dir, "AndroidManifest.xml")
32+
33+
// TODO(hajimehoshi): This works only with Go tools that assume all source files are in one directory.
34+
// Fix this to work with other Go tools.
35+
dir := filepath.Dir(pkg.GoFiles[0])
36+
37+
manifestPath := filepath.Join(dir, "AndroidManifest.xml")
3338
manifestData, err := ioutil.ReadFile(manifestPath)
3439
if err != nil {
3540
if !os.IsNotExist(err) {
@@ -70,7 +75,7 @@ func goAndroidBuild(pkg *build.Package, bundleID string, androidArchs []string,
7075
return nil, err
7176
}
7277
err = goBuild(
73-
pkg.ImportPath,
78+
pkg.PkgPath,
7479
env,
7580
"-buildmode=c-shared",
7681
"-o", libAbsPath,
@@ -182,7 +187,7 @@ func goAndroidBuild(pkg *build.Package, bundleID string, androidArchs []string,
182187
iconPath string
183188
}
184189
arsc.iconPath = iconPath
185-
assetsDir := filepath.Join(pkg.Dir, "assets")
190+
assetsDir := filepath.Join(dir, "assets")
186191
assetsDirExists := true
187192
fi, err := os.Stat(assetsDir)
188193
if err != nil {

cmd/fyne/internal/mobile/build_iosapp.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,19 @@ 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, appName string) (map[string]bool, error) {
23-
src := pkg.ImportPath
23+
func goIOSBuild(pkg *packages.Package, bundleID string, archs []string, appName string) (map[string]bool, error) {
24+
src := pkg.PkgPath
2425
buildO = rfc1034Label(appName) + ".app"
2526

2627
infoplist := new(bytes.Buffer)
@@ -108,7 +109,7 @@ func goIOSBuild(pkg *build.Package, bundleID string, archs []string, appName str
108109

109110
// TODO(jbd): Fallback to copying if renaming fails.
110111
if buildO == "" {
111-
n := pkg.ImportPath
112+
n := pkg.PkgPath
112113
if n == "." {
113114
// use cwd name
114115
cwd, err := os.Getwd()
@@ -168,13 +169,15 @@ func detectTeamID() (string, error) {
168169
return cert.Subject.OrganizationalUnit[0], nil
169170
}
170171

171-
func iosCopyAssets(pkg *build.Package, xcodeProjDir string) error {
172+
func iosCopyAssets(pkg *packages.Package, xcodeProjDir string) error {
172173
dstAssets := xcodeProjDir + "/main/assets"
173174
if err := mkdir(dstAssets); err != nil {
174175
return err
175176
}
176177

177-
srcAssets := filepath.Join(pkg.Dir, "assets")
178+
// TODO(hajimehoshi): This works only with Go tools that assume all source files are in one directory.
179+
// Fix this to work with other Go tools.
180+
srcAssets := filepath.Join(filepath.Dir(pkg.GoFiles[0]), "assets")
178181
fi, err := os.Stat(srcAssets)
179182
if err != nil {
180183
if os.IsNotExist(err) {

cmd/fyne/internal/mobile/env.go

-7
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)

0 commit comments

Comments
 (0)