Skip to content

Commit c47faff

Browse files
allightcopybara-github
authored andcommitted
Allow the extra_args flags to suport flags with commas in them.
If something ends with '\' then join it with the next arg. Tested: bazel build //some/opt/target --//xls/common/config:extra_opt_args=--alsologtostderr,--vmodule=pass_base=2\\,narrowing_pass=4 PiperOrigin-RevId: 815777765
1 parent f3d4eac commit c47faff

File tree

3 files changed

+48
-2
lines changed

3 files changed

+48
-2
lines changed

xls/build_rules/xls_codegen_rules.bzl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ load(
2222
"//xls/build_rules:xls_common_rules.bzl",
2323
"append_default_to_args",
2424
"args_to_string",
25+
"fixup_extra_args",
2526
"get_input_infos",
2627
"get_output_filename_value",
2728
"get_runfiles_for_xls",
@@ -431,7 +432,7 @@ def xls_ir_verilog_impl(ctx, src, conv_info):
431432
final_args += " --scheduling_options_used_textproto_file={}".format(
432433
sched_config_textproto_file.path,
433434
)
434-
for v in ctx.attr._extra_codegen_flags[BuildSettingInfo].value:
435+
for v in fixup_extra_args(ctx.attr._extra_codegen_flags[BuildSettingInfo].value):
435436
final_args += " " + v
436437

437438
if "local" in ctx.attr.tags:

xls/build_rules/xls_common_rules.bzl

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,3 +332,47 @@ def get_transitive_built_files_for_xls(ctx, additional_target_list = []):
332332
return None
333333

334334
return transitive_built_files
335+
336+
def _count_trailing(full, piece):
337+
"""Counts the number of trailing occurrences of 'piece' in 'full'.
338+
339+
Args:
340+
full: The string to search within.
341+
piece: The character or substring to count at the end of 'full'.
342+
343+
Returns:
344+
The number of times 'piece' appears at the end of 'full'.
345+
"""
346+
i = 0
347+
for x in reversed(full):
348+
if x != piece:
349+
return i
350+
i += 1
351+
return i
352+
353+
def fixup_extra_args(inp):
354+
"""Fixes up extra arguments by concatenating those ending in a single backslash.
355+
356+
Iterates through the input list of strings. If an element ends with a single
357+
backslash, it is concatenated with the *next* element using a comma.
358+
Elements ending with double backslashes are treated as normal.
359+
360+
Example:
361+
fixup_extra_args(["--a=1", "--b=c=4\\", "d=2", "--c=3"]) -> ["--a=1", "--b=c=4,d=2", "--c=3"]
362+
fixup_extra_args(["--a=\\", "--b=\\", "2"]) -> ["--a=, --b=,2"]
363+
fixup_extra_args(["--a=\\\\", "--b=2"]) -> ["--a=\\\\", "--b=2"]
364+
365+
Args:
366+
inp: A list of strings representing command line arguments.
367+
368+
Returns:
369+
A list of strings with backslash-escaped arguments concatenated.
370+
"""
371+
res = []
372+
for v in inp:
373+
if not res or _count_trailing(full = res[-1], piece = "\\") % 2 == 0:
374+
# No previous element or last element doesn't end with an escaped '\'.
375+
res.append(v)
376+
else:
377+
res[-1] = res[-1][:-1] + "," + v
378+
return res

xls/build_rules/xls_ir_rules.bzl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ load(
2222
"append_cmd_line_args_to",
2323
"append_default_to_args",
2424
"args_to_string",
25+
"fixup_extra_args",
2526
"get_original_input_files_for_xls",
2627
"get_output_filename_value",
2728
"get_runfiles_for_xls",
@@ -319,7 +320,7 @@ def _optimize_ir(ctx, src, original_input_files):
319320
outs.append(pprof_file)
320321
extra_outs.append(pprof_file)
321322
runfiles = get_runfiles_for_xls(ctx, [], [src.ir_file] + ram_rewrite_files + debug_src_files + original_input_files)
322-
for v in ctx.attr._extra_opt_flags[BuildSettingInfo].value:
323+
for v in fixup_extra_args(ctx.attr._extra_opt_flags[BuildSettingInfo].value):
323324
args.add(v)
324325
ctx.actions.run(
325326
outputs = outs,

0 commit comments

Comments
 (0)