diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index ed4e4ab..9626a7f 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,9 +1,54 @@ +cmake_minimum_required(VERSION 3.22) + + +###################### +## Project settings ## +###################### + +# We support building this as top-level project, e.g. in order to test the lib installation +project(util_caching_tests + LANGUAGES CXX +) + + +############### +## C++ setup ## +############### + +# Only do these if this is the main project, and not if it is included through add_subdirectory +if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) + # Require C++17 + set(CMAKE_CXX_STANDARD 17) + set(CMAKE_CXX_STANDARD_REQUIRED ON) + + # Let's ensure -std=c++xx instead of -std=g++xx + set(CMAKE_CXX_EXTENSIONS OFF) + + # Let's nicely support folders in IDEs + set_property(GLOBAL PROPERTY USE_FOLDERS ON) + + # Allow clangd and others to properly understand this C++ project + set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + + # Testing only available if this is the main project + # Note this needs to be done in the main CMakeLists + # since it calls enable_testing, which must be in the + # main CMakeLists. + include(CTest) +endif() + + ################### ## Find packages ## ################### find_package(GTest) +# Find installed lib and its dependencies, if this is build as top-level project +if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME) + find_package(util_caching REQUIRED) +endif() + ########### ## Build ## @@ -11,12 +56,13 @@ find_package(GTest) if(GTEST_FOUND) file(GLOB_RECURSE _tests CONFIGURE_DEPENDS "*.cpp" "*.cc") + list(FILTER _tests EXCLUDE REGEX "${CMAKE_CURRENT_BINARY_DIR}") foreach(_test ${_tests}) get_filename_component(_test_name ${_test} NAME_WE) # make sure we add only one -test to the target string(REGEX REPLACE "-test" "" TEST_TARGET_NAME ${_test_name}) - set(TEST_TARGET_NAME ${PROJECT_NAME}-gtest-${TEST_TARGET_NAME}) + set(TEST_TARGET_NAME util_caching_tests-gtest-${TEST_TARGET_NAME}) message(STATUS "Adding gtest unittest \"${TEST_TARGET_NAME}\" with working dir ${PROJECT_SOURCE_DIR}/${TEST_FOLDER} \n _test: ${_test}" @@ -26,7 +72,7 @@ if(GTEST_FOUND) target_link_libraries(${TEST_TARGET_NAME} PUBLIC ${GTEST_BOTH_LIBRARIES} pthread - ${PROJECT_NAME} + util_caching ) add_test(NAME ${TEST_TARGET_NAME}