diff --git a/project.cpp b/project.cpp index a1b397d..5228149 100644 --- a/project.cpp +++ b/project.cpp @@ -295,6 +295,8 @@ extern "C" Configuration CrafterBuildProject(std::span a .LinkFlag("-Wl,--export-dynamic").LinkFlag("-ldl"); cfg.AddTest("HostToolingSibling").Dependencies({ CrafterBuildLib.get() }) .LinkFlag("-Wl,--export-dynamic").LinkFlag("-ldl"); + cfg.AddTest("PragmaSharedExternal").Dependencies({ CrafterBuildLib.get() }) + .LinkFlag("-Wl,--export-dynamic").LinkFlag("-ldl"); cfg.AddTest("DuplicateModule").Dependencies({ CrafterBuildLib.get() }) .LinkFlag("-Wl,--export-dynamic").LinkFlag("-ldl"); cfg.AddTest("Script").Dependencies({ CrafterBuildLib.get() }) diff --git a/tests/PragmaSharedExternal/fixture/app/project.cpp b/tests/PragmaSharedExternal/fixture/app/project.cpp new file mode 100644 index 0000000..6e0ebfb --- /dev/null +++ b/tests/PragmaSharedExternal/fixture/app/project.cpp @@ -0,0 +1,19 @@ +// SPDX-License-Identifier: LGPL-3.0-only +// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® + +import std; +import Crafter.Build; +#pragma crafter dependency "../tool" +import Tool; +using namespace Crafter; + +// ToolValue calls into the shared library the tool's CMake external builds, +// so this file's project.so loads only when it can find that library. +extern "C" Configuration CrafterBuildProject(std::span) { + Configuration cfg; + cfg.path = "./"; + cfg.name = std::format("value {}", ToolValue()); + cfg.outputName = "app"; + cfg.type = ConfigurationType::Executable; + return cfg; +} diff --git a/tests/PragmaSharedExternal/fixture/tool/interfaces/Tool.cppm b/tests/PragmaSharedExternal/fixture/tool/interfaces/Tool.cppm new file mode 100644 index 0000000..e3eb7dd --- /dev/null +++ b/tests/PragmaSharedExternal/fixture/tool/interfaces/Tool.cppm @@ -0,0 +1,12 @@ +// SPDX-License-Identifier: LGPL-3.0-only +// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® + +export module Tool; +import std; + +// In libsharedext.so, which the tool's CMake external builds. +extern "C" int SharedExtValue(); + +export std::int32_t ToolValue() { + return SharedExtValue(); +} diff --git a/tests/PragmaSharedExternal/fixture/tool/project.cpp b/tests/PragmaSharedExternal/fixture/tool/project.cpp new file mode 100644 index 0000000..6c1e6e9 --- /dev/null +++ b/tests/PragmaSharedExternal/fixture/tool/project.cpp @@ -0,0 +1,30 @@ +// SPDX-License-Identifier: LGPL-3.0-only +// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® + +import std; +import Crafter.Build; +namespace fs = std::filesystem; +using namespace Crafter; + +// The project the consumer's project.cpp names in its pragma: a library +// built on a CMake external that makes a shared library. The test makes +// that repository and names it in the environment. +extern "C" Configuration CrafterBuildProject(std::span args) { + Configuration cfg; + cfg.path = "./"; + cfg.name = "tool"; + cfg.outputName = "tool"; + cfg.type = ConfigurationType::LibraryStatic; + ApplyStandardArgs(cfg, args); + std::array ifaces = { "interfaces/Tool" }; + std::array impls = {}; + cfg.GetInterfacesAndImplementations(ifaces, impls); + const char* repository = std::getenv("CRAFTER_BUILD_TEST_SHARED_EXTERNAL"); + ExternalDependency ext; + ext.name = "sharedext"; + ext.source.url = std::format("file://{}", repository ? repository : ""); + ext.builder = ExternalBuilder::CMake; + ext.libs = { "sharedext" }; + cfg.externalDependencies.push_back(std::move(ext)); + return cfg; +} diff --git a/tests/PragmaSharedExternal/main.cpp b/tests/PragmaSharedExternal/main.cpp new file mode 100644 index 0000000..db74577 --- /dev/null +++ b/tests/PragmaSharedExternal/main.cpp @@ -0,0 +1,91 @@ +// 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; +}