Skip to content

Commit a834794

Browse files
committed
Add range specifers for config values
1 parent 96191e2 commit a834794

File tree

11 files changed

+150
-8
lines changed

11 files changed

+150
-8
lines changed

tools/config/__init__.py

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,19 @@ def __init__(self, name, data, unit_name, unit_kind):
101101
data - the data associated with the configuration parameter
102102
unit_name - the unit (target/library/application) that defines this
103103
parameter
104-
unit_ kind - the kind of the unit ("target", "library" or "application")
104+
unit_kind - the kind of the unit ("target", "library" or "application")
105105
"""
106+
106107
self.name = self.get_full_name(name, unit_name, unit_kind,
107108
allow_prefix=False)
108109
self.defined_by = self.get_display_name(unit_name, unit_kind)
109110
self.set_value(data.get("value", None), unit_name, unit_kind)
110-
self.help_text = data.get("help", None)
111-
self.required = data.get("required", False)
112-
self.macro_name = data.get("macro_name", "MBED_CONF_%s" %
111+
self.value_min = data.get("value_min")
112+
self.value_max = data.get("value_max")
113+
self.accepted_values = data.get("accepted_values")
114+
self.help_text = data.get("help", None)
115+
self.required = data.get("required", False)
116+
self.macro_name = data.get("macro_name", "MBED_CONF_%s" %
113117
self.sanitize(self.name.upper()))
114118
self.config_errors = []
115119

@@ -749,7 +753,7 @@ def _generate_bootloader_build(self, rom_start, rom_size):
749753
if start > rom_start + rom_size:
750754
raise ConfigException("Not enough memory on device to fit all "
751755
"application regions")
752-
756+
753757
@staticmethod
754758
def _find_sector(address, sectors):
755759
target_size = -1
@@ -762,13 +766,13 @@ def _find_sector(address, sectors):
762766
if (target_size < 0):
763767
raise ConfigException("No valid sector found")
764768
return target_start, target_size
765-
769+
766770
@staticmethod
767771
def _align_floor(address, sectors):
768772
target_start, target_size = Config._find_sector(address, sectors)
769773
sector_num = (address - target_start) // target_size
770774
return target_start + (sector_num * target_size)
771-
775+
772776
@staticmethod
773777
def _align_ceiling(address, sectors):
774778
target_start, target_size = Config._find_sector(address, sectors)
@@ -1058,9 +1062,31 @@ def validate_config(self):
10581062
10591063
Arguments: None
10601064
"""
1065+
10611066
params, _ = self.get_config_data()
1067+
err_msg = ""
1068+
1069+
for name, param in sorted(params.items()):
1070+
min = param.value_min
1071+
max = param.value_max
1072+
accepted = param.accepted_values
1073+
value = param.value
1074+
1075+
if (value < min or (value > max if max is not None else False)):
1076+
err_msg += "\nInvalid config range for %s, is not in the required range: [%s:%s]"\
1077+
% (param,
1078+
min if min is not None else "-inf",
1079+
max if max is not None else "inf")
1080+
1081+
if accepted and value not in accepted:
1082+
err_msg += "\nInvalid config range for %s, is not an accepted value: %s"\
1083+
% (param, accepted)
1084+
1085+
if (err_msg):
1086+
raise ConfigException(err_msg)
1087+
10621088
for error in self.config_errors:
1063-
if (isinstance(error, UndefinedParameter) and
1089+
if (isinstance(error, UndefinedParameter) and
10641090
error.param in params):
10651091
continue
10661092
else:
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "lib1",
3+
"config": {
4+
"config1": {
5+
"help": "The default value should pass as it is in the list of accepted values",
6+
"value": 5,
7+
"accepted_values": [0, 5, 10]
8+
},
9+
"config2": {
10+
"help": "The default value should pass as it is in the rage of accepted values",
11+
"value": 7,
12+
"value_min": 0,
13+
"value_max": 10
14+
},
15+
"config3": {
16+
"help": "The default value should pass as it is in the rage of accepted values",
17+
"value": "foo",
18+
"accepted_values": ["foo", "bar"]
19+
},
20+
"config4": {
21+
"help": "The default value should pass as it is in the rage of accepted values",
22+
"value": false,
23+
"accepted_values": [true, false]
24+
}
25+
}
26+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"test_target": {
3+
"core": "Cortex-M0",
4+
"extra_labels": [],
5+
"features": [],
6+
"default_lib": "std",
7+
"supported_toolchains": ["GCC_ARM"]
8+
}
9+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"test_target": {
3+
"lib1.config1": 5,
4+
"lib1.config2": 7,
5+
"lib1.config3": "foo",
6+
"lib1.config4": false
7+
}
8+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"name": "lib1",
3+
"config": {
4+
"config1": {
5+
"help": "The default value should fail as it is not in the rage of accepted values",
6+
"value": 99,
7+
"accepted_values": [0, 5, 10]
8+
},
9+
"config2": {
10+
"help": "The default value should fail as it is not in the rage of accepted values",
11+
"value": 100,
12+
"value_min": 0,
13+
"value_max": 10
14+
},
15+
"config3": {
16+
"help": "The default value should fail as it is not in the rage of accepted values",
17+
"value": 101,
18+
"value_min": 102
19+
},
20+
"config4": {
21+
"help": "The default value should fail as it is not in the rage of accepted values",
22+
"value": 102,
23+
"value_max": 101
24+
}
25+
}
26+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"test_target": {
3+
"core": "Cortex-M0",
4+
"extra_labels": [],
5+
"features": [],
6+
"default_lib": "std",
7+
"supported_toolchains": ["GCC_ARM"]
8+
}
9+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"test_target": {
3+
"exception_msg": "\nInvalid config range for lib1.config1 = 99 (macro name: \"MBED_CONF_LIB1_CONFIG1\"), is not an accepted value: [0, 5, 10]\nInvalid config range for lib1.config2 = 100 (macro name: \"MBED_CONF_LIB1_CONFIG2\"), is not in the required range: [0:10]\nInvalid config range for lib1.config3 = 101 (macro name: \"MBED_CONF_LIB1_CONFIG3\"), is not in the required range: [102:inf]\nInvalid config range for lib1.config4 = 102 (macro name: \"MBED_CONF_LIB1_CONFIG4\"), is not in the required range: [-inf:101]"
4+
}
5+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "lib1",
3+
"config": {
4+
"config1": {
5+
"help": "The default value should pass, but will be overridden out of bounds and should error",
6+
"value": 8,
7+
"value_min": 0,
8+
"value_max": 10
9+
}
10+
}
11+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"target_overrides": {
3+
"test_target": {
4+
"lib1.config1": 12
5+
}
6+
}
7+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"test_target": {
3+
"core": "Cortex-M0",
4+
"extra_labels": [],
5+
"features": [],
6+
"default_lib": "std",
7+
"supported_toolchains": ["GCC_ARM"]
8+
}
9+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"test_target": {
3+
"lib1.config1": 8,
4+
"exception_msg": "Invalid config range for lib1.config1 = 12 (macro name: \"MBED_CONF_LIB1_CONFIG1\"), is not in the required range: [0:10]"
5+
}
6+
}

0 commit comments

Comments
 (0)