summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Help/manual/cmake-properties.7.rst1
-rw-r--r--Help/manual/cmake-variables.7.rst1
-rw-r--r--Help/prop_tgt/PCH_INSTANTIATE_TEMPLATES.rst13
-rw-r--r--Help/release/dev/PCH_INSTANTIATE_TEMPLATES.rst7
-rw-r--r--Help/variable/CMAKE_PCH_INSTANTIATE_TEMPLATES.rst7
-rw-r--r--Modules/Compiler/Clang.cmake3
-rw-r--r--Source/cmGeneratorTarget.cxx10
-rw-r--r--Source/cmTarget.cxx1
-rw-r--r--Tests/RunCMake/CMakeLists.txt4
-rw-r--r--Tests/RunCMake/PrecompileHeaders/PchInstantiateTemplates-check.cmake17
-rw-r--r--Tests/RunCMake/PrecompileHeaders/PchInstantiateTemplates.cmake16
-rw-r--r--Tests/RunCMake/PrecompileHeaders/RunCMakeTest.cmake5
12 files changed, 84 insertions, 1 deletions
diff --git a/Help/manual/cmake-properties.7.rst b/Help/manual/cmake-properties.7.rst
index 73130b6..cb9579e 100644
--- a/Help/manual/cmake-properties.7.rst
+++ b/Help/manual/cmake-properties.7.rst
@@ -315,6 +315,7 @@ Properties on Targets
/prop_tgt/OUTPUT_NAME_CONFIG
/prop_tgt/OUTPUT_NAME
/prop_tgt/PCH_WARN_INVALID
+ /prop_tgt/PCH_INSTANTIATE_TEMPLATES
/prop_tgt/PDB_NAME_CONFIG
/prop_tgt/PDB_NAME
/prop_tgt/PDB_OUTPUT_DIRECTORY_CONFIG
diff --git a/Help/manual/cmake-variables.7.rst b/Help/manual/cmake-variables.7.rst
index f776018..d50005c 100644
--- a/Help/manual/cmake-variables.7.rst
+++ b/Help/manual/cmake-variables.7.rst
@@ -444,6 +444,7 @@ Variables that Control the Build
/variable/CMAKE_OSX_DEPLOYMENT_TARGET
/variable/CMAKE_OSX_SYSROOT
/variable/CMAKE_PCH_WARN_INVALID
+ /variable/CMAKE_PCH_INSTANTIATE_TEMPLATES
/variable/CMAKE_PDB_OUTPUT_DIRECTORY
/variable/CMAKE_PDB_OUTPUT_DIRECTORY_CONFIG
/variable/CMAKE_POSITION_INDEPENDENT_CODE
diff --git a/Help/prop_tgt/PCH_INSTANTIATE_TEMPLATES.rst b/Help/prop_tgt/PCH_INSTANTIATE_TEMPLATES.rst
new file mode 100644
index 0000000..7c1af2a
--- /dev/null
+++ b/Help/prop_tgt/PCH_INSTANTIATE_TEMPLATES.rst
@@ -0,0 +1,13 @@
+PCH_INSTANTIATE_TEMPLATES
+-------------------------
+
+.. versionadded:: 3.19
+
+When this property is set to true, the precompiled header compiler options
+will contain a flag to instantiate templates during the generation of the PCH
+if supported. This can significantly improve compile times. Supported in Clang
+since version 11.
+
+This property is initialized by the value of the
+:variable:`CMAKE_PCH_INSTANTIATE_TEMPLATES` variable if it is set when a target
+is created. If that variable is not set, the property defaults to ``ON``.
diff --git a/Help/release/dev/PCH_INSTANTIATE_TEMPLATES.rst b/Help/release/dev/PCH_INSTANTIATE_TEMPLATES.rst
new file mode 100644
index 0000000..0334204
--- /dev/null
+++ b/Help/release/dev/PCH_INSTANTIATE_TEMPLATES.rst
@@ -0,0 +1,7 @@
+PCH_INSTANTIATE_TEMPLATES
+-------------------------
+
+* The :prop_tgt:`PCH_INSTANTIATE_TEMPLATES` target property was added to enable
+ template instantiation in the precompiled header. This is enabled by default
+ and offers a roughly 20% compile time improvement. Currently only supported
+ by Clang 11.
diff --git a/Help/variable/CMAKE_PCH_INSTANTIATE_TEMPLATES.rst b/Help/variable/CMAKE_PCH_INSTANTIATE_TEMPLATES.rst
new file mode 100644
index 0000000..9867f17
--- /dev/null
+++ b/Help/variable/CMAKE_PCH_INSTANTIATE_TEMPLATES.rst
@@ -0,0 +1,7 @@
+CMAKE_PCH_INSTANTIATE_TEMPLATES
+-------------------------------
+
+.. versionadded:: 3.19
+
+This variable is used to initialize the :prop_tgt:`PCH_INSTANTIATE_TEMPLATES`
+property of targets when they are created.
diff --git a/Modules/Compiler/Clang.cmake b/Modules/Compiler/Clang.cmake
index f7858d7..c43265d 100644
--- a/Modules/Compiler/Clang.cmake
+++ b/Modules/Compiler/Clang.cmake
@@ -106,6 +106,9 @@ else()
if (NOT CMAKE_GENERATOR MATCHES "Xcode")
set(CMAKE_PCH_PROLOGUE "#pragma clang system_header")
endif()
+ if(CMAKE_${lang}_COMPILER_VERSION VERSION_GREATER_EQUAL 11.0.0 AND NOT __is_apple_clang)
+ set(CMAKE_${lang}_COMPILE_OPTIONS_INSTANTIATE_TEMPLATES_PCH -fpch-instantiate-templates)
+ endif()
set(CMAKE_${lang}_COMPILE_OPTIONS_USE_PCH -Xclang -include-pch -Xclang <PCH_FILE> -Xclang -include -Xclang <PCH_HEADER>)
set(CMAKE_${lang}_COMPILE_OPTIONS_CREATE_PCH -Xclang -emit-pch -Xclang -include -Xclang <PCH_HEADER> -x ${__pch_header_${lang}})
endmacro()
diff --git a/Source/cmGeneratorTarget.cxx b/Source/cmGeneratorTarget.cxx
index 1bb069f..c0f2495 100644
--- a/Source/cmGeneratorTarget.cxx
+++ b/Source/cmGeneratorTarget.cxx
@@ -4134,6 +4134,16 @@ std::string cmGeneratorTarget::GetPchCreateCompileOptions(
cmStrCat("CMAKE_", language, "_COMPILE_OPTIONS_INVALID_PCH"));
}
+ if (this->GetPropertyAsBool("PCH_INSTANTIATE_TEMPLATES")) {
+ std::string varName = cmStrCat(
+ "CMAKE_", language, "_COMPILE_OPTIONS_INSTANTIATE_TEMPLATES_PCH");
+ std::string instantiateOption =
+ this->Makefile->GetSafeDefinition(varName);
+ if (!instantiateOption.empty()) {
+ createOptionList = cmStrCat(createOptionList, ";", instantiateOption);
+ }
+ }
+
const std::string createOptVar =
cmStrCat("CMAKE_", language, "_COMPILE_OPTIONS_CREATE_PCH");
diff --git a/Source/cmTarget.cxx b/Source/cmTarget.cxx
index ececee7..e076d1e 100644
--- a/Source/cmTarget.cxx
+++ b/Source/cmTarget.cxx
@@ -381,6 +381,7 @@ cmTarget::cmTarget(std::string const& name, cmStateEnums::TargetType type,
initPropValue("UNITY_BUILD_BATCH_SIZE", "8");
initPropValue("UNITY_BUILD_MODE", "BATCH");
initPropValue("PCH_WARN_INVALID", "ON");
+ initPropValue("PCH_INSTANTIATE_TEMPLATES", "ON");
#ifdef __APPLE__
if (this->GetGlobalGenerator()->IsXcode()) {
diff --git a/Tests/RunCMake/CMakeLists.txt b/Tests/RunCMake/CMakeLists.txt
index 7bc0e0f..e586961 100644
--- a/Tests/RunCMake/CMakeLists.txt
+++ b/Tests/RunCMake/CMakeLists.txt
@@ -740,7 +740,9 @@ endif()
add_RunCMake_test("CTestCommandExpandLists")
-add_RunCMake_test(PrecompileHeaders -DCMAKE_C_COMPILER_ID=${CMAKE_C_COMPILER_ID})
+add_RunCMake_test(PrecompileHeaders -DCMAKE_C_COMPILER_ID=${CMAKE_C_COMPILER_ID}
+ -DCMAKE_C_COMPILER_VERSION=${CMAKE_C_COMPILER_VERSION})
+
add_RunCMake_test("UnityBuild")
if(WIN32)
diff --git a/Tests/RunCMake/PrecompileHeaders/PchInstantiateTemplates-check.cmake b/Tests/RunCMake/PrecompileHeaders/PchInstantiateTemplates-check.cmake
new file mode 100644
index 0000000..648d387
--- /dev/null
+++ b/Tests/RunCMake/PrecompileHeaders/PchInstantiateTemplates-check.cmake
@@ -0,0 +1,17 @@
+file(STRINGS ${RunCMake_TEST_BINARY_DIR}/compile_commands.json empty_dir_commands
+ REGEX "command.*-fpch-instantiate-templates.*empty.dir/cmake_pch.h")
+file(STRINGS ${RunCMake_TEST_BINARY_DIR}/compile_commands.json foo_dir_commands
+ REGEX "command.*-fpch-instantiate-templates.*foo.dir/cmake_pch.h")
+
+list(LENGTH empty_dir_commands empty_dir_commands_size)
+list(LENGTH foo_dir_commands foo_dir_commands_size)
+
+if (empty_dir_commands_size EQUAL 0)
+ set(RunCMake_TEST_FAILED "empty target should have -fpch-instantiate-templates compile option present")
+ return()
+endif()
+
+if (foo_dir_commands_size GREATER 0)
+ set(RunCMake_TEST_FAILED "foo target should not have -fpch-instantiate-templates compile option present")
+ return()
+endif()
diff --git a/Tests/RunCMake/PrecompileHeaders/PchInstantiateTemplates.cmake b/Tests/RunCMake/PrecompileHeaders/PchInstantiateTemplates.cmake
new file mode 100644
index 0000000..3aebbe0
--- /dev/null
+++ b/Tests/RunCMake/PrecompileHeaders/PchInstantiateTemplates.cmake
@@ -0,0 +1,16 @@
+enable_language(C)
+
+set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
+
+add_library(empty empty.c)
+target_precompile_headers(empty PUBLIC
+ <stdio.h>
+ <string.h>
+)
+
+add_library(foo foo.c)
+target_precompile_headers(foo PUBLIC
+ <stdio.h>
+ <string.h>
+)
+set_target_properties(foo PROPERTIES PCH_INSTANTIATE_TEMPLATES OFF)
diff --git a/Tests/RunCMake/PrecompileHeaders/RunCMakeTest.cmake b/Tests/RunCMake/PrecompileHeaders/RunCMakeTest.cmake
index 381b800..74670ba 100644
--- a/Tests/RunCMake/PrecompileHeaders/RunCMakeTest.cmake
+++ b/Tests/RunCMake/PrecompileHeaders/RunCMakeTest.cmake
@@ -20,4 +20,9 @@ run_test(PchReuseFromSubdir)
run_cmake(PchMultilanguage)
if(RunCMake_GENERATOR MATCHES "Make|Ninja")
run_cmake(PchWarnInvalid)
+
+ if(CMAKE_C_COMPILER_ID STREQUAL "Clang" AND
+ CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL 11.0.0)
+ run_cmake(PchInstantiateTemplates)
+ endif()
endif()