Skip to content

Commit b17adb9

Browse files
committed
refactor: resolution.json via mcpp.libs.json (nlohmann), not string concat
Use the existing json module for safe serialization/escaping instead of hand-built JSON.
1 parent 388217d commit b17adb9

1 file changed

Lines changed: 17 additions & 28 deletions

File tree

src/cli.cppm

Lines changed: 17 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ module;
1515
export module mcpp.cli;
1616

1717
import std;
18+
import mcpp.libs.json;
1819
import mcpp.manifest;
1920
import mcpp.modgraph.graph;
2021
import mcpp.modgraph.scanner;
@@ -3167,37 +3168,25 @@ prepare_build(bool print_fingerprint,
31673168
: ctx.tc.stdlibId == "libc++" ? "libc++"
31683169
: ctx.tc.compiler == mcpp::toolchain::CompilerId::MSVC ? "msvc"
31693170
: "glibc";
3170-
auto jstr = [](std::string_view s) {
3171-
std::string o = "\"";
3172-
for (char c : s) { if (c == '\\' || c == '"') o += '\\'; o += c; }
3173-
return o + "\"";
3171+
nlohmann::json j;
3172+
j["toolchain"] = {
3173+
{"spec", ctx.tc.label()}, {"abi", tcAbi},
3174+
{"triple", ctx.tc.targetTriple}, {"stdlib", ctx.tc.stdlibId},
3175+
};
3176+
nlohmann::json dirs = nlohmann::json::array();
3177+
for (auto& d : ctx.plan.runtimeLibraryDirs) dirs.push_back(d.string());
3178+
nlohmann::json caps = nlohmann::json::array();
3179+
for (auto& [cap, prov] : ctx.plan.runtimeProviders)
3180+
caps.push_back({{"capability", cap}, {"provider", prov}});
3181+
j["runtime"] = {
3182+
{"library_dirs", dirs},
3183+
{"dlopen_libs", ctx.plan.runtimeDlopenLibs},
3184+
{"capabilities", caps},
31743185
};
31753186
std::error_code ec;
31763187
std::filesystem::create_directories(ctx.plan.outputDir, ec);
3177-
std::ofstream js(ctx.plan.outputDir / "resolution.json");
3178-
if (js) {
3179-
js << "{\n";
3180-
js << " \"toolchain\": {\"spec\": " << jstr(ctx.tc.label())
3181-
<< ", \"abi\": " << jstr(tcAbi)
3182-
<< ", \"triple\": " << jstr(ctx.tc.targetTriple)
3183-
<< ", \"stdlib\": " << jstr(ctx.tc.stdlibId) << "},\n";
3184-
js << " \"runtime\": {\n";
3185-
js << " \"library_dirs\": [";
3186-
for (std::size_t i = 0; i < ctx.plan.runtimeLibraryDirs.size(); ++i)
3187-
js << (i ? ", " : "") << jstr(ctx.plan.runtimeLibraryDirs[i].string());
3188-
js << "],\n";
3189-
js << " \"dlopen_libs\": [";
3190-
for (std::size_t i = 0; i < ctx.plan.runtimeDlopenLibs.size(); ++i)
3191-
js << (i ? ", " : "") << jstr(ctx.plan.runtimeDlopenLibs[i]);
3192-
js << "],\n";
3193-
js << " \"capabilities\": [";
3194-
for (std::size_t i = 0; i < ctx.plan.runtimeProviders.size(); ++i) {
3195-
auto& [cap, prov] = ctx.plan.runtimeProviders[i];
3196-
js << (i ? ", " : "") << "{\"capability\": " << jstr(cap)
3197-
<< ", \"provider\": " << jstr(prov) << "}";
3198-
}
3199-
js << "]\n }\n}\n";
3200-
}
3188+
if (std::ofstream js(ctx.plan.outputDir / "resolution.json"); js)
3189+
js << j.dump(2) << "\n";
32013190
}
32023191

32033192
return ctx;

0 commit comments

Comments
 (0)