Skip to content

Commit e7a849b

Browse files
committed
Support linking on Windows
1 parent bdd93e8 commit e7a849b

File tree

8 files changed

+165
-16
lines changed

8 files changed

+165
-16
lines changed

cc/toolchains/args/BUILD

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ package(default_visibility = ["//visibility:public"])
1515
cc_feature_set(
1616
name = "experimental_replace_legacy_action_config_features",
1717
all_of = [
18-
":backfill_legacy_args",
1918
"//cc/toolchains/args/archiver_flags:feature",
2019
"//cc/toolchains/args/pic_flags:feature",
2120
"//cc/toolchains/args/libraries_to_link:feature",
@@ -40,14 +39,3 @@ cc_feature_set(
4039
"//cc/toolchains/args/compile_flags:feature", # NOTE: This should come below default flags so user flags can override them
4140
],
4241
)
43-
44-
cc_feature(
45-
name = "backfill_legacy_args",
46-
feature_name = "experimental_replace_legacy_action_config_features",
47-
# TODO: Convert remaining items in this list into their actual args.
48-
implies = [
49-
"//cc/toolchains/features/legacy:build_interface_libraries",
50-
"//cc/toolchains/features/legacy:dynamic_library_linker_tool",
51-
],
52-
visibility = ["//visibility:private"],
53-
)

cc/toolchains/args/libraries_to_link/BUILD

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ cc_nested_args(
7777
name = "single_library_args",
7878
nested = select({
7979
"@platforms//os:macos": [],
80+
"@platforms//os:windows": [],
8081
"//conditions:default": [":optional_whole_archive_start"],
8182
}) + [
8283
":optional_object_file_group",
@@ -85,11 +86,14 @@ cc_nested_args(
8586
":optional_static_library",
8687
":optional_dynamic_library",
8788
] + select({
88-
# maOS has a minor nuance where it uses the path to the library instead of `-l:{library_name}`.
89+
# macOS has a minor nuance where it uses the path to the library instead of `-l:{library_name}`.
8990
"@platforms//os:macos": [":macos_optional_versioned_dynamic_library"],
91+
# Windows references the interface library (if.lib) rather than the dynamic library (dll)
92+
"@platforms//os:windows": [],
9093
"//conditions:default": [":generic_optional_versioned_dynamic_library"],
9194
}) + select({
9295
"@platforms//os:macos": [],
96+
"@platforms//os:windows": [],
9397
"//conditions:default": [":optional_whole_archive_end"],
9498
}),
9599
)

cc/toolchains/args/libraries_to_link/private/library_link_args.bzl

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,40 @@ def macos_force_load_library_args(name, variable):
4949
},
5050
)
5151

52+
def windows_whole_archive_args(name, variable):
53+
"""A helper for declaring /WHOLEARCHIVE argument expansion for a library.
54+
55+
This creates an argument expansion that will expand to /WHOLEARCHIVE:<library>
56+
if the library should be linked as a whole archive.
57+
58+
Args:
59+
name: The name of the rule.
60+
variable: The variable to expand.
61+
"""
62+
cc_nested_args(
63+
name = name,
64+
nested = [
65+
":{}_whole_archive".format(name),
66+
":{}_no_whole_archive".format(name),
67+
],
68+
)
69+
cc_nested_args(
70+
name = name + "_no_whole_archive",
71+
requires_false = "//cc/toolchains/variables:libraries_to_link.is_whole_archive",
72+
args = ["{library}"],
73+
format = {
74+
"library": variable,
75+
},
76+
)
77+
cc_nested_args(
78+
name = name + "_whole_archive",
79+
requires_true = "//cc/toolchains/variables:libraries_to_link.is_whole_archive",
80+
args = ["-Wl,/WHOLEARCHIVE:{library}"],
81+
format = {
82+
"library": variable,
83+
},
84+
)
85+
5286
def library_link_args(name, library_type, from_variable, iterate_over_variable = False):
5387
"""A helper for declaring a library to link.
5488
@@ -78,6 +112,7 @@ def library_link_args(name, library_type, from_variable, iterate_over_variable =
78112
name = name,
79113
actual = select({
80114
"@platforms//os:macos": ":macos_{}".format(name),
115+
"@platforms//os:windows": ":windows_{}".format(name),
81116
"//conditions:default": ":generic_{}".format(name),
82117
}),
83118
)
@@ -102,3 +137,14 @@ def library_link_args(name, library_type, from_variable, iterate_over_variable =
102137
name = "{}_maybe_force_load".format(name),
103138
variable = from_variable,
104139
)
140+
cc_nested_args(
141+
name = "windows_{}".format(name),
142+
requires_equal = "//cc/toolchains/variables:libraries_to_link.type",
143+
requires_equal_value = library_type,
144+
iterate_over = from_variable if iterate_over_variable else None,
145+
nested = [":{}_maybe_whole_archive".format(name)],
146+
)
147+
windows_whole_archive_args(
148+
name = "{}_maybe_whole_archive".format(name),
149+
variable = from_variable,
150+
)

cc/toolchains/args/link_flags/BUILD

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
load("//cc/toolchains:args.bzl", "cc_args")
22
load("//cc/toolchains:feature.bzl", "cc_feature")
33
load("//cc/toolchains:feature_set.bzl", "cc_feature_set")
4+
load("//cc/toolchains:nested_args.bzl", "cc_nested_args")
45

56
cc_feature_set(
67
name = "feature",
78
all_of = [
89
":user_link_flags_feature",
910
":legacy_link_flags_feature",
1011
":output_link_flags_feature",
12+
":build_interface_libraries_feature",
13+
":dynamic_library_linker_tool_feature",
1114
],
1215
visibility = ["//visibility:public"],
1316
)
@@ -58,3 +61,93 @@ cc_args(
5861
format = {"output_execpath": "//cc/toolchains/variables:output_execpath"},
5962
requires_not_none = "//cc/toolchains/variables:output_execpath",
6063
)
64+
65+
cc_feature(
66+
name = "build_interface_libraries_feature",
67+
args = ["build_interface_libraries"],
68+
overrides = "//cc/toolchains/features/legacy:build_interface_libraries",
69+
)
70+
71+
cc_args(
72+
name = "build_interface_libraries",
73+
actions = ["//cc/toolchains/actions:dynamic_library_link_actions"],
74+
nested = [":build_interface_libraries_check"],
75+
requires_not_none = "//cc/toolchains/variables:generate_interface_library",
76+
)
77+
78+
cc_nested_args(
79+
name = "build_interface_libraries_check",
80+
nested = select({
81+
"@platforms//os:windows": [":windows_build_interface_libraries"],
82+
"//conditions:default": [":generic_build_interface_libraries"],
83+
}),
84+
requires_equal = "//cc/toolchains/variables:generate_interface_library",
85+
requires_equal_value = "yes",
86+
)
87+
88+
cc_nested_args(
89+
name = "windows_build_interface_libraries",
90+
args = ["-Wl,/IMPLIB:{interface_library_output_path}"],
91+
format = {"interface_library_output_path": "//cc/toolchains/variables:interface_library_output_path"},
92+
requires_not_none = "//cc/toolchains/variables:interface_library_output_path",
93+
)
94+
95+
cc_nested_args(
96+
name = "generic_build_interface_libraries",
97+
nested = [
98+
":generate_interface_library",
99+
":interface_library_builder_path",
100+
":interface_library_input_path",
101+
":interface_library_output_path",
102+
],
103+
)
104+
105+
cc_nested_args(
106+
name = "generate_interface_library",
107+
args = ["{generate_interface_library}"],
108+
format = {"generate_interface_library": "//cc/toolchains/variables:generate_interface_library"},
109+
)
110+
111+
cc_nested_args(
112+
name = "interface_library_builder_path",
113+
args = ["{interface_library_builder_path}"],
114+
format = {"interface_library_builder_path": "//cc/toolchains/variables:interface_library_builder_path"},
115+
requires_not_none = "//cc/toolchains/variables:interface_library_builder_path",
116+
)
117+
118+
cc_nested_args(
119+
name = "interface_library_input_path",
120+
args = ["{interface_library_input_path}"],
121+
format = {"interface_library_input_path": "//cc/toolchains/variables:interface_library_input_path"},
122+
requires_not_none = "//cc/toolchains/variables:interface_library_input_path",
123+
)
124+
125+
cc_nested_args(
126+
name = "interface_library_output_path",
127+
args = ["{interface_library_output_path}"],
128+
format = {"interface_library_output_path": "//cc/toolchains/variables:interface_library_output_path"},
129+
requires_not_none = "//cc/toolchains/variables:interface_library_output_path",
130+
)
131+
132+
cc_feature(
133+
name = "dynamic_library_linker_tool_feature",
134+
args = select({
135+
"@platforms//os:windows": [],
136+
"//conditions:default": ["dynamic_library_linker_tool"],
137+
}),
138+
overrides = "//cc/toolchains/features/legacy:dynamic_library_linker_tool",
139+
)
140+
141+
cc_args(
142+
name = "dynamic_library_linker_tool",
143+
actions = ["//cc/toolchains/actions:dynamic_library_link_actions"],
144+
nested = [":dynamic_library_linker_tool_check"],
145+
requires_not_none = "//cc/toolchains/variables:generate_interface_library",
146+
)
147+
148+
cc_nested_args(
149+
name = "dynamic_library_linker_tool_check",
150+
args = [" + cppLinkDynamicLibraryToolPath + "],
151+
requires_equal = "//cc/toolchains/variables:generate_interface_library",
152+
requires_equal_value = "yes",
153+
)

cc/toolchains/args/runtime_library_search_directories/BUILD

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@ package(default_visibility = ["//visibility:private"])
88

99
cc_feature(
1010
name = "feature",
11-
args = [":runtime_library_search_directories"],
11+
args = select({
12+
"@platforms//os:windows": [],
13+
"//conditions:default": [":runtime_library_search_directories"],
14+
}),
1215
overrides = "//cc/toolchains/features/legacy:runtime_library_search_directories",
1316
visibility = ["//visibility:public"],
1417
)

cc/toolchains/args/strip_debug_symbols/BUILD

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ cc_feature(
1212
cc_args(
1313
name = "strip_debug_symbols",
1414
actions = ["//cc/toolchains/actions:link_actions"],
15-
args = ["-Wl,-S"],
15+
args = select({
16+
"@platforms//os:windows": ["-Wl,/DEBUG:NONE"],
17+
"//conditions:default": ["-Wl,-S"],
18+
}),
1619
requires_not_none = "//cc/toolchains/variables:strip_debug_symbols",
1720
)

cc/toolchains/capabilities/BUILD

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,15 @@ cc_tool_capability(
1717
cc_tool_capability(
1818
name = "supports_pic",
1919
)
20+
21+
cc_tool_capability(
22+
name = "has_configured_linker_path",
23+
)
24+
25+
cc_tool_capability(
26+
name = "targets_windows",
27+
)
28+
29+
cc_tool_capability(
30+
name = "copy_dynamic_libraries_to_binary",
31+
)

cc/toolchains/impl/toolchain_config_info.bzl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def _get_known_features(features, capability_features, fail):
6363
for ft in capability_features + features:
6464
if ft.name in feature_names:
6565
other = feature_names[ft.name]
66-
if other.overrides != ft and ft.overrides != other:
66+
if other.overrides != ft and ft.overrides != other and ft.label != other.label:
6767
fail(_FEATURE_NAME_ERR.format(
6868
name = ft.name,
6969
lhs = ft.label,

0 commit comments

Comments
 (0)