2013-04-17 00:00:25 -04:00
## 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)
if ( CMAKE_SYSTEM_NAME MATCHES "Darwin" )
add_c_defines ( DARWIN=1 _DARWIN_C_SOURCE )
elseif ( CMAKE_SYSTEM_NAME MATCHES "Linux" )
add_c_defines ( __linux__=1 )
endif ( )
## preprocessor definitions we want everywhere
add_c_defines (
_ S V I D _ S O U R C E
_ X O P E N _ S O U R C E = 6 0 0
_ F I L E _ O F F S E T _ B I T S = 6 4
_ L A R G E F I L E 6 4 _ S O U R C E
)
if ( CMAKE_SYSTEM_NAME MATCHES Darwin )
message ( WARNING "Setting TOKU_ALLOW_DEPRECATED on Darwin. TODO: remove this." )
add_c_defines ( TOKU_ALLOW_DEPRECATED )
endif ( )
## 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 )
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
C O M P I L E _ F L A G S " $ { l i b _ c o m p i l e _ f l a g s } - - c o v e r a g e "
L I N K _ F L A G S " $ { l i b _ l i n k _ f l a g s } - - c o v e r a g e "
)
target_link_libraries ( ${ lib } gcov )
endforeach ( lib )
endif ( USE_GCOV )
endfunction ( maybe_add_gcov_to_libraries )
include ( CheckCCompilerFlag )
## disable some warnings, if we can
function ( set_flag_if_exists flag )
check_c_compiler_flag ( ${ flag } HAVE_ ${ flag } )
if ( HAVE_ ${ flag } )
set ( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}" )
endif ( )
endfunction ( set_flag_if_exists )
foreach ( flag -Wno-self-assign -Wno-missing-field-initializers -Wno-maybe-uninitialized )
set_flag_if_exists ( ${ flag } )
endforeach ( flag )
## set extra debugging flags and preprocessor definitions
set ( CMAKE_C_FLAGS_DEBUG "-g3 -ggdb -O0" )
set_property ( DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS_DEBUG FORTIFY_SOURCE=2 )
## set extra release flags
set ( CMAKE_C_FLAGS_RELEASE "-O3" )
## 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
if ( HAVE_CC_FLAG_FLTO )
set ( CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -flto" )
elseif ( HAVE_CC_FLAG_IPO )
set ( CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -ip -ipo1" )
endif ( )
if ( CMAKE_C_COMPILER_ID MATCHES "^Intel$" )
2013-04-17 00:00:26 -04:00
# make sure intel libs are linked statically
set ( CMAKE_SHARED_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -static-intel" )
2013-04-17 00:00:25 -04:00
# disable some intel-specific warnings
set ( intel_warnings
9 4 # allow arrays of length 0
5 8 9 # do not complain about goto that skips initialization
2 2 5 9 # 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
1 1 0 0 0 # do not remark about multi-file optimizations, single-file optimizations, and object temp files
1 1 0 0 1
1 1 0 0 6
1 1 0 0 3 # 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}" )
2013-04-17 00:00:26 -04:00
set ( CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -debug all" )
2013-04-17 00:00:25 -04:00
2013-04-17 00:00:26 -04:00
set ( EXTRA_CFLAGS "-Wall -Wcheck" )
else ( )
set ( EXTRA_CFLAGS "-Wall -Wextra -Wcast-align -Wbad-function-cast -Wno-missing-noreturn -Wstrict-prototypes -Wmissing-prototypes -Wmissing-declarations -Wpointer-arith -Wmissing-format-attribute -Wshadow" )
2013-04-17 00:00:25 -04:00
endif ( )
2013-04-17 00:00:26 -04:00
## default warning levels
set ( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EXTRA_CFLAGS}" )
## 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
C O M P I L E _ F L A G S " $ { C O M P I L E _ F L A G S } - f v i s i b i l i t y = h i d d e n " )
endforeach ( target )
endif ( )
endfunction ( set_targets_visibility_hidden )