Allow passing compile-time configuration options through CMake

This commit is contained in:
Martin Hořeňovský 2022-02-02 15:36:58 +01:00
parent fc5552d27b
commit 33ffc3b6fc
No known key found for this signature in database
GPG Key ID: DE48307B8B0D381A
8 changed files with 313 additions and 2 deletions

View File

@ -0,0 +1,78 @@
# Copyright Catch2 Authors
# Distributed under the Boost Software License, Version 1.0.
# (See accompanying file LICENSE_1_0.txt or copy at
# https://www.boost.org/LICENSE_1_0.txt)
# SPDX-License-Identifier: BSL-1.0
##
# This file contains options that are materialized into the Catch2
# compiled library. All of them default to OFF, as even the positive
# forms correspond to the user _forcing_ them to ON, while being OFF
# means that Catch2 can use its own autodetection.
#
# For detailed docs look into docs/configuration.md
macro(AddOverridableConfigOption OptionBaseName)
option(CATCH_CONFIG_${OptionBaseName} "Read docs/configuration.md for details" OFF)
option(CATCH_CONFIG_NO_${OptionBaseName} "Read docs/configuration.md for details" OFF)
endmacro()
macro(AddConfigOption OptionBaseName)
option(CATCH_CONFIG_${OptionBaseName} "Read docs/configuration.md for details" OFF)
endmacro()
set(_OverridableOptions
"ANDROID_LOGWRITE"
"COUNTER"
"CPP11_TO_STRING"
"CPP17_BYTE"
"CPP17_OPTIONAL"
"CPP17_STRING_VIEW"
"CPP17_UNCAUGHT_EXCEPTIONS"
"CPP17_VARIANT"
"GLOBAL_NEXTAFTER"
"POSIX_SIGNALS"
"USE_ASYNC"
"WCHAR"
"WINDOWS_SEH"
)
foreach(OptionName ${_OverridableOptions})
AddOverridableConfigOption(${OptionName})
endforeach()
set(_OtherConfigOptions
"COLOUR_ANSI"
"COLOUR_NONE"
"COLOUR_WINDOWS"
"DISABLE_EXCEPTIONS"
"DISABLE_EXCEPTIONS_CUSTOM_HANDLER"
"DISABLE"
"DISABLE_STRINGIFICATION"
"ENABLE_ALL_STRINGMAKERS"
"ENABLE_OPTIONAL_STRINGMAKER"
"ENABLE_PAIR_STRINGMAKER"
"ENABLE_TUPLE_STRINGMAKER"
"ENABLE_VARIANT_STRINGMAKER"
"EXPERIMENTAL_REDIRECT"
"FAST_COMPILE"
"NOSTDOUT"
"PREFIX_ALL"
"WINDOWS_CRTDBG"
)
foreach(OptionName ${_OtherConfigOptions})
AddConfigOption(${OptionName})
endforeach()
set(CATCH_CONFIG_DEFAULT_REPORTER "console" CACHE STRING "Read docs/configuration.md for details. The name of the reporter should be without quotes.")
set(CATCH_CONFIG_CONSOLE_WIDTH "80" CACHE STRING "Read docs/configuration.md for details. Must form a valid integer literal.")
# There is no good way to both turn this into a CMake cache variable,
# and keep reasonable default semantics inside the project. Thus we do
# not define it and users have to provide it as an outside variable.
#set(CATCH_CONFIG_FALLBACK_STRINGIFIER "" CACHE STRING "Read docs/configuration.md for details.")

View File

@ -41,6 +41,7 @@ project(Catch2 LANGUAGES CXX VERSION 3.0.0)
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/CMake") list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_LIST_DIR}/CMake")
include(GNUInstallDirs) include(GNUInstallDirs)
include(CMakePackageConfigHelpers) include(CMakePackageConfigHelpers)
include(CatchConfigOptions)
if(CATCH_DEVELOPMENT_BUILD) if(CATCH_DEVELOPMENT_BUILD)
include(CTest) include(CTest)
endif() endif()

View File

@ -31,6 +31,8 @@ set(BENCHMARK_SOURCES
SOURCE_GROUP("benchmark" FILES ${BENCHMARK_HEADERS} ${BENCHMARK_SOURCES}) SOURCE_GROUP("benchmark" FILES ${BENCHMARK_HEADERS} ${BENCHMARK_SOURCES})
set(INTERNAL_HEADERS set(INTERNAL_HEADERS
"${CMAKE_BINARY_DIR}/generated-includes/catch2/catch_user_config.hpp"
"${SOURCES_DIR}/catch_user_config.hpp.in"
${SOURCES_DIR}/catch_all.hpp ${SOURCES_DIR}/catch_all.hpp
${SOURCES_DIR}/matchers/catch_matchers_all.hpp ${SOURCES_DIR}/matchers/catch_matchers_all.hpp
${SOURCES_DIR}/generators/catch_generators_all.hpp ${SOURCES_DIR}/generators/catch_generators_all.hpp
@ -282,9 +284,15 @@ target_compile_features(Catch2
cxx_variadic_macros cxx_variadic_macros
) )
configure_file(
"${SOURCES_DIR}/catch_user_config.hpp.in"
"${CMAKE_BINARY_DIR}/generated-includes/catch2/catch_user_config.hpp"
)
target_include_directories(Catch2 target_include_directories(Catch2
PUBLIC PUBLIC
$<BUILD_INTERFACE:${SOURCES_DIR}/..> $<BUILD_INTERFACE:${SOURCES_DIR}/..>
$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/generated-includes>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}> $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
) )
@ -323,7 +331,15 @@ if (NOT_SUBPROJECT)
${CATCH_CMAKE_CONFIG_DESTINATION} ${CATCH_CMAKE_CONFIG_DESTINATION}
) )
# Install the headers # Install the headers
install(DIRECTORY ${SOURCES_DIR} DESTINATION ${CMAKE_INSTALL_INCLUDEDIR} FILES_MATCHING PATTERN "*.h*") install(
DIRECTORY
"${SOURCES_DIR}"
"${CMAKE_BINARY_DIR}/generated-includes/catch2" # Also install the generated header
DESTINATION
"${CMAKE_INSTALL_INCLUDEDIR}"
FILES_MATCHING
PATTERN "*.hpp"
)
endif() endif()
# Some tests require a full recompilation of Catch2 lib with different # Some tests require a full recompilation of Catch2 lib with different
@ -342,6 +358,7 @@ if (CATCH_BUILD_EXAMPLES OR CATCH_BUILD_EXTRA_TESTS)
target_include_directories(Catch2_buildall_interface target_include_directories(Catch2_buildall_interface
INTERFACE INTERFACE
$<BUILD_INTERFACE:${SOURCES_DIR}/..> $<BUILD_INTERFACE:${SOURCES_DIR}/..>
$<BUILD_INTERFACE:${CMAKE_BINARY_DIR}/generated-includes>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}> $<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
) )
target_compile_features(Catch2_buildall_interface target_compile_features(Catch2_buildall_interface

View File

@ -9,6 +9,7 @@
#define CATCH_CONFIG_HPP_INCLUDED #define CATCH_CONFIG_HPP_INCLUDED
#include <catch2/catch_test_spec.hpp> #include <catch2/catch_test_spec.hpp>
#include <catch2/catch_user_config.hpp>
#include <catch2/interfaces/catch_interfaces_config.hpp> #include <catch2/interfaces/catch_interfaces_config.hpp>
#include <catch2/internal/catch_unique_ptr.hpp> #include <catch2/internal/catch_unique_ptr.hpp>
#include <catch2/internal/catch_optional.hpp> #include <catch2/internal/catch_optional.hpp>

View File

@ -0,0 +1,201 @@
// Copyright Catch2 Authors
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)
// SPDX-License-Identifier: BSL-1.0
/**\file
* **AUTOGENERATED FROM CMAKE CONFIGURATION**
*
* Contains materialized compile-time configuration provided to Catch2's
* CMake configuration. All compile-time configuration options need to
* be here, and also documented in `docs/configuration.md`.
*/
#ifndef CATCH_USER_CONFIG_HPP_INCLUDED
#define CATCH_USER_CONFIG_HPP_INCLUDED
// ------
// Overridable compilation flags,
// these can have 3 "states": Force Yes, Force No, Use Default.
// Setting both Force Yes and Force No is an error
// ------
#cmakedefine CATCH_CONFIG_ANDROID_LOGWRITE
#cmakedefine CATCH_CONFIG_NO_ANDROID_LOGWRITE
#if defined( CATCH_CONFIG_ANDROID_LOGWRITE ) && \
defined( CATCH_CONFIG_NO_ANDROID_LOGWRITE )
# error Cannot force ANDROID_LOGWRITE to both ON and OFF
#endif
#cmakedefine CATCH_CONFIG_COUNTER
#cmakedefine CATCH_CONFIG_NO_COUNTER
#if defined( CATCH_CONFIG_COUNTER ) && \
defined( CATCH_CONFIG_NO_COUNTER )
# error Cannot force COUNTER to both ON and OFF
#endif
#cmakedefine CATCH_CONFIG_CPP11_TO_STRING
#cmakedefine CATCH_CONFIG_NO_CPP11_TO_STRING
#if defined( CATCH_CONFIG_CPP11_TO_STRING ) && \
defined( CATCH_CONFIG_NO_CPP11_TO_STRING )
# error Cannot force CPP11_TO_STRING to both ON and OFF
#endif
#cmakedefine CATCH_CONFIG_CPP17_BYTE
#cmakedefine CATCH_CONFIG_NO_CPP17_BYTE
#if defined( CATCH_CONFIG_CPP17_BYTE ) && \
defined( CATCH_CONFIG_NO_CPP17_BYTE )
# error Cannot force CPP17_BYTE to both ON and OFF
#endif
#cmakedefine CATCH_CONFIG_CPP17_OPTIONAL
#cmakedefine CATCH_CONFIG_NO_CPP17_OPTIONAL
#if defined( CATCH_CONFIG_CPP17_OPTIONAL ) && \
defined( CATCH_CONFIG_NO_CPP17_OPTIONAL )
# error Cannot force CPP17_OPTIONAL to both ON and OFF
#endif
#cmakedefine CATCH_CONFIG_CPP17_STRING_VIEW
#cmakedefine CATCH_CONFIG_NO_CPP17_STRING_VIEW
#if defined( CATCH_CONFIG_CPP17_STRING_VIEW ) && \
defined( CATCH_CONFIG_NO_CPP17_STRING_VIEW )
# error Cannot force CPP17_STRING_VIEW to both ON and OFF
#endif
#cmakedefine CATCH_CONFIG_CPP17_UNCAUGHT_EXCEPTIONS
#cmakedefine CATCH_CONFIG_NO_CPP17_UNCAUGHT_EXCEPTIONS
#if defined( CATCH_CONFIG_CPP17_UNCAUGHT_EXCEPTIONS ) && \
defined( CATCH_CONFIG_NO_CPP17_UNCAUGHT_EXCEPTIONS )
# error Cannot force CPP17_UNCAUGHT_EXCEPTIONS to both ON and OFF
#endif
#cmakedefine CATCH_CONFIG_CPP17_VARIANT
#cmakedefine CATCH_CONFIG_NO_CPP17_VARIANT
#if defined( CATCH_CONFIG_CPP17_VARIANT ) && \
defined( CATCH_CONFIG_NO_CPP17_VARIANT )
# error Cannot force CPP17_VARIANT to both ON and OFF
#endif
#cmakedefine CATCH_CONFIG_GLOBAL_NEXTAFTER
#cmakedefine CATCH_CONFIG_NO_GLOBAL_NEXTAFTER
#if defined( CATCH_CONFIG_GLOBAL_NEXTAFTER ) && \
defined( CATCH_CONFIG_NO_GLOBAL_NEXTAFTER )
# error Cannot force GLOBAL_NEXTAFTER to both ON and OFF
#endif
#cmakedefine CATCH_CONFIG_POSIX_SIGNALS
#cmakedefine CATCH_CONFIG_NO_POSIX_SIGNALS
#if defined( CATCH_CONFIG_POSIX_SIGNALS ) && \
defined( CATCH_CONFIG_NO_POSIX_SIGNALS )
# error Cannot force POSIX_SIGNALS to both ON and OFF
#endif
#cmakedefine CATCH_CONFIG_USE_ASYNC
#cmakedefine CATCH_CONFIG_NO_USE_ASYNC
#if defined( CATCH_CONFIG_USE_ASYNC ) && \
defined( CATCH_CONFIG_NO_USE_ASYNC )
# error Cannot force USE_ASYNC to both ON and OFF
#endif
#cmakedefine CATCH_CONFIG_WCHAR
#cmakedefine CATCH_CONFIG_NO_WCHAR
#if defined( CATCH_CONFIG_WCHAR ) && \
defined( CATCH_CONFIG_NO_WCHAR )
# error Cannot force WCHAR to both ON and OFF
#endif
#cmakedefine CATCH_CONFIG_WINDOWS_SEH
#cmakedefine CATCH_CONFIG_NO_WINDOWS_SEH
#if defined( CATCH_CONFIG_WINDOWS_SEH ) && \
defined( CATCH_CONFIG_NO_WINDOWS_SEH )
# error Cannot force WINDOWS_SEH to both ON and OFF
#endif
// ------
// Simple toggle defines
// their value is never used and they cannot be overriden
// ------
#cmakedefine CATCH_CONFIG_COLOUR_ANSI
#cmakedefine CATCH_CONFIG_COLOUR_NONE
#cmakedefine CATCH_CONFIG_COLOUR_WINDOWS
#cmakedefine CATCH_CONFIG_DISABLE_EXCEPTIONS
#cmakedefine CATCH_CONFIG_DISABLE_EXCEPTIONS_CUSTOM_HANDLER
#cmakedefine CATCH_CONFIG_DISABLE
#cmakedefine CATCH_CONFIG_DISABLE_STRINGIFICATION
#cmakedefine CATCH_CONFIG_ENABLE_ALL_STRINGMAKERS
#cmakedefine CATCH_CONFIG_ENABLE_OPTIONAL_STRINGMAKER
#cmakedefine CATCH_CONFIG_ENABLE_PAIR_STRINGMAKER
#cmakedefine CATCH_CONFIG_ENABLE_TUPLE_STRINGMAKER
#cmakedefine CATCH_CONFIG_ENABLE_VARIANT_STRINGMAKER
#cmakedefine CATCH_CONFIG_EXPERIMENTAL_REDIRECT
#cmakedefine CATCH_CONFIG_FAST_COMPILE
#cmakedefine CATCH_CONFIG_NOSTDOUT
#cmakedefine CATCH_CONFIG_PREFIX_ALL
#cmakedefine CATCH_CONFIG_WINDOWS_CRTDBG
// ------
// "Variable" defines, these have actual values
// ------
#define CATCH_CONFIG_DEFAULT_REPORTER "@CATCH_CONFIG_DEFAULT_REPORTER@"
#define CATCH_CONFIG_CONSOLE_WIDTH @CATCH_CONFIG_CONSOLE_WIDTH@
// The logic here depends on CMake preprocessing of the file significantly.
// If CMake has CATCH_CONFIG_FALLBACK_STRINGIFIER it will be define below,
// and its value is written into the if. Thus, we undef the old placeholder
// and then define the macro to the actual CMake-provided value.
//
// We can't do it without this indirection, because it is a string-variable
// macro, so we need it's value, and there is no usable default value that
// would let us have it always defined the way the other two are.
#cmakedefine CATCH_CONFIG_FALLBACK_STRINGIFIER
#if defined( CATCH_CONFIG_FALLBACK_STRINGIFIER )
# undef CATCH_CONFIG_FALLBACK_STRINGIFIER
# define CATCH_CONFIG_FALLBACK_STRINGIFIER @CATCH_CONFIG_FALLBACK_STRINGIFIER@
#endif
#endif // CATCH_USER_CONFIG_HPP_INCLUDED

View File

@ -287,7 +287,9 @@
# define CATCH_CONFIG_NEW_CAPTURE # define CATCH_CONFIG_NEW_CAPTURE
#endif #endif
#if !defined(CATCH_INTERNAL_CONFIG_EXCEPTIONS_ENABLED) && !defined(CATCH_CONFIG_DISABLE_EXCEPTIONS) #if !defined( CATCH_INTERNAL_CONFIG_EXCEPTIONS_ENABLED ) && \
!defined( CATCH_CONFIG_DISABLE_EXCEPTIONS ) && \
!defined( CATCH_CONFIG_NO_DISABLE_EXCEPTIONS )
# define CATCH_CONFIG_DISABLE_EXCEPTIONS # define CATCH_CONFIG_DISABLE_EXCEPTIONS
#endif #endif

View File

@ -17,6 +17,8 @@
#ifndef CATCH_CONFIG_ANDROID_LOGWRITE_HPP_INCLUDED #ifndef CATCH_CONFIG_ANDROID_LOGWRITE_HPP_INCLUDED
#define CATCH_CONFIG_ANDROID_LOGWRITE_HPP_INCLUDED #define CATCH_CONFIG_ANDROID_LOGWRITE_HPP_INCLUDED
#include <catch2/catch_user_config.hpp>
#if defined(__ANDROID__) #if defined(__ANDROID__)
# define CATCH_INTERNAL_CONFIG_ANDROID_LOGWRITE # define CATCH_INTERNAL_CONFIG_ANDROID_LOGWRITE
#endif #endif

View File

@ -79,6 +79,15 @@ def concatenate_file(out, filename: str, expand_headers: bool) -> int:
# hundred thousands lines (~300k as of preview3 :-) ) # hundred thousands lines (~300k as of preview3 :-) )
if next_header in concatenated_headers: if next_header in concatenated_headers:
continue continue
# Skip including the auto-generated user config file,
# because it has not been generated yet at this point.
# The code around it should be written so that just not including
# it is equivalent with all-default user configuration.
if next_header == 'catch2/catch_user_config.hpp':
concatenated_headers.add(next_header)
continue
concatenated_headers.add(next_header) concatenated_headers.add(next_header)
concatenated += concatenate_file(out, os.path.join(root_path, next_header), expand_headers) concatenated += concatenate_file(out, os.path.join(root_path, next_header), expand_headers)