From d2e0420219393c8020bf2b716396b4141551f223 Mon Sep 17 00:00:00 2001 From: Vinh Tran Date: Fri, 3 Nov 2023 14:17:28 +0000 Subject: [PATCH] Bootrapping rust toolchain to rebuild std for custom platforms --- .gitignore | 1 + examples/toolchain-to-rebuild-std/.bazelrc | 10 + examples/toolchain-to-rebuild-std/BUILD.bazel | 15 + examples/toolchain-to-rebuild-std/README | 34 ++ .../toolchain-to-rebuild-std/WORKSPACE.bazel | 69 +++ .../dummy_cc_toolchain/BUILD.bazel | 41 ++ .../dummy_cc_toolchain/dummy_cc_toolchain.bzl | 30 ++ .../hello_lib/BUILD.bazel | 17 + .../hello_lib/src/greeter.rs | 75 +++ .../hello_lib/src/lib.rs | 15 + .../hello_lib/tests/greeting.rs | 27 ++ .../stdlibs.BUILD.bazel | 449 ++++++++++++++++++ examples/toolchain-to-rebuild-std/stdlibs.bzl | 19 + .../toolchain/BUILD.bazel | 82 ++++ .../toolchain/defs.bzl | 72 +++ .../tools.BUILD.bazel | 6 + rust/private/rustc.bzl | 2 + 17 files changed, 964 insertions(+) create mode 100644 examples/toolchain-to-rebuild-std/.bazelrc create mode 100644 examples/toolchain-to-rebuild-std/BUILD.bazel create mode 100644 examples/toolchain-to-rebuild-std/README create mode 100644 examples/toolchain-to-rebuild-std/WORKSPACE.bazel create mode 100644 examples/toolchain-to-rebuild-std/dummy_cc_toolchain/BUILD.bazel create mode 100644 examples/toolchain-to-rebuild-std/dummy_cc_toolchain/dummy_cc_toolchain.bzl create mode 100644 examples/toolchain-to-rebuild-std/hello_lib/BUILD.bazel create mode 100644 examples/toolchain-to-rebuild-std/hello_lib/src/greeter.rs create mode 100644 examples/toolchain-to-rebuild-std/hello_lib/src/lib.rs create mode 100644 examples/toolchain-to-rebuild-std/hello_lib/tests/greeting.rs create mode 100644 examples/toolchain-to-rebuild-std/stdlibs.BUILD.bazel create mode 100644 examples/toolchain-to-rebuild-std/stdlibs.bzl create mode 100644 examples/toolchain-to-rebuild-std/toolchain/BUILD.bazel create mode 100644 examples/toolchain-to-rebuild-std/toolchain/defs.bzl create mode 100644 examples/toolchain-to-rebuild-std/tools.BUILD.bazel diff --git a/.gitignore b/.gitignore index a4ccc50b64..d61263edbc 100644 --- a/.gitignore +++ b/.gitignore @@ -9,6 +9,7 @@ /examples/cargo_manifest_dir/external_crate/bazel-* /examples/crate_universe/bazel-* /examples/crate_universe/*/bazel-* +/examples/toolchain-to-rebuild-std/bazel-* /test/cc_common_link/bazel-* /test/cc_common_link/with_global_alloc/bazel-* /test/no_std/bazel-* diff --git a/examples/toolchain-to-rebuild-std/.bazelrc b/examples/toolchain-to-rebuild-std/.bazelrc new file mode 100644 index 0000000000..f8fc03a8f5 --- /dev/null +++ b/examples/toolchain-to-rebuild-std/.bazelrc @@ -0,0 +1,10 @@ +# new feature +build --@rules_rust//rust/settings:experimental_toolchain_generated_sysroot=True + +# enable toolchain debugging by default +build --toolchain_resolution_debug='@rules_rust//rust:toolchain_type' + +# for toolchain debugging purposes +build:beta --@rules_rust//rust/toolchain/channel:channel=beta +build:stable --@rules_rust//rust/toolchain/channel:channel=stable +build:nightly --@rules_rust//rust/toolchain/channel:channel=nightly diff --git a/examples/toolchain-to-rebuild-std/BUILD.bazel b/examples/toolchain-to-rebuild-std/BUILD.bazel new file mode 100644 index 0000000000..cb467d10dc --- /dev/null +++ b/examples/toolchain-to-rebuild-std/BUILD.bazel @@ -0,0 +1,15 @@ +platform( + name = "aarch64-apple-darwin", + constraint_values = [ + "@platforms//os:osx", + "@platforms//cpu:aarch64", + ], +) + +platform( + name = "wasm32-wasi", + constraint_values = [ + "@platforms//os:wasi", + "@platforms//cpu:wasm32", + ], +) diff --git a/examples/toolchain-to-rebuild-std/README b/examples/toolchain-to-rebuild-std/README new file mode 100644 index 0000000000..bd843eb953 --- /dev/null +++ b/examples/toolchain-to-rebuild-std/README @@ -0,0 +1,34 @@ + +This directory demonstrates an example of how to rebuild the standard library +for an arbitrary platform and custom configurations. + +TODO: Write up on how the toolchain bootrapping process works. + +To build an end-user rust_library target for specific target, run + +``` +bazel build //hello_lib --platforms=//:aarch64-apple-darwin +``` + +for `aarch64-apple-darwin` or + +``` +bazel build //hello_lib --platforms=//:wasm32-wasi +``` + +for `wasm32-wasi`. + +The toolchains orderly registered in WORKSPACE and _beta_channel_transition ensure +that the standard library is rebuilt for the given platform. + +To only rebuild std, run + +``` +bazel build @stdlbs//:std --platforms=//:aarch64-apple-darwin +``` + +or + +``` +bazel build @stdlbs//:std --platforms=//:wasm32-wasi +``` \ No newline at end of file diff --git a/examples/toolchain-to-rebuild-std/WORKSPACE.bazel b/examples/toolchain-to-rebuild-std/WORKSPACE.bazel new file mode 100644 index 0000000000..9add976945 --- /dev/null +++ b/examples/toolchain-to-rebuild-std/WORKSPACE.bazel @@ -0,0 +1,69 @@ +workspace(name = "rebuild_std_example") + +# Users of `rules_rust` will commonly be unable to load it +# using a `local_repository`. Instead, to setup the rules, +# please see https://bazelbuild.github.io/rules_rust/#setup +local_repository( + name = "rules_rust", + path = "../..", +) + +load("@rules_rust//rust:repositories.bzl", "rules_rust_dependencies", "rust_register_toolchains") + +rules_rust_dependencies() + +register_toolchains( + "//dummy_cc_toolchain:dummy_cc_aarch64_toolchain", + "//dummy_cc_toolchain:dummy_cc_wasm32_toolchain", +) + +# # The dates need to be sequential so that the stdlibs are built +# # using the previous beta rustc +[ + previous_beta_date, + current_beta_date, +] = [ + "2023-08-22", + "2023-09-18", +] + +rust_register_toolchains( + edition = "2021", + extra_rustc_flags = { + "aarch64-apple-darwin": ["--cap-lints=allow"], + }, + extra_target_triples = [ + "wasm32-wasi", + "aarch64-apple-darwin", + ], + versions = [ + # beta toolchain to rebuild stdlibs + "beta/" + previous_beta_date, + # stable toolchain to build process_wrapper and tinyjson + "1.73.0", + ], +) + +# http_archive( +# name = "wasi", +# build_file = "@rules_rust//util/import/3rdparty/crates:BUILD.wasi-0.11.0+wasi-snapshot-preview1.bazel", +# strip_prefix = "wasi-0.11.0", +# # sha256 = "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", +# url = "https://github.com/bytecodealliance/wasi/archive/refs/tags/0.11.0.zip", +# ) + +load(":stdlibs.bzl", "stdlibs") + +# # Run +# # bazel build @stage2_stdlibs//:std --@rules_rust//rust/toolchain/channel:channel=beta --platforms=//:aarch64-apple-darwin +# # To rebuild standard library for aarch64-apple-darwin +stdlibs( + name = "stdlibs", + beta_date = current_beta_date, + build_file = "//:stdlibs.BUILD.bazel", +) + +register_toolchains( + "//toolchain:toolchain_aarch64-apple-darwin", + "//toolchain:toolchain_wasm32-wasi", +) diff --git a/examples/toolchain-to-rebuild-std/dummy_cc_toolchain/BUILD.bazel b/examples/toolchain-to-rebuild-std/dummy_cc_toolchain/BUILD.bazel new file mode 100644 index 0000000000..91379fdd24 --- /dev/null +++ b/examples/toolchain-to-rebuild-std/dummy_cc_toolchain/BUILD.bazel @@ -0,0 +1,41 @@ +load("@rules_cc//cc:defs.bzl", "cc_toolchain") +load(":dummy_cc_toolchain.bzl", "dummy_cc_config", "dummy_cc_toolchain") + +dummy_cc_toolchain(name = "dummy_cc_aarch64") + +toolchain( + name = "dummy_cc_aarch64_toolchain", + target_compatible_with = ["@platforms//cpu:aarch64"], + toolchain = ":dummy_cc_aarch64_toolchain_cc", + toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", +) + +toolchain( + name = "dummy_cc_wasm32_toolchain", + target_compatible_with = ["@platforms//cpu:wasm32"], + toolchain = ":dummy_cc_aarch64_toolchain_cc", + toolchain_type = "@bazel_tools//tools/cpp:toolchain_type", +) + +cc_toolchain( + name = "dummy_cc_aarch64_toolchain_cc", + all_files = ":empty", + compiler_files = ":empty", + dwp_files = ":empty", + linker_files = ":empty", + objcopy_files = ":empty", + strip_files = ":empty", + supports_param_files = 0, + toolchain_config = ":cc_toolchain_config", + toolchain_identifier = ":dummy_cc_aarch64", +) + +dummy_cc_config( + name = "cc_toolchain_config", +) + +filegroup( + name = "empty", + srcs = [], + visibility = ["//:__subpackages__"], +) diff --git a/examples/toolchain-to-rebuild-std/dummy_cc_toolchain/dummy_cc_toolchain.bzl b/examples/toolchain-to-rebuild-std/dummy_cc_toolchain/dummy_cc_toolchain.bzl new file mode 100644 index 0000000000..a89ca23a43 --- /dev/null +++ b/examples/toolchain-to-rebuild-std/dummy_cc_toolchain/dummy_cc_toolchain.bzl @@ -0,0 +1,30 @@ +"""Cc toolchain definitions for use on wasm platforms""" + +def _dummy_cc_toolchain_impl(_ctx): + # The `all_files` attribute is referenced by rustc_compile_action(). + return [platform_common.ToolchainInfo(all_files = depset([]))] + +dummy_cc_toolchain = rule( + implementation = _dummy_cc_toolchain_impl, + attrs = {}, +) + +# dummy values from https://bazel.build/tutorials/ccp-toolchain-config#configuring_the_c_toolchain +def _config_impl(ctx): + return cc_common.create_cc_toolchain_config_info( + ctx = ctx, + toolchain_identifier = "dummy-aarch64-cc-toolchain", + host_system_name = "unknown", + target_system_name = "unknown", + target_cpu = "unknown", + target_libc = "unknown", + compiler = "unknown", + abi_version = "unknown", + abi_libc_version = "unknown", + ) + +dummy_cc_config = rule( + implementation = _config_impl, + attrs = {}, + provides = [CcToolchainConfigInfo], +) diff --git a/examples/toolchain-to-rebuild-std/hello_lib/BUILD.bazel b/examples/toolchain-to-rebuild-std/hello_lib/BUILD.bazel new file mode 100644 index 0000000000..2624299c46 --- /dev/null +++ b/examples/toolchain-to-rebuild-std/hello_lib/BUILD.bazel @@ -0,0 +1,17 @@ +load( + "@rules_rust//rust:defs.bzl", + "rust_library", +) + +package(default_visibility = ["//visibility:public"]) + +# bazel build //hello_lib:hello_lib --platforms=//:aarch64-apple-darwin +rust_library( + name = "hello_lib", + srcs = [ + "src/greeter.rs", + "src/lib.rs", + ], + crate_features = ["default"], + rustc_flags = ["--cap-lints=allow"], +) diff --git a/examples/toolchain-to-rebuild-std/hello_lib/src/greeter.rs b/examples/toolchain-to-rebuild-std/hello_lib/src/greeter.rs new file mode 100644 index 0000000000..4a9041546e --- /dev/null +++ b/examples/toolchain-to-rebuild-std/hello_lib/src/greeter.rs @@ -0,0 +1,75 @@ +// Copyright 2015 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +/// Object that displays a greeting. +pub struct Greeter { + greeting: String, +} + +/// Implementation of Greeter. +impl Greeter { + /// Constructs a new `Greeter`. + /// + /// # Examples + /// + /// ``` + /// use hello_lib::greeter::Greeter; + /// + /// let greeter = Greeter::new("Hello"); + /// ``` + pub fn new(greeting: &str) -> Greeter { + Greeter { + greeting: greeting.to_string(), + } + } + + /// Returns the greeting as a string. + /// + /// # Examples + /// + /// ``` + /// use hello_lib::greeter::Greeter; + /// + /// let greeter = Greeter::new("Hello"); + /// let greeting = greeter.greeting("World"); + /// ``` + pub fn greeting(&self, thing: &str) -> String { + format!("{} {}", &self.greeting, thing) + } + + /// Prints the greeting. + /// + /// # Examples + /// + /// ``` + /// use hello_lib::greeter::Greeter; + /// + /// let greeter = Greeter::new("Hello"); + /// greeter.greet("World"); + /// ``` + pub fn greet(&self, thing: &str) { + println!("{} {}", &self.greeting, thing); + } +} + +#[cfg(test)] +mod test { + use super::Greeter; + + #[test] + fn test_greeting() { + let hello = Greeter::new("Hi"); + assert_eq!("Hi Rust", hello.greeting("Rust")); + } +} diff --git a/examples/toolchain-to-rebuild-std/hello_lib/src/lib.rs b/examples/toolchain-to-rebuild-std/hello_lib/src/lib.rs new file mode 100644 index 0000000000..9dc608911f --- /dev/null +++ b/examples/toolchain-to-rebuild-std/hello_lib/src/lib.rs @@ -0,0 +1,15 @@ +// Copyright 2015 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +pub mod greeter; diff --git a/examples/toolchain-to-rebuild-std/hello_lib/tests/greeting.rs b/examples/toolchain-to-rebuild-std/hello_lib/tests/greeting.rs new file mode 100644 index 0000000000..6084d13650 --- /dev/null +++ b/examples/toolchain-to-rebuild-std/hello_lib/tests/greeting.rs @@ -0,0 +1,27 @@ +// Copyright 2015 The Bazel Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +extern crate hello_lib; + +// test gate is not usually required in an integration test, but the BUILD +// script is using this file to test cdylib compilation as well, and when +// compiled in that mode, greeter is an unused import. +#[cfg(test)] +use hello_lib::greeter; + +#[test] +fn test_greeting() { + let hello = greeter::Greeter::new("Hello"); + assert_eq!("Hello world", hello.greeting("world")); +} diff --git a/examples/toolchain-to-rebuild-std/stdlibs.BUILD.bazel b/examples/toolchain-to-rebuild-std/stdlibs.BUILD.bazel new file mode 100644 index 0000000000..ddcef03894 --- /dev/null +++ b/examples/toolchain-to-rebuild-std/stdlibs.BUILD.bazel @@ -0,0 +1,449 @@ +load("@rules_rust//rust:defs.bzl", "rust_library") + +package(default_visibility = ["//visibility:public"]) + +exports_files(["bin/*"]) + +# Flags for vendored dependencies. +COMMON_INTERNAL_CRATE_RUSTC_FLAGS = [ + "--cap-lints=allow", + # Mix in some metadata to distinguish the rlibs of these private vendored + # crates from the rlibs of their public counterparts over at third_party/rust. + "-Cmetadata=rustc_internal", + # Ensure standard rust clients can't accidentally pull in the private crates. + # See https://rustc-dev-guide.rust-lang.org/building/bootstrapping.html#-z-force-unstable-if-unmarked. + "-Zforce-unstable-if-unmarked", +] + +filegroup( + name = "binaries", + srcs = glob([ + "bin/*", + "lib/*", + "lib64/*", + ]), +) + +platform( + name = "aarch64-apple-darwin", + constraint_values = [ + "@platforms//os:osx", + "@platforms//cpu:aarch64", + ], +) + +platform( + name = "wasm32-wasi", + constraint_values = [ + "@platforms//os:wasi", + "@platforms//cpu:wasm32", + ], +) + +filegroup( + name = "stdlib_sources", + srcs = [ + ":alloc", + ":cfg_if", + ":compiler_builtins", + ":core", + ":hashbrown", + ":c", + ":panic_abort", + ":profiler_builtins", + ":rustc_demangle", + ":std", + ":std_detect", + ":unwind", + ] + select({ + "@rules_rust//rust/platform:wasm32-wasi": [":wasi"], + "@rules_rust//rust/platform:aarch64-apple-darwin": [], + "//conditions:default": ["@platforms//:incompatible"], + }), +) + +rust_library( + name = "std", + srcs = glob([ + "library/std/src/**/*.rs", + "library/backtrace/src/**/*.rs", + "library/portable-simd/crates/std_float/src/**/*.rs", + ]), + compile_data = glob([ + "library/std/src/**/*.md", + "library/std/primitive_docs/*.md", + "library/core/src/**/*.md", + "library/backtrace/src/**/*.md", + "library/portable-simd/crates/std_float/src/**/*.md", + "library/stdarch/crates/core_arch/src/**/*.md", + "library/stdarch/crates/core_simd/src/**/*.md", + "library/portable-simd/crates/core_simd/src/**/*.md", + "library/portable-simd/crates/core_simd/src/**/*.md", + ]), + crate_features = [ + "default", + "std_detect_dlsym_getauxval", + "std_detect_file_io", + "profiler", + "no_std", + "const-extern-fn", + ], + crate_name = "std", + rustc_env = { + "STD_ENV_ARCH": "wasm32", + "RUSTC_BOOTSTRAP": "1", + }, + rustc_flags = [ + "--cfg=backtrace_in_libstd", + "-Zforce-unstable-if-unmarked", + "-Cmetadata=rustc_internal", + ], + visibility = ["//visibility:public"], + deps = [ + ":alloc", + ":cfg_if", + ":core", + ":compiler_builtins", + ":hashbrown", + ":c", + ":profiler_builtins", + ":rustc_demangle", + ":std_detect", + ":unwind", + ":panic_abort", + ], +) + +rust_library( + name = "hashbrown", + srcs = glob([ + "vendor/hashbrown/src/**/*.rs", + ]), + compile_data = glob(["vendor/hashbrown/src/**/*.md"]), + crate_features = [ + "alloc", + "compiler_builtins", + "core", + "nightly", + "rustc-internal-api", + "no_std", + ], + crate_name = "hashbrown", + rustc_flags = [ + "--cfg=has_extern_crate_alloc", + ], + deps = [ + ":alloc", + ":compiler_builtins", + ":unwind", + ":core", + ], + rustc_env = { + "RUSTC_BOOTSTRAP": "1", + }, +) + +rust_library( + name = "alloc", + srcs = glob(["library/alloc/src/**/*.rs"]), + compile_data = glob(["library/alloc/src/**/*.md"]), + crate_features = ["no_std"], + crate_name = "alloc", + edition = "2021", + deps = [ + ":compiler_builtins", + ":core", + ], + rustc_env = { + "RUSTC_BOOTSTRAP": "1", + }, +) + +rust_library( + name = "compiler_builtins", + srcs = glob([ + "vendor/compiler_builtins/src/**/*.rs", + "vendor/compiler_builtins/libm/**/*.rs", + ]), + compile_data = glob(["vendor/compiler_builtins/src/**/*.md"]), + crate_features = [ + "mem-unaligned", + "compiler-builtins", + # "core", + # "default", + "unstable", + "no_std", + "no-asm", + ], + crate_name = "compiler_builtins", + edition = "2015", + deps = [":core"], + rustc_env = { + "RUSTC_BOOTSTRAP": "1", + }, +) + +rust_library( + name = "default_core", + srcs = glob([ + "library/core/src/**/*.rs", + "library/stdarch/crates/core_arch/src/**/*.rs", + "library/portable-simd/crates/core_simd/src/**/*.rs", + ]), + compile_data = glob([ + "library/core/src/**/*.md", + "library/core/primitive_docs/*.md", + "library/stdarch/crates/core_arch/src/**/*.md", + "library/portable-simd/crates/core_simd/src/**/*.md", + ]), + crate_features = [ + # "stdsimd", + # "no_std", + ], + crate_name = "core", + edition = "2021", + rustc_flags = [ + "--cfg=bootstrap", + "--cap-lints=allow", + "-Zmacro-backtrace", + ], + rustc_env = { + "RUSTC_BOOTSTRAP": "1", + }, +) + +rust_library( + name = "std_detect", + srcs = glob(["library/stdarch/crates/std_detect/src/**/*.rs"]), + compile_data = glob(["library/stdarch/crates/std_detect/src/**/*.md"]), + crate_features = [ + "std_detect_file_io", + "std_detect_dlsym_getauxval", + "no_std", + ], + crate_name = "std_detect", + rustc_flags = [ + "-Cmetadata=rustc_internal_rlibs", + ], + deps = [ + ":alloc", + ":cfg_if", + ":core", + ":compiler_builtins", + ], + rustc_env = { + "RUSTC_BOOTSTRAP": "1", + }, +) + +rust_library( + name = "cfg_if", + srcs = glob(["vendor/cfg-if/src/**/*.rs"]), + compile_data = glob(["vendor/cfg_if/src/**/*.md"]), + crate_features = [ + "compiler_builtins", + "core", + "no_std", + ], + crate_name = "cfg_if", + deps = [ + ":compiler_builtins", + ":core", + ], + rustc_env = { + "RUSTC_BOOTSTRAP": "1", + }, +) + +rust_library( + name = "panic_abort", + srcs = glob(["library/panic_abort/src/**/*.rs"]), + compile_data = glob(["library/panic_abort/src/**/*.md"]), + crate_features = [ + "no_std", + ], + rustc_flags = [ + "--cap-lints=allow", + "-Zforce-unstable-if-unmarked", + # Needed to correctly compile this, see https://source.corp.google.com/piper///depot/google3/third_party/unsupported_toolchains/rust/src/nightly/src/bootstrap/bin/rustc.rs;l=92?q=%22panic%22&ss=piper%2FGoogle%2FPiper:google3%2Fthird_party%2Funsupported_toolchains%2Frust%2Fsrc%2Fnightly%2Fsrc%2Fbootstrap%2F + "-Cpanic=abort", + ], + crate_name = "panic_abort", + deps = [ + ":alloc", + ":cfg_if", + ":compiler_builtins", + ":c", + ":core", + ], + rustc_env = { + "RUSTC_BOOTSTRAP": "1", + }, +) + +rust_library( + name = "c", + srcs = glob(["vendor/libc/src/**/*.rs"]), + compile_data = glob(["vendor/libc/src/**/*.md"]), + crate_features = [ + "align", + ], + crate_name = "libc", + edition = "2015", + rustc_flags = [ + "--cfg=libc_align", + "--cfg=libc_core_cvoid", + "--cfg=libc_priv_mod_use", + "--cfg=libc_const_extern_fn", + ], + deps = [ + ":cfg_if", + ":compiler_builtins", + ":core", + ], + rustc_env = { + "RUSTC_BOOTSTRAP": "1", + }, +) + +rust_library( + name = "unwind", + srcs = glob(["library/unwind/src/**/*.rs"]), + compile_data = glob(["library/unwind/src/**/*.md"]), + crate_features = [ + "no_std", + "llvm-libunwind", + ], + crate_name = "unwind", + deps = [ + ":cfg_if", + ":compiler_builtins", + ":c", + ":core", + ], + rustc_env = { + "RUSTC_BOOTSTRAP": "1", + }, + rustc_flags = [ + "-Zforce-unstable-if-unmarked", + ], +) + +rust_library( + name = "profiler_builtins", + srcs = ["library/profiler_builtins/src/lib.rs"], + crate_features = [ + "no_std", + ], + crate_name = "profiler_builtins", + deps = [ + ":compiler_builtins", + ":core", + ], + rustc_env = { + "RUSTC_BOOTSTRAP": "1", + }, +) + +rust_library( + name = "rustc_demangle", + srcs = glob(["vendor/rustc-demangle/src/**/*.rs"]), + compile_data = glob(["vendor/rustc-demangle/src/**/*.md"]), + crate_features = [ + "core", + "compiler_builtins", + "no_std", + ], + crate_name = "rustc_demangle", + crate_root = "vendor/rustc-demangle/src/lib.rs", + deps = [ + ":compiler_builtins", + ":core", + ], + rustc_env = { + "RUSTC_BOOTSTRAP": "1", + }, +) + +rust_library( + name = "panic_unwind", + srcs = glob(["library/panic_unwind/src/**/*.rs"]), + compile_data = glob(["library/panic_unwind/src/**/*.md"]), + crate_features = [ + "no_std", + ], + crate_name = "panic_unwind", + rustc_flags = [ + "-Zforce-unstable-if-unmarked", + ], + deps = [ + ":alloc", + ":cfg_if", + ":compiler_builtins", + ":c", + ":unwind", + ":core", + ], + rustc_env = { + "RUSTC_BOOTSTRAP": "1", + }, +) + +rust_library( + name = "rustc_std_workspace_alloc", + crate_name = "rustc_std_workspace_alloc", + srcs = ["library/rustc-std-workspace-alloc/lib.rs"], + deps = [ + ":alloc", + ], + rustc_env = { + "RUSTC_BOOTSTRAP": "1", + }, +) + +rust_library( + name = "rustc_std_workspace_core", + crate_name = "core", + srcs = ["library/rustc-std-workspace-core/lib.rs"], + deps = [ + ":default_core", + ], + rustc_env = { + "RUSTC_BOOTSTRAP": "1", + }, +) + +rust_library( + name = "wasi", + srcs = glob(["vendor/wasi/**/*.rs"]), + crate_features = [ + "rustc-dep-of-std", + ], + crate_root = "vendor/wasi/src/lib.rs", + edition = "2018", + rustc_env = { + "RUSTC_BOOTSTRAP": "1", + }, + deps = [ + ":rustc_std_workspace_alloc", + ":compiler_builtins", + ":rustc_std_workspace_core", + ], + version = "0.11.0+wasi-snapshot-preview1", +) + +# rustc_std_workspace_core crate needs to replace core from the perspective of rdep +# when an upstream depends on rustc_std_workspace_core +# +# In this example, libstd for wasi os transitively depends on wasi crate which +# depends on core (built from rustc-std-workspace-core) +# See https://github.com/bytecodealliance/wasi/blob/9ec04a7d8ebb1bbb9e3291503425cee1ec38a560/Cargo.toml#L20C55-L27 +# Hence, all transitive deps needs to explicitly depends on rustc_std_workspace_core +# instead of core to avoid symbol duplications +alias( + name = "core", + actual = select({ + "@rules_rust//rust/platform:wasm32-wasi": ":rustc_std_workspace_core", + "//conditions:default": ":default_core", + }), +) diff --git a/examples/toolchain-to-rebuild-std/stdlibs.bzl b/examples/toolchain-to-rebuild-std/stdlibs.bzl new file mode 100644 index 0000000000..cd42b209d9 --- /dev/null +++ b/examples/toolchain-to-rebuild-std/stdlibs.bzl @@ -0,0 +1,19 @@ +def _stdlibs_impl(repository_ctx): + repository_ctx.download_and_extract( + url = "https://static.rust-lang.org/dist/" + repository_ctx.attr.beta_date + "/rustc-beta-src.tar.gz", + type = "tar.gz", + stripPrefix = "rustc-beta-src", + ) + + repository_ctx.file( + "BUILD.bazel", + content = repository_ctx.read(repository_ctx.attr.build_file), + ) + +stdlibs = repository_rule( + implementation = _stdlibs_impl, + attrs = { + "beta_date": attr.string(mandatory = True), + "build_file": attr.label(mandatory = True), + }, +) diff --git a/examples/toolchain-to-rebuild-std/toolchain/BUILD.bazel b/examples/toolchain-to-rebuild-std/toolchain/BUILD.bazel new file mode 100644 index 0000000000..3c77fa9ca9 --- /dev/null +++ b/examples/toolchain-to-rebuild-std/toolchain/BUILD.bazel @@ -0,0 +1,82 @@ +load("@rules_rust//rust:defs.bzl", "rust_stdlib_filegroup") +load("@rules_rust//rust:toolchain.bzl", "rust_toolchain") +load(":defs.bzl", "toolchain_sysroot", "with_beta_channel") + +# Rebuild stdlibs for aarch64-apple-darwin +# bazel build //toolchain:stdlibs --platforms=//:aarch64-apple-darwin +with_beta_channel( + name = "stdlibs", + srcs = ["@stdlibs//:stdlib_sources"], +) + +rust_stdlib_filegroup( + name = "rust_stdlibs", + srcs = [":sysroot_with_stdlibs"], +) + +# bazel build //toolchain:sysroot_with_stdlibs --platforms=//:aarch64-apple-darwin +toolchain_sysroot( + name = "sysroot_with_stdlibs", + srcs = [":stdlibs"], + dirname = "sysroot_with_stdlibs", + target_triple = select({ + "//:aarch64-apple-darwin": "aarch64-apple-darwin", + "//:wasm32-wasi": "wasm32-wasi", + }), +) + +rust_toolchain( + name = "toolchain_aarch64-apple-darwin_impl", + binary_ext = "", + default_edition = "2018", + dylib_ext = ".so", + exec_triple = "x86_64-unknown-linux-gnu", + rust_doc = "@rust_linux_x86_64__aarch64-apple-darwin__beta_tools//:rustdoc", + rust_std = ":rust_stdlibs", + rustc = "@rust_linux_x86_64__aarch64-apple-darwin__beta_tools//:rustc", + staticlib_ext = ".a", + stdlib_linkflags = [], + target_triple = "aarch64-apple-darwin", +) + +toolchain( + name = "toolchain_aarch64-apple-darwin", + exec_compatible_with = [ + "@platforms//cpu:x86_64", + "@platforms//os:linux", + ], + target_compatible_with = [ + "@platforms//os:osx", + "@platforms//cpu:aarch64", + ], + toolchain = ":toolchain_aarch64-apple-darwin_impl", + toolchain_type = "@rules_rust//rust:toolchain_type", +) + +rust_toolchain( + name = "toolchain_wasm32-wasi_impl", + binary_ext = "", + default_edition = "2018", + dylib_ext = ".so", + exec_triple = "x86_64-unknown-linux-gnu", + rust_doc = "@rust_linux_x86_64__wasm32-wasi__beta_tools//:rustdoc", + rust_std = ":rust_stdlibs", + rustc = "@rust_linux_x86_64__wasm32-wasi__beta_tools//:rustc", + staticlib_ext = ".a", + stdlib_linkflags = [], + target_triple = "wasm32-wasi", +) + +toolchain( + name = "toolchain_wasm32-wasi", + exec_compatible_with = [ + "@platforms//cpu:x86_64", + "@platforms//os:linux", + ], + target_compatible_with = [ + "@platforms//os:wasi", + "@platforms//cpu:wasm32", + ], + toolchain = ":toolchain_wasm32-wasi_impl", + toolchain_type = "@rules_rust//rust:toolchain_type", +) diff --git a/examples/toolchain-to-rebuild-std/toolchain/defs.bzl b/examples/toolchain-to-rebuild-std/toolchain/defs.bzl new file mode 100644 index 0000000000..e5b992042c --- /dev/null +++ b/examples/toolchain-to-rebuild-std/toolchain/defs.bzl @@ -0,0 +1,72 @@ +def _beta_channel_transition_impl(_, __): + return { + "//command_line_option:extra_toolchains": [], + "@rules_rust//rust/toolchain/channel:channel": "beta", + "//command_line_option:compilation_mode": "opt", + } + +_beta_channel_transition = transition( + inputs = [], + outputs = [ + "//command_line_option:extra_toolchains", + "@rules_rust//rust/toolchain/channel:channel", + "//command_line_option:compilation_mode", + ], + implementation = _beta_channel_transition_impl, +) + +def _with_beta_channel_transition_impl(ctx): + return [DefaultInfo(files = depset(ctx.files.srcs))] + +with_beta_channel = rule( + implementation = _with_beta_channel_transition_impl, + attrs = { + "srcs": attr.label_list( + allow_files = True, + ), + "_allowlist_function_transition": attr.label( + default = Label("@bazel_tools//tools/allowlists/function_transition_allowlist"), + ), + }, + cfg = _beta_channel_transition, +) + +def _toolchain_sysroot_impl(ctx): + sysroot = ctx.attr.dirname + outputs = [] + + rustlibdir = "{}/lib/rustlib/{}/lib".format(sysroot, ctx.attr.target_triple) + rustbindir = "{}/bin".format(sysroot) + + for inp in ctx.files.srcs: + if inp.short_path in ctx.attr.tools: + out = ctx.actions.declare_file(rustbindir + "/" + ctx.attr.tools[inp.short_path]) + else: + out = ctx.actions.declare_file(rustlibdir + "/" + inp.basename) + + outputs.append(out) + ctx.actions.symlink(output = out, target_file = inp) + + return [DefaultInfo( + files = depset(outputs), + runfiles = ctx.runfiles(files = outputs), + )] + +toolchain_sysroot = rule( + implementation = _toolchain_sysroot_impl, + attrs = { + "dirname": attr.string( + mandatory = True, + ), + "target_triple": attr.string( + doc = "The target triple for the rlibs.", + default = "x86_64-unknown-linux-gnu", + ), + "srcs": attr.label_list( + allow_files = True, + ), + "tools": attr.string_dict( + doc = "A map from tool's short_path to its final name under bin/", + ), + }, +) diff --git a/examples/toolchain-to-rebuild-std/tools.BUILD.bazel b/examples/toolchain-to-rebuild-std/tools.BUILD.bazel new file mode 100644 index 0000000000..4de5ca7ffb --- /dev/null +++ b/examples/toolchain-to-rebuild-std/tools.BUILD.bazel @@ -0,0 +1,6 @@ +package(default_visibility = ["//visibility:public"]) + +exports_files([ + "bin/rustdoc", + "bin/rustc", +]) diff --git a/rust/private/rustc.bzl b/rust/private/rustc.bzl index 1da062d923..14abb9dd2b 100644 --- a/rust/private/rustc.bzl +++ b/rust/private/rustc.bzl @@ -1267,6 +1267,8 @@ def rustc_compile_action( dsym_folder = ctx.actions.declare_directory(crate_info.output.basename + ".dSYM", sibling = crate_info.output) action_outputs.append(dsym_folder) + # print(ctx.label.name, toolchain.target_flag_value) + if ctx.executable._process_wrapper: # Run as normal ctx.actions.run(