Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame^] | 1 | From 7a191e5191c8b813e929caedb3f3918bb08692a1 Mon Sep 17 00:00:00 2001 |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 2 | From: Alex Kube <alexander.j.kube@gmail.com> |
| 3 | Date: Wed, 23 Oct 2019 21:18:12 +0430 |
Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame^] | 4 | Subject: [PATCH 5/9] cmd/dist: separate host and target builds |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 5 | |
| 6 | Upstream-Status: Inappropriate [OE specific] |
| 7 | |
| 8 | Change the dist tool to allow for OE-style cross- |
| 9 | and 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 | |
| 26 | Note that for --host-only builds, it would be nice to specify |
| 27 | just the "cmd" package to build only the go commands/tools, |
| 28 | the staleness checks in the dist tool will fail if the "std" |
| 29 | library has not also been built. So host-only builds have to |
| 30 | build everything anyway. |
| 31 | |
| 32 | Adapted to Go 1.13 from patches originally submitted to |
| 33 | the meta/recipes-devtools/go tree by |
| 34 | Matt Madison <matt@madison.systems>. |
| 35 | |
| 36 | Signed-off-by: Alexander J Kube <alexander.j.kube@gmail.com> |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 37 | --- |
Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame^] | 38 | src/cmd/dist/build.go | 152 +++++++++++++++++++++++++++++++----------- |
| 39 | 1 file changed, 113 insertions(+), 39 deletions(-) |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 40 | |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 41 | diff --git a/src/cmd/dist/build.go b/src/cmd/dist/build.go |
Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame^] | 42 | index 5d31718..1c7f308 100644 |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 43 | --- a/src/cmd/dist/build.go |
| 44 | +++ b/src/cmd/dist/build.go |
Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame^] | 45 | @@ -44,6 +44,7 @@ var ( |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 46 | goexperiment string |
| 47 | workdir string |
| 48 | tooldir string |
| 49 | + build_tooldir string |
| 50 | oldgoos string |
| 51 | oldgoarch string |
| 52 | exe string |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 53 | @@ -55,6 +56,7 @@ var ( |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 54 | rebuildall bool |
| 55 | defaultclang bool |
Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame^] | 56 | noOpt bool |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 57 | + crossBuild bool |
| 58 | |
| 59 | vflag int // verbosity |
| 60 | ) |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 61 | @@ -267,6 +269,8 @@ func xinit() { |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 62 | 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 Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 70 | @@ -468,8 +472,10 @@ func setup() { |
Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame^] | 71 | goosGoarch := pathf("%s/pkg/%s_%s", goroot, gohostos, gohostarch) |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 72 | if rebuildall { |
Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame^] | 73 | xremoveall(goosGoarch) |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 74 | + xremoveall(build_tooldir) |
| 75 | } |
Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame^] | 76 | xmkdirall(goosGoarch) |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 77 | + xmkdirall(build_tooldir) |
Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame^] | 78 | xatexit(func() { |
| 79 | if files := xreaddir(goosGoarch); len(files) == 0 { |
| 80 | xremove(goosGoarch) |
| 81 | @@ -1276,17 +1282,35 @@ func cmdbootstrap() { |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 82 | |
| 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 Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame^] | 118 | @@ -1354,9 +1378,14 @@ func cmdbootstrap() { |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 119 | xprintf("\n") |
| 120 | } |
| 121 | |
| 122 | - gogcflags = os.Getenv("GO_GCFLAGS") // we were using $BOOT_GO_GCFLAGS until now |
Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame^] | 123 | - setNoOpt() |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 124 | - 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 Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame^] | 130 | + setNoOpt() |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 131 | + goldflags = os.Getenv("GO_LDFLAGS") // we were using $BOOT_GO_LDFLAGS until now |
| 132 | + } |
| 133 | goBootstrap := pathf("%s/go_bootstrap", tooldir) |
Patrick Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 134 | cmdGo := pathf("%s/go", gorootBin) |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 135 | if debug { |
Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame^] | 136 | @@ -1385,7 +1414,11 @@ func cmdbootstrap() { |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 137 | 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 Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame^] | 148 | goInstall(goBootstrap, toolchain...) |
| 149 | @@ -1421,46 +1454,84 @@ func cmdbootstrap() { |
| 150 | copyfile(pathf("%s/compile3", tooldir), pathf("%s/compile", tooldir), writeExec) |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 151 | } |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 152 | |
| 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 Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 201 | + |
| 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 Williams | 03907ee | 2022-05-01 06:28:52 -0500 | [diff] [blame] | 232 | } |
| 233 | - xprintf("Building packages and commands for host, %s/%s.\n", goos, goarch) |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 234 | 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 Williams | 92b42cb | 2022-09-03 06:53:57 -0500 | [diff] [blame] | 241 | + 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 Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 247 | - 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 Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 255 | - goInstall(goBootstrap, targets...) |
Andrew Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame^] | 256 | - checkNotStale(goBootstrap, append(toolchain, "runtime/internal/sys")...) |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 257 | - checkNotStale(goBootstrap, targets...) |
| 258 | - checkNotStale(cmdGo, targets...) |
| 259 | - if debug { |
| 260 | - run("", ShowOutput|CheckExit, pathf("%s/compile", tooldir), "-V=full") |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 261 | - 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 Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame^] | 266 | @@ -1477,8 +1548,11 @@ func cmdbootstrap() { |
Andrew Geissler | 595f630 | 2022-01-24 19:11:47 +0000 | [diff] [blame] | 267 | } |
| 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 Geissler | 6aa7eec | 2023-03-03 12:41:14 -0600 | [diff] [blame^] | 280 | -- |
| 281 | 2.30.2 |
| 282 | |