feat(install): generate RPM packages
VoidBuildSystem CI / build-and-test-gcc16 (push) Failing after 7s
VoidBuildSystem CI / sanitizers-gcc16 (push) Failing after 5s
VoidBuildSystem CI / signing-openssl-gcc16 (push) Failing after 9s
VoidBuildSystem CI / wine-windows-gcc16 (push) Failing after 54s

This commit is contained in:
faumaray committed 2026-12-05 08:47:13 +03:00
1 parent 2b80718f98
commit 3198b32f2b
7 files changed
+365 -82

No files matched your search

+322 -75
View File
@@ -9,6 +9,7 @@
#include <Void/Data/StrictJson.hpp>
#include <Void/System/Path.hpp>
#include <Void/System/Types.hpp>
#include <Void/Text/Str.hpp>
#include <algorithm>
#include <format>
@@ -115,10 +116,10 @@ write_stored_zip(const std::filesystem::path &output,
records.push_back(
{entry.archive_path, crc, size, static_cast<std::uint32_t>(local.size()), entry.mode});
put_u32(local, 0x04034b50u);
put_u16(local, 20); // version needed
put_u16(local, 0); // flags
put_u16(local, 0); // stored
put_u16(local, 0); // time
put_u16(local, 20); // version needed
put_u16(local, 0); // flags
put_u16(local, 0); // stored
put_u16(local, 0); // time
put_u16(local, 0x21); // 1980-01-01
put_u32(local, crc);
put_u32(local, size);
@@ -172,10 +173,10 @@ void append_ar_member(std::string &out, std::string_view name, std::string_view
header.reserve(60);
header += padded_name;
header.resize(16, ' ');
header += std::format("{:<12}", 0); // mtime
header += std::format("{:<6}", 0); // uid
header += std::format("{:<6}", 0); // gid
header += std::format("{:<8}", 100644); // mode
header += std::format("{:<12}", 0); // mtime
header += std::format("{:<6}", 0); // uid
header += std::format("{:<6}", 0); // gid
header += std::format("{:<8}", 100644); // mode
header += std::format("{:<10}", data.size()); // size
header += "`\n";
out += header;
@@ -185,6 +186,248 @@ void append_ar_member(std::string &out, std::string_view name, std::string_view
}
}
// ── Minimal native RPM writer (uncompressed newc cpio payload) ──────────────────────────────
namespace rpm {
constexpr std::uint32_t kInt16 = 3;
constexpr std::uint32_t kInt32 = 4;
constexpr std::uint32_t kString = 6;
constexpr std::uint32_t kBin = 7;
constexpr std::uint32_t kStringArray = 8;
constexpr std::uint32_t kI18nString = 9;
struct Tag {
std::uint32_t tag{0};
std::uint32_t type{0};
std::string data;
std::uint32_t count{0};
};
void put_be32(std::string &out, std::uint32_t value) {
out.push_back(static_cast<char>((value >> 24) & 0xFFu));
out.push_back(static_cast<char>((value >> 16) & 0xFFu));
out.push_back(static_cast<char>((value >> 8) & 0xFFu));
out.push_back(static_cast<char>(value & 0xFFu));
}
Tag string_tag(std::uint32_t tag, std::string value, std::uint32_t type = kString) {
value.push_back('\0');
return Tag{tag, type, std::move(value), 1};
}
Tag int32_tag(std::uint32_t tag, std::vector<std::uint32_t> values) {
std::string data;
for (const auto value : values) {
put_be32(data, value);
}
return Tag{tag, kInt32, std::move(data), static_cast<std::uint32_t>(values.size())};
}
Tag int16_tag(std::uint32_t tag, std::vector<std::uint16_t> values) {
std::string data;
for (const auto value : values) {
data.push_back(static_cast<char>((value >> 8) & 0xFFu));
data.push_back(static_cast<char>(value & 0xFFu));
}
return Tag{tag, kInt16, std::move(data), static_cast<std::uint32_t>(values.size())};
}
Tag string_array_tag(std::uint32_t tag, const std::vector<std::string> &values) {
std::string data;
for (const auto &value : values) {
data += value;
data.push_back('\0');
}
return Tag{tag, kStringArray, std::move(data), static_cast<std::uint32_t>(values.size())};
}
std::string build_header(std::vector<Tag> tags, std::uint32_t region_tag) {
std::ranges::sort(tags, {}, &Tag::tag);
std::string store;
struct Index {
std::uint32_t tag;
std::uint32_t type;
std::uint32_t offset;
std::uint32_t count;
};
std::vector<Index> index;
for (const auto &entry : tags) {
if (entry.type == kInt16 || entry.type == kInt32) {
while (store.size() % 4 != 0) {
store.push_back('\0');
}
}
index.push_back(
{entry.tag, entry.type, static_cast<std::uint32_t>(store.size()), entry.count});
store += entry.data;
}
const auto trailer_pos = static_cast<std::uint32_t>(store.size());
const auto nindex = static_cast<std::uint32_t>(index.size() + 1);
put_be32(store, region_tag);
put_be32(store, kBin);
put_be32(store, 0u - nindex * 16u);
put_be32(store, 16);
index.insert(index.begin(), Index{region_tag, kBin, trailer_pos, 16});
std::string out{'\x8e', '\xad', '\xe8', 1, 0, 0, 0, 0};
put_be32(out, static_cast<std::uint32_t>(index.size()));
put_be32(out, static_cast<std::uint32_t>(store.size()));
for (const auto &entry : index) {
put_be32(out, entry.tag);
put_be32(out, entry.type);
put_be32(out, entry.offset);
put_be32(out, entry.count);
}
out += store;
return out;
}
std::string cpio_entry(std::string_view name, std::string_view data, std::uint32_t mode,
std::uint32_t ino) {
std::string out = std::format(
"070701{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}{:08X}", ino,
mode, 0u, 0u, 1u, 0u, static_cast<std::uint32_t>(data.size()), 0u, 0u, 0u, 0u,
static_cast<std::uint32_t>(name.size() + 1), 0u);
out += name;
out.push_back('\0');
out += data;
while (out.size() % 4 != 0) {
out.push_back('\0');
}
return out;
}
std::string cpio_archive(
const std::vector<std::pair<std::string, std::pair<std::string, std::uint32_t>>> &files) {
std::string out;
std::uint32_t ino = 1;
for (const auto &[name, content] : files) {
out += cpio_entry(name, content.first, content.second, ino++);
}
out += cpio_entry("TRAILER!!!", "", 0, 0);
while (out.size() % 512 != 0) {
out.push_back('\0');
}
return out;
}
std::string arch_name(std::string arch) {
arch = Void::Str::to_lower_ascii(arch);
if (arch == "amd64") {
return "x86_64";
}
if (arch == "arm64") {
return "aarch64";
}
return arch;
}
} // namespace rpm
// RPM package: lead + signature header + main header + uncompressed newc cpio payload.
Void::Result::Result<void, Void::Result::Error>
write_rpm_package(const std::filesystem::path &output, const InstallOptions &options,
const Graph::BuildGraph &graph,
const std::vector<Package::ArchiveEntrySpec> &entries) {
std::vector<std::pair<std::string, std::pair<std::string, std::uint32_t>>> payload_files;
std::vector<std::string> basenames;
std::vector<std::string> dirnames;
std::vector<std::uint32_t> dirindexes;
std::vector<std::uint32_t> file_sizes;
std::vector<std::uint16_t> file_modes;
std::vector<std::uint16_t> file_rdevs;
std::vector<std::uint32_t> file_mtimes;
std::vector<std::uint32_t> file_flags;
std::vector<std::string> file_digests;
std::vector<std::string> file_users;
std::vector<std::string> file_groups;
for (const auto &entry : entries) {
auto content = Void::FileSystem::read_file(entry.source_file);
if (!content) {
return std::unexpected{content.error()};
}
const auto dir = std::filesystem::path{entry.archive_path}.parent_path();
std::string dirname = dir.empty() ? std::string{} : dir.generic_string() + "/";
const auto dir_it = std::ranges::find(dirnames, dirname);
if (dir_it == dirnames.end()) {
dirnames.push_back(dirname);
dirindexes.push_back(static_cast<std::uint32_t>(dirnames.size() - 1));
} else {
dirindexes.push_back(
static_cast<std::uint32_t>(std::distance(dirnames.begin(), dir_it)));
}
basenames.push_back(std::filesystem::path{entry.archive_path}.filename().string());
file_sizes.push_back(static_cast<std::uint32_t>(content->size()));
file_modes.push_back(static_cast<std::uint16_t>(entry.mode == 0755u ? 0100755u : 0100644u));
file_rdevs.push_back(0);
file_mtimes.push_back(0);
file_flags.push_back(0);
file_digests.push_back(Util::sha256_hex(*content));
file_users.emplace_back("root");
file_groups.emplace_back("root");
payload_files.emplace_back(entry.archive_path, std::pair{std::move(*content), entry.mode});
}
const std::string payload = rpm::cpio_archive(payload_files);
std::uint64_t total_size = 0;
for (const auto size : file_sizes) {
total_size += size;
}
std::vector<rpm::Tag> main_tags;
main_tags.push_back(rpm::string_tag(1000, options.package_name));
main_tags.push_back(rpm::string_tag(1001, options.package_version));
main_tags.push_back(rpm::string_tag(1002, "1"));
main_tags.push_back(rpm::string_tag(
1004,
options.package_description.empty() ? options.package_name : options.package_description,
rpm::kI18nString));
main_tags.push_back(rpm::string_tag(1005, "Generated by VoidBuildSystem", rpm::kI18nString));
main_tags.push_back(rpm::int32_tag(1006, {0}));
main_tags.push_back(rpm::string_tag(1007, "localhost"));
main_tags.push_back(rpm::int32_tag(1009, {static_cast<std::uint32_t>(total_size)}));
main_tags.push_back(rpm::string_tag(1014, "MIT"));
main_tags.push_back(rpm::string_tag(1016, "Applications/System", rpm::kI18nString));
main_tags.push_back(rpm::string_tag(1021, "linux"));
main_tags.push_back(rpm::string_tag(1022, rpm::arch_name(graph.profile.architecture)));
main_tags.push_back(rpm::int32_tag(1028, file_sizes));
main_tags.push_back(rpm::int16_tag(1030, file_modes));
main_tags.push_back(rpm::int16_tag(1033, file_rdevs));
main_tags.push_back(rpm::int32_tag(1034, file_mtimes));
main_tags.push_back(rpm::string_array_tag(1035, file_digests));
main_tags.push_back(rpm::string_array_tag(1039, file_users));
main_tags.push_back(rpm::string_array_tag(1040, file_groups));
main_tags.push_back(rpm::int32_tag(1045, file_flags));
main_tags.push_back(rpm::int32_tag(1116, dirindexes));
main_tags.push_back(rpm::string_array_tag(1117, basenames));
main_tags.push_back(rpm::string_array_tag(1118, dirnames));
main_tags.push_back(rpm::string_tag(1124, "cpio"));
main_tags.push_back(rpm::string_tag(1126, ""));
main_tags.push_back(rpm::int32_tag(5011, {8})); // SHA-256 file digests
main_tags.push_back(rpm::string_array_tag(5092, {Util::sha256_hex(payload)}));
const std::string main_header = rpm::build_header(std::move(main_tags), 63);
std::vector<rpm::Tag> sig_tags;
sig_tags.push_back(rpm::string_tag(273, Util::sha256_hex(main_header)));
sig_tags.push_back(
rpm::int32_tag(1000, {static_cast<std::uint32_t>(main_header.size() + payload.size())}));
std::string signature = rpm::build_header(std::move(sig_tags), 62);
while ((96 + signature.size()) % 8 != 0) {
signature.push_back('\0');
}
std::string lead{'\xed', '\xab', '\xee', '\xdb', 3, 0, 0, 0, 0, 1};
const std::string name_version = options.package_name + "-" + options.package_version + "-1";
lead += name_version;
lead.resize(10 + 66, '\0');
lead += {0, 1, 0, 5};
lead.resize(96, '\0');
const std::string package = lead + signature + main_header + payload;
return Platform::atomic_write_file(output, package, true);
}
// Debian package: ar container with debian-binary, control.tar and data.tar (uncompressed).
Void::Result::Result<void, Void::Result::Error>
write_deb_package(const std::filesystem::path &output, const InstallOptions &options,
@@ -219,13 +462,11 @@ write_deb_package(const std::filesystem::path &output, const InstallOptions &opt
}
const std::string control_text =
"Package: " + options.package_name + "\n" + "Version: " + options.package_version + "\n" +
"Architecture: " + deb_arch + "\n" +
"Maintainer: " +
"Architecture: " + deb_arch + "\n" + "Maintainer: " +
(options.maintainer.empty() ? "Void Build <noreply@localhost>" : options.maintainer) +
"\n" + "Installed-Size: " + std::to_string(installed_size / 1024) + "\n" +
"Section: misc\nPriority: optional\n" + "Description: " +
(options.package_description.empty() ? options.package_name
: options.package_description) +
(options.package_description.empty() ? options.package_name : options.package_description) +
"\n";
const auto control_file = scratch / "control";
@@ -233,8 +474,8 @@ write_deb_package(const std::filesystem::path &output, const InstallOptions &opt
return std::unexpected{wrote.error()};
}
const auto control_tar = scratch / "control.tar";
auto control = Package::create_deterministic_tar(
control_tar, {{"control", control_file, 0644u}}, 0);
auto control =
Package::create_deterministic_tar(control_tar, {{"control", control_file, 0644u}}, 0);
if (!control) {
return std::unexpected{control.error()};
}
@@ -583,9 +824,8 @@ install_project(const Graph::ProjectDescription &description, const Graph::Build
archive_entries.reserve(report.installed_files.size());
for (const auto &file : report.installed_files) {
const auto meta = Platform::stat_lstat(file);
archive_entries.push_back(
{file.lexically_relative(prefix_abs).generic_string(), file,
meta ? (meta->mode & 0777u) : 0644u});
archive_entries.push_back({file.lexically_relative(prefix_abs).generic_string(), file,
meta ? (meta->mode & 0777u) : 0644u});
}
if (options.archive_format == "zip") {
@@ -593,67 +833,74 @@ install_project(const Graph::ProjectDescription &description, const Graph::Build
return std::unexpected{zipped.error()};
}
} else if (options.archive_format == "deb") {
if (auto packed = write_deb_package(*options.archive_path, options, graph,
archive_entries);
if (auto packed =
write_deb_package(*options.archive_path, options, graph, archive_entries);
!packed) {
return std::unexpected{packed.error()};
}
} else if (options.archive_format == "rpm") {
if (auto packed =
write_rpm_package(*options.archive_path, options, graph, archive_entries);
!packed) {
return std::unexpected{packed.error()};
}
} else {
const bool archive_as_script = options.archive_format == "sh";
const auto archive_target = archive_as_script
? stage_root / "installer-payload.tar"
: std::filesystem::path{*options.archive_path};
auto archived = Package::create_deterministic_tar(archive_target,
std::move(archive_entries), 0);
if (!archived) {
return std::unexpected{archived.error()};
}
const bool archive_as_script = options.archive_format == "sh";
const auto archive_target = archive_as_script
? stage_root / "installer-payload.tar"
: std::filesystem::path{*options.archive_path};
auto archived =
Package::create_deterministic_tar(archive_target, std::move(archive_entries), 0);
if (!archived) {
return std::unexpected{archived.error()};
}
if (archive_as_script) {
// Self-extracting installer: a POSIX sh header followed by the tar payload.
const auto make_header = [&](std::size_t payload_line) {
return "#!/bin/sh\n"
"# VoidBuildSystem self-extracting installer for " +
options.package_name + " " + options.package_version +
"\n"
"# Usage: sh <installer> [--prefix DIR] (default: ${PREFIX:-/usr/local})\n"
"set -eu\n"
"prefix=\"${PREFIX:-/usr/local}\"\n"
"if [ \"${1:-}\" = \"--prefix\" ]; then\n"
" prefix=\"${2:?--prefix requires a directory}\"\n"
"fi\n"
"tmp=\"$(mktemp -d)\"\n"
"trap 'rm -rf \"$tmp\"' EXIT\n"
"tail -n +" +
std::to_string(payload_line) +
" \"$0\" > \"$tmp/payload.tar\"\n"
"mkdir -p \"$prefix\"\n"
"tar -x -f \"$tmp/payload.tar\" -C \"$prefix\"\n"
"echo \"installed " +
options.package_name +
" to $prefix\"\n"
"exit 0\n";
};
std::size_t header_lines = 0;
for (const char c : make_header(0)) {
if (c == '\n') {
++header_lines;
if (archive_as_script) {
// Self-extracting installer: a POSIX sh header followed by the tar payload.
const auto make_header = [&](std::size_t payload_line) {
return "#!/bin/sh\n"
"# VoidBuildSystem self-extracting installer for " +
options.package_name + " " + options.package_version +
"\n"
"# Usage: sh <installer> [--prefix DIR] (default: "
"${PREFIX:-/usr/local})\n"
"set -eu\n"
"prefix=\"${PREFIX:-/usr/local}\"\n"
"if [ \"${1:-}\" = \"--prefix\" ]; then\n"
" prefix=\"${2:?--prefix requires a directory}\"\n"
"fi\n"
"tmp=\"$(mktemp -d)\"\n"
"trap 'rm -rf \"$tmp\"' EXIT\n"
"tail -n +" +
std::to_string(payload_line) +
" \"$0\" > \"$tmp/payload.tar\"\n"
"mkdir -p \"$prefix\"\n"
"tar -x -f \"$tmp/payload.tar\" -C \"$prefix\"\n"
"echo \"installed " +
options.package_name +
" to $prefix\"\n"
"exit 0\n";
};
std::size_t header_lines = 0;
for (const char c : make_header(0)) {
if (c == '\n') {
++header_lines;
}
}
const std::string header = make_header(header_lines + 1);
auto payload = Void::FileSystem::read_file(archive_target);
if (!payload) {
return std::unexpected{payload.error()};
}
std::string script = header;
script += *payload;
if (auto written =
Platform::atomic_write_file(*options.archive_path, script, true, 0755);
!written) {
return std::unexpected{written.error()};
}
}
const std::string header = make_header(header_lines + 1);
auto payload = Void::FileSystem::read_file(archive_target);
if (!payload) {
return std::unexpected{payload.error()};
}
std::string script = header;
script += *payload;
if (auto written =
Platform::atomic_write_file(*options.archive_path, script, true, 0755);
!written) {
return std::unexpected{written.error()};
}
}
}
report.archive_path = *options.archive_path;
@@ -684,14 +931,14 @@ install_project(const Graph::ProjectDescription &description, const Graph::Build
return std::unexpected{public_key.error()};
}
report.signature_path = options.archive_path->string() + ".sig";
if (auto written = Platform::atomic_write_file(*report.signature_path,
*signature + "\n", true);
if (auto written =
Platform::atomic_write_file(*report.signature_path, *signature + "\n", true);
!written) {
return std::unexpected{written.error()};
}
report.public_key_path = options.archive_path->string() + ".pub";
if (auto written = Platform::atomic_write_file(*report.public_key_path,
*public_key + "\n", true);
if (auto written =
Platform::atomic_write_file(*report.public_key_path, *public_key + "\n", true);
!written) {
return std::unexpected{written.error()};
}
+5 -6
View File
@@ -514,7 +514,6 @@ int execute_build_or_test(BuildCmdOptions opts, bool run_tests) {
return 1;
}
const std::string target_os = opts.target_os_arg.value_or(Toolchain::host_os_name());
const std::string target_arch =
opts.target_arch_arg.value_or(Toolchain::host_architecture_name());
@@ -1877,9 +1876,10 @@ int execute_install_command(const InstallCmdOptions &opts) {
iopts.export_native_metadata = !opts.no_native_export;
iopts.bundle_runtime_dependencies = opts.bundle_dependencies;
if (opts.archive_format_arg != "tar" && opts.archive_format_arg != "sh" &&
opts.archive_format_arg != "zip" && opts.archive_format_arg != "deb") {
opts.archive_format_arg != "zip" && opts.archive_format_arg != "deb" &&
opts.archive_format_arg != "rpm") {
std::cerr << "vbs install: unsupported --archive-format '" << opts.archive_format_arg
<< "' (expected tar, sh, zip or deb)\n";
<< "' (expected tar, sh, zip, deb or rpm)\n";
return 2;
}
if (opts.archive_arg && !opts.archive_arg->empty()) {
@@ -3040,7 +3040,7 @@ int main(int argc, char **argv) {
.option("--archive", "", "Write a deterministic installer archive of the prefix",
install_opts.archive_arg)
.option_default("--archive-format", "",
"Installer archive format: tar (default), sh, zip or deb",
"Installer archive format: tar (default), sh, zip, deb or rpm",
install_opts.archive_format_arg, "tar")
.option("--signing-key", "",
"Ed25519 seed file; signs the archive and writes .sha256/.sig/.pub",
@@ -3213,8 +3213,7 @@ int main(int argc, char **argv) {
auto &gc_cmd =
root.subcommand("gc", "Recover interrupted state and garbage-collect unreferenced blobs");
gc_cmd
.option_default("--build-dir", "-B", "Build directory", gc_opts.build_arg,
gc_default_build)
.option_default("--build-dir", "-B", "Build directory", gc_opts.build_arg, gc_default_build)
.option_default("--max-bytes", "",
"Target maximum retained CAS bytes (0=unreferenced only)",
gc_opts.max_bytes_arg, "0");