|
| 1 | +use std::env; |
1 | 2 | use std::fs::File; |
2 | 3 | use std::io::{BufRead, BufReader}; |
3 | 4 | use std::path::{Path, PathBuf}; |
4 | 5 | use std::process::Command; |
5 | 6 |
|
6 | 7 | use semver::Version; |
7 | 8 |
|
| 9 | +use super::TARGET_OS_LINUX; |
| 10 | + |
8 | 11 | pub trait IncludePath { |
9 | 12 | fn get_module_header_dir(&self) -> Option<PathBuf>; |
10 | 13 | fn get_version_header(&self) -> Option<PathBuf>; |
@@ -100,42 +103,119 @@ impl IncludePath for Path { |
100 | 103 | } |
101 | 104 | } |
102 | 105 |
|
103 | | -/// Something like `/usr/include/x86_64-linux-gnu/opencv4/` on newer Debian-derived distros |
| 106 | +const POSSIBLE_MULTIARCHES: &[&str] = &[ |
| 107 | + "x86_64-linux-gnu", |
| 108 | + "aarch64-linux-gnu", |
| 109 | + "arm-linux-gnueabihf", |
| 110 | + "i386-linux-gnu", |
| 111 | + "arm-linux-gnueabi", |
| 112 | +]; |
| 113 | + |
| 114 | +/// When cross-compiling, try to derive the target's multiarch from Cargo's target triple |
104 | 115 | /// |
105 | | -/// https://wiki.debian.org/Multiarch/Implementation |
106 | | -pub fn get_multiarch_header_dir() -> Option<PathBuf> { |
107 | | - let try_multiarch = Command::new("dpkg-architecture") |
108 | | - .args(["--query", "DEB_TARGET_MULTIARCH"]) |
109 | | - .output() |
110 | | - .inspect_err(|e| eprintln!("=== Failed to get DEB_TARGET_MULTIARCH: {e}")) |
111 | | - .ok() |
112 | | - .or_else(|| { |
113 | | - Command::new("cc") |
114 | | - .arg("-print-multiarch") |
| 116 | +/// https://doc.rust-lang.org/reference/conditional-compilation.html#target_arch |
| 117 | +/// https://wiki.debian.org/Multiarch/Tuples#Architectures_in_Debian |
| 118 | +fn target_linux_multiarch_from_cargo() -> Option<&'static str> { |
| 119 | + let target_arch = env::var("CARGO_CFG_TARGET_ARCH").ok()?; |
| 120 | + let target_abi = env::var("CARGO_CFG_TARGET_ABI").ok()?; |
| 121 | + match (target_arch.as_str(), target_abi.as_str()) { |
| 122 | + ("x86", _) => Some("i386-linux-gnu"), |
| 123 | + ("x86_64", _) => Some("x86_64-linux-gnu"), |
| 124 | + ("aarch64", _) => Some("aarch64-linux-gnu"), |
| 125 | + ("arm", "eabihf") => Some("arm-linux-gnueabihf"), |
| 126 | + ("arm", _) => Some("arm-linux-gnueabi"), |
| 127 | + _ => None, |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +fn target_linux_multiarch_from_path(cmake_dir: &Path) -> Option<&str> { |
| 132 | + cmake_dir.components().rev().find_map(|comp| { |
| 133 | + comp |
| 134 | + .as_os_str() |
| 135 | + .to_str() |
| 136 | + .filter(|comp_str| POSSIBLE_MULTIARCHES.contains(comp_str)) |
| 137 | + }) |
| 138 | +} |
| 139 | + |
| 140 | +pub fn get_multiarch_header_dirs(non_multiarch_include_dirs: &[PathBuf]) -> Vec<PathBuf> { |
| 141 | + let mut try_multiarches = vec![]; |
| 142 | + |
| 143 | + try_multiarches.extend( |
| 144 | + (*TARGET_OS_LINUX) |
| 145 | + .then(target_linux_multiarch_from_cargo) |
| 146 | + .flatten() |
| 147 | + .map(|s| s.to_string()) |
| 148 | + .inspect(|target_arch| eprintln!("=== Detected target multiarch from Cargo target: {target_arch}")), |
| 149 | + ); |
| 150 | + try_multiarches.extend( |
| 151 | + env::var_os("OpenCV_DIR") |
| 152 | + .and_then(|d| target_linux_multiarch_from_path(&PathBuf::from(d)).map(|s| s.to_string())) |
| 153 | + .inspect(|target_arch| eprintln!("=== Detected target multiarch from cmake OpenCV_DIR: {target_arch}")), |
| 154 | + ); |
| 155 | + if try_multiarches.is_empty() { |
| 156 | + // Fallback to detecting the host's multiarch |
| 157 | + try_multiarches.extend( |
| 158 | + Command::new("dpkg-architecture") |
| 159 | + .args(["--query", "DEB_TARGET_MULTIARCH"]) |
115 | 160 | .output() |
116 | | - .inspect_err(|e| eprintln!("=== Failed to get -print-multiarch: {e}")) |
| 161 | + .inspect_err(|e| eprintln!("=== Failed to query DEB_TARGET_MULTIARCH: {e}")) |
117 | 162 | .ok() |
118 | | - }) |
119 | | - .and_then(|output| String::from_utf8(output.stdout).ok()) |
120 | | - .map_or_else( |
121 | | - || { |
122 | | - eprintln!("=== Failed to get DEB_TARGET_MULTIARCH, trying common multiarch paths"); |
123 | | - vec![ |
124 | | - "x86_64-linux-gnu".to_string(), |
125 | | - "aarch64-linux-gnu".to_string(), |
126 | | - "arm-linux-gnueabihf".to_string(), |
127 | | - ] |
128 | | - }, |
129 | | - |multiarch| vec![multiarch.trim().to_string()], |
| 163 | + .or_else(|| { |
| 164 | + Command::new("cc") |
| 165 | + .arg("-print-multiarch") |
| 166 | + .output() |
| 167 | + .inspect_err(|e| eprintln!("=== Failed to get -print-multiarch: {e}")) |
| 168 | + .ok() |
| 169 | + }) |
| 170 | + .and_then(|output| String::from_utf8(output.stdout).ok()) |
| 171 | + .map_or_else( |
| 172 | + || { |
| 173 | + eprintln!("=== Failed to detect multiarch path, trying common paths"); |
| 174 | + POSSIBLE_MULTIARCHES.iter().map(|s| s.to_string()).collect::<Vec<_>>() |
| 175 | + }, |
| 176 | + |multiarch| vec![multiarch.trim().to_string()], |
| 177 | + ), |
130 | 178 | ); |
| 179 | + }; |
131 | 180 |
|
132 | | - eprintln!("=== Trying multiarch paths: {try_multiarch:?}"); |
| 181 | + eprintln!("=== Trying multiarches: {try_multiarches:?}"); |
133 | 182 |
|
134 | | - for multiarch in try_multiarch { |
135 | | - let header_dir = PathBuf::from(format!("/usr/include/{multiarch}/opencv4")); |
136 | | - if header_dir.is_dir() { |
137 | | - return Some(header_dir); |
| 183 | + let mut out = Vec::with_capacity(non_multiarch_include_dirs.len()); |
| 184 | + for multiarch in try_multiarches { |
| 185 | + for non_multiarch_include_dir in non_multiarch_include_dirs { |
| 186 | + if let Some(multiarch_include_dir) = add_multiarch_dir_before_opencv4(non_multiarch_include_dir, &multiarch) { |
| 187 | + if multiarch_include_dir.is_dir() { |
| 188 | + out.push(multiarch_include_dir); |
| 189 | + } |
| 190 | + } else if let Some(multiarch_include_dir) = add_multiarch_dir_after_include(non_multiarch_include_dir, &multiarch) { |
| 191 | + if multiarch_include_dir.is_dir() { |
| 192 | + out.push(multiarch_include_dir); |
| 193 | + } |
| 194 | + } |
138 | 195 | } |
139 | 196 | } |
140 | | - None |
| 197 | + eprintln!("=== Found multiarch header dirs: {out:?}"); |
| 198 | + out |
| 199 | +} |
| 200 | + |
| 201 | +/// Finds the "opencv4" component in the path and inserts the multiarch directory before it |
| 202 | +/// |
| 203 | +/// This works starting with OpenCV 4.0.0 |
| 204 | +fn add_multiarch_dir_before_opencv4(path: &Path, multiarch: &str) -> Option<PathBuf> { |
| 205 | + let opencv4_idx = path |
| 206 | + .components() |
| 207 | + .position(|comp| comp.as_os_str().to_str().is_some_and(|s| s == "opencv4"))?; |
| 208 | + let mut comps = path.components(); |
| 209 | + let mut out = PathBuf::with_capacity(path.as_os_str().len() + multiarch.len() + 1); |
| 210 | + out.extend((&mut comps).take(opencv4_idx)); |
| 211 | + out.push(multiarch); |
| 212 | + out.extend(comps); |
| 213 | + Some(out) |
| 214 | +} |
| 215 | + |
| 216 | +/// Finds the "include" component in the path and inserts the multiarch directory after it |
| 217 | +/// |
| 218 | +/// This works starting for OpenCV 3.4 as it doesn't have the "opencv4" component |
| 219 | +fn add_multiarch_dir_after_include(path: &Path, multiarch: &str) -> Option<PathBuf> { |
| 220 | + path.ends_with("include").then(|| path.join(multiarch)) |
141 | 221 | } |
0 commit comments