blob: 1b3c7d40665cc68d69739cd43c292d2052218541 [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
23 if sig_rsa {
24 if sig_ecdsa {
25 panic!("mcuboot does not support RSA and ECDSA at the same time");
26 }
27
28 conf.define("MCUBOOT_SIGN_RSA", None);
29 conf.define("MCUBOOT_USE_MBED_TLS", None);
30
31 conf.file("../../boot/bootutil/src/image_validate.c");
32 conf.file("../../boot/bootutil/src/image_rsa.c");
33 conf.file("../../boot/zephyr/keys.c");
34
35 conf.define("MCUBOOT_USE_MBED_TLS", None);
36 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-boot.h>"));
David Brown82bf7c22017-07-12 09:49:31 -060037 conf.include("mbedtls/include");
38 conf.file("mbedtls/library/sha256.c");
David Brown63902772017-07-12 09:47:49 -060039
David Brown82bf7c22017-07-12 09:49:31 -060040 conf.file("mbedtls/library/rsa.c");
41 conf.file("mbedtls/library/bignum.c");
42 conf.file("mbedtls/library/asn1parse.c");
David Brown63902772017-07-12 09:47:49 -060043 }
44 if sig_ecdsa {
45 conf.define("MCUBOOT_SIGN_ECDSA", None);
46 conf.define("MCUBOOT_USE_TINYCRYPT", None);
47 // TODO: Compile files + tinycrypt.
48 panic!("ECDSA not yet implemented in sim");
49 }
50
51 if overwrite_only {
52 conf.define("MCUBOOT_OVERWRITE_ONLY", None);
53 }
54
55 conf.file("../../boot/bootutil/src/loader.c");
56 conf.file("../../boot/bootutil/src/caps.c");
57 conf.file("../../boot/bootutil/src/bootutil_misc.c");
David Brownd2b18532017-07-12 09:51:31 -060058 conf.file("csupport/run.c");
David Brown63902772017-07-12 09:47:49 -060059 conf.include("../../boot/bootutil/include");
60 conf.include("../../boot/zephyr/include");
61 conf.debug(true);
62 conf.flag("-Wall");
63
64 conf.compile("libbootutil.a");
65
66 walk_dir("../../boot").unwrap();
David Brownd2b18532017-07-12 09:51:31 -060067 walk_dir("csupport").unwrap();
David Brown82bf7c22017-07-12 09:49:31 -060068 walk_dir("mbedtls/include").unwrap();
69 walk_dir("mbedtls/library").unwrap();
David Brown63902772017-07-12 09:47:49 -060070}
71
72// Output the names of all files within a directory so that Cargo knows when to rebuild.
73fn walk_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
74 for ent in fs::read_dir(path.as_ref())? {
75 let ent = ent?;
76 let p = ent.path();
77 if p.is_dir() {
78 walk_dir(p)?;
79 } else {
80 // Note that non-utf8 names will fail.
81 let name = p.to_str().unwrap();
82 if name.ends_with(".c") || name.ends_with(".h") {
83 println!("cargo:rerun-if-changed={}", name);
84 }
85 }
86 }
87
88 Ok(())
89}