-
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?
Conversation
e7a849b
to
1990612
Compare
":interface_library_input_path", | ||
":interface_library_output_path", | ||
], | ||
) |
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
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"], | |
), | |
], | |
), | |
], | |
) |
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 comment
The 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
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"], | |
), | |
], | |
), | |
], | |
) |
@@ -0,0 +1,117 @@ | |||
enabled: false |
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 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
Sorry for the churn, getting everything right across multiple OSes is tough :) Should be good to go now. |
@armandomontanez any thoughts on this implementation? thanks! |
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 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).
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 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.
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.
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 :)
Allow linking on Windows, tested with clang.exe and both link.exe and lld-link.
I didn't delve into compiler flags, so this does not add any support for MSVC/clang-cl.exe. I use
-Wl,<flag>
pretty heavily, but per #434 it should be possible to make e.g. anmsvc_linker_args
rule that reads a variable and conditionally prefixes-Wl,
only when not using MSVC/clang-cl.I also didn't account for MinGW, which I think acts more like Linux, so maybe conditioning on just the target OS won't be sufficient in the long run.