Skip to content

Commit db9c0f5

Browse files
authored
Replace Xargo with build-std (#30)
1 parent 31e89a3 commit db9c0f5

File tree

17 files changed

+33
-89
lines changed

17 files changed

+33
-89
lines changed

β€Ž.github/workflows/build.yml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ jobs:
3535
- name: Build
3636
working-directory: no_std
3737
run: cargo build --release --all-features
38-
xargo:
39-
name: xargo
38+
build_std:
39+
name: build-std
4040
strategy:
4141
matrix:
42-
project_dir: [ xargo, no_main, panic_immediate_abort]
42+
project_dir: [ build_std, no_main ]
4343
runs-on: ubuntu-latest
4444
steps:
4545
- uses: actions/checkout@v2
@@ -51,5 +51,5 @@ jobs:
5151
working-directory: ${{ matrix.project_dir }}
5252
run: >
5353
rustup component add rust-src;
54-
cargo install xargo --force;
55-
xargo build --target x86_64-unknown-linux-gnu --release --verbose;
54+
cargo +nightly build -Z build-std=std,panic_abort --target x86_64-unknown-linux-gnu --release;
55+
cargo +nightly build -Z build-std=std,panic_abort -Z build-std-features=panic_immediate_abort --target x86_64-unknown-linux-gnu --release;

β€ŽREADME.md

Lines changed: 23 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -145,18 +145,14 @@ Enable this in `Cargo.toml`:
145145
panic = "abort"
146146
```
147147

148-
# Optimize `libstd` with Xargo
148+
# Optimize `libstd` with `build-std`
149149

150150
![Minimum Rust: Nightly](https://img.shields.io/badge/Minimum%20Rust%20Version-nightly-orange.svg)
151151

152-
> **Note**: See also the nightly Cargo
153-
[`-Z build-std` feature ](https://doc.rust-lang.org/cargo/reference/unstable.html#build-std),
154-
which will likely evolve into a replacement for much of what Xargo currently does.
152+
> **Note**: See also [Xargo](https://github.com/japaric/xargo), the predecessor to `build-std`.
153+
[Xargo is currently in maintenance status](https://github.com/japaric/xargo/issues/193).
155154

156-
> **Note**: [Xargo is currently in maintenance status](https://github.com/japaric/xargo/issues/193),
157-
but eventually the features used below should make their way into Cargo.
158-
159-
> Example project is located in the [`xargo`](xargo) folder.
155+
> Example project is located in the [`build_std`](build_std) folder.
160156
161157
Rust ships pre-built copies of the standard library (`libstd`) with its toolchains. This means
162158
that developers don't need to build `libstd` every time they build their applications. `libstd`
@@ -170,37 +166,29 @@ aggressively optimize for size.
170166
2. It's not possible to remove portions of `libstd` that are not used in a particular application
171167
(e.g. LTO and panic behaviour).
172168

173-
This is where [Xargo](https://github.com/japaric/xargo) comes in. Xargo is able to compile
174-
`libstd` with your application from the source. It does this with the `rust-src` component that
175-
`rustup` conveniently provides.
176-
177-
Add a `Xargo.toml` file to the root of your project
178-
(this doesn't replace `Cargo.toml`, just is in addition):
179-
180-
```toml
181-
[dependencies]
182-
std = {default-features=false}
183-
```
169+
This is where [`build-std`](https://doc.rust-lang.org/cargo/reference/unstable.html#build-std)
170+
comes in. The `build-std` feature is able to compile `libstd` with your application from the
171+
source. It does this with the `rust-src` component that `rustup` conveniently provides.
184172

185-
Install the appropriate toolchain and Xargo:
173+
Install the appropriate toolchain and the `rust-src` component:
186174

187175
```bash
188176
$ rustup toolchain install nightly
189-
$ rustup override set nightly
190-
$ rustup component add rust-src
191-
$ cargo install xargo
177+
$ rustup component add rust-src --toolchain nightly
192178
```
193179

194-
Build using Xargo:
180+
Build using `build-std`:
195181

196182
```bash
197183
# Find your host's target triple.
198184
$ rustc -vV
199185
...
200186
host: x86_64-apple-darwin
201187

202-
# Use that target triple when building with Xargo.
203-
$ xargo build --target x86_64-apple-darwin --release
188+
# Use that target triple when building with build-std.
189+
# Add the =std,panic_abort to the option to make panic = "abort" Cargo.toml option work.
190+
# See: https://github.com/rust-lang/wg-cargo-std-aware/issues/56
191+
$ cargo +nightly build -Z build-std=std,panic_abort --target x86_64-apple-darwin --release
204192
```
205193

206194
Remember to `strip` the resulting executable. On macOS, the final binary size is reduced to 51KB.
@@ -209,19 +197,18 @@ Remember to `strip` the resulting executable. On macOS, the final binary size is
209197

210198
![Minimum Rust: Nightly](https://img.shields.io/badge/Minimum%20Rust%20Version-nightly-orange.svg)
211199

212-
> Example project is located in the [`panic_immediate_abort`](panic_immediate_abort) folder.
213-
214-
Even if `panic = abort` is specified in `Cargo.toml`, `rustc` will still include panic strings
200+
Even if `panic = "abort"` is specified in `Cargo.toml`, `rustc` will still include panic strings
215201
and formatting code in final binary by default.
216202
[An unstable `panic_immediate_abort` feature](https://github.com/rust-lang/rust/pull/55011)
217203
has been merged into the `nightly` `rustc` compiler to address this.
218204

219-
To use this, repeat the instructions above to use Xargo, but instead use the following
220-
`Xargo.toml`:
205+
To use this, repeat the instructions above to use `build-std`, but also pass the following
206+
[`-Z build-std-features=panic_immediate_abort`](https://doc.rust-lang.org/cargo/reference/unstable.html#build-std-features)
207+
option.
221208

222-
```toml
223-
[dependencies]
224-
std = {default-features=false, features=["panic_immediate_abort"]}
209+
```bash
210+
$ cargo +nightly build -Z build-std=std,panic_abort -Z build-std-features=panic_immediate_abort \
211+
--target x86_64-apple-darwin --release
225212
```
226213

227214
Remember to `strip` the resulting executable. On macOS, the final binary size is reduced to 30KB.
@@ -240,7 +227,7 @@ we will restrict our usage of `libstd` in order to reduce binary size further.
240227
If you want an executable smaller than 20 kilobytes, Rust's string formatting code,
241228
[`core::fmt`](https://doc.rust-lang.org/core/fmt/index.html) must
242229
be removed. `panic_immediate_abort` only removes some usages of this code. There is a lot of other
243-
code that uses formatting in some of cases. That includes Rust's "pre-main" code in `libstd`.
230+
code that uses formatting in some cases. That includes Rust's "pre-main" code in `libstd`.
244231

245232
By using a C entry point (by adding the `#![no_main]` attribute) , managing stdio manually, and
246233
carefully analyzing which chunks of code you or your dependencies include, you can sometimes
@@ -267,7 +254,7 @@ On macOS, the final stripped binary is reduced to 8KB.
267254
> Example project is located in the [`no_std`](no_std) folder.
268255
269256
Up until this point, our application was using the Rust standard library, `libstd`. `libstd`
270-
provides many convenient, well tested cross platform APIs and data types. But if a user wants
257+
provides many convenient, well tested cross-platform APIs and data types. But if a user wants
271258
to reduce binary size to an equivalent C program size, it is possible to depend only on `libc`.
272259

273260
It's important to understand that there are many drawbacks to this approach. For one, you'll

β€Žbuild_std/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target

β€Žpanic_immediate_abort/Cargo.lock renamed to β€Žbuild_std/Cargo.lock

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

β€Žxargo/Cargo.toml renamed to β€Žbuild_std/Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[package]
2-
name = "min-sized-rust-xargo"
2+
name = "build_std"
33
version = "0.1.0"
4-
authors = ["johnthagen <johnthagen@gmail.com>"]
54
edition = "2018"
65
license = "MIT"
76

File renamed without changes.

β€Žno_main/.cargo/config

Lines changed: 0 additions & 3 deletions
This file was deleted.

β€Žno_main/Xargo.toml

Lines changed: 0 additions & 2 deletions
This file was deleted.

β€Žpanic_immediate_abort/.cargo/config

Lines changed: 0 additions & 3 deletions
This file was deleted.

β€Žpanic_immediate_abort/.gitignore

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
Β (0)