Swift/Ninja: Emit modules separately from compilation

Add policy CMP0215 to emit `.swiftmodule` from a dedicated `-emit-module`
build edge for importable Swift targets in the Ninja generator.
Downstream targets can begin compiling as soon as the module interface
is ready, overlapping with upstream object compilation and linking.

When CMP0215 is NEW and CMP0157 is NEW, separate module emission is
enabled by default.  The `Swift_SEPARATE_MODULE_EMISSION` target
property (initialized by the `CMAKE_Swift_SEPARATE_MODULE_EMISSION`
variable) can override this per target.

The compile edge still passes `-emit-module` so that swiftc continues
to produce `.swiftdoc`; only the `.swiftmodule` output moves to the new
edge.

Also fix swiftmodule dependency edges in cmNinjaNormalTargetGenerator
to use IsLanguageUsed instead of GetLinkerLanguage, so C/C++ targets
whose linker language propagated to Swift do not produce spurious
implicit dependencies.

Closes: #27748
This commit is contained in:
Roman Lavrov
2026-04-24 15:58:18 -04:00
committed by Brad King
parent 0f20ba5b68
commit b6367f723b
21 changed files with 308 additions and 35 deletions
@@ -0,0 +1,24 @@
CMAKE_Swift_SEPARATE_MODULE_EMISSION
-------------------------------------
.. versionadded:: 4.4
Specify whether Swift targets emit ``.swiftmodule`` from a dedicated build
edge. This variable is used to initialize the
:prop_tgt:`Swift_SEPARATE_MODULE_EMISSION` property on targets as they are
created.
When set to ``ON``, importable Swift targets built by :ref:`Ninja Generators`
produce the ``.swiftmodule`` from a separate ``-emit-module`` build edge,
allowing downstream Swift targets to compile in parallel with upstream
object compilation.
If this variable is not set, separate module emission is enabled by default
in targets that do not set :prop_tgt:`Swift_SEPARATE_MODULE_EMISSION`.
See policy :policy:`CMP0215`.
.. note::
Separate module emission takes effect only when policy :policy:`CMP0157`
is set to ``NEW`` prior to the first :command:`project` or
:command:`enable_language` command that enables the Swift language.