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:01:07 -04:00
|
|
|
if (APPLE)
|
2013-04-17 00:00:25 -04:00
|
|
|
add_c_defines(DARWIN=1 _DARWIN_C_SOURCE)
|
2013-04-17 00:01:07 -04:00
|
|
|
message(WARNING "Setting TOKU_ALLOW_DEPRECATED on Darwin and with clang. TODO: find a portable way to deprecate, and 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
|
|
|
|
|
|
|
## preprocessor definitions we want everywhere
|
|
|
|
add_c_defines(
|
|
|
|
_FILE_OFFSET_BITS=64
|
|
|
|
_LARGEFILE64_SOURCE
|
2013-04-17 00:00:59 -04:00
|
|
|
__STDC_FORMAT_MACROS
|
|
|
|
__STDC_LIMIT_MACROS
|
2013-04-17 00:01:10 -04:00
|
|
|
__LONG_LONG_SUPPORTED
|
2013-04-17 00:00:25 -04:00
|
|
|
)
|
2013-04-17 00:01:10 -04:00
|
|
|
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 ()
|
2013-04-17 00:00:25 -04:00
|
|
|
|
2013-04-17 00:01:04 -04:00
|
|
|
## add TOKU_PTHREAD_DEBUG for debug builds
|
|
|
|
set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS_DEBUG TOKU_PTHREAD_DEBUG)
|
|
|
|
|
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_program(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})
|
2013-04-17 00:01:11 -04:00
|
|
|
set(CMAKE_C_FLAGS "${flag} ${CMAKE_C_FLAGS}")
|
2013-04-17 00:01:00 -04:00
|
|
|
endif ()
|
|
|
|
check_cxx_compiler_flag("${flag}" HAVE_CXX_${flagname})
|
|
|
|
if (HAVE_CXX_${flagname})
|
2013-04-17 00:01:11 -04:00
|
|
|
set(CMAKE_CXX_FLAGS "${flag} ${CMAKE_CXX_FLAGS}")
|
2013-04-17 00:01:00 -04:00
|
|
|
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:01:11 -04:00
|
|
|
set(CMAKE_C_FLAGS "${flag} ${CMAKE_C_FLAGS}")
|
2013-04-17 00:00:27 -04:00
|
|
|
endif ()
|
2013-04-17 00:00:59 -04:00
|
|
|
check_cxx_compiler_flag(${flag} HAVE_CXX_${flag})
|
|
|
|
if (HAVE_CXX_${flag})
|
2013-04-17 00:01:11 -04:00
|
|
|
set(CMAKE_CXX_FLAGS "${flag} ${CMAKE_CXX_FLAGS}")
|
2013-04-17 00:00:59 -04:00
|
|
|
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})
|
2013-04-17 00:01:11 -04:00
|
|
|
set(CMAKE_EXE_LINKER_FLAGS "${flag} ${CMAKE_EXE_LINKER_FLAGS}")
|
|
|
|
set(CMAKE_SHARED_LINKER_FLAGS "${flag} ${CMAKE_SHARED_LINKER_FLAGS}")
|
2013-04-17 00:00:59 -04:00
|
|
|
endif ()
|
|
|
|
endforeach(flag)
|
|
|
|
endmacro(set_ldflags_if_supported)
|
|
|
|
|
2013-04-17 00:00:27 -04:00
|
|
|
## disable some warnings
|
|
|
|
set_cflags_if_supported(
|
|
|
|
-Wno-missing-field-initializers
|
2013-04-17 00:01:00 -04:00
|
|
|
-Wstrict-null-sentinel
|
|
|
|
-Winit-self
|
|
|
|
-Wswitch
|
|
|
|
-Wtrampolines
|
|
|
|
-Wlogical-op
|
|
|
|
-Wmissing-format-attribute
|
|
|
|
-Wno-error=missing-format-attribute
|
2013-04-17 00:01:01 -04:00
|
|
|
-fno-rtti
|
|
|
|
-fno-exceptions
|
2013-04-17 00:00:59 -04:00
|
|
|
)
|
2013-04-17 00:01:07 -04:00
|
|
|
## set_cflags_if_supported_named("-Weffc++" -Weffcpp)
|
2013-04-17 00:01:05 -04:00
|
|
|
|
|
|
|
## 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 ()
|
|
|
|
|
2013-04-17 00:01:07 -04:00
|
|
|
## this hits with optimized builds somewhere in ftleaf_split, we don't
|
|
|
|
## know why but we don't think it's a big deal
|
2013-04-17 00:00:59 -04:00
|
|
|
set_ldflags_if_supported(
|
|
|
|
-Wno-error=strict-overflow
|
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:01:07 -04:00
|
|
|
set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -g3 -O0")
|
|
|
|
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g3 -O0")
|
|
|
|
set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS_DEBUG _FORTIFY_SOURCE=2)
|
2013-04-17 00:00:25 -04:00
|
|
|
|
2013-04-17 00:01:07 -04:00
|
|
|
## 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
|
2013-04-17 00:00:50 -04:00
|
|
|
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:01:07 -04:00
|
|
|
else ()
|
|
|
|
# we overwrite this because the default passes -DNDEBUG and we don't want that
|
|
|
|
set(CMAKE_C_FLAGS_RELEASE "-g -O3 -flto -fuse-linker-plugin")
|
|
|
|
set(CMAKE_CXX_FLAGS_RELEASE "-g -O3 -flto -fuse-linker-plugin")
|
2013-04-17 00:01:11 -04:00
|
|
|
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}")
|
2013-04-17 00:00:27 -04:00
|
|
|
endif ()
|
2013-04-17 00:01:01 -04:00
|
|
|
|
2013-04-17 00:01:02 -04:00
|
|
|
option(USE_VALGRIND "Do not pass NVALGRIND to the compiler, because valgrind will be run on the generated executables." ON)
|
2013-04-17 00:01:01 -04:00
|
|
|
if (NOT USE_VALGRIND)
|
|
|
|
set_property(DIRECTORY APPEND PROPERTY COMPILE_DEFINITIONS_RELEASE NVALGRIND=1)
|
|
|
|
endif ()
|
2013-04-17 00:00:25 -04:00
|
|
|
|
2013-04-17 00:01:07 -04:00
|
|
|
## set warnings
|
|
|
|
set(WARN_FLAGS
|
|
|
|
-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
|
|
|
|
)
|
2013-04-17 00:00:26 -04:00
|
|
|
|
2013-04-17 00:01:07 -04:00
|
|
|
option(WARN_ABOUT_PACKED "Warn about useless __attribute__((packed)). This generates lots of warnings." OFF)
|
|
|
|
if (WARN_ABOUT_PACKED)
|
|
|
|
list(APPEND WARN_FLAGS -Wpacked -Wno-error=packed)
|
|
|
|
endif ()
|
2013-04-17 00:00:27 -04:00
|
|
|
|
2013-04-17 00:01:07 -04:00
|
|
|
if (CMAKE_CXX_COMPILER_ID STREQUAL Clang)
|
|
|
|
message(WARNING "Disabling -Wcast-align with clang. TODO: fix casting and re-enable it.")
|
|
|
|
else ()
|
|
|
|
list(APPEND WARN_FLAGS -Wcast-align)
|
|
|
|
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:01:07 -04:00
|
|
|
## 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 ()
|
|
|
|
set(CMAKE_CXX_FLAGS "-stdlib=libc++ ${CMAKE_CXX_FLAGS}")
|
|
|
|
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)
|
|
|
|
set(CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}")
|
|
|
|
elseif (HAVE_STDCXX0X)
|
|
|
|
set(CMAKE_CXX_FLAGS "-std=c++0x ${CMAKE_CXX_FLAGS}")
|
|
|
|
else ()
|
|
|
|
message(FATAL_ERROR "${CMAKE_CXX_COMPILER} doesn't support -std=c++11 or -std=c++0x, you need one that does.")
|
|
|
|
endif ()
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
## 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)
|
2013-04-17 00:01:08 -04:00
|
|
|
target_link_libraries(${lib} gcov)
|
2013-04-17 00:00:49 -04:00
|
|
|
endforeach(lib)
|
|
|
|
endif (USE_GCOV)
|
|
|
|
endfunction(maybe_add_gcov_to_libraries)
|
2013-04-17 00:00:49 -04:00
|
|
|
|
2013-04-17 00:01:07 -04:00
|
|
|
## adds -fvisibility=hidden
|
2013-04-17 00:00:49 -04:00
|
|
|
## good for binaries
|
|
|
|
function(add_common_options_to_binary_targets)
|
|
|
|
foreach(tgt ${ARGN})
|
2013-04-17 00:01:07 -04:00
|
|
|
add_space_separated_property(TARGET ${tgt} COMPILE_FLAGS "-fvisibility=hidden")
|
2013-04-17 00:00:49 -04:00
|
|
|
endforeach(tgt)
|
2013-04-17 00:00:49 -04:00
|
|
|
endfunction(add_common_options_to_binary_targets)
|