What is the format of `CMakeCache.txt` - cmake

I am asking because I would like to enable syntax highlighting for CMakeCache.txt in my text editor (GNU Emacs).

Turning my comments into an answer
There was a post My Customized cmake-mode.el includes support for CMakeCache.txt back in 2007, so I'm not sure if it is still valid.
Generally speaking the CMakeCache.txt does not follow the normal CMake script syntax and maybe "topic to change".
So it's recommended to Use a CMake GUI or the wizard mode (cmake -i) to edit cache entries.

Related

CMake: What is the advantage of using -D (command line) over add_compile_definitions or target_compile_definitions or set?

New to CMake. When adding libraries I see a lot of instructions saying do this:
cmake -D OPENSSL_ROOT_DIR=/usr/local/opt/openssl
Why is that preferred over putting it in the actual CMakeLists.txt? e.g.
set(OPENSSL_ROOT_DIR "/usr/local/opt/openssl")
You do not commit the first one to the repository while you either commit the second one or have to be careful to not commit it with other stuff.
The second method is usually used to allow users of some project to modify some aspect of that project. So whatever pertains to the project itself (not customizable) goes straight to the CMake files everything else might be provided from the command line.
Note, also, that the CMake presets add an ability to customize the build outside the CMake files so the method of providing variables directly via a console call is something I'd expect going away and getting replaced with presets. But the way they work is still the same: variables defined in the preset get to CMake cache with generated console call (with an IDE it might be different).

What is the proper way to document a CMake module?

A quick Google search (...actually many rather extensive Google searches) have not been able to explain how to properly document a CMake module.
What I'm looking for is a way to document custom CMake modules so that they work with the cmake --help-module <module_name> command. Is there any standard way of doing this? Can anyone point me to some good examples? The documentation process seems oddly.... not well documented. Haha.
How are modules that work with cmake --help-module documented?
Any help is appreciated.
Quote from an email response I got from Brad King (member of the CMake Developers email list):
There is no way to do this. The only reason --help-module exists
at all is because prior to 3.0 the documentation was generated by
the CMake binary itself, and people were used to the option being
available. It is only for builtin modules and only available for
legacy reasons, and may one day go away in favor of the man pages
and html docs.
The online docs, like those at https://cmake.org/cmake/help/v3.14
do publish a /objects.inv to support intersphinx:
http://www.sphinx-doc.org/en/master/usage/extensions/intersphinx.html
This was done on request of some users so I haven't looked into how
that works, but one should be able to use sphinx to generate one's
own documentation and still cross-reference CMake's online docs.
If you get that working we'd welcome a MR to add docs describing how,
perhaps in
https://gitlab.kitware.com/cmake/cmake/blob/master/Help/dev/documentation.rst
It sounds like using Sphinx is the way to go.
Additional info:
How does Sphinx know to go parse that ".cmake" file? Does Sphinx recognize the "cmake-module" keyword in a special way and know what to do with it?
it’s from a Sphinx module that you can find the in the CMake sources Utilities/Sphinx/cmake.py. Or you can install this file using pip:
pip install sphinxcontrib-moderncmakedomain
When configuring Sphinx, you have to name the extensions to use in Sphinx's configuration file (conf.py) and add the name of the extension (sphinxcontrib.moderncmakedomain) to the extensions array.
Good news: You can use sphinx to document your CMake Modules, and at the same time use Doxygen to document your C++ (or other) source code.
I created an example project which shows how to use the sphinxcontrib-moderncmakedomain in combination with Sphinx and Doxygen to generate Documentation for your C++ Code AND CMake code:
https://gitlab.com/Pro1/doxygen-cmake-sphinx
It is using the sphinxcontrib-moderncmakedomain package and CMake itself to configure the sphinx conf.py and then run sphinx.
The example is heavily based on the official CMake Documentation from: https://gitlab.kitware.com/cmake/cmake/-/tree/master/Utilities/Sphinx

Is there any utility that can reformat a cmake file

I have relatively complex cmake files and I am wondering if there is any utility that can reformat a cmakeList.txt and beautify it for easier reading?
Some simple tricks would do the jobs, such as indenting if else endif sections and so on.
Is there any such tools?
I am using notepad++ for editing, but if there is any other tools, I can switch.
Someone recently wrote a CMake autoformatter called cmake_format. Still under development but should be good enough for basic CMake files.
I searched for a code beautifier supporting CMake, and I did not find any.
There is this thread on CMake mailing lists, which does not provide a good solution
This mailing list thread provides no answer too (some use emacs mode)
I checked most of the major code beautifiers using UniversalIndentGUI on Ubuntu. None of the provided beautifiers from the long list are working with CMake out of the box.
Looks like implementing CMake support in an existing beautifier, or writing a custom one is a great idea for a nice code project.
Take a look at Atom and its plugin language-cmake (you can install easily with Atom package manager).
Once both are installed, open your CMakeLists.txt and go to Edit > Lines > Auto Indent.
In case it is not clear from the other answers and comments.
In Ubuntu (Debian?) there is a tool called cmake-format, which seems to be what is called cmake_format in other answers.
And it worked very well for me.
$ apt search cmake-format
Sorting... Done
Full Text Search... Done
cmake-format/kinetic,kinetic,now 0.6.13-2 all [installed]
source code formatter for cmake listfiles
$ cmake-format --help
usage:
cmake-format [-h]
[--dump-config {yaml,json,python} | -i | -o OUTFILE_PATH]
[-c CONFIG_FILE]
infilepath [infilepath ...]
Parse cmake listfiles and format them nicely.
...
The specification file is quite difficult to understand but I liked the default formatting anyway.
The best part is that it seems to normalize the capitalization of keywords which is what drives me crazy.

How do I change the working directory for my program

Trying out the Clion EAP on the Mac. How does one change the current working directory for a launched program? It appears to be set to the output directory of the binaries.
This can be changed via the Run -> Edit Configurations... dialog, just like Idea.
The mix of needing to make changes to CMakeLists.txt for so much of the configuration just threw me, and I've been hunting for a way to change it there, but this bit at least is consistent with the other IDEs, which is good.
Hopefully they'll improve the rest of the Project Settings in a similar fashion.
The "Working Directory" is where the executable starts. Modifying the "Working Directory" in the Configurations setting tells Clion to change to the "Working Directory" after the executable starts execution.
If you want to change where the executable starts execution you will need to add a line to the CMakesLists.txt file.
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
add_executable(TimeServer ${SOURCE_FILES})
Position in the cmakes file is important. If you add the set command after the add_executable it will not work. It must come before.
These answers are relatively old. Not sure when the feature was added but there's a very nice UI way to set the current working directory now.
I made a YouTube tutorial for my COSC1030 (Beginning C++) students but the solution is the same for everyone:
https://youtu.be/dTtZEAfh_LM
If you know your CLion fairly well and don't need a demo, here's a short-hand version: Edit Configurations (from drop-down menu at top right of screen), enter the directory you want to use in Working Directory or select via "..." pop-up.

CMake/CTest & gcovr: filename extensions?

After compiling with CMake with flags --coverage, and running my boost unit test programs, files with extension .cpp.gcda and .cpp.gcno are created. If I then run gcovr it claims it cannot find the .gcno files (error message ".gcno:cannot open graph file"). I could possibly move all output files but that would be really awkward/silly.
Related problems of other people could be solved by using CTest but as I am using Jenkins I'd like to stick to gcovr and use the cobertura xml output.
Ps. Maybe I should simply ask: how should I combine CMake with gcovr?
This is the solution we are using for the same setup inside jenkins: http://www.semipol.de/archives/320. You can simply grab the CMake macro from the linked RSC library for your own purposes.
Apart from that read something about a slightly changed format of the coverage files in recent gcc versions and it seems gcovr didn't keep up with that. But I cannot remember where I read this.