fn emit_cfg_alias(alias: &str, features: &[&str]) {
let any_enabled = features.iter().any(|f| {
let var = format!("CARGO_FEATURE_{}", f.to_uppercase().replace('-', "_"));
std::env::var(var).is_ok()
});
if any_enabled {
println!("cargo::rustc-cfg={alias}");
}
}
fn main() {
emit_cfg_alias(
"any_raw",
&[
"arw-decode",
"cr2-decode",
"cr3-decode",
"crw-decode",
"dng-decode",
"nef-decode",
"raf-decode",
],
);
emit_cfg_alias(
"any_standard_decode",
&[
"gif-decode",
"jpeg-decode",
"png-decode",
"webp-decode",
"jxl-decode",
"tiff-decode",
"avif-decode",
"heic-decode",
"svg-decode",
"ppm-decode",
],
);
emit_cfg_alias(
"any_standard_encode",
&[
"png-encode",
"jpeg-encode",
"webp-encode",
"avif-encode",
"jxl-encode",
"dng-encode",
],
);
#[cfg(feature = "jxl-encode-libjxl")]
libjxl::generate();
}
#[cfg(feature = "jxl-encode-libjxl")]
mod libjxl {
use std::env;
use std::path::{Path, PathBuf};
pub fn generate() {
let out_dir = PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR is set by cargo"));
let include_dirs = link_and_include_dirs(&out_dir);
let mut builder = bindgen::Builder::default()
.rust_edition(bindgen::RustEdition::Edition2024)
.header_contents(
"rawshift_jxl_wrapper.h",
"#include <jxl/encode.h>\n#include <jxl/resizable_parallel_runner.h>\n",
)
.allowlist_function("Jxl(Encoder|ColorEncoding|ResizableParallelRunner).*")
.allowlist_type("Jxl.*")
.allowlist_var("JXL_.*")
.default_enum_style(bindgen::EnumVariation::Consts)
.prepend_enum_name(false)
.layout_tests(false)
.generate_comments(false)
.merge_extern_blocks(true);
for dir in &include_dirs {
builder = builder.clang_arg(format!("-I{}", dir.display()));
}
let bindings = builder
.generate()
.expect("bindgen failed to generate libjxl bindings");
bindings
.write_to_file(out_dir.join("jxl_bindings.rs"))
.expect("failed to write jxl_bindings.rs");
println!("cargo:rerun-if-changed=build.rs");
}
#[cfg(feature = "jxl-encode-libjxl-vendored")]
fn link_and_include_dirs(out_dir: &Path) -> Vec<PathBuf> {
jpegxl_src::build();
vec![out_dir.join("include")]
}
#[cfg(not(feature = "jxl-encode-libjxl-vendored"))]
fn link_and_include_dirs(_out_dir: &Path) -> Vec<PathBuf> {
let probe = |lib: &str| {
pkg_config::Config::new()
.atleast_version("0.10")
.probe(lib)
.unwrap_or_else(|e| {
panic!(
"could not find system `{lib}` via pkg-config ({e}); install \
libjxl development files or enable the \
`jxl-encode-libjxl-vendored` feature to build it from source"
)
})
};
let mut dirs = probe("libjxl").include_paths;
dirs.extend(probe("libjxl_threads").include_paths);
dirs
}
}