From b4ba7eb00dc80626a79ed036f632c4dd7750cba7 Mon Sep 17 00:00:00 2001 From: Cerales Date: Thu, 11 Jul 2013 19:49:54 +1000 Subject: [PATCH] support static_embed when cross-compiling. Implemented with a new GOARCH flag and appropriately setting the GOARCH and GOOS variables when building the genfileembed tool. Change-Id: I2a3f4ad08496ad03995e67da389d125388d643eb --- make.go | 43 ++++++++++++++++++++++++++++++++----------- 1 file changed, 32 insertions(+), 11 deletions(-) diff --git a/make.go b/make.go index 8ffa19e75..0d72af45b 100644 --- a/make.go +++ b/make.go @@ -51,6 +51,7 @@ var ( targets = flag.String("targets", "", "Optional comma-separated list of targets (i.e go packages) to build and install. Empty means all. Example: camlistore.org/server/camlistored,camlistore.org/cmd/camput") quiet = flag.Bool("quiet", false, "Don't print anything unless there's a failure.") ifModsSince = flag.Int64("if_mods_since", 0, "If non-zero return immediately without building if there aren't any filesystem modifications past this time (in unix seconds)") + buildARCH = flag.String("arch", runtime.GOARCH, "Architecture to build for.") buildOS = flag.String("os", runtime.GOOS, "Operating system to build for.") ) @@ -73,16 +74,10 @@ func main() { } verifyCamlistoreRoot(camRoot) - if runtime.GOOS != *buildOS { + if runtime.GOOS != *buildOS || runtime.GOARCH != *buildARCH { if *wantSQLite { log.Fatalf("SQLite isn't available when cross-compiling to another OS. Set --sqlite=false.") } - // TODO(bradfitz): we can fix this one, though, by - // building genfileembed with the GOOS of the host - // instead: - if *embedResources { - log.Fatalf("Due to a bug, can't currently cross-compile and also embed resources. For now, set --embed_static=false") - } } sql := *wantSQLite && haveSQLite() @@ -247,6 +242,11 @@ func main() { } } +// Create an environment variable of the form key=value. +func envPair(key, value string) string { + return fmt.Sprintf("%s=%s", key, value) +} + // cleanGoEnv returns a copy of the current environment with GOPATH and GOBIN removed. func cleanGoEnv() (clean []string) { for _, env := range os.Environ() { @@ -256,11 +256,27 @@ func cleanGoEnv() (clean []string) { clean = append(clean, env) } if *buildOS != runtime.GOOS { - clean = append(clean, "GOOS="+*buildOS) + clean = append(clean, envPair("GOOS", *buildOS)) + } + if *buildARCH != runtime.GOARCH { + clean = append(clean, envPair("GOARCH", *buildARCH)) } return } +// setEnv sets the given key & value in the provided environment. +// Each value in the env list should be of the form key=value. +func setEnv(env []string, key, value string) []string { + for i, s := range env { + if strings.HasPrefix(s, fmt.Sprintf("%s=", key)) { + env[i] = envPair(key, value) + return env + } + } + env = append(env, envPair(key, value)) + return env +} + // buildSrcPath returns the full path concatenation // of buildSrcDir with fromSrc. func buildSrcPath(fromSrc string) string { @@ -319,11 +335,16 @@ func buildGenfileembed() error { filepath.FromSlash("camlistore.org/pkg/fileembed/genfileembed"), ) cmd := exec.Command("go", args...) + // We don't even need to set GOBIN as it defaults to $GOPATH/bin // and that is where we want genfileembed to go. - cmd.Env = append(cleanGoEnv(), - "GOPATH="+buildGoPath, - ) + // Here we replace the GOOS and GOARCH valuesfrom the env with the host OS, + // to support cross-compiling. + cmd.Env = cleanGoEnv() + cmd.Env = setEnv(cmd.Env, "GOPATH", buildGoPath) + cmd.Env = setEnv(cmd.Env, "GOOS", runtime.GOOS) + cmd.Env = setEnv(cmd.Env, "GOARCH", runtime.GOARCH) + cmd.Stdout = os.Stdout cmd.Stderr = os.Stderr if *verbose {