Skip to content

Commit d94d77b

Browse files
committed
Namespace CMake tests with project name and update CMake find module
1 parent 96c9d02 commit d94d77b

File tree

2 files changed

+80
-11
lines changed

2 files changed

+80
-11
lines changed

README.md

+78-9
Original file line numberDiff line numberDiff line change
@@ -310,10 +310,8 @@ target_link_libraries(
310310
"test-drive::test-drive"
311311
)
312312
313-
add_test("all-tests" "${PROJECT_NAME}-tester")
314-
315313
foreach(t IN LISTS tests)
316-
add_test("${t}" "${PROJECT_NAME}-tester" "${t}")
314+
add_test("${PROJECT_NAME}/${t}" "${PROJECT_NAME}-tester" "${t}")
317315
endforeach()
318316
```
319317

@@ -323,6 +321,57 @@ endforeach()
323321
The following module allows to find or fetch an installation of this project in CMake
324322

325323
```cmake
324+
#[[.rst:
325+
Find test-drive
326+
---------------
327+
328+
Makes the test-drive project available.
329+
330+
Imported Targets
331+
^^^^^^^^^^^^^^^^
332+
333+
This module provides the following imported target, if found:
334+
335+
``test-drive::test-drive``
336+
The test-drive library
337+
338+
339+
Result Variables
340+
^^^^^^^^^^^^^^^^
341+
342+
This module will define the following variables:
343+
344+
``TEST_DRIVE_FOUND``
345+
True if the test-drive library is available
346+
347+
``TEST_DRIVE_SOURCE_DIR``
348+
Path to the source directory of the test-drive project,
349+
only set if the project is included as source.
350+
351+
``TEST_DRIVE_BINARY_DIR``
352+
Path to the binary directory of the test-drive project,
353+
only set if the project is included as source.
354+
355+
Cache variables
356+
^^^^^^^^^^^^^^^
357+
358+
The following cache variables may be set to influence the library detection:
359+
360+
``TEST_DRIVE_FIND_METHOD``
361+
Methods to find or make the project available. Available methods are
362+
- ``cmake``: Try to find via CMake config file
363+
- ``pkgconf``: Try to find via pkg-config file
364+
- ``subproject``: Use source in subprojects directory
365+
- ``fetch``: Fetch the source from upstream
366+
367+
``TEST_DRIVE_DIR``
368+
Used for searching the CMake config file
369+
370+
``TEST_DRIVE_SUBPROJECT``
371+
Directory to find the test-drive subproject, relative to the project root
372+
373+
#]]
374+
326375
set(_lib "test-drive")
327376
set(_pkg "TEST_DRIVE")
328377
set(_url "https://github.com/fortran-lang/test-drive")
@@ -343,6 +392,10 @@ foreach(method ${${_pkg}_FIND_METHOD})
343392
344393
if("${method}" STREQUAL "cmake")
345394
message(STATUS "${_lib}: Find installed package")
395+
if(DEFINED "${_pkg}_DIR")
396+
set("_${_pkg}_DIR")
397+
set("${_lib}_DIR" "${_pkg}_DIR")
398+
endif()
346399
find_package("${_lib}" CONFIG)
347400
if("${_lib}_FOUND")
348401
message(STATUS "${_lib}: Found installed package")
@@ -367,19 +420,24 @@ foreach(method ${${_pkg}_FIND_METHOD})
367420
INTERFACE
368421
"${${_pkg}_INCLUDE_DIRS}"
369422
)
423+
370424
break()
371425
endif()
372426
endif()
373427
374428
if("${method}" STREQUAL "subproject")
375-
set("${_pkg}_SOURCE_DIR" "${PROJECT_SOURCE_DIR}/subprojects/${_lib}")
376-
set("${_pkg}_BINARY_DIR" "${PROJECT_BINARY_DIR}/subprojects/${_lib}")
429+
if(NOT DEFINED "${_pkg}_SUBPROJECT")
430+
set("_${_pkg}_SUBPROJECT")
431+
set("${_pkg}_SUBPROJECT" "subprojects/${_lib}")
432+
endif()
433+
set("${_pkg}_SOURCE_DIR" "${PROJECT_SOURCE_DIR}/${${_pkg}_SUBPROJECT}")
434+
set("${_pkg}_BINARY_DIR" "${PROJECT_BINARY_DIR}/${${_pkg}_SUBPROJECT}")
377435
if(EXISTS "${${_pkg}_SOURCE_DIR}/CMakeLists.txt")
378-
message(STATUS "Include ${_lib} from subprojects")
436+
message(STATUS "Include ${_lib} from ${${_pkg}_SUBPROJECT}")
379437
add_subdirectory(
380438
"${${_pkg}_SOURCE_DIR}"
381439
"${${_pkg}_BINARY_DIR}"
382-
)
440+
)
383441
384442
add_library("${_lib}::${_lib}" INTERFACE IMPORTED)
385443
target_link_libraries("${_lib}::${_lib}" INTERFACE "${_lib}")
@@ -407,6 +465,7 @@ foreach(method ${${_pkg}_FIND_METHOD})
407465
target_link_libraries("${_lib}::${_lib}" INTERFACE "${_lib}")
408466
409467
# We need the module directory in the subproject before we finish the configure stage
468+
FetchContent_GetProperties("${_lib}" SOURCE_DIR "${_pkg}_SOURCE_DIR")
410469
FetchContent_GetProperties("${_lib}" BINARY_DIR "${_pkg}_BINARY_DIR")
411470
if(NOT EXISTS "${${_pkg}_BINARY_DIR}/include")
412471
make_directory("${${_pkg}_BINARY_DIR}/include")
@@ -417,10 +476,20 @@ foreach(method ${${_pkg}_FIND_METHOD})
417476
418477
endforeach()
419478
420-
if(NOT TARGET "${_lib}::${_lib}")
421-
message(FATAL_ERROR "Could not find dependency ${_lib}")
479+
if(TARGET "${_lib}::${_lib}")
480+
set("${_pkg}_FOUND" TRUE)
481+
else()
482+
set("${_pkg}_FOUND" FALSE)
422483
endif()
423484
485+
if(DEFINED "_${_pkg}_SUBPROJECT")
486+
unset("${_pkg}_SUBPROJECT")
487+
unset("_${_pkg}_SUBPROJECT")
488+
endif()
489+
if(DEFINED "_${_pkg}_DIR")
490+
unset("${_lib}_DIR")
491+
unset("_${_pkg}_DIR")
492+
endif()
424493
if(DEFINED "_${_pkg}_FIND_METHOD")
425494
unset("${_pkg}_FIND_METHOD")
426495
unset("_${_pkg}_FIND_METHOD")

test/CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ target_link_libraries(
3636
"${PROJECT_NAME}-lib"
3737
)
3838

39-
add_test("all-tests" "${PROJECT_NAME}-tester")
39+
add_test("${PROJECT_NAME}/all-tests" "${PROJECT_NAME}-tester")
4040

4141
foreach(t IN LISTS tests)
42-
add_test("${t}" "${PROJECT_NAME}-tester" "${t}")
42+
add_test("${PROJECT_NAME}/${t}" "${PROJECT_NAME}-tester" "${t}")
4343
endforeach()

0 commit comments

Comments
 (0)