Skip to content

Commit

Permalink
drop Go 1.21 and start using go/version
Browse files Browse the repository at this point in the history
Needing to awkwardly treat Go versions as if they were semver
is no longer necessary thanks to go/version being in Go 1.22.0 now.
  • Loading branch information
mvdan authored and pagran committed Feb 12, 2024
1 parent d76bc2e commit ad2ecc7
Show file tree
Hide file tree
Showing 42 changed files with 75 additions and 253 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
test:
strategy:
matrix:
go-version: [1.21.x, 1.22.x]
go-version: [1.22.x]
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module mvdan.cc/garble

go 1.21
go 1.22

require (
github.com/bluekeyes/go-gitdiff v0.7.1
Expand Down
7 changes: 3 additions & 4 deletions go_std_tables.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 0 additions & 36 deletions internal/linker/patches/go1.21/0001-add-custom-magic-value.patch

This file was deleted.

This file was deleted.

43 changes: 0 additions & 43 deletions internal/linker/patches/go1.21/0003-add-entryOff-encryption.patch

This file was deleted.

30 changes: 11 additions & 19 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"go/parser"
"go/token"
"go/types"
"go/version"
"io"
"io/fs"
"log"
Expand All @@ -40,7 +41,6 @@ import (
"golang.org/x/exp/maps"
"golang.org/x/exp/slices"
"golang.org/x/mod/module"
"golang.org/x/mod/semver"
"golang.org/x/tools/go/ast/astutil"
"golang.org/x/tools/go/ssa"
"mvdan.cc/garble/internal/ctrlflow"
Expand Down Expand Up @@ -267,28 +267,21 @@ type errJustExit int
func (e errJustExit) Error() string { return fmt.Sprintf("exit: %d", e) }

func goVersionOK() bool {
// TODO(mvdan): use go/version once we can require Go 1.22 or later: https://go.dev/issue/62039
const (
minGoVersionSemver = "v1.21.0"
suggestedGoVersion = "1.21"
)
const minGoVersion = "go1.22"

// rxVersion looks for a version like "go1.2" or "go1.2.3"
// rxVersion looks for a version like "go1.2" or "go1.2.3" in `go env GOVERSION`.
rxVersion := regexp.MustCompile(`go\d+\.\d+(?:\.\d+)?`)

toolchainVersionFull := sharedCache.GoEnv.GOVERSION
toolchainVersion := rxVersion.FindString(toolchainVersionFull)
if toolchainVersion == "" {
// Go 1.15.x and older do not have GOVERSION yet.
// We could go the extra mile and fetch it via 'go toolchainVersion',
// but we'd have to error anyway.
fmt.Fprintf(os.Stderr, "Go version is too old; please upgrade to Go %s or newer\n", suggestedGoVersion)
sharedCache.GoVersion = rxVersion.FindString(toolchainVersionFull)
if sharedCache.GoVersion == "" {
// Go 1.15.x and older did not have GOVERSION yet; they are too old anyway.
fmt.Fprintf(os.Stderr, "Go version is too old; please upgrade to %s or newer\n", minGoVersion)
return false
}

sharedCache.GoVersionSemver = "v" + strings.TrimPrefix(toolchainVersion, "go")
if semver.Compare(sharedCache.GoVersionSemver, minGoVersionSemver) < 0 {
fmt.Fprintf(os.Stderr, "Go version %q is too old; please upgrade to Go %s or newer\n", toolchainVersionFull, suggestedGoVersion)
if version.Compare(sharedCache.GoVersion, minGoVersion) < 0 {
fmt.Fprintf(os.Stderr, "Go version %q is too old; please upgrade to %s or newer\n", toolchainVersionFull, minGoVersion)
return false
}

Expand All @@ -304,10 +297,9 @@ func goVersionOK() bool {
// Fall back to not performing the check against the toolchain version.
return true
}
builtVersionSemver := "v" + strings.TrimPrefix(builtVersion, "go")
if semver.Compare(builtVersionSemver, sharedCache.GoVersionSemver) < 0 {
if version.Compare(builtVersion, sharedCache.GoVersion) < 0 {
fmt.Fprintf(os.Stderr, `
garble was built with %q and is being used with %q; rebuild it with a command like:
garble was built with %q and can't be used with the newer %q; rebuild it with a command like:
go install mvdan.cc/garble@latest
`[1:], builtVersionFull, toolchainVersionFull)
return false
Expand Down
5 changes: 2 additions & 3 deletions scripts/gen-go-std-tables.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ var runtimeLinknamed = []string{
$(for path in ${runtime_linknamed}; do
echo "\"${path}\"",
done)
// Existed in Go 1.21; removed in Go 1.22.
"math/rand",
// The net package linknames to the runtime, not the other way around.
// TODO: support this automatically via our script.
"net",
}
Expand All @@ -58,7 +58,6 @@ done)
}
var compilerIntrinsicsFuncs = map[string]bool{
"runtime.mulUintptr": true, // Existed in Go 1.21; removed in Go 1.22.
$(while read path name; do
echo "\"${path}.${name}\": true,"
done <<<"${compiler_intrinsics_table}")
Expand Down
11 changes: 4 additions & 7 deletions shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import (
"time"

"golang.org/x/mod/module"
"golang.org/x/mod/semver"
)

//go:generate ./scripts/gen-go-std-tables.sh
Expand Down Expand Up @@ -50,12 +49,12 @@ type sharedCacheType struct {

GOGARBLE string

// GoVersionSemver is a semver-compatible version of the Go toolchain
// currently being used, as reported by "go env GOVERSION".
// GoVersion is a version of the Go toolchain currently being used,
// as reported by "go env GOVERSION" and compatible with go/version.
// Note that the version of Go that built the garble binary might be newer.
// Also note that a devel version like "go1.22-231f290e51" is
// currently represented as "v1.22".
GoVersionSemver string
// currently represented as "go1.22", as the suffix is ignored by go/version.
GoVersion string

// Filled directly from "go env".
// Keep in sync with fetchGoEnv.
Expand Down Expand Up @@ -267,8 +266,6 @@ func appendListedPackages(packages []string, mainBuild bool) error {
// Some packages in runtimeLinknamed need a build tag to be importable,
// like crypto/internal/boring/fipstls with boringcrypto,
// so any pkg.Error should be ignored when the build tag isn't set.
} else if pkg.ImportPath == "math/rand/v2" && semver.Compare(sharedCache.GoVersionSemver, "v1.22") < 0 {
// added in Go 1.22, so Go 1.21 runs into a "not found" error.
} else {
if pkgErrors.Len() > 0 {
pkgErrors.WriteString("\n")
Expand Down
2 changes: 1 addition & 1 deletion testdata/script/asm.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ binsubstr main$exe 'addJmp' 'AddImpl'
-- go.mod --
module test/with.many.dots/main

go 1.21
go 1.22
-- main.go --
package main

Expand Down
2 changes: 1 addition & 1 deletion testdata/script/atomic.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ cmp stderr main.stderr
-- go.mod --
module test/main

go 1.21
go 1.22
-- main.go --
package main

Expand Down
2 changes: 1 addition & 1 deletion testdata/script/basic.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ binsubstr main$exe 'garble_main.go' 'globalVar' 'globalFunc' $gofullversion
-- go.mod --
module test/mainfoo

go 1.21
go 1.22
-- garble_main.go --
package main

Expand Down
2 changes: 1 addition & 1 deletion testdata/script/cache.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ cmp stderr main.stderr
-- go.mod --
module test/main

go 1.21
go 1.22
-- main.go --
package main

Expand Down
2 changes: 1 addition & 1 deletion testdata/script/cgo.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ binsubstr main$exe 'privateAdd'
-- go.mod --
module test/main

go 1.21
go 1.22
-- main.go --
package main

Expand Down
2 changes: 1 addition & 1 deletion testdata/script/crossbuild.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ exec garble build
-- go.mod --
module test/main

go 1.21
go 1.22
-- main.go --
package main

Expand Down
2 changes: 1 addition & 1 deletion testdata/script/ctrlflow.txtar
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ grep 'func\(int\) int' $WORK/debug/test/main/GARBLE_controlflow.go
-- go.mod --
module test/main

go 1.21
go 1.22
-- garble_main.go --
package main

Expand Down
Loading

0 comments on commit ad2ecc7

Please sign in to comment.