Add user_link_flags attribute to cc_shared_library

PiperOrigin-RevId: 289444795
Change-Id: I705a315b58212c60f63e63a371e1c8dc6e00f48e
diff --git a/examples/experimental_cc_shared_library.bzl b/examples/experimental_cc_shared_library.bzl
index aff1f78..0e30bde 100644
--- a/examples/experimental_cc_shared_library.bzl
+++ b/examples/experimental_cc_shared_library.bzl
@@ -223,14 +223,18 @@
 
     linking_context = _create_linker_context(ctx, static_linker_inputs, dynamic_linker_inputs)
 
+    # TODO(plf): Decide whether ctx.attr.user_link_flags should come before or after options
+    # added by the rule logic.
     user_link_flags = []
     additional_inputs = []
     if ctx.file.visibility_file != None:
-        user_link_flags = [
+        user_link_flags.append(
             "-Wl,--version-script=" + ctx.file.visibility_file.path,
-        ]
+        )
         additional_inputs = [ctx.file.visibility_file]
 
+    user_link_flags.extend(ctx.attr.user_link_flags)
+
     linking_outputs = cc_common.link(
         actions = ctx.actions,
         feature_configuration = feature_configuration,
@@ -296,6 +300,7 @@
     attrs = {
         "dynamic_deps": attr.label_list(providers = [CcSharedLibraryInfo]),
         "preloaded_deps": attr.label_list(providers = [CcInfo]),
+        "user_link_flags": attr.string_list(),
         "visibility_file": attr.label(allow_single_file = True),
         "exports": attr.label_list(providers = [CcInfo], aspects = [graph_structure_aspect]),
         "_cc_toolchain": attr.label(default = "@bazel_tools//tools/cpp:current_cc_toolchain"),
diff --git a/examples/test_cc_shared_library/BUILD b/examples/test_cc_shared_library/BUILD
index 137a9ca..8d9b06f 100644
--- a/examples/test_cc_shared_library/BUILD
+++ b/examples/test_cc_shared_library/BUILD
@@ -31,6 +31,7 @@
         # changed, the symbols from bar4 won't be exported.
         "bar4",
     ],
+    user_link_flags = ["-Wl,-rpath,kittens"],
 )
 
 cc_library(
diff --git a/examples/test_cc_shared_library/cc_shared_library_integration_test.sh b/examples/test_cc_shared_library/cc_shared_library_integration_test.sh
index f999b03..9930ffc 100755
--- a/examples/test_cc_shared_library/cc_shared_library_integration_test.sh
+++ b/examples/test_cc_shared_library/cc_shared_library_integration_test.sh
@@ -43,6 +43,13 @@
   check_symbol_absent "$symbols" "_Z4bar4v"
 }
 
+function test_shared_library_user_link_flags() {
+  foo_so=$(find . -name libfoo_so.so)
+  objdump -x $foo_so | grep RUNPATH | grep "kittens" > /dev/null \
+      || (echo "Expected to have RUNPATH contain 'kittens' (set by user_link_flags)" \
+          && exit 1)
+}
+
 function test_binary() {
   binary=$(find . -name binary)
   symbols=$(nm -D $binary)
@@ -51,5 +58,6 @@
   $binary | (grep -q "hello 42" || (echo "Expected 'hello 42'" && exit 1))
 }
 
+test_shared_library_user_link_flags
 test_shared_library_symbols
 test_binary