118 lines
5.7 KiB
C++
118 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;
|
||
|
|
|
||
|
|
namespace {
|
||
|
|
std::int32_t Failures = 0;
|
||
|
|
|
||
|
|
void Check(bool cond, std::string_view msg) {
|
||
|
|
if (!cond) {
|
||
|
|
std::println(std::cerr, "FAIL: {}", msg);
|
||
|
|
++Failures;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// With cfg.flatpak.modules, flatpak-builder makes the app from a manifest
|
||
|
|
// crafter-build writes: the app-id, the runtime at crafter-build's branch,
|
||
|
|
// the command and the finish-args, from the fields an app without modules
|
||
|
|
// is finished with; translations kept in the app, where a bundle carries
|
||
|
|
// them, rather than split off into a .Locale extension; the project's
|
||
|
|
// module files, by absolute path; and last a module that copies the
|
||
|
|
// build's files/, so the program crafter-build linked is what ships and
|
||
|
|
// nothing is compiled twice. The manifest is checked everywhere; where the host has a
|
||
|
|
// flatpak-builder program, the app it builds holds what the extra module
|
||
|
|
// installs as well as the program.
|
||
|
|
int main() {
|
||
|
|
const fs::path scratch = fs::temp_directory_path() / "crafter-build-flatpak-modules";
|
||
|
|
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" / "FlatpakModules" / "fixture";
|
||
|
|
fs::remove_all(fixture / "bin", ec);
|
||
|
|
fs::remove_all(fixture / "build", ec);
|
||
|
|
Configuration app;
|
||
|
|
app.path = fixture;
|
||
|
|
app.name = "modules-app";
|
||
|
|
app.outputName = "modules";
|
||
|
|
app.target = HostTarget();
|
||
|
|
app.packaging = Packaging::Flatpak;
|
||
|
|
app.flatpak.appId = "net.example.Modules";
|
||
|
|
app.flatpak.permissions = { "--share=network" };
|
||
|
|
app.flatpak.modules = { fixture / "extra.json" };
|
||
|
|
{
|
||
|
|
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 std::string manifest = FlatpakManifest(app);
|
||
|
|
for (const std::string& part : {
|
||
|
|
std::string("\"app-id\": \"net.example.Modules\""),
|
||
|
|
std::string("\"runtime\": \"org.freedesktop.Platform\""),
|
||
|
|
std::string("\"runtime-version\": \"26.08\""),
|
||
|
|
std::string("\"sdk\": \"org.freedesktop.Sdk\""),
|
||
|
|
std::string("\"command\": \"modules\""),
|
||
|
|
std::string("\"separate-locales\": false"),
|
||
|
|
std::string("\"--share=network\""),
|
||
|
|
std::format("\"{}\"", (fixture / "extra.json").string()),
|
||
|
|
std::string("\"cp -a . /app/\""),
|
||
|
|
std::format("\"{}\"", fs::absolute(app.BinDir() / "files").lexically_normal().string()),
|
||
|
|
}) {
|
||
|
|
Check(manifest.contains(part), std::format("the manifest has {}:\n{}", part, manifest));
|
||
|
|
}
|
||
|
|
Check(manifest.find("extra.json") < manifest.find("cp -a . /app/"), "the project's modules come before the program's");
|
||
|
|
|
||
|
|
// Only with a flatpak-builder program on the host. Where there is none,
|
||
|
|
// crafter-build runs the org.flatpak.Builder app instead, and `flatpak
|
||
|
|
// run` writes ~/.var/app/org.flatpak.Builder in the real home, which a
|
||
|
|
// test must not touch. Inside crafter-build's Flatpak this looks in the
|
||
|
|
// sandbox, which has none, so that half is left to the native run. And
|
||
|
|
// only where the host has the Platform and the Sdk of the branch, which
|
||
|
|
// flatpak-builder makes the app with: without them it fails, which says
|
||
|
|
// nothing about the manifest.
|
||
|
|
const std::string why = HostFlatpakUnavailable(fixture);
|
||
|
|
const bool builder = why.empty() && std::system("command -v flatpak-builder > /dev/null 2>&1") == 0;
|
||
|
|
const bool runtimes = builder && RunHostFlatpak("info --show-ref org.freedesktop.Platform//26.08") == 0 && RunHostFlatpak("info --show-ref org.freedesktop.Sdk//26.08") == 0;
|
||
|
|
if (runtimes) {
|
||
|
|
const std::string bundled = BundleFlatpakApp(app);
|
||
|
|
Check(bundled.empty(), std::format("flatpak-builder makes the app: {}", bundled));
|
||
|
|
const fs::path built = FlatpakAppDir(app) / "build-dir" / "files";
|
||
|
|
Check(fs::is_regular_file(built / "share" / "extra" / "extra.txt"), "the app holds what the extra module installs");
|
||
|
|
Check(fs::is_regular_file(built / "bin" / "modules") && fs::is_regular_file(built / "lib" / "libc++.so.1"), "and the program with its libraries");
|
||
|
|
} else {
|
||
|
|
std::println("only the manifest is checked: {}", !why.empty() ? why : !builder ? "the host has no flatpak-builder program, and the org.flatpak.Builder app would be run with flatpak run" : "the host lacks org.freedesktop.Platform//26.08 or org.freedesktop.Sdk//26.08");
|
||
|
|
}
|
||
|
|
|
||
|
|
fs::remove_all(scratch, ec);
|
||
|
|
if (Failures > 0) {
|
||
|
|
std::println(std::cerr, "{} assertions failed", Failures);
|
||
|
|
return 1;
|
||
|
|
}
|
||
|
|
return 0;
|
||
|
|
}
|