#pragma once #include #include #include #include #include #include #include #include #include #include #include #include namespace vbs { inline constexpr int schema_version = 1; enum class CompilerFamily { gcc, clang }; enum class Visibility { private_, public_, interface_ }; enum class TargetKind { executable, static_library }; enum class SourceKind { file, glob }; enum class LinkKind { local, imported }; enum class DependencySourceKind { unspecified, git, package }; [[nodiscard]] std::string_view compiler_name(CompilerFamily compiler); [[nodiscard]] CompilerFamily parse_compiler(std::string_view name); struct ProfileContext { std::string name; CompilerFamily compiler{CompilerFamily::gcc}; std::string os; std::string architecture; std::vector compile_options; }; struct ProfileDescription { std::string name; std::vector compile_options; }; class Profiles { public: void add(std::string name, std::vector compile_options = {}); [[nodiscard]] const std::vector &records() const noexcept; [[nodiscard]] ProfileContext resolve(std::string_view name, CompilerFamily compiler, std::string os, std::string architecture) 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]] 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_}; }; struct LinkRecord { LinkKind kind; std::string target; std::string dependency; // Empty for a local target. Visibility visibility{Visibility::private_}; }; struct TargetRecord { std::string name; TargetKind kind; std::vector sources; std::vector include_directories; std::vector compile_options; std::vector links; }; 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; }; 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 &include_directory(std::string path, Visibility visibility = Visibility::private_); Target &compile_option(std::string option); Target &link(const Target &target, Visibility visibility = Visibility::private_); Target &link(const ImportedTarget &target, Visibility visibility = Visibility::private_); [[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 &cmake(OptionMap options = {}); Dependency &package(std::string id, std::string range); [[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_; }; 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]] Dependency dependency(std::string name); [[nodiscard]] SourceSet glob(std::string pattern) const; [[nodiscard]] const std::vector &targets() const noexcept; [[nodiscard]] const std::vector &dependencies() 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 vbs // Description entry points supplied by ordinary user C++ translation units. void build(vbs::Project &project); void profiles(vbs::Profiles &profiles);