flags.cmake 7.1 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

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()
12 13 14 15 16 17
        if(NOT ANDROID)
            # TODO(qijun) gcc 4.9 or later versions raise SEGV due to the optimization problem.
            # Use Debug mode instead for now.
            if(CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 4.9 OR CMAKE_CXX_COMPILER_VERSION VERSION_EQUAL 4.9)
                set(CMAKE_BUILD_TYPE "Debug" CACHE STRING "" FORCE)
            endif()
18
        endif()
L
liaogang 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
    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()
36
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
L
liaogang 已提交
37

Z
zhangjinchao01 已提交
38 39 40 41 42 43
# 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 已提交
44
# rest arguments: not used.
Z
zhangjinchao01 已提交
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
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 已提交
70 71 72 73 74 75 76
# 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 已提交
77
        LIST(APPEND CUDA_NVCC_FLAGS -Xcompiler ${flag_name})
G
gangliao 已提交
78 79 80 81
    endif()
endmacro()


Z
zhangjinchao01 已提交
82 83 84 85
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 已提交
86
  if(UINT64_MAX_EXISTS_HERE)
Z
zhangjinchao01 已提交
87 88 89 90 91 92 93
    set(CMAKE_REQUIRED_DEFINITIONS)
    add_definitions(-D__STDC_LIMIT_MACROS)
  else()
    message(FATAL_ERROR "Cannot find symbol UINT64_MAX")
  endif()
endif()

94 95 96 97 98 99 100 101 102 103 104
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 已提交
105 106 107 108 109 110 111 112 113 114 115
# Common flags. the compiler flag used for C/C++ sources whenever release or debug
# Do not care if this flag is support for gcc.
set(COMMON_FLAGS
    -fPIC
    -fno-omit-frame-pointer
    -Wall
    -Wextra
    -Werror
    -Wnon-virtual-dtor
    -Wdelete-non-virtual-dtor
    -Wno-unused-parameter
G
gangliao 已提交
116 117
    -Wno-unused-function
    -Wno-error=literal-suffix
118
    -Wno-error=sign-compare
119
    -Wno-error=unused-local-typedefs
120
    -Wno-error=parentheses-equality # Warnings in pybind11
121
)
G
gangliao 已提交
122 123 124 125 126 127 128 129

set(GPU_COMMON_FLAGS
    -fPIC
    -fno-omit-frame-pointer
    -Wnon-virtual-dtor
    -Wdelete-non-virtual-dtor
    -Wno-unused-parameter
    -Wno-unused-function
130
    -Wno-error=sign-compare
Z
zhangjinchao01 已提交
131
    -Wno-error=literal-suffix
132 133
    -Wno-error=unused-local-typedefs
    -Wno-error=unused-function  # Warnings in Numpy Header.
Q
qijun 已提交
134
    -Wno-error=array-bounds # Warnings in Eigen::array
135
)
Z
zhangjinchao01 已提交
136

G
gangliao 已提交
137 138 139 140 141 142 143 144 145 146 147 148
if (APPLE)
    # 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)
else()
    set(GPU_COMMON_FLAGS
        -Wall
        -Wextra
        -Werror
        ${GPU_COMMON_FLAGS})
endif()


Z
zhangjinchao01 已提交
149 150 151 152 153
foreach(flag ${COMMON_FLAGS})
    safe_set_cflag(CMAKE_C_FLAGS ${flag})
    safe_set_cxxflag(CMAKE_CXX_FLAGS ${flag})
endforeach()

G
gangliao 已提交
154 155 156 157
foreach(flag ${GPU_COMMON_FLAGS})
    safe_set_nvflag(${flag})
endforeach()

G
gangliao 已提交
158

L
liaogang 已提交
159 160
set(CUDA_PROPAGATE_HOST_FLAGS OFF)

Z
zhangjinchao01 已提交
161 162
# Release/Debug flags set by cmake. Such as -O3 -g -DNDEBUG etc.
# So, don't set these flags here.
Q
qijun 已提交
163
LIST(APPEND CUDA_NVCC_FLAGS -std=c++11 --default-stream per-thread)
L
liaogang 已提交
164 165 166 167 168 169 170 171 172 173 174
LIST(APPEND CUDA_NVCC_FLAGS --use_fast_math)

if(CMAKE_BUILD_TYPE  STREQUAL "Debug")
    LIST(APPEND CUDA_NVCC_FLAGS  ${CMAKE_CXX_FLAGS_DEBUG})
elseif(CMAKE_BUILD_TYPE  STREQUAL "Release")
    LIST(APPEND CUDA_NVCC_FLAGS  ${CMAKE_CXX_FLAGS_RELEASE})
elseif(CMAKE_BUILD_TYPE  STREQUAL "RelWithDebInfo")
    LIST(APPEND CUDA_NVCC_FLAGS  ${CMAKE_CXX_FLAGS_RELWITHDEBINFO})
elseif(CMAKE_BUILD_TYPE  STREQUAL "MinSizeRel")
    LIST(APPEND CUDA_NVCC_FLAGS  ${CMAKE_CXX_FLAGS_MINSIZEREL})
endif()
Z
zhangjinchao01 已提交
175

L
liaogang 已提交
176 177
function(specify_cuda_arch cuda_version cuda_arch)
    if(${cuda_version} VERSION_GREATER "8.0")
178
        foreach(capability 61 62)
L
liaogang 已提交
179 180 181 182
          if(${cuda_arch} STREQUAL ${capability})
            list(APPEND __arch_flags " -gencode arch=compute_${cuda_arch},code=sm_${cuda_arch}")
          endif()
        endforeach()
L
liaogang 已提交
183 184
    elseif(${cuda_version} VERSION_GREATER "7.0" and ${cuda_arch} STREQUAL "53")
        list(APPEND __arch_flags " -gencode arch=compute_${cuda_arch},code=sm_${cuda_arch}")
L
liaogang 已提交
185 186 187
    endif()
endfunction()

188
# Common gpu architectures: Kepler, Maxwell
Z
zhangjinchao01 已提交
189
foreach(capability 30 35 50)
G
gangliao 已提交
190
      list(APPEND __arch_flags " -gencode arch=compute_${capability},code=sm_${capability}")
Z
zhangjinchao01 已提交
191 192
endforeach()

G
gangliao 已提交
193
if (CUDA_VERSION VERSION_GREATER "7.0" OR CUDA_VERSION VERSION_EQUAL "7.0")
L
liaogang 已提交
194 195 196
      list(APPEND __arch_flags " -gencode arch=compute_52,code=sm_52")
endif()

197
# Modern gpu architectures: Pascal
G
gangliao 已提交
198
if (CUDA_VERSION VERSION_GREATER "8.0" OR CUDA_VERSION VERSION_EQUAL "8.0")
199
      list(APPEND __arch_flags " -gencode arch=compute_60,code=sm_60")
200
      list(APPEND CUDA_NVCC_FLAGS --expt-relaxed-constexpr)
201 202 203
endif()

# Custom gpu architecture
L
liaogang 已提交
204
set(CUDA_ARCH)
Z
zhangjinchao01 已提交
205

L
liaogang 已提交
206 207 208
if(CUDA_ARCH)
  specify_cuda_arch(${CUDA_VERSION} ${CUDA_ARCH})
endif()
Z
zhangjinchao01 已提交
209

W
wangjiangb 已提交
210
set(CUDA_NVCC_FLAGS ${__arch_flags} ${CUDA_NVCC_FLAGS})
Y
Yu Yang 已提交
211