summaryrefslogtreecommitdiffstats
path: root/Help/prop_tgt
diff options
context:
space:
mode:
authorCraig Scott <craig.scott@crascit.com>2020-12-16 12:40:31 (GMT)
committerKitware Robot <kwrobot@kitware.com>2020-12-16 12:40:41 (GMT)
commitd2456b29f44fe612dcfbb0f5b8c5aa8eec67c2c7 (patch)
tree2cb4cb67c82bf90e335a241140cd180a51162fa7 /Help/prop_tgt
parent34469a4f71c523488682661aafc8d13b054ed5b9 (diff)
parent0fe9c40494be0e15e6603a245f181bc74d8cf481 (diff)
downloadCMake-d2456b29f44fe612dcfbb0f5b8c5aa8eec67c2c7.zip
CMake-d2456b29f44fe612dcfbb0f5b8c5aa8eec67c2c7.tar.gz
CMake-d2456b29f44fe612dcfbb0f5b8c5aa8eec67c2c7.tar.bz2
Merge topic 'unity-anon-ns'
0fe9c40494 Unity Build: Add option for generating per-file unique id Acked-by: Kitware Robot <kwrobot@kitware.com> Merge-request: !4784
Diffstat (limited to 'Help/prop_tgt')
-rw-r--r--Help/prop_tgt/UNITY_BUILD.rst5
-rw-r--r--Help/prop_tgt/UNITY_BUILD_UNIQUE_ID.rst53
2 files changed, 58 insertions, 0 deletions
diff --git a/Help/prop_tgt/UNITY_BUILD.rst b/Help/prop_tgt/UNITY_BUILD.rst
index 04cede6..f827a20 100644
--- a/Help/prop_tgt/UNITY_BUILD.rst
+++ b/Help/prop_tgt/UNITY_BUILD.rst
@@ -70,6 +70,11 @@ a number of measures to help address such problems:
problems with specific files than disabling unity builds for an entire
target.
+* Projects can set :prop_tgt:`UNITY_BUILD_UNIQUE_ID` to cause a valid
+ C-identifier to be generated which is unique per file in a unity
+ build. This can be used to avoid problems with anonymous namespaces
+ in unity builds.
+
* The :prop_tgt:`UNITY_BUILD_CODE_BEFORE_INCLUDE` and
:prop_tgt:`UNITY_BUILD_CODE_AFTER_INCLUDE` target properties can be used
to inject code into the unity source files before and after every
diff --git a/Help/prop_tgt/UNITY_BUILD_UNIQUE_ID.rst b/Help/prop_tgt/UNITY_BUILD_UNIQUE_ID.rst
new file mode 100644
index 0000000..e0ee30e
--- /dev/null
+++ b/Help/prop_tgt/UNITY_BUILD_UNIQUE_ID.rst
@@ -0,0 +1,53 @@
+UNITY_BUILD_UNIQUE_ID
+---------------------
+
+The name of a valid C-identifier which is set to a unique per-file
+value during unity builds.
+
+When this property is populated and when :prop_tgt:`UNITY_BUILD`
+is true, the property value is used to define a compiler definition
+of the specified name. The value of the defined symbol is unspecified,
+but it is unique per file path.
+
+Given:
+
+.. code-block:: cmake
+
+ set_target_properties(myTarget PROPERTIES
+ UNITY_BUILD "ON"
+ UNITY_BUILD_UNIQUE_ID "MY_UNITY_ID"
+ )
+
+the ``MY_UNITY_ID`` symbol is defined to a unique per-file value.
+
+One known use case for this identifier is to disambiguate the
+variables in an anonymous namespace in a limited scope.
+Anonymous namespaces present a problem for unity builds because
+they are used to ensure that certain variables and declarations
+are scoped to a translation unit which is approximated by a
+single source file. When source files are combined in a unity
+build file, those variables in different files are combined in
+a single translation unit and the names clash. This property can
+be used to avoid that with code like the following:
+
+.. code-block:: cpp
+
+ // Needed for when unity builds are disabled
+ #ifndef MY_UNITY_ID
+ #define MY_UNITY_ID
+ #endif
+
+ namespace { namespace MY_UNITY_ID {
+ // The name 'i' clashes (or could clash) with other
+ // variables in other anonymous namespaces
+ int i = 42;
+ }}
+
+ int use_var()
+ {
+ return MY_UNITY_ID::i;
+ }
+
+The pseudononymous namespace is used within a truly anonymous namespace.
+On many platforms, this maintains the invariant that the symbols within
+do not get external linkage when performing a unity build.