build: PROGRAM, QUIET, and REQUIRED options added for enable_iwyu()

This commit is contained in:
Mateusz Pusz
2021-05-09 14:43:32 +02:00
parent cb5371dec9
commit c1cf800bd1

View File

@ -30,14 +30,6 @@ endif()
set(IWYU_VERBOSITY_LEVEL 3 CACHE STRING "IWYU verbosity level (the higher the level, the more output)")
macro(_find_iwyu)
find_program(_iwyu_path NAMES "include-what-you-use")
if(NOT _iwyu_path)
message(FATAL_ERROR "'include-what-you-use' executable not found!")
return()
endif()
endmacro()
macro(_iwyu_args_append arg)
list(APPEND _iwyu_args "-Xiwyu" "${arg}")
endmacro()
@ -48,9 +40,16 @@ macro(_iwyu_args_append_if_present option arg)
endif()
endmacro()
macro(_process_iwyu_arguments offset)
set(_options NO_DEFAULT_MAPPINGS PCH_IN_CODE TRANSITIVE_INCLUDES_ONLY NO_COMMENTS NO_FORWARD_DECLARATIONS CXX17_NAMESPACES QUOTED_INCLUDES_FIRST)
set(_one_value_args MAPPING_FILE MAX_LINE_LENGTH)
macro(_enable_iwyu_failed log_postfix)
if(NOT _enable_iwyu_QUIET)
message(STATUS "Enabling include-what-you-use${log_postfix} - failed")
endif()
return()
endmacro()
macro(_process_iwyu_arguments offset log_postfix)
set(_options QUIET REQUIRED NO_DEFAULT_MAPPINGS PCH_IN_CODE TRANSITIVE_INCLUDES_ONLY NO_COMMENTS NO_FORWARD_DECLARATIONS CXX17_NAMESPACES QUOTED_INCLUDES_FIRST)
set(_one_value_args PROGRAM MAPPING_FILE MAX_LINE_LENGTH)
set(_multi_value_args KEEP)
cmake_parse_arguments(PARSE_ARGV ${offset} _enable_iwyu "${_options}" "${_one_value_args}" "${_multi_value_args}")
@ -63,6 +62,32 @@ macro(_process_iwyu_arguments offset)
message(FATAL_ERROR "No value provided for '${_enable_iwyu_KEYWORDS_MISSING_VALUES}'")
endif()
if(NOT _enable_iwyu_QUIET)
message(STATUS "Enabling include-what-you-use${log_postfix}")
endif()
if(${_enable_iwyu_REQUIRED})
set(_error_log_level FATAL_ERROR)
elseif(NOT _enable_iwyu_QUIET)
set(_error_log_level STATUS)
endif()
if(NOT DEFINED _enable_iwyu_PROGRAM)
set(_enable_iwyu_PROGRAM include-what-you-use)
endif()
find_program(IWYU_PATH ${_enable_iwyu_PROGRAM})
if(IWYU_PATH)
if(NOT _enable_iwyu_QUIET)
message(STATUS " Executable: ${IWYU_PATH}")
endif()
else()
if(DEFINED _error_log_level)
message(${_error_log_level} " '${_enable_iwyu_PROGRAM}' executable was not found")
endif()
_enable_iwyu_failed("${log_postfix}")
endif()
if(_enable_iwyu_KEEP)
foreach(_pattern ${_enable_iwyu_KEEP})
_iwyu_args_append("--keep=${_pattern}")
@ -92,10 +117,36 @@ macro(_process_iwyu_arguments offset)
_iwyu_args_append_if_present(QUOTED_INCLUDES_FIRST "--quoted_includes_first")
_iwyu_args_append("--verbose=${IWYU_VERBOSITY_LEVEL}")
if(NOT _enable_iwyu_QUIET)
message(STATUS " Arguments: ${_iwyu_args}")
message(STATUS "Enabling include-what-you-use${log_postfix} - done")
endif()
endmacro()
#
# enable_iwyu([PROGRAM] # include-what-you-use by default
# [QUIET] [REQUIRED]
# [KEEP patterns...]
# [MAPPING_FILE file]
# [NO_DEFAULT_MAPPINGS]
# [PCH_IN_CODE]
# [TRANSITIVE_INCLUDES_ONLY]
# [MAX_LINE_LENGTH length]
# [NO_COMMENTS]
# [NO_FORWARD_DECLARATIONS]
# [CXX17_NAMESPACES]
# [QUOTED_INCLUDES_FIRST])
#
function(enable_iwyu)
_process_iwyu_arguments(0 "")
set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE "${_iwyu_path};${_iwyu_args}" PARENT_SCOPE)
endfunction()
#
# enable_target_iwyu(TargetName
# [PROGRAM] # include-what-you-use by default
# [QUIET] [REQUIRED]
# [KEEP pattern...]
# [MAPPING_FILE file]
# [NO_DEFAULT_MAPPINGS]
@ -108,39 +159,8 @@ endmacro()
# [QUOTED_INCLUDES_FIRST])
#
function(enable_target_iwyu target)
_find_iwyu()
_process_iwyu_arguments(1)
message(STATUS "Enabling include-what-you-use for '${target}'")
message(STATUS " Path: ${_iwyu_path}")
message(STATUS " Arguments: ${_iwyu_args}")
message(STATUS "Enabling include-what-you-use for '${target}' - done")
_process_iwyu_arguments(1 " for '${target}'")
set_target_properties(${target} PROPERTIES
CXX_INCLUDE_WHAT_YOU_USE "${_iwyu_path};${_iwyu_args}"
)
endfunction()
#
# enable_target_iwyu([KEEP patterns...]
# [MAPPING_FILE file]
# [NO_DEFAULT_MAPPINGS]
# [PCH_IN_CODE]
# [TRANSITIVE_INCLUDES_ONLY]
# [MAX_LINE_LENGTH length]
# [NO_COMMENTS]
# [NO_FORWARD_DECLARATIONS]
# [CXX17_NAMESPACES]
# [QUOTED_INCLUDES_FIRST])
#
function(enable_iwyu)
_find_iwyu()
_process_iwyu_arguments(0)
message(STATUS "Enabling include-what-you-use")
message(STATUS " Path: ${_iwyu_path}")
message(STATUS " Arguments: ${_iwyu_args}")
message(STATUS "Enabling include-what-you-use - done")
set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE "${_iwyu_path};${_iwyu_args}" PARENT_SCOPE)
endfunction()