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
|
From 7bc891e00be4263311d75aa2b2ee6a3b7b75355f Mon Sep 17 00:00:00 2001
From: Alex Kube <alexander.j.kube@gmail.com>
Date: Wed, 23 Oct 2019 21:18:12 +0430
Subject: [PATCH] cmd/dist: separate host and target builds
Upstream-Status: Inappropriate [OE specific]
Change the dist tool to allow for OE-style cross-
and cross-canadian builds:
- command flags --host-only and --target only are added;
if one is present, the other changes mentioned below
take effect, and arguments may also be specified on
the command line to enumerate the package(s) to be
built.
- for OE cross builds, go_bootstrap is always built for
the current build host, and is moved, along with the supporting
toolchain (asm, compile, etc.) to a separate 'native_native'
directory under GOROOT/pkg/tool.
- go_bootstrap is not automatically removed after the build,
so it can be reused later (e.g., building both static and
shared runtime).
Note that for --host-only builds, it would be nice to specify
just the "cmd" package to build only the go commands/tools,
the staleness checks in the dist tool will fail if the "std"
library has not also been built. So host-only builds have to
build everything anyway.
Adapted to Go 1.13 from patches originally submitted to
the meta/recipes-devtools/go tree by
Matt Madison <matt@madison.systems>.
Signed-off-by: Alexander J Kube <alexander.j.kube@gmail.com>
---
src/cmd/dist/build.go | 156 ++++++++++++++++++++++++++++++------------
1 file changed, 113 insertions(+), 43 deletions(-)
diff --git a/src/cmd/dist/build.go b/src/cmd/dist/build.go
index d82f612..5c8459c 100644
--- a/src/cmd/dist/build.go
+++ b/src/cmd/dist/build.go
@@ -43,6 +43,7 @@ var (
goexperiment string
workdir string
tooldir string
+ build_tooldir string
oldgoos string
oldgoarch string
exe string
@@ -55,6 +56,7 @@ var (
rebuildall bool
defaultclang bool
+ crossBuild bool
vflag int // verbosity
)
@@ -251,6 +253,8 @@ func xinit() {
if tooldir = os.Getenv("GOTOOLDIR"); tooldir == "" {
tooldir = pathf("%s/pkg/tool/%s_%s", goroot, gohostos, gohostarch)
}
+
+ build_tooldir = pathf("%s/pkg/tool/native_native", goroot)
}
// compilerEnv returns a map from "goos/goarch" to the
@@ -496,8 +500,10 @@ func setup() {
p := pathf("%s/pkg/%s_%s", goroot, gohostos, gohostarch)
if rebuildall {
xremoveall(p)
+ xremoveall(build_tooldir)
}
xmkdirall(p)
+ xmkdirall(build_tooldir)
if goos != gohostos || goarch != gohostarch {
p := pathf("%s/pkg/%s_%s", goroot, goos, goarch)
@@ -1267,17 +1273,35 @@ func cmdbootstrap() {
var noBanner, noClean bool
var debug bool
+ var hostOnly bool
+ var targetOnly bool
+ var toBuild = []string{"std", "cmd"}
+
flag.BoolVar(&rebuildall, "a", rebuildall, "rebuild all")
flag.BoolVar(&debug, "d", debug, "enable debugging of bootstrap process")
flag.BoolVar(&noBanner, "no-banner", noBanner, "do not print banner")
flag.BoolVar(&noClean, "no-clean", noClean, "print deprecation warning")
+ flag.BoolVar(&hostOnly, "host-only", hostOnly, "build only host binaries, not target")
+ flag.BoolVar(&targetOnly, "target-only", targetOnly, "build only target binaries, not host")
- xflagparse(0)
+ xflagparse(-1)
if noClean {
xprintf("warning: --no-clean is deprecated and has no effect; use 'go install std cmd' instead\n")
}
+ if hostOnly && targetOnly {
+ fatalf("specify only one of --host-only or --target-only\n")
+ }
+ crossBuild = hostOnly || targetOnly
+ if flag.NArg() > 0 {
+ if crossBuild {
+ toBuild = flag.Args()
+ } else {
+ fatalf("package names not permitted without --host-only or --target-only\n")
+ }
+ }
+
// Set GOPATH to an internal directory. We shouldn't actually
// need to store files here, since the toolchain won't
// depend on modules outside of vendor directories, but if
@@ -1345,8 +1369,13 @@ func cmdbootstrap() {
xprintf("\n")
}
- gogcflags = os.Getenv("GO_GCFLAGS") // we were using $BOOT_GO_GCFLAGS until now
- goldflags = os.Getenv("GO_LDFLAGS") // we were using $BOOT_GO_LDFLAGS until now
+ // For split host/target cross/cross-canadian builds, we don't
+ // want to be setting these flags until after we have compiled
+ // the toolchain that runs on the build host.
+ if !crossBuild {
+ gogcflags = os.Getenv("GO_GCFLAGS") // we were using $BOOT_GO_GCFLAGS until now
+ goldflags = os.Getenv("GO_LDFLAGS") // we were using $BOOT_GO_LDFLAGS until now
+ }
goBootstrap := pathf("%s/go_bootstrap", tooldir)
cmdGo := pathf("%s/go", gobin)
if debug {
@@ -1375,7 +1404,11 @@ func cmdbootstrap() {
xprintf("\n")
}
xprintf("Building Go toolchain2 using go_bootstrap and Go toolchain1.\n")
- os.Setenv("CC", compilerEnvLookup(defaultcc, goos, goarch))
+ if crossBuild {
+ os.Setenv("CC", defaultcc[""])
+ } else {
+ os.Setenv("CC", compilerEnvLookup(defaultcc, goos, goarch))
+ }
// Now that cmd/go is in charge of the build process, enable GOEXPERIMENT.
os.Setenv("GOEXPERIMENT", goexperiment)
goInstall(goBootstrap, append([]string{"-i"}, toolchain...)...)
@@ -1414,50 +1447,84 @@ func cmdbootstrap() {
}
checkNotStale(goBootstrap, append(toolchain, "runtime/internal/sys")...)
- if goos == oldgoos && goarch == oldgoarch {
- // Common case - not setting up for cross-compilation.
- timelog("build", "toolchain")
- if vflag > 0 {
- xprintf("\n")
+ if crossBuild {
+ gogcflags = os.Getenv("GO_GCFLAGS")
+ goldflags = os.Getenv("GO_LDFLAGS")
+ tool_files, _ := filepath.Glob(pathf("%s/*", tooldir))
+ for _, f := range tool_files {
+ copyfile(pathf("%s/%s", build_tooldir, filepath.Base(f)), f, writeExec)
+ xremove(f)
+ }
+ os.Setenv("GOTOOLDIR", build_tooldir)
+ goBootstrap = pathf("%s/go_bootstrap", build_tooldir)
+ if hostOnly {
+ timelog("build", "host toolchain")
+ if vflag > 0 {
+ xprintf("\n")
+ }
+ xprintf("Building %s for host, %s/%s.\n", strings.Join(toBuild, ","), goos, goarch)
+ goInstall(goBootstrap, toBuild...)
+ checkNotStale(goBootstrap, toBuild...)
+ // Skip cmdGo staleness checks here, since we can't necessarily run the cmdGo binary
+
+ timelog("build", "target toolchain")
+ if vflag > 0 {
+ xprintf("\n")
+ }
+ } else if targetOnly {
+ goos = oldgoos
+ goarch = oldgoarch
+ os.Setenv("GOOS", goos)
+ os.Setenv("GOARCH", goarch)
+ os.Setenv("CC", compilerEnvLookup(defaultcc, goos, goarch))
+ xprintf("Building %s for target, %s/%s.\n", strings.Join(toBuild, ","), goos, goarch)
+ goInstall(goBootstrap, toBuild...)
+ checkNotStale(goBootstrap, toBuild...)
+ // Skip cmdGo staleness checks here, since we can't run the target's cmdGo binary
}
- xprintf("Building packages and commands for %s/%s.\n", goos, goarch)
} else {
- // GOOS/GOARCH does not match GOHOSTOS/GOHOSTARCH.
- // Finish GOHOSTOS/GOHOSTARCH installation and then
- // run GOOS/GOARCH installation.
- timelog("build", "host toolchain")
- if vflag > 0 {
- xprintf("\n")
- }
- xprintf("Building packages and commands for host, %s/%s.\n", goos, goarch)
+
+ if goos == oldgoos && goarch == oldgoarch {
+ // Common case - not setting up for cross-compilation.
+ timelog("build", "toolchain")
+ if vflag > 0 {
+ xprintf("\n")
+ }
+ xprintf("Building packages and commands for %s/%s.\n", goos, goarch)
+ } else {
+ // GOOS/GOARCH does not match GOHOSTOS/GOHOSTARCH.
+ // Finish GOHOSTOS/GOHOSTARCH installation and then
+ // run GOOS/GOARCH installation.
+ timelog("build", "host toolchain")
+ if vflag > 0 {
+ xprintf("\n")
+ }
+ xprintf("Building packages and commands for host, %s/%s.\n", goos, goarch)
+ goInstall(goBootstrap, "std", "cmd")
+ checkNotStale(goBootstrap, "std", "cmd")
+ checkNotStale(cmdGo, "std", "cmd")
+
+ timelog("build", "target toolchain")
+ if vflag > 0 {
+ xprintf("\n")
+ }
+ goos = oldgoos
+ goarch = oldgoarch
+ os.Setenv("GOOS", goos)
+ os.Setenv("GOARCH", goarch)
+ os.Setenv("CC", compilerEnvLookup(defaultcc, goos, goarch))
+ xprintf("Building packages and commands for target, %s/%s.\n", goos, goarch)
+ }
goInstall(goBootstrap, "std", "cmd")
checkNotStale(goBootstrap, "std", "cmd")
checkNotStale(cmdGo, "std", "cmd")
- timelog("build", "target toolchain")
- if vflag > 0 {
- xprintf("\n")
+ if debug {
+ run("", ShowOutput|CheckExit, pathf("%s/compile", tooldir), "-V=full")
+ run("", ShowOutput|CheckExit, pathf("%s/buildid", tooldir), pathf("%s/pkg/%s_%s/runtime/internal/sys.a", goroot, goos, goarch))
+ checkNotStale(goBootstrap, append(toolchain, "runtime/internal/sys")...)
+ copyfile(pathf("%s/compile4", tooldir), pathf("%s/compile", tooldir), writeExec)
}
- goos = oldgoos
- goarch = oldgoarch
- os.Setenv("GOOS", goos)
- os.Setenv("GOARCH", goarch)
- os.Setenv("CC", compilerEnvLookup(defaultcc, goos, goarch))
- xprintf("Building packages and commands for target, %s/%s.\n", goos, goarch)
- }
- targets := []string{"std", "cmd"}
- if goos == "js" && goarch == "wasm" {
- // Skip the cmd tools for js/wasm. They're not usable.
- targets = targets[:1]
- }
- goInstall(goBootstrap, targets...)
- checkNotStale(goBootstrap, targets...)
- checkNotStale(cmdGo, targets...)
- if debug {
- run("", ShowOutput|CheckExit, pathf("%s/compile", tooldir), "-V=full")
- run("", ShowOutput|CheckExit, pathf("%s/buildid", tooldir), pathf("%s/pkg/%s_%s/runtime/internal/sys.a", goroot, goos, goarch))
- checkNotStale(goBootstrap, append(toolchain, "runtime/internal/sys")...)
- copyfile(pathf("%s/compile4", tooldir), pathf("%s/compile", tooldir), writeExec)
}
// Check that there are no new files in $GOROOT/bin other than
@@ -1474,8 +1541,11 @@ func cmdbootstrap() {
}
}
- // Remove go_bootstrap now that we're done.
- xremove(pathf("%s/go_bootstrap", tooldir))
+ // Except that for split host/target cross-builds, we need to
+ // keep it.
+ if !crossBuild {
+ xremove(pathf("%s/go_bootstrap", tooldir))
+ }
if goos == "android" {
// Make sure the exec wrapper will sync a fresh $GOROOT to the device.
|