blob: c67b792313da6d3932c722c0682b37c6fe0d7da7 [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
Fabio Utzigc7865402017-12-05 08:50:52 -020016 let mut conf = gcc::Build::new();
David Brown63902772017-07-12 09:47:49 -060017 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("MBEDTLS_CONFIG_FILE", Some("<config-boot.h>"));
David Brown82bf7c22017-07-12 09:49:31 -060031 conf.include("mbedtls/include");
32 conf.file("mbedtls/library/sha256.c");
David Brown704ac6f2017-07-12 10:14:47 -060033 conf.file("../../boot/zephyr/keys.c");
David Brown63902772017-07-12 09:47:49 -060034
David Brown82bf7c22017-07-12 09:49:31 -060035 conf.file("mbedtls/library/rsa.c");
36 conf.file("mbedtls/library/bignum.c");
37 conf.file("mbedtls/library/asn1parse.c");
David Brown704ac6f2017-07-12 10:14:47 -060038 } else if sig_ecdsa {
Fabio Utzigc7865402017-12-05 08:50:52 -020039 conf.define("MCUBOOT_SIGN_EC256", None);
David Brown63902772017-07-12 09:47:49 -060040 conf.define("MCUBOOT_USE_TINYCRYPT", None);
Fabio Utzigc7865402017-12-05 08:50:52 -020041
42 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-boot.h>"));
Fabio Utzigba05f2a2017-12-05 11:00:41 -020043 conf.include("../../ext/mbedtls/include");
Fabio Utzigc7865402017-12-05 08:50:52 -020044 conf.include("../../ext/tinycrypt/lib/include");
45
46 conf.file("../../boot/zephyr/keys.c");
47
48 conf.file("../../ext/tinycrypt/lib/source/utils.c");
49 conf.file("../../ext/tinycrypt/lib/source/sha256.c");
50 conf.file("../../ext/tinycrypt/lib/source/ecc.c");
51 conf.file("../../ext/tinycrypt/lib/source/ecc_dsa.c");
52 conf.file("../../ext/tinycrypt/lib/source/ecc_platform_specific.c");
53
Fabio Utzigba05f2a2017-12-05 11:00:41 -020054 conf.file("../../ext/mbedtls/src/asn1parse.c");
David Brown704ac6f2017-07-12 10:14:47 -060055 } else {
56 // Neither signature type, only verify sha256.
57 conf.define("MCUBOOT_USE_MBED_TLS", None);
58 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-boot.h>"));
59 conf.include("mbedtls/include");
60 conf.file("mbedtls/library/sha256.c");
David Brown63902772017-07-12 09:47:49 -060061 }
62
63 if overwrite_only {
64 conf.define("MCUBOOT_OVERWRITE_ONLY", None);
Fabio Utzig13d9e352017-10-05 20:32:31 -030065 conf.define("MCUBOOT_OVERWRITE_ONLY_FAST", None);
David Brown63902772017-07-12 09:47:49 -060066 }
67
David Brown704ac6f2017-07-12 10:14:47 -060068 conf.file("../../boot/bootutil/src/image_validate.c");
Fabio Utzigc7865402017-12-05 08:50:52 -020069 if sig_rsa {
70 conf.file("../../boot/bootutil/src/image_rsa.c");
71 } else if sig_ecdsa {
72 conf.file("../../boot/bootutil/src/image_ec256.c");
73 }
David Brown63902772017-07-12 09:47:49 -060074 conf.file("../../boot/bootutil/src/loader.c");
75 conf.file("../../boot/bootutil/src/caps.c");
76 conf.file("../../boot/bootutil/src/bootutil_misc.c");
David Brownd2b18532017-07-12 09:51:31 -060077 conf.file("csupport/run.c");
David Brown63902772017-07-12 09:47:49 -060078 conf.include("../../boot/bootutil/include");
79 conf.include("../../boot/zephyr/include");
80 conf.debug(true);
81 conf.flag("-Wall");
David Brown0b693c02017-07-12 12:34:33 -060082 conf.flag("-Werror");
David Brown63902772017-07-12 09:47:49 -060083
Fabio Utzig0bccf9d2017-12-07 12:13:57 -020084 // FIXME: travis-ci still uses gcc 4.8.4 which defaults to std=gnu90.
85 // It has incomplete std=c11 and std=c99 support but std=c99 was checked
86 // to build correctly so leaving it here to updated in the future...
87 conf.flag("-std=c99");
88
David Brown63902772017-07-12 09:47:49 -060089 conf.compile("libbootutil.a");
90
91 walk_dir("../../boot").unwrap();
Fabio Utzigc7865402017-12-05 08:50:52 -020092 walk_dir("../../ext/tinycrypt/lib/source").unwrap();
Fabio Utzigd32fd642017-12-18 15:19:47 -020093 walk_dir("../../ext/mbedtls").unwrap();
David Brownd2b18532017-07-12 09:51:31 -060094 walk_dir("csupport").unwrap();
David Brown82bf7c22017-07-12 09:49:31 -060095 walk_dir("mbedtls/include").unwrap();
96 walk_dir("mbedtls/library").unwrap();
David Brown63902772017-07-12 09:47:49 -060097}
98
99// Output the names of all files within a directory so that Cargo knows when to rebuild.
100fn walk_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
101 for ent in fs::read_dir(path.as_ref())? {
102 let ent = ent?;
103 let p = ent.path();
104 if p.is_dir() {
105 walk_dir(p)?;
106 } else {
107 // Note that non-utf8 names will fail.
108 let name = p.to_str().unwrap();
109 if name.ends_with(".c") || name.ends_with(".h") {
110 println!("cargo:rerun-if-changed={}", name);
111 }
112 }
113 }
114
115 Ok(())
116}