summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Help/command/target_precompile_headers.rst122
-rw-r--r--Help/guide/tutorial/index.rst4
-rw-r--r--Help/prop_tgt/INTERFACE_PRECOMPILE_HEADERS.rst2
-rw-r--r--Source/CPack/WiX/cmCPackWIXGenerator.cxx2
-rw-r--r--Source/CPack/cmCPackGenerator.cxx11
-rw-r--r--Source/CPack/cmCPackNSISGenerator.cxx5
-rw-r--r--Source/CPack/cmCPackOSXX11Generator.cxx2
-rw-r--r--Source/CPack/cmCPackPKGGenerator.cxx4
-rw-r--r--Source/CPack/cmCPackSTGZGenerator.cxx3
-rw-r--r--Source/cmGeneratorTarget.cxx9
-rw-r--r--Source/cmTarget.cxx7
-rw-r--r--Tests/RunCMake/PrecompileHeaders/PchReuseFrom.cmake9
-rw-r--r--Tests/RunCMake/PrecompileHeaders/foobar.c8
13 files changed, 117 insertions, 71 deletions
diff --git a/Help/command/target_precompile_headers.rst b/Help/command/target_precompile_headers.rst
index 5ab3766..569c7eb 100644
--- a/Help/command/target_precompile_headers.rst
+++ b/Help/command/target_precompile_headers.rst
@@ -3,33 +3,21 @@ target_precompile_headers
Add a list of header files to precompile.
+Precompiling header files can speed up compilation by creating a partially
+processed version of some header files, and then using that version during
+compilations rather than repeatedly parsing the original headers.
+
+Main Form
+^^^^^^^^^
+
.. code-block:: cmake
target_precompile_headers(<target>
<INTERFACE|PUBLIC|PRIVATE> [header1...]
[<INTERFACE|PUBLIC|PRIVATE> [header2...] ...])
- target_precompile_headers(<target> REUSE_FROM <other_target>)
-
-Adds header files to :prop_tgt:`PRECOMPILE_HEADERS` or
-:prop_tgt:`INTERFACE_PRECOMPILE_HEADERS` target properties.
-
-The second signature will reuse an already precompiled header file artefact
-from another target. This is done by setting the
-:prop_tgt:`PRECOMPILE_HEADERS_REUSE_FROM` to ``<other_target>`` value.
-The ``<other_target>`` will become a dependency of ``<target>``.
-
-.. note::
-
- The second signature will require the same set of compiler options,
- compiler flags, compiler definitions for both ``<target>``, and
- ``<other_target>``. Compilers (e.g. GCC) will issue a warning if the
- precompiled header file cannot be used (``-Winvalid-pch``).
-
-Precompiling header files can speed up compilation by creating a partially
-processed version of some header files, and then using that version during
-compilations rather than repeatedly parsing the original headers.
-
+The command adds header files to the :prop_tgt:`PRECOMPILE_HEADERS` and/or
+:prop_tgt:`INTERFACE_PRECOMPILE_HEADERS` target properties of ``<target>``.
The named ``<target>`` must have been created by a command such as
:command:`add_executable` or :command:`add_library` and must not be an
:ref:`ALIAS target <Alias Targets>`.
@@ -38,25 +26,50 @@ The ``INTERFACE``, ``PUBLIC`` and ``PRIVATE`` keywords are required to
specify the scope of the following arguments. ``PRIVATE`` and ``PUBLIC``
items will populate the :prop_tgt:`PRECOMPILE_HEADERS` property of
``<target>``. ``PUBLIC`` and ``INTERFACE`` items will populate the
-:prop_tgt:`INTERFACE_PRECOMPILE_HEADERS` property of ``<target>``.
-(:ref:`IMPORTED targets <Imported Targets>` only support ``INTERFACE`` items.)
-Repeated calls for the same ``<target>`` append items in the order called.
+:prop_tgt:`INTERFACE_PRECOMPILE_HEADERS` property of ``<target>``
+(:ref:`IMPORTED targets <Imported Targets>` only support ``INTERFACE`` items).
+Repeated calls for the same ``<target>`` will append items in the order called.
+
+Projects should generally avoid using ``PUBLIC`` or ``INTERFACE`` for targets
+that will be :ref:`exported <install(EXPORT)>`, or they should at least use
+the ``$<BUILD_INTERFACE:...>`` generator expression to prevent precompile
+headers from appearing in an installed exported target. Consumers of a target
+should typically be in control of what precompile headers they use, not have
+precompile headers forced on them by the targets being consumed (since
+precompile headers are not typically usage requirements). A notable exception
+to this is where an :ref:`interface library <Interface Libraries>` is created
+to define a commonly used set of precompile headers in one place and then other
+targets link to that interface library privately. In this case, the interface
+library exists specifically to propagate the precompile headers to its
+consumers and the consumer is effectively still in control, since it decides
+whether to link to the interface library or not.
+
+The list of header files is used to generate a header file named
+``cmake_pch.h|xx`` which is used to generate the precompiled header file
+(``.pch``, ``.gch``, ``.pchi``) artifact. The ``cmake_pch.h|xx`` header
+file will be force included (``-include`` for GCC, ``/FI`` for MSVC) to
+all source files, so sources do not need to have ``#include "pch.h"``.
+
+Header file names specified with angle brackets (e.g. ``<unordered_map>``) or
+explicit double quotes (escaped for the :manual:`cmake-language(7)`,
+e.g. ``[["other_header.h"]]``) will be treated as is, and include directories
+must be available for the compiler to find them. Other header file names
+(e.g. ``project_header.h``) are interpreted as being relative to the current
+source directory (e.g. :variable:`CMAKE_CURRENT_SOURCE_DIR`) and will be
+included by absolute path.
-Arguments to ``target_precompile_headers`` may use "generator expressions"
+Arguments to ``target_precompile_headers()`` may use "generator expressions"
with the syntax ``$<...>``.
See the :manual:`cmake-generator-expressions(7)` manual for available
expressions. See the :manual:`cmake-compile-features(7)` manual for
information on compile features and a list of supported compilers.
The ``$<COMPILE_LANGUAGE:...>`` generator expression is particularly
useful for specifying a language-specific header to precompile for
-only one language (e.g. ``CXX`` and not ``C``).
-
-Usage
-^^^^^
+only one language (e.g. ``CXX`` and not ``C``). For example:
.. code-block:: cmake
- target_precompile_headers(<target>
+ target_precompile_headers(myTarget
PUBLIC
project_header.h
"$<$<COMPILE_LANGUAGE:CXX>:cxx_only.h>"
@@ -65,20 +78,6 @@ Usage
<unordered_map>
)
-The list of header files is used to generate a header file named
-``cmake_pch.h|xx`` which is used to generate the precompiled header file
-(``.pch``, ``.gch``, ``.pchi``) artifact. The ``cmake_pch.h|xx`` header
-file will be force included (``-include`` for GCC, ``/FI`` for MSVC) to
-all source files, so sources do not need to have ``#include "pch.h"``.
-
-Header file names specified with angle brackets (e.g. ``<unordered_map>``) or
-explicit double quotes (escaped for the :manual:`cmake-language(7)`,
-e.g. ``[["other_header.h"]]``) will be treated as is, and include directories
-must be available for the compiler to find them. Other header file names
-(e.g. ``project_header.h``) are interpreted as being relative to the current
-source directory (e.g. :variable:`CMAKE_CURRENT_SOURCE_DIR`) and will be
-included by absolute path.
-
When specifying angle brackets inside a :manual:`generator expression
<cmake-generator-expressions(7)>`, be sure to encode the closing ``>``
as ``$<ANGLE-R>``. For example:
@@ -88,13 +87,38 @@ as ``$<ANGLE-R>``. For example:
target_precompile_headers(mylib PRIVATE
"$<$<COMPILE_LANGUAGE:C>:<stddef.h$<ANGLE-R>>"
"$<$<COMPILE_LANGUAGE:CXX>:<cstddef$<ANGLE-R>>"
- )
+ )
+
+
+Reusing Precompile Headers
+^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+The command also supports a second signature which can be used to specify that
+one target re-uses a precompiled header file artefact from another target
+instead of generating its own:
+
+.. code-block:: cmake
+
+ target_precompile_headers(<target> REUSE_FROM <other_target>)
+
+This form sets the :prop_tgt:`PRECOMPILE_HEADERS_REUSE_FROM` property to
+``<other_target>`` and adds a dependency such that ``<target>`` will depend
+on ``<other_target>``. CMake will halt with an error if the
+:prop_tgt:`PRECOMPILE_HEADERS` property of ``<target>`` is already set when
+the ``REUSE_FROM`` form is used.
+
+.. note::
+
+ The ``REUSE_FROM`` form requires the same set of compiler options,
+ compiler flags and compiler definitions for both ``<target>`` and
+ ``<other_target>``. Some compilers (e.g. GCC) may issue a warning if the
+ precompiled header file cannot be used (``-Winvalid-pch``).
See Also
^^^^^^^^
-For disabling precompile headers for specific targets there is the
-property :prop_tgt:`DISABLE_PRECOMPILE_HEADERS`.
+To disable precompile headers for specific targets, see the
+:prop_tgt:`DISABLE_PRECOMPILE_HEADERS` target property.
-For skipping certain source files there is the source file property
-:prop_sf:`SKIP_PRECOMPILE_HEADERS`.
+To prevent precompile headers from being used when compiling a specific
+source file, see the :prop_sf:`SKIP_PRECOMPILE_HEADERS` source file property.
diff --git a/Help/guide/tutorial/index.rst b/Help/guide/tutorial/index.rst
index 3f20aa2..d74d160 100644
--- a/Help/guide/tutorial/index.rst
+++ b/Help/guide/tutorial/index.rst
@@ -610,12 +610,12 @@ CTest will read in this file when it runs. To create a simple dashboard you can
run **cmake** or **cmake-gui** to configure the project, but do not build it
yet. Instead, change directory to the binary tree, and then run::
- ctest [-VV] –D Experimental
+ ctest [-VV] -D Experimental
Remember, for multi-config generators (e.g. Visual Studio), the configuration
type must be specified::
- ctest [-VV] -C Debug –D Experimental
+ ctest [-VV] -C Debug -D Experimental
Or, from an IDE, build the ``Experimental`` target.
diff --git a/Help/prop_tgt/INTERFACE_PRECOMPILE_HEADERS.rst b/Help/prop_tgt/INTERFACE_PRECOMPILE_HEADERS.rst
index 8ff7e8b..e285407 100644
--- a/Help/prop_tgt/INTERFACE_PRECOMPILE_HEADERS.rst
+++ b/Help/prop_tgt/INTERFACE_PRECOMPILE_HEADERS.rst
@@ -7,6 +7,8 @@ Targets may populate this property to publish the header files
for consuming targets to precompile. The :command:`target_precompile_headers`
command populates this property with values given to the ``PUBLIC`` and
``INTERFACE`` keywords. Projects may also get and set the property directly.
+See the discussion in :command:`target_precompile_headers` for guidance on
+appropriate use of this property for installed or exported targets.
Contents of ``INTERFACE_PRECOMPILE_HEADERS`` may use "generator expressions"
with the syntax ``$<...>``. See the :manual:`cmake-generator-expressions(7)`
diff --git a/Source/CPack/WiX/cmCPackWIXGenerator.cxx b/Source/CPack/WiX/cmCPackWIXGenerator.cxx
index 5fdbeab..e71a38f 100644
--- a/Source/CPack/WiX/cmCPackWIXGenerator.cxx
+++ b/Source/CPack/WiX/cmCPackWIXGenerator.cxx
@@ -610,7 +610,7 @@ std::string cmCPackWIXGenerator::GetRootFolderId() const
bool cmCPackWIXGenerator::GenerateMainSourceFileFromTemplate()
{
- std::string wixTemplate = FindTemplate("Internal/CPack/WIX.template.in");
+ std::string wixTemplate = FindTemplate("WIX.template.in");
if (GetOption("CPACK_WIX_TEMPLATE") != 0) {
wixTemplate = GetOption("CPACK_WIX_TEMPLATE");
}
diff --git a/Source/CPack/cmCPackGenerator.cxx b/Source/CPack/cmCPackGenerator.cxx
index 7a6c50b..9530227 100644
--- a/Source/CPack/cmCPackGenerator.cxx
+++ b/Source/CPack/cmCPackGenerator.cxx
@@ -23,6 +23,7 @@
#include "cmState.h"
#include "cmStateSnapshot.h"
#include "cmStringAlgorithms.h"
+#include "cmSystemTools.h"
#include "cmVersion.h"
#include "cmWorkingDirectory.h"
#include "cmXMLSafe.h"
@@ -1254,7 +1255,17 @@ std::string cmCPackGenerator::FindTemplate(const char* name)
cmCPackLogger(cmCPackLog::LOG_DEBUG,
"Look for template: " << (name ? name : "(NULL)")
<< std::endl);
+ // Search CMAKE_MODULE_PATH for a custom template.
std::string ffile = this->MakefileMap->GetModulesFile(name);
+ if (ffile.empty()) {
+ // Fall back to our internal builtin default.
+ ffile = cmStrCat(cmSystemTools::GetCMakeRoot(), "/Modules/Internal/CPack/",
+ name);
+ cmSystemTools::ConvertToUnixSlashes(ffile);
+ if (!cmSystemTools::FileExists(ffile)) {
+ ffile.clear();
+ }
+ }
cmCPackLogger(cmCPackLog::LOG_DEBUG,
"Found template: " << ffile << std::endl);
return ffile;
diff --git a/Source/CPack/cmCPackNSISGenerator.cxx b/Source/CPack/cmCPackNSISGenerator.cxx
index adea8ec..9bf72df 100644
--- a/Source/CPack/cmCPackNSISGenerator.cxx
+++ b/Source/CPack/cmCPackNSISGenerator.cxx
@@ -39,8 +39,7 @@ int cmCPackNSISGenerator::PackageFiles()
{
// TODO: Fix nsis to force out file name
- std::string nsisInFileName =
- this->FindTemplate("Internal/CPack/NSIS.template.in");
+ std::string nsisInFileName = this->FindTemplate("NSIS.template.in");
if (nsisInFileName.empty()) {
cmCPackLogger(cmCPackLog::LOG_ERROR,
"CPack error: Could not find NSIS installer template file."
@@ -48,7 +47,7 @@ int cmCPackNSISGenerator::PackageFiles()
return false;
}
std::string nsisInInstallOptions =
- this->FindTemplate("Internal/CPack/NSIS.InstallOptions.ini.in");
+ this->FindTemplate("NSIS.InstallOptions.ini.in");
if (nsisInInstallOptions.empty()) {
cmCPackLogger(cmCPackLog::LOG_ERROR,
"CPack error: Could not find NSIS installer options file."
diff --git a/Source/CPack/cmCPackOSXX11Generator.cxx b/Source/CPack/cmCPackOSXX11Generator.cxx
index cd65694..951c65f 100644
--- a/Source/CPack/cmCPackOSXX11Generator.cxx
+++ b/Source/CPack/cmCPackOSXX11Generator.cxx
@@ -240,7 +240,7 @@ bool cmCPackOSXX11Generator::CopyResourcePlistFile(
const std::string& name, const std::string& dir,
const char* outputFileName /* = 0 */, bool copyOnly /* = false */)
{
- std::string inFName = cmStrCat("Internal/CPack/CPack.", name, ".in");
+ std::string inFName = cmStrCat("CPack.", name, ".in");
std::string inFileName = this->FindTemplate(inFName.c_str());
if (inFileName.empty()) {
cmCPackLogger(cmCPackLog::LOG_ERROR,
diff --git a/Source/CPack/cmCPackPKGGenerator.cxx b/Source/CPack/cmCPackPKGGenerator.cxx
index 3e1a51b..dae5ec9 100644
--- a/Source/CPack/cmCPackPKGGenerator.cxx
+++ b/Source/CPack/cmCPackPKGGenerator.cxx
@@ -49,7 +49,7 @@ std::string cmCPackPKGGenerator::GetPackageName(
void cmCPackPKGGenerator::WriteDistributionFile(const char* metapackageFile)
{
std::string distributionTemplate =
- this->FindTemplate("Internal/CPack/CPack.distribution.dist.in");
+ this->FindTemplate("CPack.distribution.dist.in");
if (distributionTemplate.empty()) {
cmCPackLogger(cmCPackLog::LOG_ERROR,
"Cannot find input file: " << distributionTemplate
@@ -300,7 +300,7 @@ bool cmCPackPKGGenerator::CopyResourcePlistFile(const std::string& name,
outName = name.c_str();
}
- std::string inFName = cmStrCat("Internal/CPack/CPack.", name, ".in");
+ std::string inFName = cmStrCat("CPack.", name, ".in");
std::string inFileName = this->FindTemplate(inFName.c_str());
if (inFileName.empty()) {
cmCPackLogger(cmCPackLog::LOG_ERROR,
diff --git a/Source/CPack/cmCPackSTGZGenerator.cxx b/Source/CPack/cmCPackSTGZGenerator.cxx
index bb0ed4f..a4a5e6f 100644
--- a/Source/CPack/cmCPackSTGZGenerator.cxx
+++ b/Source/CPack/cmCPackSTGZGenerator.cxx
@@ -27,8 +27,7 @@ int cmCPackSTGZGenerator::InitializeInternal()
{
this->SetOptionIfNotSet("CPACK_INCLUDE_TOPLEVEL_DIRECTORY", "0");
- std::string inFile =
- this->FindTemplate("Internal/CPack/CPack.STGZ_Header.sh.in");
+ std::string inFile = this->FindTemplate("CPack.STGZ_Header.sh.in");
if (inFile.empty()) {
cmCPackLogger(cmCPackLog::LOG_ERROR,
"Cannot find template file: " << inFile << std::endl);
diff --git a/Source/cmGeneratorTarget.cxx b/Source/cmGeneratorTarget.cxx
index d38e9e1..171c3ed 100644
--- a/Source/cmGeneratorTarget.cxx
+++ b/Source/cmGeneratorTarget.cxx
@@ -3344,19 +3344,20 @@ std::string cmGeneratorTarget::GetPchHeader(const std::string& config,
if (this->GetPropertyAsBool("DISABLE_PRECOMPILE_HEADERS")) {
return std::string();
}
+ const cmGeneratorTarget* generatorTarget = this;
+ const char* pchReuseFrom =
+ generatorTarget->GetProperty("PRECOMPILE_HEADERS_REUSE_FROM");
+
const auto inserted =
this->PchHeaders.insert(std::make_pair(language + config, ""));
if (inserted.second) {
const std::vector<BT<std::string>> headers =
this->GetPrecompileHeaders(config, language);
- if (headers.empty()) {
+ if (headers.empty() && !pchReuseFrom) {
return std::string();
}
std::string& filename = inserted.first->second;
- const cmGeneratorTarget* generatorTarget = this;
- const char* pchReuseFrom =
- generatorTarget->GetProperty("PRECOMPILE_HEADERS_REUSE_FROM");
if (pchReuseFrom) {
generatorTarget =
this->GetGlobalGenerator()->FindGeneratorTarget(pchReuseFrom);
diff --git a/Source/cmTarget.cxx b/Source/cmTarget.cxx
index 99c16f2..2db89de 100644
--- a/Source/cmTarget.cxx
+++ b/Source/cmTarget.cxx
@@ -1288,11 +1288,8 @@ void cmTarget::SetProperty(const std::string& prop, const char* value)
reusedTarget->SetProperty("COMPILE_PDB_OUTPUT_DIRECTORY",
cmStrCat(reusedFrom, ".dir/").c_str());
- for (auto p : { "COMPILE_PDB_NAME", "PRECOMPILE_HEADERS",
- "INTERFACE_PRECOMPILE_HEADERS" }) {
- this->SetProperty(p, reusedTarget->GetProperty(p));
- }
-
+ this->SetProperty("COMPILE_PDB_NAME",
+ reusedTarget->GetProperty("COMPILE_PDB_NAME"));
this->AddUtility(reusedFrom, impl->Makefile);
} else {
impl->Properties.SetProperty(prop, value);
diff --git a/Tests/RunCMake/PrecompileHeaders/PchReuseFrom.cmake b/Tests/RunCMake/PrecompileHeaders/PchReuseFrom.cmake
index 4502456..03a97ed 100644
--- a/Tests/RunCMake/PrecompileHeaders/PchReuseFrom.cmake
+++ b/Tests/RunCMake/PrecompileHeaders/PchReuseFrom.cmake
@@ -1,8 +1,12 @@
cmake_minimum_required(VERSION 3.15)
project(PchReuseFrom C)
+if(CMAKE_C_COMPILE_OPTIONS_USE_PCH)
+ add_definitions(-DHAVE_PCH_SUPPORT)
+endif()
+
add_library(empty empty.c)
-target_precompile_headers(empty PUBLIC
+target_precompile_headers(empty PRIVATE
<stdio.h>
<string.h>
)
@@ -12,6 +16,9 @@ add_library(foo foo.c)
target_include_directories(foo PUBLIC include)
target_precompile_headers(foo REUSE_FROM empty)
+# should not cause problems if configured multiple times
+target_precompile_headers(foo REUSE_FROM empty)
+
add_executable(foobar foobar.c)
target_link_libraries(foobar foo )
set_target_properties(foobar PROPERTIES PRECOMPILE_HEADERS_REUSE_FROM foo)
diff --git a/Tests/RunCMake/PrecompileHeaders/foobar.c b/Tests/RunCMake/PrecompileHeaders/foobar.c
index 7a135ea..97d465c 100644
--- a/Tests/RunCMake/PrecompileHeaders/foobar.c
+++ b/Tests/RunCMake/PrecompileHeaders/foobar.c
@@ -4,5 +4,11 @@
int main()
{
- return foo() + foo2() + bar();
+ int zeroSize = 0;
+
+#ifdef HAVE_PCH_SUPPORT
+ zeroSize = (int)strlen("");
+#endif
+
+ return foo() + foo2() + bar() + zeroSize;
}