From 12bf824f8b7e85f05434aa00e866e883a551aaeb Mon Sep 17 00:00:00 2001
From: Alex Kube <alexander.j.kube@gmail.com>
Date: Fri, 28 Feb 2025 19:09:17 -0800
Subject: [PATCH 05/11] cmd/dist: separate host and target builds

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>.

Rework the patch to avoid identation, it breaks formatting rules but
makes the changes more obvious and maintainable.
Jose Quaresma <jose.quaresma@foundries.io>
Richard Purdie <richard.purdie@linuxfoundation.org>

Upstream-Status: Inappropriate [OE specific]

Signed-off-by: Alexander J Kube <alexander.j.kube@gmail.com>
Signed-off-by: Jose Quaresma <jose.quaresma@foundries.io>

Rebase to 1.24.0
Signed-off-by: Hongxu Jia <hongxu.jia@windriver.com>
---
 src/cmd/dist/build.go | 76 ++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 75 insertions(+), 1 deletion(-)

diff --git a/src/cmd/dist/build.go b/src/cmd/dist/build.go
index b62e518030..0c54d82300 100644
--- a/src/cmd/dist/build.go
+++ b/src/cmd/dist/build.go
@@ -50,6 +50,7 @@ var (
 	gofips140        string
 	workdir          string
 	tooldir          string
+	build_tooldir    string
 	oldgoos          string
 	oldgoarch        string
 	oldgocache       string
@@ -62,6 +63,7 @@ var (
 	rebuildall bool
 	noOpt      bool
 	isRelease  bool
+	crossBuild bool
 
 	vflag int // verbosity
 )
@@ -278,6 +280,8 @@ func xinit() {
 	goversion := findgoversion()
 	isRelease = (strings.HasPrefix(goversion, "release.") || strings.HasPrefix(goversion, "go")) &&
 		!strings.Contains(goversion, "devel")
+
+	build_tooldir = pathf("%s/pkg/tool/native_native", goroot)
 }
 
 // compilerEnv returns a map from "goos/goarch" to the
@@ -543,8 +547,10 @@ func setup() {
 	goosGoarch := pathf("%s/pkg/%s_%s", goroot, gohostos, gohostarch)
 	if rebuildall {
 		xremoveall(goosGoarch)
+		xremoveall(build_tooldir)
 	}
 	xmkdirall(goosGoarch)
+	xmkdirall(build_tooldir)
 	xatexit(func() {
 		if files := xreaddir(goosGoarch); len(files) == 0 {
 			xremove(goosGoarch)
@@ -1420,14 +1426,20 @@ func cmdbootstrap() {
 	defer timelog("end", "dist bootstrap")
 
 	var debug, distpack, force, noBanner, noClean 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(&distpack, "distpack", distpack, "write distribution files to pkg/distpack")
 	flag.BoolVar(&force, "force", force, "build even if the port is marked as broken")
 	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")
@@ -1439,6 +1451,18 @@ func cmdbootstrap() {
 			"Use the -force flag to build anyway.\n", goos, goarch)
 	}
 
+	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
@@ -1517,9 +1541,14 @@ func cmdbootstrap() {
 		xprintf("\n")
 	}
 
+	// 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
 	setNoOpt()
 	goldflags = os.Getenv("GO_LDFLAGS") // we were using $BOOT_GO_LDFLAGS until now
+}
 	goBootstrap := pathf("%s/go_bootstrap", tooldir)
 	if debug {
 		run("", ShowOutput|CheckExit, pathf("%s/compile", tooldir), "-V=full")
@@ -1547,7 +1576,11 @@ func cmdbootstrap() {
 		xprintf("\n")
 	}
 	xprintf("Building Go toolchain2 using go_bootstrap and Go toolchain1.\n")
+if !crossBuild {
 	os.Setenv("CC", compilerEnvLookup("CC", defaultcc, goos, goarch))
+} else {
+		os.Setenv("CC", defaultcc[""])
+}
 	// Now that cmd/go is in charge of the build process, enable GOEXPERIMENT.
 	os.Setenv("GOEXPERIMENT", goexperiment)
 	// No need to enable PGO for toolchain2.
@@ -1600,6 +1633,7 @@ func cmdbootstrap() {
 		os.Setenv("GOCACHE", oldgocache)
 	}
 
+if !crossBuild {
 	if goos == oldgoos && goarch == oldgoarch {
 		// Common case - not setting up for cross-compilation.
 		timelog("build", "toolchain")
@@ -1643,6 +1677,42 @@ func cmdbootstrap() {
 		checkNotStale(toolenv(), goBootstrap, toolchain...)
 		copyfile(pathf("%s/compile4", tooldir), pathf("%s/compile", tooldir), writeExec)
 	}
+} else {
+		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(toolenv(), goBootstrap, toBuild...)
+			checkNotStale(toolenv(), 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("CC", defaultcc, goos, goarch))
+			xprintf("Building %s for target, %s/%s.\n", strings.Join(toBuild, ","), goos, goarch)
+			goInstall(toolenv(), goBootstrap, toBuild...)
+			checkNotStale(toolenv(), goBootstrap, toBuild...)
+			// Skip cmdGo staleness checks here, since we can't run the target's cmdGo binary
+		}
+}
 
 	// Check that there are no new files in $GOROOT/bin other than
 	// go and gofmt and $GOOS_$GOARCH (target bin when cross-compiling).
@@ -1665,8 +1735,12 @@ func cmdbootstrap() {
 		}
 	}
 
+	// Except that for split host/target cross-builds, we need to
+	// keep it.
+if !crossBuild {
 	// Remove go_bootstrap now that we're done.
 	xremove(pathf("%s/go_bootstrap"+exe, tooldir))
+}
 
 	if goos == "android" {
 		// Make sure the exec wrapper will sync a fresh $GOROOT to the device.
-- 
2.25.1

