// SPDX-License-Identifier: LGPL-3.0-only // SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® #include 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; } } BuildResult BuildIt(Configuration& cfg) { std::unordered_map> depResults; std::mutex depMutex; return Build(cfg, depResults, depMutex); } struct Refusal { std::string_view what; std::string target; std::string sysroot; std::string_view reason; }; } // An executable asking to be a Flatpak where it cannot be one is refused // before anything is built, saying why: a Windows executable and a wasm // build have outputs of their own, a build against a sysroot links that // sysroot's libraries and not the runtime's, one for musl links a C library // no runtime provides, and another architecture than the runtime's has no // libc++ crafter-build could bundle. Each would otherwise be an app built // against something other than what it runs with. With Default or None the // same configurations keep the output they have, and a library asked to be // a Flatpak is built as the library it is. int main() { const fs::path scratch = fs::temp_directory_path() / "crafter-build-flatpak-refusals"; 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" / "FlatpakRefusals" / "fixture"; fs::remove_all(fixture / "bin", ec); fs::remove_all(fixture / "build", ec); const std::array refusals = {{ {"a Windows executable", "x86_64-w64-mingw32", "", "a Windows executable is not packaged as a Flatpak"}, {"a wasm build", "wasm32-wasip1", "", "a wasm build is not packaged as a Flatpak"}, {"a build against a sysroot", HostTarget(), (scratch / "sysroot").string(), "links that sysroot's libraries"}, {"a musl build", "x86_64-alpine-linux-musl", "", "links musl"}, {"another architecture", "aarch64-linux-gnu", "", "the libc++ it would bundle, are x86_64"}, }}; for (const Refusal& refusal : refusals) { Configuration app; app.path = fixture; app.name = "refused-app"; app.outputName = "refused"; app.target = refusal.target; app.sysroot = refusal.sysroot; app.packaging = Packaging::Flatpak; std::array ifaces = {}; std::array impls = { "main" }; app.GetInterfacesAndImplementations(ifaces, impls); const BuildResult r = BuildIt(app); Check(r.result.contains(refusal.reason) && r.result.contains(app.name), std::format("{} is refused as a Flatpak, saying why and naming the configuration: {}", refusal.what, r.result)); Check(!fs::exists(app.BinDir()) && !fs::exists(app.BuildDir()), std::format("refusing {} leaves nothing behind", refusal.what)); for (const Packaging packaging : {Packaging::Default, Packaging::None}) { app.packaging = packaging; Check(app.ResolvedPackaging() == Packaging::None && app.ProgramDir() == app.BinDir(), std::format("{} keeps its own output with Default and None", refusal.what)); } } Configuration lib; lib.path = fixture / "lib"; lib.name = "refused-lib"; lib.outputName = "refused"; lib.target = HostTarget(); lib.type = ConfigurationType::LibraryStatic; lib.packaging = Packaging::Flatpak; { std::array ifaces = { "Refused" }; std::array impls = {}; lib.GetInterfacesAndImplementations(ifaces, impls); } Check(lib.ResolvedPackaging() == Packaging::None, "a library is never packaged"); const BuildResult built = BuildIt(lib); Check(built.result.empty(), std::format("a library asked to be a Flatpak builds as a library: {}", built.result)); Check(fs::exists(ExpectedOutput(lib)) && !fs::exists(lib.BinDir() / "files"), "it is an archive, with no files/ beside it"); fs::remove_all(scratch, ec); if (Failures > 0) { std::println(std::cerr, "{} assertions failed", Failures); return 1; } return 0; }