TokuSetupCompiler.cmake 4.73 KB
Newer Older
1 2 3 4 5 6 7 8
## set c99 dialect
add_definitions("-std=c99")

function(add_c_defines)
  set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS ${ARGN})
endfunction(add_c_defines)

## os name detection (threadpool-test.c needs this)
9
if (CMAKE_SYSTEM_NAME MATCHES Darwin)
10
  add_c_defines(DARWIN=1 _DARWIN_C_SOURCE)
11
elseif (CMAKE_SYSTEM_NAME MATCHES Linux)
12
  add_c_defines(__linux__=1)
13
endif ()
14 15 16 17 18 19 20 21 22

## preprocessor definitions we want everywhere
add_c_defines(
  _SVID_SOURCE
  _XOPEN_SOURCE=600
  _FILE_OFFSET_BITS=64
  _LARGEFILE64_SOURCE
  )

23
if (CMAKE_SYSTEM_NAME MATCHES Darwin)
24 25
  message(WARNING "Setting TOKU_ALLOW_DEPRECATED on Darwin.  TODO: remove this.")
  add_c_defines(TOKU_ALLOW_DEPRECATED)
26
endif ()
27 28 29 30 31 32 33 34 35

## coverage
option(USE_GCOV "Use gcov for test coverage." OFF)
if (USE_GCOV)
  if (NOT CMAKE_C_COMPILER_ID MATCHES GNU)
    message(FATAL_ERROR "Must use the GNU compiler to compile for test coverage.")
  endif ()
endif (USE_GCOV)

36 37 38
## 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
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
function(maybe_add_gcov_to_libraries)
  if (USE_GCOV)
    foreach(lib ${ARGN})
      get_target_property(lib_compile_flags ${lib} COMPILE_FLAGS)
      if (lib_compile_flags MATCHES lib_compile_flags-NOTFOUND)
        set(lib_compile_flags "")
      endif ()
      get_target_property(lib_link_flags ${lib} LINK_FLAGS)
      if (lib_link_flags MATCHES lib_link_flags-NOTFOUND)
        set(lib_link_flags "")
      endif ()
      set_target_properties(${lib} PROPERTIES
        COMPILE_FLAGS "${lib_compile_flags} --coverage"
        LINK_FLAGS "${lib_link_flags} --coverage"
        )
      target_link_libraries(${lib} gcov)
    endforeach(lib)
  endif (USE_GCOV)
endfunction(maybe_add_gcov_to_libraries)

include(CheckCCompilerFlag)

61
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Werror")
62
## adds a compiler flag if the compiler supports it
63
macro(set_cflags_if_supported)
64 65 66 67 68 69
  foreach(flag ${ARGN})
    check_c_compiler_flag(${flag} HAVE_${flag})
    if (HAVE_${flag})
      set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}")
    endif ()
  endforeach(flag)
70
endmacro(set_cflags_if_supported)
71

72 73 74 75 76 77
## disable some warnings
set_cflags_if_supported(
  -Wno-self-assign
  -Wno-missing-field-initializers
  -Wno-maybe-uninitialized
  )
78 79

## set extra debugging flags and preprocessor definitions
80
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g3 -ggdb -O0")
81 82
set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS_DEBUG FORTIFY_SOURCE=2)

83 84
## set extra release flags, we overwrite this because the default passes -DNDEBUG and we don't want that
set(CMAKE_C_FLAGS_RELEASE "-O3")
85 86 87 88 89 90

## check how to do inter-procedural optimization
check_c_compiler_flag(-flto HAVE_CC_FLAG_FLTO)
check_c_compiler_flag(-ipo HAVE_CC_FLAG_IPO)

## add inter-procedural optimization flags
91
if (HAVE_CC_FLAG_FLTO)
92
  set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -flto")
93
elseif (HAVE_CC_FLAG_IPO)
94
  set(CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -ip -ipo1")
95
endif ()
96

97 98
if (CMAKE_C_COMPILER_ID MATCHES Intel)
  ## make sure intel libs are linked statically
Leif Walsh's avatar
Leif Walsh committed
99 100
  set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-intel")

101
  ## disable some intel-specific warnings
102 103 104 105 106 107 108 109 110 111 112
  set(intel_warnings
    94     # allow arrays of length 0
    589    # do not complain about goto that skips initialization
    2259   # do not complain about "non-pointer conversion from int to u_int8_t (and other small types) may lose significant bits".  this produces too many false positives
    11000  # do not remark about multi-file optimizations, single-file optimizations, and object temp files
    11001
    11006
    11003  # do not complain if some file was compiled without -ipo
    )
  string(REGEX REPLACE ";" "," intel_warning_string "${intel_warnings}")
  set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -diag-disable ${intel_warning_string}")
113 114

  ## icc does -g differently
Leif Walsh's avatar
Leif Walsh committed
115
  set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -debug all")
116

117 118 119 120
  ## set icc warnings
  set(WARN_CFLAGS
    -Wcheck  ## icc version of -Wextra
    )
Leif Walsh's avatar
Leif Walsh committed
121
else()
122 123 124 125 126 127 128 129 130 131 132 133 134
  ## set gcc warnings
  set(WARN_CFLAGS
    -Wextra
    -Wcast-align
    -Wbad-function-cast
    -Wno-missing-noreturn
    -Wstrict-prototypes
    -Wmissing-prototypes
    -Wmissing-declarations
    -Wpointer-arith
    -Wmissing-format-attribute
    -Wshadow
    )
135
endif()
Leif Walsh's avatar
Leif Walsh committed
136

137
set_cflags_if_supported(${WARN_CFLAGS})
Leif Walsh's avatar
Leif Walsh committed
138 139 140 141 142 143 144 145 146 147 148

## function for adding -fvisibility=hidden to targets
function(set_targets_visibility_hidden)
  if (NOT CMAKE_C_COMPILER_ID STREQUAL "Intel")
    foreach(target ${ARGN})
      get_target_property(flags ${target} COMPILE_FLAGS)
      set_target_properties(${target} PROPERTIES
        COMPILE_FLAGS "${COMPILE_FLAGS} -fvisibility=hidden")
    endforeach(target)
  endif ()
endfunction(set_targets_visibility_hidden)