From dae9a808fe0cb039298fab75459e3803403c5194 Mon Sep 17 00:00:00 2001 From: Craig Scott Date: Wed, 13 Nov 2019 23:17:56 +1100 Subject: Help: Reorganise target_precompile_headers() docs for readability Incorporates clarification of behavior when trying to specify REUSE_FROM on a target that already has its own precompile headers set. Fixes: #19970 --- Help/command/target_precompile_headers.rst | 108 ++++++++++++++++------------- 1 file changed, 59 insertions(+), 49 deletions(-) diff --git a/Help/command/target_precompile_headers.rst b/Help/command/target_precompile_headers.rst index 5ab3766..1be453d 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( [header1...] [ [header2...] ...]) - target_precompile_headers( REUSE_FROM ) - -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 ```` value. -The ```` will become a dependency of ````. - -.. note:: - - The second signature will require the same set of compiler options, - compiler flags, compiler definitions for both ````, and - ````. 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 ````. The named ```` must have been created by a command such as :command:`add_executable` or :command:`add_library` and must not be an :ref:`ALIAS target `. @@ -38,25 +26,36 @@ 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 ````. ``PUBLIC`` and ``INTERFACE`` items will populate the -:prop_tgt:`INTERFACE_PRECOMPILE_HEADERS` property of ````. -(:ref:`IMPORTED targets ` only support ``INTERFACE`` items.) -Repeated calls for the same ```` append items in the order called. +:prop_tgt:`INTERFACE_PRECOMPILE_HEADERS` property of ```` +(:ref:`IMPORTED targets ` only support ``INTERFACE`` items). +Repeated calls for the same ```` will append items in the order called. + +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. ````) 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 ``$`` 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_precompile_headers(myTarget PUBLIC project_header.h "$<$:cxx_only.h>" @@ -65,20 +64,6 @@ Usage ) -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. ````) 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 `, be sure to encode the closing ``>`` as ``$``. For example: @@ -88,13 +73,38 @@ as ``$``. For example: target_precompile_headers(mylib PRIVATE "$<$:>" "$<$:>" - ) + ) + + +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( REUSE_FROM ) + +This form sets the :prop_tgt:`PRECOMPILE_HEADERS_REUSE_FROM` property to +```` and adds a dependency such that ```` will depend +on ````. CMake will halt with an error if the +:prop_tgt:`PRECOMPILE_HEADERS` property of ```` 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 ```` and + ````. 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. -- cgit v0.12 From cc88ede7a37f8180f670c0d6036ba40cf005c7b9 Mon Sep 17 00:00:00 2001 From: Craig Scott Date: Wed, 13 Nov 2019 23:25:15 +1100 Subject: Help: Provide guidance on INTERFACE for target_precompile_headers() Fixes: #19953 --- Help/command/target_precompile_headers.rst | 14 ++++++++++++++ Help/prop_tgt/INTERFACE_PRECOMPILE_HEADERS.rst | 2 ++ 2 files changed, 16 insertions(+) diff --git a/Help/command/target_precompile_headers.rst b/Help/command/target_precompile_headers.rst index 1be453d..569c7eb 100644 --- a/Help/command/target_precompile_headers.rst +++ b/Help/command/target_precompile_headers.rst @@ -30,6 +30,20 @@ items will populate the :prop_tgt:`PRECOMPILE_HEADERS` property of (:ref:`IMPORTED targets ` only support ``INTERFACE`` items). Repeated calls for the same ```` will append items in the order called. +Projects should generally avoid using ``PUBLIC`` or ``INTERFACE`` for targets +that will be :ref:`exported `, or they should at least use +the ``$`` 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 ` 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 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)` -- cgit v0.12