blob: c4eb61d338a6decd27db5445db562c16fa69c2ff [file] [log] [blame]
David Brown63902772017-07-12 09:47:49 -06001// Build mcuboot as a library, based on the requested features.
2
3extern crate gcc;
4
5use std::env;
6use std::fs;
7use std::io;
8use std::path::Path;
9
10fn main() {
11 // Feature flags.
12 let sig_rsa = env::var("CARGO_FEATURE_SIG_RSA").is_ok();
13 let sig_ecdsa = env::var("CARGO_FEATURE_SIG_ECDSA").is_ok();
14 let overwrite_only = env::var("CARGO_FEATURE_OVERWRITE_ONLY").is_ok();
15
16 // TODO: Force clang if we are requestion fuzzing.
17
18 let mut conf = gcc::Config::new();
19 conf.define("__BOOTSIM__", None);
20 conf.define("MCUBOOT_USE_FLASH_AREA_GET_SECTORS", None);
21 conf.define("MCUBOOT_VALIDATE_SLOT0", None);
22
David Brown704ac6f2017-07-12 10:14:47 -060023 // Currently, mbed TLS cannot build with both RSA and ECDSA.
24 if sig_rsa && sig_ecdsa {
25 panic!("mcuboot does not support RSA and ECDSA at the same time");
26 }
David Brown63902772017-07-12 09:47:49 -060027
David Brown704ac6f2017-07-12 10:14:47 -060028 if sig_rsa {
David Brown63902772017-07-12 09:47:49 -060029 conf.define("MCUBOOT_SIGN_RSA", None);
30 conf.define("MCUBOOT_USE_MBED_TLS", None);
31
David Brown63902772017-07-12 09:47:49 -060032 conf.define("MCUBOOT_USE_MBED_TLS", None);
33 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-boot.h>"));
David Brown82bf7c22017-07-12 09:49:31 -060034 conf.include("mbedtls/include");
35 conf.file("mbedtls/library/sha256.c");
David Brown704ac6f2017-07-12 10:14:47 -060036 conf.file("../../boot/zephyr/keys.c");
David Brown63902772017-07-12 09:47:49 -060037
David Brown82bf7c22017-07-12 09:49:31 -060038 conf.file("mbedtls/library/rsa.c");
39 conf.file("mbedtls/library/bignum.c");
40 conf.file("mbedtls/library/asn1parse.c");
David Brown704ac6f2017-07-12 10:14:47 -060041 } else if sig_ecdsa {
David Brown63902772017-07-12 09:47:49 -060042 conf.define("MCUBOOT_SIGN_ECDSA", None);
43 conf.define("MCUBOOT_USE_TINYCRYPT", None);
44 // TODO: Compile files + tinycrypt.
45 panic!("ECDSA not yet implemented in sim");
David Brown704ac6f2017-07-12 10:14:47 -060046 } else {
47 // Neither signature type, only verify sha256.
48 conf.define("MCUBOOT_USE_MBED_TLS", None);
49 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-boot.h>"));
50 conf.include("mbedtls/include");
51 conf.file("mbedtls/library/sha256.c");
David Brown63902772017-07-12 09:47:49 -060052 }
53
54 if overwrite_only {
55 conf.define("MCUBOOT_OVERWRITE_ONLY", None);
56 }
57
David Brown704ac6f2017-07-12 10:14:47 -060058 conf.file("../../boot/bootutil/src/image_validate.c");
59 conf.file("../../boot/bootutil/src/image_rsa.c");
David Brown63902772017-07-12 09:47:49 -060060 conf.file("../../boot/bootutil/src/loader.c");
61 conf.file("../../boot/bootutil/src/caps.c");
62 conf.file("../../boot/bootutil/src/bootutil_misc.c");
David Brownd2b18532017-07-12 09:51:31 -060063 conf.file("csupport/run.c");
David Brown63902772017-07-12 09:47:49 -060064 conf.include("../../boot/bootutil/include");
65 conf.include("../../boot/zephyr/include");
66 conf.debug(true);
67 conf.flag("-Wall");
68
69 conf.compile("libbootutil.a");
70
71 walk_dir("../../boot").unwrap();
David Brownd2b18532017-07-12 09:51:31 -060072 walk_dir("csupport").unwrap();
David Brown82bf7c22017-07-12 09:49:31 -060073 walk_dir("mbedtls/include").unwrap();
74 walk_dir("mbedtls/library").unwrap();
David Brown63902772017-07-12 09:47:49 -060075}
76
77// Output the names of all files within a directory so that Cargo knows when to rebuild.
78fn walk_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
79 for ent in fs::read_dir(path.as_ref())? {
80 let ent = ent?;
81 let p = ent.path();
82 if p.is_dir() {
83 walk_dir(p)?;
84 } else {
85 // Note that non-utf8 names will fail.
86 let name = p.to_str().unwrap();
87 if name.ends_with(".c") || name.ends_with(".h") {
88 println!("cargo:rerun-if-changed={}", name);
89 }
90 }
91 }
92
93 Ok(())
94}