feat(install): generate ZIP and Debian packages
This commit is contained in:
1 parent
a808596462
commit
9509c0691e
8 files changed
+345
-14
No files matched your search
+215
-5
@@ -61,6 +61,199 @@ std::string escape_cmake_str(std::string_view s) {
|
||||
return out;
|
||||
}
|
||||
|
||||
void put_u16(std::string &out, std::uint16_t value) {
|
||||
out.push_back(static_cast<char>(value & 0xFFu));
|
||||
out.push_back(static_cast<char>((value >> 8) & 0xFFu));
|
||||
}
|
||||
|
||||
void put_u32(std::string &out, std::uint32_t value) {
|
||||
for (int shift = 0; shift < 32; shift += 8) {
|
||||
out.push_back(static_cast<char>((value >> shift) & 0xFFu));
|
||||
}
|
||||
}
|
||||
|
||||
std::uint32_t crc32_bytes(std::string_view data) {
|
||||
static const auto table = [] {
|
||||
std::array<std::uint32_t, 256> t{};
|
||||
for (std::uint32_t i = 0; i < 256; ++i) {
|
||||
std::uint32_t c = i;
|
||||
for (int k = 0; k < 8; ++k) {
|
||||
c = (c & 1u) != 0u ? (0xEDB88320u ^ (c >> 1)) : (c >> 1);
|
||||
}
|
||||
t[i] = c;
|
||||
}
|
||||
return t;
|
||||
}();
|
||||
std::uint32_t crc = 0xFFFFFFFFu;
|
||||
for (const char ch : data) {
|
||||
crc = table[(crc ^ static_cast<std::uint8_t>(ch)) & 0xFFu] ^ (crc >> 8);
|
||||
}
|
||||
return crc ^ 0xFFFFFFFFu;
|
||||
}
|
||||
|
||||
// Stored (uncompressed) deterministic ZIP archive of the installed files.
|
||||
Void::Result::Result<void, Void::Result::Error>
|
||||
write_stored_zip(const std::filesystem::path &output,
|
||||
const std::vector<Package::ArchiveEntrySpec> &entries) {
|
||||
struct Record {
|
||||
std::string name;
|
||||
std::uint32_t crc{0};
|
||||
std::uint32_t size{0};
|
||||
std::uint32_t offset{0};
|
||||
std::uint32_t mode{0644};
|
||||
};
|
||||
std::string local;
|
||||
std::vector<Record> records;
|
||||
for (const auto &entry : entries) {
|
||||
auto content = Void::FileSystem::read_file(entry.source_file);
|
||||
if (!content) {
|
||||
return std::unexpected{content.error()};
|
||||
}
|
||||
const auto crc = crc32_bytes(*content);
|
||||
const auto size = static_cast<std::uint32_t>(content->size());
|
||||
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, 0x21); // 1980-01-01
|
||||
put_u32(local, crc);
|
||||
put_u32(local, size);
|
||||
put_u32(local, size);
|
||||
put_u16(local, static_cast<std::uint16_t>(entry.archive_path.size()));
|
||||
put_u16(local, 0); // extra
|
||||
local += entry.archive_path;
|
||||
local += *content;
|
||||
}
|
||||
|
||||
std::string central;
|
||||
for (const auto &record : records) {
|
||||
put_u32(central, 0x02014b50u);
|
||||
put_u16(central, 0x031e); // made by UNIX
|
||||
put_u16(central, 20);
|
||||
put_u16(central, 0);
|
||||
put_u16(central, 0);
|
||||
put_u16(central, 0);
|
||||
put_u16(central, 0x21);
|
||||
put_u32(central, record.crc);
|
||||
put_u32(central, record.size);
|
||||
put_u32(central, record.size);
|
||||
put_u16(central, static_cast<std::uint16_t>(record.name.size()));
|
||||
put_u16(central, 0); // extra
|
||||
put_u16(central, 0); // comment
|
||||
put_u16(central, 0); // disk
|
||||
put_u16(central, 0); // internal attrs
|
||||
put_u32(central, (record.mode & 0777u) << 16);
|
||||
put_u32(central, record.offset);
|
||||
central += record.name;
|
||||
}
|
||||
|
||||
std::string eocd;
|
||||
put_u32(eocd, 0x06054b50u);
|
||||
put_u16(eocd, 0);
|
||||
put_u16(eocd, 0);
|
||||
put_u16(eocd, static_cast<std::uint16_t>(records.size()));
|
||||
put_u16(eocd, static_cast<std::uint16_t>(records.size()));
|
||||
put_u32(eocd, static_cast<std::uint32_t>(central.size()));
|
||||
put_u32(eocd, static_cast<std::uint32_t>(local.size()));
|
||||
put_u16(eocd, 0);
|
||||
|
||||
std::string zip = local + central + eocd;
|
||||
return Platform::atomic_write_file(output, zip, true);
|
||||
}
|
||||
|
||||
void append_ar_member(std::string &out, std::string_view name, std::string_view data) {
|
||||
std::string header;
|
||||
std::string padded_name{name};
|
||||
padded_name.push_back('/');
|
||||
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("{:<10}", data.size()); // size
|
||||
header += "`\n";
|
||||
out += header;
|
||||
out += data;
|
||||
if (data.size() % 2 != 0) {
|
||||
out.push_back('\n');
|
||||
}
|
||||
}
|
||||
|
||||
// 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,
|
||||
const Graph::BuildGraph &graph,
|
||||
const std::vector<Package::ArchiveEntrySpec> &entries) {
|
||||
const auto scratch = output.parent_path() / (output.filename().string() + ".members");
|
||||
std::filesystem::remove_all(scratch);
|
||||
std::error_code ec;
|
||||
std::filesystem::create_directories(scratch, ec);
|
||||
auto cleanup = Void::Scope::on_exit([&] { std::filesystem::remove_all(scratch, ec); });
|
||||
|
||||
const auto data_tar = scratch / "data.tar";
|
||||
auto data = Package::create_deterministic_tar(data_tar, entries, 0);
|
||||
if (!data) {
|
||||
return std::unexpected{data.error()};
|
||||
}
|
||||
|
||||
std::uint64_t installed_size = 0;
|
||||
for (const auto &entry : entries) {
|
||||
if (const auto meta = Platform::stat_lstat(entry.source_file); meta && meta->exists) {
|
||||
installed_size += meta->size;
|
||||
}
|
||||
}
|
||||
std::string deb_arch = graph.profile.architecture;
|
||||
std::ranges::transform(deb_arch, deb_arch.begin(), [](char c) {
|
||||
return static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
|
||||
});
|
||||
if (deb_arch == "x86_64") {
|
||||
deb_arch = "amd64";
|
||||
} else if (deb_arch == "aarch64" || deb_arch == "arm64") {
|
||||
deb_arch = "arm64";
|
||||
}
|
||||
const std::string control_text =
|
||||
"Package: " + options.package_name + "\n" + "Version: " + options.package_version + "\n" +
|
||||
"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) +
|
||||
"\n";
|
||||
|
||||
const auto control_file = scratch / "control";
|
||||
if (auto wrote = Platform::atomic_write_file(control_file, control_text, false); !wrote) {
|
||||
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);
|
||||
if (!control) {
|
||||
return std::unexpected{control.error()};
|
||||
}
|
||||
|
||||
auto data_bytes = Void::FileSystem::read_file(data_tar);
|
||||
if (!data_bytes) {
|
||||
return std::unexpected{data_bytes.error()};
|
||||
}
|
||||
auto control_bytes = Void::FileSystem::read_file(control_tar);
|
||||
if (!control_bytes) {
|
||||
return std::unexpected{control_bytes.error()};
|
||||
}
|
||||
|
||||
std::string deb = "!<arch>\n";
|
||||
append_ar_member(deb, "debian-binary", "2.0\n");
|
||||
append_ar_member(deb, "control.tar", *control_bytes);
|
||||
append_ar_member(deb, "data.tar", *data_bytes);
|
||||
return Platform::atomic_write_file(output, deb, true);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
Void::Result::Result<InstallReport, Void::Result::Error>
|
||||
@@ -394,17 +587,34 @@ install_project(const Graph::ProjectDescription &description, const Graph::Build
|
||||
meta ? (meta->mode & 0777u) : 0644u});
|
||||
}
|
||||
|
||||
const auto archive_target =
|
||||
options.archive_as_script
|
||||
? stage_root / "installer-payload.tar"
|
||||
: std::filesystem::path{*options.archive_path};
|
||||
if (options.archive_format == "zip") {
|
||||
if (auto zipped = write_stored_zip(*options.archive_path, archive_entries); !zipped) {
|
||||
return std::unexpected{zipped.error()};
|
||||
}
|
||||
report.archive_path = *options.archive_path;
|
||||
return report;
|
||||
}
|
||||
if (options.archive_format == "deb") {
|
||||
if (auto packed = write_deb_package(*options.archive_path, options, graph,
|
||||
archive_entries);
|
||||
!packed) {
|
||||
return std::unexpected{packed.error()};
|
||||
}
|
||||
report.archive_path = *options.archive_path;
|
||||
return report;
|
||||
}
|
||||
|
||||
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 (options.archive_as_script) {
|
||||
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"
|
||||
|
||||
Reference in new issue
Block a user