flags.cmake 8.0 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)
L
liaogang 已提交
8
    if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
9 10
        if(${CMAKE_CXX_COMPILER_VERSION} VERSION_LESS 5.4)
            message(FATAL_ERROR "Unsupported GCC version. GCC >= 5.4 required.")
11 12
        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 已提交
13 14 15 16 17 18 19 20 21 22
        endif()
    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()
23 24
            if (${CMAKE_CXX_COMPILER_VERSION} VERSION_LESS 3.4)
                message(FATAL_ERROR "Unsupported Clang version. Clang >= 3.4 required.")
L
liaogang 已提交
25
            endif()
D
dzhwinter 已提交
26
        endif()
L
liaogang 已提交
27 28 29
    endif()
endfunction()

30
CheckCompilerCXX14Flag()
31 32 33 34 35 36
if(NOT WIN32)
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")
else()
    set(CMAKE_CXX_STANDARD 14)
endif()

Z
zhangjinchao01 已提交
37 38 39 40 41 42
# 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 已提交
43
# rest arguments: not used.
Z
zhangjinchao01 已提交
44 45 46
function(safe_set_flag is_c src_list flag_name)
    string(REPLACE "-" "_" safe_name ${flag_name})
    string(REPLACE "=" "_" safe_name ${safe_name})
47 48 49 50 51 52

    if(${flag_name} MATCHES "fsanitize")
        set(CMAKE_REQUIRED_FLAGS_RETAINED ${CMAKE_REQUIRED_FLAGS})
        set(CMAKE_REQUIRED_FLAGS ${flag_name})
    endif()

Z
zhangjinchao01 已提交
53 54 55 56 57 58 59 60 61 62
    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()
63 64 65 66

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

# helper macro to set cflag
macro(safe_set_cflag src_list flag_name)
    safe_set_flag(ON ${src_list} ${flag_name})
endmacro()

# helper macro to set cxxflag
macro(safe_set_cxxflag src_list flag_name)
    safe_set_flag(OFF ${src_list} ${flag_name})
endmacro()

G
gangliao 已提交
79 80 81 82 83 84 85
# helper macro to set nvcc flag
macro(safe_set_nvflag flag_name)
    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})
T
T8T9 已提交
86
        set(SAFE_GPU_COMMON_FLAGS "${SAFE_GPU_COMMON_FLAGS} -Xcompiler=\"${flag_name}\"")
G
gangliao 已提交
87 88 89 90
    endif()
endmacro()


Z
zhangjinchao01 已提交
91 92 93 94
CHECK_CXX_SYMBOL_EXISTS(UINT64_MAX "stdint.h" UINT64_MAX_EXISTS)
if(NOT UINT64_MAX_EXISTS)
  set(CMAKE_REQUIRED_DEFINITIONS -D__STDC_LIMIT_MACROS)
  CHECK_CXX_SYMBOL_EXISTS(UINT64_MAX "stdint.h" UINT64_MAX_EXISTS_HERE)
W
wangjiangb 已提交
95
  if(UINT64_MAX_EXISTS_HERE)
Z
zhangjinchao01 已提交
96 97 98 99 100 101 102
    set(CMAKE_REQUIRED_DEFINITIONS)
    add_definitions(-D__STDC_LIMIT_MACROS)
  else()
    message(FATAL_ERROR "Cannot find symbol UINT64_MAX")
  endif()
endif()

103 104 105 106 107 108 109 110 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)
if(SPINLOCK_FOUND)
  add_definitions(-DPADDLE_USE_PTHREAD_SPINLOCK)
endif(SPINLOCK_FOUND)
if(BARRIER_FOUND)
  add_definitions(-DPADDLE_USE_PTHREAD_BARRIER)
endif(BARRIER_FOUND)
SET(CMAKE_EXTRA_INCLUDE_FILES "")

114 115 116 117 118 119 120 121 122 123 124 125 126 127
# Only one sanitizer is allowed in compile time
string(TOLOWER "${SANITIZER_TYPE}" sanitizer_type)
if(sanitizer_type STREQUAL "address")
    set(fsanitize "-fsanitize=address")
elseif(sanitizer_type STREQUAL "leak")
    set(fsanitize "-fsanitize=leak")
elseif(sanitizer_type STREQUAL "memory")
    set(fsanitize "-fsanitize=memory")
elseif(sanitizer_type STREQUAL "thread")
    set(fsanitize "-fsanitize=thread")
elseif(sanitizer_type STREQUAL "undefined")
    set(fsanitize "-fsanitize=undefined")
endif()

Z
zhangjinchao01 已提交
128 129
# 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 已提交
130 131 132

# https://github.com/PaddlePaddle/Paddle/issues/12773
if (NOT WIN32)
Z
zhangjinchao01 已提交
133 134 135
set(COMMON_FLAGS
    -fPIC
    -fno-omit-frame-pointer
D
dzhwinter 已提交
136
    -Werror
Z
zhangjinchao01 已提交
137 138
    -Wall
    -Wextra
D
dzhwinter 已提交
139
    -Wnon-virtual-dtor
Z
zhangjinchao01 已提交
140 141
    -Wdelete-non-virtual-dtor
    -Wno-unused-parameter
G
gangliao 已提交
142 143
    -Wno-unused-function
    -Wno-error=literal-suffix
144
    -Wno-error=unused-local-typedefs
145
    -Wno-error=parentheses-equality # Warnings in pybind11
Y
Yu Yang 已提交
146 147
    -Wno-error=ignored-attributes  # Warnings in Eigen, gcc 6.3
    -Wno-error=terminate  # Warning in PADDLE_ENFORCE
S
sabreshao 已提交
148 149
    -Wno-error=int-in-bool-context # Warning in Eigen gcc 7.2
    -Wimplicit-fallthrough=0 # Warning in tinyformat.h
150
    -Wno-error=maybe-uninitialized # Warning in boost gcc 7.2
151
    ${fsanitize}
152
)
G
gangliao 已提交
153

J
jianghaicheng 已提交
154 155 156 157 158 159 160
if(WITH_IPU)
    set(COMMON_FLAGS ${COMMON_FLAGS} 
        -Wno-sign-compare # Warnings in Popart
        -Wno-non-virtual-dtor # Warnings in Popart
    )
endif()

161
if(NOT APPLE)
162
    if((${CMAKE_CXX_COMPILER_VERSION} VERSION_GREATER 8.0) OR (WITH_ROCM))
163 164 165 166 167 168
        set(COMMON_FLAGS
                ${COMMON_FLAGS}
                -Wno-format-truncation # Warning in boost gcc 8.2
                -Wno-error=cast-function-type # Warning in boost gcc 8.2
                -Wno-error=parentheses # Warning in boost gcc 8.2
                -Wno-error=catch-value # Warning in boost gcc 8.2
169 170
                -Wno-error=nonnull-compare # Warning in boost gcc 8.2
                -Wno-error=address # Warning in boost gcc 8.2
171
                -Wno-ignored-qualifiers # Warning in boost gcc 8.2
T
T8T9 已提交
172
                -Wno-ignored-attributes # Warning in Eigen gcc 8.3
Z
Zeng Jinle 已提交
173
                -Wno-parentheses # Warning in Eigen gcc 8.3
174 175 176 177
                )
    endif()
endif(NOT APPLE)

G
gangliao 已提交
178 179 180 181 182 183 184
set(GPU_COMMON_FLAGS
    -fPIC
    -fno-omit-frame-pointer
    -Wnon-virtual-dtor
    -Wdelete-non-virtual-dtor
    -Wno-unused-parameter
    -Wno-unused-function
Z
zhangjinchao01 已提交
185
    -Wno-error=literal-suffix
186 187
    -Wno-error=unused-local-typedefs
    -Wno-error=unused-function  # Warnings in Numpy Header.
Q
qijun 已提交
188
    -Wno-error=array-bounds # Warnings in Eigen::array
189
)
W
Wilber 已提交
190
if (NOT WITH_NV_JETSON AND NOT WITH_ARM AND NOT WITH_SW AND NOT WITH_MIPS)
191 192
  set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -m64")
endif()
D
dzhwinter 已提交
193 194
endif(NOT WIN32)

G
gangliao 已提交
195
if (APPLE)
T
tianshuo78520a 已提交
196 197 198 199 200
    if(WITH_ARM)
      set (CMAKE_OSX_ARCHITECTURES "arm64" CACHE STRING "Build architectures for OSX" FORCE)
    else(WITH_ARM)
     set (CMAKE_OSX_ARCHITECTURES "x86_64" CACHE STRING "Build architectures for OSX" FORCE)
    endif(WITH_ARM)
J
JiabinYang 已提交
201
    # On Mac OS X register class specifier is deprecated and will cause warning error on latest clang 10.0
J
JiabinYang 已提交
202
    set (COMMON_FLAGS -Wno-deprecated-register)
D
dzhwinter 已提交
203 204 205
endif(APPLE)

if(LINUX)
G
gangliao 已提交
206 207 208
    set(GPU_COMMON_FLAGS
        -Wall
        -Wextra
209
        -Werror
G
gangliao 已提交
210
        ${GPU_COMMON_FLAGS})
D
dzhwinter 已提交
211
endif(LINUX)
G
gangliao 已提交
212

Z
zhangjinchao01 已提交
213 214 215 216 217
foreach(flag ${COMMON_FLAGS})
    safe_set_cflag(CMAKE_C_FLAGS ${flag})
    safe_set_cxxflag(CMAKE_CXX_FLAGS ${flag})
endforeach()

T
T8T9 已提交
218
set(SAFE_GPU_COMMON_FLAGS "")
G
gangliao 已提交
219 220 221
foreach(flag ${GPU_COMMON_FLAGS})
    safe_set_nvflag(${flag})
endforeach()
D
dzhwinter 已提交
222

223 224 225 226 227 228 229 230 231 232 233 234 235
if(WITH_GPU)
    set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} ${SAFE_GPU_COMMON_FLAGS}")
endif()

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

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