build.rs 3.1 KB

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