1set(IWYU_IMP "${CMAKE_SOURCE_DIR}/cmake/iwyu.imp")
2
3find_program(iwyu_path NAMES include-what-you-use iwyu)
4if(NOT iwyu_path)
5  message(FATAL_ERROR "Could not find the program include-what-you-use.")
6else(NOT iwyu_path)
7  set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE "${iwyu_path}" -Xiwyu --mapping_file=${IWYU_IMP} -Xiwyu --check_also=${CMAKE_SOURCE_DIR}/src/* -Xiwyu --check_also=${CMAKE_SOURCE_DIR}/src/*/* -Xiwyu --check_also=${CMAKE_SOURCE_DIR}/src/*/*/* -Xiwyu --check_also=${CMAKE_SOURCE_DIR}/src/*/*/*/*)
8endif()
9
10find_program(iwyu_tool_path NAMES iwyu_tool iwyu_tool.py)
11if(iwyu_tool_path)
12  add_custom_command(
13    OUTPUT "${CMAKE_BINARY_DIR}/iwyu.log"
14    COMMAND "${iwyu_tool_path}" -v -p "${CMAKE_BINARY_DIR}"
15            -- --mapping_file=${IWYU_IMP} --check_also=${CMAKE_SOURCE_DIR}/src/* --check_also=${CMAKE_SOURCE_DIR}/src/*/* --check_also=${CMAKE_SOURCE_DIR}/src/*/*/* --check_also=${CMAKE_SOURCE_DIR}/src/*/*/*/* 2>
16            "${CMAKE_BINARY_DIR}/iwyu.log"
17    WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
18    COMMENT "Running include-what-you-use tool"
19    VERBATIM
20  )
21  add_custom_target(iwyu
22    DEPENDS "${CMAKE_BINARY_DIR}/iwyu.log"
23    VERBATIM
24  )
25endif()
26
27find_program(fix_includes_path NAMES fix_include fix_includes.py)
28if(fix_includes_path)
29  add_custom_target(iwyu_fix
30    COMMAND "${fix_includes_path}" --noblank_lines --comments
31            --safe_headers < "${CMAKE_BINARY_DIR}/iwyu.log" || true
32    COMMAND ${CMAKE_COMMAND} -E remove "${CMAKE_BINARY_DIR}/iwyu.log"
33    DEPENDS "${CMAKE_BINARY_DIR}/iwyu.log"
34    WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
35    COMMENT "Running include-what-you-use fix_includes tool"
36    VERBATIM
37  )
38endif()
39
40unset(IWYU_IMP)
41