build.rs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. ("RPL_RELATIONS_BUFFER_COUNT", 16),
  21. ("RPL_PARENTS_BUFFER_COUNT", 8),
  22. // END AUTOGENERATED CONFIG FEATURES
  23. ];
  24. struct ConfigState {
  25. value: usize,
  26. seen_feature: bool,
  27. seen_env: bool,
  28. }
  29. fn main() {
  30. // only rebuild if build.rs changed. Otherwise Cargo will rebuild if any
  31. // other file changed.
  32. println!("cargo:rerun-if-changed=build.rs");
  33. // Rebuild if config envvar changed.
  34. for (name, _) in CONFIGS {
  35. println!("cargo:rerun-if-env-changed=SMOLTCP_{name}");
  36. }
  37. let mut configs = HashMap::new();
  38. for (name, default) in CONFIGS {
  39. configs.insert(
  40. *name,
  41. ConfigState {
  42. value: *default,
  43. seen_env: false,
  44. seen_feature: false,
  45. },
  46. );
  47. }
  48. for (var, value) in env::vars() {
  49. if let Some(name) = var.strip_prefix("SMOLTCP_") {
  50. let Some(cfg) = configs.get_mut(name) else {
  51. panic!("Unknown env var {name}")
  52. };
  53. let Ok(value) = value.parse::<usize>() else {
  54. panic!("Invalid value for env var {name}: {value}")
  55. };
  56. cfg.value = value;
  57. cfg.seen_env = true;
  58. }
  59. if let Some(feature) = var.strip_prefix("CARGO_FEATURE_") {
  60. if let Some(i) = feature.rfind('_') {
  61. let name = &feature[..i];
  62. let value = &feature[i + 1..];
  63. if let Some(cfg) = configs.get_mut(name) {
  64. let Ok(value) = value.parse::<usize>() else {
  65. panic!("Invalid value for feature {name}: {value}")
  66. };
  67. // envvars take priority.
  68. if !cfg.seen_env {
  69. if cfg.seen_feature {
  70. panic!(
  71. "multiple values set for feature {}: {} and {}",
  72. name, cfg.value, value
  73. );
  74. }
  75. cfg.value = value;
  76. cfg.seen_feature = true;
  77. }
  78. }
  79. }
  80. }
  81. }
  82. let mut data = String::new();
  83. for (name, cfg) in &configs {
  84. writeln!(&mut data, "pub const {}: usize = {};", name, cfg.value).unwrap();
  85. }
  86. let out_dir = PathBuf::from(env::var_os("OUT_DIR").unwrap());
  87. let out_file = out_dir.join("config.rs").to_string_lossy().to_string();
  88. fs::write(out_file, data).unwrap();
  89. }