diff --git a/cc/toolchains/args/BUILD b/cc/toolchains/args/BUILD index 84560353..a9daf76d 100644 --- a/cc/toolchains/args/BUILD +++ b/cc/toolchains/args/BUILD @@ -1,4 +1,3 @@ -load("//cc/toolchains:feature.bzl", "cc_feature") load("//cc/toolchains:feature_set.bzl", "cc_feature_set") package(default_visibility = ["//visibility:public"]) @@ -15,7 +14,6 @@ package(default_visibility = ["//visibility:public"]) cc_feature_set( name = "experimental_replace_legacy_action_config_features", all_of = [ - ":backfill_legacy_args", "//cc/toolchains/args/archiver_flags:feature", "//cc/toolchains/args/pic_flags:feature", "//cc/toolchains/args/libraries_to_link:feature", @@ -40,14 +38,3 @@ cc_feature_set( "//cc/toolchains/args/compile_flags:feature", # NOTE: This should come below default flags so user flags can override them ], ) - -cc_feature( - name = "backfill_legacy_args", - feature_name = "experimental_replace_legacy_action_config_features", - # TODO: Convert remaining items in this list into their actual args. - implies = [ - "//cc/toolchains/features/legacy:build_interface_libraries", - "//cc/toolchains/features/legacy:dynamic_library_linker_tool", - ], - visibility = ["//visibility:private"], -) diff --git a/cc/toolchains/args/libraries_to_link/BUILD b/cc/toolchains/args/libraries_to_link/BUILD index 6271856a..ca468c30 100644 --- a/cc/toolchains/args/libraries_to_link/BUILD +++ b/cc/toolchains/args/libraries_to_link/BUILD @@ -77,6 +77,7 @@ cc_nested_args( name = "single_library_args", nested = select({ "@platforms//os:macos": [], + "@platforms//os:windows": [], "//conditions:default": [":optional_whole_archive_start"], }) + [ ":optional_object_file_group", @@ -85,11 +86,14 @@ cc_nested_args( ":optional_static_library", ":optional_dynamic_library", ] + select({ - # maOS has a minor nuance where it uses the path to the library instead of `-l:{library_name}`. + # macOS has a minor nuance where it uses the path to the library instead of `-l:{library_name}`. "@platforms//os:macos": [":macos_optional_versioned_dynamic_library"], + # Windows references the interface library (if.lib) rather than the dynamic library (dll) + "@platforms//os:windows": [], "//conditions:default": [":generic_optional_versioned_dynamic_library"], }) + select({ "@platforms//os:macos": [], + "@platforms//os:windows": [], "//conditions:default": [":optional_whole_archive_end"], }), ) diff --git a/cc/toolchains/args/libraries_to_link/private/library_link_args.bzl b/cc/toolchains/args/libraries_to_link/private/library_link_args.bzl index 8b0e4091..e9c590e8 100644 --- a/cc/toolchains/args/libraries_to_link/private/library_link_args.bzl +++ b/cc/toolchains/args/libraries_to_link/private/library_link_args.bzl @@ -49,6 +49,40 @@ def macos_force_load_library_args(name, variable): }, ) +def windows_whole_archive_args(name, variable): + """A helper for declaring /WHOLEARCHIVE argument expansion for a library. + + This creates an argument expansion that will expand to /WHOLEARCHIVE: + if the library should be linked as a whole archive. + + Args: + name: The name of the rule. + variable: The variable to expand. + """ + cc_nested_args( + name = name, + nested = [ + ":{}_whole_archive".format(name), + ":{}_no_whole_archive".format(name), + ], + ) + cc_nested_args( + name = name + "_no_whole_archive", + requires_false = "//cc/toolchains/variables:libraries_to_link.is_whole_archive", + args = ["{library}"], + format = { + "library": variable, + }, + ) + cc_nested_args( + name = name + "_whole_archive", + requires_true = "//cc/toolchains/variables:libraries_to_link.is_whole_archive", + args = ["-Wl,/WHOLEARCHIVE:{library}"], + format = { + "library": variable, + }, + ) + def library_link_args(name, library_type, from_variable, iterate_over_variable = False): """A helper for declaring a library to link. @@ -78,6 +112,7 @@ def library_link_args(name, library_type, from_variable, iterate_over_variable = name = name, actual = select({ "@platforms//os:macos": ":macos_{}".format(name), + "@platforms//os:windows": ":windows_{}".format(name), "//conditions:default": ":generic_{}".format(name), }), ) @@ -102,3 +137,14 @@ def library_link_args(name, library_type, from_variable, iterate_over_variable = name = "{}_maybe_force_load".format(name), variable = from_variable, ) + cc_nested_args( + name = "windows_{}".format(name), + requires_equal = "//cc/toolchains/variables:libraries_to_link.type", + requires_equal_value = library_type, + iterate_over = from_variable if iterate_over_variable else None, + nested = [":{}_maybe_whole_archive".format(name)], + ) + windows_whole_archive_args( + name = "{}_maybe_whole_archive".format(name), + variable = from_variable, + ) diff --git a/cc/toolchains/args/link_flags/BUILD b/cc/toolchains/args/link_flags/BUILD index 58d38a31..832c5715 100644 --- a/cc/toolchains/args/link_flags/BUILD +++ b/cc/toolchains/args/link_flags/BUILD @@ -1,6 +1,7 @@ load("//cc/toolchains:args.bzl", "cc_args") load("//cc/toolchains:feature.bzl", "cc_feature") load("//cc/toolchains:feature_set.bzl", "cc_feature_set") +load("//cc/toolchains:nested_args.bzl", "cc_nested_args") cc_feature_set( name = "feature", @@ -8,6 +9,8 @@ cc_feature_set( ":user_link_flags_feature", ":legacy_link_flags_feature", ":output_link_flags_feature", + ":build_interface_libraries_feature", + ":dynamic_library_linker_tool_feature", ], visibility = ["//visibility:public"], ) @@ -58,3 +61,93 @@ cc_args( format = {"output_execpath": "//cc/toolchains/variables:output_execpath"}, requires_not_none = "//cc/toolchains/variables:output_execpath", ) + +cc_feature( + name = "build_interface_libraries_feature", + args = ["build_interface_libraries"], + overrides = "//cc/toolchains/features/legacy:build_interface_libraries", +) + +cc_args( + name = "build_interface_libraries", + actions = ["//cc/toolchains/actions:dynamic_library_link_actions"], + nested = [":build_interface_libraries_check"], + requires_not_none = "//cc/toolchains/variables:generate_interface_library", +) + +cc_nested_args( + name = "build_interface_libraries_check", + nested = select({ + "@platforms//os:windows": [":windows_build_interface_libraries"], + "//conditions:default": [":generic_build_interface_libraries"], + }), + requires_equal = "//cc/toolchains/variables:generate_interface_library", + requires_equal_value = "yes", +) + +cc_nested_args( + name = "windows_build_interface_libraries", + args = ["-Wl,/IMPLIB:{interface_library_output_path}"], + format = {"interface_library_output_path": "//cc/toolchains/variables:interface_library_output_path"}, + requires_not_none = "//cc/toolchains/variables:interface_library_output_path", +) + +cc_nested_args( + name = "generic_build_interface_libraries", + nested = [ + ":generate_interface_library", + ":interface_library_builder_path", + ":interface_library_input_path", + ":interface_library_output_path", + ], +) + +cc_nested_args( + name = "generate_interface_library", + args = ["{generate_interface_library}"], + format = {"generate_interface_library": "//cc/toolchains/variables:generate_interface_library"}, +) + +cc_nested_args( + name = "interface_library_builder_path", + args = ["{interface_library_builder_path}"], + format = {"interface_library_builder_path": "//cc/toolchains/variables:interface_library_builder_path"}, + requires_not_none = "//cc/toolchains/variables:interface_library_builder_path", +) + +cc_nested_args( + name = "interface_library_input_path", + args = ["{interface_library_input_path}"], + format = {"interface_library_input_path": "//cc/toolchains/variables:interface_library_input_path"}, + requires_not_none = "//cc/toolchains/variables:interface_library_input_path", +) + +cc_nested_args( + name = "interface_library_output_path", + args = ["{interface_library_output_path}"], + format = {"interface_library_output_path": "//cc/toolchains/variables:interface_library_output_path"}, + requires_not_none = "//cc/toolchains/variables:interface_library_output_path", +) + +cc_feature( + name = "dynamic_library_linker_tool_feature", + args = select({ + "@platforms//os:windows": [], + "//conditions:default": ["dynamic_library_linker_tool"], + }), + overrides = "//cc/toolchains/features/legacy:dynamic_library_linker_tool", +) + +cc_args( + name = "dynamic_library_linker_tool", + actions = ["//cc/toolchains/actions:dynamic_library_link_actions"], + nested = [":dynamic_library_linker_tool_check"], + requires_not_none = "//cc/toolchains/variables:generate_interface_library", +) + +cc_nested_args( + name = "dynamic_library_linker_tool_check", + args = [" + cppLinkDynamicLibraryToolPath + "], + requires_equal = "//cc/toolchains/variables:generate_interface_library", + requires_equal_value = "yes", +) diff --git a/cc/toolchains/args/runtime_library_search_directories/BUILD b/cc/toolchains/args/runtime_library_search_directories/BUILD index 51b83ea6..bc13d619 100644 --- a/cc/toolchains/args/runtime_library_search_directories/BUILD +++ b/cc/toolchains/args/runtime_library_search_directories/BUILD @@ -8,7 +8,10 @@ package(default_visibility = ["//visibility:private"]) cc_feature( name = "feature", - args = [":runtime_library_search_directories"], + args = select({ + "@platforms//os:windows": [], + "//conditions:default": [":runtime_library_search_directories"], + }), overrides = "//cc/toolchains/features/legacy:runtime_library_search_directories", visibility = ["//visibility:public"], ) diff --git a/cc/toolchains/args/strip_debug_symbols/BUILD b/cc/toolchains/args/strip_debug_symbols/BUILD index b94c5b19..c3dcaad5 100644 --- a/cc/toolchains/args/strip_debug_symbols/BUILD +++ b/cc/toolchains/args/strip_debug_symbols/BUILD @@ -12,6 +12,9 @@ cc_feature( cc_args( name = "strip_debug_symbols", actions = ["//cc/toolchains/actions:link_actions"], - args = ["-Wl,-S"], + args = select({ + "@platforms//os:windows": ["-Wl,/DEBUG:NONE"], + "//conditions:default": ["-Wl,-S"], + }), requires_not_none = "//cc/toolchains/variables:strip_debug_symbols", ) diff --git a/cc/toolchains/capabilities/BUILD b/cc/toolchains/capabilities/BUILD index 8c55804c..bbae01a8 100644 --- a/cc/toolchains/capabilities/BUILD +++ b/cc/toolchains/capabilities/BUILD @@ -17,3 +17,15 @@ cc_tool_capability( cc_tool_capability( name = "supports_pic", ) + +cc_tool_capability( + name = "has_configured_linker_path", +) + +cc_tool_capability( + name = "targets_windows", +) + +cc_tool_capability( + name = "copy_dynamic_libraries_to_binary", +) diff --git a/tests/rule_based_toolchain/legacy_features_as_args/BUILD b/tests/rule_based_toolchain/legacy_features_as_args/BUILD index 510f0c48..6f2770d9 100644 --- a/tests/rule_based_toolchain/legacy_features_as_args/BUILD +++ b/tests/rule_based_toolchain/legacy_features_as_args/BUILD @@ -32,6 +32,7 @@ compare_feature_implementation( actual_implementation = "//cc/toolchains/args/libraries_to_link", expected = select({ "@platforms//os:macos": "//tests/rule_based_toolchain/legacy_features_as_args:goldens/macos/libraries_to_link.textproto", + "@platforms//os:windows": "//tests/rule_based_toolchain/legacy_features_as_args:goldens/windows/libraries_to_link.textproto", "//conditions:default": "//tests/rule_based_toolchain/legacy_features_as_args:goldens/unix/libraries_to_link.textproto", }), ) diff --git a/tests/rule_based_toolchain/legacy_features_as_args/goldens/windows/libraries_to_link.textproto b/tests/rule_based_toolchain/legacy_features_as_args/goldens/windows/libraries_to_link.textproto new file mode 100644 index 00000000..285f6fec --- /dev/null +++ b/tests/rule_based_toolchain/legacy_features_as_args/goldens/windows/libraries_to_link.textproto @@ -0,0 +1,117 @@ +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" + actions: "objc-executable" + flag_groups { + flag_groups { + expand_if_available: "thinlto_param_file" + flags: "-Wl,@%{thinlto_param_file}" + } + flag_groups { + expand_if_available: "libraries_to_link" + flag_groups { + flag_groups { + expand_if_equal { + name: "libraries_to_link.type" + value: "object_file_group" + } + flag_groups { + expand_if_false: "libraries_to_link.is_whole_archive" + flags: "-Wl,--start-lib" + } + } + flag_groups { + flag_groups { + expand_if_equal { + name: "libraries_to_link.type" + value: "object_file_group" + } + flag_groups { + flag_groups { + expand_if_true: "libraries_to_link.is_whole_archive" + flags: "-Wl,/WHOLEARCHIVE:%{libraries_to_link.object_files}" + } + flag_groups { + expand_if_false: "libraries_to_link.is_whole_archive" + flags: "%{libraries_to_link.object_files}" + } + } + iterate_over: "libraries_to_link.object_files" + } + flag_groups { + expand_if_equal { + name: "libraries_to_link.type" + value: "object_file" + } + flag_groups { + flag_groups { + expand_if_true: "libraries_to_link.is_whole_archive" + flags: "-Wl,/WHOLEARCHIVE:%{libraries_to_link.name}" + } + flag_groups { + expand_if_false: "libraries_to_link.is_whole_archive" + flags: "%{libraries_to_link.name}" + } + } + } + flag_groups { + expand_if_equal { + name: "libraries_to_link.type" + value: "interface_library" + } + flag_groups { + flag_groups { + expand_if_true: "libraries_to_link.is_whole_archive" + flags: "-Wl,/WHOLEARCHIVE:%{libraries_to_link.name}" + } + flag_groups { + expand_if_false: "libraries_to_link.is_whole_archive" + flags: "%{libraries_to_link.name}" + } + } + } + flag_groups { + expand_if_equal { + name: "libraries_to_link.type" + value: "static_library" + } + flag_groups { + flag_groups { + expand_if_true: "libraries_to_link.is_whole_archive" + flags: "-Wl,/WHOLEARCHIVE:%{libraries_to_link.name}" + } + flag_groups { + expand_if_false: "libraries_to_link.is_whole_archive" + flags: "%{libraries_to_link.name}" + } + } + } + flag_groups { + expand_if_equal { + name: "libraries_to_link.type" + value: "dynamic_library" + } + flags: "-l%{libraries_to_link.name}" + } + } + flag_groups { + expand_if_equal { + name: "libraries_to_link.type" + value: "object_file_group" + } + flag_groups { + expand_if_false: "libraries_to_link.is_whole_archive" + flags: "-Wl,--end-lib" + } + } + iterate_over: "libraries_to_link" + } + } + } +} +name: "libraries_to_link_test"