Skip to content

Commit

Permalink
Removed rust_toolchain.os in favor of rust_toolchain.exec_triple. (
Browse files Browse the repository at this point in the history
…bazelbuild#1960)

* Removed `rust_toolchain.os` in favor of `rust_toolchain.exec_triple`.

* Regenerate documentation
  • Loading branch information
UebelAndre authored and Silcet committed Jul 10, 2023
1 parent a02b0e0 commit df9cc20
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 66 deletions.
2 changes: 1 addition & 1 deletion crate_universe/private/crates_vendor.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 11 additions & 9 deletions docs/flatten.md
Original file line number Diff line number Diff line change
Expand Up @@ -1142,8 +1142,8 @@ Run the test with `bazel test //hello_lib:greeting_test`.
rust_toolchain(<a href="#rust_toolchain-name">name</a>, <a href="#rust_toolchain-allocator_library">allocator_library</a>, <a href="#rust_toolchain-binary_ext">binary_ext</a>, <a href="#rust_toolchain-cargo">cargo</a>, <a href="#rust_toolchain-clippy_driver">clippy_driver</a>, <a href="#rust_toolchain-debug_info">debug_info</a>,
<a href="#rust_toolchain-default_edition">default_edition</a>, <a href="#rust_toolchain-dylib_ext">dylib_ext</a>, <a href="#rust_toolchain-env">env</a>, <a href="#rust_toolchain-exec_triple">exec_triple</a>, <a href="#rust_toolchain-experimental_use_cc_common_link">experimental_use_cc_common_link</a>,
<a href="#rust_toolchain-extra_exec_rustc_flags">extra_exec_rustc_flags</a>, <a href="#rust_toolchain-extra_rustc_flags">extra_rustc_flags</a>, <a href="#rust_toolchain-global_allocator_library">global_allocator_library</a>, <a href="#rust_toolchain-llvm_cov">llvm_cov</a>,
<a href="#rust_toolchain-llvm_profdata">llvm_profdata</a>, <a href="#rust_toolchain-llvm_tools">llvm_tools</a>, <a href="#rust_toolchain-opt_level">opt_level</a>, <a href="#rust_toolchain-os">os</a>, <a href="#rust_toolchain-per_crate_rustc_flags">per_crate_rustc_flags</a>, <a href="#rust_toolchain-rust_doc">rust_doc</a>, <a href="#rust_toolchain-rust_std">rust_std</a>,
<a href="#rust_toolchain-rustc">rustc</a>, <a href="#rust_toolchain-rustc_lib">rustc_lib</a>, <a href="#rust_toolchain-rustfmt">rustfmt</a>, <a href="#rust_toolchain-staticlib_ext">staticlib_ext</a>, <a href="#rust_toolchain-stdlib_linkflags">stdlib_linkflags</a>, <a href="#rust_toolchain-target_json">target_json</a>, <a href="#rust_toolchain-target_triple">target_triple</a>)
<a href="#rust_toolchain-llvm_profdata">llvm_profdata</a>, <a href="#rust_toolchain-llvm_tools">llvm_tools</a>, <a href="#rust_toolchain-opt_level">opt_level</a>, <a href="#rust_toolchain-per_crate_rustc_flags">per_crate_rustc_flags</a>, <a href="#rust_toolchain-rust_doc">rust_doc</a>, <a href="#rust_toolchain-rust_std">rust_std</a>, <a href="#rust_toolchain-rustc">rustc</a>,
<a href="#rust_toolchain-rustc_lib">rustc_lib</a>, <a href="#rust_toolchain-rustfmt">rustfmt</a>, <a href="#rust_toolchain-staticlib_ext">staticlib_ext</a>, <a href="#rust_toolchain-stdlib_linkflags">stdlib_linkflags</a>, <a href="#rust_toolchain-target_json">target_json</a>, <a href="#rust_toolchain-target_triple">target_triple</a>)
</pre>

Declares a Rust toolchain for use.
Expand All @@ -1159,32 +1159,35 @@ 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",
)
```

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**
Expand All @@ -1210,7 +1213,6 @@ See @rules_rust//rust:repositories.bzl for examples of defining the @rust_cpuX r
| <a id="rust_toolchain-llvm_profdata"></a>llvm_profdata | The location of the <code>llvm-profdata</code> binary. Can be a direct source or a filegroup containing one item. If <code>llvm_cov</code> is None, this can be None as well and rust code is not instrumented for coverage. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>None</code> |
| <a id="rust_toolchain-llvm_tools"></a>llvm_tools | LLVM tools that are shipped with the Rust toolchain. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>None</code> |
| <a id="rust_toolchain-opt_level"></a>opt_level | Rustc optimization levels. | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional | <code>{"dbg": "0", "fastbuild": "0", "opt": "3"}</code> |
| <a id="rust_toolchain-os"></a>os | The operating system for the current toolchain | String | optional | <code>""</code> |
| <a id="rust_toolchain-per_crate_rustc_flags"></a>per_crate_rustc_flags | Extra flags to pass to rustc in non-exec configuration | List of strings | optional | <code>[]</code> |
| <a id="rust_toolchain-rust_doc"></a>rust_doc | The location of the <code>rustdoc</code> binary. Can be a direct source or a filegroup containing one item. | <a href="https://bazel.build/concepts/labels">Label</a> | required | |
| <a id="rust_toolchain-rust_std"></a>rust_std | The Rust standard library. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>None</code> |
Expand Down
20 changes: 11 additions & 9 deletions docs/rust_repositories.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ A dedicated filegroup-like rule for Rust stdlib artifacts.
rust_toolchain(<a href="#rust_toolchain-name">name</a>, <a href="#rust_toolchain-allocator_library">allocator_library</a>, <a href="#rust_toolchain-binary_ext">binary_ext</a>, <a href="#rust_toolchain-cargo">cargo</a>, <a href="#rust_toolchain-clippy_driver">clippy_driver</a>, <a href="#rust_toolchain-debug_info">debug_info</a>,
<a href="#rust_toolchain-default_edition">default_edition</a>, <a href="#rust_toolchain-dylib_ext">dylib_ext</a>, <a href="#rust_toolchain-env">env</a>, <a href="#rust_toolchain-exec_triple">exec_triple</a>, <a href="#rust_toolchain-experimental_use_cc_common_link">experimental_use_cc_common_link</a>,
<a href="#rust_toolchain-extra_exec_rustc_flags">extra_exec_rustc_flags</a>, <a href="#rust_toolchain-extra_rustc_flags">extra_rustc_flags</a>, <a href="#rust_toolchain-global_allocator_library">global_allocator_library</a>, <a href="#rust_toolchain-llvm_cov">llvm_cov</a>,
<a href="#rust_toolchain-llvm_profdata">llvm_profdata</a>, <a href="#rust_toolchain-llvm_tools">llvm_tools</a>, <a href="#rust_toolchain-opt_level">opt_level</a>, <a href="#rust_toolchain-os">os</a>, <a href="#rust_toolchain-per_crate_rustc_flags">per_crate_rustc_flags</a>, <a href="#rust_toolchain-rust_doc">rust_doc</a>, <a href="#rust_toolchain-rust_std">rust_std</a>,
<a href="#rust_toolchain-rustc">rustc</a>, <a href="#rust_toolchain-rustc_lib">rustc_lib</a>, <a href="#rust_toolchain-rustfmt">rustfmt</a>, <a href="#rust_toolchain-staticlib_ext">staticlib_ext</a>, <a href="#rust_toolchain-stdlib_linkflags">stdlib_linkflags</a>, <a href="#rust_toolchain-target_json">target_json</a>, <a href="#rust_toolchain-target_triple">target_triple</a>)
<a href="#rust_toolchain-llvm_profdata">llvm_profdata</a>, <a href="#rust_toolchain-llvm_tools">llvm_tools</a>, <a href="#rust_toolchain-opt_level">opt_level</a>, <a href="#rust_toolchain-per_crate_rustc_flags">per_crate_rustc_flags</a>, <a href="#rust_toolchain-rust_doc">rust_doc</a>, <a href="#rust_toolchain-rust_std">rust_std</a>, <a href="#rust_toolchain-rustc">rustc</a>,
<a href="#rust_toolchain-rustc_lib">rustc_lib</a>, <a href="#rust_toolchain-rustfmt">rustfmt</a>, <a href="#rust_toolchain-staticlib_ext">staticlib_ext</a>, <a href="#rust_toolchain-stdlib_linkflags">stdlib_linkflags</a>, <a href="#rust_toolchain-target_json">target_json</a>, <a href="#rust_toolchain-target_triple">target_triple</a>)
</pre>

Declares a Rust toolchain for use.
Expand All @@ -56,32 +56,35 @@ 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",
)
```

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**
Expand All @@ -107,7 +110,6 @@ See @rules_rust//rust:repositories.bzl for examples of defining the @rust_cpuX r
| <a id="rust_toolchain-llvm_profdata"></a>llvm_profdata | The location of the <code>llvm-profdata</code> binary. Can be a direct source or a filegroup containing one item. If <code>llvm_cov</code> is None, this can be None as well and rust code is not instrumented for coverage. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>None</code> |
| <a id="rust_toolchain-llvm_tools"></a>llvm_tools | LLVM tools that are shipped with the Rust toolchain. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>None</code> |
| <a id="rust_toolchain-opt_level"></a>opt_level | Rustc optimization levels. | <a href="https://bazel.build/rules/lib/dict">Dictionary: String -> String</a> | optional | <code>{"dbg": "0", "fastbuild": "0", "opt": "3"}</code> |
| <a id="rust_toolchain-os"></a>os | The operating system for the current toolchain | String | optional | <code>""</code> |
| <a id="rust_toolchain-per_crate_rustc_flags"></a>per_crate_rustc_flags | Extra flags to pass to rustc in non-exec configuration | List of strings | optional | <code>[]</code> |
| <a id="rust_toolchain-rust_doc"></a>rust_doc | The location of the <code>rustdoc</code> binary. Can be a direct source or a filegroup containing one item. | <a href="https://bazel.build/concepts/labels">Label</a> | required | |
| <a id="rust_toolchain-rust_std"></a>rust_std | The Rust standard library. | <a href="https://bazel.build/concepts/labels">Label</a> | optional | <code>None</code> |
Expand Down
2 changes: 0 additions & 2 deletions rust/private/repository_utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion rust/private/rust.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,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 = ""
Expand Down
38 changes: 17 additions & 21 deletions rust/private/rustc.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,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": "",
Expand Down Expand Up @@ -460,9 +456,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"):
Expand Down Expand Up @@ -529,7 +525,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
Expand All @@ -539,10 +535,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
Expand Down Expand Up @@ -876,7 +872,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")

Expand Down Expand Up @@ -1200,7 +1196,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)
Expand All @@ -1216,10 +1212,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)

Expand Down Expand Up @@ -1318,7 +1314,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:]

Expand Down Expand Up @@ -1608,7 +1604,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 = [
Expand All @@ -1624,9 +1620,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,
))

Expand Down Expand Up @@ -1846,10 +1842,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:
Expand Down
2 changes: 1 addition & 1 deletion rust/private/rustdoc_test.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Loading

0 comments on commit df9cc20

Please sign in to comment.