1# - Configures C++14 compiler, setting TDLib-specific compilation options. 2 3function(td_set_up_compiler) 4 set(CMAKE_EXPORT_COMPILE_COMMANDS 1 PARENT_SCOPE) 5 6 set(CMAKE_POSITION_INDEPENDENT_CODE ON PARENT_SCOPE) 7 8 include(illumos) 9 10 if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") 11 set(GCC 1) 12 set(GCC 1 PARENT_SCOPE) 13 elseif (CMAKE_CXX_COMPILER_ID MATCHES "Clang") 14 set(CLANG 1) 15 set(CLANG 1 PARENT_SCOPE) 16 elseif (CMAKE_CXX_COMPILER_ID STREQUAL "Intel") 17 set(INTEL 1) 18 set(INTEL 1 PARENT_SCOPE) 19 elseif (NOT MSVC) 20 message(FATAL_ERROR "Compiler isn't supported") 21 endif() 22 23 include(CheckCXXCompilerFlag) 24 25 if (GCC OR CLANG OR INTEL) 26 if (WIN32 AND INTEL) 27 set(STD14_FLAG /Qstd=c++14) 28 else() 29 set(STD14_FLAG -std=c++14) 30 endif() 31 check_cxx_compiler_flag(${STD14_FLAG} HAVE_STD14) 32 if (NOT HAVE_STD14) 33 string(REPLACE "c++14" "c++1y" STD14_FLAG "${STD14_FLAG}") 34 check_cxx_compiler_flag(${STD14_FLAG} HAVE_STD1Y) 35 set(HAVE_STD14 ${HAVE_STD1Y}) 36 endif() 37 elseif (MSVC) 38 set(HAVE_STD14 MSVC_VERSION>=1900) 39 endif() 40 41 if (NOT HAVE_STD14) 42 message(FATAL_ERROR "No C++14 support in the compiler. Please upgrade the compiler.") 43 endif() 44 45 if (MSVC) 46 if (CMAKE_CXX_FLAGS_DEBUG MATCHES "/RTC1") 47 string(REPLACE "/RTC1" " " CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}") 48 endif() 49 add_definitions(-D_SCL_SECURE_NO_WARNINGS -D_CRT_SECURE_NO_WARNINGS) 50 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /utf-8 /GR- /W4 /wd4100 /wd4127 /wd4324 /wd4505 /wd4814 /wd4702 /bigobj") 51 elseif (CLANG OR GCC) 52 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${STD14_FLAG} -fno-omit-frame-pointer -fno-exceptions -fno-rtti") 53 if (APPLE) 54 set(TD_LINKER_FLAGS "-Wl,-dead_strip") 55 if (NOT CMAKE_BUILD_TYPE MATCHES "Deb") 56 set(TD_LINKER_FLAGS "${TD_LINKER_FLAGS},-x,-S") 57 endif() 58 else() 59 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -ffunction-sections -fdata-sections") 60 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -ffunction-sections -fdata-sections") 61 if (CMAKE_SYSTEM_NAME STREQUAL "SunOS") 62 set(TD_LINKER_FLAGS "-Wl,-z,ignore") 63 else() 64 set(TD_LINKER_FLAGS "-Wl,--gc-sections -Wl,--exclude-libs,ALL") 65 endif() 66 endif() 67 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${TD_LINKER_FLAGS}") 68 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${TD_LINKER_FLAGS}") 69 70 if (WIN32 OR CYGWIN) 71 if (GCC) 72 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wa,-mbig-obj") 73 endif() 74 endif() 75 elseif (INTEL) 76 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${STD14_FLAG}") 77 endif() 78 79 if (WIN32) 80 add_definitions(-DNTDDI_VERSION=0x06020000 -DWINVER=0x0602 -D_WIN32_WINNT=0x0602 -DPSAPI_VERSION=1 -DNOMINMAX -DUNICODE -D_UNICODE -DWIN32_LEAN_AND_MEAN) 81 endif() 82 if (CYGWIN) 83 add_definitions(-D_DEFAULT_SOURCE=1 -DFD_SETSIZE=4096) 84 endif() 85 86 if (NOT ANDROID) # _FILE_OFFSET_BITS is broken in NDK r15, r15b and r17 and doesn't work prior to Android 7.0 87 add_definitions(-D_FILE_OFFSET_BITS=64) 88 endif() 89 90 if (CMAKE_SYSTEM_NAME STREQUAL "SunOS") 91 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lsocket -lnsl") 92 if (ILLUMOS) 93 add_definitions(-DTD_ILLUMOS=1) 94 endif() 95 endif() 96 97 include(AddCXXCompilerFlag) 98 if (NOT MSVC) 99 add_cxx_compiler_flag("-Wall") 100 add_cxx_compiler_flag("-Wextra") 101 add_cxx_compiler_flag("-Wimplicit-fallthrough=2") 102 add_cxx_compiler_flag("-Wpointer-arith") 103 add_cxx_compiler_flag("-Wcast-qual") 104 add_cxx_compiler_flag("-Wsign-compare") 105 add_cxx_compiler_flag("-Wduplicated-branches") 106 add_cxx_compiler_flag("-Wduplicated-cond") 107 add_cxx_compiler_flag("-Walloc-zero") 108 add_cxx_compiler_flag("-Wlogical-op") 109 add_cxx_compiler_flag("-Wno-tautological-compare") 110 add_cxx_compiler_flag("-Wpointer-arith") 111 add_cxx_compiler_flag("-Wvla") 112 add_cxx_compiler_flag("-Wnon-virtual-dtor") 113 add_cxx_compiler_flag("-Wno-unused-parameter") 114 add_cxx_compiler_flag("-Wconversion") 115 add_cxx_compiler_flag("-Wno-sign-conversion") 116 add_cxx_compiler_flag("-Wc++14-compat-pedantic") 117 add_cxx_compiler_flag("-Wdeprecated") 118 add_cxx_compiler_flag("-Wno-unused-command-line-argument") 119 add_cxx_compiler_flag("-Qunused-arguments") 120 add_cxx_compiler_flag("-Wodr") 121 add_cxx_compiler_flag("-flto-odr-type-merging") 122 123 # add_cxx_compiler_flag("-Werror") 124 125 # add_cxx_compiler_flag("-Wcast-align") 126 127 #std::int32_t <-> int and off_t <-> std::size_t/std::int64_t 128 # add_cxx_compiler_flag("-Wuseless-cast") 129 130 #external headers like openssl 131 # add_cxx_compiler_flag("-Wzero-as-null-pointer-constant") 132 endif() 133 134 if (GCC AND NOT (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 7.0)) 135 add_cxx_compiler_flag("-Wno-maybe-uninitialized") # too much false positives 136 endif() 137 if (WIN32 AND GCC AND NOT (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 8.0)) 138 # warns about casts of function pointers returned by GetProcAddress 139 add_cxx_compiler_flag("-Wno-cast-function-type") 140 endif() 141 if (GCC AND NOT (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9.0)) 142 # warns about a lot of "return std::move", which are not redundant for compilers without fix for DR 1579, i.e. GCC 4.9 or clang 3.8 143 # see http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#1579 144 add_cxx_compiler_flag("-Wno-redundant-move") 145 endif() 146 if (CLANG AND (CMAKE_CXX_COMPILER_VERSION VERSION_LESS 3.5)) 147 # https://stackoverflow.com/questions/26744556/warning-returning-a-captured-reference-from-a-lambda 148 add_cxx_compiler_flag("-Wno-return-stack-address") 149 endif() 150 151 #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -isystem /usr/include/c++/v1") 152 #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++") 153 #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread") 154 #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=address") 155 #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=undefined") 156 #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=leak") 157 158 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" PARENT_SCOPE) 159 set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG}" PARENT_SCOPE) 160 set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}" PARENT_SCOPE) 161 set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS}" PARENT_SCOPE) 162 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS}" PARENT_SCOPE) 163endfunction() 164