Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 0 additions & 13 deletions cc/toolchains/args/BUILD
Original file line number Diff line number Diff line change
@@ -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"])
Expand All @@ -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",
Expand All @@ -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"],
)
6 changes: 5 additions & 1 deletion cc/toolchains/args/libraries_to_link/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"],
}),
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:<library>
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.

Expand Down Expand Up @@ -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),
}),
)
Expand All @@ -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,
)
93 changes: 93 additions & 0 deletions cc/toolchains/args/link_flags/BUILD
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
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",
all_of = [
":user_link_flags_feature",
":legacy_link_flags_feature",
":output_link_flags_feature",
":build_interface_libraries_feature",
":dynamic_library_linker_tool_feature",
],
visibility = ["//visibility:public"],
)
Expand Down Expand Up @@ -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",
],
)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I copied this from the legacy toolchain config, I don't know enough about how it works to be sure my implementation is correct:

build_interface_libraries_feature = feature(
name = "build_interface_libraries",
flag_sets = [
flag_set(
actions = [
ACTION_NAMES.cpp_link_dynamic_library,
ACTION_NAMES.cpp_link_nodeps_dynamic_library,
ACTION_NAMES.lto_index_for_dynamic_library,
ACTION_NAMES.lto_index_for_nodeps_dynamic_library,
],
flag_groups = [
flag_group(
flags = [
"%{generate_interface_library}",
"%{interface_library_builder_path}",
"%{interface_library_input_path}",
"%{interface_library_output_path}",
],
expand_if_available = "generate_interface_library",
),
],
with_features = [
with_feature_set(
features = ["supports_interface_shared_libraries"],
),
],
),
],
)


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",
)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I based this on the legacy toolchain config as well:

dynamic_library_linker_tool_feature = feature(
name = "dynamic_library_linker_tool",
flag_sets = [
flag_set(
actions = [
ACTION_NAMES.cpp_link_dynamic_library,
ACTION_NAMES.cpp_link_nodeps_dynamic_library,
ACTION_NAMES.lto_index_for_dynamic_library,
ACTION_NAMES.lto_index_for_nodeps_dynamic_library,
],
flag_groups = [
flag_group(
flags = [" + cppLinkDynamicLibraryToolPath + "],
expand_if_available = "generate_interface_library",
),
],
with_features = [
with_feature_set(
features = ["supports_interface_shared_libraries"],
),
],
),
],
)

Original file line number Diff line number Diff line change
Expand Up @@ -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"],
)
Expand Down
5 changes: 4 additions & 1 deletion cc/toolchains/args/strip_debug_symbols/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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"],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not totally sure how this should be handled because this will not work with normal clang.exe and gcc.exe (if youre compiling with mingw).

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I hinted at this in the PR description--the idea is to use a variable. Bazel can be modified to include the compiler variable from create_cc_toolchain_config_info. After that's available, the args can be conditioned on that with something like:

cc_args(
    name = "mingw_args",
    require_equal = "//toolchain/variables:compiler",
    require_equal_value = "mingw",
    args = LINUX_ARGS,
 )
 
 cc_args(
    name = "msvc_args",
    require_equal = "//toolchain/variables:compiler",
    require_equal_value = "msvc",
    args = MSVC_ARGS,
 )
 
 cc_args_list(
     name = "windows_args",
     args = ["mingw_args", "msvc_args"],
 )

I'd plan on implementing a macro to generally support this, since there would be so many instances of it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately I'm not really sure how to make Bazel support that, but if someone else is willing to do that, I'd be happy to do the rules_cc side :)

"//conditions:default": ["-Wl,-S"],
}),
requires_not_none = "//cc/toolchains/variables:strip_debug_symbols",
)
12 changes: 12 additions & 0 deletions cc/toolchains/capabilities/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)
1 change: 1 addition & 0 deletions tests/rule_based_toolchain/legacy_features_as_args/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -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",
}),
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
enabled: false
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did not compare this to the legacy textproto, I'm not sure how to do that, but I'm sure it will look different if the legacy assumes only MSVC

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"