-
Notifications
You must be signed in to change notification settings - Fork 3
/
build.rs
74 lines (63 loc) · 2.58 KB
/
build.rs
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
// Copyright 2021 bmc::labs Gmbh. All rights reserved.
//
// Authors:
// Florian Eich <[email protected]>
// Jonas Reitemeyer <[email protected]>
use std::{env, fs, path::Path};
fn main() {
// because of the dynamic linking foo required to make these shared libraries
// from AiM work on both platforms, we need to do some extra acrobatics in
// this build script here.
//
// first and foremost, we must find the paths for the project and the
// OUT_DIR, which is a cargo variable containing the location where cargo
// will drop all the goods (binaries et al.).
#[rustfmt::skip]
let project_dir = env::var(
"CARGO_MANIFEST_DIR"
).expect("unable to read CARGO_MANIFEST_DIR env variable");
#[rustfmt::skip]
let out_dir = env::var(
"OUT_DIR"
).expect("unable to read OUT_DIR env variable");
// cargo will let us specify
//
// (a) where to look for the libraries and stuff when we're linking, but MORE
// IMPORTANTLY
//
// (b) where we want to load from at run time, but THIS ONLY IF the path we
// provide is within the OUT_DIR
//
// so we must copy all the .dll, .so, .lib and whatnot to the OUT_DIR and
// therefore we need to know each of those locations.
let lib_src_path = format!("{}/aim", project_dir);
let lib_dst_path = format!("{}/lib", out_dir);
// if the destination directory doesn't yet exist, create it
if !Path::new(&lib_dst_path).exists() {
fs::create_dir(&lib_dst_path).expect("unable to create lib dir");
}
// now please copy all contents from `aim` (where the repo stores the .dll,
// .so and so on) to the destination directory within the OUT_DIR
let files = fs::read_dir(&lib_src_path).expect("unable to read aim dir");
for file in files {
let src_path = file.expect("could not read file").path();
let dst_path = format!("{}/{}",
&lib_dst_path,
src_path.file_name().unwrap().to_str().unwrap());
fs::copy(src_path, dst_path).expect("unable to copy libs to target dir");
}
// finally, tell cargo where it should be looking for the libs
println!(r"cargo:rustc-link-search=all={}/lib", out_dir);
// here we specify the deps for each platform. not sure why on Linux we
// depend on libxml2 and on windows not, but not too interested either tbh
#[cfg(target_family = "unix")]
{
println!(r"cargo:rustc-link-lib=xdrk-x86_64");
println!(r"cargo:rustc-link-lib=xml2");
}
#[cfg(target_family = "windows")]
{
println!(r"cargo:rustc-link-lib=dylib=libxdrk-x86_64");
}
// and we're done, now we (i.e. cargo) can start doing actual work
}