Last active
December 22, 2020 00:52
-
-
Save justanotherdot/6f9b9d9fcd07f6a712e93a1bda3951b4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Description: Static linking of rustc on debian buster with glibc. | |
# Notes: | |
# * Obligatory rustc docs: https://doc.rust-lang.org/reference/linkage.html | |
# * proc-macro does not seem to work with static linking, regardless of libc flavor. | |
# cf. https://github.com/rust-lang/cargo/issues/7563#issuecomment-553526254 | |
# * traditionally, to perform static linking static libraries are needed, hence the libgcc-8-dev install below. | |
# * static linking is the default with musl and rustc afaict, hence builds on, say, alpine | |
# should automatically be static, whereas for glibc we need to pass `-C target-feature=+crt-static` | |
# to `RUSTFLAGS`. You can alternatively place this in your `Cargo.toml` under the `rustflags` key, e.g. | |
# ``` | |
# <snip> | |
# rustflags = ["-C", "target-feature=+crt-static"] | |
# <snip> | |
# ``` | |
# * glibc isn't necessarily conducive to static linking like musl. | |
# cf. https://stackoverflow.com/a/26306630/2748415 | |
# * you'll need to consider how licensing plays into the picture. | |
# cf. https://stackoverflow.com/questions/11693320/linking-glibc-statically-and-proprietary-software-licensing | |
FROM debian:buster | |
# Dependencies. | |
RUN apt update -y && apt install -y libgcc-8-dev curl build-essential file binutils | |
RUN ls /usr/lib/x86_64-linux-gnu*/*.a | |
RUN curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh -s -- --default-toolchain none -y | |
# Install nightly as the +crt-static feature does not yet seem to be recognized on stable. | |
RUN . $HOME/.cargo/env && \ | |
echo $PATH && \ | |
rustup toolchain install nightly-2020-12-20 --profile minimal && \ | |
rustup default nightly-2020-12-20 && \ | |
cargo new example && \ | |
cd example && \ | |
RUSTFLAGS="-C target-feature=+crt-static" cargo build | |
# Multiple ways to confirm the resulting binary is statically linked ... | |
RUN cd example && \ | |
file target/debug/example; \ | |
ldd target/debug/example; \ | |
readelf --program-headers --wide target/debug/example; \ | |
readelf -x .interp target/debug/example |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment