Crafter.Build/tests/FlatpakDependencyExecutable/main.cpp
Jorijn van der Graaf e0ac8ee600
feat: install a Flatpak's data under /app/share
A program packaged as a Flatpak could only have its files beside it in
files/bin, where cfg.files goes. One that finds its data at <its
directory>/../share/<name>, as Crafter.Review finds its process/
directory, had no way to get it there: files/share belongs to the
writer, which removes anything it did not put there, and a module of
flatpak-builder's would have placed it only in the installed app, never
under -r. cfg.flatpak.share lists files and directories of the project
and the path under /app/share each goes to. The writer copies them into
files/share with the desktop entry, metainfo and icons, keeps them while
they are listed and removes them once they are not, as it does those. -r
runs the program from files/bin, so the same ../share lookup finds them
there. What is exported of them is flatpak build-finish's to decide, by
its own rules, as for any app's files: the app's D-Bus services, MIME
types, search providers and KRunner plugins, which have no field of
their own, are placed like any other file. A library's are not passed on
to the programs built on it, since data a library needs at run time
already goes through cfg.files. A dependency that packages its own
executable has the sources rebased by the loader, as its cfg.files are.
Share entries change files/share and not what is compiled, so they are
not keyed into VariantId, which its test checks.

Every file bound for files/share, the desktop entry, metainfo and icons
included, now goes through one placement check, which refuses two files
for one path, a field's file among them, and a file where another needs
a directory, naming both. A target that is absolute or leaves share/ is
refused too. Any other place is open to a share entry, a second desktop
entry, an add-on's metainfo and any icon included, as for any app's
files: flatpak build-export refuses an exported icon that is not a
square image and a D-Bus service that is not a key file, warns of the
app's own desktop entry where desktop-file-validate rejects it or its
Icon= is not the app's, and exports a second desktop entry, a MIME
package or metainfo as it is. The app's own desktop entry,
applications/<app-id>.desktop, is held to cfg.flatpak.desktopFile's
checks whichever placed it: its Icon= and desktop-file-validate are
keyed to that place now, not to the field. A source directory that holds
what the build writes is refused as well, since it would be copied into
itself, growing each time the app is built or exported: files/, and the
app's directory, where install and bundle put another copy of files/ and
the exported app, compared as written and where links lead, so that the
project's own directory is refused where its bin/ is a link elsewhere
too. So is one that lies in them, the build's own output, which the next
build, install or bundle writes again. The loader leaves an empty source
empty, as it does an empty desktop file.

FlatpakShareLarge holds the writer to comparing an entry with its copy
at constant memory, which the program's data, gigabytes of it in a game,
needs on every build: it rewrites a files/ holding a 32 MiB entry, with
nothing changed, in a child process whose address space may grow by 16
MiB, with a stand-in llvm-readelf, since the real one, which a write
starts for the libraries, would not fit under that limit, and a change
in the entry's last byte, in the short block a comparison reads last, is
still copied.
2026-11-09 01:55:31 +01:00

105 lines
5.7 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;
// --packaging=flatpak packages the executable of the project crafter-build
// was pointed at, and no other. The args reach every sibling project the
// project loads (a GitProject's or LocalProject's args default to them),
// and a sibling's executable is run by path by its consumers, as
// Crafter.Debug's client is: packaged, it would move into files/bin and
// out of their reach. A sibling that packages its own executable in its
// own project.cpp gets a Flatpak, with the files it is installed with
// found in its own directory, as its cfg.files are, wherever the consumer
// builds it from: before the loader rebased them, they resolved against the
// consumer's directory, so it shipped the consumer's file of that name or
// failed for want of one. The fixture is an app with two executable
// siblings loaded with its args, of which the second packages itself.
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>());
}
}
int main() {
const fs::path scratch = fs::temp_directory_path() / "crafter-build-flatpak-dependency-executable";
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);
const fs::path installedHome = GetCrafterBuildHome();
if (!fs::exists(installedHome / "Crafter.Build.cppm")) {
std::println(std::cerr, "SKIP: no module sources at {}", installedHome.string());
return 77;
}
// Staged because LoadProject drops a build/ tree next to every project
// file it compiles.
const fs::path fixture = fs::current_path() / "tests" / "FlatpakDependencyExecutable" / "fixture";
fs::copy(fixture, scratch, fs::copy_options::recursive);
// From the project's own directory, as crafter-build runs it: the
// fixture's LocalProject paths are relative to the working directory.
const fs::path cwd = fs::current_path();
fs::current_path(scratch / "app");
try {
std::array<std::string_view, 1> args = { "--packaging=flatpak" };
const Configuration cfg = LoadProject("project.cpp", args);
Check(cfg.packaging == Packaging::Flatpak, "the executable of the project crafter-build was pointed at is packaged");
Check(cfg.dependencies.size() == 2, std::format("the app declared its siblings (got {})", cfg.dependencies.size()));
if (cfg.dependencies.size() == 2) {
const Configuration& tool = *cfg.dependencies[0];
Check(tool.name == "tool", std::format("the first sibling is the fixture's tool (got '{}')", tool.name));
Check(tool.packaging == Packaging::Default && tool.ResolvedPackaging() == Packaging::None, "the sibling's executable stays as linked");
Check(tool.ProgramDir() == tool.BinDir(), "and lands in its bin dir, where its consumers look");
Check(tool.projectArgs.empty(), std::format("the sibling took --packaging= as a standard arg, not one of its own (projectArgs {})", tool.projectArgs));
// Built from the consumer's directory, as the consumer's build
// builds it.
Configuration& packaged = *cfg.dependencies[1];
Check(packaged.packaging == Packaging::Flatpak, "the sibling that packages itself is a Flatpak");
// The loader takes a project file where its links lead, as
// fs::canonical does, whatever the temp directory goes through.
const std::string module = std::format("\"{}\"", (fs::canonical(scratch) / "packaged" / "packaging" / "extra.json").string());
Check(FlatpakManifest(packaged).contains(module), std::format("its manifest names its own module file, {}", module));
if (NormalizeTarget(GetToolchain().hostTriple) == "x86_64-linux-gnu" && GetToolchain().libcxxDir.empty()) {
std::unordered_map<fs::path, std::shared_future<BuildResult>> depResults;
std::mutex depMutex;
const BuildResult built = Build(packaged, depResults, depMutex);
Check(built.result.empty(), std::format("it builds from the consumer's directory: {}", built.result));
const fs::path share = packaged.BinDir() / "files" / "share";
const fs::path own = scratch / "packaged" / "packaging";
Check(ReadFile(share / "metainfo" / "net.example.Packaged.metainfo.xml") == ReadFile(own / "packaged.metainfo.xml"), "its metainfo is its own");
Check(ReadFile(share / "icons" / "hicolor" / "scalable" / "apps" / "net.example.Packaged.svg") == ReadFile(own / "icon.svg"), "and so is its icon");
Check(ReadFile(share / "packaged" / "note.txt") == ReadFile(scratch / "packaged" / "data" / "note.txt"), "and so is the data it installs under /app/share");
}
}
} catch (const std::exception& e) {
Check(false, std::format("LoadProject threw: {}", e.what()));
}
fs::current_path(cwd);
fs::remove_all(scratch, ec);
if (Failures > 0) {
std::println(std::cerr, "{} assertions failed", Failures);
return 1;
}
return 0;
}