Files
Build-System/src/Build.cpp
T
faumaray 2970fbe25c
VoidBuildSystem CI / build-and-test-gcc16 (push) Failing after 4s
feat(workspace): add multi-package workspace builds with shared lock and CAS
- SDK: Workspace/workspace() declaration plus Project::workspace_dependency()
- Frontend: workspace.cpp discovery/evaluation alongside build.cpp
- Main: per-member builds sharing the root vbs.lock and build/.vbs CAS, --member and --changed-since selection
- Cross-member native linking; member builds do not steal the root compile_commands.json alias
- Path-dependency snapshots exclude generated editor metadata
- Verified in Docker GCC 16: 8/8 CTest suites; 3-member workspace fixture with unified lockfile
2026-12-03 13:07:09 +03:00

1261 lines
47 KiB
C++

#include <Void/Build.hpp>
#include <Void/Build/Util/Json.hpp>
#include <algorithm>
#include <cstdlib>
#include <set>
#include <type_traits>
namespace Void::Build {
namespace detail {
struct ProjectState {
ProfileContext context;
std::vector<TargetRecord> targets;
std::vector<DependencyRecord> dependencies;
std::vector<std::string> dependency_files;
std::vector<InstallRule> install_rules;
};
} // namespace detail
namespace {
using Util::json_array;
using Util::json_quote;
void require_text(std::string_view text, std::string_view field) {
if (text.empty()) {
throw std::invalid_argument{std::string{field} + " must not be empty"};
}
}
auto owner(const std::weak_ptr<detail::ProjectState> &weak) {
auto state = weak.lock();
if (!state) {
throw std::logic_error{"declaration handle's Project has expired"};
}
return state;
}
bool is_hex_digest(std::string_view text, std::size_t expected_len) {
if (text.size() != expected_len) {
return false;
}
return std::ranges::all_of(text, [](char c) {
return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
});
}
void validate_sanitizers(const std::vector<Sanitizer> &sanitizers) {
bool has_asan = false;
bool has_tsan = false;
bool has_msan = false;
for (const auto s : sanitizers) {
(void)sanitizer_name(s);
if (s == Sanitizer::Address) {
has_asan = true;
} else if (s == Sanitizer::Thread) {
has_tsan = true;
} else if (s == Sanitizer::Memory) {
has_msan = true;
}
}
if (has_asan && has_tsan) {
throw std::invalid_argument{"AddressSanitizer and ThreadSanitizer are mutually exclusive"};
}
if (has_asan && has_msan) {
throw std::invalid_argument{"AddressSanitizer and MemorySanitizer are mutually exclusive"};
}
if (has_tsan && has_msan) {
throw std::invalid_argument{"ThreadSanitizer and MemorySanitizer are mutually exclusive"};
}
}
void validate_context(const ProfileContext &context) {
require_text(context.name, "profile name");
(void)compiler_name(context.compiler);
require_text(context.os, "target OS");
require_text(context.architecture, "target architecture");
for (const auto &option : context.compile_options) {
require_text(option, "compile option");
}
for (const auto &option : context.link_options) {
require_text(option, "link option");
}
for (const auto &def : context.defines) {
require_text(def, "define");
}
validate_sanitizers(context.sanitizers);
}
std::string options_json(const OptionMap &options) {
std::string output{"{"};
bool first = true;
for (const auto &[name, option] : options) {
if (!first) {
output += ',';
}
first = false;
output += json_quote(name) + ':';
output += std::visit(
[](const auto &value) -> std::string {
using Type = std::remove_cvref_t<decltype(value)>;
if constexpr (std::same_as<Type, std::string>) {
return json_quote(value);
} else if constexpr (std::same_as<Type, bool>) {
return value ? "true" : "false";
} else {
return std::to_string(value);
}
},
option.value);
}
return output + '}';
}
std::string env_json(const std::map<std::string, std::string, std::less<>> &env) {
std::string output{"{"};
bool first = true;
for (const auto &[k, v] : env) {
if (!first) {
output += ',';
}
first = false;
output += json_quote(k) + ':' + json_quote(v);
}
return output + '}';
}
std::string scoped_options_json(const std::vector<ScopedOption> &opts) {
return json_array(opts, [](const ScopedOption &opt) {
return "{\"value\":" + json_quote(opt.value) +
",\"visibility\":" + json_quote(visibility_name(opt.visibility)) + '}';
});
}
std::string install_rule_json(const InstallRule &rule) {
return "{\"target_name\":" + json_quote(rule.target_name) +
",\"source_path\":" + json_quote(rule.source_path) +
",\"destination\":" + json_quote(rule.destination) +
",\"component\":" + json_quote(rule.component) +
",\"is_directory\":" + std::string{rule.is_directory ? "true" : "false"} + '}';
}
} // namespace
std::string_view compiler_name(CompilerFamily compiler) {
switch (compiler) {
case CompilerFamily::Gcc:
return "gcc";
case CompilerFamily::Clang:
return "clang";
}
throw std::invalid_argument{"unknown compiler family"};
}
CompilerFamily parse_compiler(std::string_view name) {
if (name == "gcc") {
return CompilerFamily::Gcc;
}
if (name == "clang") {
return CompilerFamily::Clang;
}
throw std::invalid_argument{"unknown compiler '" + std::string{name} +
"' (expected gcc or clang)"};
}
std::string_view visibility_name(Visibility visibility) {
switch (visibility) {
case Visibility::Private:
return "private";
case Visibility::Public:
return "public";
case Visibility::Interface:
return "interface";
}
throw std::invalid_argument{"unknown visibility"};
}
Visibility parse_visibility(std::string_view name) {
if (name == "private") {
return Visibility::Private;
}
if (name == "public") {
return Visibility::Public;
}
if (name == "interface") {
return Visibility::Interface;
}
throw std::invalid_argument{"unknown visibility '" + std::string{name} + "'"};
}
std::string_view target_kind_name(TargetKind kind) {
switch (kind) {
case TargetKind::Executable:
return "executable";
case TargetKind::StaticLibrary:
return "static_library";
case TargetKind::SharedLibrary:
return "shared_library";
case TargetKind::HeaderOnly:
return "header_only";
case TargetKind::ObjectLibrary:
return "object_library";
case TargetKind::CustomAction:
return "custom_action";
case TargetKind::Test:
return "test";
}
throw std::invalid_argument{"unknown target kind"};
}
TargetKind parse_target_kind(std::string_view name) {
if (name == "executable") {
return TargetKind::Executable;
}
if (name == "static_library") {
return TargetKind::StaticLibrary;
}
if (name == "shared_library") {
return TargetKind::SharedLibrary;
}
if (name == "header_only") {
return TargetKind::HeaderOnly;
}
if (name == "object_library") {
return TargetKind::ObjectLibrary;
}
if (name == "custom_action") {
return TargetKind::CustomAction;
}
if (name == "test") {
return TargetKind::Test;
}
throw std::invalid_argument{"unknown target kind '" + std::string{name} + "'"};
}
std::string_view source_kind_name(SourceKind kind) {
switch (kind) {
case SourceKind::File:
return "file";
case SourceKind::Glob:
return "glob";
case SourceKind::ModuleInterface:
return "module_interface";
case SourceKind::ModulePartition:
return "module_partition";
case SourceKind::HeaderUnit:
return "header_unit";
}
throw std::invalid_argument{"unknown source kind"};
}
SourceKind parse_source_kind(std::string_view name) {
if (name == "file") {
return SourceKind::File;
}
if (name == "glob") {
return SourceKind::Glob;
}
if (name == "module_interface") {
return SourceKind::ModuleInterface;
}
if (name == "module_partition") {
return SourceKind::ModulePartition;
}
if (name == "header_unit") {
return SourceKind::HeaderUnit;
}
throw std::invalid_argument{"unknown source kind '" + std::string{name} + "'"};
}
std::string_view c_standard_name(CStandard standard) {
switch (standard) {
case CStandard::Default:
return "default";
case CStandard::C11:
return "c11";
case CStandard::C17:
return "c17";
case CStandard::C23:
return "c23";
}
throw std::invalid_argument{"unknown C standard"};
}
CStandard parse_c_standard(std::string_view name) {
if (name.empty() || name == "default") {
return CStandard::Default;
}
if (name == "c11" || name == "11") {
return CStandard::C11;
}
if (name == "c17" || name == "17" || name == "c18") {
return CStandard::C17;
}
if (name == "c23" || name == "23" || name == "c2x") {
return CStandard::C23;
}
throw std::invalid_argument{"unknown C standard '" + std::string{name} + "'"};
}
std::string_view cxx_standard_name(CxxStandard standard) {
switch (standard) {
case CxxStandard::Default:
return "default";
case CxxStandard::Cxx17:
return "c++17";
case CxxStandard::Cxx20:
return "c++20";
case CxxStandard::Cxx23:
return "c++23";
case CxxStandard::Cxx26:
return "c++26";
}
throw std::invalid_argument{"unknown C++ standard"};
}
CxxStandard parse_cxx_standard(std::string_view name) {
if (name.empty() || name == "default") {
return CxxStandard::Default;
}
if (name == "c++17" || name == "17") {
return CxxStandard::Cxx17;
}
if (name == "c++20" || name == "20") {
return CxxStandard::Cxx20;
}
if (name == "c++23" || name == "23") {
return CxxStandard::Cxx23;
}
if (name == "c++26" || name == "26") {
return CxxStandard::Cxx26;
}
throw std::invalid_argument{"unknown C++ standard '" + std::string{name} + "'"};
}
std::string_view lto_mode_name(LtoMode mode) {
switch (mode) {
case LtoMode::None:
return "none";
case LtoMode::Full:
return "full";
case LtoMode::Thin:
return "thin";
}
throw std::invalid_argument{"unknown LTO mode"};
}
LtoMode parse_lto_mode(std::string_view name) {
if (name.empty() || name == "none") {
return LtoMode::None;
}
if (name == "full") {
return LtoMode::Full;
}
if (name == "thin") {
return LtoMode::Thin;
}
throw std::invalid_argument{"unknown LTO mode '" + std::string{name} + "'"};
}
std::string_view sanitizer_name(Sanitizer sanitizer) {
switch (sanitizer) {
case Sanitizer::Address:
return "address";
case Sanitizer::Undefined:
return "undefined";
case Sanitizer::Thread:
return "thread";
case Sanitizer::Memory:
return "memory";
case Sanitizer::Leak:
return "leak";
}
throw std::invalid_argument{"unknown sanitizer"};
}
Sanitizer parse_sanitizer(std::string_view name) {
if (name == "address" || name == "asan") {
return Sanitizer::Address;
}
if (name == "undefined" || name == "ubsan") {
return Sanitizer::Undefined;
}
if (name == "thread" || name == "tsan") {
return Sanitizer::Thread;
}
if (name == "memory" || name == "msan") {
return Sanitizer::Memory;
}
if (name == "leak" || name == "lsan") {
return Sanitizer::Leak;
}
throw std::invalid_argument{"unknown sanitizer '" + std::string{name} + "'"};
}
void Profiles::add(std::string name, std::vector<std::string> compile_options) {
ProfileDescription desc;
desc.name = std::move(name);
desc.compile_options = std::move(compile_options);
define(std::move(desc));
}
void Profiles::inherit(std::string name, std::string parent,
std::vector<std::string> compile_options) {
require_text(parent, "parent profile name");
ProfileDescription desc;
desc.name = std::move(name);
desc.parent = std::move(parent);
desc.compile_options = std::move(compile_options);
define(std::move(desc));
}
void Profiles::define(ProfileDescription description) {
require_text(description.name, "profile name");
if (std::ranges::any_of(records_,
[&](const auto &record) { return record.name == description.name; })) {
throw std::invalid_argument{"duplicate profile '" + description.name + "'"};
}
if (description.parent) {
require_text(*description.parent, "parent profile name");
if (*description.parent == description.name) {
throw std::invalid_argument{"profile '" + description.name +
"' cannot inherit from itself"};
}
}
for (const auto &option : description.compile_options) {
require_text(option, "compile option");
}
for (const auto &option : description.link_options) {
require_text(option, "link option");
}
for (const auto &def : description.defines) {
require_text(def, "define");
}
validate_sanitizers(description.sanitizers);
records_.push_back(std::move(description));
}
const std::vector<ProfileDescription> &Profiles::records() const noexcept { return records_; }
ProfileContext Profiles::resolve(std::string_view name, CompilerFamily compiler, std::string os,
std::string architecture) const {
return resolve(name, std::optional<CompilerFamily>{compiler}, std::move(os),
std::move(architecture));
}
ProfileContext Profiles::resolve(std::string_view name,
std::optional<CompilerFamily> compiler_override, std::string os,
std::string architecture) const {
std::vector<const ProfileDescription *> chain;
std::set<std::string_view> visiting;
std::string_view current = name;
while (!current.empty()) {
if (!visiting.insert(current).second) {
throw std::invalid_argument{"profile inheritance cycle involving '" +
std::string{current} + "'"};
}
const auto found = std::ranges::find(records_, current, &ProfileDescription::name);
if (found == records_.end()) {
if (chain.empty()) {
throw std::invalid_argument{"unknown profile '" + std::string{current} + "'"};
}
throw std::invalid_argument{"unknown parent profile '" + std::string{current} +
"' referenced by '" + chain.back()->name + "'"};
}
chain.push_back(&(*found));
if (found->parent) {
current = *found->parent;
} else {
break;
}
}
ProfileContext context;
context.name = std::string{name};
context.os = std::move(os);
context.architecture = std::move(architecture);
context.compiler = CompilerFamily::Gcc;
// Apply from root ancestor down to concrete profile.
for (auto it = chain.rbegin(); it != chain.rend(); ++it) {
const auto *desc = *it;
if (desc->compiler) {
context.compiler = *desc->compiler;
}
if (desc->c_standard) {
context.c_standard = *desc->c_standard;
}
if (desc->cxx_standard) {
context.cxx_standard = *desc->cxx_standard;
}
if (desc->lto) {
context.lto = *desc->lto;
}
if (!desc->sysroot.empty()) {
context.sysroot = desc->sysroot;
}
if (!desc->compiler_path.empty()) {
context.compiler_path = desc->compiler_path;
}
if (desc->reproducibility_epoch) {
context.reproducibility_epoch = *desc->reproducibility_epoch;
}
if (desc->nondeterministic) {
context.nondeterministic = *desc->nondeterministic;
}
context.compile_options.insert(context.compile_options.end(), desc->compile_options.begin(),
desc->compile_options.end());
context.link_options.insert(context.link_options.end(), desc->link_options.begin(),
desc->link_options.end());
context.defines.insert(context.defines.end(), desc->defines.begin(), desc->defines.end());
for (const auto s : desc->sanitizers) {
if (!std::ranges::contains(context.sanitizers, s)) {
context.sanitizers.push_back(s);
}
}
}
if (compiler_override) {
context.compiler = *compiler_override;
}
validate_context(context);
return context;
}
std::string Profiles::to_json() const {
return "{\"schema_version\":" + std::to_string(schema_version) + ",\"profiles\":" +
json_array(
records_,
[](const ProfileDescription &p) {
std::string out = "{\"name\":" + json_quote(p.name);
if (p.parent) {
out += ",\"parent\":" + json_quote(*p.parent);
}
if (p.compiler) {
out += ",\"compiler\":" + json_quote(compiler_name(*p.compiler));
}
if (p.c_standard) {
out += ",\"c_standard\":" + json_quote(c_standard_name(*p.c_standard));
}
if (p.cxx_standard) {
out += ",\"cxx_standard\":" + json_quote(cxx_standard_name(*p.cxx_standard));
}
if (p.lto) {
out += ",\"lto\":" + json_quote(lto_mode_name(*p.lto));
}
if (!p.sysroot.empty()) {
out += ",\"sysroot\":" + json_quote(p.sysroot);
}
if (!p.compiler_path.empty()) {
out += ",\"compiler_path\":" + json_quote(p.compiler_path);
}
if (p.reproducibility_epoch) {
out +=
",\"reproducibility_epoch\":" + std::to_string(*p.reproducibility_epoch);
}
if (p.nondeterministic) {
out += ",\"nondeterministic\":" +
std::string{*p.nondeterministic ? "true" : "false"};
}
out += ",\"compile_options\":" + json_array(p.compile_options, json_quote) +
",\"link_options\":" + json_array(p.link_options, json_quote) +
",\"defines\":" + json_array(p.defines, json_quote) + ",\"sanitizers\":" +
json_array(p.sanitizers,
[](Sanitizer s) { return json_quote(sanitizer_name(s)); }) +
'}';
return out;
}) +
'}';
}
Profiles default_profiles() {
Profiles result;
result.add("debug", {"-O0", "-g"});
result.add("release", {"-O3", "-DNDEBUG"});
return result;
}
SourceSet::SourceSet(std::initializer_list<std::string_view> files) {
for (const auto path : files) {
require_text(path, "source path");
records_.push_back({SourceKind::File, std::string{path}});
}
}
SourceSet SourceSet::file(std::string path) {
require_text(path, "source path");
SourceSet result;
result.records_.push_back({SourceKind::File, std::move(path)});
return result;
}
SourceSet SourceSet::glob(std::string pattern) {
require_text(pattern, "glob pattern");
SourceSet result;
result.records_.push_back({SourceKind::Glob, std::move(pattern)});
return result;
}
SourceSet SourceSet::module_interface(std::string path) {
require_text(path, "module interface path");
SourceSet result;
result.records_.push_back({SourceKind::ModuleInterface, std::move(path)});
return result;
}
SourceSet SourceSet::module_partition(std::string path) {
require_text(path, "module partition path");
SourceSet result;
result.records_.push_back({SourceKind::ModulePartition, std::move(path)});
return result;
}
SourceSet SourceSet::header_unit(std::string path) {
require_text(path, "header unit path");
SourceSet result;
result.records_.push_back({SourceKind::HeaderUnit, std::move(path)});
return result;
}
const std::vector<SourceRecord> &SourceSet::records() const noexcept { return records_; }
Target::Target(std::weak_ptr<detail::ProjectState> state, std::size_t index)
: owner_{std::move(state)}, index_{index} {}
Target &Target::sources(const SourceSet &sources) {
auto state = owner(owner_);
auto &records = state->targets.at(index_).sources;
records.insert(records.end(), sources.records().begin(), sources.records().end());
return *this;
}
Target &Target::conventional_sources() {
owner(owner_)->targets.at(index_).use_conventional_sources = true;
return *this;
}
Target &Target::module_sources(const SourceSet &sources) {
auto state = owner(owner_);
auto &records = state->targets.at(index_).sources;
for (const auto &rec : sources.records()) {
if (rec.kind == SourceKind::File) {
records.push_back({SourceKind::ModuleInterface, rec.path});
} else {
records.push_back(rec);
}
}
return *this;
}
Target &Target::header_unit(std::string header_path) {
require_text(header_path, "header unit path");
owner(owner_)->targets.at(index_).sources.push_back(
{SourceKind::HeaderUnit, std::move(header_path)});
return *this;
}
Target &Target::include_directory(std::string path, Visibility visibility) {
require_text(path, "include directory");
(void)visibility_name(visibility);
owner(owner_)->targets.at(index_).include_directories.push_back(
{std::move(path), visibility, false});
return *this;
}
Target &Target::system_include_directory(std::string path, Visibility visibility) {
require_text(path, "system include directory");
(void)visibility_name(visibility);
owner(owner_)->targets.at(index_).include_directories.push_back(
{std::move(path), visibility, true});
return *this;
}
Target &Target::compile_option(std::string option) {
require_text(option, "compile option");
auto &rec = owner(owner_)->targets.at(index_);
rec.compile_options.push_back(option);
rec.scoped_compile_options.push_back({std::move(option), Visibility::Private});
return *this;
}
Target &Target::compile_option(std::string option, Visibility visibility) {
require_text(option, "compile option");
(void)visibility_name(visibility);
auto &rec = owner(owner_)->targets.at(index_);
if (visibility != Visibility::Interface) {
rec.compile_options.push_back(option);
}
rec.scoped_compile_options.push_back({std::move(option), visibility});
return *this;
}
Target &Target::define(std::string macro, Visibility visibility) {
require_text(macro, "define");
(void)visibility_name(visibility);
owner(owner_)->targets.at(index_).defines.push_back({std::move(macro), visibility});
return *this;
}
Target &Target::link_option(std::string option, Visibility visibility) {
require_text(option, "link option");
(void)visibility_name(visibility);
owner(owner_)->targets.at(index_).link_options.push_back({std::move(option), visibility});
return *this;
}
Target &Target::link(const Target &target, Visibility visibility) {
auto state = owner(owner_);
if (owner(target.owner_) != state) {
throw std::invalid_argument{"cannot link a target from another Project"};
}
(void)visibility_name(visibility);
state->targets.at(index_).links.push_back(
{LinkKind::Local, state->targets.at(target.index_).name, {}, visibility});
return *this;
}
Target &Target::link(const ImportedTarget &target, Visibility visibility) {
auto state = owner(owner_);
if (owner(target.owner_) != state) {
throw std::invalid_argument{"cannot link an imported target from another Project"};
}
(void)visibility_name(visibility);
state->targets.at(index_).links.push_back({LinkKind::Imported, target.name_,
state->dependencies.at(target.dependency_).name,
visibility});
return *this;
}
Target &Target::depends_on(const Target &producer) {
auto state = owner(owner_);
if (owner(producer.owner_) != state) {
throw std::invalid_argument{"cannot depend on a target from another Project"};
}
const auto &producer_name = state->targets.at(producer.index_).name;
auto &prereqs = state->targets.at(index_).prerequisites;
if (!std::ranges::contains(prereqs, producer_name)) {
prereqs.push_back(producer_name);
}
return *this;
}
Target &Target::c_standard(CStandard standard) {
(void)c_standard_name(standard);
owner(owner_)->targets.at(index_).c_standard = standard;
return *this;
}
Target &Target::cxx_standard(CxxStandard standard) {
(void)cxx_standard_name(standard);
owner(owner_)->targets.at(index_).cxx_standard = standard;
return *this;
}
Target &Target::precompiled_header(std::string header_path) {
require_text(header_path, "precompiled header path");
owner(owner_)->targets.at(index_).precompiled_header = std::move(header_path);
return *this;
}
Target &Target::unity_build(bool enabled, std::size_t batch_size) {
if (enabled && batch_size == 0) {
throw std::invalid_argument{"unity batch size must be positive"};
}
auto &rec = owner(owner_)->targets.at(index_);
rec.unity_enabled = enabled;
rec.unity_batch_size = batch_size;
return *this;
}
Target &Target::lto(LtoMode mode) {
(void)lto_mode_name(mode);
owner(owner_)->targets.at(index_).lto = mode;
return *this;
}
Target &Target::sanitizer(Sanitizer sanitizer) {
auto &rec = owner(owner_)->targets.at(index_);
auto next = rec.sanitizers;
if (!std::ranges::contains(next, sanitizer)) {
next.push_back(sanitizer);
}
validate_sanitizers(next);
rec.sanitizers = std::move(next);
return *this;
}
Target &Target::per_file_option(std::string source_path, std::string option) {
require_text(source_path, "source path");
require_text(option, "per-file compile option");
owner(owner_)->targets.at(index_).per_file_options.push_back(
{std::move(source_path), std::move(option)});
return *this;
}
Target &Target::command(std::vector<std::string> argv) {
if (argv.empty()) {
throw std::invalid_argument{"command argv must not be empty"};
}
require_text(argv.front(), "command executable");
owner(owner_)->targets.at(index_).command_argv = std::move(argv);
return *this;
}
Target &Target::inputs(std::vector<std::string> paths) {
for (const auto &p : paths) {
require_text(p, "input path");
}
auto &dst = owner(owner_)->targets.at(index_).action_inputs;
dst.insert(dst.end(), std::make_move_iterator(paths.begin()),
std::make_move_iterator(paths.end()));
return *this;
}
Target &Target::outputs(std::vector<std::string> paths) {
for (const auto &p : paths) {
require_text(p, "output path");
}
auto &dst = owner(owner_)->targets.at(index_).action_outputs;
dst.insert(dst.end(), std::make_move_iterator(paths.begin()),
std::make_move_iterator(paths.end()));
return *this;
}
Target &Target::working_directory(std::string directory) {
require_text(directory, "working directory");
owner(owner_)->targets.at(index_).working_directory = std::move(directory);
return *this;
}
Target &Target::env(std::string key, std::string value) {
require_text(key, "environment variable key");
owner(owner_)->targets.at(index_).environment[std::move(key)] = std::move(value);
return *this;
}
Target &Target::nondeterministic(bool enabled) {
owner(owner_)->targets.at(index_).nondeterministic = enabled;
return *this;
}
Target &Target::enabled(bool flag) {
owner(owner_)->targets.at(index_).enabled = flag;
return *this;
}
Target &Target::install(std::string destination, std::string component) {
require_text(component, "install component");
auto &rec = owner(owner_)->targets.at(index_);
rec.install_rule =
InstallRule{rec.name, {}, std::move(destination), std::move(component), false};
return *this;
}
const TargetRecord &Target::record() const { return owner(owner_)->targets.at(index_); }
ImportedTarget::ImportedTarget(std::weak_ptr<detail::ProjectState> state, std::size_t dependency,
std::string name)
: owner_{std::move(state)}, dependency_{dependency}, name_{std::move(name)} {}
Dependency::Dependency(std::weak_ptr<detail::ProjectState> state, std::size_t index)
: owner_{std::move(state)}, index_{index} {}
Dependency &Dependency::git(std::string url, Commit commit) {
require_text(url, "Git URL");
if (!is_hex_digest(commit.sha, 40)) {
throw std::invalid_argument{"Commit must contain an exact 40-digit hexadecimal Git SHA"};
}
auto state = owner(owner_);
auto &record = state->dependencies.at(index_);
if (record.source_kind != DependencySourceKind::Unspecified) {
throw std::invalid_argument{"dependency '" + record.name +
"' already has a source provider"};
}
record.source_kind = DependencySourceKind::Git;
record.location = std::move(url);
record.revision = std::move(commit.sha);
return *this;
}
Dependency &Dependency::path(std::string local_path) {
require_text(local_path, "local dependency path");
auto state = owner(owner_);
auto &record = state->dependencies.at(index_);
if (record.source_kind != DependencySourceKind::Unspecified) {
throw std::invalid_argument{"dependency '" + record.name +
"' already has a source provider"};
}
record.source_kind = DependencySourceKind::Path;
record.location = std::move(local_path);
return *this;
}
Dependency &Dependency::archive(std::string url, std::string sha256) {
require_text(url, "archive URL");
if (!is_hex_digest(sha256, 64)) {
throw std::invalid_argument{"archive sha256 must contain 64 hexadecimal digits"};
}
auto state = owner(owner_);
auto &record = state->dependencies.at(index_);
if (record.source_kind != DependencySourceKind::Unspecified) {
throw std::invalid_argument{"dependency '" + record.name +
"' already has a source provider"};
}
record.source_kind = DependencySourceKind::Archive;
record.location = std::move(url);
record.sha256 = std::move(sha256);
return *this;
}
Dependency &Dependency::cmake(OptionMap options) {
for (const auto &[key, value] : options) {
(void)value;
require_text(key, "CMake option name");
}
auto state = owner(owner_);
auto &record = state->dependencies.at(index_);
if (record.source_kind == DependencySourceKind::Package || !record.builder.empty()) {
throw std::invalid_argument{"contradictory builder for dependency '" + record.name + "'"};
}
record.builder = "cmake";
record.options = std::move(options);
return *this;
}
Dependency &Dependency::meson(OptionMap options) {
for (const auto &[key, value] : options) {
(void)value;
require_text(key, "Meson option name");
}
auto state = owner(owner_);
auto &record = state->dependencies.at(index_);
if (record.source_kind == DependencySourceKind::Package || !record.builder.empty()) {
throw std::invalid_argument{"contradictory builder for dependency '" + record.name + "'"};
}
record.builder = "meson";
record.options = std::move(options);
return *this;
}
Dependency &Dependency::conan(std::string reference, OptionMap options) {
require_text(reference, "Conan reference");
for (const auto &[key, value] : options) {
(void)value;
require_text(key, "Conan option name");
}
auto state = owner(owner_);
auto &record = state->dependencies.at(index_);
if (record.source_kind != DependencySourceKind::Unspecified || !record.builder.empty()) {
throw std::invalid_argument{"dependency '" + record.name +
"' already has a source/provider"};
}
record.source_kind = DependencySourceKind::Conan;
record.package = std::move(reference);
record.builder = "conan";
record.options = std::move(options);
return *this;
}
Dependency &Dependency::native_builder(OptionMap options) {
for (const auto &[key, value] : options) {
(void)value;
require_text(key, "native option name");
}
auto state = owner(owner_);
auto &record = state->dependencies.at(index_);
if (!record.builder.empty()) {
throw std::invalid_argument{"contradictory builder for dependency '" + record.name + "'"};
}
record.builder = "native";
record.options = std::move(options);
return *this;
}
Dependency &Dependency::package(std::string id, std::string range) {
require_text(id, "package identifier");
require_text(range, "package version range");
auto state = owner(owner_);
auto &record = state->dependencies.at(index_);
if (record.source_kind != DependencySourceKind::Unspecified || !record.builder.empty()) {
throw std::invalid_argument{"dependency '" + record.name +
"' already has a source/provider"};
}
record.source_kind = DependencySourceKind::Package;
record.package = std::move(id);
record.version = std::move(range);
return *this;
}
Dependency &Dependency::registry(std::string authority_url) {
require_text(authority_url, "registry authority");
owner(owner_)->dependencies.at(index_).registry_authority = std::move(authority_url);
return *this;
}
Dependency &Dependency::feature(std::string name) {
require_text(name, "feature name");
auto &features = owner(owner_)->dependencies.at(index_).features;
if (!std::ranges::contains(features, name)) {
features.push_back(std::move(name));
}
return *this;
}
Dependency &Dependency::build_dependency(bool is_build_tool) {
owner(owner_)->dependencies.at(index_).build_dependency = is_build_tool;
return *this;
}
ImportedTarget Dependency::target(std::string exported_name) const {
(void)owner(owner_);
require_text(exported_name, "exported target name");
return ImportedTarget{owner_, index_, std::move(exported_name)};
}
const DependencyRecord &Dependency::record() const {
return owner(owner_)->dependencies.at(index_);
}
Project::Project(ProfileContext context) : state_{std::make_shared<detail::ProjectState>()} {
validate_context(context);
state_->context = std::move(context);
}
Project::~Project() = default;
const ProfileContext &Project::context() const noexcept { return state_->context; }
Target Project::add_target(std::string name, TargetKind kind) {
require_text(name, "target name");
if (std::ranges::any_of(state_->targets,
[&](const auto &record) { return record.name == name; })) {
throw std::invalid_argument{"duplicate target '" + name + "'"};
}
TargetRecord record;
record.name = std::move(name);
record.kind = kind;
state_->targets.push_back(std::move(record));
return Target{state_, state_->targets.size() - 1};
}
Target Project::executable(std::string name) {
return add_target(std::move(name), TargetKind::Executable);
}
Target Project::static_library(std::string name) {
return add_target(std::move(name), TargetKind::StaticLibrary);
}
Target Project::shared_library(std::string name) {
return add_target(std::move(name), TargetKind::SharedLibrary);
}
Target Project::header_only_library(std::string name) {
return add_target(std::move(name), TargetKind::HeaderOnly);
}
Target Project::object_library(std::string name) {
return add_target(std::move(name), TargetKind::ObjectLibrary);
}
Target Project::custom_action(std::string name) {
return add_target(std::move(name), TargetKind::CustomAction);
}
Target Project::test(std::string name) { return add_target(std::move(name), TargetKind::Test); }
Dependency Project::dependency(std::string name) {
require_text(name, "dependency name");
if (std::ranges::any_of(state_->dependencies,
[&](const auto &record) { return record.name == name; })) {
throw std::invalid_argument{"duplicate dependency '" + name + "'"};
}
DependencyRecord record;
record.name = std::move(name);
state_->dependencies.push_back(std::move(record));
return Dependency{state_, state_->dependencies.size() - 1};
}
Dependency Project::workspace_dependency(std::string name) {
auto dep = dependency(name);
auto &record = state_->dependencies.back();
const std::string env_name = "VBS_WORKSPACE_MEMBER_" + record.name;
const char *member_path = std::getenv(env_name.c_str());
if (!member_path || member_path[0] == '\0') {
throw std::invalid_argument{"workspace member '" + record.name +
"' is not provided by the enclosing workspace"};
}
record.source_kind = DependencySourceKind::Path;
record.location = member_path;
record.builder = "native";
record.options.insert_or_assign("vbs.workspace.member", OptionValue{record.name});
return dep;
}
Workspace &Workspace::member(std::string name, std::string path) {
require_text(name, "workspace member name");
require_text(path, "workspace member path");
if (std::ranges::any_of(members_, [&](const auto &member) { return member.name == name; })) {
throw std::invalid_argument{"duplicate workspace member '" + name + "'"};
}
members_.push_back({std::move(name), std::move(path)});
return *this;
}
const std::vector<WorkspaceMemberRecord> &Workspace::members() const noexcept { return members_; }
std::string Workspace::to_json() const {
return "{\"schema_version\":1,\"members\":" +
json_array(members_,
[](const WorkspaceMemberRecord &member) {
return "{\"name\":" + json_quote(member.name) +
",\"path\":" + json_quote(member.path) + '}';
}) +
'}';
}
void Project::dependencies_file(std::string toml_path) {
require_text(toml_path, "dependencies manifest path");
state_->dependency_files.push_back(std::move(toml_path));
}
void Project::install_file(std::string source_path, std::string destination,
std::string component) {
require_text(source_path, "install source path");
require_text(destination, "install destination");
require_text(component, "install component");
state_->install_rules.push_back(
{{}, std::move(source_path), std::move(destination), std::move(component), false});
}
void Project::install_directory(std::string source_path, std::string destination,
std::string component) {
require_text(source_path, "install source directory");
require_text(destination, "install destination");
require_text(component, "install component");
state_->install_rules.push_back(
{{}, std::move(source_path), std::move(destination), std::move(component), true});
}
SourceSet Project::glob(std::string pattern) const { return SourceSet::glob(std::move(pattern)); }
const std::vector<TargetRecord> &Project::targets() const noexcept { return state_->targets; }
const std::vector<DependencyRecord> &Project::dependencies() const noexcept {
return state_->dependencies;
}
const std::vector<std::string> &Project::dependency_files() const noexcept {
return state_->dependency_files;
}
const std::vector<InstallRule> &Project::install_rules() const noexcept {
return state_->install_rules;
}
std::string Project::to_json() const {
const auto &profile = context();
std::string output =
"{\"schema_version\":" + std::to_string(schema_version) +
",\"profile\":{\"name\":" + json_quote(profile.name) +
",\"compiler\":" + json_quote(compiler_name(profile.compiler)) +
",\"os\":" + json_quote(profile.os) +
",\"architecture\":" + json_quote(profile.architecture) +
",\"compile_options\":" + json_array(profile.compile_options, json_quote) +
",\"link_options\":" + json_array(profile.link_options, json_quote) +
",\"defines\":" + json_array(profile.defines, json_quote) +
",\"c_standard\":" + json_quote(c_standard_name(profile.c_standard)) +
",\"cxx_standard\":" + json_quote(cxx_standard_name(profile.cxx_standard)) +
",\"lto\":" + json_quote(lto_mode_name(profile.lto)) + ",\"sanitizers\":" +
json_array(profile.sanitizers, [](Sanitizer s) { return json_quote(sanitizer_name(s)); }) +
",\"sysroot\":" + json_quote(profile.sysroot) +
",\"compiler_path\":" + json_quote(profile.compiler_path) +
",\"reproducibility_epoch\":" + std::to_string(profile.reproducibility_epoch) +
",\"nondeterministic\":" + std::string{profile.nondeterministic ? "true" : "false"} +
"},\"targets\":";
output += json_array(targets(), [](const TargetRecord &target) {
std::string out =
"{\"name\":" + json_quote(target.name) +
",\"kind\":" + json_quote(target_kind_name(target.kind)) + ",\"sources\":" +
json_array(target.sources,
[](const SourceRecord &source) {
return "{\"kind\":" + json_quote(source_kind_name(source.kind)) +
",\"path\":" + json_quote(source.path) + '}';
}) +
",\"include_directories\":" +
json_array(target.include_directories,
[](const IncludeDirectory &directory) {
return "{\"path\":" + json_quote(directory.path) + ",\"visibility\":" +
json_quote(visibility_name(directory.visibility)) +
",\"is_system\":" +
std::string{directory.is_system ? "true" : "false"} + '}';
}) +
",\"compile_options\":" + json_array(target.compile_options, json_quote) +
",\"scoped_compile_options\":" + scoped_options_json(target.scoped_compile_options) +
",\"defines\":" + scoped_options_json(target.defines) +
",\"link_options\":" + scoped_options_json(target.link_options) + ",\"links\":" +
json_array(target.links,
[](const LinkRecord &link) {
return "{\"kind\":" +
json_quote(link.kind == LinkKind::Local ? "local" : "imported") +
",\"target\":" + json_quote(link.target) +
",\"dependency\":" + json_quote(link.dependency) +
",\"visibility\":" +
json_quote(visibility_name(link.visibility)) + '}';
}) +
",\"prerequisites\":" + json_array(target.prerequisites, json_quote) +
",\"c_standard\":" + json_quote(c_standard_name(target.c_standard)) +
",\"cxx_standard\":" + json_quote(cxx_standard_name(target.cxx_standard)) +
",\"precompiled_header\":" + json_quote(target.precompiled_header) +
",\"unity_enabled\":" + std::string{target.unity_enabled ? "true" : "false"} +
",\"unity_batch_size\":" + std::to_string(target.unity_batch_size) +
",\"lto\":" + json_quote(lto_mode_name(target.lto)) + ",\"sanitizers\":" +
json_array(target.sanitizers,
[](Sanitizer s) { return json_quote(sanitizer_name(s)); }) +
",\"per_file_options\":" +
json_array(target.per_file_options,
[](const PerFileOption &opt) {
return "{\"source_path\":" + json_quote(opt.source_path) +
",\"option\":" + json_quote(opt.option) + '}';
}) +
",\"command_argv\":" + json_array(target.command_argv, json_quote) +
",\"action_inputs\":" + json_array(target.action_inputs, json_quote) +
",\"action_outputs\":" + json_array(target.action_outputs, json_quote) +
",\"working_directory\":" + json_quote(target.working_directory) +
",\"environment\":" + env_json(target.environment) +
",\"nondeterministic\":" + std::string{target.nondeterministic ? "true" : "false"} +
",\"enabled\":" + std::string{target.enabled ? "true" : "false"} +
",\"use_conventional_sources\":" +
std::string{target.use_conventional_sources ? "true" : "false"};
if (target.install_rule) {
out += ",\"install_rule\":" + install_rule_json(*target.install_rule);
}
return out + '}';
});
output +=
",\"dependencies\":" + json_array(dependencies(), [](const DependencyRecord &dependency) {
if (dependency.source_kind == DependencySourceKind::Unspecified) {
throw std::invalid_argument{"dependency '" + dependency.name +
"' has no source provider"};
}
std::string result =
"{\"name\":" + json_quote(dependency.name) + ",\"source\":{\"kind\":";
switch (dependency.source_kind) {
case DependencySourceKind::Git:
result += "\"git\",\"location\":" + json_quote(dependency.location) +
",\"revision\":" + json_quote(dependency.revision);
break;
case DependencySourceKind::Package:
result += "\"package\",\"package\":" + json_quote(dependency.package) +
",\"version\":" + json_quote(dependency.version);
break;
case DependencySourceKind::Path:
result += "\"path\",\"location\":" + json_quote(dependency.location);
break;
case DependencySourceKind::Archive:
result += "\"archive\",\"location\":" + json_quote(dependency.location) +
",\"sha256\":" + json_quote(dependency.sha256);
break;
case DependencySourceKind::Conan:
result += "\"conan\",\"package\":" + json_quote(dependency.package);
break;
case DependencySourceKind::Unspecified:
break;
}
result += "},\"builder\":" + json_quote(dependency.builder) +
",\"options\":" + options_json(dependency.options) +
",\"registry_authority\":" + json_quote(dependency.registry_authority) +
",\"features\":" + json_array(dependency.features, json_quote) +
",\"build_dependency\":" +
std::string{dependency.build_dependency ? "true" : "false"} + '}';
return result;
});
output += ",\"dependency_files\":" + json_array(dependency_files(), json_quote);
output += ",\"install_rules\":" + json_array(install_rules(), install_rule_json);
return output + '}';
}
} // namespace Void::Build