Skip to content

Commit a428f5a

Browse files
alexcrichtonischeinkman
authored andcommitted
std: Depend directly on crates.io crates
Ever since we added a Cargo-based build system for the compiler the standard library has always been a little special, it's never been able to depend on crates.io crates for runtime dependencies. This has been a result of various limitations, namely that Cargo doesn't understand that crates from crates.io depend on libcore, so Cargo tries to build crates before libcore is finished. I had an idea this afternoon, however, which lifts the strategy from rust-lang#52919 to directly depend on crates.io crates from the standard library. After all is said and done this removes a whopping three submodules that we need to manage! The basic idea here is that for any crate `std` depends on it adds an *optional* dependency on an empty crate on crates.io, in this case named `rustc-std-workspace-core`. This crate is overridden via `[patch]` in this repository to point to a local crate we write, and *that* has a `path` dependency on libcore. Note that all `no_std` crates also depend on `compiler_builtins`, but if we're not using submodules we can publish `compiler_builtins` to crates.io and all crates can depend on it anyway! The basic strategy then looks like: * The standard library (or some transitive dep) decides to depend on a crate `foo`. * The standard library adds ```toml [dependencies] foo = { version = "0.1", features = ['rustc-dep-of-std'] } ``` * The crate `foo` has an optional dependency on `rustc-std-workspace-core` * The crate `foo` has an optional dependency on `compiler_builtins` * The crate `foo` has a feature `rustc-dep-of-std` which activates these crates and any other necessary infrastructure in the crate. A sample commit for `dlmalloc` [turns out to be quite simple][commit]. After that all `no_std` crates should largely build "as is" and still be publishable on crates.io! Notably they should be able to continue to use stable Rust if necessary, since the `rename-dependency` feature of Cargo is soon stabilizing. As a proof of concept, this commit removes the `dlmalloc`, `libcompiler_builtins`, and `libc` submodules from this repository. Long thorns in our side these are now gone for good and we can directly depend on crates.io! It's hoped that in the long term we can bring in other crates as necessary, but for now this is largely intended to simply make it easier to manage these crates and remove submodules. This should be a transparent non-breaking change for all users, but one possible stickler is that this almost for sure breaks out-of-tree `std`-building tools like `xargo` and `cargo-xbuild`. I think it should be relatively easy to get them working, however, as all that's needed is an entry in the `[patch]` section used to build the standard library. Hopefully we can work with these tools to solve this problem! [commit]: alexcrichton/dlmalloc-rs@28ee12d
1 parent 4f3e5ce commit a428f5a

File tree

5 files changed

+10
-5
lines changed

5 files changed

+10
-5
lines changed

src/libstd/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ libnx-rs = { git = "https://github.com/ischeinkman/libnx-rs", features = ["sysro
4343

4444

4545
[target.x86_64-fortanix-unknown-sgx.dependencies]
46-
fortanix-sgx-abi = { path = "../rustc/fortanix-sgx-abi_shim" }
46+
fortanix-sgx-abi = { version = "0.3.1", features = ['rustc-dep-of-std'] }
4747

4848
[build-dependencies]
4949
cc = "1.0"

src/libstd/build.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,12 @@ fn main() {
8282
}
8383

8484
fn build_libbacktrace(target: &str) -> Result<(), ()> {
85-
let native = native_lib_boilerplate("libbacktrace", "libbacktrace", "backtrace", "")?;
85+
let native = native_lib_boilerplate(
86+
"../libbacktrace".as_ref(),
87+
"libbacktrace",
88+
"backtrace",
89+
"",
90+
)?;
8691

8792
let mut build = cc::Build::new();
8893
build

src/libstd/fs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2082,7 +2082,7 @@ mod tests {
20822082
use fs::{self, File, OpenOptions};
20832083
use io::{ErrorKind, SeekFrom};
20842084
use path::Path;
2085-
use rand::{StdRng, FromEntropy, RngCore};
2085+
use rand::{rngs::StdRng, FromEntropy, RngCore};
20862086
use str;
20872087
use sys_common::io::test::{TempDir, tmpdir};
20882088
use thread;

src/libstd/primitive_docs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ mod prim_unit { }
427427
/// ## 3. Get it from C.
428428
///
429429
/// ```
430-
/// # #![feature(libc)]
430+
/// # #![feature(rustc_private)]
431431
/// extern crate libc;
432432
///
433433
/// use std::mem;

src/libstd/sys/unix/ext/fs.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ pub trait OpenOptionsExt {
348348
/// # Examples
349349
///
350350
/// ```no_run
351-
/// # #![feature(libc)]
351+
/// # #![feature(rustc_private)]
352352
/// extern crate libc;
353353
/// use std::fs::OpenOptions;
354354
/// use std::os::unix::fs::OpenOptionsExt;

0 commit comments

Comments
 (0)