Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build rust stdlib #1265

Merged
merged 3 commits into from
Aug 15, 2024
Merged

Build rust stdlib #1265

merged 3 commits into from
Aug 15, 2024

Conversation

NickeZ
Copy link
Collaborator

@NickeZ NickeZ commented Aug 6, 2024

Building the rust core and alloc libraries ourselves with abort as panic handler reduced the memory footprint from

Generating binary firmware.bin
   text	   data	    bss	    dec	    hex	filename
 680876	  19268	 206520	 906664	  dd5a8	firmware.elf

to

Generating binary firmware.bin
   text	   data	    bss	    dec	    hex	filename
 630452	  19268	 206520	 856240	  d10b0	firmware.elf

To test this you need to rebuild the docker build container.

@NickeZ NickeZ requested a review from benma August 6, 2024 08:10
@NickeZ NickeZ force-pushed the build-rust-stdlib branch from 08e70e9 to f6c597b Compare August 6, 2024 08:39
Copy link
Collaborator

@benma benma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested and works nicely, saving 50424 bytes in the Multi firmware binary 🎉

Comment on lines 4 to 6
# Copy the Cargo.lock for Rust to place the `cargo vendor` will see
# cp "$RUST_DIR/lib/rustlib/src/rust/Cargo.lock" "$RUST_DIR/lib/rustlib/src/rust/library/test/"
# chmod 777 $RUST_DIR/lib/rustlib/src/rust/library/test/Cargo.lock
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure what this means. I just ran this script inside docker and it seemed to work.

Copy link
Collaborator

@benma benma Aug 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok I see now that these are needed, but:

Why not uncomment and run them automatically?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved the commands to the dockerfile

@@ -0,0 +1,12 @@
RUST_DIR="$(rustc --print=sysroot)"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name of the script sounds like it vendors only the stdlib, but it vendors all our normal deps too and should be used instead of cargo vendor, right?

Maybe rename the script and add a section to src/rust/README.md about how to vendor.

src/rust/vendor-rust-std.sh Outdated Show resolved Hide resolved
@benma
Copy link
Collaborator

benma commented Aug 7, 2024

Generating binary firmware.bin
   text	   data	    bss	    dec	    hex	filename
 630452	  19268	 206520	 856240	  d10b0	firmware.elf

👍

du -sb firmware.bin results in 649720, which is the number of bytes actually taken up on the device.


RUSTC_BOOTSTRAP=1 cargo vendor \
--manifest-path Cargo.toml \
--sync "$RUST_DIR/lib/rustlib/src/rust/library/test/Cargo.toml" \
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a comment why it's this Cargo.toml? /test/ sounds wrong, so it could help to add some sort of reference.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wrote a comment, for some reason Cargo needs to see the dependencies of test. And since test depends on all the other crates it is enough to bring in the dependencies of it.

@NickeZ NickeZ force-pushed the build-rust-stdlib branch 2 times, most recently from 8c200c1 to 02757c4 Compare August 7, 2024 12:44
@NickeZ NickeZ marked this pull request as draft August 7, 2024 12:44
@NickeZ NickeZ force-pushed the build-rust-stdlib branch 2 times, most recently from 77def58 to 6864311 Compare August 7, 2024 13:22
@NickeZ
Copy link
Collaborator Author

NickeZ commented Aug 7, 2024

I found yet another flag to reduce the size of core: optimize_for_size.

   text	   data	    bss	    dec	    hex	filename
 629980	  19268	 206520	 855768	  d0ed8	firmware.elf

@NickeZ NickeZ force-pushed the build-rust-stdlib branch from 6864311 to 13248c9 Compare August 7, 2024 13:27
@NickeZ NickeZ marked this pull request as ready for review August 7, 2024 13:34
@benma
Copy link
Collaborator

benma commented Aug 7, 2024

I found yet another flag to reduce the size of core: optimize_for_size.

   text	   data	    bss	    dec	    hex	filename
 629980	  19268	 206520	 855768	  d0ed8	firmware.elf

Compared to before this, firmware.bin is now 472 bytes smaller. One would expect more for such a promising-sounding flag 😂

@NickeZ
Copy link
Collaborator Author

NickeZ commented Aug 8, 2024

Compared to before this, firmware.bin is now 472 bytes smaller. One would expect more for such a promising-sounding flag 😂

Perhaps it has a bigger impact on std :).

@NickeZ NickeZ requested a review from benma August 8, 2024 12:34
Copy link
Collaborator

@benma benma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tACK, very nice 😍

#
# The invocation below depends on the fact that rust std libs "Cargo.lock" has
# been manually copied to be next to the Cargo.toml file in the test
# directory.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe mention here that the Dockerfile is doing that.

@NickeZ NickeZ force-pushed the build-rust-stdlib branch 3 times, most recently from 7c8fe25 to 33c4a29 Compare August 12, 2024 14:24
Copy link
Collaborator

@benma benma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Please amend the commits in a way where each commit can build. Could just squash all into one.

@NickeZ NickeZ force-pushed the build-rust-stdlib branch 2 times, most recently from ca1a0b1 to 684593a Compare August 13, 2024 14:37
NickeZ added 3 commits August 13, 2024 19:54
This commit creates a script `vendor.sh` that can be used to vendor our
dependencies from crates.io. In addition to vendoring the direct
dependencies of our rust crates, it also vendors the dependencies of the
rust standard libraries.

This script requires a minor update to the Dockerfile because we need to
install the sources of the rust standard libraries and copy a file when
we are the "root" user.
@NickeZ NickeZ force-pushed the build-rust-stdlib branch from 684593a to b09c333 Compare August 13, 2024 18:06
@NickeZ
Copy link
Collaborator Author

NickeZ commented Aug 13, 2024

OK, now I'm pretty sure every commit passes .ci/ci. Do you also want to test?

@NickeZ NickeZ requested a review from benma August 13, 2024 19:32
@benma
Copy link
Collaborator

benma commented Aug 13, 2024

OK, now I'm pretty sure every commit passes .ci/ci. Do you also want to test?

No need I think, the commits look good! Thanks.

@NickeZ NickeZ merged commit ad5a40b into BitBoxSwiss:master Aug 15, 2024
1 check passed
@NickeZ NickeZ deleted the build-rust-stdlib branch August 19, 2024 07:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants