diff --git a/project.cpp b/project.cpp index 5228149..b2e30ec 100644 --- a/project.cpp +++ b/project.cpp @@ -245,6 +245,7 @@ extern "C" Configuration CrafterBuildProject(std::span a cfg.AddTest("StaticLib").Dependencies({ CrafterBuildLib.get() }); cfg.AddTest("ModuleInterface").Dependencies({ CrafterBuildLib.get() }); cfg.AddTest("DependencyLink").Dependencies({ CrafterBuildLib.get() }); + cfg.AddTest("SharedLibraryRuns").Dependencies({ CrafterBuildLib.get() }); cfg.AddTest("IncrementalInterfaceChange").Dependencies({ CrafterBuildLib.get() }); cfg.AddTest("TransitiveInterfaceChange").Dependencies({ CrafterBuildLib.get() }); cfg.AddTest("IncrementalHeaderChange").Dependencies({ CrafterBuildLib.get() }); diff --git a/tests/SharedLibraryRuns/fixture/greet/Greet.cppm b/tests/SharedLibraryRuns/fixture/greet/Greet.cppm new file mode 100644 index 0000000..805f199 --- /dev/null +++ b/tests/SharedLibraryRuns/fixture/greet/Greet.cppm @@ -0,0 +1,9 @@ +// SPDX-License-Identifier: LGPL-3.0-only +// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® + +export module Greet; +import std; + +export std::string Greeting() { + return "hello from a shared library"; +} diff --git a/tests/SharedLibraryRuns/fixture/main.cpp b/tests/SharedLibraryRuns/fixture/main.cpp new file mode 100644 index 0000000..643e2e2 --- /dev/null +++ b/tests/SharedLibraryRuns/fixture/main.cpp @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: LGPL-3.0-only +// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts® + +import std; +import Greet; + +int main() { + std::println("{}", Greeting()); + return 0; +} diff --git a/tests/SharedLibraryRuns/main.cpp b/tests/SharedLibraryRuns/main.cpp new file mode 100644 index 0000000..380dc9d --- /dev/null +++ b/tests/SharedLibraryRuns/main.cpp @@ -0,0 +1,118 @@ +// 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; + +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, through the run path the link records for that directory. +// Before, the link took the library and recorded nowhere to find it, so the +// program failed at start with "cannot open shared object file". +int main() { + std::error_code ec; + 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("the program starts (exit {}): {}", raw.exitCode, raw.output)); + + if (Failures > 0) { + std::println(std::cerr, "{} assertions failed", Failures); + return 1; + } + return 0; +}