test: a pragma dependency built on a shared CMake external loads
PragmaSharedExternal loads a project file whose #pragma crafter dependency is a library built on a CMake external that makes a shared library, and calls into that library while project.cpp loads. LoadProject links project.so against what each pragma dependency's build reports, and project.so loads only when that includes where the shared library is. It passes as the tree stands, and holds project.so to recording where a pragma dependency's shared CMake external is: without that run path, LoadProject fails with "libsharedext.so: cannot open shared object file".
This commit is contained in:
parent
5a63dd81b8
commit
06ee1da796
5 changed files with 154 additions and 0 deletions
|
|
@ -295,6 +295,8 @@ extern "C" Configuration CrafterBuildProject(std::span<const std::string_view> 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() })
|
||||
|
|
|
|||
19
tests/PragmaSharedExternal/fixture/app/project.cpp
Normal file
19
tests/PragmaSharedExternal/fixture/app/project.cpp
Normal file
|
|
@ -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<const std::string_view>) {
|
||||
Configuration cfg;
|
||||
cfg.path = "./";
|
||||
cfg.name = std::format("value {}", ToolValue());
|
||||
cfg.outputName = "app";
|
||||
cfg.type = ConfigurationType::Executable;
|
||||
return cfg;
|
||||
}
|
||||
12
tests/PragmaSharedExternal/fixture/tool/interfaces/Tool.cppm
Normal file
12
tests/PragmaSharedExternal/fixture/tool/interfaces/Tool.cppm
Normal file
|
|
@ -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();
|
||||
}
|
||||
30
tests/PragmaSharedExternal/fixture/tool/project.cpp
Normal file
30
tests/PragmaSharedExternal/fixture/tool/project.cpp
Normal file
|
|
@ -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<const std::string_view> args) {
|
||||
Configuration cfg;
|
||||
cfg.path = "./";
|
||||
cfg.name = "tool";
|
||||
cfg.outputName = "tool";
|
||||
cfg.type = ConfigurationType::LibraryStatic;
|
||||
ApplyStandardArgs(cfg, args);
|
||||
std::array<fs::path, 1> ifaces = { "interfaces/Tool" };
|
||||
std::array<fs::path, 0> 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;
|
||||
}
|
||||
91
tests/PragmaSharedExternal/main.cpp
Normal file
91
tests/PragmaSharedExternal/main.cpp
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
// SPDX-License-Identifier: LGPL-3.0-only
|
||||
// SPDX-FileCopyrightText: Copyright (C) 2026 Catcrafts®
|
||||
|
||||
#include <stdlib.h>
|
||||
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<std::string_view, 0> 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;
|
||||
}
|
||||
Loading…
Reference in a new issue