cmake: test for warning flags

This commit is contained in:
Peter Wu 2016-02-11 17:54:30 +01:00
parent 5819d03c68
commit 147e098827
3 changed files with 111 additions and 90 deletions

View File

@ -101,8 +101,8 @@ endif()
include(CheckCXXCompilerFlag) include(CheckCXXCompilerFlag)
foreach(_std gnu++11 gnu++0x) foreach(_std gnu++11 gnu++0x)
set(_cxx1x_flag -std=${_std}) set(_cxx1x_flag -std=${_std})
CHECK_CXX_COMPILER_FLAG(${_cxx1x_flag} CXX_STD_${_std}) check_cxx_compiler_flag(${_cxx1x_flag} CXX_FLAG_-std_${_std})
if(CXX_STD_${_std}) if(CXX_FLAG_-std_${_std})
set(CXX1XCXXFLAGS ${_cxx1x_flag}) set(CXX1XCXXFLAGS ${_cxx1x_flag})
break() break()
endif() endif()
@ -113,12 +113,12 @@ include(CheckCXXSourceCompiles)
cmake_push_check_state() cmake_push_check_state()
set(CMAKE_REQUIRED_DEFINITIONS "${CMAKE_REQUIRED_DEFINITIONS} ${CXX1XCXXFLAGS}") set(CMAKE_REQUIRED_DEFINITIONS "${CMAKE_REQUIRED_DEFINITIONS} ${CXX1XCXXFLAGS}")
# Check that std::future is available. # Check that std::future is available.
CHECK_CXX_SOURCE_COMPILES(" check_cxx_source_compiles("
#include <vector> #include <vector>
#include <future> #include <future>
int main() { std::vector<std::future<int>> v; }" HAVE_STD_FUTURE) int main() { std::vector<std::future<int>> v; }" HAVE_STD_FUTURE)
# Check that std::map::emplace is available for g++-4.7. # Check that std::map::emplace is available for g++-4.7.
CHECK_CXX_SOURCE_COMPILES(" check_cxx_source_compiles("
#include <map> #include <map>
int main() { std::map<int, int>().emplace(1, 2); }" HAVE_STD_MAP_EMPLACE) int main() { std::map<int, int>().emplace(1, 2); }" HAVE_STD_MAP_EMPLACE)
cmake_pop_check_state() cmake_pop_check_state()
@ -312,7 +312,7 @@ check_include_file("unistd.h" HAVE_UNISTD_H)
# add_definitions(-D_FILE_OFFSET_BITS=64) # add_definitions(-D_FILE_OFFSET_BITS=64)
include(CheckStructHasMember) include(CheckStructHasMember)
CHECK_STRUCT_HAS_MEMBER("struct tm" tm_gmtoff time.h HAVE_STRUCT_TM_TM_GMTOFF) check_struct_has_member("struct tm" tm_gmtoff time.h HAVE_STRUCT_TM_TM_GMTOFF)
# Check size of pointer to decide we need 8 bytes alignment adjustment. # Check size of pointer to decide we need 8 bytes alignment adjustment.
include(CheckTypeSize) include(CheckTypeSize)
@ -321,101 +321,88 @@ check_type_size("time_t" SIZEOF_TIME_T)
# Checks for library functions. # Checks for library functions.
include(CheckFunctionExists) include(CheckFunctionExists)
CHECK_FUNCTION_EXISTS(_Exit HAVE__EXIT) check_function_exists(_Exit HAVE__EXIT)
CHECK_FUNCTION_EXISTS(accept4 HAVE_ACCEPT4) check_function_exists(accept4 HAVE_ACCEPT4)
# timerfd_create was added in linux kernel 2.6.25 # timerfd_create was added in linux kernel 2.6.25
include(CheckSymbolExists) include(CheckSymbolExists)
# XXX does this correctly detect initgroups (un)availability on cygwin? # XXX does this correctly detect initgroups (un)availability on cygwin?
CHECK_SYMBOL_EXISTS(initgroups grp.h HAVE_DECL_INITGROUPS) check_symbol_exists(initgroups grp.h HAVE_DECL_INITGROUPS)
CHECK_FUNCTION_EXISTS(timerfd_create HAVE_TIMERFD_CREATE) check_function_exists(timerfd_create HAVE_TIMERFD_CREATE)
# Checks for epoll availability, primarily for examples/tiny-nghttpd # Checks for epoll availability, primarily for examples/tiny-nghttpd
CHECK_SYMBOL_EXISTS(epoll_create sys/epoll.h HAVE_EPOLL) check_symbol_exists(epoll_create sys/epoll.h HAVE_EPOLL)
if(HAVE_EPOLL AND HAVE_TIMERFD_CREATE) if(HAVE_EPOLL AND HAVE_TIMERFD_CREATE)
set(ENABLE_TINY_NGHTTPD 1) set(ENABLE_TINY_NGHTTPD 1)
endif() endif()
# save_CFLAGS=$CFLAGS include(ExtractValidFlags)
# save_CXXFLAGS=$CXXFLAGS if(ENABLE_WERROR)
# # For C compiler
# CFLAGS= extract_valid_c_flags(WARNCFLAGS
# CXXFLAGS= -Wall
# -Wextra
# if test "x$werror" != "xno"; then -Werror
# # For C compiler -Wmissing-prototypes
# AX_CHECK_COMPILE_FLAG([-Wall], [CFLAGS="$CFLAGS -Wall"]) -Wstrict-prototypes
# AX_CHECK_COMPILE_FLAG([-Wextra], [CFLAGS="$CFLAGS -Wextra"]) -Wmissing-declarations
# AX_CHECK_COMPILE_FLAG([-Werror], [CFLAGS="$CFLAGS -Werror"]) -Wpointer-arith
# AX_CHECK_COMPILE_FLAG([-Wmissing-prototypes], [CFLAGS="$CFLAGS -Wmissing-prototypes"]) -Wdeclaration-after-statement
# AX_CHECK_COMPILE_FLAG([-Wstrict-prototypes], [CFLAGS="$CFLAGS -Wstrict-prototypes"]) -Wformat-security
# AX_CHECK_COMPILE_FLAG([-Wmissing-declarations], [CFLAGS="$CFLAGS -Wmissing-declarations"]) -Wwrite-strings
# AX_CHECK_COMPILE_FLAG([-Wpointer-arith], [CFLAGS="$CFLAGS -Wpointer-arith"]) -Wshadow
# AX_CHECK_COMPILE_FLAG([-Wdeclaration-after-statement], [CFLAGS="$CFLAGS -Wdeclaration-after-statement"]) -Winline
# AX_CHECK_COMPILE_FLAG([-Wformat-security], [CFLAGS="$CFLAGS -Wformat-security"]) -Wnested-externs
# AX_CHECK_COMPILE_FLAG([-Wwrite-strings], [CFLAGS="$CFLAGS -Wwrite-strings"]) -Wfloat-equal
# AX_CHECK_COMPILE_FLAG([-Wshadow], [CFLAGS="$CFLAGS -Wshadow"]) -Wundef
# AX_CHECK_COMPILE_FLAG([-Winline], [CFLAGS="$CFLAGS -Winline"]) -Wendif-labels
# AX_CHECK_COMPILE_FLAG([-Wnested-externs], [CFLAGS="$CFLAGS -Wnested-externs"]) -Wempty-body
# AX_CHECK_COMPILE_FLAG([-Wfloat-equal], [CFLAGS="$CFLAGS -Wfloat-equal"]) -Wcast-align
# AX_CHECK_COMPILE_FLAG([-Wundef], [CFLAGS="$CFLAGS -Wundef"]) -Wclobbered
# AX_CHECK_COMPILE_FLAG([-Wendif-labels], [CFLAGS="$CFLAGS -Wendif-labels"]) -Wvla
# AX_CHECK_COMPILE_FLAG([-Wempty-body], [CFLAGS="$CFLAGS -Wempty-body"]) -Wpragmas
# AX_CHECK_COMPILE_FLAG([-Wcast-align], [CFLAGS="$CFLAGS -Wcast-align"]) -Wunreachable-code
# AX_CHECK_COMPILE_FLAG([-Wclobbered], [CFLAGS="$CFLAGS -Wclobbered"]) -Waddress
# AX_CHECK_COMPILE_FLAG([-Wvla], [CFLAGS="$CFLAGS -Wvla"]) -Wattributes
# AX_CHECK_COMPILE_FLAG([-Wpragmas], [CFLAGS="$CFLAGS -Wpragmas"]) -Wdiv-by-zero
# AX_CHECK_COMPILE_FLAG([-Wunreachable-code], [CFLAGS="$CFLAGS -Wunreachable-code"]) -Wshorten-64-to-32
# AX_CHECK_COMPILE_FLAG([-Waddress], [CFLAGS="$CFLAGS -Waddress"])
# AX_CHECK_COMPILE_FLAG([-Wattributes], [CFLAGS="$CFLAGS -Wattributes"]) -Wconversion
# AX_CHECK_COMPILE_FLAG([-Wdiv-by-zero], [CFLAGS="$CFLAGS -Wdiv-by-zero"]) -Wextended-offsetof
# AX_CHECK_COMPILE_FLAG([-Wshorten-64-to-32], [CFLAGS="$CFLAGS -Wshorten-64-to-32"]) -Wformat-nonliteral
# -Wlanguage-extension-token
# AX_CHECK_COMPILE_FLAG([-Wconversion], [CFLAGS="$CFLAGS -Wconversion"]) -Wmissing-field-initializers
# AX_CHECK_COMPILE_FLAG([-Wextended-offsetof], [CFLAGS="$CFLAGS -Wextended-offsetof"]) -Wmissing-noreturn
# AX_CHECK_COMPILE_FLAG([-Wformat-nonliteral], [CFLAGS="$CFLAGS -Wformat-nonliteral"]) -Wmissing-variable-declarations
# AX_CHECK_COMPILE_FLAG([-Wlanguage-extension-token], [CFLAGS="$CFLAGS -Wlanguage-extension-token"]) # Not used because we cannot change public structs
# AX_CHECK_COMPILE_FLAG([-Wmissing-field-initializers], [CFLAGS="$CFLAGS -Wmissing-field-initializers"]) # -Wpadded
# AX_CHECK_COMPILE_FLAG([-Wmissing-noreturn], [CFLAGS="$CFLAGS -Wmissing-noreturn"]) -Wsign-conversion
# AX_CHECK_COMPILE_FLAG([-Wmissing-variable-declarations], [CFLAGS="$CFLAGS -Wmissing-variable-declarations"]) # Not used because this basically disallows default case
# # Not used because we cannot change public structs # -Wswitch-enum
# # AX_CHECK_COMPILE_FLAG([-Wpadded], [CFLAGS="$CFLAGS -Wpadded"]) -Wunreachable-code-break
# AX_CHECK_COMPILE_FLAG([-Wsign-conversion], [CFLAGS="$CFLAGS -Wsign-conversion"]) -Wunused-macros
# # Not used because this basically disallows default case -Wunused-parameter
# # AX_CHECK_COMPILE_FLAG([-Wswitch-enum], [CFLAGS="$CFLAGS -Wswitch-enum"]) -Wredundant-decls
# AX_CHECK_COMPILE_FLAG([-Wunreachable-code-break], [CFLAGS="$CFLAGS -Wunreachable-code-break"]) # Only work with Clang for the moment
# AX_CHECK_COMPILE_FLAG([-Wunused-macros], [CFLAGS="$CFLAGS -Wunused-macros"]) -Wheader-guard
# AX_CHECK_COMPILE_FLAG([-Wunused-parameter], [CFLAGS="$CFLAGS -Wunused-parameter"]) )
# AX_CHECK_COMPILE_FLAG([-Wredundant-decls], [CFLAGS="$CFLAGS -Wredundant-decls"])
# # Only work with Clang for the moment extract_valid_cxx_flags(WARNCXXFLAGS
# AX_CHECK_COMPILE_FLAG([-Wheader-guard], [CFLAGS="$CFLAGS -Wheader-guard"]) # For C++ compiler
# -Wall
# # For C++ compiler -Werror
# AC_LANG_PUSH(C++) -Wformat-security
# AX_CHECK_COMPILE_FLAG([-Wall], [CXXFLAGS="$CXXFLAGS -Wall"]) )
# AX_CHECK_COMPILE_FLAG([-Werror], [CXXFLAGS="$CXXFLAGS -Werror"]) endif()
# AX_CHECK_COMPILE_FLAG([-Wformat-security], [CXXFLAGS="$CXXFLAGS -Wformat-security"])
# AC_LANG_POP() extract_valid_c_flags(EXTRACFLAG
# fi -fvisibility=hidden
# )
# WARNCFLAGS=$CFLAGS
# WARNCXXFLAGS=$CXXFLAGS if(ENABLE_DEBUG)
# set(DEBUGBUILD 1)
# CFLAGS=$save_CFLAGS endif()
# CXXFLAGS=$save_CXXFLAGS
#
# AC_SUBST([WARNCFLAGS])
# AC_SUBST([WARNCXXFLAGS])
#
# EXTRACFLAG=
# AX_CHECK_COMPILE_FLAG([-fvisibility=hidden], [EXTRACFLAG="-fvisibility=hidden"])
#
# AC_SUBST([EXTRACFLAG])
#
# if test "x$debug" != "xno"; then
# AC_DEFINE([DEBUGBUILD], [1], [Define to 1 to enable debug output.])
# fi
#
# enable_threads=yes # enable_threads=yes
# # Some platform does not have working std::future. We disable # # Some platform does not have working std::future. We disable
# # threading for those platforms. # # threading for those platforms.

View File

@ -0,0 +1,31 @@
# Convenience function that checks the availability
# of certain C or C++ compiler flags and returns the valid ones.
include(CheckCCompilerFlag)
include(CheckCXXCompilerFlag)
function(extract_valid_c_flags varname)
set(valid_flags "")
foreach(flag IN LISTS ARGN)
string(REGEX REPLACE "[^a-zA-Z0-9_-]+" "_" flag_var ${flag})
set(flag_var "C_FLAG_${flag_var}")
check_c_compiler_flag("${flag}" "${flag_var}")
if(${flag_var})
set(valid_flags "${valid_flags} ${flag}")
endif()
endforeach()
set(${varname} "${valid_flags}" PARENT_SCOPE)
endfunction()
function(extract_valid_cxx_flags varname)
set(valid_flags "")
foreach(flag IN LISTS ARGN)
string(REGEX REPLACE "[^a-zA-Z0-9_-]+" "_" flag_var ${flag})
set(flag_var "CXX_FLAG_${flag_var}")
check_cxx_compiler_flag("${flag}" "${flag_var}")
if(${flag_var})
set(valid_flags "${valid_flags} ${flag}")
endif()
endforeach()
set(${varname} "${valid_flags}" PARENT_SCOPE)
endfunction()

View File

@ -31,3 +31,6 @@
/* Define to 1 if you have the `initgroups` function. */ /* Define to 1 if you have the `initgroups` function. */
#define HAVE_DECL_INITGROUPS @HAVE_DECL_INITGROUPS@ #define HAVE_DECL_INITGROUPS @HAVE_DECL_INITGROUPS@
/* Define to 1 to enable debug output. */
#cmakedefine DEBUGBUILD 1