
joseluisq/rust-linux-darwin-builder使用同一个Docker镜像交叉编译Rust程序,支持Linux(musl libc)和macOS(osxcross)。
]([] 。
它包含交叉编译Rust项目所需的必要工具,例如通过musl-libc / musl-gcc生成Linux静态二进制文件(x86_64-unknown-linux-musl),以及通过osxcross生成macOS二进制文件(x86_64-apple-darwin),均通过同一个Linux镜像实现。
该Docker镜像支持多架构(amd64和arm64),可在原生环境中使用。此外,还可通过x86_64 Docker镜像变体交叉编译arm64架构的Linux或Darwin应用。
默认工作目录为/root/src。
以下是Docker镜像中包含的默认工具链。
shdocker run --rm \ --volume "${PWD}/sample":/root/src \ --workdir /root/src \ joseluisq/rust-linux-darwin-builder:1.68.1 \ sh -c "cargo build --release --target x86_64-unknown-linux-musl"
shdocker run --rm \ --volume "${PWD}/sample":/root/src \ --workdir /root/src \ joseluisq/rust-linux-darwin-builder:1.68.1 \ sh -c "cargo build --release --target x86_64-unknown-linux-gnu"
shdocker run --rm \ --volume "${PWD}/sample":/root/src \ --workdir /root/src \ joseluisq/rust-linux-darwin-builder:1.68.1 \ sh -c "cargo build --release --target x86_64-apple-darwin"
shdocker run --rm \ --volume "${PWD}/sample":/root/src \ --workdir /root/src \ joseluisq/rust-linux-darwin-builder:1.68.1 \ sh -c "cargo build --release --target aarch64-unknown-linux-gnu"
shdocker run --rm \ --volume "${PWD}/sample":/root/src \ --workdir /root/src \ joseluisq/rust-linux-darwin-builder:1.68.1 \ sh -c "cargo build --release --target aarch64-unknown-linux-musl"
shdocker run --rm \ --volume "${PWD}/sample":/root/src \ --workdir /root/src \ joseluisq/rust-linux-darwin-builder:1.68.1 \ sh -c "cargo build --release --target aarch64-apple-darwin"
已知CARGO_HOME默认指向$HOME/.cargo(此处为/root/.cargo)。若要使用自定义Cargo主目录,需确保在交叉编译前将Cargo config文件复制到目标目录,例如执行cp "$HOME/.cargo/config" "$CARGO_HOME/"。否则,可能会遇到链接错误(例如交叉编译至x86_64-apple-darwin目标时)。
也可将该镜像用作Dockerfile的基础镜像:
DockerfileFROM joseluisq/rust-linux-darwin-builder:1.68.1
还可将o32-clang(++)和o64-clang(++)用作常规编译器。
注意:
示例:
sh使用示例: 示例1:CC=o32-clang ./configure --host=i386-apple-darwin21.4 示例2:CC=i386-apple-darwin21.4-clang ./configure --host=i386-apple-darwin21.4 示例3:o64-clang -Wall test.c -o test 示例4:x86_64-apple-darwin21.4-strip -x test !!! 使用Automake时,需用aarch64-apple-darwin21.4-*替代arm64-* !!! !!! CC=aarch64-apple-darwin21.4-clang ./configure --host=aarch64-apple-darwin21.4 !!! !!! CC="aarch64-apple-darwin21.4-clang -arch arm64e" ./configure --host=aarch64-apple-darwin21.4 !!!
以下是使用Makefile交叉编译Rust应用的简单示例。
注意:
/root/src。创建Makefile:
shcompile: @docker run --rm -it \ -v $(PWD):/drone/src \ -w /drone/src \ joseluisq/rust-linux-darwin-builder:1.68.1 \ make cross-compile .PHONY: compile cross-compile: @echo @echo "1. 交叉编译示例..." @rustc -vV @echo @echo "2. 编译应用(linux-musl x86_64)..." @cargo build --manifest-path=tests/hello-world/Cargo.toml --release --target x86_64-unknown-linux-musl @du -sh tests/hello-world/target/x86_64-unknown-linux-musl/release/helloworld @echo @echo "3. 编译应用(apple-darwin x86_64)..." @cargo build --manifest-path=tests/hello-world/Cargo.toml --release --target x86_64-apple-darwin @du -sh tests/hello-world/target/x86_64-apple-darwin/release/helloworld .PHONY: cross-compile
运行makefile的compile目标,将生成两个发布版本二进制文件:x86_64-unknown-linux-musl和x86_64-apple-darwin。
shmake compile # 1. 交叉编译示例... # rustc 1.68.1 (8460ca823 2023-03-20) # binary: rustc # commit-hash: 8460ca823e8367a30dda430efda790588b8c84d3 # commit-date: 2023-03-20 # host: aarch64-apple-darwin # release: 1.68.1 # LLVM version: 15.0.6 # 2. 编译应用(linux-musl x86_64)... # Finished release [optimized] target(s) in 0.01s # 1.2M tests/hello-world/target/x86_64-unknown-linux-musl/release/helloworld # 3. 编译应用(apple-darwin x86_64)... # Finished release [optimized] target(s) in 0.01s # 240K tests/hello-world/target/x86_64-apple-darwin/release/helloworld
更多详情可参考James Waples的文章《从Linux交叉编译Rust至macOS》。
详见joseluisq/rust-linux-darwin-builder#7
若某些crate需要C绑定且编译或链接出错,尝试使用Clang进行C/C++构建。
例如交叉编译至macOS:
shCC=o64-clang \ CXX=o64-clang++ \ cargo build --target x86_64-apple-darwin
本项目
v1.42.0及之前版本使用旧版OpenSSLv1.0.2。
_自v1.43.x及更高版本起,使用OpenSSLv1.1.1(LTS),支持至2023-09-11。
详见[***]
除非您明确声明,否则您有意提交的任何贡献(定义于Apache-2.0许可中)将按以下双重许可分发,无额外条款或条件。
欢迎提交Pull request或提交issue。
本作品主要根据MIT许可和Apache许可(版本2.0)的条款分发。
© 2019-present Jose Quintana


manifest unknown 错误
TLS 证书验证失败
DNS 解析超时
410 错误:版本过低
402 错误:流量耗尽
身份认证失败错误
429 限流错误
凭证保存错误
来自真实用户的反馈,见证轩辕镜像的优质服务