TokuSetupCompiler.cmake 6.64 KB
Newer Older
1 2 3 4
function(add_c_defines)
  set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS ${ARGN})
endfunction(add_c_defines)

5
if (APPLE)
6
  add_c_defines(DARWIN=1 _DARWIN_C_SOURCE)
7
endif ()
8 9 10 11 12

## preprocessor definitions we want everywhere
add_c_defines(
  _FILE_OFFSET_BITS=64
  _LARGEFILE64_SOURCE
13 14
  __STDC_FORMAT_MACROS
  __STDC_LIMIT_MACROS
15
  __LONG_LONG_SUPPORTED
16
  )
17 18 19 20 21 22 23
if (NOT CMAKE_SYSTEM_NAME STREQUAL FreeBSD)
  ## on FreeBSD these types of macros actually remove functionality
  add_c_defines(
    _SVID_SOURCE
    _XOPEN_SOURCE=600
    )
endif ()
24

25
## add TOKU_PTHREAD_DEBUG for debug builds
Leif Walsh's avatar
Leif Walsh committed
26 27
set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS_DEBUG TOKU_PTHREAD_DEBUG=1)
set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS_RELWITHDEBINFO TOKU_PTHREAD_DEBUG=1)
28
set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS_RELWITHDEBINFO _FORTIFY_SOURCE=2)
29

30 31 32
## coverage
option(USE_GCOV "Use gcov for test coverage." OFF)
if (USE_GCOV)
33
  if (NOT CMAKE_CXX_COMPILER_ID MATCHES GNU)
34 35
    message(FATAL_ERROR "Must use the GNU compiler to compile for test coverage.")
  endif ()
TokuBuild's avatar
TokuBuild committed
36
  find_program(COVERAGE_COMMAND NAMES gcov47 gcov)
37 38 39
endif (USE_GCOV)

include(CheckCCompilerFlag)
40
include(CheckCXXCompilerFlag)
41

42 43 44 45
## adds a compiler flag if the compiler supports it
macro(set_cflags_if_supported_named flag flagname)
  check_c_compiler_flag("${flag}" HAVE_C_${flagname})
  if (HAVE_C_${flagname})
46
    set(CMAKE_C_FLAGS "${flag} ${CMAKE_C_FLAGS}")
47 48 49
  endif ()
  check_cxx_compiler_flag("${flag}" HAVE_CXX_${flagname})
  if (HAVE_CXX_${flagname})
50
    set(CMAKE_CXX_FLAGS "${flag} ${CMAKE_CXX_FLAGS}")
51 52 53
  endif ()
endmacro(set_cflags_if_supported_named)

54
## adds a compiler flag if the compiler supports it
55
macro(set_cflags_if_supported)
56
  foreach(flag ${ARGN})
57 58
    check_c_compiler_flag(${flag} HAVE_C_${flag})
    if (HAVE_C_${flag})
59
      set(CMAKE_C_FLAGS "${flag} ${CMAKE_C_FLAGS}")
60
    endif ()
61 62
    check_cxx_compiler_flag(${flag} HAVE_CXX_${flag})
    if (HAVE_CXX_${flag})
63
      set(CMAKE_CXX_FLAGS "${flag} ${CMAKE_CXX_FLAGS}")
64
    endif ()
65
  endforeach(flag)
66
endmacro(set_cflags_if_supported)
67

68 69 70 71 72
## adds a linker flag if the compiler supports it
macro(set_ldflags_if_supported)
  foreach(flag ${ARGN})
    check_cxx_compiler_flag(${flag} HAVE_${flag})
    if (HAVE_${flag})
73 74
      set(CMAKE_EXE_LINKER_FLAGS "${flag} ${CMAKE_EXE_LINKER_FLAGS}")
      set(CMAKE_SHARED_LINKER_FLAGS "${flag} ${CMAKE_SHARED_LINKER_FLAGS}")
75 76 77 78
    endif ()
  endforeach(flag)
endmacro(set_ldflags_if_supported)

79 80 81
## disable some warnings
set_cflags_if_supported(
  -Wno-missing-field-initializers
82 83 84 85 86 87 88
  -Wstrict-null-sentinel
  -Winit-self
  -Wswitch
  -Wtrampolines
  -Wlogical-op
  -Wmissing-format-attribute
  -Wno-error=missing-format-attribute
89
  -Wno-error=address-of-array-temporary
90 91
  -fno-rtti
  -fno-exceptions
92
  )
93
## set_cflags_if_supported_named("-Weffc++" -Weffcpp)
94 95 96 97 98 99 100 101

## Clang has stricter POD checks.  So, only enable this warning on our other builds (Linux + GCC)
if (NOT CMAKE_CXX_COMPILER_ID MATCHES Clang)
  set_cflags_if_supported(
    -Wpacked
    )
endif ()

102 103
## this hits with optimized builds somewhere in ftleaf_split, we don't
## know why but we don't think it's a big deal
104 105 106
set_cflags_if_supported(
  -Wno-error=strict-overflow
  )
107 108
set_ldflags_if_supported(
  -Wno-error=strict-overflow
109
  )
110 111

## set extra debugging flags and preprocessor definitions
112 113
set(CMAKE_C_FLAGS_DEBUG "-g3 -O0 ${CMAKE_C_FLAGS_DEBUG}")
set(CMAKE_CXX_FLAGS_DEBUG "-g3 -O0 ${CMAKE_CXX_FLAGS_DEBUG}")
Leif Walsh's avatar
Leif Walsh committed
114 115 116 117 118

## The default for this is -g -O2 -DNDEBUG.
## Since we want none of those for drd, we just overwrite it.
set(CMAKE_C_FLAGS_RELWITHDEBINFO "-g3 -O1")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "-g3 -O1")
119

120 121 122
## set extra release flags
if (APPLE AND CMAKE_CXX_COMPILER_ID STREQUAL Clang)
  # have tried -flto and -O4, both make our statically linked executables break apple's linker
Leif Walsh's avatar
Leif Walsh committed
123 124
  set(CMAKE_C_FLAGS_RELEASE "-g -O3 ${CMAKE_C_FLAGS_RELEASE} -UNDEBUG")
  set(CMAKE_CXX_FLAGS_RELEASE "-g -O3 ${CMAKE_CXX_FLAGS_RELEASE} -UNDEBUG")
125 126
else ()
  # we overwrite this because the default passes -DNDEBUG and we don't want that
Leif Walsh's avatar
Leif Walsh committed
127 128
  set(CMAKE_C_FLAGS_RELEASE "-g -O3 -flto -fuse-linker-plugin ${CMAKE_C_FLAGS_RELEASE} -UNDEBUG")
  set(CMAKE_CXX_FLAGS_RELEASE "-g -O3 -flto -fuse-linker-plugin ${CMAKE_CXX_FLAGS_RELEASE} -UNDEBUG")
129 130
  set(CMAKE_EXE_LINKER_FLAGS "-g -fuse-linker-plugin ${CMAKE_EXE_LINKER_FLAGS}")
  set(CMAKE_SHARED_LINKER_FLAGS "-g -fuse-linker-plugin ${CMAKE_SHARED_LINKER_FLAGS}")
131
endif ()
132

133
## set warnings
Leif Walsh's avatar
Leif Walsh committed
134
set_cflags_if_supported(
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
  -Wextra
  -Wbad-function-cast
  -Wno-missing-noreturn
  -Wstrict-prototypes
  -Wmissing-prototypes
  -Wmissing-declarations
  -Wpointer-arith
  -Wmissing-format-attribute
  -Wshadow
  ## other flags to try:
  #-Wunsafe-loop-optimizations
  #-Wpointer-arith
  #-Wc++-compat
  #-Wc++11-compat
  #-Wwrite-strings
  #-Wzero-as-null-pointer-constant
  #-Wlogical-op
  #-Wvector-optimization-performance
  )
Leif Walsh's avatar
Leif Walsh committed
154

155 156
if (NOT CMAKE_CXX_COMPILER_ID STREQUAL Clang)
  # Disabling -Wcast-align with clang.  TODO: fix casting and re-enable it, someday.
Leif Walsh's avatar
Leif Walsh committed
157
  set_cflags_if_supported(-Wcast-align)
158
endif ()
Leif Walsh's avatar
Leif Walsh committed
159

Leif Walsh's avatar
Leif Walsh committed
160 161
## always want these
set(CMAKE_C_FLAGS "-Wall -Werror ${CMAKE_C_FLAGS}")
162
set(CMAKE_CXX_FLAGS "-Wall -Werror ${CMAKE_CXX_FLAGS}")
Leif Walsh's avatar
Leif Walsh committed
163

164 165 166 167 168
## need to set -stdlib=libc++ to get real c++11 support on darwin
if (APPLE)
  if (CMAKE_GENERATOR STREQUAL Xcode)
    set(CMAKE_XCODE_ATTRIBUTE_CLANG_CXX_LIBRARY "libc++")
  else ()
169
    add_definitions(-stdlib=libc++)
170 171 172 173 174 175 176 177
  endif ()
endif ()

# pick language dialect
set(CMAKE_C_FLAGS "-std=c99 ${CMAKE_C_FLAGS}")
check_cxx_compiler_flag(-std=c++11 HAVE_STDCXX11)
check_cxx_compiler_flag(-std=c++0x HAVE_STDCXX0X)
if (HAVE_STDCXX11)
Leif Walsh's avatar
Leif Walsh committed
178
  set(CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}")
179
elseif (HAVE_STDCXX0X)
Leif Walsh's avatar
Leif Walsh committed
180
  set(CMAKE_CXX_FLAGS "-std=c++0x ${CMAKE_CXX_FLAGS}")
181 182 183 184
else ()
  message(FATAL_ERROR "${CMAKE_CXX_COMPILER} doesn't support -std=c++11 or -std=c++0x, you need one that does.")
endif ()

185 186 187 188 189
function(add_space_separated_property type obj propname val)
  get_property(oldval ${type} ${obj} PROPERTY ${propname})
  if (oldval MATCHES NOTFOUND)
    set_property(${type} ${obj} PROPERTY ${propname} "${val}")
  else ()
190
    set_property(${type} ${obj} PROPERTY ${propname} "${val} ${oldval}")
Leif Walsh's avatar
Leif Walsh committed
191
  endif ()
192 193 194 195 196 197 198 199 200 201
endfunction(add_space_separated_property)

## this function makes sure that the libraries passed to it get compiled
## with gcov-needed flags, we only add those flags to our libraries
## because we don't really care whether our tests get covered
function(maybe_add_gcov_to_libraries)
  if (USE_GCOV)
    foreach(lib ${ARGN})
      add_space_separated_property(TARGET ${lib} COMPILE_FLAGS --coverage)
      add_space_separated_property(TARGET ${lib} LINK_FLAGS --coverage)
202
      target_link_libraries(${lib} gcov)
203 204 205
    endforeach(lib)
  endif (USE_GCOV)
endfunction(maybe_add_gcov_to_libraries)