2 # Copyright (C) 2012-2018 Icinga Development Team (https://www.icinga.com/)
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software Foundation
16 # Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
18 cmake_minimum_required(VERSION 2.6)
19 set(BOOST_MIN_VERSION "1.48.0")
22 list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
23 list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/third-party/cmake")
25 if(NOT CMAKE_BUILD_TYPE)
26 set(CMAKE_BUILD_TYPE Release CACHE STRING
27 "Choose the type of build, options are: None Debug Release RelWithDebInfo MinSizeRel."
31 option(ICINGA2_WITH_MYSQL "Build the MySQL IDO module" ON)
32 option(ICINGA2_WITH_PGSQL "Build the PostgreSQL IDO module" ON)
33 option(ICINGA2_WITH_CHECKER "Build the checker module" ON)
34 option(ICINGA2_WITH_COMPAT "Build the compat module" ON)
35 option(ICINGA2_WITH_DEMO "Build the demo module" OFF)
36 option(ICINGA2_WITH_HELLO "Build the hello module" OFF)
37 option(ICINGA2_WITH_LIVESTATUS "Build the Livestatus module" ON)
38 option(ICINGA2_WITH_NOTIFICATION "Build the notification module" ON)
39 option(ICINGA2_WITH_PERFDATA "Build the perfdata module" ON)
40 option(ICINGA2_WITH_STUDIO "Build the Icinga Studio application" OFF)
41 option(ICINGA2_WITH_TESTS "Run unit tests" ON)
43 file(STRINGS icinga2.spec VERSION_LINE REGEX "^Version: ")
44 string(REPLACE "Version: " "" ICINGA2_VERSION ${VERSION_LINE})
46 include(GNUInstallDirs)
47 include(InstallConfig)
49 set(ICINGA2_USER "icinga" CACHE STRING "Icinga 2 user")
50 set(ICINGA2_GROUP "icinga" CACHE STRING "Icinga 2 group")
51 set(ICINGA2_COMMAND_GROUP "icingacmd" CACHE STRING "Icinga 2 command group")
52 set(ICINGA2_RUNDIR "${CMAKE_INSTALL_FULL_LOCALSTATEDIR}/run" CACHE STRING "/run directory")
53 set(ICINGA2_PLUGINDIR "/usr/lib/nagios/plugins" CACHE STRING "Path for the check plugins")
54 set(ICINGA2_GIT_VERSION_INFO ON CACHE BOOL "Whether to use git describe")
55 set(ICINGA2_UNITY_BUILD ON CACHE BOOL "Whether to perform a unity build")
56 set(ICINGA2_LTO_BUILD OFF CACHE BOOL "Whether to use LTO")
58 site_name(ICINGA2_BUILD_HOST_NAME)
59 set(ICINGA2_BUILD_COMPILER_NAME "${CMAKE_CXX_COMPILER_ID}")
61 if(NOT CMAKE_CXX_COMPILER_VERSION)
62 execute_process(COMMAND ${CMAKE_CXX_COMPILER} -dumpversion
63 OUTPUT_VARIABLE CMAKE_CXX_COMPILER_VERSION OUTPUT_STRIP_TRAILING_WHITESPACE)
66 set(ICINGA2_BUILD_COMPILER_VERSION "${CMAKE_CXX_COMPILER_VERSION}")
68 file(READ "${CMAKE_CURRENT_SOURCE_DIR}/COPYING" ICINGA2_LICENSE_GPL)
69 file(READ "${CMAKE_CURRENT_SOURCE_DIR}/COPYING.Exceptions" ICINGA2_LICENSE_ADDITIONS)
70 set(ICINGA2_LICENSE "${ICINGA2_LICENSE_GPL}\n\n---\n\n${ICINGA2_LICENSE_ADDITIONS}")
71 file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/LICENSE.txt" ${ICINGA2_LICENSE})
73 file(STRINGS icinga2.spec SPEC_VERSION REGEX "^Version:")
74 string(LENGTH "${SPEC_VERSION}" SPEC_VERSION_LENGTH)
75 math(EXPR SPEC_VERSION_LENGTH "${SPEC_VERSION_LENGTH} - 9")
76 string(SUBSTRING ${SPEC_VERSION} 9 ${SPEC_VERSION_LENGTH} SPEC_VERSION)
78 configure_file(icinga-spec-version.h.cmake icinga-spec-version.h)
80 include(GetGitRevisionDescription)
81 git_describe(GIT_VERSION --tags)
82 if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/icinga-version.h.force)
83 configure_file(icinga-version.h.force ${CMAKE_CURRENT_BINARY_DIR}/icinga-version.h COPYONLY)
85 if(NOT ICINGA2_GIT_VERSION_INFO OR GIT_VERSION MATCHES "-NOTFOUND$")
86 file(STRINGS icinga2.spec SPEC_REVISION REGEX "^%define revision ")
87 string(LENGTH "${SPEC_REVISION}" SPEC_REVISION_LENGTH)
88 math(EXPR SPEC_REVISION_LENGTH "${SPEC_REVISION_LENGTH} - 17")
89 string(SUBSTRING ${SPEC_REVISION} 17 ${SPEC_REVISION_LENGTH} SPEC_REVISION)
91 set(GIT_VERSION "r${SPEC_VERSION}-${SPEC_REVISION}")
93 configure_file(icinga-version.h.cmake icinga-version.h)
97 set(Boost_USE_STATIC_LIBS ON)
98 add_definitions(-DBOOST_ALL_NO_LIB)
99 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} /bigobj")
100 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /bigobj")
103 if(NOT DEFINED LOGROTATE_HAS_SU)
104 set(LOGROTATE_HAS_SU OFF)
105 find_program(LOGROTATE_BINARY logrotate)
106 execute_process(COMMAND ${LOGROTATE_BINARY} ERROR_VARIABLE LOGROTATE_OUTPUT)
108 string(REGEX REPLACE "^logrotate ([0-9.]*).*" "\\1" LOGROTATE_VERSION
110 message(STATUS "Found logrotate (found version \"${LOGROTATE_VERSION}\")")
111 if("${LOGROTATE_VERSION}" VERSION_GREATER "3.7.9")
112 set(LOGROTATE_HAS_SU ON)
117 set(LOGROTATE_USE_SU "\n\tsu ${ICINGA2_USER} ${ICINGA2_GROUP}")
120 find_package(Boost ${BOOST_MIN_VERSION} COMPONENTS thread system program_options regex REQUIRED)
122 link_directories(${Boost_LIBRARY_DIRS})
123 include_directories(${Boost_INCLUDE_DIRS})
125 find_package(OpenSSL REQUIRED)
126 include_directories(${OPENSSL_INCLUDE_DIR})
131 include_directories(${icinga2_BINARY_DIR}/third-party/yajl/include)
132 link_directories(${icinga2_BINARY_DIR}/third-party/yajl)
133 set(YAJL_LIBRARIES "yajl")
136 find_package(Editline)
137 set(HAVE_EDITLINE "${EDITLINE_FOUND}")
139 find_package(Termcap)
140 set(HAVE_TERMCAP "${TERMCAP_FOUND}")
143 ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/lib
144 ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_BINARY_DIR}/lib
147 set(CMAKE_MACOSX_RPATH 1)
149 if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
150 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Qunused-arguments -fcolor-diagnostics")
151 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Qunused-arguments -fcolor-diagnostics")
153 # Clang on Fedora requires -pthread, Apple Clang does not
154 # AppleClang is available since CMake 3.0.0
155 if (NOT CMAKE_CXX_COMPILER_ID MATCHES "AppleClang")
156 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread")
157 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread")
161 if(CMAKE_C_COMPILER_ID STREQUAL "SunPro")
162 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -mt")
163 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -mt -library=stlport4")
166 if(CMAKE_C_COMPILER_ID STREQUAL "GNU")
167 if(CMAKE_SYSTEM_NAME MATCHES AIX)
168 set(CMAKE_CXX_FLAGS "${CMAKE_C_FLAGS} -g -lpthread")
169 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -lpthread")
170 elseif(CMAKE_SYSTEM_NAME MATCHES "kOpenBSD.*|OpenBSD.*")
171 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -pthread")
172 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -pthread")
173 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lpthread")
174 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -lpthread")
176 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -pthread")
177 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -pthread")
181 include(CheckCXXCompilerFlag)
183 function(check_cxx_linker_flag flag var)
184 set(CMAKE_REQUIRED_FLAGS ${flag})
186 check_cxx_compiler_flag(${flag} result)
187 set(${var} ${result} PARENT_SCOPE)
190 check_cxx_linker_flag("-Wl,--gc-sections" LD_GC_SECTIONS)
193 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--gc-sections")
194 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--gc-sections")
197 check_cxx_linker_flag("-Wl,--no-export-dynamic" LD_NO_EXPORT_DYNAMIC)
199 if(LD_NO_EXPORT_DYNAMIC)
200 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--no-export-dynamic")
201 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--no-export-dynamic")
204 check_cxx_linker_flag("-Bsymbolic-functions" LD_SYMBOLIC_FUNCTIONS)
206 if(LD_SYMBOLIC_FUNCTIONS)
207 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Bsymbolic-functions")
208 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Bsymbolic-functions")
211 check_cxx_linker_flag("-Wl,--dynamic-list-cpp-typeinfo" LD_DYNAMIC_LIST_CPP_TYPEINFO)
213 if(LD_DYNAMIC_LIST_CPP_TYPEINFO)
214 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--dynamic-list-cpp-typeinfo")
215 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--dynamic-list-cpp-typeinfo")
218 check_cxx_linker_flag("-Wl,--dynamic-list-data" LD_DYNAMIC_LIST_DATA)
220 if(LD_DYNAMIC_LIST_DATA)
221 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--dynamic-list-data")
222 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--dynamic-list-data")
225 check_cxx_compiler_flag("-Winvalid-pch" CXX_INVALID_PCH)
228 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Winvalid-pch")
229 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Winvalid-pch")
232 if(ICINGA2_LTO_BUILD)
233 check_cxx_compiler_flag("-flto" CXX_FLAG_LTO)
236 message(WARNING "Compiler does not support LTO, falling back to non-LTO build")
238 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -flto")
239 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -flto")
240 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -flto")
241 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -flto")
245 include(CheckCCompilerFlag)
247 check_c_compiler_flag(-fvisibility-inlines-hidden HAVE_VISIBILITY_INLINES_HIDDEN)
249 if(HAVE_VISIBILITY_INLINES_HIDDEN)
250 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility-inlines-hidden")
251 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility-inlines-hidden")
254 check_c_compiler_flag(-fvisibility=hidden HAVE_VISIBILITY_HIDDEN)
256 if(HAVE_VISIBILITY_HIDDEN)
257 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fvisibility=hidden")
258 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fvisibility=hidden")
262 add_definitions(-D_CRT_SECURE_NO_DEPRECATE -D_CRT_NONSTDC_NO_DEPRECATE -D_SCL_SECURE_NO_WARNINGS)
265 set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR}/Bin/${CMAKE_BUILD_TYPE} CACHE PATH "Library output path")
266 set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR}/Bin/${CMAKE_BUILD_TYPE} CACHE PATH "Executable output path")
268 include(CheckSymbolExists)
269 include(CheckFunctionExists)
270 include(CheckLibraryExists)
271 include(CheckIncludeFileCXX)
273 check_symbol_exists(__COUNTER__ "" HAVE_COUNTER_MACRO)
275 if(NOT HAVE_COUNTER_MACRO)
276 message(FATAL_ERROR "Your C/C++ compiler does not support the __COUNTER__ macro.")
279 set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -DI2_DEBUG")
280 set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DI2_DEBUG")
282 check_function_exists(vfork HAVE_VFORK)
283 check_function_exists(backtrace_symbols HAVE_BACKTRACE_SYMBOLS)
284 check_function_exists(pipe2 HAVE_PIPE2)
285 check_function_exists(nice HAVE_NICE)
286 check_library_exists(dl dladdr "dlfcn.h" HAVE_DLADDR)
287 check_library_exists(execinfo backtrace_symbols "" HAVE_LIBEXECINFO)
288 check_include_file_cxx(cxxabi.h HAVE_CXXABI_H)
291 set(HAVE_BACKTRACE_SYMBOLS TRUE)
294 if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
295 exec_program(${CMAKE_CXX_COMPILER}
297 OUTPUT_VARIABLE _ICINGA2_COMPILER_VERSION
300 if("${CMAKE_CXX_COMPILER_VERSION}" VERSION_LESS "4.7.0")
301 message(FATAL_ERROR "Your version of GCC (${CMAKE_CXX_COMPILER_VERSION}) is too old for building Icinga 2 (GCC >= 4.7.0 is required).")
306 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
308 check_cxx_source_compiles("class Base { public: virtual void test(void) { } }; class Derived : public Base { virtual void test(void) override { } }; int main(){}" CXX_FEATURE_OVERRIDE)
310 if(NOT CXX_FEATURE_OVERRIDE)
311 add_definitions("-Doverride=")
315 configure_file(config.h.cmake ${CMAKE_CURRENT_BINARY_DIR}/config.h ESCAPE_QUOTES)
318 FILES README.md COPYING COPYING.Exceptions AUTHORS CHANGELOG.md NEWS
319 DESTINATION ${CMAKE_INSTALL_DOCDIR}
325 set_property(GLOBAL PROPERTY USE_FOLDERS ON)
327 add_subdirectory(third-party)
328 add_subdirectory(tools)
329 add_subdirectory(lib)
330 add_subdirectory(icinga-app)
331 add_subdirectory(etc)
332 add_subdirectory(itl)
333 add_subdirectory(doc)
334 add_subdirectory(agent)
335 add_subdirectory(plugins)
336 add_subdirectory(choco)
339 add_subdirectory(icinga-installer)
342 if(ICINGA2_WITH_STUDIO)
343 add_subdirectory(icinga-studio)
346 if(ICINGA2_WITH_TESTS)
347 add_subdirectory(test)
350 set(CPACK_PACKAGE_NAME "Icinga 2")
351 set(CPACK_PACKAGE_VENDOR "Icinga Development Team")
352 set(CPACK_PACKAGE_VERSION ${ICINGA2_VERSION})
353 set(CPACK_PACKAGE_INSTALL_DIRECTORY "ICINGA2")
354 set(CPACK_PACKAGE_ICON "${CMAKE_CURRENT_SOURCE_DIR}/icinga-app\\\\icinga.ico")
355 set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_BINARY_DIR}/LICENSE.txt")
357 set(CPACK_PACKAGE_EXECUTABLES "Icinga2SetupAgent;Icinga 2 Agent Wizard;icinga-studio;Icinga Studio")
358 set(CPACK_WIX_PRODUCT_ICON "${CMAKE_CURRENT_SOURCE_DIR}/icinga-app\\\\icinga.ico")
359 set(CPACK_WIX_UPGRADE_GUID "52F2BEAA-4DF0-4C3E-ABDC-C0F61DE4DF8A")
360 set(CPACK_WIX_EXTENSIONS "WixUtilExtension")
361 set(CPACK_WIX_UI_BANNER "${CMAKE_CURRENT_SOURCE_DIR}/icinga-installer/bannrbmp.bmp")
362 set(CPACK_WIX_UI_DIALOG "${CMAKE_CURRENT_SOURCE_DIR}/icinga-installer/dlgbmp.bmp")
363 set(CPACK_WIX_PATCH_FILE "${CMAKE_CURRENT_BINARY_DIR}/icinga-installer/icinga2.wixpatch.Debug")
364 set(CPACK_WIX_PATCH_FILE "${CMAKE_CURRENT_BINARY_DIR}/icinga-installer/icinga2.wixpatch")
366 set(CMAKE_INSTALL_SYSTEM_RUNTIME_DESTINATION "sbin")
367 set(CMAKE_INSTALL_UCRT_LIBRARIES TRUE)
368 include(InstallRequiredSystemLibraries)
371 if(CMAKE_VS_PLATFORM_NAME STREQUAL "x64")
372 set(NSCP_URL "https://github.com/mickem/nscp/releases/download/0.5.0.62/NSCP-0.5.0.62-x64.msi")
373 set(NSCP_SHA256 "1854de86ad4fda3391f273de0f9985b702c014bdec01b26ad28a1343177f537f")
375 set(NSCP_URL "https://github.com/mickem/nscp/releases/download/0.5.0.62/NSCP-0.5.0.62-Win32.msi")
376 set(NSCP_SHA256 "2186b60d588fa0811344ce709332f9c63670019c62ae92eae49698bf76205a95")
379 set(NSCP_SHA256SUM "")
380 if (EXISTS ${CMAKE_CURRENT_BINARY_DIR}/NSCP.msi)
381 file(SHA256 ${CMAKE_CURRENT_BINARY_DIR}/NSCP.msi NSCP_SHA256SUM)
384 if (NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}/NSCP.msi OR NOT ${NSCP_SHA256SUM} STREQUAL ${NSCP_SHA256})
385 file(DOWNLOAD ${NSCP_URL} ${CMAKE_CURRENT_BINARY_DIR}/NSCP.msi SHOW_PROGRESS)
388 install(FILES ${CMAKE_CURRENT_BINARY_DIR}/NSCP.msi DESTINATION ${CMAKE_INSTALL_SBINDIR})
390 if (OPENSSL_VERSION_MINOR GREATER_EQUAL 1)
391 if (CMAKE_VS_PLATFORM_NAME STREQUAL "x64")
392 list (APPEND ICINGA2_OPENSSL_DLLS ${OPENSSL_INCLUDE_DIR}/../bin/libcrypto-1_1-x64.dll ${OPENSSL_INCLUDE_DIR}/../bin/libssl-1_1-x64.dll)
394 list (APPEND ICINGA2_OPENSSL_DLLS ${OPENSSL_INCLUDE_DIR}/../bin/libcrypto-1_1.dll ${OPENSSL_INCLUDE_DIR}/../bin/libssl-1_1.dll)
397 list (APPEND ICINGA2_OPENSSL_DLLS ${OPENSSL_INCLUDE_DIR}/../bin/libeay32.dll ${OPENSSL_INCLUDE_DIR}/../bin/ssleay32.dll)
401 PROGRAMS ${ICINGA2_OPENSSL_DLLS}
402 DESTINATION ${CMAKE_INSTALL_SBINDIR}