Switch to C++20
[qBittorrent.git] / cmake / Modules / CommonConfig.cmake
blob1db86d8a5a705a8d6ba5bc84e81920fba4c0d804
1 # Set platform variables and create some interface-only library targets
2 # that some or all other targets will link to, either directly or transitively,
3 # to consume common compile options/definitions
5 # treat value specified by the CXX_STANDARD target property as a requirement by default
6 set(CMAKE_CXX_STANDARD_REQUIRED ON)
7 set(CMAKE_CXX_EXTENSIONS OFF)
9 set(CMAKE_AUTOMOC ON)
10 set(CMAKE_AUTORCC ON)
11 set(CMAKE_AUTORCC_OPTIONS --compress 9 --threshold 5)
13 add_library(qbt_common_cfg INTERFACE)
15 # Full C++ 20 support is required
16 # See also https://cmake.org/cmake/help/latest/prop_gbl/CMAKE_CXX_KNOWN_FEATURES.html
17 # for a breakdown of the features that CMake recognizes for each C++ standard
18 target_compile_features(qbt_common_cfg INTERFACE
19     cxx_std_20
22 target_compile_definitions(qbt_common_cfg INTERFACE
23     QT_DISABLE_DEPRECATED_BEFORE=0x050f02
24     QT_NO_CAST_FROM_ASCII
25     QT_NO_CAST_TO_ASCII
26     QT_NO_CAST_FROM_BYTEARRAY
27     QT_NO_NARROWING_CONVERSIONS_IN_CONNECT
28     QT_USE_QSTRINGBUILDER
29     QT_STRICT_ITERATORS
30     $<$<NOT:$<CONFIG:Debug>>:QT_NO_DEBUG_OUTPUT>
33 if (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
34     target_compile_definitions(qbt_common_cfg INTERFACE
35         _DARWIN_FEATURE_64_BIT_INODE
36     )
37 endif()
39 if (CMAKE_SYSTEM_NAME STREQUAL "Windows")
40     target_compile_definitions(qbt_common_cfg INTERFACE
41         NTDDI_VERSION=0x06010000
42         _WIN32_WINNT=0x0601
43         _WIN32_IE=0x0601
44         WIN32_LEAN_AND_MEAN
45         NOMINMAX
46         UNICODE
47         _UNICODE
48     )
49 endif()
51 if ((CMAKE_CXX_COMPILER_ID STREQUAL "GNU") OR (CMAKE_CXX_COMPILER_ID STREQUAL "Clang") OR (CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang"))
52     target_compile_options(qbt_common_cfg INTERFACE
53         -Wall
54         -Wextra
55         -Wcast-qual
56         -Wcast-align
57         -Winvalid-pch
58         -Woverloaded-virtual
59         -Wold-style-cast
60         -Wnon-virtual-dtor
61         -pedantic
62         -pedantic-errors
63     )
65     # Clang 11 still doesn't support -Wstrict-null-sentinel
66     include(CheckCXXCompilerFlag)
67     check_cxx_compiler_flag(-Wstrict-null-sentinel SNS_SUPPORT)
68     if (SNS_SUPPORT)
69         target_compile_options(qbt_common_cfg INTERFACE -Wstrict-null-sentinel)
70     endif()
71 endif()
73 if ((CMAKE_CXX_COMPILER_ID STREQUAL "Clang") OR (CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang"))
74     target_compile_options(qbt_common_cfg INTERFACE
75         -Wno-range-loop-analysis
76     )
77 endif()
79 if (MINGW)
80     target_link_options(qbt_common_cfg INTERFACE $<$<OR:$<CONFIG:Debug>,$<CONFIG:RelWithDebInfo>>:LINKER:--dynamicbase>)
81 endif()
83 if (MSVC)
84     target_compile_options(qbt_common_cfg INTERFACE
85         /guard:cf
86         /utf-8
87         # https://devblogs.microsoft.com/cppblog/msvc-now-correctly-reports-__cplusplus/
88         /Zc:__cplusplus
89     )
90     target_link_options(qbt_common_cfg INTERFACE
91         /guard:cf
92         $<$<NOT:$<CONFIG:Debug>>:/OPT:REF /OPT:ICF>
93         # suppress linking warning due to /INCREMENTAL and /OPT:ICF being both ON
94         $<$<CONFIG:RelWithDebInfo>:/INCREMENTAL:NO>
95     )
97     if (MSVC_RUNTIME_DYNAMIC)
98         set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
99     else()
100         set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")
101     endif()
102 endif()
104 if (DBUS)
105     target_compile_definitions(qbt_common_cfg INTERFACE QBT_USES_DBUS)
106 endif()
108 if (LibtorrentRasterbar_VERSION VERSION_GREATER_EQUAL ${minLibtorrentVersion})
109     target_compile_definitions(qbt_common_cfg INTERFACE QBT_USES_LIBTORRENT2)
110 endif()