Skip to content

Commit

Permalink
libafl_cc: Automatically find llvm_ar path (#2790)
Browse files Browse the repository at this point in the history
  • Loading branch information
s1341 authored Dec 24, 2024
1 parent 54202c3 commit 6927d61
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
18 changes: 18 additions & 0 deletions libafl_cc/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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");
Expand All @@ -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<usize> = None;
"
Expand All @@ -281,16 +285,24 @@ pub const LIBAFL_CC_LLVM_VERSION: Option<usize> = 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() {
Expand All @@ -302,6 +314,10 @@ pub const LIBAFL_CC_LLVM_VERSION: Option<usize> = 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
Expand Down Expand Up @@ -344,6 +360,8 @@ pub const LIBAFL_CC_LLVM_VERSION: Option<usize> = 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};
Expand Down
8 changes: 3 additions & 5 deletions libafl_cc/src/ar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -184,11 +186,7 @@ impl ToolWrapper for ArWrapper {
})
.collect::<Vec<_>>();

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

Expand Down

0 comments on commit 6927d61

Please sign in to comment.