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
// Copyright 2025 Dustin McAfee
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use std::env;
use std::path::PathBuf;
fn main() {
// Only configure linking if turbojpeg feature is enabled
if env::var("CARGO_FEATURE_TURBOJPEG").is_err() {
return;
}
let target_os = env::var("CARGO_CFG_TARGET_OS").unwrap();
match target_os.as_str() {
"macos" => {
// On macOS, turbojpeg is typically installed via Homebrew
// We need to add the Homebrew library path to the linker search path
let homebrew_paths = vec![
"/opt/homebrew/opt/jpeg-turbo/lib", // Apple Silicon (M1/M2/M3)
"/usr/local/opt/jpeg-turbo/lib", // Intel Macs
];
for path in homebrew_paths {
let path_buf = PathBuf::from(path);
if path_buf.exists() {
println!("cargo:rustc-link-search=native={}", path);
println!("cargo:rustc-link-lib=turbojpeg");
// Found the library, no need to check other paths
return;
}
}
// If neither path exists, still try to link (might be in system path)
println!("cargo:rustc-link-lib=turbojpeg");
}
"linux" => {
// Skip host library paths when cross-compiling for Android
// Android target uses RUSTFLAGS from build.gradle to specify library paths
if let Ok(target_env) = env::var("CARGO_CFG_TARGET_ENV") {
if target_env == "gnu" {
// Check if building for Android (will have TARGET containing "android")
if let Ok(target) = env::var("TARGET") {
if target.contains("android") {
// For Android, let build.gradle's RUSTFLAGS handle library paths
// Don't add host PC library paths
return;
}
}
}
}
// On Linux (non-Android), turbojpeg is typically available via system package manager
// (libjpeg-turbo8-dev on Ubuntu/Debian)
// Add common library paths for different architectures
if let Ok(target_arch) = env::var("CARGO_CFG_TARGET_ARCH") {
let lib_path = match target_arch.as_str() {
"x86_64" => "/usr/lib/x86_64-linux-gnu",
"aarch64" => "/usr/lib/aarch64-linux-gnu",
"arm" => "/usr/lib/arm-linux-gnueabihf",
_ => "/usr/lib",
};
println!("cargo:rustc-link-search=native={}", lib_path);
}
println!("cargo:rustc-link-lib=turbojpeg");
}
"windows" => {
// On Windows, turbojpeg is typically installed via vcpkg
// Check for VCPKG_ROOT environment variable
if let Ok(vcpkg_root) = env::var("VCPKG_ROOT") {
let lib_path = format!("{}\\installed\\x64-windows\\lib", vcpkg_root);
println!("cargo:rustc-link-search=native={}", lib_path);
} else if let Ok(vcpkg_root) = env::var("VCPKG_INSTALLATION_ROOT") {
let lib_path = format!("{}\\installed\\x64-windows\\lib", vcpkg_root);
println!("cargo:rustc-link-search=native={}", lib_path);
}
println!("cargo:rustc-link-lib=turbojpeg");
}
_ => {
// For other platforms, attempt standard linking
println!("cargo:rustc-link-lib=turbojpeg");
}
}
}