From: Denis Ovsienko Date: Fri, 30 Jul 2021 09:12:59 +0000 (+0100) Subject: CMake: Add a way to request -Werror and equivalents. X-Git-Url: https://git.tcpdump.org/tcpdump/commitdiff_plain/7880ad999f1a3d6c3aca4bd31ce90755c924f947 CMake: Add a way to request -Werror and equivalents. 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. --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 73acc750..4fc65f38 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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 ###################################### diff --git a/build.sh b/build.sh index 7d8e2406..40ffbace 100755 --- 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