Cpplint and CMake - Treat warnings are errors and fail - cmake

I'm using cpplint with CMake:
set(CMAKE_CXX_CPPLINT cpplint;
--filter=-build/include_subdir,-legal/copyright;
--quiet)
But even if cpplint produces some warnings, the build is still successful.
I cannot find a way to treat those warnings as error (similarly to when using -warnings-as-errors for clang-tidy) and fail the build instead.

CMake always ignores the cpplint exit code.
Source code:
https://github.com/Kitware/CMake/blame/master/Source/cmcmd.cxx#L324

Related

CMake equivalent of APP_ALLOW_MISSING_DEPS

I am moving an Android project from ndk to CMake.
In Application.mk we set following flag:
APP_ALLOW_MISSING_DEPS := true
But I couldn't find equivalent cmake flag for this. Can this be set from cmake arguments?
If you need this flag in ndk-build it means your Android.mk is wrong. That flag tells ndk-build to ignore garbage in your build script. It makes unknown dependencies exactly as meaningful as whitespace. The same behavior is achieved by removing the uses of the unknown deps from your Android.mk. ndk-build tells you this in the warning:
Note that old versions of ndk-build silently ignored this error case. If your project worked on those versions, the missing libraries were not needed and you can remove those dependencies from the module to fix your build.
Alternatively, set APP_ALLOW_MISSING_DEPS=true to allow missing dependencies.

How treat `include-what-you-use` warnings as errors with `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.

How do I get a verbose output for CMake?

I would like to investigate why I have this error:
$ cmake ..
-- The C compiler identification is unknown
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /cygdrive/c/Users/Ycr/Home/bin/arm-none-eabi-gcc
-- Check for working C compiler: /cygdrive/c/Users/Ycr/Home/bin/arm-none-eabi-gcc -- broken
CMake Error at /usr/share/cmake-3.6.2/Modules/CMakeTestCCompiler.cmake:61 (message):
The C compiler "/cygdrive/c/Users/Ycr/Home/bin/arm-none-eabi-gcc" is not
able to compile a simple test program.
Unfortunately after the error:
I have no idea of what CMake did. I don't have a verbose log of the command it executed.
The CMakeFiles/cmTC_e4aa4.dir was cleaned after the error, so I have no possibility to explore the issue myself.
How should I investigate such an error?
I tried to use the --debug-trycompile option. This time CMake creates a CMakeTmp folder which makes perfectly without errors. However, I still have this CMakeFiles/cmTC_e4aa4.dir that generates errors and even with the option CMake unlinks the folder.
Getting a Verbose Log
The try_compile() calls that CMake does in the beginning to test the compiler, gives a detailed error output on the console and writes it to
[your binary output directory]/CMakeFiles/CMakeError.log
I've checked the source code again and there is no CMake option that would give more a more detailed output for CMake's internal try_compile() calls.
You could just force the output to standard output by adding some variable_watch() calls to your main CMakeLists.txt before your project() call like:
variable_watch(__CMAKE_C_COMPILER_OUTPUT)
variable_watch(__CMAKE_CXX_COMPILER_OUTPUT)
Keeping the Temporary Files
To keep the temporary file of try_compile, add --debug-trycompile to the cmake command line.
But be aware that the multiple compiler tests at the beginning overwrite the artifacts of previous ones:
It may however change the results of the try-compiles as old junk from a previous try-compile may cause a different test to either pass or fail incorrectly. This option is best used for one try-compile at a time, and only when debugging.
References
How to keep generated temporary files?
CMake error at CMakeLists.txt:30 (project): No CMAKE_C_COMPILER could be found
For me, none of the log files in my output directory contained useful information from try_compile(), even when using --debug-trycompile.
I ended up using the OUTPUT_VARIABLE option to capture and then print the output like this:
try_compile(<options> OUTPUT_VARIABLE TRY_COMPILE_OUTPUT)
message(WARNING ${TRY_COMPILE_OUTPUT})

Report target failure in addition to translation unit compilation failure

Suppose I have hundreds of targets and some of them are not critical for the build to succeed (and for example I am using --keep-going on make or -k 9000 on ninja) and I need to figure out which cmake targets failed.
With add_custom_command() a post-build command can be added to a cmake target that prints its name like this:
success: myTarget.dll
But what about failures?
If linking fails then I can parse the verbose output of whatever I am using (ninja/make/msbuild) and see which target has failed.
However if compilation of a translation unit fails the only error I get is that a particular source file does not compile and figuring out which cmake target exactly has failed is harder.
The only thing I have come up with is running this after the build has failed: ninja -nv which will make a verbose dry run and I can intercept the link commands and parse the cmake targets that have failed that way...
Any other ideas?
I ended up using dry runs of make/ninja and parsing their output

cmake_parse_arguments missing on CMake re-run

I am trying to write a function which uses cmake_parse_arguments to interpret the parameters. This works fine when running cmake in a fresh build directory. However, any time I try to re-run cmake, CMake seems to believe the function does not exist:
CMake Error at CMakeLists.txt:6 (CMAKE_PARSE_ARGUMENTS):
Unknown CMake command "CMAKE_PARSE_ARGUMENTS".
I feel like I'm missing something obvious. I am using CMake 3.3.2 on Arch Linux.
Include CMakeParseArguments with
include(CMakeParseArguments)
Probably it worked the first time because other Find* files included the file already. As the result of the Find* files are cached and not re-run, they no longer included CMakeParseArguments.