From 6063428de7707f68b14d74d93d5440c61cc50cf4 Mon Sep 17 00:00:00 2001 From: Evan Wilde Date: Mon, 3 Oct 2022 13:21:11 -0700 Subject: Swift: Update default build flags Fully-optimized builds should be using whole-module optimizations(WMO) to get all the optimizations the compiler can do for a given module. As such, it makes sense for the release builds to pass `-whole-module-optimization` or `-wmo` to the compiler by default. `-whole-module-optimization` and `-wmo` are aliased and have the same impact on the build. Removing `-incrementa' from the `CMAKE_Swift_CREATE_*` variable: WMO is incompatible with incremental builds, so it is removed to avoid warnings from the Swift compiler. Pass `-num-threads` to the driver in `CMAKE_Swift_CREATE_*`: WMO doesn't use the `-j` flag, but instead uses `-num-threads` to get parallelism. The two flags are applied in mutually exclusive contexts, so `-j N` is a no-op in WMO, while `-num-threads` is a no-op in other modes. Passing both at the same time will catch both cases without negatively impacting the other case. --- Modules/CMakeSwiftInformation.cmake | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/Modules/CMakeSwiftInformation.cmake b/Modules/CMakeSwiftInformation.cmake index 16726d2..a0cd9da 100644 --- a/Modules/CMakeSwiftInformation.cmake +++ b/Modules/CMakeSwiftInformation.cmake @@ -65,10 +65,22 @@ set(CMAKE_Swift_COMPILE_OPTIONS_MSVC_RUNTIME_LIBRARY_MultiThreadedDLL -libc MD) set(CMAKE_Swift_COMPILE_OPTIONS_MSVC_RUNTIME_LIBRARY_MultiThreadedDebug -libc MTd) set(CMAKE_Swift_COMPILE_OPTIONS_MSVC_RUNTIME_LIBRARY_MultiThreadedDebugDLL -libc MDd) -set(CMAKE_Swift_FLAGS_DEBUG_INIT "-Onone -g") -set(CMAKE_Swift_FLAGS_RELEASE_INIT "-O") -set(CMAKE_Swift_FLAGS_RELWITHDEBINFO_INIT "-O -g") -set(CMAKE_Swift_FLAGS_MINSIZEREL_INIT "-Osize") +if(CMAKE_GENERATOR STREQUAL "Xcode") + # Xcode has a separate Xcode project option (SWIFT_COMPILATION_MODE) used to set + # whether compiling with whole-module optimizations or incrementally. Setting + # these options here will have no effect when compiling with the built-in driver, + # and will explode violently, leaving build products in the source directory, when + # using the old swift driver. + set(CMAKE_Swift_FLAGS_DEBUG_INIT "-Onone -g") + set(CMAKE_Swift_FLAGS_RELEASE_INIT "-O") + set(CMAKE_Swift_FLAGS_RELWITHDEBINFO_INIT "-O -g") + set(CMAKE_Swift_FLAGS_MINSIZEREL_INIT "-Osize") +else() + set(CMAKE_Swift_FLAGS_DEBUG_INIT "-Onone -g -incremental") + set(CMAKE_Swift_FLAGS_RELEASE_INIT "-O -wmo") + set(CMAKE_Swift_FLAGS_RELWITHDEBINFO_INIT "-O -g -wmo") + set(CMAKE_Swift_FLAGS_MINSIZEREL_INIT "-Osize -wmo") +endif() if(CMAKE_EXECUTABLE_FORMAT STREQUAL "ELF") if(NOT DEFINED CMAKE_Swift_LINK_WHAT_YOU_USE_FLAG) @@ -91,7 +103,7 @@ if(NOT CMAKE_Swift_NUM_THREADS MATCHES "^[0-9]+$") endif() if(NOT CMAKE_Swift_CREATE_SHARED_LIBRARY) - set(CMAKE_Swift_CREATE_SHARED_LIBRARY " -output-file-map -incremental -j ${CMAKE_Swift_NUM_THREADS} -emit-library -o -module-name -module-link-name -emit-module -emit-module-path -emit-dependencies ${CMAKE_Swift_IMPLIB_LINKER_FLAGS} ") + set(CMAKE_Swift_CREATE_SHARED_LIBRARY " -output-file-map -j ${CMAKE_Swift_NUM_THREADS} -num-threads ${CMAKE_Swift_NUM_THREADS} -emit-library -o -module-name -module-link-name -emit-module -emit-module-path -emit-dependencies ${CMAKE_Swift_IMPLIB_LINKER_FLAGS} ") endif() if(NOT CMAKE_Swift_CREATE_SHARED_MODULE) @@ -99,11 +111,11 @@ if(NOT CMAKE_Swift_CREATE_SHARED_MODULE) endif() if(NOT CMAKE_Swift_LINK_EXECUTABLE) - set(CMAKE_Swift_LINK_EXECUTABLE " -output-file-map -incremental -j ${CMAKE_Swift_NUM_THREADS} -emit-executable -o -emit-dependencies ") + set(CMAKE_Swift_LINK_EXECUTABLE " -output-file-map -j ${CMAKE_Swift_NUM_THREADS} -num-threads ${CMAKE_Swift_NUM_THREADS} -emit-executable -o -emit-dependencies ") endif() if(NOT CMAKE_Swift_CREATE_STATIC_LIBRARY) - set(CMAKE_Swift_CREATE_STATIC_LIBRARY " -output-file-map -incremental -j ${CMAKE_Swift_NUM_THREADS} -emit-library -static -o -module-name -module-link-name -emit-module -emit-module-path -emit-dependencies ") + set(CMAKE_Swift_CREATE_STATIC_LIBRARY " -output-file-map -j ${CMAKE_Swift_NUM_THREADS} -num-threads ${CMAKE_Swift_NUM_THREADS} -emit-library -static -o -module-name -module-link-name -emit-module -emit-module-path -emit-dependencies ") set(CMAKE_Swift_ARCHIVE_CREATE " crs ") set(CMAKE_Swift_ARCHIVE_FINISH "") -- cgit v0.12 From 238e0f25b273420efe8a49292c0f4f267f359db2 Mon Sep 17 00:00:00 2001 From: Evan Wilde Date: Tue, 11 Oct 2022 09:56:24 -0700 Subject: Xcode: Set WMO on non-debug build configurations This patch sets the swift compilation mode to wholemodule in the xcode generator for all non-debug build configurations at the project level. Leaving the target build-configurations default ensures that this default can be overridden easily by editing either the project configuration from the build settings editor or the per-target configuration. When set on versions of Xcode that do not recognize the `SWIFT_COMPILATION_MODE` setting, Xcode will include the option as a user-defined setting, but otherwise ignore it. --- Source/cmGlobalXCodeGenerator.cxx | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Source/cmGlobalXCodeGenerator.cxx b/Source/cmGlobalXCodeGenerator.cxx index 6b867d4..c28b22b 100644 --- a/Source/cmGlobalXCodeGenerator.cxx +++ b/Source/cmGlobalXCodeGenerator.cxx @@ -4378,12 +4378,20 @@ bool cmGlobalXCodeGenerator::CreateXCodeObjects( buildSettings->AddAttribute("CODE_SIGNING_ALLOWED", this->CreateString("NO")); } + auto debugConfigs = this->GetCMakeInstance()->GetDebugConfigs(); + std::set debugConfigSet(debugConfigs.begin(), + debugConfigs.end()); for (auto& config : configs) { CreateGlobalXCConfigSettings(root, config.second, config.first); cmXCodeObject* buildSettingsForCfg = this->CreateFlatClone(buildSettings); + if (debugConfigSet.count(cmSystemTools::UpperCase(config.first)) == 0) { + buildSettingsForCfg->AddAttribute("SWIFT_COMPILATION_MODE", + this->CreateString("wholemodule")); + } + // Put this last so it can override existing settings // Convert "CMAKE_XCODE_ATTRIBUTE_*" variables directly. for (const auto& var : this->CurrentMakefile->GetDefinitions()) { -- cgit v0.12