diff options
| author | Dario Nieuwenhuis <[email protected]> | 2023-02-07 20:49:10 +0100 |
|---|---|---|
| committer | Dario Nieuwenhuis <[email protected]> | 2023-02-08 00:16:04 +0100 |
| commit | 1d841cc8ac74feacc4d231958ce2c46419ae3bda (patch) | |
| tree | 44dbe827369eceb661c287d2c47ea17f00878c11 /embassy-usb/build.rs | |
| parent | 4a224efe75c7986f5b3d8c5d6083fa17cb774f12 (diff) | |
usb: make max interface count configurable at compile time.
Diffstat (limited to 'embassy-usb/build.rs')
| -rw-r--r-- | embassy-usb/build.rs | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/embassy-usb/build.rs b/embassy-usb/build.rs new file mode 100644 index 000000000..524bdc2f6 --- /dev/null +++ b/embassy-usb/build.rs | |||
| @@ -0,0 +1,93 @@ | |||
| 1 | use std::collections::HashMap; | ||
| 2 | use std::fmt::Write; | ||
| 3 | use std::path::PathBuf; | ||
| 4 | use std::{env, fs}; | ||
| 5 | |||
| 6 | static CONFIGS: &[(&str, usize)] = &[ | ||
| 7 | // BEGIN AUTOGENERATED CONFIG FEATURES | ||
| 8 | // Generated by gen_config.py. DO NOT EDIT. | ||
| 9 | ("MAX_INTERFACE_COUNT", 4), | ||
| 10 | // END AUTOGENERATED CONFIG FEATURES | ||
| 11 | ]; | ||
| 12 | |||
| 13 | struct ConfigState { | ||
| 14 | value: usize, | ||
| 15 | seen_feature: bool, | ||
| 16 | seen_env: bool, | ||
| 17 | } | ||
| 18 | |||
| 19 | fn main() { | ||
| 20 | let crate_name = env::var("CARGO_PKG_NAME") | ||
| 21 | .unwrap() | ||
| 22 | .to_ascii_uppercase() | ||
| 23 | .replace('-', "_"); | ||
| 24 | |||
| 25 | // only rebuild if build.rs changed. Otherwise Cargo will rebuild if any | ||
| 26 | // other file changed. | ||
| 27 | println!("cargo:rerun-if-changed=build.rs"); | ||
| 28 | |||
| 29 | // Rebuild if config envvar changed. | ||
| 30 | for (name, _) in CONFIGS { | ||
| 31 | println!("cargo:rerun-if-env-changed={crate_name}_{name}"); | ||
| 32 | } | ||
| 33 | |||
| 34 | let mut configs = HashMap::new(); | ||
| 35 | for (name, default) in CONFIGS { | ||
| 36 | configs.insert( | ||
| 37 | *name, | ||
| 38 | ConfigState { | ||
| 39 | value: *default, | ||
| 40 | seen_env: false, | ||
| 41 | seen_feature: false, | ||
| 42 | }, | ||
| 43 | ); | ||
| 44 | } | ||
| 45 | |||
| 46 | let prefix = format!("{crate_name}_"); | ||
| 47 | for (var, value) in env::vars() { | ||
| 48 | if let Some(name) = var.strip_prefix(&prefix) { | ||
| 49 | let Some(cfg) = configs.get_mut(name) else { | ||
| 50 | panic!("Unknown env var {name}") | ||
| 51 | }; | ||
| 52 | |||
| 53 | let Ok(value) = value.parse::<usize>() else { | ||
| 54 | panic!("Invalid value for env var {name}: {value}") | ||
| 55 | }; | ||
| 56 | |||
| 57 | cfg.value = value; | ||
| 58 | cfg.seen_env = true; | ||
| 59 | } | ||
| 60 | |||
| 61 | if let Some(feature) = var.strip_prefix("CARGO_FEATURE_") { | ||
| 62 | if let Some(i) = feature.rfind('_') { | ||
| 63 | let name = &feature[..i]; | ||
| 64 | let value = &feature[i + 1..]; | ||
| 65 | if let Some(cfg) = configs.get_mut(name) { | ||
| 66 | let Ok(value) = value.parse::<usize>() else { | ||
| 67 | panic!("Invalid value for feature {name}: {value}") | ||
| 68 | }; | ||
| 69 | |||
| 70 | // envvars take priority. | ||
| 71 | if !cfg.seen_env { | ||
| 72 | if cfg.seen_feature { | ||
| 73 | panic!("multiple values set for feature {}: {} and {}", name, cfg.value, value); | ||
| 74 | } | ||
| 75 | |||
| 76 | cfg.value = value; | ||
| 77 | cfg.seen_feature = true; | ||
| 78 | } | ||
| 79 | } | ||
| 80 | } | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | let mut data = String::new(); | ||
| 85 | |||
| 86 | for (name, cfg) in &configs { | ||
| 87 | writeln!(&mut data, "pub const {}: usize = {};", name, cfg.value).unwrap(); | ||
| 88 | } | ||
| 89 | |||
| 90 | let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap()); | ||
| 91 | let out_file = out_dir.join("config.rs").to_string_lossy().to_string(); | ||
| 92 | fs::write(out_file, data).unwrap(); | ||
| 93 | } | ||
