Skip to content

Commit

Permalink
Merge pull request #132 from dmitris/crosscompile
Browse files Browse the repository at this point in the history
add cross-compilation instructions for MacOS
  • Loading branch information
alessandrod authored Aug 4, 2023
2 parents 44b1024 + 1c06213 commit 334c23b
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 0 deletions.
57 changes: 57 additions & 0 deletions docs/book/aya/crosscompile.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# Cross-compiling aya-based programs

The instructions below show how to cross compile aya eBPF programs on Macs.
Cross compiling on other systems is possible too and we're going
to add instructions to do that soon (PRs welcome!).

# Cross-compiling aya-based programs on Mac

Cross compilation should work on both Intel and Apple Silicon Macs.

1. Install `rustup` following the instructions on <https://rustup.rs/>
1. Install the stable and nightly rust toolchains:
```bash
rustup install stable
rustup toolchain install nightly --component rust-src
```
1. Install the [rustup target](https://doc.rust-lang.org/nightly/rustc/platform-support.html#tier-1-with-host-tools) for your Linux target platform:
```bash
ARCH=x86_64
rustup target add ${ARCH}-unknown-linux-musl
```
1. Install LLVM with brew:
```bash
brew install llvm
```

1. Install the musl cross compiler:
to cross-compile for only `x86_64` targets (the default in musl-cross):
```bash
brew install FiloSottile/musl-cross/musl-cross
```
to cross-compile for only `aarch64` targets:
```bash
brew install FiloSottile/musl-cross/musl-cross --without-x86_64 --with-aarch64
```
to cross-compile for both `x86_64` and `aarch64` targets:
```bash
brew install FiloSottile/musl-cross/musl-cross --with-aarch64
```
See [homebrew-musl-cross](https://github.com/FiloSottile/homebrew-musl-cross)
for additional platform-specific options.

1. Install bpf-linker:
```bash
LLVM_SYS_160_PREFIX=$(brew --prefix llvm) cargo install bpf-linker --no-default-features
```
1. Build BPF object files:
```bash
cargo xtask build-ebpf --release
```
1. Build the userspace code:
```bash
RUSTFLAGS="-Clinker=${ARCH}-linux-musl-ld" cargo build --release --target=${ARCH}-unknown-linux-musl
```
The cross-compiled program
`target/${ARCH}-unknown-linux-musl/release/<program_name>`
can be copied to a Linux server or VM and run there.
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ nav:
- Reading Values From A Context: book/aya/context.md
- Using aya-tool: book/aya/aya-tool.md
- Using aya-log: book/aya/aya-log.md
- Cross-compilation: book/aya/crosscompile.md
- Program Types:
- book/programs/index.md
- Probes: book/programs/probes.md
Expand Down

0 comments on commit 334c23b

Please sign in to comment.