From 6927d61a89e70622dd52caa920bd5011409633ed Mon Sep 17 00:00:00 2001 From: s1341 Date: Tue, 24 Dec 2024 11:00:44 +0200 Subject: [PATCH] libafl_cc: Automatically find llvm_ar path (#2790) --- libafl_cc/build.rs | 18 ++++++++++++++++++ libafl_cc/src/ar.rs | 8 +++----- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/libafl_cc/build.rs b/libafl_cc/build.rs index c602457e58..d58b8ca835 100644 --- a/libafl_cc/build.rs +++ b/libafl_cc/build.rs @@ -234,6 +234,7 @@ fn main() { println!("cargo:rerun-if-env-changed=LLVM_CONFIG"); println!("cargo:rerun-if-env-changed=LLVM_BINDIR"); + println!("cargo:rerun-if-env-changed=LLVM_AR_PATH"); println!("cargo:rerun-if-env-changed=LLVM_CXXFLAGS"); println!("cargo:rerun-if-env-changed=LLVM_LDFLAGS"); println!("cargo:rerun-if-env-changed=LLVM_VERSION"); @@ -244,6 +245,7 @@ fn main() { println!("cargo:rerun-if-changed=build.rs"); let llvm_bindir = env::var("LLVM_BINDIR"); + let llvm_ar_path = env::var("LLVM_AR_PATH"); let llvm_cxxflags = env::var("LLVM_CXXFLAGS"); let llvm_ldflags = env::var("LLVM_LDFLAGS"); let llvm_version = env::var("LLVM_VERSION"); @@ -266,6 +268,8 @@ fn main() { pub const CLANG_PATH: &str = \"clang\"; /// The path to the `clang++` executable pub const CLANGXX_PATH: &str = \"clang++\"; +/// The path to the `llvm-ar` executable +pub const LLVM_AR_PATH: &str = \"llvm-ar\"; /// The llvm version used to build llvm passes pub const LIBAFL_CC_LLVM_VERSION: Option = None; " @@ -281,16 +285,24 @@ pub const LIBAFL_CC_LLVM_VERSION: Option = None; exec_llvm_config(&["--bindir"]) }; let bindir_path = Path::new(&llvm_bindir); + let llvm_ar_path = if let Ok(ar_path) = llvm_ar_path { + ar_path + } else { + exec_llvm_config(&["--bindir"]) + }; let clang; let clangcpp; + let llvm_ar; if cfg!(windows) { clang = bindir_path.join("clang.exe"); clangcpp = bindir_path.join("clang++.exe"); + llvm_ar = Path::new(&llvm_ar_path).join("llvm-ar.exe"); } else { clang = bindir_path.join("clang"); clangcpp = bindir_path.join("clang++"); + llvm_ar = Path::new(&llvm_ar_path).join("llvm-ar"); } if !clang.exists() { @@ -302,6 +314,10 @@ pub const LIBAFL_CC_LLVM_VERSION: Option = None; println!("cargo:warning=Failed to find clang++ frontend."); return; } + if !llvm_ar.exists() { + println!("cargo:warning=Failed to find llvm-ar archiver."); + return; + } let cxxflags = if let Ok(flags) = llvm_cxxflags { flags @@ -344,6 +360,8 @@ pub const LIBAFL_CC_LLVM_VERSION: Option = None; pub const CLANG_PATH: &str = {clang:?}; /// The path to the `clang++` executable pub const CLANGXX_PATH: &str = {clangcpp:?}; + /// The path to the `llvm-ar` executable + pub const LLVM_AR_PATH: &str = {llvm_ar:?}; /// The default size of the edges map the fuzzer uses pub const EDGES_MAP_DEFAULT_SIZE: usize = {edge_map_default_size}; diff --git a/libafl_cc/src/ar.rs b/libafl_cc/src/ar.rs index 373d6b4b2c..50eeb3e68b 100644 --- a/libafl_cc/src/ar.rs +++ b/libafl_cc/src/ar.rs @@ -5,6 +5,8 @@ use std::{env, path::PathBuf, str::FromStr}; use crate::{Error, ToolWrapper, LIB_EXT, LIB_PREFIX}; +include!(concat!(env!("OUT_DIR"), "/clang_constants.rs")); + /// Wrap Clang #[expect(clippy::struct_excessive_bools)] #[derive(Debug)] @@ -184,11 +186,7 @@ impl ToolWrapper for ArWrapper { }) .collect::>(); - let Ok(ar_path) = env::var("LLVM_AR_PATH") else { - panic!("Couldn't find llvm-ar. Specify the `LLVM_AR_PATH` environment variable"); - }; - - args.push(ar_path); + args.push(LLVM_AR_PATH.to_string()); args.extend_from_slice(base_args.as_slice());