From 7af3611807e652047f0bfc95f924a27ade624a83 Mon Sep 17 00:00:00 2001 From: Dominik Charousset Date: Sat, 29 Apr 2023 10:31:50 +0200 Subject: [PATCH 1/4] Move build defaults from configure to CMake Moving the defaults for build variables from the `configure` script to `CMakeLists.txt` gives the same default behavior on platforms where the `configure` script is not available (Windows) and also allows a pure CMake-based work flow (e.g., the standard `cmake -S . -B build`) without having to manually adjust the defaults. The `configure` script also becomes much simpler as a result. --- CMakeLists.txt | 54 +++++++++++++++++++++++++++++++++++++++++++++-- configure | 57 ++++++++++---------------------------------------- 2 files changed, 63 insertions(+), 48 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 03eab558b8..7801938e3b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -9,12 +9,62 @@ endif() project(Zeek C CXX) -option(ZEEK_STANDALONE "Build Zeek as stand-alone binary?" ON) + +# We want to set ENABLE_DEBUG to ON by default if the build type is Debug. +set(ENABLE_DEBUG_DEFAULT OFF) +if ( NOT GENERATOR_IS_MULTI_CONFIG ) + string(TOLOWER "${CMAKE_BUILD_TYPE}" build_type_lower) + if ( build_type_lower STREQUAL "debug" ) + set(ENABLE_DEBUG_DEFAULT ON) + endif () + unset(build_type_lower) +endif () + +# CMake options (Boolean flags). +option(BUILD_SHARED_LIBS "Build targets as shared libraries." ON) +option(ENABLE_DEBUG "Build Zeek with additional debugging support." ${ENABLE_DEBUG_DEFAULT}) +option(ENABLE_JEMALLOC "Link against jemalloc." OFF) +option(ENABLE_PERFTOOLS "Build with support for Google perftools." OFF) option(ENABLE_ZEEK_UNIT_TESTS "Build the C++ (doctest) unit tests?" ON) +option(ENABLE_ZEEK_UNIT_TESTS "Build the C++ unit tests." ON) +option(INSTALL_AUX_TOOLS "Install additional tools from auxil." ON) +option(INSTALL_BTEST "Install btest alongside Zeek." ON) +option(INSTALL_BTEST_PCAPS "Install pcap files for testing." ON) +option(INSTALL_ZEEKCTL "Install zeekctl." ON) +option(INSTALL_ZEEK_ARCHIVER "Install the zeek-archiver." ON) +option(INSTALL_ZEEK_CLIENT "Install the zeek-client." ON) +option(INSTALL_ZKG "Install zkg." ON) +option(PREALLOCATE_PORT_ARRAY "Pre-allocate all ports for zeek::Val." ON) +option(ZEEK_STANDALONE "Build Zeek as stand-alone binary?" ON) + +# Non-boolean options. +if ( NOT WIN32 ) + if ( CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT ) + set(CMAKE_INSTALL_PREFIX "/usr/local/zeek" + CACHE PATH "Install directory used by install()." FORCE) + endif () + # On windows, this defaults to "c:/Program Files/${PROJECT_NAME}": + # https://cmake.org/cmake/help/v3.15/variable/CMAKE_INSTALL_PREFIX.html. +endif () + +set(ZEEK_SCRIPT_INSTALL_PATH "${CMAKE_INSTALL_PREFIX}/share/zeek" + CACHE PATH "Install directory for Zeek scripts.") + +set(ZEEK_ETC_INSTALL_DIR "${CMAKE_INSTALL_PREFIX}/etc" + CACHE PATH "Install directory for Zeek configuration files.") + set(CMAKE_EXPORT_COMPILE_COMMANDS ON - CACHE INTERNAL "Write JSON compile commands database") + CACHE INTERNAL "Whether to write a JSON compile commands database") + set(ZEEK_CXX_STD cxx_std_17 CACHE STRING "The C++ standard to use.") +set(ZEEK_SANITIZERS "" CACHE STRING "Sanitizers to use when building.") + +set(CPACK_SOURCE_IGNORE_FILES "" CACHE STRING "CPack source ignore files.") + +set(ZEEK_INCLUDE_PLUGINS "" CACHE STRING "Extra plugins to add to the build.") + +# Look into the build tree for additional CMake modules. list(APPEND CMAKE_MODULE_PATH ${CMAKE_BINARY_DIR}) list(APPEND CMAKE_PREFIX_PATH ${CMAKE_BINARY_DIR}) diff --git a/configure b/configure index b9d9e16cb9..05944e217c 100755 --- a/configure +++ b/configure @@ -12,6 +12,9 @@ command="$0 $*" usage="\ Usage: $0 [OPTION]... [VAR=VALUE]... + -h, --help display this help and exit + --show-config display the most relevant config parameters of an existing build + Build Options: --cmake=PATH custom path to a CMake binary --builddir=DIR place build files in directory [build] @@ -149,42 +152,10 @@ append_cache_entry() { CMakeCacheEntries="$CMakeCacheEntries -D $1:$2=$3" } -# Function to remove a CMake cache entry definition from the -# CMakeCacheEntries variable -# $1 is the cache entry variable name -remove_cache_entry() { - CMakeCacheEntries="$CMakeCacheEntries -U $1" - - # Even with -U, cmake still warns by default if - # added previously with -D. - CMakeCacheEntries="$CMakeCacheEntries --no-warn-unused-cli" -} - # set defaults builddir=build -prefix=/usr/local/zeek CMakeCacheEntries="" display_cmake=0 -append_cache_entry CMAKE_INSTALL_PREFIX PATH $prefix -append_cache_entry ZEEK_ROOT_DIR PATH $prefix -append_cache_entry ZEEK_SCRIPT_INSTALL_PATH STRING $prefix/share/zeek -append_cache_entry ZEEK_ETC_INSTALL_DIR PATH $prefix/etc -append_cache_entry ENABLE_DEBUG BOOL false -append_cache_entry ENABLE_PERFTOOLS BOOL false -append_cache_entry ENABLE_JEMALLOC BOOL false -append_cache_entry ENABLE_ZEEK_UNIT_TESTS BOOL true -append_cache_entry BUILD_SHARED_LIBS BOOL true -append_cache_entry INSTALL_AUX_TOOLS BOOL true -append_cache_entry INSTALL_BTEST BOOL true -append_cache_entry INSTALL_BTEST_PCAPS BOOL true -append_cache_entry INSTALL_ZEEK_ARCHIVER BOOL true -append_cache_entry INSTALL_ZEEK_CLIENT BOOL true -append_cache_entry INSTALL_ZEEKCTL BOOL true -append_cache_entry INSTALL_ZKG BOOL true -append_cache_entry CPACK_SOURCE_IGNORE_FILES STRING -append_cache_entry ZEEK_SANITIZERS STRING "" -append_cache_entry ZEEK_INCLUDE_PLUGINS STRING "" -append_cache_entry PREALLOCATE_PORT_ARRAY BOOL true # parse arguments while [ $# -ne 0 ]; do @@ -198,6 +169,14 @@ while [ $# -ne 0 ]; do echo "${usage}" 1>&2 exit 1 ;; + --show-config) + if [ ! -f "$builddir/CMakeCache.txt" ]; then + echo "Error: no CMake build found under '$builddir'." 1>&2 + exit 1 + fi + grep -E "^ENABLE_|^ZEEK_|^INSTALL_|^CMAKE_INSTALL_PRE|^CMAKE_C.*_FLAGS|^CMAKE_BUILD" "$builddir/CMakeCache.txt" | grep -v ':INTERNAL' + exit 0 + ;; -D) shift if [ $# -eq 0 ]; then @@ -217,10 +196,6 @@ while [ $# -ne 0 ]; do ;; --build-type=*) append_cache_entry CMAKE_BUILD_TYPE STRING $optarg - - if [ $(echo "$optarg" | tr [:upper:] [:lower:]) = "debug" ]; then - append_cache_entry ENABLE_DEBUG BOOL true - fi ;; --generator=*) CMakeGenerator="$optarg" @@ -235,9 +210,7 @@ while [ $# -ne 0 ]; do append_cache_entry ZEEK_INCLUDE_PLUGINS STRING \"$optarg\" ;; --prefix=*) - prefix=$optarg append_cache_entry CMAKE_INSTALL_PREFIX PATH $optarg - append_cache_entry ZEEK_ROOT_DIR PATH $optarg ;; --libdir=*) append_cache_entry CMAKE_INSTALL_LIBDIR PATH $optarg @@ -454,14 +427,6 @@ if [ -z "$CMakeCommand" ]; then fi fi -if [ "$user_set_scriptdir" != "true" ]; then - append_cache_entry ZEEK_SCRIPT_INSTALL_PATH STRING $prefix/share/zeek -fi - -if [ "$user_set_conffilesdir" != "true" ]; then - append_cache_entry ZEEK_ETC_INSTALL_DIR PATH $prefix/etc -fi - if [ -d $builddir ]; then # If build directory exists, check if it has a CMake cache if [ -f $builddir/CMakeCache.txt ]; then From c2c34148caeccf5efb792606da332743b24d52c0 Mon Sep 17 00:00:00 2001 From: Dominik Charousset Date: Tue, 2 May 2023 20:43:33 +0200 Subject: [PATCH 2/4] Fix CMake option defaults on Windows --- CMakeLists.txt | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 7801938e3b..bc1698da18 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -20,20 +20,26 @@ if ( NOT GENERATOR_IS_MULTI_CONFIG ) unset(build_type_lower) endif () +# On UNIX, install additional Zeek tools by default and build shared objects. +if ( NOT WIN32 ) + set(ZEEK_INSTALL_TOOLS_DEFAULT ON) + option(BUILD_SHARED_LIBS "Build targets as shared libraries." ON) +else () + set(ZEEK_INSTALL_TOOLS_DEFAULT OFF) +endif () + # CMake options (Boolean flags). -option(BUILD_SHARED_LIBS "Build targets as shared libraries." ON) option(ENABLE_DEBUG "Build Zeek with additional debugging support." ${ENABLE_DEBUG_DEFAULT}) option(ENABLE_JEMALLOC "Link against jemalloc." OFF) option(ENABLE_PERFTOOLS "Build with support for Google perftools." OFF) -option(ENABLE_ZEEK_UNIT_TESTS "Build the C++ (doctest) unit tests?" ON) option(ENABLE_ZEEK_UNIT_TESTS "Build the C++ unit tests." ON) -option(INSTALL_AUX_TOOLS "Install additional tools from auxil." ON) -option(INSTALL_BTEST "Install btest alongside Zeek." ON) -option(INSTALL_BTEST_PCAPS "Install pcap files for testing." ON) -option(INSTALL_ZEEKCTL "Install zeekctl." ON) -option(INSTALL_ZEEK_ARCHIVER "Install the zeek-archiver." ON) -option(INSTALL_ZEEK_CLIENT "Install the zeek-client." ON) -option(INSTALL_ZKG "Install zkg." ON) +option(INSTALL_AUX_TOOLS "Install additional tools from auxil." ${ZEEK_INSTALL_TOOLS_DEFAULT}) +option(INSTALL_BTEST "Install btest alongside Zeek." ${ZEEK_INSTALL_TOOLS_DEFAULT}) +option(INSTALL_BTEST_PCAPS "Install pcap files for testing." ${ZEEK_INSTALL_TOOLS_DEFAULT}) +option(INSTALL_ZEEKCTL "Install zeekctl." ${ZEEK_INSTALL_TOOLS_DEFAULT}) +option(INSTALL_ZEEK_ARCHIVER "Install the zeek-archiver." ${ZEEK_INSTALL_TOOLS_DEFAULT}) +option(INSTALL_ZEEK_CLIENT "Install the zeek-client." ${ZEEK_INSTALL_TOOLS_DEFAULT}) +option(INSTALL_ZKG "Install zkg." ${ZEEK_INSTALL_TOOLS_DEFAULT}) option(PREALLOCATE_PORT_ARRAY "Pre-allocate all ports for zeek::Val." ON) option(ZEEK_STANDALONE "Build Zeek as stand-alone binary?" ON) From 7b35d471adf580bc2baad080c99c4948dfb4d4f0 Mon Sep 17 00:00:00 2001 From: Dominik Charousset Date: Tue, 2 May 2023 20:44:48 +0200 Subject: [PATCH 3/4] Include compiler in --show-config output --- configure | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/configure b/configure index 05944e217c..22b4178337 100755 --- a/configure +++ b/configure @@ -174,7 +174,7 @@ while [ $# -ne 0 ]; do echo "Error: no CMake build found under '$builddir'." 1>&2 exit 1 fi - grep -E "^ENABLE_|^ZEEK_|^INSTALL_|^CMAKE_INSTALL_PRE|^CMAKE_C.*_FLAGS|^CMAKE_BUILD" "$builddir/CMakeCache.txt" | grep -v ':INTERNAL' + grep -E "^ENABLE_|^ZEEK_|^INSTALL_|^CMAKE_INSTALL_PRE|^CMAKE_C.*_FLAGS|^CMAKE_C.*_COMPILER|^CMAKE_BUILD" "$builddir/CMakeCache.txt" | grep -v ':INTERNAL' exit 0 ;; -D) From 94ec8167b233961bcd044c6669ca4601d867022d Mon Sep 17 00:00:00 2001 From: Dominik Charousset Date: Tue, 2 May 2023 21:24:53 +0200 Subject: [PATCH 4/4] Integrate review feedback Co-authored-by: Tim Wojtulewicz --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index bc1698da18..43952dc00d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -66,7 +66,7 @@ set(ZEEK_CXX_STD cxx_std_17 CACHE STRING "The C++ standard to use.") set(ZEEK_SANITIZERS "" CACHE STRING "Sanitizers to use when building.") -set(CPACK_SOURCE_IGNORE_FILES "" CACHE STRING "CPack source ignore files.") +set(CPACK_SOURCE_IGNORE_FILES "" CACHE STRING "Files to be ignored by CPack") set(ZEEK_INCLUDE_PLUGINS "" CACHE STRING "Extra plugins to add to the build.")