Cmake With Clang

broken image
Cmake

NOTE: The content here has been superseeded by a better implementation outlined here.

Setup Clang Tooling Using CMake and Make ¶ If you intend to use make to build LLVM, you should have CMake 2.8.6 or later installed (can be found here). First, you need to generate Makefiles for LLVM with CMake. You need to make a build directory and run CMake from it. I'm installing clang-6.0 since I prefer to use it as my default compiler. I suppose default GCC would work. Libclang-6.0-dev and python-clang-6.0 are both being requested by the FindCppyy.cmake which subsequently calls the included FindLibClang.cmake this fails without installing the libraries from the system repositories. Maybe there is a way. It turns out that there is fairly simple way to find it though: findprogram(CLANGFORMAT NAMES clang-format clang-format-3.6 clang-format-3.5 clang-format-3.4) This picks the first match in the list of names, which is just what I need. It's not totally generic because, when a new version of clang-format is released, the command needs updating. First is to check if clang-format can be found using cmake's findprogram functionality, and only add the new ‘format' target if the program is found. This is so that the CMake script and program can still work on platforms without the tool. Next is to collect all of the files to be formatted. Cross-building with Autotools, CMake and Meson. Autotools, CMake, and Meson are arguably the most popular building systems for C and C open source projects (sorry, SCons). All of three support cross-compiling out of the box, albeit with some caveats. Over the years, Autotools has been famous for being horrendously clunky.

After the back of adding the basic clang tools, the ball began rolling to see what else could be integrated into the project infrastructure using CMake. The code coverage tools are pretty unique in that they can be linked into a build just by adding extra compiler flags.

Building

Cmake

NOTE: The content here has been superseeded by a better implementation outlined here.

Setup Clang Tooling Using CMake and Make ¶ If you intend to use make to build LLVM, you should have CMake 2.8.6 or later installed (can be found here). First, you need to generate Makefiles for LLVM with CMake. You need to make a build directory and run CMake from it. I'm installing clang-6.0 since I prefer to use it as my default compiler. I suppose default GCC would work. Libclang-6.0-dev and python-clang-6.0 are both being requested by the FindCppyy.cmake which subsequently calls the included FindLibClang.cmake this fails without installing the libraries from the system repositories. Maybe there is a way. It turns out that there is fairly simple way to find it though: findprogram(CLANGFORMAT NAMES clang-format clang-format-3.6 clang-format-3.5 clang-format-3.4) This picks the first match in the list of names, which is just what I need. It's not totally generic because, when a new version of clang-format is released, the command needs updating. First is to check if clang-format can be found using cmake's findprogram functionality, and only add the new ‘format' target if the program is found. This is so that the CMake script and program can still work on platforms without the tool. Next is to collect all of the files to be formatted. Cross-building with Autotools, CMake and Meson. Autotools, CMake, and Meson are arguably the most popular building systems for C and C open source projects (sorry, SCons). All of three support cross-compiling out of the box, albeit with some caveats. Over the years, Autotools has been famous for being horrendously clunky.

After the back of adding the basic clang tools, the ball began rolling to see what else could be integrated into the project infrastructure using CMake. The code coverage tools are pretty unique in that they can be linked into a build just by adding extra compiler flags.

Building

There are two main methods for creating a code-coverage build, the first is by specifying an option that hooks up the current build with the code coverage tools, and the second is a core build just for code coverage. Laravel docker.

As an option

The first mode of inclusion, and the preferred way is to add an option, in this case named CODE_COVERAGE then, when selected to ON either via the command line with -DCODE_COVERAGE=ON or any CMake UI, will hook in the coverage support to whatever core build type is currently selected in CMake, whether Debug, Release, or any other.

As a core build

Another option added, as was requested by some was the ability to just select coverage as the actual CMake build type, such as by -DCMAKE_BUILD_TYPE=coverage, where it builds automatically as a Release with it's flags and the coverage hooked in, essentially the same as doing -DCMAKE_BUILD_TYPE=Release -DCODE_COVERAGE=ON.

GCC vs Clang

Now, while both GCC and Clang have code coverage capabilities, there is a difference between them. The Clang-based tool can generate HTML output by itself using the llvm-cov tool, which is automatically part of the llvm toolset, where as to convert GCC's coverage files into a nice readable/exportable HTML format requires two tools, lcov and genhtml, which are less commonly found.

Compilation setup

So, when coverage tools are requested, the first determination to make is if a required compiler is found. I work on Linux, so I haven't found a real good tool for MSVC that really stood out unfortunately, but GCC and Clang both are common enough, and their requirements for programs can be checked for and appropriate flags added.

Of course if the programs required for the chosen compiler aren't found, then the script error's out fatally, letting the user know what it needs to complete.

The actual targets

In order to generate the coverage information, programs must be run to count the code usage, and thus much like the clang tools, functions are used to generate new targets based off the original, which automatically run the target program. Several new target types are then made available, each displaying the information in a different way.

Cmake Use Clang

Show

The show variant displays on the command line the coverage information.

Report

The report shows very quick per-file information about the code.

Regular

Cmake Clang Cross Compile

This generates an HTML per-file report that can be perused showing in a friendly manner the useage of the code in the run program.

This includes showing how many times each statement was executed when hovered over, and highlights items in red that were never executed. As well as expansions for each template type that was instantiated.

Source Code

Full fledged (and possibly updated) sources of this can be found under the Apache 2.0 license here.





broken image