202 lines
6.2 KiB
C++
202 lines
6.2 KiB
C++
#pragma once
|
|
|
|
#include <concepts>
|
|
#include <cstddef>
|
|
#include <cstdint>
|
|
#include <initializer_list>
|
|
#include <map>
|
|
#include <memory>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <string_view>
|
|
#include <utility>
|
|
#include <variant>
|
|
#include <vector>
|
|
|
|
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<std::string> compile_options;
|
|
};
|
|
|
|
struct ProfileDescription {
|
|
std::string name;
|
|
std::vector<std::string> compile_options;
|
|
};
|
|
|
|
class Profiles {
|
|
public:
|
|
void add(std::string name, std::vector<std::string> compile_options = {});
|
|
[[nodiscard]] const std::vector<ProfileDescription> &records() const noexcept;
|
|
[[nodiscard]] ProfileContext resolve(std::string_view name, CompilerFamily compiler,
|
|
std::string os, std::string architecture) const;
|
|
|
|
private:
|
|
std::vector<ProfileDescription> 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<std::string_view> files);
|
|
[[nodiscard]] static SourceSet file(std::string path);
|
|
[[nodiscard]] static SourceSet glob(std::string pattern);
|
|
[[nodiscard]] const std::vector<SourceRecord> &records() const noexcept;
|
|
|
|
private:
|
|
std::vector<SourceRecord> records_;
|
|
};
|
|
|
|
struct OptionValue {
|
|
using Value = std::variant<std::string, bool, std::int64_t>;
|
|
Value value;
|
|
|
|
OptionValue(std::string text) : value{std::move(text)} {}
|
|
OptionValue(const char *text) : OptionValue{std::string{text}} {}
|
|
OptionValue(bool boolean) : value{boolean} {}
|
|
template <std::integral Integer>
|
|
requires(!std::same_as<Integer, bool>)
|
|
OptionValue(Integer integer) : value{std::int64_t{0}} {
|
|
if (!std::in_range<std::int64_t>(integer)) {
|
|
throw std::invalid_argument{"option integer is out of range"};
|
|
}
|
|
value = static_cast<std::int64_t>(integer);
|
|
}
|
|
};
|
|
using OptionMap = std::map<std::string, OptionValue, std::less<>>;
|
|
|
|
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<SourceRecord> sources;
|
|
std::vector<IncludeDirectory> include_directories;
|
|
std::vector<std::string> compile_options;
|
|
std::vector<LinkRecord> 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<detail::ProjectState> owner, std::size_t dependency,
|
|
std::string name);
|
|
std::weak_ptr<detail::ProjectState> 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<detail::ProjectState> owner, std::size_t index);
|
|
std::weak_ptr<detail::ProjectState> 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<detail::ProjectState> owner, std::size_t index);
|
|
std::weak_ptr<detail::ProjectState> 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<TargetRecord> &targets() const noexcept;
|
|
[[nodiscard]] const std::vector<DependencyRecord> &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<detail::ProjectState> state_;
|
|
};
|
|
|
|
} // namespace vbs
|
|
|
|
// Description entry points supplied by ordinary user C++ translation units.
|
|
void build(vbs::Project &project);
|
|
void profiles(vbs::Profiles &profiles);
|