From 8d9710238704e08ef2c1753698de94548d096f1b Mon Sep 17 00:00:00 2001 From: Michael Steinert Date: Fri, 25 May 2012 15:40:47 -0600 Subject: Cleanup CMake build a bit 1. Add checks for Unix/Linux-specific header files (similar to configure.ac) 2. Added a check for the `-std=c99` flag. It appears that some of this code uses C99 and compiling without this flag produces quite a few warnings. Signed-off-by: Michael Steinert --- CMakeLists.txt | 26 +++++++++++ cmake/CheckCCompilerFlag.cmake | 62 +++++++++++++++++++++++++ cmake/CheckIncludeFiles.cmake | 101 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 189 insertions(+) create mode 100644 cmake/CheckCCompilerFlag.cmake create mode 100644 cmake/CheckIncludeFiles.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index 0d94a55..8b257ac 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,10 +11,36 @@ else () set(CMAKE_C_FLAGS "-Wall -Wextra -pedantic -Wstrict-prototypes -Wcast-align -Wno-unused-function -fno-common -fvisibility=hidden ${CMAKE_C_FLAGS}") endif () +include(CheckCCompilerFlag) + +check_c_compiler_flag("-std=c99" STD_C99) +if (STD_C99) + set(CMAKE_C_FLAGS "-std=c99") +endif (STD_C99) + +include(CheckIncludeFiles) + +check_include_files("arpa/inet.h" HAVE_ARPA_INET_H) +if (HAVE_ARPA_INET_H) + add_definitions(-DHAVE_ARPA_INET_H) +endif(HAVE_ARPA_INET_H) + +check_include_files("netdb.h" HAVE_NETDB_H) +if (HAVE_NETDB_H) + add_definitions(-DHAVE_NETDB_H) +endif(HAVE_NETDB_H) + +check_include_files("netinet/tcp.h" HAVE_NETINET_TCP_H) +if (HAVE_NETINET_TCP_H) + add_definitions(-DHAVE_NETINET_TCP_H) +endif (HAVE_NETINET_TCP_H) + if (NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE "Release" CACHE STRING "Choose the type of build, options are: Debug Release RelWithDebInfo MinSizeRel." FORCE) endif() +add_definitions(-D_GNU_SOURCE) + #find python find_package(PythonInterp REQUIRED) diff --git a/cmake/CheckCCompilerFlag.cmake b/cmake/CheckCCompilerFlag.cmake new file mode 100644 index 0000000..3fba297 --- /dev/null +++ b/cmake/CheckCCompilerFlag.cmake @@ -0,0 +1,62 @@ +# - Check whether the C compiler supports a given flag. +# CHECK_C_COMPILER_FLAG( ) +# - the compiler flag +# - variable to store the result +# This internally calls the check_c_source_compiles macro. +# See help for CheckCSourceCompiles for a listing of variables +# that can modify the build. + +#============================================================================= +# Copyright 2006-2011 Kitware, Inc. +# Copyright 2006 Alexander Neundorf +# Copyright 2011 Matthias Kretz +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# * Neither the names of Kitware, Inc., the Insight Software Consortium, +# nor the names of their contributors may be used to endorse or promote +# products derived from this software without specific prior written +# permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +#============================================================================= + +INCLUDE(CheckCSourceCompiles) + +MACRO (CHECK_C_COMPILER_FLAG _FLAG _RESULT) + SET(SAFE_CMAKE_REQUIRED_DEFINITIONS "${CMAKE_REQUIRED_DEFINITIONS}") + SET(CMAKE_REQUIRED_DEFINITIONS "${_FLAG}") + CHECK_C_SOURCE_COMPILES("int main(void) { return 0; }" ${_RESULT} + # Some compilers do not fail with a bad flag + FAIL_REGEX "command line option .* is valid for .* but not for C" # GNU + FAIL_REGEX "unrecognized .*option" # GNU + FAIL_REGEX "unknown .*option" # Clang + FAIL_REGEX "ignoring unknown option" # MSVC + FAIL_REGEX "warning D9002" # MSVC, any lang + FAIL_REGEX "option .*not supported" # Intel + FAIL_REGEX "[Uu]nknown option" # HP + FAIL_REGEX "[Ww]arning: [Oo]ption" # SunPro + FAIL_REGEX "command option .* is not recognized" # XL + FAIL_REGEX "WARNING: unknown flag:" # Open64 + ) + SET (CMAKE_REQUIRED_DEFINITIONS "${SAFE_CMAKE_REQUIRED_DEFINITIONS}") +ENDMACRO (CHECK_C_COMPILER_FLAG) diff --git a/cmake/CheckIncludeFiles.cmake b/cmake/CheckIncludeFiles.cmake new file mode 100644 index 0000000..0420951 --- /dev/null +++ b/cmake/CheckIncludeFiles.cmake @@ -0,0 +1,101 @@ +# - Check if the files can be included +# +# CHECK_INCLUDE_FILES(INCLUDE VARIABLE) +# +# INCLUDE - list of files to include +# VARIABLE - variable to return result +# +# The following variables may be set before calling this macro to +# modify the way the check is run: +# +# CMAKE_REQUIRED_FLAGS = string of compile command line flags +# CMAKE_REQUIRED_DEFINITIONS = list of macros to define (-DFOO=bar) +# CMAKE_REQUIRED_INCLUDES = list of include directories + +#============================================================================= +# Copyright 2003-2012 Kitware, Inc. +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# * Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# +# * Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the distribution. +# +# * Neither the names of Kitware, Inc., the Insight Software Consortium, +# nor the names of their contributors may be used to endorse or promote +# products derived from this software without specific prior written +# permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +#============================================================================= + +MACRO(CHECK_INCLUDE_FILES INCLUDE VARIABLE) + IF("${VARIABLE}" MATCHES "^${VARIABLE}$") + SET(CMAKE_CONFIGURABLE_FILE_CONTENT "/* */\n") + IF(CMAKE_REQUIRED_INCLUDES) + SET(CHECK_INCLUDE_FILES_INCLUDE_DIRS "-DINCLUDE_DIRECTORIES=${CMAKE_REQUIRED_INCLUDES}") + ELSE(CMAKE_REQUIRED_INCLUDES) + SET(CHECK_INCLUDE_FILES_INCLUDE_DIRS) + ENDIF(CMAKE_REQUIRED_INCLUDES) + SET(CHECK_INCLUDE_FILES_CONTENT "/* */\n") + SET(MACRO_CHECK_INCLUDE_FILES_FLAGS ${CMAKE_REQUIRED_FLAGS}) + FOREACH(FILE ${INCLUDE}) + SET(CMAKE_CONFIGURABLE_FILE_CONTENT + "${CMAKE_CONFIGURABLE_FILE_CONTENT}#include <${FILE}>\n") + ENDFOREACH(FILE) + SET(CMAKE_CONFIGURABLE_FILE_CONTENT + "${CMAKE_CONFIGURABLE_FILE_CONTENT}\n\nint main(){return 0;}\n") + CONFIGURE_FILE("${CMAKE_ROOT}/Modules/CMakeConfigurableFile.in" + "${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckIncludeFiles.c" @ONLY IMMEDIATE) + + SET(_INCLUDE ${INCLUDE}) # remove empty elements + IF("${_INCLUDE}" MATCHES "^([^;]+);.+;([^;]+)$") + LIST(LENGTH _INCLUDE _INCLUDE_LEN) + SET(_description "${_INCLUDE_LEN} include files ${CMAKE_MATCH_1}, ..., ${CMAKE_MATCH_2}") + ELSEIF("${_INCLUDE}" MATCHES "^([^;]+);([^;]+)$") + SET(_description "include files ${CMAKE_MATCH_1}, ${CMAKE_MATCH_2}") + ELSE() + SET(_description "include file ${_INCLUDE}") + ENDIF() + + MESSAGE(STATUS "Looking for ${_description}") + TRY_COMPILE(${VARIABLE} + ${CMAKE_BINARY_DIR} + ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeTmp/CheckIncludeFiles.c + COMPILE_DEFINITIONS ${CMAKE_REQUIRED_DEFINITIONS} + CMAKE_FLAGS + -DCOMPILE_DEFINITIONS:STRING=${MACRO_CHECK_INCLUDE_FILES_FLAGS} + "${CHECK_INCLUDE_FILES_INCLUDE_DIRS}" + OUTPUT_VARIABLE OUTPUT) + IF(${VARIABLE}) + MESSAGE(STATUS "Looking for ${_description} - found") + SET(${VARIABLE} 1 CACHE INTERNAL "Have include ${INCLUDE}") + FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeOutput.log + "Determining if files ${INCLUDE} " + "exist passed with the following output:\n" + "${OUTPUT}\n\n") + ELSE(${VARIABLE}) + MESSAGE(STATUS "Looking for ${_description} - not found.") + SET(${VARIABLE} "" CACHE INTERNAL "Have includes ${INCLUDE}") + FILE(APPEND ${CMAKE_BINARY_DIR}${CMAKE_FILES_DIRECTORY}/CMakeError.log + "Determining if files ${INCLUDE} " + "exist failed with the following output:\n" + "${OUTPUT}\nSource:\n${CMAKE_CONFIGURABLE_FILE_CONTENT}\n") + ENDIF(${VARIABLE}) + ENDIF("${VARIABLE}" MATCHES "^${VARIABLE}$") +ENDMACRO(CHECK_INCLUDE_FILES) -- cgit v1.2.1