tt4g | e13b351 | 2020-03-02 09:40:28 +0900 | [diff] [blame] | 1 | cmake_minimum_required(VERSION 3.2) |
Marco Poletti | 50e5afc | 2017-03-12 16:27:47 +0000 | [diff] [blame] | 2 | |
Marco Poletti | 29c9fd2 | 2020-07-12 10:02:58 -0700 | [diff] [blame] | 3 | project(Fruit VERSION 3.6.0 LANGUAGES CXX) |
tt4g | b23fa35 | 2020-03-01 15:44:32 +0900 | [diff] [blame] | 4 | |
tt4g | 7d72dce | 2020-03-08 11:42:12 +0900 | [diff] [blame] | 5 | set(FRUIT_IS_BEING_BUILT_BY_CONAN FALSE CACHE BOOL "This is set in Conan builds.") |
| 6 | if("${FRUIT_IS_BEING_BUILT_BY_CONAN}") |
| 7 | include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake) |
| 8 | conan_basic_setup() |
| 9 | endif() |
| 10 | |
Marco Poletti | 50e5afc | 2017-03-12 16:27:47 +0000 | [diff] [blame] | 11 | if (POLICY CMP0054) |
| 12 | cmake_policy(SET CMP0054 NEW) |
| 13 | endif() |
Marco Poletti | cf798fa | 2014-06-21 15:05:15 +0100 | [diff] [blame] | 14 | |
Marco Poletti | 074c1a3 | 2020-03-09 02:45:34 -0700 | [diff] [blame] | 15 | if ("${CMAKE_CXX_STANDARD}" STREQUAL "") |
| 16 | set(CMAKE_CXX_STANDARD 11) |
| 17 | endif() |
tt4g | e85c88e | 2020-03-07 12:46:03 +0900 | [diff] [blame] | 18 | set(CMAKE_CXX_STANDARD_REQUIRED ON) |
| 19 | |
Marco Poletti | eb92f9a | 2015-12-01 19:23:08 +0100 | [diff] [blame] | 20 | # CMake on OSX likes to see this set explicitly, otherwise it outputs a warning. |
| 21 | set(CMAKE_MACOSX_RPATH 1) |
| 22 | |
poletti-marco | c7cfd02 | 2017-03-27 19:03:42 +0100 | [diff] [blame] | 23 | if(NOT "${CMAKE_BUILD_TYPE}" MATCHES "^(Debug|Release|RelWithDebInfo|MinSizeRel)$") |
| 24 | message(FATAL_ERROR "Please re-run CMake, specifying -DCMAKE_BUILD_TYPE=Debug , -DCMAKE_BUILD_TYPE=Release , -DCMAKE_BUILD_TYPE=RelWithDebInfo or -DCMAKE_BUILD_TYPE=MinSizeRel .") |
Marco Poletti | cf798fa | 2014-06-21 15:05:15 +0100 | [diff] [blame] | 25 | endif() |
| 26 | |
Marco Poletti | dc9a191 | 2017-03-19 15:26:04 +0000 | [diff] [blame] | 27 | option(BUILD_SHARED_LIBS "Build shared library" ON) |
Marco Poletti | 905dc09 | 2017-03-12 15:54:31 +0000 | [diff] [blame] | 28 | |
| 29 | # The Visual Studio CMake generators default to multiple configurations, but Fruit doesn't support multi-configuration build directories. |
| 30 | set(CMAKE_CONFIGURATION_TYPES "${CMAKE_BUILD_TYPE}") |
| 31 | |
Marco Poletti | 050763e | 2017-06-11 18:38:16 +0100 | [diff] [blame] | 32 | set(FRUIT_ADDITIONAL_CXX_FLAGS "" CACHE STRING "Additional CXX compiler flags." FORCE) |
| 33 | |
| 34 | set(FRUIT_ADDITIONAL_COMPILE_FLAGS "${FRUIT_ADDITIONAL_CXX_FLAGS}") |
| 35 | |
Marco Poletti | 905dc09 | 2017-03-12 15:54:31 +0000 | [diff] [blame] | 36 | if(NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES "^(GNU|Clang|AppleClang|MSVC)$") |
Marco Poletti | 66906ac | 2016-01-30 16:12:40 +0000 | [diff] [blame] | 37 | message(WARNING "Compiler not officially supported: ${CMAKE_CXX_COMPILER_ID}") |
| 38 | # Full list of possible values at https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_COMPILER_ID.html . |
| 39 | # Major compilers not currently supported: |
Marco Poletti | 66906ac | 2016-01-30 16:12:40 +0000 | [diff] [blame] | 40 | # * "Intel": not supported ATM due to compiler bugs: |
| 41 | # - https://software.intel.com/en-us/forums/intel-c-compiler/topic/606048 |
| 42 | # - https://software.intel.com/en-us/forums/intel-c-compiler/topic/606049 |
| 43 | endif() |
| 44 | |
| 45 | if("${CMAKE_CXX_COMPILER_ID}" MATCHES "^(GNU|Clang|Intel|AppleClang)$") |
Marco Poletti | c6b2e6e | 2017-03-18 18:04:47 +0000 | [diff] [blame] | 46 | set(FRUIT_ADDITIONAL_COMPILE_FLAGS "${FRUIT_ADDITIONAL_COMPILE_FLAGS} -std=c++11 -W -Wall -Wno-unknown-warning-option -Wno-missing-braces") |
Marco Poletti | 66906ac | 2016-01-30 16:12:40 +0000 | [diff] [blame] | 47 | elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "^(MSVC)$") |
Marco Poletti | 905dc09 | 2017-03-12 15:54:31 +0000 | [diff] [blame] | 48 | # TODO: we currently disable the warning C4709 because MSVC emits it even when there is no reason to. Re-enable it when possible. |
Marco Poletti | 98a444c | 2017-05-20 15:52:17 +0100 | [diff] [blame] | 49 | # TODO: the warning C4141 is disabled, because MSVC emits it ("'inline': used more than once") when a function/method is marked with both __forceinline and inline. |
Marco Poletti | 35805fa | 2017-05-20 16:17:48 +0100 | [diff] [blame] | 50 | # TODO: the warning C4714 is disabled, MSVC emits it when it decides not to inline a __forceinline function/method. |
Marco Poletti | 8fdeba7 | 2017-07-08 19:55:32 +0100 | [diff] [blame] | 51 | # The warning C4577 is disabled because we don't need a termination guarantee on exceptions for functions marked with |
| 52 | # 'noexcept'. |
Marco Poletti | 3ef0301 | 2017-07-08 20:30:32 +0100 | [diff] [blame] | 53 | # The warning C4530 is disabled because it's triggered by MSVC's STL. |
Marco Poletti | c1f47b9 | 2020-03-29 16:23:38 -0700 | [diff] [blame] | 54 | # The warning C5205 is disabled, MSVC emits it for abstract classes in example code with non-virtual destructors, but we never call delete on those (even though std::default_delete<Scaler> is instantiated for those types). |
| 55 | set(FRUIT_ADDITIONAL_COMPILE_FLAGS "${FRUIT_ADDITIONAL_COMPILE_FLAGS} /nologo /FS /W4 /wd4324 /wd4709 /wd4459 /wd4141 /wd4714 /wd4577 /wd4530 /wd5205 /D_SCL_SECURE_NO_WARNINGS") |
Marco Poletti | 66906ac | 2016-01-30 16:12:40 +0000 | [diff] [blame] | 56 | endif() |
| 57 | |
Marco Poletti | 07cdd35 | 2017-07-13 19:42:13 +0100 | [diff] [blame] | 58 | option(FRUIT_ENABLE_COVERAGE "Enable collection of test coverage. This is meant to be used by Fruit developers. It's only supported when using GCC on Linux." OFF) |
| 59 | if("${FRUIT_ENABLE_COVERAGE}") |
| 60 | # We also disable exceptions because otherwise GCC considers every function/method call that could throw as an |
| 61 | # additional branch. |
| 62 | set(FRUIT_ADDITIONAL_COMPILE_FLAGS "${FRUIT_ADDITIONAL_COMPILE_FLAGS} -fprofile-arcs -ftest-coverage -fno-exceptions -fno-inline -O0") |
| 63 | set(FRUIT_ADDITIONAL_LINKER_FLAGS "${FRUIT_ADDITIONAL_LINKER_FLAGS} -fprofile-arcs -ftest-coverage -fno-exceptions -fno-inline -O0") |
| 64 | endif() |
| 65 | |
Marco Poletti | 56e84d4 | 2017-03-19 10:37:12 +0000 | [diff] [blame] | 66 | set(FRUIT_USES_BOOST TRUE CACHE BOOL |
| 67 | "Whether to use Boost (specifically, boost::unordered_set and boost::unordered_map). |
| 68 | If this is false, Fruit will use std::unordered_set and std::unordered_map instead (however this causes injection to be a bit slower).") |
| 69 | |
tt4g | 1265210 | 2020-03-06 13:31:40 +0900 | [diff] [blame] | 70 | if(${FRUIT_USES_BOOST}) |
tt4g | 17c56a6 | 2020-03-08 11:20:37 +0900 | [diff] [blame] | 71 | |
| 72 | if(DEFINED BOOST_DIR) |
tt4g | c1d802a | 2020-03-08 12:45:03 +0900 | [diff] [blame] | 73 | message(DEPRECATION "BOOST_DIR is deprecated. Use Boost_INCLUDE_DIR instead.") |
tt4g | 17c56a6 | 2020-03-08 11:20:37 +0900 | [diff] [blame] | 74 | set(Boost_INCLUDE_DIR "${BOOST_DIR}" CACHE PATH "") |
Marco Poletti | 7c74201 | 2017-03-04 12:40:27 +0000 | [diff] [blame] | 75 | endif() |
tt4g | 17c56a6 | 2020-03-08 11:20:37 +0900 | [diff] [blame] | 76 | |
tt4g | 1265210 | 2020-03-06 13:31:40 +0900 | [diff] [blame] | 77 | find_package(Boost) |
| 78 | if(Boost_FOUND) |
| 79 | include_directories(${Boost_INCLUDE_DIRS}) |
| 80 | else() |
| 81 | message(FATAL_ERROR "Please re-run CMake, specifying the boost library path as Boost_INCLUDE_DIR, e.g. -DBoost_INCLUDE_DIR=C:\\boost\\boost_1_62_0, or specify -DFRUIT_USES_BOOST=False to not use boost.") |
Marco Poletti | 7c74201 | 2017-03-04 12:40:27 +0000 | [diff] [blame] | 82 | endif() |
Marco Poletti | 7c74201 | 2017-03-04 12:40:27 +0000 | [diff] [blame] | 83 | endif() |
| 84 | |
Marco Poletti | 3bd4725 | 2015-12-07 18:57:33 +0000 | [diff] [blame] | 85 | set(RUN_TESTS_UNDER_VALGRIND FALSE CACHE BOOL "Whether to run Fruit tests under valgrind") |
Marco Poletti | dcc51a5 | 2017-03-04 13:06:46 +0000 | [diff] [blame] | 86 | if ("${RUN_TESTS_UNDER_VALGRIND}") |
Marco Poletti | 530c4c0 | 2016-10-15 11:00:55 +0100 | [diff] [blame] | 87 | set(RUN_TESTS_UNDER_VALGRIND_FLAG "1") |
| 88 | endif() |
Marco Poletti | 3bd4725 | 2015-12-07 18:57:33 +0000 | [diff] [blame] | 89 | |
Marco Poletti | 42c3b7c | 2016-05-22 09:32:36 +0100 | [diff] [blame] | 90 | # Unsafe, only for debugging/benchmarking. |
Marco Poletti | 066d689 | 2018-08-27 10:46:27 +0100 | [diff] [blame] | 91 | #set(FRUIT_ADDITIONAL_COMPILE_FLAGS "${FRUIT_ADDITIONAL_COMPILE_FLAGS} -DFRUIT_NO_LOOP_CHECK=1") |
Marco Poletti | 42c3b7c | 2016-05-22 09:32:36 +0100 | [diff] [blame] | 92 | |
Marco Poletti | 4132f22 | 2016-02-14 10:37:59 +0000 | [diff] [blame] | 93 | add_definitions(${FRUIT_ADDITIONAL_COMPILE_FLAGS}) |
Marco Poletti | f129bd2 | 2016-02-14 13:47:39 +0000 | [diff] [blame] | 94 | set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${FRUIT_ADDITIONAL_LINKER_FLAGS}") |
Marco Poletti | 227bef1 | 2016-02-14 18:00:12 +0000 | [diff] [blame] | 95 | set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${FRUIT_ADDITIONAL_LINKER_FLAGS}") |
| 96 | set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${FRUIT_ADDITIONAL_LINKER_FLAGS}") |
Marco Poletti | 905dc09 | 2017-03-12 15:54:31 +0000 | [diff] [blame] | 97 | |
| 98 | if ("${CMAKE_BUILD_TYPE}" STREQUAL "Release") |
| 99 | set(FRUIT_COMPILE_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_RELEASE} ${FRUIT_ADDITIONAL_COMPILE_FLAGS}") |
| 100 | else() |
| 101 | set(FRUIT_COMPILE_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_DEBUG} ${FRUIT_ADDITIONAL_COMPILE_FLAGS}") |
| 102 | endif() |
Marco Poletti | cf798fa | 2014-06-21 15:05:15 +0100 | [diff] [blame] | 103 | |
Marco Poletti | b8945ef | 2020-03-30 22:49:02 -0700 | [diff] [blame] | 104 | set(FRUIT_CLANG_TIDY_CHECKS |
| 105 | bugprone*,-bugprone-reserved-identifier,-bugprone-exception-escape,clang-analyzer*,performance*,google*,-google-readability*,-google-runtime-references,clang-diagnostic-unused-command-line-argument,misc-macro-parentheses,-clang-diagnostic-dtor-name) |
Marco Poletti | 0b30aa6 | 2020-03-30 21:29:06 -0700 | [diff] [blame] | 106 | |
Marco Poletti | 8325561 | 2020-03-28 15:41:11 -0700 | [diff] [blame] | 107 | set(FRUIT_ENABLE_CLANG_TIDY FALSE CACHE BOOL "Whether to run clang-tidy on the Fruit codebase during the build") |
| 108 | if(${FRUIT_ENABLE_CLANG_TIDY}) |
| 109 | set(CMAKE_CXX_CLANG_TIDY |
| 110 | clang-tidy; |
| 111 | -header-filter=fruit; |
Marco Poletti | b8945ef | 2020-03-30 22:49:02 -0700 | [diff] [blame] | 112 | -checks=${FRUIT_CLANG_TIDY_CHECKS}; |
Marco Poletti | 8325561 | 2020-03-28 15:41:11 -0700 | [diff] [blame] | 113 | -warnings-as-errors=*;) |
| 114 | endif() |
| 115 | |
Mark Swaanenburg | eb724f3 | 2016-10-30 19:41:52 +0100 | [diff] [blame] | 116 | include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include) |
Robert Jakob | f5c96b3 | 2017-03-23 08:24:59 +0100 | [diff] [blame] | 117 | include_directories(${CMAKE_CURRENT_BINARY_DIR}/include) |
Marco Poletti | 06a1d1e | 2014-09-20 20:59:51 +0100 | [diff] [blame] | 118 | |
Marco Poletti | b9aca3f | 2015-05-31 10:06:45 +0100 | [diff] [blame] | 119 | # (debug-only) compile switch to get deep template instantiation stacktraces for errors (instead |
| 120 | # of the user-friendly default that hides Fruit internals). |
Marco Poletti | 066d689 | 2018-08-27 10:46:27 +0100 | [diff] [blame] | 121 | #add_definitions(-DFRUIT_DEEP_TEMPLATE_INSTANTIATION_STACKTRACES_FOR_ERRORS=1) |
Marco Poletti | b9aca3f | 2015-05-31 10:06:45 +0100 | [diff] [blame] | 122 | |
tt4g | 2c7f55c | 2020-03-01 11:34:14 +0900 | [diff] [blame] | 123 | include(GNUInstallDirs) |
Marco Poletti | e66e1e7 | 2014-06-21 18:07:22 +0100 | [diff] [blame] | 124 | |
Marco Poletti | f289510 | 2016-01-30 13:38:37 +0000 | [diff] [blame] | 125 | add_subdirectory(configuration) |
Marco Poletti | cf798fa | 2014-06-21 15:05:15 +0100 | [diff] [blame] | 126 | add_subdirectory(src) |
Marco Poletti | 1fb101f | 2014-10-12 16:18:17 +0100 | [diff] [blame] | 127 | |
tt4g | 0cecdd8 | 2020-03-01 16:18:27 +0900 | [diff] [blame] | 128 | if(NOT "${FRUIT_IS_BEING_BUILT_BY_CONAN}") |
| 129 | if("${CMAKE_BUILD_TYPE}" STREQUAL "Debug") |
| 130 | # Do not exclude these from "make all" in debug mode, they must build. |
| 131 | add_subdirectory(examples) |
| 132 | add_subdirectory(tests) |
| 133 | else() |
| 134 | add_subdirectory(examples EXCLUDE_FROM_ALL) |
| 135 | add_subdirectory(tests) |
| 136 | endif() |
Marco Poletti | 1fb101f | 2014-10-12 16:18:17 +0100 | [diff] [blame] | 137 | |
tt4g | 0cecdd8 | 2020-03-01 16:18:27 +0900 | [diff] [blame] | 138 | add_subdirectory(extras EXCLUDE_FROM_ALL) |
| 139 | endif() |
Marco Poletti | cf798fa | 2014-06-21 15:05:15 +0100 | [diff] [blame] | 140 | |
Marco Poletti | f86df38 | 2015-03-15 10:02:34 +0000 | [diff] [blame] | 141 | install(DIRECTORY include/fruit/ |
tt4g | 2c7f55c | 2020-03-01 11:34:14 +0900 | [diff] [blame] | 142 | DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/fruit |
Marco Poletti | cf798fa | 2014-06-21 15:05:15 +0100 | [diff] [blame] | 143 | FILES_MATCHING PATTERN "*.h") |
Marco Poletti | 66906ac | 2016-01-30 16:12:40 +0000 | [diff] [blame] | 144 | |
| 145 | set(CPACK_PACKAGE_NAME "Fruit") |
| 146 | set(CPACK_PACKAGE_VENDOR "Marco Poletti") |
| 147 | set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Fruit - Dependency Injection Framework For C++") |
Marco Poletti | 66906ac | 2016-01-30 16:12:40 +0000 | [diff] [blame] | 148 | set(CPACK_PACKAGE_INSTALL_DIRECTORY "Fruit") |