Skip to content

Commit 9f3c2b6

Browse files
author
Bryan C. Mills
committed
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 <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Jay Conrod <[email protected]>
1 parent 8324aca commit 9f3c2b6

File tree

2 files changed

+36
-61
lines changed

2 files changed

+36
-61
lines changed

src/cmd/go/internal/work/init.go

Lines changed: 36 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,11 @@ func instrumentInit() {
8686
func buildModeInit() {
8787
gccgo := cfg.BuildToolchainName == "gccgo"
8888
var codegenArg string
89-
platform := cfg.Goos + "/" + cfg.Goarch
89+
90+
// Configure the build mode first, then verify that it is supported.
91+
// That way, if the flag is completely bogus we will prefer to error out with
92+
// "-buildmode=%s not supported" instead of naming the specific platform.
93+
9094
switch cfg.BuildBuildmode {
9195
case "archive":
9296
pkgsFilter = pkgsNotMain
@@ -95,20 +99,18 @@ func buildModeInit() {
9599
if gccgo {
96100
codegenArg = "-fPIC"
97101
} else {
98-
switch platform {
99-
case "darwin/arm", "darwin/arm64":
100-
codegenArg = "-shared"
101-
default:
102-
switch cfg.Goos {
103-
case "dragonfly", "freebsd", "illumos", "linux", "netbsd", "openbsd", "solaris":
104-
if platform == "linux/ppc64" {
105-
base.Fatalf("-buildmode=c-archive not supported on %s\n", platform)
106-
}
107-
// Use -shared so that the result is
108-
// suitable for inclusion in a PIE or
109-
// shared library.
102+
switch cfg.Goos {
103+
case "darwin":
104+
switch cfg.Goarch {
105+
case "arm", "arm64":
110106
codegenArg = "-shared"
111107
}
108+
109+
case "dragonfly", "freebsd", "illumos", "linux", "netbsd", "openbsd", "solaris":
110+
// Use -shared so that the result is
111+
// suitable for inclusion in a PIE or
112+
// shared library.
113+
codegenArg = "-shared"
112114
}
113115
}
114116
cfg.ExeSuffix = ".a"
@@ -118,27 +120,25 @@ func buildModeInit() {
118120
if gccgo {
119121
codegenArg = "-fPIC"
120122
} else {
121-
switch platform {
122-
case "linux/amd64", "linux/arm", "linux/arm64", "linux/386", "linux/ppc64le", "linux/s390x",
123-
"android/amd64", "android/arm", "android/arm64", "android/386",
124-
"freebsd/amd64":
123+
switch cfg.Goos {
124+
case "linux", "android", "freebsd":
125125
codegenArg = "-shared"
126-
case "darwin/amd64", "darwin/386":
127-
case "windows/amd64", "windows/386":
126+
case "windows":
128127
// Do not add usual .exe suffix to the .dll file.
129128
cfg.ExeSuffix = ""
130-
default:
131-
base.Fatalf("-buildmode=c-shared not supported on %s\n", platform)
132129
}
133130
}
134131
ldBuildmode = "c-shared"
135132
case "default":
136-
switch platform {
137-
case "android/arm", "android/arm64", "android/amd64", "android/386":
133+
switch cfg.Goos {
134+
case "android":
138135
codegenArg = "-shared"
139136
ldBuildmode = "pie"
140-
case "darwin/arm", "darwin/arm64":
141-
codegenArg = "-shared"
137+
case "darwin":
138+
switch cfg.Goarch {
139+
case "arm", "arm64":
140+
codegenArg = "-shared"
141+
}
142142
fallthrough
143143
default:
144144
ldBuildmode = "exe"
@@ -161,30 +161,15 @@ func buildModeInit() {
161161
}
162162
if gccgo {
163163
codegenArg = "-fPIE"
164-
} else {
165-
switch platform {
166-
case "linux/386", "linux/amd64", "linux/arm", "linux/arm64", "linux/ppc64le", "linux/s390x",
167-
"android/amd64", "android/arm", "android/arm64", "android/386",
168-
"freebsd/amd64":
169-
codegenArg = "-shared"
170-
case "darwin/amd64":
171-
codegenArg = "-shared"
172-
case "aix/ppc64":
173-
default:
174-
base.Fatalf("-buildmode=pie not supported on %s\n", platform)
175-
}
164+
} else if cfg.Goos != "aix" {
165+
codegenArg = "-shared"
176166
}
177167
ldBuildmode = "pie"
178168
case "shared":
179169
pkgsFilter = pkgsNotMain
180170
if gccgo {
181171
codegenArg = "-fPIC"
182172
} else {
183-
switch platform {
184-
case "linux/386", "linux/amd64", "linux/arm", "linux/arm64", "linux/ppc64le", "linux/s390x":
185-
default:
186-
base.Fatalf("-buildmode=shared not supported on %s\n", platform)
187-
}
188173
codegenArg = "-dynlink"
189174
}
190175
if cfg.BuildO != "" {
@@ -196,31 +181,26 @@ func buildModeInit() {
196181
if gccgo {
197182
codegenArg = "-fPIC"
198183
} else {
199-
switch platform {
200-
case "linux/amd64", "linux/arm", "linux/arm64", "linux/386", "linux/s390x", "linux/ppc64le",
201-
"android/amd64", "android/arm", "android/arm64", "android/386":
202-
case "darwin/amd64":
203-
case "freebsd/amd64":
204-
default:
205-
base.Fatalf("-buildmode=plugin not supported on %s\n", platform)
206-
}
207184
codegenArg = "-dynlink"
208185
}
209186
cfg.ExeSuffix = ".so"
210187
ldBuildmode = "plugin"
211188
default:
212189
base.Fatalf("buildmode=%s not supported", cfg.BuildBuildmode)
213190
}
191+
192+
if !sys.BuildModeSupported(cfg.BuildToolchainName, cfg.BuildBuildmode, cfg.Goos, cfg.Goarch) {
193+
base.Fatalf("-buildmode=%s not supported on %s/%s\n", cfg.BuildBuildmode, cfg.Goos, cfg.Goarch)
194+
}
195+
214196
if cfg.BuildLinkshared {
197+
if !sys.BuildModeSupported(cfg.BuildToolchainName, "shared", cfg.Goos, cfg.Goarch) {
198+
base.Fatalf("-linkshared not supported on %s/%s\n", cfg.Goos, cfg.Goarch)
199+
}
215200
if gccgo {
216201
codegenArg = "-fPIC"
217202
} else {
218-
switch platform {
219-
case "linux/386", "linux/amd64", "linux/arm", "linux/arm64", "linux/ppc64le", "linux/s390x":
220-
forcedAsmflags = append(forcedAsmflags, "-D=GOBUILDMODE_shared=1")
221-
default:
222-
base.Fatalf("-linkshared not supported on %s\n", platform)
223-
}
203+
forcedAsmflags = append(forcedAsmflags, "-D=GOBUILDMODE_shared=1")
224204
codegenArg = "-dynlink"
225205
forcedGcflags = append(forcedGcflags, "-linkshared")
226206
// TODO(mwhudson): remove -w when that gets fixed in linker.

src/cmd/internal/sys/supported.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,6 @@ func MustLinkExternal(goos, goarch string) bool {
4747
// BuildModeSupported reports whether goos/goarch supports the given build mode
4848
// using the given compiler.
4949
func BuildModeSupported(compiler, buildmode, goos, goarch string) bool {
50-
// This function mirrors the logic in cmd/go/internal/work.buildModeInit.
51-
//
52-
// TODO(bcmills): Refactor buildModeInit to use this function so that the two
53-
// don't get out of sync.
54-
5550
if compiler == "gccgo" {
5651
return true
5752
}

0 commit comments

Comments
 (0)