David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 1 | // Build mcuboot as a library, based on the requested features. |
| 2 | |
Fabio Utzig | 455cad5 | 2018-10-15 14:36:33 -0700 | [diff] [blame] | 3 | extern crate cc; |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 4 | |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 5 | use std::collections::BTreeSet; |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 6 | use std::env; |
| 7 | use std::fs; |
| 8 | use std::io; |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 9 | use std::path::{Path, PathBuf}; |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 10 | |
| 11 | fn main() { |
| 12 | // Feature flags. |
| 13 | let sig_rsa = env::var("CARGO_FEATURE_SIG_RSA").is_ok(); |
Fabio Utzig | 3929743 | 2019-05-08 18:51:10 -0300 | [diff] [blame] | 14 | let sig_rsa3072 = env::var("CARGO_FEATURE_SIG_RSA3072").is_ok(); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 15 | let sig_ecdsa = env::var("CARGO_FEATURE_SIG_ECDSA").is_ok(); |
David Brown | 641af45 | 2021-02-19 12:16:48 -0700 | [diff] [blame] | 16 | let sig_ecdsa_mbedtls = env::var("CARGO_FEATURE_SIG_ECDSA_MBEDTLS").is_ok(); |
Fabio Utzig | 9771028 | 2019-05-24 17:44:49 -0300 | [diff] [blame] | 17 | let sig_ed25519 = env::var("CARGO_FEATURE_SIG_ED25519").is_ok(); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 18 | let overwrite_only = env::var("CARGO_FEATURE_OVERWRITE_ONLY").is_ok(); |
Fabio Utzig | 031eb7d | 2019-11-28 10:13:14 -0300 | [diff] [blame] | 19 | let swap_move = env::var("CARGO_FEATURE_SWAP_MOVE").is_ok(); |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 20 | let validate_primary_slot = |
| 21 | env::var("CARGO_FEATURE_VALIDATE_PRIMARY_SLOT").is_ok(); |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 22 | let enc_rsa = env::var("CARGO_FEATURE_ENC_RSA").is_ok(); |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 23 | let enc_aes256_rsa = env::var("CARGO_FEATURE_ENC_AES256_RSA").is_ok(); |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 24 | let enc_kw = env::var("CARGO_FEATURE_ENC_KW").is_ok(); |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 25 | let enc_aes256_kw = env::var("CARGO_FEATURE_ENC_AES256_KW").is_ok(); |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 26 | let enc_ec256 = env::var("CARGO_FEATURE_ENC_EC256").is_ok(); |
Fabio Utzig | 6c553d6 | 2021-05-06 19:56:18 -0300 | [diff] [blame] | 27 | let enc_ec256_mbedtls = env::var("CARGO_FEATURE_ENC_EC256_MBEDTLS").is_ok(); |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 28 | let enc_aes256_ec256 = env::var("CARGO_FEATURE_ENC_AES256_EC256").is_ok(); |
Fabio Utzig | 3fa72ca | 2020-04-02 11:20:37 -0300 | [diff] [blame] | 29 | let enc_x25519 = env::var("CARGO_FEATURE_ENC_X25519").is_ok(); |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 30 | let enc_aes256_x25519 = env::var("CARGO_FEATURE_ENC_AES256_X25519").is_ok(); |
Fabio Utzig | 9b97b13 | 2018-12-18 17:21:51 -0200 | [diff] [blame] | 31 | let bootstrap = env::var("CARGO_FEATURE_BOOTSTRAP").is_ok(); |
David Brown | 5e6f5e0 | 2019-04-04 10:50:05 +0700 | [diff] [blame] | 32 | let multiimage = env::var("CARGO_FEATURE_MULTIIMAGE").is_ok(); |
David Brown | 2ee5f7f | 2020-01-13 14:04:01 -0700 | [diff] [blame] | 33 | let downgrade_prevention = env::var("CARGO_FEATURE_DOWNGRADE_PREVENTION").is_ok(); |
David Brown | 7e377ab | 2021-05-26 16:33:39 -0600 | [diff] [blame] | 34 | let ram_load = env::var("CARGO_FEATURE_RAM_LOAD").is_ok(); |
David Brown | 11ffa0a | 2021-05-26 17:10:47 -0600 | [diff] [blame] | 35 | let direct_xip = env::var("CARGO_FEATURE_DIRECT_XIP").is_ok(); |
David Brown | 1bc106e | 2021-12-16 13:23:52 -0700 | [diff] [blame] | 36 | let max_align_32 = env::var("CARGO_FEATURE_MAX_ALIGN_32").is_ok(); |
Roland Mikhel | d670352 | 2023-04-27 14:24:30 +0200 | [diff] [blame] | 37 | let hw_rollback_protection = env::var("CARGO_FEATURE_HW_ROLLBACK_PROTECTION").is_ok(); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 38 | |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 39 | let mut conf = CachedBuild::new(); |
| 40 | conf.conf.define("__BOOTSIM__", None); |
| 41 | conf.conf.define("MCUBOOT_HAVE_LOGGING", None); |
| 42 | conf.conf.define("MCUBOOT_USE_FLASH_AREA_GET_SECTORS", None); |
| 43 | conf.conf.define("MCUBOOT_HAVE_ASSERT_H", None); |
| 44 | conf.conf.define("MCUBOOT_MAX_IMG_SECTORS", Some("128")); |
Gustavo Henrique Nihei | 7bfd14b | 2021-11-24 23:27:22 -0300 | [diff] [blame] | 45 | |
David Brown | 1bc106e | 2021-12-16 13:23:52 -0700 | [diff] [blame] | 46 | if max_align_32 { |
| 47 | conf.conf.define("MCUBOOT_BOOT_MAX_ALIGN", Some("32")); |
| 48 | } else { |
| 49 | conf.conf.define("MCUBOOT_BOOT_MAX_ALIGN", Some("8")); |
| 50 | } |
Gustavo Henrique Nihei | 7bfd14b | 2021-11-24 23:27:22 -0300 | [diff] [blame] | 51 | |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 52 | conf.conf.define("MCUBOOT_IMAGE_NUMBER", Some(if multiimage { "2" } else { "1" })); |
Fabio Utzig | ebdc969 | 2017-11-23 16:28:25 -0200 | [diff] [blame] | 53 | |
David Brown | 2ee5f7f | 2020-01-13 14:04:01 -0700 | [diff] [blame] | 54 | if downgrade_prevention && !overwrite_only { |
| 55 | panic!("Downgrade prevention requires overwrite only"); |
| 56 | } |
| 57 | |
Fabio Utzig | 9b97b13 | 2018-12-18 17:21:51 -0200 | [diff] [blame] | 58 | if bootstrap { |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 59 | conf.conf.define("MCUBOOT_BOOTSTRAP", None); |
| 60 | conf.conf.define("MCUBOOT_OVERWRITE_ONLY_FAST", None); |
Fabio Utzig | 9b97b13 | 2018-12-18 17:21:51 -0200 | [diff] [blame] | 61 | } |
| 62 | |
David Vincze | 2d736ad | 2019-02-18 11:50:22 +0100 | [diff] [blame] | 63 | if validate_primary_slot { |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 64 | conf.conf.define("MCUBOOT_VALIDATE_PRIMARY_SLOT", None); |
Fabio Utzig | ebdc969 | 2017-11-23 16:28:25 -0200 | [diff] [blame] | 65 | } |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 66 | |
David Brown | 2ee5f7f | 2020-01-13 14:04:01 -0700 | [diff] [blame] | 67 | if downgrade_prevention { |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 68 | conf.conf.define("MCUBOOT_DOWNGRADE_PREVENTION", None); |
David Brown | 2ee5f7f | 2020-01-13 14:04:01 -0700 | [diff] [blame] | 69 | } |
| 70 | |
David Brown | 7e377ab | 2021-05-26 16:33:39 -0600 | [diff] [blame] | 71 | if ram_load { |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 72 | conf.conf.define("MCUBOOT_RAM_LOAD", None); |
David Brown | 7e377ab | 2021-05-26 16:33:39 -0600 | [diff] [blame] | 73 | } |
| 74 | |
David Brown | 11ffa0a | 2021-05-26 17:10:47 -0600 | [diff] [blame] | 75 | if direct_xip { |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 76 | conf.conf.define("MCUBOOT_DIRECT_XIP", None); |
David Brown | 11ffa0a | 2021-05-26 17:10:47 -0600 | [diff] [blame] | 77 | } |
| 78 | |
Roland Mikhel | d670352 | 2023-04-27 14:24:30 +0200 | [diff] [blame] | 79 | if hw_rollback_protection { |
| 80 | conf.conf.define("MCUBOOT_HW_ROLLBACK_PROT", None); |
| 81 | conf.file("csupport/security_cnt.c"); |
| 82 | } |
| 83 | |
Fabio Utzig | 3929743 | 2019-05-08 18:51:10 -0300 | [diff] [blame] | 84 | // Currently no more than one sig type can be used simultaneously. |
Fabio Utzig | 9771028 | 2019-05-24 17:44:49 -0300 | [diff] [blame] | 85 | if vec![sig_rsa, sig_rsa3072, sig_ecdsa, sig_ed25519].iter() |
Fabio Utzig | 3929743 | 2019-05-08 18:51:10 -0300 | [diff] [blame] | 86 | .fold(0, |sum, &v| sum + v as i32) > 1 { |
| 87 | panic!("mcuboot does not support more than one sig type at the same time"); |
David Brown | 704ac6f | 2017-07-12 10:14:47 -0600 | [diff] [blame] | 88 | } |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 89 | |
Fabio Utzig | 3929743 | 2019-05-08 18:51:10 -0300 | [diff] [blame] | 90 | if sig_rsa || sig_rsa3072 { |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 91 | conf.conf.define("MCUBOOT_SIGN_RSA", None); |
Fabio Utzig | 3929743 | 2019-05-08 18:51:10 -0300 | [diff] [blame] | 92 | // The Kconfig style defines must be added here as well because |
| 93 | // they are used internally by "config-rsa.h" |
| 94 | if sig_rsa { |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 95 | conf.conf.define("MCUBOOT_SIGN_RSA_LEN", "2048"); |
| 96 | conf.conf.define("CONFIG_BOOT_SIGNATURE_TYPE_RSA_LEN", "2048"); |
Fabio Utzig | 3929743 | 2019-05-08 18:51:10 -0300 | [diff] [blame] | 97 | } else { |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 98 | conf.conf.define("MCUBOOT_SIGN_RSA_LEN", "3072"); |
| 99 | conf.conf.define("CONFIG_BOOT_SIGNATURE_TYPE_RSA_LEN", "3072"); |
Fabio Utzig | 3929743 | 2019-05-08 18:51:10 -0300 | [diff] [blame] | 100 | } |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 101 | conf.conf.define("MCUBOOT_USE_MBED_TLS", None); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 102 | |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 103 | conf.conf.include("../../ext/mbedtls/include"); |
Sherry Zhang | f4580cb | 2021-07-13 22:07:31 +0800 | [diff] [blame] | 104 | conf.file("../../ext/mbedtls/library/sha256.c"); |
Fabio Utzig | 806af0e | 2018-04-26 10:53:54 -0300 | [diff] [blame] | 105 | conf.file("csupport/keys.c"); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 106 | |
Sherry Zhang | f4580cb | 2021-07-13 22:07:31 +0800 | [diff] [blame] | 107 | conf.file("../../ext/mbedtls/library/rsa.c"); |
| 108 | conf.file("../../ext/mbedtls/library/bignum.c"); |
| 109 | conf.file("../../ext/mbedtls/library/platform.c"); |
| 110 | conf.file("../../ext/mbedtls/library/platform_util.c"); |
| 111 | conf.file("../../ext/mbedtls/library/asn1parse.c"); |
Antonio de Angelis | 02bf072 | 2022-11-22 15:35:43 +0000 | [diff] [blame] | 112 | conf.file("../../ext/mbedtls/library/md.c"); |
| 113 | |
David Brown | 704ac6f | 2017-07-12 10:14:47 -0600 | [diff] [blame] | 114 | } else if sig_ecdsa { |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 115 | conf.conf.define("MCUBOOT_SIGN_EC256", None); |
| 116 | conf.conf.define("MCUBOOT_USE_TINYCRYPT", None); |
Fabio Utzig | c786540 | 2017-12-05 08:50:52 -0200 | [diff] [blame] | 117 | |
Fabio Utzig | b4d20c8 | 2018-12-27 16:08:39 -0200 | [diff] [blame] | 118 | if !enc_kw { |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 119 | conf.conf.include("../../ext/mbedtls/include"); |
Fabio Utzig | b4d20c8 | 2018-12-27 16:08:39 -0200 | [diff] [blame] | 120 | } |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 121 | conf.conf.include("../../ext/tinycrypt/lib/include"); |
Fabio Utzig | c786540 | 2017-12-05 08:50:52 -0200 | [diff] [blame] | 122 | |
Fabio Utzig | 806af0e | 2018-04-26 10:53:54 -0300 | [diff] [blame] | 123 | conf.file("csupport/keys.c"); |
Fabio Utzig | c786540 | 2017-12-05 08:50:52 -0200 | [diff] [blame] | 124 | |
| 125 | conf.file("../../ext/tinycrypt/lib/source/utils.c"); |
| 126 | conf.file("../../ext/tinycrypt/lib/source/sha256.c"); |
| 127 | conf.file("../../ext/tinycrypt/lib/source/ecc.c"); |
| 128 | conf.file("../../ext/tinycrypt/lib/source/ecc_dsa.c"); |
| 129 | conf.file("../../ext/tinycrypt/lib/source/ecc_platform_specific.c"); |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 130 | conf.file("../../ext/mbedtls/library/platform_util.c"); |
| 131 | conf.file("../../ext/mbedtls/library/asn1parse.c"); |
David Brown | 641af45 | 2021-02-19 12:16:48 -0700 | [diff] [blame] | 132 | } else if sig_ecdsa_mbedtls { |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 133 | conf.conf.define("MCUBOOT_SIGN_EC256", None); |
| 134 | conf.conf.define("MCUBOOT_USE_MBED_TLS", None); |
David Brown | 641af45 | 2021-02-19 12:16:48 -0700 | [diff] [blame] | 135 | |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 136 | conf.conf.include("../../ext/mbedtls/include"); |
Sherry Zhang | f4580cb | 2021-07-13 22:07:31 +0800 | [diff] [blame] | 137 | conf.file("../../ext/mbedtls/library/sha256.c"); |
David Brown | 641af45 | 2021-02-19 12:16:48 -0700 | [diff] [blame] | 138 | conf.file("csupport/keys.c"); |
| 139 | |
Sherry Zhang | f4580cb | 2021-07-13 22:07:31 +0800 | [diff] [blame] | 140 | conf.file("../../ext/mbedtls/library/asn1parse.c"); |
| 141 | conf.file("../../ext/mbedtls/library/bignum.c"); |
| 142 | conf.file("../../ext/mbedtls/library/ecdsa.c"); |
| 143 | conf.file("../../ext/mbedtls/library/ecp.c"); |
| 144 | conf.file("../../ext/mbedtls/library/ecp_curves.c"); |
| 145 | conf.file("../../ext/mbedtls/library/platform.c"); |
| 146 | conf.file("../../ext/mbedtls/library/platform_util.c"); |
Fabio Utzig | 9771028 | 2019-05-24 17:44:49 -0300 | [diff] [blame] | 147 | } else if sig_ed25519 { |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 148 | conf.conf.define("MCUBOOT_SIGN_ED25519", None); |
| 149 | conf.conf.define("MCUBOOT_USE_TINYCRYPT", None); |
Fabio Utzig | 9771028 | 2019-05-24 17:44:49 -0300 | [diff] [blame] | 150 | |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 151 | conf.conf.include("../../ext/tinycrypt/lib/include"); |
| 152 | conf.conf.include("../../ext/tinycrypt-sha512/lib/include"); |
| 153 | conf.conf.include("../../ext/mbedtls/include"); |
Fabio Utzig | a1c142d | 2020-01-03 08:28:11 -0300 | [diff] [blame] | 154 | conf.file("../../ext/tinycrypt/lib/source/sha256.c"); |
| 155 | conf.file("../../ext/tinycrypt-sha512/lib/source/sha512.c"); |
| 156 | conf.file("../../ext/tinycrypt/lib/source/utils.c"); |
Fabio Utzig | 9771028 | 2019-05-24 17:44:49 -0300 | [diff] [blame] | 157 | conf.file("csupport/keys.c"); |
| 158 | conf.file("../../ext/fiat/src/curve25519.c"); |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 159 | conf.file("../../ext/mbedtls/library/platform_util.c"); |
| 160 | conf.file("../../ext/mbedtls/library/asn1parse.c"); |
Fabio Utzig | 3fa72ca | 2020-04-02 11:20:37 -0300 | [diff] [blame] | 161 | } else if !enc_ec256 && !enc_x25519 { |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 162 | // No signature type, only sha256 validation. The default |
Marti Bolivar | a4818a5 | 2018-04-12 13:02:38 -0400 | [diff] [blame] | 163 | // configuration file bundled with mbedTLS is sufficient. |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 164 | // When using ECIES-P256 rely on Tinycrypt. |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 165 | conf.conf.define("MCUBOOT_USE_MBED_TLS", None); |
| 166 | conf.conf.include("../../ext/mbedtls/include"); |
Sherry Zhang | f4580cb | 2021-07-13 22:07:31 +0800 | [diff] [blame] | 167 | conf.file("../../ext/mbedtls/library/sha256.c"); |
| 168 | conf.file("../../ext/mbedtls/library/platform_util.c"); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | if overwrite_only { |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 172 | conf.conf.define("MCUBOOT_OVERWRITE_ONLY", None); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 173 | } |
| 174 | |
Fabio Utzig | 031eb7d | 2019-11-28 10:13:14 -0300 | [diff] [blame] | 175 | if swap_move { |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 176 | conf.conf.define("MCUBOOT_SWAP_USING_MOVE", None); |
Andrzej Puzdrowski | 137d797 | 2021-05-13 13:39:30 +0200 | [diff] [blame] | 177 | } else if !overwrite_only { |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 178 | conf.conf.define("CONFIG_BOOT_SWAP_USING_SCRATCH", None); |
| 179 | conf.conf.define("MCUBOOT_SWAP_USING_SCRATCH", None); |
Fabio Utzig | 031eb7d | 2019-11-28 10:13:14 -0300 | [diff] [blame] | 180 | } |
| 181 | |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 182 | if enc_rsa || enc_aes256_rsa { |
| 183 | if enc_aes256_rsa { |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 184 | conf.conf.define("MCUBOOT_AES_256", None); |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 185 | } |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 186 | conf.conf.define("MCUBOOT_ENCRYPT_RSA", None); |
| 187 | conf.conf.define("MCUBOOT_ENC_IMAGES", None); |
| 188 | conf.conf.define("MCUBOOT_USE_MBED_TLS", None); |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 189 | |
| 190 | conf.file("../../boot/bootutil/src/encrypted.c"); |
| 191 | conf.file("csupport/keys.c"); |
| 192 | |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 193 | conf.conf.include("../../ext/mbedtls/include"); |
| 194 | conf.conf.include("../../ext/mbedtls/library"); |
Sherry Zhang | f4580cb | 2021-07-13 22:07:31 +0800 | [diff] [blame] | 195 | conf.file("../../ext/mbedtls/library/sha256.c"); |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 196 | |
Sherry Zhang | f4580cb | 2021-07-13 22:07:31 +0800 | [diff] [blame] | 197 | conf.file("../../ext/mbedtls/library/platform.c"); |
| 198 | conf.file("../../ext/mbedtls/library/platform_util.c"); |
| 199 | conf.file("../../ext/mbedtls/library/rsa.c"); |
| 200 | conf.file("../../ext/mbedtls/library/rsa_alt_helpers.c"); |
| 201 | conf.file("../../ext/mbedtls/library/md.c"); |
| 202 | conf.file("../../ext/mbedtls/library/aes.c"); |
| 203 | conf.file("../../ext/mbedtls/library/bignum.c"); |
| 204 | conf.file("../../ext/mbedtls/library/asn1parse.c"); |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 205 | } |
| 206 | |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 207 | if enc_kw || enc_aes256_kw { |
| 208 | if enc_aes256_kw { |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 209 | conf.conf.define("MCUBOOT_AES_256", None); |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 210 | } |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 211 | conf.conf.define("MCUBOOT_ENCRYPT_KW", None); |
| 212 | conf.conf.define("MCUBOOT_ENC_IMAGES", None); |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 213 | |
| 214 | conf.file("../../boot/bootutil/src/encrypted.c"); |
| 215 | conf.file("csupport/keys.c"); |
| 216 | |
Fabio Utzig | 3929743 | 2019-05-08 18:51:10 -0300 | [diff] [blame] | 217 | if sig_rsa || sig_rsa3072 { |
Sherry Zhang | f4580cb | 2021-07-13 22:07:31 +0800 | [diff] [blame] | 218 | conf.file("../../ext/mbedtls/library/sha256.c"); |
Fabio Utzig | b4d20c8 | 2018-12-27 16:08:39 -0200 | [diff] [blame] | 219 | } |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 220 | |
Fabio Utzig | b4d20c8 | 2018-12-27 16:08:39 -0200 | [diff] [blame] | 221 | /* Simulator uses Mbed-TLS to wrap keys */ |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 222 | conf.conf.include("../../ext/mbedtls/include"); |
Sherry Zhang | f4580cb | 2021-07-13 22:07:31 +0800 | [diff] [blame] | 223 | conf.file("../../ext/mbedtls/library/platform.c"); |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 224 | conf.conf.include("../../ext/mbedtls/library"); |
Sherry Zhang | f4580cb | 2021-07-13 22:07:31 +0800 | [diff] [blame] | 225 | conf.file("../../ext/mbedtls/library/platform_util.c"); |
| 226 | conf.file("../../ext/mbedtls/library/nist_kw.c"); |
| 227 | conf.file("../../ext/mbedtls/library/cipher.c"); |
| 228 | conf.file("../../ext/mbedtls/library/cipher_wrap.c"); |
| 229 | conf.file("../../ext/mbedtls/library/aes.c"); |
Fabio Utzig | b4d20c8 | 2018-12-27 16:08:39 -0200 | [diff] [blame] | 230 | |
| 231 | if sig_ecdsa { |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 232 | conf.conf.define("MCUBOOT_USE_TINYCRYPT", None); |
Fabio Utzig | b4d20c8 | 2018-12-27 16:08:39 -0200 | [diff] [blame] | 233 | |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 234 | conf.conf.include("../../ext/tinycrypt/lib/include"); |
Fabio Utzig | b4d20c8 | 2018-12-27 16:08:39 -0200 | [diff] [blame] | 235 | |
| 236 | conf.file("../../ext/tinycrypt/lib/source/utils.c"); |
| 237 | conf.file("../../ext/tinycrypt/lib/source/sha256.c"); |
| 238 | conf.file("../../ext/tinycrypt/lib/source/aes_encrypt.c"); |
| 239 | conf.file("../../ext/tinycrypt/lib/source/aes_decrypt.c"); |
Blaž Hrastnik | 4f4833d | 2020-09-14 13:53:31 +0900 | [diff] [blame] | 240 | conf.file("../../ext/tinycrypt/lib/source/ctr_mode.c"); |
Fabio Utzig | b4d20c8 | 2018-12-27 16:08:39 -0200 | [diff] [blame] | 241 | } |
Fabio Utzig | 9771028 | 2019-05-24 17:44:49 -0300 | [diff] [blame] | 242 | |
| 243 | if sig_ed25519 { |
| 244 | panic!("ed25519 does not support image encryption with KW yet"); |
| 245 | } |
Fabio Utzig | 1e48b91 | 2018-09-18 09:04:18 -0300 | [diff] [blame] | 246 | } |
| 247 | |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 248 | if enc_ec256 { |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 249 | conf.conf.define("MCUBOOT_ENCRYPT_EC256", None); |
| 250 | conf.conf.define("MCUBOOT_ENC_IMAGES", None); |
| 251 | conf.conf.define("MCUBOOT_USE_TINYCRYPT", None); |
| 252 | conf.conf.define("MCUBOOT_SWAP_SAVE_ENCTLV", None); |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 253 | |
| 254 | conf.file("../../boot/bootutil/src/encrypted.c"); |
| 255 | conf.file("csupport/keys.c"); |
| 256 | |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 257 | conf.conf.include("../../ext/mbedtls/include"); |
| 258 | conf.conf.include("../../ext/tinycrypt/lib/include"); |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 259 | |
| 260 | /* FIXME: fail with other signature schemes ? */ |
| 261 | |
| 262 | conf.file("../../ext/tinycrypt/lib/source/utils.c"); |
| 263 | conf.file("../../ext/tinycrypt/lib/source/sha256.c"); |
| 264 | conf.file("../../ext/tinycrypt/lib/source/ecc.c"); |
| 265 | conf.file("../../ext/tinycrypt/lib/source/ecc_dsa.c"); |
| 266 | conf.file("../../ext/tinycrypt/lib/source/ecc_platform_specific.c"); |
| 267 | |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 268 | conf.file("../../ext/mbedtls/library/platform_util.c"); |
| 269 | conf.file("../../ext/mbedtls/library/asn1parse.c"); |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 270 | |
| 271 | conf.file("../../ext/tinycrypt/lib/source/aes_encrypt.c"); |
| 272 | conf.file("../../ext/tinycrypt/lib/source/aes_decrypt.c"); |
| 273 | conf.file("../../ext/tinycrypt/lib/source/ctr_mode.c"); |
| 274 | conf.file("../../ext/tinycrypt/lib/source/hmac.c"); |
| 275 | conf.file("../../ext/tinycrypt/lib/source/ecc_dh.c"); |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 276 | } else if enc_ec256_mbedtls || enc_aes256_ec256 { |
| 277 | if enc_aes256_ec256 { |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 278 | conf.conf.define("MCUBOOT_AES_256", None); |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 279 | } |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 280 | conf.conf.define("MCUBOOT_ENCRYPT_EC256", None); |
| 281 | conf.conf.define("MCUBOOT_ENC_IMAGES", None); |
| 282 | conf.conf.define("MCUBOOT_USE_MBED_TLS", None); |
| 283 | conf.conf.define("MCUBOOT_SWAP_SAVE_ENCTLV", None); |
Fabio Utzig | 6c553d6 | 2021-05-06 19:56:18 -0300 | [diff] [blame] | 284 | |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 285 | conf.conf.include("../../ext/mbedtls/include"); |
Fabio Utzig | 6c553d6 | 2021-05-06 19:56:18 -0300 | [diff] [blame] | 286 | |
| 287 | conf.file("../../boot/bootutil/src/encrypted.c"); |
Sherry Zhang | f4580cb | 2021-07-13 22:07:31 +0800 | [diff] [blame] | 288 | conf.file("../../ext/mbedtls/library/sha256.c"); |
| 289 | conf.file("../../ext/mbedtls/library/asn1parse.c"); |
| 290 | conf.file("../../ext/mbedtls/library/bignum.c"); |
| 291 | conf.file("../../ext/mbedtls/library/ecdh.c"); |
| 292 | conf.file("../../ext/mbedtls/library/md.c"); |
| 293 | conf.file("../../ext/mbedtls/library/aes.c"); |
| 294 | conf.file("../../ext/mbedtls/library/ecp.c"); |
| 295 | conf.file("../../ext/mbedtls/library/ecp_curves.c"); |
| 296 | conf.file("../../ext/mbedtls/library/platform.c"); |
| 297 | conf.file("../../ext/mbedtls/library/platform_util.c"); |
Fabio Utzig | 6c553d6 | 2021-05-06 19:56:18 -0300 | [diff] [blame] | 298 | conf.file("csupport/keys.c"); |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 299 | } |
| 300 | |
Fabio Utzig | 3fa72ca | 2020-04-02 11:20:37 -0300 | [diff] [blame] | 301 | if enc_x25519 { |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 302 | conf.conf.define("MCUBOOT_ENCRYPT_X25519", None); |
| 303 | conf.conf.define("MCUBOOT_ENC_IMAGES", None); |
| 304 | conf.conf.define("MCUBOOT_USE_TINYCRYPT", None); |
| 305 | conf.conf.define("MCUBOOT_SWAP_SAVE_ENCTLV", None); |
Fabio Utzig | 3fa72ca | 2020-04-02 11:20:37 -0300 | [diff] [blame] | 306 | |
| 307 | conf.file("../../boot/bootutil/src/encrypted.c"); |
| 308 | conf.file("csupport/keys.c"); |
| 309 | |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 310 | conf.conf.include("../../ext/mbedtls/include"); |
| 311 | conf.conf.include("../../ext/tinycrypt/lib/include"); |
| 312 | conf.conf.include("../../ext/tinycrypt-sha512/lib/include"); |
Fabio Utzig | 3fa72ca | 2020-04-02 11:20:37 -0300 | [diff] [blame] | 313 | |
| 314 | conf.file("../../ext/fiat/src/curve25519.c"); |
| 315 | |
| 316 | conf.file("../../ext/tinycrypt/lib/source/utils.c"); |
| 317 | conf.file("../../ext/tinycrypt/lib/source/sha256.c"); |
| 318 | |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 319 | conf.file("../../ext/mbedtls/library/platform_util.c"); |
| 320 | conf.file("../../ext/mbedtls/library/asn1parse.c"); |
Fabio Utzig | 3fa72ca | 2020-04-02 11:20:37 -0300 | [diff] [blame] | 321 | |
| 322 | conf.file("../../ext/tinycrypt/lib/source/aes_encrypt.c"); |
| 323 | conf.file("../../ext/tinycrypt/lib/source/aes_decrypt.c"); |
| 324 | conf.file("../../ext/tinycrypt/lib/source/ctr_mode.c"); |
| 325 | conf.file("../../ext/tinycrypt/lib/source/hmac.c"); |
| 326 | } |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 327 | |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 328 | else if enc_aes256_x25519 { |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 329 | conf.conf.define("MCUBOOT_AES_256", None); |
| 330 | conf.conf.define("MCUBOOT_ENCRYPT_X25519", None); |
| 331 | conf.conf.define("MCUBOOT_ENC_IMAGES", None); |
| 332 | conf.conf.define("MCUBOOT_USE_MBED_TLS", None); |
| 333 | conf.conf.define("MCUBOOT_SWAP_SAVE_ENCTLV", None); |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 334 | |
| 335 | conf.file("../../boot/bootutil/src/encrypted.c"); |
| 336 | conf.file("csupport/keys.c"); |
| 337 | |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 338 | conf.conf.include("../../ext/mbedtls/include"); |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 339 | conf.file("../../ext/fiat/src/curve25519.c"); |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 340 | conf.file("../../ext/mbedtls/library/asn1parse.c"); |
Sherry Zhang | f4580cb | 2021-07-13 22:07:31 +0800 | [diff] [blame] | 341 | conf.file("../../ext/mbedtls/library/platform.c"); |
| 342 | conf.file("../../ext/mbedtls/library/platform_util.c"); |
| 343 | conf.file("../../ext/mbedtls/library/aes.c"); |
| 344 | conf.file("../../ext/mbedtls/library/sha256.c"); |
| 345 | conf.file("../../ext/mbedtls/library/md.c"); |
| 346 | conf.file("../../ext/mbedtls/library/sha512.c"); |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 347 | } |
| 348 | |
Fabio Utzig | 251ef1d | 2018-12-18 17:20:19 -0200 | [diff] [blame] | 349 | if sig_rsa && enc_kw { |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 350 | conf.conf.define("MBEDTLS_CONFIG_FILE", Some("<config-rsa-kw.h>")); |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 351 | } else if sig_rsa || sig_rsa3072 || enc_rsa || enc_aes256_rsa { |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 352 | conf.conf.define("MBEDTLS_CONFIG_FILE", Some("<config-rsa.h>")); |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 353 | } else if sig_ecdsa_mbedtls || enc_ec256_mbedtls || enc_aes256_ec256 { |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 354 | conf.conf.define("MBEDTLS_CONFIG_FILE", Some("<config-ec.h>")); |
Fabio Utzig | 90f449e | 2019-10-24 07:43:53 -0300 | [diff] [blame] | 355 | } else if (sig_ecdsa || enc_ec256) && !enc_kw { |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 356 | conf.conf.define("MBEDTLS_CONFIG_FILE", Some("<config-asn1.h>")); |
Fabio Utzig | 3fa72ca | 2020-04-02 11:20:37 -0300 | [diff] [blame] | 357 | } else if sig_ed25519 || enc_x25519 { |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 358 | conf.conf.define("MBEDTLS_CONFIG_FILE", Some("<config-asn1.h>")); |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 359 | } else if enc_kw || enc_aes256_kw { |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 360 | conf.conf.define("MBEDTLS_CONFIG_FILE", Some("<config-kw.h>")); |
Salome Thirot | 6fdbf55 | 2021-05-14 16:46:14 +0100 | [diff] [blame] | 361 | } else if enc_aes256_x25519 { |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 362 | conf.conf.define("MBEDTLS_CONFIG_FILE", Some("<config-ed25519.h>")); |
Fabio Utzig | 04fd63e | 2018-12-14 06:43:31 -0200 | [diff] [blame] | 363 | } |
| 364 | |
David Brown | 704ac6f | 2017-07-12 10:14:47 -0600 | [diff] [blame] | 365 | conf.file("../../boot/bootutil/src/image_validate.c"); |
Fabio Utzig | 3929743 | 2019-05-08 18:51:10 -0300 | [diff] [blame] | 366 | if sig_rsa || sig_rsa3072 { |
Fabio Utzig | c786540 | 2017-12-05 08:50:52 -0200 | [diff] [blame] | 367 | conf.file("../../boot/bootutil/src/image_rsa.c"); |
David Brown | 641af45 | 2021-02-19 12:16:48 -0700 | [diff] [blame] | 368 | } else if sig_ecdsa || sig_ecdsa_mbedtls { |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 369 | conf.conf.include("../../ext/mbedtls/include"); |
Antonio de Angelis | 10529d3 | 2023-04-21 21:43:14 +0100 | [diff] [blame] | 370 | conf.file("../../boot/bootutil/src/image_ecdsa.c"); |
Fabio Utzig | 9771028 | 2019-05-24 17:44:49 -0300 | [diff] [blame] | 371 | } else if sig_ed25519 { |
| 372 | conf.file("../../boot/bootutil/src/image_ed25519.c"); |
Fabio Utzig | c786540 | 2017-12-05 08:50:52 -0200 | [diff] [blame] | 373 | } |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 374 | conf.file("../../boot/bootutil/src/loader.c"); |
Fabio Utzig | 031eb7d | 2019-11-28 10:13:14 -0300 | [diff] [blame] | 375 | conf.file("../../boot/bootutil/src/swap_misc.c"); |
| 376 | conf.file("../../boot/bootutil/src/swap_scratch.c"); |
| 377 | conf.file("../../boot/bootutil/src/swap_move.c"); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 378 | conf.file("../../boot/bootutil/src/caps.c"); |
| 379 | conf.file("../../boot/bootutil/src/bootutil_misc.c"); |
Andrzej Puzdrowski | f573b39 | 2020-11-10 14:35:15 +0100 | [diff] [blame] | 380 | conf.file("../../boot/bootutil/src/bootutil_public.c"); |
Fabio Utzig | 61fd888 | 2019-09-14 20:00:20 -0300 | [diff] [blame] | 381 | conf.file("../../boot/bootutil/src/tlv.c"); |
Raef Coles | e8fe6cf | 2020-05-26 13:07:40 +0100 | [diff] [blame] | 382 | conf.file("../../boot/bootutil/src/fault_injection_hardening.c"); |
David Brown | d2b1853 | 2017-07-12 09:51:31 -0600 | [diff] [blame] | 383 | conf.file("csupport/run.c"); |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 384 | conf.conf.include("../../boot/bootutil/include"); |
| 385 | conf.conf.include("csupport"); |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 386 | conf.conf.debug(true); |
| 387 | conf.conf.flag("-Wall"); |
| 388 | conf.conf.flag("-Werror"); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 389 | |
Fabio Utzig | 0bccf9d | 2017-12-07 12:13:57 -0200 | [diff] [blame] | 390 | // FIXME: travis-ci still uses gcc 4.8.4 which defaults to std=gnu90. |
| 391 | // It has incomplete std=c11 and std=c99 support but std=c99 was checked |
| 392 | // to build correctly so leaving it here to updated in the future... |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 393 | conf.conf.flag("-std=c99"); |
Fabio Utzig | 0bccf9d | 2017-12-07 12:13:57 -0200 | [diff] [blame] | 394 | |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 395 | conf.conf.compile("libbootutil.a"); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 396 | |
| 397 | walk_dir("../../boot").unwrap(); |
Fabio Utzig | c786540 | 2017-12-05 08:50:52 -0200 | [diff] [blame] | 398 | walk_dir("../../ext/tinycrypt/lib/source").unwrap(); |
David Brown | b748f6f | 2019-10-11 10:07:31 -0600 | [diff] [blame] | 399 | walk_dir("../../ext/mbedtls-asn1").unwrap(); |
David Brown | d2b1853 | 2017-07-12 09:51:31 -0600 | [diff] [blame] | 400 | walk_dir("csupport").unwrap(); |
Sherry Zhang | f4580cb | 2021-07-13 22:07:31 +0800 | [diff] [blame] | 401 | walk_dir("../../ext/mbedtls/include").unwrap(); |
| 402 | walk_dir("../../ext/mbedtls/library").unwrap(); |
David Brown | 6390277 | 2017-07-12 09:47:49 -0600 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | // Output the names of all files within a directory so that Cargo knows when to rebuild. |
| 406 | fn walk_dir<P: AsRef<Path>>(path: P) -> io::Result<()> { |
| 407 | for ent in fs::read_dir(path.as_ref())? { |
| 408 | let ent = ent?; |
| 409 | let p = ent.path(); |
| 410 | if p.is_dir() { |
| 411 | walk_dir(p)?; |
| 412 | } else { |
| 413 | // Note that non-utf8 names will fail. |
| 414 | let name = p.to_str().unwrap(); |
| 415 | if name.ends_with(".c") || name.ends_with(".h") { |
| 416 | println!("cargo:rerun-if-changed={}", name); |
| 417 | } |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | Ok(()) |
| 422 | } |
David Brown | 5f4e148 | 2021-09-16 16:44:09 -0600 | [diff] [blame] | 423 | |
| 424 | /// Wrap the cc::Build type so that we can make sure that files are only added a single time. |
| 425 | /// Other methods can be passed through as needed. |
| 426 | struct CachedBuild { |
| 427 | conf: cc::Build, |
| 428 | seen: BTreeSet<PathBuf>, |
| 429 | } |
| 430 | |
| 431 | impl CachedBuild { |
| 432 | fn new() -> CachedBuild { |
| 433 | CachedBuild { |
| 434 | conf: cc::Build::new(), |
| 435 | seen: BTreeSet::new(), |
| 436 | } |
| 437 | } |
| 438 | |
| 439 | /// Works like `file` in the Build, but doesn't add a file if the same path has already been |
| 440 | /// given. |
| 441 | fn file<P: AsRef<Path>>(&mut self, p: P) -> &mut CachedBuild { |
| 442 | let p = p.as_ref(); |
| 443 | if !self.seen.contains(p) { |
| 444 | self.conf.file(p); |
| 445 | self.seen.insert(p.to_owned()); |
| 446 | } |
| 447 | self |
| 448 | } |
| 449 | } |