ultrahdr-sys 0.1.4

Raw FFI bindings to libultrahdr
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
use std::env;
use std::fs;
use std::io;
use std::path::{Path, PathBuf};
use std::process::Command;

fn copy_dir_recursive(src: &Path, dst: &Path) -> io::Result<()> {
    fs::create_dir_all(dst)?;
    for entry in fs::read_dir(src)? {
        let entry = entry?;
        let file_type = entry.file_type()?;
        let file_name = entry.file_name();
        if file_name.to_str() == Some(".git") {
            continue;
        }
        let src_path = entry.path();
        let dst_path = dst.join(file_name);
        if file_type.is_dir() {
            copy_dir_recursive(&src_path, &dst_path)?;
        } else if file_type.is_file() {
            fs::copy(&src_path, &dst_path)?;
        } else if file_type.is_symlink() {
            let target = fs::read_link(&src_path)?;
            #[cfg(unix)]
            {
                std::os::unix::fs::symlink(&target, &dst_path)?;
            }
            #[cfg(windows)]
            {
                if target.is_dir() {
                    std::os::windows::fs::symlink_dir(&target, &dst_path)?;
                } else {
                    std::os::windows::fs::symlink_file(&target, &dst_path)?;
                }
            }
        }
    }
    Ok(())
}

fn wasi_toolchain() -> Option<(PathBuf, PathBuf)> {
    let target = env::var("TARGET").ok()?;
    if !target.contains("wasm32-wasi") {
        return None;
    }

    let prefix = env::var("WASI_SDK_PREFIX")
        .or_else(|_| env::var("WASI_SDK_PATH"))
        .unwrap_or_else(|_| "/opt/wasi-sdk".to_string());
    let env_toolchain = env::var_os("WASI_SDK_TOOLCHAIN_FILE").map(PathBuf::from);
    let target_env = env::var("CARGO_CFG_TARGET_ENV").unwrap_or_default();
    let toolchain = env_toolchain.unwrap_or_else(|| {
        let name = if target.contains("wasip1") || target_env == "p1" {
            "wasi-sdk-p1.cmake"
        } else {
            "wasi-sdk.cmake"
        };
        PathBuf::from(&prefix).join("share/cmake").join(name)
    });
    Some((toolchain, PathBuf::from(prefix)))
}

fn locate_src_dir(manifest_dir: &Path) -> PathBuf {
    if let Ok(env) = env::var("ULTRAHDR_SRC_DIR") {
        return PathBuf::from(env);
    }

    let submodule_path = manifest_dir.join("libultrahdr");
    if submodule_path.join("CMakeLists.txt").is_file() {
        return submodule_path;
    }

    let workspace_root = manifest_dir
        .parent()
        .expect("ultrahdr-sys has no parent dir");

    let submodule_path = workspace_root.join("libultrahdr");
    if submodule_path.join("CMakeLists.txt").is_file() {
        return submodule_path;
    }

    // Fallback: sibling checkout (old layout).
    workspace_root
        .parent()
        .expect("workspace has no parent dir")
        .join("libultrahdr")
}

fn apply_patch_once(src_dir: &Path, patch_path: &Path) {
    if !patch_path.is_file() {
        return;
    }

    let patch_str = patch_path
        .to_str()
        .expect("patch path contains non-UTF8 characters");

    // If patch applies in reverse, assume already applied.
    // NOTE: `git apply` doesn't work on crates.io source tarballs (no `.git`), so we use `patch`.
    // `patch -R` may auto-detect and ignore `-R`, so we add `--force` to make the exit status reliable.
    let reverse_ok = Command::new("patch")
        .current_dir(src_dir)
        .args(["-p1", "--dry-run", "--batch", "--silent", "--force", "-R"])
        .args(["-i", patch_str])
        .output()
        .map(|out| out.status.success())
        .unwrap_or(false);
    if reverse_ok {
        return;
    }

    let dry_run = Command::new("patch")
        .current_dir(src_dir)
        .args(["-p1", "--dry-run", "--batch", "--silent", "--forward"])
        .args(["-i", patch_str])
        .status()
        .expect("failed to execute patch (is `patch` installed?)");
    if !dry_run.success() {
        panic!(
            "failed to apply patch {} in {} (dry-run); set ULTRAHDR_SKIP_PATCHES=1 to bypass",
            patch_path.display(),
            src_dir.display()
        );
    }

    let status = Command::new("patch")
        .current_dir(src_dir)
        .args(["-p1", "--batch", "--silent", "--forward"])
        .args(["-i", patch_str])
        .status()
        .expect("failed to execute patch (is `patch` installed?)");
    if !status.success() {
        panic!(
            "failed to apply patch {} in {}",
            patch_path.display(),
            src_dir.display()
        );
    }
}

fn apply_local_patches(manifest_dir: &Path, src_dir: &Path) {
    if env::var("ULTRAHDR_SKIP_PATCHES").is_ok() {
        return;
    }
    apply_patch_once(
        src_dir,
        &manifest_dir.join("patches/libultrahdr-no-threads.patch"),
    );
}

fn prepare_src_dir(manifest_dir: &Path, src_dir: &Path, out_dir: &Path) -> PathBuf {
    let work_src = out_dir.join("libultrahdr-src");
    let _ = fs::remove_dir_all(out_dir.join("build"));
    let _ = fs::remove_dir_all(&work_src);
    copy_dir_recursive(src_dir, &work_src).expect("failed to copy libultrahdr sources");
    apply_local_patches(manifest_dir, &work_src);
    work_src
}

fn is_wasm_target(target: &str) -> bool {
    target.starts_with("wasm32-")
}

fn main() {
    let manifest_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR"));
    let source_dir = locate_src_dir(&manifest_dir);
    let out_dir = PathBuf::from(env::var("OUT_DIR").expect("OUT_DIR"));

    // docs.rs builds in a sandboxed environment without network access.
    // Skip the full CMake build and only generate bindings for documentation.
    if env::var("DOCS_RS").is_ok() {
        println!("cargo:warning=Building for docs.rs: skipping CMake build");
        let bindings = bindgen::Builder::default()
            .header(source_dir.join("ultrahdr_api.h").to_string_lossy())
            .clang_arg(format!("-I{}", source_dir.display()))
            .rustified_enum("uhdr_.*")
            .parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
            .layout_tests(false)
            .allowlist_function("uhdr_.*")
            .allowlist_type("uhdr_.*")
            .allowlist_var("UHDR_.*")
            .generate()
            .expect("bindgen failed");
        bindings
            .write_to_file(out_dir.join("bindings.rs"))
            .expect("failed to write bindings");
        return;
    }

    if !source_dir.join("CMakeLists.txt").is_file() {
        panic!(
            "Could not find libultrahdr sources; set ULTRAHDR_SRC_DIR (current: {})",
            source_dir.display()
        );
    }

    let patch_path = manifest_dir.join("patches/libultrahdr-no-threads.patch");
    println!("cargo:rerun-if-env-changed=ULTRAHDR_SRC_DIR");
    println!("cargo:rerun-if-env-changed=ULTRAHDR_SKIP_PATCHES");
    println!("cargo:rerun-if-env-changed=WASI_SDK_PREFIX");
    println!("cargo:rerun-if-env-changed=WASI_SDK_PATH");
    println!("cargo:rerun-if-env-changed=WASI_SDK_TOOLCHAIN_FILE");
    println!(
        "cargo:rerun-if-changed={}",
        source_dir.join("ultrahdr_api.h").display()
    );
    println!(
        "cargo:rerun-if-changed={}",
        source_dir.join("CMakeLists.txt").display()
    );
    println!("cargo:rerun-if-changed={}", patch_path.display());

    let src_dir = prepare_src_dir(&manifest_dir, &source_dir, &out_dir);

    let target = env::var("TARGET").expect("TARGET");
    let target_env = env::var("CARGO_CFG_TARGET_ENV").unwrap_or_default();
    let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap_or_default();
    let is_wasm = is_wasm_target(&target);
    let wasi = wasi_toolchain();

    let mut cfg = cmake::Config::new(&src_dir);
    cfg.profile("Release");
    if let Some((toolchain, prefix)) = &wasi {
        if !toolchain.is_file() {
            panic!(
                "WASI CMake toolchain file not found: {}",
                toolchain.display()
            );
        }
        cfg.define(
            "CMAKE_TOOLCHAIN_FILE",
            toolchain
                .to_str()
                .expect("toolchain path contains non-UTF8 characters"),
        );
        cfg.define(
            "WASI_SDK_PREFIX",
            prefix
                .to_str()
                .expect("WASI_SDK_PREFIX contains non-UTF8 characters"),
        );
        cfg.define("CMAKE_TRY_COMPILE_TARGET_TYPE", "STATIC_LIBRARY");
        cfg.define("CMAKE_SYSTEM_NAME", "WASI");
        cfg.define("CMAKE_SYSTEM_PROCESSOR", "wasm32");
    }
    if is_wasm && cfg!(feature = "shared") {
        panic!("shared linking is not supported for wasm32 targets");
    }
    if is_wasm && cfg!(feature = "gles") {
        panic!("gles feature is not supported for wasm32 targets");
    }

    let build_shared = cfg!(feature = "shared") && !is_wasm;
    let disable_threads = cfg!(feature = "no-threads") || is_wasm;

    cfg.define("UHDR_BUILD_EXAMPLES", "OFF");
    cfg.define("UHDR_BUILD_TESTS", "OFF");
    cfg.define("UHDR_BUILD_BENCHMARK", "OFF");
    cfg.define("UHDR_BUILD_FUZZERS", "OFF");
    cfg.define("UHDR_BUILD_JAVA", "OFF");
    cfg.define("UHDR_ENABLE_INSTALL", "OFF");
    cfg.define(
        "UHDR_BUILD_DEPS",
        if cfg!(feature = "vendored") {
            "ON"
        } else {
            "OFF"
        },
    );
    cfg.define("BUILD_SHARED_LIBS", if build_shared { "ON" } else { "OFF" });

    if cfg!(feature = "gles") {
        cfg.define("UHDR_ENABLE_GLES", "ON");
    }
    // Control ISO 21496-1 metadata emission via feature flag (default ON).
    cfg.define(
        "UHDR_WRITE_ISO",
        if cfg!(feature = "iso21496") {
            "ON"
        } else {
            "OFF"
        },
    );
    if disable_threads {
        cfg.define("UHDR_DISABLE_THREADS", "ON");
    }
    if cfg!(feature = "jpeg-max-dimension") {
        // Use libjpeg-turbo's hardcoded JPEG_MAX_DIMENSION (65500).
        cfg.define("UHDR_MAX_DIMENSION", "65500");
    }

    // Build only the main library target; install target is disabled upstream.
    let cmake_target = if target_env == "msvc" && !build_shared {
        "uhdr-static"
    } else {
        "uhdr"
    };
    cfg.build_target(cmake_target);

    let dst = cfg.build();
    // Link search paths (CMake binary dir holds libs when install is disabled).
    println!("cargo:rustc-link-search=native={}/lib", dst.display());
    println!("cargo:rustc-link-search=native={}/lib64", dst.display());
    println!("cargo:rustc-link-search=native={}/build", dst.display());
    if target_env == "msvc" {
        println!(
            "cargo:rustc-link-search=native={}/build/Release",
            dst.display()
        );
        println!(
            "cargo:rustc-link-search=native={}/build/Debug",
            dst.display()
        );
    }

    if cfg!(feature = "vendored") {
        println!(
            "cargo:rustc-link-search=native={}/build/turbojpeg/src/turbojpeg-build",
            dst.display()
        );
        if target_env == "msvc" {
            println!(
                "cargo:rustc-link-search=native={}/build/turbojpeg/src/turbojpeg-build/Release",
                dst.display()
            );
            println!(
                "cargo:rustc-link-search=native={}/build/turbojpeg/src/turbojpeg-build/Debug",
                dst.display()
            );
        }
        let jpeg_name = if target_env == "msvc" {
            "jpeg-static"
        } else {
            "jpeg"
        };
        println!("cargo:rustc-link-lib=static={}", jpeg_name);
    } else {
        println!("cargo:rustc-link-lib=jpeg");
    }

    let link_name = if target_env == "msvc" && !build_shared {
        "uhdr-static"
    } else {
        "uhdr"
    };
    let link_kind = if build_shared { "dylib" } else { "static" };
    println!("cargo:rustc-link-lib={}={}", link_kind, link_name);
    if target_env != "msvc" {
        if is_wasm {
            if let Some((_, prefix)) = &wasi {
                println!(
                    "cargo:rustc-link-search=native={}/share/wasi-sysroot/lib/wasm32-wasip1",
                    prefix.display()
                );
                println!("cargo:rustc-link-lib=static=c++");
                println!("cargo:rustc-link-lib=static=c++abi");
                println!("cargo:rustc-link-lib=static=setjmp");
            }
        } else {
            let cxx_stdlib = if target_os == "macos" {
                "c++"
            } else {
                "stdc++"
            };
            println!("cargo:rustc-link-lib={}", cxx_stdlib);
        }
    }

    let bindgen_target = if is_wasm {
        "i686-unknown-linux-gnu"
    } else {
        target.as_str()
    };

    let mut bindings = bindgen::Builder::default()
        .header(source_dir.join("ultrahdr_api.h").to_string_lossy())
        .clang_arg(format!("-I{}", source_dir.display()))
        .rustified_enum("uhdr_.*")
        .parse_callbacks(Box::new(bindgen::CargoCallbacks::new()))
        .layout_tests(false)
        .clang_arg(format!("--target={}", bindgen_target));
    if !is_wasm {
        bindings = bindings
            .allowlist_function("uhdr_.*")
            .allowlist_type("uhdr_.*")
            .allowlist_var("UHDR_.*");
    }
    if !is_wasm && let Some((_, prefix)) = &wasi {
        bindings = bindings.clang_arg(format!("--sysroot={}/share/wasi-sysroot", prefix.display()));
    }
    let bindings = bindings.generate().expect("bindgen failed");

    let bindings_path = out_dir.join("bindings.rs");
    bindings
        .write_to_file(&bindings_path)
        .expect("failed to write bindings");
    if is_wasm && let Ok(content) = fs::read_to_string(&bindings_path) {
        let fn_count = content.matches("pub fn ").count();
        if fn_count == 0 {
            println!("cargo:warning=bindgen generated 0 functions for wasm target");
        }
    }
}