419 lines
17 KiB
C++
419 lines
17 KiB
C++
#include <Void/Build/Install/Installer.hpp>
|
|
|
|
#include <Void/Build/Platform/FileOps.hpp>
|
|
#include <Void/Build/Util/Json.hpp>
|
|
#include <Void/Build/Util/Sha256.hpp>
|
|
#include <Void/Core/Scope.hpp>
|
|
#include <Void/Data/StrictJson.hpp>
|
|
#include <Void/System/Path.hpp>
|
|
#include <Void/System/Types.hpp>
|
|
|
|
#include <algorithm>
|
|
#include <format>
|
|
#include <set>
|
|
#include <unistd.h>
|
|
|
|
namespace Void::Build::Install {
|
|
namespace {
|
|
|
|
Void::Result::Error make_error(Void::System::SystemErrorCode code, std::string msg) {
|
|
return Void::System::make_system_error(code, std::move(msg));
|
|
}
|
|
|
|
bool is_safe_relative_dest(const std::filesystem::path &rel) {
|
|
if (rel.empty() || rel.is_absolute()) {
|
|
return false;
|
|
}
|
|
const auto norm = rel.lexically_normal();
|
|
for (const auto &part : norm) {
|
|
if (part == "..") {
|
|
return false;
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
std::string default_dest_for_kind(TargetKind kind) {
|
|
switch (kind) {
|
|
case TargetKind::Executable:
|
|
case TargetKind::Test:
|
|
return "bin";
|
|
case TargetKind::StaticLibrary:
|
|
case TargetKind::SharedLibrary:
|
|
case TargetKind::ObjectLibrary:
|
|
return "lib";
|
|
case TargetKind::HeaderOnly:
|
|
case TargetKind::CustomAction:
|
|
return "share";
|
|
}
|
|
return "share";
|
|
}
|
|
|
|
std::string escape_cmake_str(std::string_view s) {
|
|
std::string out;
|
|
for (const char c : s) {
|
|
if (c == '\\' || c == '"' || c == '$' || c == ';') {
|
|
out.push_back('\\');
|
|
}
|
|
out.push_back(c);
|
|
}
|
|
return out;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
Void::Result::Result<InstallReport, Void::Result::Error>
|
|
install_project(const Graph::ProjectDescription &description, const Graph::BuildGraph &graph,
|
|
const InstallOptions &options) {
|
|
if (options.prefix.empty()) {
|
|
return std::unexpected{
|
|
make_error(Void::System::SystemErrorCode::invalid_argument, "install prefix is empty")};
|
|
}
|
|
if (options.package_name.empty()) {
|
|
return std::unexpected{make_error(Void::System::SystemErrorCode::invalid_argument,
|
|
"install package_name is empty")};
|
|
}
|
|
|
|
std::error_code ec;
|
|
const auto prefix_abs = std::filesystem::weakly_canonical(options.prefix, ec);
|
|
const auto proj_root = graph.project_root;
|
|
if (Platform::path_is_within(proj_root, prefix_abs) &&
|
|
!Platform::path_is_within(graph.profile_build_dir, prefix_abs) && prefix_abs == proj_root) {
|
|
return std::unexpected{make_error(Void::System::SystemErrorCode::permission_denied,
|
|
"refusing to install directly onto project source root")};
|
|
}
|
|
|
|
// Build complete list of staged copy operations and detect destination collisions upfront!
|
|
struct StagedItem {
|
|
std::filesystem::path src;
|
|
std::filesystem::path rel_dest;
|
|
std::uint32_t mode{0644};
|
|
std::string target_name;
|
|
};
|
|
std::vector<StagedItem> items;
|
|
std::set<std::filesystem::path> dest_set;
|
|
|
|
auto add_item =
|
|
[&](const std::filesystem::path &src, const std::filesystem::path &rel_dest,
|
|
std::uint32_t mode,
|
|
std::string target_name = {}) -> Void::Result::Result<void, Void::Result::Error> {
|
|
if (!is_safe_relative_dest(rel_dest)) {
|
|
return std::unexpected{make_error(
|
|
Void::System::SystemErrorCode::permission_denied,
|
|
std::format("install destination '{}' escapes prefix", rel_dest.string()))};
|
|
}
|
|
const auto norm = rel_dest.lexically_normal();
|
|
if (!dest_set.insert(norm).second) {
|
|
return std::unexpected{
|
|
make_error(Void::System::SystemErrorCode::invalid_argument,
|
|
std::format("install destination collision on '{}'", norm.string()))};
|
|
}
|
|
if (!std::filesystem::is_regular_file(src, ec)) {
|
|
return std::unexpected{make_error(
|
|
Void::System::SystemErrorCode::not_found,
|
|
std::format("install source artifact '{}' does not exist (build target first)",
|
|
src.string()))};
|
|
}
|
|
items.push_back({src, norm, mode, std::move(target_name)});
|
|
return {};
|
|
};
|
|
|
|
// 1. Installed targets.
|
|
struct ExportedTarget {
|
|
std::string name;
|
|
TargetKind kind;
|
|
std::filesystem::path rel_artifact;
|
|
std::vector<std::string> public_defines;
|
|
std::vector<std::string> public_compile_opts;
|
|
std::vector<std::string> local_deps;
|
|
};
|
|
std::vector<ExportedTarget> exported_targets;
|
|
|
|
for (const auto &trec : description.targets) {
|
|
if (!trec.install_rule) {
|
|
continue;
|
|
}
|
|
if (options.component && trec.install_rule->component != *options.component) {
|
|
continue;
|
|
}
|
|
const std::string dest_dir = trec.install_rule->destination.empty()
|
|
? default_dest_for_kind(trec.kind)
|
|
: trec.install_rule->destination;
|
|
const auto sum_it =
|
|
std::ranges::find(graph.targets, trec.name, &Graph::ResolvedTargetSummary::name);
|
|
if (sum_it == graph.targets.end()) {
|
|
continue;
|
|
}
|
|
|
|
ExportedTarget et;
|
|
et.name = trec.name;
|
|
et.kind = trec.kind;
|
|
for (const auto &d : trec.defines) {
|
|
if (d.visibility != Visibility::Private) {
|
|
et.public_defines.push_back(d.value);
|
|
}
|
|
}
|
|
for (const auto &o : trec.scoped_compile_options) {
|
|
if (o.visibility != Visibility::Private) {
|
|
et.public_compile_opts.push_back(o.value);
|
|
}
|
|
}
|
|
for (const auto &l : trec.links) {
|
|
if (l.kind == LinkKind::Local &&
|
|
(l.visibility != Visibility::Private || trec.kind == TargetKind::StaticLibrary)) {
|
|
et.local_deps.push_back(l.target);
|
|
}
|
|
}
|
|
|
|
if (!sum_it->primary_outputs.empty()) {
|
|
const auto &art = sum_it->primary_outputs.front();
|
|
const auto rel_dest = std::filesystem::path{dest_dir} / art.filename();
|
|
const std::uint32_t mode =
|
|
(trec.kind == TargetKind::Executable || trec.kind == TargetKind::SharedLibrary)
|
|
? 0755u
|
|
: 0644u;
|
|
if (auto r = add_item(art, rel_dest, mode, trec.name); !r) {
|
|
return std::unexpected{r.error()};
|
|
}
|
|
et.rel_artifact = rel_dest;
|
|
}
|
|
exported_targets.push_back(std::move(et));
|
|
}
|
|
|
|
// 2. Project file & directory install rules (headers/data).
|
|
for (const auto &rule : description.install_rules) {
|
|
if (options.component && rule.component != *options.component) {
|
|
continue;
|
|
}
|
|
const auto src_abs = std::filesystem::path{rule.source_path}.is_absolute()
|
|
? std::filesystem::path{rule.source_path}
|
|
: (proj_root / rule.source_path);
|
|
if (rule.is_directory) {
|
|
if (!std::filesystem::is_directory(src_abs, ec)) {
|
|
return std::unexpected{make_error(
|
|
Void::System::SystemErrorCode::not_found,
|
|
std::format("install directory '{}' does not exist", src_abs.string()))};
|
|
}
|
|
for (const auto &ent : std::filesystem::recursive_directory_iterator(src_abs, ec)) {
|
|
if (ent.is_regular_file(ec)) {
|
|
const auto rel = ent.path().lexically_relative(src_abs);
|
|
if (auto r = add_item(ent.path(), std::filesystem::path{rule.destination} / rel,
|
|
0644u);
|
|
!r) {
|
|
return std::unexpected{r.error()};
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
const auto rel_dest = std::filesystem::path{rule.destination} / src_abs.filename();
|
|
if (auto r = add_item(src_abs, rel_dest, 0644u); !r) {
|
|
return std::unexpected{r.error()};
|
|
}
|
|
}
|
|
}
|
|
|
|
// 3. Stage everything privately first so failed installs never leave broken metadata.
|
|
const auto stage_root = graph.profile_build_dir /
|
|
std::format("install-stage-{}", static_cast<long long>(::getpid()));
|
|
std::filesystem::remove_all(stage_root, ec);
|
|
std::filesystem::create_directories(stage_root, ec);
|
|
auto stage_guard = Void::Scope::on_exit([&] { std::filesystem::remove_all(stage_root, ec); });
|
|
|
|
std::vector<std::filesystem::path> owned_rel_files;
|
|
for (const auto &item : items) {
|
|
const auto staged = stage_root / item.rel_dest;
|
|
auto copied = Platform::copy_isolated(item.src, staged, item.mode);
|
|
if (!copied) {
|
|
return std::unexpected{copied.error()};
|
|
}
|
|
owned_rel_files.push_back(item.rel_dest);
|
|
}
|
|
|
|
// 4. Generate relocatable native package metadata (ZERO build/snapshot paths!).
|
|
std::optional<std::filesystem::path> native_rel;
|
|
if (options.export_native_metadata) {
|
|
using Util::json_array;
|
|
using Util::json_quote;
|
|
|
|
native_rel = std::filesystem::path{"share"} / "vbs" / options.package_name / "package.json";
|
|
std::string pjson =
|
|
"{\"schema_version\":1,\"name\":" + json_quote(options.package_name) +
|
|
",\"version\":" + json_quote(options.package_version) +
|
|
",\"profile\":" + json_quote(graph.profile.name) +
|
|
",\"compiler\":" + json_quote(compiler_name(graph.profile.compiler)) +
|
|
",\"os\":" + json_quote(graph.profile.os) +
|
|
",\"architecture\":" + json_quote(graph.profile.architecture) +
|
|
",\"include_dir\":\"include\",\"targets\":" +
|
|
json_array(exported_targets,
|
|
[](const ExportedTarget &et) {
|
|
return "{\"name\":" + json_quote(et.name) +
|
|
",\"kind\":" + json_quote(target_kind_name(et.kind)) +
|
|
",\"artifact\":" + json_quote(et.rel_artifact.generic_string()) +
|
|
",\"defines\":" + json_array(et.public_defines, json_quote) +
|
|
",\"compile_options\":" +
|
|
json_array(et.public_compile_opts, json_quote) +
|
|
",\"dependencies\":" + json_array(et.local_deps, json_quote) +
|
|
'}';
|
|
}) +
|
|
"}\n";
|
|
if (auto w = Platform::atomic_write_file(stage_root / *native_rel, pjson, false); !w) {
|
|
return std::unexpected{w.error()};
|
|
}
|
|
owned_rel_files.push_back(*native_rel);
|
|
}
|
|
|
|
// 5. Generate relocatable CMake Config + Targets files using ${_vbs_prefix} relative paths!
|
|
std::optional<std::filesystem::path> cmake_rel;
|
|
if (options.export_cmake_config) {
|
|
const auto cmake_dir = std::filesystem::path{"lib"} / "cmake" / options.package_name;
|
|
cmake_rel = cmake_dir / (options.package_name + "Config.cmake");
|
|
const auto targets_rel = cmake_dir / (options.package_name + "Targets.cmake");
|
|
|
|
std::string cfg_cmake =
|
|
"# Relocatable CMake config generated by VoidBuildSystem\n"
|
|
"get_filename_component(_vbs_prefix \"${CMAKE_CURRENT_LIST_DIR}/../../..\" ABSOLUTE)\n"
|
|
"include(\"${CMAKE_CURRENT_LIST_DIR}/" +
|
|
options.package_name + "Targets.cmake\")\n";
|
|
|
|
std::string tgt_cmake =
|
|
"# Relocatable CMake imported targets generated by VoidBuildSystem\n"
|
|
"get_filename_component(_vbs_prefix \"${CMAKE_CURRENT_LIST_DIR}/../../..\" ABSOLUTE)\n";
|
|
for (const auto &et : exported_targets) {
|
|
const std::string full = options.package_name + "::" + et.name;
|
|
std::string cm_type = "STATIC";
|
|
if (et.kind == TargetKind::SharedLibrary) {
|
|
cm_type = "SHARED";
|
|
} else if (et.kind == TargetKind::HeaderOnly) {
|
|
cm_type = "INTERFACE";
|
|
} else if (et.kind == TargetKind::Executable) {
|
|
continue;
|
|
}
|
|
tgt_cmake += "if(NOT TARGET " + full + ")\n";
|
|
tgt_cmake += " add_library(" + full + " " + cm_type + " IMPORTED)\n";
|
|
tgt_cmake += " set_target_properties(" + full + " PROPERTIES\n";
|
|
if (!et.rel_artifact.empty()) {
|
|
tgt_cmake += " IMPORTED_LOCATION \"${_vbs_prefix}/" +
|
|
escape_cmake_str(et.rel_artifact.generic_string()) + "\"\n";
|
|
}
|
|
tgt_cmake += " INTERFACE_INCLUDE_DIRECTORIES \"${_vbs_prefix}/include\"\n";
|
|
if (!et.public_defines.empty()) {
|
|
tgt_cmake += " INTERFACE_COMPILE_DEFINITIONS \"";
|
|
for (std::size_t i = 0; i < et.public_defines.size(); ++i) {
|
|
if (i > 0) {
|
|
tgt_cmake += ";";
|
|
}
|
|
tgt_cmake += escape_cmake_str(et.public_defines[i]);
|
|
}
|
|
tgt_cmake += "\"\n";
|
|
}
|
|
if (!et.local_deps.empty()) {
|
|
tgt_cmake += " INTERFACE_LINK_LIBRARIES \"";
|
|
for (std::size_t i = 0; i < et.local_deps.size(); ++i) {
|
|
if (i > 0) {
|
|
tgt_cmake += ";";
|
|
}
|
|
tgt_cmake += options.package_name + "::" + et.local_deps[i];
|
|
}
|
|
tgt_cmake += "\"\n";
|
|
}
|
|
tgt_cmake += " )\nendif()\n";
|
|
}
|
|
|
|
if (auto w = Platform::atomic_write_file(stage_root / *cmake_rel, cfg_cmake, false); !w) {
|
|
return std::unexpected{w.error()};
|
|
}
|
|
if (auto w = Platform::atomic_write_file(stage_root / targets_rel, tgt_cmake, false); !w) {
|
|
return std::unexpected{w.error()};
|
|
}
|
|
owned_rel_files.push_back(*cmake_rel);
|
|
owned_rel_files.push_back(targets_rel);
|
|
}
|
|
|
|
// 6. Publish staged files into prefix atomically and write ownership manifest.
|
|
std::filesystem::create_directories(prefix_abs, ec);
|
|
InstallReport report;
|
|
report.prefix = prefix_abs;
|
|
|
|
for (const auto &rel : owned_rel_files) {
|
|
const auto staged = stage_root / rel;
|
|
const auto final_dst = prefix_abs / rel;
|
|
auto meta = Platform::stat_lstat(staged);
|
|
auto copied = Platform::copy_isolated(staged, final_dst, meta ? meta->mode : 0644u);
|
|
if (!copied) {
|
|
return std::unexpected{copied.error()};
|
|
}
|
|
report.installed_files.push_back(final_dst);
|
|
}
|
|
|
|
report.manifest_path = prefix_abs / ".vbs-install-manifest.json";
|
|
if (native_rel) {
|
|
report.native_package_file = prefix_abs / *native_rel;
|
|
}
|
|
if (cmake_rel) {
|
|
report.cmake_config_file = prefix_abs / *cmake_rel;
|
|
}
|
|
|
|
const std::string manifest_json =
|
|
"{\"schema_version\":1,\"package\":" + Util::json_quote(options.package_name) +
|
|
",\"files\":" +
|
|
Util::json_array(
|
|
owned_rel_files,
|
|
[](const std::filesystem::path &p) { return Util::json_quote(p.generic_string()); }) +
|
|
"}\n";
|
|
auto mw = Platform::atomic_write_file(report.manifest_path, manifest_json, true);
|
|
if (!mw) {
|
|
return std::unexpected{mw.error()};
|
|
}
|
|
return report;
|
|
}
|
|
|
|
Void::Result::Result<std::size_t, Void::Result::Error>
|
|
uninstall_prefix(const std::filesystem::path &prefix) {
|
|
const auto manifest_path = prefix / ".vbs-install-manifest.json";
|
|
auto text = Void::FileSystem::read_file(manifest_path);
|
|
if (!text) {
|
|
return std::unexpected{make_error(
|
|
Void::System::SystemErrorCode::not_found,
|
|
std::format("no VBS install manifest found at '{}'", manifest_path.string()))};
|
|
}
|
|
auto parsed = Void::Json::Strict::parse(*text);
|
|
if (!parsed) {
|
|
return std::unexpected{make_error(Void::System::SystemErrorCode::invalid_argument,
|
|
"corrupt VBS install manifest")};
|
|
}
|
|
const auto *root = std::get_if<Void::Json::Strict::Value::Object>(&parsed->value);
|
|
if (!root) {
|
|
return std::unexpected{make_error(Void::System::SystemErrorCode::invalid_argument,
|
|
"invalid VBS install manifest")};
|
|
}
|
|
|
|
std::size_t removed = 0;
|
|
std::error_code ec;
|
|
const auto canon_prefix = std::filesystem::weakly_canonical(prefix, ec);
|
|
const auto fit = root->find("files");
|
|
if (fit != root->end()) {
|
|
if (const auto *arr = std::get_if<Void::Json::Strict::Value::Array>(&fit->second.value)) {
|
|
for (const auto &v : *arr) {
|
|
if (const auto *s = std::get_if<std::string>(&v.value)) {
|
|
const std::filesystem::path rel{*s};
|
|
if (!is_safe_relative_dest(rel)) {
|
|
continue;
|
|
}
|
|
const auto target = (canon_prefix / rel).lexically_normal();
|
|
if (Platform::path_is_within(canon_prefix, target) &&
|
|
std::filesystem::is_regular_file(target, ec)) {
|
|
std::filesystem::remove(target, ec);
|
|
if (!ec) {
|
|
++removed;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
std::filesystem::remove(manifest_path, ec);
|
|
return removed;
|
|
}
|
|
|
|
} // namespace Void::Build::Install
|