blob: 2c28dbb6603d3816cf88765d8cbda1affbc79d73 [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);
Marti Bolivar248da082018-04-24 15:11:39 -040020 conf.define("MCUBOOT_HAVE_ASSERT_H", None);
Marti Bolivarf9bfddd2018-04-24 14:28:33 -040021 conf.define("MCUBOOT_MAX_IMG_SECTORS", Some("128"));
Fabio Utzigebdc9692017-11-23 16:28:25 -020022
23 if validate_slot0 {
24 conf.define("MCUBOOT_VALIDATE_SLOT0", None);
25 }
David Brown63902772017-07-12 09:47:49 -060026
David Brown704ac6f2017-07-12 10:14:47 -060027 // Currently, mbed TLS cannot build with both RSA and ECDSA.
28 if sig_rsa && sig_ecdsa {
29 panic!("mcuboot does not support RSA and ECDSA at the same time");
30 }
David Brown63902772017-07-12 09:47:49 -060031
David Brown704ac6f2017-07-12 10:14:47 -060032 if sig_rsa {
David Brown63902772017-07-12 09:47:49 -060033 conf.define("MCUBOOT_SIGN_RSA", None);
34 conf.define("MCUBOOT_USE_MBED_TLS", None);
35
Marti Bolivara4818a52018-04-12 13:02:38 -040036 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-rsa.h>"));
David Brown82bf7c22017-07-12 09:49:31 -060037 conf.include("mbedtls/include");
38 conf.file("mbedtls/library/sha256.c");
Fabio Utzig806af0e2018-04-26 10:53:54 -030039 conf.file("csupport/keys.c");
David Brown63902772017-07-12 09:47:49 -060040
David Brown82bf7c22017-07-12 09:49:31 -060041 conf.file("mbedtls/library/rsa.c");
42 conf.file("mbedtls/library/bignum.c");
Fabio Utzigb04afa92018-09-12 15:27:04 -030043 conf.file("mbedtls/library/platform.c");
44 conf.file("mbedtls/library/platform_util.c");
David Brown82bf7c22017-07-12 09:49:31 -060045 conf.file("mbedtls/library/asn1parse.c");
David Brown704ac6f2017-07-12 10:14:47 -060046 } else if sig_ecdsa {
Fabio Utzigc7865402017-12-05 08:50:52 -020047 conf.define("MCUBOOT_SIGN_EC256", None);
David Brown63902772017-07-12 09:47:49 -060048 conf.define("MCUBOOT_USE_TINYCRYPT", None);
Fabio Utzigc7865402017-12-05 08:50:52 -020049
Marti Bolivara4818a52018-04-12 13:02:38 -040050 conf.define("MBEDTLS_CONFIG_FILE", Some("<config-asn1.h>"));
Fabio Utzigba05f2a2017-12-05 11:00:41 -020051 conf.include("../../ext/mbedtls/include");
Fabio Utzigc7865402017-12-05 08:50:52 -020052 conf.include("../../ext/tinycrypt/lib/include");
53
Fabio Utzig806af0e2018-04-26 10:53:54 -030054 conf.file("csupport/keys.c");
Fabio Utzigc7865402017-12-05 08:50:52 -020055
56 conf.file("../../ext/tinycrypt/lib/source/utils.c");
57 conf.file("../../ext/tinycrypt/lib/source/sha256.c");
58 conf.file("../../ext/tinycrypt/lib/source/ecc.c");
59 conf.file("../../ext/tinycrypt/lib/source/ecc_dsa.c");
60 conf.file("../../ext/tinycrypt/lib/source/ecc_platform_specific.c");
61
Fabio Utzigba05f2a2017-12-05 11:00:41 -020062 conf.file("../../ext/mbedtls/src/asn1parse.c");
David Brown704ac6f2017-07-12 10:14:47 -060063 } else {
Marti Bolivara4818a52018-04-12 13:02:38 -040064 // Neither signature type, only verify sha256. The default
65 // configuration file bundled with mbedTLS is sufficient.
David Brown704ac6f2017-07-12 10:14:47 -060066 conf.define("MCUBOOT_USE_MBED_TLS", None);
David Brown704ac6f2017-07-12 10:14:47 -060067 conf.include("mbedtls/include");
68 conf.file("mbedtls/library/sha256.c");
David Brown63902772017-07-12 09:47:49 -060069 }
70
71 if overwrite_only {
72 conf.define("MCUBOOT_OVERWRITE_ONLY", None);
Fabio Utzig13d9e352017-10-05 20:32:31 -030073 conf.define("MCUBOOT_OVERWRITE_ONLY_FAST", None);
David Brown63902772017-07-12 09:47:49 -060074 }
75
David Brown704ac6f2017-07-12 10:14:47 -060076 conf.file("../../boot/bootutil/src/image_validate.c");
Fabio Utzigc7865402017-12-05 08:50:52 -020077 if sig_rsa {
78 conf.file("../../boot/bootutil/src/image_rsa.c");
79 } else if sig_ecdsa {
80 conf.file("../../boot/bootutil/src/image_ec256.c");
81 }
David Brown63902772017-07-12 09:47:49 -060082 conf.file("../../boot/bootutil/src/loader.c");
83 conf.file("../../boot/bootutil/src/caps.c");
84 conf.file("../../boot/bootutil/src/bootutil_misc.c");
David Brownd2b18532017-07-12 09:51:31 -060085 conf.file("csupport/run.c");
David Brown63902772017-07-12 09:47:49 -060086 conf.include("../../boot/bootutil/include");
Fabio Utzig57c40f72017-12-12 21:48:30 -020087 conf.include("csupport");
Fabio Utzig9a4b9ba2018-05-07 08:31:27 -030088 conf.include("../../boot/zephyr/include");
David Brown63902772017-07-12 09:47:49 -060089 conf.debug(true);
90 conf.flag("-Wall");
David Brown0b693c02017-07-12 12:34:33 -060091 conf.flag("-Werror");
David Brown63902772017-07-12 09:47:49 -060092
Fabio Utzig0bccf9d2017-12-07 12:13:57 -020093 // FIXME: travis-ci still uses gcc 4.8.4 which defaults to std=gnu90.
94 // It has incomplete std=c11 and std=c99 support but std=c99 was checked
95 // to build correctly so leaving it here to updated in the future...
96 conf.flag("-std=c99");
97
David Brown63902772017-07-12 09:47:49 -060098 conf.compile("libbootutil.a");
99
100 walk_dir("../../boot").unwrap();
Fabio Utzigc7865402017-12-05 08:50:52 -0200101 walk_dir("../../ext/tinycrypt/lib/source").unwrap();
Fabio Utzigd32fd642017-12-18 15:19:47 -0200102 walk_dir("../../ext/mbedtls").unwrap();
David Brownd2b18532017-07-12 09:51:31 -0600103 walk_dir("csupport").unwrap();
David Brown82bf7c22017-07-12 09:49:31 -0600104 walk_dir("mbedtls/include").unwrap();
105 walk_dir("mbedtls/library").unwrap();
David Brown63902772017-07-12 09:47:49 -0600106}
107
108// Output the names of all files within a directory so that Cargo knows when to rebuild.
109fn walk_dir<P: AsRef<Path>>(path: P) -> io::Result<()> {
110 for ent in fs::read_dir(path.as_ref())? {
111 let ent = ent?;
112 let p = ent.path();
113 if p.is_dir() {
114 walk_dir(p)?;
115 } else {
116 // Note that non-utf8 names will fail.
117 let name = p.to_str().unwrap();
118 if name.ends_with(".c") || name.ends_with(".h") {
119 println!("cargo:rerun-if-changed={}", name);
120 }
121 }
122 }
123
124 Ok(())
125}