Swift: Build with a package name by default

Add policy CMP0216 to enable use of the `PROJECT_NAME` as the default
package name for Swift.

Issue: #26886
This commit is contained in:
Evan Wilde
2026-04-28 15:30:44 -04:00
committed by Brad King
parent 94faee3099
commit 118ab41984
15 changed files with 103 additions and 2 deletions
+1
View File
@@ -100,6 +100,7 @@ Policies Introduced by CMake 4.4
.. toctree::
:maxdepth: 1
CMP0216: Swift targets have a default project name. </policy/CMP0216>
CMP0215: Ninja generators emit Swift modules separately from compilation. </policy/CMP0215>
CMP0214: Honor CMAKE_EXE_LINKER_FLAGS for Swift executable targets. </policy/CMP0214>
CMP0213: file(ARCHIVE_{CREATE,EXTRACT}) encode archive paths as UTF-8 by default. </policy/CMP0213>
+32
View File
@@ -0,0 +1,32 @@
CMP0216
-------
.. versionadded:: 4.4
Swift targets have a default project name.
CMake 4.3 and below did not compile Swift targets with ``-package-name``.
The package name is useful because Swift package access control makes types,
functions, and properties visible to other libraries within the same package,
but not from sources outside of the package. CMake 4.4 and above prefer to
pass ``-package-name`` by default using the :variable:`PROJECT_NAME` variable
value. This policy provides compatibility for projects that have not been
updated to expect a package name.
.. note::
Package access control was added in Swift 5.8 and Xcode 15. This policy has
no effect when using an earlier version of Swift or Xcode.
The ``OLD`` behavior for this policy is to build Swift source files without
passing a package-name. The ``NEW`` behavior for this policy is to build
Swift source files with a default package name from :variable:`PROJECT_NAME`.
Regardless of whether this policy is set, the :prop_tgt:`Swift_PACKAGE_NAME`
target property may be set to explicitly specify or suppress a package name.
.. |INTRODUCED_IN_CMAKE_VERSION| replace:: 4.4
.. |WARNS_OR_DOES_NOT_WARN| replace:: does *not* warn
.. include:: include/STANDARD_ADVICE.rst
.. include:: include/DEPRECATED.rst
+3
View File
@@ -14,5 +14,8 @@ code.
Package access control was added in Swift 5.8 and Xcode 15. This target
property has no effect when using an earlier version of Swift or Xcode.
If the property is not set, the :variable:`PROJECT_NAME` variable is used
as the default package name. See policy :policy:`CMP0216`.
Setting this property to the empty string will result in the target being
compiled without a package name.
+3
View File
@@ -1,5 +1,8 @@
swift-package-name
------------------
* Swift targets now have a package name by default.
See policy :policy:`CMP0216`.
* The :prop_tgt:`Swift_PACKAGE_NAME` target property was added to
specify a Swift package name.
+2
View File
@@ -6204,6 +6204,8 @@ std::string cmGeneratorTarget::GetSwiftPackageName() const
std::string packageName;
if (cmValue projectName = this->GetProperty("Swift_PACKAGE_NAME")) {
packageName = *projectName;
} else if (this->GetPolicyStatusCMP0216() == cmPolicies::NEW) {
packageName = this->Makefile->GetSafeDefinition("PROJECT_NAME");
}
return packageName;
}
+5 -2
View File
@@ -645,7 +645,9 @@ class cmMakefile;
0, WARN) \
SELECT(POLICY, CMP0215, \
"Ninja generators emit Swift modules separately from compilation.", \
4, 4, 0, WARN)
4, 4, 0, WARN) \
SELECT(POLICY, CMP0216, "Swift targets have a default project name.", 4, 4, \
0, WARN)
#define CM_SELECT_ID(F, A1, A2, A3, A4, A5, A6) F(A1)
#define CM_FOR_EACH_POLICY_ID(POLICY) \
@@ -703,7 +705,8 @@ class cmMakefile;
F(CMP0210) \
F(CMP0211) \
F(CMP0214) \
F(CMP0215)
F(CMP0215) \
F(CMP0216)
#define CM_FOR_EACH_CUSTOM_COMMAND_POLICY(F) \
F(CMP0116) \
@@ -0,0 +1,21 @@
if(RunCMake_GENERATOR MATCHES "Ninja")
if(RunCMake_GENERATOR_IS_MULTI_CONFIG)
set(path "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/impl-Debug.ninja")
else()
set(path "${RunCMake_TEST_BINARY_DIR}/build.ninja")
endif()
file(READ "${path}" build_ninja)
if(NOT build_ninja MATCHES "-package-name")
set(RunCMake_TEST_FAILED
"Expected -package-name in generated Ninja build files when CMP0216 is NEW")
endif()
elseif(RunCMake_GENERATOR MATCHES "Xcode")
file(READ
"${RunCMake_TEST_BINARY_DIR}/CMP0216-NEW.xcodeproj/project.pbxproj"
pbxproj)
if(NOT pbxproj MATCHES "SWIFT_PACKAGE_NAME")
set(RunCMake_TEST_FAILED
"Expected SWIFT_PACKAGE_NAME in generated Xcode project when CMP0216 is NEW")
endif()
endif()
+4
View File
@@ -0,0 +1,4 @@
cmake_minimum_required(VERSION 4.0)
cmake_policy(SET CMP0216 NEW)
include(CMP0216-common.cmake)
@@ -0,0 +1,20 @@
if(RunCMake_GENERATOR MATCHES "Ninja")
if(RunCMake_GENERATOR_IS_MULTI_CONFIG)
set(path "${RunCMake_TEST_BINARY_DIR}/CMakeFiles/impl-Debug.ninja")
else()
set(path "${RunCMake_TEST_BINARY_DIR}/build.ninja")
endif()
file(READ "${path}" build_ninja)
if(_content MATCHES "-package-name")
set(RunCMake_TEST_FAILED
"Unexpected -package-name in generated Ninja build files when CMP0216 is OLD")
endif()
elseif(RunCMake_GENERATOR MATCHES "Xcode")
file(READ
"${RunCMake_TEST_BINARY_DIR}/CMP0216-OLD.xcodeproj/project.pbxproj"
pbxproj)
if(pbxproj MATCHES "SWIFT_PACKAGE_NAME")
set(RunCMake_TEST_FAILED
"Unexpected SWIFT_PACKAGE_NAME in generated Xcode project when CMP0216 is OLD")
endif()
endif()
+4
View File
@@ -0,0 +1,4 @@
cmake_minimum_required(VERSION 4.0)
cmake_policy(SET CMP0216 OLD)
include(CMP0216-common.cmake)
@@ -0,0 +1,3 @@
enable_language(Swift)
add_library(L STATIC L.swift)
@@ -4,6 +4,7 @@ endif()
if(POLICY CMP0195)
cmake_policy(SET CMP0195 NEW)
endif()
cmake_policy(SET CMP0216 NEW)
set(CMAKE_Swift_COMPILATION_MODE "singlefile")
@@ -1,5 +1,6 @@
cmake_policy(SET CMP0157 NEW)
cmake_policy(SET CMP0195 NEW)
cmake_policy(SET CMP0216 NEW)
enable_language(Swift C)
+2
View File
@@ -186,4 +186,6 @@ endif()
if(NOT RunCMake_GENERATOR STREQUAL "Xcode" OR
(RunCMake_GENERATOR STREQUAL "Xcode" AND XCODE_VERSION VERSION_GREATER_EQUAL 15.0))
run_cmake(PackageName)
run_cmake(CMP0216-OLD)
run_cmake(CMP0216-NEW)
endif()
@@ -57,6 +57,7 @@
\* CMP0211
\* CMP0214
\* CMP0215
\* CMP0216
Call Stack \(most recent call first\):
CMakeLists\.txt:3 \(include\)