Skip to content

Commit 7535323

Browse files
committed
Defer enable_testing() to directory scope for CTest
Replaces direct inclusion of CTest with a deferred call to enable_testing() at the directory scope using cmake_language(DEFER). This ensures enable_testing() is executed after the function returns, allowing add_test() to work correctly when cpp_library_setup is used. Also updates comments for clarity.
1 parent e7baccf commit 7535323

1 file changed

Lines changed: 13 additions & 3 deletions

File tree

cpp-library.cmake

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,19 @@ function(cpp_library_setup)
165165
endif()
166166
set(ARG_NAME "${PROJECT_NAME}")
167167

168-
# Include modules that require project() to be called first
169-
# (CTest and GNUInstallDirs need language/architecture information)
170-
include(CTest)
168+
# Enable testing at directory scope (must not be inside function scope for CTest to work)
169+
# This must happen after project(), which has already been called before cpp_library_setup()
170+
# We use cmake_language(DEFER DIRECTORY) to execute enable_testing() at directory scope
171+
# after this function returns. Since project() was called before cpp_library_setup(),
172+
# the deferred enable_testing() will have access to all project information.
173+
if(PROJECT_IS_TOP_LEVEL AND BUILD_TESTING)
174+
# Defer enable_testing() to execute at directory scope after function returns
175+
# This is required because add_test() needs enable_testing() to be at directory scope
176+
cmake_language(DEFER DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} CALL enable_testing)
177+
endif()
178+
179+
# Include installation module that requires project() to be called first
180+
# (GNUInstallDirs needs language/architecture information)
171181
include("${CPP_LIBRARY_ROOT}/cmake/cpp-library-install.cmake")
172182

173183
# Calculate clean name (without namespace prefix) for target alias

0 commit comments

Comments
 (0)