Skip to content

Commit

Permalink
Rebuild stdlibs from source
Browse files Browse the repository at this point in the history
  • Loading branch information
Vinh Tran committed Nov 3, 2023
1 parent 9341d1f commit a5cd8c7
Show file tree
Hide file tree
Showing 16 changed files with 391 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,5 @@ build --nolegacy_external_runfiles
common --noenable_bzlmod

try-import %workspace%/user.bazelrc

build --@rules_rust//rust/settings:experimental_toolchain_generated_sysroot=True
1 change: 1 addition & 0 deletions examples/rebuild-std-from-source/.bazelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build --@rules_rust//rust/settings:experimental_toolchain_generated_sysroot=True
Empty file.
26 changes: 26 additions & 0 deletions examples/rebuild-std-from-source/WORKSPACE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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()

rust_register_toolchains(
edition = "2021",
versions = [
"beta/2023-09-18",
],
)

load("//:stdlibs.bzl", "stdlibs")

stdlibs(
name = "stdlibs",
)
1 change: 1 addition & 0 deletions examples/rebuild-std-from-source/bazel-bin
1 change: 1 addition & 0 deletions examples/rebuild-std-from-source/bazel-out
1 change: 1 addition & 0 deletions examples/rebuild-std-from-source/bazel-testlogs
Empty file.
335 changes: 335 additions & 0 deletions examples/rebuild-std-from-source/stdlibs.BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,335 @@
load("@rules_rust//rust:defs.bzl", "rust_library", "rust_stdlib_filegroup")

exports_files(["bin/*"])

filegroup(
name = "binaries",
srcs = glob([
"bin/*",
"lib/*",
"lib64/*",
]),
)

rust_stdlib_filegroup(
name = "prebuilt_stdlibs",
srcs = glob(["lib/rustlib/x86_64_unknown-linux-gnu/*"]),
)

filegroup(
name = "stdlib_sources",
srcs = [
":liballoc",
":libcfg_if",
# ":libcompiler_builtins",
":libcore",
":libhashbrown",
":liblibc",
":libpanic_abort",
# ":libpanic_unwind",
":libprofiler_builtins",
":librustc_demangle",
":libstd",
":libstd_detect",
":libunwind",
],
)

rust_library(
name = "libstd",
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",
],
crate_name = "std",
rustc_env = {
"STD_ENV_ARCH": "aarch64",
},
rustc_flags = [
"--cfg=backtrace_in_libstd",
],
visibility = ["//visibility:public"],
deps = [
":liballoc",
":libcfg_if",
# ":libcompiler_builtins",
":libcore",
":libhashbrown",
":liblibc",
":libprofiler_builtins",
":librustc_demangle",
":libstd_detect",
":libunwind",
":libpanic_abort",
# ":libpanic_unwind",
],
)

rust_library(
name = "libhashbrown",
srcs = glob([
"vendor/hashbrown/src/**/*.rs",
]),
compile_data = glob(["vendor/hashbrown/src/**/*.md"]),
crate_features = [
"alloc",
"compiler_builtins",
"core",
"nightly",
"rustc-dep-of-std",
"rustc-internal-api",
"no_std",
],
crate_name = "hashbrown",
rustc_flags = [
"--cfg=has_extern_crate_alloc",
],
deps = [
":liballoc",
# ":libcompiler_builtins",
":libcore",
":libunwind",
],
)

rust_library(
name = "liballoc",
srcs = glob(["library/alloc/src/**/*.rs"]),
compile_data = glob(["library/alloc/src/**/*.md"]),
crate_features = ["no_std"],
crate_name = "alloc",
edition = "2021",
deps = [
# "libcompiler_builtins",
"libcore",
],
)

# rust_library(
# name = "libcompiler_builtins",
# srcs = glob([
# "vendor/compiler_builtins/src/**/*.rs",
# ]),
# compile_data = glob(["vendor/compiler_builtins/src/**/*.md"]),
# crate_features = [
# "compiler-builtins",
# "core",
# "default",
# "no_std",
# ],
# crate_name = "compiler_builtins",
# edition = "2015",
# deps = [
# "libcore",
# ],
# )

_CODEGEN_UNITS_FLAG = "-Ccodegen-units=2"

# Flags common to builds of the standard library, common to the stage0 bootstrap and later stages.
_EXTRA_FLAGS_FOR_STDLIB_BUILDS = [
_CODEGEN_UNITS_FLAG,
# Use v0 symbol mangling, see b/261148332.
"-Csymbol-mangling-version=v0",
# Always keep frame pointers, see b/258819642.
"-Cforce-frame-pointers=yes",
# bitcode is for doing LTO, but if we're doing LTO we probably also want FDO, so
# save space in prebuilt toolchains and don't include bitcode.
"-Cembed-bitcode=off",
]

rust_library(
name = "libcore",
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 = "libstd_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 = [
":liballoc",
":libcfg_if",
# ":libcompiler_builtins",
":libcore",
],
)

rust_library(
name = "libcfg_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 = [
# ":libcompiler_builtins",
":libcore",
],
)

rust_library(
name = "libpanic_abort",
srcs = glob(["library/panic_abort/src/**/*.rs"]),
compile_data = glob(["library/panic_abort/src/**/*.md"]),
crate_features = [
"no_std",
],
crate_name = "panic_abort",
deps = [
":liballoc",
":libcfg_if",
# ":libcompiler_builtins",
":libcore",
":liblibc",
],
)

rust_library(
name = "liblibc",
srcs = glob(["vendor/libc/src/**/*.rs"]),
compile_data = glob(["vendor/libc/src/**/*.md"]),
crate_features = [
"align",
"cfg_if",
],
crate_name = "libc",
edition = "2015",
rustc_flags = [
"--cfg=freebsd11",
"--cfg=libc_priv_mod_use",
"--cfg=libc_union",
"--cfg=libc_const_size_of",
"--cfg=libc_align",
"--cfg=libc_core_cvoid",
"--cfg=libc_packedN",
"--cfg=libc_thread_local",
],
deps = [
":libcfg_if",
# ":libcompiler_builtins",
":libcore",
],
)

rust_library(
name = "libunwind",
srcs = glob(["library/unwind/src/**/*.rs"]),
compile_data = glob(["library/unwind/src/**/*.md"]),
crate_features = [
"no_std",
],
crate_name = "unwind",
deps = [
":libcfg_if",
# ":libcompiler_builtins",
":libcore",
":liblibc",
],
)

rust_library(
name = "libprofiler_builtins",
srcs = ["library/profiler_builtins/src/lib.rs"],
crate_features = [
"no_std",
],
crate_name = "profiler_builtins",
deps = [
# ":libcompiler_builtins",
":libcore",
],
)

rust_library(
name = "librustc_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 = [
# ":libcompiler_builtins",
":libcore",
],
)

rust_library(
name = "libpanic_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 = [
":liballoc",
":libcfg_if",
# ":libcompiler_builtins",
":libcore",
":liblibc",
":libunwind",
],
)
Loading

0 comments on commit a5cd8c7

Please sign in to comment.