From dd84aefdcdc20aa04dfd354c3bb756037deecab9 Mon Sep 17 00:00:00 2001 From: NopeCharon <115794254+NopeCharon@users.noreply.github.com> Date: Fri, 20 Nov 2026 04:43:09 +0530 Subject: [PATCH] feat(deps-cmake): build_shared_libs passes BUILD_SHARED_LIBS to the subproject `options::shared` only tells mcpp what the subproject installs -- which library file names to link and whether the prefix's shared-library directory reaches `mcpp run` and `mcpp pack`. It never reached CMake, so a subproject that follows CMake's convention and leaves the kind of library to `BUILD_SHARED_LIBS` still built static archives, and the link then failed on a `lib.so` that was never produced. The only way out was a hand-written `-DBUILD_SHARED_LIBS=ON` in `cache_args`. `options::build_shared_libs` (a `std::optional`) states it: - unset, the default, passes nothing, so every existing build program configures, and keys its installation, exactly as before; - `true` or `false` passes `-DBUILD_SHARED_LIBS=ON` or `OFF` to the configure step. The argument is part of the recipe, so an installation built shared and one built static have different cache keys; - together with a `BUILD_SHARED_LIBS` in `cache_args` (joined, spaced or typed) the member refuses, naming both, and declares nothing, rather than silently letting one of two contradictory statements win. `shared` keeps its meaning; the two are separate statements that usually agree. Tests: - plugin-logic gains four cases: ON reaches the configure step and the key, OFF reaches the configure step, unset passes nothing even with `shared = true`, and the conflict is refused with its reason and no action. They do not compile without this change; with it 56 of 56 cases pass. - The cmake-consumer fixture's library no longer hard-codes SHARED: it is built shared because the build program sets `build_shared_libs = true`, and check-deps-and-qt.sh asserts BUILD_SHARED_LIBS=ON in the subproject's CMakeCache.txt. Without the option the fixture fails to link its missing libgreet.so, so the lane now exercises the option end to end. docs/deps.md lists the option in the deps-cmake table. Closes #50 --- .github/scripts/check-deps-and-qt.sh | 5 ++ deps/cmake.cppm | 16 ++++++ docs/deps.md | 1 + tests/cmake-consumer/build.mcpp | 2 + tests/cmake-consumer/greet/CMakeLists.txt | 7 +-- tests/plugin-logic/build.mcpp | 63 +++++++++++++++++++++++ 6 files changed, 91 insertions(+), 3 deletions(-) diff --git a/.github/scripts/check-deps-and-qt.sh b/.github/scripts/check-deps-and-qt.sh index 20652ca..02176e3 100644 --- a/.github/scripts/check-deps-and-qt.sh +++ b/.github/scripts/check-deps-and-qt.sh @@ -185,6 +185,11 @@ cmake_consumer() { "$MCPP" build 2>&1 | tee target/ci/build.log "$MCPP" run | tee target/ci/run.log grep -q '^cmake-consumer: greet says 42$' target/ci/run.log || fail "the program did not call the subproject's library" + # The subproject leaves its library's kind to BUILD_SHARED_LIBS, which + # options::build_shared_libs states. + grep -rqs '^BUILD_SHARED_LIBS:[A-Z]*=ON$' target --include=CMakeCache.txt || + fail "options::build_shared_libs did not reach the subproject's configure step" + echo "ok: options::build_shared_libs reached the subproject as BUILD_SHARED_LIBS=ON" assert_not_rerun "$(stamp_of deps-cmake)" # The builds after an edit are planned as well. touch greet/greet.c diff --git a/deps/cmake.cppm b/deps/cmake.cppm index c950f9b..d52a088 100644 --- a/deps/cmake.cppm +++ b/deps/cmake.cppm @@ -88,6 +88,13 @@ struct options { // and whether the prefix's shared-library directory reaches `mcpp run` // and `mcpp pack`. bool shared = false; + // Whether the subproject builds its libraries shared: `true` or + // `false` passes `-DBUILD_SHARED_LIBS=ON` or `OFF` to the configure step, + // and unset leaves the choice to the subproject, as before. `shared` only + // states what the subproject installs; this asks it to build that, so the + // two usually agree. A `BUILD_SHARED_LIBS` in `cache_args` as well is an + // error. + std::optional build_shared_libs; // The `cmake` executable (0.19.0: a `tool::choice`, so a path still // assigns). Default: the `xim:cmake` payload, or an override of it. A // choice made here is not downloaded: the payload is declared on request. @@ -571,6 +578,13 @@ inline prefix use(const options& opt) { return {}; } + if (opt.build_shared_libs && defines(opt.cache_args, "BUILD_SHARED_LIBS")) { + std::cerr << std::format( + "{}: options::build_shared_libs and a BUILD_SHARED_LIBS in options::cache_args both " + "state how the subproject builds its libraries; state it once.\n", who); + return {}; + } + // THE TOOLSET. Compilers or a toolchain file among the cache arguments // are the project's decision, and so is a generator it names -- each by // the argument that states it, not by a substring of another (0.18.0). @@ -668,6 +682,8 @@ inline prefix use(const options& opt) { same("-B"); arg(buildS, ""); arg("-DCMAKE_INSTALL_PREFIX=" + p.root, "-DCMAKE_INSTALL_PREFIX="); same("-DCMAKE_BUILD_TYPE=" + opt.config); + if (opt.build_shared_libs) + same(std::string("-DBUILD_SHARED_LIBS=") + (*opt.build_shared_libs ? "ON" : "OFF")); if (!opt.prefix_path.empty()) { std::string joined; for (auto const& d : opt.prefix_path) { diff --git a/docs/deps.md b/docs/deps.md index f081949..baf3066 100644 --- a/docs/deps.md +++ b/docs/deps.md @@ -224,6 +224,7 @@ takes it. | option | meaning | |---|---| | `toolset`, `crt_linkage` | as `deps-vcpkg` takes them (0.17.0) | +| `build_shared_libs` | unset (the default) leaves the kind of library to the subproject; `true` or `false` passes `-DBUILD_SHARED_LIBS=ON` or `OFF` to the configure step, so a subproject that follows CMake's convention builds its libraries shared or static. `shared` states only what the subproject installs, so the two usually agree; the value is part of the installation's key, and a `BUILD_SHARED_LIBS` in `cache_args` as well is refused | | `generator` | `default` (and `ninja`, its 0.17.0 spelling): the Ninja generator with the toolset named, wherever the toolset is resolved; `visual_studio`: CMake's Visual Studio generator on the instance mcpp resolved, for a subproject that needs MSBuild (0.18.0) | | `cache` | where installations are kept: a directory, `"off"`, or empty for `MCPP_DEPS_CMAKE_CACHE` and then the user's cache directory (0.18.0) | | `cmake` | the `cmake` program: a path (as before), `tool::root(dir)` or `tool::on_path()`; empty is the `xim:cmake` payload, or an override of it. A tool named here is not downloaded (0.19.0) | diff --git a/tests/cmake-consumer/build.mcpp b/tests/cmake-consumer/build.mcpp index 7164df8..3fd0816 100644 --- a/tests/cmake-consumer/build.mcpp +++ b/tests/cmake-consumer/build.mcpp @@ -8,5 +8,7 @@ int main() { o.progress = true; o.libraries = { "greet" }; o.shared = true; + // The subproject builds whichever kind BUILD_SHARED_LIBS asks for. + o.build_shared_libs = true; return mcpp::deps::cmake::use(o) ? 0 : 1; } diff --git a/tests/cmake-consumer/greet/CMakeLists.txt b/tests/cmake-consumer/greet/CMakeLists.txt index 3672483..2a17c1f 100644 --- a/tests/cmake-consumer/greet/CMakeLists.txt +++ b/tests/cmake-consumer/greet/CMakeLists.txt @@ -1,9 +1,10 @@ -# A CMake subproject standing in for a vendored library: one shared library, -# installed with the layout `install()` gives it. +# A CMake subproject standing in for a vendored library: one library, shared +# when the consumer asks for it with BUILD_SHARED_LIBS (static otherwise, as +# CMake builds it by default), installed with the layout `install()` gives it. cmake_minimum_required(VERSION 3.16) project(greet LANGUAGES C) -add_library(greet SHARED greet.c) +add_library(greet greet.c) target_include_directories(greet PUBLIC $) set_target_properties(greet PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON) diff --git a/tests/plugin-logic/build.mcpp b/tests/plugin-logic/build.mcpp index a7c1a03..ea75efb 100644 --- a/tests/plugin-logic/build.mcpp +++ b/tests/plugin-logic/build.mcpp @@ -578,6 +578,69 @@ int main(int argc, char** argv) { c.expect(r.has_line(yes), std::string(yes) + ": the argument chooses"); } }, + { "cmake: build_shared_libs reaches CMake as BUILD_SHARED_LIBS, and the key states it", + with_subproject(t::row::windows_managed()), + [] { + mcpp::deps::cmake::options o; + o.build_shared_libs = true; + return cmake_use(o); + }, + [](const t::result& r, t::checker& c) { + c.expect(r.exit_code == 0, "the member succeeds"); + std::string script; + for (auto const& f : r.files_under("out/deps-cmake/greet")) + if (f.ends_with("greet.cmake")) script = r.file(f); + c.expect(script.find("[==[-DBUILD_SHARED_LIBS=ON]==]") != std::string::npos, + "the configure step asks for shared libraries"); + const auto from = script.find("set(recipe [==["); + const auto recipe = from == std::string::npos ? std::string() + : script.substr(from, script.find("]==])", from) - from); + c.expect(recipe.find("arg -DBUILD_SHARED_LIBS=ON\n") != std::string::npos, + "an installation built shared has its own key"); + } }, + + { "cmake: build_shared_libs false asks for static libraries", + with_subproject(t::row::linux_gcc()), + [] { + mcpp::deps::cmake::options o; + o.build_shared_libs = false; + return cmake_use(o); + }, + [](const t::result& r, t::checker& c) { + c.expect(r.exit_code == 0, "the member succeeds"); + c.expect(r.file("out/deps-cmake/greet/greet.cmake").find("[==[-DBUILD_SHARED_LIBS=OFF]==]") + != std::string::npos, + "the configure step asks for static libraries"); + } }, + + { "cmake: build_shared_libs unset leaves BUILD_SHARED_LIBS to the subproject", + with_subproject(t::row::linux_gcc()), + [] { + mcpp::deps::cmake::options o; + o.shared = true; + return cmake_use(o); + }, + [](const t::result& r, t::checker& c) { + c.expect(r.exit_code == 0, "the member succeeds"); + c.expect(r.file("out/deps-cmake/greet/greet.cmake").find("BUILD_SHARED_LIBS") == std::string::npos, + "options::shared alone passes nothing to CMake"); + } }, + + { "cmake: build_shared_libs and BUILD_SHARED_LIBS in cache_args together are refused", + with_subproject(t::row::linux_gcc()), + [] { + mcpp::deps::cmake::options o; + o.build_shared_libs = true; + o.cache_args = {"-D", "BUILD_SHARED_LIBS:BOOL=OFF"}; + return cmake_use(o); + }, + [](const t::result& r, t::checker& c) { + c.expect(r.exit_code != 0, "the member fails"); + c.expect(r.errors.find("options::build_shared_libs and a BUILD_SHARED_LIBS") != std::string::npos, + "the reason names both statements"); + c.expect(!r.has_line("mcpp:action=", {"deps-cmake:greet"}), "no installation is declared"); + } }, + { "cmake: a launcher in cache_args keeps the named toolset and Ninja", with_subproject(t::row::windows_managed()), [] {