1cmake_minimum_required(VERSION 3.2)
2
3project(Fruit VERSION 3.6.0 LANGUAGES CXX)
4
5set(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
11if (POLICY CMP0054)
12    cmake_policy(SET CMP0054 NEW)
13endif()
14
15if ("${CMAKE_CXX_STANDARD}" STREQUAL "")
16    set(CMAKE_CXX_STANDARD 11)
17endif()
18set(CMAKE_CXX_STANDARD_REQUIRED ON)
19
20# CMake on OSX likes to see this set explicitly, otherwise it outputs a warning.
21set(CMAKE_MACOSX_RPATH 1)
22
23if(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 .")
25endif()
26
27option(BUILD_SHARED_LIBS "Build shared library" ON)
28
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
32set(FRUIT_ADDITIONAL_CXX_FLAGS "" CACHE STRING "Additional CXX compiler flags." FORCE)
33
34set(FRUIT_ADDITIONAL_COMPILE_FLAGS "${FRUIT_ADDITIONAL_CXX_FLAGS}")
35
36if(NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES "^(GNU|Clang|AppleClang|MSVC)$")
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:
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
43endif()
44
45if("${CMAKE_CXX_COMPILER_ID}" MATCHES "^(GNU|Clang|Intel|AppleClang)$")
46  set(FRUIT_ADDITIONAL_COMPILE_FLAGS "${FRUIT_ADDITIONAL_COMPILE_FLAGS} -std=c++11 -W -Wall -Wno-unknown-warning-option -Wno-missing-braces")
47elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "^(MSVC)$")
48  # TODO: we currently disable the warning C4709 because MSVC emits it even when there is no reason to. Re-enable it when possible.
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.
50  # TODO: the warning C4714 is disabled, MSVC emits it when it decides not to inline a __forceinline function/method.
51  # The warning C4577 is disabled because we don't need a termination guarantee on exceptions for functions marked with
52  # 'noexcept'.
53  # The warning C4530 is disabled because it's triggered by MSVC's STL.
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")
56endif()
57
58option(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
66set(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
70if(${FRUIT_USES_BOOST})
71
72  if(DEFINED BOOST_DIR)
73    message(DEPRECATION "BOOST_DIR is deprecated. Use Boost_INCLUDE_DIR instead.")
74    set(Boost_INCLUDE_DIR "${BOOST_DIR}" CACHE PATH "")
75  endif()
76
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.")
82  endif()
83endif()
84
85set(RUN_TESTS_UNDER_VALGRIND FALSE CACHE BOOL "Whether to run Fruit tests under valgrind")
86if ("${RUN_TESTS_UNDER_VALGRIND}")
87  set(RUN_TESTS_UNDER_VALGRIND_FLAG "1")
88endif()
89
90# Unsafe, only for debugging/benchmarking.
91#set(FRUIT_ADDITIONAL_COMPILE_FLAGS "${FRUIT_ADDITIONAL_COMPILE_FLAGS} -DFRUIT_NO_LOOP_CHECK=1")
92
93add_definitions(${FRUIT_ADDITIONAL_COMPILE_FLAGS})
94set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${FRUIT_ADDITIONAL_LINKER_FLAGS}")
95set(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}")
97
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()
103
104set(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)
106
107set(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;
112    -checks=${FRUIT_CLANG_TIDY_CHECKS};
113    -warnings-as-errors=*;)
114endif()
115
116include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
117include_directories(${CMAKE_CURRENT_BINARY_DIR}/include)
118
119# (debug-only) compile switch to get deep template instantiation stacktraces for errors (instead
120# of the user-friendly default that hides Fruit internals).
121#add_definitions(-DFRUIT_DEEP_TEMPLATE_INSTANTIATION_STACKTRACES_FOR_ERRORS=1)
122
123include(GNUInstallDirs)
124
125add_subdirectory(configuration)
126add_subdirectory(src)
127
128if(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()
137
138  add_subdirectory(extras EXCLUDE_FROM_ALL)
139endif()
140
141install(DIRECTORY include/fruit/
142  DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/fruit
143  FILES_MATCHING PATTERN "*.h")
144
145set(CPACK_PACKAGE_NAME "Fruit")
146set(CPACK_PACKAGE_VENDOR "Marco Poletti")
147set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Fruit - Dependency Injection Framework For C++")
148set(CPACK_PACKAGE_INSTALL_DIRECTORY "Fruit")
149