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
|
use std::io;
use std::path::{Path, PathBuf};
fn main() {
if std::env::var("DOCS_RS").is_ok() {
// Don't link with libheif in case of building documentation for docs.rs.
return;
}
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=wrapper.h");
// Tell cargo to tell rustc to link the heif library.
#[cfg(not(target_os = "windows"))]
#[allow(unused_variables)]
let include_paths = find_libheif();
#[cfg(target_os = "windows")]
#[allow(unused_variables)]
let include_paths: Vec<String> = Vec::new();
#[cfg(target_os = "windows")]
install_libheif_by_vcpkg();
#[cfg(feature = "use-bindgen")]
run_bindgen(&include_paths);
}
#[allow(dead_code)]
fn prepare_libheif_src() -> PathBuf {
let out_path = PathBuf::from(std::env::var("OUT_DIR").unwrap());
let crate_dir = PathBuf::from(std::env::var("CARGO_MANIFEST_DIR").unwrap());
let libheif_dir = crate_dir.join("vendor/libheif");
let dst_dir = out_path.join("libheif");
copy_dir_all(libheif_dir, &dst_dir).unwrap();
// Patch CMakeLists.txt to disable a building `heifio` library
// that is used for example applications.
let cmake_lists_path = dst_dir.join("CMakeLists.txt");
let mut contents =
std::fs::read_to_string(&cmake_lists_path).expect("failed to read libheif/CMakeLists.txt");
contents = contents.replace("add_subdirectory(heifio)", "");
std::fs::write(&cmake_lists_path, contents).expect("failed to write libheif/CMakeLists.txt");
dst_dir
}
#[cfg(feature = "embedded-libheif")]
fn compile_libheif() -> String {
use std::path::PathBuf;
let out_path = PathBuf::from(std::env::var("OUT_DIR").unwrap());
let libheif_dir = prepare_libheif_src();
let mut build_config = cmake::Config::new(libheif_dir);
build_config.out_dir(out_path.join("libheif_build"));
build_config.define("CMAKE_INSTALL_LIBDIR", "lib");
// Disable some options
for key in [
"BUILD_SHARED_LIBS",
"BUILD_TESTING",
"WITH_GDK_PIXBUF",
"WITH_EXAMPLES",
"ENABLE_EXPERIMENTAL_FEATURES",
"ENABLE_PLUGIN_LOADING",
] {
build_config.define(key, "OFF");
}
// Enable some options
for key in [
"WITH_REDUCED_VISIBILITY",
"WITH_LIBSHARPYUV",
// TODO: Try to enable this in the future.
// Right now it is the reason of linker's errors like
// "undefined reference to `BrotliEncoderDestroyInstance'"
// "WITH_UNCOMPRESSED_CODEC",
// "WITH_HEADER_COMPRESSION",
] {
build_config.define(key, "ON");
}
// List of encoders and decoders that have corresponding plugins
let encoders_decoders = [
"AOM_DECODER",
"AOM_ENCODER",
"DAV1D",
"LIBDE265",
"RAV1E",
"SvtEnc",
"X265",
"JPEG_DECODER",
"JPEG_ENCODER",
"KVAZAAR",
"OPENJPH_ENCODER",
"OpenJPEG_DECODER",
"OpenJPEG_ENCODER",
"FFMPEG_DECODER",
"OpenH264_DECODER",
"UVG266",
"VVDEC",
"VVENC",
];
let disabled_enc_dec = [
// A lot of errors like "undefined reference to `av_packet_free'" during linking
"FFMPEG_DECODER",
];
// Enable or disable encoders and decoders
for key in encoders_decoders {
let v = if disabled_enc_dec.contains(&key) {
"OFF"
} else {
"ON"
};
build_config.define(format!("WITH_{}", key), v);
// Disable external plugin
build_config.define(format!("WITH_{}_PLUGIN", key), "OFF");
}
let libheif_build = build_config.build();
libheif_build
.join("lib/pkgconfig")
.to_string_lossy()
.to_string()
}
#[cfg(not(target_os = "windows"))]
fn find_libheif() -> Vec<String> {
#[allow(unused_mut)]
let mut config = system_deps::Config::new();
#[cfg(feature = "embedded-libheif")]
{
std::env::set_var("SYSTEM_DEPS_LIBHEIF_BUILD_INTERNAL", "always");
config = config.add_build_internal("libheif", |lib, version| {
let pc_file_path = compile_libheif();
system_deps::Library::from_internal_pkg_config(pc_file_path, lib, version)
});
}
use system_deps::Error;
match config.probe() {
Ok(deps) => deps
.all_include_paths()
.iter()
.filter_map(|p| p.to_str())
.map(|p| p.to_string())
.collect(),
Err(err) => {
let err_msg = match &err {
Error::InvalidMetadata(msg) => {
if msg.contains("No version") && msg.contains("libheif") {
"You MUST enable one of the crate features to specify \
minimal supported version of 'libheif' API (e.g. v1_17)."
.to_string()
} else {
err.to_string()
}
}
_ => err.to_string(),
};
println!("cargo:error={err_msg}");
std::process::exit(1);
}
}
}
#[cfg(target_os = "windows")]
fn install_libheif_by_vcpkg() {
let vcpkg_lib = vcpkg::Config::new()
.emit_includes(true)
.find_package("libheif");
if let Err(err) = vcpkg_lib {
println!("cargo:warning={}", err);
std::process::exit(1);
}
}
#[cfg(feature = "use-bindgen")]
fn run_bindgen(include_paths: &[String]) {
let mut base_builder = bindgen::Builder::default()
.header("wrapper.h")
.generate_comments(true)
.formatter(bindgen::Formatter::Rustfmt)
.generate_cstr(true)
.disable_name_namespacing()
.array_pointers_in_arguments(true)
.ctypes_prefix("libc")
.allowlist_function("heif_.*")
.allowlist_type("heif_.*")
.size_t_is_usize(true)
.clang_args([
"-fparse-all-comments",
"-fretain-comments-from-system-headers",
]);
for path in include_paths {
base_builder = base_builder.clang_arg(format!("-I{path}"));
}
// Don't derive Copy and Clone for structures with pointers
// and which represents shared_ptr from C++.
for struct_name in [
"heif_plugin_info",
"heif_decoding_options",
"heif_encoding_options",
"heif_property_user_description",
"heif_reader_range_request_result",
"heif_entity_group",
"heif_depth_representation_info",
"heif_camera_extrinsic_matrix",
"heif_track",
"heif_raw_sequence_sample",
"heif_track_options",
"heif_sequence_encoding_options",
"heif_context",
"heif_image_handle",
"heif_decoder_plugin",
"heif_encoder_plugin",
"heif_image",
"heif_scaling_options",
"heif_encoder",
"heif_reading_options",
"heif_encoder_descriptor",
"heif_encoder_parameter",
"heif_decoder_descriptor",
"heif_region_item",
"heif_region",
] {
base_builder = base_builder.no_copy(struct_name);
}
// The main module
// Finish the builder and generate the bindings.
let bindings = base_builder
.clone()
.generate()
.expect("Unable to generate bindings");
// Write the bindings to the $OUT_DIR/bindings.rs file.
let out_path = PathBuf::from(std::env::var("OUT_DIR").unwrap());
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings.rs!");
// Create linker_test.rs module for testing cases when not all
// functions from *.h files are really available in libheif.
let code = bindings.to_string();
let mut func_names = Vec::new();
for line in code.lines() {
if !line.contains("pub fn ") {
continue;
}
let line = line.trim();
let res: Vec<&str> = line.split(&[' ', '(']).collect();
if res.len() > 3 {
if let &["pub", "fn", name] = &res[..3] {
func_names.push(name)
}
}
}
let mut result = vec![
"use super::*;\n\n",
"#[test]\n",
"fn is_all_functions_exists_in_libheif() {\n",
" let fn_pointers = [\n",
];
for name in func_names {
result.push(" ");
result.push(name);
result.push(" as *const fn(),\n")
}
result.extend(vec![
" ];\n",
" for pointer in fn_pointers.iter() {\n",
" assert!(!pointer.is_null());\n",
" }\n",
"}\n",
]);
let test_module = result.join("");
let test_path = out_path.join("linker_test.rs");
std::fs::write(&test_path, test_module).expect("Couldn't write test module!");
let bindings = base_builder
.layout_tests(false)
.generate()
.expect("Unable to generate bindings without tests");
bindings
.write_to_file(out_path.join("bindings_wo_tests.rs"))
.expect("Couldn't write bindings_wo_tests.rs!");
}
fn copy_dir_all(src: impl AsRef<Path>, dst: impl AsRef<Path>) -> io::Result<()> {
std::fs::create_dir_all(&dst)?;
for entry in std::fs::read_dir(src)? {
let entry = entry?;
let ty = entry.file_type()?;
if ty.is_dir() {
copy_dir_all(entry.path(), dst.as_ref().join(entry.file_name()))?;
} else {
std::fs::copy(entry.path(), dst.as_ref().join(entry.file_name()))?;
}
}
Ok(())
}
|