Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/tests
diff options
context:
space:
mode:
authorMartin Hořeňovský2021-02-15 23:56:56 +0100
committerMartin Hořeňovský2021-02-19 23:22:39 +0100
commit8b27041fbe717b45d29cb18441df719e290663ba (patch)
tree766faa8c37eab071ab31aa5a21666e66398aff6c /tests
parentc12170ff69ddc9a0a25ec2025783b815354c6d26 (diff)
downloadcatch3-8b27041fbe717b45d29cb18441df719e290663ba.tar.gz
Add CMake option to generate surrogate TUs in development build
A surrogate TU is TU that includes 1 specific header, and does nothing else. This is useful to verify that the header is self-sufficient and does not require other headers to be included for compilation to succeed. Closes #2106 Closes #2166 (this is a better solution)
Diffstat (limited to 'tests')
-rw-r--r--tests/CMakeLists.txt61
1 files changed, 61 insertions, 0 deletions
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt
index f8bdb3b4..1a490398 100644
--- a/tests/CMakeLists.txt
+++ b/tests/CMakeLists.txt
@@ -1,5 +1,66 @@
include(MiscFunctions)
+if (CATCH_BUILD_SURROGATES)
+ message(STATUS "Configuring targets for surrogate TUs")
+
+ # If the folder does not exist before we ask for output redirect to
+ # a file, it won't work.
+ file(MAKE_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/surrogates)
+
+ # Creates target to generate the surrogate TU for provided header.
+ # Returns the path to the generated file.
+ function(createSurrogateFileTarget sourceHeader pathToFile)
+ set(pathPrefix ${PROJECT_SOURCE_DIR}/src)
+
+ file(RELATIVE_PATH includePath ${pathPrefix} ${sourceHeader})
+
+ get_filename_component(basicFileName "${sourceHeader}" NAME_WE)
+
+ set(surrogateFilePath ${CMAKE_CURRENT_BINARY_DIR}/surrogates/surrogate_${basicFileName}.cpp)
+
+ add_custom_command(
+ OUTPUT ${surrogateFilePath}
+ COMMAND cmake -E echo "\#include <${includePath}>" > "${surrogateFilePath}"
+ VERBATIM
+ )
+
+ set(${pathToFile} ${surrogateFilePath} PARENT_SCOPE)
+ endfunction()
+
+ # Extracts all non-helper (e.g. catch_all.hpp) headers from the
+ # Catch2 target, and returns them through the argument.
+ function(ExtractCatch2Headers OutArg)
+ get_target_property(targetSources Catch2 SOURCES)
+ foreach(Source ${targetSources})
+ string(REGEX MATCH "^.*\\.hpp$" isHeader ${Source})
+ string(REGEX MATCH "_all.hpp" isAllHeader ${Source})
+ if(isHeader AND NOT isAllHeader)
+ list(APPEND AllHeaders ${Source})
+ endif()
+ endforeach()
+ set(${OutArg} ${AllHeaders} PARENT_SCOPE)
+ endfunction()
+
+
+ ExtractCatch2Headers(mainHeaders)
+
+ if (NOT mainHeaders)
+ message(FATAL_ERROR "No headers in the main target were detected. Something is broken.")
+ endif()
+
+ foreach(header ${mainHeaders})
+ createSurrogateFileTarget(${header} pathToGeneratedFile)
+ list(APPEND surrogateFiles ${pathToGeneratedFile})
+ endforeach()
+
+
+ add_executable(Catch2SurrogateTarget
+ ${surrogateFiles}
+ )
+ target_link_libraries(Catch2SurrogateTarget PRIVATE Catch2WithMain)
+
+endif(CATCH_BUILD_SURROGATES)
+
####
# Temporary workaround for VS toolset changes in 2017
# We need to disable <UseFullPaths> property, but CMake doesn't support it