3

I am using CMake 3.6 to generate a ninja 1.6.0 configuration, compile and statically analyze my code using clang-tidy (LLVM version 3.9.1):

# file copied from http://www.labri.fr/perso/fleury/posts/programming/using-clang-tidy-and-clang-format.html

# Additional targets to perform clang-format/clang-tidy
# Get all project files
file(GLOB_RECURSE
     ALL_CXX_SOURCE_FILES
     *.[chi]pp *.[chi]xx *.cc *.hh *.ii *.[CHI]
     )

# Adding clang-tidy target if executable is found
find_program(CLANG_TIDY "clang-tidy")
if(CLANG_TIDY)
  add_custom_target(
    clang-tidy
    COMMAND clang-tidy
    ${ALL_CXX_SOURCE_FILES}
    --
    -std=c++11
    ${INCLUDE_DIRECTORIES}
    )
endif()

.clang-tidy file:

---
Checks:          '-*,readability-*'
AnalyzeTemporaryDtors: false
WarningsAsErrors: '*'
HeaderFilterRegex: 'src/'
User:            mgalos
CheckOptions:
  - key:             readability-identifier-naming.AbstractClassCase
    value:           CamelCase
...

build.bat:

cmake -H. -B_build -GNinja -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DCMAKE_INSTALL_PREFIX=_build/_release -DCMAKE_BUILD_TYPE=GENERIC_RELEASE -DBoost_COMPILER=-vc100

The output I'm getting is:

96 warnings treated as errors ninja: build stopped: subcommand failed.

The %errorlevel% is 0, although ninja clearly says subcommand failed. This variable is important, as it is evaluated by a buildserver and would flag a build as failed.

What can I do to have %errorlevel% correctly set to != 0?

valiano
  • 16,433
  • 7
  • 64
  • 79
Mihai Galos
  • 1,707
  • 1
  • 19
  • 38
  • But the sub-command would be marked as failed if the `errorlevel` is `!=0`. So how to you know it is `0`? – Florian Feb 10 '17 at 09:09
  • The `warnings treated as errors` looks more like `errorlevel` is `!=0`. So either don't treat warnings as errors, use `execute_process` in an external CMake script, call `ninja` with the `-k` "keep going" flag or if you e.g. use Jenkins "Execute Windows batch command" add an `EXIT /B 0` at the end. – Florian Feb 10 '17 at 09:19
  • Florian, I really want to treat warnings as errors because the static analysis needs to adhere to a styleguide (specified in the .clang-tidy), which will be extended. – Mihai Galos Feb 10 '17 at 10:12

2 Answers2

3

Looks like this is a known problem.

What you could do to mitigate, is to pipe all output from cmake to a file and scan that output for occurrences of a string indicating an error, which seems to be build stopped: subcommand failed in your case. If this is present, simply exit with an arbitrary non-zero exit code.

@ECHO OFF
cmake -H. -B_build -GNinja (...) > log.txt 2>&1
FOR /F %%G IN ('FINDSTR /C:"build stopped: subcommand failed" log.txt') DO EXIT /B 255
zb226
  • 9,586
  • 6
  • 49
  • 79
0

Thank you for your support. As suggested, I ended up doing a :

ninja clang-tidy | tee clang-tidy.log
grep 'error.*generated' clang-tidy.log | cut -d" " -f4 | gawk "{if($1 != \"0\")  {print \"-1\"; exit -1}}" > clang-tidy_result.log
set /p ErrorLevel=<clang-tidy_result.log
Mihai Galos
  • 1,707
  • 1
  • 19
  • 38
  • Glad to be of help! Just a side note: [`ERRORLEVEL` is not `%ERRORLEVEL%`](https://blogs.msdn.microsoft.com/oldnewthing/20080926-00/?p=20743). – zb226 Feb 10 '17 at 13:11
  • Yep. %% for evaluation, absent when setting variables in batch. – Mihai Galos Feb 10 '17 at 13:22