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
|
||||
@@ -3,6 +3,7 @@
|
||||
#include <Void/Build/Bridge/CMakeBridge.hpp>
|
||||
#include <Void/Build/Config/BuildTree.hpp>
|
||||
#include <Void/Build/Config/Frontend.hpp>
|
||||
#include <Void/Build/Config/Presets.hpp>
|
||||
#include <Void/Build/Config/Scaffold.hpp>
|
||||
#include <Void/Build/Daemon/Daemon.hpp>
|
||||
#include <Void/Build/Editor/CompDb.hpp>
|
||||
@@ -1331,6 +1332,40 @@ int execute_compdb_entries_command(const CompdbEntriesCmdOptions &opts) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct PresetsCmdOptions {
|
||||
std::string file_arg{"CMakePresets.json"};
|
||||
std::string project_arg;
|
||||
std::optional<std::string> name_arg;
|
||||
};
|
||||
|
||||
int execute_presets_command(const PresetsCmdOptions &opts) {
|
||||
auto presets = Config::load_cmake_presets(opts.file_arg, opts.project_arg);
|
||||
if (!presets) {
|
||||
std::cerr << "vbs presets: " << presets.error().message() << '\n';
|
||||
return 1;
|
||||
}
|
||||
if (opts.name_arg) {
|
||||
const auto match =
|
||||
std::ranges::find_if(*presets, [&](const Config::ImportedPreset &preset) {
|
||||
return preset.name == *opts.name_arg;
|
||||
});
|
||||
if (match == presets->end()) {
|
||||
std::cerr << "vbs presets: unknown preset '" << *opts.name_arg << "'\n";
|
||||
return 1;
|
||||
}
|
||||
std::cout << Config::suggested_build_command(*match) << '\n';
|
||||
return 0;
|
||||
}
|
||||
for (const auto &preset : *presets) {
|
||||
std::cout << "preset=" << preset.name << " profile=" << preset.profile
|
||||
<< " compiler=" << preset.compiler
|
||||
<< " build_dir=" << (preset.build_dir ? *preset.build_dir : std::string{"-"})
|
||||
<< " build_type=" << (preset.build_type.empty() ? "-" : preset.build_type)
|
||||
<< '\n';
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct VerifyCmdOptions {
|
||||
std::optional<std::string> name_arg;
|
||||
std::optional<std::string> version_arg;
|
||||
@@ -2728,6 +2763,7 @@ int main(int argc, char **argv) {
|
||||
TokensCmdOptions tokens_opts;
|
||||
LockCmdOptions lock_opts;
|
||||
VerifyCmdOptions verify_opts;
|
||||
PresetsCmdOptions presets_opts;
|
||||
CompdbEntriesCmdOptions compdb_entries_opts;
|
||||
publish_opts.project_arg = cwd;
|
||||
BridgeCmdOptions bridge_opts;
|
||||
@@ -2934,6 +2970,16 @@ int main(int argc, char **argv) {
|
||||
verify_opts.trusted_key_files);
|
||||
add_log_flags(verify_cmd, false); // `-v` is `--version` on verify.
|
||||
|
||||
auto &presets_cmd = root.subcommand(
|
||||
"presets", "Import CMake configure presets (CMakePresets.json) as VBS equivalents");
|
||||
presets_cmd
|
||||
.option_default("--file", "-f", "CMakePresets.json path", presets_opts.file_arg,
|
||||
"CMakePresets.json")
|
||||
.option_default("--project", "-C", "Project root for ${sourceDir}",
|
||||
presets_opts.project_arg, cwd)
|
||||
.option("--name", "-n", "Print the VBS command for one preset", presets_opts.name_arg);
|
||||
add_log_flags(presets_cmd);
|
||||
|
||||
auto &lock_cmd = root.subcommand("lock", "Inspect the project's vbs.lock");
|
||||
auto &lock_show_cmd = lock_cmd.subcommand("show", "Print lockfile contexts and entries");
|
||||
lock_show_cmd.option_default("--project", "-C", "Project directory", lock_opts.project_arg,
|
||||
@@ -3404,6 +3450,9 @@ int main(int argc, char **argv) {
|
||||
if (cmd == "verify") {
|
||||
return execute_verify_command(verify_opts);
|
||||
}
|
||||
if (cmd == "presets") {
|
||||
return execute_presets_command(presets_opts);
|
||||
}
|
||||
if (cmd == "compdb-entries") {
|
||||
return execute_compdb_entries_command(compdb_entries_opts);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user