jpegxl_src/
lib.rs

1/*
2 * This file is part of jpegxl-rs.
3 *
4 * jpegxl-rs is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * jpegxl-rs is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with jpegxl-rs.  If not, see <https://www.gnu.org/licenses/>.
16 */
17
18#![cfg_attr(coverage_nightly, feature(coverage_attribute))]
19
20use std::{
21    env,
22    path::{Path, PathBuf},
23};
24
25fn source_dir() -> PathBuf {
26    env::var("DEP_JXL_PATH").map_or_else(
27        |_| Path::new(env!("CARGO_MANIFEST_DIR")).join("libjxl"),
28        PathBuf::from,
29    )
30}
31
32#[cfg_attr(coverage_nightly, coverage(off))]
33pub fn build() {
34    let source = source_dir();
35
36    let mut config = cmake::Config::new(source);
37    config
38        .define("BUILD_TESTING", "OFF")
39        .define("BUILD_SHARED_LIBS", "OFF")
40        .define("JPEGXL_ENABLE_TOOLS", "OFF")
41        .define("JPEGXL_ENABLE_DOXYGEN", "OFF")
42        .define("JPEGXL_ENABLE_MANPAGES", "OFF")
43        .define("JPEGXL_ENABLE_BENCHMARK", "OFF")
44        .define("JPEGXL_ENABLE_EXAMPLES", "OFF")
45        .define("JPEGXL_ENABLE_JNI", "OFF")
46        .define("JPEGXL_ENABLE_SJPEG", "OFF")
47        .define("JPEGXL_ENABLE_OPENEXR", "OFF")
48        .define("JPEGXL_ENABLE_JPEGLI", "OFF")
49        .define("JPEGXL_BUNDLE_LIBPNG", "OFF");
50
51    if let Ok(p) = std::thread::available_parallelism() {
52        config.env("CMAKE_BUILD_PARALLEL_LEVEL", format!("{p}"));
53    }
54
55    #[cfg(target_os = "windows")]
56    {
57        config
58            .generator_toolset("ClangCL")
59            .define(
60                "CMAKE_VS_GLOBALS",
61                "UseMultiToolTask=true;EnforceProcessCountAcrossBuilds=true",
62            ) // Enable parallel builds
63            .define("CMAKE_MSVC_RUNTIME_LIBRARY", "MultiThreaded")
64            .define("CMAKE_EXE_LINKER_FLAGS", "MSVCRTD.lib")
65            .cflag("-Zl");
66    }
67
68    let mut prefix = config.build();
69    prefix.push("lib");
70    println!("cargo:rustc-link-search=native={}", prefix.display());
71    prefix.pop();
72    prefix.push("lib64");
73    println!("cargo:rustc-link-search=native={}", prefix.display());
74
75    println!("cargo:rustc-link-lib=static=jxl");
76    println!("cargo:rustc-link-lib=static=jxl_cms");
77    println!("cargo:rustc-link-lib=static=jxl_threads");
78
79    println!("cargo:rustc-link-lib=static=hwy");
80    println!("cargo:rustc-link-lib=static=brotlidec");
81    println!("cargo:rustc-link-lib=static=brotlienc");
82    println!("cargo:rustc-link-lib=static=brotlicommon");
83
84    #[cfg(any(target_vendor = "apple", target_os = "freebsd"))]
85    {
86        println!("cargo:rustc-link-lib=c++");
87    }
88    #[cfg(target_os = "linux")]
89    {
90        println!("cargo:rustc-link-lib=stdc++");
91    }
92}
93
94#[cfg(test)]
95mod tests {
96    use super::*;
97
98    #[test]
99    #[cfg_attr(coverage_nightly, coverage(off))]
100    fn test_source_dir() {
101        let mut path = source_dir();
102        assert!(path.is_dir());
103
104        path.push("lib/include/jxl/codestream_header.h");
105        assert!(path.exists());
106    }
107}