110 lines
3.8 KiB
Rust

//! Build script for coolprop-sys.
//!
//! This compiles the CoolProp C++ library statically.
//! Supports macOS, Linux, and Windows.
use std::env;
use std::path::PathBuf;
fn coolprop_src_path() -> Option<PathBuf> {
// Try to find CoolProp source in common locations
let possible_paths = vec![
// Vendor directory (recommended)
PathBuf::from("../../vendor/coolprop")
.canonicalize()
.unwrap_or(PathBuf::from("../../../vendor/coolprop")),
// External directory
PathBuf::from("external/coolprop"),
// System paths (Unix)
PathBuf::from("/usr/local/src/CoolProp"),
PathBuf::from("/opt/CoolProp"),
];
possible_paths
.into_iter()
.find(|path| path.join("CMakeLists.txt").exists())
}
fn main() {
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap_or_default();
// Check if CoolProp source is available
if let Some(coolprop_path) = coolprop_src_path() {
println!("cargo:rerun-if-changed={}", coolprop_path.display());
// Build CoolProp using CMake
let dst = cmake::Config::new(&coolprop_path)
.define("COOLPROP_SHARED_LIBRARY", "OFF")
.define("COOLPROP_STATIC_LIBRARY", "ON")
.define("COOLPROP_CATCH_TEST", "OFF")
.define("COOLPROP_C_LIBRARY", "ON")
.define("COOLPROP_MY_IFCO3_WRAPPER", "OFF")
.build();
println!("cargo:rustc-link-search=native={}/build", dst.display());
println!("cargo:rustc-link-search=native={}/lib", dst.display());
println!(
"cargo:rustc-link-search=native={}/build",
coolprop_path.display()
); // Fallback
// Link against CoolProp statically
println!("cargo:rustc-link-lib=static=CoolProp");
// On macOS, force load the static library so its symbols are exported in the final cdylib
if target_os == "macos" {
println!(
"cargo:rustc-link-arg=-Wl,-force_load,{}/build/libCoolProp.a",
dst.display()
);
}
} else {
println!(
"cargo:warning=CoolProp source not found in vendor/. \
For full static build, run: \
git clone https://github.com/CoolProp/CoolProp.git vendor/coolprop"
);
// Fallback for system library
if target_os == "windows" {
// On Windows, try to find CoolProp as a system library
println!("cargo:rustc-link-lib=CoolProp");
} else {
println!("cargo:rustc-link-lib=static=CoolProp");
}
}
// Link required system libraries for C++ standard library
match target_os.as_str() {
"macos" => {
println!("cargo:rustc-link-lib=dylib=c++");
}
"linux" | "freebsd" | "openbsd" | "netbsd" => {
println!("cargo:rustc-link-lib=dylib=stdc++");
}
"windows" => {
// MSVC links the C++ runtime automatically; nothing to do.
// For MinGW, stdc++ is needed but MinGW is less common.
}
_ => {
// Best guess for unknown Unix-like targets
println!("cargo:rustc-link-lib=dylib=stdc++");
}
}
// Link libm (only on Unix; on Windows it's part of the CRT)
if target_os != "windows" {
println!("cargo:rustc-link-lib=dylib=m");
}
// Force export symbols for Python extension (macOS only)
if target_os == "macos" {
println!("cargo:rustc-link-arg=-Wl,-all_load");
}
// Linux equivalent (only for shared library builds, e.g., Python wheels)
// Note: --whole-archive must bracket the static lib; the linker handles this
// automatically for Rust cdylib targets, so we don't need it here.
// Tell Cargo to rerun if build.rs changes
println!("cargo:rerun-if-changed=build.rs");
}