How do I build rustc with statically linked rustlib?


#1

The .tar.gz files for Linux on the Rust download page contain rustc binaries with rustlib linked dynamically. When I run “make dist” or “make install” on the rust source code I also get a rustc with rustlib linked dynamically. However, the rustc installed by rustup seems to have rustlib linked statically.

Is there an easy way to link rustlib statically when you are compiling rustc yourself? A ./configure-flag maybe?

rustc from download page:

~/rustdownloads$ ldd rust-nightly-x86_64-unknown-linux-gnu/rustc/bin/rustc 
	linux-vdso.so.1 =>  (0x00007ffef4bdb000)
	librustc_driver-c8005792.so => /home/samme/rustdownloads/rust-nightly-x86_64-unknown-linux-gnu/rustc/bin/../lib/librustc_driver-c8005792.so (0x00007fce9dae7000)
	libstd-c8005792.so => /home/samme/rustdownloads/rust-nightly-x86_64-unknown-linux-gnu/rustc/bin/../lib/libstd-c8005792.so (0x00007fce9d519000)
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fce9d12c000)
	libsyntax_ext-c8005792.so => /home/samme/rustdownloads/rust-nightly-x86_64-unknown-linux-gnu/rustc/bin/../lib/libsyntax_ext-c8005792.so (0x00007fce9ce5c000)
	librustc_save_analysis-c8005792.so => /home/samme/rustdownloads/rust-nightly-x86_64-unknown-linux-gnu/rustc/bin/../lib/librustc_save_analysis-c8005792.so (0x00007fce9cb04000)
	librustc_privacy-c8005792.so => /home/samme/rustdownloads/rust-nightly-x86_64-unknown-linux-gnu/rustc/bin/../lib/librustc_privacy-c8005792.so (0x00007fce9c8eb000)
	librustc_trans-c8005792.so => /home/samme/rustdownloads/rust-nightly-x86_64-unknown-linux-gnu/rustc/bin/../lib/librustc_trans-c8005792.so (0x00007fce9c3ab000)
	librustc_passes-c8005792.so => /home/samme/rustdownloads/rust-nightly-x86_64-unknown-linux-gnu/rustc/bin/../lib/librustc_passes-c8005792.so (0x00007fce9c170000)
	librustc_lint-c8005792.so => /home/samme/rustdownloads/rust-nightly-x86_64-unknown-linux-gnu/rustc/bin/../lib/librustc_lint-c8005792.so (0x00007fce9bf0c000)
	librustc_incremental-c8005792.so => /home/samme/rustdownloads/rust-nightly-x86_64-unknown-linux-gnu/rustc/bin/../lib/librustc_incremental-c8005792.so (0x00007fce9bc82000)
	librustc_typeck-c8005792.so => /home/samme/rustdownloads/rust-nightly-x86_64-unknown-linux-gnu/rustc/bin/../lib/librustc_typeck-c8005792.so (0x00007fce9b7db000)
	librustc_borrowck-c8005792.so => /home/samme/rustdownloads/rust-nightly-x86_64-unknown-linux-gnu/rustc/bin/../lib/librustc_borrowck-c8005792.so (0x00007fce9b4a6000)
	librustc_mir-c8005792.so => /home/samme/rustdownloads/rust-nightly-x86_64-unknown-linux-gnu/rustc/bin/../lib/librustc_mir-c8005792.so (0x00007fce9b188000)
	librustc_const_eval-c8005792.so => /home/samme/rustdownloads/rust-nightly-x86_64-unknown-linux-gnu/rustc/bin/../lib/librustc_const_eval-c8005792.so (0x00007fce9aee4000)
	librustc_plugin-c8005792.so => /home/samme/rustdownloads/rust-nightly-x86_64-unknown-linux-gnu/rustc/bin/../lib/librustc_plugin-c8005792.so (0x00007fce9acd4000)
	librustc_metadata-c8005792.so => /home/samme/rustdownloads/rust-nightly-x86_64-unknown-linux-gnu/rustc/bin/../lib/librustc_metadata-c8005792.so (0x00007fce9a7d7000)
	librustc_resolve-c8005792.so => /home/samme/rustdownloads/rust-nightly-x86_64-unknown-linux-gnu/rustc/bin/../lib/librustc_resolve-c8005792.so (0x00007fce9a4e6000)
	librustc-c8005792.so => /home/samme/rustdownloads/rust-nightly-x86_64-unknown-linux-gnu/rustc/bin/../lib/librustc-c8005792.so (0x00007fce997c5000)
	librustc_const_math-c8005792.so => /home/samme/rustdownloads/rust-nightly-x86_64-unknown-linux-gnu/rustc/bin/../lib/librustc_const_math-c8005792.so (0x00007fce99592000)
	librustc_back-c8005792.so => /home/samme/rustdownloads/rust-nightly-x86_64-unknown-linux-gnu/rustc/bin/../lib/librustc_back-c8005792.so (0x00007fce99346000)
	librustc_llvm-c8005792.so => /home/samme/rustdownloads/rust-nightly-x86_64-unknown-linux-gnu/rustc/bin/../lib/librustc_llvm-c8005792.so (0x00007fce9643d000)
	libgraphviz-c8005792.so => /home/samme/rustdownloads/rust-nightly-x86_64-unknown-linux-gnu/rustc/bin/../lib/libgraphviz-c8005792.so (0x00007fce9622c000)
	libgetopts-c8005792.so => /home/samme/rustdownloads/rust-nightly-x86_64-unknown-linux-gnu/rustc/bin/../lib/libgetopts-c8005792.so (0x00007fce9600b000)
	libflate-c8005792.so => /home/samme/rustdownloads/rust-nightly-x86_64-unknown-linux-gnu/rustc/bin/../lib/libflate-c8005792.so (0x00007fce95dfe000)
	libsyntax-c8005792.so => /home/samme/rustdownloads/rust-nightly-x86_64-unknown-linux-gnu/rustc/bin/../lib/libsyntax-c8005792.so (0x00007fce95523000)
	librustc_errors-c8005792.so => /home/samme/rustdownloads/rust-nightly-x86_64-unknown-linux-gnu/rustc/bin/../lib/librustc_errors-c8005792.so (0x00007fce952ea000)
	libsyntax_pos-c8005792.so => /home/samme/rustdownloads/rust-nightly-x86_64-unknown-linux-gnu/rustc/bin/../lib/libsyntax_pos-c8005792.so (0x00007fce950c3000)
	libserialize-c8005792.so => /home/samme/rustdownloads/rust-nightly-x86_64-unknown-linux-gnu/rustc/bin/../lib/libserialize-c8005792.so (0x00007fce94e04000)
	liblog-c8005792.so => /home/samme/rustdownloads/rust-nightly-x86_64-unknown-linux-gnu/rustc/bin/../lib/liblog-c8005792.so (0x00007fce94bf4000)
	libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fce949d7000)
	libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fce946ce000)
	libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fce944b8000)
	libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fce942b3000)
	/lib64/ld-linux-x86-64.so.2 (0x000056520a535000)
	librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007fce940ab000)
	libfmt_macros-c8005792.so => /home/samme/rustdownloads/rust-nightly-x86_64-unknown-linux-gnu/rustc/bin/../lib/libfmt_macros-c8005792.so (0x00007fce93e9c000)
	librustc_platform_intrinsics-c8005792.so => /home/samme/rustdownloads/rust-nightly-x86_64-unknown-linux-gnu/rustc/bin/../lib/librustc_platform_intrinsics-c8005792.so (0x00007fce93c32000)
	librustc_data_structures-c8005792.so => /home/samme/rustdownloads/rust-nightly-x86_64-unknown-linux-gnu/rustc/bin/../lib/librustc_data_structures-c8005792.so (0x00007fce939c9000)
	librbml-c8005792.so => /home/samme/rustdownloads/rust-nightly-x86_64-unknown-linux-gnu/rustc/bin/../lib/librbml-c8005792.so (0x00007fce93793000)
	libterm-c8005792.so => /home/samme/rustdownloads/rust-nightly-x86_64-unknown-linux-gnu/rustc/bin/../lib/libterm-c8005792.so (0x00007fce93556000)

rustc from rustup:

~$ ldd .cargo/bin/rustc 
	linux-vdso.so.1 =>  (0x00007fff39fce000)
	libz.so.1 => /lib/x86_64-linux-gnu/libz.so.1 (0x00007f86f6c2f000)
	libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f86f6a2b000)
	libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f86f680d000)
	libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f86f65f7000)
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f86f622e000)
	/lib64/ld-linux-x86-64.so.2 (0x00005605ef2c3000)
	libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f86f5f24000)
	librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007f86f5d1c000)

#2

The “rustc” from rustup is just a shim that calls the real rustc, which is dynamically linked to Rust crates. The real rustc is in $(rustc --print sysroot)/bin/rustc.

The “rustc”, “cargo” and “rustup” in ~/.cargo/bin are all shims and are also the same binary (rustup) but with different names. You can confirm this if you do diff -u ~/.cargo/bin/rustc ~/.cargo/bin/rustup.

Is there an easy way to link rustlib statically when you are compiling rustc yourself? A ./configure-flag maybe?

There is but it’s not a configure flag; it’s a make invocation. I don’t remember the full command right now.


#3

There is but it’s not a configure flag; it’s a make invocation. I don’t remember the full command right now.

Apparently it’s make snap-stage3 for a native rustc and make snap-stage3-H-$TARGET for a cross compiled rustc.


#4

Thanks for replying so quickly, and for pointing me in the right direction!

I couldn’t get “make snap-stage3” to work. It seems it was removed in this commit: https://github.com/rust-lang/rust/commit/02538d463a350f5c3658f7aabefca16eb599d31c

However, I tried calling “make rustc-stage3” instead and that gave me a statically linked rustc.

:~/git/build-rust-upstream$ du -sh x86_64-unknown-linux-gnu/stage3/bin/rustc 
54M	x86_64-unknown-linux-gnu/stage3/bin/rustc
:~/git/build-rust-upstream$ ldd x86_64-unknown-linux-gnu/stage3/bin/rustc 
	linux-vdso.so.1 =>  (0x00007ffcbf5ba000)
	librt.so.1 => /lib/x86_64-linux-gnu/librt.so.1 (0x00007fb2c089b000)
	libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fb2c0696000)
	libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007fb2c0479000)
	libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007fb2c0170000)
	libstdc++.so.6 => /usr/lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007fb2bfded000)
	libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007fb2bfbd7000)
	libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fb2bf80e000)
	/lib64/ld-linux-x86-64.so.2 (0x0000557c3c56c000)

#5

In the past, when creating stage0 snapshots, --enable-llvm-static-stdcpp was really useful for making the binary close to self-contained.