// 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; // --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(in), std::istreambuf_iterator()); } } 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 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> 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"); } } } 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; }