Skip to content

Commit ea5c73c

Browse files
authored
Merge pull request #156 from Pennycook/compiler-customization-schema
Add compiler customization schema
2 parents 01cf0b5 + cd2f953 commit ea5c73c

File tree

2 files changed

+300
-8
lines changed

2 files changed

+300
-8
lines changed

codebasin/schema/cbiconfig.schema

Lines changed: 138 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,146 @@
99
"type": "object",
1010
"patternProperties": {
1111
".*": {
12-
"type": "object",
13-
"properties": {
14-
"options": {
15-
"type": "array",
16-
"items": {
17-
"type": "string"
12+
"oneOf": [
13+
{
14+
"type": "object",
15+
"properties": {
16+
"options": {
17+
"type": "array",
18+
"items": {
19+
"type": "string"
20+
}
21+
},
22+
"parser": {
23+
"type": "array",
24+
"items": {
25+
"type": "object",
26+
"properties": {
27+
"flags": {
28+
"type": "array",
29+
"items": {
30+
"type": "string"
31+
}
32+
},
33+
"action": {
34+
"type": "string"
35+
},
36+
"dest": {
37+
"type": "string"
38+
},
39+
"const": {
40+
"type": "string"
41+
},
42+
"sep": {
43+
"type": "string"
44+
},
45+
"format": {
46+
"type": "string"
47+
},
48+
"pattern": {
49+
"type": "string"
50+
},
51+
"default": {
52+
"oneOf": [
53+
{
54+
"type": "string"
55+
},
56+
{
57+
"type": "array",
58+
"items": {
59+
"type": "string"
60+
}
61+
}
62+
]
63+
},
64+
"override": {
65+
"type": "boolean"
66+
}
67+
},
68+
"additionalProperties": false
69+
}
70+
},
71+
"modes": {
72+
"type": "array",
73+
"items": {
74+
"type": "object",
75+
"properties": {
76+
"name": {
77+
"type": "string"
78+
},
79+
"defines": {
80+
"type": "array",
81+
"items": {
82+
"type": "string"
83+
}
84+
},
85+
"include_paths": {
86+
"type": "array",
87+
"items": {
88+
"type": "string"
89+
}
90+
},
91+
"include_files": {
92+
"type": "array",
93+
"items": {
94+
"type": "string"
95+
}
96+
}
97+
},
98+
"required": ["name"],
99+
"additionalProperties": false
100+
}
101+
},
102+
"passes": {
103+
"type": "array",
104+
"items": {
105+
"type": "object",
106+
"properties": {
107+
"name": {
108+
"type": "string"
109+
},
110+
"defines": {
111+
"type": "array",
112+
"items": {
113+
"type": "string"
114+
}
115+
},
116+
"include_paths": {
117+
"type": "array",
118+
"items": {
119+
"type": "string"
120+
}
121+
},
122+
"include_files": {
123+
"type": "array",
124+
"items": {
125+
"type": "string"
126+
}
127+
},
128+
"modes": {
129+
"type": "array",
130+
"items": {
131+
"type": "string"
132+
}
133+
}
134+
},
135+
"required": ["name"],
136+
"additionalProperties": false
137+
}
18138
}
139+
},
140+
"additionalProperties": false
141+
},
142+
{
143+
"type": "object",
144+
"properties": {
145+
"alias_of": {
146+
"type": "string"
147+
}
148+
},
149+
"additionalProperties": false
19150
}
20-
},
21-
"additionalProperties": false
151+
]
22152
}
23153
}
24154
},

tests/schema/test_schema.py

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,21 @@
22
# SPDX-License-Identifier: BSD-3-Clause
33

44
import logging
5+
import tempfile
56
import unittest
67

78
import codebasin.config as config
89
import codebasin.util as util
910

1011

12+
def _test_toml_string(contents: str):
13+
f = tempfile.NamedTemporaryFile(suffix=".toml")
14+
f.write(contents.encode())
15+
f.seek(0)
16+
_ = util._load_toml(f, "cbiconfig")
17+
f.close()
18+
19+
1120
class TestSchema(unittest.TestCase):
1221
"""
1322
Test schema validation of input files.
@@ -45,6 +54,159 @@ def test_cbiconfig_file(self):
4554
with self.assertRaises(ValueError):
4655
toml = util._load_toml(f, "cbiconfig")
4756

57+
def test_cbiconfig_compilers(self):
58+
"""Check validation of compiler customization"""
59+
60+
# Compilers can be aliases of other compilers.
61+
_test_toml_string(
62+
"""
63+
[compiler.test]
64+
alias_of = "icc"
65+
""",
66+
)
67+
68+
# Compilers can have options.
69+
_test_toml_string(
70+
"""
71+
[compiler.test]
72+
options = ["-D", "ASDF"]
73+
""",
74+
)
75+
76+
# A compiler alias cannot have options.
77+
with self.assertRaises(ValueError):
78+
_test_toml_string(
79+
"""
80+
[compiler.test]
81+
alias_of = "icc"
82+
options = ["-D", "ASDF"]
83+
""",
84+
)
85+
86+
# A compiler cannot define any other keys.
87+
with self.assertRaises(ValueError):
88+
_test_toml_string(
89+
"""
90+
[compiler.test]
91+
additional = "True"
92+
""",
93+
)
94+
95+
# A compiler can define multiple parser options.
96+
_test_toml_string(
97+
"""
98+
[[compiler.test.parser]]
99+
action = "store_const"
100+
dest = "defines"
101+
const = "ASDF"
102+
103+
[[compiler.test.parser]]
104+
action = "store"
105+
dest = "defines"
106+
default = "one-value"
107+
108+
[[compiler.test.parser]]
109+
action = "append"
110+
dest = "defines"
111+
default = ["multiple", "values"]
112+
113+
[[compiler.test.parser]]
114+
action = "store_split"
115+
dest = "passes"
116+
sep = ","
117+
format = "$value"
118+
119+
[[compiler.test.parser]]
120+
action = "extend_match"
121+
dest = "passes"
122+
pattern = "*"
123+
format = "$value"
124+
override = true
125+
""",
126+
)
127+
128+
# A compiler cannot define any other parser keys.
129+
with self.assertRaises(ValueError):
130+
_test_toml_string(
131+
"""
132+
[[compiler.test.parser]]
133+
additional = "True"
134+
""",
135+
)
136+
137+
# A compiler can define multiple modes.
138+
_test_toml_string(
139+
"""
140+
[[compiler.test.modes]]
141+
name = "language"
142+
defines = ["LANGUAGE"]
143+
include_paths = ["/language/"]
144+
include_files = ["language.inc"]
145+
146+
[[compiler.test.modes]]
147+
name = "another-language"
148+
defines = ["ANOTHER_LANGUAGE"]
149+
include_paths = ["/another-language/"]
150+
include_files = ["another_language.inc"]
151+
""",
152+
)
153+
154+
# A compiler mode must have a name.
155+
with self.assertRaises(ValueError):
156+
_test_toml_string(
157+
"""
158+
[[compiler.test.modes]]
159+
defines = ["LANGUAGE"]
160+
""",
161+
)
162+
163+
# A compiler cannot define any other mode keys.
164+
with self.assertRaises(ValueError):
165+
_test_toml_string(
166+
"""
167+
[[compiler.test.modes]]
168+
name = "required"
169+
additional = "True"
170+
""",
171+
)
172+
173+
# A compiler can define multiple passes.
174+
_test_toml_string(
175+
"""
176+
[[compiler.test.passes]]
177+
name = "host"
178+
defines = ["HOST"]
179+
include_paths = ["/host/"]
180+
include_files = ["host.inc"]
181+
182+
[[compiler.test.passes]]
183+
name = "device"
184+
defines = ["DEVICE"]
185+
include_paths = ["/device/"]
186+
include_files = ["device.inc"]
187+
modes = ["device-mode"]
188+
""",
189+
)
190+
191+
# A compiler pass must have a name.
192+
with self.assertRaises(ValueError):
193+
_test_toml_string(
194+
"""
195+
[[compiler.test.passes]]
196+
defines = ["LANGUAGE"]
197+
""",
198+
)
199+
200+
# A compiler cannot define any other pass keys.
201+
with self.assertRaises(ValueError):
202+
_test_toml_string(
203+
"""
204+
[[compiler.test.passes]]
205+
name = "required"
206+
additional = "True"
207+
""",
208+
)
209+
48210
def test_analysis_file(self):
49211
"""schema/analysis_file"""
50212

0 commit comments

Comments
 (0)