Debian Codegraph @

.cargo/vendor/rav1e/build.rs

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