From 41c7570e1ea803e9635d9bcabba5fc221c94e7e6 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Mon, 9 Dec 2024 18:16:46 +0000 Subject: [PATCH] cmake: Make intrusive (formerly embedded) tests into a separate option Previously, the CMake build enabled tests by default, and enabled both modular and intrusive (embedded) tests with a single option. This is a really bad idea if anyone is using CMake-built binaries in production. DBUS_BUILD_TESTS now enables only the modular tests, which are safe to enable in production builds. A new DBUS_ENABLE_INTRUSIVE_TESTS option enables the intrusive test instrumentation. To preserve existing test coverage, explicitly enable the intrusive tests in most CMake-based Gitlab-CI jobs (Debian native, openSUSE native, Windows). In jobs that have a mirrored pair of production/debug builds (openSUSE and Debian mingw32/mingw64 cmake), instead we leave the production build as-is and only build full test coverage in the debug build. Co-authored-by: Philip Withnall Signed-off-by: Simon McVittie --- .gitlab-ci.yml | 11 +++++++---- CMakeLists.txt | 13 +++++++------ README.cmake | 3 +++ cmake/modules/Macros.cmake | 2 +- tools/ci-build.sh | 8 ++++++++ 5 files changed, 26 insertions(+), 11 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index a9928968..45b8407b 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -167,13 +167,14 @@ debian image: - .unix-host-build stage: build -debian cmake: +debian cmake debug: extends: - .cmake-common - .debian-build when: manual variables: ci_buildsys: "cmake" + ci_variant: "debug" debian meson: extends: @@ -287,12 +288,13 @@ opensuse image: - .unix-host-build stage: build -opensuse cmake: +opensuse cmake debug: extends: - .cmake-common - .suse-build variables: ci_local_packages: "no" + ci_variant: "debug" # TODO: https://gitlab.freedesktop.org/dbus/dbus/-/issues/520 opensuse mingw32 cmake: @@ -360,14 +362,14 @@ windows msys64 ucrt64 cmake: - $env:MSYS2_PATH_TYPE = "inherit" - $env:PATH += ";C:\msys64\usr\bin" # FIXME: glib from msys2 has issues, disable it for now - - C:\msys64\usr\bin\bash -lc 'cmake -G \"MinGW Makefiles\" -S . -B build -DDBUS_WITH_GLIB=OFF && cmake --build build --config Release' + - C:\msys64\usr\bin\bash -lc 'cmake -G \"MinGW Makefiles\" -S . -B build -DDBUS_WITH_GLIB=OFF -DDBUS_ENABLE_INTRUSIVE_TESTS=ON && cmake --build build --config Release' windows vs15-64 cmake: extends: - .cmake-common - .win-build script: - - cmake -DCMAKE_PREFIX_PATH=C:/ -G "Visual Studio 15 2017 Win64" -DCMAKE_BUILD_TYPE=Debug -DDBUS_ENABLE_VERBOSE_MODE=OFF -S . -B build + - cmake -DCMAKE_PREFIX_PATH=C:/ -G "Visual Studio 15 2017 Win64" -DCMAKE_BUILD_TYPE=Debug -DDBUS_ENABLE_VERBOSE_MODE=OFF -DDBUS_ENABLE_INTRUSIVE_TESTS=ON -S . -B build - cmake --build build --config Debug - cmake --install build --config Debug # FIXME: a few tests timeout on gitlab runner for unknown reason @@ -469,6 +471,7 @@ freebsd cmake debug: - .cmake-common - .build-env-freebsd variables: + ci_variant: "debug" # Don't build doxygen documentation since installing the required tools # massively increases the VM image (and therefore container) size. CI_BUILD_ARGS: "-DDBUS_ENABLE_DOXYGEN_DOCS=OFF -DDBUS_ENABLE_XML_DOCS=ON -DCMAKE_BUILD_TYPE=Debug" diff --git a/CMakeLists.txt b/CMakeLists.txt index 3f9f37f1..c2f28800 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -25,6 +25,7 @@ project(dbus ) option(DBUS_BUILD_TESTS "enable unit test code" ON) +option(DBUS_ENABLE_INTRUSIVE_TESTS "enable tests that require insecure extra code in the library and binaries" OFF) # replacement for AC_C_BIGENDIAN include (TestBigEndian) @@ -443,7 +444,6 @@ enable_testing() ########### command line options ############### if(DBUS_BUILD_TESTS) - set(DBUS_ENABLE_INTRUSIVE_TESTS ON) set(DBUS_ENABLE_MODULAR_TESTS ON) endif() @@ -639,7 +639,7 @@ add_definitions(-DHAVE_CONFIG_H) add_definitions(${DBUS_BUS_CFLAGS}) -if(DBUS_BUILD_TESTS) +if(DBUS_ENABLE_MODULAR_TESTS OR DBUS_ENABLE_INTRUSIVE_TESTS) # set variables used for the .in files (substituted by configure_file) in test/data: set(DBUS_TEST_EXEC ${Z_DRIVE_IF_WINE}${CMAKE_RUNTIME_OUTPUT_DIRECTORY}${IDE_BIN}) # Working directory for build-time tests, so that they'll pick up @@ -700,7 +700,7 @@ endif() add_subdirectory( dbus ) add_subdirectory( bus ) -if(DBUS_BUILD_TESTS) +if(DBUS_ENABLE_MODULAR_TESTS OR DBUS_ENABLE_INTRUSIVE_TESTS) add_subdirectory( test ) add_custom_target(check COMMAND ctest -R ^test-.* @@ -752,6 +752,7 @@ if(MSVC) message(" MSVC code analyze mode: ${DBUS_MSVC_ANALYZE} ") endif() message(" Building unit tests: ${DBUS_BUILD_TESTS} ") +message(" Building intrusive tests: ${DBUS_ENABLE_INTRUSIVE_TESTS} ") message(" Building with GLib: ${DBUS_WITH_GLIB} ") message(" Building verbose mode: ${DBUS_ENABLE_VERBOSE_MODE} ") message(" Building w/o assertions: ${DBUS_DISABLE_ASSERT} ") @@ -786,11 +787,11 @@ message(" build timestamp: ${DBUS_BUILD_TIMESTAMP} " endif() message(" ") -if(DBUS_BUILD_TESTS) - message("NOTE: building with unit tests increases the size of the installed library and renders it insecure.") +if(DBUS_ENABLE_INTRUSIVE_TESTS) + message("NOTE: building with intrusive test code increases the size of the installed library and renders it insecure.") endif() -if(DBUS_BUILD_TESTS AND DBUS_DISABLE_ASSERT) +if(DBUS_ENABLE_INTRUSIVE_TESTS AND DBUS_DISABLE_ASSERT) message("NOTE: building with unit tests but without assertions means tests may not properly report failures (this configuration is only useful when doing something like profiling the tests)") endif() diff --git a/README.cmake b/README.cmake index 03e5f27b..81cac84b 100644 --- a/README.cmake +++ b/README.cmake @@ -124,6 +124,9 @@ CMAKE_INSTALL_PREFIX:PATH=C:/Program Files/dbus // enable unit test code DBUS_BUILD_TESTS:BOOL=ON +// embed intrusive test code in the library and binaries +DBUS_ENABLE_INTRUSIVE_TESTS:BOOL=ON + // The name of the dbus daemon executable DBUS_DAEMON_NAME:STRING=dbus-daemon diff --git a/cmake/modules/Macros.cmake b/cmake/modules/Macros.cmake index 044e2a70..d952b5f3 100644 --- a/cmake/modules/Macros.cmake +++ b/cmake/modules/Macros.cmake @@ -1,6 +1,6 @@ option(DBUS_USE_WINE "set to 1 or ON to support running test cases with Wine" OFF) -if(DBUS_BUILD_TESTS AND CMAKE_CROSSCOMPILING AND CMAKE_SYSTEM_NAME STREQUAL "Windows") +if((DBUS_ENABLE_MODULAR_TESTS OR DBUS_ENABLE_INTRUSIVE_TESTS) AND CMAKE_CROSSCOMPILING AND CMAKE_SYSTEM_NAME STREQUAL "Windows") if(CMAKE_HOST_SYSTEM_NAME STREQUAL "Linux") find_file(WINE_EXECUTABLE NAMES wine diff --git a/tools/ci-build.sh b/tools/ci-build.sh index 55a3c1a4..8752904d 100755 --- a/tools/ci-build.sh +++ b/tools/ci-build.sh @@ -269,6 +269,14 @@ case "$ci_buildsys" in ;; esac + set -- "$@" -D DBUS_BUILD_TESTS=ON + + case "$ci_variant" in + (debug) + set -- "$@" -D DBUS_ENABLE_INTRUSIVE_TESTS=ON + ;; + esac + $cmake -DCMAKE_VERBOSE_MAKEFILE=ON -DENABLE_WERROR=ON -S "$srcdir" -B "$ci_builddir" "$@" ${make}