Skip to content

How to build std Rust programs with customized musl libc.

Wang Runji edited this page Feb 22, 2019 · 6 revisions

工具链:

  • Rust nightly
  • xargo(rust-src)
    cargo install xargo
    rustup component add rust-src
  • musl-gcc:
    # For macOS:
    brew install FiloSottile/musl-cross/musl-cross
    # For ubuntu
    sudo apt install musl-dev

首先定制并编译musl,之后在build/lib目录下应该能找到libc.a,crt*.o等文件。

这里我们需要做一步Hack,在lib中创建一个空的libunwind.a,以骗过Rust编译器:

cp libm.a libunwind.a

接下来,在项目目录下:

rustup override set nightly

添加文件.cargo/config

[target.x86_64-unknown-linux-musl]
linker = "rust-lld"
rustflags = [
    "-C", "target-feature=-mmx,-sse,+soft-float",
    "-C", "panic=abort",
    "-L", "<path/to/musl/lib>"
]

添加文件Xargo.toml

[target.x86_64-unknown-linux-musl.dependencies]
std = {}

编译:

# For Linux:
export TARGET_CC=musl-gcc
# For macOS:
export TARGET_CC=x86_64-linux-musl-gcc

xargo build --target x86_64-unknown-linux-musl