// 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; } } std::string ReadFile(const fs::path& path) { std::ifstream in(path, std::ios::binary); return std::string(std::istreambuf_iterator(in), std::istreambuf_iterator()); } std::string BuildIt(Configuration& cfg) { std::unordered_map> depResults; std::mutex depMutex; return Build(cfg, depResults, depMutex).result; } // A PNG's signature and IHDR, which is all the size is read from. void WritePng(const fs::path& path, std::uint32_t width, std::uint32_t height) { std::string bytes = "\x89PNG\r\n\x1a\n"; const auto bigEndian = [&](std::uint32_t value) { for (std::int32_t shift = 24; shift >= 0; shift -= 8) bytes.push_back(static_cast((value >> shift) & 0xff)); }; bigEndian(13); bytes += "IHDR"; bigEndian(width); bigEndian(height); bytes += std::string("\x08\x06\x00\x00\x00", 5); std::ofstream(path, std::ios::binary) << bytes; } } // A desktop entry, AppStream metainfo and icons are installed under the // app-id in files/share, where flatpak build-finish finds them to export // to the host: the entry as share/applications/.desktop, the // metainfo as share/metainfo/.metainfo.xml, an .svg as the // scalable icon and a .png in the directory of its own size. What would // make the app fail to install, or show no icon, is refused at build time: // an Icon= other than the app-id, spelled with spaces around '=' or not, a // png that is not square, an icon that is neither format, an entry // desktop-file-validate rejects (where it exists), on the write after a // failed one as well, an app-id flatpak does not accept. A project that // names no app-id gets local.crafterbuild.. int main() { const fs::path scratch = fs::temp_directory_path() / "crafter-build-flatpak-desktop"; 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); Check(DefaultFlatpakAppId("hello") == "local.crafterbuild.hello", "the default app-id"); Check(DefaultFlatpakAppId("my-app") == "local.crafterbuild.my-app", "a dash may stay in the last segment"); Check(DefaultFlatpakAppId("3d.viewer") == "local.crafterbuild._3d_viewer", std::format("a dot and a leading digit are made safe: {}", DefaultFlatpakAppId("3d.viewer"))); Check(FlatpakAppIdError(DefaultFlatpakAppId("3d.viewer")).empty(), "and the result is valid"); Check(FlatpakAppIdError("org.example.My-App").empty(), "a valid app-id"); for (const std::string_view bad : {"org.example", "org.3d.App", "org.my-company.App", "org..App", "org.example.App!", ""}) { Check(!FlatpakAppIdError(bad).empty(), std::format("'{}' is refused", bad)); } Check(!FlatpakAppIdError(std::format("org.example.{}", std::string(250, 'a'))).empty(), "an app-id over 255 characters is refused"); if (NormalizeTarget(GetToolchain().hostTriple) != "x86_64-linux-gnu") { // A skip must never cover for a failure above. if (Failures > 0) return 1; std::println(std::cerr, "SKIP: the staged runtime is x86_64 and the host is {}", GetToolchain().hostTriple); return 77; } if (!GetToolchain().libcxxDir.empty()) { // A skip must never cover for a failure above. if (Failures > 0) return 1; 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" / "FlatpakDesktop" / "fixture"; fs::remove_all(fixture / "bin", ec); fs::remove_all(fixture / "build", ec); WritePng(scratch / "icon-512.png", 512, 512); WritePng(scratch / "wide.png", 512, 256); Configuration app; app.path = fixture; app.name = "desktop-app"; app.outputName = "desktop"; app.target = HostTarget(); app.packaging = Packaging::Flatpak; app.flatpak.appId = "net.example.Desktop"; app.flatpak.desktopFile = fixture / "app.desktop"; app.flatpak.metainfo = fixture / "app.metainfo.xml"; app.flatpak.icons = { fixture / "icon.svg", scratch / "icon-512.png" }; { std::array ifaces = {}; std::array 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 share = root / "files" / "share"; Check(ReadFile(share / "applications" / "net.example.Desktop.desktop") == ReadFile(fixture / "app.desktop"), "the desktop entry is installed under the app-id"); Check(ReadFile(share / "metainfo" / "net.example.Desktop.metainfo.xml") == ReadFile(fixture / "app.metainfo.xml"), "the metainfo is installed under the app-id"); Check(fs::exists(share / "icons" / "hicolor" / "scalable" / "apps" / "net.example.Desktop.svg"), "an .svg is the scalable icon"); Check(fs::exists(share / "icons" / "hicolor" / "512x512" / "apps" / "net.example.Desktop.png"), "a .png goes under its own size"); app.flatpak.desktopFile = fixture / "wrong-icon.desktop"; Check(BuildIt(app).contains("has to be the app-id"), "an Icon= other than the app-id is refused"); // Spelled with spaces around '=', which the Desktop Entry spec allows and // every reader of the entry ignores. std::ofstream(scratch / "spaced-icon.desktop") << "[Desktop Entry]\nType=Application\nName=Desktop\nExec=desktop\nIcon = desktop\n"; app.flatpak.desktopFile = scratch / "spaced-icon.desktop"; Check(BuildIt(app).contains("has to be the app-id"), "so is one spelled 'Icon = desktop'"); app.flatpak.desktopFile = fixture / "app.desktop"; app.flatpak.icons = { scratch / "wide.png" }; Check(BuildIt(app).contains("has to be square"), "a png that is not square is refused"); app.flatpak.icons = { fixture / "app.desktop" }; Check(BuildIt(app).contains("has to be an .svg or a .png"), "an icon that is neither is refused"); app.flatpak.icons = { fixture / "icon.svg" }; if (std::system("command -v desktop-file-validate > /dev/null 2>&1") == 0) { app.flatpak.desktopFile = fixture / "invalid.desktop"; Check(BuildIt(app).contains("does not pass desktop-file-validate"), "an entry desktop-file-validate rejects is refused"); // A write that copies the entry and then fails, here for a metainfo // that does not exist, leaves it in files/share unchanged for the // next write, which copies nothing and has to check it all the same. app.flatpak.metainfo = scratch / "missing.metainfo.xml"; Check(BuildIt(app).contains("missing.metainfo.xml"), "a write that copies the entry and then fails is refused for what failed"); app.flatpak.metainfo = fixture / "app.metainfo.xml"; Check(BuildIt(app).contains("does not pass desktop-file-validate"), "and the next build refuses the entry it left unchecked"); app.flatpak.desktopFile = fixture / "app.desktop"; } app.flatpak.appId = "Desktop"; Check(BuildIt(app).contains("is not a Flatpak application ID"), "an invalid app-id is refused"); fs::remove_all(scratch, ec); if (Failures > 0) { std::println(std::cerr, "{} assertions failed", Failures); return 1; } return 0; }