From 87f00d76c4815875b1c1100c5cb1bf77b6db54ef Mon Sep 17 00:00:00 2001 From: James Tucker Date: Thu, 16 May 2024 10:53:44 -0700 Subject: [PATCH] tool/gocross: treat empty GOOS/GOARCH as native GOOS/GOARCH Tracking down the side effect can otherwise be a pain, for example on Darwin an empty GOOS resulted in CGO being implicitly disabled. The user intended for `export GOOS=` to act like unset, and while this is a misunderstanding, the main toolchain would treat it this way. Fixes tailscale/corp#20059 Signed-off-by: James Tucker --- tool/gocross/autoflags.go | 5 +-- tool/gocross/autoflags_test.go | 58 ++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/tool/gocross/autoflags.go b/tool/gocross/autoflags.go index 6bf04d3cc..fd58c1fcb 100644 --- a/tool/gocross/autoflags.go +++ b/tool/gocross/autoflags.go @@ -4,6 +4,7 @@ package main import ( + "cmp" "fmt" "runtime" "strings" @@ -32,8 +33,8 @@ func autoflagsForTest(argv []string, env *Environment, goroot, nativeGOOS, nativ subcommand = "" cc = "cc" - targetOS = env.Get("GOOS", nativeGOOS) - targetArch = env.Get("GOARCH", nativeGOARCH) + targetOS = cmp.Or(env.Get("GOOS", ""), nativeGOOS) + targetArch = cmp.Or(env.Get("GOARCH", ""), nativeGOARCH) buildFlags = []string{"-trimpath"} cgoCflags = []string{"-O3", "-std=gnu11"} cgoLdflags []string diff --git a/tool/gocross/autoflags_test.go b/tool/gocross/autoflags_test.go index 9f24a131b..438198de7 100644 --- a/tool/gocross/autoflags_test.go +++ b/tool/gocross/autoflags_test.go @@ -275,6 +275,64 @@ GOMIPS=softfloat (was ) GOOS=darwin (was ) GOROOT=/goroot (was ) GOTOOLCHAIN=local (was ) +TS_LINK_FAIL_REFLECT=0 (was )`, + wantArgv: []string{ + "gocross", "build", + "-trimpath", + "-tags=tailscale_go,omitidna,omitpemdecrypt", + "-ldflags", "-X tailscale.com/version.longStamp=1.2.3-long -X tailscale.com/version.shortStamp=1.2.3 -X tailscale.com/version.gitCommitStamp=abcd -X tailscale.com/version.extraGitCommitStamp=defg", + "./cmd/tailcontrol", + }, + }, + { + name: "darwin_arm64_to_darwin_arm64_empty_goos", + argv: []string{"gocross", "build", "./cmd/tailcontrol"}, + env: map[string]string{ + "GOOS": "", + }, + goroot: "/goroot", + nativeGOOS: "darwin", + nativeGOARCH: "arm64", + + envDiff: `CC=cc (was ) +CGO_CFLAGS=-O3 -std=gnu11 (was ) +CGO_ENABLED=1 (was ) +CGO_LDFLAGS= (was ) +GOARCH=arm64 (was ) +GOARM=5 (was ) +GOMIPS=softfloat (was ) +GOOS=darwin (was ) +GOROOT=/goroot (was ) +GOTOOLCHAIN=local (was ) +TS_LINK_FAIL_REFLECT=0 (was )`, + wantArgv: []string{ + "gocross", "build", + "-trimpath", + "-tags=tailscale_go,omitidna,omitpemdecrypt", + "-ldflags", "-X tailscale.com/version.longStamp=1.2.3-long -X tailscale.com/version.shortStamp=1.2.3 -X tailscale.com/version.gitCommitStamp=abcd -X tailscale.com/version.extraGitCommitStamp=defg", + "./cmd/tailcontrol", + }, + }, + { + name: "darwin_arm64_to_darwin_arm64_empty_goarch", + argv: []string{"gocross", "build", "./cmd/tailcontrol"}, + env: map[string]string{ + "GOARCH": "", + }, + goroot: "/goroot", + nativeGOOS: "darwin", + nativeGOARCH: "arm64", + + envDiff: `CC=cc (was ) +CGO_CFLAGS=-O3 -std=gnu11 (was ) +CGO_ENABLED=1 (was ) +CGO_LDFLAGS= (was ) +GOARCH=arm64 (was ) +GOARM=5 (was ) +GOMIPS=softfloat (was ) +GOOS=darwin (was ) +GOROOT=/goroot (was ) +GOTOOLCHAIN=local (was ) TS_LINK_FAIL_REFLECT=0 (was )`, wantArgv: []string{ "gocross", "build",