11use std:: fs;
22use std:: fs:: copy;
33use std:: path:: { Path , PathBuf } ;
4+
5+ const SLEIGH_SOURCES : & [ & str ] = & [
6+ "address.cc" ,
7+ "compression.cc" ,
8+ "context.cc" ,
9+ "globalcontext.cc" ,
10+ "float.cc" ,
11+ "marshal.cc" ,
12+ "opcodes.cc" ,
13+ "pcoderaw.cc" ,
14+ "semantics.cc" ,
15+ "slaformat.cc" ,
16+ "sleigh.cc" ,
17+ "sleighbase.cc" ,
18+ "slghpatexpress.cc" ,
19+ "slghpattern.cc" ,
20+ "slghsymbol.cc" ,
21+ "space.cc" ,
22+ "translate.cc" ,
23+ "xml.cc" ,
24+ "filemanage.cc" ,
25+ "pcodecompile.cc" ,
26+ ] ;
27+
28+ const SLEIGH_HEADERS : & [ & str ] = & [
29+ "address.hh" ,
30+ "compression.hh" ,
31+ "context.hh" ,
32+ "error.hh" ,
33+ "filemanage.hh" ,
34+ "float.hh" ,
35+ "globalcontext.hh" ,
36+ "loadimage.hh" ,
37+ "marshal.hh" ,
38+ "opbehavior.hh" ,
39+ "opcodes.hh" ,
40+ "partmap.hh" ,
41+ "pcodecompile.hh" ,
42+ "pcoderaw.hh" ,
43+ "semantics.hh" ,
44+ "slaformat.hh" ,
45+ "sleigh.hh" ,
46+ "sleighbase.hh" ,
47+ "slghpatexpress.hh" ,
48+ "slghpattern.hh" ,
49+ "slghsymbol.hh" ,
50+ "space.hh" ,
51+ "translate.hh" ,
52+ "types.h" ,
53+ "xml.hh" ,
54+ ] ;
55+
56+ const ZLIB_HEADERS : & [ & str ] = & [
57+ "deflate.h" ,
58+ "gzguts.h" ,
59+ "inffast.h" ,
60+ "inffixed.h" ,
61+ "inflate.h" ,
62+ "inftrees.h" ,
63+ "trees.h" ,
64+ "zconf.h" ,
65+ "zlib.h" ,
66+ "zutil.h" ,
67+ ] ;
68+
69+ const ZLIB_SOURCES : & [ & str ] = & [
70+ "deflate.c" ,
71+ "inflate.c" ,
72+ "zutil.c" ,
73+ "inftrees.c" ,
74+ "inffast.c" ,
75+ "trees.c" ,
76+ "adler32.c" ,
77+ ] ;
78+
79+ const JINGLE_CPP_SOURCES : & [ & str ] = & [
80+ "context.cpp" ,
81+ "dummy_load_image.cpp" ,
82+ "rust_load_image.cpp" ,
83+ "addrspace_handle.cpp" ,
84+ "addrspace_manager_handle.cpp" ,
85+ "varnode_translation.cpp" ,
86+ "jingle_pcode_emitter.cpp" ,
87+ "jingle_assembly_emitter.cpp" ,
88+ ] ;
89+
90+ const RUST_FFI_BRIDGES : & [ & str ] = & [
91+ "addrspace.rs" ,
92+ "context_ffi.rs" ,
93+ "instruction.rs" ,
94+ "opcode.rs" ,
95+ ] ;
96+
497fn main ( ) {
598 if cfg ! ( target_os = "macos" ) {
699 println ! ( "cargo::rustc-link-search=/opt/homebrew/lib" )
@@ -17,128 +110,105 @@ fn main() {
17110 copy_sources ( ) ;
18111 }
19112
20- let rust_sources = vec ! [
21- "src/ffi/addrspace.rs" ,
22- "src/ffi/context_ffi.rs" ,
23- "src/ffi/instruction.rs" ,
24- "src/ffi/opcode.rs" ,
25- ] ;
26-
27- let cpp_sources = vec ! [
28- "src/ffi/cpp/zlib/inflate.c" ,
29- "src/ffi/cpp/zlib/deflate.c" ,
30- "src/ffi/cpp/zlib/zutil.c" ,
31- "src/ffi/cpp/zlib/inftrees.c" ,
32- "src/ffi/cpp/zlib/inffast.c" ,
33- "src/ffi/cpp/zlib/adler32.c" ,
34- "src/ffi/cpp/zlib/trees.c" ,
35- "src/ffi/cpp/sleigh/address.cc" ,
36- "src/ffi/cpp/sleigh/compression.cc" ,
37- "src/ffi/cpp/sleigh/context.cc" ,
38- "src/ffi/cpp/sleigh/globalcontext.cc" ,
39- "src/ffi/cpp/sleigh/float.cc" ,
40- "src/ffi/cpp/sleigh/marshal.cc" ,
41- "src/ffi/cpp/sleigh/opcodes.cc" ,
42- "src/ffi/cpp/sleigh/pcoderaw.cc" ,
43- "src/ffi/cpp/sleigh/semantics.cc" ,
44- "src/ffi/cpp/sleigh/slaformat.cc" ,
45- "src/ffi/cpp/sleigh/sleigh.cc" ,
46- "src/ffi/cpp/sleigh/sleighbase.cc" ,
47- "src/ffi/cpp/sleigh/slghpatexpress.cc" ,
48- "src/ffi/cpp/sleigh/slghpattern.cc" ,
49- "src/ffi/cpp/sleigh/slghsymbol.cc" ,
50- "src/ffi/cpp/sleigh/space.cc" ,
51- "src/ffi/cpp/sleigh/translate.cc" ,
52- "src/ffi/cpp/sleigh/xml.cc" ,
53- "src/ffi/cpp/sleigh/filemanage.cc" ,
54- "src/ffi/cpp/sleigh/pcodecompile.cc" ,
55- "src/ffi/cpp/context.cpp" ,
56- "src/ffi/cpp/dummy_load_image.cpp" ,
57- "src/ffi/cpp/rust_load_image.cpp" ,
58- "src/ffi/cpp/addrspace_handle.cpp" ,
59- "src/ffi/cpp/addrspace_manager_handle.cpp" ,
60- "src/ffi/cpp/varnode_translation.cpp" ,
61- "src/ffi/cpp/jingle_pcode_emitter.cpp" ,
62- "src/ffi/cpp/jingle_assembly_emitter.cpp" ,
63- ] ;
113+ let map_path = |p : fn ( ) -> PathBuf | {
114+ move |s : & & str | {
115+ let mut b = p ( ) ;
116+ b. push ( s) ;
117+ b
118+ }
119+ } ;
120+
121+ let rust_bridges: Vec < PathBuf > = RUST_FFI_BRIDGES . iter ( ) . map ( map_path ( ffi_rs_path) ) . collect ( ) ;
122+
123+ let jingle_cpp_sources: Vec < PathBuf > = JINGLE_CPP_SOURCES
124+ . iter ( )
125+ . map ( map_path ( ffi_cpp_path) )
126+ . collect ( ) ;
127+
128+ let sleigh_sources: Vec < PathBuf > = SLEIGH_SOURCES . iter ( ) . map ( map_path ( sleigh_path) ) . collect ( ) ;
129+ let zlib_sources: Vec < PathBuf > = ZLIB_SOURCES . iter ( ) . map ( map_path ( zlib_path) ) . collect ( ) ;
130+
64131 // This assumes all your C++ bindings are in lib
65- let mut bridge = cxx_build:: bridges ( & rust_sources ) ;
132+ let mut bridge = cxx_build:: bridges ( & rust_bridges ) ;
66133 bridge
67- . files ( cpp_sources)
134+ . files ( jingle_cpp_sources)
135+ . files ( sleigh_sources)
136+ . files ( zlib_sources)
68137 . flag_if_supported ( "-std=c++17" )
69138 . flag_if_supported ( "-DLOCAL_ZLIB" )
70139 . flag_if_supported ( "-DNO_GZIP" )
71140 . flag_if_supported ( "-Wno-register" )
72- . flag_if_supported ( "-Wno-deprecated" )
73- . flag_if_supported ( "-Wno-unused-const-variable" )
74- . flag_if_supported ( "-Wno-unused-parameter" )
75- . flag_if_supported ( "-Wno-unused-function" )
76- . flag_if_supported ( "-Wno-unneeded-internal-declaration" )
77- . flag_if_supported ( "-Wno-format" )
78- . flag_if_supported ( "-Wno-unused-but-set-variable" )
79- . flag_if_supported ( "-Wno-sign-compare" )
80- . flag_if_supported ( "-Wno-deprecated-copy-with-user-provided-copy" ) ;
141+ . flag_if_supported ( "-w" ) ;
81142
82143 if cfg ! ( windows) {
83144 bridge. flag_if_supported ( "-D_WINDOWS" ) ;
84145 }
85146 bridge. compile ( "jingle_sleigh" ) ;
86147
87148 println ! ( "cargo::rerun-if-changed=src/ffi/cpp/" ) ;
88- for src in rust_sources {
89- println ! ( "cargo::rerun-if-changed={src}" ) ;
149+ for src in rust_bridges {
150+ println ! ( "cargo::rerun-if-changed={}" , src . to_str ( ) . unwrap ( ) ) ;
90151 }
91- println ! ( "cargo::rerun-if-changed=src/ffi/addrspace.rs" ) ;
92- println ! ( "cargo::rerun-if-changed=src/ffi/context_ffi.rs" ) ;
93- println ! ( "cargo::rerun-if-changed=src/ffi/instruction.rs" ) ;
94- println ! (
95- "cargo::rerun-if-changed={}" ,
96- ghidra_sleigh_path( ) . to_str( ) . unwrap( )
97- ) ;
98152}
99153
100154fn copy_sources ( ) {
101- copy_cpp_sources ( ghidra_sleigh_path ( ) , sleigh_path ( ) ) ;
102- copy_cpp_sources ( ghidra_zlib_path ( ) , zlib_path ( ) ) ;
155+ copy_cpp_sources (
156+ ghidra_sleigh_path ( ) ,
157+ sleigh_path ( ) ,
158+ SLEIGH_SOURCES ,
159+ SLEIGH_HEADERS ,
160+ ) ;
161+ copy_cpp_sources ( ghidra_zlib_path ( ) , zlib_path ( ) , ZLIB_SOURCES , ZLIB_HEADERS ) ;
103162}
104163
105- fn copy_cpp_sources < T : AsRef < Path > , E : AsRef < Path > > ( inpath : T , outpath : E ) {
164+ fn copy_cpp_sources < T : AsRef < Path > , E : AsRef < Path > > (
165+ inpath : T ,
166+ outpath : E ,
167+ sources : & [ & str ] ,
168+ headers : & [ & str ] ,
169+ ) {
106170 let _ = fs:: create_dir ( & outpath) ;
107- for path in fs:: read_dir ( inpath) . unwrap ( ) . flatten ( ) {
108- if let Some ( name) = path. file_name ( ) . to_str ( ) {
109- if name. ends_with ( ".cc" )
110- || name. ends_with ( ".c" )
111- || name. ends_with ( ".hh" )
112- || name. ends_with ( ".h" )
113- {
171+ for direntry in fs:: read_dir ( inpath) . unwrap ( ) . flatten ( ) {
172+ let path = direntry. path ( ) ;
173+ let filename = path. file_name ( ) ;
174+ if let Some ( filename) = filename {
175+ let filename = filename. to_str ( ) . unwrap ( ) ;
176+ if sources. contains ( & filename) || headers. contains ( & filename) {
114177 let mut result = PathBuf :: from ( outpath. as_ref ( ) ) ;
115- result. push ( name ) ;
116- copy ( path . path ( ) . as_path ( ) , result. as_path ( ) ) . unwrap ( ) ;
178+ result. push ( filename ) ;
179+ copy ( direntry . path ( ) , result. as_path ( ) ) . unwrap ( ) ;
117180 println ! (
118181 "Copying {} ({} => {})" ,
119- name ,
120- path . path( ) . to_str( ) . unwrap( ) ,
182+ filename ,
183+ direntry . path( ) . to_str( ) . unwrap( ) ,
121184 result. to_str( ) . unwrap( )
122185 ) ;
123186 }
124187 }
125188 }
126189}
127190
128- fn sleigh_path ( ) -> PathBuf {
191+ fn ffi_rs_path ( ) -> PathBuf {
129192 let mut p = PathBuf :: new ( ) ;
130193 p. push ( "src" ) ;
131194 p. push ( "ffi" ) ;
195+ p
196+ }
197+
198+ fn ffi_cpp_path ( ) -> PathBuf {
199+ let mut p = ffi_rs_path ( ) ;
132200 p. push ( "cpp" ) ;
201+ p
202+ }
203+
204+ fn sleigh_path ( ) -> PathBuf {
205+ let mut p = ffi_cpp_path ( ) ;
133206 p. push ( "sleigh" ) ;
134207 p
135208}
136209
137210fn zlib_path ( ) -> PathBuf {
138- let mut p = PathBuf :: new ( ) ;
139- p. push ( "src" ) ;
140- p. push ( "ffi" ) ;
141- p. push ( "cpp" ) ;
211+ let mut p = ffi_cpp_path ( ) ;
142212 p. push ( "zlib" ) ;
143213 p
144214}
@@ -150,8 +220,7 @@ fn submod_path() -> PathBuf {
150220}
151221
152222fn ghidra_sleigh_path ( ) -> PathBuf {
153- let mut p = PathBuf :: new ( ) ;
154- p. push ( submod_path ( ) ) ;
223+ let mut p = submod_path ( ) ;
155224 p. push ( "Ghidra" ) ;
156225 p. push ( "Features" ) ;
157226 p. push ( "Decompiler" ) ;
@@ -162,8 +231,7 @@ fn ghidra_sleigh_path() -> PathBuf {
162231}
163232
164233fn ghidra_zlib_path ( ) -> PathBuf {
165- let mut p = PathBuf :: new ( ) ;
166- p. push ( submod_path ( ) ) ;
234+ let mut p = submod_path ( ) ;
167235 p. push ( "Ghidra" ) ;
168236 p. push ( "Features" ) ;
169237 p. push ( "Decompiler" ) ;
0 commit comments