5

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.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
Alexis Le Provost
  • 1,803
  • 2
  • 11
  • 9
  • Yes it is, but I don't know how. I would search for how people have integrated more common static analysis tools, like `clang-tidy`, and just replace the other tool with `iwyu`. – Ted Lyngmo Mar 18 '20 at 16:59
  • Does the `include-what-you-use` program accept options to force an *error* for each report, instead of a warning? – Kevin Mar 18 '20 at 17:09
  • @squareskittles `include-what-you-use` options isn't that clear hence my question. I think I set properly `cmake` side to treat warnings as errors (I use also `clang-tidy` and it works like a charm on that side). – Alexis Le Provost Mar 18 '20 at 17:32
  • 2
    `include-what-you-use` returns `2` on success so what I've done to integrate it with my (non-`cmake`) build is to wrap it in a little bash script that simply does `/usr/local/bin/include-what-you-use "$@" ; rv=$? ; if [ $rv -eq 2 ]; then rv=0; fi ; exit $rv` – Ted Lyngmo Mar 18 '20 at 18:12
  • You can list the command line options for `include-what-you-use` by providing a `-h` or `--help`, but I don't think there is an option to force an *error*. I think a solution would depend on the type of Makefile generator used. – Kevin Mar 18 '20 at 18:41
  • 1
    @squareskittles No, there is no such option which is why I've wrapped it in a script to convert its exit code for success to the usual `0` for success as I described above. – Ted Lyngmo Mar 19 '20 at 00:03
  • @TedLyngmo Your solution doesn't work properly for build based on cmake – Alexis Le Provost Mar 19 '20 at 12:46
  • @AlexisLeProvost We have integrated `clang-tidy` in our `cmake` builds at work, making the builds fail if `clang-tidy` reports a failure. I don't see my `iwyu` should be any different when using a wrapper like the obove. Too bad I'm not good at `cmake` ... – Ted Lyngmo Mar 19 '20 at 14:23

0 Answers0