How does rustup build additional targets?

I basically would like to replicate the way that rustup (or the server that rustup uses) to build the additional targets that can be downloaded (e.g.: rustup target add thumb...) so that any other distribution can distribute those in the same way that rustup does.

There exists the build-std nightly feature of Cargo but I don't believe that is used for official builds of these targets as it is too recent and rustup has had support for these targets for quite a few time.

So the goal is to build the targets the same way that rustup does.

Haven't found a way to do so by looking at the Rust targets documentation but I think I found a way by modifying the config.toml in the build tree as so:

target = ["...", "thumbv7em-none-eabihf"]

ar = "arm-none-eabi-ar"
cc = "arm-none-eabi-gcc"
cxx = "arm-none-eabi-g++"
no-std = true

And it seemed to work quite alright with ./ build library/std as the target got built and it worked afterwards when installed.

Is this how it should be done?

This might be a duplicate of this thread but I can't download a compiler but can use the same one that was compiled on the same source tree that is already bootstrapped, so, only need to compile the extra targets.

I believe you might be looking for ./ setup choosing dist, or ./ dist - that gets you the "suitable for distribution" compile options.

Then rustup toolchain link to make it available everywhere on your system.

1 Like

Thanks for the reply!

But that unfortunately bootstraps rustc again, so, I already have a stage 2 compiler that was built from that source tree, but want to avoid building it again, only need to build the extra targets.

For anyone else looking it seems that the targets are built using Docker from the files on the following directory:

For example, thumb7.. targets are built on:

But it still performs the full bootstrap of the compiler from a stage0 compiler.

Perhaps it's efficient for Rust CI to perform the full bootstrap for various common targets instead of a single full-bootstrap per target.

You can use cargo build -Zbuild-std --target thumbv7em-none-eabihf when building your project. This will make cargo build the standard library for use in just this project and then innediately use it.

1 Like

I'm trying to build the core and std libraries to distribute it as components like rustup would do, and also alternatively I'm trying to build a fork of libstd for an unsupported operating system so it can't use the official sources distributed with rustup as rust-src.

The -Zbuild-std option would work for me on some projects but I need to use stable Rust, so it is out of the equation, I guess I can use RUSTC_BOOTSTRAP=1 but kind of defeats the point of using stable Rust.

This is to distribute additional targets, for example, on GNU Guix without slowing down the compilation of the compiler that targets the same host and optionally build other targets that can then be used with the compiler.

For example, when building Rust packages on GNU Guix one does not need the other targets for cross-compilation if not cross-compiling but if one wants to cross-compile one could pull a package with Rust libraries for that target.

I think this would be something similar that other distributions would like to accomplish for distribution purposes and can't depend on Rust official released binaries as everything must be built from source.

You can pass a --keep-stage argument, but if you're actually planning to distribute these to others I highly recommend going through the whole regular dist compile chain. stage0 is the previous release's compiler.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.