Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .github/scripts/check-deps-and-qt.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions deps/cmake.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool> 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.
Expand Down Expand Up @@ -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).
Expand Down Expand Up @@ -668,6 +682,8 @@ inline prefix use(const options& opt) {
same("-B"); arg(buildS, "<build>");
arg("-DCMAKE_INSTALL_PREFIX=" + p.root, "-DCMAKE_INSTALL_PREFIX=<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) {
Expand Down
1 change: 1 addition & 0 deletions docs/deps.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
Expand Down
2 changes: 2 additions & 0 deletions tests/cmake-consumer/build.mcpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
7 changes: 4 additions & 3 deletions tests/cmake-consumer/greet/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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 $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>)
set_target_properties(greet PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS ON)

Expand Down
63 changes: 63 additions & 0 deletions tests/plugin-logic/build.mcpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
[] {
Expand Down