Skip to content

Commit c2f42b7

Browse files
committed
Speedup compilepkg args handling
1 parent 85eef05 commit c2f42b7

File tree

2 files changed

+49
-69
lines changed

2 files changed

+49
-69
lines changed

go/private/actions/compilepkg.bzl

Lines changed: 45 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,13 @@ def emit_compilepkg(
8181
fail("sources is a required parameter")
8282
if out_lib == None:
8383
fail("out_lib is a required parameter")
84-
if bool(nogo) != bool(out_facts):
84+
85+
have_nogo = nogo != None
86+
if have_nogo != (out_facts != None):
8587
fail("nogo must be specified if and only if out_facts is specified")
86-
if bool(nogo) != bool(out_nogo_log):
88+
if have_nogo != (out_nogo_log != None):
8789
fail("nogo must be specified if and only if out_nogo_log is specified")
88-
if bool(nogo) != bool(out_nogo_validation):
90+
if have_nogo != (out_nogo_validation != None):
8991
fail("nogo must be specified if and only if out_nogo_validation is specified")
9092

9193
if cover and go.coverdata:
@@ -97,52 +99,53 @@ def emit_compilepkg(
9799
inputs_transitive = [sdk.headers, sdk.tools, go.stdlib.libs]
98100
outputs = [out_lib, out_export]
99101

100-
args = go.builder_args(go, "compilepkg", use_path_mapping = True)
101-
args.add_all(sources, before_each = "-src")
102-
args.add_all(embedsrcs, before_each = "-embedsrc", expand_directories = False)
103-
args.add_all(
102+
shared_args = go.builder_args(go, use_path_mapping = True)
103+
shared_args.add_all(sources, before_each = "-src")
104+
105+
compile_args = go.tool_args(go)
106+
compile_args.add_all(embedsrcs, before_each = "-embedsrc", expand_directories = False)
107+
compile_args.add_all(
104108
sources + [out_lib] + embedsrcs,
105109
map_each = _embedroot_arg,
106110
before_each = "-embedroot",
107111
uniquify = True,
108112
expand_directories = False,
109113
)
110-
args.add_all(
114+
compile_args.add_all(
111115
sources + [out_lib],
112116
map_each = _embedlookupdir_arg,
113117
before_each = "-embedlookupdir",
114118
uniquify = True,
115119
expand_directories = False,
116120
)
117121

118-
cover_mode = None
119122
if cover and go.coverdata:
120123
if go.mode.race:
121124
cover_mode = "atomic"
122125
else:
123126
cover_mode = "set"
124-
args.add("-cover_mode", cover_mode)
125-
args.add("-cover_format", go.mode.cover_format)
126-
args.add_all(cover, before_each = "-cover")
127+
shared_args.add("-cover_mode", cover_mode)
128+
compile_args.add("-cover_format", go.mode.cover_format)
129+
compile_args.add_all(cover, before_each = "-cover")
127130

128-
args.add_all(archives, before_each = "-arc", map_each = _archive)
131+
shared_args.add_all(archives, before_each = "-arc", map_each = _archive)
129132
if recompile_internal_deps:
130-
args.add_all(recompile_internal_deps, before_each = "-recompile_internal_deps")
133+
shared_args.add_all(recompile_internal_deps, before_each = "-recompile_internal_deps")
131134
if importpath:
132-
args.add("-importpath", importpath)
135+
shared_args.add("-importpath", importpath)
133136
else:
134-
args.add("-importpath", go.label.name)
137+
shared_args.add("-importpath", go.label.name)
135138
if importmap:
136-
args.add("-p", importmap)
137-
args.add("-package_list", sdk.package_list)
139+
shared_args.add("-p", importmap)
140+
shared_args.add("-package_list", sdk.package_list)
138141

139-
args.add("-lo", out_lib)
140-
args.add("-o", out_export)
142+
compile_args.add("-lo", out_lib)
143+
compile_args.add("-o", out_export)
141144
if out_cgo_export_h:
142-
args.add("-cgoexport", out_cgo_export_h)
145+
compile_args.add("-cgoexport", out_cgo_export_h)
143146
outputs.append(out_cgo_export_h)
144147
if testfilter:
145-
args.add("-testfilter", testfilter)
148+
shared_args.add("-testfilter", testfilter)
146149

147150
link_mode_flag = link_mode_arg(go.mode)
148151

@@ -156,10 +159,10 @@ def emit_compilepkg(
156159
gc_flags.extend(go.toolchain.flags.compile)
157160
if link_mode_flag:
158161
gc_flags.append(link_mode_flag)
159-
args.add("-gcflags", quote_opts(gc_flags))
162+
compile_args.add("-gcflags", quote_opts(gc_flags))
160163

161164
if link_mode_flag:
162-
args.add("-asmflags", link_mode_flag)
165+
compile_args.add("-asmflags", link_mode_flag)
163166

164167
# cgo and the linker action don't support path mapping yet
165168
# TODO: Remove the second condition after https://github.com/bazelbuild/bazel/pull/21921.
@@ -175,33 +178,33 @@ def emit_compilepkg(
175178
if nogo:
176179
cgo_go_srcs_for_nogo = go.declare_directory(go, path = out_lib.basename + ".cgo")
177180
outputs.append(cgo_go_srcs_for_nogo)
178-
args.add("-cgo_go_srcs", cgo_go_srcs_for_nogo.path)
181+
compile_args.add("-cgo_go_srcs", cgo_go_srcs_for_nogo.path)
179182
inputs_transitive.append(cgo_inputs)
180183
inputs_transitive.append(go.cc_toolchain_files)
181184
env["CC"] = go.cgo_tools.c_compiler_path
182185
if cppopts:
183-
args.add("-cppflags", quote_opts(cppopts))
186+
compile_args.add("-cppflags", quote_opts(cppopts))
184187
if copts:
185-
args.add("-cflags", quote_opts(copts))
188+
compile_args.add("-cflags", quote_opts(copts))
186189
if cxxopts:
187-
args.add("-cxxflags", quote_opts(cxxopts))
190+
compile_args.add("-cxxflags", quote_opts(cxxopts))
188191
if objcopts:
189-
args.add("-objcflags", quote_opts(objcopts))
192+
compile_args.add("-objcflags", quote_opts(objcopts))
190193
if objcxxopts:
191-
args.add("-objcxxflags", quote_opts(objcxxopts))
194+
compile_args.add("-objcxxflags", quote_opts(objcxxopts))
192195
if clinkopts:
193-
args.add("-ldflags", quote_opts(clinkopts))
196+
compile_args.add("-ldflags", quote_opts(clinkopts))
194197

195198
if go.mode.pgoprofile:
196-
args.add("-pgoprofile", go.mode.pgoprofile)
199+
compile_args.add("-pgoprofile", go.mode.pgoprofile)
197200
inputs_direct.append(go.mode.pgoprofile)
198201

199202
go.actions.run(
200203
inputs = depset(inputs_direct, transitive = inputs_transitive),
201204
outputs = outputs,
202205
mnemonic = "GoCompilePkgExternal" if is_external_pkg else "GoCompilePkg",
203206
executable = go.toolchain._builder,
204-
arguments = [args],
207+
arguments = ["compilepkg", shared_args, compile_args],
205208
env = env,
206209
toolchain = GO_TOOLCHAIN_LABEL,
207210
execution_requirements = execution_requirements,
@@ -210,14 +213,10 @@ def emit_compilepkg(
210213
if nogo:
211214
_run_nogo(
212215
go,
216+
shared_args = shared_args,
213217
sources = sources,
214218
cgo_go_srcs = cgo_go_srcs_for_nogo,
215-
importpath = importpath,
216-
importmap = importmap,
217219
archives = archives,
218-
recompile_internal_deps = recompile_internal_deps,
219-
cover_mode = cover_mode,
220-
testfilter = testfilter,
221220
out_facts = out_facts,
222221
out_log = out_nogo_log,
223222
out_validation = out_nogo_validation,
@@ -226,15 +225,11 @@ def emit_compilepkg(
226225

227226
def _run_nogo(
228227
go,
228+
shared_args,
229229
*,
230230
sources,
231231
cgo_go_srcs,
232-
importpath,
233-
importmap,
234232
archives,
235-
recompile_internal_deps,
236-
cover_mode,
237-
testfilter,
238233
out_facts,
239234
out_log,
240235
out_validation,
@@ -248,30 +243,15 @@ def _run_nogo(
248243
inputs_transitive = [sdk.tools, sdk.headers, go.stdlib.libs]
249244
outputs = [out_facts, out_log]
250245

251-
args = go.builder_args(go, "nogo", use_path_mapping = True)
252-
args.add_all(sources, before_each = "-src")
246+
nogo_args = go.tool_args(go)
253247
if cgo_go_srcs:
254248
inputs_direct.append(cgo_go_srcs)
255-
args.add_all([cgo_go_srcs], before_each = "-ignore_src")
256-
if cover_mode:
257-
args.add("-cover_mode", cover_mode)
258-
if recompile_internal_deps:
259-
args.add_all(recompile_internal_deps, before_each = "-recompile_internal_deps")
260-
args.add_all(archives, before_each = "-arc", map_each = _archive)
261-
if importpath:
262-
args.add("-importpath", importpath)
263-
else:
264-
args.add("-importpath", go.label.name)
265-
if importmap:
266-
args.add("-p", importmap)
267-
args.add("-package_list", sdk.package_list)
268-
if testfilter:
269-
args.add("-testfilter", testfilter)
249+
nogo_args.add_all([cgo_go_srcs], before_each = "-ignore_src")
270250

271-
args.add_all(archives, before_each = "-facts", map_each = _facts)
272-
args.add("-out_facts", out_facts)
273-
args.add("-out_log", out_log)
274-
args.add("-nogo", nogo)
251+
nogo_args.add_all(archives, before_each = "-facts", map_each = _facts)
252+
nogo_args.add("-out_facts", out_facts)
253+
nogo_args.add("-out_log", out_log)
254+
nogo_args.add("-nogo", nogo)
275255

276256
# This action runs nogo and produces the facts files for downstream nogo actions.
277257
# It is important that this action doesn't fail if nogo produces findings, which allows users
@@ -285,7 +265,7 @@ def _run_nogo(
285265
outputs = outputs,
286266
mnemonic = "RunNogo",
287267
executable = go.toolchain._builder,
288-
arguments = [args],
268+
arguments = ["nogo", shared_args, nogo_args],
289269
env = go.env_for_path_mapping,
290270
toolchain = GO_TOOLCHAIN_LABEL,
291271
execution_requirements = SUPPORTS_PATH_MAPPING_REQUIREMENT,

go/private/context.bzl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,6 @@ def _dirname(file):
170170
def _builder_args(go, command = None, use_path_mapping = False):
171171
args = go.actions.args()
172172
args.use_param_file("-param=%s")
173-
args.set_param_file_format("shell")
174173
if command:
175174
args.add(command)
176175
sdk_root_file = go.sdk.root_file
@@ -187,14 +186,15 @@ def _builder_args(go, command = None, use_path_mapping = False):
187186
# Use a file rather than goroot as the latter is just a string and thus
188187
# not subject to path mapping.
189188
args.add_all("-goroot", [goroot_file], map_each = _dirname, expand_directories = False)
190-
args.add("-installsuffix", installsuffix(go.mode))
191-
args.add_joined("-tags", go.mode.tags, join_with = ",")
189+
mode = go.mode
190+
args.add("-installsuffix", installsuffix(mode))
191+
if mode.tags:
192+
args.add_joined("-tags", mode.tags, join_with = ",")
192193
return args
193194

194195
def _tool_args(go):
195196
args = go.actions.args()
196197
args.use_param_file("-param=%s")
197-
args.set_param_file_format("shell")
198198
return args
199199

200200
def _new_library(go, name = None, importpath = None, resolver = None, importable = True, testfilter = None, is_main = False, **kwargs):

0 commit comments

Comments
 (0)