2013-04-17 00:00:25 -04:00
function ( add_c_defines )
set_property ( DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS ${ ARGN } )
endfunction ( add_c_defines )
2013-04-17 00:00:59 -04:00
## os name detection (threadpool-test.cc 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:59 -04:00
_ _ S T D C _ F O R M A T _ M A C R O S
_ _ S T D C _ L I M I T _ M A C R O S
2013-04-17 00:00:25 -04:00
)
2013-04-17 00:00:59 -04:00
if ( CMAKE_SYSTEM_NAME STREQUAL Darwin OR CMAKE_CXX_COMPILER_ID MATCHES Clang )
2013-04-17 00:00:50 -04:00
message ( WARNING "Setting TOKU_ALLOW_DEPRECATED on Darwin and with clang. TODO: remove this." )
2013-04-17 00:00:25 -04:00
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 )
2013-04-17 00:00:59 -04:00
if ( NOT CMAKE_CXX_COMPILER_ID MATCHES GNU )
2013-04-17 00:00:25 -04:00
message ( FATAL_ERROR "Must use the GNU compiler to compile for test coverage." )
endif ( )
2013-04-17 00:01:01 -04:00
find_path ( COVERAGE_COMMAND NAMES gcov47 gcov )
2013-04-17 00:00:25 -04:00
endif ( USE_GCOV )
include ( CheckCCompilerFlag )
2013-04-17 00:00:59 -04:00
include ( CheckCXXCompilerFlag )
2013-04-17 00:00:25 -04:00
2013-04-17 00:01:00 -04:00
## 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 } )
set ( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}" )
endif ( )
check_cxx_compiler_flag ( "${flag}" HAVE_CXX_ ${ flagname } )
if ( HAVE_CXX_ ${ flagname } )
set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}" )
endif ( )
endmacro ( set_cflags_if_supported_named )
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 } )
2013-04-17 00:00:59 -04:00
check_c_compiler_flag ( ${ flag } HAVE_C_ ${ flag } )
if ( HAVE_C_ ${ flag } )
2013-04-17 00:00:27 -04:00
set ( CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${flag}" )
endif ( )
2013-04-17 00:00:59 -04:00
check_cxx_compiler_flag ( ${ flag } HAVE_CXX_ ${ flag } )
if ( HAVE_CXX_ ${ flag } )
set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${flag}" )
endif ( )
2013-04-17 00:00:27 -04:00
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:59 -04:00
## 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 } )
set ( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${flag}" )
set ( CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} ${flag}" )
endif ( )
endforeach ( flag )
endmacro ( set_ldflags_if_supported )
2013-04-17 00:00:27 -04:00
## disable some warnings
set_cflags_if_supported (
- 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
2013-04-17 00:00:59 -04:00
- W n o - e r r o r = s t r i c t - o v e r f l o w
2013-04-17 00:01:00 -04:00
- W s t r i c t - n u l l - s e n t i n e l
- W i n i t - s e l f
- W s w i t c h
- W t r a m p o l i n e s
- W l o g i c a l - o p
- W m i s s i n g - f o r m a t - a t t r i b u t e
- W n o - e r r o r = m i s s i n g - f o r m a t - a t t r i b u t e
- W p a c k e d
2013-04-17 00:00:59 -04:00
)
2013-04-17 00:01:00 -04:00
## set_cflags_if_supported_named("-Weffc++" -Weffcpp)
2013-04-17 00:00:59 -04:00
set_ldflags_if_supported (
- W n o - e r r o r = s t r i c t - o v e r f l o w
2013-04-17 00:00:27 -04:00
)
2013-04-17 00:00:25 -04:00
## set extra debugging flags and preprocessor definitions
2013-04-17 00:00:59 -04:00
set ( CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g3 -ggdb -O0" )
set ( CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_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:59 -04:00
if ( CMAKE_CXX_COMPILER_ID STREQUAL Clang AND CMAKE_SYSTEM_NAME STREQUAL Darwin )
2013-04-17 00:00:50 -04:00
set ( CMAKE_C_FLAGS_RELEASE "-g -O4" )
2013-04-17 00:00:59 -04:00
set ( CMAKE_CXX_FLAGS_RELEASE "-g -O4" )
2013-04-17 00:00:50 -04:00
else ( )
set ( CMAKE_C_FLAGS_RELEASE "-g -O3" )
2013-04-17 00:00:59 -04:00
set ( CMAKE_CXX_FLAGS_RELEASE "-g -O3" )
2013-04-17 00:00:25 -04:00
2013-04-17 00:00:50 -04:00
## check how to do inter-procedural optimization
2013-04-17 00:00:59 -04:00
check_c_compiler_flag ( -flto HAVE_C_FLAG_FLTO )
check_c_compiler_flag ( -ipo HAVE_C_FLAG_IPO )
if ( HAVE_C_FLAG_FLTO )
set ( CMAKE_C_FLAGS_RELEASE "-O3 -flto" ) # -fuse-linker-plugin")
elseif ( HAVE_C_FLAG_IPO )
2013-04-17 00:00:50 -04:00
set ( CMAKE_C_FLAGS_RELEASE "${CMAKE_C_FLAGS_RELEASE} -ip -ipo1" )
endif ( )
2013-04-17 00:00:59 -04:00
check_cxx_compiler_flag ( -flto HAVE_CXX_FLAG_FLTO )
check_cxx_compiler_flag ( -ipo HAVE_CXX_FLAG_IPO )
if ( HAVE_CXX_FLAG_FLTO )
set ( CMAKE_CXX_FLAGS_RELEASE "-O3 -flto" ) # -fuse-linker-plugin")
elseif ( HAVE_CXX_FLAG_IPO )
set ( CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -ip -ipo1" )
endif ( )
2013-04-17 00:00:27 -04:00
endif ( )
2013-04-17 00:00:53 -04:00
## but we do want -DNVALGRIND
set_property ( DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS_RELEASE NVALGRIND=1 )
2013-04-17 00:00:25 -04:00
2013-04-17 00:00:59 -04:00
if ( CMAKE_CXX_COMPILER_ID MATCHES Intel )
set ( CMAKE_C_FLAGS "-std=c99 ${CMAKE_C_FLAGS}" )
set ( CMAKE_CXX_FLAGS "-std=c++0x ${CMAKE_CXX_FLAGS}" )
2013-04-17 00:00:27 -04:00
## 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
2013-04-17 00:00:59 -04:00
4 1 1 # allow const struct members without a constructor
2013-04-17 00:00:25 -04:00
5 8 9 # do not complain about goto that skips initialization
2013-04-17 00:00:59 -04:00
1 2 9 2 # icc lies (it says it is "__GNUC__", but it doesn't handle the resulting macroexpansions from glibc 2.15.37 (which is designed for gcc 4.7, and appears in Fedora 17)
2013-04-17 00:00:25 -04:00
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:59 -04:00
set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -diag-disable ${intel_warning_string}" )
set ( CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -diag-disable ${intel_warning_string}" )
set ( CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_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:29 -04:00
set ( CMAKE_C_FLAGS "-Wcheck ${CMAKE_C_FLAGS}" )
2013-04-17 00:00:59 -04:00
set ( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -debug all" )
set ( CMAKE_CXX_FLAGS "-Wcheck ${CMAKE_CXX_FLAGS}" )
2013-04-17 00:00:26 -04:00
else ( )
2013-04-17 00:00:59 -04:00
set ( CMAKE_C_FLAGS "-std=c99 ${CMAKE_C_FLAGS}" )
set ( CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}" )
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:59 -04:00
set ( CMAKE_CXX_FLAGS "-Wextra ${CMAKE_CXX_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:58 -04:00
if ( WARN_ABOUT_PACKED )
list ( APPEND WARN_CFLAGS -Wpacked -Wno-error=packed )
endif ( )
## 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
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:59 -04:00
elseif ( CMAKE_CXX_COMPILER_ID STREQUAL Clang )
2013-04-17 00:00:49 -04:00
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:59 -04:00
set ( CMAKE_CXX_FLAGS "-Wall -Werror ${CMAKE_CXX_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 ( )
2013-04-17 00:00:59 -04:00
set_property ( ${ type } ${ obj } PROPERTY ${ propname } "${val} ${oldval}" )
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 )
2013-04-17 00:00:59 -04:00
if ( CMAKE_CXX_COMPILER_ID STREQUAL Intel )
2013-04-17 00:00:49 -04:00
foreach ( tgt ${ ARGN } )
2013-04-17 00:01:01 -04:00
target_link_libraries ( ${ tgt } LINK_PUBLIC -Bstatic irc -Bdynamic stdc++ )
2013-04-17 00:00:49 -04:00
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 } )
if ( CMAKE_C_COMPILER_ID STREQUAL Clang )
2013-04-17 00:00:50 -04:00
add_space_separated_property ( TARGET ${ tgt } COMPILE_FLAGS "-fvisibility=hidden" )
2013-04-17 00:00:49 -04:00
else ( )
2013-04-17 00:00:50 -04:00
add_space_separated_property ( TARGET ${ tgt } COMPILE_FLAGS "-fvisibility=hidden -fPIE" )
2013-04-17 00:00:50 -04:00
add_space_separated_property ( TARGET ${ tgt } LINK_FLAGS "-fPIE -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 )