|
9 | 9 | import collections
|
10 | 10 | import logging
|
11 | 11 | import os
|
| 12 | +import pkgutil |
12 | 13 | import re
|
13 | 14 | import string
|
14 |
| -from dataclasses import asdict, dataclass |
| 15 | +import tomllib |
| 16 | +from dataclasses import asdict, dataclass, field |
| 17 | +from pathlib import Path |
| 18 | +from typing import Self |
15 | 19 |
|
16 | 20 | from codebasin import CompilationDatabase, util
|
17 | 21 |
|
18 | 22 | log = logging.getLogger(__name__)
|
19 | 23 |
|
20 | 24 |
|
21 | 25 | _importcfg = None
|
| 26 | +_compilers = None |
22 | 27 |
|
23 | 28 |
|
24 | 29 | def _parse_compiler_args(argv: list[str]):
|
@@ -151,6 +156,88 @@ def __call__(
|
151 | 156 | dest.extend(matches)
|
152 | 157 |
|
153 | 158 |
|
| 159 | +@dataclass |
| 160 | +class _CompilerMode: |
| 161 | + name: str |
| 162 | + defines: list[str] = field(default_factory=list) |
| 163 | + include_paths: list[str] = field(default_factory=list) |
| 164 | + include_files: list[str] = field(default_factory=list) |
| 165 | + |
| 166 | + @classmethod |
| 167 | + def from_toml(cls, toml: object) -> Self: |
| 168 | + return _CompilerMode(**toml) |
| 169 | + |
| 170 | + |
| 171 | +@dataclass |
| 172 | +class _CompilerPass: |
| 173 | + name: str |
| 174 | + defines: list[str] = field(default_factory=list) |
| 175 | + include_paths: list[str] = field(default_factory=list) |
| 176 | + include_files: list[str] = field(default_factory=list) |
| 177 | + modes: list[str] = field(default_factory=list) |
| 178 | + |
| 179 | + @classmethod |
| 180 | + def from_toml(cls, toml: object) -> Self: |
| 181 | + return _CompilerPass(**toml) |
| 182 | + |
| 183 | + |
| 184 | +@dataclass |
| 185 | +class _Compiler: |
| 186 | + alias_of: str | None = None |
| 187 | + options: list[str] = field(default_factory=list) |
| 188 | + parser: list[dict] = field(default_factory=list) |
| 189 | + modes: dict[str, _CompilerMode] = field(default_factory=dict) |
| 190 | + passes: dict[str, _CompilerPass] = field(default_factory=dict) |
| 191 | + |
| 192 | + @classmethod |
| 193 | + def from_toml(cls, toml: object) -> Self: |
| 194 | + kwargs = toml.copy() |
| 195 | + if "parser" in kwargs: |
| 196 | + for option in kwargs["parser"]: |
| 197 | + if option["action"] == "store_split": |
| 198 | + option["action"] = _StoreSplitAction |
| 199 | + if option["action"] == "extend_match": |
| 200 | + option["action"] = _ExtendMatchAction |
| 201 | + if "modes" in toml: |
| 202 | + kwargs["modes"] = { |
| 203 | + m["name"]: _CompilerMode.from_toml(m) for m in kwargs["modes"] |
| 204 | + } |
| 205 | + if "passes" in toml: |
| 206 | + kwargs["passes"] = { |
| 207 | + p["name"]: _CompilerPass.from_toml(p) for p in kwargs["passes"] |
| 208 | + } |
| 209 | + return _Compiler(**kwargs) |
| 210 | + |
| 211 | + |
| 212 | +def _load_compilers(): |
| 213 | + """ |
| 214 | + Load the configuration from the following files: |
| 215 | + - ${PACKAGE}/compilers/*.toml |
| 216 | + """ |
| 217 | + global _compilers |
| 218 | + _compilers = {} |
| 219 | + |
| 220 | + # Load the package-provided configuration files. |
| 221 | + for compiler in ["clang", "gnu", "intel", "nvidia"]: |
| 222 | + filename = str((Path("compilers") / compiler).with_suffix(".toml")) |
| 223 | + toml = tomllib.loads( |
| 224 | + pkgutil.get_data("codebasin", filename).decode(), |
| 225 | + ) |
| 226 | + try: |
| 227 | + util._validate_toml(toml, "cbiconfig") |
| 228 | + except ValueError as e: |
| 229 | + log.error(str(e)) |
| 230 | + return |
| 231 | + |
| 232 | + for name, definition in toml["compiler"].items(): |
| 233 | + _compilers[name] = _Compiler.from_toml(definition) |
| 234 | + |
| 235 | + |
| 236 | +# Load the compiler configuration when this module is imported. |
| 237 | +if not _compilers: |
| 238 | + _load_compilers() |
| 239 | + |
| 240 | + |
154 | 241 | @dataclass
|
155 | 242 | class PreprocessorConfiguration:
|
156 | 243 | """
|
|
0 commit comments