forked from 0xC9C3/rust-ms-detours
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
147 lines (124 loc) · 4.56 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
extern crate bindgen;
extern crate core;
use std::env;
use std::fs;
use std::path::{Path, PathBuf};
use cc::windows_registry::find_tool;
// adopted and modified from https://github.com/compass-rs/sass-rs/blob/master/sass-sys/build.rs
macro_rules! t {
($e:expr) => (match $e {
Ok(n) => n,
Err(e) => panic!("\n{} failed with {}\n", stringify!($e), e),
})
}
fn main() {
let src = get_detours_folder();
let tool = find_tool("x86_64-pc-windows-msvc", "msbuild").unwrap();
let target = env::var("TARGET").expect("TARGET not found in environment");
let mut msvc_platform = if target.contains("x86_64") {
"x64"
} else {
"x86"
};
if target.starts_with("aarch64") {
msvc_platform = "ARM64";
}
if target.starts_with("arm") {
msvc_platform = "ARM";
}
let dest = PathBuf::from(env::var_os("OUT_DIR").expect("OUT_DIR not found in environment"));
let build = dest.join("build");
t!(fs::create_dir_all(&build));
cp_r(&src, &build);
fs::copy(&env::current_dir().unwrap().join("wrapper.h"), &build.join("wrapper.h")).unwrap();
tool.to_command()
.current_dir(&build)
.args(
[
"vc\\Detours.sln",
"/p:Configuration=ReleaseMD",
format!("/p:Platform={}", msvc_platform).as_str(),
]
)
.status()
.unwrap();
// Tell cargo to look for shared libraries in the specified directory
let target_folder = format!("lib.{}", msvc_platform);
println!("cargo:rustc-link-search={}", build.join(target_folder).display());
// Tell cargo to tell rustc to link the system bzip2
// shared library.
println!("cargo:rustc-link-lib=detours");
println!("cargo:rustc-link-lib=syelog");
// Tell cargo to invalidate the built crate whenever the wrapper changes
println!("cargo:rerun-if-changed=wrapper.h");
//generate_bindings(build);
}
fn generate_bindings(build: PathBuf) {
// The bindgen::Builder is the main entry point
// to bindgen, and lets you build up options for
// the resulting bindings.
let bindings = bindgen::Builder::default()
// The input header we would like to generate
// bindings for.
.header(build.join("wrapper.h").to_str().unwrap())
.allowlist_function("Detour.*")
.blocklist_type("LP.*")
.blocklist_type("_GUID")
.blocklist_type("GUID")
.blocklist_type("ULONG")
.blocklist_type("PVOID")
.blocklist_type("DWORD")
.blocklist_type("wchar_t")
.blocklist_type("BOOL")
.blocklist_type("BYTE")
.blocklist_type("WORD")
.blocklist_type("PBYTE")
.blocklist_type("PDWORD")
.blocklist_type("INT")
.blocklist_type("CHAR")
.blocklist_type("LONG")
.blocklist_type("WCHAR")
.blocklist_type("HANDLE")
.blocklist_type("HMODULE")
.blocklist_type("HINSTANCE.*")
.blocklist_type("HWND.*")
.blocklist_type("_SECURITY_ATTRIBUTES")
.blocklist_type("_PROCESS_INFORMATION")
.blocklist_type("_STARTUPINFOA")
.blocklist_type("_STARTUPINFOW")
.raw_line("use winapi::shared::minwindef::*;")
.raw_line("use winapi::um::winnt::*;")
.raw_line("use winapi::um::winnt::{INT};")
.raw_line("use winapi::um::minwinbase::*;")
.raw_line("use winapi::um::processthreadsapi::*;")
.raw_line("use winapi::shared::guiddef::*;")
.raw_line("use winapi::shared::windef::*;")
// Tell cargo to invalidate the built crate whenever any of the
// included header files changed.
.layout_tests(false)
.parse_callbacks(Box::new(bindgen::CargoCallbacks))
// Finish the builder and generate the bindings.
.generate()
// Unwrap the Result and panic on failure.
.expect("Unable to generate bindings");
let out_path = PathBuf::from("src");
bindings
.write_to_file(out_path.join("bindings.rs"))
.expect("Couldn't write bindings!");
}
fn cp_r(dir: &Path, dest: &Path) {
for entry in t!(fs::read_dir(dir)) {
let entry = t!(entry);
let path = entry.path();
let dst = dest.join(path.file_name().expect("Failed to get filename of path"));
if t!(fs::metadata(&path)).is_file() {
t!(fs::copy(path, dst));
} else {
t!(fs::create_dir_all(&dst));
cp_r(&path, &dst);
}
}
}
fn get_detours_folder() -> PathBuf {
env::current_dir().expect("Failed to get the current directory").join("detours")
}