flags.cmake 6.8 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 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28

function(CheckCompilerCXX11Flag)
    if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
        if(${CMAKE_CXX_COMPILER_VERSION} VERSION_LESS 4.8)
            message(FATAL_ERROR "Unsupported GCC version. GCC >= 4.8 required.")
        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()
            if (${CMAKE_CXX_COMPILER_VERSION} VERSION_LESS 3.3)
                message(FATAL_ERROR "Unsupported Clang version. Clang >= 3.3 required.")
            endif()
        endif()   
    endif()
endfunction()

CheckCompilerCXX11Flag()
W
wanghaoshuang 已提交
29
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
Z
zhangjinchao01 已提交
30 31 32 33 34 35
# 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 已提交
36
# rest arguments: not used.
Z
zhangjinchao01 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
function(safe_set_flag is_c src_list flag_name)
    string(REPLACE "-" "_" safe_name ${flag_name})
    string(REPLACE "=" "_" safe_name ${safe_name})
    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()
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 已提交
62 63 64 65 66 67 68
# 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})
L
liaogang 已提交
69
        LIST(APPEND CUDA_NVCC_FLAGS -Xcompiler ${flag_name})
G
gangliao 已提交
70 71 72
    endif()
endmacro()

D
dzhwinter 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86
macro(safe_set_static_flag) # set c_flags and cxx_flags to static or shared
    if (BUILD_SHARED_LIBS) 
        return() # if build shared libs, the flags keep same with '/MD'
    endif(BUILD_SHARED_LIBS)
    foreach(flag_var
        CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
        CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO
        CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
        CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO)
      if(${flag_var} MATCHES "/MD")
        string(REGEX REPLACE "/MD" "/MT" ${flag_var} "${${flag_var}}")
      endif(${flag_var} MATCHES "/MD")
    endforeach(flag_var)
endmacro()
G
gangliao 已提交
87

Z
zhangjinchao01 已提交
88 89 90 91
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 已提交
92
  if(UINT64_MAX_EXISTS_HERE)
Z
zhangjinchao01 已提交
93 94 95 96 97 98 99
    set(CMAKE_REQUIRED_DEFINITIONS)
    add_definitions(-D__STDC_LIMIT_MACROS)
  else()
    message(FATAL_ERROR "Cannot find symbol UINT64_MAX")
  endif()
endif()

100 101 102 103 104 105 106 107 108 109 110
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 "")

Z
zhangjinchao01 已提交
111 112
# 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 已提交
113 114 115

# https://github.com/PaddlePaddle/Paddle/issues/12773
if (NOT WIN32)
Z
zhangjinchao01 已提交
116 117 118
set(COMMON_FLAGS
    -fPIC
    -fno-omit-frame-pointer
D
dzhwinter 已提交
119
    -Werror
Z
zhangjinchao01 已提交
120 121
    -Wall
    -Wextra
D
dzhwinter 已提交
122
    -Wnon-virtual-dtor
Z
zhangjinchao01 已提交
123 124
    -Wdelete-non-virtual-dtor
    -Wno-unused-parameter
G
gangliao 已提交
125 126
    -Wno-unused-function
    -Wno-error=literal-suffix
127
    -Wno-error=sign-compare
128
    -Wno-error=unused-local-typedefs
129
    -Wno-error=parentheses-equality # Warnings in pybind11
Y
Yu Yang 已提交
130 131
    -Wno-error=ignored-attributes  # Warnings in Eigen, gcc 6.3
    -Wno-error=terminate  # Warning in PADDLE_ENFORCE
S
sabreshao 已提交
132 133 134
    -Wno-error=int-in-bool-context # Warning in Eigen gcc 7.2
    -Wimplicit-fallthrough=0 # Warning in tinyformat.h
    -Wno-error=maybe-uninitialized # Warning in boost gcc 7.2
135
)
G
gangliao 已提交
136 137 138 139 140 141 142 143

set(GPU_COMMON_FLAGS
    -fPIC
    -fno-omit-frame-pointer
    -Wnon-virtual-dtor
    -Wdelete-non-virtual-dtor
    -Wno-unused-parameter
    -Wno-unused-function
144
    -Wno-error=sign-compare
Z
zhangjinchao01 已提交
145
    -Wno-error=literal-suffix
146 147
    -Wno-error=unused-local-typedefs
    -Wno-error=unused-function  # Warnings in Numpy Header.
Q
qijun 已提交
148
    -Wno-error=array-bounds # Warnings in Eigen::array
149
)
150

D
dzhwinter 已提交
151 152 153 154 155 156 157
else(NOT WIN32)
set(COMMON_FLAGS
    "/w") #disable all warnings.
set(GPU_COMMON_FLAGS
    "/w") #disable all warnings
endif(NOT WIN32)

G
gangliao 已提交
158
if (APPLE)
159 160 161 162
    if(NOT CMAKE_CROSSCOMPILING)
        # On Mac OS X build fat binaries with x86_64 architectures by default.
        set (CMAKE_OSX_ARCHITECTURES "x86_64" CACHE STRING "Build architectures for OSX" FORCE)
    endif()
J
JiabinYang 已提交
163
    # On Mac OS X register class specifier is deprecated and will cause warning error on latest clang 10.0
J
JiabinYang 已提交
164
    set (COMMON_FLAGS -Wno-deprecated-register)
D
dzhwinter 已提交
165 166 167
endif(APPLE)

if(LINUX)
G
gangliao 已提交
168 169 170
    set(GPU_COMMON_FLAGS
        -Wall
        -Wextra
171
        -Werror
G
gangliao 已提交
172
        ${GPU_COMMON_FLAGS})
D
dzhwinter 已提交
173
endif(LINUX)
G
gangliao 已提交
174

D
dzhwinter 已提交
175 176 177 178 179
if(UNIX AND NOT APPLE)
  # except apple from nix*Os family
  set(LINUX TRUE)
endif(UNIX AND NOT APPLE)

Z
zhangjinchao01 已提交
180 181 182
foreach(flag ${COMMON_FLAGS})
    safe_set_cflag(CMAKE_C_FLAGS ${flag})
    safe_set_cxxflag(CMAKE_CXX_FLAGS ${flag})
D
dzhwinter 已提交
183

Z
zhangjinchao01 已提交
184 185
endforeach()

G
gangliao 已提交
186 187 188
foreach(flag ${GPU_COMMON_FLAGS})
    safe_set_nvflag(${flag})
endforeach()
D
dzhwinter 已提交
189

D
dzhwinter 已提交
190
if(WIN32)
D
dzhwinter 已提交
191
# windows build turn off warnings.
D
dzhwinter 已提交
192
safe_set_static_flag()
D
dzhwinter 已提交
193 194 195 196 197 198 199 200 201 202
    foreach(flag_var
        CMAKE_CXX_FLAGS CMAKE_CXX_FLAGS_DEBUG CMAKE_CXX_FLAGS_RELEASE
        CMAKE_CXX_FLAGS_MINSIZEREL CMAKE_CXX_FLAGS_RELWITHDEBINFO
        CMAKE_C_FLAGS CMAKE_C_FLAGS_DEBUG CMAKE_C_FLAGS_RELEASE
        CMAKE_C_FLAGS_MINSIZEREL CMAKE_C_FLAGS_RELWITHDEBINFO)
      if(${flag_var} MATCHES "/W3")
        string(REGEX REPLACE "/W3" "/w" ${flag_var} "${${flag_var}}")
      endif(${flag_var} MATCHES "/W3")
    endforeach(flag_var)
endif(WIN32)