Skip to content

Commit 0a2ac74

Browse files
authored
[5.5] Build PackageDescription and PackagePlugin universal on macOS platform (#3536)
* [5.5] Bootstrap script needs to build PackageDescription and PackagePlugin libraries universal when cross-compiling. Also unify and clean up some of the logic by making the helper function that installs libSwiftPM be more generic, and also apply to PackageDescription and PackagePlugin. rdar://75186958 (cherry picked from commit 637fa7a) * [5.5] Only pass `-enable-library-evolution` for PackageDescription and PackagePlugin on macOS Because PackageDescription unintentionally exports Foundation (for which a fix was attempted but then reverted after it broke some packages), we can only enable library evolution on Darwin platforms. rdar://78827075 (cherry picked from commit 6e7bb87) * [5.5] Enable swift module interfaces if the package author enables library evolution via unsafe flags There isn't currently a way for package authors to enable library evolution or module interfaces from the package manifest. They can pass `-enable-library-evolution` in their unsafe flags, but because `-emit-module-interface` requires a path parameter, it isn't something that can be set in the manifest. This adds a way to infer XCBuild settings based on values set in manifest-declared settings. The idea is to implement semantics appropriately for each platform based on generalized flags passed from the manifest. rdar://78773077 (cherry picked from commit 4569c7f) * Filter out `*.swiftmodule` and `Project` entries when installing binaries, since these should not be part of the installed toolchain. (cherry picked from commit 3741792)
1 parent 2ccaf04 commit 0a2ac74

File tree

12 files changed

+159
-68
lines changed

12 files changed

+159
-68
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// swift-tools-version:5.1
2+
import PackageDescription
3+
4+
let package = Package(
5+
name: "LibraryEvolution",
6+
products: [
7+
],
8+
targets: [
9+
.target(name: "A", dependencies: [], swiftSettings: [.unsafeFlags(["-enable-library-evolution"])]),
10+
.target(name: "B", dependencies: ["A"], swiftSettings: [.unsafeFlags(["-enable-library-evolution"])]),
11+
])

Fixtures/Miscellaneous/LibraryEvolution/Sources/A/A.swift

Whitespace-only changes.

Fixtures/Miscellaneous/LibraryEvolution/Sources/B/B.swift

Whitespace-only changes.

Package.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ let package = Package(
120120
name: "PackageDescription",
121121
swiftSettings: [
122122
.unsafeFlags(["-package-description-version", "999.0"]),
123-
.unsafeFlags(["-enable-library-evolution"])
123+
.unsafeFlags(["-enable-library-evolution"], .when(platforms: [.macOS]))
124124
]),
125125

126126
// The `PackagePlugin` target provides the API that is available to
@@ -130,7 +130,7 @@ let package = Package(
130130
name: "PackagePlugin",
131131
swiftSettings: [
132132
.unsafeFlags(["-package-description-version", "999.0"]),
133-
.unsafeFlags(["-enable-library-evolution"])
133+
.unsafeFlags(["-enable-library-evolution"], .when(platforms: [.macOS]))
134134
]),
135135

136136
// MARK: SwiftPM specific support libraries

Sources/Build/BuildPlan.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -783,14 +783,14 @@ public final class SwiftTargetBuildDescription {
783783
args += ["-color-diagnostics"]
784784
}
785785

786-
// Add the output for the `.swiftinterface`, if requested.
787-
if buildParameters.enableParseableModuleInterfaces {
788-
args += ["-emit-parseable-module-interface-path", parseableModuleInterfaceOutputPath.pathString]
789-
}
790-
791786
// Add agruments from declared build settings.
792787
args += self.buildSettingsFlags()
793788

789+
// Add the output for the `.swiftinterface`, if requested or if library evolution has been enabled some other way.
790+
if buildParameters.enableParseableModuleInterfaces || args.contains("-enable-library-evolution") {
791+
args += ["-emit-module-interface-path", parseableModuleInterfaceOutputPath.pathString]
792+
}
793+
794794
// User arguments (from -Xswiftc) should follow generated arguments to allow user overrides
795795
args += buildParameters.swiftCompilerFlags
796796
return args

Sources/PackageDescription/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ add_library(PackageDescription
2121

2222
target_compile_options(PackageDescription PUBLIC
2323
$<$<COMPILE_LANGUAGE:Swift>:-package-description-version$<SEMICOLON>999.0>)
24-
target_compile_options(PackageDescription PUBLIC
25-
$<$<COMPILE_LANGUAGE:Swift>:-enable-library-evolution>)
2624

2725
if(CMAKE_HOST_SYSTEM_NAME STREQUAL Darwin)
26+
target_compile_options(PackageDescription PUBLIC
27+
$<$<COMPILE_LANGUAGE:Swift>:-enable-library-evolution>)
2828
set(SWIFT_INTERFACE_PATH ${CMAKE_BINARY_DIR}/pm/ManifestAPI/PackageDescription.swiftinterface)
2929
target_compile_options(PackageDescription PUBLIC
3030
$<$<COMPILE_LANGUAGE:Swift>:-emit-module-interface-path$<SEMICOLON>${SWIFT_INTERFACE_PATH}>)

Sources/PackagePlugin/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ add_library(PackagePlugin
1717

1818
target_compile_options(PackagePlugin PUBLIC
1919
$<$<COMPILE_LANGUAGE:Swift>:-package-description-version$<SEMICOLON>999.0>)
20-
target_compile_options(PackagePlugin PUBLIC
21-
$<$<COMPILE_LANGUAGE:Swift>:-enable-library-evolution>)
2220

2321
if(CMAKE_HOST_SYSTEM_NAME STREQUAL Darwin)
22+
target_compile_options(PackagePlugin PUBLIC
23+
$<$<COMPILE_LANGUAGE:Swift>:-enable-library-evolution>)
2424
set(SWIFT_INTERFACE_PATH ${CMAKE_BINARY_DIR}/pm/PluginAPI/PackagePlugin.swiftinterface)
2525
target_compile_options(PackagePlugin PUBLIC
2626
$<$<COMPILE_LANGUAGE:Swift>:-emit-module-interface-path$<SEMICOLON>${SWIFT_INTERFACE_PATH}>)

Sources/XCBuildSupport/PIF.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -936,6 +936,7 @@ public enum PIF {
936936
case WATCHOS_DEPLOYMENT_TARGET
937937
case MARKETING_VERSION
938938
case CURRENT_PROJECT_VERSION
939+
case SWIFT_EMIT_MODULE_INTERFACE
939940
}
940941

941942
public enum MultipleValueSetting: String, Codable {

Sources/XCBuildSupport/PIFBuilder.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,21 @@ final class PackagePIFProjectBuilder: PIFProjectBuilder {
812812

813813
return bundleName
814814
}
815+
816+
// Add inferred build settings for a particular value for a manifest setting and value.
817+
private func addInferredBuildSettings(
818+
for setting: PIF.BuildSettings.MultipleValueSetting,
819+
value: [String],
820+
platform: PIF.BuildSettings.Platform? = nil,
821+
configuration: BuildConfiguration,
822+
settings: inout PIF.BuildSettings
823+
) {
824+
// Automatically set SWIFT_EMIT_MODULE_INTERFACE if the package author uses unsafe flags to enable
825+
// library evolution (this is needed until there is a way to specify this in the package manifest).
826+
if setting == .OTHER_SWIFT_FLAGS && value.contains("-enable-library-evolution") {
827+
settings[.SWIFT_EMIT_MODULE_INTERFACE] = "YES"
828+
}
829+
}
815830

816831
// Apply target-specific build settings defined in the manifest.
817832
private func addManifestBuildSettings(
@@ -833,8 +848,10 @@ final class PackagePIFProjectBuilder: PIFProjectBuilder {
833848
switch configuration {
834849
case .debug:
835850
debugSettings[setting, for: platform, default: ["$(inherited)"]] += value
851+
addInferredBuildSettings(for: setting, value: value, platform: platform, configuration: .debug, settings: &debugSettings)
836852
case .release:
837853
releaseSettings[setting, for: platform, default: ["$(inherited)"]] += value
854+
addInferredBuildSettings(for: setting, value: value, platform: platform, configuration: .release, settings: &releaseSettings)
838855
}
839856
}
840857

@@ -847,8 +864,10 @@ final class PackagePIFProjectBuilder: PIFProjectBuilder {
847864
switch configuration {
848865
case .debug:
849866
debugSettings[setting, default: ["$(inherited)"]] += value
867+
addInferredBuildSettings(for: setting, value: value, configuration: .debug, settings: &debugSettings)
850868
case .release:
851869
releaseSettings[setting, default: ["$(inherited)"]] += value
870+
addInferredBuildSettings(for: setting, value: value, configuration: .release, settings: &releaseSettings)
852871
}
853872
}
854873

Tests/CommandsTests/BuildToolTests.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,18 @@ final class BuildToolTests: XCTestCase {
253253
}
254254
}
255255

256+
func testAutomaticParseableInterfacesWithLibraryEvolution() {
257+
fixture(name: "Miscellaneous/LibraryEvolution") { path in
258+
do {
259+
let result = try build([], packagePath: path)
260+
XCTAssert(result.binContents.contains("A.swiftinterface"))
261+
XCTAssert(result.binContents.contains("B.swiftinterface"))
262+
} catch SwiftPMProductError.executionFailure(_, _, let stderr) {
263+
XCTFail(stderr)
264+
}
265+
}
266+
}
267+
256268
func testBuildCompleteMessage() {
257269
fixture(name: "DependencyResolution/Internal/Simple") { path in
258270
do {

0 commit comments

Comments
 (0)