feat(config): import CMake configure presets
This commit is contained in:
1 parent
7772697a48
commit
083af6af79
10 files changed
+387
-1
No files matched your search
@@ -0,0 +1,235 @@
|
||||
#include <Void/Build/Config/Presets.hpp>
|
||||
|
||||
#include <Void/Data/StrictJson.hpp>
|
||||
#include <Void/System/Path.hpp>
|
||||
#include <Void/System/Types.hpp>
|
||||
#include <Void/Text/Str.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <format>
|
||||
#include <functional>
|
||||
#include <set>
|
||||
|
||||
namespace Void::Build::Config {
|
||||
namespace {
|
||||
|
||||
using JsonValue = Void::Json::Strict::Value;
|
||||
using JsonObject = JsonValue::Object;
|
||||
|
||||
Void::Result::Error make_error(std::string message) {
|
||||
return Void::System::make_system_error(Void::System::SystemErrorCode::invalid_argument,
|
||||
std::move(message));
|
||||
}
|
||||
|
||||
std::string lower(std::string_view text) { return Void::Str::to_lower_ascii(text); }
|
||||
|
||||
std::string map_profile(std::string_view build_type) {
|
||||
const std::string type = lower(build_type);
|
||||
if (type == "debug") {
|
||||
return "debug";
|
||||
}
|
||||
if (type == "release" || type == "relwithdebinfo" || type == "minsizerel") {
|
||||
return "release";
|
||||
}
|
||||
return "debug";
|
||||
}
|
||||
|
||||
std::string map_compiler(std::string_view compiler_path) {
|
||||
const std::string path = lower(compiler_path);
|
||||
if (path.find("clang") != std::string::npos) {
|
||||
return "clang";
|
||||
}
|
||||
return "gcc";
|
||||
}
|
||||
|
||||
std::string expand_source_dir(std::string value, const std::filesystem::path &project_root) {
|
||||
const std::string token{"${sourceDir}"};
|
||||
std::size_t pos = 0;
|
||||
while ((pos = value.find(token, pos)) != std::string::npos) {
|
||||
value.replace(pos, token.size(), project_root.string());
|
||||
pos += project_root.string().size();
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
Void::Result::Result<std::vector<ImportedPreset>, Void::Result::Error>
|
||||
load_cmake_presets(const std::filesystem::path &presets_file,
|
||||
const std::filesystem::path &project_root) {
|
||||
auto text = Void::FileSystem::read_file(presets_file);
|
||||
if (!text) {
|
||||
return std::unexpected{text.error()};
|
||||
}
|
||||
auto parsed = Void::Json::Strict::parse(*text);
|
||||
if (!parsed) {
|
||||
return std::unexpected{
|
||||
make_error(std::format("CMakePresets '{}' is not valid JSON", presets_file.string()))};
|
||||
}
|
||||
const auto *root = std::get_if<JsonObject>(&parsed->value);
|
||||
if (!root) {
|
||||
return std::unexpected{make_error("CMakePresets root must be a JSON object")};
|
||||
}
|
||||
const auto list_it = root->find("configurePresets");
|
||||
if (list_it == root->end()) {
|
||||
return std::vector<ImportedPreset>{};
|
||||
}
|
||||
const auto *presets = std::get_if<JsonValue::Array>(&list_it->second.value);
|
||||
if (!presets) {
|
||||
return std::unexpected{make_error("configurePresets must be a JSON array")};
|
||||
}
|
||||
|
||||
std::map<std::string, JsonObject, std::less<>> raw_by_name;
|
||||
std::vector<std::string> order;
|
||||
for (const auto &entry : *presets) {
|
||||
const auto *object = std::get_if<JsonObject>(&entry.value);
|
||||
if (!object) {
|
||||
continue;
|
||||
}
|
||||
const auto name_it = object->find("name");
|
||||
if (name_it == object->end()) {
|
||||
continue;
|
||||
}
|
||||
const auto *name = std::get_if<std::string>(&name_it->second.value);
|
||||
if (!name) {
|
||||
continue;
|
||||
}
|
||||
if (!raw_by_name.contains(*name)) {
|
||||
order.push_back(*name);
|
||||
}
|
||||
raw_by_name[*name] = *object;
|
||||
}
|
||||
|
||||
// Flattens `inherits` (string or array) depth-first with cycle detection.
|
||||
std::map<std::string, JsonObject, std::less<>> merged;
|
||||
std::function<Void::Result::Result<JsonObject, Void::Result::Error>(const std::string &,
|
||||
std::set<std::string>)>
|
||||
flatten = [&](const std::string &name, std::set<std::string> stack)
|
||||
-> Void::Result::Result<JsonObject, Void::Result::Error> {
|
||||
if (const auto it = merged.find(name); it != merged.end()) {
|
||||
return it->second;
|
||||
}
|
||||
const auto raw_it = raw_by_name.find(name);
|
||||
if (raw_it == raw_by_name.end()) {
|
||||
return std::unexpected{make_error(
|
||||
std::format("CMakePresets preset '{}' is inherited but not defined", name))};
|
||||
}
|
||||
if (!stack.insert(name).second) {
|
||||
return std::unexpected{
|
||||
make_error(std::format("CMakePresets inherits cycle at '{}'", name))};
|
||||
}
|
||||
JsonObject result;
|
||||
const auto inherits_it = raw_it->second.find("inherits");
|
||||
std::vector<std::string> parents;
|
||||
if (inherits_it != raw_it->second.end()) {
|
||||
if (const auto *single = std::get_if<std::string>(&inherits_it->second.value)) {
|
||||
parents.push_back(*single);
|
||||
} else if (const auto *many =
|
||||
std::get_if<JsonValue::Array>(&inherits_it->second.value)) {
|
||||
for (const auto &parent : *many) {
|
||||
if (const auto *text = std::get_if<std::string>(&parent.value)) {
|
||||
parents.push_back(*text);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for (const auto &parent : parents) {
|
||||
auto parent_merged = flatten(parent, stack);
|
||||
if (!parent_merged) {
|
||||
return std::unexpected{parent_merged.error()};
|
||||
}
|
||||
for (const auto &[key, value] : *parent_merged) {
|
||||
result[key] = value;
|
||||
}
|
||||
}
|
||||
for (const auto &[key, value] : raw_it->second) {
|
||||
if (key == "cacheVariables") {
|
||||
auto &target = result[key];
|
||||
if (target.value.index() == 0) {
|
||||
target = JsonValue{JsonObject{}};
|
||||
}
|
||||
auto *target_object = std::get_if<JsonObject>(&target.value);
|
||||
const auto *source_object = std::get_if<JsonObject>(&value.value);
|
||||
if (target_object != nullptr && source_object != nullptr) {
|
||||
for (const auto &[cache_key, cache_value] : *source_object) {
|
||||
(*target_object)[cache_key] = cache_value;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
result[key] = value;
|
||||
}
|
||||
}
|
||||
merged[name] = result;
|
||||
return result;
|
||||
};
|
||||
|
||||
std::vector<ImportedPreset> imported;
|
||||
for (const auto &name : order) {
|
||||
auto flat = flatten(name, {});
|
||||
if (!flat) {
|
||||
return std::unexpected{flat.error()};
|
||||
}
|
||||
ImportedPreset preset;
|
||||
preset.name = name;
|
||||
const auto read_string = [&](std::string_view key) -> std::string {
|
||||
const auto it = flat->find(std::string{key});
|
||||
if (it == flat->end()) {
|
||||
return {};
|
||||
}
|
||||
const auto *value = std::get_if<std::string>(&it->second.value);
|
||||
return value ? *value : std::string{};
|
||||
};
|
||||
preset.generator = read_string("generator");
|
||||
if (auto binary_dir = read_string("binaryDir"); !binary_dir.empty()) {
|
||||
preset.build_dir = expand_source_dir(binary_dir, project_root);
|
||||
}
|
||||
if (const auto cache_it = flat->find("cacheVariables"); cache_it != flat->end()) {
|
||||
if (const auto *cache = std::get_if<JsonObject>(&cache_it->second.value)) {
|
||||
for (const auto &[key, value] : *cache) {
|
||||
std::string text;
|
||||
if (const auto *as_text = std::get_if<std::string>(&value.value)) {
|
||||
text = *as_text;
|
||||
} else if (const auto *as_bool = std::get_if<bool>(&value.value)) {
|
||||
text = *as_bool ? "ON" : "OFF";
|
||||
}
|
||||
preset.cache_variables[key] = text;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (const auto bt = preset.cache_variables.find("CMAKE_BUILD_TYPE");
|
||||
bt != preset.cache_variables.end()) {
|
||||
preset.build_type = bt->second;
|
||||
}
|
||||
preset.profile = map_profile(preset.build_type);
|
||||
if (const auto cxx = preset.cache_variables.find("CMAKE_CXX_COMPILER");
|
||||
cxx != preset.cache_variables.end()) {
|
||||
preset.compiler = map_compiler(cxx->second);
|
||||
}
|
||||
imported.push_back(std::move(preset));
|
||||
}
|
||||
return imported;
|
||||
}
|
||||
|
||||
std::string suggested_build_command(const ImportedPreset &preset) {
|
||||
const auto quote = [](const std::string &value) {
|
||||
std::string out{"'"};
|
||||
for (const char c : value) {
|
||||
if (c == '\'') {
|
||||
out += "'\\''";
|
||||
} else {
|
||||
out.push_back(c);
|
||||
}
|
||||
}
|
||||
out.push_back('\'');
|
||||
return out;
|
||||
};
|
||||
std::string command = "vbs build";
|
||||
if (preset.build_dir) {
|
||||
command += " --build-dir " + quote(*preset.build_dir);
|
||||
}
|
||||
command += " --profile " + quote(preset.profile);
|
||||
command += " --compiler " + quote(preset.compiler);
|
||||
return command;
|
||||
}
|
||||
|
||||
} // namespace Void::Build::Config
|
||||
Reference in new issue
Block a user