Skip to content

Commit 3cb70b9

Browse files
committed
Add support for user-defined compilers
Replaces the original _importcfg with support for completely user-defined compilers (and argument parsers). Signed-off-by: John Pennycook <john.pennycook@intel.com>
1 parent 08e141b commit 3cb70b9

File tree

2 files changed

+59
-30
lines changed

2 files changed

+59
-30
lines changed

codebasin/config.py

Lines changed: 59 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
"""
77

88
import argparse
9-
import collections
109
import logging
1110
import os
1211
import pkgutil
@@ -22,30 +21,9 @@
2221

2322
log = logging.getLogger(__name__)
2423

25-
26-
_importcfg = None
2724
_compilers = None
2825

2926

30-
def load_importcfg():
31-
"""
32-
Load the import configuration file, if it exists.
33-
"""
34-
global _importcfg
35-
_importcfg = collections.defaultdict(list)
36-
path = ".cbi/config"
37-
if os.path.exists(path):
38-
log.info(f"Found configuration file at {path}")
39-
with open(path, "rb") as f:
40-
try:
41-
_importcfg_toml = util._load_toml(f, "cbiconfig")
42-
except ValueError as e:
43-
log.error(str(e))
44-
return
45-
for name, compiler in _importcfg_toml["compiler"].items():
46-
_importcfg[name] = compiler["options"]
47-
48-
4927
class _StoreSplitAction(argparse.Action):
5028
"""
5129
A custom argparse.Action that splits the value based on a user-provided
@@ -210,13 +188,65 @@ def _load_compilers():
210188
_compilers[name] = _Compiler.from_toml(definition)
211189

212190
# Check for any user-defined compiler behavior.
213-
# Currently, users can only override default defines.
214-
if _importcfg is None:
215-
load_importcfg()
216-
for name in _importcfg.keys():
217-
if name not in _compilers:
218-
_compilers[name] = _Compiler()
219-
_compilers[name].options.extend(_importcfg[name])
191+
path = ".cbi/config"
192+
if os.path.exists(path):
193+
log.info(f"Found configuration file at {path}.")
194+
with open(path, "rb") as f:
195+
try:
196+
toml = util._load_toml(f, "cbiconfig")
197+
except ValueError as e:
198+
log.error(str(e))
199+
return
200+
201+
for name, definition in toml["compiler"].items():
202+
# Check if the user is defining a new compiler.
203+
if name not in _compilers:
204+
_compilers[name] = _Compiler.from_toml(definition)
205+
continue
206+
207+
compiler = _compilers[name]
208+
209+
# Check if the user is redefining a compiler as an alias.
210+
# Warn because options may be lost.
211+
if "alias_of" in definition:
212+
log.warning(
213+
f'{name} redefined as alias of {definition["alias_of"]}.',
214+
)
215+
_compilers[name] = _Compiler.from_toml(definition)
216+
continue
217+
218+
# Check if the user is redefining an alias as a compiler.
219+
# Warn because options may be lost.
220+
if compiler.alias_of:
221+
log.warning(
222+
f"definition of {name} in .cbi/config overrides alias.",
223+
)
224+
compiler.alias_of = None
225+
226+
# Append options, parser options, modes and passes.
227+
# Warn if modes/passes are redefined because options may be lost.
228+
if "options" in definition:
229+
compiler.options.extend(definition["options"])
230+
if "parser" in definition:
231+
for option in definition["parser"]:
232+
option = option.copy()
233+
if option["action"] == "store_split":
234+
option["action"] = _StoreSplitAction
235+
if option["action"] == "extend_match":
236+
option["action"] = _ExtendMatchAction
237+
compiler.parser.append(option)
238+
if "modes" in definition:
239+
for m in definition["modes"]:
240+
name = m["name"]
241+
if name in compiler.modes:
242+
log.warning(f"compiler mode '{name}' redefined")
243+
compiler.modes[name] = _CompilerMode.from_toml(m)
244+
if "passes" in definition:
245+
for p in definition["passes"]:
246+
name = p["name"]
247+
if name in compiler.passes:
248+
log.warning(f"compiler pass '{name}' redefined")
249+
compiler.passes[name] = _CompilerPass.from_toml(p)
220250

221251

222252
# Load the compiler configuration when this module is imported.

tests/compilers/test_compilers.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,6 @@ def test_user_options(self):
183183
with open(path / ".cbi" / "config", mode="w") as f:
184184
f.write('[compiler."c++"]\n')
185185
f.write('options = ["-D", "ASDF"]\n')
186-
config._importcfg = None
187186
config._load_compilers()
188187

189188
argv = [

0 commit comments

Comments
 (0)