cebb61b0e00bc5bf8f995278b89c70a8191ad519
[quassel.git] / CMakeLists.txt
1 # Main CMake file for building Quassel IRC
2 #
3 # See INSTALL for possible CMake options (or read the code, Luke)
4 #####################################################################
5
6 # General setup
7 #####################################################################
8
9 cmake_minimum_required(VERSION 3.5)
10
11 # Tell CMake about or own modules
12 set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)
13
14 include(QuasselVersion)
15
16 message(STATUS "Using CMake ${CMAKE_VERSION}")
17
18 # Set up build type rather early
19 include(BuildType)
20
21 # Support ccache if found
22 # This should happen before calling project(), so compiler settings are validated.
23 option(USE_CCACHE "Enable support for ccache if available" ON)
24 if (USE_CCACHE)
25     message(STATUS "Checking for ccache")
26     find_program(CCACHE_PROGRAM ccache)
27     if (CCACHE_PROGRAM)
28         set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${CCACHE_PROGRAM}")
29         message(STATUS "Checking for ccache - enabled")
30     else()
31         message(STATUS "Checking for ccache - not found")
32     endif()
33 endif()
34
35 # Set up project
36 project(Quassel CXX)
37
38 # Let CMake handle file generation for Qt
39 set(CMAKE_AUTOMOC ON)
40 set(CMAKE_AUTORCC ON)
41 set(CMAKE_AUTOUIC ON)
42
43 # Needed, otherwise some .moc files won't be found with older CMake versions
44 set(CMAKE_INCLUDE_CURRENT_DIR ON)
45
46 # Include various CMake modules...
47 include(CMakePushCheckState)
48 include(CheckFunctionExists)
49 include(CheckIncludeFileCXX)
50 include(CheckCXXSourceCompiles)
51 include(CMakeDependentOption)
52 include(FeatureSummary)
53
54 # ... and our own
55 include(QuasselCompileSettings)
56 include(QuasselMacros)
57
58 # Options and variables that can be set on the command line
59 #####################################################################
60
61 # Select the binaries to build
62 option(WANT_CORE     "Build the core (server) binary"           ON)
63 option(WANT_QTCLIENT "Build the client-only binary"             ON)
64 option(WANT_MONO     "Build the monolithic (all-in-one) binary" ON)
65 add_feature_info(WANT_CORE WANT_CORE "Build the core (server) binary")
66 add_feature_info(WANT_QTCLIENT WANT_QTCLIENT "Build the client-only binary (requires a core to connect to)")
67 add_feature_info(WANT_MONO WANT_MONO "Build the monolithic (all-in-one) binary")
68
69 # Whether to enable integration with higher-tier KDE frameworks that require runtime support.
70 # We still optionally make use of certain Tier 1 frameworks even if WITH_KDE is disabled.
71 option(WITH_KDE "Integration with the KDE Frameworks runtime environment")
72 add_feature_info(WITH_KDE WITH_KDE "Integrate with the KDE Frameworks runtime environment")
73
74 # Icon theme support. By default, install the Breeze icon theme (may be disabled if a system installation is present)
75 option(WITH_BUNDLED_ICONS "Install required icons from the Breeze icon theme" ON)
76 add_feature_info(WITH_BUNDLED_ICONS WITH_BUNDLED_ICONS "Install required icons from the Breeze icon theme")
77
78 option(WITH_OXYGEN_ICONS "Support the Oxygen icon theme (KDE4)" OFF)
79 add_feature_info(WITH_OXYGEN_ICONS WITH_OXYGEN_ICONS "Support the Oxygen icon theme (KDE4)")
80
81 # For this, the feature info is added after we know if QtWebkit is installed
82 option(WITH_WEBKIT "WebKit support (for link previews) (legacy)" OFF)
83
84 # For this, the feature info is added after we know if QtWebEngine is installed
85 option(WITH_WEBENGINE "WebEngine support (for link previews)" ON)
86
87 if (APPLE)
88     # Notification Center is only available in > 10.8, which is Darwin v12
89     if (NOT CMAKE_SYSTEM_VERSION VERSION_LESS 12)
90         option(WITH_NOTIFICATION_CENTER "OS X Notification Center support" ON)
91         add_feature_info(WITH_NOTIFICATION_CENTER WITH_NOTIFICATION_CENTER "Use the OS X Notification Center")
92     endif()
93     find_library(CARBON_LIBRARY Carbon)
94     mark_as_advanced(CARBON_LIBRARY)
95     link_libraries(${CARBON_LIBRARY})
96 endif()
97
98 # Always embed on Windows or OSX; never embed when enabling KDE integration
99 set(EMBED_DEFAULT OFF)
100 if (WIN32 OR APPLE)
101     set(EMBED_DEFAULT ON)
102 endif()
103 cmake_dependent_option(EMBED_DATA "Embed icons and translations into the binaries instead of installing them" ${EMBED_DEFAULT}
104                                    "NOT WIN32;NOT WITH_KDE" ${EMBED_DEFAULT})
105 if (NOT EMBED_DEFAULT)
106     add_feature_info(EMBED_DATA EMBED_DATA "Embed icons and translations in the binaries instead of installing them")
107 endif()
108
109 # The following options are not for end-user consumption, so don't list them in the feature summary
110 option(FATAL_WARNINGS "Make compile warnings fatal (most useful for CI builds)" OFF)
111 cmake_dependent_option(DEPLOY "Add required libs to bundle resources and create a dmg" OFF "APPLE" OFF)
112
113 # List of authenticators and the cmake flags to build them
114 # (currently that's just LDAP, but more can be added here).
115 ####################################################################
116 option(WITH_LDAP "Enable LDAP authentication support if present on system" ON)
117
118 # Setup CMake
119 #####################################################################
120
121 # Visibility settings apply to all targets
122 if (POLICY CMP0063)
123     cmake_policy(SET CMP0063 NEW)
124 endif()
125
126 # Let automoc/autouic process generated files
127 if (POLICY CMP0071)
128     cmake_policy(SET CMP0071 NEW)
129 endif()
130
131 set(BUILD_SHARED_LIBS TRUE CACHE BOOL "" FORCE)
132
133 # Don't use X11 on OSX
134 if (APPLE)
135     set(CMAKE_DISABLE_FIND_PACKAGE_X11 true)
136     set(CMAKE_DISABLE_FIND_PACKAGE_XCB true)
137     set(CMAKE_DISABLE_FIND_PACKAGE_Qt5X11Extras true)
138 endif()
139
140 # Simplify later checks
141 #####################################################################
142
143 if (WANT_MONO OR WANT_QTCLIENT)
144     set(BUILD_GUI true)
145 endif()
146 if (WANT_MONO OR WANT_CORE)
147     set(BUILD_CORE true)
148 endif()
149
150 # Set up Qt
151 #####################################################################
152
153 set(QT_MIN_VERSION "5.5.0")
154
155 # Enable Qt deprecation warnings for Qt < 5.13 (on by default in newer versions)
156 add_definitions(-DQT_DEPRECATED_WARNINGS)
157
158 # Disable all Qt APIs that were deprecated in 5.5 and before
159 add_definitions(-DQT_DISABLE_DEPRECATED_BEFORE=0x050500)
160
161 # Find package dependencies
162 #
163 # Note that you can forcefully disable optional packages
164 # using -DCMAKE_DISABLE_FIND_PACKAGE_<PkgName>=TRUE
165 #####################################################################
166
167 # Required Qt components
168 set(qt_components Core Network)
169 if (BUILD_GUI)
170     list(APPEND qt_components Gui Widgets)
171 endif()
172 if (BUILD_CORE)
173     list(APPEND qt_components Sql)
174 endif()
175
176 find_package(Qt5 ${QT_MIN_VERSION} REQUIRED COMPONENTS ${qt_components})
177 set_package_properties(Qt5 PROPERTIES TYPE REQUIRED
178     URL "https://www.qt.io/"
179     DESCRIPTION "the Qt libraries"
180 )
181 message(STATUS "Found Qt ${Qt5Core_VERSION}")
182
183 # Determine minimum deployment target for macOS supported by Qt
184 if(APPLE)
185     if(NOT QMAKE_MACOSX_DEPLOYMENT_TARGET)
186         # qmake cannot be queried directly for QMAKE_MACOSX_DEPLOYMENT_TARGET (it is a mkspec, not a property).
187         # Instead, invoke qmake on an empty project file, which causes it to output the relevant keys and their values
188         # for subsequent parsing.
189         # A file named .qmake.stash is always created, so remove it (and empty.pro) afterwards.
190         set(qmakeEmptyProjectFile "${CMAKE_BINARY_DIR}/empty.pro")
191         set(qmakeStashFile "${CMAKE_BINARY_DIR}/.qmake.stash")
192         file(WRITE ${qmakeEmptyProjectFile} "")
193         get_target_property(QMAKE_EXECUTABLE Qt5::qmake IMPORTED_LOCATION)
194         execute_process(
195             COMMAND ${QMAKE_EXECUTABLE} -E ${qmakeEmptyProjectFile}
196             WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
197             OUTPUT_VARIABLE qmakeOutput
198         )
199         file(REMOVE ${qmakeEmptyProjectFile} ${qmakeStashFile})
200         string(REGEX MATCH "QMAKE_MACOSX_DEPLOYMENT_TARGET[ ]*=[ ]*([0-9.]+)" foo ${qmakeOutput})
201         if(NOT CMAKE_MATCH_1)
202             message(FATAL_ERROR "Could not determine the deployment target for Qt")
203         endif()
204         set(QMAKE_MACOSX_DEPLOYMENT_TARGET ${CMAKE_MATCH_1} CACHE INTERNAL "")
205         mark_as_advanced(QMAKE_MACOSX_DEPLOYMENT_TARGET)
206     endif()
207     message(STATUS "Minimum macOS version supported by Qt: ${QMAKE_MACOSX_DEPLOYMENT_TARGET}")
208 endif()
209
210 # Check for SSL support in Qt
211 cmake_push_check_state(RESET)
212 set(CMAKE_REQUIRED_LIBRARIES Qt5::Core)
213 check_cxx_source_compiles("
214     #include \"qglobal.h\"
215     #if defined QT_NO_SSL
216     #  error \"No SSL support\"
217     #endif
218     int main() {}"
219     HAVE_SSL)
220 cmake_pop_check_state()
221
222 if (NOT HAVE_SSL)
223     message(FATAL_ERROR "Quassel requires SSL support, but Qt is built with QT_NO_SSL")
224 endif()
225
226 # Optional Qt components
227
228 find_package(Qt5LinguistTools QUIET)
229 set_package_properties(Qt5LinguistTools PROPERTIES TYPE RECOMMENDED
230     DESCRIPTION "contains tools for handling translation files"
231     PURPOSE "Required for having translations"
232 )
233
234 if (BUILD_GUI)
235     if (NOT WIN32)
236         find_package(Qt5DBus QUIET)
237         set_package_properties(Qt5DBus PROPERTIES TYPE RECOMMENDED
238             URL "https://www.qt.io/"
239             DESCRIPTION "D-Bus support for Qt5"
240             PURPOSE     "Needed for supporting D-Bus-based notifications and tray icon, used by most modern desktop environments"
241         )
242         if (Qt5DBus_FOUND)
243             find_package(dbusmenu-qt5 QUIET CONFIG)
244             set_package_properties(dbusmenu-qt5 PROPERTIES TYPE RECOMMENDED
245                 URL "https://launchpad.net/libdbusmenu-qt"
246                 DESCRIPTION "a library implementing the DBusMenu specification"
247                 PURPOSE     "Required for having a context menu for the D-Bus-based tray icon"
248             )
249         endif()
250     endif()
251
252     find_package(Qt5Multimedia QUIET)
253     set_package_properties(Qt5Multimedia PROPERTIES TYPE RECOMMENDED
254         URL "https://www.qt.io/"
255         DESCRIPTION "Multimedia support for Qt5"
256         PURPOSE     "Required for audio notifications"
257     )
258
259     # snorenotify segfaults on startup on msys2
260     # we don't check for just MSYS to support the Ninja generator
261     if(NOT (WIN32 AND (NOT $ENV{MSYSTEM} STREQUAL "")))
262         find_package(LibsnoreQt5 0.7.0 QUIET)
263         set_package_properties(LibsnoreQt5 PROPERTIES TYPE OPTIONAL
264             URL "https://projects.kde.org/projects/playground/libs/snorenotify"
265             DESCRIPTION "a cross-platform notification framework"
266             PURPOSE     "Enable support for the snorenotify framework"
267         )
268         if (LibsnoreQt5_FOUND)
269             find_package(LibsnoreSettingsQt5 QUIET)
270             set_package_properties(LibsnoreSettingsQt5 PROPERTIES TYPE OPTIONAL
271                 URL "https://projects.kde.org/projects/playground/libs/snorenotify"
272                 DESCRIPTION "a cross-platform notification framework"
273                 PURPOSE     "Enable support for the snorenotify framework"
274             )
275         endif()
276     endif()
277
278     if (WITH_WEBENGINE)
279         find_package(Qt5WebEngine QUIET)
280         set_package_properties(Qt5WebEngine PROPERTIES TYPE RECOMMENDED
281             URL "https://www.qt.io/"
282             DESCRIPTION "a WebEngine implementation for Qt"
283             PURPOSE     "Needed for displaying previews for URLs in chat"
284         )
285         if (Qt5WebEngine_FOUND)
286             find_package(Qt5WebEngineWidgets QUIET)
287             set_package_properties(Qt5WebEngineWidgets PROPERTIES TYPE RECOMMENDED
288                 URL "https://www.qt.io/"
289                 DESCRIPTION "widgets for Qt's WebEngine implementation"
290                 PURPOSE     "Needed for displaying previews for URLs in chat"
291             )
292         endif()
293     endif()
294
295     if (WITH_WEBENGINE AND Qt5WebEngineWidgets_FOUND)
296         set(HAVE_WEBENGINE true)
297     endif()
298     add_feature_info("WITH_WEBENGINE, QtWebEngine and QtWebEngineWidgets modules" HAVE_WEBENGINE "Support showing previews for URLs in chat")
299
300     if (NOT HAVE_WEBENGINE)
301         if (WITH_WEBKIT)
302             find_package(Qt5WebKit QUIET)
303             set_package_properties(Qt5WebKit PROPERTIES TYPE OPTIONAL
304                 URL "https://www.qt.io/"
305                 DESCRIPTION "a WebKit implementation for Qt"
306                 PURPOSE     "Needed for displaying previews for URLs in chat"
307                 )
308             if (Qt5WebKit_FOUND)
309                 find_package(Qt5WebKitWidgets QUIET)
310                 set_package_properties(Qt5WebKitWidgets PROPERTIES TYPE OPTIONAL
311                     URL "https://www.qt.io/"
312                     DESCRIPTION "widgets for Qt's WebKit implementation"
313                     PURPOSE     "Needed for displaying previews for URLs in chat"
314                     )
315             endif()
316         endif()
317
318         if (WITH_WEBKIT AND Qt5WebKitWidgets_FOUND)
319             set(HAVE_WEBKIT true)
320         endif()
321         add_feature_info("WITH_WEBKIT, QtWebKit and QtWebKitWidgets modules" HAVE_WEBKIT "Support showing previews for URLs in chat (legacy)")
322     endif()
323
324     # KDE Frameworks
325     ################
326
327     # extra-cmake-modules
328     if (WITH_KDE)
329         set(ecm_find_type "REQUIRED")
330         find_package(ECM NO_MODULE REQUIRED)
331     else()
332         # Even with KDE integration disabled, we optionally use tier1 frameworks if we find them
333         set(ecm_find_type "RECOMMENDED")
334         find_package(ECM NO_MODULE QUIET)
335     endif()
336
337     set_package_properties(ECM PROPERTIES TYPE ${ecm_find_type}
338         URL "https://projects.kde.org/projects/kdesupport/extra-cmake-modules"
339         DESCRIPTION "extra modules for CMake, maintained by the KDE project"
340         PURPOSE     "Required to find KDE Frameworks components"
341     )
342
343     if (ECM_FOUND)
344         list(APPEND CMAKE_MODULE_PATH ${ECM_MODULE_PATH})
345         if (WITH_KDE)
346             find_package(KF5 REQUIRED COMPONENTS ConfigWidgets CoreAddons Notifications NotifyConfig Sonnet TextWidgets WidgetsAddons XmlGui)
347             set_package_properties(KF5 PROPERTIES TYPE REQUIRED
348                 URL "http://www.kde.org"
349                 DESCRIPTION "KDE Frameworks"
350                 PURPOSE     "Required for integration into the Plasma desktop"
351             )
352             message(STATUS "Found KDE Frameworks ${KF5_VERSION}")
353         endif()
354
355         # Optional KF5 tier1 components
356         find_package(KF5Sonnet QUIET)
357         set_package_properties(KF5Sonnet PROPERTIES TYPE RECOMMENDED
358             URL "http://api.kde.org/frameworks-api/frameworks5-apidocs/sonnet/html"
359             DESCRIPTION "framework for providing spell-checking capabilities"
360             PURPOSE "Enables spell-checking support in input widgets"
361         )
362     endif()
363 endif()
364
365 if (BUILD_CORE)
366     find_package(Qca-qt5 2.0 QUIET)
367     set_package_properties(Qca-qt5 PROPERTIES TYPE RECOMMENDED
368         URL "https://projects.kde.org/projects/kdesupport/qca"
369         DESCRIPTION "Qt Cryptographic Architecture"
370         PURPOSE "Required for encryption support"
371     )
372
373     if (WITH_LDAP)
374         find_package(Ldap QUIET)
375         set_package_properties(Ldap PROPERTIES TYPE OPTIONAL
376             URL "http://www.openldap.org/"
377             DESCRIPTION "LDAP (Lightweight Directory Access Protocol) libraries"
378             PURPOSE "Enables core user authentication via LDAP"
379         )
380     endif()
381 endif()
382
383 # Non-Qt-based packages
384 #####################################################################
385
386 find_package(Boost 1.54 REQUIRED)
387 set_package_properties(Boost PROPERTIES TYPE REQUIRED
388     URL "https://www.boost.org/"
389     DESCRIPTION "Boost libraries for C++"
390 )
391 # Older versions don't define the imported target
392 if (NOT TARGET Boost::boost)
393     add_library(Boost::boost INTERFACE IMPORTED GLOBAL)
394     if (Boost_INCLUDE_DIRS)
395         set_target_properties(Boost::boost PROPERTIES
396             INTERFACE_INCLUDE_DIRECTORIES "${Boost_INCLUDE_DIRS}")
397     endif()
398 endif()
399
400 find_package(ZLIB REQUIRED)
401 set_package_properties(ZLIB PROPERTIES TYPE REQUIRED
402     URL "http://www.zlib.net"
403     DESCRIPTION "a popular compression library"
404     PURPOSE     "Used for protocol compression"
405 )
406
407 if (NOT WIN32)
408     # Needed for generating backtraces
409     find_package(Backtrace QUIET)
410     set_package_properties(Backtrace PROPERTIES TYPE RECOMMENDED
411         DESCRIPTION "a header (and possibly library) for inspecting backtraces"
412         PURPOSE "Used for generating backtraces in case of a crash"
413     )
414 endif()
415
416 # Shared library support
417 #####################################################################
418
419 option(ENABLE_SHARED "Build modules as shared libraries" ON)
420 add_feature_info(ENABLE_SHARED ENABLE_SHARED "Build modules as shared libraries")
421
422 # Setup unit testing
423 #####################################################################
424
425 option(BUILD_TESTING "Enable unit tests" OFF)
426 add_feature_info(BUILD_TESTING BUILD_TESTING "Build unit tests")
427
428 if (BUILD_TESTING)
429     find_package(GTest QUIET)
430     set_package_properties(GTest PROPERTIES TYPE REQUIRED
431         DESCRIPTION "Google's unit testing framework"
432         PURPOSE "Required for building unit tests"
433     )
434
435     find_package(Qt5Test QUIET)
436     set_package_properties(Qt5Test PROPERTIES TYPE REQUIRED
437         DESCRIPTION "unit testing library for the Qt5 framework"
438         PURPOSE "Required for building unit tests"
439     )
440     enable_testing()
441
442     # GTest messes with CMAKE_CXX_FLAGS, so process them again
443     process_cmake_cxx_flags()
444 endif()
445
446 # Setup support for KDE Frameworks
447 #####################################################################
448
449 if (WITH_KDE)
450     add_definitions(-DHAVE_KDE -DHAVE_KF5)
451     set(WITH_KF5 TRUE)
452
453     # If KDE Frameworks are present, they're most probably providing Qt5 integration including icon loading
454     set(EMBED_DATA OFF)
455
456     include(KDEInstallDirs)
457 endif()
458
459 # This needs to come after setting up KDE integration, so we can use KDE-specific paths
460 include(QuasselInstallDirs)
461
462 # RPATH and output settings
463 #####################################################################
464
465 # Build artifacts in a well-known location; especially important for Windows DLLs
466 # (which go into RUNTIME_OUTPUT_DIRECTORY and can thus be found by executables)
467 set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")
468 set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
469 set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
470
471 # These RPATH settings allow for running directly from the build dir
472 set(CMAKE_SKIP_BUILD_RPATH            FALSE)
473 set(CMAKE_BUILD_WITH_INSTALL_RPATH    FALSE)
474 set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE )
475
476 # Set install RPATH only if libdir isn't a system directory
477 if (IS_ABSOLUTE "${CMAKE_INSTALL_LIBDIR}")
478     set(libdir "${CMAKE_INSTALL_LIBDIR}")
479 else()
480     set(libdir "${CMAKE_INSTALL_PREFIX}/${CMAKE_INSTALL_LIBDIR}")
481 endif()
482 list(FIND CMAKE_PLATFORM_IMPLICIT_LINK_DIRECTORIES "${libdir}" is_systemdir)
483 if ("${is_systemdir}" STREQUAL "-1")
484    set(CMAKE_INSTALL_RPATH "${libdir}")
485 endif()
486
487 # Various config-dependent checks and settings
488 #####################################################################
489
490 # Check for syslog support
491 if (NOT WIN32)
492     check_include_file_cxx(syslog.h HAVE_SYSLOG)
493     add_feature_info("syslog.h" HAVE_SYSLOG "Provide support for logging to the syslog")
494 endif()
495
496 if (NOT WIN32)
497     check_function_exists(umask HAVE_UMASK)
498 endif()
499
500 if (EMBED_DATA)
501     message(STATUS "Embedding data files into the binary")
502 else()
503     message(STATUS "Installing data files separately")
504 endif()
505
506 # Windows-specific stuff
507 #####################################################################
508
509 if (WIN32)
510     link_libraries(imm32 winmm dbghelp Secur32)  # missing by default :/
511     if (MSVC)
512         link_libraries(Version dwmapi shlwapi)
513     endif()
514 endif()
515
516 # Prepare the build
517 #####################################################################
518
519 # Add needed subdirs - the order is important, since src needs some vars set by other dirs
520 add_subdirectory(data)
521 add_subdirectory(icons)
522 add_subdirectory(pics)
523 add_subdirectory(po)
524
525 # Set up and display feature summary
526 #####################################################################
527
528 feature_summary(WHAT ALL
529                 INCLUDE_QUIET_PACKAGES
530                 FATAL_ON_MISSING_REQUIRED_PACKAGES
531 )
532
533 # Finally, compile the sources
534 # We want this after displaying the feature summary to avoid ugly
535 # CMake backtraces in case a required Qt5 module is missing
536 #####################################################################
537
538 add_subdirectory(src)
539
540 # Build tests if so desired
541 if (BUILD_TESTING)
542     add_subdirectory(tests)
543 endif()