Add cross-compilation to CI

Updates the README to use cargo target config instead of RUSTFLAGS to
avoid setting the linker for ebpf in cargo-in-cargo.
reviewable/pr132/r1
Tamir Duberstein 3 months ago
parent 89eb550856
commit 27e7867528

@ -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
@ -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
@ -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
@ -65,8 +89,18 @@ 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 && sudo apt install expect
if: runner.os == 'Linux'
- run: sudo apt update
- run: sudo apt install expect
- run: ./test.sh ${{ github.workspace }} ${{ matrix.program }}

@ -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.

@ -14,7 +14,7 @@ if [ -z "${PROG_TYPE}" ]; then
fi
CRATE_NAME=aya-test-crate
case "${PROG_TYPE}" in
case ${PROG_TYPE} in
"cgroup_sockopt")
ADDITIONAL_ARGS=(-d sockopt_target=getsockopt)
;;
@ -61,35 +61,53 @@ 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)
case $OS in
"Darwin")
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\"
;;
"Linux")
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 <<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
;;
*)
echo "Unsupported OS: ${OS}"
exit 1
;;
esac

@ -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(())

Loading…
Cancel
Save