Marco Poletti | 66906ac | 2016-01-30 16:12:40 +0000 | [diff] [blame] | 1 | project(Fruit) |
Marco Poletti | cf798fa | 2014-06-21 15:05:15 +0100 | [diff] [blame] | 2 | cmake_minimum_required(VERSION 2.8) |
Marco Poletti | 50e5afc | 2017-03-12 16:27:47 +0000 | [diff] [blame] | 3 | |
| 4 | if (POLICY CMP0054) |
| 5 | cmake_policy(SET CMP0054 NEW) |
| 6 | endif() |
Marco Poletti | cf798fa | 2014-06-21 15:05:15 +0100 | [diff] [blame] | 7 | |
Marco Poletti | eb92f9a | 2015-12-01 19:23:08 +0100 | [diff] [blame] | 8 | # CMake on OSX likes to see this set explicitly, otherwise it outputs a warning. |
| 9 | set(CMAKE_MACOSX_RPATH 1) |
| 10 | |
poletti-marco | c7cfd02 | 2017-03-27 19:03:42 +0100 | [diff] [blame] | 11 | if(NOT "${CMAKE_BUILD_TYPE}" MATCHES "^(Debug|Release|RelWithDebInfo|MinSizeRel)$") |
| 12 | 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] | 13 | endif() |
| 14 | |
Marco Poletti | dc9a191 | 2017-03-19 15:26:04 +0000 | [diff] [blame] | 15 | option(BUILD_SHARED_LIBS "Build shared library" ON) |
Marco Poletti | 905dc09 | 2017-03-12 15:54:31 +0000 | [diff] [blame] | 16 | |
| 17 | # The Visual Studio CMake generators default to multiple configurations, but Fruit doesn't support multi-configuration build directories. |
| 18 | set(CMAKE_CONFIGURATION_TYPES "${CMAKE_BUILD_TYPE}") |
| 19 | |
Marco Poletti | dcc51a5 | 2017-03-04 13:06:46 +0000 | [diff] [blame] | 20 | if(NOT "${RUNTIME_OUTPUT_DIRECTORY}") |
Marco Poletti | 905dc09 | 2017-03-12 15:54:31 +0000 | [diff] [blame] | 21 | set(RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}") |
Marco Poletti | 7c74201 | 2017-03-04 12:40:27 +0000 | [diff] [blame] | 22 | endif() |
| 23 | |
Marco Poletti | 050763e | 2017-06-11 18:38:16 +0100 | [diff] [blame] | 24 | set(FRUIT_ADDITIONAL_CXX_FLAGS "" CACHE STRING "Additional CXX compiler flags." FORCE) |
| 25 | |
| 26 | set(FRUIT_ADDITIONAL_COMPILE_FLAGS "${FRUIT_ADDITIONAL_CXX_FLAGS}") |
| 27 | |
Marco Poletti | 905dc09 | 2017-03-12 15:54:31 +0000 | [diff] [blame] | 28 | if(NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES "^(GNU|Clang|AppleClang|MSVC)$") |
Marco Poletti | 66906ac | 2016-01-30 16:12:40 +0000 | [diff] [blame] | 29 | message(WARNING "Compiler not officially supported: ${CMAKE_CXX_COMPILER_ID}") |
| 30 | # Full list of possible values at https://cmake.org/cmake/help/latest/variable/CMAKE_LANG_COMPILER_ID.html . |
| 31 | # Major compilers not currently supported: |
Marco Poletti | 66906ac | 2016-01-30 16:12:40 +0000 | [diff] [blame] | 32 | # * "Intel": not supported ATM due to compiler bugs: |
| 33 | # - https://software.intel.com/en-us/forums/intel-c-compiler/topic/606048 |
| 34 | # - https://software.intel.com/en-us/forums/intel-c-compiler/topic/606049 |
| 35 | endif() |
| 36 | |
| 37 | if("${CMAKE_CXX_COMPILER_ID}" MATCHES "^(GNU|Clang|Intel|AppleClang)$") |
Marco Poletti | c6b2e6e | 2017-03-18 18:04:47 +0000 | [diff] [blame] | 38 | 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] | 39 | elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "^(MSVC)$") |
Marco Poletti | 905dc09 | 2017-03-12 15:54:31 +0000 | [diff] [blame] | 40 | # 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] | 41 | # 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] | 42 | # 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] | 43 | # The warning C4577 is disabled because we don't need a termination guarantee on exceptions for functions marked with |
| 44 | # 'noexcept'. |
Marco Poletti | 3ef0301 | 2017-07-08 20:30:32 +0100 | [diff] [blame] | 45 | # The warning C4530 is disabled because it's triggered by MSVC's STL. |
| 46 | set(FRUIT_ADDITIONAL_COMPILE_FLAGS "${FRUIT_ADDITIONAL_COMPILE_FLAGS} /nologo /FS /W4 /wd4324 /wd4709 /wd4459 /wd4141 /wd4714 /wd4577 /wd4530 /D_SCL_SECURE_NO_WARNINGS") |
Marco Poletti | 66906ac | 2016-01-30 16:12:40 +0000 | [diff] [blame] | 47 | endif() |
| 48 | |
Marco Poletti | 07cdd35 | 2017-07-13 19:42:13 +0100 | [diff] [blame] | 49 | 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) |
| 50 | if("${FRUIT_ENABLE_COVERAGE}") |
| 51 | # We also disable exceptions because otherwise GCC considers every function/method call that could throw as an |
| 52 | # additional branch. |
| 53 | set(FRUIT_ADDITIONAL_COMPILE_FLAGS "${FRUIT_ADDITIONAL_COMPILE_FLAGS} -fprofile-arcs -ftest-coverage -fno-exceptions -fno-inline -O0") |
| 54 | set(FRUIT_ADDITIONAL_LINKER_FLAGS "${FRUIT_ADDITIONAL_LINKER_FLAGS} -fprofile-arcs -ftest-coverage -fno-exceptions -fno-inline -O0") |
| 55 | endif() |
| 56 | |
Marco Poletti | 56e84d4 | 2017-03-19 10:37:12 +0000 | [diff] [blame] | 57 | set(FRUIT_USES_BOOST TRUE CACHE BOOL |
| 58 | "Whether to use Boost (specifically, boost::unordered_set and boost::unordered_map). |
| 59 | If this is false, Fruit will use std::unordered_set and std::unordered_map instead (however this causes injection to be a bit slower).") |
| 60 | |
Marco Poletti | 7d6f051 | 2018-09-15 11:37:23 +0100 | [diff] [blame] | 61 | set(FRUIT_IS_BEING_BUILT_BY_CONAN FALSE CACHE BOOL "This is set in Conan builds.") |
| 62 | |
| 63 | if("${WIN32}" AND "${FRUIT_USES_BOOST}" AND NOT "${FRUIT_IS_BEING_BUILT_BY_CONAN}") |
Marco Poletti | 7c74201 | 2017-03-04 12:40:27 +0000 | [diff] [blame] | 64 | set(BOOST_DIR "" CACHE PATH "The directory where the boost library is installed, e.g. C:\\boost\\boost_1_62_0.") |
| 65 | if("${BOOST_DIR}" STREQUAL "") |
Marco Poletti | 9fff407 | 2018-09-01 09:27:00 +0100 | [diff] [blame] | 66 | message(FATAL_ERROR "Please re-run CMake, specifying the boost library path as BOOST_DIR, e.g. -DBOOST_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] | 67 | endif() |
| 68 | include_directories("${BOOST_DIR}") |
| 69 | endif() |
| 70 | |
Marco Poletti | 3bd4725 | 2015-12-07 18:57:33 +0000 | [diff] [blame] | 71 | 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] | 72 | if ("${RUN_TESTS_UNDER_VALGRIND}") |
Marco Poletti | 530c4c0 | 2016-10-15 11:00:55 +0100 | [diff] [blame] | 73 | set(RUN_TESTS_UNDER_VALGRIND_FLAG "1") |
| 74 | endif() |
Marco Poletti | 3bd4725 | 2015-12-07 18:57:33 +0000 | [diff] [blame] | 75 | |
Marco Poletti | 42c3b7c | 2016-05-22 09:32:36 +0100 | [diff] [blame] | 76 | # Unsafe, only for debugging/benchmarking. |
Marco Poletti | 066d689 | 2018-08-27 10:46:27 +0100 | [diff] [blame] | 77 | #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] | 78 | |
Marco Poletti | 4132f22 | 2016-02-14 10:37:59 +0000 | [diff] [blame] | 79 | add_definitions(${FRUIT_ADDITIONAL_COMPILE_FLAGS}) |
Marco Poletti | f129bd2 | 2016-02-14 13:47:39 +0000 | [diff] [blame] | 80 | 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] | 81 | set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${FRUIT_ADDITIONAL_LINKER_FLAGS}") |
| 82 | 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] | 83 | |
| 84 | if ("${CMAKE_BUILD_TYPE}" STREQUAL "Release") |
| 85 | set(FRUIT_COMPILE_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_RELEASE} ${FRUIT_ADDITIONAL_COMPILE_FLAGS}") |
| 86 | else() |
| 87 | set(FRUIT_COMPILE_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_DEBUG} ${FRUIT_ADDITIONAL_COMPILE_FLAGS}") |
| 88 | endif() |
Marco Poletti | cf798fa | 2014-06-21 15:05:15 +0100 | [diff] [blame] | 89 | |
Mark Swaanenburg | eb724f3 | 2016-10-30 19:41:52 +0100 | [diff] [blame] | 90 | include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include) |
Robert Jakob | f5c96b3 | 2017-03-23 08:24:59 +0100 | [diff] [blame] | 91 | include_directories(${CMAKE_CURRENT_BINARY_DIR}/include) |
Marco Poletti | 06a1d1e | 2014-09-20 20:59:51 +0100 | [diff] [blame] | 92 | |
Marco Poletti | b9aca3f | 2015-05-31 10:06:45 +0100 | [diff] [blame] | 93 | # (debug-only) compile switch to get deep template instantiation stacktraces for errors (instead |
| 94 | # of the user-friendly default that hides Fruit internals). |
Marco Poletti | 066d689 | 2018-08-27 10:46:27 +0100 | [diff] [blame] | 95 | #add_definitions(-DFRUIT_DEEP_TEMPLATE_INSTANTIATION_STACKTRACES_FOR_ERRORS=1) |
Marco Poletti | b9aca3f | 2015-05-31 10:06:45 +0100 | [diff] [blame] | 96 | |
Marco Poletti | 7c74201 | 2017-03-04 12:40:27 +0000 | [diff] [blame] | 97 | set(INSTALL_INCLUDE_DIR include/fruit CACHE PATH "Installation directory for headers") |
| 98 | set(INSTALL_LIBRARY_DIR lib CACHE PATH "Installation directory for libraries") |
Marco Poletti | e66e1e7 | 2014-06-21 18:07:22 +0100 | [diff] [blame] | 99 | |
Marco Poletti | ece0f72 | 2018-09-12 20:50:28 +0100 | [diff] [blame] | 100 | set(FRUIT_VERSION "3.4.0") |
Marco Poletti | cc3a623 | 2014-07-27 16:23:11 +0100 | [diff] [blame] | 101 | |
Marco Poletti | f289510 | 2016-01-30 13:38:37 +0000 | [diff] [blame] | 102 | add_subdirectory(configuration) |
Marco Poletti | cf798fa | 2014-06-21 15:05:15 +0100 | [diff] [blame] | 103 | add_subdirectory(src) |
Marco Poletti | 1fb101f | 2014-10-12 16:18:17 +0100 | [diff] [blame] | 104 | |
Marco Poletti | dcc51a5 | 2017-03-04 13:06:46 +0000 | [diff] [blame] | 105 | if("${CMAKE_BUILD_TYPE}" STREQUAL "Debug") |
Marco Poletti | 1fb101f | 2014-10-12 16:18:17 +0100 | [diff] [blame] | 106 | # Do not exclude these from "make all" in debug mode, they must build. |
Marco Poletti | 1fb101f | 2014-10-12 16:18:17 +0100 | [diff] [blame] | 107 | add_subdirectory(examples) |
| 108 | add_subdirectory(tests) |
| 109 | else() |
Marco Poletti | 1fb101f | 2014-10-12 16:18:17 +0100 | [diff] [blame] | 110 | add_subdirectory(examples EXCLUDE_FROM_ALL) |
Marco Poletti | 9c7dc58 | 2017-06-17 18:34:28 +0100 | [diff] [blame] | 111 | add_subdirectory(tests) |
Marco Poletti | 1fb101f | 2014-10-12 16:18:17 +0100 | [diff] [blame] | 112 | endif() |
| 113 | |
Marco Poletti | f0dc0ea | 2016-04-07 12:18:30 +0200 | [diff] [blame] | 114 | add_subdirectory(extras EXCLUDE_FROM_ALL) |
Marco Poletti | cf798fa | 2014-06-21 15:05:15 +0100 | [diff] [blame] | 115 | |
Marco Poletti | f86df38 | 2015-03-15 10:02:34 +0000 | [diff] [blame] | 116 | install(DIRECTORY include/fruit/ |
Marco Poletti | 66906ac | 2016-01-30 16:12:40 +0000 | [diff] [blame] | 117 | DESTINATION "${INSTALL_INCLUDE_DIR}" |
Marco Poletti | cf798fa | 2014-06-21 15:05:15 +0100 | [diff] [blame] | 118 | FILES_MATCHING PATTERN "*.h") |
Marco Poletti | 66906ac | 2016-01-30 16:12:40 +0000 | [diff] [blame] | 119 | |
| 120 | set(CPACK_PACKAGE_NAME "Fruit") |
| 121 | set(CPACK_PACKAGE_VENDOR "Marco Poletti") |
| 122 | set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Fruit - Dependency Injection Framework For C++") |
| 123 | string(REGEX REPLACE "([^.]*)\\.([^.]*)\\.([^.]*)" "\\1" CPACK_PACKAGE_VERSION_MAJOR "${FRUIT_VERSION}") |
| 124 | string(REGEX REPLACE "([^.]*)\\.([^.]*)\\.([^.]*)" "\\2" CPACK_PACKAGE_VERSION_MINOR "${FRUIT_VERSION}") |
| 125 | string(REGEX REPLACE "([^.]*)\\.([^.]*)\\.([^.]*)" "\\3" CPACK_PACKAGE_VERSION_PATCH "${FRUIT_VERSION}") |
| 126 | set(CPACK_PACKAGE_VERSION "${FRUIT_VERSION}") |
| 127 | set(CPACK_PACKAGE_INSTALL_DIRECTORY "Fruit") |