diff --git a/extensions/wasm_bindgen/MODULE.bazel b/extensions/wasm_bindgen/MODULE.bazel index 0c7e901617..b712d79ca6 100644 --- a/extensions/wasm_bindgen/MODULE.bazel +++ b/extensions/wasm_bindgen/MODULE.bazel @@ -54,8 +54,6 @@ use_repo( register_toolchains( "//:default_wasm_bindgen_toolchain", - "@rules_rust//rust/private/dummy_cc_toolchain:dummy_cc_wasm32_toolchain", - "@rules_rust//rust/private/dummy_cc_toolchain:dummy_cc_wasm64_toolchain", ) bazel_dep(name = "bazel_ci_rules", version = "1.0.0", dev_dependency = True) diff --git a/extensions/wasm_bindgen/defs.bzl b/extensions/wasm_bindgen/defs.bzl index cd57b75ce4..b3fad04399 100644 --- a/extensions/wasm_bindgen/defs.bzl +++ b/extensions/wasm_bindgen/defs.bzl @@ -45,7 +45,7 @@ load( _rust_wasm_bindgen_toolchain = "rust_wasm_bindgen_toolchain", ) load( - "//private:wasm_bindgen_test.bzl", + "//private:wasm_bindgen_test_wrapper.bzl", _rust_wasm_bindgen_test = "rust_wasm_bindgen_test", ) diff --git a/extensions/wasm_bindgen/private/BUILD.bazel b/extensions/wasm_bindgen/private/BUILD.bazel index 45660286f3..270a213e40 100644 --- a/extensions/wasm_bindgen/private/BUILD.bazel +++ b/extensions/wasm_bindgen/private/BUILD.bazel @@ -7,6 +7,7 @@ bzl_library( "transitions.bzl", "wasm_bindgen.bzl", "wasm_bindgen_test.bzl", + "wasm_bindgen_test_wrapper.bzl", ], visibility = ["//:__pkg__"], deps = [ @@ -15,8 +16,8 @@ bzl_library( ) rust_binary( - name = "wasm_bindgen_test_wrapper", - srcs = ["wasm_bindgen_test_wrapper.rs"], + name = "wasm_bindgen_test_runner", + srcs = ["wasm_bindgen_test_runner.rs"], edition = "2021", visibility = ["//visibility:public"], deps = [ diff --git a/extensions/wasm_bindgen/private/transitions.bzl b/extensions/wasm_bindgen/private/transitions.bzl index 9025a7ad86..4ec05c97b0 100644 --- a/extensions/wasm_bindgen/private/transitions.bzl +++ b/extensions/wasm_bindgen/private/transitions.bzl @@ -19,3 +19,12 @@ wasm_bindgen_transition = transition( inputs = [], outputs = ["//command_line_option:platforms"], ) + +def _opt_transition(_settings, _attr): + return {"//command_line_option:compilation_mode": "opt"} + +opt_transition = transition( + implementation = _opt_transition, + inputs = [], + outputs = ["//command_line_option:compilation_mode"], +) diff --git a/extensions/wasm_bindgen/private/wasm_bindgen.bzl b/extensions/wasm_bindgen/private/wasm_bindgen.bzl index 44a3da51d9..b7e9fecbe6 100644 --- a/extensions/wasm_bindgen/private/wasm_bindgen.bzl +++ b/extensions/wasm_bindgen/private/wasm_bindgen.bzl @@ -176,7 +176,7 @@ WASM_BINDGEN_ATTR = { mandatory = True, ), "_allowlist_function_transition": attr.label( - default = "@bazel_tools//tools/allowlists/function_transition_allowlist", + default = Label("@bazel_tools//tools/allowlists/function_transition_allowlist"), ), } diff --git a/extensions/wasm_bindgen/private/wasm_bindgen_test.bzl b/extensions/wasm_bindgen/private/wasm_bindgen_test.bzl index 8a2dcb5738..0fdbf48eeb 100644 --- a/extensions/wasm_bindgen/private/wasm_bindgen_test.bzl +++ b/extensions/wasm_bindgen/private/wasm_bindgen_test.bzl @@ -20,7 +20,7 @@ load( "transform_sources", ) load("//:providers.bzl", "RustWasmBindgenInfo") -load("//private:transitions.bzl", "wasm_bindgen_transition") +load("//private:transitions.bzl", "opt_transition", "wasm_bindgen_transition") WasmBindgenTestCrateInfo = provider( doc = "A provider encompassing the crate from a `rust_wasm_bindgen` target.", @@ -56,11 +56,8 @@ def _rlocationpath(file, workspace_name): return "{}/{}".format(workspace_name, file.short_path) -def _rust_wasm_bindgen_test_impl(ctx): +def _rust_wasm_bindgen_test_binary_impl(ctx): wb_toolchain = ctx.toolchains[Label("//:toolchain_type")] - if not wb_toolchain.webdriver: - fail("The currently registered wasm_bindgen_toolchain does not have a webdriver assigned. Tests are unavailable without one.") - toolchain = find_toolchain(ctx) crate_type = "bin" @@ -142,62 +139,12 @@ def _rust_wasm_bindgen_test_impl(ctx): rust_flags = get_rust_test_flags(ctx.attr), skip_expanding_rustc_env = True, ) - data = getattr(ctx.attr, "data", []) - - env = expand_dict_value_locations( - ctx, - getattr(ctx.attr, "env", {}), - data, - {}, - ) - - components = "{}/{}".format(ctx.label.workspace_root, ctx.label.package).split("/") - env["CARGO_MANIFEST_DIR"] = "/".join([c for c in components if c]) - - wrapper = ctx.actions.declare_file(ctx.label.name) - ctx.actions.symlink( - output = wrapper, - target_file = ctx.executable._wrapper, - is_executable = True, - ) - - if wb_toolchain.browser: - env["BROWSER"] = _rlocationpath(wb_toolchain.browser, ctx.workspace_name) - - env["BROWSER_TYPE"] = wb_toolchain.browser_type - env["WEBDRIVER"] = _rlocationpath(wb_toolchain.webdriver, ctx.workspace_name) - env["WEBDRIVER_ARGS"] = " ".join(wb_toolchain.webdriver_args) - env["WEBDRIVER_JSON"] = _rlocationpath(wb_toolchain.webdriver_json, ctx.workspace_name) - env["WASM_BINDGEN_TEST_RUNNER"] = _rlocationpath(wb_toolchain.wasm_bindgen_test_runner, ctx.workspace_name) - - # Force the use of a browser for now as there is no node integration. - env["WASM_BINDGEN_USE_BROWSER"] = "1" - providers = [] + return crate_providers - for prov in crate_providers: - if type(prov) == "DefaultInfo": - files = prov.files.to_list() - if len(files) != 1: - fail("Unexpected number of output files for `{}`: {}".format(ctx.label, files)) - wasm_file = files[0] - env["TEST_WASM_BINARY"] = _rlocationpath(files[0], ctx.workspace_name) - providers.append(DefaultInfo( - files = prov.files, - runfiles = prov.default_runfiles.merge(ctx.runfiles(files = [wasm_file], transitive_files = wb_toolchain.all_test_files)), - executable = wrapper, - )) - else: - providers.append(prov) - - providers.append(testing.TestEnvironment(env)) - - return providers - -rust_wasm_bindgen_test = rule( +rust_wasm_bindgen_test_binary = rule( doc = "Rules for running [wasm-bindgen tests](https://rustwasm.github.io/wasm-bindgen/wasm-bindgen-test/index.html).", - implementation = _rust_wasm_bindgen_test_impl, - cfg = wasm_bindgen_transition, + implementation = _rust_wasm_bindgen_test_binary_impl, attrs = { "aliases": attr.label_keyed_string_dict( doc = """\ @@ -305,11 +252,6 @@ rust_wasm_bindgen_test = rule( file of arguments to rustc: `@$(location //package:target)`. """, ), - "target_arch": attr.string( - doc = "The target architecture to use for the wasm-bindgen command line option.", - default = "wasm32", - values = ["wasm32", "wasm64"], - ), "version": attr.string( doc = "A version to inject in the cargo environment variable.", default = "0.0.0", @@ -320,12 +262,6 @@ rust_wasm_bindgen_test = rule( providers = [RustWasmBindgenInfo], mandatory = True, ), - "_wrapper": attr.label( - doc = "The process wrapper for wasm-bindgen-test-runner.", - cfg = "exec", - executable = True, - default = Label("//private:wasm_bindgen_test_wrapper"), - ), } | RUSTC_ATTRS, fragments = ["cpp"], toolchains = [ @@ -333,5 +269,106 @@ rust_wasm_bindgen_test = rule( "@rules_rust//rust:toolchain_type", config_common.toolchain_type("@bazel_tools//tools/cpp:toolchain_type", mandatory = False), ], + executable = True, +) + +def _rust_wasm_bindgen_test_impl(ctx): + wb_toolchain = ctx.toolchains[Label("//:toolchain_type")] + if not wb_toolchain.webdriver: + fail("The currently registered wasm_bindgen_toolchain does not have a webdriver assigned. Tests are unavailable without one.") + + data = getattr(ctx.attr, "data", []) + + env = expand_dict_value_locations( + ctx, + getattr(ctx.attr, "env", {}), + data, + {}, + ) + + components = "{}/{}".format(ctx.label.workspace_root, ctx.label.package).split("/") + env["CARGO_MANIFEST_DIR"] = "/".join([c for c in components if c]) + + wrapper = ctx.actions.declare_file(ctx.label.name) + ctx.actions.symlink( + output = wrapper, + target_file = ctx.executable._runner, + is_executable = True, + ) + + if wb_toolchain.browser: + env["BROWSER"] = _rlocationpath(wb_toolchain.browser, ctx.workspace_name) + + env["BROWSER_TYPE"] = wb_toolchain.browser_type + env["WEBDRIVER"] = _rlocationpath(wb_toolchain.webdriver, ctx.workspace_name) + env["WEBDRIVER_ARGS"] = " ".join(wb_toolchain.webdriver_args) + env["WEBDRIVER_JSON"] = _rlocationpath(wb_toolchain.webdriver_json, ctx.workspace_name) + env["WASM_BINDGEN_TEST_RUNNER"] = _rlocationpath(wb_toolchain.wasm_bindgen_test_runner, ctx.workspace_name) + + # Force the use of a browser for now as there is no node integration. + env["WASM_BINDGEN_USE_BROWSER"] = "1" + + wasm_file = ctx.executable.wasm + + env["TEST_WASM_BINARY"] = _rlocationpath(wasm_file, ctx.workspace_name) + return [ + DefaultInfo( + files = ctx.attr.wasm[0][DefaultInfo].files, + runfiles = ctx.attr.wasm[0][DefaultInfo].default_runfiles.merge(ctx.runfiles(files = [wasm_file], transitive_files = wb_toolchain.all_test_files)), + executable = wrapper, + ), + RunEnvironmentInfo( + environment = env, + inherited_environment = ctx.attr.env_inherit, + ), + ] + +rust_wasm_bindgen_test = rule( + doc = "A test rule for running [wasm-bindgen tests](https://rustwasm.github.io/wasm-bindgen/wasm-bindgen-test/index.html).", + implementation = _rust_wasm_bindgen_test_impl, + attrs = { + "data": attr.label_list( + doc = """\ + List of files used by this rule at compile time and runtime. + + If including data at compile time with include_str!() and similar, + prefer `compile_data` over `data`, to prevent the data also being included + in the runfiles. + """, + allow_files = True, + ), + "env": attr.string_dict( + mandatory = False, + doc = """\ + Specifies additional environment variables to set when the test is executed by bazel test. + Values are subject to `$(rootpath)`, `$(execpath)`, location, and + ["Make variable"](https://docs.bazel.build/versions/master/be/make-variables.html) substitution. + """, + ), + "env_inherit": attr.string_list( + doc = "Specifies additional environment variables to inherit from the external environment when the test is executed by bazel test.", + ), + "target_arch": attr.string( + doc = "The target architecture to use for the wasm-bindgen command line option.", + default = "wasm32", + values = ["wasm32", "wasm64"], + ), + "wasm": attr.label( + doc = "The wasm target to test.", + executable = True, + cfg = wasm_bindgen_transition, + mandatory = True, + ), + "_runner": attr.label( + doc = "The process wrapper for wasm-bindgen-test-runner.", + # Try to get as close to `exec` as possible. + cfg = opt_transition, + executable = True, + default = Label("//private:wasm_bindgen_test_runner"), + ), + }, + toolchains = [ + str(Label("//:toolchain_type")), + ], test = True, ) diff --git a/extensions/wasm_bindgen/private/wasm_bindgen_test_wrapper.rs b/extensions/wasm_bindgen/private/wasm_bindgen_test_runner.rs similarity index 100% rename from extensions/wasm_bindgen/private/wasm_bindgen_test_wrapper.rs rename to extensions/wasm_bindgen/private/wasm_bindgen_test_runner.rs diff --git a/extensions/wasm_bindgen/private/wasm_bindgen_test_wrapper.bzl b/extensions/wasm_bindgen/private/wasm_bindgen_test_wrapper.bzl new file mode 100644 index 0000000000..bd5087302e --- /dev/null +++ b/extensions/wasm_bindgen/private/wasm_bindgen_test_wrapper.bzl @@ -0,0 +1,88 @@ +"""wasm_bindgen_test_wrapper""" + +load( + ":wasm_bindgen_test.bzl", + "rust_wasm_bindgen_test_binary", + _rust_wasm_bindgen_test = "rust_wasm_bindgen_test", +) + +def rust_wasm_bindgen_test( + *, + name, + aliases = {}, + compile_data = [], + crate_features = [], + data = [], + edition = None, + env = {}, + env_inherit = [], + proc_macro_deps = [], + rustc_env = {}, + rustc_env_files = [], + rustc_flags = [], + target_arch = None, + version = "0.0.0", + wasm = None, + tags = [], + **kwargs): + """"A test rule for running [wasm-bindgen tests](https://rustwasm.github.io/wasm-bindgen/wasm-bindgen-test/index.html)." + + Args: + name (str): A unique name for this target. + aliases (dict, optional): Remap crates to a new name or moniker for linkage to this target. + compile_data (list, optional): List of files used by this rule at compile time. + crate_features (list, optional): List of features to enable for this crate. + data (list, optional): List of files used by this rule at compile time and runtime. + edition (str, optional): The rust edition to use for this crate. Defaults to the edition specified in the rust_toolchain. + env (dict, optional): Specifies additional environment variables to set when the test is executed by bazel test. + env_inherit (list, optional): Specifies additional environment variables to inherit from the external environment when the test is executed by bazel test. + proc_macro_deps (list, optional): List of `rust_proc_macro` targets used to help build this library target. + rustc_env (dict, optional): Dictionary of additional `"key": "value"` environment variables to set for rustc. + rustc_env_files (list, optional): Files containing additional environment variables to set for rustc. + rustc_flags (list, optional): List of compiler flags passed to `rustc`. + target_arch (str, optional): The target architecture to use for the wasm-bindgen command line option. + version (str, optional): A version to inject in the cargo environment variable. + wasm (Label, optional): The wasm target to test. + tags (list, optional): Tags to apply to the target. + **kwargs (dict): Additional keyword arguments. + """ + visibility = kwargs.pop("visibility", []) + + # Create a test binary for `wasm-bindgen-test-runner` to invoke. + # Ideally this target would be produced within the `wasm_bindgen_test` + # rule directly but the design of that rule is to consume wasm files + # and run a test on the target environment. + rust_wasm_bindgen_test_binary( + name = name + ".bin", + aliases = aliases, + compile_data = compile_data, + crate_features = crate_features, + data = data, + edition = edition, + env = env, + env_inherit = env_inherit, + proc_macro_deps = proc_macro_deps, + rustc_env = rustc_env, + rustc_env_files = rustc_env_files, + rustc_flags = rustc_flags, + version = version, + wasm = wasm, + tags = depset(tags + ["manual"]).to_list(), + visibility = ["//visibility:private"], + target_compatible_with = select({ + "@platforms//cpu:wasm32": [], + "@platforms//cpu:wasm64": [], + "//conditions:default": ["@platforms//:incompatible"], + }), + **kwargs + ) + + _rust_wasm_bindgen_test( + name = name, + wasm = name + ".bin", + target_arch = target_arch, + tags = tags, + env = env, + visibility = visibility, + **kwargs + ) diff --git a/extensions/wasm_bindgen/test/BUILD.bazel b/extensions/wasm_bindgen/test/BUILD.bazel index 37237f8fdc..f5f4b0edb8 100644 --- a/extensions/wasm_bindgen/test/BUILD.bazel +++ b/extensions/wasm_bindgen/test/BUILD.bazel @@ -13,7 +13,7 @@ # limitations under the License. load("@rules_rust//rust:defs.bzl", "rust_binary", "rust_shared_library") -load("@rules_rust_wasm_bindgen//:defs.bzl", "rust_wasm_bindgen", "rust_wasm_bindgen_test") +load("//:defs.bzl", "rust_wasm_bindgen", "rust_wasm_bindgen_test") package(default_visibility = ["//:__subpackages__"]) diff --git a/ffi/cc/allocator_library/BUILD.bazel b/ffi/cc/allocator_library/BUILD.bazel index 0e5f8845f7..9494a55277 100644 --- a/ffi/cc/allocator_library/BUILD.bazel +++ b/ffi/cc/allocator_library/BUILD.bazel @@ -1,17 +1,28 @@ load("@rules_cc//cc:cc_library.bzl", "cc_library") -cc_library( +alias( name = "allocator_library", - srcs = select({ + actual = select({ + # It's not guaranteed that an unknown OS is going to support weak linking + # In these contexts it's best for users to provide their own. + "@platforms//os:none": "//rust/private/cc:empty", # WASI doesn't need the allocator library - WASI runtime provides allocation # Use empty srcs list to avoid needing archiving tools - "@platforms//os:wasi": [], + "@platforms//os:wasi": "//rust/private/cc:empty", # Windows doesn't support weak symbol linkage. # If someone can make this work on Windows, please do! # For now we will silently not supply any symbols, because it would be very messy to conditionally define the default allocator library on toolchains depending on the platform. - "@platforms//os:windows": ["empty.cc"], - "//conditions:default": ["allocator_library.cc"], + "@platforms//os:windows": "//rust/private/cc:empty", + "//conditions:default": ":cc_allocator_library", }), + visibility = ["//visibility:public"], +) + +cc_library( + name = "cc_allocator_library", + srcs = [ + "allocator_library.cc", + ], tags = ["manual"], visibility = ["//visibility:public"], ) diff --git a/ffi/cc/allocator_library/empty.cc b/ffi/cc/allocator_library/empty.cc deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/rust/private/cc/BUILD.bazel b/rust/private/cc/BUILD.bazel index 25beb854eb..b8664fa05b 100644 --- a/rust/private/cc/BUILD.bazel +++ b/rust/private/cc/BUILD.bazel @@ -8,7 +8,7 @@ cc_empty_library( alias( name = "malloc", actual = select({ - "//rust/settings:experimental_use_cc_common_link_on": "@bazel_tools//tools/cpp:malloc", + # "//rust/settings:experimental_use_cc_common_link_on": "@bazel_tools//tools/cpp:malloc", "//conditions:default": ":empty", }), visibility = ["//visibility:public"], diff --git a/rust/private/dummy_cc_toolchain/BUILD.bazel b/rust/private/dummy_cc_toolchain/BUILD.bazel deleted file mode 100644 index df4290afc9..0000000000 --- a/rust/private/dummy_cc_toolchain/BUILD.bazel +++ /dev/null @@ -1,46 +0,0 @@ -# When compiling Rust code for wasm (wasm32 or wasm64), we avoid linking to cpp code so we introduce a dummy cc -# toolchain since we know we'll never look it up. -# -# TODO(jedmonds@spotify.com): Need to support linking C code to rust code when compiling for wasm32 or wasm64. - -load("@rules_cc//cc:defs.bzl", "cc_toolchain") -load(":dummy_cc_toolchain.bzl", "dummy_cc_config", "dummy_cc_toolchain") - -dummy_cc_toolchain(name = "dummy_cc_wasm") - -toolchain( - name = "dummy_cc_wasm32_toolchain", - target_compatible_with = ["@platforms//cpu:wasm32"], - toolchain = ":dummy_cc_wasm_toolchain_cc", - toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", -) - -toolchain( - name = "dummy_cc_wasm64_toolchain", - target_compatible_with = ["@platforms//cpu:wasm64"], - toolchain = ":dummy_cc_wasm_toolchain_cc", - toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", -) - -cc_toolchain( - name = "dummy_cc_wasm_toolchain_cc", - all_files = ":empty", - compiler_files = ":empty", - dwp_files = ":empty", - linker_files = ":empty", - objcopy_files = ":empty", - strip_files = ":empty", - supports_param_files = 0, - toolchain_config = ":cc_toolchain_config", - toolchain_identifier = "dummy_wasm_cc", -) - -dummy_cc_config( - name = "cc_toolchain_config", -) - -filegroup( - name = "empty", - srcs = [], - visibility = ["//:__subpackages__"], -) diff --git a/rust/private/dummy_cc_toolchain/dummy_cc_toolchain.bzl b/rust/private/dummy_cc_toolchain/dummy_cc_toolchain.bzl deleted file mode 100644 index 4b888fe4e6..0000000000 --- a/rust/private/dummy_cc_toolchain/dummy_cc_toolchain.bzl +++ /dev/null @@ -1,33 +0,0 @@ -"""Cc toolchain definitions for use on wasm platforms""" - -load("@rules_cc//cc/common:cc_common.bzl", "cc_common") -load("@rules_cc//cc/toolchains:cc_toolchain_config_info.bzl", "CcToolchainConfigInfo") - -def _dummy_cc_toolchain_impl(_ctx): - # The `all_files` attribute is referenced by rustc_compile_action(). - return [platform_common.ToolchainInfo(all_files = depset([]))] - -dummy_cc_toolchain = rule( - implementation = _dummy_cc_toolchain_impl, - attrs = {}, -) - -# dummy values from https://bazel.build/tutorials/ccp-toolchain-config#configuring_the_c_toolchain -def _config_impl(ctx): - return cc_common.create_cc_toolchain_config_info( - ctx = ctx, - toolchain_identifier = "dummy-wasm-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", - ) - -dummy_cc_config = rule( - implementation = _config_impl, - attrs = {}, - provides = [CcToolchainConfigInfo], -) diff --git a/rust/private/rust.bzl b/rust/private/rust.bzl index d06f5fa71f..3c0d5e104c 100644 --- a/rust/private/rust.bzl +++ b/rust/private/rust.bzl @@ -657,7 +657,7 @@ RUSTC_ATTRS = { ), } -_common_attrs = { +_COMMON_ATTRS = { "aliases": attr.label_keyed_string_dict( doc = dedent("""\ Remap crates to a new name or moniker for linkage to this target @@ -828,7 +828,17 @@ _common_attrs = { ), } | RUSTC_ATTRS | RUSTC_ALLOCATOR_LIBRARIES_ATTRS -_coverage_attrs = { +_PLATFORM_ATTRS = { + "platform": attr.label( + doc = "Optional platform to transition the static library to.", + default = None, + ), + "_allowlist_function_transition": attr.label( + default = Label("@bazel_tools//tools/allowlists/function_transition_allowlist"), + ), +} + +_COVERAGE_ATTRS = { "_collect_cc_coverage": attr.label( default = Label("//util/collect_coverage"), executable = True, @@ -850,7 +860,7 @@ _coverage_attrs = { ), } -_experimental_use_cc_common_link_attrs = { +_EXPERIMENTAL_USE_CC_COMMON_LINK_ATTRS = { "experimental_use_cc_common_link": attr.int( doc = ( "Whether to use cc_common.link to link rust binaries. " + @@ -867,9 +877,10 @@ _experimental_use_cc_common_link_attrs = { default = Label("//rust/private/cc:malloc"), doc = """Override the default dependency on `malloc`. -By default, Rust binaries linked with cc_common.link are linked against -`//rust/private/cc:malloc"`, which is an empty library and the resulting binary will use -libc's `malloc`. This label must refer to a `cc_library` rule. +By default, Rust binaries linked with [`cc_common.link`](https://bazel.build/rules/lib/toplevel/cc_common#link) +are linked against `@rules_rust//rust/private/cc:malloc`, which is an empty library +and the resulting binary will use libc's `malloc`. This label must refer to a +`cc_library` rule. """, mandatory = False, providers = [[CcInfo]], @@ -884,7 +895,7 @@ libc's `malloc`. This label must refer to a `cc_library` rule. ), } -_rust_test_attrs = { +_RUST_TEST_ATTRS = { "crate": attr.label( mandatory = False, doc = dedent("""\ @@ -914,12 +925,12 @@ _rust_test_attrs = { E.g. `bazel test //src:rust_test --test_arg=foo::test::test_fn`. """), ), -} | _coverage_attrs | _experimental_use_cc_common_link_attrs +} | _COVERAGE_ATTRS | _EXPERIMENTAL_USE_CC_COMMON_LINK_ATTRS rust_library = rule( implementation = _rust_library_impl, provides = COMMON_PROVIDERS, - attrs = _common_attrs | { + attrs = _COMMON_ATTRS | { "disable_pipelining": attr.bool( default = False, doc = dedent("""\ @@ -1017,15 +1028,7 @@ _rust_static_library_transition = transition( rust_static_library = rule( implementation = _rust_static_library_impl, - attrs = _common_attrs | { - "platform": attr.label( - doc = "Optional platform to transition the static library to.", - default = None, - ), - "_allowlist_function_transition": attr.label( - default = "@bazel_tools//tools/allowlists/function_transition_allowlist", - ), - }, + attrs = _COMMON_ATTRS | _PLATFORM_ATTRS, fragments = ["cpp"], cfg = _rust_static_library_transition, toolchains = [ @@ -1066,15 +1069,7 @@ _rust_shared_library_transition = transition( rust_shared_library = rule( implementation = _rust_shared_library_impl, - attrs = _common_attrs | _experimental_use_cc_common_link_attrs | { - "platform": attr.label( - doc = "Optional platform to transition the shared library to.", - default = None, - ), - "_allowlist_function_transition": attr.label( - default = "@bazel_tools//tools/allowlists/function_transition_allowlist", - ), - }, + attrs = _COMMON_ATTRS | _PLATFORM_ATTRS | _EXPERIMENTAL_USE_CC_COMMON_LINK_ATTRS, fragments = ["cpp"], cfg = _rust_shared_library_transition, toolchains = [ @@ -1118,7 +1113,7 @@ rust_proc_macro = rule( # need to declare `_allowlist_function_transition`, see # https://docs.bazel.build/versions/main/skylark/config.html#user-defined-transitions. attrs = dict( - _common_attrs.items(), + _COMMON_ATTRS.items(), _allowlist_function_transition = attr.label( default = Label("//tools/allowlists/function_transition_allowlist"), ), @@ -1142,7 +1137,7 @@ rust_proc_macro = rule( """), ) -_rust_binary_attrs = { +_RUST_BINARY_ATTRS = { "binary_name": attr.string( doc = dedent("""\ Override the resulting binary file name. By default, the binary file will be named using the `name` attribute on this rule, @@ -1177,7 +1172,7 @@ _rust_binary_attrs = { allow_single_file = True, ), "stamp": _stamp_attribute(default_value = -1), -} | _experimental_use_cc_common_link_attrs +} | _EXPERIMENTAL_USE_CC_COMMON_LINK_ATTRS def _rust_binary_transition_impl(settings, attr): return { @@ -1197,15 +1192,7 @@ _rust_binary_transition = transition( rust_binary = rule( implementation = _rust_binary_impl, provides = COMMON_PROVIDERS, - attrs = _common_attrs | _rust_binary_attrs | { - "platform": attr.label( - doc = "Optional platform to transition the binary to.", - default = None, - ), - "_allowlist_function_transition": attr.label( - default = "@bazel_tools//tools/allowlists/function_transition_allowlist", - ), - }, + attrs = _COMMON_ATTRS | _RUST_BINARY_ATTRS | _PLATFORM_ATTRS, executable = True, fragments = ["cpp"], cfg = _rust_binary_transition, @@ -1349,7 +1336,7 @@ rust_binary_without_process_wrapper = rule( implementation = _rust_binary_without_process_wrapper_impl, doc = "A variant of `rust_binary` that uses a minimal process wrapper for `Rustc` actions.", provides = COMMON_PROVIDERS + [_RustBuiltWithoutProcessWrapperInfo], - attrs = _common_attrs_for_binary_without_process_wrapper(_common_attrs | _rust_binary_attrs), + attrs = _common_attrs_for_binary_without_process_wrapper(_COMMON_ATTRS | _RUST_BINARY_ATTRS), executable = True, fragments = ["cpp"], toolchains = [ @@ -1366,7 +1353,7 @@ rust_library_without_process_wrapper = rule( implementation = _rust_library_without_process_wrapper_impl, doc = "A variant of `rust_library` that uses a minimal process wrapper for `Rustc` actions.", provides = COMMON_PROVIDERS + [_RustBuiltWithoutProcessWrapperInfo], - attrs = dict(_common_attrs_for_binary_without_process_wrapper(_common_attrs).items()), + attrs = dict(_common_attrs_for_binary_without_process_wrapper(_COMMON_ATTRS).items()), fragments = ["cpp"], toolchains = [ str(Label("//rust:toolchain_type")), @@ -1381,7 +1368,7 @@ def _rust_static_library_without_process_wrapper_impl(ctx): rust_static_library_without_process_wrapper = rule( implementation = _rust_static_library_without_process_wrapper_impl, doc = "A variant of `rust_static_library` that uses a minimal process wrapper for `Rustc` actions.", - attrs = dict(_common_attrs_for_binary_without_process_wrapper(_common_attrs).items()), + attrs = dict(_common_attrs_for_binary_without_process_wrapper(_COMMON_ATTRS).items()), fragments = ["cpp"], toolchains = [ str(Label("//rust:toolchain_type")), @@ -1424,7 +1411,7 @@ rust_test_without_process_wrapper_test = rule( implementation = _rust_test_without_process_wrapper_test_impl, doc = "Unlike other `*_without_process_wrapper` rules, this rule does use the process wrapper but requires it's dependencies were not built with one.", provides = COMMON_PROVIDERS, - attrs = _test_attrs_for_binary_without_process_wrapper(_common_attrs | _rust_test_attrs), + attrs = _test_attrs_for_binary_without_process_wrapper(_COMMON_ATTRS | _RUST_TEST_ATTRS), executable = True, fragments = ["cpp"], test = True, @@ -1452,15 +1439,7 @@ _rust_test_transition = transition( rust_test = rule( implementation = _rust_test_impl, provides = COMMON_PROVIDERS, - attrs = _common_attrs | _rust_test_attrs | { - "platform": attr.label( - doc = "Optional platform to transition the test to.", - default = None, - ), - "_allowlist_function_transition": attr.label( - default = "@bazel_tools//tools/allowlists/function_transition_allowlist", - ), - }, + attrs = _COMMON_ATTRS | _RUST_TEST_ATTRS | _PLATFORM_ATTRS, executable = True, fragments = ["cpp"], cfg = _rust_test_transition, diff --git a/rust/repositories.bzl b/rust/repositories.bzl index 4a138bf5d5..ea78cdb96b 100644 --- a/rust/repositories.bzl +++ b/rust/repositories.bzl @@ -1335,7 +1335,5 @@ def rust_repository_set( # Register toolchains if register_toolchain: native.register_toolchains(*toolchain_labels) - native.register_toolchains(str(Label("//rust/private/dummy_cc_toolchain:dummy_cc_wasm32_toolchain"))) - native.register_toolchains(str(Label("//rust/private/dummy_cc_toolchain:dummy_cc_wasm64_toolchain"))) return all_toolchain_details diff --git a/test/unit/cc_info/cc_info_test.bzl b/test/unit/cc_info/cc_info_test.bzl index 4d03578261..9b1eabce89 100644 --- a/test/unit/cc_info/cc_info_test.bzl +++ b/test/unit/cc_info/cc_info_test.bzl @@ -9,7 +9,7 @@ load("@rules_cc//cc:defs.bzl", "cc_import", "cc_library") load("@rules_cc//cc/common:cc_info.bzl", "CcInfo") load("//rust:defs.bzl", "rust_binary", "rust_common", "rust_library", "rust_proc_macro", "rust_shared_library", "rust_static_library") -def _is_dylib_on_windows(ctx): +def _is_windows(ctx): return ctx.target_platform_has_constraint(ctx.attr._windows[platform_common.ConstraintValueInfo]) def _assert_cc_info_has_library_to_link(env, tut, type, ccinfo_count): @@ -28,7 +28,7 @@ def _assert_cc_info_has_library_to_link(env, tut, type, ccinfo_count): if type == "cdylib": asserts.true(env, library_to_link.dynamic_library != None) - if _is_dylib_on_windows(env.ctx): + if _is_windows(env.ctx): asserts.true(env, library_to_link.interface_library != None) asserts.true(env, library_to_link.resolved_symlink_dynamic_library == None) else: @@ -59,8 +59,10 @@ def _rlib_provides_cc_info_test_impl(ctx): tut = analysistest.target_under_test(env) count = 4 + if _is_windows(env.ctx): + count -= 1 if ctx.attr._experimental_use_allocator_libraries_with_mangled_symbols[BuildSettingInfo].value: - count = 3 + count -= 1 _assert_cc_info_has_library_to_link(env, tut, "rlib", count) return analysistest.end(env) @@ -117,6 +119,7 @@ rlib_provides_cc_info_test = analysistest.make( "_experimental_use_allocator_libraries_with_mangled_symbols": attr.label( default = Label("//rust/settings:experimental_use_allocator_libraries_with_mangled_symbols"), ), + "_windows": attr.label(default = Label("@platforms//os:windows")), }, ) rlib_with_dep_only_has_stdlib_linkflags_once_test = analysistest.make(