build.rs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. use std::collections::HashMap;
  2. use std::fmt::Write;
  3. use std::path::PathBuf;
  4. use std::{env, fs};
  5. static CONFIGS: &[(&str, usize)] = &[
  6. // BEGIN AUTOGENERATED CONFIG FEATURES
  7. // Generated by gen_config.py. DO NOT EDIT.
  8. ("IFACE_MAX_ADDR_COUNT", 2),
  9. ("IFACE_MAX_MULTICAST_GROUP_COUNT", 4),
  10. ("IFACE_MAX_SIXLOWPAN_ADDRESS_CONTEXT_COUNT", 4),
  11. ("IFACE_NEIGHBOR_CACHE_COUNT", 4),
  12. ("IFACE_MAX_ROUTE_COUNT", 2),
  13. ("FRAGMENTATION_BUFFER_SIZE", 1500),
  14. ("ASSEMBLER_MAX_SEGMENT_COUNT", 4),
  15. ("REASSEMBLY_BUFFER_SIZE", 1500),
  16. ("REASSEMBLY_BUFFER_COUNT", 1),
  17. ("DNS_MAX_RESULT_COUNT", 1),
  18. ("DNS_MAX_SERVER_COUNT", 1),
  19. ("DNS_MAX_NAME_SIZE", 255),
  20. // END AUTOGENERATED CONFIG FEATURES
  21. ];
  22. struct ConfigState {
  23. value: usize,
  24. seen_feature: bool,
  25. seen_env: bool,
  26. }
  27. fn main() {
  28. // only rebuild if build.rs changed. Otherwise Cargo will rebuild if any
  29. // other file changed.
  30. println!("cargo:rerun-if-changed=build.rs");
  31. // Rebuild if config envvar changed.
  32. for (name, _) in CONFIGS {
  33. println!("cargo:rerun-if-env-changed=SMOLTCP_{name}");
  34. }
  35. let mut configs = HashMap::new();
  36. for (name, default) in CONFIGS {
  37. configs.insert(
  38. *name,
  39. ConfigState {
  40. value: *default,
  41. seen_env: false,
  42. seen_feature: false,
  43. },
  44. );
  45. }
  46. for (var, value) in env::vars() {
  47. if let Some(name) = var.strip_prefix("SMOLTCP_") {
  48. let Some(cfg) = configs.get_mut(name) else {
  49. panic!("Unknown env var {name}")
  50. };
  51. let Ok(value) = value.parse::<usize>() else {
  52. panic!("Invalid value for env var {name}: {value}")
  53. };
  54. cfg.value = value;
  55. cfg.seen_env = true;
  56. }
  57. if let Some(feature) = var.strip_prefix("CARGO_FEATURE_") {
  58. if let Some(i) = feature.rfind('_') {
  59. let name = &feature[..i];
  60. let value = &feature[i + 1..];
  61. if let Some(cfg) = configs.get_mut(name) {
  62. let Ok(value) = value.parse::<usize>() else {
  63. panic!("Invalid value for feature {name}: {value}")
  64. };
  65. // envvars take priority.
  66. if !cfg.seen_env {
  67. if cfg.seen_feature {
  68. panic!(
  69. "multiple values set for feature {}: {} and {}",
  70. name, cfg.value, value
  71. );
  72. }
  73. cfg.value = value;
  74. cfg.seen_feature = true;
  75. }
  76. }
  77. }
  78. }
  79. }
  80. let mut data = String::new();
  81. for (name, cfg) in &configs {
  82. writeln!(&mut data, "pub const {}: usize = {};", name, cfg.value).unwrap();
  83. }
  84. let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
  85. let out_file = out_dir.join("config.rs").to_string_lossy().to_string();
  86. fs::write(out_file, data).unwrap();
  87. }