blob: 29598449da5df5d87b3762e5abf13c730e813eb7 [file] [log] [blame]
Andrew Geissler6aa7eec2023-03-03 12:41:14 -06001From 7a191e5191c8b813e929caedb3f3918bb08692a1 Mon Sep 17 00:00:00 2001
Andrew Geissler595f6302022-01-24 19:11:47 +00002From: Alex Kube <alexander.j.kube@gmail.com>
3Date: Wed, 23 Oct 2019 21:18:12 +0430
Andrew Geissler6aa7eec2023-03-03 12:41:14 -06004Subject: [PATCH 5/9] cmd/dist: separate host and target builds
Andrew Geissler595f6302022-01-24 19:11:47 +00005
6Upstream-Status: Inappropriate [OE specific]
7
8Change the dist tool to allow for OE-style cross-
9and cross-canadian builds:
10
11 - command flags --host-only and --target only are added;
12 if one is present, the other changes mentioned below
13 take effect, and arguments may also be specified on
14 the command line to enumerate the package(s) to be
15 built.
16
17 - for OE cross builds, go_bootstrap is always built for
18 the current build host, and is moved, along with the supporting
19 toolchain (asm, compile, etc.) to a separate 'native_native'
20 directory under GOROOT/pkg/tool.
21
22 - go_bootstrap is not automatically removed after the build,
23 so it can be reused later (e.g., building both static and
24 shared runtime).
25
26Note that for --host-only builds, it would be nice to specify
27just the "cmd" package to build only the go commands/tools,
28the staleness checks in the dist tool will fail if the "std"
29library has not also been built. So host-only builds have to
30build everything anyway.
31
32Adapted to Go 1.13 from patches originally submitted to
33the meta/recipes-devtools/go tree by
34Matt Madison <matt@madison.systems>.
35
36Signed-off-by: Alexander J Kube <alexander.j.kube@gmail.com>
Andrew Geissler595f6302022-01-24 19:11:47 +000037---
Andrew Geissler6aa7eec2023-03-03 12:41:14 -060038 src/cmd/dist/build.go | 152 +++++++++++++++++++++++++++++++-----------
39 1 file changed, 113 insertions(+), 39 deletions(-)
Andrew Geissler595f6302022-01-24 19:11:47 +000040
Patrick Williams92b42cb2022-09-03 06:53:57 -050041diff --git a/src/cmd/dist/build.go b/src/cmd/dist/build.go
Andrew Geissler6aa7eec2023-03-03 12:41:14 -060042index 5d31718..1c7f308 100644
Andrew Geissler595f6302022-01-24 19:11:47 +000043--- a/src/cmd/dist/build.go
44+++ b/src/cmd/dist/build.go
Andrew Geissler6aa7eec2023-03-03 12:41:14 -060045@@ -44,6 +44,7 @@ var (
Andrew Geissler595f6302022-01-24 19:11:47 +000046 goexperiment string
47 workdir string
48 tooldir string
49+ build_tooldir string
50 oldgoos string
51 oldgoarch string
52 exe string
Patrick Williams92b42cb2022-09-03 06:53:57 -050053@@ -55,6 +56,7 @@ var (
Andrew Geissler595f6302022-01-24 19:11:47 +000054 rebuildall bool
55 defaultclang bool
Andrew Geissler6aa7eec2023-03-03 12:41:14 -060056 noOpt bool
Andrew Geissler595f6302022-01-24 19:11:47 +000057+ crossBuild bool
58
59 vflag int // verbosity
60 )
Patrick Williams92b42cb2022-09-03 06:53:57 -050061@@ -267,6 +269,8 @@ func xinit() {
Andrew Geissler595f6302022-01-24 19:11:47 +000062 if tooldir = os.Getenv("GOTOOLDIR"); tooldir == "" {
63 tooldir = pathf("%s/pkg/tool/%s_%s", goroot, gohostos, gohostarch)
64 }
65+
66+ build_tooldir = pathf("%s/pkg/tool/native_native", goroot)
67 }
68
69 // compilerEnv returns a map from "goos/goarch" to the
Patrick Williams92b42cb2022-09-03 06:53:57 -050070@@ -468,8 +472,10 @@ func setup() {
Andrew Geissler6aa7eec2023-03-03 12:41:14 -060071 goosGoarch := pathf("%s/pkg/%s_%s", goroot, gohostos, gohostarch)
Andrew Geissler595f6302022-01-24 19:11:47 +000072 if rebuildall {
Andrew Geissler6aa7eec2023-03-03 12:41:14 -060073 xremoveall(goosGoarch)
Andrew Geissler595f6302022-01-24 19:11:47 +000074+ xremoveall(build_tooldir)
75 }
Andrew Geissler6aa7eec2023-03-03 12:41:14 -060076 xmkdirall(goosGoarch)
Andrew Geissler595f6302022-01-24 19:11:47 +000077+ xmkdirall(build_tooldir)
Andrew Geissler6aa7eec2023-03-03 12:41:14 -060078 xatexit(func() {
79 if files := xreaddir(goosGoarch); len(files) == 0 {
80 xremove(goosGoarch)
81@@ -1276,17 +1282,35 @@ func cmdbootstrap() {
Andrew Geissler595f6302022-01-24 19:11:47 +000082
83 var noBanner, noClean bool
84 var debug bool
85+ var hostOnly bool
86+ var targetOnly bool
87+ var toBuild = []string{"std", "cmd"}
88+
89 flag.BoolVar(&rebuildall, "a", rebuildall, "rebuild all")
90 flag.BoolVar(&debug, "d", debug, "enable debugging of bootstrap process")
91 flag.BoolVar(&noBanner, "no-banner", noBanner, "do not print banner")
92 flag.BoolVar(&noClean, "no-clean", noClean, "print deprecation warning")
93+ flag.BoolVar(&hostOnly, "host-only", hostOnly, "build only host binaries, not target")
94+ flag.BoolVar(&targetOnly, "target-only", targetOnly, "build only target binaries, not host")
95
96- xflagparse(0)
97+ xflagparse(-1)
98
99 if noClean {
100 xprintf("warning: --no-clean is deprecated and has no effect; use 'go install std cmd' instead\n")
101 }
102
103+ if hostOnly && targetOnly {
104+ fatalf("specify only one of --host-only or --target-only\n")
105+ }
106+ crossBuild = hostOnly || targetOnly
107+ if flag.NArg() > 0 {
108+ if crossBuild {
109+ toBuild = flag.Args()
110+ } else {
111+ fatalf("package names not permitted without --host-only or --target-only\n")
112+ }
113+ }
114+
115 // Set GOPATH to an internal directory. We shouldn't actually
116 // need to store files here, since the toolchain won't
117 // depend on modules outside of vendor directories, but if
Andrew Geissler6aa7eec2023-03-03 12:41:14 -0600118@@ -1354,9 +1378,14 @@ func cmdbootstrap() {
Andrew Geissler595f6302022-01-24 19:11:47 +0000119 xprintf("\n")
120 }
121
122- gogcflags = os.Getenv("GO_GCFLAGS") // we were using $BOOT_GO_GCFLAGS until now
Andrew Geissler6aa7eec2023-03-03 12:41:14 -0600123- setNoOpt()
Andrew Geissler595f6302022-01-24 19:11:47 +0000124- goldflags = os.Getenv("GO_LDFLAGS") // we were using $BOOT_GO_LDFLAGS until now
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.
128+ if !crossBuild {
129+ gogcflags = os.Getenv("GO_GCFLAGS") // we were using $BOOT_GO_GCFLAGS until now
Andrew Geissler6aa7eec2023-03-03 12:41:14 -0600130+ setNoOpt()
Andrew Geissler595f6302022-01-24 19:11:47 +0000131+ goldflags = os.Getenv("GO_LDFLAGS") // we were using $BOOT_GO_LDFLAGS until now
132+ }
133 goBootstrap := pathf("%s/go_bootstrap", tooldir)
Patrick Williams92b42cb2022-09-03 06:53:57 -0500134 cmdGo := pathf("%s/go", gorootBin)
Andrew Geissler595f6302022-01-24 19:11:47 +0000135 if debug {
Andrew Geissler6aa7eec2023-03-03 12:41:14 -0600136@@ -1385,7 +1414,11 @@ func cmdbootstrap() {
Andrew Geissler595f6302022-01-24 19:11:47 +0000137 xprintf("\n")
138 }
139 xprintf("Building Go toolchain2 using go_bootstrap and Go toolchain1.\n")
140- os.Setenv("CC", compilerEnvLookup(defaultcc, goos, goarch))
141+ if crossBuild {
142+ os.Setenv("CC", defaultcc[""])
143+ } else {
144+ os.Setenv("CC", compilerEnvLookup(defaultcc, goos, goarch))
145+ }
146 // Now that cmd/go is in charge of the build process, enable GOEXPERIMENT.
147 os.Setenv("GOEXPERIMENT", goexperiment)
Andrew Geissler6aa7eec2023-03-03 12:41:14 -0600148 goInstall(goBootstrap, toolchain...)
149@@ -1421,46 +1454,84 @@ func cmdbootstrap() {
150 copyfile(pathf("%s/compile3", tooldir), pathf("%s/compile", tooldir), writeExec)
Andrew Geissler595f6302022-01-24 19:11:47 +0000151 }
Andrew Geissler595f6302022-01-24 19:11:47 +0000152
153- if goos == oldgoos && goarch == oldgoarch {
154- // Common case - not setting up for cross-compilation.
155- timelog("build", "toolchain")
156- if vflag > 0 {
157- xprintf("\n")
158+ if crossBuild {
159+ gogcflags = os.Getenv("GO_GCFLAGS")
160+ goldflags = os.Getenv("GO_LDFLAGS")
161+ tool_files, _ := filepath.Glob(pathf("%s/*", tooldir))
162+ for _, f := range tool_files {
163+ copyfile(pathf("%s/%s", build_tooldir, filepath.Base(f)), f, writeExec)
164+ xremove(f)
165+ }
166+ os.Setenv("GOTOOLDIR", build_tooldir)
167+ goBootstrap = pathf("%s/go_bootstrap", build_tooldir)
168+ if hostOnly {
169+ timelog("build", "host toolchain")
170+ if vflag > 0 {
171+ xprintf("\n")
172+ }
173+ xprintf("Building %s for host, %s/%s.\n", strings.Join(toBuild, ","), goos, goarch)
174+ goInstall(goBootstrap, toBuild...)
175+ checkNotStale(goBootstrap, toBuild...)
176+ // Skip cmdGo staleness checks here, since we can't necessarily run the cmdGo binary
177+
178+ timelog("build", "target toolchain")
179+ if vflag > 0 {
180+ xprintf("\n")
181+ }
182+ } else if targetOnly {
183+ goos = oldgoos
184+ goarch = oldgoarch
185+ os.Setenv("GOOS", goos)
186+ os.Setenv("GOARCH", goarch)
187+ os.Setenv("CC", compilerEnvLookup(defaultcc, goos, goarch))
188+ xprintf("Building %s for target, %s/%s.\n", strings.Join(toBuild, ","), goos, goarch)
189+ goInstall(goBootstrap, toBuild...)
190+ checkNotStale(goBootstrap, toBuild...)
191+ // Skip cmdGo staleness checks here, since we can't run the target's cmdGo binary
192 }
193- xprintf("Building packages and commands for %s/%s.\n", goos, goarch)
194 } else {
195- // GOOS/GOARCH does not match GOHOSTOS/GOHOSTARCH.
196- // Finish GOHOSTOS/GOHOSTARCH installation and then
197- // run GOOS/GOARCH installation.
198- timelog("build", "host toolchain")
199- if vflag > 0 {
200- xprintf("\n")
Andrew Geissler595f6302022-01-24 19:11:47 +0000201+
202+ if goos == oldgoos && goarch == oldgoarch {
203+ // Common case - not setting up for cross-compilation.
204+ timelog("build", "toolchain")
205+ if vflag > 0 {
206+ xprintf("\n")
207+ }
208+ xprintf("Building packages and commands for %s/%s.\n", goos, goarch)
209+ } else {
210+ // GOOS/GOARCH does not match GOHOSTOS/GOHOSTARCH.
211+ // Finish GOHOSTOS/GOHOSTARCH installation and then
212+ // run GOOS/GOARCH installation.
213+ timelog("build", "host toolchain")
214+ if vflag > 0 {
215+ xprintf("\n")
216+ }
217+ xprintf("Building packages and commands for host, %s/%s.\n", goos, goarch)
218+ goInstall(goBootstrap, "std", "cmd")
219+ checkNotStale(goBootstrap, "std", "cmd")
220+ checkNotStale(cmdGo, "std", "cmd")
221+
222+ timelog("build", "target toolchain")
223+ if vflag > 0 {
224+ xprintf("\n")
225+ }
226+ goos = oldgoos
227+ goarch = oldgoarch
228+ os.Setenv("GOOS", goos)
229+ os.Setenv("GOARCH", goarch)
230+ os.Setenv("CC", compilerEnvLookup(defaultcc, goos, goarch))
231+ xprintf("Building packages and commands for target, %s/%s.\n", goos, goarch)
Patrick Williams03907ee2022-05-01 06:28:52 -0500232 }
233- xprintf("Building packages and commands for host, %s/%s.\n", goos, goarch)
Andrew Geissler595f6302022-01-24 19:11:47 +0000234 goInstall(goBootstrap, "std", "cmd")
235 checkNotStale(goBootstrap, "std", "cmd")
236 checkNotStale(cmdGo, "std", "cmd")
237
238- timelog("build", "target toolchain")
239- if vflag > 0 {
240- xprintf("\n")
Patrick Williams92b42cb2022-09-03 06:53:57 -0500241+ if debug {
242+ run("", ShowOutput|CheckExit, pathf("%s/compile", tooldir), "-V=full")
243+ run("", ShowOutput|CheckExit, pathf("%s/buildid", tooldir), pathf("%s/pkg/%s_%s/runtime/internal/sys.a", goroot, goos, goarch))
244+ checkNotStale(goBootstrap, append(toolchain, "runtime/internal/sys")...)
245+ copyfile(pathf("%s/compile4", tooldir), pathf("%s/compile", tooldir), writeExec)
246 }
Andrew Geissler595f6302022-01-24 19:11:47 +0000247- goos = oldgoos
248- goarch = oldgoarch
249- os.Setenv("GOOS", goos)
250- os.Setenv("GOARCH", goarch)
251- os.Setenv("CC", compilerEnvLookup(defaultcc, goos, goarch))
252- xprintf("Building packages and commands for target, %s/%s.\n", goos, goarch)
253- }
254- targets := []string{"std", "cmd"}
Andrew Geissler595f6302022-01-24 19:11:47 +0000255- goInstall(goBootstrap, targets...)
Andrew Geissler6aa7eec2023-03-03 12:41:14 -0600256- checkNotStale(goBootstrap, append(toolchain, "runtime/internal/sys")...)
Andrew Geissler595f6302022-01-24 19:11:47 +0000257- checkNotStale(goBootstrap, targets...)
258- checkNotStale(cmdGo, targets...)
259- if debug {
260- run("", ShowOutput|CheckExit, pathf("%s/compile", tooldir), "-V=full")
Andrew Geissler595f6302022-01-24 19:11:47 +0000261- checkNotStale(goBootstrap, append(toolchain, "runtime/internal/sys")...)
262- copyfile(pathf("%s/compile4", tooldir), pathf("%s/compile", tooldir), writeExec)
263 }
264
265 // Check that there are no new files in $GOROOT/bin other than
Andrew Geissler6aa7eec2023-03-03 12:41:14 -0600266@@ -1477,8 +1548,11 @@ func cmdbootstrap() {
Andrew Geissler595f6302022-01-24 19:11:47 +0000267 }
268 }
269
270- // Remove go_bootstrap now that we're done.
271- xremove(pathf("%s/go_bootstrap", tooldir))
272+ // Except that for split host/target cross-builds, we need to
273+ // keep it.
274+ if !crossBuild {
275+ xremove(pathf("%s/go_bootstrap", tooldir))
276+ }
277
278 if goos == "android" {
279 // Make sure the exec wrapper will sync a fresh $GOROOT to the device.
Andrew Geissler6aa7eec2023-03-03 12:41:14 -0600280--
2812.30.2
282