122 lines
4.2 KiB
C++
122 lines
4.2 KiB
C++
#include "VoidLibraryRecipe.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <filesystem>
|
|
#include <map>
|
|
#include <set>
|
|
#include <stdexcept>
|
|
|
|
namespace Void::Build::Recipes {
|
|
namespace {
|
|
|
|
struct ModuleDef {
|
|
std::vector<std::string> internal_deps;
|
|
bool needs_threads{false};
|
|
};
|
|
|
|
const std::map<std::string, ModuleDef, std::less<>> &known_modules() {
|
|
static const std::map<std::string, ModuleDef, std::less<>> kDefs = {
|
|
{"Core", {{}, false}},
|
|
{"Functional", {{}, false}},
|
|
{"Math", {{}, false}},
|
|
{"Algorithms", {{"Core", "Math"}, false}},
|
|
{"Memory", {{"Core"}, false}},
|
|
{"Time", {{}, false}},
|
|
{"Text", {{}, false}},
|
|
{"Unicode", {{"Core", "Text"}, false}},
|
|
{"LogState", {{}, false}},
|
|
{"Data", {{"Core", "Math", "Time"}, false}},
|
|
{"System", {{"Functional", "Time", "LogState"}, true}},
|
|
{"Async", {{"Functional", "Time", "LogState"}, true}},
|
|
{"Crypto", {{"Functional"}, false}},
|
|
{"Archive", {{"Functional", "Time", "System"}, false}},
|
|
{"Configuration", {{"Core", "Functional", "System", "Data", "Time", "LogState"}, true}},
|
|
{"Testing", {{"Core", "Functional", "Time", "LogState", "Configuration", "Text"}, true}},
|
|
};
|
|
return kDefs;
|
|
}
|
|
|
|
std::vector<std::string> collect_sources(const std::filesystem::path &src_dir) {
|
|
std::vector<std::string> files;
|
|
std::error_code ec;
|
|
if (!std::filesystem::is_directory(src_dir, ec)) {
|
|
return files;
|
|
}
|
|
for (const auto &ent : std::filesystem::recursive_directory_iterator(src_dir, ec)) {
|
|
if (ent.is_regular_file(ec) && ent.path().extension() == ".cpp") {
|
|
files.push_back(ent.path().lexically_normal().string());
|
|
}
|
|
}
|
|
std::ranges::sort(files);
|
|
return files;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
VoidLibraryTargets add_void_library_native(Project &project,
|
|
const std::filesystem::path &void_source_dir,
|
|
const std::vector<std::string> &enabled_modules) {
|
|
const auto &defs = known_modules();
|
|
|
|
// Compute transitive module closure in deterministic topological order.
|
|
std::set<std::string> visited;
|
|
std::vector<std::string> topo;
|
|
auto visit = [&](auto &&self, const std::string &mod) -> void {
|
|
if (!visited.insert(mod).second) {
|
|
return;
|
|
}
|
|
const auto it = defs.find(mod);
|
|
if (it == defs.end()) {
|
|
throw std::invalid_argument{"VoidLibraryRecipe: unsupported native module '" + mod +
|
|
"'"};
|
|
}
|
|
for (const auto &dep : it->second.internal_deps) {
|
|
self(self, dep);
|
|
}
|
|
topo.push_back(mod);
|
|
};
|
|
for (const auto &mod : enabled_modules) {
|
|
visit(visit, mod);
|
|
}
|
|
|
|
const auto include_dir = (void_source_dir / "include").lexically_normal().string();
|
|
VoidLibraryTargets result;
|
|
|
|
for (const auto &mod : topo) {
|
|
const auto &def = defs.at(mod);
|
|
const auto srcs = collect_sources(void_source_dir / "src" / mod);
|
|
const std::string target_name = "Void_" + mod;
|
|
|
|
Target tgt = srcs.empty() ? project.header_only_library(target_name)
|
|
: project.static_library(target_name);
|
|
tgt.cxx_standard(CxxStandard::Cxx26)
|
|
.include_directory(include_dir, Visibility::Public)
|
|
.compile_option("-freflection", Visibility::Public);
|
|
|
|
for (const auto &s : srcs) {
|
|
tgt.sources(SourceSet::file(s));
|
|
}
|
|
for (const auto &dep : def.internal_deps) {
|
|
tgt.link(result.modules.at(dep), Visibility::Public);
|
|
}
|
|
if (def.needs_threads) {
|
|
tgt.link_option("-pthread", Visibility::Public);
|
|
}
|
|
if (mod == "Crypto") {
|
|
tgt.define("VOID_CRYPTO_HAS_OPENSSL=0", Visibility::Public);
|
|
}
|
|
if (mod == "Archive") {
|
|
tgt.define("VOID_ARCHIVE_HAS_LIBARCHIVE=0", Visibility::Public);
|
|
}
|
|
if (mod == "Testing") {
|
|
tgt.define("VOID_HAS_CONFIGURATION", Visibility::Public)
|
|
.define("VOID_HAS_TEXT", Visibility::Public);
|
|
}
|
|
result.modules.emplace(mod, tgt);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
} // namespace Void::Build::Recipes
|