Allow unlimited number of additional linker inputs for cc_shared_library

RELNOTES:none
PiperOrigin-RevId: 295908805
Change-Id: I42a727d6ab0f4d342036df39a131d66ecd62734e
diff --git a/examples/experimental_cc_shared_library.bzl b/examples/experimental_cc_shared_library.bzl
index 24cb7a6..a95fc3b 100644
--- a/examples/experimental_cc_shared_library.bzl
+++ b/examples/experimental_cc_shared_library.bzl
@@ -289,7 +289,10 @@
         )
         additional_inputs = [ctx.file.visibility_file]
 
-    user_link_flags.extend(ctx.attr.user_link_flags)
+    for user_link_flag in ctx.attr.user_link_flags:
+        user_link_flags.append(ctx.expand_location(user_link_flag, targets = ctx.attr.additional_linker_inputs))
+
+    additional_inputs.extend(ctx.files.additional_linker_inputs)
 
     linking_outputs = cc_common.link(
         actions = ctx.actions,
@@ -365,6 +368,7 @@
 cc_shared_library = rule(
     implementation = _cc_shared_library_impl,
     attrs = {
+        "additional_linker_inputs": attr.label_list(allow_files = True),
         "dynamic_deps": attr.label_list(providers = [CcSharedLibraryInfo]),
         "exports": attr.label_list(providers = [CcInfo], aspects = [graph_structure_aspect]),
         "preloaded_deps": attr.label_list(providers = [CcInfo]),
diff --git a/examples/test_cc_shared_library/BUILD b/examples/test_cc_shared_library/BUILD
index b5c942c..875390b 100644
--- a/examples/test_cc_shared_library/BUILD
+++ b/examples/test_cc_shared_library/BUILD
@@ -1,6 +1,6 @@
 load("//cc:defs.bzl", "cc_binary", "cc_library", "cc_test")
 load("//examples:experimental_cc_shared_library.bzl", "cc_shared_library")
-load(":starlark_tests.bzl", "linking_suffix_test")
+load(":starlark_tests.bzl", "additional_inputs_test", "linking_suffix_test")
 
 cc_test(
     name = "cc_test",
@@ -18,12 +18,16 @@
 
 cc_shared_library(
     name = "foo_so",
+    additional_linker_inputs = [":additional_script.txt"],
     dynamic_deps = ["bar_so"],
     preloaded_deps = ["preloaded_dep"],
     static_deps = [
         "//examples/test_cc_shared_library:qux",
     ],
-    user_link_flags = ["-Wl,-rpath,kittens"],
+    user_link_flags = [
+        "-Wl,-rpath,kittens",
+        "-Wl,--script=$(location :additional_script.txt)",
+    ],
     visibility_file = "foo.lds",
     exports = [
         "foo",
@@ -141,3 +145,8 @@
     name = "linking_action_test",
     target_under_test = ":foo_so",
 )
+
+additional_inputs_test(
+    name = "additional_inputs_test",
+    target_under_test = ":foo_so",
+)
diff --git a/examples/test_cc_shared_library/additional_script.txt b/examples/test_cc_shared_library/additional_script.txt
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/examples/test_cc_shared_library/additional_script.txt
diff --git a/examples/test_cc_shared_library/starlark_tests.bzl b/examples/test_cc_shared_library/starlark_tests.bzl
index 9c9f3ac..3c4cc43 100644
--- a/examples/test_cc_shared_library/starlark_tests.bzl
+++ b/examples/test_cc_shared_library/starlark_tests.bzl
@@ -16,3 +16,21 @@
     return analysistest.end(env)
 
 linking_suffix_test = analysistest.make(_linking_suffix_test_impl)
+
+def _additional_inputs_test_impl(ctx):
+    env = analysistest.begin(ctx)
+
+    target_under_test = analysistest.target_under_test(env)
+    actions = analysistest.target_actions(env)
+
+    found = False
+    for arg in actions[1].argv:
+        if arg.find("-Wl,--script=") != -1:
+            asserts.equals(env, "examples/test_cc_shared_library/additional_script.txt", arg[13:])
+            found = True
+            break
+    asserts.true(env, found, "Should have seen option --script=")
+
+    return analysistest.end(env)
+
+additional_inputs_test = analysistest.make(_additional_inputs_test_impl)