-
Notifications
You must be signed in to change notification settings - Fork 0
Refactor CMake preset parser to own module #621
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
nikobockerman
merged 1 commit into
main
from
refactor-cmake-preset-parser-to-separate-module
Jul 21, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| """Minimal reader for CMake preset files. | ||
|
|
||
| CMake's preset schema (``CMakePresets.json`` / ``CMakeUserPresets.json``) is | ||
| large; this module implements only the slice the C++ solver build needs: | ||
|
|
||
| * looking up a preset by *type* and *name*, with user presets taking precedence | ||
| over project presets (mirroring CMake's own override order); | ||
| * finding the configure step of a workflow preset; | ||
| * resolving the ``binaryDir`` of a configure preset, walking ``inherits`` chains | ||
| when the field is not set on the preset directly. | ||
|
|
||
| Features such as ``include`` and macro expansion (e.g. ``${sourceDir}``) are | ||
| intentionally unsupported. | ||
| """ | ||
|
|
||
| import json | ||
| import pathlib | ||
| from functools import cached_property | ||
| from typing import Any, Literal | ||
|
|
||
| from aoc_main import _logging | ||
|
|
||
| _logger = _logging.logger | ||
|
|
||
| PresetType = Literal["build", "configure", "workflow"] | ||
|
|
||
|
|
||
| class CMakePresetError(Exception): | ||
| """Raised when the preset files cannot satisfy a requested lookup.""" | ||
|
|
||
|
|
||
| class CMakePresets: | ||
| """Reads CMake preset files from a directory and answers queries about them. | ||
|
|
||
| Pass the directory that contains ``CMakePresets.json`` (and optionally | ||
| ``CMakeUserPresets.json``). Files are read lazily and cached on first | ||
| access, so an instance reflects their contents at that point. A missing | ||
| file is treated as containing no presets. | ||
| """ | ||
|
|
||
| def __init__(self, root_dir: pathlib.Path) -> None: | ||
| self._root_dir = root_dir | ||
|
|
||
| @cached_property | ||
| def _user_presets(self) -> dict[str, Any]: | ||
| return self._load(self._root_dir / "CMakeUserPresets.json") | ||
|
|
||
| @cached_property | ||
| def _project_presets(self) -> dict[str, Any]: | ||
| return self._load(self._root_dir / "CMakePresets.json") | ||
|
|
||
| @staticmethod | ||
| def _load(preset_file: pathlib.Path) -> dict[str, Any]: | ||
| if not preset_file.exists(): | ||
| return {} | ||
| return json.loads(preset_file.read_text()) | ||
|
|
||
| def _get_preset(self, preset_type: PresetType, name: str) -> dict[str, Any] | None: | ||
| for presets in (self._user_presets, self._project_presets): | ||
| preset = next( | ||
| ( | ||
| preset | ||
| for preset in presets.get(f"{preset_type}Presets", []) | ||
| if preset["name"] == name | ||
| ), | ||
| None, | ||
| ) | ||
| if preset is not None: | ||
| return preset | ||
| return None | ||
|
nikobockerman marked this conversation as resolved.
|
||
|
|
||
| def workflow_configure_preset_name(self, workflow_name: str) -> str: | ||
| """Return the configure step's preset name for a workflow preset.""" | ||
| workflow_preset = self._get_preset("workflow", workflow_name) | ||
| if workflow_preset is None: | ||
| msg = f"Requested workflow preset not found: {workflow_name}" | ||
| raise CMakePresetError(msg) | ||
|
|
||
| try: | ||
| return next( | ||
| step["name"] | ||
| for step in workflow_preset["steps"] | ||
| if step["type"] == "configure" | ||
| ) | ||
| except KeyError as e: | ||
| msg = f"Invalid CMake Preset json for workflow preset: {workflow_name}" | ||
| raise CMakePresetError(msg) from e | ||
|
nikobockerman marked this conversation as resolved.
|
||
| except StopIteration: | ||
| msg = f"No configure step found for workflow: {workflow_name}" | ||
| raise CMakePresetError(msg) from None | ||
|
|
||
| def binary_dir(self, configure_preset_name: str) -> pathlib.Path: | ||
| """Resolve the ``binaryDir`` of a configure preset. | ||
|
|
||
| If the named preset does not set ``binaryDir`` itself, its ``inherits`` | ||
| parents are searched breadth-first and the first one that sets the field | ||
| wins. | ||
| """ | ||
| pending = [configure_preset_name] | ||
| while pending: | ||
| name = pending.pop(0) | ||
| preset = self._get_preset("configure", name) | ||
| if preset is None: | ||
| msg = f"Configure preset not found: {name}" | ||
| raise CMakePresetError(msg) | ||
|
|
||
| binary_dir = preset.get("binaryDir") | ||
| if binary_dir is not None: | ||
| _logger.debug("Found binaryDir from preset %s: %s", name, binary_dir) | ||
| return pathlib.Path(binary_dir) | ||
|
|
||
| _logger.debug( | ||
| "binaryDir not set on preset %s; checking inherited presets", name | ||
| ) | ||
| pending.extend(preset.get("inherits", [])) | ||
|
|
||
|
nikobockerman marked this conversation as resolved.
|
||
| msg = f"binaryDir not found for configure preset {configure_preset_name}" | ||
| raise CMakePresetError(msg) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.