Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor build.rs #16

Merged
merged 11 commits into from
Sep 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Somehow, libffi doesn't show up in --system-libs on both Linux and macOS...


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