From 05899953a1726328b0ff9d940720e363260ce5c4 Mon Sep 17 00:00:00 2001 From: Nicholas Junge Date: Wed, 16 Aug 2023 09:07:55 -0700 Subject: [PATCH] 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 --- .../bazel/rules/cpp/BazelCppRuleClasses.java | 24 +++++++++---------- .../builtins_bzl/common/cc/cc_library.bzl | 11 ++++++--- 2 files changed, 20 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/bazel/rules/cpp/BazelCppRuleClasses.java b/src/main/java/com/google/devtools/build/lib/bazel/rules/cpp/BazelCppRuleClasses.java index b55ec25cd9e089..4719125ed3a7ed 100644 --- a/src/main/java/com/google/devtools/build/lib/bazel/rules/cpp/BazelCppRuleClasses.java +++ b/src/main/java/com/google/devtools/build/lib/bazel/rules/cpp/BazelCppRuleClasses.java @@ -305,6 +305,18 @@ public RuleClass build(RuleClass.Builder builder, final RuleDefinitionEnvironmen

*/ .add(attr("linkopts", STRING_LIST)) + /* + Pass these files to the C++ linker command. +

+ For example, compiled Windows .res files can be provided here to be embedded in + the binary target. +

+ */ + .add( + attr("additional_linker_inputs", LABEL_LIST) + .orderIndependent() + .direct_compile_time_input() + .allowedFileTypes(FileTypeSet.ANY_FILE)) /* Remove matching options from the C++ compilation command. Subject to "Make" variable substitution. @@ -458,18 +470,6 @@ public static final class CcBinaryBaseRule implements RuleDefinition { @Override public RuleClass build(RuleClass.Builder builder, RuleDefinitionEnvironment env) { return builder - /* - Pass these files to the C++ linker command. -

- For example, compiled Windows .res files can be provided here to be embedded in - the binary target. -

- */ - .add( - attr("additional_linker_inputs", LABEL_LIST) - .orderIndependent() - .direct_compile_time_input() - .allowedFileTypes(FileTypeSet.ANY_FILE)) .override( attr("deps", LABEL_LIST) .allowedRuleClasses(DEPS_ALLOWED_RULES) diff --git a/src/main/starlark/builtins_bzl/common/cc/cc_library.bzl b/src/main/starlark/builtins_bzl/common/cc/cc_library.bzl index f36de71a639c59..c55f3f7ebe0eea 100755 --- a/src/main/starlark/builtins_bzl/common/cc/cc_library.bzl +++ b/src/main/starlark/builtins_bzl/common/cc/cc_library.bzl @@ -148,7 +148,7 @@ def _cc_library_impl(ctx): compilation_outputs = compilation_outputs, cc_toolchain = cc_toolchain, feature_configuration = feature_configuration, - additional_inputs = _filter_linker_scripts(ctx.files.deps), + additional_inputs = _filter_linker_scripts(ctx.files.deps) + ctx.files.additional_linker_inputs, linking_contexts = linking_contexts, grep_includes = ctx.executable._grep_includes, user_link_flags = cc_helper.linkopts(ctx, additional_make_variable_substitutions, cc_toolchain), @@ -206,11 +206,12 @@ def _cc_library_impl(ctx): else: user_link_flags = cc_helper.linkopts(ctx, additional_make_variable_substitutions, cc_toolchain) linker_scripts = _filter_linker_scripts(ctx.files.deps) - if len(user_link_flags) > 0 or len(linker_scripts) > 0 or not semantics.should_create_empty_archive(): + additional_linker_inputs = ctx.files.additional_linker_inputs + if len(user_link_flags) > 0 or len(linker_scripts) > 0 or len(additional_linker_inputs) > 0 or not semantics.should_create_empty_archive(): linker_input = cc_common.create_linker_input( owner = ctx.label, user_link_flags = user_link_flags, - additional_inputs = depset(linker_scripts), + additional_inputs = depset(linker_scripts + additional_linker_inputs), ) contexts_to_merge.append(cc_common.create_linking_context(linker_inputs = depset([linker_input]))) @@ -575,6 +576,10 @@ attrs = { ), "linkstamp": attr.label(allow_single_file = True), "linkopts": attr.string_list(), + "additional_linker_inputs": attr.label_list( + allow_files = True, + flags = ["ORDER_INDEPENDENT", "DIRECT_COMPILE_TIME_INPUT"], + ), "includes": attr.string_list(), "defines": attr.string_list(), "copts": attr.string_list(),