flags.cmake 8.3 KB
Newer Older
Z
zhangjinchao01 已提交
1 2 3 4
# Setting Paddle Compile Flags
include(CheckCXXCompilerFlag)
include(CheckCCompilerFlag)
include(CheckCXXSymbolExists)
5
include(CheckTypeSize)
L
liaogang 已提交
6

7
function(CheckCompilerCXX14Flag)
8 9 10 11 12 13 14 15
  if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
    if(${CMAKE_CXX_COMPILER_VERSION} VERSION_LESS 5.4)
      message(FATAL_ERROR "Unsupported GCC version. GCC >= 5.4 required.")
    elseif(${CMAKE_CXX_COMPILER_VERSION} VERSION_GREATER 8.2)
      message(
        WARNING
          "Found GCC ${CMAKE_CXX_COMPILER_VERSION} which is too high, recommended to use GCC 8.2"
      )
L
liaogang 已提交
16
    endif()
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
  elseif(CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang" OR CMAKE_CXX_COMPILER_ID
                                                        STREQUAL "Clang")
    # cmake >= 3.0 compiler id "AppleClang" on Mac OS X, otherwise "Clang"
    # Apple Clang is a different compiler than upstream Clang which havs different version numbers.
    # https://gist.github.com/yamaya/2924292
    if(APPLE) # cmake < 3.0 compiler id "Clang" on Mac OS X
      if(${CMAKE_CXX_COMPILER_VERSION} VERSION_LESS 5.1)
        message(
          FATAL_ERROR
            "Unsupported AppleClang version. AppleClang >= 5.1 required.")
      endif()
    else()
      if(${CMAKE_CXX_COMPILER_VERSION} VERSION_LESS 3.4)
        message(FATAL_ERROR "Unsupported Clang version. Clang >= 3.4 required.")
      endif()
    endif()
  endif()
L
liaogang 已提交
34 35
endfunction()

36
checkcompilercxx14flag()
37
if(NOT WIN32)
38
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")
39
else()
40
  set(CMAKE_CXX_STANDARD 14)
41 42
endif()

Z
zhangjinchao01 已提交
43 44 45 46 47 48
# safe_set_flag
#
# Set a compile flag only if compiler is support
# is_c: is C flag or C++ flag, bool type.
# src_list: The list name which the flag name will be append to.
# flag_name: the flag name for compiler, such as '-Werror' '-Wall' etc
W
wangjiangb 已提交
49
# rest arguments: not used.
Z
zhangjinchao01 已提交
50
function(safe_set_flag is_c src_list flag_name)
51 52
  string(REPLACE "-" "_" safe_name ${flag_name})
  string(REPLACE "=" "_" safe_name ${safe_name})
53

54 55 56 57
  if(${flag_name} MATCHES "fsanitize")
    set(CMAKE_REQUIRED_FLAGS_RETAINED ${CMAKE_REQUIRED_FLAGS})
    set(CMAKE_REQUIRED_FLAGS ${flag_name})
  endif()
58

59 60 61 62 63 64 65 66 67 68 69 70
  if(is_c)
    check_c_compiler_flag(${flag_name} C_COMPILER_SUPPORT_FLAG_${safe_name})
    set(safe_name C_COMPILER_SUPPORT_FLAG_${safe_name})
  else()
    check_cxx_compiler_flag(${flag_name} CXX_COMPILER_SUPPORT_FLAG_${safe_name})
    set(safe_name CXX_COMPILER_SUPPORT_FLAG_${safe_name})
  endif()
  if(${safe_name})
    set(${src_list}
        "${${src_list}} ${flag_name}"
        PARENT_SCOPE)
  endif()
71

72 73 74
  if(${flag_name} MATCHES "fsanitize")
    set(CMAKE_REQUIRED_FLAGS ${CMAKE_REQUIRED_FLAGS_RETAINED})
  endif()
Z
zhangjinchao01 已提交
75 76 77 78
endfunction()

# helper macro to set cflag
macro(safe_set_cflag src_list flag_name)
79
  safe_set_flag(ON ${src_list} ${flag_name})
Z
zhangjinchao01 已提交
80 81 82 83
endmacro()

# helper macro to set cxxflag
macro(safe_set_cxxflag src_list flag_name)
84
  safe_set_flag(OFF ${src_list} ${flag_name})
Z
zhangjinchao01 已提交
85 86
endmacro()

G
gangliao 已提交
87 88
# helper macro to set nvcc flag
macro(safe_set_nvflag flag_name)
89 90 91 92 93 94 95 96
  string(REPLACE "-" "_" safe_name ${flag_name})
  string(REPLACE "=" "_" safe_name ${safe_name})
  check_c_compiler_flag(${flag_name} C_COMPILER_SUPPORT_FLAG_${safe_name})
  set(safe_name C_COMPILER_SUPPORT_FLAG_${safe_name})
  if(${safe_name})
    set(SAFE_GPU_COMMON_FLAGS
        "${SAFE_GPU_COMMON_FLAGS} -Xcompiler=\"${flag_name}\"")
  endif()
G
gangliao 已提交
97 98
endmacro()

99
check_cxx_symbol_exists(UINT64_MAX "stdint.h" UINT64_MAX_EXISTS)
Z
zhangjinchao01 已提交
100 101
if(NOT UINT64_MAX_EXISTS)
  set(CMAKE_REQUIRED_DEFINITIONS -D__STDC_LIMIT_MACROS)
102
  check_cxx_symbol_exists(UINT64_MAX "stdint.h" UINT64_MAX_EXISTS_HERE)
W
wangjiangb 已提交
103
  if(UINT64_MAX_EXISTS_HERE)
Z
zhangjinchao01 已提交
104 105 106 107 108 109 110
    set(CMAKE_REQUIRED_DEFINITIONS)
    add_definitions(-D__STDC_LIMIT_MACROS)
  else()
    message(FATAL_ERROR "Cannot find symbol UINT64_MAX")
  endif()
endif()

111 112 113
set(CMAKE_EXTRA_INCLUDE_FILES "pthread.h")
check_type_size(pthread_spinlock_t SPINLOCK_FOUND)
check_type_size(pthread_barrier_t BARRIER_FOUND)
114 115
if(SPINLOCK_FOUND)
  add_definitions(-DPADDLE_USE_PTHREAD_SPINLOCK)
W
Wilber 已提交
116
endif()
117 118
if(BARRIER_FOUND)
  add_definitions(-DPADDLE_USE_PTHREAD_BARRIER)
W
Wilber 已提交
119
endif()
120
set(CMAKE_EXTRA_INCLUDE_FILES "")
121

122 123 124
# Only one sanitizer is allowed in compile time
string(TOLOWER "${SANITIZER_TYPE}" sanitizer_type)
if(sanitizer_type STREQUAL "address")
125
  set(fsanitize "-fsanitize=address")
126
elseif(sanitizer_type STREQUAL "leak")
127
  set(fsanitize "-fsanitize=leak")
128
elseif(sanitizer_type STREQUAL "memory")
129
  set(fsanitize "-fsanitize=memory")
130
elseif(sanitizer_type STREQUAL "thread")
131
  set(fsanitize "-fsanitize=thread")
132
elseif(sanitizer_type STREQUAL "undefined")
133
  set(fsanitize "-fsanitize=undefined")
134 135
endif()

Z
zhangjinchao01 已提交
136 137
# Common flags. the compiler flag used for C/C++ sources whenever release or debug
# Do not care if this flag is support for gcc.
D
dzhwinter 已提交
138 139

# https://github.com/PaddlePaddle/Paddle/issues/12773
140 141 142 143 144 145 146 147 148 149 150 151
if(NOT WIN32)
  set(COMMON_FLAGS
      -fPIC
      -fno-omit-frame-pointer
      -Werror
      -Wall
      -Wextra
      -Wnon-virtual-dtor
      -Wdelete-non-virtual-dtor
      -Wno-unused-parameter
      -Wno-unused-function
      -Wno-error=literal-suffix
R
risemeup1 已提交
152
      -Wno-error=array-bounds #Warning in Eigen, gcc 12.2
153 154 155 156 157 158 159 160 161
      -Wno-error=ignored-attributes # Warnings in Eigen, gcc 6.3
      -Wno-error=terminate # Warning in PADDLE_ENFORCE
      -Wno-error=int-in-bool-context # Warning in Eigen gcc 7.2
      -Wimplicit-fallthrough=0 # Warning in tinyformat.h
      ${fsanitize})

  if(WITH_IPU)
    set(COMMON_FLAGS ${COMMON_FLAGS} -Wno-sign-compare # Warnings in Popart
                     -Wno-non-virtual-dtor # Warnings in Popart
J
jianghaicheng 已提交
162
    )
163
  endif()
J
jianghaicheng 已提交
164

165 166 167 168 169
  if(WITH_CUDNN_FRONTEND)
    # flags from https://github.com/NVIDIA/cudnn-frontend/blob/v0.7.1/CMakeLists.txt
    set(COMMON_FLAGS ${COMMON_FLAGS} -Wno-sign-compare -Wno-non-virtual-dtor)
  endif()

170
  if(NOT APPLE)
171
    if((${CMAKE_CXX_COMPILER_VERSION} VERSION_GREATER 8.0) OR (WITH_ROCM))
172
      set(COMMON_FLAGS
173
          ${COMMON_FLAGS} -Wno-ignored-qualifiers # Warning in Paddle-Lite
174 175 176
          -Wno-ignored-attributes # Warning in Eigen gcc 8.3
          -Wno-parentheses # Warning in Eigen gcc 8.3
      )
177
    endif()
W
Wilber 已提交
178
  endif()
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194

  set(GPU_COMMON_FLAGS
      -fPIC
      -fno-omit-frame-pointer
      -Wnon-virtual-dtor
      -Wdelete-non-virtual-dtor
      -Wno-unused-parameter
      -Wno-unused-function
      -Wno-error=literal-suffix
      -Wno-error=unused-local-typedefs
      -Wno-error=unused-function # Warnings in Numpy Header.
      -Wno-error=array-bounds # Warnings in Eigen::array
  )
  if(NOT WITH_NV_JETSON
     AND NOT WITH_ARM
     AND NOT WITH_SW
Z
Zhang Na 已提交
195 196
     AND NOT WITH_MIPS
     AND NOT WITH_LOONGARCH)
197 198
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m64")
  endif()
W
Wilber 已提交
199
endif()
D
dzhwinter 已提交
200

201 202 203 204 205
if(APPLE)
  if(WITH_ARM)
    set(CMAKE_OSX_ARCHITECTURES
        "arm64"
        CACHE STRING "Build architectures for OSX" FORCE)
W
Wilber 已提交
206
  else()
207 208 209
    set(CMAKE_OSX_ARCHITECTURES
        "x86_64"
        CACHE STRING "Build architectures for OSX" FORCE)
W
Wilber 已提交
210
  endif()
211
  # On Mac OS X register class specifier is deprecated and will cause warning error on latest clang 10.0
212
  set(COMMON_FLAGS
213 214 215 216 217 218
      -Wno-deprecated-register
      -Werror=format
      -Werror=inconsistent-missing-override
      -Werror=braced-scalar-init
      -Werror=uninitialized
      -Werror=tautological-constant-out-of-range-compare
219
      -Werror=literal-conversion
220 221
      -Werror=pragma-pack
      -Werror=c++17-extensions)
W
Wilber 已提交
222
endif()
D
dzhwinter 已提交
223

T
Thunderbrook 已提交
224
if(WITH_HETERPS AND WITH_PSLIB)
225
  set(COMMON_FLAGS -D_GLIBCXX_USE_CXX11_ABI=0 ${COMMON_FLAGS})
T
Thunderbrook 已提交
226

227
  set(GPU_COMMON_FLAGS -D_GLIBCXX_USE_CXX11_ABI=0 ${GPU_COMMON_FLAGS})
T
Thunderbrook 已提交
228 229
endif()

D
dzhwinter 已提交
230
if(LINUX)
231
  set(GPU_COMMON_FLAGS -Wall -Wextra -Werror ${GPU_COMMON_FLAGS})
W
Wilber 已提交
232
endif()
G
gangliao 已提交
233

Z
zhangjinchao01 已提交
234
foreach(flag ${COMMON_FLAGS})
235 236
  safe_set_cflag(CMAKE_C_FLAGS ${flag})
  safe_set_cxxflag(CMAKE_CXX_FLAGS ${flag})
Z
zhangjinchao01 已提交
237 238
endforeach()

T
T8T9 已提交
239
set(SAFE_GPU_COMMON_FLAGS "")
G
gangliao 已提交
240
foreach(flag ${GPU_COMMON_FLAGS})
241
  safe_set_nvflag(${flag})
G
gangliao 已提交
242
endforeach()
D
dzhwinter 已提交
243

244
if(WITH_GPU)
245
  set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} ${SAFE_GPU_COMMON_FLAGS}")
246 247 248
endif()

if(WITH_ROCM)
249
  set(HIP_HIPCC_FLAGS "${HIP_HIPCC_FLAGS} ${SAFE_GPU_COMMON_FLAGS}")
250 251
endif()

252
# Disable -Werror, otherwise the compile will fail for rocblas_gemm_ex
253
if(WITH_ROCM)
254 255
  string(REPLACE "-Werror" "-Wno-error" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
  string(REPLACE "-Werror" "-Wno-error" CMAKE_C_FLAGS ${CMAKE_C_FLAGS})
256
endif()
T
T8T9 已提交
257

W
Wilber 已提交
258 259 260
if(WITH_PSCORE
   OR WITH_PSLIB
   OR WITH_TENSORRT)
261 262 263 264
  string(REPLACE "-Wnon-virtual-dtor" "-Wno-non-virtual-dtor" CMAKE_CXX_FLAGS
                 ${CMAKE_CXX_FLAGS})
  string(REPLACE "-Wnon-virtual-dtor" "-Wno-non-virtual-dtor" CMAKE_C_FLAGS
                 ${CMAKE_C_FLAGS})
265
endif()