|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2025 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See http://swift.org/LICENSE.txt for license information |
| 9 | +// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import Basics |
| 14 | +import Testing |
| 15 | +import PackageGraph |
| 16 | +import PackageLoading |
| 17 | +import PackageModel |
| 18 | +import SPMBuildCore |
| 19 | +import SwiftBuild |
| 20 | +import SwiftBuildSupport |
| 21 | +import _InternalTestSupport |
| 22 | +import Workspace |
| 23 | + |
| 24 | +extension PIFBuilderParameters { |
| 25 | + fileprivate static func constructDefaultParametersForTesting(temporaryDirectory: Basics.AbsolutePath) throws -> Self { |
| 26 | + self.init( |
| 27 | + isPackageAccessModifierSupported: true, |
| 28 | + enableTestability: false, |
| 29 | + shouldCreateDylibForDynamicProducts: false, |
| 30 | + toolchainLibDir: temporaryDirectory.appending(component: "toolchain-lib-dir"), |
| 31 | + pkgConfigDirectories: [], |
| 32 | + supportedSwiftVersions: [.v4, .v4_2, .v5, .v6], |
| 33 | + pluginScriptRunner: DefaultPluginScriptRunner( |
| 34 | + fileSystem: localFileSystem, |
| 35 | + cacheDir: temporaryDirectory.appending(component: "plugin-cache-dir"), |
| 36 | + toolchain: try UserToolchain.default |
| 37 | + ), |
| 38 | + disableSandbox: false, |
| 39 | + pluginWorkingDirectory: temporaryDirectory.appending(component: "plugin-working-dir"), |
| 40 | + additionalFileRules: [] |
| 41 | + ) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +fileprivate func withGeneratedPIF(fromFixture fixtureName: String, do doIt: (SwiftBuildSupport.PIF.TopLevelObject, TestingObservability) async throws -> ()) async throws { |
| 46 | + try await fixture(name: fixtureName) { fixturePath in |
| 47 | + let observabilitySystem = ObservabilitySystem.makeForTesting() |
| 48 | + let workspace = try Workspace( |
| 49 | + fileSystem: localFileSystem, |
| 50 | + forRootPackage: fixturePath, |
| 51 | + customManifestLoader: ManifestLoader(toolchain: UserToolchain.default), |
| 52 | + delegate: MockWorkspaceDelegate() |
| 53 | + ) |
| 54 | + let rootInput = PackageGraphRootInput(packages: [fixturePath], dependencies: []) |
| 55 | + let graph = try await workspace.loadPackageGraph( |
| 56 | + rootInput: rootInput, |
| 57 | + observabilityScope: observabilitySystem.topScope |
| 58 | + ) |
| 59 | + let builder = PIFBuilder( |
| 60 | + graph: graph, |
| 61 | + parameters: try PIFBuilderParameters.constructDefaultParametersForTesting(temporaryDirectory: fixturePath), |
| 62 | + fileSystem: localFileSystem, |
| 63 | + observabilityScope: observabilitySystem.topScope |
| 64 | + ) |
| 65 | + let pif = try await builder.constructPIF( |
| 66 | + buildParameters: mockBuildParameters(destination: .host) |
| 67 | + ) |
| 68 | + try await doIt(pif, observabilitySystem) |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +extension SwiftBuildSupport.PIF.Workspace { |
| 73 | + fileprivate func project(named name: String) throws -> SwiftBuildSupport.PIF.Project { |
| 74 | + let matchingProjects = projects.filter { |
| 75 | + $0.underlying.name == name |
| 76 | + } |
| 77 | + if matchingProjects.isEmpty { |
| 78 | + throw StringError("No project named \(name) in PIF workspace") |
| 79 | + } else if matchingProjects.count > 1 { |
| 80 | + throw StringError("Multiple projects named \(name) in PIF workspace") |
| 81 | + } else { |
| 82 | + return matchingProjects[0] |
| 83 | + } |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +extension SwiftBuildSupport.PIF.Project { |
| 88 | + fileprivate func target(named name: String) throws -> ProjectModel.BaseTarget { |
| 89 | + let matchingTargets = underlying.targets.filter { |
| 90 | + $0.common.name == name |
| 91 | + } |
| 92 | + if matchingTargets.isEmpty { |
| 93 | + throw StringError("No target named \(name) in PIF project") |
| 94 | + } else if matchingTargets.count > 1 { |
| 95 | + throw StringError("Multiple target named \(name) in PIF project") |
| 96 | + } else { |
| 97 | + return matchingTargets[0] |
| 98 | + } |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +extension SwiftBuild.ProjectModel.BaseTarget { |
| 103 | + fileprivate func buildConfig(named name: String) throws -> SwiftBuild.ProjectModel.BuildConfig { |
| 104 | + let matchingConfigs = common.buildConfigs.filter { |
| 105 | + $0.name == name |
| 106 | + } |
| 107 | + if matchingConfigs.isEmpty { |
| 108 | + throw StringError("No config named \(name) in PIF target") |
| 109 | + } else if matchingConfigs.count > 1 { |
| 110 | + throw StringError("Multiple configs named \(name) in PIF target") |
| 111 | + } else { |
| 112 | + return matchingConfigs[0] |
| 113 | + } |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +@Suite |
| 118 | +struct PIFBuilderTests { |
| 119 | + @Test func platformConditionBasics() async throws { |
| 120 | + try await withGeneratedPIF(fromFixture: "PIFBuilder/UnknownPlatforms") { pif, observabilitySystem in |
| 121 | + // We should emit a warning to the PIF log about the unknown platform |
| 122 | + #expect(observabilitySystem.diagnostics.filter { |
| 123 | + $0.severity == .warning && $0.message.contains("Ignoring settings assignments for unknown platform 'DoesNotExist'") |
| 124 | + }.count > 0) |
| 125 | + |
| 126 | + let releaseConfig = try pif.workspace |
| 127 | + .project(named: "UnknownPlatforms") |
| 128 | + .target(named: "UnknownPlatforms") |
| 129 | + .buildConfig(named: "Release") |
| 130 | + |
| 131 | + // The platforms with conditional settings should have those propagated to the PIF. |
| 132 | + #expect(releaseConfig.settings.platformSpecificSettings[.linux]?[.SWIFT_ACTIVE_COMPILATION_CONDITIONS] == ["$(inherited)", "BAR"]) |
| 133 | + #expect(releaseConfig.settings.platformSpecificSettings[.macOS]?[.SWIFT_ACTIVE_COMPILATION_CONDITIONS] == ["$(inherited)", "BAZ"]) |
| 134 | + // Platforms without conditional settings should get the default. |
| 135 | + #expect(releaseConfig.settings.platformSpecificSettings[.windows]?[.SWIFT_ACTIVE_COMPILATION_CONDITIONS] == ["$(inherited)"]) |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments