How to add a dependency to Rust libstd

I am trying to add a new dependency to libstd in my own port of the Rust compiler, which right now is similar to the official Rust compiler from a couple of months ago. I am trying to do what is mentioned in Adding a new crate to libstd dependency - help - The Rust Programming Language Forum, but I cannot make it work. Please let me know if you see where I am doing something wrong.

I want to add another crate my-crate to Rust compiler's libstd like:

my-crate = { path = "../../../my-crate" }

I understand for my-crate to work here, I need to make core a dependency of my-crate. So I add this in my-crate/Cargo.toml which I got from rust/library/rustc-std-workspace-core at master · rust-lang/rust · GitHub :

[package]
name = "my-crate"
...
edition = "2018"

...

[dependencies]
core = { version = "1.0.0", optional = true, package = 'rustc-std-workspace-core' }

However, building the compiler with libstd gives me the same error as before:

./x.py build -i --stage=1 --target=a-new-crosscompiling-target
Updating only changed submodules
Submodules updated in 0.02 seconds
    Finished dev [unoptimized] target(s) in 0.22s
Building stage0 std artifacts (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
   Compiling my-crate v19.0.0 (/home/utsav/Desktop/my-crate)
error[E0463]: can't find crate for `core`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0463`.
error: could not compile `my-crate`.

If I try to directly use my-crate in Cargo.toml of libstd where my-crate has no dependencies, I get the same error.

1 Like

Just checking: you are enabling the rustc-dep-of-std (or equivalent) feature of my-crate, correct?

2 Likes

I was completely overlooking rustc-dep-of-std! I also realized that cfg-if was a good example I should have followed earlier on, as it is a crate that is a dependency of std, but it does not have anything patch-related going on.

I needed to have this in my-crate/Cargo.toml.

[dependencies]
core = { version = "1.0.0", optional = true, package = 'rustc-std-workspace-core' }
compiler_builtins = { version = "0.1.16", optional = true }

[features]
rustc-dep-of-std = ['core', 'compiler_builtins']

And then in libstd/Cargo.toml, I could do:

[dependencies]
my-crate = { git = "...", features = ['rustc-dep-of-std'] }
1 Like