Rust Cross Compile on OSX
Last updated on December 31, 2024 pm
Talk is cheap, show me the Makefile.
Setup
.PHONY: osx-setup-musl
setup-osx-musl:
@brew install filosottile/musl-cross/musl-cross
# @sudo ln -s "$(brew --prefix musl-cross)/bin/x86_64-linux-musl-gcc" /usr/local/bin/musl-gcc
.PHONY: osx-setup-gnu
setup-osx-gnu:
@brew tap SergioBenitez/osxct
@brew install x86_64-unknown-linux-gnu
Build
.PHONY: osx-cross-build-gnu
osx-cross-build-gnu:
@echo "Building presence-tool with gnu"
@rustup target add x86_64-unknown-linux-gnu
@CARGO_TARGET_X86_64_UNKNOWN_LINUX_GNU_LINKER=x86_64-unknown-linux-gnu-gcc cargo build --release --target=x86_64-unknown-linux-gnu
.PHONY: osx-cross-build-musl
osx-cross-build-musl:
@echo "Building presence-tool with musl"
@rustup target add x86_64-unknown-linux-musl
@CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_LINKER=x86_64-linux-musl-gcc cargo build --release --target x86_64-unknown-linux-musl
Chore
.PHONY: osx-setup-cargo-hint
setup-osx-cargo-hint:
@echo "Put these lines into your ~/.cargo/config.toml:"
@echo '```toml'
@echo "[target.x86_64-unknown-linux-musl]"
@echo "linker = \"x86_64-linux-musl-gcc\""
@echo "[target.x86_64-unknown-linux-gnu]"
@echo "linker = \"x86_64-unknown-linux-gnu-gcc\""
@echo '```'
.PHONY: install-bin
install-bin:
@echo "Installing current bin crate to ~/.cargo/bin"
@cargo install --path .
.PHONY: check-dynamic-link-osx
check-dynamic-link-osx: build
@echo "Checking for dynamic linking on OSX"
@otool -L target/release/presence-tool
.PHONY: check-dynamic-link-linux
check-dynamic-link-linux: build-musl
@echo "Checking for dynamic linking on Linux"
@ldd target/release/presence-tool
Cargo Config
[target.x86_64-unknown-linux-musl]
linker = "x86_64-linux-musl-gcc"
[target.x86_64-unknown-linux-gnu]
linker = "x86_64-unknown-linux-gnu-gcc"
Once you’ve configured your Cargo.toml
and ~/.cargo/config.toml
, you can simply try this:
.PHONY: build-musl
build-musl:
@rustup target add x86_64-unknown-linux-musl # this line is optional
@cargo build --release --target x86_64-unknown-linux-musl
Docker
You can use a docker image to set up a build env:
docker run -it --name rust-linux-builder rust:1.83.0-bullseye
docker exec -it rust-linux-builder bash
docker cp . rust-linux-builder:/root # cp the source code to the container
Then you can install the necessary dependencies and start building:
apt install gcc-multilib-x86-64-linux-gnu
CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_LINKER=x86_64-linux-gnu-gcc cargo build --release --target x86_64-unknown-linux-musl
Or, you can use a docker-compose.yml
file:
services:
rust-linux-builder:
container_name: rust-linux-builder
image: rust:1.83.0-bullseye
volumes:
- .:/root/myapp
working_dir: /root/myapp
command: bash -c "
rustup target add x86_64-unknown-linux-musl
&& apt update
&& apt install -y gcc-multilib-x86-64-linux-gnu
&& CARGO_TARGET_X86_64_UNKNOWN_LINUX_MUSL_LINKER=x86_64-linux-gnu-gcc cargo build --release --target x86_64-unknown-linux-musl
"
Rust Cross Compile on OSX
https://kayce.world/tech/rust_cross_compile_on_osx/