#![cfg_attr(coverage_nightly, feature(coverage_attribute))]
use core::panic;
use std::{
env,
path::{Path, PathBuf},
};
fn source_dir() -> PathBuf {
env::var("DEP_JXL_PATH").map_or_else(
|_| Path::new(env!("CARGO_MANIFEST_DIR")).join("libjxl"),
PathBuf::from,
)
}
#[cfg_attr(coverage_nightly, coverage(off))]
pub fn build() {
let source = source_dir();
assert!(
source.exists() && source.is_dir(),
"Source directory {} does not exist",
source.display()
);
let mut config = cmake::Config::new(source);
config
.define("BUILD_TESTING", "OFF")
.define("BUILD_SHARED_LIBS", "OFF")
.define("JPEGXL_ENABLE_TOOLS", "OFF")
.define("JPEGXL_ENABLE_DOXYGEN", "OFF")
.define("JPEGXL_ENABLE_MANPAGES", "OFF")
.define("JPEGXL_ENABLE_BENCHMARK", "OFF")
.define("JPEGXL_ENABLE_EXAMPLES", "OFF")
.define("JPEGXL_ENABLE_JNI", "OFF")
.define("JPEGXL_ENABLE_SJPEG", "OFF")
.define("JPEGXL_ENABLE_OPENEXR", "OFF")
.define("JPEGXL_ENABLE_JPEGLI", "OFF")
.define("JPEGXL_BUNDLE_LIBPNG", "OFF");
if let Ok(p) = std::thread::available_parallelism() {
config.env("CMAKE_BUILD_PARALLEL_LEVEL", format!("{p}"));
}
if cfg!(asan) {
config
.env("SANITIZER", "asan")
.cflag("-g -DADDRESS_SANITIZER -fsanitize=address")
.cxxflag("-g -DADDRESS_SANITIZER -fsanitize=address")
.define("JPEGXL_ENABLE_TCMALLOC", "OFF");
} else if cfg!(tsan) {
config
.env("SANITIZER", "tsan")
.cflag("-g -DTHREAD_SANITIZER -fsanitize=thread")
.cxxflag("-g -DTHREAD_SANITIZER -fsanitize=thread")
.define("JPEGXL_ENABLE_TCMALLOC", "OFF");
}
if cfg!(windows) {
assert!(!cfg!(tsan), "Thread sanitizer is not supported on Windows");
let exeflags = "MSVCRTD.lib".to_string();
config
.define(
"CMAKE_VS_GLOBALS",
"UseMultiToolTask=true;EnforceProcessCountAcrossBuilds=true",
) .define("CMAKE_MSVC_RUNTIME_LIBRARY", "MultiThreaded")
.define("CMAKE_EXE_LINKER_FLAGS", exeflags)
.cflag("/Zl");
}
let prefix = config.build();
let lib_dir = {
let mut lib_dir = prefix.join("lib");
if lib_dir.exists() {
lib_dir
} else {
lib_dir.pop();
lib_dir.push("lib64");
if lib_dir.exists() {
lib_dir
} else {
panic!(
"Could not find the library directory, please check the files in {prefix:?}"
);
}
}
};
println!("cargo:rustc-link-search=native={}", lib_dir.display());
println!("cargo:rustc-link-lib=static=jxl");
println!("cargo:rustc-link-lib=static=jxl_cms");
println!("cargo:rustc-link-lib=static=jxl_threads");
println!("cargo:rustc-link-lib=static=hwy");
println!("cargo:rustc-link-lib=static=brotlidec");
println!("cargo:rustc-link-lib=static=brotlienc");
println!("cargo:rustc-link-lib=static=brotlicommon");
if cfg!(any(target_vendor = "apple", target_os = "freebsd")) {
println!("cargo:rustc-link-lib=c++");
} else if cfg!(target_os = "linux") {
println!("cargo:rustc-link-lib=stdc++");
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[cfg_attr(coverage_nightly, coverage(off))]
fn test_source_dir() {
let mut path = source_dir();
assert!(path.is_dir());
path.push("lib/include/jxl/codestream_header.h");
assert!(path.exists());
}
}