build static binary with container

This commit is contained in:
guochao 2023-11-02 10:46:46 +08:00
parent 66cd73498c
commit 1a921e1b8b
3 changed files with 24 additions and 9 deletions

3
Docker.static-builder Normal file
View File

@ -0,0 +1,3 @@
FROM rust:alpine
RUN apk add --no-cache pkgconf libseccomp-static libseccomp-dev musl-dev
ENTRYPOINT ["/usr/local/cargo/bin/cargo", "build"]

View File

@ -21,14 +21,29 @@ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
### Build
```
```bash
cargo build
```
### Build static with container
```bash
docker build -t x2t-sandbox-builder:1 -f Docker.static-builder .
docker run -it --rm \
-v $PWD:/src --workdir /src \
-e RUSTFLAGS='-C target-feature=+crt-static' \
x2t-sandbox-builder:1 \
--features tracing-mode \
--target x86_64-unknown-linux-musl \
--release \
./target/x86_64-unknown-linux-musl/release/x2t-sandbox --help
```
### Generate syscalls with strace
```
```bash
strace -f --output x2t-syscalls.txt /path/to/x2t some.xml
```
@ -36,7 +51,7 @@ strace -f --output x2t-syscalls.txt /path/to/x2t some.xml
cargo 开启 tracing-mode 后,宏找不到环境变量和文件不会失败,可以直接生成一个。
```
```bash
cargo build --features tracing-mode
RUST_LOG=trace ./target/debug/x2t-sandbox -- -l x2t-syscalls.txt /path/to/x2t some.xml
cat x2t-syscalls.txt | sort | uniq | sponge x2t-syscalls.txt
@ -57,7 +72,7 @@ cargo build
nix 是一个 fully reproducible 的构建和配置系统。
- 安装 nix
```
```bash
curl --proto '=https' --tlsv1.2 -sSf -L https://install.determinate.systems/nix | sh -s -- install
```
- 构建
@ -75,7 +90,7 @@ nix 是一个 fully reproducible 的构建和配置系统。
# git push
```
- 二进制在 result 目录下
```
```bash
./result/bin/x2t-sandbox /path/to/x2t some.xml
```
@ -86,7 +101,7 @@ nix 是一个 fully reproducible 的构建和配置系统。
- [x2t-syscalls.txt](x2t-syscalls.txt): x2t 用到的 syscall
- [build.rs](build.rs): 解决少数构建平台无法找到 libseccomp 的问题
- [x2t-sandbox-rulegen](x2t-sandbox-rulegen/): 通过 macro 直接从 syscall 列表生成代码不用内嵌syscall名字文本了不容易被修改
- Cargo.toml/Cargo.lock/rust-toolchain.toml: 已经被配置为默认构建静态链接的二进制
- Cargo.toml/Cargo.lock: 已经被配置为默认构建静态链接的二进制
- optional: flake.nix/flake.lock: nix 搭建的、完全统一的统一开发环境,配置为默认构建静态链接的二进制
- .envrc: 如果不适用nix注释掉或者 direnv block 一下不加载就行
- .gitignore

View File

@ -1,3 +0,0 @@
[toolchain]
channel = "stable"
targets = ["x86_64-unknown-linux-gnu", "x86_64-unknown-linux-musl"]