|
1 |
| -// The script here will translate some of the C headers necessary for the gradual Rust port into the corresponding Rust files. |
2 |
| -// Going to take the *whitelisting* approach, converting just the necessary definitions, in order to keep the builds fast. |
3 |
| - |
4 |
| -// Bindgen requirements: https://rust-lang-nursery.github.io/rust-bindgen/requirements.html |
5 |
| -// Windows: https://github.com/rust-lang-nursery/rustup.rs/issues/1003#issuecomment-289825927 |
6 |
| -// On build.rs: https://doc.rust-lang.org/cargo/reference/build-scripts.html |
7 |
| - |
8 |
| -extern crate bindgen; |
9 |
| -extern crate cc; |
10 |
| -extern crate gstuff; |
11 |
| - |
12 |
| -use gstuff::last_modified_sec; |
13 |
| -use std::env; |
14 |
| -use std::fs; |
15 |
| -use std::io::Read; |
16 |
| - |
17 |
| -const OS_PORTABLE_FUNCTIONS: [&'static str; 1] = ["OS_init"]; |
18 |
| - |
19 |
| -// Will probably refactor in the future (to be generic over multiple headers), |
20 |
| -// right now we're in the "collect as much information as possible" phase (https://www.agilealliance.org/glossary/simple-design). |
21 |
| -fn generate_bindings() { |
22 |
| - // We'd like to regenerate the bindings whenever the build.rs changes, in case we changed bindgen configuration here. |
23 |
| - let lm_build_rs = last_modified_sec(&"build.rs").expect("Can't stat build.rs"); |
24 |
| - |
25 |
| - let from = "crypto777/OS_portable.h"; |
26 |
| - let to = "crypto777/OS_portable.rs"; |
27 |
| - let lm_from = match last_modified_sec(&from) { |
28 |
| - Ok(sec) => sec, |
29 |
| - Err(err) => panic!("Can't stat the header {}: {}", from, err), |
30 |
| - }; |
31 |
| - let lm_to = last_modified_sec(&to).unwrap_or(0.); |
32 |
| - if lm_from >= lm_to || lm_build_rs >= lm_to { |
33 |
| - let bindings = { |
34 |
| - // https://docs.rs/bindgen/0.37.*/bindgen/struct.Builder.html |
35 |
| - let mut builder = bindgen::builder().header(from); |
36 |
| - for name in OS_PORTABLE_FUNCTIONS.iter() { |
37 |
| - builder = builder.whitelist_function(name) |
38 |
| - } |
39 |
| - match builder.generate() { |
40 |
| - Ok(bindings) => bindings, |
41 |
| - Err(()) => panic!("Error generating the bindings for {}", from), |
42 |
| - } |
43 |
| - }; |
44 |
| - |
45 |
| - if let Err(err) = bindings.write_to_file(to) { |
46 |
| - panic!("Error writing to {}: {}", to, err) |
47 |
| - } |
48 |
| - } |
49 |
| -} |
50 |
| - |
51 |
| -/// The build script will usually help us by putting the MarketMaker version |
52 |
| -/// into the "MM_VERSION" environment or the "MM_VERSION" file. |
53 |
| -/// If neither is there then we're probably in a non-released, local development branch |
54 |
| -/// (we're using the "UNKNOWN" version designator then). |
55 |
| -/// This function ensures that we have the "MM_VERSION" variable during the build. |
56 |
| -fn mm_version() { |
57 |
| - if option_env!("MM_VERSION").is_some() { |
58 |
| - return; // The variable is already there. |
59 |
| - } |
60 |
| - |
61 |
| - // Try to load the variable from the file. |
62 |
| - let mut buf; |
63 |
| - let version = if let Ok(mut file) = fs::File::open("MM_VERSION") { |
64 |
| - buf = String::new(); |
65 |
| - file.read_to_string(&mut buf) |
66 |
| - .expect("Can't read from MM_VERSION"); |
67 |
| - buf.trim() |
68 |
| - } else { |
69 |
| - "UNKNOWN" |
70 |
| - }; |
71 |
| - println!("cargo:rustc-env=MM_VERSION={}", version); |
72 |
| -} |
73 |
| - |
74 |
| -/// Build helper C code. |
75 |
| -/// |
76 |
| -/// I think "git clone ... && cargo build" should be enough to start hacking on the Rust code. |
77 |
| -/// |
78 |
| -/// For now we're building the Structured Exception Handling code here, |
79 |
| -/// but in the future we might subsume the rest of the C build under build.rs. |
80 |
| -fn build_c_code() { |
81 |
| - if cfg!(windows) { |
82 |
| - // TODO: Only (re)build the library when the source code or the build script changes. |
83 |
| - cc::Build::new().file("OSlibs/win/seh.c").warnings(true).compile("seh"); |
84 |
| - println! ("cargo:rustc-link-lib=static=seh"); |
85 |
| - println! ("cargo:rustc-link-search=native={}", env::var("OUT_DIR").expect("!OUT_DIR")); |
86 |
| - } |
87 |
| -} |
88 |
| - |
89 |
| -fn main() { |
90 |
| - build_c_code(); |
91 |
| - mm_version(); |
92 |
| - generate_bindings(); |
93 |
| -} |
| 1 | +// The script here will translate some of the C headers necessary for the gradual Rust port into the corresponding Rust files. |
| 2 | +// Going to take the *whitelisting* approach, converting just the necessary definitions, in order to keep the builds fast. |
| 3 | + |
| 4 | +// Bindgen requirements: https://rust-lang-nursery.github.io/rust-bindgen/requirements.html |
| 5 | +// Windows: https://github.com/rust-lang-nursery/rustup.rs/issues/1003#issuecomment-289825927 |
| 6 | +// On build.rs: https://doc.rust-lang.org/cargo/reference/build-scripts.html |
| 7 | + |
| 8 | +extern crate bindgen; |
| 9 | +extern crate cc; |
| 10 | +extern crate gstuff; |
| 11 | + |
| 12 | +use gstuff::last_modified_sec; |
| 13 | +use std::env; |
| 14 | +use std::fs; |
| 15 | +use std::io::Read; |
| 16 | + |
| 17 | +const OS_PORTABLE_FUNCTIONS: [&'static str; 1] = ["OS_init"]; |
| 18 | + |
| 19 | +// Will probably refactor in the future (to be generic over multiple headers), |
| 20 | +// right now we're in the "collect as much information as possible" phase (https://www.agilealliance.org/glossary/simple-design). |
| 21 | +fn generate_bindings() { |
| 22 | + // We'd like to regenerate the bindings whenever the build.rs changes, in case we changed bindgen configuration here. |
| 23 | + let lm_build_rs = last_modified_sec(&"build.rs").expect("Can't stat build.rs"); |
| 24 | + |
| 25 | + let from = "crypto777/OS_portable.h"; |
| 26 | + let to = "crypto777/OS_portable.rs"; |
| 27 | + let lm_from = match last_modified_sec(&from) { |
| 28 | + Ok(sec) => sec, |
| 29 | + Err(err) => panic!("Can't stat the header {}: {}", from, err), |
| 30 | + }; |
| 31 | + let lm_to = last_modified_sec(&to).unwrap_or(0.); |
| 32 | + if lm_from >= lm_to || lm_build_rs >= lm_to { |
| 33 | + let bindings = { |
| 34 | + // https://docs.rs/bindgen/0.37.*/bindgen/struct.Builder.html |
| 35 | + let mut builder = bindgen::builder().header(from); |
| 36 | + for name in OS_PORTABLE_FUNCTIONS.iter() { |
| 37 | + builder = builder.whitelist_function(name) |
| 38 | + } |
| 39 | + match builder.generate() { |
| 40 | + Ok(bindings) => bindings, |
| 41 | + Err(()) => panic!("Error generating the bindings for {}", from), |
| 42 | + } |
| 43 | + }; |
| 44 | + |
| 45 | + if let Err(err) = bindings.write_to_file(to) { |
| 46 | + panic!("Error writing to {}: {}", to, err) |
| 47 | + } |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +/// The build script will usually help us by putting the MarketMaker version |
| 52 | +/// into the "MM_VERSION" environment or the "MM_VERSION" file. |
| 53 | +/// If neither is there then we're probably in a non-released, local development branch |
| 54 | +/// (we're using the "UNKNOWN" version designator then). |
| 55 | +/// This function ensures that we have the "MM_VERSION" variable during the build. |
| 56 | +fn mm_version() { |
| 57 | + if option_env!("MM_VERSION").is_some() { |
| 58 | + return; // The variable is already there. |
| 59 | + } |
| 60 | + |
| 61 | + // Try to load the variable from the file. |
| 62 | + let mut buf; |
| 63 | + let version = if let Ok(mut file) = fs::File::open("MM_VERSION") { |
| 64 | + buf = String::new(); |
| 65 | + file.read_to_string(&mut buf) |
| 66 | + .expect("Can't read from MM_VERSION"); |
| 67 | + buf.trim() |
| 68 | + } else { |
| 69 | + "UNKNOWN" |
| 70 | + }; |
| 71 | + println!("cargo:rustc-env=MM_VERSION={}", version); |
| 72 | +} |
| 73 | + |
| 74 | +/// Build helper C code. |
| 75 | +/// |
| 76 | +/// I think "git clone ... && cargo build" should be enough to start hacking on the Rust code. |
| 77 | +/// |
| 78 | +/// For now we're building the Structured Exception Handling code here, |
| 79 | +/// but in the future we might subsume the rest of the C build under build.rs. |
| 80 | +fn build_c_code() { |
| 81 | + if cfg!(windows) { |
| 82 | + // TODO: Only (re)build the library when the source code or the build script changes. |
| 83 | + cc::Build::new() |
| 84 | + .file("OSlibs/win/seh.c") |
| 85 | + .warnings(true) |
| 86 | + .compile("seh"); |
| 87 | + println!("cargo:rustc-link-lib=static=seh"); |
| 88 | + println!( |
| 89 | + "cargo:rustc-link-search=native={}", |
| 90 | + env::var("OUT_DIR").expect("!OUT_DIR") |
| 91 | + ); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +fn main() { |
| 96 | + build_c_code(); |
| 97 | + mm_version(); |
| 98 | + generate_bindings(); |
| 99 | +} |
0 commit comments