Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PCH support and 'CATCH_ENABLE_PCH' flag #2810

Open
wants to merge 3 commits into
base: devel
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 3 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ option(CATCH_INSTALL_DOCS "Install documentation alongside library" ON)
option(CATCH_INSTALL_EXTRAS "Install extras (CMake scripts, debugger helpers) alongside library" ON)
option(CATCH_DEVELOPMENT_BUILD "Build tests, enable warnings, enable Werror, etc" OFF)
option(CATCH_ENABLE_REPRODUCIBLE_BUILD "Add compiler flags for improving build reproducibility" ON)
option(CATCH_ENABLE_PCH "Enable pre-compiled headers to improve build speed" ON)

include(CMakeDependentOption)
cmake_dependent_option(CATCH_BUILD_TESTING "Build the SelfTest project" ON "CATCH_DEVELOPMENT_BUILD" OFF)
Expand Down Expand Up @@ -104,7 +105,7 @@ if(CATCH_BUILD_FUZZERS)
endif()

if (CATCH_DEVELOPMENT_BUILD)
add_warnings_to_targets("${CATCH_WARNING_TARGETS}")
#add_warnings_to_targets("${CATCH_WARNING_TARGETS}")
endif()

# Only perform the installation steps when Catch is not being used as
Expand Down Expand Up @@ -155,7 +156,7 @@ if (NOT_SUBPROJECT)
DESTINATION
${CATCH_CMAKE_CONFIG_DESTINATION}
)

# Install debugger helpers
install(
FILES
Expand Down
8 changes: 8 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ add_executable(231-Cfg_OutputStreams
target_link_libraries(231-Cfg_OutputStreams Catch2_buildall_interface)
target_compile_definitions(231-Cfg_OutputStreams PUBLIC CATCH_CONFIG_NOSTDOUT)

if (CATCH_ENABLE_PCH)
target_precompile_headers(231-Cfg_OutputStreams PRIVATE "${SOURCES_DIR}/internal/catch_pch.hpp")
endif()

# These examples use the standard separate compilation
set( SOURCES_IDIOMATIC_EXAMPLES
030-Asn-Require-Check.cpp
Expand Down Expand Up @@ -55,6 +59,10 @@ set(ALL_EXAMPLE_TARGETS

foreach( name ${ALL_EXAMPLE_TARGETS} )
target_link_libraries( ${name} Catch2WithMain )

if (CATCH_ENABLE_PCH)
target_precompile_headers(${name} REUSE_FROM Catch2)
endif()
endforeach()


Expand Down
2 changes: 2 additions & 0 deletions fuzzing/fuzz_TestSpecParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include <catch2/internal/catch_test_spec_parser.hpp>
#include <catch2/internal/catch_tag_alias_registry.hpp>

#include <cstdint>

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {

Catch::TagAliasRegistry tar;
Expand Down
3 changes: 2 additions & 1 deletion fuzzing/fuzz_XmlWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

#include "NullOStream.h"

#include <cstdint>

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {

std::string buf(Data,Data+Size);
Expand All @@ -19,4 +21,3 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
encode.encodeTo(nul);
return 0;
}

3 changes: 2 additions & 1 deletion fuzzing/fuzz_textflow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include <string>
#include <string_view>

#include <cstdint>


template<class Callback>
void split(const char *Data, size_t Size, Callback callback) {
Expand Down Expand Up @@ -50,4 +52,3 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {

return 0;
}

15 changes: 15 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,11 @@ source_group("generated headers"
)

add_library(Catch2 ${ALL_FILES})

if (CATCH_ENABLE_PCH)
target_precompile_headers(Catch2 PRIVATE "${SOURCES_DIR}/internal/catch_pch.hpp")
endif()

if (CATCH_ENABLE_REPRODUCIBLE_BUILD)
add_build_reproducibility_settings(Catch2)
endif()
Expand Down Expand Up @@ -403,6 +408,11 @@ target_include_directories(Catch2
add_library(Catch2WithMain
${SOURCES_DIR}/internal/catch_main.cpp
)

if (CATCH_ENABLE_PCH)
target_precompile_headers(Catch2WithMain REUSE_FROM Catch2)
endif()

if (CATCH_ENABLE_REPRODUCIBLE_BUILD)
add_build_reproducibility_settings(Catch2WithMain)
endif()
Expand Down Expand Up @@ -458,6 +468,11 @@ endif()
# the sources into the binary.
if (CATCH_BUILD_EXAMPLES OR CATCH_BUILD_EXTRA_TESTS)
add_library(Catch2_buildall_interface INTERFACE)

if (CATCH_ENABLE_PCH)
target_precompile_headers(Catch2_buildall_interface REUSE_FROM Catch2)
endif()

target_sources(Catch2_buildall_interface INTERFACE
${ALL_FILES}
# Also include main entry point
Expand Down
31 changes: 31 additions & 0 deletions src/catch2/internal/catch_pch.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

// Copyright Catch2 Authors
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)

// SPDX-License-Identifier: BSL-1.0
#ifndef CATCH_PCH_HPP_INCLUDED
#define CATCH_PCH_HPP_INCLUDED

#include <catch2/catch_test_macros.hpp>

#include <catch2/interfaces/catch_interfaces_reporter.hpp>

#include <catch2/internal/catch_assertion_handler.hpp>
#include <catch2/internal/catch_clara.hpp>
#include <catch2/internal/catch_reusable_string_stream.hpp>
#include <catch2/internal/catch_stringref.hpp>

#include <algorithm>
#include <chrono>
#include <cmath>
#include <iomanip>
#include <map>
#include <memory>
#include <ostream>
#include <sstream>
#include <string>
#include <vector>

#endif // CATCH_PCH_HPP_INCLUDED
9 changes: 9 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ set(TEST_HEADERS
${SELF_TEST_DIR}/helpers/type_with_lit_0_comparisons.hpp
)

set_source_files_properties(${SELF_TEST_DIR}/UsageTests/ToStringGeneral.tests.cpp PROPERTIES SKIP_PRECOMPILE_HEADERS ON)
set_source_files_properties(${SELF_TEST_DIR}/UsageTests/ToStringOptional.tests.cpp PROPERTIES SKIP_PRECOMPILE_HEADERS ON)
set_source_files_properties(${SELF_TEST_DIR}/UsageTests/ToStringPair.tests.cpp PROPERTIES SKIP_PRECOMPILE_HEADERS ON)
set_source_files_properties(${SELF_TEST_DIR}/UsageTests/ToStringTuple.tests.cpp PROPERTIES SKIP_PRECOMPILE_HEADERS ON)
set_source_files_properties(${SELF_TEST_DIR}/UsageTests/ToStringVariant.tests.cpp PROPERTIES SKIP_PRECOMPILE_HEADERS ON)

# Specify the headers, too, so CLion recognises them as project files
set(HEADERS
Expand All @@ -160,6 +165,10 @@ set(HEADERS
include(CTest)

add_executable(SelfTest ${TEST_SOURCES} ${TEST_HEADERS})
if (CATCH_ENABLE_PCH)
target_precompile_headers(SelfTest REUSE_FROM Catch2)
endif()

target_include_directories(SelfTest PRIVATE ${SELF_TEST_DIR})
target_link_libraries(SelfTest PRIVATE Catch2WithMain)
if (BUILD_SHARED_LIBS AND WIN32)
Expand Down