refactor(build): compile_commands.json architecture redesign + nlohmann::json#25
Closed
Sunrisepeak wants to merge 6 commits into
Closed
refactor(build): compile_commands.json architecture redesign + nlohmann::json#25Sunrisepeak wants to merge 6 commits into
Sunrisepeak wants to merge 6 commits into
Conversation
Extract compute_cxxflags() and compute_cflags() from emit_ninja_string() into standalone helper functions in the anonymous namespace. These helpers encapsulate all flag computation logic (PIC, sysroot, binutils, musl opt, include dirs, user flags) and will be reused by compile_commands.json generation in a follow-up commit. The ninja output is byte-identical to before the refactoring.
Automatically emit a Clang-compatible compile_commands.json to the target/ directory on every mcpp build. The JSON includes all compile units (project + dependencies) with exact compiler commands, matching the flags used by the ninja backend. - Add json_escape() helper for JSON string escaping - Add emit_compile_commands_json() using shared flag helpers - Wire into NinjaBackend::build() after build.ninja generation
Architecture redesign on top of PR #24's compile_commands.json feature: 1. `src/build/flags.cppm` (NEW) — shared compile/link flag computation. One definition consumed by both ninja backend and compile_commands emitter (and future backends). Eliminates the duplicated compute_cxxflags/compute_cflags functions. 2. `src/build/compile_commands.cppm` (NEW) — generates compile_commands.json using nlohmann::json (no manual string escaping). Uses `arguments` array format (clangd recommended). Writes to <projectRoot>/compile_commands.json for zero-config clangd. Incremental: skips write if content unchanged. 3. `src/libs/json.cppm` + `src/libs/json/json.hpp` (NEW) — nlohmann/json single-header library wrapped as C++23 module, copied from openxlings/xlings. MIT license. 4. `src/build/ninja_backend.cppm` — slimmed: imports flags.cppm, delegates compile_commands to its module, removes ~200 lines of duplicated flag computation. 5. `.agents/docs/2026-05-12-compile-commands-design.md` — design doc. 6. `.gitignore` — add compile_commands.json (generated). 7. `mcpp.toml` — add `include_dirs = ["src/libs/json"]` for json.hpp. All code formatted with .clang-format from PR #24.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Builds on top of PR #24's compile_commands.json feature with proper architecture:
New modules
src/build/flags.cppm— shared flag computation (one definition, multiple consumers)src/build/compile_commands.cppm— JSON emitter using nlohmann::json (argumentsarray format)src/libs/json.cppm+json.hpp— nlohmann/json C++23 module wrapper (from xlings)Changes
ninja_backend.cppmslimmed ~200 lines (usesflags.cppm)compile_commands.jsonusesargumentsarray (clangd recommended) instead ofcommandstring<projectRoot>/(clangd zero-config).clang-formatDesign doc
.agents/docs/2026-05-12-compile-commands-design.mdRelationship to PR #24
This branch includes PR #24's commits (
.clang-format,.gitignore.sisyphus, initialcompile_commands.json). The refactor commit sits on top without modifying the original commits.Test plan
compile_commands.jsongenerated: 39 entries, valid JSON,argumentsformat.clang-formatapplied to all new/modified files