1project(Fruit)
2cmake_minimum_required(VERSION 2.8)
3
4if (POLICY CMP0054)
5    cmake_policy(SET CMP0054 NEW)
6endif()
7
8# CMake on OSX likes to see this set explicitly, otherwise it outputs a warning.
9set(CMAKE_MACOSX_RPATH 1)
10
11if(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 .")
13endif()
14
15option(BUILD_SHARED_LIBS "Build shared library" ON)
16
17# The Visual Studio CMake generators default to multiple configurations, but Fruit doesn't support multi-configuration build directories.
18set(CMAKE_CONFIGURATION_TYPES "${CMAKE_BUILD_TYPE}")
19
20if(NOT "${RUNTIME_OUTPUT_DIRECTORY}")
21  set(RUNTIME_OUTPUT_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}")
22endif()
23
24set(FRUIT_ADDITIONAL_CXX_FLAGS "" CACHE STRING "Additional CXX compiler flags." FORCE)
25
26set(FRUIT_ADDITIONAL_COMPILE_FLAGS "${FRUIT_ADDITIONAL_CXX_FLAGS}")
27
28if(NOT "${CMAKE_CXX_COMPILER_ID}" MATCHES "^(GNU|Clang|AppleClang|MSVC)$")
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:
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
35endif()
36
37if("${CMAKE_CXX_COMPILER_ID}" MATCHES "^(GNU|Clang|Intel|AppleClang)$")
38  set(FRUIT_ADDITIONAL_COMPILE_FLAGS "${FRUIT_ADDITIONAL_COMPILE_FLAGS} -std=c++11 -W -Wall -Wno-unknown-warning-option -Wno-missing-braces")
39elseif("${CMAKE_CXX_COMPILER_ID}" MATCHES "^(MSVC)$")
40  # TODO: we currently disable the warning C4709 because MSVC emits it even when there is no reason to. Re-enable it when possible.
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.
42  # TODO: the warning C4714 is disabled, MSVC emits it when it decides not to inline a __forceinline function/method.
43  # The warning C4577 is disabled because we don't need a termination guarantee on exceptions for functions marked with
44  # 'noexcept'.
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")
47endif()
48
49option(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)
50if("${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")
55endif()
56
57set(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
61set(FRUIT_IS_BEING_BUILT_BY_CONAN FALSE CACHE BOOL "This is set in Conan builds.")
62
63if("${WIN32}" AND "${FRUIT_USES_BOOST}" AND NOT "${FRUIT_IS_BEING_BUILT_BY_CONAN}")
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 "")
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.")
67  endif()
68  include_directories("${BOOST_DIR}")
69endif()
70
71set(RUN_TESTS_UNDER_VALGRIND FALSE CACHE BOOL "Whether to run Fruit tests under valgrind")
72if ("${RUN_TESTS_UNDER_VALGRIND}")
73  set(RUN_TESTS_UNDER_VALGRIND_FLAG "1")
74endif()
75
76# Unsafe, only for debugging/benchmarking.
77#set(FRUIT_ADDITIONAL_COMPILE_FLAGS "${FRUIT_ADDITIONAL_COMPILE_FLAGS} -DFRUIT_NO_LOOP_CHECK=1")
78
79add_definitions(${FRUIT_ADDITIONAL_COMPILE_FLAGS})
80set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${FRUIT_ADDITIONAL_LINKER_FLAGS}")
81set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${FRUIT_ADDITIONAL_LINKER_FLAGS}")
82set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} ${FRUIT_ADDITIONAL_LINKER_FLAGS}")
83
84if ("${CMAKE_BUILD_TYPE}" STREQUAL "Release")
85    set(FRUIT_COMPILE_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_RELEASE} ${FRUIT_ADDITIONAL_COMPILE_FLAGS}")
86else()
87    set(FRUIT_COMPILE_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_DEBUG} ${FRUIT_ADDITIONAL_COMPILE_FLAGS}")
88endif()
89
90include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
91include_directories(${CMAKE_CURRENT_BINARY_DIR}/include)
92
93# (debug-only) compile switch to get deep template instantiation stacktraces for errors (instead
94# of the user-friendly default that hides Fruit internals).
95#add_definitions(-DFRUIT_DEEP_TEMPLATE_INSTANTIATION_STACKTRACES_FOR_ERRORS=1)
96
97set(INSTALL_INCLUDE_DIR include/fruit CACHE PATH "Installation directory for headers")
98set(INSTALL_LIBRARY_DIR lib CACHE PATH "Installation directory for libraries")
99
100set(FRUIT_VERSION "3.4.0")
101
102add_subdirectory(configuration)
103add_subdirectory(src)
104
105if("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
106  # Do not exclude these from "make all" in debug mode, they must build.
107  add_subdirectory(examples)
108  add_subdirectory(tests)
109else()
110  add_subdirectory(examples EXCLUDE_FROM_ALL)
111  add_subdirectory(tests)
112endif()
113
114add_subdirectory(extras EXCLUDE_FROM_ALL)
115
116install(DIRECTORY include/fruit/
117  DESTINATION "${INSTALL_INCLUDE_DIR}"
118  FILES_MATCHING PATTERN "*.h")
119
120set(CPACK_PACKAGE_NAME "Fruit")
121set(CPACK_PACKAGE_VENDOR "Marco Poletti")
122set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "Fruit - Dependency Injection Framework For C++")
123string(REGEX REPLACE "([^.]*)\\.([^.]*)\\.([^.]*)" "\\1" CPACK_PACKAGE_VERSION_MAJOR "${FRUIT_VERSION}")
124string(REGEX REPLACE "([^.]*)\\.([^.]*)\\.([^.]*)" "\\2" CPACK_PACKAGE_VERSION_MINOR "${FRUIT_VERSION}")
125string(REGEX REPLACE "([^.]*)\\.([^.]*)\\.([^.]*)" "\\3" CPACK_PACKAGE_VERSION_PATCH "${FRUIT_VERSION}")
126set(CPACK_PACKAGE_VERSION "${FRUIT_VERSION}")
127set(CPACK_PACKAGE_INSTALL_DIRECTORY "Fruit")
128