From d26188048572a9e8849879840480ac0beca857a4 Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Tue, 18 Jul 2023 14:57:40 -0400 Subject: [PATCH] integration-ebpf: artifact dependency bpf-linker - integration-ebpf: add artifact dependency on bpf-linker. - integration-ebpf: remove manual rerun-if-changed=bpf-linker. - integration-ebpf: make bpf-linker availble to rustc in build.rs. - xtask: filter for test binaries now that the build also produces the bpf-linker binary. - github: remove `cargo install bpf-linker`. - integration-test: remove bpf-linker installation instructions. Blockers: - https://github.com/rust-lang/cargo/issues/12385 (artifact = "bin" makes entire workspace nightly-only). --- .cargo/config.toml | 6 ++++++ .github/workflows/ci.yml | 12 ------------ Cargo.toml | 5 ++++- test/README.md | 1 - test/integration-ebpf/Cargo.toml | 2 +- test/integration-ebpf/build.rs | 30 ++++++++++++++++++++++++++---- xtask/Cargo.toml | 1 - xtask/src/run.rs | 9 ++++++--- 8 files changed, 43 insertions(+), 23 deletions(-) diff --git a/.cargo/config.toml b/.cargo/config.toml index 0c1258f6..dad2bc39 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -9,3 +9,9 @@ linker = "arm-linux-gnueabihf-gcc" [target.aarch64-unknown-linux-musl] linker = "aarch64-linux-musl-gcc" + +# See https://github.com/rust-lang/rust-analyzer/issues/14510. +# +# Turns out this affects all cargo invocations in the project, not just rust-analyzer. +[unstable] +bindeps = true diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 59bff5bf..6519c482 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -131,9 +131,6 @@ jobs: - uses: Swatinem/rust-cache@v2 - - name: bpf-linker - run: cargo install bpf-linker --git https://github.com/aya-rs/bpf-linker.git - - uses: taiki-e/install-action@cargo-hack - name: Build env: @@ -185,10 +182,6 @@ jobs: sudo apt update sudo apt -y install clang gcc-multilib llvm locate qemu-system-{arm,x86} - - name: bpf-linker - if: runner.os == 'Linux' - run: cargo install bpf-linker --git https://github.com/aya-rs/bpf-linker.git - - name: Install prerequisites if: runner.os == 'macOS' # The xargs shipped on macOS always exits 0 with -P0, so we need GNU findutils. @@ -211,11 +204,6 @@ jobs: # https://github.com/Homebrew/homebrew-core/issues/140244 codesign --verify $(which qemu-system-x86_64) || brew reinstall qemu --build-from-source - - name: bpf-linker - if: runner.os == 'macOS' - # NB: rustc doesn't ship libLLVM.so on macOS, so disable proxying (default feature). - run: cargo install bpf-linker --git https://github.com/aya-rs/bpf-linker.git --no-default-features - - name: Download debian kernels if: runner.arch == 'ARM64' run: | diff --git a/Cargo.toml b/Cargo.toml index 947d9131..da8f356b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -59,6 +59,7 @@ aya-obj = { path = "aya-obj", version = "0.1.0", default-features = false } aya-tool = { path = "aya-tool", default-features = false } bindgen = { version = "0.69", default-features = false } bitflags = { version = "2.2.1", default-features = false } +bpf-linker = { version = "0.9.10", artifact = "bin" } bytes = { version = "1", default-features = false } cargo_metadata = { version = "0.18.0", default-features = false } clap = { version = "4", default-features = false } @@ -95,7 +96,6 @@ test-log = { version = "0.2.13", default-features = false } testing_logger = { version = "0.1.1", default-features = false } thiserror = { version = "1", default-features = false } tokio = { version = "1.24.0", default-features = false } -which = { version = "5.0.0", default-features = false } xtask = { path = "xtask", default-features = false } [profile.dev] @@ -107,3 +107,6 @@ panic = "abort" [profile.release.package.integration-ebpf] debug = 2 codegen-units = 1 + +[patch.crates-io] +bpf-linker = { git = "https://github.com/aya-rs/bpf-linker.git", artifact = "bin" } diff --git a/test/README.md b/test/README.md index 82e41da0..1122104a 100644 --- a/test/README.md +++ b/test/README.md @@ -10,7 +10,6 @@ You'll need: 1. `rustup toolchain install nightly` 1. `rustup target add {aarch64,x86_64}-unknown-linux-musl` -1. `cargo install bpf-linker` 1. (virtualized only) `qemu` ## Usage diff --git a/test/integration-ebpf/Cargo.toml b/test/integration-ebpf/Cargo.toml index d471acf3..7fc1bb56 100644 --- a/test/integration-ebpf/Cargo.toml +++ b/test/integration-ebpf/Cargo.toml @@ -9,8 +9,8 @@ aya-bpf = { path = "../../bpf/aya-bpf" } aya-log-ebpf = { path = "../../bpf/aya-log-ebpf" } [build-dependencies] -which = { workspace = true } xtask = { path = "../../xtask" } +bpf-linker = { workspace = true } [[bin]] name = "log" diff --git a/test/integration-ebpf/build.rs b/test/integration-ebpf/build.rs index d339e195..0a33dd2e 100644 --- a/test/integration-ebpf/build.rs +++ b/test/integration-ebpf/build.rs @@ -1,6 +1,4 @@ use std::env; - -use which::which; use xtask::AYA_BUILD_INTEGRATION_BPF; /// Building this crate has an undeclared dependency on the `bpf-linker` binary. This would be @@ -25,7 +23,31 @@ fn main() { .unwrap_or_default(); if build_integration_bpf { - let bpf_linker = which("bpf-linker").unwrap(); - println!("cargo:rerun-if-changed={}", bpf_linker.to_str().unwrap()); + let out_dir = env::var_os("OUT_DIR").unwrap(); + let out_dir = std::path::PathBuf::from(out_dir); + + let bpf_linker = env::var("CARGO_BIN_FILE_BPF_LINKER").unwrap(); + + // There seems to be no way to pass `-Clinker={}` to rustc from here. + // + // We assume rustc is going to look for `bpf-linker` on the PATH, so we can create a symlink + // and put it on the PATH. + let bin_dir = out_dir.join("bin"); + std::fs::create_dir_all(&bin_dir).unwrap(); + let bpf_linker_symlink = bin_dir.join("bpf-linker"); + match std::fs::remove_file(&bpf_linker_symlink) { + Ok(()) => {} + Err(err) => { + if err.kind() != std::io::ErrorKind::NotFound { + panic!("failed to remove symlink: {err}") + } + } + } + std::os::unix::fs::symlink(bpf_linker, bpf_linker_symlink).unwrap(); + let path = env::var_os("PATH"); + let path = path.as_ref(); + let paths = std::iter::once(bin_dir).chain(path.into_iter().flat_map(env::split_paths)); + let path = env::join_paths(paths).unwrap(); + println!("cargo:rustc-env=PATH={}", path.to_str().unwrap()); } } diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml index 1a2ae931..e789a3ee 100644 --- a/xtask/Cargo.toml +++ b/xtask/Cargo.toml @@ -20,4 +20,3 @@ rustdoc-json = { workspace = true } rustup-toolchain = { workspace = true } syn = { workspace = true } tempfile = { workspace = true } -which = { workspace = true } diff --git a/xtask/src/run.rs b/xtask/src/run.rs index 64114620..010b27b6 100644 --- a/xtask/src/run.rs +++ b/xtask/src/run.rs @@ -11,7 +11,7 @@ use std::{ }; use anyhow::{anyhow, bail, Context as _, Result}; -use cargo_metadata::{Artifact, CompilerMessage, Message, Target}; +use cargo_metadata::{Artifact, ArtifactProfile, CompilerMessage, Message, Target}; use clap::Parser; use xtask::{exec, Errors, AYA_BUILD_INTEGRATION_BPF}; @@ -79,10 +79,13 @@ where Message::CompilerArtifact(Artifact { executable, target: Target { name, .. }, + profile: ArtifactProfile { test, .. }, .. }) => { - if let Some(executable) = executable { - executables.push((name, executable.into())); + if test { + if let Some(executable) = executable { + executables.push((name, executable.into())); + } } } Message::CompilerMessage(CompilerMessage { message, .. }) => {