162 lines
8.3 KiB
C++
162 lines
8.3 KiB
C++
|
|
// SPDX-License-Identifier: LGPL-3.0-only
|
||
|
|
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||
|
|
|
||
|
|
#include <stdlib.h>
|
||
|
|
import std;
|
||
|
|
import Crafter.Build;
|
||
|
|
namespace fs = std::filesystem;
|
||
|
|
using namespace Crafter;
|
||
|
|
|
||
|
|
namespace {
|
||
|
|
std::int32_t Failures = 0;
|
||
|
|
|
||
|
|
void Check(bool cond, std::string_view msg) {
|
||
|
|
if (!cond) {
|
||
|
|
std::println(std::cerr, "FAIL: {}", msg);
|
||
|
|
++Failures;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string ReadFile(const fs::path& path) {
|
||
|
|
std::ifstream in(path, std::ios::binary);
|
||
|
|
return std::string(std::istreambuf_iterator<char>(in), std::istreambuf_iterator<char>());
|
||
|
|
}
|
||
|
|
|
||
|
|
std::string BuildIt(Configuration& cfg) {
|
||
|
|
std::unordered_map<fs::path, std::shared_future<BuildResult>> depResults;
|
||
|
|
std::mutex depMutex;
|
||
|
|
return Build(cfg, depResults, depMutex).result;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// What a Flatpak's files/ holds is what the build puts there, and it is
|
||
|
|
// kept so without rewriting what did not change. A cfg.files entry
|
||
|
|
// dropped since goes from files/bin, which ships; a file the program wrote
|
||
|
|
// there itself stays, since the build cannot know what it is (the build
|
||
|
|
// reports it instead). A renamed app-id leaves no desktop entry or icon
|
||
|
|
// under the old name behind in files/share. What a directory
|
||
|
|
// written with a trailing separator or as "dir/." held, which the build
|
||
|
|
// copies flat, goes with the entry, and so does a program renamed in place,
|
||
|
|
// under its old name: neither may stay, taken for files the program wrote,
|
||
|
|
// nor may what a build that then failed copied there. And a library is
|
||
|
|
// copied again when its copy no longer matches the source, whatever the
|
||
|
|
// mtimes say: every file of an ostree deployment, the Flatpak's /app among
|
||
|
|
// them, has mtime 0, so a newer libc++ never looks newer than its copy.
|
||
|
|
int main() {
|
||
|
|
const fs::path scratch = fs::temp_directory_path() / "crafter-build-flatpak-incremental";
|
||
|
|
std::error_code ec;
|
||
|
|
fs::remove_all(scratch, ec);
|
||
|
|
fs::create_directories(scratch);
|
||
|
|
const fs::path info = scratch / "flatpak-info";
|
||
|
|
std::ofstream(info) << "[Application]\nname=net.catcrafts.CrafterBuild\nruntime=runtime/org.freedesktop.Sdk/x86_64/26.08\n";
|
||
|
|
setenv("CRAFTER_BUILD_FLATPAK_INFO", info.string().c_str(), 1);
|
||
|
|
if (NormalizeTarget(GetToolchain().hostTriple) != "x86_64-linux-gnu") {
|
||
|
|
std::println(std::cerr, "SKIP: the staged runtime is x86_64 and the host is {}", GetToolchain().hostTriple);
|
||
|
|
return 77;
|
||
|
|
}
|
||
|
|
if (!GetToolchain().libcxxDir.empty()) {
|
||
|
|
std::println(std::cerr, "SKIP: CRAFTER_BUILD_LIBCXX_DIR is set, and a build against it is no Flatpak");
|
||
|
|
return 77;
|
||
|
|
}
|
||
|
|
|
||
|
|
const fs::path fixture = fs::current_path() / "tests" / "FlatpakIncremental" / "fixture";
|
||
|
|
fs::remove_all(fixture / "bin", ec);
|
||
|
|
fs::remove_all(fixture / "build", ec);
|
||
|
|
Configuration app;
|
||
|
|
app.path = fixture;
|
||
|
|
app.name = "incremental-app";
|
||
|
|
app.outputName = "incremental";
|
||
|
|
app.target = HostTarget();
|
||
|
|
app.packaging = Packaging::Flatpak;
|
||
|
|
app.files = { fixture / "kept.txt", fixture / "dropped.txt" };
|
||
|
|
app.flatpak.appId = "net.example.Incremental";
|
||
|
|
app.flatpak.desktopFile = fixture / "app.desktop";
|
||
|
|
app.flatpak.icons = { fixture / "icon.svg" };
|
||
|
|
{
|
||
|
|
std::array<fs::path, 0> ifaces = {};
|
||
|
|
std::array<fs::path, 1> impls = { "main" };
|
||
|
|
app.GetInterfacesAndImplementations(ifaces, impls);
|
||
|
|
}
|
||
|
|
if (const std::string failed = BuildIt(app); !failed.empty()) {
|
||
|
|
std::println(std::cerr, "FAIL: build failed: {}", failed);
|
||
|
|
return 1;
|
||
|
|
}
|
||
|
|
const fs::path root = app.BinDir();
|
||
|
|
const fs::path bin = root / "files" / "bin";
|
||
|
|
Check(fs::exists(bin / "kept.txt") && fs::exists(bin / "dropped.txt"), "both files are placed");
|
||
|
|
|
||
|
|
app.files = { fixture / "kept.txt" };
|
||
|
|
Check(BuildIt(app).empty(), "the build without dropped.txt succeeds");
|
||
|
|
Check(!fs::exists(bin / "dropped.txt"), "a dropped cfg.files entry goes from files/bin");
|
||
|
|
Check(fs::exists(bin / "kept.txt") && fs::exists(bin / "incremental"), "what is still placed stays");
|
||
|
|
|
||
|
|
std::ofstream(bin / "written.bin") << "written by the program";
|
||
|
|
Check(BuildIt(app).empty(), "the build after the program wrote a file succeeds");
|
||
|
|
Check(fs::exists(bin / "written.bin"), "a file the program wrote stays");
|
||
|
|
|
||
|
|
app.flatpak.appId = "net.example.Renamed";
|
||
|
|
Check(BuildIt(app).empty(), "the build under the new app-id succeeds");
|
||
|
|
const fs::path share = root / "files" / "share";
|
||
|
|
Check(fs::exists(share / "applications" / "net.example.Renamed.desktop") && !fs::exists(share / "applications" / "net.example.Incremental.desktop"), "the desktop entry is renamed");
|
||
|
|
Check(fs::exists(share / "icons" / "hicolor" / "scalable" / "apps" / "net.example.Renamed.svg") && !fs::exists(share / "icons" / "hicolor" / "scalable" / "apps" / "net.example.Incremental.svg"), "the icon is renamed");
|
||
|
|
|
||
|
|
// A directory written with a trailing separator, "flat/", or as
|
||
|
|
// "flat/.", has no filename() of its own, and the build copies what it
|
||
|
|
// holds straight into files/bin, an asset directory's files as they
|
||
|
|
// are: those are the names it placed, and once the entries are dropped
|
||
|
|
// they go, rather than stay as files the program wrote.
|
||
|
|
const fs::path flat = scratch / "flat";
|
||
|
|
fs::create_directories(flat / "inner");
|
||
|
|
std::ofstream(flat / "flat.txt") << "flat\n";
|
||
|
|
std::ofstream(flat / "inner" / "nested.txt") << "nested\n";
|
||
|
|
const fs::path flatAssets = scratch / "flat-assets";
|
||
|
|
fs::create_directories(flatAssets);
|
||
|
|
std::ofstream(flatAssets / "mod.json") << "{}\n";
|
||
|
|
for (const fs::path& spelling : { fs::path(""), fs::path(".") }) {
|
||
|
|
app.files = { fixture / "kept.txt", flat / spelling };
|
||
|
|
app.assets = { flatAssets / spelling };
|
||
|
|
const std::string written = (flat / spelling).string();
|
||
|
|
Check(BuildIt(app).empty(), std::format("the build with directories written as {} succeeds", written));
|
||
|
|
Check(fs::exists(bin / "flat.txt") && fs::exists(bin / "inner" / "nested.txt") && fs::exists(bin / "mod.json"), std::format("what they hold is placed straight in files/bin, written as {}", written));
|
||
|
|
app.files = { fixture / "kept.txt" };
|
||
|
|
app.assets = {};
|
||
|
|
Check(BuildIt(app).empty(), "the build without them succeeds");
|
||
|
|
Check(!fs::exists(bin / "flat.txt") && !fs::exists(bin / "inner") && !fs::exists(bin / "mod.json"), std::format("and what they placed goes, written as {}", written));
|
||
|
|
}
|
||
|
|
|
||
|
|
// A program renamed in place: outputName is not in VariantId(), so the
|
||
|
|
// bin dir stays, and the old program goes as one the last build
|
||
|
|
// placed, rather than stay as a file the program wrote.
|
||
|
|
app.outputName = "renamed";
|
||
|
|
Check(BuildIt(app).empty(), "the build of the renamed program succeeds");
|
||
|
|
Check(fs::exists(bin / "renamed") && !fs::exists(bin / "incremental"), "files/bin holds the program under its new name, and not under the old");
|
||
|
|
|
||
|
|
// A build that fails after copying into files/bin, here at its link,
|
||
|
|
// placed what it copied all the same: once a build places it no more, it
|
||
|
|
// goes, rather than stay as a file the program wrote.
|
||
|
|
std::ofstream(scratch / "failed.txt") << "failed\n";
|
||
|
|
app.files = { fixture / "kept.txt", scratch / "failed.txt" };
|
||
|
|
app.linkFlags = { "-lcrafter-build-missing" };
|
||
|
|
Check(!BuildIt(app).empty(), "a build whose link fails fails");
|
||
|
|
Check(fs::exists(bin / "failed.txt"), "after copying its files into files/bin");
|
||
|
|
app.files = { fixture / "kept.txt" };
|
||
|
|
app.linkFlags = {};
|
||
|
|
Check(BuildIt(app).empty(), "the build after it succeeds");
|
||
|
|
Check(!fs::exists(bin / "failed.txt"), "and what the failed build placed goes");
|
||
|
|
|
||
|
|
// A copy that no longer matches its source, under the mtime it had.
|
||
|
|
const fs::path copy = root / "files" / "lib" / "libc++.so.1";
|
||
|
|
const std::string good = ReadFile(copy);
|
||
|
|
const fs::file_time_type stamp = fs::last_write_time(copy);
|
||
|
|
std::ofstream(copy, std::ios::binary | std::ios::trunc) << std::string(good.size(), 'x');
|
||
|
|
fs::last_write_time(copy, stamp);
|
||
|
|
Check(BuildIt(app).empty(), "the build after the copy went stale succeeds");
|
||
|
|
Check(ReadFile(copy) == good, "a library whose copy differs from its source is copied again, mtimes unchanged or not");
|
||
|
|
|
||
|
|
fs::remove_all(scratch, ec);
|
||
|
|
if (Failures > 0) {
|
||
|
|
std::println(std::cerr, "{} assertions failed", Failures);
|
||
|
|
return 1;
|
||
|
|
}
|
||
|
|
return 0;
|
||
|
|
}
|