-
Notifications
You must be signed in to change notification settings - Fork 522
Add runtime_libs as rust compile action inputs #3741
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
Merged
+169
−21
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -727,10 +727,18 @@ def collect_inputs( | |
| if linker_script: | ||
| nolinkstamp_compile_direct_inputs.append(linker_script) | ||
|
|
||
| if crate_info.type in ["dylib", "cdylib"]: | ||
| # For shared libraries we want to link C++ runtime library dynamically | ||
| # (for example libstdc++.so or libc++.so). | ||
| runtime_libs = cc_toolchain.dynamic_runtime_lib(feature_configuration = feature_configuration) | ||
| else: | ||
| runtime_libs = cc_toolchain.static_runtime_lib(feature_configuration = feature_configuration) | ||
|
|
||
| nolinkstamp_compile_inputs = depset( | ||
| nolinkstamp_compile_direct_inputs + | ||
| additional_transitive_inputs, | ||
| transitive = [ | ||
| runtime_libs, | ||
| linker_depset, | ||
| crate_info.srcs, | ||
| transitive_crate_outputs, | ||
|
|
@@ -2253,36 +2261,22 @@ def _add_native_link_flags(args, dep_info, linkstamp_outs, ambiguous_libs, crate | |
|
|
||
| args.add_all(make_link_flags_args, map_each = make_link_flags) | ||
|
|
||
| args.add_all(linkstamp_outs, before_each = "-C", format_each = "link-args=%s") | ||
| args.add_all(linkstamp_outs, format_each = "-Clink-args=%s") | ||
|
|
||
| if crate_type in ["dylib", "cdylib"]: | ||
| # For shared libraries we want to link C++ runtime library dynamically | ||
| # (for example libstdc++.so or libc++.so). | ||
| args.add_all( | ||
|
Contributor
Author
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. all of this is drive-by cleanup; I initially thought they needed to be passed by full path and then discovered they weren't in the sandbox at all :) |
||
| cc_toolchain.dynamic_runtime_lib(feature_configuration = feature_configuration), | ||
| map_each = _get_dirname, | ||
| format_each = "-Lnative=%s", | ||
| ) | ||
| runtime_libs = cc_toolchain.dynamic_runtime_lib(feature_configuration = feature_configuration) | ||
| args.add_all(runtime_libs, map_each = _get_dirname, format_each = "-Lnative=%s") | ||
| if include_link_flags: | ||
| args.add_all( | ||
| cc_toolchain.dynamic_runtime_lib(feature_configuration = feature_configuration), | ||
| map_each = get_lib_name, | ||
| format_each = "-ldylib=%s", | ||
| ) | ||
| args.add_all(runtime_libs, map_each = get_lib_name, format_each = "-ldylib=%s") | ||
| else: | ||
| # For all other crate types we want to link C++ runtime library statically | ||
| # (for example libstdc++.a or libc++.a). | ||
| args.add_all( | ||
| cc_toolchain.static_runtime_lib(feature_configuration = feature_configuration), | ||
| map_each = _get_dirname, | ||
| format_each = "-Lnative=%s", | ||
| ) | ||
| runtime_libs = cc_toolchain.static_runtime_lib(feature_configuration = feature_configuration) | ||
| args.add_all(runtime_libs, map_each = _get_dirname, format_each = "-Lnative=%s") | ||
| if include_link_flags: | ||
| args.add_all( | ||
| cc_toolchain.static_runtime_lib(feature_configuration = feature_configuration), | ||
| map_each = get_lib_name, | ||
| format_each = "-lstatic=%s", | ||
| ) | ||
| args.add_all(runtime_libs, map_each = get_lib_name, format_each = "-lstatic=%s") | ||
|
|
||
| def _get_dirname(file): | ||
| """A helper function for `_add_native_link_flags`. | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| load(":cc_toolchain_runtime_lib_test.bzl", "runtime_libs_test") | ||
|
|
||
| runtime_libs_test(name = "runtime_libs_test") |
151 changes: 151 additions & 0 deletions
151
test/unit/cc_toolchain_runtime_lib/cc_toolchain_runtime_lib_test.bzl
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| """ | ||
| Tests for handling of cc_toolchain's static_runtime_lib/dynamic_runtime_lib. | ||
| """ | ||
|
|
||
| load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts") | ||
| load("@rules_cc//cc:cc_toolchain_config_lib.bzl", "feature") | ||
| load("@rules_cc//cc:defs.bzl", "cc_toolchain") | ||
| load("@rules_cc//cc/common:cc_common.bzl", "cc_common") | ||
| load("//rust:defs.bzl", "rust_shared_library", "rust_static_library") | ||
|
|
||
| def _test_cc_config_impl(ctx): | ||
| config_info = cc_common.create_cc_toolchain_config_info( | ||
| ctx = ctx, | ||
| toolchain_identifier = "test-cc-toolchain", | ||
| host_system_name = "unknown", | ||
| target_system_name = "unknown", | ||
| target_cpu = "unknown", | ||
| target_libc = "unknown", | ||
| compiler = "unknown", | ||
| abi_version = "unknown", | ||
| abi_libc_version = "unknown", | ||
| features = [ | ||
| feature(name = "static_link_cpp_runtimes", enabled = True), | ||
| ], | ||
| ) | ||
| return config_info | ||
|
|
||
| test_cc_config = rule( | ||
| implementation = _test_cc_config_impl, | ||
| provides = [CcToolchainConfigInfo], | ||
| ) | ||
|
|
||
| def _with_extra_toolchain_transition_impl(_settings, attr): | ||
| return {"//command_line_option:extra_toolchains": [attr.extra_toolchain]} | ||
|
|
||
| with_extra_toolchain_transition = transition( | ||
| implementation = _with_extra_toolchain_transition_impl, | ||
| inputs = [], | ||
| outputs = ["//command_line_option:extra_toolchains"], | ||
| ) | ||
|
|
||
| DepActionsInfo = provider( | ||
| "Contains information about dependencies actions.", | ||
| fields = {"actions": "List[Action]"}, | ||
| ) | ||
|
|
||
| def _with_extra_toolchain_impl(ctx): | ||
| return [ | ||
| DepActionsInfo(actions = ctx.attr.target[0].actions), | ||
| ] | ||
|
|
||
| with_extra_toolchain = rule( | ||
| implementation = _with_extra_toolchain_impl, | ||
| attrs = { | ||
| "extra_toolchain": attr.label(), | ||
| "target": attr.label(cfg = with_extra_toolchain_transition), | ||
| }, | ||
| ) | ||
|
|
||
| def _inputs_analysis_test_impl(ctx): | ||
| env = analysistest.begin(ctx) | ||
| tut = analysistest.target_under_test(env) | ||
| action = tut[DepActionsInfo].actions[0] | ||
dzbarsky marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| asserts.equals(env, action.mnemonic, "Rustc") | ||
| inputs = action.inputs.to_list() | ||
| for expected in ctx.attr.expected_inputs: | ||
| asserts.true( | ||
| env, | ||
| any([input.path.endswith("/" + expected) for input in inputs]), | ||
| "error: expected '{}' to be in inputs: '{}'".format(expected, inputs), | ||
| ) | ||
|
|
||
| return analysistest.end(env) | ||
|
|
||
| inputs_analysis_test = analysistest.make( | ||
| impl = _inputs_analysis_test_impl, | ||
| doc = """An analysistest to examine the inputs of a library target.""", | ||
| attrs = { | ||
| "expected_inputs": attr.string_list(), | ||
| }, | ||
| ) | ||
|
|
||
| def runtime_libs_test(name): | ||
| """Produces test shared and static library targets that are set up to use a custom cc_toolchain with custom runtime libs. | ||
|
|
||
| Args: | ||
| name: The name of the test target. | ||
| """ | ||
|
|
||
| test_cc_config( | ||
| name = "%s/cc_toolchain_config" % name, | ||
| ) | ||
| cc_toolchain( | ||
| name = "%s/test_cc_toolchain_impl" % name, | ||
| all_files = ":empty", | ||
| compiler_files = ":empty", | ||
| dwp_files = ":empty", | ||
| linker_files = ":empty", | ||
| objcopy_files = ":empty", | ||
| strip_files = ":empty", | ||
| supports_param_files = 0, | ||
| toolchain_config = ":%s/cc_toolchain_config" % name, | ||
| toolchain_identifier = "dummy_wasm32_cc", | ||
| static_runtime_lib = ":dummy.a", | ||
| dynamic_runtime_lib = ":dummy.so", | ||
| ) | ||
| native.toolchain( | ||
| name = "%s/test_cc_toolchain" % name, | ||
| toolchain = ":%s/test_cc_toolchain_impl" % name, | ||
| toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", | ||
| ) | ||
|
|
||
| rust_shared_library( | ||
| name = "%s/__shared_library" % name, | ||
| edition = "2018", | ||
| srcs = ["lib.rs"], | ||
| tags = ["manual", "nobuild"], | ||
| ) | ||
|
|
||
| with_extra_toolchain( | ||
| name = "%s/_shared_library" % name, | ||
| extra_toolchain = ":%s/test_cc_toolchain" % name, | ||
| target = "%s/__shared_library" % name, | ||
| tags = ["manual"], | ||
| ) | ||
|
|
||
| inputs_analysis_test( | ||
| name = "%s/shared_library" % name, | ||
| target_under_test = "%s/_shared_library" % name, | ||
| expected_inputs = ["dummy.so"], | ||
| ) | ||
|
|
||
| rust_static_library( | ||
| name = "%s/__static_library" % name, | ||
| edition = "2018", | ||
| srcs = ["lib.rs"], | ||
| tags = ["manual", "nobuild"], | ||
| ) | ||
|
|
||
| with_extra_toolchain( | ||
| name = "%s/_static_library" % name, | ||
| extra_toolchain = ":%s/test_cc_toolchain" % name, | ||
| target = "%s/__static_library" % name, | ||
| tags = ["manual"], | ||
| ) | ||
|
|
||
| inputs_analysis_test( | ||
| name = "%s/static_library" % name, | ||
| target_under_test = "%s/_static_library" % name, | ||
| expected_inputs = ["dummy.a"], | ||
| ) | ||
Empty file.
Empty file.
Empty file.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
drive-by cleanup