Skip to content

Commit

Permalink
Add additional_compiler_inputs attribute for cc_library.
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
brentleyjones committed Jul 10, 2023
1 parent 273d3f7 commit 993b206
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 85 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
7 changes: 5 additions & 2 deletions src/main/starlark/builtins_bzl/common/cc/cc_helper.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -868,16 +868,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 993b206

Please sign in to comment.