Skip to content

Commit 12cbde2

Browse files
authored
Simplify build script; format with rustfmt (capstone-engine#13)
1 parent 54a17d1 commit 12cbde2

File tree

3 files changed

+38
-38
lines changed

3 files changed

+38
-38
lines changed

build.rs

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,11 @@ extern crate pkg_config;
1212
#[cfg(feature = "build_capstone_cmake")]
1313
extern crate cmake;
1414

15-
#[cfg(feature = "use_bindgen")]
1615
use std::fs::copy;
1716
use std::path::PathBuf;
1817
use std::process::Command;
1918
use std::env;
2019

21-
#[cfg(feature = "use_bindgen")]
2220
include!("common.rs");
2321

2422
const CAPSTONE_DIR: &'static str = "capstone";
@@ -52,13 +50,17 @@ fn find_capstone_header(header_search_paths: &Vec<PathBuf>, name: &str) -> Optio
5250

5351
/// Gets environment variable value. Panics if variable is not set.
5452
fn env_var(var: &str) -> String {
55-
env::var(var)
56-
.expect(&format!("Environment variable {} is not set", var))
53+
env::var(var).expect(&format!("Environment variable {} is not set", var))
5754
}
5855

5956
/// Create bindings using bindgen
6057
#[cfg(feature = "use_bindgen")]
61-
fn write_bindgen_bindings(header_search_paths: &Vec<PathBuf>, update_pregenerated_bindings: bool) {
58+
fn write_bindgen_bindings(
59+
header_search_paths: &Vec<PathBuf>,
60+
update_pregenerated_bindings: bool,
61+
pregenerated_bindgen_header: PathBuf,
62+
out_path: PathBuf,
63+
) {
6264
let mut builder = bindgen::Builder::default()
6365
.rust_target(bindgen::RustTarget::Stable_1_19)
6466
.header(
@@ -88,19 +90,12 @@ fn write_bindgen_bindings(header_search_paths: &Vec<PathBuf>, update_pregenerate
8890
let bindings = builder.generate().expect("Unable to generate bindings");
8991

9092
// Write bindings to $OUT_DIR/bindings.rs
91-
let out_path = PathBuf::from(env_var("OUT_DIR")).join(BINDINGS_FILE);
92-
bindings
93-
.write_to_file(out_path.clone())
94-
.expect("Unable to write bindings");
93+
bindings.write_to_file(out_path.clone()).expect(
94+
"Unable to write bindings",
95+
);
9596

9697
if update_pregenerated_bindings {
97-
let stored_bindgen_header: PathBuf = [
98-
env_var("CARGO_MANIFEST_DIR"),
99-
"pre_generated".into(),
100-
BINDINGS_FILE.into(),
101-
].iter()
102-
.collect();
103-
copy(out_path, stored_bindgen_header).expect("Unable to update capstone bindings");
98+
copy(out_path, pregenerated_bindgen_header).expect("Unable to update capstone bindings");
10499
}
105100
}
106101

@@ -133,8 +128,7 @@ fn main() {
133128
}
134129
} else {
135130
if cfg!(feature = "build_capstone_cmake") {
136-
#[cfg(feature = "build_capstone_cmake")]
137-
cmake();
131+
#[cfg(feature = "build_capstone_cmake")] cmake();
138132
} else {
139133
// TODO: need to add this argument for windows 64-bit, msvc, dunno, read
140134
// COMPILE_MSVC.txt file cygwin-mingw64
@@ -186,7 +180,24 @@ fn main() {
186180
);
187181
}
188182

183+
let pregenerated_bindgen_header: PathBuf = [
184+
env_var("CARGO_MANIFEST_DIR"),
185+
"pre_generated".into(),
186+
BINDINGS_FILE.into(),
187+
].iter()
188+
.collect();
189+
let out_path = PathBuf::from(env_var("OUT_DIR")).join(BINDINGS_FILE);
190+
189191
// Only run bindgen if we are *not* using the bundled capstone bindings
190192
#[cfg(feature = "use_bindgen")]
191-
write_bindgen_bindings(&header_search_paths, update_pregenerated_bindings);
193+
write_bindgen_bindings(
194+
&header_search_paths,
195+
update_pregenerated_bindings,
196+
pregenerated_bindgen_header,
197+
out_path,
198+
);
199+
200+
// Otherwise, copy the pregenerated bindings
201+
#[cfg(not(feature = "use_bindgen"))]
202+
copy(&pregenerated_bindgen_header, &out_path).expect("Unable to update capstone bindings");
192203
}

common.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
#[derive(Clone, Copy, PartialEq, Eq, Debug, Hash)]
66
/// Information specific to architecture
7-
struct CapstoneArchInfo<'a> {
7+
pub struct CapstoneArchInfo<'a> {
88
/// name of C header
99
header_name: &'a str,
1010

1111
/// name used within capstone C library
1212
cs_name: &'a str,
1313
}
1414

15-
static ARCH_INCLUDES: &'static [CapstoneArchInfo<'static>] = &[
15+
pub static ARCH_INCLUDES: &'static [CapstoneArchInfo<'static>] = &[
1616
CapstoneArchInfo {
1717
header_name: "arm.h",
1818
cs_name: "arm",
@@ -47,4 +47,4 @@ static ARCH_INCLUDES: &'static [CapstoneArchInfo<'static>] = &[
4747
},
4848
];
4949

50-
static BINDINGS_FILE: &'static str = "capstone.rs";
50+
pub static BINDINGS_FILE: &'static str = "capstone.rs";

src/lib.rs

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
//! * `struct cs_ARCH_op`: instruction operand
3232
//! * `struct cs_ARCH`: instruction
3333
//!
34-
//! **Note**: documentation for functions/types was taken directly from [Capstone C headers][capstone headers].
34+
//! **Note**: documentation for functions/types was taken directly from
35+
//! [Capstone C headers][capstone headers].
3536
//!
3637
//! [capstone headers]: https://github.com/capstone-rust/capstone-sys/blob/master/capstone/include/capstone.h
3738
//! <sup>1</sup>: Defined as a ["constified" enum modules](https://docs.rs/bindgen/0.30.0/bindgen/struct.Builder.html#method.constified_enum_module)
@@ -45,15 +46,7 @@
4546

4647
use std::os::raw::c_int;
4748

48-
// Include pre-generated bindgen bindings
49-
#[cfg(not(feature = "use_bindgen"))]
50-
include!(concat!(
51-
env!("CARGO_MANIFEST_DIR"),
52-
"/pre_generated/capstone.rs"
53-
));
54-
55-
// Include dynamically generated bindings
56-
#[cfg(feature = "use_bindgen")]
49+
// Bindings should be copied here
5750
include!(concat!(env!("OUT_DIR"), "/capstone.rs"));
5851

5952
pub const CS_SUPPORT_DIET: c_int = (cs_arch::CS_ARCH_ALL as c_int) + 1;
@@ -222,9 +215,7 @@ mod test {
222215
// Union structs
223216
let op = cs_ppc_op {
224217
type_: ppc_op_type::PPC_OP_REG,
225-
__bindgen_anon_1: cs_ppc_op__bindgen_ty_1 {
226-
reg: ppc_reg::PPC_REG_CARRY,
227-
},
218+
__bindgen_anon_1: cs_ppc_op__bindgen_ty_1 { reg: ppc_reg::PPC_REG_CARRY },
228219
};
229220
cs_ppc {
230221
bc: ppc_bc::PPC_BC_LT,
@@ -327,9 +318,7 @@ mod test {
327318
// Union types
328319
let op = cs_x86_op {
329320
type_: x86_op_type::X86_OP_REG,
330-
__bindgen_anon_1: cs_x86_op__bindgen_ty_1 {
331-
reg: x86_reg::X86_REG_AH,
332-
},
321+
__bindgen_anon_1: cs_x86_op__bindgen_ty_1 { reg: x86_reg::X86_REG_AH },
333322
size: 0,
334323
avx_bcast: x86_avx_bcast::X86_AVX_BCAST_2,
335324
avx_zero_opmask: false,

0 commit comments

Comments
 (0)