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: jobs:
build: build:
runs-on: ubuntu-22.04
strategy: strategy:
fail-fast: false fail-fast: false
matrix: matrix:
runner:
- ubuntu-latest # x86
rust: rust:
- stable
- 1.80.1 - 1.80.1
program: program:
- kprobe - kprobe
@ -48,6 +48,15 @@ jobs:
- raw_tracepoint - raw_tracepoint
- tp_btf - tp_btf
- tracepoint - 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: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
@ -57,6 +66,21 @@ jobs:
components: rust-src,rustfmt components: rust-src,rustfmt
- uses: dtolnay/rust-toolchain@master - 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: with:
toolchain: ${{ matrix.rust }} toolchain: ${{ matrix.rust }}
components: clippy components: clippy
@ -65,8 +89,18 @@ jobs:
- uses: taiki-e/install-action@v2 - uses: taiki-e/install-action@v2
with: 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 }} - 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. Cross compilation should work on both Intel and Apple Silicon Macs.
```bash ```shell
AYA_BUILD_EBPF=true \ AYA_BUILD_EBPF=true CC=${ARCH}-linux-musl-gcc cargo build --package {{project-name}} --release \
CC=${ARCH}-linux-musl-gcc \ --target=${ARCH}-unknown-linux-musl \
RUSTFLAGS="-C linker=${ARCH}-linux-musl-gcc" \ --config=target.${ARCH}-unknown-linux-musl.linker=\"${ARCH}-linux-musl-gcc\"
cargo build --package {{project-name}} --release --target=${ARCH}-unknown-linux-musl
``` ```
The cross-compiled program `target/${ARCH}-unknown-linux-musl/release/{{project-name}}` can be The cross-compiled program `target/${ARCH}-unknown-linux-musl/release/{{project-name}}` can be
copied to a Linux server or VM and run there. copied to a Linux server or VM and run there.

@ -14,7 +14,7 @@ if [ -z "${PROG_TYPE}" ]; then
fi fi
CRATE_NAME=aya-test-crate CRATE_NAME=aya-test-crate
case "${PROG_TYPE}" in case ${PROG_TYPE} in
"cgroup_sockopt") "cgroup_sockopt")
ADDITIONAL_ARGS=(-d sockopt_target=getsockopt) 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[@]}" cargo generate --path "${TEMPLATE_DIR}" -n "${CRATE_NAME}" -d program_type="${PROG_TYPE}" "${ADDITIONAL_ARGS[@]}"
pushd "${CRATE_NAME}" pushd "${CRATE_NAME}"
cargo +nightly fmt --all -- --check OS=$(uname)
cargo build --package "${CRATE_NAME}" case $OS in
cargo build --package "${CRATE_NAME}" --release "Darwin")
# We cannot run clippy over the whole workspace at once due to feature unification. Since both ARCH=$(uname -m)
# ${CRATE_NAME} and ${CRATE_NAME}-ebpf depend on ${CRATE_NAME}-common and ${CRATE_NAME} activates if [[ "$ARCH" == "arm64" ]]; then
# ${CRATE_NAME}-common's aya dependency, we end up trying to compile the panic handler twice: once ARCH="aarch64"
# from the bpf program, and again from std via aya. fi
cargo clippy --exclude "${CRATE_NAME}-ebpf" --all-targets --workspace -- --deny warnings AYA_BUILD_EBPF=true CC=${ARCH}-linux-musl-gcc cargo build --package "${CRATE_NAME}" --release \
cargo clippy --package "${CRATE_NAME}-ebpf" --all-targets -- --deny warnings --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 expect <<EOF
set timeout 30 ;# Increase timeout if necessary set timeout 30 ;# Increase timeout if necessary
spawn cargo xtask run spawn cargo xtask run
expect { expect {
-re "Waiting for Ctrl-C.*" { -re "Waiting for Ctrl-C.*" {
send -- \003 ;# Send Ctrl-C send -- \003 ;# Send Ctrl-C
} }
timeout { timeout {
puts "Error: Timed out waiting for 'Waiting for Ctrl-C...'" puts "Error: Timed out waiting for 'Waiting for Ctrl-C...'"
exit 1 exit 1
} }
eof { eof {
puts "Error: Process exited prematurely" puts "Error: Process exited prematurely"
exit 1 exit 1
}
} }
}
expect { expect {
-re "Exiting.*" { } -re "Exiting.*" { }
eof { } eof { }
} }
EOF EOF
;;
*)
echo "Unsupported OS: ${OS}"
exit 1
;;
esac

@ -205,8 +205,9 @@ async fn main() -> anyhow::Result<()> {
program.attach("{{tracepoint_name}}")?; program.attach("{{tracepoint_name}}")?;
{%- endcase %} {%- endcase %}
let ctrl_c = signal::ctrl_c();
println!("Waiting for Ctrl-C..."); println!("Waiting for Ctrl-C...");
signal::ctrl_c().await?; ctrl_c.await?;
println!("Exiting..."); println!("Exiting...");
Ok(()) Ok(())

Loading…
Cancel
Save