@@ -145,18 +145,14 @@ Enable this in `Cargo.toml`:
145
145
panic = " abort"
146
146
```
147
147
148
- # Optimize ` libstd ` with Xargo
148
+ # Optimize ` libstd ` with ` build-std `
149
149
150
150
![ Minimum Rust: Nightly] ( https://img.shields.io/badge/Minimum%20Rust%20Version-nightly-orange.svg )
151
151
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 ) .
155
154
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.
160
156
161
157
Rust ships pre-built copies of the standard library (` libstd ` ) with its toolchains. This means
162
158
that developers don't need to build ` libstd ` every time they build their applications. ` libstd `
@@ -170,37 +166,29 @@ aggressively optimize for size.
170
166
2 . It's not possible to remove portions of ` libstd ` that are not used in a particular application
171
167
(e.g. LTO and panic behaviour).
172
168
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.
184
172
185
- Install the appropriate toolchain and Xargo :
173
+ Install the appropriate toolchain and the ` rust-src ` component :
186
174
187
175
``` bash
188
176
$ 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
192
178
```
193
179
194
- Build using Xargo :
180
+ Build using ` build-std ` :
195
181
196
182
``` bash
197
183
# Find your host's target triple.
198
184
$ rustc -vV
199
185
...
200
186
host: x86_64-apple-darwin
201
187
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
204
192
```
205
193
206
194
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
209
197
210
198
![ Minimum Rust: Nightly] ( https://img.shields.io/badge/Minimum%20Rust%20Version-nightly-orange.svg )
211
199
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
215
201
and formatting code in final binary by default.
216
202
[ An unstable ` panic_immediate_abort ` feature] ( https://github.com/rust-lang/rust/pull/55011 )
217
203
has been merged into the ` nightly ` ` rustc ` compiler to address this.
218
204
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.
221
208
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
225
212
```
226
213
227
214
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.
240
227
If you want an executable smaller than 20 kilobytes, Rust's string formatting code,
241
228
[ ` core::fmt ` ] ( https://doc.rust-lang.org/core/fmt/index.html ) must
242
229
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 ` .
244
231
245
232
By using a C entry point (by adding the ` #![no_main] ` attribute) , managing stdio manually, and
246
233
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.
267
254
> Example project is located in the [ ` no_std ` ] ( no_std ) folder.
268
255
269
256
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
271
258
to reduce binary size to an equivalent C program size, it is possible to depend only on ` libc ` .
272
259
273
260
It's important to understand that there are many drawbacks to this approach. For one, you'll
0 commit comments