Skip to content

refactor(build): compile_commands.json architecture redesign + nlohmann::json#25

Closed
Sunrisepeak wants to merge 6 commits into
mainfrom
feat/compile-commands
Closed

refactor(build): compile_commands.json architecture redesign + nlohmann::json#25
Sunrisepeak wants to merge 6 commits into
mainfrom
feat/compile-commands

Conversation

@Sunrisepeak
Copy link
Copy Markdown
Member

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 (arguments array format)
  • src/libs/json.cppm + json.hpp — nlohmann/json C++23 module wrapper (from xlings)

Changes

  • ninja_backend.cppm slimmed ~200 lines (uses flags.cppm)
  • compile_commands.json uses arguments array (clangd recommended) instead of command string
  • Output to <projectRoot>/ (clangd zero-config)
  • Incremental write (skip if unchanged)
  • All code passes .clang-format

Design doc

.agents/docs/2026-05-12-compile-commands-design.md

Relationship to PR #24

This branch includes PR #24's commits (.clang-format, .gitignore .sisyphus, initial compile_commands.json). The refactor commit sits on top without modifying the original commits.

Test plan

  • mcpp self-build succeeds (two rounds)
  • compile_commands.json generated: 39 entries, valid JSON, arguments format
  • .clang-format applied to all new/modified files
  • CI green

yizhinailong and others added 6 commits May 10, 2026 23:37
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.
@Sunrisepeak Sunrisepeak deleted the feat/compile-commands branch May 11, 2026 18:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants