Skip to content

Commit 2f9e8c1

Browse files
[6.3.0] Add additional_compiler_inputs attribute for cc_library. (#18882)
* Add additional_compiler_inputs attribute for cc_library. Also, ensure that location function expansion works with these inputs as expected. RELNOTES: Additional source inputs can now be specified for compilation in cc_library targets using the additional_compiler_inputs attribute, and these inputs can be used in the $(location) function. Fixes #18766. PiperOrigin-RevId: 545766084 Change-Id: I2d9f195d81a1358c696601873e60d3cad810a150 (cherry picked from commit ade32e6) Signed-off-by: Brentley Jones <[email protected]> * Add `_expand(targets)` From ff92499. Signed-off-by: Brentley Jones <[email protected]> --------- Signed-off-by: Brentley Jones <[email protected]>
1 parent 237ae6b commit 2f9e8c1

File tree

4 files changed

+118
-90
lines changed

4 files changed

+118
-90
lines changed

src/main/java/com/google/devtools/build/lib/bazel/rules/cpp/BazelCcLibraryRule.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment env)
6363
attr("implementation_deps", LABEL_LIST)
6464
.allowedFileTypes(FileTypeSet.NO_FILE)
6565
.mandatoryProviders(CcInfo.PROVIDER.id()))
66+
/*<!-- #BLAZE_RULE(cc_library).ATTRIBUTE(additional_compiler_inputs) -->
67+
Any additional files you might want to pass to the compiler command line, such as sanitizer
68+
ignorelists, for example. Files specified here can then be used in copts with the
69+
$(location) function.
70+
<!-- #END_BLAZE_RULE.ATTRIBUTE -->*/
71+
.add(attr("additional_compiler_inputs", LABEL_LIST).allowedFileTypes(FileTypeSet.ANY_FILE))
6672
.advertiseStarlarkProvider(CcInfo.PROVIDER.id())
6773
.build();
6874
}

src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -706,16 +706,15 @@ def _get_cc_flags_make_variable(ctx, common, cc_toolchain):
706706
cc_flags.extend(feature_config_cc_flags)
707707
return {"CC_FLAGS": " ".join(cc_flags)}
708708

709-
def _expand_nested_variable(ctx, additional_vars, exp, execpath = True):
709+
def _expand_nested_variable(ctx, additional_vars, exp, execpath = True, targets = []):
710710
# If make variable is predefined path variable(like $(location ...))
711711
# we will expand it first.
712712
if exp.find(" ") != -1:
713713
if not execpath:
714714
if exp.startswith("location"):
715715
exp = exp.replace("location", "rootpath", 1)
716-
targets = []
717716
if ctx.attr.data != None:
718-
targets = ctx.attr.data
717+
targets.extend(ctx.attr.data)
719718
return ctx.expand_location("$({})".format(exp), targets = targets)
720719

721720
# Recursively expand nested make variables, but since there is no recursion
@@ -739,7 +738,7 @@ def _expand_nested_variable(ctx, additional_vars, exp, execpath = True):
739738
fail("potentially unbounded recursion during expansion of {}".format(exp))
740739
return exp
741740

742-
def _expand(ctx, expression, additional_make_variable_substitutions, execpath = True):
741+
def _expand(ctx, expression, additional_make_variable_substitutions, execpath = True, targets = []):
743742
idx = 0
744743
last_make_var_end = 0
745744
result = []
@@ -781,7 +780,7 @@ def _expand(ctx, expression, additional_make_variable_substitutions, execpath =
781780
# last_make_var_end make_var_start make_var_end
782781
result.append(expression[last_make_var_end:make_var_start - 1])
783782
make_var = expression[make_var_start + 1:make_var_end]
784-
exp = _expand_nested_variable(ctx, additional_make_variable_substitutions, make_var, execpath)
783+
exp = _expand_nested_variable(ctx, additional_make_variable_substitutions, make_var, execpath, targets)
785784
result.append(exp)
786785

787786
# Update indexes.
@@ -868,16 +867,19 @@ def _expand_single_make_variable(ctx, token, additional_make_variable_substituti
868867

869868
def _expand_make_variables_for_copts(ctx, tokenization, unexpanded_tokens, additional_make_variable_substitutions):
870869
tokens = []
870+
targets = []
871+
for additional_compiler_input in getattr(ctx.attr, "additional_compiler_inputs", []):
872+
targets.append(additional_compiler_input)
871873
for token in unexpanded_tokens:
872874
if tokenization:
873-
expanded_token = _expand(ctx, token, additional_make_variable_substitutions)
875+
expanded_token = _expand(ctx, token, additional_make_variable_substitutions, targets = targets)
874876
_tokenize(tokens, expanded_token)
875877
else:
876878
exp = _expand_single_make_variable(ctx, token, additional_make_variable_substitutions)
877879
if exp != None:
878880
_tokenize(tokens, exp)
879881
else:
880-
tokens.append(_expand(ctx, token, additional_make_variable_substitutions))
882+
tokens.append(_expand(ctx, token, additional_make_variable_substitutions, targets = targets))
881883
return tokens
882884

883885
def _get_copts(ctx, common, feature_configuration, additional_make_variable_substitutions):

src/main/starlark/builtins_bzl/common/cc/cc_library.bzl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ def _cc_library_impl(ctx):
7272
textual_hdrs = ctx.files.textual_hdrs,
7373
include_prefix = ctx.attr.include_prefix,
7474
strip_include_prefix = ctx.attr.strip_include_prefix,
75+
additional_inputs = ctx.files.additional_compiler_inputs,
7576
)
7677

7778
precompiled_objects = cc_common.create_compilation_outputs(
@@ -600,6 +601,10 @@ attrs = {
600601
),
601602
"win_def_file": attr.label(allow_single_file = [".def"]),
602603
"licenses": attr.license() if hasattr(attr, "license") else attr.string_list(),
604+
"additional_compiler_inputs": attr.label_list(
605+
allow_files = True,
606+
flags = ["ORDER_INDEPENDENT", "DIRECT_COMPILE_TIME_INPUT"],
607+
),
603608
"_stl": semantics.get_stl(),
604609
"_grep_includes": attr.label(
605610
allow_files = True,

0 commit comments

Comments
 (0)