diff --git a/cc/toolchains/args/BUILD b/cc/toolchains/args/BUILD index a0e48a2..3fd023d 100644 --- a/cc/toolchains/args/BUILD +++ b/cc/toolchains/args/BUILD @@ -17,6 +17,7 @@ cc_feature( "//cc/toolchains/args/archiver_flags", "//cc/toolchains/args/force_pic_flags", "//cc/toolchains/args/linker_param_file", + "//cc/toolchains/args/runtime_library_search_directories", "//cc/toolchains/args/shared_flag", ], feature_name = "experimental_replace_legacy_action_config_features", @@ -37,6 +38,5 @@ cc_feature( "//cc/toolchains/features/legacy:user_compile_flags", "//cc/toolchains/features/legacy:user_link_flags", "//cc/toolchains/features/legacy:libraries_to_link", - "//cc/toolchains/features/legacy:runtime_library_search_directories", ], ) diff --git a/cc/toolchains/args/runtime_library_search_directories/BUILD b/cc/toolchains/args/runtime_library_search_directories/BUILD new file mode 100644 index 0000000..a6bc392 --- /dev/null +++ b/cc/toolchains/args/runtime_library_search_directories/BUILD @@ -0,0 +1,122 @@ +load("//cc/toolchains:args.bzl", "cc_args") +load("//cc/toolchains:args_list.bzl", "cc_args_list") +load("//cc/toolchains:feature_constraint.bzl", "cc_feature_constraint") +load("//cc/toolchains:nested_args.bzl", "cc_nested_args") + +package(default_visibility = ["//visibility:private"]) + +# TODO: b/27153401 - The implementation of this is particularly complex because +# of what appears to be a workaround where macOS cc_test targets with +# static_link_cpp_runtimes enabled utilize a $EXEC_ORIGIN/ prefix. This can be +# paired down significantly after it is clear this workaround is no longer +# required. + +cc_feature_constraint( + name = "static_link_cpp_runtimes_enabled", + all_of = ["//cc/toolchains/features:static_link_cpp_runtimes"], +) + +cc_feature_constraint( + name = "static_link_cpp_runtimes_disabled", + none_of = ["//cc/toolchains/features:static_link_cpp_runtimes"], +) + +cc_args_list( + name = "runtime_library_search_directories", + args = [ + ":runtime_library_search_directories_static_runtimes_args", + ":runtime_library_search_directories_args", + ], + visibility = ["//visibility:public"], +) + +cc_args( + name = "runtime_library_search_directories_static_runtimes_args", + actions = [ + "//cc/toolchains/actions:cpp_link_executable", + "//cc/toolchains/actions:cpp_link_dynamic_library", + "//cc/toolchains/actions:cpp_link_nodeps_dynamic_library", + "//cc/toolchains/actions:lto_index_for_executable", + "//cc/toolchains/actions:lto_index_for_dynamic_library", + "//cc/toolchains/actions:lto_index_for_nodeps_dynamic_library", + ], + nested = [":iterate_over_search_dirs"], + requires_any_of = [":static_link_cpp_runtimes_enabled"], + requires_not_none = "//cc/toolchains/variables:runtime_library_search_directories", +) + +cc_nested_args( + name = "iterate_over_search_dirs", + iterate_over = "//cc/toolchains/variables:runtime_library_search_directories", + nested = [ + ":unit_test_static_runtime_search_dir_args", + ":static_runtime_search_dir_args", + ], +) + +cc_nested_args( + name = "unit_test_static_runtime_search_dir_args", + args = [ + "-Xlinker", + "-rpath", + "-Xlinker", + # TODO(b/27153401): This should probably be @loader_path on osx. + "$EXEC_ORIGIN/{search_path}", + ], + format = { + "search_path": "//cc/toolchains/variables:runtime_library_search_directories", + }, + requires_true = "//cc/toolchains/variables:is_cc_test", +) + +cc_nested_args( + name = "static_runtime_search_dir_args", + args = [ + "-Xlinker", + "-rpath", + "-Xlinker", + ] + select({ + "@platforms//os:macos": ["@loader_path/{search_path}"], + "//conditions:default": ["$EXEC_ORIGIN/{search_path}"], + }), + format = { + "search_path": "//cc/toolchains/variables:runtime_library_search_directories", + }, + requires_false = "//cc/toolchains/variables:is_cc_test", +) + +# TODO: b/27153401 - runtime_library_search_directories_args and +# search_dir_args are all we need to keep if the workaround is no +# longer required. +cc_args( + name = "runtime_library_search_directories_args", + actions = [ + "//cc/toolchains/actions:cpp_link_executable", + "//cc/toolchains/actions:cpp_link_dynamic_library", + "//cc/toolchains/actions:cpp_link_nodeps_dynamic_library", + "//cc/toolchains/actions:lto_index_for_executable", + "//cc/toolchains/actions:lto_index_for_dynamic_library", + "//cc/toolchains/actions:lto_index_for_nodeps_dynamic_library", + ], + nested = [":search_dir_args"], + # Remove the requires_any_of here if the workaround for b/27153401 is no + # longer required. + requires_any_of = [":static_link_cpp_runtimes_disabled"], + requires_not_none = "//cc/toolchains/variables:runtime_library_search_directories", +) + +cc_nested_args( + name = "search_dir_args", + args = [ + "-Xlinker", + "-rpath", + "-Xlinker", + ] + select({ + "@platforms//os:macos": ["@loader_path/{search_path}"], + "//conditions:default": ["$EXEC_ORIGIN/{search_path}"], + }), + format = { + "search_path": "//cc/toolchains/variables:runtime_library_search_directories", + }, + iterate_over = "//cc/toolchains/variables:runtime_library_search_directories", +) diff --git a/cc/toolchains/features/legacy/BUILD b/cc/toolchains/features/legacy/BUILD index f0f0041..cb6e918 100644 --- a/cc/toolchains/features/legacy/BUILD +++ b/cc/toolchains/features/legacy/BUILD @@ -116,6 +116,7 @@ cc_external_feature( cc_external_feature( name = "runtime_library_search_directories", + deprecation = "Use //cc/toolchains/args/runtime_library_search_directories instead", feature_name = "runtime_library_search_directories", overridable = True, ) diff --git a/tests/rule_based_toolchain/legacy_features_as_args/BUILD b/tests/rule_based_toolchain/legacy_features_as_args/BUILD index acbe297..65ef22a 100644 --- a/tests/rule_based_toolchain/legacy_features_as_args/BUILD +++ b/tests/rule_based_toolchain/legacy_features_as_args/BUILD @@ -33,6 +33,15 @@ compare_feature_implementation( expected = "//tests/rule_based_toolchain/legacy_features_as_args:goldens/unix/linker_param_file.textproto", ) +compare_feature_implementation( + name = "runtime_library_search_directories_test", + actual_implementation = "//cc/toolchains/args/runtime_library_search_directories", + expected = select({ + "@platforms//os:macos": "//tests/rule_based_toolchain/legacy_features_as_args:goldens/macos/runtime_library_search_directories.textproto", + "//conditions:default": "//tests/rule_based_toolchain/legacy_features_as_args:goldens/unix/runtime_library_search_directories.textproto", + }), +) + compare_feature_implementation( name = "shared_flag_test", actual_implementation = "//cc/toolchains/args/shared_flag", diff --git a/tests/rule_based_toolchain/legacy_features_as_args/goldens/macos/runtime_library_search_directories.textproto b/tests/rule_based_toolchain/legacy_features_as_args/goldens/macos/runtime_library_search_directories.textproto new file mode 100644 index 0000000..520de8e --- /dev/null +++ b/tests/rule_based_toolchain/legacy_features_as_args/goldens/macos/runtime_library_search_directories.textproto @@ -0,0 +1,54 @@ +enabled: false +flag_sets { + actions: "c++-link-dynamic-library" + actions: "c++-link-executable" + actions: "c++-link-nodeps-dynamic-library" + actions: "lto-index-for-dynamic-library" + actions: "lto-index-for-executable" + actions: "lto-index-for-nodeps-dynamic-library" + flag_groups { + expand_if_available: "runtime_library_search_directories" + flag_groups { + flag_groups { + expand_if_true: "is_cc_test" + flags: "-Xlinker" + flags: "-rpath" + flags: "-Xlinker" + flags: "$EXEC_ORIGIN/%{runtime_library_search_directories}" + } + flag_groups { + expand_if_false: "is_cc_test" + flags: "-Xlinker" + flags: "-rpath" + flags: "-Xlinker" + flags: "@loader_path/%{runtime_library_search_directories}" + } + iterate_over: "runtime_library_search_directories" + } + } + with_features { + features: "static_link_cpp_runtimes" + } +} +flag_sets { + actions: "c++-link-dynamic-library" + actions: "c++-link-executable" + actions: "c++-link-nodeps-dynamic-library" + actions: "lto-index-for-dynamic-library" + actions: "lto-index-for-executable" + actions: "lto-index-for-nodeps-dynamic-library" + flag_groups { + expand_if_available: "runtime_library_search_directories" + flag_groups { + flags: "-Xlinker" + flags: "-rpath" + flags: "-Xlinker" + flags: "@loader_path/%{runtime_library_search_directories}" + iterate_over: "runtime_library_search_directories" + } + } + with_features { + not_features: "static_link_cpp_runtimes" + } +} +name: "runtime_library_search_directories_test" diff --git a/tests/rule_based_toolchain/legacy_features_as_args/goldens/unix/runtime_library_search_directories.textproto b/tests/rule_based_toolchain/legacy_features_as_args/goldens/unix/runtime_library_search_directories.textproto new file mode 100644 index 0000000..1c69c14 --- /dev/null +++ b/tests/rule_based_toolchain/legacy_features_as_args/goldens/unix/runtime_library_search_directories.textproto @@ -0,0 +1,54 @@ +enabled: false +flag_sets { + actions: "c++-link-dynamic-library" + actions: "c++-link-executable" + actions: "c++-link-nodeps-dynamic-library" + actions: "lto-index-for-dynamic-library" + actions: "lto-index-for-executable" + actions: "lto-index-for-nodeps-dynamic-library" + flag_groups { + expand_if_available: "runtime_library_search_directories" + flag_groups { + flag_groups { + expand_if_true: "is_cc_test" + flags: "-Xlinker" + flags: "-rpath" + flags: "-Xlinker" + flags: "$EXEC_ORIGIN/%{runtime_library_search_directories}" + } + flag_groups { + expand_if_false: "is_cc_test" + flags: "-Xlinker" + flags: "-rpath" + flags: "-Xlinker" + flags: "$EXEC_ORIGIN/%{runtime_library_search_directories}" + } + iterate_over: "runtime_library_search_directories" + } + } + with_features { + features: "static_link_cpp_runtimes" + } +} +flag_sets { + actions: "c++-link-dynamic-library" + actions: "c++-link-executable" + actions: "c++-link-nodeps-dynamic-library" + actions: "lto-index-for-dynamic-library" + actions: "lto-index-for-executable" + actions: "lto-index-for-nodeps-dynamic-library" + flag_groups { + expand_if_available: "runtime_library_search_directories" + flag_groups { + flags: "-Xlinker" + flags: "-rpath" + flags: "-Xlinker" + flags: "$EXEC_ORIGIN/%{runtime_library_search_directories}" + iterate_over: "runtime_library_search_directories" + } + } + with_features { + not_features: "static_link_cpp_runtimes" + } +} +name: "runtime_library_search_directories_test"