Modernize Boost detection for CMake 3.30+ master
authorThomas Jarosch <thomas.jarosch@intra2net.com>
Wed, 31 Dec 2025 15:02:00 +0000 (16:02 +0100)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Wed, 31 Dec 2025 16:05:21 +0000 (17:05 +0100)
Add CMP0167 policy for CMake 3.30+ to suppress FindBoost deprecation warning.
Use modern Boost imported targets when available (CMake 3.30+).

Fall back to old-style variables for backward compatibility.

Move detection of "Boost" to test/CMakeLists.txt only.

Tested with:

cmake version   gcc version   Boost version
3.31.6 (F42)    15.2.1        1.83.0
3.28.2          4.4.4         1.44.0
3.28.2          8.3.1         1.76.0

CMakeLists.txt
test/CMakeLists.txt

index 105f83a..9519a09 100644 (file)
@@ -46,11 +46,6 @@ endif (${CMAKE_BUILD_TYPE} STREQUAL Debug)
 find_package(LibUSB REQUIRED)
 include_directories(${LIBUSB_INCLUDE_DIR})
 
-# Find Boost
-if (BUILD_TESTS)
-    find_package(Boost REQUIRED)
-endif ()
-
 # Set components
 set(CPACK_COMPONENTS_ALL sharedlibs staticlibs headers)
 set(CPACK_COMPONENT_SHAREDLIBS_DISPLAY_NAME "Shared libraries")
index c44c614..2ba4206 100644 (file)
@@ -1,13 +1,38 @@
+# Unit tests only: Modern Boost integration (CMake 3.30+)
+# Use CMake's built-in Boost support instead of deprecated FindBoost module
+# For CMake < 3.30, keep old behavior to avoid errors
+if (CMAKE_VERSION VERSION_GREATER_EQUAL 3.30)
+    cmake_policy(SET CMP0167 NEW)
+endif ()
+
+# Make CMake respect variables like BOOST_ROOT even though we look for "Boost"
+if (CMAKE_VERSION VERSION_GREATER_EQUAL 3.27)
+    cmake_policy(SET CMP0144 NEW)
+endif ()
+
+# Find Boost unit test framework. We don't want to link that to libftdi1 itself.
 find_package(Boost COMPONENTS unit_test_framework REQUIRED)
 
 enable_testing()
 
-INCLUDE_DIRECTORIES(BEFORE ${PROJECT_SOURCE_DIR}/src ${Boost_INCLUDE_DIRS})
+# CMake 3.28 compatibility: include_directories only needed for CMake < 3.30
+# (imported targets handle include paths automatically in CMake 3.30+)
+if (CMAKE_VERSION VERSION_LESS 3.30)
+    INCLUDE_DIRECTORIES(BEFORE ${Boost_INCLUDE_DIRS})
+endif ()
+
+INCLUDE_DIRECTORIES(BEFORE ${PROJECT_SOURCE_DIR}/src)
 
 set(cpp_tests basic.cpp baudrate.cpp)
 
 add_executable(test_libftdi1 ${cpp_tests})
-target_link_libraries(test_libftdi1 ftdi1 ${Boost_UNIT_TEST_FRAMEWORK_LIBRARIES})
+
+# Use modern imported targets for CMake 3.30+, fallback to old-style variables
+if (CMAKE_VERSION VERSION_GREATER_EQUAL 3.30)
+    target_link_libraries(test_libftdi1 ftdi1 Boost::unit_test_framework)
+else ()
+    target_link_libraries(test_libftdi1 ftdi1 ${Boost_UNIT_TEST_FRAMEWORK_LIBRARIES})
+endif ()
 
 add_test(test_libftdi1 test_libftdi1)