Skip to content

Commit 0589995

Browse files
nicholasjngcopybara-github
authored andcommitted
Add additional_linker_inputs option to cc_library rule
This is achieved by the following related changes: 1) Moving the `additional_linker_inputs` attribute up from the `cc_binary_base` rule to the `cc_rule`. The rationale here is that any rule that allows `linkopts` to be set should also support `additional_linker_inputs`. 2) Adding the `additional_linker_inputs` attr to the `cc_library` rule context. This was copied verbatim from the existing `cc_binary` attribute list. 3) Appending the `ctx.files.additional_linker_inputs` list to the list of linker scripts wherever they are passed as `additional_inputs` in the `cc_library.bzl` builtin file corresponding to the `cc_library` rule. ---------------------- For context on this PR, see issue #17788 as well as the original [Google group discussion](https://groups.google.com/g/bazel-discuss/c/1VFvNBDqzVU/m/F5UmYO-RAAAJ), which inspired the feature request. Resolves #17788. Closes #18952. PiperOrigin-RevId: 557505297 Change-Id: I4811b60e102c6426697efcb202296fe77a3d5f25
1 parent ad6d4d7 commit 0589995

2 files changed

Lines changed: 20 additions & 15 deletions

File tree

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,18 @@ public RuleClass build(RuleClass.Builder builder, final RuleDefinitionEnvironmen
305305
</p>
306306
<!-- #END_BLAZE_RULE.ATTRIBUTE -->*/
307307
.add(attr("linkopts", STRING_LIST))
308+
/*<!-- #BLAZE_RULE($cc_rule).ATTRIBUTE(additional_linker_inputs) -->
309+
Pass these files to the C++ linker command.
310+
<p>
311+
For example, compiled Windows .res files can be provided here to be embedded in
312+
the binary target.
313+
</p>
314+
<!-- #END_BLAZE_RULE.ATTRIBUTE -->*/
315+
.add(
316+
attr("additional_linker_inputs", LABEL_LIST)
317+
.orderIndependent()
318+
.direct_compile_time_input()
319+
.allowedFileTypes(FileTypeSet.ANY_FILE))
308320
/*<!-- #BLAZE_RULE($cc_rule).ATTRIBUTE(nocopts) -->
309321
Remove matching options from the C++ compilation command.
310322
Subject to <a href="${link make-variables}">"Make" variable</a> substitution.
@@ -458,18 +470,6 @@ public static final class CcBinaryBaseRule implements RuleDefinition {
458470
@Override
459471
public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment env) {
460472
return builder
461-
/*<!-- #BLAZE_RULE($cc_binary_base).ATTRIBUTE(additional_linker_inputs) -->
462-
Pass these files to the C++ linker command.
463-
<p>
464-
For example, compiled Windows .res files can be provided here to be embedded in
465-
the binary target.
466-
</p>
467-
<!-- #END_BLAZE_RULE.ATTRIBUTE -->*/
468-
.add(
469-
attr("additional_linker_inputs", LABEL_LIST)
470-
.orderIndependent()
471-
.direct_compile_time_input()
472-
.allowedFileTypes(FileTypeSet.ANY_FILE))
473473
.override(
474474
attr("deps", LABEL_LIST)
475475
.allowedRuleClasses(DEPS_ALLOWED_RULES)

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ def _cc_library_impl(ctx):
148148
compilation_outputs = compilation_outputs,
149149
cc_toolchain = cc_toolchain,
150150
feature_configuration = feature_configuration,
151-
additional_inputs = _filter_linker_scripts(ctx.files.deps),
151+
additional_inputs = _filter_linker_scripts(ctx.files.deps) + ctx.files.additional_linker_inputs,
152152
linking_contexts = linking_contexts,
153153
grep_includes = ctx.executable._grep_includes,
154154
user_link_flags = cc_helper.linkopts(ctx, additional_make_variable_substitutions, cc_toolchain),
@@ -206,11 +206,12 @@ def _cc_library_impl(ctx):
206206
else:
207207
user_link_flags = cc_helper.linkopts(ctx, additional_make_variable_substitutions, cc_toolchain)
208208
linker_scripts = _filter_linker_scripts(ctx.files.deps)
209-
if len(user_link_flags) > 0 or len(linker_scripts) > 0 or not semantics.should_create_empty_archive():
209+
additional_linker_inputs = ctx.files.additional_linker_inputs
210+
if len(user_link_flags) > 0 or len(linker_scripts) > 0 or len(additional_linker_inputs) > 0 or not semantics.should_create_empty_archive():
210211
linker_input = cc_common.create_linker_input(
211212
owner = ctx.label,
212213
user_link_flags = user_link_flags,
213-
additional_inputs = depset(linker_scripts),
214+
additional_inputs = depset(linker_scripts + additional_linker_inputs),
214215
)
215216
contexts_to_merge.append(cc_common.create_linking_context(linker_inputs = depset([linker_input])))
216217

@@ -575,6 +576,10 @@ attrs = {
575576
),
576577
"linkstamp": attr.label(allow_single_file = True),
577578
"linkopts": attr.string_list(),
579+
"additional_linker_inputs": attr.label_list(
580+
allow_files = True,
581+
flags = ["ORDER_INDEPENDENT", "DIRECT_COMPILE_TIME_INPUT"],
582+
),
578583
"includes": attr.string_list(),
579584
"defines": attr.string_list(),
580585
"copts": attr.string_list(),

0 commit comments

Comments
 (0)