// 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; // A project file whose `#pragma crafter dependency` is a library built on // a CMake external that makes a shared library loads: project.so links that // library, and records where it is, so that dlopen finds it. LoadProject // links project.so against what each pragma dependency's build reports, and // a CMake external reports its library directory twice, as a -L for the // link and as a run path for the loader. A link of project.so that took the // -L and not the run path still linked, and LoadProject then failed with // "libsharedext.so: cannot open shared object file". // // The external is a local repository with a one-file C library, made here. // Everything is local, and XDG_CACHE_HOME keeps the clone and the host // PCMs out of the real cache. namespace { std::int32_t Failures = 0; void Check(bool cond, std::string_view msg) { if (!cond) { std::println(std::cerr, "FAIL: {}", msg); ++Failures; } } void Git(const fs::path& dir, std::string_view args) { const std::string command = std::format("git -C {} {}", ShellQuote(dir.string()), args); const auto result = RunCommandWithTimeout(command, std::chrono::seconds(60)); if (result.exitCode != 0) throw std::runtime_error(std::format("{} failed: {}", command, result.output)); } void Write(const fs::path& file, std::string_view text) { std::ofstream(file, std::ios::binary | std::ios::trunc) << text; } } int main() { 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; } if (std::system("command -v cmake > /dev/null 2>&1") != 0) { std::println(std::cerr, "SKIP: cmake not on PATH"); return 77; } const fs::path scratch = fs::temp_directory_path() / "crafter-build-pragma-shared-external"; std::error_code ec; fs::remove_all(scratch, ec); fs::create_directories(scratch); fs::copy(fs::current_path() / "tests" / "PragmaSharedExternal" / "fixture", scratch, fs::copy_options::recursive); setenv("XDG_CACHE_HOME", (scratch / "cache").c_str(), 1); setenv("GIT_CONFIG_NOSYSTEM", "1", 1); setenv("GIT_CONFIG_GLOBAL", "/dev/null", 1); for (const char* name : { "GIT_AUTHOR_NAME", "GIT_COMMITTER_NAME" }) setenv(name, "crafter-build test", 1); for (const char* name : { "GIT_AUTHOR_EMAIL", "GIT_COMMITTER_EMAIL" }) setenv(name, "test@example.invalid", 1); const fs::path repo = scratch / "sharedext"; fs::create_directories(repo); Write(repo / "lib.c", "int SharedExtValue(void) { return 42; }\n"); Write(repo / "CMakeLists.txt", "cmake_minimum_required(VERSION 3.16)\n" "project(sharedext C)\n" "add_library(sharedext SHARED lib.c)\n"); Git(repo, "init -q -b main"); Git(repo, "add ."); Git(repo, "commit -q -m sharedext"); setenv("CRAFTER_BUILD_TEST_SHARED_EXTERNAL", repo.c_str(), 1); try { std::array noArgs = {}; const Configuration cfg = LoadProject(scratch / "app" / "project.cpp", noArgs); Check(cfg.name == "value 42", std::format("project.cpp called into the shared library while it loaded (got '{}')", cfg.name)); } catch (const std::exception& e) { Check(false, std::format("LoadProject threw: {}", e.what())); } fs::remove_all(scratch, ec); if (Failures > 0) { std::println(std::cerr, "{} assertions failed", Failures); return 1; } return 0; }