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)
2013-04-17 00:00:27 -04:00
if ( CMAKE_SYSTEM_NAME MATCHES Darwin )
2013-04-17 00:00:25 -04:00
add_c_defines ( DARWIN=1 _DARWIN_C_SOURCE )
2013-04-17 00:00:27 -04:00
elseif ( CMAKE_SYSTEM_NAME MATCHES Linux )
2013-04-17 00:00:49 -04:00
# add_c_defines(__linux__=1)
2013-04-17 00:00:27 -04:00
endif ( )
2013-04-17 00:00:25 -04:00
## 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
)
2013-04-17 00:00:27 -04:00
if ( CMAKE_SYSTEM_NAME MATCHES Darwin )
2013-04-17 00:00:25 -04:00
message ( WARNING "Setting TOKU_ALLOW_DEPRECATED on Darwin. TODO: remove this." )
add_c_defines ( TOKU_ALLOW_DEPRECATED )
2013-04-17 00:00:27 -04:00
endif ( )
2013-04-17 00:00:25 -04:00
## 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 )
include ( CheckCCompilerFlag )
2013-04-17 00:00:27 -04:00
## adds a compiler flag if the compiler supports it
2013-04-17 00:00:29 -04:00
macro ( set_cflags_if_supported )
2013-04-17 00:00:27 -04:00
foreach ( flag ${ ARGN } )
check_c_compiler_flag ( ${ flag } HAVE_ ${ flag } )
if ( HAVE_ ${ flag } )
set ( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}" )
endif ( )
endforeach ( flag )
2013-04-17 00:00:29 -04:00
endmacro ( set_cflags_if_supported )
2013-04-17 00:00:25 -04:00
2013-04-17 00:00:27 -04:00
## disable some warnings
set_cflags_if_supported (
- W n o - s e l f - a s s i g n
- W n o - m i s s i n g - f i e l d - i n i t i a l i z e r s
- W n o - m a y b e - u n i n i t i a l i z e d
)
2013-04-17 00:00:25 -04:00
## set extra debugging flags and preprocessor definitions
2013-04-17 00:00:27 -04:00
set ( CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g3 -ggdb -O0" )
2013-04-17 00:00:25 -04:00
set_property ( DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS_DEBUG FORTIFY_SOURCE=2 )
2013-04-17 00:00:27 -04:00
## set extra release flags, we overwrite this because the default passes -DNDEBUG and we don't want that
2013-04-17 00:00:49 -04:00
set ( CMAKE_C_FLAGS_RELEASE "-g -O3" )
2013-04-17 00:00:25 -04:00
## 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
2013-04-17 00:00:27 -04:00
if ( HAVE_CC_FLAG_FLTO )
2013-04-17 00:00:25 -04:00
set ( CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -flto" )
2013-04-17 00:00:27 -04:00
elseif ( HAVE_CC_FLAG_IPO )
2013-04-17 00:00:25 -04:00
set ( CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -ip -ipo1" )
2013-04-17 00:00:27 -04:00
endif ( )
2013-04-17 00:00:25 -04:00
2013-04-17 00:00:27 -04:00
if ( CMAKE_C_COMPILER_ID MATCHES Intel )
## make sure intel libs are linked statically
2013-04-17 00:00:49 -04:00
set ( CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -static-intel" )
2013-04-17 00:00:26 -04:00
2013-04-17 00:00:27 -04:00
## disable some intel-specific warnings
2013-04-17 00:00:25 -04:00
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:27 -04:00
## icc does -g differently
2013-04-17 00:00:49 -04:00
set ( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -debug all" )
2013-04-17 00:00:25 -04:00
2013-04-17 00:00:27 -04:00
## set icc warnings
2013-04-17 00:00:29 -04:00
set ( CMAKE_C_FLAGS "-Wcheck ${CMAKE_C_FLAGS}" )
2013-04-17 00:00:26 -04:00
else ( )
2013-04-17 00:00:27 -04:00
## set gcc warnings
2013-04-17 00:00:29 -04:00
set ( CMAKE_C_FLAGS "-Wextra ${CMAKE_C_FLAGS}" )
2013-04-17 00:00:27 -04:00
set ( WARN_CFLAGS
- W b a d - f u n c t i o n - c a s t
- W n o - m i s s i n g - n o r e t u r n
- W s t r i c t - p r o t o t y p e s
- W m i s s i n g - p r o t o t y p e s
- W m i s s i n g - d e c l a r a t i o n s
- W p o i n t e r - a r i t h
- W m i s s i n g - f o r m a t - a t t r i b u t e
)
2013-04-17 00:00:36 -04:00
if ( CMAKE_SYSTEM_NAME STREQUAL Darwin )
2013-04-17 00:00:49 -04:00
message ( WARNING "Disabling -Wcast-align and -Wshadow on osx. TODO: fix casting and shadowed declarations and re-enable them." )
2013-04-17 00:00:49 -04:00
elseif ( CMAKE_C_COMPILER_ID STREQUAL Clang )
message ( WARNING "Disabling -Wcast-align with clang. TODO: fix casting and re-enable it." )
list ( APPEND WARN_CFLAGS -Wshadow )
2013-04-17 00:00:36 -04:00
else ( )
2013-04-17 00:00:49 -04:00
list ( APPEND WARN_CFLAGS -Wcast-align -Wshadow )
2013-04-17 00:00:36 -04:00
endif ( )
2013-04-17 00:00:25 -04:00
endif ( )
2013-04-17 00:00:26 -04:00
2013-04-17 00:00:27 -04:00
set_cflags_if_supported ( ${ WARN_CFLAGS } )
2013-04-17 00:00:29 -04:00
## always want these
set ( CMAKE_C_FLAGS "-Wall -Werror ${CMAKE_C_FLAGS}" )
2013-04-17 00:00:26 -04:00
2013-04-17 00:00:49 -04:00
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 ( )
set_property ( ${ type } ${ obj } PROPERTY ${ propname } "${oldval} ${val}" )
2013-04-17 00:00:26 -04:00
endif ( )
2013-04-17 00:00:49 -04:00
endfunction ( add_space_separated_property )
function ( set_targets_need_intel_libs )
if ( CMAKE_C_COMPILER_ID STREQUAL Intel )
foreach ( tgt ${ ARGN } )
target_link_libraries ( ${ tgt } LINK_PUBLIC -Bstatic irc -Bdynamic c )
endforeach ( tgt )
endif ( )
endfunction ( set_targets_need_intel_libs )
## 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 )
target_link_libraries ( ${ lib } gcov )
endforeach ( lib )
endif ( USE_GCOV )
endfunction ( maybe_add_gcov_to_libraries )
2013-04-17 00:00:49 -04:00
## adds -fvisibility=hidden -fPIE to compile phase
## adds -pie (or -Wl,-pie) to link phase
## good for binaries
function ( add_common_options_to_binary_targets )
foreach ( tgt ${ ARGN } )
2013-04-17 00:00:49 -04:00
add_space_separated_property ( TARGET ${ tgt } COMPILE_FLAGS "-fvisibility=hidden -fPIE" )
2013-04-17 00:00:49 -04:00
if ( CMAKE_C_COMPILER_ID STREQUAL Clang )
2013-04-17 00:00:49 -04:00
add_space_separated_property ( TARGET ${ tgt } LINK_FLAGS "-Wl,-pie" )
2013-04-17 00:00:49 -04:00
else ( )
2013-04-17 00:00:49 -04:00
add_space_separated_property ( TARGET ${ tgt } LINK_FLAGS "-pie" )
2013-04-17 00:00:49 -04:00
endif ( )
endforeach ( tgt )
2013-04-17 00:00:49 -04:00
endfunction ( add_common_options_to_binary_targets )