Skip to content

Commit

Permalink
Merge pull request #16 from edg-l/chore/refactor-ci
Browse files Browse the repository at this point in the history
Refactor `build.rs`
  • Loading branch information
edg-l authored Sep 25, 2024
2 parents 1415a4f + 5089b04 commit 3d94eef
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
matrix:
os:
- ubuntu-latest
- macos-14
- macos-latest
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
Expand Down
47 changes: 18 additions & 29 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ use std::{
str,
};

#[cfg(feature = "llvm16-0")]
const LLVM_MAJOR_VERSION: usize = 16;
#[cfg(feature = "llvm17-0")]
const LLVM_MAJOR_VERSION: usize = 17;
#[cfg(feature = "llvm18-0")]
const LLVM_MAJOR_VERSION: usize = 18;
#[cfg(feature = "llvm19-0")]
const LLVM_MAJOR_VERSION: usize = 19;
const LLVM_MAJOR_VERSION: usize = if cfg!(feature = "llvm16-0") {
16
} else if cfg!(feature = "llvm17-0") {
17
} else if cfg!(feature = "llvm18-0") {
18
} else {
19
};

fn main() {
if let Err(error) = run() {
Expand All @@ -36,14 +37,9 @@ fn run() -> Result<(), Box<dyn Error>> {
println!("cargo:rerun-if-changed=wrapper.h");
println!("cargo:rerun-if-changed=cc");
println!("cargo:rustc-link-search={}", llvm_config("--libdir")?);
println!("cargo:rustc-link-lib=static=LLVMCore");
println!("cargo:rustc-link-lib=static=LLVMSupport");
println!("cargo:rustc-link-lib=static=LLVMTableGen");

for name in llvm_config("--libnames")?.trim().split(' ') {
if let Some(name) = trim_library_name(name) {
println!("cargo:rustc-link-lib={}", name);
}
println!("cargo:rustc-link-lib=static={}", parse_library_name(name)?);
}

for flag in llvm_config("--system-libs")?.trim().split(' ') {
Expand All @@ -59,20 +55,15 @@ fn run() -> Result<(), Box<dyn Error>> {
);
println!(
"cargo:rustc-link-lib={}",
path.file_name()
.unwrap()
.to_str()
.unwrap()
.split_once('.')
.unwrap()
.0
.trim_start_matches("lib")
parse_library_name(path.file_name().unwrap().to_str().unwrap())?
);
} else {
println!("cargo:rustc-link-lib={}", flag);
}
}

println!("cargo:rustc-link-lib=ffi");

if let Some(name) = get_system_libcpp() {
println!("cargo:rustc-link-lib={}", name);
}
Expand Down Expand Up @@ -100,7 +91,7 @@ fn run() -> Result<(), Box<dyn Error>> {

bindgen::builder()
.header("wrapper.h")
.clang_arg(format!("-I{}", "cc/include"))
.clang_arg("-Icc/include")
.clang_arg(format!("-I{}", llvm_config("--includedir")?))
.default_enum_style(bindgen::EnumVariation::ModuleConsts)
.parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
Expand Down Expand Up @@ -143,10 +134,8 @@ fn llvm_config(argument: &str) -> Result<String, Box<dyn Error>> {
.to_string())
}

fn trim_library_name(name: &str) -> Option<&str> {
if let Some(name) = name.strip_prefix("lib") {
name.strip_suffix(".a")
} else {
None
}
fn parse_library_name(name: &str) -> Result<&str, String> {
name.strip_prefix("lib")
.and_then(|name| name.split('.').next())
.ok_or_else(|| format!("failed to parse library name: {}", name))
}
8 changes: 5 additions & 3 deletions tools/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ set -e
llvm_version=19

brew update
brew install llvm@$llvm_version z3
brew install llvm@$llvm_version

echo TABLEGEN_190_PREFIX=$(brew --prefix)/opt/llvm@$llvm_version >>$GITHUB_ENV
echo PATH=$(brew --prefix)/opt/llvm@$llvm_version/bin:$PATH >>$GITHUB_ENV
llvm_prefix=$(brew --prefix llvm@$llvm_version)

echo TABLEGEN_190_PREFIX=$llvm_prefix >>$GITHUB_ENV
echo PATH=$llvm_prefix/bin:$PATH >>$GITHUB_ENV
echo LIBRARY_PATH=$(brew --prefix)/lib:$LIBRARY_PATH >>$GITHUB_ENV
echo LD_LIBRARY_PATH=$(brew --prefix)/lib:$LD_LIBRARY_PATH >>$GITHUB_ENV

0 comments on commit 3d94eef

Please sign in to comment.