-
Notifications
You must be signed in to change notification settings - Fork 138
Support linking on Windows with rules-based toolchains #436
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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"], | ||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
@@ -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", | ||||||||||||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I based this on the legacy toolchain config as well: rules_cc/cc/private/toolchain/unix_cc_toolchain_config.bzl Lines 1494 to 1517 in 0692005
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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"], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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). There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
I'd plan on implementing a macro to generally support this, since there would be so many instances of it. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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", | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
enabled: false | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" |
There was a problem hiding this comment.
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:
rules_cc/cc/private/toolchain/unix_cc_toolchain_config.bzl
Lines 1119 to 1147 in 0692005