// 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; } } // The NEEDED entries of an ELF file, by the toolchain's llvm-readelf. std::set Needed(const fs::path& readelf, const fs::path& file) { const auto r = RunCommandWithTimeout(std::format("{} --needed-libs {}", ShellQuote(readelf.string()), ShellQuote(file.string())), std::chrono::seconds(30)); std::set names; bool inList = false; std::istringstream lines(r.output); for (std::string line; std::getline(lines, line);) { const std::size_t first = line.find_first_not_of(" \t"); const std::string t = first == std::string::npos ? std::string() : line.substr(first); if (!inList) { inList = t.starts_with("NeededLibraries ["); continue; } if (t == "]") break; if (!t.empty()) names.insert(t); } return names; } } // A program packaged as a Flatpak carries what its link takes from the // toolchain's own lib dir, which the Flatpak link searches with -L. In // crafter-build's Flatpak that is /app/lib, which holds libclang, libLLVM, // liblldb and libedit beside the LLVM runtime libraries, none of them a // runtime's. The writer used to look only in the directories of the shared // libraries the program is built on and took any other name for the // runtime's, so a program linked against one of those started under -r, // where crafter-build's own /app/lib is on the loader's path, and failed // once installed. The fixture links libedit. Skips where the toolchain is in // /usr, whose lib dir is the system's, and where its lib dir has no libedit. int main() { const fs::path scratch = fs::temp_directory_path() / "crafter-build-flatpak-toolchain-library"; 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 Toolchain& tc = GetToolchain(); if (NormalizeTarget(tc.hostTriple) != "x86_64-linux-gnu") { std::println(std::cerr, "SKIP: the staged runtime is x86_64 and the host is {}", tc.hostTriple); return 77; } if (!tc.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 toolchainLib = tc.LibcxxLibDir(); if (toolchainLib.empty()) { std::println(std::cerr, "SKIP: the toolchain is in /usr, whose libraries are the system's"); return 77; } if (!fs::exists(toolchainLib / "libedit.so")) { std::println(std::cerr, "SKIP: the toolchain's lib dir, {}, has no libedit.so", toolchainLib.string()); return 77; } const fs::path fixture = fs::current_path() / "tests" / "FlatpakToolchainLibrary" / "fixture"; fs::remove_all(fixture / "bin", ec); fs::remove_all(fixture / "build", ec); Configuration app; app.path = fixture; app.name = "toolchain-library-app"; app.outputName = "edit"; app.target = HostTarget(); app.packaging = Packaging::Flatpak; app.flatpak.appId = "net.example.ToolchainLibrary"; app.linkFlags = { "-ledit" }; { std::array ifaces = {}; std::array impls = { "main" }; app.GetInterfacesAndImplementations(ifaces, impls); } { std::unordered_map> 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 fs::path lib = app.BinDir() / "files" / "lib"; const std::set needed = Needed(tc.llvmReadelf, ExpectedOutput(app)); const auto edit = std::ranges::find_if(needed, [](const std::string& name) { return name.starts_with("libedit.so"); }); Check(edit != needed.end(), std::format("the program needs libedit (NEEDED: {})", needed)); if (edit != needed.end()) { Check(fs::exists(toolchainLib / *edit), std::format("{} is the toolchain's, in {}", *edit, toolchainLib.string())); Check(fs::is_regular_file(lib / *edit), std::format("{} is carried in files/lib", *edit)); } // Whatever the program and the libraries it carries need from the // toolchain's lib dir is carried too. std::set all = needed; for (const fs::directory_entry& entry : fs::directory_iterator(lib)) all.merge(Needed(tc.llvmReadelf, entry.path())); for (const std::string& name : all) { if (fs::exists(toolchainLib / name)) Check(fs::exists(lib / name), std::format("{}, from the toolchain's lib dir, is carried in files/lib", name)); } fs::remove_all(scratch, ec); if (Failures > 0) { std::println(std::cerr, "{} assertions failed", Failures); return 1; } return 0; }