Debian Codegraph @

.cargo/vendor/av-scenechange/build.rs

raw
1 use std::{
2 env,
3 fs,
4 path::{Path, PathBuf},
5 };
6
7 fn rerun_dir<P: AsRef<Path>>(dir: P) {
8 for entry in fs::read_dir(dir).unwrap() {
9 let entry = entry.unwrap();
10 let path = entry.path();
11 println!("cargo:rerun-if-changed={}", path.to_string_lossy());
12
13 if path.is_dir() {
14 rerun_dir(path);
15 }
16 }
17 }
18
19 fn hash_changed(files: &[&str], out_dir: &str, config: &Path) -> Option<([u8; 8], PathBuf)> {
20 use std::{collections::hash_map::DefaultHasher, hash::Hasher};
21
22 let mut hasher = DefaultHasher::new();
23
24 let paths = files
25 .iter()
26 .map(Path::new)
27 .chain(std::iter::once(config))
28 .chain(std::iter::once(Path::new("build.rs")));
29
30 for path in paths {
31 if let Ok(buf) = std::fs::read(path) {
32 hasher.write(&buf);
33 } else {
34 panic!("Cannot open {}", path.display());
35 }
36 }
37
38 if let Some(cmd) = strip_command() {
39 hasher.write(cmd.as_bytes());
40 }
41
42 let hash = hasher.finish().to_be_bytes();
43
44 let hash_path = Path::new(&out_dir).join("asm.hash");
45
46 if let Ok(old_hash) = std::fs::read(&hash_path) {
47 if old_hash == hash {
48 return None;
49 }
50 }
51
52 Some((hash, hash_path))
53 }
54
55 #[cfg(feature = "asm")]
56 fn build_nasm_files() {
57 let mut config = "
58 %pragma preproc sane_empty_expansion true
59 %define private_prefix avsc
60 %define ARCH_X86_32 0
61 %define ARCH_X86_64 1
62 %define PIC 1
63 %define STACK_ALIGNMENT 16
64 %define HAVE_AVX512ICL 1
65 "
66 .to_owned();
67
68 if env::var("CARGO_CFG_TARGET_VENDOR").unwrap() == "apple" {
69 config += "%define PREFIX 1\n";
70 }
71
72 let out_dir = env::var("OUT_DIR").unwrap();
73 let dest_path = Path::new(&out_dir).join("config.asm");
74 std::fs::write(&dest_path, config).expect("can write config.asm");
75
76 let asm_files = &[
77 // "src/asm/x86/cdef_avx2.asm",
78 // "src/asm/x86/cdef_avx512.asm",
79 // "src/asm/x86/cdef_dist.asm",
80 // "src/asm/x86/cdef_rav1e.asm",
81 // "src/asm/x86/cdef_sse.asm",
82 // "src/asm/x86/cdef16_avx2.asm",
83 // "src/asm/x86/cdef16_avx512.asm",
84 // "src/asm/x86/cdef16_sse.asm",
85 "src/asm/x86/ipred_avx2.asm",
86 "src/asm/x86/ipred_avx512.asm",
87 "src/asm/x86/ipred_sse.asm",
88 "src/asm/x86/ipred16_avx2.asm",
89 "src/asm/x86/ipred16_avx512.asm",
90 "src/asm/x86/ipred16_sse.asm",
91 // "src/asm/x86/itx_avx2.asm",
92 // "src/asm/x86/itx_avx512.asm",
93 // "src/asm/x86/itx_sse.asm",
94 // "src/asm/x86/itx16_avx2.asm",
95 // "src/asm/x86/itx16_avx512.asm",
96 // "src/asm/x86/itx16_sse.asm",
97 // "src/asm/x86/looprestoration_avx2.asm",
98 // "src/asm/x86/looprestoration_avx512.asm",
99 // "src/asm/x86/looprestoration_sse.asm",
100 // "src/asm/x86/looprestoration16_avx2.asm",
101 // "src/asm/x86/looprestoration16_avx512.asm",
102 // "src/asm/x86/looprestoration16_sse.asm",
103 "src/asm/x86/mc_avx2.asm",
104 "src/asm/x86/mc_avx512.asm",
105 "src/asm/x86/mc_sse.asm",
106 "src/asm/x86/mc16_avx2.asm",
107 "src/asm/x86/mc16_avx512.asm",
108 "src/asm/x86/mc16_sse.asm",
109 // "src/asm/x86/me.asm",
110 "src/asm/x86/sad_avx.asm",
111 "src/asm/x86/sad_plane.asm",
112 "src/asm/x86/sad_sse2.asm",
113 "src/asm/x86/satd.asm",
114 "src/asm/x86/satd16_avx2.asm",
115 // "src/asm/x86/sse.asm",
116 "src/asm/x86/tables.asm",
117 ];
118
119 if let Some((hash, hash_path)) = hash_changed(asm_files, &out_dir, &dest_path) {
120 let obj = nasm_rs::Build::new()
121 .min_version(2, 15, 0)
122 .include(&out_dir)
123 .include("src")
124 .files(asm_files)
125 .compile_objects()
126 .unwrap_or_else(|e| {
127 panic!("NASM build failed. Make sure you have nasm installed or disable the \"asm\" feature.\n\
128 You can get NASM from https://nasm.us or your system's package manager.\n\
129 \n\
130 error: {e}");
131 });
132
133 // cc is better at finding the correct archiver
134 let mut cc = cc::Build::new();
135 for o in obj {
136 cc.object(o);
137 }
138 cc.compile("avscasm");
139
140 // Strip local symbols from the asm library since they
141 // confuse the debugger.
142 if let Some(strip) = strip_command() {
143 let _ = std::process::Command::new(strip)
144 .arg("-x")
145 .arg(Path::new(&out_dir).join("libavscasm.a"))
146 .status();
147 }
148
149 std::fs::write(hash_path, &hash[..]).unwrap();
150 } else {
151 println!("cargo:rustc-link-search={out_dir}");
152 }
153 println!("cargo:rustc-link-lib=static=avscasm");
154 rerun_dir("src/asm/x86");
155 }
156
157 fn strip_command() -> Option<String> {
158 let target = env::var("TARGET").expect("TARGET");
159 // follows Cargo's naming convention for the linker setting
160 let normalized_target = target.replace('-', "_").to_uppercase();
161 let explicit_strip = env::var(format!("CARGO_TARGET_{normalized_target}_STRIP"))
162 .ok()
163 .or_else(|| env::var("STRIP").ok());
164 if explicit_strip.is_some() {
165 return explicit_strip;
166 }
167
168 // strip command is target-specific, e.g. macOS's strip breaks MUSL's archives
169 let host = env::var("HOST").expect("HOST");
170 if host != target {
171 return None;
172 }
173
174 Some("strip".into())
175 }
176
177 #[cfg(feature = "asm")]
178 fn build_neon_asm_files() {
179 let mut config = "
180 #define PRIVATE_PREFIX avsc_
181 #define ARCH_AARCH64 1
182 #define ARCH_ARM 0
183 #define CONFIG_LOG 1
184 #define HAVE_ASM 1
185 "
186 .to_owned();
187
188 if env::var("CARGO_CFG_TARGET_VENDOR").unwrap() == "apple" {
189 config += "#define PREFIX 1\n";
190 }
191 let out_dir = env::var("OUT_DIR").unwrap();
192 let dest_path = Path::new(&out_dir).join("config.h");
193 std::fs::write(&dest_path, config).expect("can write config.h");
194
195 let asm_files = &[
196 // "src/asm/arm/64/cdef.S",
197 // "src/asm/arm/64/cdef16.S",
198 // "src/asm/arm/64/cdef_dist.S",
199 "src/asm/arm/64/mc.S",
200 "src/asm/arm/64/mc16.S",
201 // "src/asm/arm/64/itx.S",
202 // "src/asm/arm/64/itx16.S",
203 "src/asm/arm/64/ipred.S",
204 "src/asm/arm/64/ipred16.S",
205 // "src/asm/arm/64/sad.S",
206 "src/asm/arm/64/satd.S",
207 // "src/asm/arm/64/sse.S",
208 "src/asm/arm/tables.S",
209 ];
210
211 if let Some((hash, hash_path)) = hash_changed(asm_files, &out_dir, &dest_path) {
212 cc::Build::new()
213 .files(asm_files)
214 .include(".")
215 .include(&out_dir)
216 .compile("avsc-aarch64");
217
218 std::fs::write(hash_path, &hash[..]).unwrap();
219 } else {
220 println!("cargo:rustc-link-search={out_dir}");
221 println!("cargo:rustc-link-lib=static=avsc-aarch64");
222 }
223 rerun_dir("src/asm/arm");
224 }
225
226 #[allow(unused_variables)]
227 fn main() {
228 let arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
229 let os = env::var("CARGO_CFG_TARGET_OS").unwrap();
230 // let env = env::var("CARGO_CFG_TARGET_ENV").unwrap();
231
232 #[cfg(feature = "asm")]
233 {
234 if arch == "x86_64" {
235 println!("cargo:rustc-cfg=asm_x86_64");
236 build_nasm_files()
237 }
238 if arch == "aarch64" {
239 println!("cargo:rustc-cfg=asm_neon");
240 build_neon_asm_files()
241 }
242 }
243
244 println!("cargo:rustc-env=PROFILE={}", env::var("PROFILE").unwrap());
245 if let Ok(value) = env::var("CARGO_CFG_TARGET_FEATURE") {
246 println!("cargo:rustc-env=CARGO_CFG_TARGET_FEATURE={value}");
247 }
248 println!(
249 "cargo:rustc-env=CARGO_ENCODED_RUSTFLAGS={}",
250 env::var("CARGO_ENCODED_RUSTFLAGS").unwrap()
251 );
252 }