1 #------------------------------------------
2 # Manage Include what you use
3 #-------------------------------------------
5 # see https://stackoverflow.com/a/30951493
6 # and https://blog.kitware.com/static-checks-with-cmake-cdash-iwyu-clang-tidy-lwyu-cpplint-and-cppcheck/
8 # Flag CMAKE_CXX_INCLUDE_WHAT_YOU_USE is only available in CMake 3.3 and above
9 if(${CMAKE_VERSION} VERSION_GREATER
"3.3" OR ${CMAKE_VERSION} VERSION_EQUAL
"3.3")
11 option(CAMITK_INCLUDE_WHAT_YOU_USE
"Enable the header analysis on you code, CMake version 3.3 or greater is needed" OFF)
13 if (CAMITK_INCLUDE_WHAT_YOU_USE)
15 find_program(IWYU_EXECUTABLE NAMES
include-what-you-use iwyu)
19 set(IWYU_OUTPUT
"${CMAKE_CURRENT_BINARY_DIR}/iwyu.out" CACHE PATH
"Output filename for include-what-you-use")
21 # set the cmake flag in order to generate include-what-you-use report all the time
22 set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE ${IWYU_EXECUTABLE})
24 #-- define
a target that generate the
include-what-you-use report
for all the project
26 # determine cmake macro path
27 if (NOT EXISTS ${SDK_TOP_LEVEL_SOURCE_DIR})
28 #
this macro is called outside the sdk
29 set(CAMITK_CMAKE_MACRO_PATH ${CAMITK_CMAKE_DIR}/macros/
camitk/test)
30 if(NOT IS_DIRECTORY ${CAMITK_CMAKE_MACRO_PATH})
31 # inside communityedition but not in sdk (modeling or imaging)
32 set(CAMITK_CMAKE_MACRO_PATH ${CMAKE_SOURCE_DIR}/sdk/cmake/modules/macros/
camitk/test)
35 # directly use the macro source dir
36 set(CAMITK_CMAKE_MACRO_PATH ${SDK_TOP_LEVEL_SOURCE_DIR}/cmake/modules/macros/
camitk/test)
39 add_custom_target(
camitk-ce-iwyu
40 COMMAND ${CMAKE_COMMAND} -E echo
"Running include-what-you-use. Results can be found in ${IWYU_OUTPUT}"
41 COMMAND ${CMAKE_COMMAND} --build ${CMAKE_CURRENT_BINARY_DIR} --target clean
42 COMMAND ${CMAKE_COMMAND}
"-DCMAKE_CXX_INCLUDE_WHAT_YOU_USE=${IWYU_EXECUTABLE}" ${CMAKE_SOURCE_DIR}
43 COMMAND ${CMAKE_COMMAND}
44 -DMAKE_ERROR_OUTPUT_FILE=${IWYU_OUTPUT}
45 -P ${CAMITK_CMAKE_MACRO_PATH}/CamiTKMakeWithRedirectedError.cmake
46 COMMAND ${CMAKE_COMMAND}
"-UCMAKE_CXX_INCLUDE_WHAT_YOU_USE" ${CMAKE_SOURCE_DIR}
47 COMMENT
"Running include-what-you-use for all project"
51 message(FATAL_ERROR "Could not find the program
include-what-you-use")
54 unset(CMAKE_CXX_INCLUDE_WHAT_YOU_USE)