1
0
mirror of https://github.com/golang/go synced 2024-11-17 09:54:46 -07:00

cmd/go/internal/work: reduce code duplication in buildModeInit by using sys.BuildModeSupported

Updates #34347

Change-Id: I6ea02d4737999bf24f5335508b5ed2352b498122
Reviewed-on: https://go-review.googlesource.com/c/go/+/208458
Run-TryBot: Bryan C. Mills <bcmills@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Jay Conrod <jayconrod@google.com>
This commit is contained in:
Bryan C. Mills 2019-11-22 13:30:47 -05:00
parent 8324acadfe
commit 9f3c2b6d16
2 changed files with 36 additions and 61 deletions

View File

@ -86,7 +86,11 @@ func instrumentInit() {
func buildModeInit() { func buildModeInit() {
gccgo := cfg.BuildToolchainName == "gccgo" gccgo := cfg.BuildToolchainName == "gccgo"
var codegenArg string var codegenArg string
platform := cfg.Goos + "/" + cfg.Goarch
// Configure the build mode first, then verify that it is supported.
// That way, if the flag is completely bogus we will prefer to error out with
// "-buildmode=%s not supported" instead of naming the specific platform.
switch cfg.BuildBuildmode { switch cfg.BuildBuildmode {
case "archive": case "archive":
pkgsFilter = pkgsNotMain pkgsFilter = pkgsNotMain
@ -95,20 +99,18 @@ func buildModeInit() {
if gccgo { if gccgo {
codegenArg = "-fPIC" codegenArg = "-fPIC"
} else { } else {
switch platform { switch cfg.Goos {
case "darwin/arm", "darwin/arm64": case "darwin":
codegenArg = "-shared" switch cfg.Goarch {
default: case "arm", "arm64":
switch cfg.Goos {
case "dragonfly", "freebsd", "illumos", "linux", "netbsd", "openbsd", "solaris":
if platform == "linux/ppc64" {
base.Fatalf("-buildmode=c-archive not supported on %s\n", platform)
}
// Use -shared so that the result is
// suitable for inclusion in a PIE or
// shared library.
codegenArg = "-shared" codegenArg = "-shared"
} }
case "dragonfly", "freebsd", "illumos", "linux", "netbsd", "openbsd", "solaris":
// Use -shared so that the result is
// suitable for inclusion in a PIE or
// shared library.
codegenArg = "-shared"
} }
} }
cfg.ExeSuffix = ".a" cfg.ExeSuffix = ".a"
@ -118,27 +120,25 @@ func buildModeInit() {
if gccgo { if gccgo {
codegenArg = "-fPIC" codegenArg = "-fPIC"
} else { } else {
switch platform { switch cfg.Goos {
case "linux/amd64", "linux/arm", "linux/arm64", "linux/386", "linux/ppc64le", "linux/s390x", case "linux", "android", "freebsd":
"android/amd64", "android/arm", "android/arm64", "android/386",
"freebsd/amd64":
codegenArg = "-shared" codegenArg = "-shared"
case "darwin/amd64", "darwin/386": case "windows":
case "windows/amd64", "windows/386":
// Do not add usual .exe suffix to the .dll file. // Do not add usual .exe suffix to the .dll file.
cfg.ExeSuffix = "" cfg.ExeSuffix = ""
default:
base.Fatalf("-buildmode=c-shared not supported on %s\n", platform)
} }
} }
ldBuildmode = "c-shared" ldBuildmode = "c-shared"
case "default": case "default":
switch platform { switch cfg.Goos {
case "android/arm", "android/arm64", "android/amd64", "android/386": case "android":
codegenArg = "-shared" codegenArg = "-shared"
ldBuildmode = "pie" ldBuildmode = "pie"
case "darwin/arm", "darwin/arm64": case "darwin":
codegenArg = "-shared" switch cfg.Goarch {
case "arm", "arm64":
codegenArg = "-shared"
}
fallthrough fallthrough
default: default:
ldBuildmode = "exe" ldBuildmode = "exe"
@ -161,18 +161,8 @@ func buildModeInit() {
} }
if gccgo { if gccgo {
codegenArg = "-fPIE" codegenArg = "-fPIE"
} else { } else if cfg.Goos != "aix" {
switch platform { codegenArg = "-shared"
case "linux/386", "linux/amd64", "linux/arm", "linux/arm64", "linux/ppc64le", "linux/s390x",
"android/amd64", "android/arm", "android/arm64", "android/386",
"freebsd/amd64":
codegenArg = "-shared"
case "darwin/amd64":
codegenArg = "-shared"
case "aix/ppc64":
default:
base.Fatalf("-buildmode=pie not supported on %s\n", platform)
}
} }
ldBuildmode = "pie" ldBuildmode = "pie"
case "shared": case "shared":
@ -180,11 +170,6 @@ func buildModeInit() {
if gccgo { if gccgo {
codegenArg = "-fPIC" codegenArg = "-fPIC"
} else { } else {
switch platform {
case "linux/386", "linux/amd64", "linux/arm", "linux/arm64", "linux/ppc64le", "linux/s390x":
default:
base.Fatalf("-buildmode=shared not supported on %s\n", platform)
}
codegenArg = "-dynlink" codegenArg = "-dynlink"
} }
if cfg.BuildO != "" { if cfg.BuildO != "" {
@ -196,14 +181,6 @@ func buildModeInit() {
if gccgo { if gccgo {
codegenArg = "-fPIC" codegenArg = "-fPIC"
} else { } else {
switch platform {
case "linux/amd64", "linux/arm", "linux/arm64", "linux/386", "linux/s390x", "linux/ppc64le",
"android/amd64", "android/arm", "android/arm64", "android/386":
case "darwin/amd64":
case "freebsd/amd64":
default:
base.Fatalf("-buildmode=plugin not supported on %s\n", platform)
}
codegenArg = "-dynlink" codegenArg = "-dynlink"
} }
cfg.ExeSuffix = ".so" cfg.ExeSuffix = ".so"
@ -211,16 +188,19 @@ func buildModeInit() {
default: default:
base.Fatalf("buildmode=%s not supported", cfg.BuildBuildmode) base.Fatalf("buildmode=%s not supported", cfg.BuildBuildmode)
} }
if !sys.BuildModeSupported(cfg.BuildToolchainName, cfg.BuildBuildmode, cfg.Goos, cfg.Goarch) {
base.Fatalf("-buildmode=%s not supported on %s/%s\n", cfg.BuildBuildmode, cfg.Goos, cfg.Goarch)
}
if cfg.BuildLinkshared { if cfg.BuildLinkshared {
if !sys.BuildModeSupported(cfg.BuildToolchainName, "shared", cfg.Goos, cfg.Goarch) {
base.Fatalf("-linkshared not supported on %s/%s\n", cfg.Goos, cfg.Goarch)
}
if gccgo { if gccgo {
codegenArg = "-fPIC" codegenArg = "-fPIC"
} else { } else {
switch platform { forcedAsmflags = append(forcedAsmflags, "-D=GOBUILDMODE_shared=1")
case "linux/386", "linux/amd64", "linux/arm", "linux/arm64", "linux/ppc64le", "linux/s390x":
forcedAsmflags = append(forcedAsmflags, "-D=GOBUILDMODE_shared=1")
default:
base.Fatalf("-linkshared not supported on %s\n", platform)
}
codegenArg = "-dynlink" codegenArg = "-dynlink"
forcedGcflags = append(forcedGcflags, "-linkshared") forcedGcflags = append(forcedGcflags, "-linkshared")
// TODO(mwhudson): remove -w when that gets fixed in linker. // TODO(mwhudson): remove -w when that gets fixed in linker.

View File

@ -47,11 +47,6 @@ func MustLinkExternal(goos, goarch string) bool {
// BuildModeSupported reports whether goos/goarch supports the given build mode // BuildModeSupported reports whether goos/goarch supports the given build mode
// using the given compiler. // using the given compiler.
func BuildModeSupported(compiler, buildmode, goos, goarch string) bool { func BuildModeSupported(compiler, buildmode, goos, goarch string) bool {
// This function mirrors the logic in cmd/go/internal/work.buildModeInit.
//
// TODO(bcmills): Refactor buildModeInit to use this function so that the two
// don't get out of sync.
if compiler == "gccgo" { if compiler == "gccgo" {
return true return true
} }