An executable packaged as a Flatpak came out as the files/ of one, which
only flatpak build-finish makes an app of, and getting it installed or
onto another machine took flatpak commands on the host, typed by hand.
Two verbs do it where the host's flatpak can be reached:
`crafter-build install` builds the project and installs its app for this
user, and `crafter-build bundle` builds it into bin/<variant>.flatpak, a
single file for `flatpak install --user` that names Flathub's repository
for the runtime. Inside crafter-build's Flatpak they need the
org.freedesktop.Flatpak permission, which its manifest does not grant in
this commit. Both put the app into the project's repo first, in
build/flatpak-apps/<app-id>/ (FlatpakAppDir), one per app rather than
per variant, since flatpak-builder's state there is its cache. Every
variant exports into it, so the export and the install or bundle made
from it hold a lock there, and another crafter-build exporting meanwhile
waits rather than leave this one installing or bundling its program. The
user remote an install re-points is every checkout's that builds the
app-id, so re-pointing it and installing from it hold a second lock,
which every crafter-build installing there takes, natively or in the
Flatpak, whatever its XDG_CACHE_HOME: in
~/.cache/crafter.build/flatpak-remotes/, found by HOME, for the user's
own installation, which flatpak hides from the sandbox behind a tmpfs of
the sandbox's own, and in the directory of a private one,
FLATPAK_USER_DIR, one lock for all of them where the sandbox shares that
directory with the host. Without modules, the host's flatpak
build-finish finishes build/flatpak-apps/<app-id>/build-dir, made afresh
each time from a metadata naming the app, its runtime and the Sdk and a
tree of hard links to the build's files/, copies where a link cannot be
made, across filesystems or on one without links, vfat or exFAT: it
writes the command, the sandbox's permissions and export/ by flatpak's
own rules, those of whichever flatpak the host runs, and refuses a
permission it does not know, and flatpak build-export puts the result
into the repo. When cfg.flatpak.modules names flatpak-builder module
files for what the app needs in /app besides the program,
flatpak-builder does both, from a manifest that lists them and copies
the built program in last, so nothing is compiled twice. That manifest
keeps share/locale in the app ("separate-locales": false):
flatpak-builder would split it into a .Locale extension by default,
which a bundle, holding the app alone, leaves out. The loader rebases a
dependency's module files as it does its cfg.files. install uses a user
remote named after the app-id, re-pointed at the repo each time, and
says how to remove both. The app's tree in
build/flatpak-apps/<app-id>/build-dir, where flatpak-builder installs a
module's files too, is left out of the include walk with every other
build/, so the headers a module installed reach no compile.
cfg.flatpak gets the runtime, the Platform or the Sdk at the branch
crafter-build runs on, and the sandbox's permissions, finish-args as
Flatpak spells them, which any configuration may declare: an executable
gets its dependencies' first and its own last, so it can take one back.
The build reads neither, and a change to either links nothing. install
and bundle print the permissions, with where each came from, and hand
them to build-finish, the one judge of what they mean, so a refusal can
be traced to the configuration that declared it. FlatpakPermissions
checks the order everywhere, and that a change to one neither moves the
build to another directory nor links anything, and, where the host's
flatpak runs, what build-finish makes of them and that it refuses a
socket it does not have; FlatpakBundle that build-finish exports the
desktop entry and the icon, and that the build's own files/ is left as
it was; VariantId's test has the runtime, permissions and modules move
no directory.
The host's flatpak and flatpak-builder are reached only through
HostCommand in :Platform, which builds the command lines of
RunHostFlatpak, RunHostFlatpakBuilder and HostFlatpakUnavailable's
probes: flatpak-spawn --host --watch-bus inside crafter-build's Flatpak,
started in the host's / and passing FLATPAK_USER_DIR on, and the command
itself natively. flatpak-spawn would otherwise start a command in
crafter-build's own working directory, which the host need not have, and
from /app, say, every host command would fail. HostFlatpakUnavailable
says why the host cannot be reached: no org.freedesktop.Flatpak
permission, no session bus (CI's build sandbox has neither), a host that
does not see the project's bin/ and build/ where crafter-build does, as
it does not in the sandbox's own /tmp, /var, /usr, /etc and /app or
where a link leads into one, or no flatpak natively. That, and an
executable that is not packaged as a Flatpak, install and bundle refuse
as soon as the project is loaded, before anything is built;
FlatpakVerbRefusal shows the second. The host is asked rather than the
sandbox's paths modelled, which an override sharing a directory inside
the sandbox's own would defeat: its flatpak is started in a directory
just made in each of bin/ and build/ under a name no other has, which it
enters only where it sees that very directory, and the directory is
removed again. --help gets a section on both, and its Clean section says
the repo an install points at goes with build/. FlatpakModules runs
flatpak-builder only with a flatpak-builder program on the host,
crafter-build falling back to the org.flatpak.Builder app, whose
`flatpak run` would write ~/.var/app in the real home, and with the
Platform and Sdk it builds the app with. The library's install function
is InstallFlatpakApp because project.cpp files define an InstallFlatpak
script of their own under `using namespace Crafter`, this repository's
included, and a library InstallFlatpak would make the name an overload
set, which the scripts table's std::function cannot be built from;
BundleFlatpakApp is named to match. JsonQuote writes the manifest's
strings, and variants.json's, which escaped quotes and backslashes only.
FlatpakExport, FlatpakManifestText, ExportFlatpak and FlatpakPermission,
which only :Clang uses, stay out of the API a project.cpp compiles
against, as RecordFlatpakPlaced does, so their names cannot meet a
project's own under `using namespace Crafter`; ExportFlatpak's comment
names the app's lock, which its callers hold, beside
HostFlatpakUnavailable.
135 lines
6.9 KiB
C++
135 lines
6.9 KiB
C++
// SPDX-License-Identifier: LGPL-3.0-only
|
|
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
|
|
|
#include <fcntl.h>
|
|
#include <stdlib.h>
|
|
#include <sys/file.h>
|
|
#include <unistd.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>());
|
|
}
|
|
|
|
// Every file under `root` with its size and mtime.
|
|
std::map<fs::path, std::pair<std::uintmax_t, fs::file_time_type>> Snapshot(const fs::path& root) {
|
|
std::map<fs::path, std::pair<std::uintmax_t, fs::file_time_type>> files;
|
|
for (const fs::directory_entry& entry : fs::recursive_directory_iterator(root)) {
|
|
if (entry.is_regular_file()) files.emplace(entry.path(), std::pair{entry.file_size(), entry.last_write_time()});
|
|
}
|
|
return files;
|
|
}
|
|
}
|
|
|
|
// `crafter-build bundle` turns the Flatpak a project was built as into one
|
|
// file that `flatpak install --user` takes on another machine: the host's
|
|
// flatpak build-finish finishes the files/ the build laid out, linked into
|
|
// a build directory of the app's, flatpak build-export puts that into the
|
|
// project's repo, and the bundle is written from there, next to the bin
|
|
// dir under the same variant name. build-finish exports the desktop entry
|
|
// and the icon files/share holds, and the build's own files/ is left as it
|
|
// was, linked rather than copied. Bundling again with nothing changed works
|
|
// as the first time did, and a bundle waits while another export holds the
|
|
// repo, which every variant of the app shares. It needs the host's flatpak
|
|
// and skips without it, which is the case in flatpak-builder's build
|
|
// sandbox, where CI runs the suite.
|
|
int main() {
|
|
const fs::path scratch = fs::temp_directory_path() / "crafter-build-flatpak-bundle";
|
|
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 fixture = fs::current_path() / "tests" / "FlatpakBundle" / "fixture";
|
|
if (const std::string why = HostFlatpakUnavailable(fixture); !why.empty()) {
|
|
std::println(std::cerr, "SKIP: {}", why);
|
|
return 77;
|
|
}
|
|
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;
|
|
}
|
|
fs::remove_all(fixture / "bin", ec);
|
|
fs::remove_all(fixture / "build", ec);
|
|
|
|
Configuration app;
|
|
app.path = fixture;
|
|
app.name = "bundle-app";
|
|
app.outputName = "bundled";
|
|
app.target = HostTarget();
|
|
app.packaging = Packaging::Flatpak;
|
|
app.flatpak.appId = "net.example.Bundled";
|
|
std::ofstream(scratch / "app.desktop") << "[Desktop Entry]\nType=Application\nName=Bundled\nExec=bundled\nIcon=net.example.Bundled\nCategories=Utility;\n";
|
|
std::ofstream(scratch / "icon.svg") << "<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"16\" height=\"16\"><rect width=\"16\" height=\"16\"/></svg>\n";
|
|
app.flatpak.desktopFile = scratch / "app.desktop";
|
|
app.flatpak.icons = { scratch / "icon.svg" };
|
|
{
|
|
std::array<fs::path, 0> ifaces = {};
|
|
std::array<fs::path, 1> impls = { "main" };
|
|
app.GetInterfacesAndImplementations(ifaces, impls);
|
|
}
|
|
{
|
|
std::unordered_map<fs::path, std::shared_future<BuildResult>> depResults;
|
|
std::mutex depMutex;
|
|
if (const BuildResult r = Build(app, depResults, depMutex); !r.result.empty()) {
|
|
std::println(std::cerr, "FAIL: build failed: {}", r.result);
|
|
return 1;
|
|
}
|
|
}
|
|
|
|
const auto built = Snapshot(app.BinDir() / "files");
|
|
const std::string first = BundleFlatpakApp(app);
|
|
Check(first.empty(), std::format("the bundle is written: {}", first));
|
|
const fs::path finished = FlatpakAppDir(app) / "build-dir";
|
|
const fs::path exported = finished / "export" / "share";
|
|
Check(fs::exists(exported / "applications" / "net.example.Bundled.desktop") && fs::exists(exported / "icons" / "hicolor" / "scalable" / "apps" / "net.example.Bundled.svg"), "build-finish exports the desktop entry and the icon");
|
|
Check(ReadFile(finished / "metadata").contains("\ncommand=bundled\n"), std::format("and makes the program the app's command:\n{}", ReadFile(finished / "metadata")));
|
|
Check(fs::equivalent(finished / "files" / "bin" / "bundled", ExpectedOutput(app), ec), "files/ is linked into the build directory, not copied");
|
|
Check(Snapshot(app.BinDir() / "files") == built && !fs::exists(app.BinDir() / "metadata") && !fs::exists(app.BinDir() / "export"), "and the build's own files/ is left as it was");
|
|
const fs::path bundle = FlatpakBundlePath(app);
|
|
Check(bundle.parent_path() == app.BinDir().parent_path() && bundle.filename() == std::format("{}.flatpak", app.VariantId()), std::format("it is named after the variant, next to its bin dir: {}", bundle.string()));
|
|
Check(fs::is_regular_file(bundle) && fs::file_size(bundle, ec) > 0, "the bundle exists");
|
|
Check(fs::exists(FlatpakAppDir(app) / "repo" / "config"), "the app went into the project's repo");
|
|
const std::string second = BundleFlatpakApp(app);
|
|
Check(second.empty(), std::format("bundling again with nothing changed works: {}", second));
|
|
|
|
// While another export holds the app's repo, every variant's being one,
|
|
// a bundle waits for it rather than bundle what that one exported, and
|
|
// is written once it is free. Held here as another crafter-build would.
|
|
{
|
|
const std::int32_t held = open((FlatpakAppDir(app) / ".lock").c_str(), O_RDWR | O_CREAT | O_CLOEXEC, 0644);
|
|
Check(held >= 0 && flock(held, LOCK_EX) == 0, "the test holds the app's repo");
|
|
std::future<std::string> third = std::async(std::launch::async, [&app] { return BundleFlatpakApp(app); });
|
|
Check(third.wait_for(std::chrono::seconds(5)) == std::future_status::timeout, "a bundle waits while another export holds the app's repo");
|
|
if (held >= 0) close(held);
|
|
const std::string waited = third.get();
|
|
Check(waited.empty(), std::format("and is written once the repo is free: {}", waited));
|
|
}
|
|
|
|
fs::remove_all(scratch, ec);
|
|
if (Failures > 0) {
|
|
std::println(std::cerr, "{} assertions failed", Failures);
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|