Skip to content

Commit 91dbf29

Browse files
committed
Remove dependency on nlohmann json
1 parent 2bcf15c commit 91dbf29

File tree

1 file changed

+38
-16
lines changed

1 file changed

+38
-16
lines changed

src/cmake_generator.cpp

Lines changed: 38 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
#include <fstream>
1111
#include <iomanip>
1212
#include <new>
13-
#include <nlohmann/json.hpp>
1413
#include <regex>
1514
#include <sstream>
1615
#include <stdexcept>
@@ -164,7 +163,7 @@ struct Command {
164163
}
165164
}
166165

167-
std::string quote(const std::string &str) {
166+
static std::string quote(const std::string &str) {
168167
// Don't quote arguments that don't need quoting
169168
if (str.find(' ') == std::string::npos && str.find('\"') == std::string::npos && str.find('/') == std::string::npos &&
170169
str.find(';') == std::string::npos) {
@@ -582,24 +581,47 @@ void generate_cmake(const char *path, const parser::Project *parent_project) {
582581
endl();
583582
// clang-format on
584583

585-
// Generate vcpkg.json
586-
using namespace nlohmann;
587-
json j;
588-
j["$cmkr"] = "This file is automatically generated from cmake.toml - DO NOT EDIT";
589-
j["$cmkr-url"] = cmkr_url;
590-
j["$schema"] = "https://raw.githubusercontent.com/microsoft/vcpkg/master/scripts/vcpkg.schema.json";
591-
j["name"] = vcpkg_escape_identifier(project.project_name);
592-
j["version-string"] = project.project_version;
593-
j["dependencies"] = project.vcpkg.packages;
594-
if (!project.project_description.empty()) {
595-
j["description"] = project.project_description;
596-
}
597-
584+
// Generate vcpkg.json (sorry for the ugly string handling, nlohmann compiles very slowly)
598585
std::ofstream ofs("vcpkg.json", std::ios::binary);
599586
if (!ofs) {
600587
throw std::runtime_error("Failed to create a vcpkg.json manifest file!");
601588
}
602-
ofs << std::setw(2) << j << std::endl;
589+
ofs << R"({
590+
"$cmkr": "This file is automatically generated from cmake.toml - DO NOT EDIT",
591+
"$cmkr-url": "https://github.com/build-cpp/cmkr",
592+
"$schema": "https://raw.githubusercontent.com/microsoft/vcpkg/master/scripts/vcpkg.schema.json",
593+
"dependencies": [
594+
)";
595+
596+
const auto &packages = project.vcpkg.packages;
597+
for (size_t i = 0; i < packages.size(); i++) {
598+
const auto &package = packages[i];
599+
if (!vcpkg_valid_identifier(package)) {
600+
throw std::runtime_error("Invalid vcpkg package name '" + package + "'");
601+
}
602+
ofs << " \"" << package << '\"';
603+
if (i + 1 < packages.size()) {
604+
ofs << ',';
605+
}
606+
ofs << '\n';
607+
}
608+
609+
auto escape = [](const std::string &str) {
610+
std::string result;
611+
for (auto ch : str) {
612+
if (ch == '\\' || ch == '\"') {
613+
result += '\\';
614+
}
615+
result += ch;
616+
}
617+
return result;
618+
};
619+
620+
ofs << " ],\n";
621+
ofs << " \"description\": \"" << escape(project.project_description) << "\",\n";
622+
ofs << " \"name\": \"" << escape(project.project_name) << "\",\n";
623+
ofs << R"( "version-string": "")" << '\n';
624+
ofs << "}\n";
603625
}
604626

605627
if (!project.packages.empty()) {

0 commit comments

Comments
 (0)