]> The Tcpdump Group git mirrors - tcpdump/commitdiff
CMake: Add a way to request -Werror and equivalents.
authorDenis Ovsienko <[email protected]>
Fri, 30 Jul 2021 09:12:59 +0000 (10:12 +0100)
committerDenis Ovsienko <[email protected]>
Fri, 30 Jul 2021 09:44:45 +0000 (10:44 +0100)
As it turns out, most FreeBSD/CMake tcpdump builds were emitting some
warnings.  This was difficult to notice because make returned success
regardless of the warnings because the compiler never received -Werror
because there was no way to tell CMake to append any compiler flags at
the build time, but not at the buildsystem generation time.  In other
words, the command "make CFLAGS=-Werror" had the same effect as just
"make".

Implement the desired behaviour with "cmake -DEXTRA_CFLAGS='xyz'" and
set the variable in build.sh when and as required.  Reword the comment
for clarity and define two known exemptions.

CMakeLists.txt
build.sh

index 73acc7501c8410d1685a406a6dd533a85995affa..4fc65f387570a6d1deecd5fa4b39e25f81791b04 100644 (file)
@@ -917,6 +917,18 @@ if(EXISTS ${CMAKE_SOURCE_DIR}/.devel OR EXISTS ${CMAKE_BINARY_DIR}/.devel)
     endif()
 endif()
 
+#
+# Extra compiler options for the build matrix scripts to request -Werror or
+# its equivalent if required.  The CMake variable name cannot be CFLAGS
+# because that is already used for a different purpose in CMake.  Example
+# usage: cmake -DEXTRA_CFLAGS='-Wall -Wextra -Werror' ...
+#
+if(NOT "${EXTRA_CFLAGS}" STREQUAL "")
+    string(REPLACE " " ";" _extra_cflags_list ${EXTRA_CFLAGS})
+    add_compile_options(${_extra_cflags_list})
+    message(STATUS "Added extra compile options (${EXTRA_CFLAGS})")
+endif()
+
 ######################################
 # Input files
 ######################################
index 7d8e2406b574eb01a64451a2b7b833985a11d590..40ffbace2558e94d0a0b2fdf2a3a9c44dc974a06 100755 (executable)
--- a/build.sh
+++ b/build.sh
@@ -23,6 +23,35 @@ TCPDUMP_BIN="$PREFIX/bin/tcpdump"
 export TCPDUMP_BIN
 
 print_cc_version
+
+# The norm is to compile without any warnings, but tcpdump builds on some OSes
+# are not warning-free for one or another reason.  If you manage to fix one of
+# these cases, please remember to remove respective exemption below to help any
+# later warnings in the same matrix subset trigger an error.
+# shellcheck disable=SC2006
+case `os_id`/"$CMAKE" in
+FreeBSD-*/yes)
+    # tcpdump.c:2290:3: error: implicit declaration of function 'bpf_dump'
+    #   [-Werror=implicit-function-declaration]
+    [ "$BUILD_LIBPCAP" = yes ] && TCPDUMP_TAINTED=yes
+    case `cc_id` in
+    clang-*)
+        # tcpdump.c:2434:32: error: '_Generic' is a C11 extension
+        #   [-Werror,-Wc11-extensions]
+        # tcpdump.c:2439:26: error: '_Generic' is a C11 extension
+        #   [-Werror,-Wc11-extensions]
+        # tcpdump.c:2443:9: error: '_Generic' is a C11 extension
+        #   [-Werror,-Wc11-extensions]
+        # tcpdump.c:244:16: error: no previous extern declaration for non-static variable
+        #   'capdns' [-Werror,-Wmissing-variable-declarations]
+        TCPDUMP_TAINTED=yes
+        ;;
+    esac
+    ;;
+esac
+# shellcheck disable=SC2006
+[ "$TCPDUMP_TAINTED" != yes ] && CFLAGS=`cc_werr_cflags`
+
 if [ "$CMAKE" = no ]; then
     if [ "$BUILD_LIBPCAP" = yes ]; then
         echo "Using PKG_CONFIG_PATH=$PKG_CONFIG_PATH"
@@ -40,22 +69,23 @@ else
     run_after_echo cd build
     if [ "$BUILD_LIBPCAP" = yes ]; then
         run_after_echo cmake -DWITH_CRYPTO="$CRYPTO" -DENABLE_SMB="$SMB" \
+            ${CFLAGS:+-DEXTRA_CFLAGS="$CFLAGS"} \
             -DCMAKE_INSTALL_PREFIX="$PREFIX" -DCMAKE_PREFIX_PATH="$PREFIX" ..
         LD_LIBRARY_PATH="$PREFIX/lib"
         export LD_LIBRARY_PATH
     else
         run_after_echo cmake -DWITH_CRYPTO="$CRYPTO" -DENABLE_SMB="$SMB" \
+             ${CFLAGS:+-DEXTRA_CFLAGS="$CFLAGS"} \
             -DCMAKE_INSTALL_PREFIX="$PREFIX" ..
     fi
 fi
 run_after_echo make -s clean
-# The norm is to compile without any warnings, but tcpdump builds on some OSes
-# are not warning-free for one or another reason. If you manage to fix one of
-# these cases, please remember to raise the bar here so if the warnings appear
-# again, it will trigger an error.
-# shellcheck disable=SC2006
-[ "$TCPDUMP_TAINTED" != yes ] && CFLAGS=`cc_werr_cflags`
-run_after_echo make -s ${CFLAGS:+CFLAGS="$CFLAGS"}
+if [ "$CMAKE" = no ]; then
+    run_after_echo make -s ${CFLAGS:+CFLAGS="$CFLAGS"}
+else
+    # The "-s" flag is a no-op and CFLAGS is set using -DEXTRA_CFLAGS above.
+    run_after_echo make
+fi
 run_after_echo make install
 print_so_deps "$TCPDUMP_BIN"
 run_after_echo "$TCPDUMP_BIN" -h