Skip to content

Conversation

calebzulawski
Copy link

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. an msvc_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.

":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"],
),
],
),
],
)

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"],
),
],
),
],
)

@@ -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

@calebzulawski
Copy link
Author

Sorry for the churn, getting everything right across multiple OSes is tough :) Should be good to go now.

@calebzulawski
Copy link
Author

@armandomontanez any thoughts on this implementation? thanks!

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 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants