|
| 1 | +# Sets cache variable QBT_ADDITONAL_FLAGS and QBT_ADDITONAL_CXX_FLAGS to list of additional |
| 2 | +# compiler flags for C and C++ (QBT_ADDITONAL_FLAGS) and for C++ only (QBT_ADDITONAL_CXX_FLAGS) |
| 3 | +# and appends them to CMAKE_XXX_FLAGS variables. |
| 4 | + |
| 5 | +# It could use add_compile_options(), but then it is needed to use generator expressions, |
| 6 | +# and most interesting of them are not compatible with Visual Studio :( |
| 7 | + |
| 8 | +macro(qbt_set_compiler_options) |
| 9 | +# if (NOT QBT_ADDITONAL_FLAGS) |
| 10 | + if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") |
| 11 | + #-Wshadow -Wconversion ? |
| 12 | + set(_GCC_COMMON_C_AND_CXX_FLAGS "-Wall -Wextra" |
| 13 | + "-Wfloat-equal -Wcast-qual -Wcast-align" |
| 14 | + "-Wsign-conversion -Winvalid-pch -Werror=return-type -Wno-long-long" |
| 15 | +# -fstack-protector-all |
| 16 | + "-Werror -Wno-error=deprecated-declarations" |
| 17 | + ) |
| 18 | + set (_GCC_COMMON_CXX_FLAGS "-fexceptions -frtti" |
| 19 | + "-Woverloaded-virtual -Wold-style-cast -Wstrict-null-sentinel" |
| 20 | + "-Wnon-virtual-dtor -Wfloat-equal -Wcast-qual -Wcast-align" |
| 21 | + "-Werror=overloaded-virtual" |
| 22 | + # "-Weffc++" |
| 23 | + "-Werror -Wno-error=cpp" |
| 24 | + # we should modify code to make these ones obsolete |
| 25 | + "-Wno-error=old-style-cast -Wno-error=sign-conversion -Wno-error=float-equal" |
| 26 | + ) |
| 27 | + |
| 28 | + if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 4.9) |
| 29 | + # GCC 4.8 has problems with std::array and its initialization |
| 30 | + list(APPEND _GCC_COMMON_CXX_FLAGS "-Wno-error=missing-field-initializers") |
| 31 | + endif() |
| 32 | + |
| 33 | + include(CheckCXXCompilerFlag) |
| 34 | + # check for -pedantic |
| 35 | + check_cxx_compiler_flag(-pedantic _PEDANTIC_IS_SUPPORTED) |
| 36 | + if (_PEDANTIC_IS_SUPPORTED) |
| 37 | + list(APPEND _GCC_COMMON_CXX_FLAGS "-pedantic -pedantic-errors") |
| 38 | + else (_PEDANTIC_IS_SUPPORTED) |
| 39 | + list(APPEND _GCC_COMMON_CXX_FLAGS "-Wpedantic") |
| 40 | + endif (_PEDANTIC_IS_SUPPORTED) |
| 41 | + |
| 42 | + if (CMAKE_SYSTEM_NAME MATCHES Linux) |
| 43 | + # if Glibc version is 2.20 or higher, set -D_DEFAULT_SOURCE |
| 44 | + include(MacroGlibcDetect) |
| 45 | + message(STATUS "Detecting Glibc version...") |
| 46 | + glibc_detect(GLIBC_VERSION) |
| 47 | + if(${GLIBC_VERSION}) |
| 48 | + if(GLIBC_VERSION LESS "220") |
| 49 | + message(STATUS "Glibc version is ${GLIBC_VERSION}") |
| 50 | + else(GLIBC_VERSION LESS "220") |
| 51 | + message(STATUS "Glibc version is ${GLIBC_VERSION}, adding -D_DEFAULT_SOURCE") |
| 52 | + add_definitions(-D_DEFAULT_SOURCE) |
| 53 | + endif(GLIBC_VERSION LESS "220") |
| 54 | + endif(${GLIBC_VERSION}) |
| 55 | + endif (CMAKE_SYSTEM_NAME MATCHES Linux) |
| 56 | + |
| 57 | + string(REPLACE ";" " " _GCC_COMMON_C_AND_CXX_FLAGS_STRING "${_GCC_COMMON_C_AND_CXX_FLAGS}") |
| 58 | + string(REPLACE ";" " " _GCC_COMMON_CXX_FLAGS_STRING "${_GCC_COMMON_CXX_FLAGS}") |
| 59 | + |
| 60 | + string(APPEND CMAKE_C_FLAGS " ${_GCC_COMMON_C_AND_CXX_FLAGS_STRING}") |
| 61 | + string(APPEND CMAKE_CXX_FLAGS " ${_GCC_COMMON_C_AND_CXX_FLAGS_STRING} ${_GCC_COMMON_CXX_FLAGS_STRING}") |
| 62 | + |
| 63 | + set(QBT_ADDITONAL_FLAGS "${_GCC_COMMON_C_AND_CXX_FLAGS_STRING}" CACHE STRING |
| 64 | + "Additional qBittorent compile flags" FORCE) |
| 65 | + set(QBT_ADDITONAL_CXX_FLAGS "${_GCC_COMMON_CXX_FLAGS_STRING}" CACHE STRING |
| 66 | + "Additional qBittorent C++ compile flags" FORCE) |
| 67 | + |
| 68 | + # check whether we can enable -Og optimization for debug build |
| 69 | + # also let's enable -march=native for debug builds |
| 70 | + check_cxx_compiler_flag(-Og _DEBUG_OPTIMIZATION_LEVEL_IS_SUPPORTED) |
| 71 | + |
| 72 | + if (_DEBUG_OPTIMIZATION_LEVEL_IS_SUPPORTED) |
| 73 | + string(APPEND CMAKE_C_FLAGS_DEBUG " -Og -g3 -march=native -pipe" ) |
| 74 | + string(APPEND CMAKE_CXX_FLAGS_DEBUG " -Og -g3 -march=native -pipe" ) |
| 75 | + else(_DEBUG_OPTIMIZATION_LEVEL_IS_SUPPORTED) |
| 76 | + string(APPEND CMAKE_C_FLAGS_DEBUG " -O0 -g3 -march=native -pipe" ) |
| 77 | + string(APPEND CMAKE_CXX_FLAGS_DEBUG " -O0 -g3 -march=native -pipe" ) |
| 78 | + endif (_DEBUG_OPTIMIZATION_LEVEL_IS_SUPPORTED) |
| 79 | + endif ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "GNU" OR "${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang") |
| 80 | + |
| 81 | + if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "MSVC") |
| 82 | + set(QBT_ADDITONAL_FLAGS "-wd4290 -wd4275 -wd4251 /W4" CACHE STRING "Additional qBittorent compile flags") |
| 83 | + string(APPEND CMAKE_C_FLAGS " ${QBT_ADDITONAL_FLAGS}") |
| 84 | + string(APPEND CMAKE_CXX_FLAGS " ${QBT_ADDITONAL_FLAGS}") |
| 85 | + endif () |
| 86 | + |
| 87 | +# endif (NOT QBT_ADDITONAL_FLAGS) |
| 88 | +endmacro(qbt_set_compiler_options) |
| 89 | + |
0 commit comments