-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuild.rs
More file actions
39 lines (32 loc) · 1.37 KB
/
Copy pathbuild.rs
File metadata and controls
39 lines (32 loc) · 1.37 KB
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
use std::{env, path::PathBuf};
fn main() {
// Set by Cargo to the output directory of this build script.
// Assume it is in the "target" directory of the top level crate.
let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
// "debug", "release", etc.
let profile = env::var("PROFILE").unwrap();
// E.g. "x86_64-unknown-linux-gnu", "x86_64-pc-windows-msvc".
let target = env::var("TARGET").unwrap();
// Iterate the components of OUT_DIR in reverse, attempting to match "{target}/{profile}"
// or "target/{profile}" (literal "target" directory).
let mut out_dir_components = out_dir.components().rev().collect::<Vec<_>>();
let target_dir_pos = out_dir_components
.windows(2)
.position(|w| {
(w[1].as_os_str() == "target" || w[1].as_os_str() == &*target)
&& w[0].as_os_str() == &*profile
})
.expect("failed to match \"target\" directory pattern");
// Reverse and build the real target directory path.
let target_dir = out_dir_components
.drain(target_dir_pos..)
.rev()
.collect::<PathBuf>();
println!(
"cargo:rustc-env=LIBHOTPATCH_TARGET_DIR={}",
target_dir.display()
);
println!("cargo:rerun-if-changed-env=OUT_DIR");
println!("cargo:rerun-if-changed-env=PROFILE");
println!("cargo:rerun-if-changed-env=TARGET");
}