feat(config): add vbs migrate to scaffold build.cpp from CMakeLists
This commit is contained in:
1 parent
1b803f7a22
commit
399b52c31c
10 files changed
+599
-1
No files matched your search
@@ -0,0 +1,440 @@
|
||||
#include <Void/Build/Config/CmakeImport.hpp>
|
||||
|
||||
#include <Void/Build/Util/Json.hpp>
|
||||
#include <Void/System/Path.hpp>
|
||||
#include <Void/System/Types.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cctype>
|
||||
#include <format>
|
||||
#include <map>
|
||||
#include <set>
|
||||
|
||||
namespace Void::Build::Config {
|
||||
namespace {
|
||||
|
||||
Void::Result::Error make_error(std::string message) {
|
||||
return Void::System::make_system_error(Void::System::SystemErrorCode::invalid_argument,
|
||||
std::move(message));
|
||||
}
|
||||
|
||||
struct Command {
|
||||
std::string name;
|
||||
std::vector<std::string> args;
|
||||
};
|
||||
|
||||
// Splits a CMake argument list on whitespace, honouring double quotes.
|
||||
std::vector<std::string> split_args(std::string_view text) {
|
||||
std::vector<std::string> args;
|
||||
std::string current;
|
||||
bool quoted = false;
|
||||
for (const char c : text) {
|
||||
if (c == '"') {
|
||||
quoted = !quoted;
|
||||
continue;
|
||||
}
|
||||
if (!quoted && (c == ' ' || c == '\t' || c == '\r' || c == '\n')) {
|
||||
if (!current.empty()) {
|
||||
args.push_back(current);
|
||||
current.clear();
|
||||
}
|
||||
continue;
|
||||
}
|
||||
current.push_back(c);
|
||||
}
|
||||
if (!current.empty()) {
|
||||
args.push_back(current);
|
||||
}
|
||||
return args;
|
||||
}
|
||||
|
||||
std::vector<Command> parse_commands(std::string_view text) {
|
||||
std::vector<Command> commands;
|
||||
std::size_t pos = 0;
|
||||
while (pos < text.size()) {
|
||||
// Skip whitespace and comments.
|
||||
while (pos < text.size() &&
|
||||
(text[pos] == ' ' || text[pos] == '\t' || text[pos] == '\r' || text[pos] == '\n')) {
|
||||
++pos;
|
||||
}
|
||||
if (pos < text.size() && text[pos] == '#') {
|
||||
while (pos < text.size() && text[pos] != '\n') {
|
||||
++pos;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if (pos >= text.size()) {
|
||||
break;
|
||||
}
|
||||
const auto name_start = pos;
|
||||
while (pos < text.size() &&
|
||||
(std::isalnum(static_cast<unsigned char>(text[pos])) != 0 || text[pos] == '_')) {
|
||||
++pos;
|
||||
}
|
||||
if (pos == name_start) {
|
||||
++pos; // Unexpected character; skip it.
|
||||
continue;
|
||||
}
|
||||
const std::string name{text.substr(name_start, pos - name_start)};
|
||||
while (pos < text.size() && std::isspace(static_cast<unsigned char>(text[pos])) != 0) {
|
||||
++pos;
|
||||
}
|
||||
if (pos >= text.size() || text[pos] != '(') {
|
||||
continue;
|
||||
}
|
||||
const auto args_start = ++pos;
|
||||
int depth = 1;
|
||||
bool quoted = false;
|
||||
while (pos < text.size() && depth > 0) {
|
||||
const char c = text[pos];
|
||||
if (c == '"') {
|
||||
quoted = !quoted;
|
||||
} else if (!quoted && c == '(') {
|
||||
++depth;
|
||||
} else if (!quoted && c == ')') {
|
||||
--depth;
|
||||
}
|
||||
++pos;
|
||||
}
|
||||
const auto args_end = pos > args_start ? pos - 1 : args_start;
|
||||
commands.push_back({name, split_args(text.substr(args_start, args_end - args_start))});
|
||||
}
|
||||
return commands;
|
||||
}
|
||||
|
||||
std::string expand_variables(const std::string &value,
|
||||
const std::map<std::string, std::string, std::less<>> &variables,
|
||||
std::vector<std::string> ¬es) {
|
||||
std::string out;
|
||||
std::size_t pos = 0;
|
||||
while (pos < value.size()) {
|
||||
const auto start = value.find("${", pos);
|
||||
if (start == std::string::npos) {
|
||||
out += value.substr(pos);
|
||||
break;
|
||||
}
|
||||
out += value.substr(pos, start - pos);
|
||||
const auto end = value.find('}', start);
|
||||
if (end == std::string::npos) {
|
||||
out += value.substr(start);
|
||||
break;
|
||||
}
|
||||
const auto name = value.substr(start + 2, end - start - 2);
|
||||
const auto it = variables.find(name);
|
||||
if (it == variables.end()) {
|
||||
notes.push_back("unresolved CMake variable ${" + name + "}");
|
||||
out += value.substr(start, end - start + 1);
|
||||
} else {
|
||||
out += it->second;
|
||||
}
|
||||
pos = end + 1;
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
bool is_source_like(const std::string &token) {
|
||||
static constexpr const char *kExtensions[] = {".c", ".cc", ".cpp", ".cxx", ".c++",
|
||||
".h", ".hh", ".hpp", ".hxx", ".mm"};
|
||||
return std::ranges::any_of(kExtensions, [&](const char *ext) { return token.ends_with(ext); });
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
Void::Result::Result<MigrationResult, Void::Result::Error>
|
||||
import_cmake_lists(const std::filesystem::path &cmake_file,
|
||||
const std::filesystem::path &project_root) {
|
||||
auto text = Void::FileSystem::read_file(cmake_file);
|
||||
if (!text) {
|
||||
return std::unexpected{text.error()};
|
||||
}
|
||||
|
||||
MigrationResult result;
|
||||
std::map<std::string, std::string, std::less<>> variables{
|
||||
{"CMAKE_CURRENT_SOURCE_DIR", project_root.string()},
|
||||
{"CMAKE_SOURCE_DIR", project_root.string()},
|
||||
{"PROJECT_SOURCE_DIR", project_root.string()}};
|
||||
std::map<std::string, std::size_t, std::less<>> target_index;
|
||||
std::vector<std::string> install_targets;
|
||||
std::vector<std::string> install_destinations;
|
||||
|
||||
const auto commands = parse_commands(*text);
|
||||
const auto expand = [&](std::vector<std::string> args) {
|
||||
for (auto &arg : args) {
|
||||
arg = expand_variables(arg, variables, result.notes);
|
||||
}
|
||||
return args;
|
||||
};
|
||||
|
||||
for (const auto &command : commands) {
|
||||
if (command.name == "project" && !command.args.empty()) {
|
||||
result.project_name = command.args.front();
|
||||
} else if (command.name == "set" && command.args.size() >= 2) {
|
||||
variables[command.args[0]] = command.args[1];
|
||||
} else if (command.name == "add_executable" || command.name == "add_library") {
|
||||
const auto args = expand(command.args);
|
||||
if (args.empty()) {
|
||||
continue;
|
||||
}
|
||||
MigratedTarget target;
|
||||
target.name = args[0];
|
||||
std::size_t first_source = 1;
|
||||
if (command.name == "add_executable") {
|
||||
target.kind = "executable";
|
||||
} else if (args.size() > 1 && args[1] == "SHARED") {
|
||||
target.kind = "shared";
|
||||
first_source = 2;
|
||||
} else if (args.size() > 1 && args[1] == "INTERFACE") {
|
||||
target.kind = "interface";
|
||||
first_source = 2;
|
||||
} else {
|
||||
target.kind = "static";
|
||||
if (args.size() > 1 && args[1] == "STATIC") {
|
||||
first_source = 2;
|
||||
}
|
||||
}
|
||||
for (std::size_t i = first_source; i < args.size(); ++i) {
|
||||
if (is_source_like(args[i])) {
|
||||
target.sources.push_back(args[i]);
|
||||
} else if (args[i] != "STATIC" && args[i] != "SHARED" && args[i] != "INTERFACE") {
|
||||
result.notes.push_back(std::format("{} '{}': ignored non-source token '{}'",
|
||||
command.name, target.name, args[i]));
|
||||
}
|
||||
}
|
||||
target_index[target.name] = result.targets.size();
|
||||
result.targets.push_back(std::move(target));
|
||||
} else if (command.name == "target_link_libraries") {
|
||||
const auto args = expand(command.args);
|
||||
if (args.size() < 2) {
|
||||
continue;
|
||||
}
|
||||
const auto it = target_index.find(args[0]);
|
||||
if (it == target_index.end()) {
|
||||
result.notes.push_back("target_link_libraries for unknown target '" + args[0] +
|
||||
"'");
|
||||
continue;
|
||||
}
|
||||
auto &target = result.targets[it->second];
|
||||
std::string scope = "public";
|
||||
for (std::size_t i = 1; i < args.size(); ++i) {
|
||||
if (args[i] == "PUBLIC" || args[i] == "PRIVATE" || args[i] == "INTERFACE") {
|
||||
scope = args[i] == "PUBLIC" ? "public"
|
||||
: args[i] == "PRIVATE" ? "private"
|
||||
: "public";
|
||||
continue;
|
||||
}
|
||||
if (args[i].starts_with("$<")) {
|
||||
result.notes.push_back("generator expression link item '" + args[i] + "'");
|
||||
continue;
|
||||
}
|
||||
(scope == "private" ? target.private_links : target.public_links)
|
||||
.push_back(args[i]);
|
||||
}
|
||||
} else if (command.name == "target_include_directories") {
|
||||
const auto args = expand(command.args);
|
||||
if (args.size() < 2) {
|
||||
continue;
|
||||
}
|
||||
const auto it = target_index.find(args[0]);
|
||||
if (it == target_index.end()) {
|
||||
result.notes.push_back("target_include_directories for unknown target '" + args[0] +
|
||||
"'");
|
||||
continue;
|
||||
}
|
||||
auto &target = result.targets[it->second];
|
||||
std::string scope = "public";
|
||||
for (std::size_t i = 1; i < args.size(); ++i) {
|
||||
if (args[i] == "PUBLIC" || args[i] == "PRIVATE" || args[i] == "INTERFACE") {
|
||||
scope = args[i] == "PRIVATE" ? "private" : "public";
|
||||
continue;
|
||||
}
|
||||
(scope == "private" ? target.private_includes : target.public_includes)
|
||||
.push_back(args[i]);
|
||||
}
|
||||
} else if (command.name == "target_compile_definitions") {
|
||||
const auto args = expand(command.args);
|
||||
if (args.size() < 2) {
|
||||
continue;
|
||||
}
|
||||
const auto it = target_index.find(args[0]);
|
||||
if (it == target_index.end()) {
|
||||
result.notes.push_back("target_compile_definitions for unknown target '" + args[0] +
|
||||
"'");
|
||||
continue;
|
||||
}
|
||||
auto &target = result.targets[it->second];
|
||||
std::string scope = "public";
|
||||
for (std::size_t i = 1; i < args.size(); ++i) {
|
||||
if (args[i] == "PUBLIC" || args[i] == "PRIVATE" || args[i] == "INTERFACE") {
|
||||
scope = args[i] == "PRIVATE" ? "private" : "public";
|
||||
continue;
|
||||
}
|
||||
(scope == "private" ? target.private_defines : target.public_defines)
|
||||
.push_back(args[i]);
|
||||
}
|
||||
} else if (command.name == "install") {
|
||||
// Only `install(TARGETS t1 t2 ... [DESTINATION dir])` is translated.
|
||||
std::size_t i = 0;
|
||||
if (!command.args.empty() && command.args[0] == "TARGETS") {
|
||||
i = 1;
|
||||
std::string destination;
|
||||
for (; i < command.args.size(); ++i) {
|
||||
if (command.args[i] == "DESTINATION" && i + 1 < command.args.size()) {
|
||||
destination = command.args[++i];
|
||||
continue;
|
||||
}
|
||||
if (command.args[i].starts_with("EXPORT") ||
|
||||
command.args[i].starts_with("COMPONENT") ||
|
||||
command.args[i].starts_with("RUNTIME") ||
|
||||
command.args[i].starts_with("LIBRARY") ||
|
||||
command.args[i].starts_with("ARCHIVE")) {
|
||||
continue;
|
||||
}
|
||||
install_targets.push_back(command.args[i]);
|
||||
install_destinations.push_back(destination);
|
||||
}
|
||||
} else if (!command.args.empty()) {
|
||||
result.notes.push_back("install(" + command.args.front() +
|
||||
" ...) is not translated");
|
||||
}
|
||||
} else if (command.name == "cmake_minimum_required" || command.name == "include" ||
|
||||
command.name == "message" || command.name == "option" ||
|
||||
command.name == "add_subdirectory" || command.name == "find_package" ||
|
||||
command.name == "if" || command.name == "endif" || command.name == "set" ||
|
||||
command.name == "target_compile_options" ||
|
||||
command.name == "target_compile_features") {
|
||||
if (command.name == "add_subdirectory" || command.name == "find_package") {
|
||||
result.notes.push_back(command.name + "(...) requires manual review");
|
||||
}
|
||||
} else {
|
||||
result.notes.push_back("unsupported CMake command '" + command.name + "'");
|
||||
}
|
||||
}
|
||||
|
||||
for (std::size_t i = 0; i < install_targets.size(); ++i) {
|
||||
const auto it = target_index.find(install_targets[i]);
|
||||
if (it == target_index.end()) {
|
||||
result.notes.push_back("install(TARGETS) for unknown target '" + install_targets[i] +
|
||||
"'");
|
||||
continue;
|
||||
}
|
||||
auto &target = result.targets[it->second];
|
||||
target.installed = true;
|
||||
if (!install_destinations[i].empty()) {
|
||||
target.install_destination = install_destinations[i];
|
||||
} else {
|
||||
target.install_destination = target.kind == "executable"
|
||||
? "bin"
|
||||
: (target.kind == "interface" ? "include" : "lib");
|
||||
}
|
||||
}
|
||||
if (result.project_name.empty()) {
|
||||
result.project_name = project_root.filename().string();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
std::string render_build_cpp(const MigrationResult &result) {
|
||||
using Util::json_quote;
|
||||
|
||||
// C++ braced initializer list, e.g. {"a", "b"}.
|
||||
const auto brace_list = [](const std::vector<std::string> &values) {
|
||||
std::string out{"{"};
|
||||
for (std::size_t i = 0; i < values.size(); ++i) {
|
||||
if (i > 0) {
|
||||
out += ", ";
|
||||
}
|
||||
out += Util::json_quote(values[i]);
|
||||
}
|
||||
out += '}';
|
||||
return out;
|
||||
};
|
||||
const auto identifier = [](const std::string &name) {
|
||||
std::string out{"t_"};
|
||||
for (const char c : name) {
|
||||
out.push_back(std::isalnum(static_cast<unsigned char>(c)) != 0 ? c : '_');
|
||||
}
|
||||
return out;
|
||||
};
|
||||
|
||||
std::string out = "// Generated by `vbs migrate` from CMakeLists.txt. Review before use.\n";
|
||||
out += "#include <Void/Build.hpp>\n\n";
|
||||
out += "void build(Void::Build::Project &project) {\n";
|
||||
if (!result.project_name.empty()) {
|
||||
out += " // Project: " + result.project_name + "\n";
|
||||
}
|
||||
|
||||
// Libraries first so executables can link them by name.
|
||||
std::vector<const MigratedTarget *> ordered;
|
||||
for (const auto &target : result.targets) {
|
||||
if (target.kind != "executable") {
|
||||
ordered.push_back(&target);
|
||||
}
|
||||
}
|
||||
for (const auto &target : result.targets) {
|
||||
if (target.kind == "executable") {
|
||||
ordered.push_back(&target);
|
||||
}
|
||||
}
|
||||
|
||||
for (const auto &target : ordered) {
|
||||
const std::string variable = identifier(target->name);
|
||||
out += "\n auto " + variable + " = project.";
|
||||
if (target->kind == "executable") {
|
||||
out += "executable(" + json_quote(target->name) + ")";
|
||||
} else if (target->kind == "shared") {
|
||||
out += "shared_library(" + json_quote(target->name) + ")";
|
||||
} else if (target->kind == "interface") {
|
||||
out += "header_only_library(" + json_quote(target->name) + ")";
|
||||
} else {
|
||||
out += "static_library(" + json_quote(target->name) + ")";
|
||||
}
|
||||
out += ";\n " + variable + ".cxx_standard(Void::Build::CxxStandard::Cxx26)";
|
||||
if (!target->sources.empty()) {
|
||||
out += "\n .sources(" + brace_list(target->sources) + ")";
|
||||
}
|
||||
for (const auto &dir : target->public_includes) {
|
||||
out += "\n .include_directory(" + json_quote(dir) +
|
||||
", Void::Build::Visibility::Public)";
|
||||
}
|
||||
for (const auto &dir : target->private_includes) {
|
||||
out += "\n .include_directory(" + json_quote(dir) +
|
||||
", Void::Build::Visibility::Private)";
|
||||
}
|
||||
for (const auto &define : target->public_defines) {
|
||||
out += "\n .define(" + json_quote(define) + ", Void::Build::Visibility::Public)";
|
||||
}
|
||||
for (const auto &define : target->private_defines) {
|
||||
out +=
|
||||
"\n .define(" + json_quote(define) + ", Void::Build::Visibility::Private)";
|
||||
}
|
||||
for (const auto &link : target->public_links) {
|
||||
const auto local = std::ranges::find_if(
|
||||
result.targets, [&](const MigratedTarget &t) { return t.name == link; });
|
||||
out += "\n .link(" +
|
||||
(local != result.targets.end() ? identifier(link) : json_quote(link)) +
|
||||
", Void::Build::Visibility::Public)";
|
||||
}
|
||||
for (const auto &link : target->private_links) {
|
||||
const auto local = std::ranges::find_if(
|
||||
result.targets, [&](const MigratedTarget &t) { return t.name == link; });
|
||||
out += "\n .link(" +
|
||||
(local != result.targets.end() ? identifier(link) : json_quote(link)) +
|
||||
", Void::Build::Visibility::Private)";
|
||||
}
|
||||
if (target->installed) {
|
||||
out += "\n .install(" + json_quote(target->install_destination) + ")";
|
||||
}
|
||||
out += ";\n";
|
||||
}
|
||||
|
||||
out += "}\n";
|
||||
if (!result.notes.empty()) {
|
||||
out += "\n// NOTES (untranslated or needs review):\n";
|
||||
for (const auto ¬e : result.notes) {
|
||||
out += "// - " + note + "\n";
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace Void::Build::Config
|
||||
Reference in new issue
Block a user