#pragma once #include #include #include #include #include #include #include #include #include #include #include #include #include namespace Void::Build { inline constexpr int schema_version = 1; enum class CompilerFamily { Gcc, Clang }; enum class Visibility { Private, Public, Interface }; enum class TargetKind { Executable, StaticLibrary, SharedLibrary, HeaderOnly, ObjectLibrary, CustomAction, Test }; enum class SourceKind { File, Glob, ModuleInterface, ModulePartition, HeaderUnit }; enum class LinkKind { Local, Imported }; enum class DependencySourceKind { Unspecified, Git, Package, Path, Archive, Conan }; enum class CStandard { Default, C11, C17, C23 }; enum class CxxStandard { Default, Cxx17, Cxx20, Cxx23, Cxx26 }; enum class LtoMode { None, Full, Thin }; enum class Sanitizer { Address, Undefined, Thread, Memory, Leak }; [[nodiscard]] std::string_view compiler_name(CompilerFamily compiler); [[nodiscard]] CompilerFamily parse_compiler(std::string_view name); [[nodiscard]] std::string_view visibility_name(Visibility visibility); [[nodiscard]] Visibility parse_visibility(std::string_view name); [[nodiscard]] std::string_view target_kind_name(TargetKind kind); [[nodiscard]] TargetKind parse_target_kind(std::string_view name); [[nodiscard]] std::string_view source_kind_name(SourceKind kind); [[nodiscard]] SourceKind parse_source_kind(std::string_view name); [[nodiscard]] std::string_view c_standard_name(CStandard standard); [[nodiscard]] CStandard parse_c_standard(std::string_view name); [[nodiscard]] std::string_view cxx_standard_name(CxxStandard standard); [[nodiscard]] CxxStandard parse_cxx_standard(std::string_view name); [[nodiscard]] std::string_view lto_mode_name(LtoMode mode); [[nodiscard]] LtoMode parse_lto_mode(std::string_view name); [[nodiscard]] std::string_view sanitizer_name(Sanitizer sanitizer); [[nodiscard]] Sanitizer parse_sanitizer(std::string_view name); struct ProfileContext { std::string name; CompilerFamily compiler{CompilerFamily::Gcc}; std::string os; std::string architecture; std::vector compile_options; std::vector link_options; std::vector defines; CStandard c_standard{CStandard::Default}; CxxStandard cxx_standard{CxxStandard::Default}; LtoMode lto{LtoMode::None}; std::vector sanitizers; std::string sysroot; std::string compiler_path; std::int64_t reproducibility_epoch{0}; bool nondeterministic{false}; }; struct ProfileDescription { std::string name; std::vector compile_options; std::optional parent; std::vector link_options; std::vector defines; std::optional compiler; std::optional c_standard; std::optional cxx_standard; std::optional lto; std::vector sanitizers; std::string sysroot; std::string compiler_path; std::optional reproducibility_epoch; std::optional nondeterministic; }; class Profiles { public: void add(std::string name, std::vector compile_options = {}); void inherit(std::string name, std::string parent, std::vector compile_options = {}); void define(ProfileDescription description); [[nodiscard]] const std::vector &records() const noexcept; [[nodiscard]] ProfileContext resolve(std::string_view name, CompilerFamily compiler, std::string os, std::string architecture) const; [[nodiscard]] ProfileContext resolve(std::string_view name, std::optional compiler_override, std::string os, std::string architecture) const; [[nodiscard]] std::string to_json() const; private: std::vector records_; }; [[nodiscard]] Profiles default_profiles(); struct SourceRecord { SourceKind kind; std::string path; }; // Source spelling is recorded verbatim; construction never reads the filesystem. class SourceSet { public: SourceSet() = default; SourceSet(std::initializer_list files); [[nodiscard]] static SourceSet file(std::string path); [[nodiscard]] static SourceSet glob(std::string pattern); [[nodiscard]] static SourceSet module_interface(std::string path); [[nodiscard]] static SourceSet module_partition(std::string path); [[nodiscard]] static SourceSet header_unit(std::string path); [[nodiscard]] const std::vector &records() const noexcept; private: std::vector records_; }; struct OptionValue { using Value = std::variant; Value value; OptionValue(std::string text) : value{std::move(text)} {} OptionValue(const char *text) : OptionValue{std::string{text}} {} OptionValue(bool boolean) : value{boolean} {} template requires(!std::same_as) OptionValue(Integer integer) : value{std::int64_t{0}} { if (!std::in_range(integer)) { throw std::invalid_argument{"option integer is out of range"}; } value = static_cast(integer); } }; using OptionMap = std::map>; struct Commit { std::string sha; }; struct IncludeDirectory { std::string path; Visibility visibility{Visibility::Private}; bool is_system{false}; }; struct ScopedOption { std::string value; Visibility visibility{Visibility::Private}; }; struct LinkRecord { LinkKind kind; std::string target; std::string dependency; // Empty for a local target. Visibility visibility{Visibility::Private}; }; struct PerFileOption { std::string source_path; std::string option; }; struct InstallRule { std::string target_name; std::string source_path; std::string destination; std::string component{"default"}; bool is_directory{false}; }; struct TargetRecord { std::string name; TargetKind kind; std::vector sources; std::vector include_directories; std::vector compile_options; std::vector links; std::vector scoped_compile_options; std::vector defines; std::vector link_options; std::vector prerequisites; CStandard c_standard{CStandard::Default}; CxxStandard cxx_standard{CxxStandard::Default}; std::string precompiled_header; bool unity_enabled{false}; std::size_t unity_batch_size{8}; LtoMode lto{LtoMode::None}; std::vector sanitizers; std::vector per_file_options; std::vector command_argv; std::vector action_inputs; std::vector action_outputs; std::string working_directory; std::map> environment; bool nondeterministic{false}; bool enabled{true}; bool use_conventional_sources{false}; std::optional install_rule; }; struct DependencyRecord { std::string name; DependencySourceKind source_kind{DependencySourceKind::Unspecified}; std::string location; std::string revision; std::string package; std::string version; std::string builder; OptionMap options; std::string registry_authority; std::string sha256; std::vector features; bool build_dependency{false}; }; // Workspace declaration (E2): a root workspace.cpp lists member projects that share one // vbs.lock and one build/.vbs CAS while each keeps its own build.cpp and profile outputs. struct WorkspaceMemberRecord { std::string name; std::string path; // Relative to the workspace root (or absolute). }; class Workspace { public: Workspace() = default; Workspace &member(std::string name, std::string path); [[nodiscard]] const std::vector &members() const noexcept; [[nodiscard]] std::string to_json() const; private: std::vector members_; }; namespace detail { struct ProjectState; } class Project; class Dependency; // Handles retain an index and weak owner identity, never a pointer into a growing vector. class ImportedTarget { private: friend class Dependency; friend class Target; ImportedTarget(std::weak_ptr owner, std::size_t dependency, std::string name); std::weak_ptr owner_; std::size_t dependency_; std::string name_; }; class Target { public: Target &sources(const SourceSet &sources); Target &conventional_sources(); Target &module_sources(const SourceSet &sources); Target &header_unit(std::string header_path); Target &include_directory(std::string path, Visibility visibility = Visibility::Private); Target &system_include_directory(std::string path, Visibility visibility = Visibility::Private); Target &compile_option(std::string option); Target &compile_option(std::string option, Visibility visibility); Target &define(std::string macro, Visibility visibility = Visibility::Private); Target &link_option(std::string option, Visibility visibility = Visibility::Private); Target &link(const Target &target, Visibility visibility = Visibility::Private); Target &link(const ImportedTarget &target, Visibility visibility = Visibility::Private); Target &depends_on(const Target &producer); Target &c_standard(CStandard standard); Target &cxx_standard(CxxStandard standard); Target &precompiled_header(std::string header_path); Target &unity_build(bool enabled, std::size_t batch_size = 8); Target <o(LtoMode mode); Target &sanitizer(Sanitizer sanitizer); Target &per_file_option(std::string source_path, std::string option); Target &command(std::vector argv); Target &inputs(std::vector paths); Target &outputs(std::vector paths); Target &working_directory(std::string directory); Target &env(std::string key, std::string value); Target &nondeterministic(bool enabled = true); Target &enabled(bool flag); Target &install(std::string destination = {}, std::string component = "default"); [[nodiscard]] const TargetRecord &record() const; private: friend class Project; Target(std::weak_ptr owner, std::size_t index); std::weak_ptr owner_; std::size_t index_; }; class Dependency { public: Dependency &git(std::string url, Commit commit); Dependency &path(std::string local_path); Dependency &archive(std::string url, std::string sha256); Dependency &cmake(OptionMap options = {}); Dependency &meson(OptionMap options = {}); Dependency &conan(std::string reference, OptionMap options = {}); Dependency &native_builder(OptionMap options = {}); Dependency &package(std::string id, std::string range); Dependency ®istry(std::string authority_url); Dependency &feature(std::string name); Dependency &build_dependency(bool is_build_tool = true); [[nodiscard]] ImportedTarget target(std::string exported_name) const; [[nodiscard]] const DependencyRecord &record() const; private: friend class Project; Dependency(std::weak_ptr owner, std::size_t index); std::weak_ptr owner_; std::size_t index_; }; // Value of a `.native_builder({...})` option passed by the consuming project to this // dependency's build.cpp (exposed as VBS_DEP_OPTION_), or an empty string when unset. [[nodiscard]] std::string dependency_option(std::string_view name); class Project { public: explicit Project(ProfileContext context); ~Project(); Project(const Project &) = delete; Project &operator=(const Project &) = delete; Project(Project &&) = delete; Project &operator=(Project &&) = delete; [[nodiscard]] const ProfileContext &context() const noexcept; [[nodiscard]] Target executable(std::string name); [[nodiscard]] Target static_library(std::string name); [[nodiscard]] Target shared_library(std::string name); [[nodiscard]] Target header_only_library(std::string name); [[nodiscard]] Target object_library(std::string name); [[nodiscard]] Target custom_action(std::string name); [[nodiscard]] Target test(std::string name); [[nodiscard]] Dependency dependency(std::string name); // Links to another member of the enclosing workspace. The member path is supplied by the // workspace driver through VBS_WORKSPACE_MEMBER_ and built with the native provider. [[nodiscard]] Dependency workspace_dependency(std::string name); void dependencies_file(std::string toml_path = "vbs.deps.toml"); void install_file(std::string source_path, std::string destination, std::string component = "default"); void install_directory(std::string source_path, std::string destination, std::string component = "default"); [[nodiscard]] SourceSet glob(std::string pattern) const; [[nodiscard]] const std::vector &targets() const noexcept; [[nodiscard]] const std::vector &dependencies() const noexcept; [[nodiscard]] const std::vector &dependency_files() const noexcept; [[nodiscard]] const std::vector &install_rules() const noexcept; // Emits UTF-8 JSON; malformed UTF-8 in any recorded string throws invalid_argument. [[nodiscard]] std::string to_json() const; private: Target add_target(std::string name, TargetKind kind); std::shared_ptr state_; }; } // namespace Void::Build // Description entry points supplied by ordinary user C++ translation units. void build(Void::Build::Project &project); void profiles(Void::Build::Profiles &profiles); void workspace(Void::Build::Workspace &workspace);