clang-tidy for CMake?

Imagine: clang-tidy CMakeLists.txt -checks=modern-*

There are many blogs and videos out there telling us how to write modern CMake but having the mental capacity to stick to those guidelines is a different story.

Is there anything out there that would spew a warning on a bad practice? I am quite impressed how clang-tidy can suggest effective improvements to C++ code so doing the same for CMake code does not seem too far fetched.

The closest discussion is: CMake linter?

If you’re using VSCode, the plug-in for CMake has a nice CMake-tidy extension.

Which plug-in/extension do you mean exactly? I tried installing the “CMake Tools” by Microsoft and “CMake” by twxs, but haven’t found anything about tidying (just nice syntax highlighting). Could you elaborate a bit on how to access this feature?

Note that I’ve only very recently started to use VSCode, so thanks in advance for your help!

If you right click inside a CMake list file, you’ll see a couple of options for “Format Document”. You can play around with them.

Get BlueMail for Android

I mean left click, not right click…

Get BlueMail for Android

I should quit while I’m ahead. It is right click.

Get BlueMail for Android

Format and syntax highlighting is one thing but actual best-practice linter is a different story which is what I am talking about.

The most basic example would be: you have a include_directories directive and the linter would produce a warning suggesting to use target_ include_directories instead.

Just with CMake and CMake Tools extension, I don’t have any additional options in the right click menu, it looks exactly as for other files. I looked a bit more now, and found the “cmake-format” extension. It provides the mentioned “Format Document” right click menu entry, however that doesn’t seem to do anything - after clicking that, there is no change whatsoever in the file…

Edit: Seems the “cmake-format” extension is just a bridge for calling the cmake-format tool (probably the one from cheshirekow/cmake_format @ github - looks like this also contains a cmake-lint! Or is it a part of that collection? They also provide a vsix file under release asses :confused: But the project seems a bit abandoned, no change since 3 years

Yeah, there are two entries when I right click

One says “Format Document [Ctrl+K Ctrl+D]”

The other says "Format Document With… "

Selecting the ‘with’ one, I get to formatters to choose from, “CMake-format” which does nothing, and another that says “CMake Language Support” and that’s the one that works…

Get BlueMail for Android

Yes, I see what you mean. I haven’t seen anything as capable as that.

Get BlueMail for Android

As far as I can tell, the cmake-lint command I mentioned above is mostly reporting on what it considers formatting problems. It has a few style remarks as well (regarding variable, function and argument names), but I don’t know from which recomendations it pulls its recommendations. For example, for this line:

function (ExtractVersion filename identifier output_varname)

it brings up this message:

Invalid function name "ExtractVersion" doesn't match `[0-9a-z_]+`.

Apart from the formatting messages and such low level coding style remarks, I don’t see any “more useful” output at the moment, for example regarding modern CMake.