blob: 7efcf7b14cf1c61ed952f486f5066244d9172cf6 [file] [log] [blame]
tt4ge13b3512020-03-02 09:40:28 +09001cmake_minimum_required(VERSION 3.2)
Marco Poletti50e5afc2017-03-12 16:27:47 +00002
Marco Poletti29c9fd22020-07-12 10:02:58 -07003project(Fruit VERSION 3.6.0 LANGUAGES CXX)
tt4gb23fa352020-03-01 15:44:32 +09004
tt4g7d72dce2020-03-08 11:42:12 +09005set(FRUIT_IS_BEING_BUILT_BY_CONAN FALSE CACHE BOOL "This is set in Conan builds.")
6if("${FRUIT_IS_BEING_BUILT_BY_CONAN}")
7 include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
8 conan_basic_setup()
9endif()
10
Marco Poletti50e5afc2017-03-12 16:27:47 +000011if (POLICY CMP0054)
12 cmake_policy(SET CMP0054 NEW)
13endif()
Marco Poletticf798fa2014-06-21 15:05:15 +010014
Marco Poletti074c1a32020-03-09 02:45:34 -070015if ("${CMAKE_CXX_STANDARD}" STREQUAL "")
16 set(CMAKE_CXX_STANDARD 11)
17endif()
tt4ge85c88e2020-03-07 12:46:03 +090018set(CMAKE_CXX_STANDARD_REQUIRED ON)
19
Marco Polettieb92f9a2015-12-01 19:23:08 +010020# CMake on OSX likes to see this set explicitly, otherwise it outputs a warning.
21set(CMAKE_MACOSX_RPATH 1)
22
poletti-marcoc7cfd022017-03-27 19:03:42 +010023if(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 Poletticf798fa2014-06-21 15:05:15 +010025endif()
26
Marco Polettidc9a1912017-03-19 15:26:04 +000027option(BUILD_SHARED_LIBS "Build shared library" ON)
Marco Poletti905dc092017-03-12 15:54:31 +000028
29# The Visual Studio CMake generators default to multiple configurations, but Fruit doesn't support multi-configuration build directories.
30set(CMAKE_CONFIGURATION_TYPES "${CMAKE_BUILD_TYPE}")
31
Marco Poletti050763e2017-06-11 18:38:16 +010032set(FRUIT_ADDITIONAL_CXX_FLAGS "" CACHE STRING "Additional CXX compiler flags." FORCE)
33
34set(FRUIT_ADDITIONAL_COMPILE_FLAGS "${FRUIT_ADDITIONAL_CXX_FLAGS}")
35
Marco Poletti905dc092017-03-12 15:54:31 +000036if(NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES "^(GNU|Clang|AppleClang|MSVC)$")
Marco Poletti66906ac2016-01-30 16:12:40 +000037 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 Poletti66906ac2016-01-30 16:12:40 +000040 # * "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
43endif()
44
45if("${CMAKE_CXX_COMPILER_ID}" MATCHES "^(GNU|Clang|Intel|AppleClang)$")
Marco Polettic6b2e6e2017-03-18 18:04:47 +000046 set(FRUIT_ADDITIONAL_COMPILE_FLAGS "${FRUIT_ADDITIONAL_COMPILE_FLAGS} -std=c++11 -W -Wall -Wno-unknown-warning-option -Wno-missing-braces")
Marco Poletti66906ac2016-01-30 16:12:40 +000047elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "^(MSVC)$")
Marco Poletti905dc092017-03-12 15:54:31 +000048 # TODO: we currently disable the warning C4709 because MSVC emits it even when there is no reason to. Re-enable it when possible.
Marco Poletti98a444c2017-05-20 15:52:17 +010049 # 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 Poletti35805fa2017-05-20 16:17:48 +010050 # TODO: the warning C4714 is disabled, MSVC emits it when it decides not to inline a __forceinline function/method.
Marco Poletti8fdeba72017-07-08 19:55:32 +010051 # The warning C4577 is disabled because we don't need a termination guarantee on exceptions for functions marked with
52 # 'noexcept'.
Marco Poletti3ef03012017-07-08 20:30:32 +010053 # The warning C4530 is disabled because it's triggered by MSVC's STL.
Marco Polettic1f47b92020-03-29 16:23:38 -070054 # 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 Poletti66906ac2016-01-30 16:12:40 +000056endif()
57
Marco Poletti07cdd352017-07-13 19:42:13 +010058option(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)
59if("${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")
64endif()
65
Marco Poletti56e84d42017-03-19 10:37:12 +000066set(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
tt4g12652102020-03-06 13:31:40 +090070if(${FRUIT_USES_BOOST})
tt4g17c56a62020-03-08 11:20:37 +090071
72 if(DEFINED BOOST_DIR)
tt4gc1d802a2020-03-08 12:45:03 +090073 message(DEPRECATION "BOOST_DIR is deprecated. Use Boost_INCLUDE_DIR instead.")
tt4g17c56a62020-03-08 11:20:37 +090074 set(Boost_INCLUDE_DIR "${BOOST_DIR}" CACHE PATH "")
Marco Poletti7c742012017-03-04 12:40:27 +000075 endif()
tt4g17c56a62020-03-08 11:20:37 +090076
tt4g12652102020-03-06 13:31:40 +090077 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 Poletti7c742012017-03-04 12:40:27 +000082 endif()
Marco Poletti7c742012017-03-04 12:40:27 +000083endif()
84
Marco Poletti3bd47252015-12-07 18:57:33 +000085set(RUN_TESTS_UNDER_VALGRIND FALSE CACHE BOOL "Whether to run Fruit tests under valgrind")
Marco Polettidcc51a52017-03-04 13:06:46 +000086if ("${RUN_TESTS_UNDER_VALGRIND}")
Marco Poletti530c4c02016-10-15 11:00:55 +010087 set(RUN_TESTS_UNDER_VALGRIND_FLAG "1")
88endif()
Marco Poletti3bd47252015-12-07 18:57:33 +000089
Marco Poletti42c3b7c2016-05-22 09:32:36 +010090# Unsafe, only for debugging/benchmarking.
Marco Poletti066d6892018-08-27 10:46:27 +010091#set(FRUIT_ADDITIONAL_COMPILE_FLAGS "${FRUIT_ADDITIONAL_COMPILE_FLAGS} -DFRUIT_NO_LOOP_CHECK=1")
Marco Poletti42c3b7c2016-05-22 09:32:36 +010092
Marco Poletti4132f222016-02-14 10:37:59 +000093add_definitions(${FRUIT_ADDITIONAL_COMPILE_FLAGS})
Marco Polettif129bd22016-02-14 13:47:39 +000094set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${FRUIT_ADDITIONAL_LINKER_FLAGS}")
Marco Poletti227bef12016-02-14 18:00:12 +000095set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${FRUIT_ADDITIONAL_LINKER_FLAGS}")
96set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${FRUIT_ADDITIONAL_LINKER_FLAGS}")
Marco Poletti905dc092017-03-12 15:54:31 +000097
98if ("${CMAKE_BUILD_TYPE}" STREQUAL "Release")
99 set(FRUIT_COMPILE_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_RELEASE} ${FRUIT_ADDITIONAL_COMPILE_FLAGS}")
100else()
101 set(FRUIT_COMPILE_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_DEBUG} ${FRUIT_ADDITIONAL_COMPILE_FLAGS}")
102endif()
Marco Poletticf798fa2014-06-21 15:05:15 +0100103
Marco Polettib8945ef2020-03-30 22:49:02 -0700104set(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 Poletti0b30aa62020-03-30 21:29:06 -0700106
Marco Poletti83255612020-03-28 15:41:11 -0700107set(FRUIT_ENABLE_CLANG_TIDY FALSE CACHE BOOL "Whether to run clang-tidy on the Fruit codebase during the build")
108if(${FRUIT_ENABLE_CLANG_TIDY})
109 set(CMAKE_CXX_CLANG_TIDY
110 clang-tidy;
111 -header-filter=fruit;
Marco Polettib8945ef2020-03-30 22:49:02 -0700112 -checks=${FRUIT_CLANG_TIDY_CHECKS};
Marco Poletti83255612020-03-28 15:41:11 -0700113 -warnings-as-errors=*;)
114endif()
115
Mark Swaanenburgeb724f32016-10-30 19:41:52 +0100116include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
Robert Jakobf5c96b32017-03-23 08:24:59 +0100117include_directories(${CMAKE_CURRENT_BINARY_DIR}/include)
Marco Poletti06a1d1e2014-09-20 20:59:51 +0100118
Marco Polettib9aca3f2015-05-31 10:06:45 +0100119# (debug-only) compile switch to get deep template instantiation stacktraces for errors (instead
120# of the user-friendly default that hides Fruit internals).
Marco Poletti066d6892018-08-27 10:46:27 +0100121#add_definitions(-DFRUIT_DEEP_TEMPLATE_INSTANTIATION_STACKTRACES_FOR_ERRORS=1)
Marco Polettib9aca3f2015-05-31 10:06:45 +0100122
tt4g2c7f55c2020-03-01 11:34:14 +0900123include(GNUInstallDirs)
Marco Polettie66e1e72014-06-21 18:07:22 +0100124
Marco Polettif2895102016-01-30 13:38:37 +0000125add_subdirectory(configuration)
Marco Poletticf798fa2014-06-21 15:05:15 +0100126add_subdirectory(src)
Marco Poletti1fb101f2014-10-12 16:18:17 +0100127
tt4g0cecdd82020-03-01 16:18:27 +0900128if(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 Poletti1fb101f2014-10-12 16:18:17 +0100137
tt4g0cecdd82020-03-01 16:18:27 +0900138 add_subdirectory(extras EXCLUDE_FROM_ALL)
139endif()
Marco Poletticf798fa2014-06-21 15:05:15 +0100140
Marco Polettif86df382015-03-15 10:02:34 +0000141install(DIRECTORY include/fruit/
tt4g2c7f55c2020-03-01 11:34:14 +0900142 DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/fruit
Marco Poletticf798fa2014-06-21 15:05:15 +0100143 FILES_MATCHING PATTERN "*.h")
Marco Poletti66906ac2016-01-30 16:12:40 +0000144
145set(CPACK_PACKAGE_NAME "Fruit")
146set(CPACK_PACKAGE_VENDOR "Marco Poletti")
147set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Fruit - Dependency Injection Framework For C++")
Marco Poletti66906ac2016-01-30 16:12:40 +0000148set(CPACK_PACKAGE_INSTALL_DIRECTORY "Fruit")