David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 1 | // Build mcuboot as a library, based on the requested features. |
| 2 | |
| 3 | extern crate gcc; |
| 4 | |
| 5 | use std::env; |
| 6 | use std::fs; |
| 7 | use std::io; |
| 8 | use std::path::Path; |
| 9 | |
| 10 | fn 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 Brown | 82bf7c2 | 2017-07-12 09:49:31 -0600 | [diff] [blame] | 37 | conf.include("mbedtls/include"); |
| 38 | conf.file("mbedtls/library/sha256.c"); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 39 | |
David Brown | 82bf7c2 | 2017-07-12 09:49:31 -0600 | [diff] [blame] | 40 | conf.file("mbedtls/library/rsa.c"); |
| 41 | conf.file("mbedtls/library/bignum.c"); |
| 42 | conf.file("mbedtls/library/asn1parse.c"); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 43 | } |
| 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 Brown | d2b1853 | 2017-07-12 09:51:31 -0600 | [diff] [blame] | 58 | conf.file("csupport/run.c"); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 59 | 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 Brown | d2b1853 | 2017-07-12 09:51:31 -0600 | [diff] [blame] | 67 | walk_dir("csupport").unwrap(); |
David Brown | 82bf7c2 | 2017-07-12 09:49:31 -0600 | [diff] [blame] | 68 | walk_dir("mbedtls/include").unwrap(); |
| 69 | walk_dir("mbedtls/library").unwrap(); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | // Output the names of all files within a directory so that Cargo knows when to rebuild. |
| 73 | fn 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 | } |