-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathbuild.rs
71 lines (62 loc) · 2.5 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
#[cfg(feature = "use_system_unicorn")]
use pkg_config;
#[cfg(feature = "build_unicorn_cmake")]
use std::env;
#[cfg(feature = "build_unicorn_cmake")]
use std::path::Path;
#[cfg(all(feature = "build_unicorn_cmake", target_os = "windows"))]
fn setup_env_windows() {
let devenv_path = cc::windows_registry::find_tool(
env::var("TARGET").unwrap().as_str(), "devenv").unwrap();
let devenv_dir = devenv_path.path().parent().unwrap();
let cmake_path = devenv_dir.join(r"CommonExtensions\Microsoft\CMake\CMake\bin\cmake.exe");
let ninja_path = devenv_dir.join(r"CommonExtensions\Microsoft\CMake\Ninja\ninja.exe");
if !cmake_path.is_file() {
panic!("missing cmake");
}
if !ninja_path.is_file() {
panic!("missing ninja");
}
if let Some(path) = env::var_os("PATH") {
let mut paths = env::split_paths(&path).collect::<Vec<_>>();
for tool_path in [cmake_path, ninja_path] {
paths.push(tool_path.parent().unwrap().to_path_buf());
}
let new_path = env::join_paths(paths).unwrap();
env::set_var("PATH", &new_path);
}
}
#[cfg(feature = "build_unicorn_cmake")]
fn build_with_cmake() {
let manifest_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
let manifest_path = Path::new(&manifest_dir).to_path_buf();
let src_dir = manifest_path.parent().unwrap().parent().unwrap();
#[cfg(target_os = "windows")]
setup_env_windows();
// need to clear build target and append "build" to the path because
// unicorn's CMakeLists.txt doesn't properly support 'install', so we use
// the build artifacts from the build directory, which cmake crate sets
// to "<out_dir>/build/"
let dst = cmake::Config::new(src_dir)
.generator("Ninja")
.define("BUILD_SHARED_LIBS", "OFF")
.no_build_target(true)
.build();
println!("cargo:rustc-link-search=native={}", dst.join("build").display());
// Lazymio(@wtdcode): Why do I stick to static link? See: https://github.com/rust-lang/cargo/issues/5077
println!("cargo:rustc-link-lib=unicorn-static");
println!("cargo:rerun-if-changed=build.rs");
println!("cargo:rerun-if-changed=src");
}
fn main() {
if cfg!(feature = "use_system_unicorn") {
#[cfg(feature = "use_system_unicorn")]
pkg_config::Config::new()
.atleast_version("2")
.probe("unicorn")
.expect("Could not find system unicorn2");
} else {
#[cfg(feature = "build_unicorn_cmake")]
build_with_cmake();
}
}