Skip to content

Commit

Permalink
Setup cross compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
brumhard committed May 4, 2023
1 parent 9a6b38f commit 9d53827
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# compilation targets
/target
/out

# used for tests
*.tar
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,8 @@ rl layer dump <image> -l <layer> -o <dir> # dump only this layer
rl layer dump <image> -l <layer> -o <dir> --stack # include preceding layers into the output
rl layer extract <image> -l <layer> -p <src> -o <dest> # extract a file or dir from the layer
```

## Troubleshooting

- <https://github.com/cross-rs/cross/issues/1184>
- "version `GLIBC_2.28' not found" -> <https://github.com/cross-rs/cross/issues/724>
16 changes: 15 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,26 @@
};
};

devShell = mkShell {
# should not include c toolchain but use host toolchain.
# this seems to be required to cross compile x86_64-apple-darwin on M1
# https://github.com/NixOS/nixpkgs/commit/9b3091a94cad63ebd0bd7aafbcfed7c133ef899d
devShell = mkShellNoCC {
packages = [
rustup
cargo-audit
mask
goreleaser
cargo-cross
yq-go
ripgrep
# required for rust
libiconv
];

shellHook = ''
# see https://github.com/cross-rs/cross/issues/1241
export CROSS_CONTAINER_OPTS="--platform linux/amd64"
'';
};
}
);
Expand Down
26 changes: 26 additions & 0 deletions maskfile.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,29 @@ rm -rf "$out_dir"
docker build -f test.Dockerfile -t "$image_name" .
cargo run -- --image "$image_name" dump -o "$out_dir" --verbose
```

## cross

> builds the images for all supported platforms
**OPTIONS**

* filter
* flags: --filter -f
* type: string
* desc: filter all targets with the given string, e.g. "linux", "aarch64"

```sh
out_dir="out"
targets=$(yq -o json -p toml -r '.toolchain.targets[]' rust-toolchain)
if [ "$filter" != "" ]; then
targets=$(echo "$targets"|rg "$filter")
fi
mkdir -p "$out_dir"/bin
for target in $targets; do
echo "building for $target"
# specifying target-dir is a hack for https://github.com/cross-rs/cross/issues/724
cross build --release --target "$target" --target-dir "$out_dir/$target"
cp "$out_dir/$target/$target/release/rl" "$out_dir/bin/rl-$target"
done
```
8 changes: 7 additions & 1 deletion rust-toolchain
Original file line number Diff line number Diff line change
@@ -1,2 +1,8 @@
[toolchain]
channel = "1.69"
channel = "1.69"
targets = [
"x86_64-apple-darwin",
"aarch64-apple-darwin",
"x86_64-unknown-linux-musl",
"aarch64-unknown-linux-musl"
]

0 comments on commit 9d53827

Please sign in to comment.