1import os
2
3abspath = os.path.abspath(__file__)
4dname = os.path.dirname(abspath)
5os.chdir(dname)
6
7features = []
8
9
10def feature(name, default, min, max, pow2=None):
11    vals = set()
12    val = min
13    while val <= max:
14        vals.add(val)
15        if pow2 == True or (isinstance(pow2, int) and val >= pow2):
16            val *= 2
17        else:
18            val += 1
19    vals.add(default)
20
21    features.append(
22        {
23            "name": name,
24            "default": default,
25            "vals": sorted(list(vals)),
26        }
27    )
28
29
30feature("iface_max_addr_count", default=2, min=1, max=8)
31feature("iface_max_multicast_group_count", default=4, min=1, max=1024, pow2=8)
32feature("iface_max_sixlowpan_address_context_count", default=4, min=1, max=1024, pow2=8)
33feature("iface_neighbor_cache_count", default=4, min=1, max=1024, pow2=8)
34feature("iface_max_route_count", default=2, min=1, max=1024, pow2=8)
35feature("fragmentation_buffer_size", default=1500, min=256, max=65536, pow2=True)
36feature("assembler_max_segment_count", default=4, min=1, max=32, pow2=4)
37feature("reassembly_buffer_size", default=1500, min=256, max=65536, pow2=True)
38feature("reassembly_buffer_count", default=1, min=1, max=32, pow2=4)
39feature("dns_max_result_count", default=1, min=1, max=32, pow2=4)
40feature("dns_max_server_count", default=1, min=1, max=32, pow2=4)
41feature("dns_max_name_size", default=255, min=64, max=255, pow2=True)
42
43# ========= Update Cargo.toml
44
45things = ""
46for f in features:
47    name = f["name"].replace("_", "-")
48    for val in f["vals"]:
49        things += f"{name}-{val} = []"
50        if val == f["default"]:
51            things += " # Default"
52        things += "\n"
53    things += "\n"
54
55SEPARATOR_START = "# BEGIN AUTOGENERATED CONFIG FEATURES\n"
56SEPARATOR_END = "# END AUTOGENERATED CONFIG FEATURES\n"
57HELP = "# Generated by gen_config.py. DO NOT EDIT.\n"
58with open("Cargo.toml", "r") as f:
59    data = f.read()
60before, data = data.split(SEPARATOR_START, maxsplit=1)
61_, after = data.split(SEPARATOR_END, maxsplit=1)
62data = before + SEPARATOR_START + HELP + things + SEPARATOR_END + after
63with open("Cargo.toml", "w") as f:
64    f.write(data)
65
66
67# ========= Update build.rs
68
69things = ""
70for f in features:
71    name = f["name"].upper()
72    things += f'    ("{name}", {f["default"]}),\n'
73
74SEPARATOR_START = "// BEGIN AUTOGENERATED CONFIG FEATURES\n"
75SEPARATOR_END = "// END AUTOGENERATED CONFIG FEATURES\n"
76HELP = "    // Generated by gen_config.py. DO NOT EDIT.\n"
77with open("build.rs", "r") as f:
78    data = f.read()
79before, data = data.split(SEPARATOR_START, maxsplit=1)
80_, after = data.split(SEPARATOR_END, maxsplit=1)
81data = before + SEPARATOR_START + HELP + things + "    " + SEPARATOR_END + after
82with open("build.rs", "w") as f:
83    f.write(data)
84