From b3842e3074dbb898ac6da6291f6830d81aeb4cf4 Mon Sep 17 00:00:00 2001 From: Michal Rostecki Date: Wed, 13 Mar 2024 20:22:18 +0100 Subject: [PATCH] integration-test: Prefer system-wide lld over rust-lld rust-lld is unaware of system libraries and seems like there is no way to point it to them. That often triggers issues like: ``` cargo:warning=error: linking with `rust-lld` failed: exit status: 1ger, ppv-lite86, libc... cargo:warning= | cargo:warning= = note: LC_ALL="C" PATH="/home/vadorovsky/.rustup/toolchains/stable-x86_64-un cargo:warning= = note: rust-lld: error: unable to find library -lgcc_s cargo:warning= rust-lld: error: unable to find library -lc cargo:warning= cargo:warning= cargo:warning= cargo:warning=error: aborting due to 1 previous error ``` Using system-wide LLD makes the issue disappear. Fixes #907 --- xtask/src/run.rs | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/xtask/src/run.rs b/xtask/src/run.rs index 8bba7a5325..8a1f2da875 100644 --- a/xtask/src/run.rs +++ b/xtask/src/run.rs @@ -13,6 +13,7 @@ use std::{ use anyhow::{anyhow, bail, Context as _, Result}; use cargo_metadata::{Artifact, CompilerMessage, Message, Target}; use clap::Parser; +use which::which; use xtask::{exec, Errors, AYA_BUILD_INTEGRATION_BPF}; #[derive(Parser)] @@ -55,11 +56,30 @@ pub fn build(target: Option<&str>, f: F) -> Result> where F: FnOnce(&mut Command) -> &mut Command, { - // Always use rust-lld and -Zbuild-std in case we're cross-compiling. + // Always use lld and -Zbuild-std in case we're cross-compiling. let mut cmd = Command::new("cargo"); cmd.args(["build", "--message-format=json"]); if let Some(target) = target { - let config = format!("target.{target}.linker = \"rust-lld\""); + #[cfg(target_os = "linux")] + let linker = if which("clang").is_ok() && which("lld").is_ok() { + // If clang and lld are available in the system, use them. + "clang" + } else { + // Otherwise, fall back to rust-lld. However, it's unaware of + // system library paths and therefore it might throw "unable to + // find library" errors. When experiencing that, better install an + // LLVM toolchain. + // + // See: https://github.com/rust-lang/rust/issues/130062 + // + // If the linked issue gets ever fixed, we could use rust-lld by + // default. + "rust-lld" + }; + #[cfg(not(target_os = "linux"))] + // On non-Linux systems, rust-lld should work fine. + let linker = "rust-lld"; + let config = format!("target.{target}.linker = \"{linker}\""); cmd.args(["--target", target, "--config", &config]); } f(&mut cmd);