From a2a1109dc8d9611bd8ea24148abaa7fbdb81d732 Mon Sep 17 00:00:00 2001 From: UebelAndre Date: Fri, 5 May 2023 09:46:57 -0700 Subject: [PATCH 1/7] Add T2 support for thumbv7em-none-eabi (#1957) * Add T2 support for thumbv7em-none-eabi * Regenerate documentation * Updated crate_unverse examples --- docs/crate_universe.md | 4 ++-- .../crate_universe/cargo_aliases/cargo-bazel-lock.json | 2 +- .../crate_universe/cargo_workspace/cargo-bazel-lock.json | 4 +++- .../crate_universe/multi_package/cargo-bazel-lock.json | 7 ++++++- .../no_cargo_manifests/cargo-bazel-lock.json | 5 ++++- rust/platform/triple_mappings.bzl | 1 + test/no_std/cargo-bazel-lock.json | 2 +- 7 files changed, 18 insertions(+), 7 deletions(-) diff --git a/docs/crate_universe.md b/docs/crate_universe.md index 54b63da0bd..ac23ad5026 100644 --- a/docs/crate_universe.md +++ b/docs/crate_universe.md @@ -388,7 +388,7 @@ CARGO_BAZEL_REPIN=1 CARGO_BAZEL_REPIN_ONLY=crate_index bazel sync --only=crate_i | rust_toolchain_rustc_template | The template to use for finding the host rustc binary. {version} (eg. '1.53.0'), {triple} (eg. 'x86_64-unknown-linux-gnu'), {arch} (eg. 'aarch64'), {vendor} (eg. 'unknown'), {system} (eg. 'darwin'), {cfg} (eg. 'exec'), {channel} (eg. 'stable'), and {tool} (eg. 'cargo.exe') will be replaced in the string if present. | String | optional | "@rust_{system}_{arch}__{triple}__{channel}_tools//:bin/{tool}" | | rust_version | The version of Rust the currently registered toolchain is using. Eg. 1.56.0, or nightly/2021-09-08 | String | optional | "1.69.0" | | splicing_config | The configuration flags to use for splicing Cargo maniests. Use //crate_universe:defs.bzl\%rsplicing_config to generate the value for this field. If unset, the defaults defined there will be used. | String | optional | "" | -| supported_platform_triples | A set of all platform triples to consider when generating dependencies. | List of strings | optional | ["aarch64-unknown-linux-gnu", "i686-apple-darwin", "i686-pc-windows-msvc", "i686-unknown-linux-gnu", "x86_64-apple-darwin", "x86_64-pc-windows-msvc", "x86_64-unknown-linux-gnu", "aarch64-apple-darwin", "aarch64-apple-ios-sim", "aarch64-apple-ios", "aarch64-fuchsia", "aarch64-linux-android", "aarch64-pc-windows-msvc", "arm-unknown-linux-gnueabi", "armv7-linux-androideabi", "armv7-unknown-linux-gnueabi", "i686-linux-android", "i686-unknown-freebsd", "powerpc-unknown-linux-gnu", "riscv32imc-unknown-none-elf", "riscv64gc-unknown-none-elf", "s390x-unknown-linux-gnu", "wasm32-unknown-unknown", "wasm32-wasi", "x86_64-apple-ios", "x86_64-fuchsia", "x86_64-linux-android", "x86_64-unknown-freebsd"] | +| supported_platform_triples | A set of all platform triples to consider when generating dependencies. | List of strings | optional | ["aarch64-unknown-linux-gnu", "i686-apple-darwin", "i686-pc-windows-msvc", "i686-unknown-linux-gnu", "x86_64-apple-darwin", "x86_64-pc-windows-msvc", "x86_64-unknown-linux-gnu", "aarch64-apple-darwin", "aarch64-apple-ios-sim", "aarch64-apple-ios", "aarch64-fuchsia", "aarch64-linux-android", "aarch64-pc-windows-msvc", "arm-unknown-linux-gnueabi", "armv7-linux-androideabi", "armv7-unknown-linux-gnueabi", "i686-linux-android", "i686-unknown-freebsd", "powerpc-unknown-linux-gnu", "riscv32imc-unknown-none-elf", "riscv64gc-unknown-none-elf", "s390x-unknown-linux-gnu", "thumbv7em-none-eabi", "wasm32-unknown-unknown", "wasm32-wasi", "x86_64-apple-ios", "x86_64-fuchsia", "x86_64-linux-android", "x86_64-unknown-freebsd"] | @@ -493,7 +493,7 @@ call against the generated workspace. The following table describes how to contr | render_config | The configuration flags to use for rendering. Use //crate_universe:defs.bzl\%render_config to generate the value for this field. If unset, the defaults defined there will be used. | String | optional | "" | | repository_name | The name of the repository to generate for remote vendor modes. If unset, the label name will be used | String | optional | "" | | splicing_config | The configuration flags to use for splicing Cargo maniests. Use //crate_universe:defs.bzl\%rsplicing_config to generate the value for this field. If unset, the defaults defined there will be used. | String | optional | "" | -| supported_platform_triples | A set of all platform triples to consider when generating dependencies. | List of strings | optional | ["aarch64-unknown-linux-gnu", "i686-apple-darwin", "i686-pc-windows-msvc", "i686-unknown-linux-gnu", "x86_64-apple-darwin", "x86_64-pc-windows-msvc", "x86_64-unknown-linux-gnu", "aarch64-apple-darwin", "aarch64-apple-ios-sim", "aarch64-apple-ios", "aarch64-fuchsia", "aarch64-linux-android", "aarch64-pc-windows-msvc", "arm-unknown-linux-gnueabi", "armv7-linux-androideabi", "armv7-unknown-linux-gnueabi", "i686-linux-android", "i686-unknown-freebsd", "powerpc-unknown-linux-gnu", "riscv32imc-unknown-none-elf", "riscv64gc-unknown-none-elf", "s390x-unknown-linux-gnu", "wasm32-unknown-unknown", "wasm32-wasi", "x86_64-apple-ios", "x86_64-fuchsia", "x86_64-linux-android", "x86_64-unknown-freebsd"] | +| supported_platform_triples | A set of all platform triples to consider when generating dependencies. | List of strings | optional | ["aarch64-unknown-linux-gnu", "i686-apple-darwin", "i686-pc-windows-msvc", "i686-unknown-linux-gnu", "x86_64-apple-darwin", "x86_64-pc-windows-msvc", "x86_64-unknown-linux-gnu", "aarch64-apple-darwin", "aarch64-apple-ios-sim", "aarch64-apple-ios", "aarch64-fuchsia", "aarch64-linux-android", "aarch64-pc-windows-msvc", "arm-unknown-linux-gnueabi", "armv7-linux-androideabi", "armv7-unknown-linux-gnueabi", "i686-linux-android", "i686-unknown-freebsd", "powerpc-unknown-linux-gnu", "riscv32imc-unknown-none-elf", "riscv64gc-unknown-none-elf", "s390x-unknown-linux-gnu", "thumbv7em-none-eabi", "wasm32-unknown-unknown", "wasm32-wasi", "x86_64-apple-ios", "x86_64-fuchsia", "x86_64-linux-android", "x86_64-unknown-freebsd"] | | vendor_path | The path to a directory to write files into. Absolute paths will be treated as relative to the workspace root | String | optional | "crates" | diff --git a/examples/crate_universe/cargo_aliases/cargo-bazel-lock.json b/examples/crate_universe/cargo_aliases/cargo-bazel-lock.json index b8f3b273a0..6661c07156 100644 --- a/examples/crate_universe/cargo_aliases/cargo-bazel-lock.json +++ b/examples/crate_universe/cargo_aliases/cargo-bazel-lock.json @@ -1,5 +1,5 @@ { - "checksum": "c39e464dcf411dcc290b4b8dedd008c38653535c5999463a87f19ce18d2a5ca1", + "checksum": "66bd7b84cfb273e65ddb047ae27c72d99e795ec3f12369ddeb2e9fc006e53a8a", "crates": { "aho-corasick 0.7.20": { "name": "aho-corasick", diff --git a/examples/crate_universe/cargo_workspace/cargo-bazel-lock.json b/examples/crate_universe/cargo_workspace/cargo-bazel-lock.json index e546f2ef78..72d0b3d502 100644 --- a/examples/crate_universe/cargo_workspace/cargo-bazel-lock.json +++ b/examples/crate_universe/cargo_workspace/cargo-bazel-lock.json @@ -1,5 +1,5 @@ { - "checksum": "2a5f199cb5327faf2760b22a963b81d84409fe915c7a883e7aa4ef4a30122708", + "checksum": "d0c772ec9a1b712837102c7b763fcc7fb9114613850a46964f6e7c70ae4aed4e", "crates": { "ansi_term 0.12.1": { "name": "ansi_term", @@ -1295,6 +1295,7 @@ "riscv32imc-unknown-none-elf", "riscv64gc-unknown-none-elf", "s390x-unknown-linux-gnu", + "thumbv7em-none-eabi", "wasm32-unknown-unknown", "wasm32-wasi", "x86_64-apple-darwin", @@ -1323,6 +1324,7 @@ "riscv32imc-unknown-none-elf", "riscv64gc-unknown-none-elf", "s390x-unknown-linux-gnu", + "thumbv7em-none-eabi", "wasm32-unknown-unknown", "wasm32-wasi", "x86_64-apple-darwin", diff --git a/examples/crate_universe/multi_package/cargo-bazel-lock.json b/examples/crate_universe/multi_package/cargo-bazel-lock.json index 579657b656..8894e57616 100644 --- a/examples/crate_universe/multi_package/cargo-bazel-lock.json +++ b/examples/crate_universe/multi_package/cargo-bazel-lock.json @@ -1,5 +1,5 @@ { - "checksum": "41312207c4958da6e624a8f83a45b739a7309bffc7bff36ebb31db2b9a58d605", + "checksum": "c2dfd0205262fb9ad37e31083e0eca5ca7363c493e983cb9d9fbce00aae4e546", "crates": { "aho-corasick 0.7.20": { "name": "aho-corasick", @@ -9402,6 +9402,7 @@ "riscv32imc-unknown-none-elf", "riscv64gc-unknown-none-elf", "s390x-unknown-linux-gnu", + "thumbv7em-none-eabi", "x86_64-apple-darwin", "x86_64-apple-ios", "x86_64-fuchsia", @@ -9430,6 +9431,7 @@ "riscv32imc-unknown-none-elf", "riscv64gc-unknown-none-elf", "s390x-unknown-linux-gnu", + "thumbv7em-none-eabi", "x86_64-apple-darwin", "x86_64-apple-ios", "x86_64-fuchsia", @@ -9458,6 +9460,7 @@ "riscv32imc-unknown-none-elf", "riscv64gc-unknown-none-elf", "s390x-unknown-linux-gnu", + "thumbv7em-none-eabi", "wasm32-wasi", "x86_64-apple-darwin", "x86_64-apple-ios", @@ -9485,6 +9488,7 @@ "riscv32imc-unknown-none-elf", "riscv64gc-unknown-none-elf", "s390x-unknown-linux-gnu", + "thumbv7em-none-eabi", "wasm32-unknown-unknown", "wasm32-wasi", "x86_64-apple-darwin", @@ -9514,6 +9518,7 @@ "riscv32imc-unknown-none-elf", "riscv64gc-unknown-none-elf", "s390x-unknown-linux-gnu", + "thumbv7em-none-eabi", "wasm32-unknown-unknown", "wasm32-wasi", "x86_64-apple-darwin", diff --git a/examples/crate_universe/no_cargo_manifests/cargo-bazel-lock.json b/examples/crate_universe/no_cargo_manifests/cargo-bazel-lock.json index 86c1693efb..0279d086fc 100644 --- a/examples/crate_universe/no_cargo_manifests/cargo-bazel-lock.json +++ b/examples/crate_universe/no_cargo_manifests/cargo-bazel-lock.json @@ -1,5 +1,5 @@ { - "checksum": "7c38e1a80bcee2cc2c6e5bbca20107dab2d5edb5b4f0fe89df6ed28f55fecb5f", + "checksum": "e5a6a8bd126d76a92466d0b710a37b36e03ecf9212ec5116858d57b9989cb966", "crates": { "async-trait 0.1.64": { "name": "async-trait", @@ -4884,6 +4884,7 @@ "riscv32imc-unknown-none-elf", "riscv64gc-unknown-none-elf", "s390x-unknown-linux-gnu", + "thumbv7em-none-eabi", "x86_64-apple-darwin", "x86_64-apple-ios", "x86_64-fuchsia", @@ -4910,6 +4911,7 @@ "riscv32imc-unknown-none-elf", "riscv64gc-unknown-none-elf", "s390x-unknown-linux-gnu", + "thumbv7em-none-eabi", "wasm32-unknown-unknown", "wasm32-wasi", "x86_64-apple-darwin", @@ -4939,6 +4941,7 @@ "riscv32imc-unknown-none-elf", "riscv64gc-unknown-none-elf", "s390x-unknown-linux-gnu", + "thumbv7em-none-eabi", "wasm32-unknown-unknown", "wasm32-wasi", "x86_64-apple-darwin", diff --git a/rust/platform/triple_mappings.bzl b/rust/platform/triple_mappings.bzl index 532ef474c7..61498d8d13 100644 --- a/rust/platform/triple_mappings.bzl +++ b/rust/platform/triple_mappings.bzl @@ -36,6 +36,7 @@ SUPPORTED_T2_PLATFORM_TRIPLES = [ "riscv32imc-unknown-none-elf", "riscv64gc-unknown-none-elf", "s390x-unknown-linux-gnu", + "thumbv7em-none-eabi", "wasm32-unknown-unknown", "wasm32-wasi", "x86_64-apple-ios", diff --git a/test/no_std/cargo-bazel-lock.json b/test/no_std/cargo-bazel-lock.json index 337d8e7f9f..af8338e6b0 100644 --- a/test/no_std/cargo-bazel-lock.json +++ b/test/no_std/cargo-bazel-lock.json @@ -1,5 +1,5 @@ { - "checksum": "6164a46262c78505261060c94f1edf16c7aa28aa5079fda34d529cea5b840e65", + "checksum": "d2f60d30b5b898f2ef5b7c36d267b835cd10fc7e5841255da4c216ccc6de544a", "crates": { "direct-cargo-bazel-deps 0.0.1": { "name": "direct-cargo-bazel-deps", From 0f25cb46213c3431fe8d363e8a9cfc719f50af67 Mon Sep 17 00:00:00 2001 From: UebelAndre Date: Mon, 8 May 2023 09:51:39 -0700 Subject: [PATCH 2/7] Removed `rust_toolchain.os` in favor of `rust_toolchain.exec_triple`. (#1960) * Removed `rust_toolchain.os` in favor of `rust_toolchain.exec_triple`. * Regenerate documentation --- crate_universe/private/crates_vendor.bzl | 2 +- docs/flatten.md | 20 +++++++------ docs/rust_repositories.md | 20 +++++++------ rust/private/repository_utils.bzl | 2 -- rust/private/rust.bzl | 2 +- rust/private/rustc.bzl | 38 +++++++++++------------- rust/private/rustdoc_test.bzl | 2 +- rust/private/utils.bzl | 4 +-- rust/toolchain.bzl | 30 +++++++++---------- test/toolchain/toolchain_test.bzl | 1 - test/unit/toolchain/toolchain_test.bzl | 4 --- 11 files changed, 59 insertions(+), 66 deletions(-) diff --git a/crate_universe/private/crates_vendor.bzl b/crate_universe/private/crates_vendor.bzl index 3cd6ed533b..0f9a6255f3 100644 --- a/crate_universe/private/crates_vendor.bzl +++ b/crate_universe/private/crates_vendor.bzl @@ -42,7 +42,7 @@ def _runfiles_path(file, is_windows): def _is_windows(ctx): toolchain = ctx.toolchains[Label("@rules_rust//rust:toolchain_type")] - return toolchain.target_triple.system == "windows" + return toolchain.target_os == "windows" def _get_output_package(ctx): # Determine output directory diff --git a/docs/flatten.md b/docs/flatten.md index 5f8bdd30d3..5fea2fdbcd 100644 --- a/docs/flatten.md +++ b/docs/flatten.md @@ -1142,8 +1142,8 @@ Run the test with `bazel test //hello_lib:greeting_test`. rust_toolchain(name, allocator_library, binary_ext, cargo, clippy_driver, debug_info, default_edition, dylib_ext, env, exec_triple, experimental_use_cc_common_link, extra_exec_rustc_flags, extra_rustc_flags, global_allocator_library, llvm_cov, - llvm_profdata, llvm_tools, opt_level, os, per_crate_rustc_flags, rust_doc, rust_std, - rustc, rustc_lib, rustfmt, staticlib_ext, stdlib_linkflags, target_json, target_triple) + llvm_profdata, llvm_tools, opt_level, per_crate_rustc_flags, rust_doc, rust_std, rustc, + rustc_lib, rustfmt, staticlib_ext, stdlib_linkflags, target_json, target_triple) Declares a Rust toolchain for use. @@ -1159,24 +1159,27 @@ load('@rules_rust//rust:toolchain.bzl', 'rust_toolchain') rust_toolchain( name = "rust_cpuX_impl", + binary_ext = "", + dylib_ext = ".so", + exec_triple = "cpuX-unknown-linux-gnu", + rust_doc = "@rust_cpuX//:rustdoc", + rust_std = "@rust_cpuX//:rust_std", rustc = "@rust_cpuX//:rustc", rustc_lib = "@rust_cpuX//:rustc_lib", - rust_std = "@rust_cpuX//:rust_std", - rust_doc = "@rust_cpuX//:rustdoc", - binary_ext = "", staticlib_ext = ".a", - dylib_ext = ".so", stdlib_linkflags = ["-lpthread", "-ldl"], - os = "linux", + target_triple = "cpuX-unknown-linux-gnu", ) toolchain( name = "rust_cpuX", exec_compatible_with = [ "@platforms//cpu:cpuX", + "@platforms//os:linux", ], target_compatible_with = [ "@platforms//cpu:cpuX", + "@platforms//os:linux", ], toolchain = ":rust_cpuX_impl", ) @@ -1184,7 +1187,7 @@ toolchain( Then, either add the label of the toolchain rule to `register_toolchains` in the WORKSPACE, or pass it to the `"--extra_toolchains"` flag for Bazel, and it will be used. -See @rules_rust//rust:repositories.bzl for examples of defining the @rust_cpuX repository with the actual binaries and libraries. +See `@rules_rust//rust:repositories.bzl` for examples of defining the `@rust_cpuX` repository with the actual binaries and libraries. **ATTRIBUTES** @@ -1210,7 +1213,6 @@ See @rules_rust//rust:repositories.bzl for examples of defining the @rust_cpuX r | llvm_profdata | The location of the llvm-profdata binary. Can be a direct source or a filegroup containing one item. If llvm_cov is None, this can be None as well and rust code is not instrumented for coverage. | Label | optional | None | | llvm_tools | LLVM tools that are shipped with the Rust toolchain. | Label | optional | None | | opt_level | Rustc optimization levels. | Dictionary: String -> String | optional | {"dbg": "0", "fastbuild": "0", "opt": "3"} | -| os | The operating system for the current toolchain | String | optional | "" | | per_crate_rustc_flags | Extra flags to pass to rustc in non-exec configuration | List of strings | optional | [] | | rust_doc | The location of the rustdoc binary. Can be a direct source or a filegroup containing one item. | Label | required | | | rust_std | The Rust standard library. | Label | optional | None | diff --git a/docs/rust_repositories.md b/docs/rust_repositories.md index fc7386dd28..1e7e8f524c 100644 --- a/docs/rust_repositories.md +++ b/docs/rust_repositories.md @@ -39,8 +39,8 @@ A dedicated filegroup-like rule for Rust stdlib artifacts. rust_toolchain(name, allocator_library, binary_ext, cargo, clippy_driver, debug_info, default_edition, dylib_ext, env, exec_triple, experimental_use_cc_common_link, extra_exec_rustc_flags, extra_rustc_flags, global_allocator_library, llvm_cov, - llvm_profdata, llvm_tools, opt_level, os, per_crate_rustc_flags, rust_doc, rust_std, - rustc, rustc_lib, rustfmt, staticlib_ext, stdlib_linkflags, target_json, target_triple) + llvm_profdata, llvm_tools, opt_level, per_crate_rustc_flags, rust_doc, rust_std, rustc, + rustc_lib, rustfmt, staticlib_ext, stdlib_linkflags, target_json, target_triple) Declares a Rust toolchain for use. @@ -56,24 +56,27 @@ load('@rules_rust//rust:toolchain.bzl', 'rust_toolchain') rust_toolchain( name = "rust_cpuX_impl", + binary_ext = "", + dylib_ext = ".so", + exec_triple = "cpuX-unknown-linux-gnu", + rust_doc = "@rust_cpuX//:rustdoc", + rust_std = "@rust_cpuX//:rust_std", rustc = "@rust_cpuX//:rustc", rustc_lib = "@rust_cpuX//:rustc_lib", - rust_std = "@rust_cpuX//:rust_std", - rust_doc = "@rust_cpuX//:rustdoc", - binary_ext = "", staticlib_ext = ".a", - dylib_ext = ".so", stdlib_linkflags = ["-lpthread", "-ldl"], - os = "linux", + target_triple = "cpuX-unknown-linux-gnu", ) toolchain( name = "rust_cpuX", exec_compatible_with = [ "@platforms//cpu:cpuX", + "@platforms//os:linux", ], target_compatible_with = [ "@platforms//cpu:cpuX", + "@platforms//os:linux", ], toolchain = ":rust_cpuX_impl", ) @@ -81,7 +84,7 @@ toolchain( Then, either add the label of the toolchain rule to `register_toolchains` in the WORKSPACE, or pass it to the `"--extra_toolchains"` flag for Bazel, and it will be used. -See @rules_rust//rust:repositories.bzl for examples of defining the @rust_cpuX repository with the actual binaries and libraries. +See `@rules_rust//rust:repositories.bzl` for examples of defining the `@rust_cpuX` repository with the actual binaries and libraries. **ATTRIBUTES** @@ -107,7 +110,6 @@ See @rules_rust//rust:repositories.bzl for examples of defining the @rust_cpuX r | llvm_profdata | The location of the llvm-profdata binary. Can be a direct source or a filegroup containing one item. If llvm_cov is None, this can be None as well and rust code is not instrumented for coverage. | Label | optional | None | | llvm_tools | LLVM tools that are shipped with the Rust toolchain. | Label | optional | None | | opt_level | Rustc optimization levels. | Dictionary: String -> String | optional | {"dbg": "0", "fastbuild": "0", "opt": "3"} | -| os | The operating system for the current toolchain | String | optional | "" | | per_crate_rustc_flags | Extra flags to pass to rustc in non-exec configuration | List of strings | optional | [] | | rust_doc | The location of the rustdoc binary. Can be a direct source or a filegroup containing one item. | Label | required | | | rust_std | The Rust standard library. | Label | optional | None | diff --git a/rust/private/repository_utils.bzl b/rust/private/repository_utils.bzl index 0a8acd197c..f4188102bf 100644 --- a/rust/private/repository_utils.bzl +++ b/rust/private/repository_utils.bzl @@ -247,7 +247,6 @@ rust_toolchain( staticlib_ext = "{staticlib_ext}", dylib_ext = "{dylib_ext}", stdlib_linkflags = [{stdlib_linkflags}], - os = "{system}", default_edition = "{default_edition}", exec_triple = "{exec_triple}", target_triple = "{target_triple}", @@ -312,7 +311,6 @@ def BUILD_for_rust_toolchain( allocator_library = allocator_library_label, global_allocator_library = global_allocator_library_label, stdlib_linkflags = stdlib_linkflags, - system = target_triple.system, default_edition = default_edition, exec_triple = exec_triple.str, target_triple = target_triple.str, diff --git a/rust/private/rust.bzl b/rust/private/rust.bzl index 1cdc4252d5..0cc569ecaf 100644 --- a/rust/private/rust.bzl +++ b/rust/private/rust.bzl @@ -96,7 +96,7 @@ def _determine_lib_name(name, crate_type, toolchain, lib_hash = None): "please file an issue!").format(crate_type)) prefix = "lib" - if toolchain.target_triple and toolchain.target_triple.system == "windows" and crate_type not in ("lib", "rlib"): + if toolchain.target_triple and toolchain.target_os == "windows" and crate_type not in ("lib", "rlib"): prefix = "" if toolchain.target_arch == "wasm32" and crate_type == "cdylib": prefix = "" diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl index b69e6e4ba0..1feba504b1 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -115,13 +115,9 @@ def _get_rustc_env(attr, toolchain, crate_name): else: pre = "" - target_arch = "" - if toolchain.target_triple: - target_arch = toolchain.target_triple.arch - result = { - "CARGO_CFG_TARGET_ARCH": target_arch, - "CARGO_CFG_TARGET_OS": toolchain.os, + "CARGO_CFG_TARGET_ARCH": "" if toolchain.target_arch == None else toolchain.target_arch, + "CARGO_CFG_TARGET_OS": "" if toolchain.target_os == None else toolchain.target_os, "CARGO_CRATE_NAME": crate_name, "CARGO_PKG_AUTHORS": "", "CARGO_PKG_DESCRIPTION": "", @@ -455,9 +451,9 @@ def _symlink_for_ambiguous_lib(actions, toolchain, crate_info, lib): # Take the absolute value of hash() since it could be negative. path_hash = abs(hash(lib.path)) - lib_name = get_lib_name_for_windows(lib) if toolchain.os.startswith("windows") else get_lib_name_default(lib) + lib_name = get_lib_name_for_windows(lib) if toolchain.target_os.startswith("windows") else get_lib_name_default(lib) - if toolchain.os.startswith("windows"): + if toolchain.target_os.startswith("windows"): prefix = "" extension = ".lib" elif lib_name.endswith(".pic"): @@ -524,7 +520,7 @@ def _disambiguate_libs(actions, toolchain, crate_info, dep_info, use_pic): if _is_dylib(lib): continue artifact = get_preferred_artifact(lib, use_pic) - name = get_lib_name_for_windows(artifact) if toolchain.os.startswith("windows") else get_lib_name_default(artifact) + name = get_lib_name_for_windows(artifact) if toolchain.target_os.startswith("windows") else get_lib_name_default(artifact) # On Linux-like platforms, normally library base names start with # `lib`, following the pattern `lib[name].(a|lo)` and we pass @@ -534,10 +530,10 @@ def _disambiguate_libs(actions, toolchain, crate_info, dep_info, use_pic): # FIXME: Under the native-link-modifiers unstable rustc feature, # we could use -lstatic:+verbatim instead. needs_symlink_to_standardize_name = ( - (toolchain.os.startswith("linux") or toolchain.os.startswith("mac") or toolchain.os.startswith("darwin")) and + toolchain.target_os.startswith(("linux", "mac", "darwin")) and artifact.basename.endswith(".a") and not artifact.basename.startswith("lib") ) or ( - toolchain.os.startswith("windows") and not artifact.basename.endswith(".lib") + toolchain.target_os.startswith("windows") and not artifact.basename.endswith(".lib") ) # Detect cases where we need to disambiguate library dependencies @@ -871,7 +867,7 @@ def construct_arguments( json = ["artifacts"] if error_format == "short": json.append("diagnostic-short") - elif error_format == "human" and toolchain.os != "windows": + elif error_format == "human" and toolchain.target_os != "windows": # If the os is not windows, we can get colorized output. json.append("diagnostic-rendered-ansi") @@ -1190,7 +1186,7 @@ def rustc_compile_action( # For a cdylib that might be added as a dependency to a cc_* target on Windows, it is important to include the # interface library that rustc generates in the output files. interface_library = None - if toolchain.os == "windows" and crate_info.type == "cdylib": + if toolchain.target_os == "windows" and crate_info.type == "cdylib": # Rustc generates the import library with a `.dll.lib` extension rather than the usual `.lib` one that msvc # expects (see https://github.com/rust-lang/rust/pull/29520 for more context). interface_library = ctx.actions.declare_file(crate_info.output.basename + ".lib", sibling = crate_info.output) @@ -1204,10 +1200,10 @@ def rustc_compile_action( pdb_file = None dsym_folder = None if crate_info.type in ("cdylib", "bin"): - if toolchain.os == "windows": + if toolchain.target_os == "windows": pdb_file = ctx.actions.declare_file(crate_info.output.basename[:-len(crate_info.output.extension)] + "pdb", sibling = crate_info.output) action_outputs.append(pdb_file) - elif toolchain.os == "darwin": + elif toolchain.target_os == "darwin": dsym_folder = ctx.actions.declare_directory(crate_info.output.basename + ".dSYM", sibling = crate_info.output) action_outputs.append(dsym_folder) @@ -1306,7 +1302,7 @@ def rustc_compile_action( # a (lib)foo_bar output file. if crate_info.type == "cdylib": output_lib = crate_info.output.basename - if toolchain.os != "windows": + if toolchain.target_os != "windows": # Strip the leading "lib" prefix output_lib = output_lib[3:] @@ -1584,7 +1580,7 @@ def _compute_rpaths(toolchain, output_dir, dep_info, use_pic): # Windows has no rpath equivalent, so always return an empty depset. # Fuchsia assembles shared libraries during packaging. - if toolchain.os == "windows" or toolchain.os == "fuchsia": + if toolchain.target_os == "windows" or toolchain.target_os == "fuchsia": return depset([]) dylibs = [ @@ -1600,9 +1596,9 @@ def _compute_rpaths(toolchain, output_dir, dep_info, use_pic): # without a version of Bazel that includes # https://github.com/bazelbuild/bazel/pull/13427. This is known to not be # included in Bazel 4.1 and below. - if toolchain.os != "linux" and toolchain.os != "darwin": + if toolchain.target_os != "linux" and toolchain.target_os != "darwin": fail("Runtime linking is not supported on {}, but found {}".format( - toolchain.os, + toolchain.target_os, dep_info.transitive_noncrates, )) @@ -1822,10 +1818,10 @@ def _add_native_link_flags(args, dep_info, linkstamp_outs, ambiguous_libs, crate use_pic = _should_use_pic(cc_toolchain, feature_configuration, crate_type, compilation_mode) - if toolchain.os == "windows": + if toolchain.target_os == "windows": make_link_flags = _make_link_flags_windows get_lib_name = get_lib_name_for_windows - elif toolchain.os.startswith("mac") or toolchain.os.startswith("darwin") or toolchain.os.startswith("ios"): + elif toolchain.target_os.startswith(("mac", "darwin", "ios")): make_link_flags = _make_link_flags_darwin get_lib_name = get_lib_name_default else: diff --git a/rust/private/rustdoc_test.bzl b/rust/private/rustdoc_test.bzl index 10be63eb6b..e621d20a5c 100644 --- a/rust/private/rustdoc_test.bzl +++ b/rust/private/rustdoc_test.bzl @@ -130,7 +130,7 @@ def _rust_doc_test_impl(ctx): owner = ctx.label, ) - if toolchain.os == "windows": + if toolchain.target_os == "windows": test_runner = ctx.actions.declare_file(ctx.label.name + ".rustdoc_test.bat") else: test_runner = ctx.actions.declare_file(ctx.label.name + ".rustdoc_test.sh") diff --git a/rust/private/utils.bzl b/rust/private/utils.bzl index dfaf3f3e9e..6cb8b0691c 100644 --- a/rust/private/utils.bzl +++ b/rust/private/utils.bzl @@ -135,7 +135,7 @@ def get_lib_name_default(lib): # so the following doesn't work: # args.add_all( # cc_toolchain.dynamic_runtime_lib(feature_configuration = feature_configuration), -# map_each = lambda x: get_lib_name(x, for_windows = toolchain.os.startswith("windows)), +# map_each = lambda x: get_lib_name(x, for_windows = toolchain.target_os.startswith("windows)), # format_each = "-ldylib=%s", # ) def get_lib_name_for_windows(lib): @@ -679,7 +679,7 @@ def can_build_metadata(toolchain, ctx, crate_type): # 3) process_wrapper is enabled (this is disabled when compiling process_wrapper itself), # 4) the crate_type is rlib or lib. return toolchain._pipelined_compilation and \ - toolchain.os != "windows" and \ + toolchain.exec_triple.system != "windows" and \ ctx.attr._process_wrapper and \ crate_type in ("rlib", "lib") diff --git a/rust/toolchain.bzl b/rust/toolchain.bzl index 7ceb6c8744..6b2aac76a7 100644 --- a/rust/toolchain.bzl +++ b/rust/toolchain.bzl @@ -559,11 +559,7 @@ def _rust_toolchain_impl(ctx): exec_triple = triple(ctx.attr.exec_triple) - exec_os = ctx.attr.os - if not ctx.attr.os: - exec_os = exec_triple.system - - if not exec_os: + if not exec_triple.system: fail("No system was provided for the execution platform. Please update {}".format( ctx.label, )) @@ -576,10 +572,12 @@ def _rust_toolchain_impl(ctx): target_triple = None target_json = None target_arch = None + target_os = None if ctx.attr.target_triple: target_triple = triple(ctx.attr.target_triple) target_arch = target_triple.arch + target_os = target_triple.system elif ctx.attr.target_json: # Ensure the data provided is valid json @@ -593,6 +591,8 @@ def _rust_toolchain_impl(ctx): if "arch" in target_json_content: target_arch = target_json_content["arch"] + if "os" in target_json_content: + target_os = target_json_content["os"] else: fail("Either `target_triple` or `target_json` must be provided. Please update {}".format( ctx.label, @@ -615,7 +615,6 @@ def _rust_toolchain_impl(ctx): llvm_cov = ctx.file.llvm_cov, llvm_profdata = ctx.file.llvm_profdata, make_variables = make_variable_info, - os = exec_os, rust_doc = sysroot.rustdoc, rust_std = sysroot.rust_std, rust_std_paths = depset([file.dirname for file in sysroot.rust_std.to_list()]), @@ -632,6 +631,7 @@ def _rust_toolchain_impl(ctx): target_arch = target_arch, target_flag_value = target_json.path if target_json else target_triple.str, target_json = target_json, + target_os = target_os, target_triple = target_triple, # Experimental and incompatible flags @@ -731,9 +731,6 @@ rust_toolchain = rule( "opt": "3", }, ), - "os": attr.string( - doc = "The operating system for the current toolchain", - ), "per_crate_rustc_flags": attr.string_list( doc = "Extra flags to pass to rustc in non-exec configuration", ), @@ -824,24 +821,27 @@ load('@rules_rust//rust:toolchain.bzl', 'rust_toolchain') rust_toolchain( name = "rust_cpuX_impl", + binary_ext = "", + dylib_ext = ".so", + exec_triple = "cpuX-unknown-linux-gnu", + rust_doc = "@rust_cpuX//:rustdoc", + rust_std = "@rust_cpuX//:rust_std", rustc = "@rust_cpuX//:rustc", rustc_lib = "@rust_cpuX//:rustc_lib", - rust_std = "@rust_cpuX//:rust_std", - rust_doc = "@rust_cpuX//:rustdoc", - binary_ext = "", staticlib_ext = ".a", - dylib_ext = ".so", stdlib_linkflags = ["-lpthread", "-ldl"], - os = "linux", + target_triple = "cpuX-unknown-linux-gnu", ) toolchain( name = "rust_cpuX", exec_compatible_with = [ "@platforms//cpu:cpuX", + "@platforms//os:linux", ], target_compatible_with = [ "@platforms//cpu:cpuX", + "@platforms//os:linux", ], toolchain = ":rust_cpuX_impl", ) @@ -850,7 +850,7 @@ toolchain( Then, either add the label of the toolchain rule to `register_toolchains` in the WORKSPACE, or pass \ it to the `"--extra_toolchains"` flag for Bazel, and it will be used. -See @rules_rust//rust:repositories.bzl for examples of defining the @rust_cpuX repository \ +See `@rules_rust//rust:repositories.bzl` for examples of defining the `@rust_cpuX` repository \ with the actual binaries and libraries. """, ) diff --git a/test/toolchain/toolchain_test.bzl b/test/toolchain/toolchain_test.bzl index 04010ec8b5..2b820e7bea 100644 --- a/test/toolchain/toolchain_test.bzl +++ b/test/toolchain/toolchain_test.bzl @@ -121,7 +121,6 @@ def _define_targets(): dylib_ext = ".so", exec_triple = "x86_64-unknown-none", target_triple = "x86_64-unknown-none", - os = "linux", rust_doc = ":mock_rustdoc", rust_std = ":std_libs", rustc = ":mock_rustc", diff --git a/test/unit/toolchain/toolchain_test.bzl b/test/unit/toolchain/toolchain_test.bzl index 7b0e7263f2..d4056f1424 100644 --- a/test/unit/toolchain/toolchain_test.bzl +++ b/test/unit/toolchain/toolchain_test.bzl @@ -85,7 +85,6 @@ def _define_test_targets(): binary_ext = "", dylib_ext = ".so", exec_triple = "x86_64-unknown-none", - os = "linux", rust_doc = ":mock_rustdoc", rust_std = ":std_libs", rustc = ":mock_rustc", @@ -101,7 +100,6 @@ def _define_test_targets(): binary_ext = "", dylib_ext = ".so", exec_triple = "x86_64-unknown-none", - os = "linux", rust_doc = ":mock_rustdoc", rust_std = ":std_libs", rustc = ":mock_rustc", @@ -115,7 +113,6 @@ def _define_test_targets(): binary_ext = "", dylib_ext = ".so", exec_triple = "x86_64-unknown-none", - os = "linux", rust_doc = ":mock_rustdoc", rust_std = ":std_libs", rustc = ":mock_rustc", @@ -137,7 +134,6 @@ def _define_test_targets(): binary_ext = "", dylib_ext = ".so", exec_triple = "x86_64-unknown-none", - os = "linux", rust_doc = ":mock_rustdoc", rust_std = ":std_libs", rustc = ":mock_rustc", From 285dcbbb9d93fcc09388f4ffd3c59740e73886ef Mon Sep 17 00:00:00 2001 From: James Leitch Date: Wed, 10 May 2023 09:22:55 -0700 Subject: [PATCH 3/7] feature: expose `extra_rustc_flags` and `extra_exec_rustc_flags` at `rust_register_toolchains` (#1959) --- docs/flatten.md | 22 +++++++++++++++++----- docs/rust_repositories.md | 22 +++++++++++++++++----- rust/private/repository_utils.bzl | 11 +++++++++-- rust/repositories.bzl | 26 ++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 12 deletions(-) diff --git a/docs/flatten.md b/docs/flatten.md index 5fea2fdbcd..580eb0e214 100644 --- a/docs/flatten.md +++ b/docs/flatten.md @@ -1256,8 +1256,9 @@ Generates a toolchain-bearing repository that declares the toolchains from some
 rust_toolchain_tools_repository(name, allocator_library, auth, dev_components, edition, exec_triple,
-                                global_allocator_library, iso_date, repo_mapping, rustfmt_version,
-                                sha256s, target_triple, urls, version)
+                                extra_exec_rustc_flags, extra_rustc_flags, global_allocator_library,
+                                iso_date, repo_mapping, rustfmt_version, sha256s, target_triple, urls,
+                                version)
 
Composes a single workspace containing the toolchain components for compiling on a given platform to a series of target platforms. @@ -1275,6 +1276,8 @@ A given instance of this rule should be accompanied by a toolchain_repository_pr | dev_components | Whether to download the rustc-dev components (defaults to False). Requires version to be "nightly". | Boolean | optional | False | | edition | The rust edition to be used by default (2015, 2018, or 2021). If absent, every rule is required to specify its edition attribute. | String | optional | "" | | exec_triple | The Rust-style target that this compiler runs on | String | required | | +| extra_exec_rustc_flags | Extra flags to pass to rustc in exec configuration | List of strings | optional | [] | +| extra_rustc_flags | Extra flags to pass to rustc in non-exec configuration | List of strings | optional | [] | | global_allocator_library | Target that provides allocator functions when a global allocator is used with cc_common.link. | String | optional | "" | | iso_date | The date of the tool (or None, if the version is a specific version). | String | optional | "" | | repo_mapping | A dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.<p>For example, an entry "@foo": "@bar" declares that, for any time this repository depends on @foo (such as a dependency on @foo//some:target, it should actually resolve that dependency within globally-declared @bar (@bar//some:target). | Dictionary: String -> String | required | | @@ -1779,7 +1782,8 @@ This macro should be called immediately after the `rust_proto_repositories` macr
 rust_register_toolchains(dev_components, edition, allocator_library, global_allocator_library,
                          iso_date, register_toolchains, rustfmt_version, rust_analyzer_version,
-                         sha256s, extra_target_triples, urls, version, versions)
+                         sha256s, extra_target_triples, extra_rustc_flags, extra_exec_rustc_flags,
+                         urls, version, versions)
 
Emits a default set of toolchains for Linux, MacOS, and Freebsd @@ -1814,6 +1818,8 @@ See `load_arbitrary_tool` in `@rules_rust//rust:repositories.bzl` for more detai | rust_analyzer_version | The version of Rustc to pair with rust-analyzer. | `None` | | sha256s | A dict associating tool subdirectories to sha256 hashes. | `None` | | extra_target_triples | Additional rust-style targets that rust toolchains should support. | `["wasm32-unknown-unknown", "wasm32-wasi"]` | +| extra_rustc_flags | Dictionary of target triples to list of extra flags to pass to rustc in non-exec configuration. | `None` | +| extra_exec_rustc_flags | Extra flags to pass to rustc in exec configuration. | `None` | | urls | A list of mirror urls containing the tools from the Rust-lang static file server. These must contain the '{}' used to substitute the tool being fetched (using .format). | `["https://static.rust-lang.org/dist/{}.tar.gz"]` | | version | **Deprecated**: Use versions instead. | `None` | | versions | A list of toolchain versions to download. This paramter only accepts one versions per channel. E.g. ["1.65.0", "nightly/2022-11-02", "beta/2020-12-30"]. | `[]` | @@ -1844,7 +1850,8 @@ rust_repositories(kwargs)
 rust_repository_set(name, exec_triple, target_settings, version, versions, allocator_library,
                     global_allocator_library, extra_target_triples, iso_date, rustfmt_version,
-                    edition, dev_components, sha256s, urls, auth, register_toolchain)
+                    edition, dev_components, extra_rustc_flags, extra_exec_rustc_flags, sha256s, urls,
+                    auth, register_toolchain)
 
Assembles a remote repository for the given toolchain params, produces a proxy repository to contain the toolchain declaration, and registers the toolchains. @@ -1866,6 +1873,8 @@ Assembles a remote repository for the given toolchain params, produces a proxy r | rustfmt_version | The version of rustfmt to be associated with the toolchain. | `None` | | edition | The rust edition to be used by default (2015, 2018, or 2021). If absent, every rule is required to specify its edition attribute. | `None` | | dev_components | Whether to download the rustc-dev components. Requires version to be "nightly". | `False` | +| extra_rustc_flags | Dictionary of target triples to list of extra flags to pass to rustc in non-exec configuration. | `None` | +| extra_exec_rustc_flags | Extra flags to pass to rustc in exec configuration. | `None` | | sha256s | A dict associating tool subdirectories to sha256 hashes. See [rust_repositories](#rust_repositories) for more details. | `None` | | urls | A list of mirror urls containing the tools from the Rust-lang static file server. These must contain the '{}' used to substitute the tool being fetched (using .format). | `["https://static.rust-lang.org/dist/{}.tar.gz"]` | | auth | Auth object compatible with repository_ctx.download to use when downloading files. See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details. | `None` | @@ -1944,7 +1953,8 @@ rust_test_suite( rust_toolchain_repository(name, version, exec_triple, target_triple, exec_compatible_with, target_compatible_with, target_settings, channel, allocator_library, global_allocator_library, iso_date, rustfmt_version, edition, - dev_components, sha256s, urls, auth) + dev_components, extra_rustc_flags, extra_exec_rustc_flags, sha256s, urls, + auth) Assembles a remote repository for the given toolchain params, produces a proxy repository to contain the toolchain declaration, and registers the toolchains. @@ -1968,6 +1978,8 @@ Assembles a remote repository for the given toolchain params, produces a proxy r | rustfmt_version | The version of rustfmt to be associated with the toolchain. | `None` | | edition | The rust edition to be used by default (2015, 2018, or 2021). If absent, every rule is required to specify its edition attribute. | `None` | | dev_components | Whether to download the rustc-dev components. Requires version to be "nightly". Defaults to False. | `False` | +| extra_rustc_flags | Extra flags to pass to rustc in non-exec configuration. | `None` | +| extra_exec_rustc_flags | Extra flags to pass to rustc in exec configuration. | `None` | | sha256s | A dict associating tool subdirectories to sha256 hashes. See [rust_repositories](#rust_repositories) for more details. | `None` | | urls | A list of mirror urls containing the tools from the Rust-lang static file server. These must contain the '{}' used to substitute the tool being fetched (using .format). Defaults to ['https://static.rust-lang.org/dist/{}.tar.gz'] | `["https://static.rust-lang.org/dist/{}.tar.gz"]` | | auth | Auth object compatible with repository_ctx.download to use when downloading files. See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details. | `None` | diff --git a/docs/rust_repositories.md b/docs/rust_repositories.md index 1e7e8f524c..f5d836d92c 100644 --- a/docs/rust_repositories.md +++ b/docs/rust_repositories.md @@ -153,8 +153,9 @@ Generates a toolchain-bearing repository that declares the toolchains from some
 rust_toolchain_tools_repository(name, allocator_library, auth, dev_components, edition, exec_triple,
-                                global_allocator_library, iso_date, repo_mapping, rustfmt_version,
-                                sha256s, target_triple, urls, version)
+                                extra_exec_rustc_flags, extra_rustc_flags, global_allocator_library,
+                                iso_date, repo_mapping, rustfmt_version, sha256s, target_triple, urls,
+                                version)
 
Composes a single workspace containing the toolchain components for compiling on a given platform to a series of target platforms. @@ -172,6 +173,8 @@ A given instance of this rule should be accompanied by a toolchain_repository_pr | dev_components | Whether to download the rustc-dev components (defaults to False). Requires version to be "nightly". | Boolean | optional | False | | edition | The rust edition to be used by default (2015, 2018, or 2021). If absent, every rule is required to specify its edition attribute. | String | optional | "" | | exec_triple | The Rust-style target that this compiler runs on | String | required | | +| extra_exec_rustc_flags | Extra flags to pass to rustc in exec configuration | List of strings | optional | [] | +| extra_rustc_flags | Extra flags to pass to rustc in non-exec configuration | List of strings | optional | [] | | global_allocator_library | Target that provides allocator functions when a global allocator is used with cc_common.link. | String | optional | "" | | iso_date | The date of the tool (or None, if the version is a specific version). | String | optional | "" | | repo_mapping | A dictionary from local repository name to global repository name. This allows controls over workspace dependency resolution for dependencies of this repository.<p>For example, an entry "@foo": "@bar" declares that, for any time this repository depends on @foo (such as a dependency on @foo//some:target, it should actually resolve that dependency within globally-declared @bar (@bar//some:target). | Dictionary: String -> String | required | | @@ -231,7 +234,8 @@ str: The name of a registerable rust_analyzer_toolchain.
 rust_register_toolchains(dev_components, edition, allocator_library, global_allocator_library,
                          iso_date, register_toolchains, rustfmt_version, rust_analyzer_version,
-                         sha256s, extra_target_triples, urls, version, versions)
+                         sha256s, extra_target_triples, extra_rustc_flags, extra_exec_rustc_flags,
+                         urls, version, versions)
 
Emits a default set of toolchains for Linux, MacOS, and Freebsd @@ -266,6 +270,8 @@ See `load_arbitrary_tool` in `@rules_rust//rust:repositories.bzl` for more detai | rust_analyzer_version | The version of Rustc to pair with rust-analyzer. | `None` | | sha256s | A dict associating tool subdirectories to sha256 hashes. | `None` | | extra_target_triples | Additional rust-style targets that rust toolchains should support. | `["wasm32-unknown-unknown", "wasm32-wasi"]` | +| extra_rustc_flags | Dictionary of target triples to list of extra flags to pass to rustc in non-exec configuration. | `None` | +| extra_exec_rustc_flags | Extra flags to pass to rustc in exec configuration. | `None` | | urls | A list of mirror urls containing the tools from the Rust-lang static file server. These must contain the '{}' used to substitute the tool being fetched (using .format). | `["https://static.rust-lang.org/dist/{}.tar.gz"]` | | version | **Deprecated**: Use versions instead. | `None` | | versions | A list of toolchain versions to download. This paramter only accepts one versions per channel. E.g. ["1.65.0", "nightly/2022-11-02", "beta/2020-12-30"]. | `[]` | @@ -296,7 +302,8 @@ rust_repositories(kwargs)
 rust_repository_set(name, exec_triple, target_settings, version, versions, allocator_library,
                     global_allocator_library, extra_target_triples, iso_date, rustfmt_version,
-                    edition, dev_components, sha256s, urls, auth, register_toolchain)
+                    edition, dev_components, extra_rustc_flags, extra_exec_rustc_flags, sha256s, urls,
+                    auth, register_toolchain)
 
Assembles a remote repository for the given toolchain params, produces a proxy repository to contain the toolchain declaration, and registers the toolchains. @@ -318,6 +325,8 @@ Assembles a remote repository for the given toolchain params, produces a proxy r | rustfmt_version | The version of rustfmt to be associated with the toolchain. | `None` | | edition | The rust edition to be used by default (2015, 2018, or 2021). If absent, every rule is required to specify its edition attribute. | `None` | | dev_components | Whether to download the rustc-dev components. Requires version to be "nightly". | `False` | +| extra_rustc_flags | Dictionary of target triples to list of extra flags to pass to rustc in non-exec configuration. | `None` | +| extra_exec_rustc_flags | Extra flags to pass to rustc in exec configuration. | `None` | | sha256s | A dict associating tool subdirectories to sha256 hashes. See [rust_repositories](#rust_repositories) for more details. | `None` | | urls | A list of mirror urls containing the tools from the Rust-lang static file server. These must contain the '{}' used to substitute the tool being fetched (using .format). | `["https://static.rust-lang.org/dist/{}.tar.gz"]` | | auth | Auth object compatible with repository_ctx.download to use when downloading files. See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details. | `None` | @@ -332,7 +341,8 @@ Assembles a remote repository for the given toolchain params, produces a proxy r rust_toolchain_repository(name, version, exec_triple, target_triple, exec_compatible_with, target_compatible_with, target_settings, channel, allocator_library, global_allocator_library, iso_date, rustfmt_version, edition, - dev_components, sha256s, urls, auth) + dev_components, extra_rustc_flags, extra_exec_rustc_flags, sha256s, urls, + auth) Assembles a remote repository for the given toolchain params, produces a proxy repository to contain the toolchain declaration, and registers the toolchains. @@ -356,6 +366,8 @@ Assembles a remote repository for the given toolchain params, produces a proxy r | rustfmt_version | The version of rustfmt to be associated with the toolchain. | `None` | | edition | The rust edition to be used by default (2015, 2018, or 2021). If absent, every rule is required to specify its edition attribute. | `None` | | dev_components | Whether to download the rustc-dev components. Requires version to be "nightly". Defaults to False. | `False` | +| extra_rustc_flags | Extra flags to pass to rustc in non-exec configuration. | `None` | +| extra_exec_rustc_flags | Extra flags to pass to rustc in exec configuration. | `None` | | sha256s | A dict associating tool subdirectories to sha256 hashes. See [rust_repositories](#rust_repositories) for more details. | `None` | | urls | A list of mirror urls containing the tools from the Rust-lang static file server. These must contain the '{}' used to substitute the tool being fetched (using .format). Defaults to ['https://static.rust-lang.org/dist/{}.tar.gz'] | `["https://static.rust-lang.org/dist/{}.tar.gz"]` | | auth | Auth object compatible with repository_ctx.download to use when downloading files. See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details. | `None` | diff --git a/rust/private/repository_utils.bzl b/rust/private/repository_utils.bzl index f4188102bf..f593dbeab8 100644 --- a/rust/private/repository_utils.bzl +++ b/rust/private/repository_utils.bzl @@ -251,6 +251,8 @@ rust_toolchain( exec_triple = "{exec_triple}", target_triple = "{target_triple}", visibility = ["//visibility:public"], + extra_rustc_flags = {extra_rustc_flags}, + extra_exec_rustc_flags = {extra_exec_rustc_flags}, ) """ @@ -263,7 +265,9 @@ def BUILD_for_rust_toolchain( default_edition, include_rustfmt, include_llvm_tools, - stdlib_linkflags = None): + stdlib_linkflags = None, + extra_rustc_flags = None, + extra_exec_rustc_flags = None): """Emits a toolchain declaration to match an existing compiler and stdlib. Args: @@ -280,7 +284,8 @@ def BUILD_for_rust_toolchain( stdlib_linkflags (list, optional): Overriden flags needed for linking to rust stdlib, akin to BAZEL_LINKLIBS. Defaults to None. - + extra_rustc_flags (list, optional): Extra flags to pass to rustc in non-exec configuration. + extra_exec_rustc_flags (list, optional): Extra flags to pass to rustc in exec configuration. Returns: str: A rendered template of a `rust_toolchain` declaration @@ -317,6 +322,8 @@ def BUILD_for_rust_toolchain( rustfmt_label = rustfmt_label, llvm_cov_label = llvm_cov_label, llvm_profdata_label = llvm_profdata_label, + extra_rustc_flags = extra_rustc_flags, + extra_exec_rustc_flags = extra_exec_rustc_flags, ) _build_file_for_toolchain_template = """\ diff --git a/rust/repositories.bzl b/rust/repositories.bzl index 2fdd11dc29..2bc65e8f7d 100644 --- a/rust/repositories.bzl +++ b/rust/repositories.bzl @@ -106,6 +106,8 @@ def rust_register_toolchains( rust_analyzer_version = None, sha256s = None, extra_target_triples = DEFAULT_EXTRA_TARGET_TRIPLES, + extra_rustc_flags = None, + extra_exec_rustc_flags = None, urls = DEFAULT_STATIC_RUST_URL_TEMPLATES, version = None, versions = []): @@ -138,6 +140,8 @@ def rust_register_toolchains( rust_analyzer_version (str, optional): The version of Rustc to pair with rust-analyzer. sha256s (str, optional): A dict associating tool subdirectories to sha256 hashes. extra_target_triples (list, optional): Additional rust-style targets that rust toolchains should support. + extra_rustc_flags (dict, list, optional): Dictionary of target triples to list of extra flags to pass to rustc in non-exec configuration. + extra_exec_rustc_flags (list, optional): Extra flags to pass to rustc in exec configuration. urls (list, optional): A list of mirror urls containing the tools from the Rust-lang static file server. These must contain the '{}' used to substitute the tool being fetched (using .format). version (str, optional): **Deprecated**: Use `versions` instead. versions (list, optional): A list of toolchain versions to download. This paramter only accepts one versions @@ -225,6 +229,8 @@ def rust_register_toolchains( iso_date = iso_date, register_toolchain = register_toolchains, rustfmt_version = rustfmt_version, + extra_rustc_flags = extra_rustc_flags, + extra_exec_rustc_flags = extra_exec_rustc_flags, sha256s = sha256s, urls = urls, version = version, @@ -351,6 +357,8 @@ def _rust_toolchain_tools_repository_impl(ctx): default_edition = ctx.attr.edition, include_rustfmt = not (not ctx.attr.rustfmt_version), include_llvm_tools = include_llvm_tools, + extra_rustc_flags = ctx.attr.extra_rustc_flags, + extra_exec_rustc_flags = ctx.attr.extra_exec_rustc_flags, )) # Not all target triples are expected to have dev components @@ -393,6 +401,12 @@ rust_toolchain_tools_repository = repository_rule( doc = "The Rust-style target that this compiler runs on", mandatory = True, ), + "extra_exec_rustc_flags": attr.string_list( + doc = "Extra flags to pass to rustc in exec configuration", + ), + "extra_rustc_flags": attr.string_list( + doc = "Extra flags to pass to rustc in non-exec configuration", + ), "global_allocator_library": attr.string( doc = "Target that provides allocator functions when a global allocator is used with cc_common.link.", ), @@ -482,6 +496,8 @@ def rust_toolchain_repository( rustfmt_version = None, edition = None, dev_components = False, + extra_rustc_flags = None, + extra_exec_rustc_flags = None, sha256s = None, urls = DEFAULT_STATIC_RUST_URL_TEMPLATES, auth = None): @@ -505,6 +521,8 @@ def rust_toolchain_repository( edition (str, optional): The rust edition to be used by default (2015, 2018, or 2021). If absent, every rule is required to specify its `edition` attribute. dev_components (bool, optional): Whether to download the rustc-dev components. Requires version to be "nightly". Defaults to False. + extra_rustc_flags (list, optional): Extra flags to pass to rustc in non-exec configuration. + extra_exec_rustc_flags (list, optional): Extra flags to pass to rustc in exec configuration. sha256s (str, optional): A dict associating tool subdirectories to sha256 hashes. See [rust_repositories](#rust_repositories) for more details. urls (list, optional): A list of mirror urls containing the tools from the Rust-lang static file server. These must contain the '{}' used to substitute the tool being fetched (using .format). Defaults to ['https://static.rust-lang.org/dist/{}.tar.gz'] @@ -539,6 +557,8 @@ def rust_toolchain_repository( rustfmt_version = rustfmt_version, edition = edition, dev_components = dev_components, + extra_rustc_flags = extra_rustc_flags, + extra_exec_rustc_flags = extra_exec_rustc_flags, sha256s = sha256s, urls = urls, auth = auth, @@ -866,6 +886,8 @@ def rust_repository_set( rustfmt_version = None, edition = None, dev_components = False, + extra_rustc_flags = None, + extra_exec_rustc_flags = None, sha256s = None, urls = DEFAULT_STATIC_RUST_URL_TEMPLATES, auth = None, @@ -892,6 +914,8 @@ def rust_repository_set( required to specify its `edition` attribute. dev_components (bool, optional): Whether to download the rustc-dev components. Requires version to be "nightly". + extra_rustc_flags (dict, list, optional): Dictionary of target triples to list of extra flags to pass to rustc in non-exec configuration. + extra_exec_rustc_flags (list, optional): Extra flags to pass to rustc in exec configuration. sha256s (str, optional): A dict associating tool subdirectories to sha256 hashes. See [rust_repositories](#rust_repositories) for more details. urls (list, optional): A list of mirror urls containing the tools from the Rust-lang static file server. These @@ -929,6 +953,8 @@ def rust_repository_set( dev_components = dev_components, edition = edition, exec_triple = exec_triple, + extra_exec_rustc_flags = extra_exec_rustc_flags, + extra_rustc_flags = extra_rustc_flags.get(toolchain.target_triple) if extra_rustc_flags != None else None, target_settings = target_settings, iso_date = toolchain.channel.iso_date, rustfmt_version = rustfmt_version, From 66b1bf1655cc61147ba800daeb3b51f8e547195b Mon Sep 17 00:00:00 2001 From: James Leitch Date: Wed, 10 May 2023 14:01:12 -0700 Subject: [PATCH 4/7] fix: lld-link (MSVC) fix flags including `-l` prefix (#1958) --- rust/private/rustc.bzl | 47 ++++++-- test/unit/native_deps/native_deps_test.bzl | 106 +++++++++++++----- .../proc_macro_does_not_leak_deps.bzl | 16 ++- 3 files changed, 128 insertions(+), 41 deletions(-) diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl index 1feba504b1..2e8e82ca5b 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -1703,7 +1703,7 @@ def _get_crate_dirname(crate): """ return crate.output.dirname -def _portable_link_flags(lib, use_pic, ambiguous_libs, get_lib_name, for_windows = False, for_darwin = False): +def _portable_link_flags(lib, use_pic, ambiguous_libs, get_lib_name, for_windows = False, for_darwin = False, flavor_msvc = False): artifact = get_preferred_artifact(lib, use_pic) if ambiguous_libs and artifact.path in ambiguous_libs: artifact = ambiguous_libs[artifact.path] @@ -1742,10 +1742,23 @@ def _portable_link_flags(lib, use_pic, ambiguous_libs, get_lib_name, for_windows artifact.basename.startswith("test-") or artifact.basename.startswith("std-") ): return [] if for_darwin else ["-lstatic=%s" % get_lib_name(artifact)] - return [ - "-lstatic=%s" % get_lib_name(artifact), - "-Clink-arg=-l%s" % (get_lib_name(artifact) if not for_windows else artifact.basename), - ] + + if for_windows: + if flavor_msvc: + return [ + "-lstatic=%s" % get_lib_name(artifact), + "-Clink-arg={}".format(artifact.basename), + ] + else: + return [ + "-lstatic=%s" % get_lib_name(artifact), + "-Clink-arg=-l{}".format(artifact.basename), + ] + else: + return [ + "-lstatic=%s" % get_lib_name(artifact), + "-Clink-arg=-l{}".format(get_lib_name(artifact)), + ] elif _is_dylib(lib): return [ "-ldylib=%s" % get_lib_name(artifact), @@ -1753,16 +1766,32 @@ def _portable_link_flags(lib, use_pic, ambiguous_libs, get_lib_name, for_windows return [] -def _make_link_flags_windows(linker_input_and_use_pic_and_ambiguous_libs): +def _make_link_flags_windows(linker_input_and_use_pic_and_ambiguous_libs, flavor_msvc): linker_input, use_pic, ambiguous_libs = linker_input_and_use_pic_and_ambiguous_libs ret = [] for lib in linker_input.libraries: if lib.alwayslink: - ret.extend(["-C", "link-arg=/WHOLEARCHIVE:%s" % get_preferred_artifact(lib, use_pic).path]) + if flavor_msvc: + ret.extend(["-C", "link-arg=/WHOLEARCHIVE:%s" % get_preferred_artifact(lib, use_pic).path]) + else: + ret.extend([ + "-C", + "link-arg=-Wl,--whole-archive", + "-C", + ("link-arg=%s" % get_preferred_artifact(lib, use_pic).path), + "-C", + "link-arg=-Wl,--no-whole-archive", + ]) else: - ret.extend(_portable_link_flags(lib, use_pic, ambiguous_libs, get_lib_name_for_windows, for_windows = True)) + ret.extend(_portable_link_flags(lib, use_pic, ambiguous_libs, get_lib_name_for_windows, for_windows = True, flavor_msvc = flavor_msvc)) return ret +def _make_link_flags_windows_msvc(linker_input_and_use_pic_and_ambiguous_libs): + return _make_link_flags_windows(linker_input_and_use_pic_and_ambiguous_libs, flavor_msvc = True) + +def _make_link_flags_windows_gnu(linker_input_and_use_pic_and_ambiguous_libs): + return _make_link_flags_windows(linker_input_and_use_pic_and_ambiguous_libs, flavor_msvc = False) + def _make_link_flags_darwin(linker_input_and_use_pic_and_ambiguous_libs): linker_input, use_pic, ambiguous_libs = linker_input_and_use_pic_and_ambiguous_libs ret = [] @@ -1819,7 +1848,7 @@ def _add_native_link_flags(args, dep_info, linkstamp_outs, ambiguous_libs, crate use_pic = _should_use_pic(cc_toolchain, feature_configuration, crate_type, compilation_mode) if toolchain.target_os == "windows": - make_link_flags = _make_link_flags_windows + make_link_flags = _make_link_flags_windows_msvc if toolchain.target_triple.abi == "msvc" else _make_link_flags_windows_gnu get_lib_name = get_lib_name_for_windows elif toolchain.target_os.startswith(("mac", "darwin", "ios")): make_link_flags = _make_link_flags_darwin diff --git a/test/unit/native_deps/native_deps_test.bzl b/test/unit/native_deps/native_deps_test.bzl index 66c4bc07fa..a086afb3c1 100644 --- a/test/unit/native_deps/native_deps_test.bzl +++ b/test/unit/native_deps/native_deps_test.bzl @@ -13,6 +13,9 @@ load( "assert_list_contains_adjacent_elements", ) +def _get_toolchain(ctx): + return ctx.attr._toolchain[platform_common.ToolchainInfo] + def _get_darwin_component(arg): # path/to/darwin_x86_64-fastbuild-fastbuild/package -> darwin_x86_64-fastbuild darwin_component = [x for x in arg.split("/") if x.startswith("darwin")][0] @@ -34,12 +37,19 @@ def _cdylib_has_native_libs_test_impl(ctx): env = analysistest.begin(ctx) tut = analysistest.target_under_test(env) action = tut.actions[0] + toolchain = _get_toolchain(ctx) compilation_mode = ctx.var["COMPILATION_MODE"] pic_suffix = _get_pic_suffix(ctx, compilation_mode) assert_argv_contains_prefix_suffix(env, action, "-Lnative=", "/native_deps") assert_argv_contains(env, action, "--crate-type=cdylib") assert_argv_contains(env, action, "-lstatic=native_dep{}".format(pic_suffix)) - native_link_arg = "-Clink-arg=-lnative_dep.lib" if ctx.target_platform_has_constraint(ctx.attr._windows_constraint[platform_common.ConstraintValueInfo]) else "-Clink-arg=-lnative_dep{}".format(pic_suffix) + if toolchain.target_os == "windows": + if toolchain.target_triple.abi == "msvc": + native_link_arg = "-Clink-arg=native_dep.lib" + else: + native_link_arg = "-Clink-arg=-lnative_dep.lib" + else: + native_link_arg = "-Clink-arg=-lnative_dep{}".format(pic_suffix) assert_argv_contains(env, action, native_link_arg) assert_argv_contains_prefix(env, action, "--codegen=linker=") return analysistest.end(env) @@ -48,10 +58,17 @@ def _staticlib_has_native_libs_test_impl(ctx): env = analysistest.begin(ctx) tut = analysistest.target_under_test(env) action = tut.actions[0] + toolchain = _get_toolchain(ctx) assert_argv_contains_prefix_suffix(env, action, "-Lnative=", "/native_deps") assert_argv_contains(env, action, "--crate-type=staticlib") assert_argv_contains(env, action, "-lstatic=native_dep") - native_link_arg = "-Clink-arg=-lnative_dep.lib" if ctx.target_platform_has_constraint(ctx.attr._windows_constraint[platform_common.ConstraintValueInfo]) else "-Clink-arg=-lnative_dep" + if toolchain.target_os == "windows": + if toolchain.target_triple.abi == "msvc": + native_link_arg = "-Clink-arg=native_dep.lib" + else: + native_link_arg = "-Clink-arg=-lnative_dep.lib" + else: + native_link_arg = "-Clink-arg=-lnative_dep" assert_argv_contains(env, action, native_link_arg) assert_argv_contains_prefix(env, action, "--codegen=linker=") return analysistest.end(env) @@ -60,12 +77,19 @@ def _proc_macro_has_native_libs_test_impl(ctx): env = analysistest.begin(ctx) tut = analysistest.target_under_test(env) action = tut.actions[0] + toolchain = _get_toolchain(ctx) compilation_mode = ctx.var["COMPILATION_MODE"] pic_suffix = _get_pic_suffix(ctx, compilation_mode) assert_argv_contains_prefix_suffix(env, action, "-Lnative=", "/native_deps") assert_argv_contains(env, action, "--crate-type=proc-macro") assert_argv_contains(env, action, "-lstatic=native_dep{}".format(pic_suffix)) - native_link_arg = "-Clink-arg=-lnative_dep.lib" if ctx.target_platform_has_constraint(ctx.attr._windows_constraint[platform_common.ConstraintValueInfo]) else "-Clink-arg=-lnative_dep{}".format(pic_suffix) + if toolchain.target_os == "windows": + if toolchain.target_triple.abi == "msvc": + native_link_arg = "-Clink-arg=native_dep.lib" + else: + native_link_arg = "-Clink-arg=-lnative_dep.lib" + else: + native_link_arg = "-Clink-arg=-lnative_dep{}".format(pic_suffix) assert_argv_contains(env, action, native_link_arg) assert_argv_contains_prefix(env, action, "--codegen=linker=") return analysistest.end(env) @@ -74,9 +98,16 @@ def _bin_has_native_libs_test_impl(ctx): env = analysistest.begin(ctx) tut = analysistest.target_under_test(env) action = tut.actions[0] + toolchain = _get_toolchain(ctx) assert_argv_contains_prefix_suffix(env, action, "-Lnative=", "/native_deps") assert_argv_contains(env, action, "-lstatic=native_dep") - native_link_arg = "-Clink-arg=-lnative_dep.lib" if ctx.target_platform_has_constraint(ctx.attr._windows_constraint[platform_common.ConstraintValueInfo]) else "-Clink-arg=-lnative_dep" + if toolchain.target_os == "windows": + if toolchain.target_triple.abi == "msvc": + native_link_arg = "-Clink-arg=native_dep.lib" + else: + native_link_arg = "-Clink-arg=-lnative_dep.lib" + else: + native_link_arg = "-Clink-arg=-lnative_dep" assert_argv_contains(env, action, native_link_arg) assert_argv_contains_prefix(env, action, "--codegen=linker=") return analysistest.end(env) @@ -95,6 +126,7 @@ def _bin_has_native_dep_and_alwayslink_test_impl(ctx): tut = analysistest.target_under_test(env) action = tut.actions[0] + toolchain = _get_toolchain(ctx) compilation_mode = ctx.var["COMPILATION_MODE"] workspace_prefix = "" if ctx.workspace_name == "rules_rust" else "external/rules_rust/" individual_link_args = [ @@ -102,17 +134,26 @@ def _bin_has_native_dep_and_alwayslink_test_impl(ctx): for arg in _extract_linker_args(action.argv) if arg.startswith("link-arg=") or arg.startswith("-lstatic=") ] - if ctx.target_platform_has_constraint(ctx.attr._macos_constraint[platform_common.ConstraintValueInfo]): + if toolchain.target_os == "darwin": darwin_component = _get_darwin_component(individual_link_args[-1]) want = [ "-lstatic=native_dep", "link-arg=-Wl,-force_load,bazel-out/{}-{}/bin/{}test/unit/native_deps/libalwayslink.lo".format(darwin_component, compilation_mode, workspace_prefix), ] - elif ctx.target_platform_has_constraint(ctx.attr._windows_constraint[platform_common.ConstraintValueInfo]): - want = [ - "-lstatic=native_dep", - "link-arg=/WHOLEARCHIVE:bazel-out/x64_windows-{}/bin/{}test/unit/native_deps/alwayslink.lo.lib".format(compilation_mode, workspace_prefix), - ] + asserts.equals(env, want, individual_link_args) + elif toolchain.target_os == "windows": + if toolchain.target_triple.abi == "msvc": + want = [ + "-lstatic=native_dep", + "link-arg=/WHOLEARCHIVE:bazel-out/x64_windows-{}/bin/{}test/unit/native_deps/alwayslink.lo.lib".format(compilation_mode, workspace_prefix), + ] + else: + want = [ + "-lstatic=native_dep", + "link-arg=-Wl,--whole-archive", + "link-arg=bazel-out/x64_windows-{}/bin/{}test/unit/native_deps/alwayslink.lo.lib".format(compilation_mode, workspace_prefix), + "link-arg=-Wl,--no-whole-archive", + ] else: want = [ "-lstatic=native_dep", @@ -124,6 +165,8 @@ def _bin_has_native_dep_and_alwayslink_test_impl(ctx): return analysistest.end(env) def _cdylib_has_native_dep_and_alwayslink_test_impl(ctx): + toolchain = _get_toolchain(ctx) + env = analysistest.begin(ctx) tut = analysistest.target_under_test(env) action = tut.actions[0] @@ -131,20 +174,29 @@ def _cdylib_has_native_dep_and_alwayslink_test_impl(ctx): # skipping first link-arg since it contains unrelated linker flags linker_args = _extract_linker_args(action.argv)[1:] + toolchain = _get_toolchain(ctx) compilation_mode = ctx.var["COMPILATION_MODE"] workspace_prefix = "" if ctx.workspace_name == "rules_rust" else "external/rules_rust/" pic_suffix = _get_pic_suffix(ctx, compilation_mode) - if ctx.target_platform_has_constraint(ctx.attr._macos_constraint[platform_common.ConstraintValueInfo]): + if toolchain.target_os == "darwin": darwin_component = _get_darwin_component(linker_args[-1]) want = [ "-lstatic=native_dep{}".format(pic_suffix), "link-arg=-Wl,-force_load,bazel-out/{}-{}/bin/{}test/unit/native_deps/libalwayslink{}.lo".format(darwin_component, compilation_mode, workspace_prefix, pic_suffix), ] - elif ctx.target_platform_has_constraint(ctx.attr._windows_constraint[platform_common.ConstraintValueInfo]): - want = [ - "-lstatic=native_dep", - "link-arg=/WHOLEARCHIVE:bazel-out/x64_windows-{}/bin/{}test/unit/native_deps/alwayslink.lo.lib".format(compilation_mode, workspace_prefix), - ] + elif toolchain.target_os == "windows": + if toolchain.target_triple.abi == "msvc": + want = [ + "-lstatic=native_dep", + "link-arg=/WHOLEARCHIVE:bazel-out/x64_windows-{}/bin/{}test/unit/native_deps/alwayslink.lo.lib".format(compilation_mode, workspace_prefix), + ] + else: + want = [ + "-lstatic=native_dep", + "link-arg=-Wl,--whole-archive", + "link-arg=bazel-out/x64_windows-{}/bin/{}test/unit/native_deps/alwayslink.lo.lib".format(compilation_mode, workspace_prefix), + "link-arg=-Wl,--no-whole-archive", + ] else: want = [ "-lstatic=native_dep{}".format(pic_suffix), @@ -156,35 +208,29 @@ def _cdylib_has_native_dep_and_alwayslink_test_impl(ctx): return analysistest.end(env) def _get_pic_suffix(ctx, compilation_mode): - if (ctx.target_platform_has_constraint(ctx.attr._macos_constraint[platform_common.ConstraintValueInfo]) or - ctx.target_platform_has_constraint(ctx.attr._windows_constraint[platform_common.ConstraintValueInfo])): + toolchain = _get_toolchain(ctx) + if toolchain.target_os == "darwin" or toolchain.target_os == "windows": return "" return ".pic" if compilation_mode == "opt" else "" rlib_has_no_native_libs_test = analysistest.make(_rlib_has_no_native_libs_test_impl) staticlib_has_native_libs_test = analysistest.make(_staticlib_has_native_libs_test_impl, attrs = { - "_macos_constraint": attr.label(default = Label("@platforms//os:macos")), - "_windows_constraint": attr.label(default = Label("@platforms//os:windows")), + "_toolchain": attr.label(default = Label("//rust/toolchain:current_rust_toolchain")), }) cdylib_has_native_libs_test = analysistest.make(_cdylib_has_native_libs_test_impl, attrs = { - "_macos_constraint": attr.label(default = Label("@platforms//os:macos")), - "_windows_constraint": attr.label(default = Label("@platforms//os:windows")), + "_toolchain": attr.label(default = Label("//rust/toolchain:current_rust_toolchain")), }) proc_macro_has_native_libs_test = analysistest.make(_proc_macro_has_native_libs_test_impl, attrs = { - "_macos_constraint": attr.label(default = Label("@platforms//os:macos")), - "_windows_constraint": attr.label(default = Label("@platforms//os:windows")), + "_toolchain": attr.label(default = Label("//rust/toolchain:current_rust_toolchain")), }) bin_has_native_libs_test = analysistest.make(_bin_has_native_libs_test_impl, attrs = { - "_macos_constraint": attr.label(default = Label("@platforms//os:macos")), - "_windows_constraint": attr.label(default = Label("@platforms//os:windows")), + "_toolchain": attr.label(default = Label("//rust/toolchain:current_rust_toolchain")), }) bin_has_native_dep_and_alwayslink_test = analysistest.make(_bin_has_native_dep_and_alwayslink_test_impl, attrs = { - "_macos_constraint": attr.label(default = Label("@platforms//os:macos")), - "_windows_constraint": attr.label(default = Label("@platforms//os:windows")), + "_toolchain": attr.label(default = Label("//rust/toolchain:current_rust_toolchain")), }) cdylib_has_native_dep_and_alwayslink_test = analysistest.make(_cdylib_has_native_dep_and_alwayslink_test_impl, attrs = { - "_macos_constraint": attr.label(default = Label("@platforms//os:macos")), - "_windows_constraint": attr.label(default = Label("@platforms//os:windows")), + "_toolchain": attr.label(default = Label("//rust/toolchain:current_rust_toolchain")), }) def _native_dep_test(): diff --git a/test/unit/proc_macro/leaks_deps/proc_macro_does_not_leak_deps.bzl b/test/unit/proc_macro/leaks_deps/proc_macro_does_not_leak_deps.bzl index db86506305..d302647e30 100644 --- a/test/unit/proc_macro/leaks_deps/proc_macro_does_not_leak_deps.bzl +++ b/test/unit/proc_macro/leaks_deps/proc_macro_does_not_leak_deps.bzl @@ -3,6 +3,9 @@ load("@bazel_skylib//lib:unittest.bzl", "analysistest", "asserts") load("//rust:defs.bzl", "rust_library", "rust_proc_macro", "rust_test") +def _get_toolchain(ctx): + return ctx.attr._toolchain[platform_common.ToolchainInfo] + def _proc_macro_does_not_leak_deps_impl(ctx): env = analysistest.begin(ctx) actions = analysistest.target_under_test(env).actions @@ -11,6 +14,7 @@ def _proc_macro_does_not_leak_deps_impl(ctx): if action.mnemonic == "Rustc": rustc_action = action break + toolchain = _get_toolchain(ctx) asserts.false(env, rustc_action == None) @@ -25,7 +29,13 @@ def _proc_macro_does_not_leak_deps_impl(ctx): # Our test target depends on proc_macro_dep:native directly, as well as transitively through the # proc_macro. The proc_macro should not leak its dependency, so we should only get the "native" # library once on the command line. - native_deps = [arg for arg in rustc_action.argv if arg.startswith("-Clink-arg=-lnative")] + if toolchain.target_os == "windows": + if toolchain.target_triple.abi == "msvc": + native_deps = [arg for arg in rustc_action.argv if arg == "-Clink-arg=native.lib"] + else: + native_deps = [arg for arg in rustc_action.argv if arg == "-Clink-arg=-lnative.lib"] + else: + native_deps = [arg for arg in rustc_action.argv if arg == "-Clink-arg=-lnative"] asserts.equals(env, 1, len(native_deps)) return analysistest.end(env) @@ -66,7 +76,9 @@ def _proc_macro_does_not_leak_deps_test(): target_under_test = ":deps_not_leaked", ) -proc_macro_does_not_leak_deps_test = analysistest.make(_proc_macro_does_not_leak_deps_impl) +proc_macro_does_not_leak_deps_test = analysistest.make(_proc_macro_does_not_leak_deps_impl, attrs = { + "_toolchain": attr.label(default = Label("//rust/toolchain:current_rust_toolchain")), +}) # Tests that a lib_a -> proc_macro -> lib_b does not propagate lib_b to the inputs of lib_a def _proc_macro_does_not_leak_lib_deps_impl(ctx): From 937e63399b111a6d7ee53b187e4d113300b089e9 Mon Sep 17 00:00:00 2001 From: Amari Robinson Date: Fri, 12 May 2023 12:40:53 -0500 Subject: [PATCH 5/7] Add T2 support for x86_64-unknown-none (#1967) * Add T2 support for x86-64-unknown-none * Regenerate documentation * Update crate_universe examples * Update no_std test --------- Co-authored-by: Amari Robinson --- docs/crate_universe.md | 4 ++-- .../cargo_aliases/cargo-bazel-lock.json | 2 +- .../cargo_workspace/cargo-bazel-lock.json | 8 +++++--- .../multi_package/cargo-bazel-lock.json | 20 ++++++++++++------- .../no_cargo_manifests/cargo-bazel-lock.json | 11 ++++++---- rust/platform/triple_mappings.bzl | 1 + test/no_std/cargo-bazel-lock.json | 2 +- 7 files changed, 30 insertions(+), 18 deletions(-) diff --git a/docs/crate_universe.md b/docs/crate_universe.md index ac23ad5026..8afaea4f1d 100644 --- a/docs/crate_universe.md +++ b/docs/crate_universe.md @@ -388,7 +388,7 @@ CARGO_BAZEL_REPIN=1 CARGO_BAZEL_REPIN_ONLY=crate_index bazel sync --only=crate_i | rust_toolchain_rustc_template | The template to use for finding the host rustc binary. {version} (eg. '1.53.0'), {triple} (eg. 'x86_64-unknown-linux-gnu'), {arch} (eg. 'aarch64'), {vendor} (eg. 'unknown'), {system} (eg. 'darwin'), {cfg} (eg. 'exec'), {channel} (eg. 'stable'), and {tool} (eg. 'cargo.exe') will be replaced in the string if present. | String | optional | "@rust_{system}_{arch}__{triple}__{channel}_tools//:bin/{tool}" | | rust_version | The version of Rust the currently registered toolchain is using. Eg. 1.56.0, or nightly/2021-09-08 | String | optional | "1.69.0" | | splicing_config | The configuration flags to use for splicing Cargo maniests. Use //crate_universe:defs.bzl\%rsplicing_config to generate the value for this field. If unset, the defaults defined there will be used. | String | optional | "" | -| supported_platform_triples | A set of all platform triples to consider when generating dependencies. | List of strings | optional | ["aarch64-unknown-linux-gnu", "i686-apple-darwin", "i686-pc-windows-msvc", "i686-unknown-linux-gnu", "x86_64-apple-darwin", "x86_64-pc-windows-msvc", "x86_64-unknown-linux-gnu", "aarch64-apple-darwin", "aarch64-apple-ios-sim", "aarch64-apple-ios", "aarch64-fuchsia", "aarch64-linux-android", "aarch64-pc-windows-msvc", "arm-unknown-linux-gnueabi", "armv7-linux-androideabi", "armv7-unknown-linux-gnueabi", "i686-linux-android", "i686-unknown-freebsd", "powerpc-unknown-linux-gnu", "riscv32imc-unknown-none-elf", "riscv64gc-unknown-none-elf", "s390x-unknown-linux-gnu", "thumbv7em-none-eabi", "wasm32-unknown-unknown", "wasm32-wasi", "x86_64-apple-ios", "x86_64-fuchsia", "x86_64-linux-android", "x86_64-unknown-freebsd"] | +| supported_platform_triples | A set of all platform triples to consider when generating dependencies. | List of strings | optional | ["aarch64-unknown-linux-gnu", "i686-apple-darwin", "i686-pc-windows-msvc", "i686-unknown-linux-gnu", "x86_64-apple-darwin", "x86_64-pc-windows-msvc", "x86_64-unknown-linux-gnu", "aarch64-apple-darwin", "aarch64-apple-ios-sim", "aarch64-apple-ios", "aarch64-fuchsia", "aarch64-linux-android", "aarch64-pc-windows-msvc", "arm-unknown-linux-gnueabi", "armv7-linux-androideabi", "armv7-unknown-linux-gnueabi", "i686-linux-android", "i686-unknown-freebsd", "powerpc-unknown-linux-gnu", "riscv32imc-unknown-none-elf", "riscv64gc-unknown-none-elf", "s390x-unknown-linux-gnu", "thumbv7em-none-eabi", "wasm32-unknown-unknown", "wasm32-wasi", "x86_64-apple-ios", "x86_64-fuchsia", "x86_64-linux-android", "x86_64-unknown-freebsd", "x86_64-unknown-none"] | @@ -493,7 +493,7 @@ call against the generated workspace. The following table describes how to contr | render_config | The configuration flags to use for rendering. Use //crate_universe:defs.bzl\%render_config to generate the value for this field. If unset, the defaults defined there will be used. | String | optional | "" | | repository_name | The name of the repository to generate for remote vendor modes. If unset, the label name will be used | String | optional | "" | | splicing_config | The configuration flags to use for splicing Cargo maniests. Use //crate_universe:defs.bzl\%rsplicing_config to generate the value for this field. If unset, the defaults defined there will be used. | String | optional | "" | -| supported_platform_triples | A set of all platform triples to consider when generating dependencies. | List of strings | optional | ["aarch64-unknown-linux-gnu", "i686-apple-darwin", "i686-pc-windows-msvc", "i686-unknown-linux-gnu", "x86_64-apple-darwin", "x86_64-pc-windows-msvc", "x86_64-unknown-linux-gnu", "aarch64-apple-darwin", "aarch64-apple-ios-sim", "aarch64-apple-ios", "aarch64-fuchsia", "aarch64-linux-android", "aarch64-pc-windows-msvc", "arm-unknown-linux-gnueabi", "armv7-linux-androideabi", "armv7-unknown-linux-gnueabi", "i686-linux-android", "i686-unknown-freebsd", "powerpc-unknown-linux-gnu", "riscv32imc-unknown-none-elf", "riscv64gc-unknown-none-elf", "s390x-unknown-linux-gnu", "thumbv7em-none-eabi", "wasm32-unknown-unknown", "wasm32-wasi", "x86_64-apple-ios", "x86_64-fuchsia", "x86_64-linux-android", "x86_64-unknown-freebsd"] | +| supported_platform_triples | A set of all platform triples to consider when generating dependencies. | List of strings | optional | ["aarch64-unknown-linux-gnu", "i686-apple-darwin", "i686-pc-windows-msvc", "i686-unknown-linux-gnu", "x86_64-apple-darwin", "x86_64-pc-windows-msvc", "x86_64-unknown-linux-gnu", "aarch64-apple-darwin", "aarch64-apple-ios-sim", "aarch64-apple-ios", "aarch64-fuchsia", "aarch64-linux-android", "aarch64-pc-windows-msvc", "arm-unknown-linux-gnueabi", "armv7-linux-androideabi", "armv7-unknown-linux-gnueabi", "i686-linux-android", "i686-unknown-freebsd", "powerpc-unknown-linux-gnu", "riscv32imc-unknown-none-elf", "riscv64gc-unknown-none-elf", "s390x-unknown-linux-gnu", "thumbv7em-none-eabi", "wasm32-unknown-unknown", "wasm32-wasi", "x86_64-apple-ios", "x86_64-fuchsia", "x86_64-linux-android", "x86_64-unknown-freebsd", "x86_64-unknown-none"] | | vendor_path | The path to a directory to write files into. Absolute paths will be treated as relative to the workspace root | String | optional | "crates" | diff --git a/examples/crate_universe/cargo_aliases/cargo-bazel-lock.json b/examples/crate_universe/cargo_aliases/cargo-bazel-lock.json index 6661c07156..3354ec18a1 100644 --- a/examples/crate_universe/cargo_aliases/cargo-bazel-lock.json +++ b/examples/crate_universe/cargo_aliases/cargo-bazel-lock.json @@ -1,5 +1,5 @@ { - "checksum": "66bd7b84cfb273e65ddb047ae27c72d99e795ec3f12369ddeb2e9fc006e53a8a", + "checksum": "73c543aaec1c9028c26c64cd4b975a2ed32b43248a90750ac5fbda2d736e8eda", "crates": { "aho-corasick 0.7.20": { "name": "aho-corasick", diff --git a/examples/crate_universe/cargo_workspace/cargo-bazel-lock.json b/examples/crate_universe/cargo_workspace/cargo-bazel-lock.json index 72d0b3d502..53f92f594a 100644 --- a/examples/crate_universe/cargo_workspace/cargo-bazel-lock.json +++ b/examples/crate_universe/cargo_workspace/cargo-bazel-lock.json @@ -1,5 +1,5 @@ { - "checksum": "d0c772ec9a1b712837102c7b763fcc7fb9114613850a46964f6e7c70ae4aed4e", + "checksum": "8dcac739a6ae8ab1f19b863d7da1d9b4e99078d8616a458f9fa9abb1d7a5c6ba", "crates": { "ansi_term 0.12.1": { "name": "ansi_term", @@ -1304,7 +1304,8 @@ "x86_64-linux-android", "x86_64-pc-windows-msvc", "x86_64-unknown-freebsd", - "x86_64-unknown-linux-gnu" + "x86_64-unknown-linux-gnu", + "x86_64-unknown-none" ], "cfg(not(windows))": [ "aarch64-apple-darwin", @@ -1332,7 +1333,8 @@ "x86_64-fuchsia", "x86_64-linux-android", "x86_64-unknown-freebsd", - "x86_64-unknown-linux-gnu" + "x86_64-unknown-linux-gnu", + "x86_64-unknown-none" ], "cfg(target_os = \"emscripten\")": [], "cfg(target_os = \"hermit\")": [], diff --git a/examples/crate_universe/multi_package/cargo-bazel-lock.json b/examples/crate_universe/multi_package/cargo-bazel-lock.json index 8894e57616..9f8abdcaea 100644 --- a/examples/crate_universe/multi_package/cargo-bazel-lock.json +++ b/examples/crate_universe/multi_package/cargo-bazel-lock.json @@ -1,5 +1,5 @@ { - "checksum": "c2dfd0205262fb9ad37e31083e0eca5ca7363c493e983cb9d9fbce00aae4e546", + "checksum": "28b36befccdbe5075b4ec0c75543600400d65b6707b641e9e8b1eb20e0ad300b", "crates": { "aho-corasick 0.7.20": { "name": "aho-corasick", @@ -9339,7 +9339,8 @@ "x86_64-linux-android", "x86_64-pc-windows-msvc", "x86_64-unknown-freebsd", - "x86_64-unknown-linux-gnu" + "x86_64-unknown-linux-gnu", + "x86_64-unknown-none" ], "cfg(any(target_os = \"android\", target_os = \"linux\"))": [ "aarch64-linux-android", @@ -9409,7 +9410,8 @@ "x86_64-linux-android", "x86_64-pc-windows-msvc", "x86_64-unknown-freebsd", - "x86_64-unknown-linux-gnu" + "x86_64-unknown-linux-gnu", + "x86_64-unknown-none" ], "cfg(not(target_arch = \"wasm32\"))": [ "aarch64-apple-darwin", @@ -9438,7 +9440,8 @@ "x86_64-linux-android", "x86_64-pc-windows-msvc", "x86_64-unknown-freebsd", - "x86_64-unknown-linux-gnu" + "x86_64-unknown-linux-gnu", + "x86_64-unknown-none" ], "cfg(not(target_os = \"unknown\"))": [ "aarch64-apple-darwin", @@ -9468,7 +9471,8 @@ "x86_64-linux-android", "x86_64-pc-windows-msvc", "x86_64-unknown-freebsd", - "x86_64-unknown-linux-gnu" + "x86_64-unknown-linux-gnu", + "x86_64-unknown-none" ], "cfg(not(windows))": [ "aarch64-apple-darwin", @@ -9496,7 +9500,8 @@ "x86_64-fuchsia", "x86_64-linux-android", "x86_64-unknown-freebsd", - "x86_64-unknown-linux-gnu" + "x86_64-unknown-linux-gnu", + "x86_64-unknown-none" ], "cfg(not(windows_raw_dylib))": [ "aarch64-apple-darwin", @@ -9527,7 +9532,8 @@ "x86_64-linux-android", "x86_64-pc-windows-msvc", "x86_64-unknown-freebsd", - "x86_64-unknown-linux-gnu" + "x86_64-unknown-linux-gnu", + "x86_64-unknown-none" ], "cfg(target_arch = \"wasm32\")": [ "wasm32-unknown-unknown", diff --git a/examples/crate_universe/no_cargo_manifests/cargo-bazel-lock.json b/examples/crate_universe/no_cargo_manifests/cargo-bazel-lock.json index 0279d086fc..3bf2de4219 100644 --- a/examples/crate_universe/no_cargo_manifests/cargo-bazel-lock.json +++ b/examples/crate_universe/no_cargo_manifests/cargo-bazel-lock.json @@ -1,5 +1,5 @@ { - "checksum": "e5a6a8bd126d76a92466d0b710a37b36e03ecf9212ec5116858d57b9989cb966", + "checksum": "706a0dca240bd9373f771f4ad67da55cef007a8abb453ae934a33409e20f649c", "crates": { "async-trait 0.1.64": { "name": "async-trait", @@ -4891,7 +4891,8 @@ "x86_64-linux-android", "x86_64-pc-windows-msvc", "x86_64-unknown-freebsd", - "x86_64-unknown-linux-gnu" + "x86_64-unknown-linux-gnu", + "x86_64-unknown-none" ], "cfg(not(windows))": [ "aarch64-apple-darwin", @@ -4919,7 +4920,8 @@ "x86_64-fuchsia", "x86_64-linux-android", "x86_64-unknown-freebsd", - "x86_64-unknown-linux-gnu" + "x86_64-unknown-linux-gnu", + "x86_64-unknown-none" ], "cfg(not(windows_raw_dylib))": [ "aarch64-apple-darwin", @@ -4950,7 +4952,8 @@ "x86_64-linux-android", "x86_64-pc-windows-msvc", "x86_64-unknown-freebsd", - "x86_64-unknown-linux-gnu" + "x86_64-unknown-linux-gnu", + "x86_64-unknown-none" ], "cfg(target_os = \"redox\")": [], "cfg(target_os = \"wasi\")": [ diff --git a/rust/platform/triple_mappings.bzl b/rust/platform/triple_mappings.bzl index 61498d8d13..7f6ef2c7fa 100644 --- a/rust/platform/triple_mappings.bzl +++ b/rust/platform/triple_mappings.bzl @@ -43,6 +43,7 @@ SUPPORTED_T2_PLATFORM_TRIPLES = [ "x86_64-fuchsia", "x86_64-linux-android", "x86_64-unknown-freebsd", + "x86_64-unknown-none", ] SUPPORTED_PLATFORM_TRIPLES = SUPPORTED_T1_PLATFORM_TRIPLES + SUPPORTED_T2_PLATFORM_TRIPLES diff --git a/test/no_std/cargo-bazel-lock.json b/test/no_std/cargo-bazel-lock.json index af8338e6b0..3ebd6bb916 100644 --- a/test/no_std/cargo-bazel-lock.json +++ b/test/no_std/cargo-bazel-lock.json @@ -1,5 +1,5 @@ { - "checksum": "d2f60d30b5b898f2ef5b7c36d267b835cd10fc7e5841255da4c216ccc6de544a", + "checksum": "6e2310f38b1e6c6ae4a9e542eb88259f6214818e4f94196b7c522f17bd4515e1", "crates": { "direct-cargo-bazel-deps 0.0.1": { "name": "direct-cargo-bazel-deps", From f1b19c394f3edffa041b88be1bc371950476d1d2 Mon Sep 17 00:00:00 2001 From: Keith Smiley Date: Wed, 17 May 2023 11:14:46 -0700 Subject: [PATCH 6/7] Update rules_apple in tests (#1972) Fixes https://github.com/bazelbuild/rules_rust/issues/1963 --- examples/ios/WORKSPACE.bazel | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/ios/WORKSPACE.bazel b/examples/ios/WORKSPACE.bazel index fe2a2970ed..053b1a533f 100644 --- a/examples/ios/WORKSPACE.bazel +++ b/examples/ios/WORKSPACE.bazel @@ -24,8 +24,8 @@ load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive") http_archive( name = "build_bazel_rules_apple", - sha256 = "a19cf84dd060eda50be9ba5b0eca88377e0306ffbc1cc059df6a6947e48ac61a", - url = "https://github.com/bazelbuild/rules_apple/releases/download/1.1.1/rules_apple.1.1.1.tar.gz", + sha256 = "a6141240657093fa7ccc7ca1ee5a62408dd9996d1bf47bc2369b8b9faefb2698", + url = "https://github.com/bazelbuild/rules_apple/releases/download/2.3.0/rules_apple.2.3.0.tar.gz", ) load( From 4aaa6de306f83ce366ab989f8413e29436ab0838 Mon Sep 17 00:00:00 2001 From: Daniel Wagner-Hall Date: Fri, 19 May 2023 20:09:52 +0200 Subject: [PATCH 7/7] Allow specifying exec and target compatibility constraints (#1971) Extra target triples may only be appropriate for specific platforms (e.g. if you register a toolchain with the exec_triple `x86_64-unknown-linux-gnu` with an extra target triple of `x86_64-unknown-linux-musl`, you may want differing `target_compatible_with` values for each of those two target triples. This allows for specifying explicit `exec_compatible_with` lists per toolchain (which was already supported by `rust_toolchain_repository` just not proxied by `rust_repository_set`), and explicit `target_compatible_with` lists, both for the default triple and for extra ones. I'm not 100% sure on our pre-existing assumption that every toolchain supports compiling targeting its own exec platform, i.e. the '`[exec_triple]`' part of https://github.com/bazelbuild/rules_rust/blob/937e63399b111a6d7ee53b187e4d113300b089e9/rust/repositories.bzl#L845 but it _does_ generally hold, and supplying a way of removing that default assumption is definitely a separate PR for another day if it becomes relevant to anyone. --- docs/flatten.md | 6 ++++-- docs/rust_repositories.md | 6 ++++-- rust/repositories.bzl | 28 +++++++++++++++++++++++----- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/docs/flatten.md b/docs/flatten.md index 580eb0e214..3cc9a09128 100644 --- a/docs/flatten.md +++ b/docs/flatten.md @@ -1851,7 +1851,7 @@ rust_repositories(kwargs) rust_repository_set(name, exec_triple, target_settings, version, versions, allocator_library, global_allocator_library, extra_target_triples, iso_date, rustfmt_version, edition, dev_components, extra_rustc_flags, extra_exec_rustc_flags, sha256s, urls, - auth, register_toolchain) + auth, register_toolchain, exec_compatible_with, default_target_compatible_with) Assembles a remote repository for the given toolchain params, produces a proxy repository to contain the toolchain declaration, and registers the toolchains. @@ -1868,7 +1868,7 @@ Assembles a remote repository for the given toolchain params, produces a proxy r | versions | A list of toolchain versions to download. This paramter only accepts one versions per channel. E.g. ["1.65.0", "nightly/2022-11-02", "beta/2020-12-30"]. | `[]` | | allocator_library | Target that provides allocator functions when rust_library targets are embedded in a cc_binary. | `None` | | global_allocator_library | Target that provides allocator functions a global allocator is used with cc_common.link. | `None` | -| extra_target_triples | Additional rust-style targets that this set of toolchains should support. | `[]` | +| extra_target_triples | Additional rust-style targets that this set of toolchains should support. If a map, values should be (optional) target_compatible_with lists for that particular target triple. | `{}` | | iso_date | The date of the tool. | `None` | | rustfmt_version | The version of rustfmt to be associated with the toolchain. | `None` | | edition | The rust edition to be used by default (2015, 2018, or 2021). If absent, every rule is required to specify its edition attribute. | `None` | @@ -1879,6 +1879,8 @@ Assembles a remote repository for the given toolchain params, produces a proxy r | urls | A list of mirror urls containing the tools from the Rust-lang static file server. These must contain the '{}' used to substitute the tool being fetched (using .format). | `["https://static.rust-lang.org/dist/{}.tar.gz"]` | | auth | Auth object compatible with repository_ctx.download to use when downloading files. See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details. | `None` | | register_toolchain | If True, the generated rust_toolchain target will become a registered toolchain. | `True` | +| exec_compatible_with | A list of constraints for the execution platform for this toolchain. | `None` | +| default_target_compatible_with | A list of constraints for the target platform for this toolchain when the exec platform is the same as the target platform. | `None` | diff --git a/docs/rust_repositories.md b/docs/rust_repositories.md index f5d836d92c..07766bb966 100644 --- a/docs/rust_repositories.md +++ b/docs/rust_repositories.md @@ -303,7 +303,7 @@ rust_repositories(kwargs) rust_repository_set(name, exec_triple, target_settings, version, versions, allocator_library, global_allocator_library, extra_target_triples, iso_date, rustfmt_version, edition, dev_components, extra_rustc_flags, extra_exec_rustc_flags, sha256s, urls, - auth, register_toolchain) + auth, register_toolchain, exec_compatible_with, default_target_compatible_with) Assembles a remote repository for the given toolchain params, produces a proxy repository to contain the toolchain declaration, and registers the toolchains. @@ -320,7 +320,7 @@ Assembles a remote repository for the given toolchain params, produces a proxy r | versions | A list of toolchain versions to download. This paramter only accepts one versions per channel. E.g. ["1.65.0", "nightly/2022-11-02", "beta/2020-12-30"]. | `[]` | | allocator_library | Target that provides allocator functions when rust_library targets are embedded in a cc_binary. | `None` | | global_allocator_library | Target that provides allocator functions a global allocator is used with cc_common.link. | `None` | -| extra_target_triples | Additional rust-style targets that this set of toolchains should support. | `[]` | +| extra_target_triples | Additional rust-style targets that this set of toolchains should support. If a map, values should be (optional) target_compatible_with lists for that particular target triple. | `{}` | | iso_date | The date of the tool. | `None` | | rustfmt_version | The version of rustfmt to be associated with the toolchain. | `None` | | edition | The rust edition to be used by default (2015, 2018, or 2021). If absent, every rule is required to specify its edition attribute. | `None` | @@ -331,6 +331,8 @@ Assembles a remote repository for the given toolchain params, produces a proxy r | urls | A list of mirror urls containing the tools from the Rust-lang static file server. These must contain the '{}' used to substitute the tool being fetched (using .format). | `["https://static.rust-lang.org/dist/{}.tar.gz"]` | | auth | Auth object compatible with repository_ctx.download to use when downloading files. See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details. | `None` | | register_toolchain | If True, the generated rust_toolchain target will become a registered toolchain. | `True` | +| exec_compatible_with | A list of constraints for the execution platform for this toolchain. | `None` | +| default_target_compatible_with | A list of constraints for the target platform for this toolchain when the exec platform is the same as the target platform. | `None` | diff --git a/rust/repositories.bzl b/rust/repositories.bzl index 2bc65e8f7d..b7c26b766b 100644 --- a/rust/repositories.bzl +++ b/rust/repositories.bzl @@ -881,7 +881,7 @@ def rust_repository_set( versions = [], allocator_library = None, global_allocator_library = None, - extra_target_triples = [], + extra_target_triples = {}, iso_date = None, rustfmt_version = None, edition = None, @@ -891,7 +891,9 @@ def rust_repository_set( sha256s = None, urls = DEFAULT_STATIC_RUST_URL_TEMPLATES, auth = None, - register_toolchain = True): + register_toolchain = True, + exec_compatible_with = None, + default_target_compatible_with = None): """Assembles a remote repository for the given toolchain params, produces a proxy repository \ to contain the toolchain declaration, and registers the toolchains. @@ -905,8 +907,8 @@ def rust_repository_set( allocator_library (str, optional): Target that provides allocator functions when rust_library targets are embedded in a cc_binary. global_allocator_library (str, optional): Target that provides allocator functions a global allocator is used with cc_common.link. - extra_target_triples (list, optional): Additional rust-style targets that this set of - toolchains should support. + extra_target_triples (list or map, optional): Additional rust-style targets that this set of + toolchains should support. If a map, values should be (optional) target_compatible_with lists for that particular target triple. iso_date (str, optional): The date of the tool. rustfmt_version (str, optional): The version of rustfmt to be associated with the toolchain. @@ -923,6 +925,8 @@ def rust_repository_set( auth (dict): Auth object compatible with repository_ctx.download to use when downloading files. See [repository_ctx.download](https://docs.bazel.build/versions/main/skylark/lib/repository_ctx.html#download) for more details. register_toolchain (bool): If True, the generated `rust_toolchain` target will become a registered toolchain. + exec_compatible_with (list, optional): A list of constraints for the execution platform for this toolchain. + default_target_compatible_with (list, optional): A list of constraints for the target platform for this toolchain when the exec platform is the same as the target platform. """ if version and versions: @@ -942,8 +946,20 @@ def rust_repository_set( if version and not versions: versions = [version] + # extra_target_triples may be a dict or list - make a list we can pass to _get_toolchain_repositories + extra_target_triples_list = [] + for extra_target_triple in extra_target_triples: + extra_target_triples_list.append(extra_target_triple) + all_toolchain_names = [] - for toolchain in _get_toolchain_repositories(name, exec_triple, extra_target_triples, versions, iso_date): + for toolchain in _get_toolchain_repositories(name, exec_triple, extra_target_triples_list, versions, iso_date): + target_compatible_with = None + if toolchain.target_triple == exec_triple: + # The exec triple implicitly gets a toolchain with itself as a target - use default_target_compatible_with for it + target_compatible_with = default_target_compatible_with + elif type(extra_target_triples) == "dict": + target_compatible_with = extra_target_triples.get(toolchain.target_triple) + all_toolchain_names.append(rust_toolchain_repository( name = toolchain.name, allocator_library = allocator_library, @@ -962,6 +978,8 @@ def rust_repository_set( target_triple = toolchain.target_triple, urls = urls, version = toolchain.channel.version, + exec_compatible_with = exec_compatible_with, + target_compatible_with = target_compatible_with, )) # This repository exists to allow `rust_repository_set` to work with the `maybe` wrapper.