CMP0157 — CMake 4.3.0 Documentation

# CMP0157

Added in version 3.29.

Swift compilation mode is selected by an abstraction.

The Swift compiler can compile modules in different modes. The desired build mode depends whether the developer is iterating and wants to incrementally make changes, or if they are building a release for distribution and want more optimizations applied to the resulting binary.

CMake versions 3.26 through 3.28 build Swift binaries with whole-module optimizations enabled when configured in a non-debug build type. For CMake versions earlier than 3.26, the developer needs to specify the necessary flag manually for the Ninja Generators, and cannot not specify whole-module optimizations to the `Xcode` generator.

CMake versions 3.29 and above prefer to set the compilation mode using the `Swift_COMPILATION_MODE` target property, which can be initialized by the `CMAKE_Swift_COMPILATION_MODE` variable.

This policy provides compatibility for projects that have not been updated. The policy setting takes effect as of the first `project()` or `enable_language()` command that enables the `Swift` language.

Note

Once the policy has taken effect at the top of a project, that choice must be used throughout the tree. In projects that have nested projects in subdirectories, be sure to convert everything together.

The `OLD` behavior for this policy builds all Swift targets in `wholemodule` mode for non-debug configurations. Ninja Generators prepend the `-wmo` flag to the default set of Swift flags. The `Xcode` generator sets the `SWIFT_COMPILATION_MODE` attribute to `wholemodule` in the generated Xcode project file.

The `NEW` behavior for this policy is to apply the compilation mode specified in the `Swift_COMPILATION_MODE` target property, initialized as each target is created by the `CMAKE_Swift_COMPILATION_MODE` variable.

This policy was introduced in CMake version 3.29. It may be set by `cmake_policy()` or `cmake_minimum_required()`. If it is not set, CMake does not warn, and uses `OLD` behavior.

Note

The `OLD` behavior of a policy is deprecated by definition and may be removed in a future version of CMake.