-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbuild.rs
242 lines (219 loc) · 5.54 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
use std::fs;
use std::fs::copy;
use std::path::{Path, PathBuf};
const SLEIGH_SOURCES: &[&str] = &[
"address.cc",
"compression.cc",
"context.cc",
"globalcontext.cc",
"float.cc",
"marshal.cc",
"opcodes.cc",
"pcoderaw.cc",
"semantics.cc",
"slaformat.cc",
"sleigh.cc",
"sleighbase.cc",
"slghpatexpress.cc",
"slghpattern.cc",
"slghsymbol.cc",
"space.cc",
"translate.cc",
"xml.cc",
"filemanage.cc",
"pcodecompile.cc",
];
const SLEIGH_HEADERS: &[&str] = &[
"address.hh",
"compression.hh",
"context.hh",
"error.hh",
"filemanage.hh",
"float.hh",
"globalcontext.hh",
"loadimage.hh",
"marshal.hh",
"opbehavior.hh",
"opcodes.hh",
"partmap.hh",
"pcodecompile.hh",
"pcoderaw.hh",
"semantics.hh",
"slaformat.hh",
"sleigh.hh",
"sleighbase.hh",
"slghpatexpress.hh",
"slghpattern.hh",
"slghsymbol.hh",
"space.hh",
"translate.hh",
"types.h",
"xml.hh",
];
const ZLIB_HEADERS: &[&str] = &[
"deflate.h",
"gzguts.h",
"inffast.h",
"inffixed.h",
"inflate.h",
"inftrees.h",
"trees.h",
"zconf.h",
"zlib.h",
"zutil.h",
];
const ZLIB_SOURCES: &[&str] = &[
"deflate.c",
"inflate.c",
"zutil.c",
"inftrees.c",
"inffast.c",
"trees.c",
"adler32.c",
];
const JINGLE_CPP_SOURCES: &[&str] = &[
"context.cpp",
"dummy_load_image.cpp",
"rust_load_image.cpp",
"addrspace_handle.cpp",
"addrspace_manager_handle.cpp",
"varnode_translation.cpp",
"jingle_pcode_emitter.cpp",
"jingle_assembly_emitter.cpp",
];
const RUST_FFI_BRIDGES: &[&str] = &[
"addrspace.rs",
"context_ffi.rs",
"instruction.rs",
"opcode.rs",
];
fn main() {
if cfg!(target_os = "macos") {
println!("cargo::rustc-link-search=/opt/homebrew/lib")
}
if !sleigh_path().exists() | !zlib_path().exists() {
let submod = submod_path();
if !submod.read_dir().is_ok_and(|f| f.count() != 0) {
panic!(
"SLEIGH sources not found! This likely means that you are developing on a fresh \
clone of jingle and need to pull in the SLEIGH sources. Please run: \n\
git submodule init && git submodule update"
)
}
copy_sources();
}
let map_path = |p: fn() -> PathBuf| {
move |s: &&str| {
let mut b = p();
b.push(s);
b
}
};
let rust_bridges: Vec<PathBuf> = RUST_FFI_BRIDGES.iter().map(map_path(ffi_rs_path)).collect();
let jingle_cpp_sources: Vec<PathBuf> = JINGLE_CPP_SOURCES
.iter()
.map(map_path(ffi_cpp_path))
.collect();
let sleigh_sources: Vec<PathBuf> = SLEIGH_SOURCES.iter().map(map_path(sleigh_path)).collect();
let zlib_sources: Vec<PathBuf> = ZLIB_SOURCES.iter().map(map_path(zlib_path)).collect();
// This assumes all your C++ bindings are in lib
let mut bridge = cxx_build::bridges(&rust_bridges);
bridge
.files(jingle_cpp_sources)
.files(sleigh_sources)
.files(zlib_sources)
.flag_if_supported("-std=c++17")
.flag_if_supported("-DLOCAL_ZLIB")
.flag_if_supported("-DNO_GZIP")
.flag_if_supported("-Wno-register")
.flag_if_supported("-w");
if cfg!(windows) {
bridge.flag_if_supported("-D_WINDOWS");
}
bridge.compile("jingle_sleigh");
println!("cargo::rerun-if-changed=src/ffi/cpp/");
for src in rust_bridges {
println!("cargo::rerun-if-changed={}", src.to_str().unwrap());
}
}
fn copy_sources() {
copy_cpp_sources(
ghidra_sleigh_path(),
sleigh_path(),
SLEIGH_SOURCES,
SLEIGH_HEADERS,
);
copy_cpp_sources(ghidra_zlib_path(), zlib_path(), ZLIB_SOURCES, ZLIB_HEADERS);
}
fn copy_cpp_sources<T: AsRef<Path>, E: AsRef<Path>>(
inpath: T,
outpath: E,
sources: &[&str],
headers: &[&str],
) {
let _ = fs::create_dir(&outpath);
for direntry in fs::read_dir(inpath).unwrap().flatten() {
let path = direntry.path();
let filename = path.file_name();
if let Some(filename) = filename {
let filename = filename.to_str().unwrap();
if sources.contains(&filename) || headers.contains(&filename) {
let mut result = PathBuf::from(outpath.as_ref());
result.push(filename);
copy(direntry.path(), result.as_path()).unwrap();
println!(
"Copying {} ({} => {})",
filename,
direntry.path().to_str().unwrap(),
result.to_str().unwrap()
);
}
}
}
}
fn ffi_rs_path() -> PathBuf {
let mut p = PathBuf::new();
p.push("src");
p.push("ffi");
p
}
fn ffi_cpp_path() -> PathBuf {
let mut p = ffi_rs_path();
p.push("cpp");
p
}
fn sleigh_path() -> PathBuf {
let mut p = ffi_cpp_path();
p.push("sleigh");
p
}
fn zlib_path() -> PathBuf {
let mut p = ffi_cpp_path();
p.push("zlib");
p
}
fn submod_path() -> PathBuf {
let mut p = PathBuf::new();
p.push("ghidra");
p
}
fn ghidra_sleigh_path() -> PathBuf {
let mut p = submod_path();
p.push("Ghidra");
p.push("Features");
p.push("Decompiler");
p.push("src");
p.push("decompile");
p.push("cpp");
p
}
fn ghidra_zlib_path() -> PathBuf {
let mut p = submod_path();
p.push("Ghidra");
p.push("Features");
p.push("Decompiler");
p.push("src");
p.push("decompile");
p.push("zlib");
p
}