|
6 | 6 | """
|
7 | 7 |
|
8 | 8 | import argparse
|
9 |
| -import collections |
10 | 9 | import logging
|
11 | 10 | import os
|
12 | 11 | import pkgutil
|
|
22 | 21 |
|
23 | 22 | log = logging.getLogger(__name__)
|
24 | 23 |
|
25 |
| - |
26 |
| -_importcfg = None |
27 | 24 | _compilers = None
|
28 | 25 |
|
29 | 26 |
|
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 |
| - |
49 | 27 | class _StoreSplitAction(argparse.Action):
|
50 | 28 | """
|
51 | 29 | A custom argparse.Action that splits the value based on a user-provided
|
@@ -210,13 +188,65 @@ def _load_compilers():
|
210 | 188 | _compilers[name] = _Compiler.from_toml(definition)
|
211 | 189 |
|
212 | 190 | # 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) |
220 | 250 |
|
221 | 251 |
|
222 | 252 | # Load the compiler configuration when this module is imported.
|
|
0 commit comments