Repository navigation
Expand file tree
/
Copy pathcompile_commands.cppm
More file actions
135 lines (113 loc) · 4.33 KB
/
Copy pathcompile_commands.cppm
File metadata and controls
135 lines (113 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// mcpp.build.compile_commands — generate compile_commands.json for IDE integration.
//
// Produces a Clang JSON Compilation Database (compile_commands.json)
// from the BuildPlan + CompileFlags using nlohmann::json for safe
// serialisation (no manual escaping).
//
// Uses the `arguments` array format (preferred over `command` string
// per clangd docs).
//
// Output location: <projectRoot>/compile_commands.json so clangd finds
// it via its default upward directory walk — zero configuration needed.
//
// See .agents/docs/2026-05-12-compile-commands-design.md.
export module mcpp.build.compile_commands;
import std;
import mcpp.build.plan;
import mcpp.build.flags;
import mcpp.libs.json;
export namespace mcpp::build {
// Generate compile_commands.json content as a string.
std::string emit_compile_commands(const BuildPlan& plan, const CompileFlags& flags);
// Write compile_commands.json to the project root.
void write_compile_commands(const BuildPlan& plan, const CompileFlags& flags);
} // namespace mcpp::build
namespace mcpp::build {
namespace {
bool is_c_source(const std::filesystem::path& src) {
return src.extension() == ".c";
}
// Split a flag string into individual tokens AND un-escape ninja-style
// path escapes (`$ ` → space, `$:` → `:`, `$$` → `$`).
//
// `flags.cppm::escape_path` ninja-escapes path arguments so they survive
// embedding in ninja rule strings. Those escaped strings are then captured
// into `f.cxx` / `f.cc` which is what we receive here. CDB consumers like
// clangd exec the `arguments` array literally — no ninja involved — so
// escaped chars must be undone or paths like `C:\Users\...` come through
// as `C$:\Users\...` and break clangd's path resolution on Windows. (The
// same issue would silently affect any POSIX path containing a space or
// `$` — those just happen to be rare.)
//
// Splitting and un-escaping in one pass is correct: a literal space inside
// a path appears as `$ ` in the input, which we must NOT treat as a token
// separator.
std::vector<std::string> split_flags(std::string_view s) {
std::vector<std::string> out;
std::string token;
auto flush = [&] {
if (!token.empty()) {
out.push_back(std::move(token));
token.clear();
}
};
for (std::size_t i = 0; i < s.size(); ++i) {
char c = s[i];
if (c == '$' && i + 1 < s.size()) {
char nc = s[i + 1];
if (nc == ' ' || nc == ':' || nc == '$') {
token.push_back(nc);
++i;
continue;
}
}
if (c == ' ') {
flush();
} else {
token.push_back(c);
}
}
flush();
return out;
}
} // namespace
std::string emit_compile_commands(const BuildPlan& plan, const CompileFlags& flags) {
nlohmann::json entries = nlohmann::json::array();
for (auto& cu : plan.compileUnits) {
// Pick compiler + flags based on source type.
const auto& compiler = is_c_source(cu.source) ? flags.ccBinary : flags.cxxBinary;
const auto& flagStr = is_c_source(cu.source) ? flags.cc : flags.cxx;
auto output_path = (plan.outputDir / cu.object).string();
// Build arguments array.
nlohmann::json args = nlohmann::json::array();
args.push_back(compiler.string());
for (auto& f : split_flags(flagStr))
args.push_back(std::move(f));
args.push_back("-c");
args.push_back(cu.source.string());
args.push_back("-o");
args.push_back(output_path);
nlohmann::json entry;
entry["directory"] = plan.projectRoot.string();
entry["file"] = cu.source.string();
entry["arguments"] = std::move(args);
entry["output"] = output_path;
entries.push_back(std::move(entry));
}
return entries.dump(2) + "\n";
}
void write_compile_commands(const BuildPlan& plan, const CompileFlags& flags) {
auto content = emit_compile_commands(plan, flags);
auto path = plan.projectRoot / "compile_commands.json";
// Only write if content changed (avoid triggering clangd re-index).
if (std::filesystem::exists(path)) {
std::ifstream is(path);
std::stringstream ss;
ss << is.rdbuf();
if (ss.str() == content)
return;
}
std::ofstream os(path);
os << content;
}
} // namespace mcpp::build