blob: 9fe699628463ef3320c0802da68af2124f21d1f7 [file] [log] [blame]
Brad Bishop316dfdd2018-06-25 12:45:53 -04001From 2f07af34697c61decdcfa5b11434451fbcf37704 Mon Sep 17 00:00:00 2001
2From: Matt Madison <matt@madison.systems>
3Date: Sat, 17 Feb 2018 10:03:48 -0800
4Subject: [PATCH 6/9] cmd/dist: separate host and target builds
5
6Change the dist tool to allow for OE-style cross-
7and cross-canadian builds:
8
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
13 built.
14
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.
19
20 - go_bootstrap is not automatically removed after the build,
21 so it can be reused later (e.g., building both static and
22 shared runtime).
23
24Note that for --host-only builds, it would be nice to specify
25just the "cmd" package to build only the go commands/tools,
26the staleness checks in the dist tool will fail if the "std"
27library has not also been built. So host-only builds have to
28build everything anyway.
29
30Upstream-Status: Inappropriate [OE specific]
31
32Signed-off-by: Matt Madison <matt@madison.systems>
33
34more dist cleanup
35---
36 src/cmd/dist/build.go | 149 +++++++++++++++++++++++++++++++++++++-------------
37 1 file changed, 111 insertions(+), 38 deletions(-)
38
39diff --git a/src/cmd/dist/build.go b/src/cmd/dist/build.go
40index afc615b5c2..36262665b2 100644
41--- a/src/cmd/dist/build.go
42+++ b/src/cmd/dist/build.go
43@@ -38,6 +38,7 @@ var (
44 goldflags string
45 workdir string
46 tooldir string
47+ build_tooldir string
48 oldgoos string
49 oldgoarch string
50 exe string
51@@ -49,6 +50,7 @@ var (
52
53 rebuildall bool
54 defaultclang bool
55+ crossBuild bool
56
57 vflag int // verbosity
58 )
59@@ -223,6 +225,8 @@ func xinit() {
60 if tooldir = os.Getenv("GOTOOLDIR"); tooldir == "" {
61 tooldir = pathf("%s/pkg/tool/%s_%s", goroot, gohostos, gohostarch)
62 }
63+ build_tooldir = pathf("%s/pkg/tool/native_native", goroot)
64+
65 }
66
67 // compilerEnv returns a map from "goos/goarch" to the
68@@ -252,7 +256,6 @@ func compilerEnv(envName, def string) map[string]string {
69 if gohostos != goos || gohostarch != goarch {
70 m[gohostos+"/"+gohostarch] = m[""]
71 }
72- m[""] = env
73 }
74
75 for _, goos := range okgoos {
76@@ -479,8 +482,10 @@ func setup() {
77 // We keep it in pkg/, just like the object directory above.
78 if rebuildall {
79 xremoveall(tooldir)
80+ xremoveall(build_tooldir)
81 }
82 xmkdirall(tooldir)
83+ xmkdirall(build_tooldir)
84
85 // Remove tool binaries from before the tool/gohostos_gohostarch
86 xremoveall(pathf("%s/bin/tool", goroot))
87@@ -1130,11 +1135,29 @@ func cmdbootstrap() {
88
89 var noBanner bool
90 var debug bool
91+ var hostOnly bool
92+ var targetOnly bool
93+ var toBuild = []string { "std", "cmd" }
94+
95 flag.BoolVar(&rebuildall, "a", rebuildall, "rebuild all")
96 flag.BoolVar(&debug, "d", debug, "enable debugging of bootstrap process")
97 flag.BoolVar(&noBanner, "no-banner", noBanner, "do not print banner")
98+ flag.BoolVar(&hostOnly, "host-only", hostOnly, "build only host binaries, not target")
99+ flag.BoolVar(&targetOnly, "target-only", targetOnly, "build only target binaries, not host")
100
101- xflagparse(0)
102+ xflagparse(-1)
103+
104+ if (hostOnly && targetOnly) {
105+ fatalf("specify only one of --host-only or --target-only\n")
106+ }
107+ crossBuild = hostOnly || targetOnly
108+ if flag.NArg() > 0 {
109+ if crossBuild {
110+ toBuild = flag.Args()
111+ } else {
112+ fatalf("package names not permitted without --host-only or --target-only\n")
113+ }
114+ }
115
116 if debug {
117 // cmd/buildid is used in debug mode.
118@@ -1182,8 +1205,13 @@ func cmdbootstrap() {
119 xprintf("\n")
120 }
121
122- gogcflags = os.Getenv("GO_GCFLAGS") // we were using $BOOT_GO_GCFLAGS until now
123- goldflags = os.Getenv("GO_LDFLAGS")
124+ // For split host/target cross/cross-canadian builds, we don't
125+ // want to be setting these flags until after we have compiled
126+ // the toolchain that runs on the build host.
127+ if ! crossBuild {
128+ gogcflags = os.Getenv("GO_GCFLAGS") // we were using $BOOT_GO_GCFLAGS until now
129+ goldflags = os.Getenv("GO_LDFLAGS")
130+ }
131 goBootstrap := pathf("%s/go_bootstrap", tooldir)
132 cmdGo := pathf("%s/go", gobin)
133 if debug {
134@@ -1212,7 +1240,11 @@ func cmdbootstrap() {
135 xprintf("\n")
136 }
137 xprintf("Building Go toolchain2 using go_bootstrap and Go toolchain1.\n")
138- os.Setenv("CC", compilerEnvLookup(defaultcc, goos, goarch))
139+ if crossBuild {
140+ os.Setenv("CC", defaultcc[""])
141+ } else {
142+ os.Setenv("CC", compilerEnvLookup(defaultcc, goos, goarch))
143+ }
144 goInstall(goBootstrap, append([]string{"-i"}, toolchain...)...)
145 if debug {
146 run("", ShowOutput|CheckExit, pathf("%s/compile", tooldir), "-V=full")
147@@ -1249,45 +1281,82 @@ func cmdbootstrap() {
148 }
149 checkNotStale(goBootstrap, append(toolchain, "runtime/internal/sys")...)
150
151- if goos == oldgoos && goarch == oldgoarch {
152- // Common case - not setting up for cross-compilation.
153- timelog("build", "toolchain")
154- if vflag > 0 {
155- xprintf("\n")
156+ if crossBuild {
157+ gogcflags = os.Getenv("GO_GCFLAGS")
158+ goldflags = os.Getenv("GO_LDFLAGS")
159+ tool_files, _ := filepath.Glob(pathf("%s/*", tooldir))
160+ for _, f := range tool_files {
161+ copyfile(pathf("%s/%s", build_tooldir, filepath.Base(f)), f, writeExec)
162+ xremove(f)
163+ }
164+ os.Setenv("GOTOOLDIR", build_tooldir)
165+ goBootstrap = pathf("%s/go_bootstrap", build_tooldir)
166+ if hostOnly {
167+ timelog("build", "host toolchain")
168+ if vflag > 0 {
169+ xprintf("\n")
170+ }
171+ xprintf("Building %s for host, %s/%s.\n", strings.Join(toBuild, ","), goos, goarch)
172+ goInstall(goBootstrap, toBuild...)
173+ checkNotStale(goBootstrap, toBuild...)
174+ // Skip cmdGo staleness checks here, since we can't necessarily run the cmdGo binary
175+
176+ timelog("build", "target toolchain")
177+ if vflag > 0 {
178+ xprintf("\n")
179+ }
180+ } else if targetOnly {
181+ goos = oldgoos
182+ goarch = oldgoarch
183+ os.Setenv("GOOS", goos)
184+ os.Setenv("GOARCH", goarch)
185+ os.Setenv("CC", compilerEnvLookup(defaultcc, goos, goarch))
186+ xprintf("Building %s for target, %s/%s.\n", strings.Join(toBuild, ","), goos, goarch)
187+ goInstall(goBootstrap, toBuild...)
188+ checkNotStale(goBootstrap, toBuild...)
189+ // Skip cmdGo staleness checks here, since we can't run the target's cmdGo binary
190 }
191- xprintf("Building packages and commands for %s/%s.\n", goos, goarch)
192 } else {
193- // GOOS/GOARCH does not match GOHOSTOS/GOHOSTARCH.
194- // Finish GOHOSTOS/GOHOSTARCH installation and then
195- // run GOOS/GOARCH installation.
196- timelog("build", "host toolchain")
197- if vflag > 0 {
198- xprintf("\n")
199+ if goos == oldgoos && goarch == oldgoarch {
200+ // Common case - not setting up for cross-compilation.
201+ timelog("build", "toolchain")
202+ if vflag > 0 {
203+ xprintf("\n")
204+ }
205+ xprintf("Building packages and commands for %s/%s.\n", goos, goarch)
206+ } else {
207+ // GOOS/GOARCH does not match GOHOSTOS/GOHOSTARCH.
208+ // Finish GOHOSTOS/GOHOSTARCH installation and then
209+ // run GOOS/GOARCH installation.
210+ timelog("build", "host toolchain")
211+ if vflag > 0 {
212+ xprintf("\n")
213+ }
214+ xprintf("Building packages and commands for host, %s/%s.\n", goos, goarch)
215+ goInstall(goBootstrap, "std", "cmd")
216+ checkNotStale(goBootstrap, "std", "cmd")
217+ checkNotStale(cmdGo, "std", "cmd")
218+
219+ timelog("build", "target toolchain")
220+ if vflag > 0 {
221+ xprintf("\n")
222+ }
223+ goos = oldgoos
224+ goarch = oldgoarch
225+ os.Setenv("GOOS", goos)
226+ os.Setenv("GOARCH", goarch)
227+ os.Setenv("CC", compilerEnvLookup(defaultcc, goos, goarch))
228+ xprintf("Building packages and commands for target, %s/%s.\n", goos, goarch)
229 }
230- xprintf("Building packages and commands for host, %s/%s.\n", goos, goarch)
231 goInstall(goBootstrap, "std", "cmd")
232 checkNotStale(goBootstrap, "std", "cmd")
233 checkNotStale(cmdGo, "std", "cmd")
234-
235- timelog("build", "target toolchain")
236- if vflag > 0 {
237- xprintf("\n")
238+ if debug {
239+ run("", ShowOutput|CheckExit, pathf("%s/compile", tooldir), "-V=full")
240+ run("", ShowOutput|CheckExit, pathf("%s/buildid", tooldir), pathf("%s/pkg/%s_%s/runtime/internal/sys.a", goroot, goos, goarch))
241+ checkNotStale(goBootstrap, append(toolchain, "runtime/internal/sys")...)
242+ copyfile(pathf("%s/compile4", tooldir), pathf("%s/compile", tooldir), writeExec)
243 }
244- goos = oldgoos
245- goarch = oldgoarch
246- os.Setenv("GOOS", goos)
247- os.Setenv("GOARCH", goarch)
248- os.Setenv("CC", compilerEnvLookup(defaultcc, goos, goarch))
249- xprintf("Building packages and commands for target, %s/%s.\n", goos, goarch)
250- }
251- goInstall(goBootstrap, "std", "cmd")
252- checkNotStale(goBootstrap, "std", "cmd")
253- checkNotStale(cmdGo, "std", "cmd")
254- if debug {
255- run("", ShowOutput|CheckExit, pathf("%s/compile", tooldir), "-V=full")
256- run("", ShowOutput|CheckExit, pathf("%s/buildid", tooldir), pathf("%s/pkg/%s_%s/runtime/internal/sys.a", goroot, goos, goarch))
257- checkNotStale(goBootstrap, append(toolchain, "runtime/internal/sys")...)
258- copyfile(pathf("%s/compile4", tooldir), pathf("%s/compile", tooldir), writeExec)
259 }
260
261 // Check that there are no new files in $GOROOT/bin other than
262@@ -1305,7 +1374,11 @@ func cmdbootstrap() {
263 }
264
265 // Remove go_bootstrap now that we're done.
266- xremove(pathf("%s/go_bootstrap", tooldir))
267+ // Except that for split host/target cross-builds, we need to
268+ // keep it.
269+ if ! crossBuild {
270+ xremove(pathf("%s/go_bootstrap", tooldir))
271+ }
272
273 // Print trailing banner unless instructed otherwise.
274 if !noBanner {
275--
2762.14.1
277