blob: e6414cbe2c13ee5c08f67f949929f0d8b2575118 [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
David Brown63902772017-07-12 09:47:49 -060016 let mut conf = gcc::Config::new();
17 conf.define("__BOOTSIM__", None);
18 conf.define("MCUBOOT_USE_FLASH_AREA_GET_SECTORS", None);
19 conf.define("MCUBOOT_VALIDATE_SLOT0", None);
20
David Brown704ac6f2017-07-12 10:14:47 -060021 // Currently, mbed TLS cannot build with both RSA and ECDSA.
22 if sig_rsa && sig_ecdsa {
23 panic!("mcuboot does not support RSA and ECDSA at the same time");
24 }
David Brown63902772017-07-12 09:47:49 -060025
David Brown704ac6f2017-07-12 10:14:47 -060026 if sig_rsa {
David Brown63902772017-07-12 09:47:49 -060027 conf.define("MCUBOOT_SIGN_RSA", None);
28 conf.define("MCUBOOT_USE_MBED_TLS", None);
29
David Brown63902772017-07-12 09:47:49 -060030 conf.define("MCUBOOT_USE_MBED_TLS", None);
31 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-boot.h>"));
David Brown82bf7c22017-07-12 09:49:31 -060032 conf.include("mbedtls/include");
33 conf.file("mbedtls/library/sha256.c");
David Brown704ac6f2017-07-12 10:14:47 -060034 conf.file("../../boot/zephyr/keys.c");
David Brown63902772017-07-12 09:47:49 -060035
David Brown82bf7c22017-07-12 09:49:31 -060036 conf.file("mbedtls/library/rsa.c");
37 conf.file("mbedtls/library/bignum.c");
38 conf.file("mbedtls/library/asn1parse.c");
David Brown704ac6f2017-07-12 10:14:47 -060039 } else if sig_ecdsa {
David Brown63902772017-07-12 09:47:49 -060040 conf.define("MCUBOOT_SIGN_ECDSA", None);
41 conf.define("MCUBOOT_USE_TINYCRYPT", None);
42 // TODO: Compile files + tinycrypt.
43 panic!("ECDSA not yet implemented in sim");
David Brown704ac6f2017-07-12 10:14:47 -060044 } else {
45 // Neither signature type, only verify sha256.
46 conf.define("MCUBOOT_USE_MBED_TLS", None);
47 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-boot.h>"));
48 conf.include("mbedtls/include");
49 conf.file("mbedtls/library/sha256.c");
David Brown63902772017-07-12 09:47:49 -060050 }
51
52 if overwrite_only {
53 conf.define("MCUBOOT_OVERWRITE_ONLY", None);
54 }
55
David Brown704ac6f2017-07-12 10:14:47 -060056 conf.file("../../boot/bootutil/src/image_validate.c");
57 conf.file("../../boot/bootutil/src/image_rsa.c");
David Brown63902772017-07-12 09:47:49 -060058 conf.file("../../boot/bootutil/src/loader.c");
59 conf.file("../../boot/bootutil/src/caps.c");
60 conf.file("../../boot/bootutil/src/bootutil_misc.c");
David Brownd2b18532017-07-12 09:51:31 -060061 conf.file("csupport/run.c");
David Brown63902772017-07-12 09:47:49 -060062 conf.include("../../boot/bootutil/include");
63 conf.include("../../boot/zephyr/include");
64 conf.debug(true);
65 conf.flag("-Wall");
David Brown0b693c02017-07-12 12:34:33 -060066 conf.flag("-Werror");
David Brown63902772017-07-12 09:47:49 -060067
68 conf.compile("libbootutil.a");
69
70 walk_dir("../../boot").unwrap();
David Brownd2b18532017-07-12 09:51:31 -060071 walk_dir("csupport").unwrap();
David Brown82bf7c22017-07-12 09:49:31 -060072 walk_dir("mbedtls/include").unwrap();
73 walk_dir("mbedtls/library").unwrap();
David Brown63902772017-07-12 09:47:49 -060074}
75
76// Output the names of all files within a directory so that Cargo knows when to rebuild.
77fn walk_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
78 for ent in fs::read_dir(path.as_ref())? {
79 let ent = ent?;
80 let p = ent.path();
81 if p.is_dir() {
82 walk_dir(p)?;
83 } else {
84 // Note that non-utf8 names will fail.
85 let name = p.to_str().unwrap();
86 if name.ends_with(".c") || name.ends_with(".h") {
87 println!("cargo:rerun-if-changed={}", name);
88 }
89 }
90 }
91
92 Ok(())
93}