@@ -13,8 +13,11 @@ import (
13
13
"io"
14
14
"os"
15
15
"os/exec"
16
+ "path"
16
17
"regexp"
17
18
"strings"
19
+
20
+ "golang.org/x/tools/go/packages"
18
21
)
19
22
20
23
var ctx = build .Default
@@ -72,7 +75,7 @@ func runBuild(cmd *command) (err error) {
72
75
73
76
// runBuildImpl builds a package for mobiles based on the given commands.
74
77
// 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 ) {
76
79
cleanup , err := buildEnvInit ()
77
80
if err != nil {
78
81
return nil , err
@@ -86,30 +89,37 @@ func runBuildImpl(cmd *command) (*build.Package, error) {
86
89
return nil , fmt .Errorf (`invalid -target=%q: %v` , buildTarget , err )
87
90
}
88
91
92
+ // TODO(hajimehoshi): ctx is now used only for recording build tags in build. Remove this.
89
93
oldCtx := ctx
90
94
defer func () {
91
95
ctx = oldCtx
92
96
}()
93
- ctx .GOARCH = targetArchs [0 ]
94
- ctx .GOOS = targetOS
95
97
96
- if ctx . GOOS == "darwin" {
98
+ if targetOS == "darwin" {
97
99
ctx .BuildTags = append (ctx .BuildTags , "ios" )
98
100
}
99
101
100
- var pkg * build. Package
102
+ var buildPath string
101
103
switch len (args ) {
102
104
case 0 :
103
- pkg , err = ctx . ImportDir ( cwd , build . ImportComment )
105
+ buildPath = "."
104
106
case 1 :
105
- pkg , err = ctx . Import (args [0 ], cwd , build . ImportComment )
107
+ buildPath = path . Clean (args [0 ])
106
108
default :
107
109
cmd .usage ()
108
110
os .Exit (1 )
109
111
}
112
+ pkgs , err := packages .Load (packagesConfig (targetOS ), buildPath )
110
113
if err != nil {
111
114
return nil , err
112
115
}
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 ]
113
123
114
124
if pkg .Name != "main" && buildO != "" {
115
125
return nil , fmt .Errorf ("cannot set -o when building non-main package" )
@@ -121,7 +131,7 @@ func runBuildImpl(cmd *command) (*build.Package, error) {
121
131
if pkg .Name != "main" {
122
132
for _ , arch := range targetArchs {
123
133
env := androidEnv [arch ]
124
- if err := goBuild (pkg .ImportPath , env ); err != nil {
134
+ if err := goBuild (pkg .PkgPath , env ); err != nil {
125
135
return nil , err
126
136
}
127
137
}
@@ -138,7 +148,7 @@ func runBuildImpl(cmd *command) (*build.Package, error) {
138
148
if pkg .Name != "main" {
139
149
for _ , arch := range targetArchs {
140
150
env := darwinEnv [arch ]
141
- if err := goBuild (pkg .ImportPath , env ); err != nil {
151
+ if err := goBuild (pkg .PkgPath , env ); err != nil {
142
152
return nil , err
143
153
}
144
154
}
@@ -154,7 +164,7 @@ func runBuildImpl(cmd *command) (*build.Package, error) {
154
164
}
155
165
156
166
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 )
158
168
}
159
169
160
170
return pkg , nil
0 commit comments