diff options
author | Xiangdong Ji <xiangdong.ji@gmail.com> | 2020-10-18 11:43:23 -0700 |
---|---|---|
committer | Bryan C. Mills <bcmills@google.com> | 2020-10-22 14:51:57 +0000 |
commit | 61313dab524f2c82add8442e15d87fca5b7103de (patch) | |
tree | 249c438c7f1eaa55537f1ac5012a5baeacdc6211 /src/cmd/go/internal/load/pkg.go | |
parent | b4c8b67adcd39da54f210bef5c201b1df8124d73 (diff) | |
download | go-61313dab524f2c82add8442e15d87fca5b7103de.tar.gz go-61313dab524f2c82add8442e15d87fca5b7103de.zip |
cmd/go: use the last -linkmode flag to determine external linking
Current linkmode checking in determining package dependencies doesn't
take multiple -linkmode options into consideration, may lead to redundant
dependency on 'runtime/cgo'.
Fixes the problem and adds a testcase.
Change-Id: Iac5ea9fb3ca5ef931201afd0f3441f41f946c919
Reviewed-on: https://go-review.googlesource.com/c/go/+/263497
Trust: Jay Conrod <jayconrod@google.com>
Reviewed-by: Bryan C. Mills <bcmills@google.com>
Diffstat (limited to 'src/cmd/go/internal/load/pkg.go')
-rw-r--r-- | src/cmd/go/internal/load/pkg.go | 14 |
1 files changed, 9 insertions, 5 deletions
diff --git a/src/cmd/go/internal/load/pkg.go b/src/cmd/go/internal/load/pkg.go index c9665265e9..f07bd3e075 100644 --- a/src/cmd/go/internal/load/pkg.go +++ b/src/cmd/go/internal/load/pkg.go @@ -1978,16 +1978,20 @@ func externalLinkingForced(p *Package) bool { // external linking mode, as of course does // -ldflags=-linkmode=external. External linking mode forces // an import of runtime/cgo. + // If there are multiple -linkmode options, the last one wins. pieCgo := cfg.BuildBuildmode == "pie" && !sys.InternalLinkPIESupported(cfg.BuildContext.GOOS, cfg.BuildContext.GOARCH) linkmodeExternal := false if p != nil { ldflags := BuildLdflags.For(p) - for i, a := range ldflags { - if a == "-linkmode=external" { - linkmodeExternal = true - } - if a == "-linkmode" && i+1 < len(ldflags) && ldflags[i+1] == "external" { + for i := len(ldflags) - 1; i >= 0; i-- { + a := ldflags[i] + if a == "-linkmode=external" || + a == "-linkmode" && i+1 < len(ldflags) && ldflags[i+1] == "external" { linkmodeExternal = true + break + } else if a == "-linkmode=internal" || + a == "-linkmode" && i+1 < len(ldflags) && ldflags[i+1] == "internal" { + break } } } |