From b9b8bfc62abbdcd4418589944ffa4b2005c65898 Mon Sep 17 00:00:00 2001 From: Martchus Date: Tue, 31 Jan 2023 22:36:21 +0100 Subject: [PATCH] Fix and improve code for finding CppUnit * Remove `FORCE` in initialization of cache variables as this makes the library/include dir effectively *not* configurable * Try using `find_package()` as the vcpkg package provides a CMake module --- cmake/modules/TestTarget.cmake | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/cmake/modules/TestTarget.cmake b/cmake/modules/TestTarget.cmake index 1cb4c62..69adb4a 100644 --- a/cmake/modules/TestTarget.cmake +++ b/cmake/modules/TestTarget.cmake @@ -14,10 +14,10 @@ if (NOT META_NO_CPP_UNIT) # make cppunit library/include dir configurable set(CPP_UNIT_LIB NOTFOUND - CACHE FILEPATH "cppunit lib" FORCE) + CACHE FILEPATH "cppunit lib") set(CPP_UNIT_INCLUDE_DIR NOTFOUND - CACHE FILEPATH "cppunit include dir" FORCE) + CACHE FILEPATH "cppunit include dir") # set default for minimum version (only checked when using pkg-config) if (NOT META_REQUIRED_CPP_UNIT_VERSION) @@ -31,20 +31,28 @@ if (NOT META_NO_CPP_UNIT) if (CPP_UNIT_CONFIG_${META_PROJECT_NAME}_FOUND) set(CPP_UNIT_LIB "${CPP_UNIT_CONFIG_${META_PROJECT_NAME}_LDFLAGS_OTHER}" "${CPP_UNIT_CONFIG_${META_PROJECT_NAME}_LIBRARIES}" - CACHE FILEPATH "cppunit lib" FORCE) + CACHE FILEPATH "CppUnit library" FORCE) set(CPP_UNIT_INCLUDE_DIR ${CPP_UNIT_CONFIG_${META_PROJECT_NAME}_INCLUDE_DIRS} - CACHE FILEPATH "cppunit include dir" FORCE) + CACHE FILEPATH "CppUnit include dir" FORCE) link_directories(${CPP_UNIT_CONFIG_${META_PROJECT_NAME}_LIBRARY_DIRS}) - else () - # fall back to find_library - find_library(DETECTED_CPP_UNIT_LIB cppunit) - set(CPP_UNIT_LIB - "${DETECTED_CPP_UNIT_LIB}" - CACHE FILEPATH "cppunit lib" FORCE) endif () endif () + # fall back to find_package (as vcpkg provides one) + if (NOT CPP_UNIT_LIB AND NOT CPP_UNIT_INCLUDE_DIR) + find_package(CppUnit CONFIG) + if (TARGET CppUnit) + set(CPP_UNIT_LIB CppUnit CACHE STRING "CppUnit target" FORCE) + endif () + endif () + + # fall back to find_library + if (NOT CPP_UNIT_LIB AND NOT CPP_UNIT_INCLUDE_DIR) + find_library(DETECTED_CPP_UNIT_LIB cppunit) + set(CPP_UNIT_LIB "${DETECTED_CPP_UNIT_LIB}" CACHE FILEPATH "CppUnit library" FORCE) + endif () + if (NOT CPP_UNIT_LIB) message(WARNING "Unable to add test target because cppunit could not be located.") set(META_HAVE_TESTS NO)