build: IWYU support added

This commit is contained in:
Mateusz Pusz
2021-03-30 13:15:47 +02:00
parent 5251a8a241
commit 01935996e6
8 changed files with 194 additions and 15 deletions

7
.mp-units.imp Normal file
View File

@ -0,0 +1,7 @@
[
{ include: ["<functional>", "public", "<algorithm>", "public"] },
{ include: ["<string>", "public", "<fmt/format.h>", "public"] },
{ include: ["<memory>", "public", "<catch2/catch.hpp>", "public"] },
{ include: ["<initializer_list>", "public", "<linear_algebra.hpp>", "public"] },
{ symbol: ["abs", "private", "<cmath>", public]}
]

View File

@ -34,9 +34,23 @@ set_warnings()
# set all contracts checking in a Debug build
add_compile_definitions($<$<CONFIG:Debug>:gsl_CONFIG_CONTRACT_CHECKING_AUDIT>)
# enable static analysis
# enable include-what-you-use
option(UNITS_IWYU "Enables include-what-you-use" OFF)
if(UNITS_IWYU)
include(include-what-you-use)
set_iwyu(
MAPPING_FILE "${PROJECT_SOURCE_DIR}/.mp-units.imp"
NO_FORWARD_DECLARATIONS
QUOTED_INCLUDES_FIRST
MAX_LINE_LENGTH 120
NO_COMMENTS
)
if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
set(UNITS_AS_SYSTEM_HEADERS ON)
endif()
endif()
#enable_clang_tidy()
#enable_iwyu()
# add project code
add_subdirectory(src)

View File

@ -0,0 +1,142 @@
# The MIT License (MIT)
#
# Copyright (c) 2017 Mateusz Pusz
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
cmake_minimum_required(VERSION 3.7)
if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "Clang")
# run IWYU only for clang compiler
# other compilers will generate i.e. unknown compilation flags warnings
return()
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(WARNING "'include-what-you-use' executable not found!")
return()
endif()
endmacro()
macro(_iwyu_args_append arg)
list(APPEND _iwyu_args "-Xiwyu" "${arg}")
endmacro()
macro(_iwyu_args_append_if_present option arg)
if(_set_iwyu_${option})
_iwyu_args_append("${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)
set(_multi_value_args KEEP)
cmake_parse_arguments(PARSE_ARGV ${offset} _set_iwyu "${_options}" "${_one_value_args}" "${_multi_value_args}")
# validate and process arguments
if(${prefix}_UNPARSED_ARGUMENTS)
message(FATAL_ERROR "Invalid arguments '${${prefix}_UNPARSED_ARGUMENTS}'")
endif()
if(_set_iwyu_KEEP)
foreach(_pattern ${_set_iwyu_KEEP})
_iwyu_args_append("--keep=${_pattern}")
endforeach()
endif()
if(_set_iwyu_MAPPING_FILE)
if(NOT EXISTS ${_set_iwyu_MAPPING_FILE})
message(FATAL_ERROR "IWYU: Mapping file '${_set_iwyu_MAPPING_FILE}' does not exist")
endif()
_iwyu_args_append("--mapping_file=${_set_iwyu_MAPPING_FILE}")
endif()
if(_set_iwyu_MAX_LINE_LENGTH)
if(NOT _set_iwyu_MAX_LINE_LENGTH GREATER 0)
message(FATAL_ERROR "IWYU: Invalid MAX_LINE_LENGTH value = '${_set_iwyu_MAX_LINE_LENGTH}")
endif()
_iwyu_args_append("--max_line_length=${_set_iwyu_MAX_LINE_LENGTH}")
endif()
_iwyu_args_append_if_present(NO_DEFAULT_MAPPINGS "--no_default_mappings")
_iwyu_args_append_if_present(PCH_IN_CODE "--pch_in_code")
_iwyu_args_append_if_present(TRANSITIVE_INCLUDES_ONLY "--transitive_includes_only")
_iwyu_args_append_if_present(NO_COMMENTS "--no_comments")
_iwyu_args_append_if_present(NO_FORWARD_DECLARATIONS "--no_fwd_decls")
_iwyu_args_append_if_present(CXX17_NAMESPACES "--cxx17ns")
_iwyu_args_append_if_present(QUOTED_INCLUDES_FIRST "--quoted_includes_first")
_iwyu_args_append("--verbose=${IWYU_VERBOSITY_LEVEL}")
endmacro()
#
# set_target_iwyu(TargetName
# [KEEP pattern...]
# [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(set_target_iwyu target)
_find_iwyu()
_process_iwyu_arguments(1)
message(STATUS "Setting include-what-you-use for '${target}'")
message(STATUS " Path: ${_iwyu_path}")
message(STATUS " Arguments: ${_iwyu_args}")
message(STATUS "Setting include-what-you-use for '${target}' - done")
set_target_properties(${target} PROPERTIES
CXX_INCLUDE_WHAT_YOU_USE "${_iwyu_path};${_iwyu_args}"
)
endfunction()
#
# set_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(set_iwyu)
_find_iwyu()
_process_iwyu_arguments(0)
message(STATUS "Setting include-what-you-use")
message(STATUS " Path: ${_iwyu_path}")
message(STATUS " Arguments: ${_iwyu_args}")
message(STATUS "Setting include-what-you-use - done")
set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE "${_iwyu_path};${_iwyu_args}" PARENT_SCOPE)
endfunction()

View File

@ -20,7 +20,7 @@
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
cmake_minimum_required(VERSION 3.2)
cmake_minimum_required(VERSION 3.3)
macro(enable_clang_tidy)
find_program(clang_tidy_cmd NAMES "clang-tidy")
@ -33,13 +33,3 @@ macro(enable_clang_tidy)
set(CMAKE_CXX_CLANG_TIDY "${clang_tidy_cmd}")
endif()
endmacro()
macro(enable_iwyu)
find_program(iwyu_cmd NAMES "include-what-you-use")
if(NOT iwyu_cmd)
message(WARNING "include-what-you-use not found!")
else()
set(CMAKE_CXX_INCLUDE_WHAT_YOU_USE "${iwyu_cmd}")
endif()
endmacro()

View File

@ -159,6 +159,16 @@ UNITS_DOWNCAST_MODE
Equivalent to `downcast_mode`_.
UNITS_AS_SYSTEM_HEADERS
+++++++++++++++++++++++
**Values**: ``ON``/``OFF``
**Defaulted to**: ``OFF``
Exports library as system headers.
UNITS_BUILD_DOCS
++++++++++++++++
@ -169,6 +179,17 @@ UNITS_BUILD_DOCS
Enables project documentation generation.
UNITS_IWYU
++++++++++
**Values**: ``ON``/``OFF``
**Defaulted to**: ``OFF``
Enables include-what-you-use when compiling with a clang compiler.
Additionally turns on `UNITS_AS_SYSTEM_HEADERS`.
Installation and Reuse
----------------------

View File

@ -26,6 +26,11 @@ project(mp-units
LANGUAGES CXX
)
option(UNITS_AS_SYSTEM_HEADERS "Exports library as system headers" OFF)
if(UNITS_AS_SYSTEM_HEADERS)
set(units_as_system SYSTEM)
endif()
list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake")
include(AddUnitsModule)

View File

@ -28,7 +28,7 @@ cmake_minimum_required(VERSION 3.15)
function(add_units_module name)
add_library(mp-units-${name} INTERFACE)
target_link_libraries(mp-units-${name} INTERFACE ${ARGN})
target_include_directories(mp-units-${name} INTERFACE
target_include_directories(mp-units-${name} ${units_as_system} INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)

View File

@ -39,7 +39,7 @@ target_compile_features(mp-units-core INTERFACE cxx_std_20)
target_link_libraries(mp-units-core INTERFACE
gsl::gsl-lite
)
target_include_directories(mp-units-core INTERFACE
target_include_directories(mp-units-core ${units_as_system} INTERFACE
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>
)