|
1 | 1 | /* Copyright (c) Fortanix, Inc.
|
2 | 2 | *
|
3 |
| - * Licensed under the GNU General Public License, version 2 <LICENSE-GPL or |
4 |
| - * https://www.gnu.org/licenses/gpl-2.0.html> or the Apache License, Version |
5 |
| - * 2.0 <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0>, at your |
6 |
| - * option. This file may not be copied, modified, or distributed except |
| 3 | + * Licensed under the GNU General Public License, version 2 <LICENSE-GPL or |
| 4 | + * https://www.gnu.org/licenses/gpl-2.0.html> or the Apache License, Version |
| 5 | + * 2.0 <LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0>, at your |
| 6 | + * option. This file may not be copied, modified, or distributed except |
7 | 7 | * according to those terms. */
|
8 | 8 |
|
9 | 9 | extern crate bindgen;
|
10 | 10 | extern crate cmake;
|
11 | 11 |
|
12 | 12 | mod config;
|
13 | 13 | mod headers;
|
14 |
| -#[path="cmake.rs"] |
15 |
| -mod mod_cmake; |
16 |
| -#[path="bindgen.rs"] |
| 14 | +#[path = "bindgen.rs"] |
17 | 15 | mod mod_bindgen;
|
| 16 | +#[path = "cmake.rs"] |
| 17 | +mod mod_cmake; |
18 | 18 |
|
19 | 19 | use std::collections::HashMap;
|
20 |
| -use std::path::{Path,PathBuf}; |
21 | 20 | use std::env;
|
22 | 21 | use std::fs::File;
|
23 | 22 | use std::io::Write;
|
| 23 | +use std::path::{Path, PathBuf}; |
24 | 24 |
|
25 | 25 | pub fn have_feature(feature: &'static str) -> bool {
|
26 |
| - env::var_os(format!("CARGO_FEATURE_{}",feature).to_uppercase().replace("-","_")).is_some() |
| 26 | + env::var_os( |
| 27 | + format!("CARGO_FEATURE_{}", feature) |
| 28 | + .to_uppercase() |
| 29 | + .replace("-", "_"), |
| 30 | + ).is_some() |
27 | 31 | }
|
28 | 32 |
|
29 | 33 | struct BuildConfig {
|
30 |
| - out_dir: PathBuf, |
31 |
| - mbedtls_src: PathBuf, |
32 |
| - config_h: PathBuf, |
| 34 | + out_dir: PathBuf, |
| 35 | + mbedtls_src: PathBuf, |
| 36 | + config_h: PathBuf, |
33 | 37 | }
|
34 | 38 |
|
35 | 39 | impl BuildConfig {
|
36 |
| - fn create_config_h(&self) { |
37 |
| - let mut defines=config::DEFAULT_DEFINES.iter().cloned().collect::<HashMap<_,_>>(); |
38 |
| - for &(feat,def) in config::FEATURE_DEFINES { |
39 |
| - if have_feature(feat) { |
40 |
| - defines.insert(def.0,def.1); |
41 |
| - } |
42 |
| - } |
43 |
| - |
44 |
| - File::create(&self.config_h).and_then(|mut f|{ |
45 |
| - try!(f.write_all(config::PREFIX.as_bytes())); |
46 |
| - for (name,def) in defines { |
47 |
| - try!(f.write_all(def.define(name).as_bytes())); |
48 |
| - } |
49 |
| - if have_feature("custom_printf") { |
50 |
| - try!(writeln!(f,"int mbedtls_printf(const char *format, ...);")); |
51 |
| - } |
52 |
| - if have_feature("custom_has_support") { |
53 |
| - try!(writeln!(f,"int mbedtls_aesni_has_support( unsigned int what ) __attribute__((weak));")); |
54 |
| - try!(writeln!(f,"int mbedtls_padlock_has_support( int feature ) __attribute__((weak));")); |
55 |
| - } |
56 |
| - if have_feature("custom_threading") { |
57 |
| - try!(writeln!(f,"typedef void* mbedtls_threading_mutex_t;")); |
58 |
| - } |
59 |
| - f.write_all(config::SUFFIX.as_bytes()) |
60 |
| - }).expect("config.h I/O error"); |
61 |
| - } |
| 40 | + fn create_config_h(&self) { |
| 41 | + let mut defines = config::DEFAULT_DEFINES |
| 42 | + .iter() |
| 43 | + .cloned() |
| 44 | + .collect::<HashMap<_, _>>(); |
| 45 | + for &(feat, def) in config::FEATURE_DEFINES { |
| 46 | + if have_feature(feat) { |
| 47 | + defines.insert(def.0, def.1); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + File::create(&self.config_h) |
| 52 | + .and_then(|mut f| { |
| 53 | + try!(f.write_all(config::PREFIX.as_bytes())); |
| 54 | + for (name, def) in defines { |
| 55 | + try!(f.write_all(def.define(name).as_bytes())); |
| 56 | + } |
| 57 | + if have_feature("custom_printf") { |
| 58 | + try!(writeln!(f, "int mbedtls_printf(const char *format, ...);")); |
| 59 | + } |
| 60 | + if have_feature("custom_has_support") { |
| 61 | + try!(writeln!( |
| 62 | + f, |
| 63 | + "int mbedtls_aesni_has_support( unsigned int what ) __attribute__((weak));" |
| 64 | + )); |
| 65 | + try!(writeln!( |
| 66 | + f, |
| 67 | + "int mbedtls_padlock_has_support( int feature ) __attribute__((weak));" |
| 68 | + )); |
| 69 | + } |
| 70 | + if have_feature("custom_threading") { |
| 71 | + try!(writeln!(f, "typedef void* mbedtls_threading_mutex_t;")); |
| 72 | + } |
| 73 | + f.write_all(config::SUFFIX.as_bytes()) |
| 74 | + }).expect("config.h I/O error"); |
| 75 | + } |
62 | 76 |
|
63 |
| - fn print_rerun_files(&self) { |
| 77 | + fn print_rerun_files(&self) { |
64 | 78 | println!("cargo:rerun-if-env-changed=RUST_MBEDTLS_SYS_SOURCE");
|
65 |
| - println!("cargo:rerun-if-changed={}",self.mbedtls_src.join("CMakeLists.txt").display()); |
66 |
| - let include=self.mbedtls_src.join(Path::new("include").join("mbedtls")); |
| 79 | + println!( |
| 80 | + "cargo:rerun-if-changed={}", |
| 81 | + self.mbedtls_src.join("CMakeLists.txt").display() |
| 82 | + ); |
| 83 | + let include = self.mbedtls_src.join(Path::new("include").join("mbedtls")); |
67 | 84 | for h in headers::enabled_ordered() {
|
68 |
| - println!("cargo:rerun-if-changed={}",include.join(h).display()); |
69 |
| - } |
70 |
| - for f in self.mbedtls_src.join("library").read_dir().expect("read_dir failed") { |
71 |
| - println!("cargo:rerun-if-changed={}",f.expect("DirEntry failed").path().display()); |
72 |
| - } |
73 |
| - } |
| 85 | + println!("cargo:rerun-if-changed={}", include.join(h).display()); |
| 86 | + } |
| 87 | + for f in self |
| 88 | + .mbedtls_src |
| 89 | + .join("library") |
| 90 | + .read_dir() |
| 91 | + .expect("read_dir failed") |
| 92 | + { |
| 93 | + println!( |
| 94 | + "cargo:rerun-if-changed={}", |
| 95 | + f.expect("DirEntry failed").path().display() |
| 96 | + ); |
| 97 | + } |
| 98 | + } |
74 | 99 | }
|
75 | 100 |
|
76 | 101 | fn main() {
|
77 |
| - let out_dir=PathBuf::from(env::var_os("OUT_DIR").expect("OUT_DIR environment not set?")); |
78 |
| - let src=PathBuf::from(env::var("RUST_MBEDTLS_SYS_SOURCE").unwrap_or("vendor".to_owned())); |
79 |
| - let cfg=BuildConfig { |
80 |
| - config_h: out_dir.join("config.h"), |
81 |
| - out_dir: out_dir, |
82 |
| - mbedtls_src: src, |
83 |
| - }; |
| 102 | + let out_dir = PathBuf::from(env::var_os("OUT_DIR").expect("OUT_DIR environment not set?")); |
| 103 | + let src = PathBuf::from(env::var("RUST_MBEDTLS_SYS_SOURCE").unwrap_or("vendor".to_owned())); |
| 104 | + let cfg = BuildConfig { |
| 105 | + config_h: out_dir.join("config.h"), |
| 106 | + out_dir: out_dir, |
| 107 | + mbedtls_src: src, |
| 108 | + }; |
84 | 109 |
|
85 |
| - cfg.create_config_h(); |
86 |
| - cfg.print_rerun_files(); |
87 |
| - cfg.cmake(); |
88 |
| - cfg.bindgen(); |
| 110 | + cfg.create_config_h(); |
| 111 | + cfg.print_rerun_files(); |
| 112 | + cfg.cmake(); |
| 113 | + cfg.bindgen(); |
89 | 114 | }
|
0 commit comments