Skip to content

Commit

Permalink
[6.3.0] Add additional_compiler_inputs attribute for cc_library. (#18882
Browse files Browse the repository at this point in the history
)

* 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]>
  • Loading branch information
brentleyjones authored Jul 10, 2023
1 parent 237ae6b commit 2f9e8c1
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment env)
attr("implementation_deps", LABEL_LIST)
.allowedFileTypes(FileTypeSet.NO_FILE)
.mandatoryProviders(CcInfo.PROVIDER.id()))
/*<!-- #BLAZE_RULE(cc_library).ATTRIBUTE(additional_compiler_inputs) -->
Any additional files you might want to pass to the compiler command line, such as sanitizer
ignorelists, for example. Files specified here can then be used in copts with the
$(location) function.
<!-- #END_BLAZE_RULE.ATTRIBUTE -->*/
.add(attr("additional_compiler_inputs", LABEL_LIST).allowedFileTypes(FileTypeSet.ANY_FILE))
.advertiseStarlarkProvider(CcInfo.PROVIDER.id())
.build();
}
Expand Down
16 changes: 9 additions & 7 deletions src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -706,16 +706,15 @@ def _get_cc_flags_make_variable(ctx, common, cc_toolchain):
cc_flags.extend(feature_config_cc_flags)
return {"CC_FLAGS": " ".join(cc_flags)}

def _expand_nested_variable(ctx, additional_vars, exp, execpath = True):
def _expand_nested_variable(ctx, additional_vars, exp, execpath = True, targets = []):
# If make variable is predefined path variable(like $(location ...))
# we will expand it first.
if exp.find(" ") != -1:
if not execpath:
if exp.startswith("location"):
exp = exp.replace("location", "rootpath", 1)
targets = []
if ctx.attr.data != None:
targets = ctx.attr.data
targets.extend(ctx.attr.data)
return ctx.expand_location("$({})".format(exp), targets = targets)

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

def _expand(ctx, expression, additional_make_variable_substitutions, execpath = True):
def _expand(ctx, expression, additional_make_variable_substitutions, execpath = True, targets = []):
idx = 0
last_make_var_end = 0
result = []
Expand Down Expand Up @@ -781,7 +780,7 @@ def _expand(ctx, expression, additional_make_variable_substitutions, execpath =
# last_make_var_end make_var_start make_var_end
result.append(expression[last_make_var_end:make_var_start - 1])
make_var = expression[make_var_start + 1:make_var_end]
exp = _expand_nested_variable(ctx, additional_make_variable_substitutions, make_var, execpath)
exp = _expand_nested_variable(ctx, additional_make_variable_substitutions, make_var, execpath, targets)
result.append(exp)

# Update indexes.
Expand Down Expand Up @@ -868,16 +867,19 @@ def _expand_single_make_variable(ctx, token, additional_make_variable_substituti

def _expand_make_variables_for_copts(ctx, tokenization, unexpanded_tokens, additional_make_variable_substitutions):
tokens = []
targets = []
for additional_compiler_input in getattr(ctx.attr, "additional_compiler_inputs", []):
targets.append(additional_compiler_input)
for token in unexpanded_tokens:
if tokenization:
expanded_token = _expand(ctx, token, additional_make_variable_substitutions)
expanded_token = _expand(ctx, token, additional_make_variable_substitutions, targets = targets)
_tokenize(tokens, expanded_token)
else:
exp = _expand_single_make_variable(ctx, token, additional_make_variable_substitutions)
if exp != None:
_tokenize(tokens, exp)
else:
tokens.append(_expand(ctx, token, additional_make_variable_substitutions))
tokens.append(_expand(ctx, token, additional_make_variable_substitutions, targets = targets))
return tokens

def _get_copts(ctx, common, feature_configuration, additional_make_variable_substitutions):
Expand Down
5 changes: 5 additions & 0 deletions src/main/starlark/builtins_bzl/common/cc/cc_library.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def _cc_library_impl(ctx):
textual_hdrs = ctx.files.textual_hdrs,
include_prefix = ctx.attr.include_prefix,
strip_include_prefix = ctx.attr.strip_include_prefix,
additional_inputs = ctx.files.additional_compiler_inputs,
)

precompiled_objects = cc_common.create_compilation_outputs(
Expand Down Expand Up @@ -600,6 +601,10 @@ attrs = {
),
"win_def_file": attr.label(allow_single_file = [".def"]),
"licenses": attr.license() if hasattr(attr, "license") else attr.string_list(),
"additional_compiler_inputs": attr.label_list(
allow_files = True,
flags = ["ORDER_INDEPENDENT", "DIRECT_COMPILE_TIME_INPUT"],
),
"_stl": semantics.get_stl(),
"_grep_includes": attr.label(
allow_files = True,
Expand Down
Loading

0 comments on commit 2f9e8c1

Please sign in to comment.