blob: 599d501139d6923bebfabc355d1a5b55b7ad8c81 [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();
Fabio Utzigebdc9692017-11-23 16:28:25 -020015 let validate_slot0 = env::var("CARGO_FEATURE_VALIDATE_SLOT0").is_ok();
David Brown63902772017-07-12 09:47:49 -060016
Fabio Utzigc7865402017-12-05 08:50:52 -020017 let mut conf = gcc::Build::new();
David Brown63902772017-07-12 09:47:49 -060018 conf.define("__BOOTSIM__", None);
19 conf.define("MCUBOOT_USE_FLASH_AREA_GET_SECTORS", None);
Fabio Utzigebdc9692017-11-23 16:28:25 -020020
21 if validate_slot0 {
22 conf.define("MCUBOOT_VALIDATE_SLOT0", None);
23 }
David Brown63902772017-07-12 09:47:49 -060024
David Brown704ac6f2017-07-12 10:14:47 -060025 // Currently, mbed TLS cannot build with both RSA and ECDSA.
26 if sig_rsa && sig_ecdsa {
27 panic!("mcuboot does not support RSA and ECDSA at the same time");
28 }
David Brown63902772017-07-12 09:47:49 -060029
David Brown704ac6f2017-07-12 10:14:47 -060030 if sig_rsa {
David Brown63902772017-07-12 09:47:49 -060031 conf.define("MCUBOOT_SIGN_RSA", None);
32 conf.define("MCUBOOT_USE_MBED_TLS", None);
33
Marti Bolivara4818a52018-04-12 13:02:38 -040034 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-rsa.h>"));
David Brown82bf7c22017-07-12 09:49:31 -060035 conf.include("mbedtls/include");
36 conf.file("mbedtls/library/sha256.c");
David Brown704ac6f2017-07-12 10:14:47 -060037 conf.file("../../boot/zephyr/keys.c");
David Brown63902772017-07-12 09:47:49 -060038
David Brown82bf7c22017-07-12 09:49:31 -060039 conf.file("mbedtls/library/rsa.c");
40 conf.file("mbedtls/library/bignum.c");
41 conf.file("mbedtls/library/asn1parse.c");
David Brown704ac6f2017-07-12 10:14:47 -060042 } else if sig_ecdsa {
Fabio Utzigc7865402017-12-05 08:50:52 -020043 conf.define("MCUBOOT_SIGN_EC256", None);
David Brown63902772017-07-12 09:47:49 -060044 conf.define("MCUBOOT_USE_TINYCRYPT", None);
Fabio Utzigc7865402017-12-05 08:50:52 -020045
Marti Bolivara4818a52018-04-12 13:02:38 -040046 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-asn1.h>"));
Fabio Utzigba05f2a2017-12-05 11:00:41 -020047 conf.include("../../ext/mbedtls/include");
Fabio Utzigc7865402017-12-05 08:50:52 -020048 conf.include("../../ext/tinycrypt/lib/include");
49
50 conf.file("../../boot/zephyr/keys.c");
51
52 conf.file("../../ext/tinycrypt/lib/source/utils.c");
53 conf.file("../../ext/tinycrypt/lib/source/sha256.c");
54 conf.file("../../ext/tinycrypt/lib/source/ecc.c");
55 conf.file("../../ext/tinycrypt/lib/source/ecc_dsa.c");
56 conf.file("../../ext/tinycrypt/lib/source/ecc_platform_specific.c");
57
Fabio Utzigba05f2a2017-12-05 11:00:41 -020058 conf.file("../../ext/mbedtls/src/asn1parse.c");
David Brown704ac6f2017-07-12 10:14:47 -060059 } else {
Marti Bolivara4818a52018-04-12 13:02:38 -040060 // Neither signature type, only verify sha256. The default
61 // configuration file bundled with mbedTLS is sufficient.
David Brown704ac6f2017-07-12 10:14:47 -060062 conf.define("MCUBOOT_USE_MBED_TLS", None);
David Brown704ac6f2017-07-12 10:14:47 -060063 conf.include("mbedtls/include");
64 conf.file("mbedtls/library/sha256.c");
David Brown63902772017-07-12 09:47:49 -060065 }
66
67 if overwrite_only {
68 conf.define("MCUBOOT_OVERWRITE_ONLY", None);
Fabio Utzig13d9e352017-10-05 20:32:31 -030069 conf.define("MCUBOOT_OVERWRITE_ONLY_FAST", None);
David Brown63902772017-07-12 09:47:49 -060070 }
71
David Brown704ac6f2017-07-12 10:14:47 -060072 conf.file("../../boot/bootutil/src/image_validate.c");
Fabio Utzigc7865402017-12-05 08:50:52 -020073 if sig_rsa {
74 conf.file("../../boot/bootutil/src/image_rsa.c");
75 } else if sig_ecdsa {
76 conf.file("../../boot/bootutil/src/image_ec256.c");
77 }
David Brown63902772017-07-12 09:47:49 -060078 conf.file("../../boot/bootutil/src/loader.c");
79 conf.file("../../boot/bootutil/src/caps.c");
80 conf.file("../../boot/bootutil/src/bootutil_misc.c");
David Brownd2b18532017-07-12 09:51:31 -060081 conf.file("csupport/run.c");
David Brown63902772017-07-12 09:47:49 -060082 conf.include("../../boot/bootutil/include");
83 conf.include("../../boot/zephyr/include");
Fabio Utzig57c40f72017-12-12 21:48:30 -020084 conf.include("csupport");
David Brown63902772017-07-12 09:47:49 -060085 conf.debug(true);
86 conf.flag("-Wall");
David Brown0b693c02017-07-12 12:34:33 -060087 conf.flag("-Werror");
David Brown63902772017-07-12 09:47:49 -060088
Fabio Utzig0bccf9d2017-12-07 12:13:57 -020089 // FIXME: travis-ci still uses gcc 4.8.4 which defaults to std=gnu90.
90 // It has incomplete std=c11 and std=c99 support but std=c99 was checked
91 // to build correctly so leaving it here to updated in the future...
92 conf.flag("-std=c99");
93
David Brown63902772017-07-12 09:47:49 -060094 conf.compile("libbootutil.a");
95
96 walk_dir("../../boot").unwrap();
Fabio Utzigc7865402017-12-05 08:50:52 -020097 walk_dir("../../ext/tinycrypt/lib/source").unwrap();
Fabio Utzigd32fd642017-12-18 15:19:47 -020098 walk_dir("../../ext/mbedtls").unwrap();
David Brownd2b18532017-07-12 09:51:31 -060099 walk_dir("csupport").unwrap();
David Brown82bf7c22017-07-12 09:49:31 -0600100 walk_dir("mbedtls/include").unwrap();
101 walk_dir("mbedtls/library").unwrap();
David Brown63902772017-07-12 09:47:49 -0600102}
103
104// Output the names of all files within a directory so that Cargo knows when to rebuild.
105fn walk_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
106 for ent in fs::read_dir(path.as_ref())? {
107 let ent = ent?;
108 let p = ent.path();
109 if p.is_dir() {
110 walk_dir(p)?;
111 } else {
112 // Note that non-utf8 names will fail.
113 let name = p.to_str().unwrap();
114 if name.ends_with(".c") || name.ends_with(".h") {
115 println!("cargo:rerun-if-changed={}", name);
116 }
117 }
118 }
119
120 Ok(())
121}