Skip to content

Commit

Permalink
chore: remove custom go toolchain
Browse files Browse the repository at this point in the history
Change-Id: Ic5e4198e3cb42225deea908169b91b157c398815
Signed-off-by: Thomas Kosiewski <[email protected]>
  • Loading branch information
ThomasK33 committed Jan 21, 2025
1 parent b53d914 commit f69567d
Show file tree
Hide file tree
Showing 8 changed files with 434 additions and 740 deletions.
637 changes: 340 additions & 297 deletions .github/workflows/test.yml

Large diffs are not rendered by default.

80 changes: 42 additions & 38 deletions scripts/check_license_headers.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,56 +7,60 @@
# directory tree have a correct-looking Tailscale license header.

check_file() {
got=$1
got=$1

want=$(cat <<EOF
want=$(
cat <<EOF
// Copyright (c) Tailscale Inc & AUTHORS
// SPDX-License-Identifier: BSD-3-Clause
EOF
)
if [ "$got" = "$want" ]; then
return 0
fi
return 1
)
if [ "$got" = "$want" ]; then
return 0
fi
return 1
}

if [ $# != 1 ]; then
echo "Usage: $0 rootdir" >&2
exit 1
echo "Usage: $0 rootdir" >&2
exit 1
fi

fail=0
for file in $(find $1 -name '*.go' -not -path '*/.git/*'); do
case $file in
$1/tempfork/*)
# Skip, tempfork of third-party code
;;
$1/wgengine/router/ifconfig_windows.go)
# WireGuard copyright.
;;
$1/cmd/tailscale/cli/authenticode_windows.go)
# WireGuard copyright.
;;
*_string.go)
# Generated file from go:generate stringer
;;
$1/control/controlbase/noiseexplorer_test.go)
# Noiseexplorer.com copyright.
;;
*/zsyscall_windows.go)
# Generated syscall wrappers
;;
*)
header="$(head -2 $file)"
if ! check_file "$header"; then
fail=1
echo "${file#$1/} doesn't have the right copyright header:"
echo "$header" | sed -e 's/^/ /g'
fi
;;
esac
case $file in
$1/tempfork/*)
# Skip, tempfork of third-party code
;;
$1/wgengine/router/ifconfig_windows.go)
# WireGuard copyright.
;;
$1/cmd/tailscale/cli/authenticode_windows.go)
# WireGuard copyright.
;;
*_string.go)
# Generated file from go:generate stringer
;;
$1/control/controlbase/noiseexplorer_test.go)
# Noiseexplorer.com copyright.
;;
*/zsyscall_windows.go)
# Generated syscall wrappers
;;
$1/wgengine/netstack/endpoint.go)
# gVisor copyright
;;
*)
header="$(head -2 $file)"
if ! check_file "$header"; then
fail=1
echo "${file#$1/} doesn't have the right copyright header:"
echo "$header" | sed -e 's/^/ /g'
fi
;;
esac
done

if [ $fail -ne 0 ]; then
exit 1
exit 1
fi
2 changes: 1 addition & 1 deletion tool/gocross/autoflags.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func autoflagsForTest(argv []string, env *Environment, goroot, nativeGOOS, nativ
cgoCflags = []string{"-O3", "-std=gnu11"}
cgoLdflags []string
ldflags []string
tags = []string{"tailscale_go"}
tags = []string{}
cgo = false
failReflect = false
)
Expand Down
26 changes: 12 additions & 14 deletions tool/gocross/autoflags_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ TS_LINK_FAIL_REFLECT=0 (was <nil>)`,
wantArgv: []string{
"gocross", "build",
"-trimpath",
"-tags=tailscale_go,osusergo,netgo",
"-tags=osusergo,netgo",
"-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 '-extldflags=-static'",
"./cmd/tailcontrol",
},
Expand All @@ -78,7 +78,7 @@ TS_LINK_FAIL_REFLECT=0 (was <nil>)`,
wantArgv: []string{
"gocross", "install",
"-trimpath",
"-tags=tailscale_go,osusergo,netgo",
"-tags=osusergo,netgo",
"-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 '-extldflags=-static'",
"./cmd/tailcontrol",
},
Expand Down Expand Up @@ -106,7 +106,7 @@ TS_LINK_FAIL_REFLECT=0 (was <nil>)`,
wantArgv: []string{
"gocross", "build",
"-trimpath",
"-tags=tailscale_go,osusergo,netgo",
"-tags=osusergo,netgo",
"-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 '-extldflags=-static'",
"./cmd/tailcontrol",
},
Expand Down Expand Up @@ -134,7 +134,6 @@ TS_LINK_FAIL_REFLECT=0 (was <nil>)`,
wantArgv: []string{
"gocross", "build",
"-trimpath",
"-tags=tailscale_go",
"-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",
},
Expand All @@ -159,7 +158,7 @@ TS_LINK_FAIL_REFLECT=0 (was <nil>)`,
wantArgv: []string{
"gocross", "test",
"-trimpath",
"-tags=tailscale_go,osusergo,netgo",
"-tags=osusergo,netgo",
"-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 '-extldflags=-static'",
"-race",
"./cmd/tailcontrol",
Expand Down Expand Up @@ -188,7 +187,6 @@ TS_LINK_FAIL_REFLECT=0 (was <nil>)`,
wantArgv: []string{
"gocross", "build",
"-trimpath",
"-tags=tailscale_go",
"-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 -H windows -s",
"./cmd/tailcontrol",
},
Expand All @@ -213,7 +211,7 @@ TS_LINK_FAIL_REFLECT=0 (was <nil>)`,
wantArgv: []string{
"gocross", "build",
"-trimpath",
"-tags=tailscale_go,omitidna,omitpemdecrypt",
"-tags=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",
},
Expand Down Expand Up @@ -241,7 +239,7 @@ TS_LINK_FAIL_REFLECT=0 (was <nil>)`,
wantArgv: []string{
"gocross", "build",
"-trimpath",
"-tags=tailscale_go,omitidna,omitpemdecrypt",
"-tags=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",
},
Expand Down Expand Up @@ -269,7 +267,7 @@ TS_LINK_FAIL_REFLECT=1 (was <nil>)`,
wantArgv: []string{
"gocross", "build",
"-trimpath",
"-tags=tailscale_go,omitidna,omitpemdecrypt",
"-tags=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",
},
Expand Down Expand Up @@ -301,7 +299,7 @@ TS_LINK_FAIL_REFLECT=0 (was <nil>)`,
wantArgv: []string{
"gocross", "build",
"-trimpath",
"-tags=tailscale_go,omitidna,omitpemdecrypt,ts_macext",
"-tags=omitidna,omitpemdecrypt,ts_macext",
"-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 -w",
"./cmd/tailcontrol",
},
Expand Down Expand Up @@ -333,7 +331,7 @@ TS_LINK_FAIL_REFLECT=1 (was <nil>)`,
wantArgv: []string{
"gocross", "build",
"-trimpath",
"-tags=tailscale_go,omitidna,omitpemdecrypt,ts_macext",
"-tags=omitidna,omitpemdecrypt,ts_macext",
"-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 -w",
"./cmd/tailcontrol",
},
Expand All @@ -358,7 +356,7 @@ TS_LINK_FAIL_REFLECT=0 (was <nil>)`,
wantArgv: []string{
"go", "build",
"-trimpath",
"-tags=tailscale_go,osusergo,netgo",
"-tags=osusergo,netgo",
"-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 '-extldflags=-static'",
"./cmd/tailcontrol",
},
Expand All @@ -383,7 +381,7 @@ TS_LINK_FAIL_REFLECT=0 (was <nil>)`,
wantArgv: []string{
"gocross", "list",
"-trimpath",
"-tags=tailscale_go,osusergo,netgo",
"-tags=osusergo,netgo",
"-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 '-extldflags=-static'",
"./cmd/tailcontrol",
},
Expand Down Expand Up @@ -411,7 +409,7 @@ TS_LINK_FAIL_REFLECT=0 (was <nil>)`,
wantArgv: []string{
"gocross", "build",
"-trimpath",
"-tags=tailscale_go,osusergo,netgo",
"-tags=osusergo,netgo",
"-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 '-extldflags=-static -L /my/glibc/path'",
"./cmd/tailcontrol",
},
Expand Down
107 changes: 27 additions & 80 deletions tool/gocross/gocross-wrapper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,99 +9,46 @@
set -euo pipefail

if [[ "${CI:-}" == "true" ]]; then
set -x
set -x
fi

# Locate a bootstrap toolchain and (re)build gocross if necessary. We run all of
# this in a subshell because posix shell semantics make it very easy to
# accidentally mutate the input environment that will get passed to gocross at
# the bottom of this script.
(
repo_root="${BASH_SOURCE%/*}/../.."

# Figuring out if gocross needs a rebuild, as well as the rebuild itself, need
# to happen with CWD inside this repo. Since we're in a subshell entirely
# dedicated to wrangling gocross and toolchains, cd over now before doing
# anything further so that the rest of this logic works the same if gocross is
# being invoked from somewhere else.
cd "$repo_root"

toolchain="$HOME/.cache/tailscale-go"

if [[ -d "$toolchain" ]]; then
# A toolchain exists, but is it recent enough to compile gocross? If not,
# wipe it out so that the next if block fetches a usable one.
want_go_minor=$(grep -E '^go ' "go.mod" | cut -f2 -d'.')
have_go_minor=$(head -1 "$toolchain/VERSION" | cut -f2 -d'.')
# Shortly before stable releases, we run release candidate
# toolchains, which have a non-numeric suffix on the version
# number. Remove the rc qualifier, we just care about the minor
# version.
have_go_minor="${have_go_minor%rc*}"
if [[ -z "$have_go_minor" || "$have_go_minor" -lt "$want_go_minor" ]]; then
rm -rf "$toolchain" "$toolchain.extracted"
repo_root="${BASH_SOURCE%/*}/../.."

# Figuring out if gocross needs a rebuild, as well as the rebuild itself, need
# to happen with CWD inside this repo. Since we're in a subshell entirely
# dedicated to wrangling gocross and toolchains, cd over now before doing
# anything further so that the rest of this logic works the same if gocross is
# being invoked from somewhere else.
cd "$repo_root"

# Binaries run with `gocross run` can reinvoke gocross, resulting in a
# potentially fancy build that invokes external linkers, might be
# cross-building for other targets, and so forth. In one hilarious
# case, cmd/cloner invokes go with GO111MODULE=off at some stage.
#
# Anyway, build gocross in a stripped down universe.
gocross_path="gocross"
gocross_ok=0
wantver="$(git rev-parse HEAD)"
if [[ -x "$gocross_path" ]]; then
gotver="$($gocross_path gocross-version 2>/dev/null || echo '')"
if [[ "$gotver" == "$wantver" ]]; then
gocross_ok=1
fi
fi
if [[ ! -d "$toolchain" ]]; then
mkdir -p "$HOME/.cache"

# We need any Go toolchain to build gocross, but the toolchain also has to
# be reasonably recent because we upgrade eagerly and gocross might not
# build with Go N-1. So, if we have no cached tailscale toolchain at all,
# fetch the initial one in shell. Once gocross is built, it'll manage
# updates.
read -r REV <go.toolchain.rev

case "$REV" in
/*)
toolchain="$REV"
;;
*)
# This works for linux and darwin, which is sufficient
# (we do not build tailscale-go for other targets).
HOST_OS=$(uname -s | tr A-Z a-z)
HOST_ARCH="$(uname -m)"
if [[ "$HOST_ARCH" == "aarch64" ]]; then
# Go uses the name "arm64".
HOST_ARCH="arm64"
elif [[ "$HOST_ARCH" == "x86_64" ]]; then
# Go uses the name "amd64".
HOST_ARCH="amd64"
fi

rm -rf "$toolchain" "$toolchain.extracted"
curl -f -L -o "$toolchain.tar.gz" "https://github.com/tailscale/go/releases/download/build-${REV}/${HOST_OS}-${HOST_ARCH}.tar.gz"
mkdir -p "$toolchain"
(cd "$toolchain" && tar --strip-components=1 -xf "$toolchain.tar.gz")
echo "$REV" >"$toolchain.extracted"
rm -f "$toolchain.tar.gz"
;;
esac
fi

# Binaries run with `gocross run` can reinvoke gocross, resulting in a
# potentially fancy build that invokes external linkers, might be
# cross-building for other targets, and so forth. In one hilarious
# case, cmd/cloner invokes go with GO111MODULE=off at some stage.
#
# Anyway, build gocross in a stripped down universe.
gocross_path="gocross"
gocross_ok=0
wantver="$(git rev-parse HEAD)"
if [[ -x "$gocross_path" ]]; then
gotver="$($gocross_path gocross-version 2>/dev/null || echo '')"
if [[ "$gotver" == "$wantver" ]]; then
gocross_ok=1
fi
fi
if [[ "$gocross_ok" == "0" ]]; then
fi
if [[ "$gocross_ok" == "0" ]]; then
unset GOOS
unset GOARCH
unset GO111MODULE
unset GOROOT
export CGO_ENABLED=0
"$toolchain/bin/go" build -o "$gocross_path" -ldflags "-X tailscale.com/version.gitCommitStamp=$wantver" tailscale.com/tool/gocross
fi
go build -o "$gocross_path" -ldflags "-X tailscale.com/version.gitCommitStamp=$wantver" tailscale.com/tool/gocross
fi
) # End of the subshell execution.

exec "${BASH_SOURCE%/*}/../../gocross" "$@"
Loading

0 comments on commit f69567d

Please sign in to comment.