From d03d7291e4f1c4cbf8eb441c37f4f636c38a8953 Mon Sep 17 00:00:00 2001 From: Mateusz Pusz Date: Sun, 6 Sep 2020 10:23:31 +0200 Subject: [PATCH] Removed cmake/common submodule --- .gitmodules | 3 - CMakeLists.txt | 2 +- cmake/common | 1 - cmake/common/conan.cmake | 46 ++++++++++++ cmake/common/install.cmake | 81 ++++++++++++++++++++ cmake/common/scripts.cmake | 28 +++++++ cmake/common/simple-config.cmake.in | 1 + cmake/common/static_analysis.cmake | 44 +++++++++++ cmake/common/warnings.cmake | 110 ++++++++++++++++++++++++++++ docs/usage.rst | 6 +- src/CMakeLists.txt | 4 +- 11 files changed, 316 insertions(+), 10 deletions(-) delete mode 100644 .gitmodules delete mode 160000 cmake/common create mode 100644 cmake/common/conan.cmake create mode 100644 cmake/common/install.cmake create mode 100644 cmake/common/scripts.cmake create mode 100644 cmake/common/simple-config.cmake.in create mode 100644 cmake/common/static_analysis.cmake create mode 100644 cmake/common/warnings.cmake diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index cafa8d43..00000000 --- a/.gitmodules +++ /dev/null @@ -1,3 +0,0 @@ -[submodule "cmake/common"] - path = cmake/common - url = https://github.com/mpusz/cmake-scripts.git diff --git a/CMakeLists.txt b/CMakeLists.txt index 08831032..b18a30a9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,7 +27,7 @@ project(mp-units) list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake") # include common tools and workarounds -include(common/cmake/scripts) +include(common/scripts) # use Conan configuration if available conan_init(cmake) diff --git a/cmake/common b/cmake/common deleted file mode 160000 index b951a327..00000000 --- a/cmake/common +++ /dev/null @@ -1 +0,0 @@ -Subproject commit b951a32791923f22f9992fc297c111e472c93a4f diff --git a/cmake/common/conan.cmake b/cmake/common/conan.cmake new file mode 100644 index 00000000..6219e05c --- /dev/null +++ b/cmake/common/conan.cmake @@ -0,0 +1,46 @@ +# 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. + + +# Helper to use conan generated configuration if provided +macro(conan_init generator) + if(${generator} STREQUAL "cmake_paths") + include(${CMAKE_BINARY_DIR}/conan_paths.cmake OPTIONAL) + elseif(${generator} STREQUAL "cmake") + if(NOT DEFINED CONAN_PACKAGE_NAME) + if(EXISTS ${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) + include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) + conan_basic_setup(TARGETS) + endif() + endif() + else() + message(FATAL_ERROR "Unknown Conan generator: ${generator}") + endif() +endmacro() + + +# Checks if conan installed testing dependencies +macro(conan_check_testing test_framework) + if(NOT TARGET CONAN_PKG::${test_framework}) + message(FATAL_ERROR "CONAN_PKG::${test_framework} not found!\nPlease run `conan install` with `-e CONAN_RUN_TESTS=True`.") + endif() +endmacro() diff --git a/cmake/common/install.cmake b/cmake/common/install.cmake new file mode 100644 index 00000000..49f9c7e1 --- /dev/null +++ b/cmake/common/install.cmake @@ -0,0 +1,81 @@ +# 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. + + +# A path to scripts directory +set(CMAKE_SCRIPTS_ROOT ${CMAKE_CURRENT_LIST_DIR}) + +# Install provided targets +function(install_targets) + if(NOT CMAKE_INSTALL_BINDIR) + set(CMAKE_INSTALL_BINDIR "bin") + endif() + if(NOT CMAKE_INSTALL_LIBDIR) + set(CMAKE_INSTALL_LIBDIR "lib") + endif() + if(NOT CMAKE_INSTALL_INCLUDEDIR) + set(CMAKE_INSTALL_INCLUDEDIR "include") + endif() + install(TARGETS ${ARGN} + EXPORT ${PROJECT_NAME}Targets + RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} # TODO Remove when CMAKE 3.14 + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} # TODO Remove when CMAKE 3.14 + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} # TODO Remove when CMAKE 3.14 + PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} + INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} + ) +endfunction() + +# Generate configuration files and install the package +function(configure_and_install configure_in_file_path namespace version_compare_rules) + if(NOT APPLE) + set(CMAKE_INSTALL_RPATH ${ORIGIN}) + endif() + + # prepare installation files + include(CMakePackageConfigHelpers) + set(ConfigPackageSource ${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}) + set(ConfigPackageDestination lib/cmake/${PROJECT_NAME}) + write_basic_package_version_file( + ${ConfigPackageSource}/${PROJECT_NAME}-config-version.cmake + COMPATIBILITY ${version_compare_rules}) + configure_package_config_file(${configure_in_file_path} + ${ConfigPackageSource}/${PROJECT_NAME}-config.cmake + INSTALL_DESTINATION ${ConfigPackageDestination}) + + # install library + install(EXPORT ${PROJECT_NAME}Targets + DESTINATION ${ConfigPackageDestination} + FILE ${PROJECT_NAME}-targets.cmake + NAMESPACE ${namespace}:: + COMPONENT Devel) + install(FILES + "${ConfigPackageSource}/${PROJECT_NAME}-config.cmake" + "${ConfigPackageSource}/${PROJECT_NAME}-config-version.cmake" + DESTINATION ${ConfigPackageDestination} + COMPONENT Devel) + + # local package + export(EXPORT ${PROJECT_NAME}Targets + NAMESPACE ${namespace}:: + FILE ${ConfigPackageSource}/${PROJECT_NAME}-targets.cmake) +endfunction() diff --git a/cmake/common/scripts.cmake b/cmake/common/scripts.cmake new file mode 100644 index 00000000..b3ea4224 --- /dev/null +++ b/cmake/common/scripts.cmake @@ -0,0 +1,28 @@ +# 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. + +list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}") + +include(conan) +include(install) +include(static_analysis) +include(warnings) diff --git a/cmake/common/simple-config.cmake.in b/cmake/common/simple-config.cmake.in new file mode 100644 index 00000000..f40ebfa8 --- /dev/null +++ b/cmake/common/simple-config.cmake.in @@ -0,0 +1 @@ +include("${CMAKE_CURRENT_LIST_DIR}/@PROJECT_NAME@-targets.cmake") diff --git a/cmake/common/static_analysis.cmake b/cmake/common/static_analysis.cmake new file mode 100644 index 00000000..99c2c9f2 --- /dev/null +++ b/cmake/common/static_analysis.cmake @@ -0,0 +1,44 @@ +# 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. + + +macro(enable_clang_tidy) + find_program(clang_tidy_cmd NAMES "clang-tidy") + if(NOT clang_tidy_cmd) + message(WARNING "clang-tidy not found!") + else() + if(NOT EXISTS "${CMAKE_SOURCE_DIR}/.clang-tidy") + message(FATAL_ERROR "'${CMAKE_SOURCE_DIR}/.clang-tidy' configuration file not found!") + endif() + 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() diff --git a/cmake/common/warnings.cmake b/cmake/common/warnings.cmake new file mode 100644 index 00000000..c60a5c8c --- /dev/null +++ b/cmake/common/warnings.cmake @@ -0,0 +1,110 @@ +# The MIT License (MIT) +# +# Copyright (c) 2016 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. + +# Based on https://github.com/lefticus/cpp_starter_project/blob/master/cmake/CompilerWarnings.cmake + +# Configure compiler warning level +function(set_warnings target) + option(WARNINGS_AS_ERRORS "Treat compiler warnings as errors" TRUE) + + if(NOT TARGET ${target}) + message(FATAL_ERROR "'${target}' is not a CMake target") + endif() + + set(MSVC_WARNINGS + /W4 # Baseline reasonable warnings + /w14062 # enumerator 'identifier' in a switch of enum 'enumeration' is not handled + /w14242 # 'identifier': conversion from 'type1' to 'type1', possible loss of data + /w14254 # 'operator': conversion from 'type1:field_bits' to 'type2:field_bits', possible loss of data + /w14263 # 'function': member function does not override any base class virtual member function + /w14265 # 'classname': class has virtual functions, but destructor is not + # virtual instances of this class may not be destructed correctly + /w14266 # 'function': no override available for virtual member function from base 'type'; function is hidden + /w14287 # 'operator': unsigned/negative constant mismatch + /we4289 # nonstandard extension used: 'variable': loop control variable + # declared in the for-loop is used outside the for-loop scope + /w14296 # 'operator': expression is always 'boolean_value' + /w14311 # 'variable': pointer truncation from 'type1' to 'type2' + /w14545 # expression before comma evaluates to a function which is missing + # an argument list + /w14546 # function call before comma missing argument list + /w14547 # 'operator': operator before comma has no effect; expected + # operator with side-effect + /w14549 # 'operator': operator before comma has no effect; did you intend + # 'operator'? + /w14555 # expression has no effect; expected expression with side- effect + /w14619 # pragma warning: there is no warning number 'number' + /w14640 # Enable warning on thread un-safe static member initialization + /w14826 # Conversion from 'type1' to 'type_2' is sign-extended. This may + # cause unexpected runtime behavior. + /w14905 # wide string literal cast to 'LPSTR' + /w14906 # string literal cast to 'LPWSTR' + /w14928 # illegal copy-initialization; more than one user-defined + # conversion has been implicitly applied + /permissive- # standards conformance mode for MSVC compiler. + ) + + set(CLANG_WARNINGS + -Wall + -Wextra # reasonable and standard + -Wpedantic # warn if non-standard C++ is used + -Wshadow # warn the user if a variable declaration shadows one from a parent context + -Wnon-virtual-dtor # warn the user if a class with virtual functions has a + # non-virtual destructor. This helps catch hard to + # track down memory errors + -Wold-style-cast # warn for c-style casts + -Wcast-align # warn for potential performance problem casts + -Wunused # warn on anything being unused + -Woverloaded-virtual # warn if you overload (not override) a virtual function + -Wcast-qual # warn on dropping const or volatile qualifiers + -Wconversion # warn on type conversions that may lose data + -Wsign-conversion # warn on sign conversions + -Wnull-dereference # warn if a null dereference is detected + -Wdouble-promotion # warn if float is implicit promoted to double + -Wformat=2 # warn on security issues around functions that format output (ie printf) + ) + + if(WARNINGS_AS_ERRORS) + set(CLANG_WARNINGS ${CLANG_WARNINGS} -Werror) + set(MSVC_WARNINGS ${MSVC_WARNINGS} /WX) + endif() + + set(GCC_WARNINGS + ${CLANG_WARNINGS} + -Wmisleading-indentation # warn if indentation implies blocks where blocks do not exist + -Wduplicated-cond # warn if if / else chain has duplicated conditions + -Wduplicated-branches # warn if if / else branches have duplicated code + -Wlogical-op # warn about logical operations being used where bitwise were probably wanted + ) + + if(MSVC) + string(REGEX REPLACE "/W[0-4]" "" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" PARENT_SCOPE) + target_compile_options(${target} INTERFACE ${MSVC_WARNINGS}) + elseif(CMAKE_CXX_COMPILER_ID MATCHES ".*Clang") + target_compile_options(${target} INTERFACE ${CLANG_WARNINGS}) + elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") + target_compile_options(${target} INTERFACE ${GCC_WARNINGS}) + else() + message(AUTHOR_WARNING "No compiler warnings set for '${CMAKE_CXX_COMPILER_ID}' compiler.") + endif() +endfunction() diff --git a/docs/usage.rst b/docs/usage.rst index f11d87bf..f7b703b4 100644 --- a/docs/usage.rst +++ b/docs/usage.rst @@ -80,7 +80,7 @@ in *~/.conan/profile* directory. An example profile can look as follows: arch=x86_64 arch_build=x86_64 compiler=gcc - compiler.version=9 + compiler.version=10 compiler.cppstd=20 compiler.libcxx=libstdc++11 build_type=Release @@ -89,8 +89,8 @@ in *~/.conan/profile* directory. An example profile can look as follows: [build_requires] [env] - CC=/usr/bin/gcc-9 - CXX=/usr/bin/g++-9 + CC=/usr/bin/gcc-10 + CXX=/usr/bin/g++-10 .. tip:: diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 23332d47..832cc02c 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -31,7 +31,7 @@ project(mp-units list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../cmake") # include common tools and workarounds -include(common/cmake/scripts) +include(common/scripts) # use Conan configuration if available conan_init(cmake) @@ -103,4 +103,4 @@ install(DIRECTORY include/units ) # generate configuration files and install the package -configure_and_install(../cmake/common/cmake/simple-config.cmake.in mp-units SameMajorVersion) +configure_and_install(../cmake/common/simple-config.cmake.in mp-units SameMajorVersion)