summaryrefslogtreecommitdiff
path: root/CMakeLists.txt
diff options
context:
space:
mode:
authorGuy Harris <guy@alum.mit.edu>2018-03-27 13:13:30 -0700
committerGuy Harris <guy@alum.mit.edu>2018-03-27 13:13:30 -0700
commit111e17e87d9941b9e52450ed7176e8cb2aa17637 (patch)
tree16875c4194f942f43aa72f746b6effd627f802c7 /CMakeLists.txt
parent28b4ea1f3498ce4a4e9c5b45a59ed398c790110a (diff)
downloadtcpdump-111e17e87d9941b9e52450ed7176e8cb2aa17637.tar.gz
Don't use CMAKE_C_STANDARD, it doesn't work on all versions of CMake.
It doesn't work at all prior to CMake 3.1 and, even in newer versions of CMake, it doesn't support all the vendor compilers out there for various UNIXes.
Diffstat (limited to 'CMakeLists.txt')
-rw-r--r--CMakeLists.txt58
1 files changed, 56 insertions, 2 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index d0154774..07785330 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -7,9 +7,63 @@ project(tcpdump)
#
# Try to enable as many C99 features as we can.
# At minimum, we want C++/C99-style // comments.
-# (Sadly, this won't work with CMake prior to 3.1.)
#
-set(CMAKE_C_STANDARD 99)
+# Newer versions of compilers might default to supporting C99, but older
+# versions may require a special flag.
+#
+# Prior to CMake 3.1, setting CMAKE_C_STANDARD will not have any effect,
+# so, unless and until we require CMake 3.1 or later, we have to do it
+# ourselves on pre-3.1 CMake, so we just do it ourselves on all versions
+# of CMake.
+#
+# Note: with CMake 3.1 through 3.5, the only compilers for which CMake
+# handles CMAKE_C_STANDARD are GCC and Clang. 3.6 adds support only
+# for Intel C; 3.9 adds support for PGI C, Sun C, and IBM XL C, and
+# 3.10 adds support for Cray C and IAR C, but no version of CMake has
+# support for HP C. Therefore, even if we use CMAKE_C_STANDARD with
+# compilers for which CMake supports it, we may still have to do it
+# ourselves on other compilers.
+#
+# See the CMake documentation for the CMAKE_<LANG>_COMPILER_ID variables
+# for a list of compiler IDs.
+#
+# We don't worry about MSVC; it doesn't have such a flag - either it
+# doesn't support the C99 features we need at all, or it supports them
+# regardless of the compiler flag.
+#
+# XXX - this just tests whether the option works and adds it if it does.
+# We don't test whether it's necessary in order to get the C99 features
+# that we use; if we ever have a user who tries to compile with a compiler
+# that can't be made to support those features, we can add a test to make
+# sure we actually *have* C99 support.
+#
+include(CheckCCompilerFlag)
+macro(check_and_add_compiler_option _option)
+ message(STATUS "Checking C compiler flag ${_option}")
+ string(REPLACE "=" "-" _temp_option_variable ${_option})
+ string(REGEX REPLACE "^-" "" _option_variable ${_temp_option_variable})
+ check_c_compiler_flag("${_option}" ${_option_variable})
+ if(${${_option_variable}})
+ set(C_ADDITIONAL_FLAGS ${C_ADDITIONAL_FLAGS} "${_option}")
+ endif()
+endmacro()
+
+if(CMAKE_C_COMPILER_ID MATCHES "GNU" OR
+ CMAKE_C_COMPILER_ID MATCHES "Clang")
+ check_and_add_compiler_option("-std=gnu99")
+elseif(CMAKE_C_COMPILER_ID MATCHES "XL")
+ #
+ # We want support for extensions picked up for GNU C compatibility,
+ # so we use -qlanglvl=extc99.
+ #
+ check_and_add_compiler_option("-qlanglvl=extc99")
+elseif(CMAKE_C_COMPILER_ID MATCHES "HP")
+ check_and_add_compiler_option("-AC99")
+elseif(CMAKE_C_COMPILER_ID MATCHES "Sun")
+ check_and_add_compiler_option("-xc99")
+elseif(CMAKE_C_COMPILER_ID MATCHES "Intel")
+ check_and_add_compiler_option("-c99")
+endif()
set(LIBRARY_NAME netdissect)