r/cpp • • May 23 '26

Run CMake executable targets via the cmake command

https://a4z.noexcept.dev/blog/2026/05/23/CMake-Run-Targets.html

Every time I use bazel for a while, and come back to CMake, I notice that I miss a cmake --run command. Bazel has a bazel run //target command.
We can not do that with cmake, but something similar.

21 Upvotes

41 comments sorted by

View all comments

Show parent comment

-2

u/throw_cpp_account May 24 '26

What are you talking about?

I thought I was pretty clear: CMake gives you no tools to build your tests. It is just up to you to figure out how to do it. And if you forget to do so, well now you just have stale test results.

"Making a different build directory" is something you do with every build system.

No, it isn't something that you have to manually do with every build system.

I don't understand how people become zealots for a build system

Pointing out usability limitations with one tool does not mean I'm a zealot for a different one. That it apparently offends you to point out CMake issues is not my problem.

1

u/13steinj May 24 '26

I thought I was pretty clear: CMake gives you no tools to build your tests. It is just up to you to figure out how to do it. And if you forget to do so, well now you just have stale test results.

This is just so far out of left field this isn't worth debunking. The short version though, is if you're using cmake --build <test target>, unless you're doing something I haven't seen before, it will build if necessary and run your tests. If you're using ctest without a sufficiently configured CTestTestfile.cmake, then yeah, it won't build for you, because CTest is a sophisticated test harness that is effectively (if not literally) its own, separate tool.

Totally agree that the defaults for both cmake and ctest suck, though.

No, it isn't something that you have to manually do with every build system.

Not every, but nearly all of them, or you have separate problems with in-source builds and we've long since learned the issues there with original Make-style systems. Heaven forbid gasp two additional one time commands? mkdir and the cmake configure step, which you generally don't have to rerun? If you're using the ninja generator, it's generally intelligent enough to only reconfigure-when-needed, whereas in Bazel not only is it a slow (from a responsiveness perspective) server-client model but its behavior is "slowly check if needed, then do the equivalent of a reconfigure." Waiting a minute in a large repo for "checking cached actions" always makes a small piece of me die inside.

Pointing out usability limitations with one tool does not mean I'm a zealot for a different one. That it apparently offends you to point out CMake issues is not my problem.

Firstly, you've severely edited your original comment since my reply. Secondly, what offends me isn't pointing out issues. All build systems have their problems! But something I've found distasteful over the past 2 years is: "How do you know someone uses Bazel? They won't shut up about it." Bazel has plenty of issues, from not supporting run correctly depending on the current working directory, to inefficient analysis, to not being able to dynamically create nodes and edges in the build dag. It's still great for a large monorepo with true cross-language work (I think it's overused when all you have is some thin python or java bindings, and your true app core is C++).

CMake has many problems, particularly regarding bad defaults and focusing too much on backwards compatibility of bad decisions. But I will constantly see Bazel (and less so, meson) zealots dump on it for non-existent issues, or worse yet things that don't matter in practice, while pretending that Bazel doesn't have its own limitations that forbid basic functionality (x-fail, compile tests, the --cpu=k8 vs config debacle, the mess that is the bazel prelude, build-dag tests).


I think the dichotomy of the two build systems can be seen simply as:

Kitware is a company that happened to make CMake, and now provides consulting for it. Google is a company that wanted a Python DSL for a cross-language build system, then quickly outgrew the actual need for it; the internal authors leaving and forming 4 different consulting companies, then laying off who was left inside Google maintaining it.

4+ different consulting agencies for a build system does not look like a healthy ecosystem to me, especially when their only non-consulting product / service is effectively "lets sell remote build compute at a premium."

1

u/throw_cpp_account May 25 '26

This really is something else. I don't know what your problem is. I posted a comment showing that Bazel is more user-friendly than CMake (and later edited it to show that many other build systems are also more user-friendly in this way) on a post pointing out another way in which Bazel is more user-friendly. This gets me vitriol and labeled a zealot dumping on non-existent issues? Buddy, you don't know anything about me. You don't know my opinion on Bazel. You certainly don't even know if I use Bazel (incidentally, I do not).

Are you going to call me a Rust zealot if I point out that, of course, cargo test vs cargo test --release just does the right thing and puts things in debug and release directories and builds everything as you'd expect (I don't use Rust either).

After all of that, do you even understand what it is I'm complaining about? Because you posted this:

This is just so far out of left field this isn't worth debunking. The short version though, is if you're using cmake --build <test target>, unless you're doing something I haven't seen before, it will build if necessary and run your tests.

Alright. If it is indeed so far out of left field then it should actually be very easy to prove me wrong. If it's actually possible to do this (i.e. have a single command to run a test, or set of tests, that also builds it/them if necessary) in CMake, I'd love to hear it, because I actually do use CMake and it would make my life, and my colleagues' lives, easier. So I'd appreciate if you'd enlighten me.

But the patronizing bullshit ain't it. cmake --build takes a build directory, not a test target, so that obviously does not build and run my tests. So do better.

Given a simple CMakeLists.txt

add_executable(foo foo.cxx)
add_test(NAME test_foo COMMAND foo)

Doing something like cmake --build build --target test will run the test, but won't build it (which best case will fail because it was never built, but worst case is stale). That's the "non-existent issue" that plenty of other build systems actually handle correctly. cmake --build build --target test_foo is invalid. cmake --build build --target foo will of course build the binary properly, but the point is to do this in one step, not two. I can also ctest --test-dir build -R '^test_foo$', but that also only runs the test, it doesn't build it.

I don't know what you actually meant to suggest, maybe you just meant that building all works. Which yes, all works. But all is far more than necessary, and especially isn't helpful if I'm iterating on a single test.

2

u/13steinj May 25 '26 edited May 25 '26

Doing something like cmake --build build --target test will run the test, but won't build it (which best case will fail because it was never built, but worst case is stale). That's the "non-existent issue" that plenty of other build systems actually handle correctly.

https://gist.github.com/13steinj/09b1cb4206689ca14e7922c09b3f465a#file-proof-log

Adapted from here. I don't like how verbose this is though, and generally write a function that does this for me. Key warning: if you get the "link" groups wrong (PUBLIC/PRIVATE/INTERFACE) you'll either error out or get the wrong behavior (which is expected... but both the "wrong behavior" case and

You can use Build and Test mode at the top level for a one-shot command if you like, or as shown here to have the DAG wired correctly. Note that the makefile generator is a known mess here, I can't tell you if it has the correct behavior. The VS generators in theory should have the right behavior, but I'm not a Windows/msbuild guy so I can't confirm.


There are multiple ways to map individual tests to their own target, but I prefer build-and-test mode (e.g. I think with -R works?) at the toplevel for this, and use an alias to make the command shorter. You can generally create a bunch of subdirs / subproject()s and that will give you names for groups of tests that you can --build.

Again, I completely agree that the defaults / default behavior of add_test absolutely sucks here, even worse that you need to read "Mastering CMake" to learn how to do it right. I think the history here is that ctest and cmake started off as separate projects, ctest for use and integration with cdash, and eventually they merged codebases (because they use the same scripting language, but different builtins / "standard library" modules) but for historical reasons didn't add the automatic dag-wiring (because the cmake add_test builtin and the ctest add_test builtin are actively different in the source code).

I would take my bet that if one were to follow their contribution guidelines, and have an LLM slop together:

  • a policy
  • docs
  • a deprecation/removal timeline

They'd accept it, same goes for automatic mapping of add_test to individual targets (rather than a singular test target per subdir/subproject).


E: Automatic grouped targets are made for add_subdirectory, I could have sworn the same is true for subproject()s but I can't get that to work with 4.3.2.

I've slopped it up myself, I'd link it here but I think that's against sub rules. I'm sure you can find it on my github. It might take some iteration to actually get merged, I don't know how this policy interacts with all the different existing modes of ctest.

1

u/throw_cpp_account May 27 '26

Wow. What in the world. Ok, well I've never seen anything remotely like that before. Not in any blog, examples, nothing. Thank you for the demo.

Again, I completely agree that the defaults / default behavior of add_test absolutely sucks here

Putting it mildly. I don't understand how this isn't just the default. Or, for that matter, if it were the default, I can't really imagine wanting to opt-out.

1

u/13steinj May 27 '26

I think this isn't the default because, as I mentioned, history and backwards compatibility, and then no one cared enough to make a pull request with a cmake policy.

You have to take into consideration that while they may have similar names and now share a repo, cmake and ctest are actually completely separate tools that happen to share the same language (maybe not even that) with different builtins and a different stdlib.

Under the hood cmake add_test calls a builtin, that adds to the generation of an automatic CTestTestfile.cmake basically escaping / prettifying the name and just calling add_test again. ctest is what is intended to interpret this file, so this time add_test runs something different.

If you check my github (again not linking because against sub rules) I've created 3 branches that

  • slop this up (should probably change to changing the default test launcher instead)
  • slop this up + a build target per test (instead of per subdir)
  • the above + wire the build dag internally rather than rely on build-and-test, but only because this might lead to some better behavior with other ctest features).

Even after getting merged I imagine it will take 2 years to get all the edge cases shook out and the policys set to NEW by default.


But also, shit like this (and various bullshit in every build system) is why companies [should] hire dedicated build engineers. It's its own complex ball that deserves a dedicated resource. Thank your nearest build engineer/random dev who cares about a good clean build.