How treat `include-what-you-use` warnings as errors with `cmake` - cmake

Is it possible to treat include-what-you-use warnings as errors using cmake?
I am playing around with include-what-you-use trying to integrate it into our cmake build process. The desired behaviour is to stop the build process when include-what-you-use generates a report, then print a warning. Currently, the build continues.
The tool is integrated to the cmake build process thanks to:
find_program(IWYU NAMES include-what-you-use)
if(IWYU)
message(STATUS "Using include-what-you-use")
set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE ${IWYU})
endif()
I also add the Werror flag to my cmake target.

Related

add_subdirectory for cmake tensorflow lite fails on subsequent runs

I am trying to include tensorflow-lite in a project per the minimal example here: Build TensorFlow Lite with CMake. Specifically, I'm trying to add_subdirectory the CMakeLists.txt for tflite, as recommended.
This works when the project is first built, but if I change my top-level CMakeLists.txt for any reason (adding a test or linking something to a different target, say), then the build fails with the following:
-- Setting build type to Release, for debug builds use'-DCMAKE_BUILD_TYPE=Debug'.
CMake Warning at build/abseil-cpp/CMakeLists.txt:70 (message):
A future Abseil release will default ABSL_PROPAGATE_CXX_STD to ON for CMake
3.8 and up. We recommend enabling this option to ensure your project still
builds correctly.
-- Standard libraries to link to explicitly: none
-- The Fortran compiler identification is GNU 9.4.0
-- Could NOT find CLANG_FORMAT: Found unsuitable version "0.0", but required is exact version "9" (found CLANG_FORMAT_EXECUTABLE-NOTFOUND)
--
-- Configured Eigen 3.4.90
--
-- Proceeding with version: 2.0.6.v2.0.6
-- CMAKE_CXX_FLAGS: -std=c++0x -Wall -pedantic -Werror -Wextra -Werror=shadow -faligned-new -Werror=implicit-fallthrough=2 -Wunused-result -Werror=unused-result -Wunused-parameter -Werror=unused-parameter -fsigned-char
CMake Error at build/cpuinfo/CMakeLists.txt:262 (ADD_SUBDIRECTORY):
ADD_SUBDIRECTORY not given a binary directory but the given source
directory "/opt/------/workspace/------/build/clog-source"
is not a subdirectory of
"/opt/------/workspace/------/build/cpuinfo". When
specifying an out-of-tree source a binary directory must be explicitly
specified.
CMake Error at build/cpuinfo/CMakeLists.txt:265 (SET_PROPERTY):
SET_PROPERTY could not find TARGET clog. Perhaps it has not yet been
created.
-- Configuring incomplete, errors occurred!
See also "/opt/------/workspace/------/build/CMakeFiles/CMakeOutput.log".
See also "/opt/------/workspace/------/build/CMakeFiles/CMakeError.log".
(Those dashes aren't real paths, they're just to mask sensitive info.)
Just to reiterate, it DOES configure and build correctly the first time, but reruns of cmake .. will fail.
I've tried this on a couple of platforms and got the same result. You should be able to reproduce with a minimal CMakeLists.txt containing add_subdirectory and some helloworld target to link tensorflow-lite.
I tried giving the add_subdirectory for clog a binary location but just got a cascade of new errors, and at that point my CMake know-how definitely runs out.
The issue is due to the same cmake variable CLOG_SOURCE_DIR used in both xnnpack and cpuinfo.
In xnnpack, it uses a cmake script to download, configure and build the clog.
In cpuinfo, it add as a subdirectory.
So they use different ways to add dependency. We need to fix this issue in either xnnpack or cpuinfo but as a walkaround, we can change the CLOG_SOURCE_DIR in either project to a different name (e.g. CLOG_SOURCE_DIR1) then this problem will go away.

How can I make colcon work with a plain preset-based CMake project with multiple presets in parallel?

Prologue
I have a preset-based plain CMake project so that I can build and test it with cmake --preset $PRESET && cmake --build --preset $PRESET && ctest --preset $PRESET. Note that it nicely interacts with Microsoft's CMake Tools extension for Visual Studio Code, be it for building, testing, debugging and Intellisense.
Since I want to handle multiple presets in parallel, I set CMakePresets.json's binaryDir property to ${sourceDir}/build/${presetName}/.
Issue
I want to also build this plain CMake project with colcon. colcon build --cmake-args "--preset $PRESET" doesn't work, though, as it produces
WARNING:colcon.colcon_cmake.task.cmake.build:Could not build CMake package 'root_project_name' because the CMake cache has no 'CMAKE_PROJECT_NAME' variable
root_project_name being the argument to CMake's project() command in the top CMakeLists.txt.
How can I resolve this warning and the subsequent build failure?
Straightforward solution
Not setting CMakePresets.json's binaryDir property at all works fine with colcon, but doesn't allow for multiple preset builds in parallel.
Solution with multiple preset builds in parallel
The reason for this behavior is colcon-core's build verb's passing the build base directory (default: build) suffixed by the found package's name (here: root_project_name) to the colcon-cmake extension here.
The solution is to pass the correct build base to colcon (i.e. colcon build --build-base ./build/$PRESET/ --cmake-args "--preset $PRESET") and to adapt your CMakePresets.json's binaryDir property to ${sourceDir}/build/${presetName}/root_project_name/.
Note that this then works with colcon test as well, i.e. colcon test --build-base ./build/$PRESET/ --ctest-args "--preset $PRESET".

CLion: How do I get coverage for the actual library code instead of just the tests

I am testing a C++ library and I want to record coverage when I run the tests. Based on the CLion documentation, I added the necessary compiler switches:
option(CODE_COVERAGE "enable code coverage" OFF)
if(CODE_COVERAGE)
if("${CMAKE_CXX_COMPILER_ID}" MATCHES "(Apple)?[Cc]lang")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-instr-generate -fcoverage-mapping")
else()
message(FATAL_ERROR "Code coverage only available with LLVM/clang.")
endif()
endif()
add_library(libfoo SHARED
# ... snip: source files ...
)
# ... snip: library deps & config
add_executable(libfoo_tests
# ... snip: source files ...
)
target_link_libraries(libfoo_tests gtest_main libfoo) # using Googletest
With this, I can run the tests with coverage in CLion. And I get coverage reports… on the testing code, but not on the library's code. So this is pretty much useless.
I have no idea what CLion actually does when running the tests with coverage, but the docs on LLVM's coverage tool state clearly that
BIN may be an executable, object file, dynamic library, or archive (thin or otherwise).
So it should be possible to get coverage on the library. What do I need to do to make it happen?
I have the same problem. If you remove that code, you would get an error (described here: https://www.jetbrains.com/help/clion/code-coverage-clion.html), that automatically creates a new debug profile with the flags needed for the code coverage. This time the src folder is included (even not perfect), but maybe from there you could find a perfect solution.

How to make a project written with Tcl in CMake?

I am using cmake version 3.14.0-rc3 to make my codes. When I target any code written in C or C++ in my CMakelist.txt as follows, it works pretty and makes the executable file.
cmake_minimum_required(VERSION 3.3)
PROJECT (HELLO)
ADD_EXECUTABLE(hello hello_world.cpp)
but while I am trying to make this code with Tcl scripts, it fails and I receive the following fatal error:
Fatal error while making a tcl script with cmake
Can anyone help me to overcome this issue? It seems that cmake is not normally able to compile Tcl scripts.
Thank in advance for your kind replies and helps.
Bests,
Daryon
You have to watch out for a different path, e.g., running custom commands or adding custom targets to your CMake project. You seem to confuse the nature of libraries, executables, and external commands in the context of CMake, I am afraid.
I think there should be a way to execute Tcl scripts with CMake as
well.
You might want to attempt the following: In CMakeLists.txt, you define a custom target MyTarget that calls out to a TCLSH executable, if available:
CMAKE_MINIMUM_REQUIRED(VERSION 3.3)
PROJECT (HELLO)
find_program (TCLSH tclsh)
if (TCLSH)
add_custom_target(
MyTarget ALL
COMMAND TCLSH myScript.tcl
)
endif (TCLSH)
(1) find_program and if/endif will make the custom target only available under the condition that an executable called tclsh was found.
(2) myScript.tcl is a Tcl script in your project directory.
(3) Running cmake . && make will call out to effectively to: tclsh myScript.tcl, producing:
$cmake . && make
-- Configuring done
-- Generating done
-- Build files have been written to: /tmp/hello
This is a message written by Tcl scripts
Built target MyTarget
This is just to get you started, you will have to read more about adding commands, targets, or executing sub-processes from within CMake.

specify a custom build tool in CMake

I have a simple CMakeLists.txt
add_executable(myexecutable test.cpp)
I don't intend to use CMake to actually indirectly be responsible for building the program. I intend to use it so I can use CLion and have a nice IDE experience for working on my c++ program.
If I hit build in CLion, or cmake --build on the commandline, I want it to invoke my real buildtool. How can I specify a buildtool to run instead of the normal build cmake/clion would do? I have only been able to find add_custom_command which can be run before/after the normal cmake/clion build.
You need some fake target to let CLion understand which files are there in the project. Then you can add add_custom_target with your own build commands. So if you avoid building fake target, but instead run custom targets via run configuration by name (after CMake reload CLion will automatically create run configurations for such targets), they will call build for you.