1
0
mirror of https://github.com/golang/go synced 2024-11-21 21:34:40 -07:00
This commit is contained in:
Laurent Goderre 2024-07-22 16:28:26 -04:00
parent 7f6c9d55dd
commit 00923182d5
2 changed files with 13 additions and 12 deletions

View File

@ -20,6 +20,7 @@ import (
"os" "os"
pathpkg "path" pathpkg "path"
"path/filepath" "path/filepath"
"regexp"
"runtime" "runtime"
"runtime/debug" "runtime/debug"
"slices" "slices"
@ -2343,12 +2344,8 @@ func (p *Package) setBuildInfo(ctx context.Context, autoVCS bool) {
// since it can include system paths through various linker flags (notably // since it can include system paths through various linker flags (notably
// -extar, -extld, and -extldflags). // -extar, -extld, and -extldflags).
// //
// TODO: since we control cmd/link, in theory we can parse ldflags to if cfg.BuildTrimpath {
// determine whether they may refer to system paths. If we do that, we can ldflags = trimPathsFromLdFlags(ldflags)
// redact only those paths from the recorded -ldflags setting and still
// record the system-independent parts of the flags.
if !cfg.BuildTrimpath {
ldflags = trimLdFlags(ldflags)
} }
appendSetting("-ldflags", ldflags) appendSetting("-ldflags", ldflags)
} }
@ -2506,10 +2503,14 @@ omitVCS:
p.Internal.BuildInfo = info p.Internal.BuildInfo = info
} }
// trimLdFlags replaces know paths with variable and removes // trimLdFlags removes system paths from the ldflag.
// flags with absolute paths // since we control cmd/link, in theory we parse ldflags to
func trimLdFlags(flags string) string { // determine whether they refer to system paths. We then can
return flags // redact only those paths from the recorded -ldflags setting and still
// record the system-independent parts of the flags.
func trimPathsFromLdFlags(flags string) string {
re := regexp.MustCompile(`(?:(['"])|\s)(?:-[A-Z])?\/[aA-zZ0-9][^\s'"]+`)
return re.ReplaceAllString(flags, `$1`)
} }
// SafeArg reports whether arg is a "safe" command-line argument, // SafeArg reports whether arg is a "safe" command-line argument,

View File

@ -256,14 +256,14 @@ build GOOS=bar
"os": "bar", "os": "bar",
"compiler": "baz", "compiler": "baz",
"cgo": "false", "cgo": "false",
"ldflags": "-flag1 -flag2", "ldflags": "all=-buildid '1649548598' -linkmode external -extldflags '-L/media/path1 -L/media/path2 -Wl,-z,now -Wl,-z,relro' -X 'github.com/foo/bar/app.Version=v1.2.3'",
"trimpath": "true", "trimpath": "true",
}, },
pkg: Package{}, pkg: Package{},
autoVCS: true, autoVCS: true,
want: `build -buildmode= want: `build -buildmode=
build -compiler=baz build -compiler=baz
build -ldflags="-flag1 -flag2" build -ldflags="all=-buildid '1649548598' -linkmode external -extldflags ' -Wl,-z,now -Wl,-z,relro' -X 'github.com/foo/bar/app.Version=v1.2.3'"
build -trimpath=true build -trimpath=true
build CGO_ENABLED=0 build CGO_ENABLED=0
build GOARCH=foo build GOARCH=foo