Swift: First-class support for modeling Swift compilation modes
Swift has different compilation modes that affect how the compiler behaves, and can impact the generated build graph. Under normal compilation, the driver will emit a single object file for each source file in the module. Whole-Module-Optimization (WMO) mode results in only a single object file for the entire module, changing the behavior of the build graph.
Currently, folks must force the -wmo
/-whole-module-optimization
flag through the various channels of smashing flags to the compiler, but this only really works with the Ninja generator at this time. The Xcode project format requires that wholemodule optimizations be specified via setting the SWIFT_COMPILATION_MODE
attribute to wholemodule
so that it can model the difference in behavior. Passing it via "other flags" to the Swift compiler will not work, so flag-smashing the compilation mode is not portable across generators.
At the moment, CMake sets WMO on non-debug builds on both the ninja and Xcode generator. The Xcode generator injects the attribute directly, while the ninja generator picks it up from CMAKE_Swift_FLAGS_{appropriate config names}_INIT
. We should instead migrate at least WMO to a target property (potentially with some CMAKE_Swift_COMPILATION_MODE
default cmake variable for folks who want a consistent mode applied wholesale when targets are created) that then can be checked inside of CMake more easily than trying to detect the flags at generation-time.
Currently, Xcode has wholemodule
and incremental
compilation modes reflected in the UI, though because Xcode only supports whole-module compilation and incremental compilation, and not single-file compilation, incremental
is the default so there is no affect in the Xcode project file. The Ninja generator can represent all three states though. Do we want to represent enabling WMO as a boolean, or make it a string that takes wholemodule
, incremental
, and to represent each state? Given that CMake is already emitting the OFM for incremental builds, there isn't a terribly big difference between incremental and single-file, though someone may find a reason to need single-file.