1 From fe0fcaf43ef3aab81541dad2a71b46254dc4cf6a Mon Sep 17 00:00:00 2001
2 From: Matt Madison <matt@madison.systems>
3 Date: Sat, 17 Feb 2018 10:03:48 -0800
4 Subject: [PATCH] cmd/dist: separate host and target builds
6 Change the dist tool to allow for OE-style cross-
7 and cross-canadian builds:
9 - command flags --host-only and --target only are added;
10 if one is present, the other changes mentioned below
11 take effect, and arguments may also be specified on
12 the command line to enumerate the package(s) to be
15 - for OE cross builds, go_bootstrap is always built for
16 the current build host, and is moved, along with the supporting
17 toolchain (asm, compile, etc.) to a separate 'native_native'
18 directory under GOROOT/pkg/tool.
20 - go_bootstrap is not automatically removed after the build,
21 so it can be reused later (e.g., building both static and
24 Note that for --host-only builds, it would be nice to specify
25 just the "cmd" package to build only the go commands/tools,
26 the staleness checks in the dist tool will fail if the "std"
27 library has not also been built. So host-only builds have to
28 build everything anyway.
30 Upstream-Status: Inappropriate [OE specific]
32 Signed-off-by: Matt Madison <matt@madison.systems>
37 src/cmd/dist/build.go | 153 ++++++++++++++++++++++++++++++------------
38 1 file changed, 111 insertions(+), 42 deletions(-)
40 Index: go/src/cmd/dist/build.go
41 ===================================================================
42 --- go.orig/src/cmd/dist/build.go
43 +++ go/src/cmd/dist/build.go
44 @@ -39,6 +39,7 @@ var (
48 + build_tooldir string
52 @@ -50,6 +51,7 @@ var (
58 vflag int // verbosity
60 @@ -231,6 +233,8 @@ func xinit() {
61 if tooldir = os.Getenv("GOTOOLDIR"); tooldir == "" {
62 tooldir = pathf("%s/pkg/tool/%s_%s", goroot, gohostos, gohostarch)
64 + build_tooldir = pathf("%s/pkg/tool/native_native", goroot)
68 // compilerEnv returns a map from "goos/goarch" to the
69 @@ -260,7 +264,6 @@ func compilerEnv(envName, def string) ma
70 if gohostos != goos || gohostarch != goarch {
71 m[gohostos+"/"+gohostarch] = m[""]
76 for _, goos := range okgoos {
77 @@ -487,8 +490,10 @@ func setup() {
78 // We keep it in pkg/, just like the object directory above.
81 + xremoveall(build_tooldir)
84 + xmkdirall(build_tooldir)
86 // Remove tool binaries from before the tool/gohostos_gohostarch
87 xremoveall(pathf("%s/bin/tool", goroot))
88 @@ -1155,11 +1160,29 @@ func cmdbootstrap() {
94 + var toBuild = []string { "std", "cmd" }
96 flag.BoolVar(&rebuildall, "a", rebuildall, "rebuild all")
97 flag.BoolVar(&debug, "d", debug, "enable debugging of bootstrap process")
98 flag.BoolVar(&noBanner, "no-banner", noBanner, "do not print banner")
99 + flag.BoolVar(&hostOnly, "host-only", hostOnly, "build only host binaries, not target")
100 + flag.BoolVar(&targetOnly, "target-only", targetOnly, "build only target binaries, not host")
105 + if (hostOnly && targetOnly) {
106 + fatalf("specify only one of --host-only or --target-only\n")
108 + crossBuild = hostOnly || targetOnly
109 + if flag.NArg() > 0 {
111 + toBuild = flag.Args()
113 + fatalf("package names not permitted without --host-only or --target-only\n")
118 // cmd/buildid is used in debug mode.
119 @@ -1207,8 +1230,13 @@ func cmdbootstrap() {
123 - gogcflags = os.Getenv("GO_GCFLAGS") // we were using $BOOT_GO_GCFLAGS until now
124 - goldflags = os.Getenv("GO_LDFLAGS")
125 + // For split host/target cross/cross-canadian builds, we don't
126 + // want to be setting these flags until after we have compiled
127 + // the toolchain that runs on the build host.
129 + gogcflags = os.Getenv("GO_GCFLAGS") // we were using $BOOT_GO_GCFLAGS until now
130 + goldflags = os.Getenv("GO_LDFLAGS")
132 goBootstrap := pathf("%s/go_bootstrap", tooldir)
133 cmdGo := pathf("%s/go", gobin)
135 @@ -1237,7 +1265,11 @@ func cmdbootstrap() {
138 xprintf("Building Go toolchain2 using go_bootstrap and Go toolchain1.\n")
139 - os.Setenv("CC", compilerEnvLookup(defaultcc, goos, goarch))
141 + os.Setenv("CC", defaultcc[""])
143 + os.Setenv("CC", compilerEnvLookup(defaultcc, goos, goarch))
145 goInstall(goBootstrap, append([]string{"-i"}, toolchain...)...)
147 run("", ShowOutput|CheckExit, pathf("%s/compile", tooldir), "-V=full")
148 @@ -1274,50 +1306,84 @@ func cmdbootstrap() {
150 checkNotStale(goBootstrap, append(toolchain, "runtime/internal/sys")...)
152 - if goos == oldgoos && goarch == oldgoarch {
153 - // Common case - not setting up for cross-compilation.
154 - timelog("build", "toolchain")
158 + gogcflags = os.Getenv("GO_GCFLAGS")
159 + goldflags = os.Getenv("GO_LDFLAGS")
160 + tool_files, _ := filepath.Glob(pathf("%s/*", tooldir))
161 + for _, f := range tool_files {
162 + copyfile(pathf("%s/%s", build_tooldir, filepath.Base(f)), f, writeExec)
165 + os.Setenv("GOTOOLDIR", build_tooldir)
166 + goBootstrap = pathf("%s/go_bootstrap", build_tooldir)
168 + timelog("build", "host toolchain")
172 + xprintf("Building %s for host, %s/%s.\n", strings.Join(toBuild, ","), goos, goarch)
173 + goInstall(goBootstrap, toBuild...)
174 + checkNotStale(goBootstrap, toBuild...)
175 + // Skip cmdGo staleness checks here, since we can't necessarily run the cmdGo binary
177 + timelog("build", "target toolchain")
181 + } else if targetOnly {
184 + os.Setenv("GOOS", goos)
185 + os.Setenv("GOARCH", goarch)
186 + os.Setenv("CC", compilerEnvLookup(defaultcc, goos, goarch))
187 + xprintf("Building %s for target, %s/%s.\n", strings.Join(toBuild, ","), goos, goarch)
188 + goInstall(goBootstrap, toBuild...)
189 + checkNotStale(goBootstrap, toBuild...)
190 + // Skip cmdGo staleness checks here, since we can't run the target's cmdGo binary
192 - xprintf("Building packages and commands for %s/%s.\n", goos, goarch)
194 - // GOOS/GOARCH does not match GOHOSTOS/GOHOSTARCH.
195 - // Finish GOHOSTOS/GOHOSTARCH installation and then
196 - // run GOOS/GOARCH installation.
197 - timelog("build", "host toolchain")
201 + if goos == oldgoos && goarch == oldgoarch {
202 + // Common case - not setting up for cross-compilation.
203 + timelog("build", "toolchain")
207 + xprintf("Building packages and commands for %s/%s.\n", goos, goarch)
209 + // GOOS/GOARCH does not match GOHOSTOS/GOHOSTARCH.
210 + // Finish GOHOSTOS/GOHOSTARCH installation and then
211 + // run GOOS/GOARCH installation.
212 + timelog("build", "host toolchain")
216 + xprintf("Building packages and commands for host, %s/%s.\n", goos, goarch)
217 + goInstall(goBootstrap, "std", "cmd")
218 + checkNotStale(goBootstrap, "std", "cmd")
219 + checkNotStale(cmdGo, "std", "cmd")
221 + timelog("build", "target toolchain")
227 + os.Setenv("GOOS", goos)
228 + os.Setenv("GOARCH", goarch)
229 + os.Setenv("CC", compilerEnvLookup(defaultcc, goos, goarch))
230 + xprintf("Building packages and commands for target, %s/%s.\n", goos, goarch)
232 - xprintf("Building packages and commands for host, %s/%s.\n", goos, goarch)
233 goInstall(goBootstrap, "std", "cmd")
234 checkNotStale(goBootstrap, "std", "cmd")
235 checkNotStale(cmdGo, "std", "cmd")
237 - timelog("build", "target toolchain")
243 - os.Setenv("GOOS", goos)
244 - os.Setenv("GOARCH", goarch)
245 - os.Setenv("CC", compilerEnvLookup(defaultcc, goos, goarch))
246 - xprintf("Building packages and commands for target, %s/%s.\n", goos, goarch)
248 - targets := []string{"std", "cmd"}
249 - if goos == "js" && goarch == "wasm" {
250 - // Skip the cmd tools for js/wasm. They're not usable.
251 - targets = targets[:1]
253 - goInstall(goBootstrap, targets...)
254 - checkNotStale(goBootstrap, targets...)
255 - checkNotStale(cmdGo, targets...)
257 - run("", ShowOutput|CheckExit, pathf("%s/compile", tooldir), "-V=full")
258 - run("", ShowOutput|CheckExit, pathf("%s/buildid", tooldir), pathf("%s/pkg/%s_%s/runtime/internal/sys.a", goroot, goos, goarch))
259 - checkNotStale(goBootstrap, append(toolchain, "runtime/internal/sys")...)
260 - copyfile(pathf("%s/compile4", tooldir), pathf("%s/compile", tooldir), writeExec)
262 + run("", ShowOutput|CheckExit, pathf("%s/compile", tooldir), "-V=full")
263 + run("", ShowOutput|CheckExit, pathf("%s/buildid", tooldir), pathf("%s/pkg/%s_%s/runtime/internal/sys.a", goroot, goos, goarch))
264 + checkNotStale(goBootstrap, append(toolchain, "runtime/internal/sys")...)
265 + copyfile(pathf("%s/compile4", tooldir), pathf("%s/compile", tooldir), writeExec)
269 // Check that there are no new files in $GOROOT/bin other than
270 @@ -1335,7 +1401,11 @@ func cmdbootstrap() {
273 // Remove go_bootstrap now that we're done.
274 - xremove(pathf("%s/go_bootstrap", tooldir))
275 + // Except that for split host/target cross-builds, we need to
278 + xremove(pathf("%s/go_bootstrap", tooldir))
281 // Print trailing banner unless instructed otherwise.