Skip to content

Commit

Permalink
Add cross-compilation to CI
Browse files Browse the repository at this point in the history
Updates the README to use cargo target config instead of RUSTFLAGS to
avoid setting the linker for ebpf in cargo-in-cargo.
  • Loading branch information
tamird committed Oct 18, 2024
1 parent 597658d commit 4965f15
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 39 deletions.
42 changes: 37 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ concurrency:

jobs:
build:
runs-on: ubuntu-22.04
strategy:
fail-fast: false
matrix:
runner:
- ubuntu-latest # x86
rust:
- stable
- 1.80.1
program:
- kprobe
Expand All @@ -48,6 +48,15 @@ jobs:
- raw_tracepoint
- tp_btf
- tracepoint
include:
- runner: macos-13 # x86
rust: 1.80.1
program: kprobe
- runner: macos-14 # arm64
rust: 1.80.1
program: kprobe

runs-on: ${{ matrix.runner }}

steps:
- uses: actions/checkout@v4
Expand All @@ -57,6 +66,21 @@ jobs:
components: rust-src,rustfmt

- uses: dtolnay/rust-toolchain@master
if: runner.os == 'macOS' && runner.arch == 'X64'
with:
toolchain: ${{ matrix.rust }}
targets: x86_64-unknown-linux-musl
components: clippy

- uses: dtolnay/rust-toolchain@master
if: runner.os == 'macOS' && runner.arch == 'ARM64'
with:
toolchain: ${{ matrix.rust }}
targets: aarch64-unknown-linux-musl
components: clippy

- uses: dtolnay/rust-toolchain@master
if: runner.os == 'Linux'
with:
toolchain: ${{ matrix.rust }}
components: clippy
Expand All @@ -65,8 +89,16 @@ jobs:

- uses: taiki-e/install-action@v2
with:
tool: bpf-linker,cargo-generate
tool: cargo-generate

- run: brew install filosottile/musl-cross/musl-cross llvm
if: runner.os == 'macos'

- run: cargo install bpf-linker --git https://github.com/aya-rs/bpf-linker.git --no-default-features
if: runner.os == 'macos'

- run: cargo install bpf-linker --git https://github.com/aya-rs/bpf-linker.git
if: runner.os == 'Linux'

- run: sudo apt update
- run: sudo apt install expect
- run: sudo apt update && sudo apt install expect
- run: ./test.sh ${{ github.workspace }} ${{ matrix.program }}
9 changes: 4 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,10 @@ experience; this compromise necessitates the use of `xtask` to actually build th

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

```bash
AYA_BUILD_EBPF=true \
CC=${ARCH}-linux-musl-gcc \
RUSTFLAGS="-C linker=${ARCH}-linux-musl-gcc" \
cargo build --package {{project-name}} --release --target=${ARCH}-unknown-linux-musl
```shell
AYA_BUILD_EBPF=true CC=${ARCH}-linux-musl-gcc cargo build --package {{project-name}} --release \
--target=${ARCH}-unknown-linux-musl \
--config=target.${ARCH}-unknown-linux-musl.linker=\"${ARCH}-linux-musl-gcc\"
```
The cross-compiled program `target/${ARCH}-unknown-linux-musl/release/{{project-name}}` can be
copied to a Linux server or VM and run there.
71 changes: 43 additions & 28 deletions test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -61,35 +61,50 @@ pushd "${TMP_DIR}"
cargo generate --path "${TEMPLATE_DIR}" -n "${CRATE_NAME}" -d program_type="${PROG_TYPE}" "${ADDITIONAL_ARGS[@]}"
pushd "${CRATE_NAME}"

cargo +nightly fmt --all -- --check
cargo build --package "${CRATE_NAME}"
cargo build --package "${CRATE_NAME}" --release
# We cannot run clippy over the whole workspace at once due to feature unification. Since both
# ${CRATE_NAME} and ${CRATE_NAME}-ebpf depend on ${CRATE_NAME}-common and ${CRATE_NAME} activates
# ${CRATE_NAME}-common's aya dependency, we end up trying to compile the panic handler twice: once
# from the bpf program, and again from std via aya.
cargo clippy --exclude "${CRATE_NAME}-ebpf" --all-targets --workspace -- --deny warnings
cargo clippy --package "${CRATE_NAME}-ebpf" --all-targets -- --deny warnings
OS=$(uname)
if [[ "$OS" == "Darwin" ]]; then
ARCH=$(uname -m)
if [[ "$ARCH" == "arm64" ]]; then
ARCH="aarch64"
fi
AYA_BUILD_EBPF=true CC=${ARCH}-linux-musl-gcc cargo build --package "${CRATE_NAME}" --release \
--target="${ARCH}"-unknown-linux-musl \
--config=target."${ARCH}"-unknown-linux-musl.linker=\""${ARCH}"-linux-musl-gcc\"

expect <<EOF
set timeout 30 ;# Increase timeout if necessary
spawn cargo xtask run
expect {
-re "Waiting for Ctrl-C.*" {
send -- \003 ;# Send Ctrl-C
}
timeout {
puts "Error: Timed out waiting for 'Waiting for Ctrl-C...'"
exit 1
}
eof {
puts "Error: Process exited prematurely"
exit 1
echo "You are on macOS"
fi

if [[ "$OS" == "Linux" ]]; then
cargo +nightly fmt --all -- --check
cargo build --package "${CRATE_NAME}"
cargo build --package "${CRATE_NAME}" --release
# We cannot run clippy over the whole workspace at once due to feature unification. Since both
# ${CRATE_NAME} and ${CRATE_NAME}-ebpf depend on ${CRATE_NAME}-common and ${CRATE_NAME} activates
# ${CRATE_NAME}-common's aya dependency, we end up trying to compile the panic handler twice: once
# from the bpf program, and again from std via aya.
cargo clippy --exclude "${CRATE_NAME}-ebpf" --all-targets --workspace -- --deny warnings
cargo clippy --package "${CRATE_NAME}-ebpf" --all-targets -- --deny warnings

expect <<EOF
set timeout 30 ;# Increase timeout if necessary
spawn cargo xtask run
expect {
-re "Waiting for Ctrl-C.*" {
send -- \003 ;# Send Ctrl-C
}
timeout {
puts "Error: Timed out waiting for 'Waiting for Ctrl-C...'"
exit 1
}
eof {
puts "Error: Process exited prematurely"
exit 1
}
}
}
expect {
-re "Exiting.*" { }
eof { }
}
expect {
-re "Exiting.*" { }
eof { }
}
EOF
fi
3 changes: 2 additions & 1 deletion {{project-name}}/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,9 @@ async fn main() -> anyhow::Result<()> {
program.attach("{{tracepoint_name}}")?;
{%- endcase %}

let ctrl_c = signal::ctrl_c();
println!("Waiting for Ctrl-C...");
signal::ctrl_c().await?;
ctrl_c.await?;
println!("Exiting...");

Ok(())
Expand Down

0 comments on commit 4965f15

Please sign in to comment.