// 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 BuildIt(Configuration& cfg) { std::unordered_map> depResults; std::mutex depMutex; return Build(cfg, depResults, depMutex).result; } // The fixture's program and library again, built from a directory whose // name holds a comma. clang splits a -Wl, value at every comma, so a run // path passed as -Wl,-rpath, came apart there: the link failed, or // recorded the wrong directory. Why it did not start; empty when it did. std::string FromCommaDirectory(const fs::path& fixture) { const fs::path dir = fs::temp_directory_path() / "crafter-build-shared,library"; std::error_code ec; fs::remove_all(dir, ec); fs::create_directories(dir / "greet"); fs::copy_file(fixture / "main.cpp", dir / "main.cpp"); fs::copy_file(fixture / "greet" / "Greet.cppm", dir / "greet" / "Greet.cppm"); Configuration greet; greet.path = dir / "greet"; greet.name = "greet"; greet.outputName = "greet"; greet.target = HostTarget(); greet.type = ConfigurationType::LibraryDynamic; { std::array ifaces = { "Greet" }; std::array impls = {}; greet.GetInterfacesAndImplementations(ifaces, impls); } Configuration app; app.path = dir; app.name = "greet-app"; app.outputName = "greet-app"; app.target = HostTarget(); app.dependencies = { &greet }; { std::array ifaces = {}; std::array impls = { "main" }; app.GetInterfacesAndImplementations(ifaces, impls); } std::string why = BuildIt(app); if (why.empty()) { const auto run = RunCommandWithTimeout(ShellQuote(ExpectedOutput(app).string()), std::chrono::seconds(30)); if (run.exitCode != 0 || !run.output.contains("hello from a shared library")) why = std::format("it did not start (exit {}): {}", run.exitCode, run.output); } fs::remove_all(dir, ec); return why; } } // An executable built on a LibraryDynamic dependency starts: the loader // finds lib.so, which the executable's link took from the library's // bin dir. Linked as it is, the executable records that directory as a run // path; packaged as a Flatpak, it carries the library in files/lib, where // its run path looks. Before, the link took the library and recorded // nowhere to find it, so the program failed at start with "cannot open // shared object file", raw and packaged alike. int main() { const fs::path scratch = fs::temp_directory_path() / "crafter-build-shared-library-runs"; 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" / "SharedLibraryRuns" / "fixture"; fs::remove_all(fixture / "bin", ec); fs::remove_all(fixture / "build", ec); fs::remove_all(fixture / "greet" / "bin", ec); fs::remove_all(fixture / "greet" / "build", ec); auto greet = std::make_unique(); greet->path = fixture / "greet"; greet->name = "greet"; greet->outputName = "greet"; greet->target = HostTarget(); greet->type = ConfigurationType::LibraryDynamic; { std::array ifaces = { "Greet" }; std::array impls = {}; greet->GetInterfacesAndImplementations(ifaces, impls); } Configuration app; app.path = fixture; app.name = "greet-app"; app.outputName = "greet-app"; app.target = HostTarget(); app.dependencies = { greet.get() }; { 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 std::string comma = FromCommaDirectory(fixture); Check(comma.empty(), std::format("built from a directory whose name holds a comma, it starts: {}", comma)); const auto raw = RunCommandWithTimeout(ShellQuote(ExpectedOutput(app).string()), std::chrono::seconds(30)); Check(raw.exitCode == 0 && raw.output.contains("hello from a shared library"), std::format("linked as it is, it starts (exit {}): {}", raw.exitCode, raw.output)); if (NormalizeTarget(GetToolchain().hostTriple) == "x86_64-linux-gnu" && GetToolchain().libcxxDir.empty()) { app.packaging = Packaging::Flatpak; if (const std::string failed = BuildIt(app); !failed.empty()) { std::println(std::cerr, "FAIL: the Flatpak build failed: {}", failed); return 1; } Check(fs::is_regular_file(app.BinDir() / "files" / "lib" / "libgreet.so"), "the Flatpak carries the library in files/lib"); const auto packaged = RunCommandWithTimeout(ShellQuote(ExpectedOutput(app).string()), std::chrono::seconds(30)); Check(packaged.exitCode == 0 && packaged.output.contains("hello from a shared library"), std::format("packaged as a Flatpak, it starts (exit {}): {}", packaged.exitCode, packaged.output)); } else if (!GetToolchain().libcxxDir.empty()) { std::println("the Flatpak half is left out: CRAFTER_BUILD_LIBCXX_DIR is set, and a build against it is no Flatpak"); } else { std::println("the Flatpak half is left out: the staged runtime is x86_64 and the host is {}", GetToolchain().hostTriple); } fs::remove_all(scratch, ec); if (Failures > 0) { std::println(std::cerr, "{} assertions failed", Failures); return 1; } return 0; }