Skip to content

Commit abb0ed4

Browse files
committed
chore: remove custom go toolchain
Change-Id: Ic5e4198e3cb42225deea908169b91b157c398815 Signed-off-by: Thomas Kosiewski <tk@coder.com>
1 parent b53d914 commit abb0ed4

File tree

2 files changed

+27
-170
lines changed

2 files changed

+27
-170
lines changed

tool/gocross/gocross-wrapper.sh

Lines changed: 27 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -9,99 +9,46 @@
99
set -euo pipefail
1010

1111
if [[ "${CI:-}" == "true" ]]; then
12-
set -x
12+
set -x
1313
fi
1414

1515
# Locate a bootstrap toolchain and (re)build gocross if necessary. We run all of
1616
# this in a subshell because posix shell semantics make it very easy to
1717
# accidentally mutate the input environment that will get passed to gocross at
1818
# the bottom of this script.
1919
(
20-
repo_root="${BASH_SOURCE%/*}/../.."
21-
22-
# Figuring out if gocross needs a rebuild, as well as the rebuild itself, need
23-
# to happen with CWD inside this repo. Since we're in a subshell entirely
24-
# dedicated to wrangling gocross and toolchains, cd over now before doing
25-
# anything further so that the rest of this logic works the same if gocross is
26-
# being invoked from somewhere else.
27-
cd "$repo_root"
28-
29-
toolchain="$HOME/.cache/tailscale-go"
30-
31-
if [[ -d "$toolchain" ]]; then
32-
# A toolchain exists, but is it recent enough to compile gocross? If not,
33-
# wipe it out so that the next if block fetches a usable one.
34-
want_go_minor=$(grep -E '^go ' "go.mod" | cut -f2 -d'.')
35-
have_go_minor=$(head -1 "$toolchain/VERSION" | cut -f2 -d'.')
36-
# Shortly before stable releases, we run release candidate
37-
# toolchains, which have a non-numeric suffix on the version
38-
# number. Remove the rc qualifier, we just care about the minor
39-
# version.
40-
have_go_minor="${have_go_minor%rc*}"
41-
if [[ -z "$have_go_minor" || "$have_go_minor" -lt "$want_go_minor" ]]; then
42-
rm -rf "$toolchain" "$toolchain.extracted"
20+
repo_root="${BASH_SOURCE%/*}/../.."
21+
22+
# Figuring out if gocross needs a rebuild, as well as the rebuild itself, need
23+
# to happen with CWD inside this repo. Since we're in a subshell entirely
24+
# dedicated to wrangling gocross and toolchains, cd over now before doing
25+
# anything further so that the rest of this logic works the same if gocross is
26+
# being invoked from somewhere else.
27+
cd "$repo_root"
28+
29+
# Binaries run with `gocross run` can reinvoke gocross, resulting in a
30+
# potentially fancy build that invokes external linkers, might be
31+
# cross-building for other targets, and so forth. In one hilarious
32+
# case, cmd/cloner invokes go with GO111MODULE=off at some stage.
33+
#
34+
# Anyway, build gocross in a stripped down universe.
35+
gocross_path="gocross"
36+
gocross_ok=0
37+
wantver="$(git rev-parse HEAD)"
38+
if [[ -x "$gocross_path" ]]; then
39+
gotver="$($gocross_path gocross-version 2>/dev/null || echo '')"
40+
if [[ "$gotver" == "$wantver" ]]; then
41+
gocross_ok=1
4342
fi
44-
fi
45-
if [[ ! -d "$toolchain" ]]; then
46-
mkdir -p "$HOME/.cache"
47-
48-
# We need any Go toolchain to build gocross, but the toolchain also has to
49-
# be reasonably recent because we upgrade eagerly and gocross might not
50-
# build with Go N-1. So, if we have no cached tailscale toolchain at all,
51-
# fetch the initial one in shell. Once gocross is built, it'll manage
52-
# updates.
53-
read -r REV <go.toolchain.rev
54-
55-
case "$REV" in
56-
/*)
57-
toolchain="$REV"
58-
;;
59-
*)
60-
# This works for linux and darwin, which is sufficient
61-
# (we do not build tailscale-go for other targets).
62-
HOST_OS=$(uname -s | tr A-Z a-z)
63-
HOST_ARCH="$(uname -m)"
64-
if [[ "$HOST_ARCH" == "aarch64" ]]; then
65-
# Go uses the name "arm64".
66-
HOST_ARCH="arm64"
67-
elif [[ "$HOST_ARCH" == "x86_64" ]]; then
68-
# Go uses the name "amd64".
69-
HOST_ARCH="amd64"
70-
fi
71-
72-
rm -rf "$toolchain" "$toolchain.extracted"
73-
curl -f -L -o "$toolchain.tar.gz" "https://github.com/tailscale/go/releases/download/build-${REV}/${HOST_OS}-${HOST_ARCH}.tar.gz"
74-
mkdir -p "$toolchain"
75-
(cd "$toolchain" && tar --strip-components=1 -xf "$toolchain.tar.gz")
76-
echo "$REV" >"$toolchain.extracted"
77-
rm -f "$toolchain.tar.gz"
78-
;;
79-
esac
80-
fi
81-
82-
# Binaries run with `gocross run` can reinvoke gocross, resulting in a
83-
# potentially fancy build that invokes external linkers, might be
84-
# cross-building for other targets, and so forth. In one hilarious
85-
# case, cmd/cloner invokes go with GO111MODULE=off at some stage.
86-
#
87-
# Anyway, build gocross in a stripped down universe.
88-
gocross_path="gocross"
89-
gocross_ok=0
90-
wantver="$(git rev-parse HEAD)"
91-
if [[ -x "$gocross_path" ]]; then
92-
gotver="$($gocross_path gocross-version 2>/dev/null || echo '')"
93-
if [[ "$gotver" == "$wantver" ]]; then
94-
gocross_ok=1
95-
fi
96-
fi
97-
if [[ "$gocross_ok" == "0" ]]; then
43+
fi
44+
if [[ "$gocross_ok" == "0" ]]; then
9845
unset GOOS
9946
unset GOARCH
10047
unset GO111MODULE
10148
unset GOROOT
10249
export CGO_ENABLED=0
103-
"$toolchain/bin/go" build -o "$gocross_path" -ldflags "-X tailscale.com/version.gitCommitStamp=$wantver" tailscale.com/tool/gocross
104-
fi
50+
go build -o "$gocross_path" -ldflags "-X tailscale.com/version.gitCommitStamp=$wantver" tailscale.com/tool/gocross
51+
fi
10552
) # End of the subshell execution.
10653

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

tool/gocross/toolchain.go

Lines changed: 0 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,8 @@ package main
66
import (
77
"bytes"
88
"fmt"
9-
"io"
10-
"net/http"
119
"os"
12-
"os/exec"
1310
"path/filepath"
14-
"runtime"
1511
)
1612

1713
func toolchainRev() (string, error) {
@@ -86,104 +82,18 @@ func getToolchain() (toolchainDir, gorootDir string, err error) {
8682
// means that if you invoke 'go test' via gocross, and that test tries to
8783
// build code, that build will also end up using gocross.
8884

89-
if err := ensureToolchain(cache, toolchainDir); err != nil {
90-
return "", "", err
91-
}
9285
if err := ensureGoroot(toolchainDir, gorootDir); err != nil {
9386
return "", "", err
9487
}
9588

9689
return toolchainDir, gorootDir, nil
9790
}
9891

99-
func ensureToolchain(cacheDir, toolchainDir string) error {
100-
stampFile := toolchainDir + ".extracted"
101-
102-
wantRev, err := toolchainRev()
103-
if err != nil {
104-
return err
105-
}
106-
gotRev, err := readRevFile(stampFile)
107-
if err != nil {
108-
return fmt.Errorf("reading stamp file %q: %v", stampFile, err)
109-
}
110-
if gotRev == wantRev {
111-
// Toolchain already good.
112-
return nil
113-
}
114-
115-
if err := os.RemoveAll(toolchainDir); err != nil {
116-
return err
117-
}
118-
if err := os.RemoveAll(stampFile); err != nil {
119-
return err
120-
}
121-
122-
if filepath.IsAbs(wantRev) {
123-
// Local dev toolchain.
124-
if err := os.Symlink(wantRev, toolchainDir); err != nil {
125-
return err
126-
}
127-
return nil
128-
} else {
129-
if err := downloadCachedgo(toolchainDir, wantRev); err != nil {
130-
return err
131-
}
132-
}
133-
134-
if err := os.WriteFile(stampFile, []byte(wantRev), 0644); err != nil {
135-
return err
136-
}
137-
138-
return nil
139-
}
140-
14192
func ensureGoroot(toolchainDir, gorootDir string) error {
14293
if _, err := os.Stat(gorootDir); err == nil {
14394
return nil
14495
} else if !os.IsNotExist(err) {
14596
return err
14697
}
14798
return makeGoroot(toolchainDir, gorootDir)
148-
149-
}
150-
151-
func downloadCachedgo(toolchainDir, toolchainRev string) error {
152-
url := fmt.Sprintf("https://github.com/tailscale/go/releases/download/build-%s/%s-%s.tar.gz", toolchainRev, runtime.GOOS, runtime.GOARCH)
153-
154-
archivePath := toolchainDir + ".tar.gz"
155-
f, err := os.Create(archivePath)
156-
if err != nil {
157-
return err
158-
}
159-
160-
resp, err := http.Get(url)
161-
if err != nil {
162-
return err
163-
}
164-
defer resp.Body.Close()
165-
if resp.StatusCode != 200 {
166-
return fmt.Errorf("failed to get %q: %v", url, resp.Status)
167-
}
168-
if _, err := io.Copy(f, resp.Body); err != nil {
169-
return err
170-
}
171-
if err := f.Close(); err != nil {
172-
return err
173-
}
174-
175-
if err := os.MkdirAll(toolchainDir, 0755); err != nil {
176-
return err
177-
}
178-
cmd := exec.Command("tar", "--strip-components=1", "-xf", archivePath)
179-
cmd.Dir = toolchainDir
180-
if err := cmd.Run(); err != nil {
181-
return err
182-
}
183-
184-
if err := os.RemoveAll(archivePath); err != nil {
185-
return err
186-
}
187-
188-
return nil
18999
}

0 commit comments

Comments
 (0)