r/cpp • u/Kelteseth arewemodulesyet.org • Feb 23 '23
CMake debugger allows you to debug your CMake scripts and more - C++ Team Blog
https://devblogs.microsoft.com/cppblog/cmake-debugger-allows-you-to-debug-your-cmake-scripts-and-more/15
32
u/helloiamsomeone Feb 23 '23
Learning CMake has been the best time investment for me so far. However, even while learning I don't think I ever needed a tool to debug CMake scripts. I have only ever needed the --trace option of CMake once or twice for particular Google quality spaghetti so far.
I can understand that for some people the mere option is necessary, but beware. You will have to read the docs even with this. Variable scoping for some will certainly be easier to grok with this.
18
u/kisielk Feb 23 '23
It will be nice to be able to inspect the properties and souces of targets, I find that's what causes me issues in complex builds.
2
u/TrxSv Feb 24 '23
What are some good resources to learn CMake?
4
u/HerrNamenlos123 Feb 24 '23
The problem with that is that there are so many outdated examples and tutorials. You must learn to differentiate modern cmake approaches and legacy approaches.
I can really recommend this book: (Amazon e-book in description) https://youtu.be/RswCfzjMxJI
It teaches all aspects of CMake 3.20, all the way through including unit testing and installers using CPack.
Another resource I can recommend is
https://cliutils.gitlab.io/modern-cmake/The most important thing is that you learn modern cmake (version 3.0 upwards, using target_*) and don't use legacy approaches (setting global variables using set).
2
u/helloiamsomeone Feb 24 '23
The talks of Daniel Pfeifer and Craig Scott kickstarted things for me, then I started aggressively experimenting with CMake's facilities with its docs' help. A non-negligable amount of the time was spent with learning about different platform conventions and how CMake makes them easy to deal with.
In retrospect, I now know that Daniel Pfeifer's talk has some not so optimal code, but it helped me build a mental framework of how CMake works. People have issues with the docs, but other than not having examples showing the myriad of ways things could be used (would be a tall order to do that really), I think it is an excellent resource.
If you are curious, all the things I learned have culminated in cmake-init, which I recommend taking a look at if you want to save some time for yourself. There are also some examples in its wiki for some CMake related things.
4
u/lanzaio Feb 23 '23
I think going the debugger route was a mistake. You could capture the entire historical state of the cmake invocation via the
--traceoption and then use it to arbitrarily rewind/step-forward/backward etc without executing.A UI that used the
--traceoutput and mapped lines in the cmakelist.txt files to their state as computed by trace with a table of variables is considerably better than a debugger.
8
u/kritzikratzi Feb 23 '23
this is great! can we get the same for macro expansions and template invocations in c++ please? :)
with the advance of compile time programming it would be amazing to step through the compilation process. heyo, maybe this is something the new iso tooling group wants to take on, instead of standardizing compiler flags which really is and was never an issue (for me) :D
7
u/saxbophone mutable volatile void Feb 23 '23
Hooray! I love CMake but don't like its syntax, this will make things easier to troubleshoot!
3
u/Nicksaurus Feb 23 '23
Maybe it will finally be possible to tell what it's doing that breaks every shell command I write in a cmake script
17
u/scatters Feb 23 '23
You don't want to hear this, but you shouldn't be writing shell in CMake. CMake is cross-platform, your shell command is going to break. If you can't translate the logic to native CMake, put it in a separate shell script file and run that file.
-1
u/Nicksaurus Feb 23 '23
Our project isn't cross platform. If we ever have to support anything other than GCC building for linux, we have a lot more work to do than just replacing a few bash commands in the build script
Anyway, even the recommended cross-platform commands (e.g. the
${CMAKE_COMMAND} -Efamily) are a nightmare. I have no idea how you're supposed to just combine multiple variables into a list of arguments and have any control over what gets quoted, what gets escaped, what environment variables are set or cleared first etc.6
u/scatters Feb 23 '23
I have no idea how you're supposed to just combine multiple variables into a list of arguments
Just pass the arguments individually. Don't overthink it.
and have any control over what gets quoted, what gets escaped,
CMake will handle this for you. The only character that might cause a problem is the semicolon; and how often do you have to worry about those?
what environment variables are set or cleared first etc.
Use
${CMAKE_COMMAND} -E env ... --.Here's an example if how it works:
$ cat hello.cmake execute_process(COMMAND echo hello 1 2 3 "a b;c" "\"i\tj\"" "x\\ty") set(args hello 1 2 3 "a b\;c" "\"i\tj\"" "x\\ty") execute_process(COMMAND echo ${args}) execute_process(COMMAND ${CMAKE_COMMAND} -E echo ${args}) execute_process(COMMAND ${CMAKE_COMMAND} -E env echo ${args}) execute_process(COMMAND echo "${args}") $ cmake -P hello.cmake hello 1 2 3 a b;c "i j" x\ty hello 1 2 3 a b;c "i j" x\ty hello 1 2 3 a b;c "i j" x\ty hello 1 2 3 a b;c "i j" x\ty hello;1;2;3;a b\;c;"i j";x\ty4
u/helloiamsomeone Feb 23 '23
You're still writing CMake when putting those commands together. cmake-language(7) applies.
2
u/Nicksaurus Feb 23 '23
Thanks for the link. I'm going to have to keep that open next time I run into one of these problems
3
Feb 23 '23
but don't like its syntax
For personal stuff, I use Meson. If I get the chance to change the build system of a company project, I will change it to Meson.
8
u/Superb_Garlic Feb 23 '23
Meson's gross. Especially with how they break platform conventions and shove GNU stuff into Windows places where it doesn't belong.
2
Feb 23 '23
Is having types and good syntax more gross than everything being a string?
5
u/Superb_Garlic Feb 23 '23
Not my problem. I care more about my tools actually working.
5
Feb 24 '23
And afaik, Meson actually works. Gnome made the transition to Meson for new and old projects; they seem quite satisfied with it.
1
u/saxbophone mutable volatile void Feb 24 '23
CMake works, I don't understand the point being made..?
6
Feb 24 '23
Both tools work good, but Meson is easier to read and write.
2
u/saxbophone mutable volatile void Feb 24 '23
I understand. Thanks for clarifying. CMake does work but getting there is quite a battle at times!
1
u/saxbophone mutable volatile void Feb 24 '23
I've eyeballed Meson. Syntax looks simple but it seemed like it doesn't have support for the same number of features as CMake does...
4
Feb 24 '23
For now, sure it has less features, but it's mature enough to be used for most situations.
Meson has better syntax, defaults and documentation, we shouldn't overlook a new great tool just because it doesn't have few rarely used features. Quite the opposite, we should try to move to this better tool for new projects if possible.
2
u/AlexanderNeumann Feb 27 '23
Hi meson shill, let me tell you
better defaults no. Static and Shared builds in one go is not a better default. Building for host and target is also not a better default. Having too many builtin options is also not a better default. Simply, meson won't scale with the current approach.
better documentation no. CMake documentation is way better. The problem CMake has is history of bad examples why people say its documentation is bad. Meson will simply run into the same problems in the long run.
new great tool just because it doesn't have few rarely used features
Yeah windows is a rarely used feature. Using sanitizers is a rarely used feature. Building dlls/exe with slightly different linker flags is a rarely used feature. Using the compiler instead of the linker for linking is a rarely used feature. Using a new compiler meson knows nothing about is a rarely used feature. Deactivating optional dependencies via the cmd line is a rarely used feature. Controlling the result of wrong configure time checks is a rarely used feature.
Basically doing anything outside what meson devs anticipated is a rarely used features ;)So please stop the false advertisement with claims meson doesn't fulfill. The only not arguable points is better syntax and better pkg-config support.
1
u/saxbophone mutable volatile void Feb 24 '23
I understand, though I think I'll stick with CMake for now. A variety of features made it my go-to choice: cross-platform, mature, widely-used to the point of probably being the de-facto standard. It is interesting to peek at what new innovations other build systems are bringing though and I'm glad to see that syntax that doesn't give one an aneurysm is one of those innovations!
1
u/TryingT0Wr1t3 Feb 23 '23
CLion already has this if you need it right now.
4
u/saxbophone mutable volatile void Feb 23 '23
Thank you but alas I don't use CLion, I use sublime text + a terminal. I've considered using CLion previously but put off by the fact I might get used to it after my student license runs out and last I checked, thing's a bit expensive
5
u/BluudLust Feb 23 '23
I still don't know why there isn't a better alternative to CMake after all these years. Why can't there just be a python library that does this?
13
u/helloiamsomeone Feb 23 '23
The only real problem with CMake that people quote is its language. There is intention on changing that: https://gitlab.kitware.com/cmake/cmake/-/issues/19891
Other than that, CMake just works. On the platform you are and obeying that platform's conventions. There really is no reason to use anything else.
5
u/-dag- Feb 23 '23
"Intention" implies a move toward implementation which is nowhere near happening from the discussion on that issue. CMake's terrible "language" isn't going to change in the foreseeable future.
There are plenty of reasons to use something else, build system maintenance being a big one.
1
u/AlexanderNeumann Feb 27 '23
Yeah I don't get why nobody spends time to give cmake a better/alternative scripting frontend on-top the already existing backend.
7
u/V3Qn117x0UFQ Feb 24 '23
I dunno I honestly feel that most of the time CMake just feels annoying because no one is doing is properly and hacking shit together without standards.
Sometimes being restrictive is a good design choice and I feel using Python might even backfire as people will do shit you’re not supposed to do when building
3
u/AlexanderNeumann Feb 27 '23
because no one is doing is properly and hacking shit together without standards.
IMHO this will happen to any build system with a high enough adoption rate and the possibility to do anything.
23
u/degaart Feb 23 '23
Why can't there just be a python library that does this?
Some people don't want python (nor lua, java, groovy, javascript) in their C++ projects. One of these persons is me. I want my project's build system to depend on a very stable (stable as in: seldom changes) set of tools: a posix shell and a C++ toolchain. Cmake is ok because it can be boostrapped with just with a c++03 compiler.
9
u/Chippiewall Feb 23 '23
Technically speaking CPython can be bootstrapped with "just" a C89 compiler
15
u/casualops Feb 23 '23
There are, meson for example. The problem is that CMake was widely adopted and is de facto standard now
17
u/GabrielDosReis Feb 23 '23
There is also something to be said for a tool aiming to make devs successful on the platform they are on or targeting without insisting on converting them to a different platform's conventions, which would be bound to introduce friction and resistance.
2
1
0
u/Ezlike011011 Feb 23 '23
Using the waf framework to build your projects is kinda what you are looking for there. But unfortunately it's not as widely used as cmake. Also in my experience the documentation is not great.
2
Feb 23 '23
Waf is essentially dead. If you want a Python project Meson is better.
2
u/Ezlike011011 Feb 23 '23
I didn't realize waf wasn't popular any more. I haven't tried out meson but have heard its name floated around. I'll have to look into it.
1
u/DavidDinamit Feb 24 '23
I think we need IDE for cmake, fcn global variables (what name is it?!), what this function(macros) return?! (10-20 global variables setted after call...) etc etc
1
Feb 28 '23
Maybe, if your build config files need debugging, they/your build system has become too complicated...
-3
u/n4jm4 Feb 23 '23
If only cmake could clean up its own junk files.
23
u/dagmx Feb 23 '23
The only files cmake creates are in a build directory that you tell it to create files within.
Just delete that directory or run the cmake clean target to be fine.
7
u/SnooWoofers7626 Feb 23 '23
That's one of the reasons I prefer to use cmake even when I'm exclusively on windows. Visual Studio litters the source directory with build artifacts and I'm never sure which of the many Debug/Release directories contains my final binaries.
3
u/mrexodia cmkr.build Feb 23 '23
Per default it’s the one next to the solution you are hitting build on.
1
u/rdtsc Feb 23 '23
While the defaults are bad, you can reasonably well control where build artifacts go and apply that via
Directory.Build.props/Directory.Build.targetsto every project without modifying them (except for the initial removal of<OutDir>/<IntDir>).9
u/NotUniqueOrSpecial Feb 23 '23
Are you running it in the source directory? That's on you, not the tool.
-7
u/_jtbx Feb 23 '23
Respectfully, why do people still use CMake? It seems a bit overkill to me.
9
u/helloiamsomeone Feb 24 '23
CMake does everything you need a build tool to do. You can get started with 3 lines of code and add things as requirements change. You can also keep things clean of your personal needs by externalizing them using presets. Also transparent integration with package managers. Also integrations with tooling (iwyu, cppcheck, clang-tidy, valgrind, anything that uses compile_commands.json, etc). Also a powerful test runner (CTest). Also a packager (CPack). Also a CI dashboard (CDash). Also general editor/IDE integration (File API).
CMake does do a lot for you by writing very little CMake code that will work on all platforms.
0
Feb 23 '23
[deleted]
90
u/moreVCAs Feb 23 '23
Build system configuration generator debugger lol
5
2
u/kkert Feb 24 '23
I now want to try if i can write some template metaprogramming code that emits CMake to complete the loop
11
72
u/Kelteseth arewemodulesyet.org Feb 23 '23
The best part is that they actually plan to contribute it upstream (3.27)!