From de25fd5eaaea2f6918dd6776a5855ec50f33564a Mon Sep 17 00:00:00 2001 From: Kabir Oberai Date: Tue, 20 Oct 2026 18:57:45 -0400 Subject: [PATCH 01/15] pif: add XPIF model --- .swiftlint.yml | 1 + Package.swift | 12 + Sources/XPIF/BuildConfig.swift | 59 + Sources/XPIF/BuildFile.swift | 161 ++ Sources/XPIF/BuildPhases.swift | 453 +++++ Sources/XPIF/BuildRule.swift | 219 +++ Sources/XPIF/BuildSettings.swift | 287 ++++ Sources/XPIF/Container.swift | 377 +++++ Sources/XPIF/CustomTask.swift | 103 ++ Sources/XPIF/ExternalTarget.swift | 63 + Sources/XPIF/ImpartedBuildProperties.swift | 32 + Sources/XPIF/JSONValue.swift | 100 ++ Sources/XPIF/LICENSE.txt | 211 +++ Sources/XPIF/PlatformFilter.swift | 43 + Sources/XPIF/ProductReference.swift | 36 + Sources/XPIF/Project.swift | 236 +++ Sources/XPIF/ProvisioningSourceData.swift | 43 + Sources/XPIF/README.md | 17 + Sources/XPIF/References.swift | 323 ++++ Sources/XPIF/SandboxingOverride.swift | 12 + Sources/XPIF/StringList.swift | 26 + Sources/XPIF/TargetDependency.swift | 43 + Sources/XPIF/Targets.swift | 588 +++++++ Sources/XPIF/XPIF.swift | 90 + Tests/XToolTests/Fixtures/complex.json | 1470 +++++++++++++++++ Tests/XToolTests/Fixtures/workspace-only.json | 12 + .../Fixtures/workspace-with-target.json | 48 + Tests/XToolTests/Fixtures/xcode-project.json | 248 +++ Tests/XToolTests/PIFContainerTests.swift | 107 ++ Tests/XToolTests/TestFixtures.swift | 5 + Tests/XToolTests/XPIFTests.swift | 217 +++ 31 files changed, 5642 insertions(+) create mode 100644 Sources/XPIF/BuildConfig.swift create mode 100644 Sources/XPIF/BuildFile.swift create mode 100644 Sources/XPIF/BuildPhases.swift create mode 100644 Sources/XPIF/BuildRule.swift create mode 100644 Sources/XPIF/BuildSettings.swift create mode 100644 Sources/XPIF/Container.swift create mode 100644 Sources/XPIF/CustomTask.swift create mode 100644 Sources/XPIF/ExternalTarget.swift create mode 100644 Sources/XPIF/ImpartedBuildProperties.swift create mode 100644 Sources/XPIF/JSONValue.swift create mode 100644 Sources/XPIF/LICENSE.txt create mode 100644 Sources/XPIF/PlatformFilter.swift create mode 100644 Sources/XPIF/ProductReference.swift create mode 100644 Sources/XPIF/Project.swift create mode 100644 Sources/XPIF/ProvisioningSourceData.swift create mode 100644 Sources/XPIF/README.md create mode 100644 Sources/XPIF/References.swift create mode 100644 Sources/XPIF/SandboxingOverride.swift create mode 100644 Sources/XPIF/StringList.swift create mode 100644 Sources/XPIF/TargetDependency.swift create mode 100644 Sources/XPIF/Targets.swift create mode 100644 Sources/XPIF/XPIF.swift create mode 100644 Tests/XToolTests/Fixtures/complex.json create mode 100644 Tests/XToolTests/Fixtures/workspace-only.json create mode 100644 Tests/XToolTests/Fixtures/workspace-with-target.json create mode 100644 Tests/XToolTests/Fixtures/xcode-project.json create mode 100644 Tests/XToolTests/PIFContainerTests.swift create mode 100644 Tests/XToolTests/TestFixtures.swift create mode 100644 Tests/XToolTests/XPIFTests.swift diff --git a/.swiftlint.yml b/.swiftlint.yml index 70833bc1..4e610427 100644 --- a/.swiftlint.yml +++ b/.swiftlint.yml @@ -73,3 +73,4 @@ included: - macOS/XToolMac excluded: - Sources/DeveloperAPI/Generated + - Sources/XPIF diff --git a/Package.swift b/Package.swift index 1f472f4a..73cf6211 100644 --- a/Package.swift +++ b/Package.swift @@ -36,6 +36,7 @@ let package = Package( .macOS(.v14), ], products: [ + .library(name: "XPIF", targets: ["XPIF"]), .library( name: "XKit", targets: ["XKit"] @@ -87,6 +88,15 @@ let package = Package( .package(url: "https://github.com/tuist/XcodeProj.git", from: "9.17.5"), ], targets: [ + .target( + name: "XPIF", + dependencies: [ + .product(name: "Crypto", package: "swift-crypto"), + .product(name: "OrderedCollections", package: "swift-collections"), + ], + exclude: ["LICENSE.txt", "README.md"], + swiftSettings: swiftSettings, + ), .target( name: "CXKit", dependencies: [ @@ -165,6 +175,7 @@ let package = Package( .testTarget( name: "XToolTests", dependencies: [ + "XPIF", "XKit", "XToolSupport", "XCLIUtils", @@ -173,6 +184,7 @@ let package = Package( .product(name: "X509", package: "swift-certificates"), .product(name: "CustomDump", package: "swift-custom-dump"), ], + exclude: ["Fixtures"], swiftSettings: swiftSettings, ), .target( diff --git a/Sources/XPIF/BuildConfig.swift b/Sources/XPIF/BuildConfig.swift new file mode 100644 index 00000000..f234a0b6 --- /dev/null +++ b/Sources/XPIF/BuildConfig.swift @@ -0,0 +1,59 @@ +// Adapted from SwiftBuild/BuildConfig.swift: mutable Xcode PIF support and lossless JSON fields. + +import Foundation + +extension XPIF { + /// A build configuration, which is a named collection of build settings. + public struct BuildConfig: Sendable, Hashable, Identifiable { + public let id: GUID + public var name: String + public var settings: BuildSettings + public var additionalFields: [String: JSONValue] = [:] + var explicitDefaults: [String: JSONValue] = [:] + public var baseConfigurationFileReference: GUID? = nil + public var impartedBuildProperties: ImpartedBuildProperties + + public init(id: GUID, name: String, settings: BuildSettings, impartedBuildSettings: BuildSettings = .init()) { + precondition(!name.isEmpty) + self.id = id + self.name = name + self.settings = settings + self.impartedBuildProperties = ImpartedBuildProperties(settings: impartedBuildSettings) + } + } +} + +extension XPIF.BuildConfig: Codable { + public init(from decoder: any Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + self.id = try container.decode(XPIF.GUID.self, forKey: .guid) + self.name = try container.decode(String.self, forKey: .name) + self.settings = try container.decode(XPIF.BuildSettings.self, forKey: .buildSettings) + self.impartedBuildProperties = try container.decodeIfPresent(XPIF.ImpartedBuildProperties.self, forKey: .impartedBuildProperties) ?? .init(settings: .init()) + self.baseConfigurationFileReference = try container.decodeIfPresent(XPIF.GUID.self, forKey: .baseConfigurationFileReference) + self.additionalFields = try decodeAdditionalFields(from: decoder, excluding: CodingKeys.self) + self.explicitDefaults = try decodeExplicitDefaults(from: decoder, defaults: ["impartedBuildProperties": .object(["buildSettings": .object([:])])]) + } + + public func encode(to encoder: any Encoder) throws { + try encodeExplicitDefaults(explicitDefaults, to: encoder) + try encodeAdditionalFields(self.additionalFields, to: encoder, excluding: CodingKeys.self) + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .guid) + try container.encode(name, forKey: .name) + try container.encode(settings, forKey: .buildSettings) + if !impartedBuildProperties.settings.values.isEmpty || !impartedBuildProperties.additionalFields.isEmpty { + try container.encode(impartedBuildProperties, forKey: .impartedBuildProperties) + } + try container.encodeIfPresent(baseConfigurationFileReference, forKey: .baseConfigurationFileReference) + } + + enum CodingKeys: String, CodingKey, CaseIterable { + case guid + case name + case buildSettings + case impartedBuildProperties + case baseConfigurationFileReference + } +} + diff --git a/Sources/XPIF/BuildFile.swift b/Sources/XPIF/BuildFile.swift new file mode 100644 index 00000000..602503f7 --- /dev/null +++ b/Sources/XPIF/BuildFile.swift @@ -0,0 +1,161 @@ +// Adapted from SwiftBuild/BuildFile.swift: mutable Xcode PIF support and lossless JSON fields. + +import Foundation + +extension XPIF { + /// A build file, representing the membership of either a file or target product reference in a build phase. + public struct BuildFile: Sendable, Hashable, Identifiable { + public var additionalFields: [String: JSONValue] = [:] + var explicitDefaults: [String: JSONValue] = [:] + public enum Ref: Sendable, Hashable { + case reference(id: GUID) + case targetProduct(id: GUID) + case namedReference(name: String, fileType: String) + } + public enum HeaderVisibility: String, Sendable, Hashable, Codable { + case `public` = "public" + case `private` = "private" + } + public enum GeneratedCodeVisibility: String, Sendable, Hashable, Codable { + case `public` + case `private` + case project + case noCodegen = "no_codegen" + } + public enum ResourceRule: String, Sendable, Hashable, Codable { + case process + case copy + case embedInCode + } + + public let id: GUID + public var ref: Ref + public var headerVisibility: HeaderVisibility? = nil + public var generatedCodeVisibility: GeneratedCodeVisibility? = nil + public var platformFilters: Set = [] + public var codeSignOnCopy: Bool = false + public var removeHeadersOnCopy: Bool = false + public var resourceRule: ResourceRule? = nil + public var additionalCompilerOptions: StringList? = nil + public var shouldLinkWeakly: Bool = false + public var assetTags: Set = [] + public var decompress: Bool = false + public var shouldWarnIfNoRuleToProcess: Bool = true + public var migCodegenFiles: String? = nil + + public init( + id: GUID, + ref: Ref, + headerVisibility: HeaderVisibility? = nil, + generatedCodeVisibility: GeneratedCodeVisibility? = nil, + platformFilters: Set = [], + codeSignOnCopy: Bool = false, + removeHeadersOnCopy: Bool = false, + resourceRule: ResourceRule? = nil + ) { + self.id = id + self.ref = ref + self.headerVisibility = headerVisibility + self.generatedCodeVisibility = generatedCodeVisibility + self.platformFilters = platformFilters + self.codeSignOnCopy = codeSignOnCopy + self.removeHeadersOnCopy = removeHeadersOnCopy + self.resourceRule = resourceRule + } + + public init( + id: GUID, + fileRef: FileReference, + headerVisibility: HeaderVisibility? = nil, + generatedCodeVisibility: GeneratedCodeVisibility? = nil, + platformFilters: Set = [], + codeSignOnCopy: Bool = false, + removeHeadersOnCopy: Bool = false, + resourceRule: ResourceRule? = nil + ) { + self.init(id: id, ref: .reference(id: fileRef.id), headerVisibility: headerVisibility, generatedCodeVisibility: generatedCodeVisibility, platformFilters: platformFilters, codeSignOnCopy: codeSignOnCopy, removeHeadersOnCopy: removeHeadersOnCopy, resourceRule: resourceRule) + } + } +} + + +extension XPIF.BuildFile: Codable { + public init(from decoder: any Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + self.id = try container.decode(XPIF.GUID.self, forKey: .guid) + self.platformFilters = try container.decodeIfPresent(Set.self, forKey: .platformFilters) ?? [] + + if let refId = try container.decodeIfPresent(XPIF.GUID.self, forKey: .fileReference) { + self.ref = .reference(id: refId) + } else if let refId = try container.decodeIfPresent(XPIF.GUID.self, forKey: .targetReference) { + self.ref = .targetProduct(id: refId) + } else { + self.ref = .namedReference( + name: try container.decode(String.self, forKey: .name), + fileType: try container.decode(String.self, forKey: .fileType) + ) + } + self.headerVisibility = try container.decodeIfPresent(HeaderVisibility.self, forKey: .headerVisibility) + self.generatedCodeVisibility = try container.decodeIfPresent(GeneratedCodeVisibility.self, forKey: .intentsCodegenVisibility) + if generatedCodeVisibility == nil, try container.decodePIFBool(forKey: .intentsCodegenFiles) { + generatedCodeVisibility = .public + } + self.resourceRule = try container.decodeIfPresent(ResourceRule.self, forKey: .resourceRule) + self.additionalCompilerOptions = try container.decodeIfPresent(XPIF.StringList.self, forKey: .additionalCompilerOptions) + self.codeSignOnCopy = try container.decodePIFBool(forKey: .codeSignOnCopy) + self.removeHeadersOnCopy = try container.decodePIFBool(forKey: .removeHeadersOnCopy) + self.shouldLinkWeakly = try container.decodePIFBool(forKey: .shouldLinkWeakly) + self.assetTags = try container.decodeIfPresent(Set.self, forKey: .assetTags) ?? [] + self.decompress = try container.decodePIFBool(forKey: .decompress) + self.shouldWarnIfNoRuleToProcess = try container.decodePIFBool(forKey: .shouldWarnIfNoRuleToProcess, default: true) + self.migCodegenFiles = try container.decodeIfPresent(String.self, forKey: .migCodegenFiles) + + self.additionalFields = try decodeAdditionalFields(from: decoder, excluding: CodingKeys.self) + self.explicitDefaults = try decodeExplicitDefaults(from: decoder, defaults: ["platformFilters": .array([]), "codeSignOnCopy": .string("false"), "removeHeadersOnCopy": .string("false")]) + } + + public func encode(to encoder: any Encoder) throws { + try encodeExplicitDefaults(explicitDefaults, to: encoder) + try encodeAdditionalFields(self.additionalFields, to: encoder, excluding: CodingKeys.self) + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(self.id, forKey: .guid) + if !platformFilters.isEmpty { try container.encode(platformFilters.sorted(), forKey: .platformFilters) } + + switch self.ref { + case .reference(id: let refId): + try container.encode(refId, forKey: .fileReference) + case .targetProduct(id: let refId): + try container.encode(refId, forKey: .targetReference) + case .namedReference(let name, let fileType): + try container.encode(name, forKey: .name) + try container.encode(fileType, forKey: .fileType) + } + try container.encodeIfPresent(headerVisibility, forKey: .headerVisibility) + try container.encodeIfPresent(generatedCodeVisibility, forKey: .intentsCodegenVisibility) + try container.encodeIfPresent(resourceRule, forKey: .resourceRule) + try container.encodeIfPresent(additionalCompilerOptions, forKey: .additionalCompilerOptions) + if codeSignOnCopy { try container.encode("true", forKey: .codeSignOnCopy) } + if removeHeadersOnCopy { try container.encode("true", forKey: .removeHeadersOnCopy) } + if shouldLinkWeakly { try container.encode("true", forKey: .shouldLinkWeakly) } + if !assetTags.isEmpty { try container.encode(assetTags.sorted(), forKey: .assetTags) } + if decompress { try container.encode("true", forKey: .decompress) } + if !shouldWarnIfNoRuleToProcess { try container.encode("false", forKey: .shouldWarnIfNoRuleToProcess) } + try container.encodeIfPresent(migCodegenFiles, forKey: .migCodegenFiles) + } + + enum CodingKeys: String, CodingKey, CaseIterable { + case guid + case fileReference + case headerVisibility + case platformFilters + case codeSignOnCopy + case removeHeadersOnCopy + case intentsCodegenVisibility + case resourceRule + case targetReference + case name, fileType, additionalCompilerOptions, shouldLinkWeakly, assetTags + case decompress, shouldWarnIfNoRuleToProcess, migCodegenFiles, intentsCodegenFiles + } +} + + diff --git a/Sources/XPIF/BuildPhases.swift b/Sources/XPIF/BuildPhases.swift new file mode 100644 index 00000000..226ab87a --- /dev/null +++ b/Sources/XPIF/BuildPhases.swift @@ -0,0 +1,453 @@ +// Adapted from SwiftBuild/BuildPhases.swift: mutable Xcode PIF support and lossless JSON fields. + +import Foundation + +extension XPIF { + /// All possible build phases in a target. + public enum BuildPhase: Sendable, Hashable { + case headers(HeadersBuildPhase) + case sources(SourcesBuildPhase) + case frameworks(FrameworksBuildPhase) + case copyBundleResources(CopyBundleResourcesBuildPhase) + case copyFiles(CopyFilesBuildPhase) + case shellScript(ShellScriptBuildPhase) + + public var common: BuildPhaseCommon { + switch self { + case .headers(let phase): return phase.common + case .sources(let phase): return phase.common + case .frameworks(let phase): return phase.common + case .copyBundleResources(let phase): return phase.common + case .copyFiles(let phase): return phase.common + case .shellScript(let phase): return phase.common + } + } + } + + public struct BuildPhaseCommon: Sendable, Hashable { + public var additionalFields: [String: JSONValue] = [:] + public var id: GUID + public var files: [BuildFile] + + public init(id: GUID, files: [BuildFile] = []) { + self.id = id + self.files = files + } + + private var nextBuildFileId: GUID { + return "\(self.id.value)::\(self.files.count)" + } + + // MARK: - BuildFile + + @discardableResult public mutating func addBuildFile(_ create: CreateFn) -> BuildFile { + let buildFile = create(nextBuildFileId) + self.files.append(buildFile) + return buildFile + } + + public subscript(file tag: Tag) -> BuildFile { + get { files[id: tag.value] } + set { files[id: tag.value] = newValue } + } + + } + + /// A "headers" build phase, i.e. one that copies headers into a directory of the product, after suitable processing. + public struct HeadersBuildPhase: Sendable, Hashable, Common { + public var common: BuildPhaseCommon + + public init(id: GUID, files: [BuildFile] = []) { + self.common = BuildPhaseCommon(id: id, files: files) + } + } + + /// A "sources" build phase, i.e. one that compiles sources and provides them to be linked into the executable code of the product. + public struct SourcesBuildPhase: Sendable, Hashable, Common { + public var common: BuildPhaseCommon + + public init(id: GUID, files: [BuildFile] = []) { + self.common = BuildPhaseCommon(id: id, files: files) + } + } + + /// A "frameworks" build phase, i.e. one that links compiled code and libraries into the executable of the product. + public struct FrameworksBuildPhase: Sendable, Hashable, Common { + public var common: BuildPhaseCommon + + public init(id: GUID, files: [BuildFile] = []) { + self.common = BuildPhaseCommon(id: id, files: files) + } + } + + public struct CopyBundleResourcesBuildPhase: Sendable, Hashable, Common { + public var common: BuildPhaseCommon + + public init(id: GUID, files: [BuildFile] = []) { + self.common = BuildPhaseCommon(id: id, files: files) + } + } + + /// A "copy files" build phase, i.e. one that copies files to an arbitrary location relative to the product. + public struct CopyFilesBuildPhase: Sendable, Hashable, Common { + public enum DestinationSubfolder: Sendable, Hashable, Codable { + case absolute + case builtProductsDir + case buildSetting(String) + + public static let wrapper = DestinationSubfolder.buildSetting("$(WRAPPER_NAME)") + public static let resources = DestinationSubfolder.buildSetting("$(UNLOCALIZED_RESOURCES_FOLDER_PATH)") + public static let frameworks = DestinationSubfolder.buildSetting("$(FRAMEWORKS_FOLDER_PATH)") + public static let sharedFrameworks = DestinationSubfolder.buildSetting("$(SHARED_FRAMEWORKS_FOLDER_PATH)") + public static let sharedSupport = DestinationSubfolder.buildSetting("$(SHARED_SUPPORT_FOLDER_PATH)") + public static let plugins = DestinationSubfolder.buildSetting("$(PLUGINS_FOLDER_PATH)") + public static let javaResources = DestinationSubfolder.buildSetting("$(JAVA_FOLDER_PATH)") + + public var pathString: String { + switch self { + case .absolute: + return "" + case .builtProductsDir: + return "" + case .buildSetting(let value): + return value + } + } + + public init(from decoder: any Decoder) throws { + let container = try decoder.singleValueContainer() + let str = try container.decode(String.self) + switch str { + case "": self = .absolute + case "": self = .builtProductsDir + default: self = .buildSetting(str) + } + } + + public func encode(to encoder: any Encoder) throws { + var container = encoder.singleValueContainer() + try container.encode(self.pathString) + } + } + + public var common: BuildPhaseCommon + public var destinationSubfolder: CopyFilesBuildPhase.DestinationSubfolder + public var destinationSubpath: String = "" + public var runOnlyForDeploymentPostprocessing: Bool = false + + public init(common: BuildPhaseCommon, destinationSubfolder: CopyFilesBuildPhase.DestinationSubfolder, destinationSubpath: String = "", runOnlyForDeploymentPostprocessing: Bool = false) { + self.common = common + self.destinationSubfolder = destinationSubfolder + self.destinationSubpath = destinationSubpath + self.runOnlyForDeploymentPostprocessing = runOnlyForDeploymentPostprocessing + } + } + + /// A "shell script" build phase, i.e. one that runs a custom shell script. + public struct ShellScriptBuildPhase: Sendable, Hashable, Common { + public var common: BuildPhaseCommon + public var name: String + public var scriptContents: String + public var shellPath: String + public var inputPaths: [String] + public var outputPaths: [String] + public var emitEnvironment: Bool + public var alwaysOutOfDate: Bool + public var runOnlyForDeploymentPostprocessing: Bool + public var originalObjectID: String + public var sandboxingOverride: SandboxingOverride + public var inputFileListPaths: [String] = [] + public var outputFileListPaths: [String] = [] + public var dependencyInfo: BuildRule.Action.DependencyInfoFormat? = nil + public var alwaysRunForInstallHdrs: Bool + + public init( + id: GUID, + name: String, + scriptContents: String, + shellPath: String, + inputPaths: [String], + outputPaths: [String], + emitEnvironment: Bool, + alwaysOutOfDate: Bool, + runOnlyForDeploymentPostprocessing: Bool = false, + originalObjectID: String, + sandboxingOverride: SandboxingOverride = .basedOnBuildSetting, + alwaysRunForInstallHdrs: Bool = false + ) { + self.common = BuildPhaseCommon(id: id) + self.name = name + self.scriptContents = scriptContents + self.shellPath = shellPath + self.inputPaths = inputPaths + self.outputPaths = outputPaths + self.emitEnvironment = emitEnvironment + self.alwaysOutOfDate = alwaysOutOfDate + self.runOnlyForDeploymentPostprocessing = runOnlyForDeploymentPostprocessing + self.originalObjectID = originalObjectID + self.sandboxingOverride = sandboxingOverride + self.alwaysRunForInstallHdrs = alwaysRunForInstallHdrs + } + } +} + + + +extension XPIF.BuildPhase: Codable { + public init(from decoder: any Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + let type = try container.decode(String.self, forKey: .type) + switch type { + case "com.apple.buildphase.headers": + self = .headers(try .init(from: decoder)) + case "com.apple.buildphase.sources": + self = .sources(try .init(from: decoder)) + case "com.apple.buildphase.frameworks": + self = .frameworks(try .init(from: decoder)) + case "com.apple.buildphase.resources": + self = .copyBundleResources(try .init(from: decoder)) + case "com.apple.buildphase.copy-files": + self = .copyFiles(try .init(from: decoder)) + case "com.apple.buildphase.shell-script": + self = .shellScript(try .init(from: decoder)) + default: + throw DecodingError.dataCorruptedError(forKey: .type, in: container, debugDescription: "Unknown value") + } + } + + public func encode(to encoder: any Encoder) throws { + switch self { + case .headers(let buildPhase): try buildPhase.encode(to: encoder) + case .sources(let buildPhase): try buildPhase.encode(to: encoder) + case .frameworks(let buildPhase): try buildPhase.encode(to: encoder) + case .copyBundleResources(let buildPhase): try buildPhase.encode(to: encoder) + case .copyFiles(let buildPhase): try buildPhase.encode(to: encoder) + case .shellScript(let buildPhase): try buildPhase.encode(to: encoder) + } + } + + enum CodingKeys: String, CodingKey { + case type + } +} + +extension XPIF.BuildPhaseCommon: Codable { + public init(from decoder: any Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + self.id = try container.decode(XPIF.GUID.self, forKey: .guid) + self.files = try container.decodeIfPresent([XPIF.BuildFile].self, forKey: .buildFiles) ?? [] + self.additionalFields = try decodeAdditionalFields(from: decoder, excluding: CodingKeys.self) + } + + public func encode(to encoder: any Encoder) throws { + try encodeAdditionalFields(self.additionalFields, to: encoder, excluding: CodingKeys.self) + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(self.id, forKey: .guid) + try container.encode(self.files, forKey: .buildFiles) + } + + enum CodingKeys: String, CodingKey, CaseIterable { + case guid + case buildFiles + } +} + +extension XPIF.HeadersBuildPhase: Codable { + public init(from decoder: any Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + let id = try container.decode(XPIF.GUID.self, forKey: .guid) + let files = try container.decodeIfPresent([XPIF.BuildFile].self, forKey: .buildFiles) ?? [] + self.common = .init(id: id, files: files) + self.common.additionalFields = try decodeAdditionalFields(from: decoder, excluding: CodingKeys.self) + } + + public func encode(to encoder: any Encoder) throws { + try encodeAdditionalFields(self.common.additionalFields, to: encoder, excluding: CodingKeys.self) + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode("com.apple.buildphase.headers", forKey: .type) + try container.encode(self.id, forKey: .guid) + try container.encode(self.files, forKey: .buildFiles) + } + + enum CodingKeys: String, CodingKey, CaseIterable { + case type + case guid + case buildFiles + } +} + +extension XPIF.SourcesBuildPhase: Codable { + public init(from decoder: any Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + let id = try container.decode(XPIF.GUID.self, forKey: .guid) + let files = try container.decodeIfPresent([XPIF.BuildFile].self, forKey: .buildFiles) ?? [] + self.common = .init(id: id, files: files) + self.common.additionalFields = try decodeAdditionalFields(from: decoder, excluding: CodingKeys.self) + } + + public func encode(to encoder: any Encoder) throws { + try encodeAdditionalFields(self.common.additionalFields, to: encoder, excluding: CodingKeys.self) + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode("com.apple.buildphase.sources", forKey: .type) + try container.encode(self.id, forKey: .guid) + try container.encode(self.files, forKey: .buildFiles) + } + + enum CodingKeys: String, CodingKey, CaseIterable { + case type + case guid + case buildFiles + } +} + +extension XPIF.FrameworksBuildPhase: Codable { + public init(from decoder: any Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + let id = try container.decode(XPIF.GUID.self, forKey: .guid) + let files = try container.decodeIfPresent([XPIF.BuildFile].self, forKey: .buildFiles) ?? [] + self.common = .init(id: id, files: files) + self.common.additionalFields = try decodeAdditionalFields(from: decoder, excluding: CodingKeys.self) + } + + public func encode(to encoder: any Encoder) throws { + try encodeAdditionalFields(self.common.additionalFields, to: encoder, excluding: CodingKeys.self) + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode("com.apple.buildphase.frameworks", forKey: .type) + try container.encode(self.id, forKey: .guid) + try container.encode(self.files, forKey: .buildFiles) + } + + enum CodingKeys: String, CodingKey, CaseIterable { + case type + case guid + case buildFiles + } +} + +extension XPIF.CopyBundleResourcesBuildPhase: Codable { + public init(from decoder: any Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + let id = try container.decode(XPIF.GUID.self, forKey: .guid) + let files = try container.decodeIfPresent([XPIF.BuildFile].self, forKey: .buildFiles) ?? [] + self.common = .init(id: id, files: files) + self.common.additionalFields = try decodeAdditionalFields(from: decoder, excluding: CodingKeys.self) + } + + public func encode(to encoder: any Encoder) throws { + try encodeAdditionalFields(self.common.additionalFields, to: encoder, excluding: CodingKeys.self) + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode("com.apple.buildphase.resources", forKey: .type) + try container.encode(self.id, forKey: .guid) + try container.encode(self.files, forKey: .buildFiles) + } + + enum CodingKeys: String, CodingKey, CaseIterable { + case type + case guid + case buildFiles + } +} + +extension XPIF.CopyFilesBuildPhase: Codable { + public init(from decoder: any Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + self.common = .init(id: try container.decode(XPIF.GUID.self, forKey: .guid)) + self.destinationSubfolder = try container.decode(DestinationSubfolder.self, forKey: .destinationSubfolder) + self.files = try container.decodeIfPresent([XPIF.BuildFile].self, forKey: .buildFiles) ?? [] + self.destinationSubpath = try container.decodeIfPresent(String.self, forKey: .destinationSubpath) ?? "" + self.runOnlyForDeploymentPostprocessing = try container.decodePIFBool(forKey: .runOnlyForDeploymentPostprocessing) + self.common.additionalFields = try decodeAdditionalFields(from: decoder, excluding: CodingKeys.self) + } + + public func encode(to encoder: any Encoder) throws { + try encodeAdditionalFields(self.common.additionalFields, to: encoder, excluding: CodingKeys.self) + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode("com.apple.buildphase.copy-files", forKey: .type) + try container.encode(self.id, forKey: .guid) + try container.encode(self.files, forKey: .buildFiles) + try container.encode(self.destinationSubfolder.pathString, forKey: .destinationSubfolder) + try container.encode(self.destinationSubpath, forKey: .destinationSubpath) + if self.runOnlyForDeploymentPostprocessing { try container.encode("true", forKey: .runOnlyForDeploymentPostprocessing) } + } + + enum CodingKeys: String, CodingKey, CaseIterable { + case type + case guid + case buildFiles + case destinationSubfolder + case destinationSubpath + case runOnlyForDeploymentPostprocessing + } +} + +extension XPIF.ShellScriptBuildPhase: Codable { + public init(from decoder: any Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + let id = try container.decode(XPIF.GUID.self, forKey: .guid) + let files = try container.decodeIfPresent([XPIF.BuildFile].self, forKey: .buildFiles) ?? [] + self.common = .init(id: id, files: files) + + self.name = try container.decodeIfPresent(String.self, forKey: .name) ?? "" + self.shellPath = try container.decodeIfPresent(String.self, forKey: .shellPath) ?? "/bin/sh" + self.scriptContents = try container.decodeIfPresent(String.self, forKey: .scriptContents) ?? "" + self.inputPaths = try container.decodeIfPresent([String].self, forKey: .inputFilePaths) ?? [] + self.outputPaths = try container.decodeIfPresent([String].self, forKey: .outputFilePaths) ?? [] + self.emitEnvironment = try container.decodePIFBool(forKey: .emitEnvironment) + self.sandboxingOverride = try container.decodeIfPresent(XPIF.SandboxingOverride.self, forKey: .sandboxingOverride) ?? .basedOnBuildSetting + self.alwaysOutOfDate = try container.decodePIFBool(forKey: .alwaysOutOfDate) + self.runOnlyForDeploymentPostprocessing = try container.decodePIFBool(forKey: .runOnlyForDeploymentPostprocessing) + self.originalObjectID = try container.decodeIfPresent(String.self, forKey: .originalObjectID) ?? "" + self.alwaysRunForInstallHdrs = try container.decodePIFBool(forKey: .alwaysRunForInstallHdrs) + self.inputFileListPaths = try container.decodeIfPresent([String].self, forKey: .inputFileListPaths) ?? [] + self.outputFileListPaths = try container.decodeIfPresent([String].self, forKey: .outputFileListPaths) ?? [] + if let format = try container.decodeIfPresent(String.self, forKey: .dependencyFileFormat) { + let paths = try container.decode([String].self, forKey: .dependencyFilePaths) + self.dependencyInfo = try .init(format: format, paths: paths, codingPath: decoder.codingPath) + } + self.common.additionalFields = try decodeAdditionalFields(from: decoder, excluding: CodingKeys.self) + } + + public func encode(to encoder: any Encoder) throws { + try encodeAdditionalFields(self.common.additionalFields, to: encoder, excluding: CodingKeys.self) + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode("com.apple.buildphase.shell-script", forKey: .type) + try container.encode(self.id, forKey: .guid) + try container.encode(self.name, forKey: .name) + try container.encode(self.shellPath, forKey: .shellPath) + try container.encode(self.scriptContents, forKey: .scriptContents) + try container.encode(self.files, forKey: .buildFiles) + try container.encode(self.inputPaths, forKey: .inputFilePaths) + try container.encode(self.outputPaths, forKey: .outputFilePaths) + try container.encode(self.emitEnvironment ? "true" : "false", forKey: .emitEnvironment) + try container.encode(self.sandboxingOverride, forKey: .sandboxingOverride) + try container.encode(self.alwaysOutOfDate ? "true" : "false", forKey: .alwaysOutOfDate) + if self.runOnlyForDeploymentPostprocessing { try container.encode("true", forKey: .runOnlyForDeploymentPostprocessing) } + try container.encode(self.originalObjectID, forKey: .originalObjectID) + try container.encode(self.alwaysRunForInstallHdrs ? "true" : "false", forKey: .alwaysRunForInstallHdrs) + try container.encode(inputFileListPaths, forKey: .inputFileListPaths) + try container.encode(outputFileListPaths, forKey: .outputFileListPaths) + if let dependencyInfo { + try container.encode(dependencyInfo.formatAndPaths.format, forKey: .dependencyFileFormat) + try container.encode(dependencyInfo.formatAndPaths.paths, forKey: .dependencyFilePaths) + } + } + + enum CodingKeys: String, CodingKey, CaseIterable { + case type + case guid + case name + case shellPath + case scriptContents + case buildFiles + case inputFilePaths + case outputFilePaths + case emitEnvironment + case sandboxingOverride + case alwaysOutOfDate + case runOnlyForDeploymentPostprocessing + case originalObjectID + case alwaysRunForInstallHdrs + case inputFileListPaths, outputFileListPaths, dependencyFileFormat, dependencyFilePaths + } +} + diff --git a/Sources/XPIF/BuildRule.swift b/Sources/XPIF/BuildRule.swift new file mode 100644 index 00000000..311e58c5 --- /dev/null +++ b/Sources/XPIF/BuildRule.swift @@ -0,0 +1,219 @@ +// Adapted from SwiftBuild/BuildRule.swift: mutable Xcode PIF support and lossless JSON fields. + +import Foundation + +extension XPIF { + public struct BuildRule: Sendable, Hashable, Identifiable { + public enum Input: Sendable, Hashable { + case fileType(_ identifier: String) + case filePattern(_ pattern: String) + } + + public enum Action: Sendable, Hashable { + case compiler(_ identifier: String) + case shellScriptWithFileList(_ contents: String, inputPaths: [String], inputFileListPaths: [String], outputPaths: [String], outputFileListPaths: [String], outputFilesCompilerFlags: [String]?, dependencyInfo: DependencyInfoFormat?, runOncePerArchitecture: Bool) + + public enum DependencyInfoFormat: Sendable, Hashable { + case dependencyInfo(String) + case makefile(String) + case makefiles([String]) + } + } + + public let id: GUID + public var name: String + public var input: Input + public var action: Action + public var additionalFields: [String: JSONValue] = [:] + + public init(id: GUID, name: String, input: Input, action: Action) { + self.id = id + self.name = name + self.input = input + self.action = action + } + } +} + + +extension XPIF.BuildRule: Codable { + public init(from decoder: any Decoder) throws { + let container = try decoder.singleValueContainer() + let keyed = try decoder.container(keyedBy: CodingKeys.self) + var dict: [String: Value] = [:] + for key in keyed.allKeys { + dict[key.stringValue] = try keyed.decode(Value.self, forKey: key) + } + self.additionalFields = try decodeAdditionalFields(from: decoder, excluding: CodingKeys.self) + guard let id = dict["guid"]?.string, + let name = dict["name"]?.string, + let typeId = dict["fileTypeIdentifier"]?.string + else { + throw DecodingError.dataCorruptedError(in: container, debugDescription: "Unable to find expected values") + } + self.id = XPIF.GUID(id) + self.name = name + + if typeId == "pattern.proxy", + let pattern = dict["filePatterns"]?.string { + self.input = .filePattern(pattern) + } else { + self.input = .fileType(typeId) + } + + switch dict["compilerSpecificationIdentifier"]?.string { + case "com.apple.compilers.proxy.script": + guard let contents = dict["scriptContents"]?.string, + let inputPaths = dict["inputFilePaths"]?.array, + let outputPaths = dict["outputFilePaths"]?.array + else { + throw DecodingError.dataCorruptedError(in: container, debugDescription: "Failed to decode proxy script information.") + } + + let inputFileListPaths = dict["inputFileListPaths"]?.array ?? [] + let outputFileListPaths = dict["outputFileListPaths"]?.array ?? [] + + let outputFilesCompilerFlags = dict["outputFilesCompilerFlags"]?.array + let runOncePerArchitecture = dict["runOncePerArchitecture"]?.string == "true" + + let dependencyInfo: Action.DependencyInfoFormat? + if let format = dict["dependencyFileFormat"]?.string, + let paths = dict["dependencyFilePaths"]?.array { + + dependencyInfo = try .init(format: format, paths: paths, codingPath: decoder.codingPath) + } else { + dependencyInfo = nil + } + + self.action = .shellScriptWithFileList(contents, inputPaths: inputPaths, inputFileListPaths: inputFileListPaths, outputPaths: outputPaths, outputFileListPaths: outputFileListPaths, outputFilesCompilerFlags: outputFilesCompilerFlags, dependencyInfo: dependencyInfo, runOncePerArchitecture: runOncePerArchitecture) + case let .some(specId): + self.action = .compiler(specId) + case .none: + throw DecodingError.dataCorruptedError(in: container, debugDescription: "Failed to decode action information.") + } + } + + public func encode(to encoder: any Encoder) throws { + try encodeAdditionalFields(additionalFields, to: encoder, excluding: CodingKeys.self) + var container = encoder.container(keyedBy: StringKey.self) + for (key, value) in serialize() { + try container.encode(value, forKey: StringKey(key)) + } + } + + private enum CodingKeys: String, CodingKey, CaseIterable { + case guid, name, fileTypeIdentifier, filePatterns, compilerSpecificationIdentifier, scriptContents + case inputFilePaths, inputFileListPaths, outputFilePaths, outputFileListPaths, outputFilesCompilerFlags + case dependencyFileFormat, dependencyFilePaths, runOncePerArchitecture + } + + private enum Value: Codable { + case str(String) + case arr([String]) + + fileprivate var string: String? { + switch self { + case .str(let str): return str + case .arr: return nil + } + } + + fileprivate var array: [String]? { + switch self { + case .str(_): return nil + case .arr(let arr): return arr + } + } + + func encode(to encoder: any Encoder) throws { + switch self { + case .str(let str): try str.encode(to: encoder) + case .arr(let arr): try arr.encode(to: encoder) + } + } + + init(from decoder: any Decoder) throws { + let container = try decoder.singleValueContainer() + if let str = try? container.decode(String.self) { + self = .str(str) + } else if let arr = try? container.decode([String].self) { + self = .arr(arr) + } else { + throw DecodingError.dataCorruptedError(in: container, debugDescription: "Expected string or array in container.") + } + } + } + + // TODO: fold this into 'encode' + private func serialize() -> [String: Value] { + var dict: [String: Value] = [:] + dict["guid"] = .str(id.value) + dict["name"] = .str(name) + switch self.input { + case .fileType(let typeId): + dict["fileTypeIdentifier"] = .str(typeId) + case .filePattern(let pattern): + dict["fileTypeIdentifier"] = .str("pattern.proxy") + dict["filePatterns"] = .str(pattern) + } + + func populateDictFromShellScript(_ contents: String, inputPaths: [String], inputFileListPaths: [String]?, outputPaths: [String], outputFileListPaths: [String]?, outputFilesCompilerFlags: [String]?, dependencyInfo: Action.DependencyInfoFormat?, runOncePerArchitecture: Bool) { + dict["compilerSpecificationIdentifier"] = .str("com.apple.compilers.proxy.script") + dict["scriptContents"] = .str(contents) + dict["inputFilePaths"] = .arr(inputPaths) + if let inputFileListPaths { + dict["inputFileListPaths"] = .arr(inputFileListPaths) + } + dict["outputFilePaths"] = .arr(outputPaths) + if let outputFileListPaths { + dict["outputFileListPaths"] = .arr(outputFileListPaths) + } + if let outputFilesCompilerFlags = outputFilesCompilerFlags { + dict["outputFilesCompilerFlags"] = .arr(outputFilesCompilerFlags) + } + if let dependencyInfo = dependencyInfo { + let pifInfo: (format: String, paths: [String]) + switch dependencyInfo { + case .dependencyInfo(let path): + pifInfo = (format: "dependencyInfo", paths: [path]) + case .makefile(let path): + pifInfo = (format: "makefile", paths: [path]) + case .makefiles(let paths): + pifInfo = (format: "makefiles", paths: paths) + } + dict["dependencyFileFormat"] = .str(pifInfo.format) + dict["dependencyFilePaths"] = .arr(pifInfo.paths) + } + dict["runOncePerArchitecture"] = runOncePerArchitecture ? .str("true") : .str("false") + } + + switch self.action { + case .compiler(let specId): + dict["compilerSpecificationIdentifier"] = .str(specId) + case .shellScriptWithFileList(let contents, let inputPaths, let inputFileListPaths, let outputPaths, let outputFileListPaths, let outputFilesCompilerFlags, let dependencyInfo, let runOncePerArchitecture): + populateDictFromShellScript(contents, inputPaths: inputPaths, inputFileListPaths: inputFileListPaths, outputPaths: outputPaths, outputFileListPaths: outputFileListPaths, outputFilesCompilerFlags: outputFilesCompilerFlags, dependencyInfo: dependencyInfo, runOncePerArchitecture: runOncePerArchitecture) + } + return dict + } +} + + +extension XPIF.BuildRule.Action.DependencyInfoFormat { + init(format: String, paths: [String], codingPath: [any CodingKey]) throws { + switch format { + case "dependencyInfo" where paths.count == 1: self = .dependencyInfo(paths[0]) + case "makefile" where paths.count == 1: self = .makefile(paths[0]) + case "makefiles": self = .makefiles(paths) + default: + throw DecodingError.dataCorrupted(.init(codingPath: codingPath, debugDescription: "Invalid dependency file format or paths")) + } + } + + var formatAndPaths: (format: String, paths: [String]) { + switch self { + case .dependencyInfo(let path): ("dependencyInfo", [path]) + case .makefile(let path): ("makefile", [path]) + case .makefiles(let paths): ("makefiles", paths) + } + } +} diff --git a/Sources/XPIF/BuildSettings.swift b/Sources/XPIF/BuildSettings.swift new file mode 100644 index 00000000..ae199f1a --- /dev/null +++ b/Sources/XPIF/BuildSettings.swift @@ -0,0 +1,287 @@ +// Adapted from SwiftBuild/BuildSettings.swift: mutable Xcode PIF support and lossless JSON fields. + +import Foundation + +extension XPIF { + public struct BuildSettings: Sendable, Hashable { + public enum SingleValueSetting: String, CaseIterable, Sendable, Equatable, Codable { + case APPLICATION_EXTENSION_API_ONLY + case BUILT_PRODUCTS_DIR + case CLANG_CXX_LANGUAGE_STANDARD + case CLANG_ENABLE_MODULES + case CLANG_DISABLE_CXX_MODULES + case CLANG_ENABLE_OBJC_ARC + case CODE_SIGNING_REQUIRED + case CODE_SIGN_IDENTITY + case COMBINE_HIDPI_IMAGES + case COPY_PHASE_STRIP + case DEBUG_INFORMATION_FORMAT + case DEFINES_MODULE + case DEPLOYMENT_POSTPROCESSING + case DOCC_EXTRACT_SPI_DOCUMENTATION + case DOCC_EXTRACT_PROJECT_HEADERS_DOCUMENTATION + case DRIVERKIT_DEPLOYMENT_TARGET + case DYLIB_INSTALL_NAME_BASE + case EMBEDDED_CONTENT_CONTAINS_SWIFT + case ENABLE_NS_ASSERTIONS + case ENABLE_TESTABILITY + case ENABLE_TESTING_SEARCH_PATHS + case ENTITLEMENTS_REQUIRED + case EXECUTABLE_PREFIX + case GENERATE_INFOPLIST_FILE + case GCC_C_LANGUAGE_STANDARD + case GCC_OPTIMIZATION_LEVEL + case GENERATE_PRELINK_OBJECT_FILE + case INDEX_ENABLE_DATA_STORE + case INFOPLIST_FILE + case IPHONEOS_DEPLOYMENT_TARGET + case KEEP_PRIVATE_EXTERNS + case CLANG_COVERAGE_MAPPING_LINKER_ARGS + case MACH_O_TYPE + case MACOSX_DEPLOYMENT_TARGET + case MODULEMAP_FILE + case MODULEMAP_FILE_CONTENTS + case MODULEMAP_PATH + case MODULE_CACHE_DIR + case ONLY_ACTIVE_ARCH + case PACKAGE_RESOURCE_BUNDLE_NAME + case PACKAGE_RESOURCE_TARGET_KIND + case PRODUCT_BUNDLE_IDENTIFIER + case PRODUCT_MODULE_NAME + case PRODUCT_NAME + case PROJECT_NAME + case SDKROOT + case SDK_VARIANT + case SKIP_INSTALL + case INSTALL_PATH + case SUPPORTS_MACCATALYST + case SWIFT_SERIALIZE_DEBUGGING_OPTIONS + case SWIFT_INSTALL_OBJC_HEADER + case SWIFT_OBJC_INTERFACE_HEADER_NAME + case SWIFT_OBJC_INTERFACE_HEADER_DIR + case SWIFT_OPTIMIZATION_LEVEL + case SWIFT_VERSION + case SYMBOL_GRAPH_EXTRACTOR_OUTPUT_DIR + case TARGET_NAME + case TARGET_TEMP_DIR_SUFFIX + case TARGET_BUILD_DIR + case TVOS_DEPLOYMENT_TARGET + case USE_HEADERMAP + case USES_SWIFTPM_UNSAFE_FLAGS + case WATCHOS_DEPLOYMENT_TARGET + case XROS_DEPLOYMENT_TARGET + case _IOS_DEPLOYMENT_TARGET + case MARKETING_VERSION + case CURRENT_PROJECT_VERSION + case SWIFT_EMIT_MODULE_INTERFACE + case GENERATE_RESOURCE_ACCESSORS + case BUILD_LIBRARY_FOR_DISTRIBUTION + case BUILD_PACKAGE_FOR_DISTRIBUTION + case COMPILER_WORKING_DIRECTORY + case COREML_CODEGEN_LANGUAGE + case COREML_COMPILER_CONTAINER + case EXECUTABLE_NAME + case GENERATE_EMBED_IN_CODE_ACCESSORS + case INSTALLAPI_COPY_PHASE + case INSTALLHDRS_COPY_PHASE + case IS_ZIPPERED + case PACKAGE_REGISTRY_SIGNATURE + case PACKAGE_TARGET_NAME_CONFLICTS_WITH_PRODUCT_NAME + case SKIP_BUILDING_DOCUMENTATION + case SKIP_CLANG_STATIC_ANALYZER + case STRIP_INSTALLED_PRODUCT + case SUPPORTS_TEXT_BASED_API + case SUPPRESS_WARNINGS + case SWIFT_TREAT_WARNINGS_AS_ERRORS + case SWIFT_COMPILE_FOR_STATIC_LINKING + case SWIFT_DISABLE_PARSE_AS_LIBRARY + case SWIFT_ENABLE_BARE_SLASH_REGEX + case SWIFT_INDEX_STORE_ENABLE + case SWIFT_INSTALL_MODULE + case SWIFT_LIBRARIES_ONLY + case SWIFT_PACKAGE_NAME + case SWIFT_USER_MODULE_VERSION + case TAPI_DYLIB_INSTALL_NAME + case TAPI_EXTRACT_API_OUTPUT_DIR + case TARGETED_DEVICE_FAMILY + case LINKER_DRIVER + case LIBTOOL_NO_WARNING_FOR_NO_SYMBOLS + case LD_WARN_DUPLICATE_LIBRARIES + + // @available(*, deprecated, renamed: "GENERATE_PRELINK_OBJECT_FILE") // can't add @available because it breaks CaseIterable + case GENERATE_MASTER_OBJECT_FILE // ignore-unacceptable-language + } + + public enum MultipleValueSetting: String, CaseIterable, Sendable, Hashable, Codable { + case ARCHS + case BUILD_SERVER_PROTOCOL_TARGET_TAGS + case EMBED_PACKAGE_RESOURCE_BUNDLE_NAMES + case FRAMEWORK_SEARCH_PATHS + case GCC_PREPROCESSOR_DEFINITIONS + case HEADER_SEARCH_PATHS + case LD_RUNPATH_SEARCH_PATHS + case LIBRARY_SEARCH_PATHS + case OTHER_CFLAGS + case OTHER_CPLUSPLUSFLAGS + case OTHER_LDFLAGS + case OTHER_LDRFLAGS + case OTHER_SWIFT_FLAGS + case PRELINK_FLAGS + case SPECIALIZATION_SDK_OPTIONS + case SUPPORTED_PLATFORMS + case SWIFT_ACTIVE_COMPILATION_CONDITIONS + case SWIFT_DISABLE_COMPILATION_FOR_STATIC_LINKING_WHEN_ANY_TARGET_IS_PROMOTED_TO_DYNAMIC + case SWIFT_IMPLEMENTS_MACROS_FOR_MODULE_NAMES + case SWIFT_MODULE_ALIASES + case SWIFT_SDK_TOOLSETS + case SWIFT_WARNINGS_AS_WARNINGS_GROUPS + case SWIFT_WARNINGS_AS_ERRORS_GROUPS + } + + public enum Platform: Hashable, CaseIterable, Sendable { + case macOS + case macCatalyst + case iOS + + // Special case to support arm64e-only iOS app builds with SwiftPM dependencies. Don't use it for anything else! + case _iOSDevice + + case tvOS + case watchOS + case xrOS + + case driverKit + + case linux + case android + case windows + case wasi + case openbsd + case freebsd + + public var asConditionStrings: [String] { + let filters = self.toPlatformFilter().map { (filter: XPIF.PlatformFilter) -> String in + if filter.environment.isEmpty { + return filter.platform + } else { + return "\(filter.platform)-\(filter.environment)" + } + }.sorted() + return ["__platform_filter=\(filters.joined(separator: ";"))"] + } + } + + /// Keys are stored verbatim, including arbitrary Xcode conditions and plugin settings. + public var values: [String: StringList] = [:] + + public init() {} + + public subscript(_ setting: SingleValueSetting) -> String? { + get { self[single: setting.rawValue] } + set { self[single: setting.rawValue] = newValue } + } + + public subscript(single setting: String) -> String? { + get { + guard case .string(let value) = values[setting] else { return nil } + return value + } + set { values[setting] = newValue.map(StringList.string) } + } + + public subscript(_ setting: MultipleValueSetting) -> [String]? { + get { self[multiple: setting.rawValue] } + set { self[multiple: setting.rawValue] = newValue } + } + + public subscript(multiple setting: String) -> [String]? { + get { + guard case .array(let value) = values[setting] else { return nil } + return value + } + set { values[setting] = newValue.map(StringList.array) } + } + + public subscript(_ setting: SingleValueSetting, platform: Platform) -> String? { + get { self[single: "\(setting.rawValue)[\(platform.asConditionStrings[0])]"] } + set { self[single: "\(setting.rawValue)[\(platform.asConditionStrings[0])]"] = newValue } + } + + public subscript(_ setting: MultipleValueSetting, platform: Platform) -> [String]? { + get { self[multiple: "\(setting.rawValue)[\(platform.asConditionStrings[0])]"] } + set { self[multiple: "\(setting.rawValue)[\(platform.asConditionStrings[0])]"] = newValue } + } + } +} + + +public extension XPIF.BuildSettings.Platform { + func toPlatformFilter() -> [XPIF.PlatformFilter] { + var result: [XPIF.PlatformFilter] = [] + + switch self { + case .macOS: + result.append(.init(platform: "macos")) + + case .macCatalyst: + result.append(.init(platform: "ios", environment: "maccatalyst")) + + case .iOS: + result.append(.init(platform: "ios")) + result.append(.init(platform: "ios", environment: "simulator")) + + case ._iOSDevice: + result.append(.init(platform: "ios")) + + case .tvOS: + result.append(.init(platform: "tvos")) + result.append(.init(platform: "tvos", environment: "simulator")) + + case .watchOS: + result.append(.init(platform: "watchos")) + result.append(.init(platform: "watchos", environment: "simulator")) + + case .xrOS: + result.append(.init(platform: "xros")) + result.append(.init(platform: "xros", environment: "simulator")) + + case .driverKit: + result.append(.init(platform: "driverkit")) + + case .linux: + result.append(.init(platform: "linux", environment: "gnu")) + result.append(.init(platform: "linux", environment: "musl")) + + case .android: + result.append(.init(platform: "linux", environment: "android")) + result.append(.init(platform: "linux", environment: "androideabi")) + + case .windows: + result.append(.init(platform: "windows", environment: "msvc")) + + case .wasi: + result.append(.init(platform: "wasi")) + result.append(.init(platform: "wasip1")) + result.append(.init(platform: "wasi", environment: "threads")) + result.append(.init(platform: "wasip1", environment: "threads")) + + case .openbsd: + result.append(.init(platform: "openbsd")) + + case .freebsd: + result.append(.init(platform: "freebsd")) + } + return result + } +} + +extension XPIF.BuildSettings: Codable { + public init(from decoder: any Decoder) throws { + values = try decoder.singleValueContainer().decode([String: XPIF.StringList].self) + } + + public func encode(to encoder: any Encoder) throws { + var container = encoder.singleValueContainer() + try container.encode(values) + } +} diff --git a/Sources/XPIF/Container.swift b/Sources/XPIF/Container.swift new file mode 100644 index 00000000..04bafc55 --- /dev/null +++ b/Sources/XPIF/Container.swift @@ -0,0 +1,377 @@ +// Adapted from swift-package-manager/SwiftBuildSupport/PIF.swift: mutable Xcode PIF support and lossless JSON fields. + +import Foundation +import Crypto +import OrderedCollections + +extension XPIF { + public struct Container: Sendable { + public var workspace: Workspace + + public init(workspace: Workspace) { + self.workspace = workspace + } + + /// The high-level PIF *workspace* object. + public struct Workspace: HighLevelObject { + fileprivate static var type: String { "workspace" } + + public let id: XPIF.GUID + public var name: String + public var path: String + public var additionalFields: [String: JSONValue] = [:] + public var projects: [XPIF.Project] { + get { projectsWithSignature.map(\.underlying) } + set { projectsWithSignature = newValue.map { Container.Project(wrapping: $0) } } + } + + fileprivate var projectsWithSignature: [Container.Project] + fileprivate var projectSignatures: [String] + fileprivate var signature: String? + + public init(id: XPIF.GUID, name: String, path: String, projects: [XPIF.Project]) { + precondition(!id.value.isEmpty) + precondition(!name.isEmpty) + precondition(Set(projects.map(\.id)).count == projects.count) + + self.id = id + self.name = name + self.path = path + + self.projectsWithSignature = [] + self.projectSignatures = [] + self.projects = projects + } + + public init(from decoder: any Decoder) throws { + let container = try decoder.container(keyedBy: Container.CodingKeys.self) + try Self.validateType(in: container) + signature = try container.decode(String.self, forKey: .signature) + + let contents = try container.nestedContainer(keyedBy: CodingKeys.self, forKey: .contents) + id = try contents.decode(XPIF.GUID.self, forKey: .guid) + name = try contents.decode(String.self, forKey: .name) + path = try contents.decode(String.self, forKey: .path) + projectSignatures = try contents.decode([String].self, forKey: .projects) + projectsWithSignature = [] + additionalFields = try decodeAdditionalFields(from: container.superDecoder(forKey: .contents), excluding: CodingKeys.self) + } + + private enum CodingKeys: String, CodingKey, CaseIterable { + case guid, name, path, projects + } + + fileprivate mutating func sign() throws { + for projectIndex in projects.indices { + try projectsWithSignature[projectIndex].sign() + } + self.signature = try Container.signature(of: self) + } + + fileprivate func encodeContents(to encoder: Encoder) throws { + var superContainer = encoder.container(keyedBy: Container.CodingKeys.self) + let contentsEncoder = superContainer.superEncoder(forKey: .contents) + try encodeAdditionalFields(additionalFields, to: contentsEncoder, excluding: CodingKeys.self) + var contents = contentsEncoder.container(keyedBy: CodingKeys.self) + + try contents.encode(id, forKey: .guid) + try contents.encode(name, forKey: .name) + try contents.encode(path, forKey: .path) + try contents.encode(projectsWithSignature.map(\.signature), forKey: .projects) + + if encoder.userInfo.keys.contains(.encodeForSwiftBuild) { + guard let signature else { + throw Errors.missingSignature(type: Self.type) + } + try superContainer.encode(signature, forKey: .signature) + } + } + } + + /// Decodes a `Container` from PIF JSON. + public init(data: Data) throws { + let objects = try JSONDecoder().decode([TopLevelObject.Entry].self, from: data) + var workspace: Workspace? + var projects: [String: Project] = [:] + var targets: OrderedDictionary = [:] + + for object in objects { + switch object { + case .workspace(let value): + guard workspace == nil else { + throw Self.invalidPIF("PIF contains more than one workspace") + } + workspace = value + case .project(let value): + guard let signature = value.signature, projects[signature] == nil else { + throw Self.invalidPIF("PIF contains a project with a missing or duplicate signature") + } + projects[signature] = value + case .target(let value): + guard let signature = value.underlying.common.signature, targets[signature] == nil else { + throw Self.invalidPIF("PIF contains a target with a missing or duplicate signature") + } + targets[signature] = value.underlying + } + } + + guard var workspace else { + throw Self.invalidPIF("PIF is missing a workspace") + } + workspace.projectsWithSignature = try workspace.projectSignatures.map { signature in + guard var project = projects[signature] else { + throw Self.invalidPIF("PIF is missing project with signature \(signature)") + } + project.underlying.targets = try project.targetSignatures.map { signature in + guard let target = targets[signature] else { + throw Self.invalidPIF("PIF is missing target with signature \(signature)") + } + return target + } + let projectSignatures = Set(project.targetSignatures) + let serializedIDs = targets.filter { projectSignatures.contains($0.key) }.values.map(\.id) + if serializedIDs != project.underlying.targets.map(\.id) { + project.underlying.targetSerializationOrder = serializedIDs + } + return project + } + self.workspace = workspace + } + + private static func invalidPIF(_ description: String) -> DecodingError { + .dataCorrupted(.init(codingPath: [], debugDescription: description)) + } + + /// Encodes the receiver as PIF JSON data. + public func data() throws -> Data { + var workspace = workspace + try workspace.sign() + let topLevel = TopLevelObject(workspace: workspace) + return try Self.swiftBuildEncoder.encode(topLevel) + } + + public enum Errors: Swift.Error, CustomStringConvertible { + case missingSignature(type: String) + + public var description: String { + switch self { + case .missingSignature(let type): + return "Expected to have \(type) *signature* when encoding for SwiftBuild" + } + } + } + + fileprivate static func makeEncoder(forSwiftBuild: Bool) -> JSONEncoder { + let encoder = JSONEncoder() + encoder.outputFormatting = [.sortedKeys] + if forSwiftBuild { + encoder.userInfo[.encodeForSwiftBuild] = true + } + return encoder + } + fileprivate static let swiftBuildEncoder = makeEncoder(forSwiftBuild: true) + fileprivate static let standardEncoder = makeEncoder(forSwiftBuild: false) + + /// The top-level PIF object. + fileprivate struct TopLevelObject: Encodable { + let workspace: Workspace + + enum Entry: Decodable { + case workspace(Workspace) + case project(Project) + case target(Target) + + init(from decoder: any Decoder) throws { + let container = try decoder.container(keyedBy: Container.CodingKeys.self) + switch try container.decode(String.self, forKey: .type) { + case "workspace": self = .workspace(try Workspace(from: decoder)) + case "project": self = .project(try Project(from: decoder)) + case "target": self = .target(try Target(from: decoder)) + default: + throw DecodingError.dataCorruptedError(forKey: .type, in: container, debugDescription: "Unknown PIF object type") + } + } + } + + func encode(to encoder: Encoder) throws { + var container = encoder.unkeyedContainer() + + // Encode the workspace. + try container.encode(workspace) + + // Encode the projects and their targets. + for project in workspace.projectsWithSignature { + try container.encode(project) + + var targets = project.underlying.targets + if let order = project.underlying.targetSerializationOrder { + let ranks = Dictionary(uniqueKeysWithValues: order.enumerated().map { ($0.element, $0.offset) }) + targets.sort { ranks[$0.id, default: .max] < ranks[$1.id, default: .max] } + } + for target in targets { + try container.encode(Target(wrapping: target)) + } + } + } + } + + fileprivate enum CodingKeys: CodingKey { + case type + case signature, contents // Used by subclasses. + } + + /// Represents a high-level PIF object. + /// + /// For instance, a JSON serialized *workspace* might look like this: + /// ```json + /// { + /// "type" : "workspace", + /// "signature" : "22e9436958aec481799", + /// "contents" : { + /// "guid" : "Workspace:/Users/foo/BarPackage", + /// "name" : "BarPackage", + /// "path" : "/Users/foo/BarPackage", + /// "projects" : [ + /// "70a588f37dcfcddbc1f", + /// "c1d9cb257bd42cafbb8" + /// ] + /// } + /// } + /// ``` + fileprivate protocol HighLevelObject: Codable, Sendable { + static var type: String { get } + func encodeContents(to encoder: Encoder) throws + } + + /// A high-level PIF *project* object. + fileprivate struct Project: HighLevelObject { + static var type: String { "project" } + + var underlying: XPIF.Project + var signature: String? + var targetSignatures: [String] = [] + var id: XPIF.GUID { underlying.id } + + init(wrapping underlying: XPIF.Project) { + precondition(!underlying.name.isEmpty) + precondition(!underlying.id.value.isEmpty) + precondition(!underlying.path.isEmpty) + precondition(!underlying.projectDir.isEmpty) + + precondition(Set(underlying.targets.map(\.id)).count == underlying.targets.count) + precondition(Set(underlying.buildConfigs.map(\.id)).count == underlying.buildConfigs.count) + + self.underlying = underlying + } + + init(from decoder: any Decoder) throws { + let container = try decoder.container(keyedBy: Container.CodingKeys.self) + try Self.validateType(in: container) + signature = try container.decode(String.self, forKey: .signature) + let contents = try container.nestedContainer(keyedBy: TargetCodingKeys.self, forKey: .contents) + targetSignatures = try contents.decode([String].self, forKey: .targets) + underlying = try container.decode(XPIF.Project.self, forKey: .contents) + } + + private enum TargetCodingKeys: CodingKey { + case targets + } + + mutating func sign() throws { + for targetIndex in self.underlying.targets.indices { + try self.underlying.targets[targetIndex].sign() + } + self.signature = try Container.signature(of: self) + } + + func encodeContents(to encoder: any Encoder) throws { + var superContainer = encoder.container(keyedBy: CodingKeys.self) + try superContainer.encode(underlying, forKey: .contents) + + if encoder.userInfo.keys.contains(.encodeForSwiftBuild) { + guard let signature else { + throw Container.Errors.missingSignature(type: Self.type) + } + try superContainer.encode(signature, forKey: .signature) + } + } + } + + /// A high-level PIF *target* object. + fileprivate struct Target: HighLevelObject { + static var type: String { "target" } + + var underlying: XPIF.BaseTarget + var id: XPIF.GUID { underlying.id } + + init(wrapping underlying: XPIF.BaseTarget) { + precondition(!underlying.id.value.isEmpty) + precondition(!underlying.common.name.isEmpty) + + self.underlying = underlying + } + + init(from decoder: any Decoder) throws { + let container = try decoder.container(keyedBy: Container.CodingKeys.self) + try Self.validateType(in: container) + let signature = try container.decode(String.self, forKey: .signature) + underlying = try container.decode(XPIF.BaseTarget.self, forKey: .contents) + underlying.common.signature = signature + } + + func encodeContents(to encoder: any Encoder) throws { + var superContainer = encoder.container(keyedBy: Container.CodingKeys.self) + try superContainer.encode(underlying, forKey: .contents) + + if encoder.userInfo.keys.contains(.encodeForSwiftBuild) { + guard let signature = underlying.common.signature else { + throw Container.Errors.missingSignature(type: Self.type) + } + try superContainer.encode(signature, forKey: .signature) + } + } + } + + fileprivate static func signature(of obj: some Encodable) throws -> String { + let signatureContent = try standardEncoder.encode(obj) + return SHA256.hash(data: signatureContent) + .map { String(format: "%02X", $0) } + .joined() + } + } +} + +extension XPIF.BaseTarget { + fileprivate mutating func sign() throws { + self.common.signature = try XPIF.Container.signature(of: self) + } +} + +extension CodingUserInfoKey { + /// Perform the encoding for SwiftBuild consumption. + fileprivate static let encodeForSwiftBuild: CodingUserInfoKey = CodingUserInfoKey(rawValue: "encodeForXCBuild")! +} + +extension XPIF.Container.HighLevelObject { + fileprivate static func validateType(in container: KeyedDecodingContainer) throws { + let expected = Self.type + let type = try container.decode(String.self, forKey: .type) + guard type == expected else { + throw DecodingError.dataCorruptedError( + forKey: .type, + in: container, + debugDescription: "Expected \(expected), got \(type)" + ) + } + } + + public func encode(to encoder: any Encoder) throws { + var container = encoder.container(keyedBy: XPIF.Container.CodingKeys.self) + try container.encode(Self.type, forKey: .type) + try self.encodeContents(to: encoder) + } +} + +extension XPIF { + public typealias Workspace = Container.Workspace +} diff --git a/Sources/XPIF/CustomTask.swift b/Sources/XPIF/CustomTask.swift new file mode 100644 index 00000000..939466cc --- /dev/null +++ b/Sources/XPIF/CustomTask.swift @@ -0,0 +1,103 @@ +// Adapted from SwiftBuild/CustomTask.swift: mutable Xcode PIF support and lossless JSON fields. + +import Foundation + +extension XPIF { + public struct Pair { + public var first: First + public var second: Second + + public init(_ first: First, _ second: Second) { + self.first = first + self.second = second + } + } +} + +extension XPIF.Pair: Equatable where First: Equatable, Second: Equatable {} +extension XPIF.Pair: Hashable where First: Hashable, Second: Hashable {} +extension XPIF.Pair: Sendable where First: Sendable, Second: Sendable {} +extension XPIF.Pair: Comparable where First: Comparable, Second: Comparable { + public static func < (lhs: Self, rhs: Self) -> Bool { + (lhs.first, lhs.second) < (rhs.first, rhs.second) + } +} + +extension XPIF { + public struct CustomTask: Hashable, Sendable { + public var additionalFields: [String: JSONValue] = [:] + public var commandLine: [String] + public var environment: [Pair] + public var workingDirectory: String? + public var executionDescription: String + public var inputFilePaths: [String] + public var outputFilePaths: [String] + public var enableSandboxing: Bool + public var preparesForIndexing: Bool + + public init( + commandLine: [String], + environment: [Pair], + workingDirectory: String?, + executionDescription: String, + inputFilePaths: [String], + outputFilePaths: [String], + enableSandboxing: Bool, + preparesForIndexing: Bool + ) { + self.commandLine = commandLine + self.environment = environment + self.workingDirectory = workingDirectory + self.executionDescription = executionDescription + self.inputFilePaths = inputFilePaths + self.outputFilePaths = outputFilePaths + self.enableSandboxing = enableSandboxing + self.preparesForIndexing = preparesForIndexing + } + } +} + +extension XPIF.CustomTask: Codable { + public init(from decoder: any Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + self.commandLine = try container.decode([String].self, forKey: .commandLine) + let environment = try container.decode([[String]].self, forKey: .environment) + self.environment = environment.compactMap { strs in + guard let k = strs.first, let v = strs.dropFirst().first else { + return nil + } + return XPIF.Pair(k, v) + } + self.workingDirectory = try container.decodeIfPresent(String.self, forKey: .workingDirectory) + self.executionDescription = try container.decode(String.self, forKey: .executionDescription) + self.inputFilePaths = try container.decode([String].self, forKey: .inputFilePaths) + self.outputFilePaths = try container.decode([String].self, forKey: .outputFilePaths) + self.enableSandboxing = try container.decode(String.self, forKey: .enableSandboxing) == "true" + self.preparesForIndexing = try container.decode(String.self, forKey: .preparesForIndexing) == "true" + self.additionalFields = try decodeAdditionalFields(from: decoder, excluding: CodingKeys.self) + } + + public func encode(to encoder: any Encoder) throws { + try encodeAdditionalFields(self.additionalFields, to: encoder, excluding: CodingKeys.self) + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(self.commandLine, forKey: .commandLine) + try container.encode(self.environment.map { [$0.first, $0.second] }, forKey: .environment) + try container.encodeIfPresent(self.workingDirectory, forKey: .workingDirectory) + try container.encode(self.executionDescription, forKey: .executionDescription) + try container.encode(self.inputFilePaths, forKey: .inputFilePaths) + try container.encode(self.outputFilePaths, forKey: .outputFilePaths) + try container.encode(self.enableSandboxing ? "true" : "false", forKey: .enableSandboxing) + try container.encode(self.preparesForIndexing ? "true" : "false", forKey: .preparesForIndexing) + } + + enum CodingKeys: String, CodingKey, CaseIterable { + case commandLine + case environment + case workingDirectory + case executionDescription + case inputFilePaths + case outputFilePaths + case enableSandboxing + case preparesForIndexing + } +} diff --git a/Sources/XPIF/ExternalTarget.swift b/Sources/XPIF/ExternalTarget.swift new file mode 100644 index 00000000..44b660fc --- /dev/null +++ b/Sources/XPIF/ExternalTarget.swift @@ -0,0 +1,63 @@ +import Foundation + +extension XPIF { + public struct ExternalTarget: Common, Identifiable, Sendable, Hashable, Codable { + public var common: TargetCommon + public var id: GUID { common.id } + public var toolPath: StringList + public var arguments: StringList + public var workingDirectory: StringList + public var passBuildSettingsInEnvironment: Bool + + public init( + id: GUID, + name: String, + toolPath: StringList, + arguments: StringList = .string(""), + workingDirectory: StringList = .string(""), + passBuildSettingsInEnvironment: Bool = true + ) { + self.common = .init(id: id, name: name) + self.toolPath = toolPath + self.arguments = arguments + self.workingDirectory = workingDirectory + self.passBuildSettingsInEnvironment = passBuildSettingsInEnvironment + } + + public init(from decoder: any Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + common = .init( + id: try container.decode(GUID.self, forKey: .guid), + name: try container.decode(String.self, forKey: .name), + buildConfigs: try container.decodeIfPresent([BuildConfig].self, forKey: .buildConfigurations) ?? [], + dependencies: try container.decodeIfPresent([TargetDependency].self, forKey: .dependencies) ?? [], + customTasks: try container.decodeIfPresent([CustomTask].self, forKey: .customTasks) ?? [] + ) + toolPath = try container.decode(StringList.self, forKey: .toolPath) + arguments = try container.decodeIfPresent(StringList.self, forKey: .arguments) ?? .string("") + workingDirectory = try container.decodeIfPresent(StringList.self, forKey: .workingDirectory) ?? .string("") + passBuildSettingsInEnvironment = try container.decodePIFBool(forKey: .passBuildSettingsInEnvironment, default: true) + common.additionalFields = try decodeAdditionalFields(from: decoder, excluding: CodingKeys.self) + } + + public func encode(to encoder: any Encoder) throws { + try encodeAdditionalFields(common.additionalFields, to: encoder, excluding: CodingKeys.self) + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode("external", forKey: .type) + try container.encode(id, forKey: .guid) + try container.encode(common.name, forKey: .name) + try container.encode(common.buildConfigs, forKey: .buildConfigurations) + try container.encode(common.dependencies, forKey: .dependencies) + if !common.customTasks.isEmpty { try container.encode(common.customTasks, forKey: .customTasks) } + try container.encode(toolPath, forKey: .toolPath) + try container.encode(arguments, forKey: .arguments) + try container.encode(workingDirectory, forKey: .workingDirectory) + try container.encode(passBuildSettingsInEnvironment ? "true" : "false", forKey: .passBuildSettingsInEnvironment) + } + + private enum CodingKeys: String, CodingKey, CaseIterable { + case type, guid, name, buildConfigurations, dependencies, customTasks + case toolPath, arguments, workingDirectory, passBuildSettingsInEnvironment + } + } +} diff --git a/Sources/XPIF/ImpartedBuildProperties.swift b/Sources/XPIF/ImpartedBuildProperties.swift new file mode 100644 index 00000000..63a3eead --- /dev/null +++ b/Sources/XPIF/ImpartedBuildProperties.swift @@ -0,0 +1,32 @@ +// Adapted from SwiftBuild/ImpartedBuildProperties.swift: mutable Xcode PIF support and lossless JSON fields. + +import Foundation + +extension XPIF { + public struct ImpartedBuildProperties: Sendable, Hashable { + public var additionalFields: [String: JSONValue] = [:] + public var settings: BuildSettings + + public init(settings: BuildSettings) { + self.settings = settings + } + } +} + +extension XPIF.ImpartedBuildProperties: Codable { + public init(from decoder: any Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + self.settings = try container.decode(XPIF.BuildSettings.self, forKey: .buildSettings) + self.additionalFields = try decodeAdditionalFields(from: decoder, excluding: CodingKeys.self) + } + + public func encode(to encoder: any Encoder) throws { + try encodeAdditionalFields(self.additionalFields, to: encoder, excluding: CodingKeys.self) + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(settings, forKey: .buildSettings) + } + + enum CodingKeys: String, CodingKey, CaseIterable { + case buildSettings + } +} diff --git a/Sources/XPIF/JSONValue.swift b/Sources/XPIF/JSONValue.swift new file mode 100644 index 00000000..38d00c48 --- /dev/null +++ b/Sources/XPIF/JSONValue.swift @@ -0,0 +1,100 @@ +import Foundation + +extension XPIF { + /// JSON fields not yet represented by the typed model survive decoding, editing, and encoding. + public enum JSONValue: Sendable, Hashable, Codable { + case null + case bool(Bool) + case number(Decimal) + case string(String) + case array([JSONValue]) + case object([String: JSONValue]) + + public init(from decoder: any Decoder) throws { + let container = try decoder.singleValueContainer() + if container.decodeNil() { + self = .null + } else if let value = try? container.decode(Bool.self) { + self = .bool(value) + } else if let value = try? container.decode(String.self) { + self = .string(value) + } else if let value = try? container.decode(Decimal.self) { + self = .number(value) + } else if let value = try? container.decode([JSONValue].self) { + self = .array(value) + } else { + self = .object(try container.decode([String: JSONValue].self)) + } + } + + public func encode(to encoder: any Encoder) throws { + var container = encoder.singleValueContainer() + switch self { + case .null: try container.encodeNil() + case .bool(let value): try container.encode(value) + case .number(let value): try container.encode(value) + case .string(let value): try container.encode(value) + case .array(let value): try container.encode(value) + case .object(let value): try container.encode(value) + } + } + } +} + +struct StringKey: CodingKey { + let stringValue: String + var intValue: Int? { nil } + + init(_ value: String) { stringValue = value } + init(stringValue: String) { self.stringValue = stringValue } + init?(intValue: Int) { return nil } +} + +func decodeAdditionalFields( + from decoder: any Decoder, excluding: Keys.Type +) throws -> [String: XPIF.JSONValue] { + let known = Set(Keys.allCases.map(\.stringValue)) + let container = try decoder.container(keyedBy: StringKey.self) + var fields: [String: XPIF.JSONValue] = [:] + for key in container.allKeys where !known.contains(key.stringValue) { + fields[key.stringValue] = try container.decode(XPIF.JSONValue.self, forKey: key) + } + return fields +} + +func encodeAdditionalFields( + _ fields: [String: XPIF.JSONValue], to encoder: any Encoder, excluding: Keys.Type +) throws { + let known = Set(Keys.allCases.map(\.stringValue)) + var container = encoder.container(keyedBy: StringKey.self) + for (key, value) in fields where !known.contains(key) { + try container.encode(value, forKey: StringKey(key)) + } +} + +extension KeyedDecodingContainer { + /// The legacy PIF wire format uses strings for booleans. + func decodePIFBool(forKey key: Key, default defaultValue: Bool = false) throws -> Bool { + guard contains(key) else { return defaultValue } + let value = try decode(String.self, forKey: key) + switch value { + case "true": return true + case "false": return false + default: + throw DecodingError.dataCorruptedError(forKey: key, in: self, debugDescription: "Expected true or false") + } + } +} + +// Keep explicit default fields emitted by SwiftPM, while allowing Xcode's sparse form. +func decodeExplicitDefaults(from decoder: any Decoder, defaults: [String: XPIF.JSONValue]) throws -> [String: XPIF.JSONValue] { + let container = try decoder.container(keyedBy: StringKey.self) + return try defaults.filter { key, value in + try container.decodeIfPresent(XPIF.JSONValue.self, forKey: StringKey(key)) == value + } +} + +func encodeExplicitDefaults(_ defaults: [String: XPIF.JSONValue], to encoder: any Encoder) throws { + var container = encoder.container(keyedBy: StringKey.self) + for (key, value) in defaults { try container.encode(value, forKey: StringKey(key)) } +} diff --git a/Sources/XPIF/LICENSE.txt b/Sources/XPIF/LICENSE.txt new file mode 100644 index 00000000..61b0c781 --- /dev/null +++ b/Sources/XPIF/LICENSE.txt @@ -0,0 +1,211 @@ + Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright [yyyy] [name of copyright owner] + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + + + +## Runtime Library Exception to the Apache 2.0 License: ## + + + As an exception, if you use this Software to compile your source code and + portions of this Software are embedded into the binary product as a result, + you may redistribute such product without providing attribution as would + otherwise be required by Sections 4(a), 4(b) and 4(d) of the License. diff --git a/Sources/XPIF/PlatformFilter.swift b/Sources/XPIF/PlatformFilter.swift new file mode 100644 index 00000000..8bf5addd --- /dev/null +++ b/Sources/XPIF/PlatformFilter.swift @@ -0,0 +1,43 @@ +// Adapted from SwiftBuild/PlatformFilter.swift: mutable Xcode PIF support and lossless JSON fields. + +import Foundation + +extension XPIF { + public struct PlatformFilter: Hashable, Sendable, Comparable { + public var additionalFields: [String: JSONValue] = [:] + public var platform: String + public var environment: String + + public init(platform: String, environment: String = "") { + self.platform = platform + self.environment = environment + } + + public static func < (lhs: XPIF.PlatformFilter, rhs: XPIF.PlatformFilter) -> Bool { + return (lhs.platform, lhs.environment) < (rhs.platform, rhs.environment) + } + } +} + +extension XPIF.PlatformFilter: Codable { + public init(from decoder: any Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + self.platform = try container.decode(String.self, forKey: .platform) + self.environment = try container.decodeIfPresent(String.self, forKey: .environment) ?? "" + self.additionalFields = try decodeAdditionalFields(from: decoder, excluding: CodingKeys.self) + } + + public func encode(to encoder: any Encoder) throws { + try encodeAdditionalFields(self.additionalFields, to: encoder, excluding: CodingKeys.self) + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(self.platform, forKey: .platform) + if !self.environment.isEmpty { + try container.encode(self.environment, forKey: .environment) + } + } + + enum CodingKeys: String, CodingKey, CaseIterable { + case platform + case environment + } +} diff --git a/Sources/XPIF/ProductReference.swift b/Sources/XPIF/ProductReference.swift new file mode 100644 index 00000000..4b4da499 --- /dev/null +++ b/Sources/XPIF/ProductReference.swift @@ -0,0 +1,36 @@ +import Foundation + +extension XPIF { + /// A target's product reference has an identity independent of its target GUID. + public struct ProductReference: Sendable, Hashable, Codable { + public var id: GUID + public var name: String + public var type: String = "product" + public var additionalFields: [String: JSONValue] = [:] + + public init(id: GUID, name: String) { + self.id = id + self.name = name + } + + public init(from decoder: any Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + id = try container.decode(GUID.self, forKey: .guid) + name = try container.decode(String.self, forKey: .name) + type = try container.decodeIfPresent(String.self, forKey: .type) ?? "product" + additionalFields = try decodeAdditionalFields(from: decoder, excluding: CodingKeys.self) + } + + public func encode(to encoder: any Encoder) throws { + try encodeAdditionalFields(additionalFields, to: encoder, excluding: CodingKeys.self) + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(type, forKey: .type) + try container.encode(id, forKey: .guid) + try container.encode(name, forKey: .name) + } + + private enum CodingKeys: String, CodingKey, CaseIterable { + case type, guid, name + } + } +} diff --git a/Sources/XPIF/Project.swift b/Sources/XPIF/Project.swift new file mode 100644 index 00000000..67c62a7c --- /dev/null +++ b/Sources/XPIF/Project.swift @@ -0,0 +1,236 @@ +// Adapted from SwiftBuild/Project.swift: mutable Xcode PIF support and lossless JSON fields. + +import Foundation + +extension XPIF { + /// A PIF project, consisting of a tree of groups and file references, a list of targets, and some additional information. + public struct Project: Sendable, Hashable { + public let id: GUID + public var name: String + public var developmentRegion: String? + public var path: String + public var mainGroup: Group + public var buildConfigs: [BuildConfig] + public var projectDir: String + public var targets: [BaseTarget] + /// Object order can differ from the order of target references in a project. + public var targetSerializationOrder: [GUID]? = nil + public var isPackage: Bool + public var defaultConfigurationName: String = "Release" + public var knownRegions: [String]? = nil + public var classPrefix: String? = nil + public var appPreferencesBuildSettings: BuildSettings? = nil + public var additionalFields: [String: JSONValue] = [:] + + public init( + id: GUID, + path: String, + projectDir: String, + name: String, + mainGroup: Group? = nil, + buildConfigs: [BuildConfig] = [], + targets: [BaseTarget] = [], + developmentRegion: String? = nil, + isPackage: Bool = false + ) { + precondition(!id.value.isEmpty) + precondition(!path.isEmpty) + precondition(!projectDir.isEmpty) + self.id = id + self.name = name + self.developmentRegion = developmentRegion + self.path = path + self.mainGroup = mainGroup ?? Group(id: .init("\(id.value)::MAINGROUP"), path: "") + self.buildConfigs = buildConfigs + self.projectDir = projectDir + self.targets = targets + self.isPackage = isPackage + } + + private var nextTargetId: GUID { + return "\(self.id.value)::TARGET_\(targets.count)" + } + + // MARK: - Test + public mutating func withKeyPath(_ kp: WritableKeyPath, _ fn: (inout T) -> WritableKeyPath) -> WritableKeyPath { + let subkp = fn(&self[keyPath: kp]) + return kp.appending(path: subkp) + } + + // MARK: - BuildConfig + + /// Creates and adds a new empty build configuration, i.e. one that does not initially have any build settings. The name must not be empty and must not be equal to the name of any existing build configuration in the project. + @discardableResult public mutating func addBuildConfig(_ create: CreateFn) -> Tag { + let id = GUID("\(self.id.value)::BUILDCONFIG_\(buildConfigs.count)") + let buildConfig = create(id) + buildConfigs.append(buildConfig) + return .init(value: buildConfig.id) + } + + public subscript(buildConfig tag: Tag) -> BuildConfig { + get { buildConfigs[id: tag.value] } + set { buildConfigs[id: tag.value] = newValue } + } + + // MARK: - BaseTarget + + /// Creates and adds a new empty target, i.e. one that does not initially have any build phases. + /// The Target ID must be non-empty and unique within the PIF workspace; an arbitrary + /// guaranteed-to-be-unique identifier is passed in but the user may ignore it and use a + /// different one, as long as it adheres to the rules. The name must not be empty and must not + /// be equal to the name of any existing target in the project. + @discardableResult public mutating func addTarget(_ create: CreateFn) throws -> WritableKeyPath { + let target = create(nextTargetId) + guard !target.name.isEmpty else { + throw Error.emptyTargetName(targetIdentifier: target.id, productName: target.productName) + } + guard !target.id.value.isEmpty else { + throw Error.emptyTargetIdentifier(targetName: target.name, productName: target.productName) + } + guard !targets.contains(where: { $0.common.id == target.id }) else { + throw Error.duplicatedTargetIdentifier(targetIdentifier: target.id, targetName: target.name, productName: target.productName) + } + targets.append(.target(target)) + return \.[target: .init(value: target.id)] + } + + /// Adds a new aggregate target returned by the `create` closure. + /// The Target ID must be non-empty and unique within the PIF workspace; an arbitrary + /// guaranteed-to-be-unique identifier is passed in but the user may ignore it and use a + /// different one, as long as it adheres to the rules. The name must not be empty and must not + /// be equal to the name of any existing target in the project. + @discardableResult public mutating func addAggregateTarget(_ create: CreateFn) throws -> WritableKeyPath { + let target = create(nextTargetId) + guard !target.name.isEmpty else { + throw Error.emptyTargetName(targetIdentifier: target.id, productName: "") + } + guard !target.id.value.isEmpty else { + throw Error.emptyTargetIdentifier(targetName: target.name, productName: "") + } + guard !targets.contains(where: { $0.common.id == target.id }) else { + throw Error.duplicatedTargetIdentifier(targetIdentifier: target.id, targetName: target.name, productName: "") + } + targets.append(.aggregate(target)) + let tag = Tag(value: target.id) + return \.[target: tag] + } + + public subscript(target tag: Tag) -> Target { + get { + guard case let .target(t) = targets[id: tag.value] else { fatalError() } + return t + } + set { targets[id: tag.value] = .target(newValue) } + } + + public subscript(target tag: Tag) -> AggregateTarget { + get { + guard case let .aggregate(t) = targets[id: tag.value] else { fatalError() } + return t + } + set { targets[id: tag.value] = .aggregate(newValue) } + } + + public subscript(baseTarget tag: Tag) -> BaseTarget { + get { targets[id: tag.value] } + set { targets[id: tag.value] = newValue } + } + + public subscript(baseTarget tag: Tag) -> BaseTarget { + get { targets[id: tag.value] } + set { targets[id: tag.value] = newValue } + } + + public subscript(baseTarget keyPath: WritableKeyPath) -> BaseTarget { + get { .aggregate(self[keyPath: keyPath]) } + set { + switch newValue { + case .aggregate(let target): self[keyPath: keyPath] = target + default: break + } + } + } + + public subscript(baseTarget keyPath: WritableKeyPath) -> BaseTarget { + get { .target(self[keyPath: keyPath]) } + set { + switch newValue { + case .target(let target): self[keyPath: keyPath] = target + default: break + } + } + } + + public func findTarget(id: GUID) -> WritableKeyPath? { + guard targets.contains(where: { if case .target(let target) = $0 { return target.id == id }; return false }) else { + return nil + } + return \.[target: .init(value: id)] + } + } + +} + + +extension XPIF.Project: Codable { + public init(from decoder: any Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + self.id = try container.decode(XPIF.GUID.self, forKey: .guid) + self.name = try container.decodeIfPresent(String.self, forKey: .projectName) + ?? URL(fileURLWithPath: container.decode(String.self, forKey: .path)).deletingPathExtension().lastPathComponent + self.isPackage = try container.decodePIFBool(forKey: .projectIsPackage) + self.path = try container.decode(String.self, forKey: .path) + self.projectDir = try container.decode(String.self, forKey: .projectDirectory) + self.buildConfigs = try container.decode([XPIF.BuildConfig].self, forKey: .buildConfigurations) + self.mainGroup = try container.decode(XPIF.Group.self, forKey: .groupTree) + let signatures = try container.decode([String].self, forKey: .targets) + self.targets = signatures.compactMap { signature in + guard let key = CodingUserInfoKey(rawValue: signature) else { + return nil + } + return decoder.userInfo[key] as? XPIF.BaseTarget + } + self.developmentRegion = try container.decodeIfPresent(String.self, forKey: .developmentRegion) + self.defaultConfigurationName = try container.decodeIfPresent(String.self, forKey: .defaultConfigurationName) ?? "Release" + self.knownRegions = try container.decodeIfPresent([String].self, forKey: .knownRegions) + self.classPrefix = try container.decodeIfPresent(String.self, forKey: .classPrefix) + self.appPreferencesBuildSettings = try container.decodeIfPresent(XPIF.BuildSettings.self, forKey: .appPreferencesBuildSettings) + self.additionalFields = try decodeAdditionalFields(from: decoder, excluding: CodingKeys.self) + } + + public func encode(to encoder: any Encoder) throws { + try encodeAdditionalFields(self.additionalFields, to: encoder, excluding: CodingKeys.self) + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(id, forKey: .guid) + if name != URL(fileURLWithPath: path).deletingPathExtension().lastPathComponent { + try container.encode(name, forKey: .projectName) + } + if isPackage { try container.encode("true", forKey: .projectIsPackage) } + try container.encode(path, forKey: .path) + try container.encode(projectDir, forKey: .projectDirectory) + try container.encode(buildConfigs, forKey: .buildConfigurations) + try container.encode(defaultConfigurationName, forKey: .defaultConfigurationName) + try container.encode(mainGroup, forKey: .groupTree) + try container.encode(targets.compactMap { $0.common.signature }, forKey: .targets) + try container.encodeIfPresent(developmentRegion, forKey: .developmentRegion) + try container.encodeIfPresent(knownRegions, forKey: .knownRegions) + try container.encodeIfPresent(classPrefix, forKey: .classPrefix) + try container.encodeIfPresent(appPreferencesBuildSettings, forKey: .appPreferencesBuildSettings) + } + + enum CodingKeys: String, CodingKey, CaseIterable { + case guid + case projectDirectory + case projectName + case projectIsPackage + case path + case buildConfigurations + case targets + case groupTree + case defaultConfigurationName + case developmentRegion + case knownRegions + case classPrefix + case appPreferencesBuildSettings + } +} diff --git a/Sources/XPIF/ProvisioningSourceData.swift b/Sources/XPIF/ProvisioningSourceData.swift new file mode 100644 index 00000000..f3464d96 --- /dev/null +++ b/Sources/XPIF/ProvisioningSourceData.swift @@ -0,0 +1,43 @@ +// Adapted from SWBProtocol/ProvisioningSourceData.swift: Serializable -> Codable + +import Foundation + +extension XPIF { + public enum ProvisioningStyle: Int, Sendable, Hashable, Codable { + case automatic = 0 + case manual = 1 + } + + public struct ProvisioningSourceData: Sendable, Hashable, Codable { + public var configurationName: String + public var provisioningStyle: ProvisioningStyle + public var bundleIdentifierFromInfoPlist: String + public var additionalFields: [String: JSONValue] = [:] + + public init(configurationName: String, provisioningStyle: ProvisioningStyle, bundleIdentifierFromInfoPlist: String) { + self.configurationName = configurationName + self.provisioningStyle = provisioningStyle + self.bundleIdentifierFromInfoPlist = bundleIdentifierFromInfoPlist + } + + public init(from decoder: any Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + configurationName = try container.decode(String.self, forKey: .configurationName) + provisioningStyle = try container.decode(ProvisioningStyle.self, forKey: .provisioningStyle) + bundleIdentifierFromInfoPlist = try container.decode(String.self, forKey: .bundleIdentifierFromInfoPlist) + additionalFields = try decodeAdditionalFields(from: decoder, excluding: CodingKeys.self) + } + + public func encode(to encoder: any Encoder) throws { + try encodeAdditionalFields(additionalFields, to: encoder, excluding: CodingKeys.self) + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(configurationName, forKey: .configurationName) + try container.encode(provisioningStyle, forKey: .provisioningStyle) + try container.encode(bundleIdentifierFromInfoPlist, forKey: .bundleIdentifierFromInfoPlist) + } + + private enum CodingKeys: String, CodingKey, CaseIterable { + case configurationName, provisioningStyle, bundleIdentifierFromInfoPlist + } + } +} diff --git a/Sources/XPIF/README.md b/Sources/XPIF/README.md new file mode 100644 index 00000000..0d70e2f2 --- /dev/null +++ b/Sources/XPIF/README.md @@ -0,0 +1,17 @@ +# XPIF + +A model of SwiftBuild's Project Interchange Format (PIF). + +## Commandments + +### Round tripping is critical + +Decoding and re-encoding PIF JSON should preserve fields and values. `additionalFields` is your friend. Prefer RawRepresentable structs (with `static let` constants) over String enums. + +### Easy mutation + +Fields should be mutable so they're easy to modify. + +## License + +XPIF adapts parts of [swift-build](https://github.com/swiftlang/swift-build/tree/3125911a8d/Sources/SwiftBuild/ProjectModel) and [swift-package-manager](https://github.com/swiftlang/swift-package-manager/blob/28b775e7/Sources/SwiftBuildSupport/PIF.swift), and is licensed under Apache 2.0 with the Runtime Library Exception. See [LICENSE.txt](./LICENSE.txt). diff --git a/Sources/XPIF/References.swift b/Sources/XPIF/References.swift new file mode 100644 index 00000000..a77393bb --- /dev/null +++ b/Sources/XPIF/References.swift @@ -0,0 +1,323 @@ +// Adapted from SwiftBuild/References.swift: mutable Xcode PIF support and lossless JSON fields. + +import Foundation + +extension XPIF { + // MARK: - References + + /// Base enum for all items in the group hierarchy. + public enum Reference: Sendable, Hashable { + /// Determines the base path for a reference's relative path. + public enum RefPathBase: Sendable, Hashable, Codable { + case absolute + case groupDir + case buildSetting(String) + + public static let projectDir = Self.buildSetting("SOURCE_ROOT") + public static let buildDir = Self.buildSetting("BUILT_PRODUCTS_DIR") + + public var asString: String { + switch self { + case .absolute: "" + case .groupDir: "" + case .buildSetting(let name): name + } + } + + public init(from decoder: any Decoder) throws { + let value = try decoder.singleValueContainer().decode(String.self) + switch value { + case "": self = .absolute + case "": self = .groupDir + default: self = .buildSetting(value) + } + } + + public func encode(to encoder: any Encoder) throws { + var container = encoder.singleValueContainer() + try container.encode(asString) + } + } + + case file(FileReference) + case group(Group) + + public var id: GUID { + switch self { + case let .file(fref): return fref.id + case let .group(group): return group.common.id + } + } + } + + public struct ReferenceCommon: Sendable, Hashable { + public let id: GUID + public var additionalFields: [String: JSONValue] = [:] + var explicitDefaults: [String: JSONValue] = [:] + + /// Relative path of the reference. It is usually a literal, but may in fact contain build settings. + public var path: String + + /// Determines the base path for the reference's relative path. + public var pathBase: Reference.RefPathBase + + /// Name of the reference. + public var name: String + + init(id: GUID, path: String, pathBase: Reference.RefPathBase, name: String) { + self.id = id + self.path = path + self.pathBase = pathBase + self.name = name + } + } + + /// A reference to a file system entity (a file, folder, etc). + public struct FileReference: Common, Sendable, Hashable { + public var common: ReferenceCommon + public var fileType: String + public var regionVariantName: String? = nil + public var fileTextEncoding: String? = nil + public var expectedSignature: String? = nil + + public init(id: GUID, path: String, pathBase: Reference.RefPathBase = .groupDir, name: String? = nil, fileType: String? = nil) { + self.common = ReferenceCommon(id: id, path: path, pathBase: pathBase, name: name ?? path) + self.fileType = fileType ?? Self.fileTypeIdentifier(for: path) + } + } + + /// A group that can contain References (FileReferences and other Groups). The resolved path of a group is used as the base path for any child references whose source tree type is GroupRelative. + public struct Group: Common, Sendable, Hashable { + public enum Kind: String, Sendable, Hashable, Codable { + case group + case variantGroup + case versionGroup + } + + public var kind: Kind = .group + public var common: ReferenceCommon + public var subitems: [Reference] = [] + + public init(id: GUID, path: String, pathBase: Reference.RefPathBase = .groupDir, name: String? = nil, subitems: [Reference] = []) { + self.common = ReferenceCommon(id: id, path: path, pathBase: pathBase, name: name ?? path) + self.subitems = subitems + } + + private var nextRefId: GUID { + return "\(self.common.id.value)::REF_\(subitems.count)" + } + + // MARK: - Reference subitems + + /// Creates and appends a new FileReference to the list of subitems. + @discardableResult public mutating func addFileReference(_ create: CreateFn) -> FileReference { + let fref = create(nextRefId) + subitems.append(.file(fref)) + return fref + } + + @discardableResult public mutating func addGroup(_ create: CreateFn) -> WritableKeyPath { + let group = create(nextRefId) + subitems.append(.group(group)) + let tag = Tag(value: group.id) + return \.[group: tag] + } + + fileprivate subscript(group tag: Tag) -> Group { + get { + guard case let .group(t) = subitems.first(where: { $0.id == tag.value }) else { fatalError() } + return t + } + set { + guard let idx = subitems.firstIndex(where: { $0.id == tag.value }) else { fatalError() } + subitems[idx] = .group(newValue) + } + } + } + + +} + + +extension XPIF.Reference: Codable { + public init(from decoder: any Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + switch try container.decode(String.self, forKey: .type) { + case "file": + self = .file(try .init(from: decoder)) + case "group", "variantGroup", "versionGroup": + self = .group(try .init(from: decoder)) + default: + throw DecodingError.dataCorruptedError(forKey: .type, in: container, debugDescription: "Unknown value for key") + } + } + + public func encode(to encoder: any Encoder) throws { + switch self { + case let .file(fref): + try fref.encode(to: encoder) + case let .group(group): + try group.encode(to: encoder) + } + } + + enum CodingKeys: String, CodingKey { + case type + } +} + +extension XPIF.FileReference: Codable { + public init(from decoder: any Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + let id = try container.decode(XPIF.GUID.self, forKey: .guid) + let pathBase = try container.decodeIfPresent(XPIF.Reference.RefPathBase.self, forKey: .sourceTree) ?? .groupDir + let path = try container.decodeIfPresent(String.self, forKey: .path) ?? "" + let name = try container.decodeIfPresent(String.self, forKey: .name) ?? (path as NSString).lastPathComponent + self.common = .init(id: id, path: path, pathBase: pathBase, name: name) + self.fileType = try container.decodeIfPresent(String.self, forKey: .fileType) ?? Self.fileTypeIdentifier(for: path) + self.regionVariantName = try container.decodeIfPresent(String.self, forKey: .regionVariantName) + self.fileTextEncoding = try container.decodeIfPresent(String.self, forKey: .fileTextEncoding) + self.expectedSignature = try container.decodeIfPresent(String.self, forKey: .expectedSignature) + self.common.additionalFields = try decodeAdditionalFields(from: decoder, excluding: CodingKeys.self) + self.common.explicitDefaults = try decodeExplicitDefaults(from: decoder, defaults: ["name": .string((path as NSString).lastPathComponent)]) + } + + public func encode(to encoder: any Encoder) throws { + try encodeExplicitDefaults(common.explicitDefaults, to: encoder) + try encodeAdditionalFields(self.common.additionalFields, to: encoder, excluding: CodingKeys.self) + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode("file", forKey: .type) + try container.encode(self.id, forKey: .guid) + if common.explicitDefaults["name"] != nil || self.name != (self.path as NSString).lastPathComponent { + try container.encode(self.name, forKey: .name) + } + try container.encode(self.pathBase, forKey: .sourceTree) + try container.encode(self.path, forKey: .path) + try container.encode(self.fileType, forKey: .fileType) + try container.encodeIfPresent(regionVariantName, forKey: .regionVariantName) + try container.encodeIfPresent(fileTextEncoding, forKey: .fileTextEncoding) + try container.encodeIfPresent(expectedSignature, forKey: .expectedSignature) + } + + enum CodingKeys: String, CodingKey, CaseIterable { + case type + case guid + case sourceTree + case path + case fileType + case regionVariantName, fileTextEncoding, expectedSignature + case name + } + + private static func fileTypeIdentifier(for path: String) -> String { + // FIXME: We need real logic here. [SwiftPM] When generating PIF, we need a standard way to determine the file type + let pathExtension = (path as NSString).pathExtension as String + switch pathExtension { + case "txt": return "text" + case "xcodeproj": return "wrapper.pb-project" + case "xcconfig": return "text.xcconfig" + case "plist": return "text.plist.xml" + case "strings": return "text.plist.strings" + case "h": return "sourcecode.c.h" + case "framework": return "wrapper.framework" + case "app": return "wrapper.application" + case "xcdatamodeld": return "wrapper.xcdatamodeld" + case "xcdatamodel": return "wrapper.xcdatamodel" + case "xcmappingmodel": return "wrapper.xcmappingmodel" + case "mlmodel": return "file.mlmodel" + case "mlpackage": return "folder.mlpackage" + case "referenceobject": return "file.referenceobject" + case "a": + return "archive.ar" + case "s", "S": + return "sourcecode.asm" + case "c": + return "sourcecode.c.c" + case "cl": + return "sourcecode.opencl" + case "cpp", "cp", "cxx", "cc", "c++", "C", "tcc": + return "sourcecode.cpp.cpp" + case "d": + return "sourcecode.dtrace" + case "defs", "mig": + return "sourcecode.mig" + case "m": + return "sourcecode.c.objc" + case "mm", "M": + return "sourcecode.cpp.objcpp" + case "metal": + return "sourcecode.metal" + case "l", "lm", "lmm", "lpp", "lp", "lxx": + return "sourcecode.lex" + case "swift": + return "sourcecode.swift" + case "y", "ym", "ymm", "ypp", "yp", "yxx": + return "sourcecode.yacc" + + + // FIXME: This is probably now more important because of resources support. + case "xcassets": + return "folder.assetcatalog" + case "icon": + return "folder.iconcomposer.icon" + case "xcstrings": + return "text.json.xcstrings" + case "storyboard": + return "file.storyboard" + case "xib": + return "file.xib" + + case "xcframework": + return "wrapper.xcframework" + case "artifactbundle": + return "wrapper.artifactbundle" + + case "docc": + return "folder.documentationcatalog" + + case "rkassets": + return "folder.rkassets" + + default: + return "file" + } + } + +} + +extension XPIF.Group: Codable { + public init(from decoder: any Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + let id = try container.decode(XPIF.GUID.self, forKey: .guid) + let pathBase = try container.decodeIfPresent(XPIF.Reference.RefPathBase.self, forKey: .sourceTree) ?? .groupDir + let path = try container.decodeIfPresent(String.self, forKey: .path) ?? "" + let name = try container.decodeIfPresent(String.self, forKey: .name) ?? path + self.common = .init(id: id, path: path, pathBase: pathBase, name: name) + self.subitems = try container.decodeIfPresent([XPIF.Reference].self, forKey: .children) ?? [] + self.kind = try container.decodeIfPresent(Kind.self, forKey: .type) ?? .group + self.common.additionalFields = try decodeAdditionalFields(from: decoder, excluding: CodingKeys.self) + self.common.explicitDefaults = try decodeExplicitDefaults(from: decoder, defaults: ["children": .array([])]) + } + + public func encode(to encoder: any Encoder) throws { + try encodeExplicitDefaults(common.explicitDefaults, to: encoder) + try encodeAdditionalFields(self.common.additionalFields, to: encoder, excluding: CodingKeys.self) + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(kind, forKey: .type) + try container.encode(self.id, forKey: .guid) + try container.encode(self.pathBase, forKey: .sourceTree) + try container.encode(self.path, forKey: .path) + try container.encode(self.name, forKey: .name) + if !subitems.isEmpty { try container.encode(subitems, forKey: .children) } + } + + enum CodingKeys: String, CodingKey, CaseIterable { + case type + case guid + case sourceTree + case path + case name + case children + } + +} diff --git a/Sources/XPIF/SandboxingOverride.swift b/Sources/XPIF/SandboxingOverride.swift new file mode 100644 index 00000000..b9221c24 --- /dev/null +++ b/Sources/XPIF/SandboxingOverride.swift @@ -0,0 +1,12 @@ +// Adapted from SwiftBuild/SandboxingOverride.swift: mutable Xcode PIF support and lossless JSON fields. + +import Foundation + +extension XPIF { + public enum SandboxingOverride: String, Sendable, Codable { + case forceDisabled + case forceEnabled + case basedOnBuildSetting + + } +} diff --git a/Sources/XPIF/StringList.swift b/Sources/XPIF/StringList.swift new file mode 100644 index 00000000..5d4a97fe --- /dev/null +++ b/Sources/XPIF/StringList.swift @@ -0,0 +1,26 @@ +import Foundation + +extension XPIF { + /// A macro expression can be supplied as a single string or a list of strings. + public enum StringList: Sendable, Hashable, Codable { + case string(String) + case array([String]) + + public init(from decoder: any Decoder) throws { + let container = try decoder.singleValueContainer() + if let value = try? container.decode(String.self) { + self = .string(value) + } else { + self = .array(try container.decode([String].self)) + } + } + + public func encode(to encoder: any Encoder) throws { + var container = encoder.singleValueContainer() + switch self { + case .string(let value): try container.encode(value) + case .array(let value): try container.encode(value) + } + } + } +} diff --git a/Sources/XPIF/TargetDependency.swift b/Sources/XPIF/TargetDependency.swift new file mode 100644 index 00000000..db79045a --- /dev/null +++ b/Sources/XPIF/TargetDependency.swift @@ -0,0 +1,43 @@ +// Adapted from SwiftBuild/TargetDependency.swift: mutable Xcode PIF support and lossless JSON fields. + +import Foundation + +extension XPIF { + /// Represents a dependency on another target (identified by its PIF ID). + public struct TargetDependency: Sendable, Hashable { + public var additionalFields: [String: JSONValue] = [:] + var explicitDefaults: [String: JSONValue] = [:] + /// Identifier of depended-upon target. + public var targetId: GUID + /// The platform filters for this target dependency. + public var platformFilters: Set + + public init(targetId: XPIF.GUID, platformFilters: Set = []) { + self.targetId = targetId + self.platformFilters = platformFilters + } + } +} + +extension XPIF.TargetDependency: Codable { + public init(from decoder: any Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + self.targetId = try container.decode(XPIF.GUID.self, forKey: .guid) + self.platformFilters = try container.decodeIfPresent(Set.self, forKey: .platformFilters) ?? [] + self.additionalFields = try decodeAdditionalFields(from: decoder, excluding: CodingKeys.self) + self.explicitDefaults = try decodeExplicitDefaults(from: decoder, defaults: ["platformFilters": .array([])]) + } + + public func encode(to encoder: any Encoder) throws { + try encodeExplicitDefaults(explicitDefaults, to: encoder) + try encodeAdditionalFields(self.additionalFields, to: encoder, excluding: CodingKeys.self) + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(self.targetId, forKey: .guid) + if !platformFilters.isEmpty { try container.encode(platformFilters.sorted(), forKey: .platformFilters) } + } + + enum CodingKeys: String, CodingKey, CaseIterable { + case guid + case platformFilters + } +} diff --git a/Sources/XPIF/Targets.swift b/Sources/XPIF/Targets.swift new file mode 100644 index 00000000..7149abe1 --- /dev/null +++ b/Sources/XPIF/Targets.swift @@ -0,0 +1,588 @@ +// Adapted from SwiftBuild/Targets.swift: mutable Xcode PIF support and lossless JSON fields. + +import Foundation + +extension XPIF { + public enum BaseTarget: Identifiable, Sendable, Hashable { + case target(Target) + case aggregate(AggregateTarget) + case external(ExternalTarget) + + public var id: GUID { common.id } + + public var common: TargetCommon { + get { + switch self { + case let .target(target): return target.common + case let .aggregate(target): return target.common + case let .external(target): return target.common + } + } + _modify { + switch self { + case var .target(target): + yield &target.common + self = .target(target) + case var .aggregate(target): + yield &target.common + self = .aggregate(target) + case var .external(target): + yield &target.common + self = .external(target) + } + } + } + } + + public struct TargetCommon: Sendable, Identifiable, Hashable { + public let id: GUID + public var signature: String? = nil + public var name: String + public var buildConfigs: [BuildConfig] = [] + public var buildPhases: [BuildPhase] = [] + public var buildRules: [BuildRule] = [] + public var dependencies: [TargetDependency] = [] + public var customTasks: [CustomTask] = [] + public var additionalFields: [String: JSONValue] = [:] + var explicitDefaults: [String: JSONValue] = [:] + + // MARK: - BuildConfig + + /// Creates and adds a new empty build configuration, i.e. one that does not initially have any build settings. The name must not be empty and must not be equal to the name of any existing build configuration in the target. + @discardableResult public mutating func addBuildConfig(_ create: CreateFn) -> BuildConfig { + let id = GUID("\(self.id.value)::BUILDCONFIG_\(self.buildConfigs.count)") + let buildConfig = create(id) + + precondition(!buildConfig.name.isEmpty) + precondition(!buildConfigs.contains{ $0.name == buildConfig.name }) + + self.buildConfigs.append(buildConfig) + return buildConfig + } + + public subscript(buildConfig tag: Tag) -> BuildConfig { + get { buildConfigs[id: tag.value] } + set { buildConfigs[id: tag.value] = newValue } + } + + + // MARK: - BuildPhase + public typealias BuildPhaseTag = XTag + + private var nextBuildPhaseId: GUID { + return "\(self.id.value)::BUILDPHASE_\(buildPhases.count)" + } + + /// Adds a "headers" build phase, i.e. one that copies headers into a directory of the product, after suitable processing. + @discardableResult public mutating func addHeadersBuildPhase(_ create: CreateFn) -> BuildPhaseTag { + let phase = create(nextBuildPhaseId) + buildPhases.append(.headers(phase)) + return .init(value: phase.id) + } + + /// Adds a "sources" build phase, i.e. one that compiles sources and provides them to be linked into the executable code of the product. + @discardableResult public mutating func addSourcesBuildPhase(_ create: CreateFn) -> BuildPhaseTag { + let phase = create(nextBuildPhaseId) + buildPhases.append(.sources(phase)) + return .init(value: phase.id) + } + + /// Adds a "frameworks" build phase, i.e. one that links compiled code and libraries into the executable of the product. + @discardableResult public mutating func addFrameworksBuildPhase(_ create: CreateFn) -> BuildPhaseTag { + let phase = create(nextBuildPhaseId) + buildPhases.append(.frameworks(phase)) + return .init(value: phase.id) + } + + /// Adds a "copy files" build phase, i.e. one that copies files to an arbitrary location relative to the product. + @discardableResult public mutating func addCopyBundleResourcesBuildPhase(_ create: CreateFn) -> BuildPhaseTag { + let phase = create(nextBuildPhaseId) + buildPhases.append(.copyBundleResources(phase)) + return .init(value: phase.id) + } + + @discardableResult public mutating func addCopyFilesBuildPhase(_ create: CreateFn) -> WritableKeyPath { + let phase = create(nextBuildPhaseId) + buildPhases.append(.copyFiles(phase)) + let tag = Tag(value: phase.id) + return \.[buildPhase: tag] + } + + /// Adds a "shell script" build phase, i.e. one that runs a custom shell script as part of the build. + @discardableResult public mutating func addShellScriptBuildPhase(insertAtFront: Bool = false, _ create: CreateFn) -> BuildPhaseTag { + let phase = create(nextBuildPhaseId) + if insertAtFront { + buildPhases.insert(.shellScript(phase), at: 0) + } else { + buildPhases.append(.shellScript(phase)) + } + return .init(value: phase.id) + } + + public subscript(buildPhase tag: BuildPhaseTag) -> HeadersBuildPhase { + get { + guard let idx = firstPhaseIndex(tag.value), + case let .headers(t) = buildPhases[idx] else { fatalError() } + return t + } + set { + guard let idx = firstPhaseIndex(tag.value) else { + fatalError() + } + buildPhases[idx] = .headers(newValue) + } + } + + public subscript(buildPhase tag: BuildPhaseTag) -> SourcesBuildPhase { + get { + guard let idx = firstPhaseIndex(tag.value), + case let .sources(t) = buildPhases[idx] else { fatalError() } + return t + } + set { + guard let idx = firstPhaseIndex(tag.value) else { + fatalError() + } + buildPhases[idx] = .sources(newValue) + } + } + + public subscript(buildPhase tag: BuildPhaseTag) -> FrameworksBuildPhase { + get { + guard let idx = firstPhaseIndex(tag.value), + case let .frameworks(t) = buildPhases[idx] else { fatalError() } + return t + } + set { + guard let idx = firstPhaseIndex(tag.value) else { + fatalError() + } + buildPhases[idx] = .frameworks(newValue) + } + } + + public subscript(buildPhase tag: BuildPhaseTag) -> CopyBundleResourcesBuildPhase { + get { + guard let idx = firstPhaseIndex(tag.value), + case let .copyBundleResources(t) = buildPhases[idx] else { fatalError() } + return t + } + set { + guard let idx = firstPhaseIndex(tag.value) else { + fatalError() + } + buildPhases[idx] = .copyBundleResources(newValue) + } + } + + public subscript(buildPhase tag: BuildPhaseTag) -> CopyFilesBuildPhase { + get { + guard let idx = firstPhaseIndex(tag.value), + case let .copyFiles(t) = buildPhases[idx] else { fatalError() } + return t + } + set { + guard let idx = firstPhaseIndex(tag.value) else { + fatalError() + } + buildPhases[idx] = .copyFiles(newValue) + } + } + + public subscript(buildPhase tag: BuildPhaseTag) -> ShellScriptBuildPhase { + get { + guard let idx = firstPhaseIndex(tag.value), + case let .shellScript(t) = buildPhases[idx] else { fatalError() } + return t + } + set { + guard let idx = firstPhaseIndex(tag.value) else { + fatalError() + } + buildPhases[idx] = .shellScript(newValue) + } + } + + @discardableResult public mutating func withFrameworksBuildPhase(_ modify: (inout FrameworksBuildPhase) -> T) -> T { + let phaseTag = firstPhaseTag({ + switch $0 { + case .frameworks(_): return true + default: return false + } + }) ?? (addFrameworksBuildPhase { id in FrameworksBuildPhase(id: id) }) + + return modify(&self[buildPhase: phaseTag]) + } + + @discardableResult public mutating func withCopyBundleResourcesBuildPhase(_ modify: (inout CopyBundleResourcesBuildPhase) -> T) -> T { + let phaseTag = firstPhaseTag({ + switch $0 { + case .copyBundleResources(_): return true + default: return false + } + }) ?? (addCopyBundleResourcesBuildPhase { id in CopyBundleResourcesBuildPhase(id: id) }) + + return modify(&self[buildPhase: phaseTag]) + } + + @discardableResult public mutating func withHeadersBuildPhase(_ modify: (inout HeadersBuildPhase) -> T) -> T { + let phaseTag = firstPhaseTag({ + switch $0 { + case .headers(_): return true + default: return false + } + }) ?? (addHeadersBuildPhase { id in HeadersBuildPhase(id: id) }) + + return modify(&self[buildPhase: phaseTag]) + } + + @discardableResult public mutating func withSourcesBuildPhase(_ modify: (inout SourcesBuildPhase) -> T) -> T { + + let phaseTag = firstPhaseTag({ + switch $0 { + case .sources(_): return true + default: return false + } + }) ?? (addSourcesBuildPhase { id in SourcesBuildPhase(id: id) }) + + return modify(&self[buildPhase: phaseTag]) + } + + private func firstPhaseTag(_ pred: (BuildPhase) -> Bool) -> BuildPhaseTag? { + for buildPhase in buildPhases { + if pred(buildPhase) { + return BuildPhaseTag(value: buildPhase.common.id) + } + } + return nil + } + + private func firstPhaseIndex(_ guid: GUID) -> Int? { + return buildPhases.firstIndex(where: { $0.common.id == guid }) + } + + + // MARK: - BuildRule + + private var nextBuildRuleId: GUID { + return "\(self.id.value)::BUILDRULE_\(buildRules.count)" + } + + /// Adds a build rule with an input specifying files to match and an action specifying either a compiler or a shell script. + @discardableResult public mutating func addBuildRule(_ create: CreateFn) -> Tag { + let buildRule = create(nextBuildRuleId) + buildRules.append(buildRule) + return .init(value: buildRule.id) + } + + public subscript(buildRule tag: Tag) -> BuildRule { + get { buildRules[id: tag.value] } + set { buildRules[id: tag.value] = newValue } + } + + // MARK: - TargetDependency + + /// Adds a dependency on another target. It is the caller's responsibility to avoid creating dependency cycles. A dependency of one target on another ensures that the other target is built first. + public mutating func addTargetDependency(_ dependency: TargetDependency) { + dependencies.append(dependency) + } + + + /// Adds a dependency on another target. It is the caller's responsibility to avoid creating dependency cycles. A dependency of one target on another ensures that the other target is built first. If `linkProduct` is true, the receiver will also be configured to link against the product produced by the other target (this presumes that the product type is one that can be linked against). + @discardableResult public mutating func addDependency(on targetId: GUID, platformFilters: Set, linkProduct: Bool = false) -> TargetDependency { + let dependency = TargetDependency(targetId: targetId, platformFilters: platformFilters) + addTargetDependency(dependency) + if linkProduct { + withFrameworksBuildPhase { phase in + phase.common.addBuildFile { id in BuildFile(id: id, ref: .targetProduct(id: targetId), platformFilters: platformFilters) } + } + } + return dependency + } + } + + /// An Xcode target, representing a single entity to build. + public struct Target: Common, Identifiable, Sendable, Hashable { + /// Product type identifiers are extensible; Xcode and plugins can introduce new ones. + public struct ProductType: RawRepresentable, Sendable, Hashable, Codable { + public var rawValue: String + + public init(rawValue: String) { self.rawValue = rawValue } + + public init(from decoder: any Decoder) throws { + rawValue = try decoder.singleValueContainer().decode(String.self) + } + + public func encode(to encoder: any Encoder) throws { + var container = encoder.singleValueContainer() + try container.encode(rawValue) + } + + public static let application = Self(rawValue: "com.apple.product-type.application") + public static let staticArchive = Self(rawValue: "com.apple.product-type.library.static") + public static let commonStaticArchive = Self(rawValue: "org.swift.product-type.library.static") + public static let commonObject = Self(rawValue: "org.swift.product-type.common.object") + public static let objectLibrary = Self(rawValue: "org.swift.product-type.library.object") + public static let objectFile = Self(rawValue: "com.apple.product-type.objfile") + public static let dynamicLibrary = Self(rawValue: "com.apple.product-type.library.dynamic") + public static let framework = Self(rawValue: "com.apple.product-type.framework") + public static let executable = Self(rawValue: "com.apple.product-type.tool") + public static let hostBuildTool = Self(rawValue: "com.apple.product-type.tool.host-build") + public static let unitTest = Self(rawValue: "com.apple.product-type.bundle.unit-test") + public static let swiftpmTestRunner = Self(rawValue: "com.apple.product-type.tool.swiftpm-test-runner") + public static let bundle = Self(rawValue: "com.apple.product-type.bundle") + public static let packageProduct = Self(rawValue: "packageProduct") + public static let appExtension = Self(rawValue: "com.apple.product-type.app-extension") + public static let uiTest = Self(rawValue: "com.apple.product-type.bundle.ui-testing") + public static let watchApplication = Self(rawValue: "com.apple.product-type.application.watchapp2") + public static let watchExtension = Self(rawValue: "com.apple.product-type.watchkit2-extension") + } + + public var common: TargetCommon + public var productName: String { + get { productReference.name } + set { productReference.name = newValue } + } + public var productType: ProductType + public var productReference: ProductReference + var omitEmptyProvisioningSourceData = false + public var provisioningSourceData: [ProvisioningSourceData] = [] + public var isPackageTarget: Bool = false + public var classPrefix: String? = nil + public var predominantSourceCodeLanguage: String? = nil + public var performanceTestsBaselinesPath: String? = nil + public var approvedByUser: Bool + + /// Package products can have an optional reference to a dynamic target which allows building the same product dynamically instead. + public var dynamicTargetVariantId: Optional + + public var id: GUID { return common.id } + + public init( + id: GUID, + productType: ProductType, + name: String, + productName: String, + approvedByUser: Bool = true, + buildConfigs: [BuildConfig] = [], + buildPhases: [BuildPhase] = [], + buildRules: [BuildRule] = [], + dependencies: [TargetDependency] = [] + ) { + self.common = TargetCommon(id: id, name: name, buildConfigs: buildConfigs, buildPhases: buildPhases, buildRules: buildRules, dependencies: dependencies) + self.productType = productType + self.productReference = ProductReference(id: .init("PRODUCTREF-\(id.value)"), name: productName) + self.approvedByUser = approvedByUser + self.dynamicTargetVariantId = nil + } + + /// Convenience function to add a file reference to the Frameworks build phase, after creating it if needed. + @discardableResult public mutating func addLibrary(_ create: CreateFn) -> BuildFile { + // need to access the first build phase, or create one if it doesn't exist, then edit in place + common.withFrameworksBuildPhase { phase in + phase.common.addBuildFile(create) + } + } + + @discardableResult public mutating func addResourceFile(_ create: CreateFn) -> BuildFile { + // need to access the first build phase, or create one if it doesn't exist, then edit in place + common.withCopyBundleResourcesBuildPhase { phase in + phase.common.addBuildFile(create) + } + } + + @discardableResult public mutating func addSourceFile(_ create: CreateFn) -> BuildFile { + // need to access the first build phase, or create one if it doesn't exist, then edit in place + common.withSourcesBuildPhase { phase in + phase.common.addBuildFile(create) + } + } + } + + public struct AggregateTarget: Common, Identifiable, Sendable, Hashable { + public var common: TargetCommon + public var id: GUID { common.id } + + public init( + id: GUID, + name: String, + buildConfigs: [BuildConfig] = [], + buildPhases: [BuildPhase] = [], + buildRules: [BuildRule] = [], + dependencies: [TargetDependency] = [] + ) { + self.common = TargetCommon(id: id, name: name, buildConfigs: buildConfigs, buildPhases: buildPhases, buildRules: buildRules, dependencies: dependencies) + } + } +} + +extension XPIF.BaseTarget: Codable { + public init(from decoder: any Decoder) throws { + let container = try decoder.container(keyedBy: StringKey.self) + let type = try container.decode(String.self, forKey: StringKey("type")) + switch type { + case "standard", "packageProduct": self = .target(try XPIF.Target(from: decoder)) + case "aggregate": self = .aggregate(try XPIF.AggregateTarget(from: decoder)) + case "external": self = .external(try XPIF.ExternalTarget(from: decoder)) + default: + throw DecodingError.dataCorruptedError( + forKey: StringKey("type"), in: container, debugDescription: "Unknown target type: \(type)" + ) + } + } + + public func encode(to encoder: any Encoder) throws { + switch self { + case .target(let target): try target.encode(to: encoder) + case .aggregate(let target): try target.encode(to: encoder) + case .external(let target): try target.encode(to: encoder) + } + } +} + +extension XPIF.Target: Codable { + public init(from decoder: any Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + + let id = try container.decode(XPIF.GUID.self, forKey: .guid) + let dependencies = try container.decodeIfPresent([XPIF.TargetDependency].self, forKey: .dependencies) ?? [] + let buildConfigs = try container.decodeIfPresent([XPIF.BuildConfig].self, forKey: .buildConfigurations) ?? [] + let buildPhases: [XPIF.BuildPhase] + let buildRules: [XPIF.BuildRule] + let name = try container.decode(String.self, forKey: .name) + + let productType = try container.decode(String.self, forKey: .type) + switch productType { + case "packageProduct": + buildRules = [] + if let phase = try container.decodeIfPresent(XPIF.FrameworksBuildPhase.self, forKey: .frameworksBuildPhase) { + buildPhases = [.frameworks(phase)] + } else { + buildPhases = [] + } + self.productReference = .init(id: .init("PRODUCTREF-\(id.value)"), name: "") + self.productType = .packageProduct + default: + buildPhases = try container.decodeIfPresent([XPIF.BuildPhase].self, forKey: .buildPhases) ?? [] + buildRules = try container.decodeIfPresent([XPIF.BuildRule].self, forKey: .buildRules) ?? [] + self.productType = try container.decode(ProductType.self, forKey: .productTypeIdentifier) + self.productReference = try container.decode(XPIF.ProductReference.self, forKey: .productReference) + } + self.common = .init(id: id, name: name, buildConfigs: buildConfigs, buildPhases: buildPhases, buildRules: buildRules, dependencies: dependencies) + self.dynamicTargetVariantId = try container.decodeIfPresent(XPIF.GUID.self, forKey: .dynamicTargetVariantGuid) + self.approvedByUser = try container.decodePIFBool(forKey: .approvedByUser, default: true) + self.provisioningSourceData = try container.decodeIfPresent([XPIF.ProvisioningSourceData].self, forKey: .provisioningSourceData) ?? [] + self.omitEmptyProvisioningSourceData = !container.contains(.provisioningSourceData) + self.isPackageTarget = try container.decodePIFBool(forKey: .isPackageTarget) + self.classPrefix = try container.decodeIfPresent(String.self, forKey: .classPrefix) + self.predominantSourceCodeLanguage = try container.decodeIfPresent(String.self, forKey: .predominantSourceCodeLanguage) + self.performanceTestsBaselinesPath = try container.decodeIfPresent(String.self, forKey: .performanceTestsBaselinesPath) + self.customTasks = try container.decodeIfPresent([XPIF.CustomTask].self, forKey: .customTasks) ?? [] + self.common.additionalFields = try decodeAdditionalFields(from: decoder, excluding: CodingKeys.self) + self.common.explicitDefaults = try decodeExplicitDefaults(from: decoder, defaults: ["approvedByUser": .string("true"), "customTasks": .array([])]) + } + + public func encode(to encoder: any Encoder) throws { + try encodeExplicitDefaults(common.explicitDefaults, to: encoder) + try encodeAdditionalFields(self.common.additionalFields, to: encoder, excluding: CodingKeys.self) + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode(self.id, forKey: .guid) + try container.encode(self.name, forKey: .name) + try container.encode(self.dependencies, forKey: .dependencies) + try container.encode(self.buildConfigs, forKey: .buildConfigurations) + try container.encodeIfPresent(self.dynamicTargetVariantId, forKey: .dynamicTargetVariantGuid) + if !approvedByUser { try container.encode("false", forKey: .approvedByUser) } + if !self.customTasks.isEmpty { try container.encode(self.customTasks, forKey: .customTasks) } + + switch self.productType { + case .packageProduct: + try container.encode("packageProduct", forKey: .type) + if let first = self.buildPhases.first, + case let .frameworks(phase) = first { + try container.encode(phase, forKey: .frameworksBuildPhase) + } + + default: + try container.encode("standard", forKey: .type) + try container.encode(self.buildPhases, forKey: .buildPhases) + try container.encode(self.buildRules, forKey: .buildRules) + try container.encode(self.productType, forKey: .productTypeIdentifier) + try container.encode(productReference, forKey: .productReference) + if !omitEmptyProvisioningSourceData || !provisioningSourceData.isEmpty { + try container.encode(provisioningSourceData, forKey: .provisioningSourceData) + } + if isPackageTarget { try container.encode("true", forKey: .isPackageTarget) } + try container.encodeIfPresent(classPrefix, forKey: .classPrefix) + try container.encodeIfPresent(predominantSourceCodeLanguage, forKey: .predominantSourceCodeLanguage) + try container.encodeIfPresent(performanceTestsBaselinesPath, forKey: .performanceTestsBaselinesPath) + } + } + + enum CodingKeys: String, CodingKey, CaseIterable { + case guid + case name + case dependencies + case buildRules + case buildPhases + case frameworksBuildPhase + case buildConfigurations + case type + case dynamicTargetVariantGuid + case approvedByUser + case customTasks + case productReference + case productTypeIdentifier + case provisioningSourceData, isPackageTarget, classPrefix + case predominantSourceCodeLanguage, performanceTestsBaselinesPath + } +} + +extension XPIF.AggregateTarget: Codable { + public init(from decoder: any Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + let id = try container.decode(XPIF.GUID.self, forKey: .guid) + let name = try container.decode(String.self, forKey: .name) + let dependencies = try container.decodeIfPresent([XPIF.TargetDependency].self, forKey: .dependencies) ?? [] + let buildRules = try container.decodeIfPresent([XPIF.BuildRule].self, forKey: .buildRules) ?? [] + let buildPhases = try container.decodeIfPresent([XPIF.BuildPhase].self, forKey: .buildPhases) ?? [] + let buildConfigs = try container.decodeIfPresent([XPIF.BuildConfig].self, forKey: .buildConfigurations) ?? [] + let customTasks = try container.decodeIfPresent([XPIF.CustomTask].self, forKey: .customTasks) ?? [] + + self.common = XPIF.TargetCommon( + id: id, + name: name, + buildConfigs: buildConfigs, + buildPhases: buildPhases, + buildRules: buildRules, + dependencies: dependencies, + customTasks: customTasks + ) + self.common.additionalFields = try decodeAdditionalFields(from: decoder, excluding: CodingKeys.self) + self.common.explicitDefaults = try decodeExplicitDefaults(from: decoder, defaults: ["approvedByUser": .string("true"), "customTasks": .array([])]) + } + + public func encode(to encoder: any Encoder) throws { + try encodeExplicitDefaults(common.explicitDefaults, to: encoder) + try encodeAdditionalFields(self.common.additionalFields, to: encoder, excluding: CodingKeys.self) + var container = encoder.container(keyedBy: CodingKeys.self) + try container.encode("aggregate", forKey: .type) + try container.encode(self.id, forKey: .guid) + try container.encode(self.name, forKey: .name) + try container.encode(self.dependencies, forKey: .dependencies) + try container.encode(self.buildRules, forKey: .buildRules) + try container.encode(self.buildPhases, forKey: .buildPhases) + try container.encode(self.buildConfigs, forKey: .buildConfigurations) + if !self.customTasks.isEmpty { try container.encode(self.customTasks, forKey: .customTasks) } + } + + enum CodingKeys: String, CodingKey, CaseIterable { + case type + case guid + case name + case dependencies + case buildRules + case buildPhases + case buildConfigurations + case customTasks + } +} diff --git a/Sources/XPIF/XPIF.swift b/Sources/XPIF/XPIF.swift new file mode 100644 index 00000000..f266c137 --- /dev/null +++ b/Sources/XPIF/XPIF.swift @@ -0,0 +1,90 @@ +// Adapted from SwiftBuild/ProjectModel.swift: mutable Xcode PIF support and lossless JSON fields. + +import Foundation + +/// Mutable models for authoring and importing the JSON Project Interchange Format. +public enum XPIF: Sendable {} + +extension XPIF { + /// The type used for identifying PIF objects. + public struct GUID: ExpressibleByStringInterpolation, Hashable, Sendable, Codable { + public var value: String + + public init(_ value: String) { + self.value = value + } + + public init(stringLiteral value: StringLiteralType) { + self.value = value + } + + public init(from decoder: any Decoder) throws { + let container = try decoder.singleValueContainer() + self.value = try container.decode(String.self) + } + + public func encode(to encoder: any Encoder) throws { + var container = encoder.singleValueContainer() + try container.encode(self.value) + } + } + + public enum Error: Swift.Error { + case duplicatedTargetIdentifier(targetIdentifier: GUID, targetName: String, productName: String) + case emptyTargetIdentifier(targetName: String, productName: String) + case emptyTargetName(targetIdentifier: GUID, productName: String) + } + + /// The PIF schema version supported by the SwiftBuild source this model was adapted from. + public static var pifEncodingSchemaVersion: Int { + return 12 + } + + public struct XTag: Sendable, Hashable where T: Sendable & Hashable { + var value: T + } + public typealias Tag = XTag + + @dynamicMemberLookup + public protocol Common { + associatedtype Delegate + var common: Delegate { get set } + } + + /// Signature for a function to create an instance with a default GUID provided. + public typealias CreateFn = (GUID) -> T +} + +extension XPIF.XTag { + public init(rawValue: T) { + self.value = rawValue + } +} + +extension XPIF.Common { + public subscript(dynamicMember keyPath: KeyPath) -> T { + _read { + yield common[keyPath: keyPath] + } + } + public subscript(dynamicMember keyPath: WritableKeyPath) -> T { + _read { + yield common[keyPath: keyPath] + } + _modify { + yield &common[keyPath: keyPath] + } + } +} + +extension Array where Element: Identifiable { + subscript(id id: Element.ID) -> Element { + get { + first(where: { $0.id == id })! + } + set { + self[firstIndex(where: { $0.id == id })!] = newValue + } + } + +} diff --git a/Tests/XToolTests/Fixtures/complex.json b/Tests/XToolTests/Fixtures/complex.json new file mode 100644 index 00000000..bacfc17c --- /dev/null +++ b/Tests/XToolTests/Fixtures/complex.json @@ -0,0 +1,1470 @@ +[ + { + "contents" : { + "guid" : "Workspace:/work/Hello", + "name" : "Hello", + "path" : "/work/Hello", + "projects" : [ + "a6ab00f2a3d22197249867918a2c8f626072c35e3dbe9023c3f78ea8011ee2a6", + "c8ab7e08118d83c9b147267ffab231358f72066a6e807dc4314dbe293a0c45af" + ] + }, + "signature" : "875c95da3515d7bfe1a715f62c4ae1bec32632ff3e01f9e667bd49d46af55448", + "type" : "workspace" + }, + { + "contents" : { + "buildConfigurations" : [ + { + "buildSettings" : { + "CLANG_ENABLE_MODULES[__platform_filter=driverkit]" : "YES", + "CLANG_ENABLE_MODULES[__platform_filter=ios-maccatalyst]" : "YES", + "CLANG_ENABLE_MODULES[__platform_filter=ios;ios-simulator]" : "YES", + "CLANG_ENABLE_MODULES[__platform_filter=macos]" : "YES", + "CLANG_ENABLE_MODULES[__platform_filter=tvos;tvos-simulator]" : "YES", + "CLANG_ENABLE_MODULES[__platform_filter=watchos;watchos-simulator]" : "YES", + "CLANG_ENABLE_MODULES[__platform_filter=xros;xros-simulator]" : "YES", + "CODE_SIGNING_REQUIRED" : "NO", + "CODE_SIGN_IDENTITY" : "", + "COMPILER_WORKING_DIRECTORY" : "$(WORKSPACE_DIR)", + "COPY_PHASE_STRIP" : "NO", + "DEBUG_INFORMATION_FORMAT" : "dwarf", + "DRIVERKIT_DEPLOYMENT_TARGET" : "21.0", + "DYLIB_INSTALL_NAME_BASE[__platform_filter=driverkit]" : "@rpath", + "DYLIB_INSTALL_NAME_BASE[__platform_filter=ios-maccatalyst]" : "@rpath", + "DYLIB_INSTALL_NAME_BASE[__platform_filter=ios;ios-simulator]" : "@rpath", + "DYLIB_INSTALL_NAME_BASE[__platform_filter=macos]" : "@rpath", + "DYLIB_INSTALL_NAME_BASE[__platform_filter=tvos;tvos-simulator]" : "@rpath", + "DYLIB_INSTALL_NAME_BASE[__platform_filter=watchos;watchos-simulator]" : "@rpath", + "DYLIB_INSTALL_NAME_BASE[__platform_filter=xros;xros-simulator]" : "@rpath", + "ENABLE_NS_ASSERTIONS" : "YES", + "ENABLE_TESTABILITY" : "YES", + "ENABLE_TESTING_SEARCH_PATHS" : "YES", + "ENTITLEMENTS_REQUIRED" : "NO", + "GCC_OPTIMIZATION_LEVEL" : "0", + "GCC_PREPROCESSOR_DEFINITIONS" : [ + "$(inherited)", + "SWIFT_PACKAGE", + "DEBUG=1" + ], + "INDEX_ENABLE_DATA_STORE" : "YES", + "IPHONEOS_DEPLOYMENT_TARGET" : "17.0", + "IPHONEOS_DEPLOYMENT_TARGET[__platform_filter=ios-maccatalyst]" : "17.0", + "KEEP_PRIVATE_EXTERNS" : "NO", + "LIBTOOL_NO_WARNING_FOR_NO_SYMBOLS" : "YES", + "LINKER_DRIVER" : "auto", + "MACOSX_DEPLOYMENT_TARGET" : "14.0", + "ONLY_ACTIVE_ARCH" : "YES", + "OTHER_CFLAGS" : [ + "$(inherited)", + "-DXcode" + ], + "OTHER_LDRFLAGS" : [ + + ], + "OTHER_SWIFT_FLAGS" : [ + "$(inherited)", + "-DXcode" + ], + "PRODUCT_NAME" : "$(TARGET_NAME)", + "SKIP_INSTALL" : "YES", + "SPECIALIZATION_SDK_OPTIONS[__platform_filter=ios;ios-simulator]" : [ + + ], + "SPECIALIZATION_SDK_OPTIONS[__platform_filter=macos]" : [ + + ], + "SUPPORTED_PLATFORMS" : [ + "$(AVAILABLE_PLATFORMS)" + ], + "SWIFT_ACTIVE_COMPILATION_CONDITIONS" : [ + "$(inherited)", + "SWIFT_PACKAGE", + "DEBUG" + ], + "SWIFT_INSTALL_OBJC_HEADER" : "NO", + "SWIFT_OBJC_INTERFACE_HEADER_NAME" : "", + "SWIFT_OPTIMIZATION_LEVEL" : "-Onone", + "TARGETED_DEVICE_FAMILY" : "", + "TVOS_DEPLOYMENT_TARGET" : "15.0", + "USE_HEADERMAP" : "NO", + "WATCHOS_DEPLOYMENT_TARGET" : "9.0", + "XROS_DEPLOYMENT_TARGET" : "1.0" + }, + "guid" : "PACKAGE:hello::BUILDCONFIG_0", + "impartedBuildProperties" : { + "buildSettings" : { + + } + }, + "name" : "Debug" + }, + { + "buildSettings" : { + "CLANG_ENABLE_MODULES[__platform_filter=driverkit]" : "YES", + "CLANG_ENABLE_MODULES[__platform_filter=ios-maccatalyst]" : "YES", + "CLANG_ENABLE_MODULES[__platform_filter=ios;ios-simulator]" : "YES", + "CLANG_ENABLE_MODULES[__platform_filter=macos]" : "YES", + "CLANG_ENABLE_MODULES[__platform_filter=tvos;tvos-simulator]" : "YES", + "CLANG_ENABLE_MODULES[__platform_filter=watchos;watchos-simulator]" : "YES", + "CLANG_ENABLE_MODULES[__platform_filter=xros;xros-simulator]" : "YES", + "CODE_SIGNING_REQUIRED" : "NO", + "CODE_SIGN_IDENTITY" : "", + "COMPILER_WORKING_DIRECTORY" : "$(WORKSPACE_DIR)", + "COPY_PHASE_STRIP" : "YES", + "DEBUG_INFORMATION_FORMAT" : "dwarf-with-dsym", + "DEPLOYMENT_POSTPROCESSING" : "YES", + "DRIVERKIT_DEPLOYMENT_TARGET" : "21.0", + "DYLIB_INSTALL_NAME_BASE[__platform_filter=driverkit]" : "@rpath", + "DYLIB_INSTALL_NAME_BASE[__platform_filter=ios-maccatalyst]" : "@rpath", + "DYLIB_INSTALL_NAME_BASE[__platform_filter=ios;ios-simulator]" : "@rpath", + "DYLIB_INSTALL_NAME_BASE[__platform_filter=macos]" : "@rpath", + "DYLIB_INSTALL_NAME_BASE[__platform_filter=tvos;tvos-simulator]" : "@rpath", + "DYLIB_INSTALL_NAME_BASE[__platform_filter=watchos;watchos-simulator]" : "@rpath", + "DYLIB_INSTALL_NAME_BASE[__platform_filter=xros;xros-simulator]" : "@rpath", + "ENABLE_TESTING_SEARCH_PATHS" : "YES", + "ENTITLEMENTS_REQUIRED" : "NO", + "GCC_OPTIMIZATION_LEVEL" : "s", + "GCC_PREPROCESSOR_DEFINITIONS" : [ + "$(inherited)", + "SWIFT_PACKAGE" + ], + "IPHONEOS_DEPLOYMENT_TARGET" : "17.0", + "IPHONEOS_DEPLOYMENT_TARGET[__platform_filter=ios-maccatalyst]" : "17.0", + "KEEP_PRIVATE_EXTERNS" : "NO", + "LIBTOOL_NO_WARNING_FOR_NO_SYMBOLS" : "YES", + "LINKER_DRIVER" : "auto", + "MACOSX_DEPLOYMENT_TARGET" : "14.0", + "OTHER_CFLAGS" : [ + "$(inherited)", + "-DXcode" + ], + "OTHER_LDRFLAGS" : [ + + ], + "OTHER_SWIFT_FLAGS" : [ + "$(inherited)", + "-DXcode" + ], + "PRODUCT_NAME" : "$(TARGET_NAME)", + "SKIP_INSTALL" : "YES", + "SPECIALIZATION_SDK_OPTIONS[__platform_filter=ios;ios-simulator]" : [ + + ], + "SPECIALIZATION_SDK_OPTIONS[__platform_filter=macos]" : [ + + ], + "SUPPORTED_PLATFORMS" : [ + "$(AVAILABLE_PLATFORMS)" + ], + "SWIFT_ACTIVE_COMPILATION_CONDITIONS" : [ + "$(inherited)", + "SWIFT_PACKAGE" + ], + "SWIFT_INSTALL_OBJC_HEADER" : "NO", + "SWIFT_OBJC_INTERFACE_HEADER_NAME" : "", + "SWIFT_OPTIMIZATION_LEVEL" : "-Owholemodule", + "TARGETED_DEVICE_FAMILY" : "", + "TVOS_DEPLOYMENT_TARGET" : "15.0", + "USE_HEADERMAP" : "NO", + "WATCHOS_DEPLOYMENT_TARGET" : "9.0", + "XROS_DEPLOYMENT_TARGET" : "1.0" + }, + "guid" : "PACKAGE:hello::BUILDCONFIG_1", + "impartedBuildProperties" : { + "buildSettings" : { + + } + }, + "name" : "Release" + } + ], + "defaultConfigurationName" : "Release", + "groupTree" : { + "children" : [ + { + "children" : [ + + ], + "guid" : "PACKAGE:hello::MAINGROUP::REF_0", + "name" : "AdditionalFiles", + "path" : "/", + "sourceTree" : "", + "type" : "group" + }, + { + "children" : [ + + ], + "guid" : "PACKAGE:hello::MAINGROUP::REF_1", + "name" : "Binaries", + "path" : "/", + "sourceTree" : "", + "type" : "group" + }, + { + "children" : [ + { + "fileType" : "sourcecode.swift", + "guid" : "PACKAGE:hello::MAINGROUP::REF_2::REF_0", + "name" : "MyTests.swift", + "path" : "MyTests.swift", + "sourceTree" : "", + "type" : "file" + } + ], + "guid" : "PACKAGE:hello::MAINGROUP::REF_2", + "name" : "/work/Hello/Tests/HelloTests", + "path" : "/work/Hello/Tests/HelloTests", + "sourceTree" : "", + "type" : "group" + }, + { + "children" : [ + { + "fileType" : "sourcecode.swift", + "guid" : "PACKAGE:hello::MAINGROUP::REF_3::REF_0", + "name" : "ContentView.swift", + "path" : "ContentView.swift", + "sourceTree" : "", + "type" : "file" + }, + { + "fileType" : "sourcecode.swift", + "guid" : "PACKAGE:hello::MAINGROUP::REF_3::REF_1", + "name" : "HelloApp.swift", + "path" : "HelloApp.swift", + "sourceTree" : "", + "type" : "file" + } + ], + "guid" : "PACKAGE:hello::MAINGROUP::REF_3", + "name" : "/work/Hello/Sources/Hello", + "path" : "/work/Hello/Sources/Hello", + "sourceTree" : "", + "type" : "group" + }, + { + "children" : [ + { + "fileType" : "sourcecode.swift", + "guid" : "PACKAGE:hello::MAINGROUP::REF_4::REF_0", + "name" : "Widget.swift", + "path" : "Widget.swift", + "sourceTree" : "", + "type" : "file" + } + ], + "guid" : "PACKAGE:hello::MAINGROUP::REF_4", + "name" : "/work/Hello/Sources/MyWidget", + "path" : "/work/Hello/Sources/MyWidget", + "sourceTree" : "", + "type" : "group" + }, + { + "children" : [ + { + "fileType" : "sourcecode.swift", + "guid" : "PACKAGE:hello::MAINGROUP::REF_5::REF_0", + "name" : "Widget.swift", + "path" : "Widget.swift", + "sourceTree" : "", + "type" : "file" + } + ], + "guid" : "PACKAGE:hello::MAINGROUP::REF_5", + "name" : "/work/Hello/Sources/MyWidget", + "path" : "/work/Hello/Sources/MyWidget", + "sourceTree" : "", + "type" : "group" + } + ], + "guid" : "PACKAGE:hello::MAINGROUP", + "name" : "", + "path" : "", + "sourceTree" : "", + "type" : "group" + }, + "guid" : "PACKAGE:hello", + "path" : "/work/Hello/Package.swift", + "projectDirectory" : "/work/Hello", + "projectIsPackage" : "true", + "projectName" : "Hello", + "targets" : [ + "ead219f9093804bf4292e290a9ef9188c6c8d636c4548a5c80095fe3150b9b0b", + "21a17ae05fa5185b2886cffe901fa48c86d62cc6a255f667f288502b3d77a9d7", + "98a21dc3b8df6cc1903f494181d49f4b3127a18eaeb2735bcb5d0ef9befec602", + "406e195565df919b35d1912d888854276b0b12bdff637cdbac9b1c636098ab3a", + "65f048c8cb6d05d5d99bf3802fd8058ffcdcb0428470f78a8b10fbcdd416f4d3", + "7379381b124adc9f1681d646309fe4cca96928ae0def3a735f24a1f14b6fe864", + "92bf7ab8b6f69b56e2745999713d6be6a737097b70fb7b09b82256b59bc8ce67" + ] + }, + "signature" : "a6ab00f2a3d22197249867918a2c8f626072c35e3dbe9023c3f78ea8011ee2a6", + "type" : "project" + }, + { + "contents" : { + "approvedByUser" : "true", + "buildConfigurations" : [ + { + "buildSettings" : { + "APPLE_TEST_BUNDLE_RPATH" : "$(APPLE_TEST_BUNDLE_RPATH_SHALLOW_BUNDLE_$(SHALLOW_BUNDLE:default=NO))", + "APPLE_TEST_BUNDLE_RPATH_SHALLOW_BUNDLE_NO" : "@loader_path/../../..", + "APPLE_TEST_BUNDLE_RPATH_SHALLOW_BUNDLE_YES" : "@loader_path/../..", + "BUILD_SERVER_PROTOCOL_TARGET_TAGS" : [ + "$(inherited)", + "test" + ], + "DOCC_EXTRACT_PROJECT_HEADERS_DOCUMENTATION" : "YES", + "DRIVERKIT_DEPLOYMENT_TARGET" : "21.0", + "GENERATE_EMBED_IN_CODE_ACCESSORS" : "NO", + "GENERATE_INFOPLIST_FILE" : "YES", + "GENERATE_RESOURCE_ACCESSORS" : "YES", + "INDEX_ENABLE_DATA_STORE" : "YES", + "IPHONEOS_DEPLOYMENT_TARGET" : "17.0", + "IPHONEOS_DEPLOYMENT_TARGET[__platform_filter=ios-maccatalyst]" : "17.0", + "LD_RUNPATH_SEARCH_PATHS" : [ + "$(RPATH_ORIGIN)/Frameworks", + "$(RPATH_ORIGIN)/../Frameworks", + "$(inherited)" + ], + "LD_RUNPATH_SEARCH_PATHS[__platform_filter=driverkit]" : [ + "$(APPLE_TEST_BUNDLE_RPATH)", + "$(inherited)" + ], + "LD_RUNPATH_SEARCH_PATHS[__platform_filter=ios-maccatalyst]" : [ + "$(APPLE_TEST_BUNDLE_RPATH)", + "$(inherited)" + ], + "LD_RUNPATH_SEARCH_PATHS[__platform_filter=ios;ios-simulator]" : [ + "$(APPLE_TEST_BUNDLE_RPATH)", + "$(inherited)" + ], + "LD_RUNPATH_SEARCH_PATHS[__platform_filter=macos]" : [ + "$(APPLE_TEST_BUNDLE_RPATH)", + "$(inherited)" + ], + "LD_RUNPATH_SEARCH_PATHS[__platform_filter=tvos;tvos-simulator]" : [ + "$(APPLE_TEST_BUNDLE_RPATH)", + "$(inherited)" + ], + "LD_RUNPATH_SEARCH_PATHS[__platform_filter=watchos;watchos-simulator]" : [ + "$(APPLE_TEST_BUNDLE_RPATH)", + "$(inherited)" + ], + "LD_RUNPATH_SEARCH_PATHS[__platform_filter=xros;xros-simulator]" : [ + "$(APPLE_TEST_BUNDLE_RPATH)", + "$(inherited)" + ], + "MACOSX_DEPLOYMENT_TARGET" : "14.0", + "PACKAGE_RESOURCE_TARGET_KIND" : "regular", + "PRODUCT_BUNDLE_IDENTIFIER" : "hello.HelloTests", + "PRODUCT_MODULE_NAME" : "HelloTests", + "PRODUCT_NAME" : "$(TARGET_NAME)", + "SDKROOT" : "auto", + "SDK_VARIANT" : "auto", + "SKIP_INSTALL" : "NO", + "SWIFT_ACTIVE_COMPILATION_CONDITIONS" : [ + "$(inherited)" + ], + "SWIFT_DISABLE_PARSE_AS_LIBRARY" : "NO", + "SWIFT_ENABLE_BARE_SLASH_REGEX" : "NO", + "SWIFT_LIBRARIES_ONLY" : "YES", + "SWIFT_PACKAGE_NAME" : "hello", + "SWIFT_VERSION" : "6", + "SYMBOL_GRAPH_EXTRACTOR_OUTPUT_DIR" : "$(TARGET_BUILD_DIR)/$(CURRENT_ARCH)/HelloTests.symbolgraphs", + "TAPI_EXTRACT_API_OUTPUT_DIR" : "$(TARGET_BUILD_DIR)/$(CURRENT_ARCH)/HelloTests.symbolgraphs", + "TARGET_NAME" : "HelloTests", + "TARGET_TEMP_DIR_SUFFIX" : "-p", + "TVOS_DEPLOYMENT_TARGET" : "15.0", + "WATCHOS_DEPLOYMENT_TARGET" : "9.0", + "XROS_DEPLOYMENT_TARGET" : "1.0" + }, + "guid" : "PACKAGE-PRODUCT:hello_HelloTests.HelloTests::BUILDCONFIG_0", + "impartedBuildProperties" : { + "buildSettings" : { + + } + }, + "name" : "Debug" + }, + { + "buildSettings" : { + "APPLE_TEST_BUNDLE_RPATH" : "$(APPLE_TEST_BUNDLE_RPATH_SHALLOW_BUNDLE_$(SHALLOW_BUNDLE:default=NO))", + "APPLE_TEST_BUNDLE_RPATH_SHALLOW_BUNDLE_NO" : "@loader_path/../../..", + "APPLE_TEST_BUNDLE_RPATH_SHALLOW_BUNDLE_YES" : "@loader_path/../..", + "BUILD_SERVER_PROTOCOL_TARGET_TAGS" : [ + "$(inherited)", + "test" + ], + "DOCC_EXTRACT_PROJECT_HEADERS_DOCUMENTATION" : "YES", + "DRIVERKIT_DEPLOYMENT_TARGET" : "21.0", + "GENERATE_EMBED_IN_CODE_ACCESSORS" : "NO", + "GENERATE_INFOPLIST_FILE" : "YES", + "GENERATE_RESOURCE_ACCESSORS" : "YES", + "INDEX_ENABLE_DATA_STORE" : "YES", + "IPHONEOS_DEPLOYMENT_TARGET" : "17.0", + "IPHONEOS_DEPLOYMENT_TARGET[__platform_filter=ios-maccatalyst]" : "17.0", + "LD_RUNPATH_SEARCH_PATHS" : [ + "$(RPATH_ORIGIN)/Frameworks", + "$(RPATH_ORIGIN)/../Frameworks", + "$(inherited)" + ], + "LD_RUNPATH_SEARCH_PATHS[__platform_filter=driverkit]" : [ + "$(APPLE_TEST_BUNDLE_RPATH)", + "$(inherited)" + ], + "LD_RUNPATH_SEARCH_PATHS[__platform_filter=ios-maccatalyst]" : [ + "$(APPLE_TEST_BUNDLE_RPATH)", + "$(inherited)" + ], + "LD_RUNPATH_SEARCH_PATHS[__platform_filter=ios;ios-simulator]" : [ + "$(APPLE_TEST_BUNDLE_RPATH)", + "$(inherited)" + ], + "LD_RUNPATH_SEARCH_PATHS[__platform_filter=macos]" : [ + "$(APPLE_TEST_BUNDLE_RPATH)", + "$(inherited)" + ], + "LD_RUNPATH_SEARCH_PATHS[__platform_filter=tvos;tvos-simulator]" : [ + "$(APPLE_TEST_BUNDLE_RPATH)", + "$(inherited)" + ], + "LD_RUNPATH_SEARCH_PATHS[__platform_filter=watchos;watchos-simulator]" : [ + "$(APPLE_TEST_BUNDLE_RPATH)", + "$(inherited)" + ], + "LD_RUNPATH_SEARCH_PATHS[__platform_filter=xros;xros-simulator]" : [ + "$(APPLE_TEST_BUNDLE_RPATH)", + "$(inherited)" + ], + "MACOSX_DEPLOYMENT_TARGET" : "14.0", + "PACKAGE_RESOURCE_TARGET_KIND" : "regular", + "PRODUCT_BUNDLE_IDENTIFIER" : "hello.HelloTests", + "PRODUCT_MODULE_NAME" : "HelloTests", + "PRODUCT_NAME" : "$(TARGET_NAME)", + "SDKROOT" : "auto", + "SDK_VARIANT" : "auto", + "SKIP_INSTALL" : "NO", + "SWIFT_ACTIVE_COMPILATION_CONDITIONS" : [ + "$(inherited)" + ], + "SWIFT_DISABLE_PARSE_AS_LIBRARY" : "NO", + "SWIFT_ENABLE_BARE_SLASH_REGEX" : "NO", + "SWIFT_LIBRARIES_ONLY" : "YES", + "SWIFT_PACKAGE_NAME" : "hello", + "SWIFT_VERSION" : "6", + "SYMBOL_GRAPH_EXTRACTOR_OUTPUT_DIR" : "$(TARGET_BUILD_DIR)/$(CURRENT_ARCH)/HelloTests.symbolgraphs", + "TAPI_EXTRACT_API_OUTPUT_DIR" : "$(TARGET_BUILD_DIR)/$(CURRENT_ARCH)/HelloTests.symbolgraphs", + "TARGET_NAME" : "HelloTests", + "TARGET_TEMP_DIR_SUFFIX" : "-p", + "TVOS_DEPLOYMENT_TARGET" : "15.0", + "WATCHOS_DEPLOYMENT_TARGET" : "9.0", + "XROS_DEPLOYMENT_TARGET" : "1.0" + }, + "guid" : "PACKAGE-PRODUCT:hello_HelloTests.HelloTests::BUILDCONFIG_1", + "impartedBuildProperties" : { + "buildSettings" : { + + } + }, + "name" : "Release" + } + ], + "buildPhases" : [ + { + "buildFiles" : [ + { + "codeSignOnCopy" : "false", + "fileReference" : "PACKAGE:hello::MAINGROUP::REF_2::REF_0", + "guid" : "PACKAGE-PRODUCT:hello_HelloTests.HelloTests::BUILDPHASE_0::0", + "platformFilters" : [ + + ], + "removeHeadersOnCopy" : "false" + } + ], + "guid" : "PACKAGE-PRODUCT:hello_HelloTests.HelloTests::BUILDPHASE_0", + "type" : "com.apple.buildphase.sources" + }, + { + "buildFiles" : [ + { + "guid" : "PACKAGE-PRODUCT:hello_HelloTests.HelloTests::BUILDPHASE_1::0", + "platformFilters" : [ + + ], + "targetReference" : "PACKAGE-TARGET:Hello" + } + ], + "guid" : "PACKAGE-PRODUCT:hello_HelloTests.HelloTests::BUILDPHASE_1", + "type" : "com.apple.buildphase.frameworks" + } + ], + "buildRules" : [ + + ], + "customTasks" : [ + + ], + "dependencies" : [ + { + "guid" : "PACKAGE-TARGET:Hello", + "platformFilters" : [ + + ] + } + ], + "guid" : "PACKAGE-PRODUCT:hello_HelloTests.HelloTests", + "name" : "HelloTests-product", + "productReference" : { + "guid" : "PRODUCTREF-PACKAGE-PRODUCT:hello_HelloTests.HelloTests", + "name" : "$(EXECUTABLE_NAME)", + "type" : "file" + }, + "productTypeIdentifier" : "com.apple.product-type.bundle.unit-test", + "type" : "standard" + }, + "signature" : "ead219f9093804bf4292e290a9ef9188c6c8d636c4548a5c80095fe3150b9b0b", + "type" : "target" + }, + { + "contents" : { + "approvedByUser" : "true", + "buildConfigurations" : [ + { + "buildSettings" : { + "DRIVERKIT_DEPLOYMENT_TARGET" : "21.0", + "IPHONEOS_DEPLOYMENT_TARGET" : "17.0", + "IPHONEOS_DEPLOYMENT_TARGET[__platform_filter=ios-maccatalyst]" : "17.0", + "LD_RUNPATH_SEARCH_PATHS" : [ + "$(inherited)", + "$(RPATH_ORIGIN)" + ], + "LINKER_DRIVER" : "swiftc", + "MACOSX_DEPLOYMENT_TARGET" : "14.0", + "PACKAGE_RESOURCE_TARGET_KIND" : "regular", + "PRODUCT_BUNDLE_IDENTIFIER" : "hello.HelloTests-test-runner", + "PRODUCT_MODULE_NAME" : "HelloTests_test_runner", + "PRODUCT_NAME" : "$(TARGET_NAME)", + "SDKROOT" : "auto", + "SDK_VARIANT" : "auto", + "SKIP_INSTALL" : "NO", + "SWIFT_VERSION" : "5.0", + "TARGET_NAME" : "HelloTests-test-runner", + "TVOS_DEPLOYMENT_TARGET" : "15.0", + "WATCHOS_DEPLOYMENT_TARGET" : "9.0", + "XROS_DEPLOYMENT_TARGET" : "1.0" + }, + "guid" : "PACKAGE-TARGET:HelloTests_test_runner::BUILDCONFIG_0", + "impartedBuildProperties" : { + "buildSettings" : { + + } + }, + "name" : "Debug" + }, + { + "buildSettings" : { + "DRIVERKIT_DEPLOYMENT_TARGET" : "21.0", + "IPHONEOS_DEPLOYMENT_TARGET" : "17.0", + "IPHONEOS_DEPLOYMENT_TARGET[__platform_filter=ios-maccatalyst]" : "17.0", + "LD_RUNPATH_SEARCH_PATHS" : [ + "$(inherited)", + "$(RPATH_ORIGIN)" + ], + "LINKER_DRIVER" : "swiftc", + "MACOSX_DEPLOYMENT_TARGET" : "14.0", + "PACKAGE_RESOURCE_TARGET_KIND" : "regular", + "PRODUCT_BUNDLE_IDENTIFIER" : "hello.HelloTests-test-runner", + "PRODUCT_MODULE_NAME" : "HelloTests_test_runner", + "PRODUCT_NAME" : "$(TARGET_NAME)", + "SDKROOT" : "auto", + "SDK_VARIANT" : "auto", + "SKIP_INSTALL" : "NO", + "SWIFT_VERSION" : "5.0", + "TARGET_NAME" : "HelloTests-test-runner", + "TVOS_DEPLOYMENT_TARGET" : "15.0", + "WATCHOS_DEPLOYMENT_TARGET" : "9.0", + "XROS_DEPLOYMENT_TARGET" : "1.0" + }, + "guid" : "PACKAGE-TARGET:HelloTests_test_runner::BUILDCONFIG_1", + "impartedBuildProperties" : { + "buildSettings" : { + + } + }, + "name" : "Release" + } + ], + "buildPhases" : [ + { + "buildFiles" : [ + + ], + "guid" : "PACKAGE-TARGET:HelloTests_test_runner::BUILDPHASE_0", + "type" : "com.apple.buildphase.sources" + }, + { + "buildFiles" : [ + { + "guid" : "PACKAGE-TARGET:HelloTests_test_runner::BUILDPHASE_1::0", + "platformFilters" : [ + + ], + "targetReference" : "PACKAGE-PRODUCT:hello_HelloTests.HelloTests" + } + ], + "guid" : "PACKAGE-TARGET:HelloTests_test_runner::BUILDPHASE_1", + "type" : "com.apple.buildphase.frameworks" + } + ], + "buildRules" : [ + + ], + "customTasks" : [ + + ], + "dependencies" : [ + { + "guid" : "PACKAGE-PRODUCT:hello_HelloTests.HelloTests", + "platformFilters" : [ + + ] + } + ], + "guid" : "PACKAGE-TARGET:HelloTests_test_runner", + "name" : "HelloTests-test-runner", + "productReference" : { + "guid" : "PRODUCTREF-PACKAGE-TARGET:HelloTests_test_runner", + "name" : "HelloTests-test-runner", + "type" : "file" + }, + "productTypeIdentifier" : "com.apple.product-type.tool.swiftpm-test-runner", + "type" : "standard" + }, + "signature" : "21a17ae05fa5185b2886cffe901fa48c86d62cc6a255f667f288502b3d77a9d7", + "type" : "target" + }, + { + "contents" : { + "approvedByUser" : "true", + "buildConfigurations" : [ + { + "buildSettings" : { + "EXECUTABLE_PREFIX" : "lib", + "EXECUTABLE_PREFIX[__platform_filter=windows-msvc]" : "", + "PRODUCT_BUNDLE_IDENTIFIER" : "hello.Hello", + "PRODUCT_MODULE_NAME" : "Hello", + "PRODUCT_NAME" : "Hello", + "SDKROOT" : "auto", + "SDK_VARIANT" : "auto", + "SWIFT_PACKAGE_NAME" : "hello", + "TARGET_NAME" : "Hello-product", + "TARGET_TEMP_DIR_SUFFIX" : "-t", + "USES_SWIFTPM_UNSAFE_FLAGS" : "NO" + }, + "guid" : "PACKAGE-PRODUCT:hello_Hello.Hello::BUILDCONFIG_0", + "impartedBuildProperties" : { + "buildSettings" : { + + } + }, + "name" : "Debug" + }, + { + "buildSettings" : { + "EXECUTABLE_PREFIX" : "lib", + "EXECUTABLE_PREFIX[__platform_filter=windows-msvc]" : "", + "PRODUCT_BUNDLE_IDENTIFIER" : "hello.Hello", + "PRODUCT_MODULE_NAME" : "Hello", + "PRODUCT_NAME" : "Hello", + "SDKROOT" : "auto", + "SDK_VARIANT" : "auto", + "SWIFT_PACKAGE_NAME" : "hello", + "TARGET_NAME" : "Hello-product", + "TARGET_TEMP_DIR_SUFFIX" : "-t", + "USES_SWIFTPM_UNSAFE_FLAGS" : "NO" + }, + "guid" : "PACKAGE-PRODUCT:hello_Hello.Hello::BUILDCONFIG_1", + "impartedBuildProperties" : { + "buildSettings" : { + + } + }, + "name" : "Release" + } + ], + "buildPhases" : [ + { + "buildFiles" : [ + { + "guid" : "PACKAGE-PRODUCT:hello_Hello.Hello::BUILDPHASE_0::0", + "platformFilters" : [ + + ], + "targetReference" : "PACKAGE-TARGET:Hello" + } + ], + "guid" : "PACKAGE-PRODUCT:hello_Hello.Hello::BUILDPHASE_0", + "type" : "com.apple.buildphase.frameworks" + }, + { + "buildFiles" : [ + + ], + "guid" : "PACKAGE-PRODUCT:hello_Hello.Hello::BUILDPHASE_1", + "type" : "com.apple.buildphase.sources" + } + ], + "buildRules" : [ + + ], + "customTasks" : [ + + ], + "dependencies" : [ + { + "guid" : "PACKAGE-TARGET:Hello", + "platformFilters" : [ + + ] + } + ], + "guid" : "PACKAGE-PRODUCT:hello_Hello.Hello", + "name" : "Hello-product", + "productReference" : { + "guid" : "PRODUCTREF-PACKAGE-PRODUCT:hello_Hello.Hello", + "name" : "$(EXECUTABLE_NAME)", + "type" : "file" + }, + "productTypeIdentifier" : "com.apple.product-type.library.dynamic", + "type" : "standard" + }, + "signature" : "98a21dc3b8df6cc1903f494181d49f4b3127a18eaeb2735bcb5d0ef9befec602", + "type" : "target" + }, + { + "contents" : { + "approvedByUser" : "true", + "buildConfigurations" : [ + { + "buildSettings" : { + "DEFINES_MODULE" : "YES", + "DOCC_EXTRACT_PROJECT_HEADERS_DOCUMENTATION" : "YES", + "GENERATE_PRELINK_OBJECT_FILE" : "NO", + "MODULEMAP_FILE_CONTENTS" : "module Hello {\nheader \"Hello-Swift.h\"\nexport *\n}", + "MODULEMAP_PATH" : "$(GENERATED_MODULEMAP_DIR)/Hello.modulemap", + "PACKAGE_RESOURCE_TARGET_KIND" : "regular", + "PACKAGE_TARGET_NAME_CONFLICTS_WITH_PRODUCT_NAME" : "YES", + "PRODUCT_BUNDLE_IDENTIFIER" : "hello.Hello", + "PRODUCT_MODULE_NAME" : "Hello", + "PRODUCT_NAME" : "$(TARGET_NAME)", + "SDKROOT" : "auto", + "SDK_VARIANT" : "auto", + "STRIP_INSTALLED_PRODUCT" : "NO", + "SUPPORTS_TEXT_BASED_API" : "NO", + "SWIFT_ACTIVE_COMPILATION_CONDITIONS" : [ + "$(inherited)", + "SWIFT_MODULE_RESOURCE_BUNDLE_UNAVAILABLE" + ], + "SWIFT_COMPILE_FOR_STATIC_LINKING[__platform_filter=windows-msvc]" : "NO", + "SWIFT_DISABLE_PARSE_AS_LIBRARY" : "NO", + "SWIFT_ENABLE_BARE_SLASH_REGEX" : "NO", + "SWIFT_LIBRARIES_ONLY" : "YES", + "SWIFT_OBJC_INTERFACE_HEADER_DIR" : "$(GENERATED_MODULEMAP_DIR)", + "SWIFT_OBJC_INTERFACE_HEADER_NAME" : "Hello-Swift.h", + "SWIFT_PACKAGE_NAME" : "hello", + "SWIFT_VERSION" : "6", + "SYMBOL_GRAPH_EXTRACTOR_OUTPUT_DIR" : "$(TARGET_BUILD_DIR)/$(CURRENT_ARCH)/Hello.symbolgraphs", + "TAPI_DYLIB_INSTALL_NAME" : "Hello", + "TAPI_EXTRACT_API_OUTPUT_DIR" : "$(TARGET_BUILD_DIR)/$(CURRENT_ARCH)/Hello.symbolgraphs", + "TARGET_NAME" : "Hello", + "TARGET_TEMP_DIR_SUFFIX" : "-t" + }, + "guid" : "PACKAGE-TARGET:Hello::BUILDCONFIG_0", + "impartedBuildProperties" : { + "buildSettings" : { + "FRAMEWORK_SEARCH_PATHS" : [ + "$(BUILT_PRODUCTS_DIR)/PackageFrameworks", + "$(inherited)" + ], + "LD_RUNPATH_SEARCH_PATHS" : [ + "$(RPATH_ORIGIN)", + "$(BUILT_PRODUCTS_DIR)/PackageFrameworks", + "$(inherited)" + ], + "LD_WARN_DUPLICATE_LIBRARIES" : "NO", + "OTHER_CFLAGS" : [ + "-fmodule-map-file=$(GENERATED_MODULEMAP_DIR)/Hello.modulemap", + "$(inherited)" + ] + } + }, + "name" : "Debug" + }, + { + "buildSettings" : { + "DEFINES_MODULE" : "YES", + "DOCC_EXTRACT_PROJECT_HEADERS_DOCUMENTATION" : "YES", + "GENERATE_PRELINK_OBJECT_FILE" : "NO", + "MODULEMAP_FILE_CONTENTS" : "module Hello {\nheader \"Hello-Swift.h\"\nexport *\n}", + "MODULEMAP_PATH" : "$(GENERATED_MODULEMAP_DIR)/Hello.modulemap", + "PACKAGE_RESOURCE_TARGET_KIND" : "regular", + "PACKAGE_TARGET_NAME_CONFLICTS_WITH_PRODUCT_NAME" : "YES", + "PRODUCT_BUNDLE_IDENTIFIER" : "hello.Hello", + "PRODUCT_MODULE_NAME" : "Hello", + "PRODUCT_NAME" : "$(TARGET_NAME)", + "SDKROOT" : "auto", + "SDK_VARIANT" : "auto", + "STRIP_INSTALLED_PRODUCT" : "NO", + "SUPPORTS_TEXT_BASED_API" : "NO", + "SWIFT_ACTIVE_COMPILATION_CONDITIONS" : [ + "$(inherited)", + "SWIFT_MODULE_RESOURCE_BUNDLE_UNAVAILABLE" + ], + "SWIFT_COMPILE_FOR_STATIC_LINKING[__platform_filter=windows-msvc]" : "NO", + "SWIFT_DISABLE_PARSE_AS_LIBRARY" : "NO", + "SWIFT_ENABLE_BARE_SLASH_REGEX" : "NO", + "SWIFT_LIBRARIES_ONLY" : "YES", + "SWIFT_OBJC_INTERFACE_HEADER_DIR" : "$(GENERATED_MODULEMAP_DIR)", + "SWIFT_OBJC_INTERFACE_HEADER_NAME" : "Hello-Swift.h", + "SWIFT_PACKAGE_NAME" : "hello", + "SWIFT_VERSION" : "6", + "SYMBOL_GRAPH_EXTRACTOR_OUTPUT_DIR" : "$(TARGET_BUILD_DIR)/$(CURRENT_ARCH)/Hello.symbolgraphs", + "TAPI_DYLIB_INSTALL_NAME" : "Hello", + "TAPI_EXTRACT_API_OUTPUT_DIR" : "$(TARGET_BUILD_DIR)/$(CURRENT_ARCH)/Hello.symbolgraphs", + "TARGET_NAME" : "Hello", + "TARGET_TEMP_DIR_SUFFIX" : "-t" + }, + "guid" : "PACKAGE-TARGET:Hello::BUILDCONFIG_1", + "impartedBuildProperties" : { + "buildSettings" : { + "FRAMEWORK_SEARCH_PATHS" : [ + "$(BUILT_PRODUCTS_DIR)/PackageFrameworks", + "$(inherited)" + ], + "LD_RUNPATH_SEARCH_PATHS" : [ + "$(RPATH_ORIGIN)", + "$(inherited)" + ], + "LD_WARN_DUPLICATE_LIBRARIES" : "NO", + "OTHER_CFLAGS" : [ + "-fmodule-map-file=$(GENERATED_MODULEMAP_DIR)/Hello.modulemap", + "$(inherited)" + ] + } + }, + "name" : "Release" + } + ], + "buildPhases" : [ + { + "buildFiles" : [ + { + "codeSignOnCopy" : "false", + "fileReference" : "PACKAGE:hello::MAINGROUP::REF_3::REF_0", + "guid" : "PACKAGE-TARGET:Hello::BUILDPHASE_0::0", + "platformFilters" : [ + + ], + "removeHeadersOnCopy" : "false" + }, + { + "codeSignOnCopy" : "false", + "fileReference" : "PACKAGE:hello::MAINGROUP::REF_3::REF_1", + "guid" : "PACKAGE-TARGET:Hello::BUILDPHASE_0::1", + "platformFilters" : [ + + ], + "removeHeadersOnCopy" : "false" + } + ], + "guid" : "PACKAGE-TARGET:Hello::BUILDPHASE_0", + "type" : "com.apple.buildphase.sources" + } + ], + "buildRules" : [ + + ], + "customTasks" : [ + + ], + "dependencies" : [ + + ], + "guid" : "PACKAGE-TARGET:Hello", + "name" : "Hello", + "productReference" : { + "guid" : "PRODUCTREF-PACKAGE-TARGET:Hello", + "name" : "$(EXECUTABLE_NAME)", + "type" : "file" + }, + "productTypeIdentifier" : "org.swift.product-type.library.static", + "type" : "standard" + }, + "signature" : "406e195565df919b35d1912d888854276b0b12bdff637cdbac9b1c636098ab3a", + "type" : "target" + }, + { + "contents" : { + "approvedByUser" : "true", + "buildConfigurations" : [ + { + "buildSettings" : { + "DEFINES_MODULE" : "YES", + "DOCC_EXTRACT_PROJECT_HEADERS_DOCUMENTATION" : "YES", + "GENERATE_PRELINK_OBJECT_FILE" : "NO", + "MODULEMAP_FILE_CONTENTS" : "module MyWidget {\nheader \"MyWidget-Swift.h\"\nexport *\n}", + "MODULEMAP_PATH" : "$(GENERATED_MODULEMAP_DIR)/MyWidget.modulemap", + "PACKAGE_RESOURCE_TARGET_KIND" : "regular", + "PRODUCT_BUNDLE_IDENTIFIER" : "hello.MyWidget", + "PRODUCT_MODULE_NAME" : "MyWidget", + "PRODUCT_NAME" : "$(TARGET_NAME)", + "SDKROOT" : "auto", + "SDK_VARIANT" : "auto", + "STRIP_INSTALLED_PRODUCT" : "NO", + "SUPPORTS_TEXT_BASED_API" : "NO", + "SWIFT_ACTIVE_COMPILATION_CONDITIONS" : [ + "$(inherited)", + "SWIFT_MODULE_RESOURCE_BUNDLE_UNAVAILABLE" + ], + "SWIFT_DISABLE_PARSE_AS_LIBRARY" : "NO", + "SWIFT_ENABLE_BARE_SLASH_REGEX" : "NO", + "SWIFT_LIBRARIES_ONLY" : "YES", + "SWIFT_OBJC_INTERFACE_HEADER_DIR" : "$(GENERATED_MODULEMAP_DIR)", + "SWIFT_OBJC_INTERFACE_HEADER_NAME" : "MyWidget-Swift.h", + "SWIFT_PACKAGE_NAME" : "hello", + "SWIFT_VERSION" : "6", + "SYMBOL_GRAPH_EXTRACTOR_OUTPUT_DIR" : "$(TARGET_BUILD_DIR)/$(CURRENT_ARCH)/MyWidget.symbolgraphs", + "TAPI_DYLIB_INSTALL_NAME" : "MyWidget", + "TAPI_EXTRACT_API_OUTPUT_DIR" : "$(TARGET_BUILD_DIR)/$(CURRENT_ARCH)/MyWidget.symbolgraphs", + "TARGET_NAME" : "MyWidget", + "TARGET_TEMP_DIR_SUFFIX" : "-t" + }, + "guid" : "PACKAGE-TARGET:MyWidget::BUILDCONFIG_0", + "impartedBuildProperties" : { + "buildSettings" : { + "FRAMEWORK_SEARCH_PATHS" : [ + "$(BUILT_PRODUCTS_DIR)/PackageFrameworks", + "$(inherited)" + ], + "LD_RUNPATH_SEARCH_PATHS" : [ + "$(RPATH_ORIGIN)", + "$(BUILT_PRODUCTS_DIR)/PackageFrameworks", + "$(inherited)" + ], + "LD_WARN_DUPLICATE_LIBRARIES" : "NO", + "OTHER_CFLAGS" : [ + "-fmodule-map-file=$(GENERATED_MODULEMAP_DIR)/MyWidget.modulemap", + "$(inherited)" + ] + } + }, + "name" : "Debug" + }, + { + "buildSettings" : { + "DEFINES_MODULE" : "YES", + "DOCC_EXTRACT_PROJECT_HEADERS_DOCUMENTATION" : "YES", + "GENERATE_PRELINK_OBJECT_FILE" : "NO", + "MODULEMAP_FILE_CONTENTS" : "module MyWidget {\nheader \"MyWidget-Swift.h\"\nexport *\n}", + "MODULEMAP_PATH" : "$(GENERATED_MODULEMAP_DIR)/MyWidget.modulemap", + "PACKAGE_RESOURCE_TARGET_KIND" : "regular", + "PRODUCT_BUNDLE_IDENTIFIER" : "hello.MyWidget", + "PRODUCT_MODULE_NAME" : "MyWidget", + "PRODUCT_NAME" : "$(TARGET_NAME)", + "SDKROOT" : "auto", + "SDK_VARIANT" : "auto", + "STRIP_INSTALLED_PRODUCT" : "NO", + "SUPPORTS_TEXT_BASED_API" : "NO", + "SWIFT_ACTIVE_COMPILATION_CONDITIONS" : [ + "$(inherited)", + "SWIFT_MODULE_RESOURCE_BUNDLE_UNAVAILABLE" + ], + "SWIFT_DISABLE_PARSE_AS_LIBRARY" : "NO", + "SWIFT_ENABLE_BARE_SLASH_REGEX" : "NO", + "SWIFT_LIBRARIES_ONLY" : "YES", + "SWIFT_OBJC_INTERFACE_HEADER_DIR" : "$(GENERATED_MODULEMAP_DIR)", + "SWIFT_OBJC_INTERFACE_HEADER_NAME" : "MyWidget-Swift.h", + "SWIFT_PACKAGE_NAME" : "hello", + "SWIFT_VERSION" : "6", + "SYMBOL_GRAPH_EXTRACTOR_OUTPUT_DIR" : "$(TARGET_BUILD_DIR)/$(CURRENT_ARCH)/MyWidget.symbolgraphs", + "TAPI_DYLIB_INSTALL_NAME" : "MyWidget", + "TAPI_EXTRACT_API_OUTPUT_DIR" : "$(TARGET_BUILD_DIR)/$(CURRENT_ARCH)/MyWidget.symbolgraphs", + "TARGET_NAME" : "MyWidget", + "TARGET_TEMP_DIR_SUFFIX" : "-t" + }, + "guid" : "PACKAGE-TARGET:MyWidget::BUILDCONFIG_1", + "impartedBuildProperties" : { + "buildSettings" : { + "FRAMEWORK_SEARCH_PATHS" : [ + "$(BUILT_PRODUCTS_DIR)/PackageFrameworks", + "$(inherited)" + ], + "LD_RUNPATH_SEARCH_PATHS" : [ + "$(RPATH_ORIGIN)", + "$(inherited)" + ], + "LD_WARN_DUPLICATE_LIBRARIES" : "NO", + "OTHER_CFLAGS" : [ + "-fmodule-map-file=$(GENERATED_MODULEMAP_DIR)/MyWidget.modulemap", + "$(inherited)" + ] + } + }, + "name" : "Release" + } + ], + "buildPhases" : [ + { + "buildFiles" : [ + { + "codeSignOnCopy" : "false", + "fileReference" : "PACKAGE:hello::MAINGROUP::REF_4::REF_0", + "guid" : "PACKAGE-TARGET:MyWidget::BUILDPHASE_0::0", + "platformFilters" : [ + + ], + "removeHeadersOnCopy" : "false" + } + ], + "guid" : "PACKAGE-TARGET:MyWidget::BUILDPHASE_0", + "type" : "com.apple.buildphase.sources" + } + ], + "buildRules" : [ + + ], + "customTasks" : [ + + ], + "dependencies" : [ + + ], + "dynamicTargetVariantGuid" : "PACKAGE-TARGET:MyWidget--4160AE51FF53E3CE-dynamic", + "guid" : "PACKAGE-TARGET:MyWidget", + "name" : "MyWidget", + "productReference" : { + "guid" : "PRODUCTREF-PACKAGE-TARGET:MyWidget", + "name" : "$(EXECUTABLE_NAME)", + "type" : "file" + }, + "productTypeIdentifier" : "org.swift.product-type.library.static", + "type" : "standard" + }, + "signature" : "65f048c8cb6d05d5d99bf3802fd8058ffcdcb0428470f78a8b10fbcdd416f4d3", + "type" : "target" + }, + { + "contents" : { + "buildConfigurations" : [ + { + "buildSettings" : { + + }, + "guid" : "PACKAGE-PRODUCT:hello-HelloPackageTests.HelloPackageTests::BUILDCONFIG_0", + "impartedBuildProperties" : { + "buildSettings" : { + + } + }, + "name" : "Debug" + }, + { + "buildSettings" : { + + }, + "guid" : "PACKAGE-PRODUCT:hello-HelloPackageTests.HelloPackageTests::BUILDCONFIG_1", + "impartedBuildProperties" : { + "buildSettings" : { + + } + }, + "name" : "Release" + } + ], + "buildPhases" : [ + + ], + "buildRules" : [ + + ], + "customTasks" : [ + + ], + "dependencies" : [ + { + "guid" : "PACKAGE-PRODUCT:hello_HelloTests.HelloTests", + "platformFilters" : [ + + ] + }, + { + "guid" : "PACKAGE-TARGET:HelloTests_test_runner", + "platformFilters" : [ + + ] + } + ], + "guid" : "PACKAGE-PRODUCT:hello-HelloPackageTests.HelloPackageTests", + "name" : "HelloPackageTests-product", + "type" : "aggregate" + }, + "signature" : "92bf7ab8b6f69b56e2745999713d6be6a737097b70fb7b09b82256b59bc8ce67", + "type" : "target" + }, + { + "contents" : { + "approvedByUser" : "true", + "buildConfigurations" : [ + { + "buildSettings" : { + "DEFINES_MODULE" : "YES", + "DOCC_EXTRACT_PROJECT_HEADERS_DOCUMENTATION" : "YES", + "EXECUTABLE_PREFIX" : "lib", + "EXECUTABLE_PREFIX[__platform_filter=windows-msvc]" : "", + "MODULEMAP_FILE_CONTENTS" : "module MyWidget {\nheader \"MyWidget-Swift.h\"\nexport *\n}", + "MODULEMAP_PATH" : "$(GENERATED_MODULEMAP_DIR)/MyWidget.modulemap", + "PACKAGE_RESOURCE_TARGET_KIND" : "regular", + "PRODUCT_BUNDLE_IDENTIFIER" : "hello.MyWidget", + "PRODUCT_MODULE_NAME" : "MyWidget", + "PRODUCT_NAME" : "MyWidget", + "SDKROOT" : "auto", + "SDK_VARIANT" : "auto", + "SUPPORTS_TEXT_BASED_API" : "NO", + "SWIFT_ACTIVE_COMPILATION_CONDITIONS" : [ + "$(inherited)", + "SWIFT_MODULE_RESOURCE_BUNDLE_UNAVAILABLE" + ], + "SWIFT_DISABLE_PARSE_AS_LIBRARY" : "NO", + "SWIFT_ENABLE_BARE_SLASH_REGEX" : "NO", + "SWIFT_LIBRARIES_ONLY" : "YES", + "SWIFT_OBJC_INTERFACE_HEADER_DIR" : "$(GENERATED_MODULEMAP_DIR)", + "SWIFT_OBJC_INTERFACE_HEADER_NAME" : "MyWidget-Swift.h", + "SWIFT_PACKAGE_NAME" : "hello", + "SWIFT_VERSION" : "6", + "SYMBOL_GRAPH_EXTRACTOR_OUTPUT_DIR" : "$(TARGET_BUILD_DIR)/$(CURRENT_ARCH)/MyWidget.symbolgraphs", + "TAPI_EXTRACT_API_OUTPUT_DIR" : "$(TARGET_BUILD_DIR)/$(CURRENT_ARCH)/MyWidget.symbolgraphs", + "TARGET_NAME" : "MyWidget", + "TARGET_TEMP_DIR_SUFFIX" : "-t" + }, + "guid" : "PACKAGE-TARGET:MyWidget--4160AE51FF53E3CE-dynamic::BUILDCONFIG_0", + "impartedBuildProperties" : { + "buildSettings" : { + "FRAMEWORK_SEARCH_PATHS" : [ + "$(BUILT_PRODUCTS_DIR)/PackageFrameworks", + "$(inherited)" + ], + "LD_RUNPATH_SEARCH_PATHS" : [ + "$(RPATH_ORIGIN)", + "$(BUILT_PRODUCTS_DIR)/PackageFrameworks", + "$(inherited)" + ], + "LD_WARN_DUPLICATE_LIBRARIES" : "NO", + "OTHER_CFLAGS" : [ + "-fmodule-map-file=$(GENERATED_MODULEMAP_DIR)/MyWidget.modulemap", + "$(inherited)" + ] + } + }, + "name" : "Debug" + }, + { + "buildSettings" : { + "DEFINES_MODULE" : "YES", + "DOCC_EXTRACT_PROJECT_HEADERS_DOCUMENTATION" : "YES", + "EXECUTABLE_PREFIX" : "lib", + "EXECUTABLE_PREFIX[__platform_filter=windows-msvc]" : "", + "MODULEMAP_FILE_CONTENTS" : "module MyWidget {\nheader \"MyWidget-Swift.h\"\nexport *\n}", + "MODULEMAP_PATH" : "$(GENERATED_MODULEMAP_DIR)/MyWidget.modulemap", + "PACKAGE_RESOURCE_TARGET_KIND" : "regular", + "PRODUCT_BUNDLE_IDENTIFIER" : "hello.MyWidget", + "PRODUCT_MODULE_NAME" : "MyWidget", + "PRODUCT_NAME" : "MyWidget", + "SDKROOT" : "auto", + "SDK_VARIANT" : "auto", + "SUPPORTS_TEXT_BASED_API" : "NO", + "SWIFT_ACTIVE_COMPILATION_CONDITIONS" : [ + "$(inherited)", + "SWIFT_MODULE_RESOURCE_BUNDLE_UNAVAILABLE" + ], + "SWIFT_DISABLE_PARSE_AS_LIBRARY" : "NO", + "SWIFT_ENABLE_BARE_SLASH_REGEX" : "NO", + "SWIFT_LIBRARIES_ONLY" : "YES", + "SWIFT_OBJC_INTERFACE_HEADER_DIR" : "$(GENERATED_MODULEMAP_DIR)", + "SWIFT_OBJC_INTERFACE_HEADER_NAME" : "MyWidget-Swift.h", + "SWIFT_PACKAGE_NAME" : "hello", + "SWIFT_VERSION" : "6", + "SYMBOL_GRAPH_EXTRACTOR_OUTPUT_DIR" : "$(TARGET_BUILD_DIR)/$(CURRENT_ARCH)/MyWidget.symbolgraphs", + "TAPI_EXTRACT_API_OUTPUT_DIR" : "$(TARGET_BUILD_DIR)/$(CURRENT_ARCH)/MyWidget.symbolgraphs", + "TARGET_NAME" : "MyWidget", + "TARGET_TEMP_DIR_SUFFIX" : "-t" + }, + "guid" : "PACKAGE-TARGET:MyWidget--4160AE51FF53E3CE-dynamic::BUILDCONFIG_1", + "impartedBuildProperties" : { + "buildSettings" : { + "FRAMEWORK_SEARCH_PATHS" : [ + "$(BUILT_PRODUCTS_DIR)/PackageFrameworks", + "$(inherited)" + ], + "LD_RUNPATH_SEARCH_PATHS" : [ + "$(RPATH_ORIGIN)", + "$(inherited)" + ], + "LD_WARN_DUPLICATE_LIBRARIES" : "NO", + "OTHER_CFLAGS" : [ + "-fmodule-map-file=$(GENERATED_MODULEMAP_DIR)/MyWidget.modulemap", + "$(inherited)" + ] + } + }, + "name" : "Release" + } + ], + "buildPhases" : [ + { + "buildFiles" : [ + { + "codeSignOnCopy" : "false", + "fileReference" : "PACKAGE:hello::MAINGROUP::REF_5::REF_0", + "guid" : "PACKAGE-TARGET:MyWidget--4160AE51FF53E3CE-dynamic::BUILDPHASE_0::0", + "platformFilters" : [ + + ], + "removeHeadersOnCopy" : "false" + } + ], + "guid" : "PACKAGE-TARGET:MyWidget--4160AE51FF53E3CE-dynamic::BUILDPHASE_0", + "type" : "com.apple.buildphase.sources" + } + ], + "buildRules" : [ + + ], + "customTasks" : [ + + ], + "dependencies" : [ + + ], + "guid" : "PACKAGE-TARGET:MyWidget--4160AE51FF53E3CE-dynamic", + "name" : "MyWidget", + "productReference" : { + "guid" : "PRODUCTREF-PACKAGE-TARGET:MyWidget--4160AE51FF53E3CE-dynamic", + "name" : "$(EXECUTABLE_NAME)", + "type" : "file" + }, + "productTypeIdentifier" : "com.apple.product-type.library.dynamic", + "type" : "standard" + }, + "signature" : "7379381b124adc9f1681d646309fe4cca96928ae0def3a735f24a1f14b6fe864", + "type" : "target" + }, + { + "contents" : { + "buildConfigurations" : [ + { + "buildSettings" : { + "PRODUCT_NAME" : "$(TARGET_NAME)", + "SDKROOT" : "auto", + "SDK_VARIANT" : "auto", + "SKIP_INSTALL" : "YES", + "SUPPORTED_PLATFORMS" : [ + "$(AVAILABLE_PLATFORMS)" + ] + }, + "guid" : "AGGREGATE::BUILDCONFIG_0", + "impartedBuildProperties" : { + "buildSettings" : { + + } + }, + "name" : "Debug" + }, + { + "buildSettings" : { + "PRODUCT_NAME" : "$(TARGET_NAME)", + "SDKROOT" : "auto", + "SDK_VARIANT" : "auto", + "SKIP_INSTALL" : "YES", + "SUPPORTED_PLATFORMS" : [ + "$(AVAILABLE_PLATFORMS)" + ] + }, + "guid" : "AGGREGATE::BUILDCONFIG_1", + "impartedBuildProperties" : { + "buildSettings" : { + + } + }, + "name" : "Release" + } + ], + "defaultConfigurationName" : "Release", + "developmentRegion" : "en", + "groupTree" : { + "children" : [ + + ], + "guid" : "AGGREGATE::MAINGROUP", + "name" : "", + "path" : "", + "sourceTree" : "", + "type" : "group" + }, + "guid" : "AGGREGATE", + "path" : "/work/Hello/Package.swift", + "projectDirectory" : "/work/Hello", + "projectIsPackage" : "true", + "projectName" : "Aggregate", + "targets" : [ + "b0e177441ada5d502e1343ed3c6098089575650f2d97479e182103f23e9c4ec9", + "ac314e4b65c318b1916eb0551e231b0b6ed6a8efe5f89d9b2512084d6dd189c5" + ] + }, + "signature" : "c8ab7e08118d83c9b147267ffab231358f72066a6e807dc4314dbe293a0c45af", + "type" : "project" + }, + { + "contents" : { + "buildConfigurations" : [ + { + "buildSettings" : { + + }, + "guid" : "ALL-INCLUDING-TESTS::BUILDCONFIG_0", + "impartedBuildProperties" : { + "buildSettings" : { + + } + }, + "name" : "Debug" + }, + { + "buildSettings" : { + + }, + "guid" : "ALL-INCLUDING-TESTS::BUILDCONFIG_1", + "impartedBuildProperties" : { + "buildSettings" : { + + } + }, + "name" : "Release" + } + ], + "buildPhases" : [ + + ], + "buildRules" : [ + + ], + "customTasks" : [ + + ], + "dependencies" : [ + { + "guid" : "PACKAGE-PRODUCT:hello_HelloTests.HelloTests", + "platformFilters" : [ + + ] + }, + { + "guid" : "PACKAGE-TARGET:HelloTests_test_runner", + "platformFilters" : [ + + ] + }, + { + "guid" : "PACKAGE-PRODUCT:hello_Hello.Hello", + "platformFilters" : [ + + ] + }, + { + "guid" : "PACKAGE-TARGET:Hello", + "platformFilters" : [ + + ] + }, + { + "guid" : "PACKAGE-TARGET:MyWidget", + "platformFilters" : [ + + ] + } + ], + "guid" : "ALL-INCLUDING-TESTS", + "name" : "AllIncludingTests", + "type" : "aggregate" + }, + "signature" : "b0e177441ada5d502e1343ed3c6098089575650f2d97479e182103f23e9c4ec9", + "type" : "target" + }, + { + "contents" : { + "buildConfigurations" : [ + { + "buildSettings" : { + + }, + "guid" : "ALL-EXCLUDING-TESTS::BUILDCONFIG_0", + "impartedBuildProperties" : { + "buildSettings" : { + + } + }, + "name" : "Debug" + }, + { + "buildSettings" : { + + }, + "guid" : "ALL-EXCLUDING-TESTS::BUILDCONFIG_1", + "impartedBuildProperties" : { + "buildSettings" : { + + } + }, + "name" : "Release" + } + ], + "buildPhases" : [ + + ], + "buildRules" : [ + + ], + "customTasks" : [ + + ], + "dependencies" : [ + { + "guid" : "PACKAGE-PRODUCT:hello_Hello.Hello", + "platformFilters" : [ + + ] + }, + { + "guid" : "PACKAGE-TARGET:Hello", + "platformFilters" : [ + + ] + }, + { + "guid" : "PACKAGE-TARGET:MyWidget", + "platformFilters" : [ + + ] + } + ], + "guid" : "ALL-EXCLUDING-TESTS", + "name" : "AllExcludingTests", + "type" : "aggregate" + }, + "signature" : "ac314e4b65c318b1916eb0551e231b0b6ed6a8efe5f89d9b2512084d6dd189c5", + "type" : "target" + } +] diff --git a/Tests/XToolTests/Fixtures/workspace-only.json b/Tests/XToolTests/Fixtures/workspace-only.json new file mode 100644 index 00000000..57373d35 --- /dev/null +++ b/Tests/XToolTests/Fixtures/workspace-only.json @@ -0,0 +1,12 @@ +[ + { + "type": "workspace", + "signature": "workspace-signature", + "contents": { + "guid": "workspace", + "name": "Fixture Workspace", + "path": "/tmp/Fixture", + "projects": [] + } + } +] diff --git a/Tests/XToolTests/Fixtures/workspace-with-target.json b/Tests/XToolTests/Fixtures/workspace-with-target.json new file mode 100644 index 00000000..e23a6a0e --- /dev/null +++ b/Tests/XToolTests/Fixtures/workspace-with-target.json @@ -0,0 +1,48 @@ +[ + { + "type": "workspace", + "signature": "workspace-signature", + "contents": { + "guid": "workspace", + "name": "Fixture Workspace", + "path": "/tmp/Fixture", + "projects": ["project-signature"] + } + }, + { + "type": "project", + "signature": "project-signature", + "contents": { + "guid": "project", + "projectName": "Fixture Project", + "projectIsPackage": "true", + "path": "/tmp/Fixture/Project.xcodeproj", + "projectDirectory": "/tmp/Fixture", + "buildConfigurations": [], + "defaultConfigurationName": "Release", + "groupTree": { + "type": "group", + "guid": "project::MAINGROUP", + "sourceTree": "", + "path": "", + "name": "", + "children": [] + }, + "targets": ["target-signature"] + } + }, + { + "type": "target", + "signature": "target-signature", + "contents": { + "type": "aggregate", + "guid": "aggregate-target", + "name": "Fixture Aggregate", + "dependencies": [], + "buildRules": [], + "buildPhases": [], + "buildConfigurations": [], + "customTasks": [] + } + } +] diff --git a/Tests/XToolTests/Fixtures/xcode-project.json b/Tests/XToolTests/Fixtures/xcode-project.json new file mode 100644 index 00000000..51a2b0f4 --- /dev/null +++ b/Tests/XToolTests/Fixtures/xcode-project.json @@ -0,0 +1,248 @@ +[ + { + "contents": { + "guid": "76d16796b5fada9968ee2530ea43ddc3", + "name": "Probe", + "path": "/tmp/XcodeFixture/Probe.xcodeproj/project.xcworkspace", + "projects": [ + "PROJECT@v11_mod=a185f829b0ec766f5b7f5452265db1ac_hash=e199eb287a19a5a0c958424ee41fbfd2plugins=1OJSG6M1FOV3XYQCBH7Z29RZ0FPR9XDE1" + ] + }, + "signature": "WORKSPACE@v11_hash=da7c32131a38c07ce5c6bd4a806881da_subobjects=62ed7487a98748c26563ed057d056fb7", + "type": "workspace" + }, + { + "contents": { + "buildConfigurations": [ + { + "buildSettings": { + "ALWAYS_SEARCH_USER_PATHS": "NO", + "CLANG_ANALYZER_NONNULL": "YES", + "CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION": "YES_AGGRESSIVE", + "CLANG_CXX_LANGUAGE_STANDARD": "gnu++14", + "CLANG_CXX_LIBRARY": "libc++", + "CLANG_ENABLE_MODULES": "YES", + "CLANG_ENABLE_OBJC_ARC": "YES", + "CLANG_ENABLE_OBJC_WEAK": "YES", + "CLANG_WARN__DUPLICATE_METHOD_MATCH": "YES", + "CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING": "YES", + "CLANG_WARN_BOOL_CONVERSION": "YES", + "CLANG_WARN_COMMA": "YES", + "CLANG_WARN_CONSTANT_CONVERSION": "YES", + "CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS": "YES", + "CLANG_WARN_DIRECT_OBJC_ISA_USAGE": "YES_ERROR", + "CLANG_WARN_DOCUMENTATION_COMMENTS": "YES", + "CLANG_WARN_EMPTY_BODY": "YES", + "CLANG_WARN_ENUM_CONVERSION": "YES", + "CLANG_WARN_INFINITE_RECURSION": "YES", + "CLANG_WARN_INT_CONVERSION": "YES", + "CLANG_WARN_NON_LITERAL_NULL_CONVERSION": "YES", + "CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF": "YES", + "CLANG_WARN_OBJC_LITERAL_CONVERSION": "YES", + "CLANG_WARN_OBJC_ROOT_CLASS": "YES_ERROR", + "CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER": "YES", + "CLANG_WARN_RANGE_LOOP_ANALYSIS": "YES", + "CLANG_WARN_STRICT_PROTOTYPES": "YES", + "CLANG_WARN_SUSPICIOUS_MOVE": "YES", + "CLANG_WARN_UNGUARDED_AVAILABILITY": "YES_AGGRESSIVE", + "CLANG_WARN_UNREACHABLE_CODE": "YES", + "COPY_PHASE_STRIP": "NO", + "DEBUG_INFORMATION_FORMAT": "dwarf", + "ENABLE_STRICT_OBJC_MSGSEND": "YES", + "ENABLE_TESTABILITY": "YES", + "GCC_C_LANGUAGE_STANDARD": "gnu11", + "GCC_DYNAMIC_NO_PIC": "NO", + "GCC_NO_COMMON_BLOCKS": "YES", + "GCC_OPTIMIZATION_LEVEL": "0", + "GCC_PREPROCESSOR_DEFINITIONS": "$(inherited) DEBUG=1", + "GCC_WARN_64_TO_32_BIT_CONVERSION": "YES", + "GCC_WARN_ABOUT_RETURN_TYPE": "YES_ERROR", + "GCC_WARN_UNDECLARED_SELECTOR": "YES", + "GCC_WARN_UNINITIALIZED_AUTOS": "YES_AGGRESSIVE", + "GCC_WARN_UNUSED_FUNCTION": "YES", + "GCC_WARN_UNUSED_VARIABLE": "YES", + "IPHONEOS_DEPLOYMENT_TARGET": "17.0", + "MTL_ENABLE_DEBUG_INFO": "INCLUDE_SOURCE", + "MTL_FAST_MATH": "YES", + "ONLY_ACTIVE_ARCH": "YES", + "PRODUCT_NAME": "$(TARGET_NAME)", + "SDKROOT": "iphoneos", + "SWIFT_ACTIVE_COMPILATION_CONDITIONS": "DEBUG", + "SWIFT_OPTIMIZATION_LEVEL": "-Onone", + "SWIFT_VERSION": "5.0" + }, + "guid": "e199eb287a19a5a0c958424ee41fbfd2becc6934627368c14bb884aa8f71b387", + "name": "Debug" + }, + { + "buildSettings": { + "ALWAYS_SEARCH_USER_PATHS": "NO", + "CLANG_ANALYZER_NONNULL": "YES", + "CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION": "YES_AGGRESSIVE", + "CLANG_CXX_LANGUAGE_STANDARD": "gnu++14", + "CLANG_CXX_LIBRARY": "libc++", + "CLANG_ENABLE_MODULES": "YES", + "CLANG_ENABLE_OBJC_ARC": "YES", + "CLANG_ENABLE_OBJC_WEAK": "YES", + "CLANG_WARN__DUPLICATE_METHOD_MATCH": "YES", + "CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING": "YES", + "CLANG_WARN_BOOL_CONVERSION": "YES", + "CLANG_WARN_COMMA": "YES", + "CLANG_WARN_CONSTANT_CONVERSION": "YES", + "CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS": "YES", + "CLANG_WARN_DIRECT_OBJC_ISA_USAGE": "YES_ERROR", + "CLANG_WARN_DOCUMENTATION_COMMENTS": "YES", + "CLANG_WARN_EMPTY_BODY": "YES", + "CLANG_WARN_ENUM_CONVERSION": "YES", + "CLANG_WARN_INFINITE_RECURSION": "YES", + "CLANG_WARN_INT_CONVERSION": "YES", + "CLANG_WARN_NON_LITERAL_NULL_CONVERSION": "YES", + "CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF": "YES", + "CLANG_WARN_OBJC_LITERAL_CONVERSION": "YES", + "CLANG_WARN_OBJC_ROOT_CLASS": "YES_ERROR", + "CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER": "YES", + "CLANG_WARN_RANGE_LOOP_ANALYSIS": "YES", + "CLANG_WARN_STRICT_PROTOTYPES": "YES", + "CLANG_WARN_SUSPICIOUS_MOVE": "YES", + "CLANG_WARN_UNGUARDED_AVAILABILITY": "YES_AGGRESSIVE", + "CLANG_WARN_UNREACHABLE_CODE": "YES", + "COPY_PHASE_STRIP": "NO", + "DEBUG_INFORMATION_FORMAT": "dwarf-with-dsym", + "ENABLE_NS_ASSERTIONS": "NO", + "ENABLE_STRICT_OBJC_MSGSEND": "YES", + "GCC_C_LANGUAGE_STANDARD": "gnu11", + "GCC_NO_COMMON_BLOCKS": "YES", + "GCC_WARN_64_TO_32_BIT_CONVERSION": "YES", + "GCC_WARN_ABOUT_RETURN_TYPE": "YES_ERROR", + "GCC_WARN_UNDECLARED_SELECTOR": "YES", + "GCC_WARN_UNINITIALIZED_AUTOS": "YES_AGGRESSIVE", + "GCC_WARN_UNUSED_FUNCTION": "YES", + "GCC_WARN_UNUSED_VARIABLE": "YES", + "IPHONEOS_DEPLOYMENT_TARGET": "17.0", + "MTL_ENABLE_DEBUG_INFO": "NO", + "MTL_FAST_MATH": "YES", + "PRODUCT_NAME": "$(TARGET_NAME)", + "SDKROOT": "iphoneos", + "SWIFT_COMPILATION_MODE": "wholemodule", + "SWIFT_OPTIMIZATION_LEVEL": "-O", + "SWIFT_VERSION": "5.0" + }, + "guid": "e199eb287a19a5a0c958424ee41fbfd24396a4590027712d42d9bb6288ae690c", + "name": "Release" + } + ], + "classPrefix": "", + "defaultConfigurationName": "Debug", + "developmentRegion": "en", + "groupTree": { + "children": [ + { + "fileType": "sourcecode.swift", + "guid": "e199eb287a19a5a0c958424ee41fbfd28f63b54eb04ab9fa6382285fd598b2b2", + "path": "App.swift", + "sourceTree": "", + "type": "file" + }, + { + "guid": "e199eb287a19a5a0c958424ee41fbfd25cac4b9d413b38a60e200a98f62c887e", + "name": "Products", + "path": "", + "sourceTree": "", + "type": "group" + } + ], + "guid": "e199eb287a19a5a0c958424ee41fbfd26791c3857da8aa5de2253ccbd228fcf4", + "name": "Probe", + "path": "", + "sourceTree": "", + "type": "group" + }, + "guid": "e199eb287a19a5a0c958424ee41fbfd2", + "knownRegions": [ + "Base", + "en" + ], + "path": "/tmp/XcodeFixture/Probe.xcodeproj", + "projectDirectory": "/tmp/XcodeFixture", + "targets": [ + "TARGET@v11_hash=d87aa33fea2895c0ec717a30ce07d946" + ] + }, + "signature": "PROJECT@v11_mod=a185f829b0ec766f5b7f5452265db1ac_hash=e199eb287a19a5a0c958424ee41fbfd2plugins=1OJSG6M1FOV3XYQCBH7Z29RZ0FPR9XDE1", + "type": "project" + }, + { + "contents": { + "buildConfigurations": [ + { + "buildSettings": { + "ASSETCATALOG_COMPILER_APPICON_NAME": "AppIcon", + "CODE_SIGN_IDENTITY": "iPhone Developer", + "GENERATE_INFOPLIST_FILE": "YES", + "LD_RUNPATH_SEARCH_PATHS": "$(inherited) @executable_path/Frameworks", + "PRODUCT_BUNDLE_IDENTIFIER": "com.example.Probe", + "PRODUCT_NAME": "CustomApp", + "SDKROOT": "iphoneos", + "SWIFT_VERSION": "6.0", + "TARGETED_DEVICE_FAMILY": "1,2" + }, + "guid": "e199eb287a19a5a0c958424ee41fbfd2b209ce43c50a552356fbc4fac5e57ffb", + "name": "Debug" + }, + { + "buildSettings": { + "ASSETCATALOG_COMPILER_APPICON_NAME": "AppIcon", + "CODE_SIGN_IDENTITY": "iPhone Developer", + "GENERATE_INFOPLIST_FILE": "YES", + "LD_RUNPATH_SEARCH_PATHS": "$(inherited) @executable_path/Frameworks", + "PRODUCT_BUNDLE_IDENTIFIER": "com.example.Probe", + "PRODUCT_NAME": "CustomApp", + "SDKROOT": "iphoneos", + "SWIFT_VERSION": "6.0", + "TARGETED_DEVICE_FAMILY": "1,2" + }, + "guid": "e199eb287a19a5a0c958424ee41fbfd29faf9568becf2252d5df952d59d33039", + "name": "Release" + } + ], + "buildPhases": [ + { + "buildFiles": [ + { + "fileReference": "e199eb287a19a5a0c958424ee41fbfd28f63b54eb04ab9fa6382285fd598b2b2", + "guid": "e199eb287a19a5a0c958424ee41fbfd2198a16cd2dba8b2b1897a667252b2cb4" + } + ], + "guid": "e199eb287a19a5a0c958424ee41fbfd2e86d8f7517e2d1f6df6abbc126d3b47b", + "type": "com.apple.buildphase.sources" + } + ], + "buildRules": [], + "dependencies": [], + "guid": "e199eb287a19a5a0c958424ee41fbfd2687c4f52285acc59fe15a024562ab8d7", + "name": "Probe", + "predominantSourceCodeLanguage": "Xcode.SourceCodeLanguage.Swift", + "productReference": { + "guid": "e199eb287a19a5a0c958424ee41fbfd29ed18f4a6114b6bbf69cb1f25bf2986c", + "name": "CustomApp.app", + "type": "product" + }, + "productTypeIdentifier": "com.apple.product-type.application", + "provisioningSourceData": [ + { + "bundleIdentifierFromInfoPlist": "$(PRODUCT_BUNDLE_IDENTIFIER)", + "configurationName": "Debug", + "provisioningStyle": 0 + }, + { + "bundleIdentifierFromInfoPlist": "$(PRODUCT_BUNDLE_IDENTIFIER)", + "configurationName": "Release", + "provisioningStyle": 0 + } + ], + "type": "standard" + }, + "signature": "TARGET@v11_hash=d87aa33fea2895c0ec717a30ce07d946", + "type": "target" + } +] diff --git a/Tests/XToolTests/PIFContainerTests.swift b/Tests/XToolTests/PIFContainerTests.swift new file mode 100644 index 00000000..621204e9 --- /dev/null +++ b/Tests/XToolTests/PIFContainerTests.swift @@ -0,0 +1,107 @@ +import XPIF +import Foundation +import Testing +@testable import XToolSupport + +@Test func pifContainerEncodesFlatPIFObjects() throws { + let target = XPIF.AggregateTarget(id: "target", name: "Target") + let project = XPIF.Project( + id: "project", + path: "/tmp/Project.xcodeproj", + projectDir: "/tmp", + name: "Project", + targets: [.aggregate(target)] + ) + let pif = XPIF.Container(workspace: .init( + id: "workspace", name: "Workspace", path: "/tmp", projects: [project] + )) + + let data = try pif.data() + let objects = try #require(JSONSerialization.jsonObject(with: data) as? [[String: Any]]) + #expect(objects.count == 3) + #expect(objects.compactMap { $0["type"] as? String } == ["workspace", "project", "target"]) + + let workspace = try #require(objects[0]["contents"] as? [String: Any]) + let projectContents = try #require(objects[1]["contents"] as? [String: Any]) + let targetContents = try #require(objects[2]["contents"] as? [String: Any]) + let projectSignature = try #require(objects[1]["signature"] as? String) + let targetSignature = try #require(objects[2]["signature"] as? String) + #expect(workspace["guid"] as? String == "workspace") + #expect(workspace["projects"] as? [String] == [projectSignature]) + #expect(projectContents["targets"] as? [String] == [targetSignature]) + #expect(targetContents["type"] as? String == "aggregate") +} + +@Test func pifContainerRoundTripsProjectsAndTargets() throws { + let standard = XPIF.Target( + id: "standard-target", + productType: .executable, + name: "Executable", + productName: "Executable" + ) + let aggregate = XPIF.AggregateTarget(id: "aggregate-target", name: "Aggregate") + let project = XPIF.Project( + id: "project", + path: "/tmp/Project.xcodeproj", + projectDir: "/tmp", + name: "Project", + targets: [.target(standard), .aggregate(aggregate)] + ) + let original = XPIF.Container(workspace: .init( + id: "workspace", + name: "Workspace", + path: "/tmp", + projects: [project] + )) + + let decoded = try XPIF.Container(data: original.data()) + #expect(decoded.workspace.id == original.workspace.id) + #expect(decoded.workspace.name == original.workspace.name) + #expect(decoded.workspace.path == original.workspace.path) + #expect(decoded.workspace.projects.map(\.id) == [project.id]) + #expect(decoded.workspace.projects[0].targets.map(\.id) == [standard.id, aggregate.id]) + #expect(try !decoded.data().isEmpty) +} + +@Test func pifContainerDecodesWorkspace() throws { + let decoded = try XPIF.Container(data: fixtureData("workspace-only")) + #expect(decoded.workspace.name == "Fixture Workspace") + #expect(decoded.workspace.projects.isEmpty) +} + +@Test func pifContainerDecodesWorkspaceWithTarget() throws { + let decoded = try XPIF.Container(data: fixtureData("workspace-with-target")) + #expect(decoded.workspace.name == "Fixture Workspace") + #expect(decoded.workspace.projects.map(\.name) == ["Fixture Project"]) + #expect(decoded.workspace.projects[0].targets.map(\.id) == ["aggregate-target"]) +} + +@Test func pifContainerDecodesComplexWorkspace() throws { + let complexData = try fixtureData("complex") + let decoded = try XPIF.Container(data: complexData) + #expect(decoded.workspace.name == "Hello") +} + +private func fixtureData(_ name: String) throws -> Data { + let url = fixtureURL("\(name).json") + return try Data(contentsOf: url) +} + +@Test func pifContainerRejectsMissingProjectReference() throws { + let original = XPIF.Container(workspace: .init( + id: "workspace", + name: "Workspace", + path: "/tmp", + projects: [XPIF.Project(id: "project", path: "/tmp/Project.xcodeproj", projectDir: "/tmp", name: "Project")] + )) + let data = try original.data() + var objects = try #require(JSONSerialization.jsonObject(with: data) as? [[String: Any]]) + var contents = try #require(objects[0]["contents"] as? [String: Any]) + contents["projects"] = ["missing-signature"] + objects[0]["contents"] = contents + + let invalidData = try JSONSerialization.data(withJSONObject: objects) + #expect(throws: DecodingError.self) { + try XPIF.Container(data: invalidData) + } +} diff --git a/Tests/XToolTests/TestFixtures.swift b/Tests/XToolTests/TestFixtures.swift new file mode 100644 index 00000000..47a42a1a --- /dev/null +++ b/Tests/XToolTests/TestFixtures.swift @@ -0,0 +1,5 @@ +import Foundation + +func fixtureURL(_ path: String) -> URL { + URL(filePath: #filePath).deletingLastPathComponent().appending(path: "Fixtures/\(path)") +} diff --git a/Tests/XToolTests/XPIFTests.swift b/Tests/XToolTests/XPIFTests.swift new file mode 100644 index 00000000..93e82d75 --- /dev/null +++ b/Tests/XToolTests/XPIFTests.swift @@ -0,0 +1,217 @@ +import Foundation +import Testing +import XPIF + +struct XPIFTests { + @Test func preservesXcodeFieldsWhenEditingAndResigning() throws { + let fixture = fixtureURL("xcode-project.json") + let original = try #require(JSONSerialization.jsonObject(with: Data(contentsOf: fixture)) as? [[String: Any]]) + var model = try XPIF.Container(data: JSONSerialization.data(withJSONObject: original)) + model.workspace.name = "Edited" + let encoded = try model.data() + let result = try #require(JSONSerialization.jsonObject(with: encoded) as? [[String: Any]]) + for object in original { + let originalContents = try #require(object["contents"] as? [String: Any]) + let guid = try #require(originalContents["guid"] as? String) + let output = try #require(result.first { ($0["contents"] as? [String: Any])?["guid"] as? String == guid }) + let contents = try #require(output["contents"] as? [String: Any]) + for (key, value) in originalContents where !["projects", "targets", "name"].contains(key) { + try expectContains(jsonValue(contents[key]), original: jsonValue(value), path: "\(guid).\(key)") + } + } + #expect(try XPIF.Container(data: encoded).data() == encoded) + } + + @Test func constructsXcodeModelWithMetadataAndUnknownFields() throws { + var config = XPIF.BuildConfig(id: "debug", name: "Debug", settings: .init()) + config.baseConfigurationFileReference = "xcconfig" + config.settings[single: "CUSTOM_SETTING[sdk=iphoneos*][arch=arm64]"] = "value" + config.additionalFields["futureConfigField"] = .array([.null, .bool(true)]) + var target = XPIF.Target(id: "target", productType: .appExtension, name: "Extension", productName: "Extension.appex") + target.productReference.id = "actual-product-reference" + target.productReference.additionalFields["futureProductField"] = .number(42) + target.provisioningSourceData = [.init( + configurationName: "Debug", provisioningStyle: .manual, bundleIdentifierFromInfoPlist: "com.example.extension" + )] + target.isPackageTarget = false + target.classPrefix = "EX" + target.predominantSourceCodeLanguage = "Xcode.SourceCodeLanguage.Swift" + target.performanceTestsBaselinesPath = "Baselines" + target.buildConfigs = [config] + target.additionalFields["futureTargetField"] = .object(["data": .array([.number(1), .string("two")])]) + var project = XPIF.Project(id: "project", path: "/tmp/Project.xcodeproj", projectDir: "/tmp", name: "Project") + project.targets = [.target(target)] + project.buildConfigs = [config] + project.defaultConfigurationName = "Debug" + project.knownRegions = ["en", "Base"] + project.classPrefix = "PR" + project.appPreferencesBuildSettings = .init() + project.appPreferencesBuildSettings?[single: "DEVELOPMENT_TEAM"] = "TEAM" + project.additionalFields["futureProjectField"] = .bool(false) + var workspace = XPIF.Workspace(id: "workspace", name: "Workspace", path: "/tmp", projects: [project]) + workspace.additionalFields["futureWorkspaceField"] = .string("kept") + let decoded = try XPIF.Container(data: XPIF.Container(workspace: workspace).data()) + let decodedProject = decoded.workspace.projects[0] + guard case .target(let decodedTarget) = decodedProject.targets[0] else { + Issue.record("Expected a standard target") + return + } + #expect(decodedTarget.productReference == target.productReference) + #expect(decodedTarget.provisioningSourceData == target.provisioningSourceData) + #expect(decodedTarget.classPrefix == "EX") + #expect(decodedTarget.predominantSourceCodeLanguage == target.predominantSourceCodeLanguage) + #expect(decodedTarget.performanceTestsBaselinesPath == "Baselines") + #expect(decodedTarget.additionalFields == target.additionalFields) + #expect(decodedProject.buildConfigs == project.buildConfigs) + #expect(decodedProject.defaultConfigurationName == "Debug") + #expect(decodedProject.knownRegions == ["en", "Base"]) + #expect(decodedProject.appPreferencesBuildSettings == project.appPreferencesBuildSettings) + #expect(decodedProject.additionalFields == project.additionalFields) + #expect(decoded.workspace.additionalFields == workspace.additionalFields) + } + + @Test func roundTripsLocalizedAndVersionedGroups() throws { + let data = Data(""" + {"guid":"root","type":"group","sourceTree":"","futureField":7,"children":[ + {"guid":"localized","type":"variantGroup","name":"Main.storyboard","children":[ + {"guid":"en","type":"file","sourceTree":"SDKROOT","path":"en.lproj/Main.storyboard", + "fileType":"file.storyboard","regionVariantName":"en","fileTextEncoding":"utf-8", + "expectedSignature":"signature","futureFileField":true} + ]}, + {"guid":"versions","type":"versionGroup","path":"Model.xcdatamodeld","children":[]} + ]} + """.utf8) + var group = try JSONDecoder().decode(XPIF.Group.self, from: data) + group.name = "Edited" + let decoded = try roundTrip(group) + #expect(decoded == group) + guard case .group(let localized) = decoded.subitems[0], case .file(let file) = localized.subitems[0], + case .group(let versions) = decoded.subitems[1] else { + Issue.record("Expected localized and versioned groups") + return + } + #expect(localized.kind == .variantGroup) + #expect(versions.kind == .versionGroup) + #expect(file.pathBase == .buildSetting("SDKROOT")) + #expect(file.regionVariantName == "en") + #expect(file.fileTextEncoding == "utf-8") + #expect(file.expectedSignature == "signature") + #expect(file.additionalFields["futureFileField"] == .bool(true)) + } + + @Test(arguments: ["fileReference", "targetReference", "named"]) + func roundTripsBuildFileAttributes(reference: String) throws { + var object: [String: Any] = [ + "guid": "buildfile", "headerVisibility": "public", "intentsCodegenVisibility": "private", + "resourceRule": "copy", "additionalCompilerOptions": ["-DTEST", "-Werror"], + "codeSignOnCopy": "true", "removeHeadersOnCopy": "true", "shouldLinkWeakly": "true", + "assetTags": ["tag"], "decompress": "true", "shouldWarnIfNoRuleToProcess": "false", + "migCodegenFiles": "client", "futureField": ["value": true], + ] + if reference == "named" { + object["name"] = "UIKit.framework" + object["fileType"] = "wrapper.framework" + } else { + object[reference] = "reference" + } + let file = try JSONDecoder().decode(XPIF.BuildFile.self, from: JSONSerialization.data(withJSONObject: object)) + #expect(file.headerVisibility == .public) + #expect(file.codeSignOnCopy && file.removeHeadersOnCopy && file.shouldLinkWeakly) + #expect(file.generatedCodeVisibility == .private) + #expect(file.resourceRule == .copy) + #expect(file.additionalCompilerOptions == .array(["-DTEST", "-Werror"])) + #expect(file.assetTags == ["tag"]) + #expect(file.platformFilters.isEmpty) + #expect(try roundTrip(file) == file) + } + + @Test func roundTripsScriptFileListsAndDependencyInfo() throws { + let data = Data(""" + {"guid":"script","type":"com.apple.buildphase.shell-script","name":"Generate", + "scriptContents":"echo generated","inputFileListPaths":["Inputs.xcfilelist"], + "outputFileListPaths":["Outputs.xcfilelist"],"dependencyFileFormat":"makefile", + "dependencyFilePaths":["generated.d"],"futurePhaseField":[false,null]} + """.utf8) + let phase = try JSONDecoder().decode(XPIF.ShellScriptBuildPhase.self, from: data) + #expect(phase.inputFileListPaths == ["Inputs.xcfilelist"]) + #expect(phase.outputFileListPaths == ["Outputs.xcfilelist"]) + #expect(phase.dependencyInfo == .makefile("generated.d")) + #expect(phase.sandboxingOverride == .basedOnBuildSetting) + #expect(try roundTrip(phase) == phase) + } + + @Test func preservesArbitraryBuildSettingConditionsAndProductTypes() throws { + let settings = Data(""" + {"OTHER_SWIFT_FLAGS[__platform_filter=ios;ios-simulator]":["$(inherited)"], + "CUSTOM[sdk=iphoneos*][arch=arm64]":"value","PLUGIN_FLAGS":["one","two"]} + """.utf8) + let decoded = try JSONDecoder().decode(XPIF.BuildSettings.self, from: settings) + #expect(try jsonValue(JSONSerialization.jsonObject(with: JSONEncoder().encode(decoded))) + == jsonValue(JSONSerialization.jsonObject(with: settings))) + let type = XPIF.Target.ProductType(rawValue: "custom.plugin.product-type") + #expect(try roundTrip(type) == type) + } + + @Test func constructsAndDecodesExternalTarget() throws { + var target = XPIF.ExternalTarget(id: "external", name: "External", toolPath: .string("/usr/bin/make")) + target.arguments = .array(["all"]) + target.additionalFields["futureExternalField"] = .null + let decoded = try roundTrip(XPIF.BaseTarget.external(target)) + #expect(decoded == .external(target)) + } + + @Test func changingTargetResignsItsProjectAndWorkspace() throws { + let fixture = fixtureURL("xcode-project.json") + var model = try XPIF.Container(data: Data(contentsOf: fixture)) + let original = try signatures(in: model) + model.workspace.projects[0].targets[0].common.name = "Edited" + let edited = try signatures(in: model) + #expect(original.count == 3) + #expect(zip(original, edited).allSatisfy { $0 != $1 }) + #expect(try signatures(in: model) == edited) + } + + @Test func rejectsInvalidEnumAndDependencyData() throws { + #expect(throws: DecodingError.self) { + try JSONDecoder().decode(XPIF.SandboxingOverride.self, from: Data("\"unknown\"".utf8)) + } + let invalid = Data(""" + {"guid":"rule","name":"Rule","fileTypeIdentifier":"pattern.proxy","filePatterns":"*.in", + "compilerSpecificationIdentifier":"com.apple.compilers.proxy.script","scriptContents":"echo", + "inputFilePaths":[],"outputFilePaths":[],"dependencyFileFormat":"makefile","dependencyFilePaths":[]} + """.utf8) + #expect(throws: DecodingError.self) { try JSONDecoder().decode(XPIF.BuildRule.self, from: invalid) } + } + + private func roundTrip(_ value: T) throws -> T { + try JSONDecoder().decode(T.self, from: JSONEncoder().encode(value)) + } + + private func jsonValue(_ value: Any?) throws -> XPIF.JSONValue { + try JSONDecoder().decode(XPIF.JSONValue.self, from: JSONSerialization.data( + withJSONObject: value ?? NSNull(), options: [.fragmentsAllowed] + )) + } + + private func signatures(in model: XPIF.Container) throws -> [String] { + let objects = try #require(JSONSerialization.jsonObject(with: model.data()) as? [[String: Any]]) + return objects.compactMap { $0["signature"] as? String } + } + + private func expectContains(_ result: XPIF.JSONValue, original: XPIF.JSONValue, path: String) throws { + switch (result, original) { + case (.object(let actual), .object(let expected)): + for (key, value) in expected { + let field = try #require(actual[key], "Missing field: \(path).\(key)") + try expectContains(field, original: value, path: "\(path).\(key)") + } + case (.array(let actual), .array(let expected)): + #expect(actual.count == expected.count) + for (index, values) in zip(actual, expected).enumerated() { + try expectContains(values.0, original: values.1, path: "\(path)[\(index)]") + } + default: + #expect(result == original, "Changed field: \(path)") + } + } +} From 5aa56489e2994d251f384402504f25c460f01c94 Mon Sep 17 00:00:00 2001 From: Kabir Oberai Date: Tue, 20 Oct 2026 18:53:22 -0400 Subject: [PATCH 02/15] swb: directly invoke SwiftBuild --- .swiftlint.yml | 9 + Dockerfile | 1 + Package.resolved | 56 ++- Package.swift | 10 +- Sources/PackLib/BuildSettings.swift | 127 ++--- Sources/PackLib/DarwinSDK.swift | 71 ++- Sources/PackLib/PackSchema.swift | 2 +- Sources/PackLib/Packer.swift | 268 ----------- Sources/PackLib/Planner.swift | 453 ------------------ Sources/XToolSupport/BuildCoordinator.swift | 193 ++++++++ Sources/XToolSupport/BuildDescription.swift | 19 + .../XToolSupport/BuildSessionManager.swift | 178 +++++++ Sources/XToolSupport/DevBSPCommand.swift | 20 +- Sources/XToolSupport/DevCommand.swift | 145 +++--- Sources/XToolSupport/EmulateToolCommand.swift | 46 ++ Sources/XToolSupport/EmulatedTools+Live.swift | 152 ++++++ Sources/XToolSupport/EmulatedTools.swift | 58 +++ .../XToolSupport/ProjectBuildOperation.swift | 116 +++++ Sources/XToolSupport/SDKBuilder.swift | 361 ++------------ Sources/XToolSupport/SDKCommand.swift | 2 +- Sources/XToolSupport/Signposter.swift | 21 + Sources/XToolSupport/SwiftBuildService.swift | 9 + .../SwiftPMBuildDescriber+Products.swift | 116 +++++ .../SwiftPMBuildDescriber+XcodeProject.swift} | 107 +++-- .../XToolSupport/SwiftPMBuildDescriber.swift | 212 ++++++++ Sources/XToolSupport/SwiftPMPIFDumper.swift | 46 ++ Sources/XToolSupport/XTool.swift | 4 + .../XToolSupport/XToolInstallerDelegate.swift | 4 + Sources/XUtils/Foundation+Utils.swift | 27 ++ .../BuildCoordinatorTripleTests.swift | 71 +++ Tests/XToolTests/DarwinSDKTests.swift | 149 +++++- .../DirectBuildDescriptionTests.swift | 153 ++++++ Tests/XToolTests/PackerTests.swift | 15 - .../SwiftPMBuildDescriberTests.swift | 257 ++++++++++ 34 files changed, 2166 insertions(+), 1312 deletions(-) delete mode 100644 Sources/PackLib/Packer.swift delete mode 100644 Sources/PackLib/Planner.swift create mode 100644 Sources/XToolSupport/BuildCoordinator.swift create mode 100644 Sources/XToolSupport/BuildDescription.swift create mode 100644 Sources/XToolSupport/BuildSessionManager.swift create mode 100644 Sources/XToolSupport/EmulateToolCommand.swift create mode 100644 Sources/XToolSupport/EmulatedTools+Live.swift create mode 100644 Sources/XToolSupport/EmulatedTools.swift create mode 100644 Sources/XToolSupport/ProjectBuildOperation.swift create mode 100644 Sources/XToolSupport/Signposter.swift create mode 100644 Sources/XToolSupport/SwiftBuildService.swift create mode 100644 Sources/XToolSupport/SwiftPMBuildDescriber+Products.swift rename Sources/{PackLib/XcodePacker.swift => XToolSupport/SwiftPMBuildDescriber+XcodeProject.swift} (74%) create mode 100644 Sources/XToolSupport/SwiftPMBuildDescriber.swift create mode 100644 Sources/XToolSupport/SwiftPMPIFDumper.swift create mode 100644 Tests/XToolTests/BuildCoordinatorTripleTests.swift create mode 100644 Tests/XToolTests/DirectBuildDescriptionTests.swift delete mode 100644 Tests/XToolTests/PackerTests.swift create mode 100644 Tests/XToolTests/SwiftPMBuildDescriberTests.swift diff --git a/.swiftlint.yml b/.swiftlint.yml index 4e610427..aa4d1556 100644 --- a/.swiftlint.yml +++ b/.swiftlint.yml @@ -51,6 +51,15 @@ custom_rules: - identifier message: "Use d.out.print instead of print or Swift.print." severity: error + prefer_xtool_subprocess_output: + name: "Subprocess Dependency Output" + included: '/Sources/' + excluded: '/(Tests|Examples)/' + regex: '(?<=\.)\s*currentStandard(?:Output|Error)\b' + match_kinds: + - identifier + message: "Use .xtoolOutput() or .xtoolError() instead of .currentStandardOutput or .currentStandardError, respectively." + severity: error spaces_not_tabs: name: "Spaces not Tabs" regex: ^\t diff --git a/Dockerfile b/Dockerfile index 561ee77e..793dc4dc 100644 --- a/Dockerfile +++ b/Dockerfile @@ -20,6 +20,7 @@ RUN apt-get update \ curl libcurl4-openssl-dev \ zip unzip \ liblzma-dev zlib1g-dev \ + libsqlite3-dev libncurses-dev \ && rm -rf /var/lib/apt/lists/* diff --git a/Package.resolved b/Package.resolved index fd48bf56..24bb1b16 100644 --- a/Package.resolved +++ b/Package.resolved @@ -1,5 +1,5 @@ { - "originHash" : "2ccd077fc130f0f1b1279d87aa61d1fc243964d7ad49489a12021577c54df9de", + "originHash" : "5008a43e53ee72f8c3441b7879d0528a1595547ef65419251a5b65a72a347a0a", "pins" : [ { "identity" : "aexml", @@ -118,6 +118,15 @@ "version" : "1.3.1" } }, + { + "identity" : "swift-build", + "kind" : "remoteSourceControl", + "location" : "https://github.com/xtool-org/swift-build", + "state" : { + "branch" : "release/6.4.x", + "revision" : "48b9868b66c3aeab33e0237844f35815c235f5b8" + } + }, { "identity" : "swift-certificates", "kind" : "remoteSourceControl", @@ -217,6 +226,15 @@ "version" : "1.0.0" } }, + { + "identity" : "swift-driver", + "kind" : "remoteSourceControl", + "location" : "https://github.com/xtool-org/swift-driver.git", + "state" : { + "branch" : "release/6.4.x", + "revision" : "8e7a60e14b81b451f2bb8d844e927cdff62d73a5" + } + }, { "identity" : "swift-http-structured-headers", "kind" : "remoteSourceControl", @@ -244,6 +262,15 @@ "version" : "2.1.2" } }, + { + "identity" : "swift-llbuild", + "kind" : "remoteSourceControl", + "location" : "https://github.com/xtool-org/swift-llbuild.git", + "state" : { + "branch" : "release/6.4.x", + "revision" : "cb8b25e399e8f2fa82c5894736def23bea5dceb3" + } + }, { "identity" : "swift-log", "kind" : "remoteSourceControl", @@ -397,6 +424,33 @@ "version" : "1.8.1" } }, + { + "identity" : "swift-toolchain-sqlite", + "kind" : "remoteSourceControl", + "location" : "https://github.com/swiftlang/swift-toolchain-sqlite", + "state" : { + "revision" : "d9b11cb79071d5ab992ff47dfd2e8d6e19418d97", + "version" : "1.0.13" + } + }, + { + "identity" : "swift-tools-protocols", + "kind" : "remoteSourceControl", + "location" : "https://github.com/swiftlang/swift-tools-protocols.git", + "state" : { + "branch" : "release/6.4.x", + "revision" : "4345e69c5572ec3d1f737fba90bca024b32da5cf" + } + }, + { + "identity" : "swift-tools-support-core", + "kind" : "remoteSourceControl", + "location" : "https://github.com/swiftlang/swift-tools-support-core.git", + "state" : { + "branch" : "release/6.4.x", + "revision" : "75a4f8586889fff3813f88ac3a0179ad568247e0" + } + }, { "identity" : "swiftymobiledevice", "kind" : "remoteSourceControl", diff --git a/Package.swift b/Package.swift index 73cf6211..5662a22e 100644 --- a/Package.swift +++ b/Package.swift @@ -54,6 +54,7 @@ let package = Package( .package(url: "https://github.com/xtool-org/xtool-core", .upToNextMinor(from: "1.5.1")), .package(url: "https://github.com/xtool-org/SwiftyMobileDevice", .upToNextMinor(from: "1.7.0")), .package(url: "https://github.com/xtool-org/swift-rcodesign", .upToNextMinor(from: "0.2.0")), + .package(url: "https://github.com/xtool-org/swift-build", branch: "release/6.4.x"), .package(url: "https://github.com/apple/swift-system", from: "1.4.0"), .package(url: "https://github.com/apple/swift-http-types", from: "1.3.1"), @@ -176,6 +177,7 @@ let package = Package( name: "XToolTests", dependencies: [ "XPIF", + "XcodeProjects", "XKit", "XToolSupport", "XCLIUtils", @@ -183,6 +185,7 @@ let package = Package( .product(name: "Crypto", package: "swift-crypto"), .product(name: "X509", package: "swift-certificates"), .product(name: "CustomDump", package: "swift-custom-dump"), + .product(name: "SwiftBuild", package: "swift-build"), ], exclude: ["Fixtures"], swiftSettings: swiftSettings, @@ -199,14 +202,19 @@ let package = Package( .target( name: "XToolSupport", dependencies: [ + "XPIF", + "XcodeProjects", "SwiftyMobileDevice", "XKit", "XCLIUtils", "PackLib", .product(name: "ArgumentParser", package: "swift-argument-parser"), + .product(name: "ConcurrencyExtras", package: "swift-concurrency-extras"), .product(name: "SystemPackage", package: "swift-system"), .product(name: "Dependencies", package: "swift-dependencies"), .product(name: "libunxip", package: "unxip"), + .product(name: "SWBBuildService", package: "swift-build"), + .product(name: "SwiftBuild", package: "swift-build"), ], cSettings: cSettings, swiftSettings: swiftSettings, @@ -222,10 +230,8 @@ let package = Package( name: "PackLib", dependencies: [ "XUtils", - "XcodeProjects", .product(name: "Superutils", package: "xtool-core"), .product(name: "Yams", package: "Yams"), - .product(name: "XcodeProjectFormat", package: "xcode-project-format"), ], swiftSettings: swiftSettings, ), diff --git a/Sources/PackLib/BuildSettings.swift b/Sources/PackLib/BuildSettings.swift index 9d5a041c..08a5c911 100644 --- a/Sources/PackLib/BuildSettings.swift +++ b/Sources/PackLib/BuildSettings.swift @@ -11,28 +11,25 @@ public struct BuildSettings: Sendable { public var packagePath: String public let configuration: BuildConfiguration public let triple: String - public let buildSystem: BuildSystem public let customOptions: [String] - public var sdkOptions: [String] - public var sdkEnvironment: [Environment.Key: String?] + public var sdkEnvironment: [Environment.Key: String?] { + return [ + // xcrun passes an SDKROOT that messes with our sdk configuration + "SDKROOT": nil, + ] + } private var configOptions: [String] { return [ - "--configuration", configuration.rawValue, - "--build-system", buildSystem.pmName, + "--build-system", "swiftbuild", "--package-path", packagePath, ] } - private var resolvedBaseOptions: [String] { - configOptions + sdkOptions + customOptions - } - public init( configuration: BuildConfiguration, triple: String, - buildSystem: BuildSystem? = nil, packagePath: String = ".", options: [String] = [] ) async throws { @@ -40,36 +37,6 @@ public struct BuildSettings: Sendable { self.configuration = configuration self.customOptions = options self.triple = triple - self.buildSystem = if let buildSystem { buildSystem } else { try await .default() } - - self.sdkEnvironment = [ - // xcrun passes an SDKROOT that messes with our sdk configuration - "SDKROOT": nil, - ] - - switch self.buildSystem { - case .swiftPM: - // on macOS we don't explicitly install a Swift SDK but - // SwiftPM vends "implicit" Darwin SDKs as of Swift 6.1, - // i.e. we can pass `--swift-sdk arm64-apple-ios` and it - // just works. See: - // https://github.com/swiftlang/swift-package-manager/pull/6828 - self.sdkOptions = ["--swift-sdk", triple] - case .swiftBuild: - self.sdkOptions = ["--triple", triple] - #if !os(macOS) - let darwinSDK = try DarwinSDK.current() - .orThrow(StringError("No Darwin SDK configured. Please run `xtool setup`.")) - self.sdkOptions += [ - "--toolset", "\(darwinSDK.bundle.path)/toolset-swb.json", - ] - self.sdkEnvironment.merge([ - "XCODE_EXTRA_PLATFORM_FOLDERS": "\(darwinSDK.bundle.path)/Developer/Platforms", - // SWB looks for dsymutil in the PATH. Other stuff (lld, libtool) is handled by toolset-swb.json. - "PATH": "\(darwinSDK.bundle.path)/toolset/bin:\(ProcessInfo.processInfo.environment["PATH"] ?? "")", - ]) { $1 } - #endif - } } #if os(macOS) @@ -107,6 +74,44 @@ public struct BuildSettings: Sendable { } #endif + private static let _runtimeResourcePath = Task { + struct SwiftTargetInfo: Decodable { + struct Paths: Decodable { + var runtimeResourcePath: String + } + var paths: Paths + } + let process = try await Subprocess.run( + .path(try await swiftcURL()), + arguments: ["-print-target-info"], + output: .data(limit: .max) + ).checkSuccess() + let targetInfo = try JSONDecoder().decode( + SwiftTargetInfo.self, + from: process.standardOutput + ) + return targetInfo.paths.runtimeResourcePath + } + + public static func runtimeResourcePath() async throws -> String { + try await _runtimeResourcePath.value + } + + public static func toolchainPath() async throws -> String { + // we can't simply return the parent of `swiftc` because the `swiftc` in PATH + // might be a trampoline (e.g. xcrun or swiftly) + let toolchainResourcePath = try await runtimeResourcePath() + let trimmed: Substring + if toolchainResourcePath.hasSuffix("/usr/lib/swift") { + trimmed = toolchainResourcePath.dropLast("/usr/lib/swift".count) + } else if toolchainResourcePath.hasSuffix("/usr/local/lib/swift") { + trimmed = toolchainResourcePath.dropLast("/usr/local/lib/swift".count) + } else { + throw StringError("Unexpected toolchain resource path: \(toolchainResourcePath)") + } + return trimmed.isEmpty ? "/" : String(trimmed) + } + public func withPackagePath(_ path: String) -> Self { var copy = self copy.packagePath = path @@ -138,31 +143,11 @@ public struct BuildSettings: Sendable { return Configuration( executable: executable, - arguments: .init(baseArguments + resolvedBaseOptions + arguments), + arguments: .init(baseArguments + configOptions + customOptions + arguments), environment: .inherit.updating(sdkEnvironment), platformOptions: .withGracefulShutDown, ) } - - // loosely based on - // https://github.com/swiftlang/sourcekit-lsp/blob/c55899a/Sources/BuildServerIntegration/ExternalBuildServerAdapter.swift#L92 - private var baseBuildServerArguments: [String] { - return [ - "experimental-build-server", - "--disable-automatic-resolution", - "--scratch-path", ".build/index-build", - // this requires Swift 6.4 but we need 6.4 on Linux anyway, for the platform toolset fixes - "--experimental-skip-acquiring-lock", - ] - } - - public var buildServerArguments: [String] { - ["package"] + baseBuildServerArguments + resolvedBaseOptions - } - - public func buildServerInvocation() async throws -> Subprocess.Configuration { - try await swiftPMInvocation(forTool: "package", arguments: baseBuildServerArguments) - } } public enum BuildConfiguration: String, CaseIterable, Sendable { @@ -176,23 +161,3 @@ public enum BuildConfiguration: String, CaseIterable, Sendable { } } } - -public enum BuildSystem: Sendable { - case swiftPM - case swiftBuild - - public static func `default`() async throws -> Self { - if try await SwiftVersion.current.supportsSwiftBuild { - return .swiftBuild - } else { - return .swiftPM - } - } - - var pmName: String { - switch self { - case .swiftPM: "native" - case .swiftBuild: "swiftbuild" - } - } -} diff --git a/Sources/PackLib/DarwinSDK.swift b/Sources/PackLib/DarwinSDK.swift index cadd72dd..04263e42 100644 --- a/Sources/PackLib/DarwinSDK.swift +++ b/Sources/PackLib/DarwinSDK.swift @@ -3,7 +3,7 @@ import XUtils import Subprocess import Superutils -public struct DarwinSDK { +public struct DarwinSDK: Sendable { package struct TemporaryBundle: ~Copyable { package let url: URL @@ -16,10 +16,8 @@ public struct DarwinSDK { throw StringError("Invalid Darwin SDK at '\(url.path)'") } - try await DarwinSDK.addHostClangResourceDir(to: url) - let destination = url.deletingLastPathComponent() - .appendingPathComponent("darwin.artifactbundle", isDirectory: true) + .appendingPathComponent("darwin.xtoolsdk", isDirectory: true) try FileManager.default.moveItem(at: url, to: destination) } @@ -28,7 +26,7 @@ public struct DarwinSDK { } } - public enum Flavor { + public enum Flavor: Sendable { // can't be updated in place case slim // can be updated in place, includes a whole copy of Xcode.app @@ -41,6 +39,10 @@ public struct DarwinSDK { public let version: String public let flavor: Flavor + package var developerDirectory: URL { + bundle.appendingPathComponent("Xcode.app/Contents/Developer", isDirectory: true) + } + static func swiftPMDirectory( environment: [String: String] = ProcessInfo.processInfo.environment, homeDirectory: URL = FileManager.default.homeDirectoryForCurrentUser @@ -69,60 +71,55 @@ public struct DarwinSDK { if let version = try? Data(contentsOf: bundle.appendingPathComponent("darwin-sdk-version.txt")) { self.version = String(decoding: version, as: UTF8.self) .trimmingCharacters(in: .whitespacesAndNewlines) - } else if ["darwin.xtoolsdk", "darwin.artifactbundle", "darwin.artifactbundle.tmp"].contains(bundle.lastPathComponent) { + } else if [ + "darwin.xtoolsdk", "darwin.xtoolsdk.tmp", "darwin.artifactbundle", "darwin.artifactbundle.tmp", + ].contains(bundle.lastPathComponent) { self.version = "develop" } else { return nil } + let xcodeApp = bundle.appendingPathComponent("Xcode.app", isDirectory: true) + let slimMarker = bundle.appendingPathComponent("slim-marker", isDirectory: false).path + if version == "develop" { self.flavor = .legacy - } else if bundle.appendingPathComponent("Xcode.app").dirExists { + } else if xcodeApp.dirExists && !FileManager.default.fileExists(atPath: slimMarker) { self.flavor = .normal } else { self.flavor = .slim } } - package static func prepareTemporaryBundle( - environment: [String: String] = ProcessInfo.processInfo.environment, - homeDirectory: URL = FileManager.default.homeDirectoryForCurrentUser - ) throws -> TemporaryBundle { - let sdksDirectory = try swiftSDKsDirectory(environment: environment, homeDirectory: homeDirectory) - try FileManager.default.createDirectory(at: sdksDirectory, withIntermediateDirectories: true) - let bundle = sdksDirectory.appendingPathComponent("darwin.artifactbundle.tmp", isDirectory: true) + package static func prepareTemporaryBundle() throws -> TemporaryBundle { + let directory = d.persistentDirectory + try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true) + let bundle = directory.appendingPathComponent("darwin.xtoolsdk.tmp", isDirectory: true) if FileManager.default.fileExists(atPath: bundle.path) { try FileManager.default.removeItem(at: bundle) } return TemporaryBundle(url: bundle) } - private static func addHostClangResourceDir(to sdk: URL) async throws { - struct SwiftTargetInfo: Decodable { - struct Paths: Decodable { - let runtimeResourcePath: String - } - let paths: Paths - } - - let process = try await Subprocess.run( - .path(try await BuildSettings.swiftcURL()), - arguments: ["-print-target-info"], - output: .data(limit: .max) - ).checkSuccess() - let targetInfo = try JSONDecoder().decode( - SwiftTargetInfo.self, - from: process.standardOutput + public static func current() throws -> DarwinSDK? { + try current( + environment: ProcessInfo.processInfo.environment, + homeDirectory: FileManager.default.homeDirectoryForCurrentUser ) - let hostClangResources = URL(filePath: targetInfo.paths.runtimeResourcePath).appending(path: "clang") - let hostInclude = hostClangResources.appending(path: "include") - let sdkInclude = sdk.appending(path: "Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/clang/include") - try await FileManager.default.copyItem(at: hostInclude, to: sdkInclude, preserveOwner: false) } - public static func current() throws -> DarwinSDK? { - let bundle = try swiftSDKsDirectory().appendingPathComponent("darwin.artifactbundle", isDirectory: true) - guard bundle.dirExists else { return nil } + static func current( + environment: [String: String], + homeDirectory: URL + ) throws -> DarwinSDK? { + let bundle = d.persistentDirectory.appendingPathComponent("darwin.xtoolsdk", isDirectory: true) + if !bundle.dirExists { + let legacyBundle = try swiftSDKsDirectory(environment: environment, homeDirectory: homeDirectory) + .appendingPathComponent("darwin.artifactbundle", isDirectory: true) + guard legacyBundle.dirExists else { return nil } + try FileManager.default.createDirectory(at: bundle.deletingLastPathComponent(), withIntermediateDirectories: true) + try FileManager.default.moveItem(at: legacyBundle, to: bundle) + } return DarwinSDK(bundle: bundle) } diff --git a/Sources/PackLib/PackSchema.swift b/Sources/PackLib/PackSchema.swift index 297c4241..7da0e211 100644 --- a/Sources/PackLib/PackSchema.swift +++ b/Sources/PackLib/PackSchema.swift @@ -42,7 +42,7 @@ public struct PackSchema: Sendable { case orgID(String) case bundleID(String) - func formBundleID(product: String) -> String { + public func formBundleID(product: String) -> String { switch self { case .orgID(let orgID): "\(orgID).\(product)" case .bundleID(let bundleID): bundleID diff --git a/Sources/PackLib/Packer.swift b/Sources/PackLib/Packer.swift deleted file mode 100644 index 6506fe7d..00000000 --- a/Sources/PackLib/Packer.swift +++ /dev/null @@ -1,268 +0,0 @@ -import Foundation -import XUtils -import Subprocess - -public struct Packer: Sendable { - public let buildSettings: BuildSettings - public let plan: Plan - - public init(buildSettings: BuildSettings, plan: Plan) { - self.plan = plan - self.buildSettings = buildSettings - } - - private func build() async throws { - let xtoolDir = URL(fileURLWithPath: "xtool") - let packageDir = xtoolDir.appendingPathComponent(".xtool-tmp") - try? FileManager.default.removeItem(at: packageDir) - try FileManager.default.createDirectory(at: packageDir, withIntermediateDirectories: true) - - if URL(fileURLWithPath: "Packages").dirExists { - try FileManager.default.createSymbolicLink( - atPath: packageDir.appendingPathComponent("Packages").path, - withDestinationPath: "../../Packages" - ) - } - - let packageSwift = packageDir.appendingPathComponent("Package.swift") - let contents = """ - // swift-tools-version: 6.0 - import PackageDescription - let package = Package( - name: "\(plan.app.product)-Builder", - platforms: [ - .iOS("\(plan.app.deploymentTarget)"), - ], - dependencies: [ - .package(name: "RootPackage", path: "../.."), - ], - targets: [ - \( - plan.allProducts.map { - """ - .executableTarget( - name: "\($0.targetName)", - dependencies: [ - .product(name: "\($0.product)", package: "RootPackage"), - ], - linkerSettings: \($0.linkerSettings) - ) - """ - } - .joined(separator: ",\n") - ) - ] - )\n - """ - try Data(contents.utf8).write(to: packageSwift) - - for product in plan.allProducts { - let sources: URL = packageDir.appendingPathComponent("Sources/\(product.targetName)", isDirectory: true) - try FileManager.default.createDirectory(at: sources, withIntermediateDirectories: true) - try Data().write(to: sources.appendingPathComponent("stub.c", isDirectory: false)) - } - - let buildConfig = try await buildSettings - .withPackagePath(packageDir.path) - .swiftPMInvocation( - forTool: "build", - arguments: [ - "--scratch-path", ".build", - // resolving can cause SwiftPM to overwrite the root package deps - // with just the deps needed for the builder package (which is to - // say, any "dev dependencies" of the root package may be removed.) - // fortunately we've already resolved the root package by this point - // in order to dump the plan, so we can skip resolution here to skirt - // the issue. - "--disable-automatic-resolution", - ], - ) - try await Subprocess.run( - buildConfig, - output: .currentStandardOutput, - error: .currentStandardError, - ) - .checkSuccess() - } - - public func pack() async throws -> URL { - try await build() - - let output = try TemporaryDirectory(name: "\(plan.app.product).app") - - let outputURL = output.url - - let binPath: String - switch buildSettings.buildSystem { - case .swiftPM: - binPath = "\(buildSettings.triple)/\(buildSettings.configuration.rawValue)" - case .swiftBuild: - let platformName = buildSettings.triple.contains("simulator") ? "iphonesimulator" : "iphoneos" - binPath = "out/Products/\(buildSettings.configuration.swiftBuildValue)-\(platformName)" - } - let binDir = URL( - fileURLWithPath: ".build/\(binPath)", - isDirectory: true - ) - - try await withThrowingTaskGroup(of: Void.self) { group in - for product in plan.allProducts { - try pack( - product: product, - binDir: binDir, - outputURL: product.directory(inApp: outputURL), - &group - ) - } - - while !group.isEmpty { - do { - try await group.next() - } catch is CancellationError { - // continue - } catch { - group.cancelAll() - throw error - } - } - } - - let dest = URL(fileURLWithPath: "xtool").appendingPathComponent(outputURL.lastPathComponent) - try? FileManager.default.removeItem(at: dest) - try output.persist(at: dest) - return dest - } - - @Sendable private func pack( - product: Plan.Product, - binDir: URL, - outputURL: URL, - _ group: inout ThrowingTaskGroup - ) throws { - @Sendable func packFileToRoot(srcName: String) async throws { - let srcURL = URL(fileURLWithPath: srcName) - let destURL = outputURL.appendingPathComponent(srcURL.lastPathComponent) - try FileManager.default.copyItem(at: srcURL, to: destURL) - - try Task.checkCancellation() - } - - @Sendable func packFile(srcName: String, dstName: String? = nil, sign: Bool = false) async throws { - let srcURL = URL(fileURLWithPath: srcName, relativeTo: binDir) - let dstURL = URL(fileURLWithPath: dstName ?? srcURL.lastPathComponent, relativeTo: outputURL) - try? FileManager.default.createDirectory(at: dstURL.deletingLastPathComponent(), withIntermediateDirectories: true) - try FileManager.default.copyItem(at: srcURL, to: dstURL) - - try Task.checkCancellation() - } - - // Ensure output directory is available - try? FileManager.default.createDirectory(at: outputURL, withIntermediateDirectories: true) - - for command in product.resources { - group.addTask { - switch command { - case .bundle(let package, let target): - try await packFile(srcName: "\(package)_\(target).bundle") - case .binaryTarget(let name): - let src = URL(fileURLWithPath: "\(name).framework/\(name)", relativeTo: binDir) - let magic = Data("!\n".utf8) - let thinMagic = Data("!\n".utf8) - guard let bytes = try? FileHandle(forReadingFrom: src).read(upToCount: magic.count) else { - // if we can't find the binary, it might be a static framework that SwiftPM - // did not copy into the .build directory. we don't need to pack it anyway. - break - } - // if the magic matches one of these it's a static archive; don't embed it. - // https://github.com/apple/llvm-project/blob/e716ff14c46490d2da6b240806c04e2beef01f40/llvm/include/llvm/Object/Archive.h#L33 - // swiftlint:disable:previous line_length - if bytes != magic && bytes != thinMagic { - try await packFile(srcName: "\(name).framework", dstName: "Frameworks/\(name).framework", sign: true) - } - case .library(let name): - try await packFile(srcName: "lib\(name).dylib", dstName: "Frameworks/lib\(name).dylib", sign: true) - case .root(let source): - try await packFileToRoot(srcName: source) - } - } - } - if let iconPath = product.iconPath { - group.addTask { - try await packFileToRoot(srcName: iconPath) - } - } - group.addTask { - try await packFile(srcName: product.targetName, dstName: product.product) - } - group.addTask { - var info = product.infoPlist - - if product.type == .application { - info["UIRequiredDeviceCapabilities"] = ["arm64"] - info["LSRequiresIPhoneOS"] = true - info["CFBundleSupportedPlatforms"] = Packer.cfBundleSupportedPlatforms(for: buildSettings.triple) - } - - if let iconPath = product.iconPath { - let iconName = URL(fileURLWithPath: iconPath).deletingPathExtension().lastPathComponent - info["CFBundleIconFile"] = iconName - } - - let infoPath = outputURL.appendingPathComponent("Info.plist") - let encodedPlist = try PropertyListSerialization.data( - fromPropertyList: info, - format: .xml, - options: 0 - ) - try encodedPlist.write(to: infoPath) - } - } - - static func cfBundleSupportedPlatforms(for triple: String) -> [String] { - let components = triple.split(separator: "-").map(String.init) - guard let appleIndex = components.firstIndex(of: "apple"), - components.indices.contains(appleIndex + 1) else { - return ["iPhoneOS"] - } - - let os = components[appleIndex + 1].prefix { !$0.isNumber } - switch os { - case "ios" where components.contains("simulator"): - return ["iPhoneSimulator"] - case "ios": - return ["iPhoneOS"] - case "macos", "macosx": - return ["MacOSX"] - default: - return ["iPhoneOS"] - } - } -} - -extension Plan.Product { - fileprivate var linkerSettings: String { - switch self.type { - case .application: """ - [ - .unsafeFlags([ - "-Xlinker", "-rpath", "-Xlinker", "@executable_path/Frameworks", - ]), - ] - """ - case .appExtension: """ - [ - // Link to Foundation framework which implements the _NSExtensionMain entrypoint - .linkedFramework("Foundation"), - .unsafeFlags([ - // Set the entry point to Foundation`_NSExtensionMain - "-Xlinker", "-e", "-Xlinker", "_NSExtensionMain", - // Include frameworks that the host app may use - "-Xlinker", "-rpath", "-Xlinker", "@executable_path/../../Frameworks", - // ...as well as our own - "-Xlinker", "-rpath", "-Xlinker", "@executable_path/Frameworks", - ]), - ] - """ - } - } -} diff --git a/Sources/PackLib/Planner.swift b/Sources/PackLib/Planner.swift deleted file mode 100644 index df2f0125..00000000 --- a/Sources/PackLib/Planner.swift +++ /dev/null @@ -1,453 +0,0 @@ -import Foundation -import XUtils -import Subprocess - -public struct Planner: Sendable { - public var buildSettings: BuildSettings - public var schema: PackSchema - - public init( - buildSettings: BuildSettings, - schema: PackSchema - ) { - self.buildSettings = buildSettings - self.schema = schema - } - - private static let decoder: JSONDecoder = { - let decoder = JSONDecoder() - decoder.keyDecodingStrategy = .convertFromSnakeCase - return decoder - }() - - // anything older than this requires bundling the stdlib which - // is doable but probably not worth the effort - private static let minSupportedIOSVersion = "13.0" - - private func buildGraph() async throws -> PackageGraph { - let dependencyRoot = try await dumpDependencies() - - let packages = try await withThrowingTaskGroup( - of: (PackageDependency, PackageDump).self, - returning: [String: PackageDump].self - ) { group in - var visited: Set = [] - var dependencies: [PackageDependency] = [dependencyRoot] - while let dependencyNode = dependencies.popLast() { - guard visited.insert(dependencyNode.identity).inserted else { continue } - dependencies.append(contentsOf: dependencyNode.dependencies) - group.addTask { (dependencyNode, try await dumpPackage(at: dependencyNode.path)) } - } - - var packages: [String: PackageDump] = [:] - while let result = await group.nextResult() { - switch result { - case .success((let dependency, let dump)): - packages[dependency.identity] = dump - case .failure(_ as CancellationError): - // continue loop - break - case .failure(let error): - group.cancelAll() - throw error - } - } - - return packages - } - - var packagesByProductName: [String: String] = [:] - for (packageID, package) in packages { - for product in package.products ?? [] { - packagesByProductName[product.name] = packageID - } - } - - let rootPackage = packages[dependencyRoot.identity]! - - return PackageGraph( - root: rootPackage, - packages: packages, - packagesByProductName: packagesByProductName - ) - } - - public func createPlan() async throws -> Plan { - // TODO: cache plan using (Package.swift+Package.resolved) as the key? - - let graph = try await buildGraph() - - let app = try await product( - from: graph, - matching: schema.product, - type: .application, - plist: schema.infoPath, - idSpecifier: schema.idSpecifier, - iconPath: schema.iconPath, - rootResources: schema.resources, - entitlementsPath: schema.entitlementsPath - ) - - let extensionProducts: [Plan.Product] - if let extensions = schema.extensions, !extensions.isEmpty { - extensionProducts = try await withThrowingTaskGroup(of: Plan.Product.self) { group in - for ext in extensions { - group.addTask { - try await product( - from: graph, - matching: ext.product, - type: .appExtension, - plist: ext.infoPath, - idSpecifier: ext.bundleID.flatMap(PackSchema.IDSpecifier.bundleID) ?? .orgID(app.bundleID), - iconPath: nil, - rootResources: ext.resources, - entitlementsPath: ext.entitlementsPath - ) - } - } - return try await group.reduce(into: []) { $0.append($1) } - } - } else { - extensionProducts = [] - } - - return Plan(app: app, extensions: extensionProducts) - } - - // swiftlint:disable cyclomatic_complexity function_parameter_count - private func product( - from graph: PackageGraph, - matching name: String?, - type: Plan.ProductType, - plist: String?, - idSpecifier: PackSchema.IDSpecifier, - iconPath: String?, - rootResources: [String]?, - entitlementsPath: String? - ) async throws -> Plan.Product { - let library = try selectLibrary( - from: graph.root.products?.filter { $0.type == .autoLibrary } ?? [], - matching: name - ) - var resources: [Plan.Resource] = [] - var visited: Set = [] - var targets = library.targets.map { (graph.root, $0) } - while let (targetPackage, targetName) = targets.popLast() { - guard let target = targetPackage.targets?.first(where: { $0.name == targetName }) else { - throw StringError("Could not find target '\(targetName)' in package '\(targetPackage.name)'") - } - guard visited.insert(targetName).inserted else { continue } - // Macro targets are compiler plugins that run on the host, not code - // linked into the iOS app. Skip them to avoid resolving their - // host-only dependencies (e.g. SwiftSyntaxMacros). - if target.type == "macro" { continue } - if target.moduleType == "BinaryTarget" { - resources.append(.binaryTarget(name: targetName)) - } - if target.resources?.isEmpty == false { - resources.append(.bundle(package: targetPackage.name, target: targetName)) - } - for targetName in (target.targetDependencies ?? []) { - targets.append((targetPackage, targetName)) - } - for productName in (target.productDependencies ?? []) { - let (package, product) = try graph.product(name: productName) - if product.type == .dynamicLibrary { - resources.append(.library(name: productName)) - } - targets.append(contentsOf: product.targets.map { (package, $0) }) - } - } - - if let rootResources { - resources += rootResources.map { .root(source: $0) } - } - - let bundleID = idSpecifier.formBundleID(product: library.name) - let deploymentTarget = graph.root.platforms?.first { $0.name == "ios" }?.version - ?? Self.minSupportedIOSVersion - - var infoPlist: [String: Sendable] = [ - "CFBundleInfoDictionaryVersion": "6.0", - "CFBundleDevelopmentRegion": "en", - "CFBundleVersion": "1", - "CFBundleShortVersionString": "1.0.0", - "MinimumOSVersion": deploymentTarget, - "CFBundleIdentifier": bundleID, - "CFBundleName": library.name, - "CFBundleExecutable": library.name, - "CFBundleDisplayName": library.name, - "CFBundlePackageType": type.fourCharCode, - ] - - switch type { - case .application: - infoPlist["UIDeviceFamily"] = [1, 2] - infoPlist["UISupportedInterfaceOrientations"] = ["UIInterfaceOrientationPortrait"] - infoPlist["UISupportedInterfaceOrientations~ipad"] = [ - "UIInterfaceOrientationPortrait", - "UIInterfaceOrientationPortraitUpsideDown", - "UIInterfaceOrientationLandscapeLeft", - "UIInterfaceOrientationLandscapeRight", - ] - infoPlist["UILaunchScreen"] = [:] as [String: Sendable] - case .appExtension: - // Should set default parameters? - infoPlist["NSExtension"] = [:] as [String: Sendable] - } - - if let plist { - let data = try await Data(reading: URL(fileURLWithPath: plist)) - let info = try PropertyListSerialization.propertyList(from: data, format: nil) - if let info = info as? [String: Sendable] { - infoPlist.merge(info, uniquingKeysWith: { $1 }) - } else { - throw StringError("Info.plist has invalid format: expected a dictionary.") - } - } - - return Plan.Product( - type: type, - product: library.name, - deploymentTarget: deploymentTarget, - bundleID: bundleID, - infoPlist: infoPlist, - resources: resources, - iconPath: iconPath, - entitlementsPath: entitlementsPath - ) - } - - private func dumpDependencies() async throws -> PackageDependency { - let tempDir = try TemporaryDirectory(name: "xtool-dump") - let tempFileURL = tempDir.url.appendingPathComponent("dump.json") - - // SwiftPM sometimes prints extraneous data to stdout, so ask - // it to write the JSON to a temp file instead. See: - // https://github.com/xtool-org/xtool/pull/97#discussion_r2203618825 - _ = try await _dumpAction( - arguments: ["-q", "show-dependencies", "--format", "json", "-o", tempFileURL.path], - path: buildSettings.packagePath - ) - - return try Self.decoder.decode( - PackageDependency.self, - from: Data(contentsOf: tempFileURL) - ) - } - - private func dumpPackage(at path: String) async throws -> PackageDump { - let data = try await _dumpAction(arguments: ["-q", "describe", "--type", "json"], path: path) - try Task.checkCancellation() - - // As in dumpDependencies, we may end up with extraneous data, but `describe` - // doesn't have a `-o` flag for a clean workaround. Resort to a heuristic, - // looking for the opening brace. - let fromBrace = data.drop(while: { $0 != Character("{").asciiValue }) - return try Self.decoder.decode(PackageDump.self, from: fromBrace) - } - - private func _dumpAction(arguments: [String], path: String) async throws -> Data { - let dumpConfig = try await buildSettings.withPackagePath(path).swiftPMInvocation( - forTool: "package", - arguments: arguments, - ) - return try await Subprocess.run( - dumpConfig, - output: .data(limit: .max), - error: .currentStandardError, - ) - .checkSuccess() - .standardOutput - } - - private func selectLibrary( - from products: [PackageDump.Product], - matching name: String? - ) throws -> PackageDump.Product { - switch products.count { - case 0: - throw StringError("No library products were found in the package") - case 1: - let product = products[0] - if let name, product.name != name { - throw StringError(""" - Product name ('\(product.name)') does not match the 'product' value in the schema ('\(name)') - """) - } - return product - default: - guard let name else { - throw StringError(""" - Multiple library products were found (\(products.map(\.name))). Please either: - 1) Expose exactly one library product, or - 2) Specify the product you want via the 'product' key in xtool.yml. - """) - } - guard let product = products.first(where: { $0.name == name }) else { - throw StringError(""" - Schema declares a 'product' name of '\(name)' but no matching product was found. - Found: \(products.map(\.name)). - """) - } - return product - } - } -} - -public struct Plan: Sendable { - public var app: Product - public var extensions: [Product] - - public var allProducts: [Product] { - [app] + extensions - } - - public enum Resource: Codable, Sendable, Hashable { - case bundle(package: String, target: String) - case binaryTarget(name: String) - case library(name: String) - case root(source: String) - } - - public struct Product: Sendable { - public var type: ProductType - public var product: String - public var deploymentTarget: String - public var bundleID: String - public var infoPlist: [String: any Sendable] - public var resources: [Resource] - public var iconPath: String? - public var entitlementsPath: String? - - public var targetName: String { - "\(self.product)-\(self.type.targetSuffix)" - } - - public func directory(inApp baseDir: URL) -> URL { - switch type { - case .application: - baseDir - .appendingPathComponent(".", isDirectory: true) - case .appExtension: - baseDir - .appendingPathComponent("PlugIns", isDirectory: true) - .appendingPathComponent(product, isDirectory: true) - .appendingPathExtension("appex") - } - } - } - - public enum ProductType: Sendable { - case application - case appExtension - - fileprivate var targetSuffix: String { - switch self { - case .application: "App" - case .appExtension: "Extension" - } - } - - fileprivate var fourCharCode: String { - switch self { - case .application: "APPL" - case .appExtension: "XPC!" - } - } - } -} - -private struct PackageDependency: Decodable { - let identity: String - let name: String - let path: String // on disk - let dependencies: [PackageDependency] -} - -private struct PackageDump: Decodable { - enum ProductType: Decodable { - case executable - case dynamicLibrary - case staticLibrary - case autoLibrary - case other - - private enum CodingKeys: String, CodingKey { - case executable - case library - } - - init(from decoder: Decoder) throws { - let container = try decoder.container(keyedBy: CodingKeys.self) - if container.contains(.executable) { - self = .executable - } else if let library = try container.decodeIfPresent([String].self, forKey: .library) { - if library.count == 1 { - switch library[0] { - case "dynamic": - self = .dynamicLibrary - case "static": - self = .staticLibrary - case "automatic": - self = .autoLibrary - default: - self = .other - } - } else { - self = .other - } - } else { - self = .other - } - } - } - - struct Product: Decodable { - let name: String - let targets: [String] - let type: ProductType - } - - struct Target: Decodable { - let name: String - let moduleType: String - let type: String? - let productDependencies: [String]? - let targetDependencies: [String]? - let resources: [Resource]? - } - - struct Resource: Decodable { - let path: String - } - - struct Platform: Decodable { - let name: String - let version: String - } - - let name: String - let products: [Product]? - let targets: [Target]? - let platforms: [Platform]? -} - -private struct PackageGraph { - let root: PackageDump - let packages: [String: PackageDump] - let packagesByProductName: [String: String] - - func product(name productName: String) throws -> (PackageDump, PackageDump.Product) { - guard let packageID = packagesByProductName[productName] else { - throw StringError("Could not find package containing product '\(productName)'") - } - guard let package = packages[packageID] else { - throw StringError("Could not find package by id '\(packageID)'") - } - guard let product = package.products?.first(where: { $0.name == productName }) else { - throw StringError("Could not find product '\(productName)' in package '\(packageID)'") - } - return (package, product) - } -} diff --git a/Sources/XToolSupport/BuildCoordinator.swift b/Sources/XToolSupport/BuildCoordinator.swift new file mode 100644 index 00000000..6f336085 --- /dev/null +++ b/Sources/XToolSupport/BuildCoordinator.swift @@ -0,0 +1,193 @@ +import XCLIUtils +import XPIF +import Foundation +import PackLib +import XUtils +import Subprocess +import SwiftBuild + +public actor BuildCoordinator { + let sessionManager: BuildSessionManager + let buildRequest: SWBBuildRequest + let pifContainer: XPIF.Container + + init(description: BuildDescription) async throws { + self.pifContainer = description.pif + + Signposter.track("Creating BuildSessionManager") + + let sessionManager = try await BuildSessionManager() + self.sessionManager = sessionManager + + do { + Signposter.track("Sending PIF to build service") + + try await sessionManager.sendPIF(pifContainer) + + let targetInfo = try await sessionManager.session.buildTargetInfo(triple: description.triple) + // buildTargetInfo validates the triple's structure. Like SwiftPM's archName, + // preserve the architecture spelling from its first component. + let arch = String(description.triple.prefix { $0 != "-" }) + var parameters = SWBBuildParameters() + parameters.activeRunDestination = SWBRunDestinationInfo( + buildTarget: .toolchainSDK( + platform: targetInfo.platformName, + sdk: targetInfo.sdkName, + sdkVariant: targetInfo.sdkVariant, + ), + targetArchitecture: arch, + supportedArchitectures: [arch], + disableOnlyActiveArch: false, + ) + parameters.activeArchitecture = arch + + var overrides: [String: String] = [:] + + let xtoolDir = description.directory.appending(path: "xtool") + // if you have a macOS Xcode project with a package dependency, and build in debug + // mode, the Xcode targets will use [arm64, x86_64] but the package targets will + // only declare [arm64], causing the build to fail + overrides["ONLY_ACTIVE_ARCH"] = "YES" + // Only override deployment targets when the triple explicitly includes a version. + // Otherwise each package retains the deployment targets from its manifest. + if let setting = targetInfo.deploymentTargetSettingName, let version = targetInfo.deploymentTarget { + overrides[setting] = version + } + + switch description.configuration { + case .debug: + parameters.configurationName = "Debug" + parameters.action = "build" + case .release: + parameters.configurationName = "Release" + parameters.action = "install" + overrides["DSTROOT"] = xtoolDir.appending(path: ".xtool-tmp/Install/\(description.triple)").path + overrides["INSTALL_PATH"] = "/" + } + + #if !os(macOS) + let bundle = sessionManager.darwinSDK.bundle.path + // we don't have appintentsmetadataprocessor yet + overrides["LM_SKIP_METADATA_EXTRACTION"] = "1" + // Our toolset doesn't yet include the tapi tool + overrides["EAGER_LINKING"] = "NO" + overrides["GENERATE_TEXT_BASED_STUBS"] = "NO" + overrides["GENERATE_INTERMEDIATE_TEXT_BASED_STUBS"] = "NO" + overrides["SUPPORTS_TEXT_BASED_API"] = "NO" + // equivalent of our SwiftPM-era `toolset.json` config + let bin = "\(bundle)/toolset/bin" + overrides["PBXCP_BITCODE_STRIP_TOOL[__destination_platform=YES]"] = "\(bin)/bitcode_strip" + overrides["ALTERNATE_LINKER[__destination_platform=YES]"] = "lld" + overrides["ALTERNATE_LINKER_PATH[__destination_platform=YES]"] = "\(bin)/ld64.lld" + overrides["AR[__destination_platform=YES]"] = "\(bin)/llvm-lib" + overrides["LIBTOOL[__destination_platform=YES]"] = "\(bin)/llvm-lib" + let developerDir = sessionManager.darwinSDK.developerDirectory.path + let swiftResourceDir = "\(developerDir)/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift" + overrides["SWIFT_RESOURCE_DIR[__destination_platform=YES]"] = swiftResourceDir + let runtimeResourcePath = try await BuildSettings.runtimeResourcePath() + let hostClangResources = URL(filePath: runtimeResourcePath).appending(path: "clang") + // Compile with the host's intrinsic headers... + overrides["CLANG_RESOURCE_DIR[__destination_platform=YES]"] = hostClangResources.path + // ...but at link time, point to Apple's Darwin compiler runtimes (incl OS availability helpers) + // from the Xcode toolchain. + overrides["CLANG_RESOURCE_DIR_CLANG[__destination_platform=YES]"] = "\(swiftResourceDir)/clang" + overrides["CLANG_RESOURCE_DIR_SWIFTC[__destination_platform=YES]"] = "\(swiftResourceDir)/clang" + // Use OpenAppleMacrosServer, which reimplements Apple's proprietary macros. + // Also explicitly enable cross-import-overlays: they're on by default in Apple Swift + // but off by default in OSS Swift. + overrides["OTHER_SWIFT_FLAGS[__destination_platform=YES]"] = """ + $(inherited) \ + -external-plugin-path "\(bundle)/toolset/plugins#\(bundle)/toolset/bin/OpenAppleMacrosServer" \ + -Xfrontend -enable-cross-import-overlays + """ + #endif + + var overridesTable = SWBSettingsTable() + for (key, value) in overrides { + overridesTable.set(value: value, for: key) + } + parameters.overrides.synthesized = overridesTable + var request = SWBBuildRequest() + request.parameters = parameters + request.useImplicitDependencies = true + request.setDerivedDataPath(xtoolDir.appending(path: ".xtool-tmp/DerivedData")) + self.buildRequest = request + } catch { + await sessionManager.close() + throw error + } + } + + func close() async { + await sessionManager.close() + } + + func mainProductGUID() throws -> String { + let apps = pifContainer.workspace.projects.flatMap(\.targets).compactMap { base -> XPIF.Target? in + guard case let .target(target) = base, target.productType == .application else { return nil } + return target + } + guard apps.count == 1, let app = apps.first else { + throw Console.Error("Expected one application target, found \(apps.count).") + } + return app.id.value + } + + // TODO: swb: use product info request + func mainProductDetails() async throws -> (productName: String, bundleID: String, output: URL) { + let guid = try mainProductGUID() + // PBX product references can have stale names. Resolve the actual output with Swift Build, + // including configuration, SDK conditions, xcconfigs, and build setting expressions. + let fullProductName = try await sessionManager.session.evaluateMacroAsString( + "FULL_PRODUCT_NAME", level: .target(guid), + buildParameters: buildRequest.parameters, + overrides: nil, + ) + let bundleID = try await sessionManager.session.evaluateMacroAsString( + "PRODUCT_BUNDLE_IDENTIFIER", level: .target(guid), + buildParameters: buildRequest.parameters, + overrides: nil, + ) + let targetBuildDirectory = try await sessionManager.session.evaluateMacroAsString( + "TARGET_BUILD_DIR", level: .target(guid), + buildParameters: buildRequest.parameters, + overrides: nil, + ) + return ( + productName: fullProductName.withoutSuffix(".app"), + bundleID: bundleID, + output: URL(filePath: targetBuildDirectory).appending(path: fullProductName) + ) + } + + func createBuildOperation() async throws -> SWBBuildOperation { + var request = buildRequest + if request.configuredTargets.isEmpty { + request.configuredTargets = [SWBConfiguredTarget(guid: try mainProductGUID())] + } + return try await sessionManager.createBuildOperation(request: request) + } +} + +extension SWBBuildRequest { + fileprivate mutating func setDerivedDataPath(_ derivedDataPath: URL) { + let arenaInfo = SWBArenaInfo( + derivedDataPath: derivedDataPath.path, + buildProductsPath: derivedDataPath.appendingPathComponent("Products").path, + buildIntermediatesPath: derivedDataPath.appendingPathComponent("Intermediates.noindex").path, + pchPath: derivedDataPath.path, + indexRegularBuildProductsPath: nil, + indexRegularBuildIntermediatesPath: nil, + indexPCHPath: derivedDataPath.path, + indexDataStoreFolderPath: derivedDataPath.path, + indexEnableDataStore: parameters.arenaInfo?.indexEnableDataStore ?? false + ) + + parameters.arenaInfo = arenaInfo + configuredTargets = configuredTargets.map { configuredTarget in + var configuredTarget = configuredTarget + configuredTarget.parameters?.arenaInfo = arenaInfo + return configuredTarget + } + } +} diff --git a/Sources/XToolSupport/BuildDescription.swift b/Sources/XToolSupport/BuildDescription.swift new file mode 100644 index 00000000..0ea42d14 --- /dev/null +++ b/Sources/XToolSupport/BuildDescription.swift @@ -0,0 +1,19 @@ +import XCLIUtils +import XUtils +import XPIF +import PackLib +import Foundation + +struct BuildDescription { + var directory: URL + var pif: XPIF.Container + var configuration: BuildConfiguration + var triple: String + + init(pif: XPIF.Container, buildSettings: BuildSettings) { + self.directory = URL(filePath: buildSettings.packagePath) + self.pif = pif + self.configuration = buildSettings.configuration + self.triple = buildSettings.triple + } +} diff --git a/Sources/XToolSupport/BuildSessionManager.swift b/Sources/XToolSupport/BuildSessionManager.swift new file mode 100644 index 00000000..5ed89497 --- /dev/null +++ b/Sources/XToolSupport/BuildSessionManager.swift @@ -0,0 +1,178 @@ +import XCLIUtils +import XUtils +import XPIF +import SwiftBuild +import PackLib +import Foundation +import Dependencies + +actor BuildSessionManager { + let service: SWBBuildService + let session: SWBBuildServiceSession + + #if !os(macOS) + nonisolated let darwinSDK: DarwinSDK + #endif + + private let planningDelegate = PlanningOperationDelegate() + + private var hasCalledClose = false + + init() async throws { + #if !os(macOS) + self.darwinSDK = try DarwinSDK.current().orThrow(Console.Error("Darwin SDK not installed")) + let toolsetPath = darwinSDK.bundle.appending(path: "toolset/bin").path + // swb looks here for tools first... + setenv("__XCODE_BUILT_PRODUCTS_DIR_PATHS", toolsetPath, 1) + // ...but also provide PATH as a fallback for nested tool invocations + setenv("PATH", "\(toolsetPath):\(ProcessInfo.processInfo.environment["PATH"] ?? "")", 1) + setenv("XCODE_EXTRA_PLATFORM_FOLDERS", "\(darwinSDK.developerDirectory.path)/Platforms", 1) + if let executablePath = Bundle.main.executablePath { + setenv("XTL_LOOPBACK_PATH", executablePath, 1) + } + #endif + + self.service = try await SWBBuildService( + connectionMode: .xtool, + ) + + Signposter.track("Creating build session") + + #if os(macOS) + let (result, diagnostics) = await service.createSession( + name: "xtool", + developerPath: nil, + resourceSearchPaths: [], + cachePath: nil, + inferiorProductsPath: nil, + environment: nil, + ) + #else + let (result, diagnostics) = await service.createSession( + name: "xtool", + swiftToolchainPath: try await BuildSettings.toolchainPath(), + resourceSearchPaths: [], + cachePath: nil, + inferiorProductsPath: nil, + environment: nil, + ) + #endif + + if !diagnostics.isEmpty { + d.out.print("Diagnostics:") + for diagnostic in diagnostics { + d.out.print(diagnostic) + } + } + self.session = try result.get() + } + + /// Must call this before deinit. Idempotent. + func close() async { + guard !hasCalledClose else { return } + hasCalledClose = true + + do { + try await session.close() + } catch { + d.out.print("Error closing build session: \(error)") + } + + await service.close() + } + + func sendPIF(_ container: XPIF.Container) async throws { + let serialized = try container.data() + let propertyList = try JSONSerialization.jsonObject(with: serialized, options: []) + + try await session.sendPIF(SWBPropertyListItem(unsafePropertyList: propertyList)) + + try await session.setSystemInfo(.default()) + try await session.setUserInfo(.default) + try await session.setUserPreferences( + enableDebugActivityLogs: false, + enableBuildDebugging: false, + enableBuildSystemCaching: true, + activityTextShorteningLevel: 3, + usePerConfigurationBuildLocations: nil, + // changed from default, allows delegation to our executeExternalTool() + allowsExternalToolExecution: true, + ) + } + + func createBuildOperation(request: SWBBuildRequest) async throws -> SWBBuildOperation { + try await session.createBuildOperation(request: request, delegate: planningDelegate) + } +} + +private final class PlanningOperationDelegate: SWBPlanningOperationDelegate, Sendable { + let dependencies = withEscapedDependencies(\.self) + + func provisioningTaskInputs( + targetGUID: String, + provisioningSourceData: SWBProvisioningTaskInputsSourceData + ) async -> SWBProvisioningTaskInputs { + // called on a GCD queue without task locals, so we need to explicitly propagate deps + await dependencies.yield { + await _provisioningTaskInputs( + targetGUID: targetGUID, + provisioningSourceData: provisioningSourceData + ) + } + } + + func _provisioningTaskInputs( + targetGUID: String, + provisioningSourceData: SWBProvisioningTaskInputsSourceData + ) async -> SWBProvisioningTaskInputs { + SWBProvisioningTaskInputs() + } + + func executeExternalTool( + commandLine: [String], + workingDirectory: String?, + environment: [String: String] + ) async throws -> SWBExternalToolResult { + try await dependencies.yield { + try await self._executeExternalTool( + commandLine: commandLine, + workingDirectory: workingDirectory, + environment: environment + ) + } + } + + private func _executeExternalTool( + commandLine: [String], + workingDirectory: String?, + environment: [String: String] + ) async throws -> SWBExternalToolResult { + return try await d.emulatedTools.execute( + commandLine: commandLine, + workingDirectory: workingDirectory, + environment: environment, + ).swb + } +} + +extension EmulatedToolResult { + fileprivate var swb: SWBExternalToolResult { + switch self { + case .deferred: + .deferred + case .result(let status, let stdout, let stderr): + .result(status: status.swb, stdout: stdout, stderr: stderr) + } + } +} + +extension EmulatedToolExitStatus { + fileprivate var swb: SWBProcessExitStatus { + switch self { + case .exit(let code): + .exit(code) + case .uncaughtSignal(let signal): + .uncaughtSignal(signal) + } + } +} diff --git a/Sources/XToolSupport/DevBSPCommand.swift b/Sources/XToolSupport/DevBSPCommand.swift index 0bf50a20..afcf9eb6 100644 --- a/Sources/XToolSupport/DevBSPCommand.swift +++ b/Sources/XToolSupport/DevBSPCommand.swift @@ -1,10 +1,5 @@ import XCLIUtils -import XUtils import ArgumentParser -import Foundation -import XKit -import PackLib -import Subprocess struct DevBSPCommand: AsyncParsableCommand { static let configuration = CommandConfiguration( @@ -16,19 +11,6 @@ struct DevBSPCommand: AsyncParsableCommand { var triple: String? func run() async throws { - guard try await SwiftVersion.current.supportsSwiftBuild else { - throw Console.Error("`xtool dev build-server` requires Swift 6.4 or later") - } - let settings = try await BuildSettings( - configuration: .debug, - triple: triple ?? PackOperation.defaultTriple - ) - try await Subprocess.run( - settings.buildServerInvocation(), - input: .currentStandardInput, - output: .currentStandardOutput, - error: .currentStandardError, - ) - .checkSuccess() + throw Console.Error("Stub") } } diff --git a/Sources/XToolSupport/DevCommand.swift b/Sources/XToolSupport/DevCommand.swift index 12852b97..a371d037 100644 --- a/Sources/XToolSupport/DevCommand.swift +++ b/Sources/XToolSupport/DevCommand.swift @@ -5,6 +5,7 @@ import PackLib import XKit import Dependencies import XUtils +import Subprocess struct PackOperation { struct BuildOptions: ParsableArguments { @@ -13,10 +14,20 @@ struct PackOperation { help: "Build with configuration" ) var configuration: BuildConfiguration = .debug + @Flag( + name: .shortAndLong, + help: "Verbose output" + ) var verbose = false + init() {} - init(configuration: BuildConfiguration) { + // this is load bearing: synthesized init doesn't work with ArgumentParser + init( + configuration: BuildConfiguration, + verbose: Bool = false, + ) { self.configuration = configuration + self.verbose = verbose } } @@ -27,7 +38,7 @@ struct PackOperation { var xcode = false @discardableResult - func run() async throws -> URL { + func run() async throws -> ProjectBuildResult { // TODO: support running inside a child directory let contents = try FileManager.default.contentsOfDirectory(atPath: ".") @@ -39,7 +50,7 @@ struct PackOperation { try await EnsureSDKOperation(quiet: true).run() - d.out.print("Planning...") + Signposter.track("Planning") let schema: PackSchema let configPath = URL(fileURLWithPath: "xtool.yml") @@ -61,46 +72,17 @@ struct PackOperation { options: [] ) - let planner = Planner( - buildSettings: buildSettings, - schema: schema - ) - let plan = try await planner.createPlan() - + let describer = try SwiftPMBuildDescriber(buildSettings: buildSettings, schema: schema) if xcode { - return try await XcodePacker(plan: plan).createProject() - } - - let packer = Packer( - buildSettings: buildSettings, - plan: plan - ) - let bundle = try await packer.pack() - - let productsWithEntitlements = plan - .allProducts - .compactMap { p in p.entitlementsPath.map { (p, $0) } } - if !productsWithEntitlements.isEmpty { - let mapping = try await withThrowingTaskGroup(of: (URL, Data).self) { group in - for (product, path) in productsWithEntitlements { - group.addTask { - let entitlements = try await Data(reading: URL(fileURLWithPath: path)) - return (product.directory(inApp: bundle), entitlements) - } - } - return try await group.reduce(into: [:]) { $0[$1.0] = $1.1 } - } - d.out.print("Applying entitlements...") - try await d.signer.sign( - app: bundle, - identity: .adhoc, - entitlementMapping: mapping, - deep: true, - progress: { _ in } - ) + return try await describer.writeXcodeProject() } - return bundle + let coordinator = try await BuildCoordinator(description: describer.describe()) + defer { await coordinator.close() } + return try await ProjectBuildOperation( + buildCoordinator: coordinator, + verbose: buildOptions.verbose, + ).run() } } @@ -112,16 +94,16 @@ struct DevXcodeCommand: AsyncParsableCommand { func run() async throws { let result = try await PackOperation(xcode: true).run() - d.out.print("Wrote to \(result.path)") + d.out.print("Wrote to \(result.url.path)") } } struct DevBuildCommand: AsyncParsableCommand { static let configuration = CommandConfiguration( commandName: "build", - abstract: "Build app with SwiftPM", + abstract: "Build iOS app", discussion: """ - This command builds the SwiftPM-based iOS app in the current directory + This command builds the Swift package iOS app in the current directory """ ) @@ -158,7 +140,7 @@ struct DevBuildCommand: AsyncParsableCommand { let url = try await PackOperation( triple: triple, buildOptions: packOptions - ).run() + ).run().url if let signingAuthToken { let installDelegate = XToolInstallerDelegate() @@ -167,7 +149,10 @@ struct DevBuildCommand: AsyncParsableCommand { delegate: installDelegate ) do { - defer { d.out.print() } + defer { + await installDelegate.finish() + d.out.print() + } try await installer.signInPlace(app: url) } catch let error as CancellationError { throw error @@ -206,20 +191,25 @@ struct DevBuildCommand: AsyncParsableCommand { struct DevRunCommand: AsyncParsableCommand { static let configuration = CommandConfiguration( commandName: "run", - abstract: "Build and run app with SwiftPM", + abstract: "Build and run iOS app", discussion: """ - This command deploys the SwiftPM-based iOS app in the current directory + This command deploys the Swift package iOS app in the current directory """ ) @OptionGroup var packOptions: PackOperation.BuildOptions - #if os(macOS) @Flag( name: .shortAndLong, - help: "Target the iOS Simulator" + help: "Target the iOS Simulator (macOS only)", ) var simulator = false + @Flag( + inversion: .prefixedEnableDisable, + help: "Attach console when running on the simulator (default: true)", + ) var console: Bool? + + #if os(macOS) var triple: String? { if simulator { #if arch(arm64) @@ -243,7 +233,11 @@ struct DevRunCommand: AsyncParsableCommand { #if os(macOS) if simulator { - try await SimInstallOperation(path: output).run() + try await SimInstallOperation( + path: output.url, + bundleID: output.bundleID, + console: console != false, + ).run() return } #endif @@ -259,11 +253,14 @@ struct DevRunCommand: AsyncParsableCommand { delegate: installDelegate ) - defer { d.out.print() } + defer { + await installDelegate.finish() + d.out.print() + } do { try await installer.install( - app: output, + app: output.url, udid: client.udid, lookupMode: .only(client.connectionType), configureDevice: false, @@ -275,12 +272,24 @@ struct DevRunCommand: AsyncParsableCommand { throw ExitCode.failure } } + + func validate() throws { + #if !os(macOS) + guard !simulator else { + throw ValidationError("`--simulator` is only supported on macOS") + } + #endif + + if console == true && !simulator { + throw ValidationError("`--enable-console` is only supported when targeting the simulator") + } + } } struct DevCommand: AsyncParsableCommand { static let configuration = CommandConfiguration( commandName: "dev", - abstract: "Build and run an xtool SwiftPM project", + abstract: "Build and run a Swift package", subcommands: [ DevXcodeCommand.self, DevBuildCommand.self, @@ -298,18 +307,44 @@ import Subprocess struct SimInstallOperation { var path: URL + var bundleID: String + var console: Bool // TODO: allow customizing this var simulator = "booted" func run() async throws { - try await Subprocess.run( + let simctlRaw = try await Subprocess.run( .path("/usr/bin/xcrun"), - arguments: ["simctl", "install", simulator, path.path], + arguments: ["-f", "simctl"], + output: .string(limit: .max) + ).checkSuccess().standardOutput.trimmingCharacters(in: .whitespacesAndNewlines) + let simctl = FilePath(simctlRaw) + + try await Subprocess.run( + .path(simctl), + arguments: ["install", simulator, path.path], output: .discarded ) .checkSuccess() + d.out.print("Installed to simulator") + + var launchFlags: [String] = [ + "--terminate-running-process", + ] + if console { + launchFlags.append("--console-pty") + } + + try await Subprocess.run( + .path(simctl), + arguments: Arguments(["launch"] + launchFlags + [simulator, bundleID]), + platformOptions: .withGracefulShutDown, + output: .xtoolOutput(), + error: .xtoolError(), + ) + .checkSuccess() } } #endif diff --git a/Sources/XToolSupport/EmulateToolCommand.swift b/Sources/XToolSupport/EmulateToolCommand.swift new file mode 100644 index 00000000..072a198a --- /dev/null +++ b/Sources/XToolSupport/EmulateToolCommand.swift @@ -0,0 +1,46 @@ +import ArgumentParser +import Foundation +import XCLIUtils +import XUtils + +struct EmulateToolCommand: AsyncParsableCommand { + static let configuration = CommandConfiguration( + commandName: "_emulate", + abstract: "Emulate a build tool", + shouldDisplay: false, + ) + + @Argument(help: "The name of the tool to emulate") + var toolName: String + + @Argument( + parsing: .captureForPassthrough, + help: "The arguments to pass to the emulated tool", + ) + var arguments: [String] = [] + + func run() async throws { + let result = try await d.emulatedTools.execute( + commandLine: [toolName] + arguments, + workingDirectory: nil, + environment: ProcessInfo.processInfo.environment, + ) + let status: EmulatedToolExitStatus + switch result { + case .deferred: + throw Console.Error("Unable to emulate tool \(toolName)") + case .result(let exitStatus, let stdout, let stderr): + d.out.write(stdout, .stdout, true) + d.out.write(stderr, .stderr, true) + status = exitStatus + } + switch status { + case .exit(0): + break + case .exit(let code): + throw ExitCode(code) + case .uncaughtSignal(let signal): + throw Console.Error("Tool \(toolName) terminated with signal \(signal)") + } + } +} diff --git a/Sources/XToolSupport/EmulatedTools+Live.swift b/Sources/XToolSupport/EmulatedTools+Live.swift new file mode 100644 index 00000000..b0f1f1de --- /dev/null +++ b/Sources/XToolSupport/EmulatedTools+Live.swift @@ -0,0 +1,152 @@ +import Dependencies +import Foundation +import Subprocess +import XUtils + +// MARK: - Tools + +extension EmulatedTools: DependencyKey { + static let liveValue = EmulatedTools({ + var tools: [any EmulatedTool] = [] + #if !os(macOS) + tools += [ + LSRegisterTool(), + EmbeddedBinaryValidationTool(), + ObjcopyTool(mode: .strip), + ObjcopyTool(mode: .bitcodeStrip), + ChmodTool(), + CodesignTool(), + ] + #endif + return tools + }()) +} + +private struct LSRegisterTool: EmulatedTool { + var name: String { "lsregister" } + + func execute( + commandLine: [String], + workingDirectory: String?, + environment: [String: String] + ) async throws -> EmulatedToolResult { + .result(status: .exit(0), stdout: Data(), stderr: Data()) + } +} + +private struct EmbeddedBinaryValidationTool: EmulatedTool { + var name: String { "embeddedBinaryValidationUtility" } + + func execute( + commandLine: [String], + workingDirectory: String?, + environment: [String: String] + ) async throws -> EmulatedToolResult { + .result(status: .exit(0), stdout: Data(), stderr: Data()) + } +} + +private struct ObjcopyTool: EmulatedTool { + enum Mode { + case strip + case bitcodeStrip + + var name: String { + switch self { + case .strip: "strip" + case .bitcodeStrip: "bitcode_strip" + } + } + } + + let mode: Mode + var name: String { mode.name } + + func execute( + commandLine: [String], + workingDirectory: String?, + environment: [String: String] + ) async throws -> EmulatedToolResult { + let remaining = commandLine.dropFirst().filter { $0 != "-no_atom_info" } + return try await Subprocess.run( + .name("llvm-objcopy"), + arguments: Arguments( + executablePathOverride: name, + remainingValues: remaining, + ), + environment: Environment(environment), + workingDirectory: workingDirectory.map { FilePath($0) }, + output: .data(limit: .max), + error: .data(limit: .max), + ) + .emulatedToolResult + } +} + +private struct ChmodTool: EmulatedTool { + var name: String { "chmod" } + + func execute( + commandLine: [String], + workingDirectory: String?, + environment: [String: String] + ) async throws -> EmulatedToolResult { + var remaining = Array(commandLine.dropFirst()) + // SetAttributesSpec conditions the -H flag on whether the target is Darwin, rather + // than whether the host is Darwin + if let index = remaining.firstIndex(of: "-RH") { + remaining[index] = "-R" + } + return try await Subprocess.run( + .name("chmod"), + arguments: Arguments(remaining), + environment: Environment(environment), + workingDirectory: workingDirectory.map { FilePath($0) }, + output: .data(limit: .max), + error: .data(limit: .max), + ) + .emulatedToolResult + } +} + +private struct CodesignTool: EmulatedTool { + var name: String { "codesign" } + + func execute( + commandLine: [String], + workingDirectory: String?, + environment: [String: String] + ) async throws -> EmulatedToolResult { + // TODO: implement + .result(status: .exit(0), stdout: Data(), stderr: Data()) + } +} + +extension Environment { + init(_ dictionary: [String: String]) { + self = .custom([]).updating( + Dictionary(uniqueKeysWithValues: dictionary.map { + (Environment.Key(stringLiteral: $0.key), $0.value) + }) + ) + } +} + +extension ExecutionResult where Output == DataOutput, Error == DataOutput { + var emulatedToolResult: EmulatedToolResult { + .result( + status: terminationStatus.emulatedToolExitStatus, + stdout: standardOutput, + stderr: standardError + ) + } +} + +extension TerminationStatus { + var emulatedToolExitStatus: EmulatedToolExitStatus { + switch self { + case .exited(let code): .exit(numericCast(code)) + case .signaled(let signal): .uncaughtSignal(numericCast(signal)) + } + } +} diff --git a/Sources/XToolSupport/EmulatedTools.swift b/Sources/XToolSupport/EmulatedTools.swift new file mode 100644 index 00000000..e5107877 --- /dev/null +++ b/Sources/XToolSupport/EmulatedTools.swift @@ -0,0 +1,58 @@ +import Foundation +import Dependencies + +struct EmulatedTools: Sendable, TestDependencyKey { + var tools: [String: any EmulatedTool] + + init(_ tools: [any EmulatedTool]) { + self.tools = Dictionary( + tools.map { ($0.name, $0) }, + uniquingKeysWith: { $1 }, + ) + } + + func execute( + commandLine: [String], + workingDirectory: String?, + environment: [String: String] + ) async throws -> EmulatedToolResult { + guard let toolPath = commandLine.first else { return .deferred } + let toolURL = URL(fileURLWithPath: toolPath) + let toolName = toolURL.lastPathComponent + guard let tool = tools[toolName] else { return .deferred } + return try await tool.execute( + commandLine: commandLine, + workingDirectory: workingDirectory, + environment: environment, + ) + } + + static let testValue = Self([]) +} + +extension DependencyValues { + var emulatedTools: EmulatedTools { + get { self[EmulatedTools.self] } + set { self[EmulatedTools.self] = newValue } + } +} + +protocol EmulatedTool: Sendable { + var name: String { get } + + func execute( + commandLine: [String], + workingDirectory: String?, + environment: [String: String] + ) async throws -> EmulatedToolResult +} + +enum EmulatedToolResult: Sendable { + case deferred + case result(status: EmulatedToolExitStatus, stdout: Data, stderr: Data) +} + +enum EmulatedToolExitStatus: Sendable { + case exit(_ code: Int32) + case uncaughtSignal(_ signal: Int32) +} diff --git a/Sources/XToolSupport/ProjectBuildOperation.swift b/Sources/XToolSupport/ProjectBuildOperation.swift new file mode 100644 index 00000000..3ce2ce6d --- /dev/null +++ b/Sources/XToolSupport/ProjectBuildOperation.swift @@ -0,0 +1,116 @@ +import XCLIUtils +import Foundation +import SwiftBuild +import XUtils + +struct ProjectBuildResult: Sendable { + var url: URL + var bundleID: String +} + +struct ProjectBuildOperation { + let buildCoordinator: BuildCoordinator + let verbose: Bool + + func run() async throws -> ProjectBuildResult { + let (_, bundleID, installedProduct) = try await buildCoordinator.mainProductDetails() + + Signposter.track("Starting build") + + let operation = try await buildCoordinator.createBuildOperation() + try await withTaskCancellationHandler { + for await event in try await operation.start() { + printEvent(event) + } + await operation.waitForCompletion() + } onCancel: { + operation.cancel() + } + try Task.checkCancellation() + switch operation.state { + case .succeeded: + break + case .requested, .running: + throw Console.Error("Internal error: build operation did not complete") + case .aborted, .cancelled: + throw CancellationError() + case .failed: + d.out.print() + throw Console.Error("Build failed") + } + + Signposter.track("Build completed") + + let xtoolDir = URL(filePath: "xtool").absoluteURL + let output = xtoolDir.appending(path: installedProduct.lastPathComponent) + let staging = xtoolDir.appending(path: ".xtool-tmp/copy-stage") + try FileManager.default.atomicallyCopyItem(at: installedProduct, to: output, via: staging) + + if verbose { + d.out.print("Build completed.") + } else { + printProgress(100, targetName: nil, message: "Build completed.\n") + } + + return ProjectBuildResult(url: output, bundleID: bundleID) + } + + private func printEvent(_ event: SwiftBuildMessage) { + if verbose { + d.out.print("\(event)") + } else { + switch event { + case .diagnostic(let diagnostic): + switch diagnostic.kind { + case .note, .remark: return + case .warning, .error: break + } + d.out.print(""" + \n\(diagnostic.kind.rawValue): \ + \(diagnostic.location.userDescription.map { "\($0): " } ?? "")\ + \(diagnostic.message) + """) + case .didUpdateProgress(let progress): + printProgress( + progress.percentComplete, + targetName: progress.targetName ?? "Building", + message: progress.message, + ) + default: + break + } + } + } + + private func printProgress(_ percentComplete: Double, targetName: String?, message: String) { + let progress = percentComplete == -1 ? "0%" : "\(String(format: "%.2f", percentComplete))%" + d.out.print("\u{1B}[2K\r[\(progress)] \(targetName.map { "\($0) " } ?? "")\(message)", terminator: "") + } +} + +fileprivate extension SwiftBuild.SwiftBuildMessage.DiagnosticInfo.Location { + var userDescription: String? { + switch self { + case .path(let path, let fileLocation): + switch fileLocation { + case .textual(let line, let column): + var description = "\(path):\(line)" + if let column { description += ":\(column)" } + return description + case .object(let identifier): + return "\(path):\(identifier)" + case .none: + return path + } + + case .buildSettings(let names): + return names.joined(separator: ", ") + + case .buildFiles(let buildFiles, let targetGUID): + return "\(targetGUID): " + buildFiles.map { String(describing: $0) }.joined(separator: ", ") + + case .unknown: + return nil + } + } +} diff --git a/Sources/XToolSupport/SDKBuilder.swift b/Sources/XToolSupport/SDKBuilder.swift index f9f7b92d..f2acff54 100644 --- a/Sources/XToolSupport/SDKBuilder.swift +++ b/Sources/XToolSupport/SDKBuilder.swift @@ -6,7 +6,6 @@ import Subprocess import XKit // HTTPClient, stdoutSafe import XUtils // System.File{Path,Descriptor} -// swiftlint:disable:next type_body_length struct SDKBuilder { enum Arch: String { case x86_64 @@ -63,7 +62,7 @@ struct SDKBuilder { /// Update a normal SDK in-place. case update - var usesHardLinks: Bool { + var retainsXcode: Bool { switch self { case .buildSlim: false case .buildNormal, .update: true @@ -77,20 +76,24 @@ struct SDKBuilder { let mode: Mode // bump this when the sdk builder logic changes - static let sdkEpoch = 4 + static let sdkEpoch = 5 // tag from https://github.com/xtool-org/darwin-tools-linux-llvm static let darwinToolsVersion = "1.1.0" // tag from https://github.com/xtool-org/OpenAppleMacros static let oamVersion = "1.4.1" + // bump sdk epoch when this changes + static let toolsToEmulate = [ + "strip", + ] + static var currentSDKVersion: String { """ epoch=\(sdkEpoch),darwinTools=\(darwinToolsVersion),oam=\(oamVersion) """ } - // swiftlint:disable:next function_body_length func buildSDK() async throws { let sdkVersion = Self.currentSDKVersion @@ -100,12 +103,8 @@ struct SDKBuilder { withIntermediateDirectories: true ) - // TODO: parallelize these three steps - // we need to synchronize progress reporting though - - try await installMacros(in: output) - try await installToolset(in: output) + try await installMacros(in: output) let dev = try await installDeveloper(in: output) @@ -121,17 +120,6 @@ struct SDKBuilder { return name } - func triple(platform: String, sdk: String) -> SDKDefinition.Triple { - SDKDefinition.Triple( - sdkRootPath: "Developer/Platforms/\(platform).platform/Developer/SDKs/\(sdk)", - includeSearchPaths: ["Developer/Platforms/\(platform).platform/Developer/usr/lib"], - librarySearchPaths: ["Developer/Platforms/\(platform).platform/Developer/usr/lib"], - swiftResourcesPath: "Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift", - swiftStaticResourcesPath: "Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift_static", - toolsetPaths: ["toolset.json"] - ) - } - let iPhoneOSSDK = try sdk(platform: "iPhoneOS", prefix: "iPhoneOS") let iPhoneSimSDK = try sdk(platform: "iPhoneSimulator", prefix: "iPhoneSimulator") let macOSSDK = try sdk(platform: "MacOSX", prefix: "MacOSX") @@ -142,93 +130,6 @@ struct SDKBuilder { - \(macOSSDK) """) - d.out.print("[Writing metadata]") - try """ - { - "schemaVersion": "1.0", - "artifacts": { - "darwin": { - "type": "swiftSDK", - "version": "0.0.1", - "variants": [ - { - "path": ".", - "supportedTriples": ["aarch64-unknown-linux-gnu", "x86_64-unknown-linux-gnu"] - } - ] - } - } - } - """.write( - to: output.appendingPathComponent("info.json"), - atomically: false, - encoding: .utf8 - ) - - try """ - { - "schemaVersion": "1.0", - "rootPath": "toolset/bin", - "linker": { - "path": "ld64.lld" - }, - "librarian": { - "path": "llvm-lib" - }, - "swiftCompiler": { - "extraCLIOptions": [ - "-Xfrontend", "-enable-cross-import-overlays", - "-use-ld=lld" - ] - } - } - """.write( - to: output.appendingPathComponent("toolset.json"), - atomically: false, - encoding: .utf8 - ) - - // this toolset works with the swiftbuild system on Linux - // note that we need Swift 6.4+ because 6.3 has bugs in - // resolving the librarian and linker paths. - try """ - { - "schemaVersion": "1.0", - "rootPath": "toolset/bin", - "linker": { - "path": "ld64.lld" - }, - "librarian": { - "path": "llvm-lib" - }, - "swiftCompiler": { - "extraCLIOptions": [ - "-Xfrontend", "-enable-cross-import-overlays" - ] - } - } - """.write( - to: output.appendingPathComponent("toolset-swb.json"), - atomically: false, - encoding: .utf8 - ) - - let sdkDefinition = SDKDefinition( - schemaVersion: "4.0", - targetTriples: [ - "arm64-apple-ios": triple(platform: "iPhoneOS", sdk: iPhoneOSSDK), - "arm64-apple-ios-simulator": triple(platform: "iPhoneSimulator", sdk: iPhoneSimSDK), - "x86_64-apple-ios-simulator": triple(platform: "iPhoneSimulator", sdk: iPhoneSimSDK), - "arm64-apple-macosx": triple(platform: "MacOSX", sdk: macOSSDK), - "x86_64-apple-macosx": triple(platform: "MacOSX", sdk: macOSSDK), - ] - ) - - let encoder = JSONEncoder() - try encoder - .encode(sdkDefinition) - .write(to: output.appendingPathComponent("swift-sdk.json")) - try Data("\(sdkVersion)\n".utf8) .write(to: output.appendingPathComponent("darwin-sdk-version.txt")) } @@ -317,7 +218,6 @@ struct SDKBuilder { .checkSuccess() } - // swiftlint:disable:next function_body_length private func installToolset(in output: URL) async throws { let toolsetDir = output.appendingPathComponent("toolset") let url = URL(string: """ @@ -339,111 +239,37 @@ struct SDKBuilder { to: toolsetDir.appending(path: "bin/llvm-lib") ) - let ld64 = toolsetDir.appending(path: "bin/ld64.lld") - let origBins = toolsetDir.appending(path: "bin/orig") - let origLD64 = origBins.appending(path: "ld64.lld") - try FileManager.default.createDirectory(at: origBins, withIntermediateDirectories: false) - try FileManager.default.moveItem(at: ld64, to: origLD64) - - // this terrible hack works around the fact that lld doesn't support the -r option, - // which merges multiple object files into one (used by SPM's SWB backend to combine - // per-file MachOs into a single one for the module, on Darwin). - // - // so instead, we write a trampoline that intercepts `ld64.lld -r` and instead runs - // libtool to build a static archive. - try """ + let emulatorURL = toolsetDir.appending(path: "bin/xtool-emulate-tool") + try Data(""" #!/bin/sh + exec "${XTL_LOOPBACK_PATH}" _emulate "$0" "$@" + """.utf8).write(to: emulatorURL) + // chmod +x + try FileManager.default.setAttributes( + [.posixPermissions: 0o755], + ofItemAtPath: emulatorURL.path + ) - set -eu - - case "$0" in - */*) script_path="$0" ;; - *) script_path="$(command -v "$0")" ;; - esac - case "$script_path" in - /*) script_dir="${script_path%/*}"; [ -n "$script_dir" ] || script_dir="/" ;; - */*) script_dir="${script_path%/*}" ;; - *) script_dir="." ;; - esac - bin_dir="$(CDPATH= cd -P "$script_dir" && pwd -P)" - - find_argument_value() { - argument_name="$1" - shift - - while [ "$#" -gt 0 ]; do - if [ "$1" = "$argument_name" ]; then - shift - if [ "$#" -gt 0 ]; then - argument_value="$1" - return 0 - fi - return 1 - fi - shift - done - - return 1 + for tool in Self.toolsToEmulate { + try FileManager.default.createSymbolicLink( + atPath: toolsetDir.appending(path: "bin/\(tool)").path, + withDestinationPath: "xtool-emulate-tool" + ) } - - relocatable= - for argument do - if [ "$argument" = "-r" ]; then - relocatable=1 - fi - done - - if [ -n "$relocatable" ]; then - missing_argument= - - if find_argument_value -filelist "$@"; then - filelist="$argument_value" - else - missing_argument=1 - fi - if find_argument_value -dependency_info "$@"; then - dependency_info="$argument_value" - else - missing_argument=1 - fi - if find_argument_value -o "$@"; then - output="$argument_value" - else - missing_argument=1 - fi - - if [ -n "$missing_argument" ]; then - echo "ld64.lld trampoline could not process arguments." >&2 - echo "Please file an issue at https://github.com/xtool-org/xtool/issues" >&2 - echo " Arguments: $@" >&2 - exit 2 - fi - - exec "$bin_dir/llvm-lib" -static \ - -filelist "$filelist" \ - -dependency_info "$dependency_info" \ - -o "$output" - fi - - exec "$bin_dir/orig/ld64.lld" "$@" - """.write( - to: ld64, - atomically: false, - encoding: .utf8 - ) - // set -x - try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: ld64.path) } private func installMacros(in output: URL) async throws { + let toolsetDir = output.appendingPathComponent("toolset") + let url = URL(string: """ https://github.com/xtool-org/OpenAppleMacros/releases/download/\ v\(Self.oamVersion)/OpenAppleMacrosServer-\(arch.rawValue).tar.gz """)! - let macrosDir = output.appendingPathComponent("OpenAppleMacros") + + let tmpMacrosDir = output.appendingPathComponent("OpenAppleMacros") try await fetchTarball( url: url, - destination: macrosDir, + destination: tmpMacrosDir, displayName: "OpenAppleMacros", envOverride: "XTL_OAM_TARBALL", ) @@ -453,16 +279,16 @@ struct SDKBuilder { } let macrosInfo = try JSONDecoder().decode( MacrosInfo.self, - from: Data(contentsOf: macrosDir.appendingPathComponent("info.json")) + from: Data(contentsOf: tmpMacrosDir.appendingPathComponent("info.json")) ) try FileManager.default.moveItem( - at: macrosDir.appendingPathComponent("OpenAppleMacrosServer"), - to: output.appendingPathComponent("OpenAppleMacrosServer") + at: tmpMacrosDir.appendingPathComponent("OpenAppleMacrosServer"), + to: toolsetDir.appendingPathComponent("bin/OpenAppleMacrosServer") ) - try FileManager.default.removeItem(at: macrosDir) + try FileManager.default.removeItem(at: tmpMacrosDir) - let pluginsDir = output.appendingPathComponent("OpenAppleMacrosPlugins") - try FileManager.default.createDirectory(at: pluginsDir, withIntermediateDirectories: false) + let pluginsDir = toolsetDir.appendingPathComponent("plugins") + try FileManager.default.createDirectory(at: pluginsDir, withIntermediateDirectories: true) // create a lib*.so file for each macro 'library' that we support. // the files are stubs (empty) because we just need them to convince swiftc that // yes we can handle said modules. the actual logic lives in the OpenAppleMacros @@ -476,10 +302,9 @@ struct SDKBuilder { private static let expectedXcodeChildren = ["Info.plist", "version.plist", "Developer"] // swiftlint:disable:next cyclomatic_complexity function_body_length - private func installDeveloper(in output: URL) async throws -> URL { - let dev = output.appendingPathComponent("Developer") - + func installDeveloper(in output: URL) async throws -> URL { let expectedAppDir = output.appendingPathComponent("Xcode.app") + let dev = expectedAppDir.appendingPathComponent("Contents/Developer") let appDir: URL let cleanupStageDir: URL? let wanted: Int? @@ -543,7 +368,12 @@ struct SDKBuilder { } try Task.checkCancellation() - try FileManager.default.createDirectory(at: dev, withIntermediateDirectories: false) + if mode.retainsXcode { + // During an update, Xcode.app moves into the new bundle only after the build succeeds. + return appDir.appendingPathComponent("Contents/Developer") + } + + try FileManager.default.createDirectory(at: dev, withIntermediateDirectories: true) var toDoDirs: [String] = ["Contents/Developer"] var count = 0 @@ -575,11 +405,8 @@ struct SDKBuilder { if try child.resourceValues(forKeys: [.isDirectoryKey]).isDirectory == true { toDoDirs.append(path) try FileManager.default.createDirectory(at: dest, withIntermediateDirectories: false) - } else if mode.usesHardLinks { - // Installed SDKs retain Xcode.app developer dir, so avoid storing their developer files twice. - try FileManager.default.linkItem(at: child, to: dest) } else { - // Slim SDKs omit Xcode.app and contain independent copies. + // Slim SDKs contain independent copies of just the selected developer files. try FileManager.default.copyItem(at: child, to: dest) } } @@ -596,76 +423,7 @@ struct SDKBuilder { try FileManager.default.removeItem(at: cleanupStageDir) } - d.out.print("[Finalizing SDKs]") - - for platform in ["iPhoneOS", "MacOSX", "iPhoneSimulator"] { - let lib = "../../../../../Library" - let platformDir = dev.appending(path: "Platforms/\(platform).platform") - - try patchPlatformManifest(path: platformDir.appending(path: "Info.plist")) - - /* - XCTest and Testing.framework are located in *.platform/Developer/{Library/Frameworks,usr/lib} rather than inside - the SDK. These search paths are explicitly included when building tests, which presumably ensures that normal - applications don't accidentally link against them in production. - - SwiftPM makes no such affordances outside of macOS, so we add the usr/lib path as include/library search paths - in the SDK config, and symlink the frameworks into the SDKs (since there's no frameworkSearchPaths option). - While this drops a safeguard it's better than not having the testing libs at all. - */ - - let fwk = platformDir.appending(path: "Developer/SDKs/\(platform).sdk/System/Library/Frameworks").path - - try FileManager.default.createSymbolicLink( - atPath: "\(fwk)/Testing.framework", - withDestinationPath: "\(lib)/Frameworks/Testing.framework" - ) - - try FileManager.default.createSymbolicLink( - atPath: "\(fwk)/XCTest.framework", - withDestinationPath: "\(lib)/Frameworks/XCTest.framework" - ) - - try FileManager.default.createSymbolicLink( - atPath: "\(fwk)/XCUIAutomation.framework", - withDestinationPath: "\(lib)/Frameworks/XCUIAutomation.framework" - ) - - try FileManager.default.createSymbolicLink( - atPath: "\(fwk)/XCTestCore.framework", - withDestinationPath: "\(lib)/PrivateFrameworks/XCTestCore.framework" - ) - - /* - Apply patches to load OpenAppleMacros as the swift-plugin-server - */ - - let bin = platformDir.appendingPathComponent("Developer/usr/bin") - let pluginServer = bin.appendingPathComponent("swift-plugin-server") - try? FileManager.default.createDirectory(at: bin, withIntermediateDirectories: true) - try? FileManager.default.removeItem(at: pluginServer) - try FileManager.default.createSymbolicLink( - atPath: pluginServer.path, - withDestinationPath: "../../../../../../OpenAppleMacrosServer" - ) - - let hostDir = platformDir.appendingPathComponent("Developer/usr/lib/swift/host") - let pluginsDir = hostDir.appendingPathComponent("plugins") - - try FileManager.default.createDirectory(at: hostDir, withIntermediateDirectories: true) - try? FileManager.default.removeItem(at: pluginsDir) - try FileManager.default.createSymbolicLink( - atPath: pluginsDir.path, - withDestinationPath: "../../../../../../../../OpenAppleMacrosPlugins" - ) - } - - // we copy over the host's clang resorurce dir during install, because intrinsics can differ - // by LLVM version - try FileManager.default.removeItem( - at: dev.appending(path: "Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/clang/include") - ) - + try Data().write(to: output.appendingPathComponent("slim-marker")) return dev } @@ -702,26 +460,6 @@ struct SDKBuilder { }.value } - private func patchPlatformManifest(path: URL) throws { - let data = try Data(contentsOf: path) - guard var plist = try PropertyListSerialization.propertyList(from: data, options: [], format: nil) as? [String: Any] else { - throw Console.Error("Could not read '\(path.path)'") - } - - var defaultProperties = (plist["DefaultProperties"] as? [String: Any]) ?? [:] - defaultProperties["SWIFT_RESOURCE_DIR"] = "$(PLATFORM_DIR)/../../Toolchains/XcodeDefault.xctoolchain/usr/lib/swift" - defaultProperties["CLANG_RESOURCE_DIR"] = "$(SWIFT_RESOURCE_DIR)/clang" - defaultProperties["ALTERNATE_LINKER"] = "lld" - // stub out codesign as /bin/true (SwiftBuild often emits CodeSign tasks, e.g. when - // building for sim). we sign post-build. - defaultProperties["CODESIGN"] = "true" - plist["DefaultProperties"] = defaultProperties - - let newData = try PropertyListSerialization.data(fromPropertyList: plist, format: .xml, options: 0) - // a non-atomic write would modify the shared inode that's hard-linked from Xcode.app - try newData.write(to: path, options: .atomic) - } - // returns the number of files we actually want to keep, // useful for computing progress % during fs traversal private func _extractXIP(inputPath: String, outDir: String) async throws -> Int { @@ -811,19 +549,6 @@ struct SDKBuilder { } } -struct SDKDefinition: Encodable { - struct Triple: Encodable { - var sdkRootPath: String - var includeSearchPaths: [String] - var librarySearchPaths: [String] - var swiftResourcesPath: String - var swiftStaticResourcesPath: String - var toolsetPaths: [String] - } - var schemaVersion: String - var targetTriples: [String: Triple] -} - struct SDKEntry { var names: Set var values: [SDKEntry] = [] diff --git a/Sources/XToolSupport/SDKCommand.swift b/Sources/XToolSupport/SDKCommand.swift index ff9d8de1..a4bafbf0 100644 --- a/Sources/XToolSupport/SDKCommand.swift +++ b/Sources/XToolSupport/SDKCommand.swift @@ -255,7 +255,7 @@ struct UpdateSDKOperation { throw Console.Error("Could not locate existing SDK; cannot perform update.") } let xcode = existing.bundle.appendingPathComponent("Xcode.app") - guard xcode.dirExists else { + guard existing.flavor != .slim, xcode.dirExists else { // This includes prebuilt .xtoolsdk installs and installs created with --slim. throw Console.Error(""" The installed SDK was built in 'slim' mode and cannot be updated in place. \ diff --git a/Sources/XToolSupport/Signposter.swift b/Sources/XToolSupport/Signposter.swift new file mode 100644 index 00000000..3c941e49 --- /dev/null +++ b/Sources/XToolSupport/Signposter.swift @@ -0,0 +1,21 @@ +import XUtils +import Foundation + +enum Signposter { + static let start = Date() + + static let isEnabled = ProcessInfo.processInfo.environment["XTL_SIGNPOST"] == "1" + + static func setup() { + _ = start + } + + static func track(_ message: @autoclosure () -> String) { + guard isEnabled else { return } + let start = Self.start + let now = Date() + let elapsed = now.timeIntervalSince(start) + let elapsedString = String(format: "%.2f", elapsed) + d.out.print("[\(elapsedString)s] \(message())") + } +} diff --git a/Sources/XToolSupport/SwiftBuildService.swift b/Sources/XToolSupport/SwiftBuildService.swift new file mode 100644 index 00000000..a7110681 --- /dev/null +++ b/Sources/XToolSupport/SwiftBuildService.swift @@ -0,0 +1,9 @@ +import SwiftBuild +// we don't want to import SWB* in other places +private import SWBBuildService + +extension SWBBuildServiceConnectionMode { + static var xtool: Self { + .inProcessStatic(swiftbuildServiceEntryPoint) + } +} diff --git a/Sources/XToolSupport/SwiftPMBuildDescriber+Products.swift b/Sources/XToolSupport/SwiftPMBuildDescriber+Products.swift new file mode 100644 index 00000000..fece2260 --- /dev/null +++ b/Sources/XToolSupport/SwiftPMBuildDescriber+Products.swift @@ -0,0 +1,116 @@ +import Foundation +import PackLib +import XCLIUtils +import XPIF +import XUtils + +extension SwiftPMBuildDescriber { + func bundleProducts(in project: XPIF.Project) throws -> [ProjectProduct] { + let appProduct = try libraryProduct(in: project, matching: schema.product) + let app = ProjectProduct( + packageProduct: appProduct, + isExtension: false, + bundleID: idSpecifier.formBundleID(product: appProduct.name.withoutSuffix("-product")), + infoPath: schema.infoPath, + resources: schema.resources ?? [], + iconPath: schema.iconPath, + entitlementsPath: schema.entitlementsPath, + ) + let extensions = try (schema.extensions ?? []).map { ext in + let product = try libraryProduct(in: project, matching: ext.product) + return ProjectProduct( + packageProduct: product, + isExtension: true, + bundleID: ext.bundleID ?? "\(app.bundleID).\(product.name.withoutSuffix("-product"))", + infoPath: ext.infoPath, + resources: ext.resources ?? [], + iconPath: nil, + entitlementsPath: ext.entitlementsPath, + ) + } + return [app] + extensions + } + + private func libraryProduct(in project: XPIF.Project, matching expected: String?) throws -> XPIF.Target { + let products = Self.products(in: project).filter { + [.packageProduct, .staticArchive].contains($0.productType) + } + if let expected { + return try products.first { $0.name == "\(expected)-product" } + .orThrow(Console.Error("Could not find library product '\(expected)'")) + } + switch products.count { + case 0: throw Console.Error("Could not find any library products in main target") + case 1: return products[0] + default: throw Console.Error("Found multiple library products in main target; specify 'product' in xtool.yml") + } + } +} + +struct ProjectProduct { + var packageProduct: XPIF.Target + var product: String { packageProduct.name.withoutSuffix("-product") } + var isExtension: Bool + var bundleID: String + var infoPath: String? + var resources: [String] + var iconPath: String? + var entitlementsPath: String? + + var targetName: String { "\(product)-\(isExtension ? "Extension" : "App")" } + + func infoPlistConfiguration( + at root: URL, + ) throws -> (contents: [String: Any], settings: XPIF.BuildSettings) { + var plist: [String: Any] = [:] + if let path = infoPath { + let data = try Data(contentsOf: URL(fileURLWithPath: path, relativeTo: root)) + plist = try (PropertyListSerialization.propertyList(from: data, format: nil) as? [String: Any]) + .orThrow(Console.Error("Info.plist has invalid format: expected a dictionary.")) + } + if let iconPath { + plist["CFBundleIconFile"] = URL(fileURLWithPath: iconPath).deletingPathExtension().lastPathComponent + } + + var settings = XPIF.BuildSettings() + settings[.GENERATE_INFOPLIST_FILE] = "YES" + settings[.PRODUCT_NAME] = product + settings[.PRODUCT_BUNDLE_IDENTIFIER] = plistSetting(plist, "CFBundleIdentifier", "PRODUCT_BUNDLE_IDENTIFIER", bundleID) + settings[.CURRENT_PROJECT_VERSION] = plistSetting(plist, "CFBundleVersion", "CURRENT_PROJECT_VERSION", "1") + settings[.MARKETING_VERSION] = plistSetting(plist, "CFBundleShortVersionString", "MARKETING_VERSION", "1.0.0") + let families = (plist.removeValue(forKey: "UIDeviceFamily") as? [Int]) ?? [1, 2] + settings[single: "TARGETED_DEVICE_FAMILY[sdk=iphone*]"] = families.map(String.init).joined(separator: ",") + if plist["CFBundleExecutable"] != nil { + settings[single: "EXECUTABLE_NAME"] = plistSetting(plist, "CFBundleExecutable", "EXECUTABLE_NAME", product) + } + if plist["CFBundleDevelopmentRegion"] != nil { + settings[single: "DEVELOPMENT_LANGUAGE"] = plistSetting(plist, "CFBundleDevelopmentRegion", "DEVELOPMENT_LANGUAGE", "en") + } + if !isExtension { + if plist["UILaunchScreen"] == nil { + settings[single: "INFOPLIST_KEY_UILaunchScreen_Generation[sdk=iphone*]"] = "YES" + } + // Swift Build unions arrays when merging; only supply defaults for absent keys. + if plist["UISupportedInterfaceOrientations"] == nil { + settings[multiple: "INFOPLIST_KEY_UISupportedInterfaceOrientations[sdk=iphone*]"] = [ + "UIInterfaceOrientationPortrait", + ] + } + if plist["UISupportedInterfaceOrientations~ipad"] == nil { + settings[multiple: "INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad[sdk=iphone*]"] = [ + "UIInterfaceOrientationPortrait", "UIInterfaceOrientationPortraitUpsideDown", + "UIInterfaceOrientationLandscapeLeft", "UIInterfaceOrientationLandscapeRight", + ] + } + } + return (plist, settings) + } + + private func plistSetting(_ plist: [String: Any], _ key: String, _ setting: String, _ fallback: String) -> String { + // Standard Info.plists often reference the very build setting we're deriving. + // Substitute its default instead of introducing a self-referential setting. + ((plist[key] as? String) ?? fallback) + .replacingOccurrences(of: "$(\(setting))", with: fallback) + .replacingOccurrences(of: "${\(setting)}", with: fallback) + } +} diff --git a/Sources/PackLib/XcodePacker.swift b/Sources/XToolSupport/SwiftPMBuildDescriber+XcodeProject.swift similarity index 74% rename from Sources/PackLib/XcodePacker.swift rename to Sources/XToolSupport/SwiftPMBuildDescriber+XcodeProject.swift index ecac7d41..f9cf7cfd 100644 --- a/Sources/PackLib/XcodePacker.swift +++ b/Sources/XToolSupport/SwiftPMBuildDescriber+XcodeProject.swift @@ -1,17 +1,26 @@ +import XCLIUtils import Foundation +import Dependencies import XcodeProjects +import XPIF +import PackLib +import XUtils -public struct XcodePacker { - public var plan: Plan - - public init(plan: Plan) { - self.plan = plan +extension SwiftPMBuildDescriber { + func writeXcodeProject() async throws -> ProjectBuildResult { + try writeXcodeProject(basePIF: await d.swiftPMPIFDumper.dump(buildSettings: buildSettings)) } // swiftlint:disable:next function_body_length - public func createProject( - at root: URL = URL(fileURLWithPath: FileManager.default.currentDirectoryPath, isDirectory: true) - ) async throws -> URL { + func writeXcodeProject(basePIF pif: XPIF.Container) throws -> ProjectBuildResult { + let root = URL(filePath: pif.workspace.path, directoryHint: .isDirectory) + let mainProject = try pif.workspace.projects.first { + $0.projectDir == pif.workspace.path && $0.id != "AGGREGATE" + }.orThrow(Console.Error("Could not find main target")) + let products = try bundleProducts(in: mainProject) + let app = products[0] + let extensions = Array(products.dropFirst()) + let xtoolDir = root.appendingPathComponent("xtool", isDirectory: true) let projectDir = xtoolDir.appendingPathComponent(".xtool-tmp", isDirectory: true) try? FileManager.default.removeItem(at: projectDir) @@ -20,7 +29,6 @@ public struct XcodePacker { let fromProjectToRoot = "../.." let emptyText = Data("// leave this file empty".utf8) - let products = plan.allProducts let targetIDs = products.map { XCSchema.ObjectID("xtool:\($0.product)") } @@ -36,30 +44,27 @@ public struct XcodePacker { try FileManager.default.createDirectory(at: productDir, withIntermediateDirectories: true) try emptyText.write(to: productDir.appendingPathComponent("empty.c")) - var plist = product.infoPlist - let families = (plist.removeValue(forKey: "UIDeviceFamily") as? [Int]) ?? [1, 2] - plist["CFBundleExecutable"] = product.targetName - plist["CFBundleName"] = product.targetName - plist["CFBundleDisplayName"] = product.product - if let iconPath = product.iconPath { - plist["CFBundleIconFile"] = URL(fileURLWithPath: iconPath).deletingPathExtension().lastPathComponent - } + let (plist, plistSettings) = try product.infoPlistConfiguration(at: root) let encodedPlist = try PropertyListSerialization.data(fromPropertyList: plist, format: .xml, options: 0) try encodedPlist.write(to: productDir.appendingPathComponent("Info.plist")) - var buildSettings: [String: XCSchema.BuildSetting] = [ - "PRODUCT_BUNDLE_IDENTIFIER": .string(product.bundleID), - "PRODUCT_NAME": .string(product.targetName), - "TARGETED_DEVICE_FAMILY": .string(families.map(String.init).joined(separator: ",")), - "IPHONEOS_DEPLOYMENT_TARGET": .string(product.deploymentTarget), - "INFOPLIST_FILE": .string("\(product.product)/Info.plist"), - "LD_RUNPATH_SEARCH_PATHS": .array(["$(inherited)", "@executable_path/Frameworks"]), - ] - if product.type == .appExtension { + var buildSettings: [String: XCSchema.BuildSetting] = plistSettings.values.mapValues { value in + switch value { + case .string(let value): .string(value) + case .array(let value): .array(value) + } + } + buildSettings["INFOPLIST_FILE"] = .string("\(product.product)/Info.plist") + buildSettings["LD_RUNPATH_SEARCH_PATHS[sdk=iphone*]"] = .array(["$(inherited)", "@executable_path/Frameworks"]) + buildSettings["LD_RUNPATH_SEARCH_PATHS[sdk=macosx*]"] = .array(["$(inherited)", "@executable_path/../Frameworks"]) + if product.isExtension { buildSettings["APPLICATION_EXTENSION_API_ONLY"] = .string("YES") - buildSettings["LD_RUNPATH_SEARCH_PATHS"] = .array([ + buildSettings["LD_RUNPATH_SEARCH_PATHS[sdk=iphone*]"] = .array([ "$(inherited)", "@executable_path/Frameworks", "@executable_path/../../Frameworks", ]) + buildSettings["LD_RUNPATH_SEARCH_PATHS[sdk=macosx*]"] = .array([ + "$(inherited)", "@executable_path/../Frameworks", "@executable_path/../../../../Frameworks", + ]) } if let entitlementsPath = product.entitlementsPath { let path = (entitlementsPath as NSString).isAbsolutePath @@ -105,11 +110,11 @@ public struct XcodePacker { children: [.fileReference(source), .fileReference(info)] + resources, ))) - let productFilename = "\(product.targetName).\(product.type == .application ? "app" : "appex")" + let productFilename = "\(product.product).\(product.isExtension ? "appex" : "app")" let productFile = XCSchema.FileReference( objectID: nil, path: try .init(base: .buildProducts, path: productFilename), - explicitFileType: .init(fileTypeID: product.type == .application ? "wrapper.application" : "wrapper.app-extension"), + explicitFileType: .init(fileTypeID: product.isExtension ? "wrapper.app-extension" : "wrapper.application"), expectedSignature: nil, textEncoding: nil, lineEnding: nil, @@ -117,7 +122,7 @@ public struct XcodePacker { buildFiles: index == 0 ? [] : [.init( objectID: nil, buildPhase: .named( - target: .init(targetName: plan.app.targetName), + target: .init(targetName: app.targetName), kind: .copy, name: "Embed App Extensions", ), @@ -150,7 +155,7 @@ public struct XcodePacker { scope: .always, ))) } - if index == 0 && !plan.extensions.isEmpty { + if index == 0 && !extensions.isEmpty { phases.append(.copy(.init( objectID: nil, name: "Embed App Extensions", @@ -160,7 +165,7 @@ public struct XcodePacker { ))) } let dependencies: [XCSchema.TargetDependency] = index == 0 - ? plan.extensions.map { + ? extensions.map { .localTarget(XCSchema.LocalTargetReference(targetName: $0.targetName), []) } : [] @@ -175,9 +180,9 @@ public struct XcodePacker { buildSettings: buildSettings, product: .namePath(.init(components: [.child("Products"), .child(productFilename)])), productTypeID: XCSchema.ProductTypeID( - abbreviatedRepresentation: product.type == .application - ? "application" - : "app-extension" + abbreviatedRepresentation: product.isExtension + ? "app-extension" + : "application" ), testHostTarget: nil, legacyProvisioningStyle: nil, @@ -205,7 +210,7 @@ public struct XcodePacker { configurations: ["Debug", "Release"].map { .init(name: .init(name: $0), file: nil, objectID: nil) }, - buildSettings: ["SDKROOT": .string("iphoneos")], + buildSettings: projectBuildSettings(from: mainProject), defaultConfigurationName: .init(name: "Debug"), targets: targets, localizationInfo: .init(development: .init(languageID: "en"), supported: []), @@ -220,24 +225,40 @@ public struct XcodePacker { importedProducts: [], ) - let projectURL = projectDir.appending(path: "\(plan.app.product).xcodeproj") + let projectURL = projectDir.appending(path: "\(app.product).xcodeproj") let project = XcodeProject(project: schema, url: projectURL) try project.write() - let workspaceURL = xtoolDir.appending(path: "\(plan.app.product).xcworkspace") + let workspaceURL = xtoolDir.appending(path: "\(app.product).xcworkspace") let workspace = XcodeWorkspace(children: [ .file(XcodeWorkspace.Location(base: .container, path: "..")), .file(XcodeWorkspace.Location(base: .group, path: ".xtool-tmp/\(projectURL.lastPathComponent)")) ], url: workspaceURL) try workspace.write() - return workspaceURL + return ProjectBuildResult(url: workspaceURL, bundleID: app.bundleID) } - private func resourceReferences(for product: Plan.Product, at root: URL) throws -> [XCSchema.Reference] { - var paths = product.resources.compactMap { resource -> String? in - guard case .root(let source) = resource else { return nil } - return source + private func projectBuildSettings(from project: XPIF.Project) -> [String: XCSchema.BuildSetting] { + var settings: [String: XCSchema.BuildSetting] = [ + "SDKROOT": .string("auto"), + "SDK_VARIANT": .string("auto"), + "SUPPORTED_PLATFORMS": .array(Self.supportedPlatforms), + ] + // Preserve each configuration's platform minimums without copying SwiftPM's + // package-specific compiler and linker settings onto the app and extensions. + for configuration in project.buildConfigs { + for (key, value) in configuration.settings.values { + let name = key.prefix { $0 != "[" } + guard name.hasSuffix("_DEPLOYMENT_TARGET"), + case .string(let value) = value else { continue } + settings["\(key)[config=\(configuration.name)]"] = .string(value) + } } + return settings + } + + private func resourceReferences(for product: ProjectProduct, at root: URL) throws -> [XCSchema.Reference] { + var paths = product.resources if let iconPath = product.iconPath { paths.append(iconPath) } diff --git a/Sources/XToolSupport/SwiftPMBuildDescriber.swift b/Sources/XToolSupport/SwiftPMBuildDescriber.swift new file mode 100644 index 00000000..99f92579 --- /dev/null +++ b/Sources/XToolSupport/SwiftPMBuildDescriber.swift @@ -0,0 +1,212 @@ +import XCLIUtils +import XPIF +import Foundation +import PackLib +import XUtils +import Dependencies + +public struct SwiftPMBuildDescriber { + let buildSettings: BuildSettings + let schema: PackSchema + let idSpecifier: PackSchema.IDSpecifier + + static let supportedPlatforms = ["macosx", "iphoneos", "iphonesimulator"] + + public init(buildSettings: BuildSettings, schema: PackSchema) throws { + self.buildSettings = buildSettings + self.schema = schema + self.idSpecifier = schema.idSpecifier + } + + func describe() async throws -> BuildDescription { + try describe(basePIF: await d.swiftPMPIFDumper.dump(buildSettings: buildSettings)) + } + + /// Generated dynamic variants remain in the graph, but are not separate manifest products. + static func products(in project: XPIF.Project) -> [XPIF.Target] { + let targets = project.targets.compactMap { base -> XPIF.Target? in + guard case .target(let target) = base else { return nil } + return target + } + let variants = Set(targets.compactMap(\.dynamicTargetVariantId)) + return targets.filter { $0.id.value.hasPrefix("PACKAGE-PRODUCT:") && !variants.contains($0.id) } + } + + func describe(basePIF pif: XPIF.Container) throws -> BuildDescription { + var container = pif + #if !os(macOS) + container.workspace.projects = container.workspace.projects.map { $0.withoutRelocatableLinkage() } + #endif + let mainProjectIndex = try container.workspace.projects.firstIndex { + $0.projectDir == container.workspace.path && $0.id != "AGGREGATE" + }.orThrow(Console.Error("Could not find main target")) + var project = container.workspace.projects[mainProjectIndex] + let products = try bundleProducts(in: project) + let root = URL(filePath: container.workspace.path, directoryHint: .isDirectory) + // we don't use TemporaryDirectory: we need stable paths to avoid the PIF changing, but + // TemporaryDirectory includes a random UUID in the path + // Stable paths also keep Swift Build's incremental state reusable. + let temporaryDirectory = root.appending(path: "xtool/.xtool-tmp") + try FileManager.default.createDirectory(at: temporaryDirectory, withIntermediateDirectories: true) + let source = temporaryDirectory.appending(path: "empty.c") + try writeIfChanged(Data(), to: source) + + for product in products { + if product.packageProduct.productType == .staticArchive { + var packageProduct = product.packageProduct + // Let SwiftBuild expand the app or extension product's libraries into the final link. A static + // archive cannot carry its dynamic dependencies into the app or extension's linker command. + packageProduct.productType = .packageProduct + project[target: .init(rawValue: packageProduct.id)] = packageProduct + } + let target = try bundleTarget( + product, extensions: product.isExtension ? [] : Array(products.dropFirst()), + root: root, source: source, project: &project + ) + try project.addTarget { _ in target } + } + container.workspace.projects[mainProjectIndex] = project + return BuildDescription(pif: container, buildSettings: buildSettings) + } + + private func bundleTarget( + _ product: ProjectProduct, + extensions: [ProjectProduct], + root: URL, + source: URL, + project: inout XPIF.Project + ) throws -> XPIF.Target { + let id = product.targetGUID + let settings = try bundleSettings(product, root: root) + let sourceReference = project.mainGroup.addFileReference { _ in + XPIF.FileReference(id: "\(id):source-ref", path: source.path, pathBase: .absolute) + } + var phases: [XPIF.BuildPhase] = [ + .sources(.init(id: "\(id):sources", files: [ + .init(id: "\(id):source", fileRef: sourceReference), + ])), + .frameworks(.init(id: "\(id):frameworks", files: [ + .init(id: "\(id):product", ref: .targetProduct(id: product.packageProduct.id)), + ])), + ] + let resources = resourceBuildFiles(product, root: root, project: &project) + if !resources.isEmpty { + // Copy verbatim, including directory resources, just as the original packer did. + phases.append(.copyFiles(.init( + common: .init(id: "\(id):resources", files: resources), + destinationSubfolder: .resources + ))) + } + if !extensions.isEmpty { + phases.append(.copyFiles(.init( + common: .init(id: "\(id):extensions", files: extensions.map { ext in + .init(id: "\(id):embed:\(ext.product)", ref: .targetProduct(id: ext.targetGUID), + removeHeadersOnCopy: true) + }), + destinationSubfolder: .plugins + ))) + } + return XPIF.Target( + id: id, + productType: product.isExtension ? .appExtension : .application, + name: product.targetName, + productName: "\(product.product).\(product.isExtension ? "appex" : "app")", + approvedByUser: true, + buildConfigs: ["Debug", "Release"].map { configuration in + .init(id: "\(id):\(configuration)", name: configuration, settings: settings) + }, + buildPhases: phases, + dependencies: [.init(targetId: product.packageProduct.id)] + + extensions.map { .init(targetId: $0.targetGUID) } + ) + } + + private func bundleSettings(_ product: ProjectProduct, root: URL) throws -> XPIF.BuildSettings { + let (plist, plistSettings) = try product.infoPlistConfiguration(at: root) + var settings = plistSettings + if !plist.isEmpty { + let directory = root.appending(path: "xtool/.xtool-tmp/\(product.targetName)") + try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true) + let infoPath = directory.appending(path: "Info.plist") + try writeIfChanged(PropertyListSerialization.data(fromPropertyList: plist, format: .xml, options: 0), to: infoPath) + settings[.INFOPLIST_FILE] = infoPath.path + } + // Swift Build specializes the app or extension for the build request's run destination. + // Deployment targets are inherited from SwiftPM's project configurations. + settings[.SDKROOT] = "auto" + settings[.SDK_VARIANT] = "auto" + settings[.SUPPORTED_PLATFORMS] = Self.supportedPlatforms + if let entitlementsPath = product.entitlementsPath { + settings[single: "CODE_SIGN_ENTITLEMENTS"] = URL(fileURLWithPath: entitlementsPath, relativeTo: root) + .standardizedFileURL.path + } + settings[.SKIP_INSTALL] = product.isExtension ? "YES" : "NO" + settings[.INSTALL_PATH] = "/" + settings[.LD_RUNPATH_SEARCH_PATHS, .iOS] = ["$(inherited)", "@executable_path/Frameworks"] + settings[.LD_RUNPATH_SEARCH_PATHS, .macOS] = ["$(inherited)", "@executable_path/../Frameworks"] + if product.isExtension { + settings[.APPLICATION_EXTENSION_API_ONLY] = "YES" + // SwiftPM selects swiftc as the linker driver, but Swift Build's extension + // product type passes -fapplication-extension, which requires clang. + settings[single: "LINKER_DRIVER"] = "clang" + settings[.LD_RUNPATH_SEARCH_PATHS, .iOS] = [ + "$(inherited)", "@executable_path/Frameworks", "@executable_path/../../Frameworks", + ] + settings[.LD_RUNPATH_SEARCH_PATHS, .macOS] = [ + "$(inherited)", "@executable_path/../Frameworks", "@executable_path/../../../../Frameworks", + ] + } + return settings + } + + private func resourceBuildFiles( + _ product: ProjectProduct, root: URL, project: inout XPIF.Project + ) -> [XPIF.BuildFile] { + var visited: Set = [] + let paths = product.resources + (product.iconPath.map { [$0] } ?? []) + return paths.enumerated().compactMap { index, path in + let url = URL(fileURLWithPath: path, relativeTo: root).standardizedFileURL + guard visited.insert(url.path).inserted else { return nil } + var isDirectory: ObjCBool = false + _ = FileManager.default.fileExists(atPath: url.path, isDirectory: &isDirectory) + let id: XPIF.GUID = "\(product.targetGUID):resource:\(index)" + let reference = project.mainGroup.addFileReference { _ in + XPIF.FileReference(id: "\(id):ref", path: url.path, pathBase: .absolute, + fileType: isDirectory.boolValue ? "folder" : nil) + } + return .init(id: id, fileRef: reference) + } + } + + private func writeIfChanged(_ data: Data, to url: URL) throws { + guard (try? Data(contentsOf: url)) != data else { return } + try data.write(to: url) + } +} + +private extension ProjectProduct { + var targetGUID: XPIF.GUID { + isExtension ? "xtool:extension:\(product)" : "xtool:main-app" + } +} + +extension XPIF.Project { + func withoutRelocatableLinkage() -> XPIF.Project { + var project: XPIF.Project = self + project.targets = project.targets.map { base in + guard case var .target(target) = base else { return base } + if target.productType == .commonStaticArchive { + // `commonStaticArchive` combines individual `.o` files into one mega `.o` for + // the entire module, using ` -r`. + // + // Unfortunately, lld (which we use on Linux) doesn't yet support `-r` for Darwin + // targets. So use the `objectLibrary` product type instead, which keeps around + // loose `.o` files for each source file, instead of trying to merge them. + target.productType = .objectLibrary + // FIXME: objlib breaks lipo for fat binaries targetting macOS + } + return .target(target) + } + return project + } +} diff --git a/Sources/XToolSupport/SwiftPMPIFDumper.swift b/Sources/XToolSupport/SwiftPMPIFDumper.swift new file mode 100644 index 00000000..a78b19e0 --- /dev/null +++ b/Sources/XToolSupport/SwiftPMPIFDumper.swift @@ -0,0 +1,46 @@ +import XPIF +import Foundation +import PackLib +import XUtils +import Subprocess +import Dependencies + +/// Uses the installed SwiftPM to resolve a package graph and generate its PIF. +struct SwiftPMPIFDumper: DependencyKey, Sendable { + static var liveValue: Self { Self() } + static var testValue: Self { liveValue } + + func dump(buildSettings: BuildSettings) async throws -> XPIF.Container { + let dumpPIFInvocation = try await buildSettings.swiftPMInvocation( + forTool: "package", + arguments: ["dump-pif", "--experimental-build-dylibs-as-frameworks"], + ) + let result = try await Subprocess.run( + dumpPIFInvocation, + input: .none, + output: .data(limit: .max), + error: .sequence, + ) { execution in + for try await contents in execution.standardError { + @Dependency(\.out) var output + output.write( + contents.bytes.withUnsafeBytes { Data($0) }, + .stderr, + false + ) + } + } + let pif = try result.checkSuccess().standardOutput + + Signposter.track("Dumped PIF") + + return try XPIF.Container(data: pif) + } +} + +extension DependencyValues { + var swiftPMPIFDumper: SwiftPMPIFDumper { + get { self[SwiftPMPIFDumper.self] } + set { self[SwiftPMPIFDumper.self] = newValue } + } +} diff --git a/Sources/XToolSupport/XTool.swift b/Sources/XToolSupport/XTool.swift index 4b4cbeb3..f6677872 100644 --- a/Sources/XToolSupport/XTool.swift +++ b/Sources/XToolSupport/XTool.swift @@ -7,6 +7,7 @@ import Dependencies public enum XTool { public static func run(arguments: [String]? = nil) async throws { + Signposter.setup() TemporaryDirectory.prepare() await XToolCommand.cancellableMain(arguments) } @@ -17,6 +18,9 @@ private struct XToolCommand: AsyncParsableCommand { commandName: "xtool", abstract: "Cross-platform Xcode replacement", version: "xtool \(XTool.version)", + subcommands: [ + EmulateToolCommand.self, + ], groupedSubcommands: [ CommandGroup( name: "Configuration", diff --git a/Sources/XToolSupport/XToolInstallerDelegate.swift b/Sources/XToolSupport/XToolInstallerDelegate.swift index 2f1cea47..d3d18b3a 100644 --- a/Sources/XToolSupport/XToolInstallerDelegate.swift +++ b/Sources/XToolSupport/XToolInstallerDelegate.swift @@ -77,6 +77,10 @@ actor XToolInstallerDelegate: IntegratedInstallerDelegate { } } + func finish() async { + await updateTask.value?.value + } + func confirmRevocation(of certificates: [DeveloperServicesCertificate]) async -> Bool { output.print("\nThe following certificates must be revoked:") output.print( diff --git a/Sources/XUtils/Foundation+Utils.swift b/Sources/XUtils/Foundation+Utils.swift index 1af45333..70ea20b3 100644 --- a/Sources/XUtils/Foundation+Utils.swift +++ b/Sources/XUtils/Foundation+Utils.swift @@ -52,6 +52,33 @@ extension FileManager { try self.copyItem(at: srcURL, to: dstURL) } + + /// Copies source to destination _nearly_ atomically. + /// + /// We first perform an intermediate copy into `stagingDir`, then swap + /// it with `destination`. + /// + /// The operation has a strong-exception guarantee except if the final + /// swap fails, in which case the destination may be left absent (but this + /// is less likely than the copy failing). + public func atomicallyCopyItem( + at source: URL, + to destination: URL, + via stagingDir: URL, + ) throws { + let newStaged = stagingDir.appending(path: "new-\(destination.lastPathComponent)") + let oldStaged = stagingDir.appending(path: "old-\(destination.lastPathComponent)") + try? removeItem(at: stagingDir) + try createDirectory(at: stagingDir, withIntermediateDirectories: true) + defer { try? removeItem(at: stagingDir) } + try copyItem(at: source, to: newStaged) + do { + try moveItem(at: destination, to: oldStaged) + } catch CocoaError.fileNoSuchFile { + // destination doesn't exist yet, that's fine + } + try moveItem(at: newStaged, to: destination) + } } package func stderrPrint(_ message: String, terminator: String = "\n") { diff --git a/Tests/XToolTests/BuildCoordinatorTripleTests.swift b/Tests/XToolTests/BuildCoordinatorTripleTests.swift new file mode 100644 index 00000000..53832902 --- /dev/null +++ b/Tests/XToolTests/BuildCoordinatorTripleTests.swift @@ -0,0 +1,71 @@ +import CustomDump +import Dependencies +import Foundation +import PackLib +import SwiftBuild +import Testing +import XPIF +import XUtils +@testable import XToolSupport + +#if os(macOS) +struct BuildCoordinatorTripleTests { + @Test(.requiresXcode, arguments: [ + ("arm64-apple-ios", "arm64", "iphoneos", nil, "IPHONEOS_DEPLOYMENT_TARGET", "16.0"), + ("arm64e-apple-ios17.0", "arm64e", "iphoneos", nil, "IPHONEOS_DEPLOYMENT_TARGET", "17.0"), + ("arm64-apple-ios17.2-simulator", "arm64", "iphonesimulator", nil, "IPHONEOS_DEPLOYMENT_TARGET", "17.2"), + ("x86_64-apple-ios17.0-simulator", "x86_64", "iphonesimulator", nil, "IPHONEOS_DEPLOYMENT_TARGET", "17.0"), + ("arm64-apple-ios17.0-macabi", "arm64", "macosx", "iosmac", "IPHONEOS_DEPLOYMENT_TARGET", "17.0"), + ("arm64-apple-macos14.0", "arm64", "macosx", nil, "MACOSX_DEPLOYMENT_TARGET", "14.0"), + ] as [(String, String, String, String?, String, String)]) + func resolvesTriple( + triple: String, arch: String, platform: String, variant: String?, deploymentSetting: String, deployment: String + ) async throws { + let directory = try TemporaryDirectory(name: "BuildCoordinatorTripleTests") + let description = try await description(triple: triple, root: directory.url) + let coordinator = try await withDependencies { + $0.out = .liveValue + } operation: { + try await BuildCoordinator(description: description) + } + defer { await coordinator.close() } + let parameters = await coordinator.buildRequest.parameters + let destination = try #require(parameters.activeRunDestination) + let expectedTarget = SWBRunDestinationInfo.SWBBuildTarget.toolchainSDK( + platform: platform, sdk: platform, sdkVariant: variant + ) + expectNoDifference(diff(destination.buildTarget, expectedTarget), nil) + expectNoDifference(destination.targetArchitecture, arch) + expectNoDifference(parameters.activeArchitecture, arch) + let session = await coordinator.sessionManager.session + let actualDeployment = try await session.evaluateMacroAsString( + deploymentSetting, level: .project("project"), buildParameters: parameters, overrides: nil + ) + expectNoDifference(actualDeployment, deployment) + } + + @Test(.requiresXcode, arguments: ["arm64", "arm64-apple-ios-invalid-version", "arm64-apple-unrecognized"]) + func rejectsInvalidTriples(triple: String) async throws { + let directory = try TemporaryDirectory(name: "InvalidTripleTests") + let description = try await description(triple: triple, root: directory.url) + await #expect(throws: (any Error).self) { + let coordinator = try await BuildCoordinator(description: description) + await coordinator.close() + } + } + + private func description(triple: String, root: URL) async throws -> BuildDescription { + let settings = try await BuildSettings(configuration: .debug, triple: triple) + var projectSettings = XPIF.BuildSettings() + projectSettings[.SDKROOT] = "auto" + projectSettings[.IPHONEOS_DEPLOYMENT_TARGET] = "16.0" + projectSettings[.MACOSX_DEPLOYMENT_TARGET] = "13.0" + let project = XPIF.Project( + id: "project", path: root.appending(path: "Package.swift").path, projectDir: root.path, name: "Package", + buildConfigs: [.init(id: "debug", name: "Debug", settings: projectSettings)] + ) + let pif = XPIF.Container(workspace: .init(id: "workspace", name: "Package", path: root.path, projects: [project])) + return BuildDescription(pif: pif, buildSettings: settings) + } +} +#endif diff --git a/Tests/XToolTests/DarwinSDKTests.swift b/Tests/XToolTests/DarwinSDKTests.swift index ed5ecb57..96c2e4a6 100644 --- a/Tests/XToolTests/DarwinSDKTests.swift +++ b/Tests/XToolTests/DarwinSDKTests.swift @@ -1,5 +1,7 @@ +import Dependencies import Foundation import Testing +import XUtils @testable import PackLib @Test func swiftPMDirectoryUsesXDGConfigHome() throws { @@ -29,27 +31,138 @@ import Testing } } -@Test func temporaryDarwinSDKBundleUsesSwiftSDKsDirectory() throws { - let configDirectory = FileManager.default.temporaryDirectory +@Test func temporaryDarwinSDKBundleUsesXtoolDirectory() throws { + let homeDirectory = FileManager.default.temporaryDirectory .appendingPathComponent("DarwinSDKTests-\(UUID().uuidString)", isDirectory: true) - defer { try? FileManager.default.removeItem(at: configDirectory) } - - let environment = ["XDG_CONFIG_HOME": configDirectory.path] - let sdksDirectory = configDirectory.appending(path: "swiftpm/swift-sdks") - let temporaryURL = sdksDirectory.appendingPathComponent("darwin.artifactbundle.tmp", isDirectory: true) - let installedBundle = sdksDirectory.appending(path: "darwin.artifactbundle") - try FileManager.default.createDirectory(at: installedBundle, withIntermediateDirectories: true) - try FileManager.default.createDirectory(at: temporaryURL, withIntermediateDirectories: false) - - do { - let temporaryBundle = try DarwinSDK.prepareTemporaryBundle(environment: environment) - #expect(temporaryBundle.url == temporaryURL) + defer { try? FileManager.default.removeItem(at: homeDirectory) } + + try withDependencies { + $0.persistentDirectory = homeDirectory.appending(path: "persistent") + } operation: { + let directory = homeDirectory.appending(path: "persistent") + let temporaryURL = directory.appendingPathComponent("darwin.xtoolsdk.tmp", isDirectory: true) + let installedBundle = directory.appending(path: "darwin.xtoolsdk") + try FileManager.default.createDirectory(at: installedBundle, withIntermediateDirectories: true) + try FileManager.default.createDirectory(at: temporaryURL, withIntermediateDirectories: false) + + do { + let temporaryBundle = try DarwinSDK.prepareTemporaryBundle() + #expect(temporaryBundle.url == temporaryURL) + #expect(!FileManager.default.fileExists(atPath: temporaryURL.path)) + + try FileManager.default.createDirectory(at: temporaryBundle.url, withIntermediateDirectories: false) + #expect(DarwinSDK(bundle: temporaryBundle.url)?.version == "develop") + } + #expect(!FileManager.default.fileExists(atPath: temporaryURL.path)) + #expect(FileManager.default.fileExists(atPath: installedBundle.path)) + } +} + +@Test(arguments: [false, true]) +func installsDarwinSDKInXtoolDirectory(legacy: Bool) async throws { + let homeDirectory = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString) + defer { try? FileManager.default.removeItem(at: homeDirectory) } + + try await withDependencies { + $0.persistentDirectory = homeDirectory.appending(path: "persistent") + } operation: { + let temporaryBundle = try DarwinSDK.prepareTemporaryBundle() + let temporaryURL = temporaryBundle.url + try FileManager.default.createDirectory(at: temporaryURL, withIntermediateDirectories: false) + if !legacy { + try Data("test\n".utf8).write(to: temporaryURL.appendingPathComponent("darwin-sdk-version.txt")) + } + try await temporaryBundle.install() + + let sdk = try #require(try DarwinSDK.current(environment: [:], homeDirectory: homeDirectory)) + #expect(sdk.bundle.path == homeDirectory.appending(path: "persistent/darwin.xtoolsdk").path) + #expect(sdk.version == (legacy ? "develop" : "test")) + #expect(!FileManager.default.fileExists(atPath: temporaryURL.path)) + #expect(!FileManager.default.fileExists(atPath: homeDirectory.appending(path: ".swiftpm").path)) + } +} + +@Test(arguments: [false, true], ["develop", "normal", "slim"]) +func migratesDarwinSDKToXtoolDirectory(usesXDG: Bool, flavor: String) throws { + let homeDirectory = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString) + defer { try? FileManager.default.removeItem(at: homeDirectory) } - try FileManager.default.createDirectory(at: temporaryBundle.url, withIntermediateDirectories: false) - #expect(DarwinSDK(bundle: temporaryBundle.url)?.version == "develop") + try withDependencies { + $0.persistentDirectory = homeDirectory.appending(path: "persistent") + } operation: { + let environment = usesXDG ? ["XDG_CONFIG_HOME": homeDirectory.appending(path: "xdg").path] : [:] + let legacyBundle = try DarwinSDK.swiftPMDirectory(environment: environment, homeDirectory: homeDirectory) + .appending(path: "swift-sdks/darwin.artifactbundle") + try FileManager.default.createDirectory( + at: legacyBundle.appending(path: "Xcode.app/Contents/Developer"), + withIntermediateDirectories: true + ) + if flavor != "develop" { + try Data("test".utf8).write(to: legacyBundle.appendingPathComponent("darwin-sdk-version.txt")) + } + if flavor == "slim" { + try Data().write(to: legacyBundle.appendingPathComponent("slim-marker")) + } + let payload = legacyBundle.appending(path: "toolset/bin") + try FileManager.default.createDirectory(at: payload, withIntermediateDirectories: true) + try Data("tool".utf8).write(to: payload.appendingPathComponent("clang")) + + for _ in 0..<2 { + let sdk = try #require(try DarwinSDK.current(environment: environment, homeDirectory: homeDirectory)) + #expect(sdk.bundle.path == homeDirectory.appending(path: "persistent/darwin.xtoolsdk").path) + #expect(sdk.version == (flavor == "develop" ? "develop" : "test")) + #expect(sdk.flavor == (flavor == "develop" ? .legacy : flavor == "slim" ? .slim : .normal)) + #expect(FileManager.default.fileExists(atPath: sdk.developerDirectory.path)) + #expect(try Data(contentsOf: sdk.bundle.appending(path: "toolset/bin/clang")) == Data("tool".utf8)) + } + #expect(!FileManager.default.fileExists(atPath: legacyBundle.path)) + } +} + +@Test func currentDarwinSDKPrefersXtoolDirectory() throws { + let homeDirectory = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString) + defer { try? FileManager.default.removeItem(at: homeDirectory) } + + try withDependencies { + $0.persistentDirectory = homeDirectory.appending(path: "persistent") + } operation: { + let installedBundle = homeDirectory.appending(path: "persistent/darwin.xtoolsdk") + let legacyBundle = homeDirectory.appending(path: ".swiftpm/swift-sdks/darwin.artifactbundle") + for bundle in [installedBundle, legacyBundle] { + try FileManager.default.createDirectory(at: bundle, withIntermediateDirectories: true) + } + try Data("new".utf8).write(to: installedBundle.appendingPathComponent("darwin-sdk-version.txt")) + + let sdk = try #require(try DarwinSDK.current(environment: [:], homeDirectory: homeDirectory)) + #expect(sdk.version == "new") + #expect(FileManager.default.fileExists(atPath: legacyBundle.path)) } +} + +@Test func currentDarwinSDKReturnsNilWhenNotInstalled() throws { + let homeDirectory = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString) + defer { try? FileManager.default.removeItem(at: homeDirectory) } + + try withDependencies { + $0.persistentDirectory = homeDirectory.appending(path: "persistent") + } operation: { + let sdk = try DarwinSDK.current(environment: [:], homeDirectory: homeDirectory) + #expect(sdk == nil) + #expect(!FileManager.default.fileExists(atPath: homeDirectory.path)) + } +} + +@Test func slimMarkerIdentifiesSDKWithXcodeApp() throws { + let temporary = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString) + defer { try? FileManager.default.removeItem(at: temporary) } + let bundle = temporary.appendingPathComponent("darwin.xtoolsdk") + let developer = bundle.appendingPathComponent("Xcode.app/Contents/Developer", isDirectory: true) + try FileManager.default.createDirectory(at: developer, withIntermediateDirectories: true) + try Data().write(to: bundle.appendingPathComponent("slim-marker")) + try Data("test".utf8).write(to: bundle.appendingPathComponent("darwin-sdk-version.txt")) - #expect(!FileManager.default.fileExists(atPath: temporaryURL.path)) - #expect(FileManager.default.fileExists(atPath: installedBundle.path)) + let sdk = try #require(DarwinSDK(bundle: bundle)) + #expect(sdk.flavor == .slim) + #expect(sdk.developerDirectory == developer) } diff --git a/Tests/XToolTests/DirectBuildDescriptionTests.swift b/Tests/XToolTests/DirectBuildDescriptionTests.swift new file mode 100644 index 00000000..d2061e43 --- /dev/null +++ b/Tests/XToolTests/DirectBuildDescriptionTests.swift @@ -0,0 +1,153 @@ +import CustomDump +import Foundation +@testable import PackLib +import Testing +import XCLIUtils +import XPIF +@testable import XToolSupport + +struct DirectBuildDescriptionTests { + @Test(arguments: [nil, "org.example.widget"] as [String?]) + func packagesSchemaContents(extensionBundleID: String?) async throws { + let root = try temporaryDirectory() + defer { try? FileManager.default.removeItem(at: root) } + try FileManager.default.createDirectory(at: root.appending(path: "Assets"), withIntermediateDirectories: true) + let shared = root.appending(path: "Shared.txt") + try Data().write(to: shared) + try writePlist([ + "CFBundleVersion": "42", "CFBundleShortVersionString": "2.3", + "CFBundleIdentifier": "$(PRODUCT_BUNDLE_IDENTIFIER)", "CFBundleExecutable": "${EXECUTABLE_NAME}", + "CFBundleDevelopmentRegion": "$(DEVELOPMENT_LANGUAGE)", + "CFBundleName": "Custom name", + "CFBundleDisplayName": "My App", "NSCameraUsageDescription": "Take a photo", + "UIDeviceFamily": [2], "UISupportedInterfaceOrientations": ["UIInterfaceOrientationLandscapeLeft"], + ], to: root.appending(path: "App.plist")) + try writePlist(["NSExtension": ["NSExtensionPointIdentifier": "com.apple.widgetkit-extension"]], + to: root.appending(path: "Widget.plist")) + let schema = try PackSchema(validating: .init( + version: .v1, bundleID: "org.example.app", product: "App", infoPath: "App.plist", + iconPath: "Icon.png", resources: ["Assets", "Icon.png", shared.path], + extensions: [.init(product: "Widget", bundleID: extensionBundleID, infoPath: "Widget.plist", + resources: ["Assets", shared.path])] + )) + let description = try await describe(schema: schema, root: root, products: ["App", "Widget"]) + let project = try #require(description.pif.workspace.projects.first) + let app = try #require(project.targets.first { $0.id == "xtool:main-app" }) + let widget = try #require(project.targets.first { $0.id == "xtool:extension:Widget" }) + if case .target(let target) = widget { + expectNoDifference(target.productName, "Widget.appex") + expectNoDifference(target.productType, .appExtension) + } else { + Issue.record("Expected a native extension target") + } + expectNoDifference(app.common.dependencies.map(\.targetId), ["PACKAGE-PRODUCT:App", widget.id]) + expectNoDifference(widget.common.dependencies.map(\.targetId), ["PACKAGE-PRODUCT:Widget"]) + let appSettings = try #require(app.common.buildConfigs.first?.settings) + let widgetSettings = try #require(widget.common.buildConfigs.first?.settings) + expectNoDifference(appSettings[single: "TARGETED_DEVICE_FAMILY[sdk=iphone*]"], "2") + expectNoDifference(widgetSettings[.PRODUCT_BUNDLE_IDENTIFIER], extensionBundleID ?? "org.example.app.Widget") + expectNoDifference(widgetSettings[.APPLICATION_EXTENSION_API_ONLY], "YES") + expectNoDifference(widgetSettings[.SKIP_INSTALL], "YES") + expectNoDifference(widgetSettings[.PRODUCT_NAME], "Widget") + expectNoDifference(appSettings[.GENERATE_INFOPLIST_FILE], "YES") + expectNoDifference(appSettings[.CURRENT_PROJECT_VERSION], "42") + expectNoDifference(appSettings[.MARKETING_VERSION], "2.3") + expectNoDifference(appSettings[.PRODUCT_BUNDLE_IDENTIFIER], "org.example.app") + expectNoDifference(appSettings[single: "EXECUTABLE_NAME"], "App") + expectNoDifference(appSettings[single: "DEVELOPMENT_LANGUAGE"], "en") + #expect(appSettings[multiple: "INFOPLIST_KEY_UISupportedInterfaceOrientations[sdk=iphone*]"] == nil) + let embed = try #require(copyPhases(app).first { $0.destinationSubfolder == .plugins }) + expectNoDifference(embed.files.map(\.ref), [.targetProduct(id: widget.id)]) + let appResources = try #require(copyPhases(app).first { $0.destinationSubfolder == .resources }) + let widgetResources = try #require(copyPhases(widget).first { $0.destinationSubfolder == .resources }) + let references = project.mainGroup.subitems.compactMap { reference -> XPIF.FileReference? in + guard case .file(let file) = reference else { return nil } + return file + } + func paths(_ phase: XPIF.CopyFilesBuildPhase) -> [String] { + phase.files.compactMap { file in + guard case .reference(let id) = file.ref else { return nil } + return references.first { $0.id == id }?.path + } + } + expectNoDifference(paths(appResources), [root.appending(path: "Assets").path, + root.appending(path: "Icon.png").path, shared.path]) + expectNoDifference(paths(widgetResources), [root.appending(path: "Assets").path, shared.path]) + expectNoDifference(references.first { $0.path == root.appending(path: "Assets").path }?.fileType, "folder") + let appPlist = try readPlist(appSettings) + expectNoDifference(appPlist["CFBundleVersion"] as? String, "42") + expectNoDifference(appPlist["CFBundleShortVersionString"] as? String, "2.3") + expectNoDifference(appPlist["CFBundleDisplayName"] as? String, "My App") + expectNoDifference(appPlist["NSCameraUsageDescription"] as? String, "Take a photo") + expectNoDifference(appPlist["CFBundleIconFile"] as? String, "Icon") + expectNoDifference(appPlist["UISupportedInterfaceOrientations"] as? [String], + ["UIInterfaceOrientationLandscapeLeft"]) + let widgetPlist = try readPlist(widgetSettings) + expectNoDifference(widgetPlist["NSExtension"] as? [String: String], + ["NSExtensionPointIdentifier": "com.apple.widgetkit-extension"]) + #expect(widgetPlist["CFBundleExecutable"] == nil) + #expect(widgetPlist["CFBundlePackageType"] == nil) + // Verify the build service receives the phases and references through PIF serialization. + let roundTrip = try XPIF.Container(data: description.pif.data()) + expectNoDifference(roundTrip.workspace.projects.flatMap(\.targets).map(\.common.buildPhases), + description.pif.workspace.projects.flatMap(\.targets).map(\.common.buildPhases)) + } + + @Test func delegatesPlistDefaultsToSwiftBuild() async throws { + let root = try temporaryDirectory() + defer { try? FileManager.default.removeItem(at: root) } + let description = try await describe(schema: .default, root: root, products: ["App"]) + let app = try #require(description.pif.workspace.projects.first?.targets.first { $0.id == "xtool:main-app" }) + let settings = try #require(app.common.buildConfigs.first?.settings) + #expect(settings[.INFOPLIST_FILE] == nil) + expectNoDifference(settings[.GENERATE_INFOPLIST_FILE], "YES") + expectNoDifference(settings[.CURRENT_PROJECT_VERSION], "1") + expectNoDifference(settings[.MARKETING_VERSION], "1.0.0") + expectNoDifference(settings[.PRODUCT_NAME], "App") + #expect(settings[single: "EXECUTABLE_NAME"] == nil) + #expect(settings[single: "INFOPLIST_KEY_CFBundleName"] == nil) + expectNoDifference(settings[multiple: "INFOPLIST_KEY_UISupportedInterfaceOrientations[sdk=iphone*]"], + ["UIInterfaceOrientationPortrait"]) + expectNoDifference(settings[single: "INFOPLIST_KEY_UILaunchScreen_Generation[sdk=iphone*]"], "YES") + expectNoDifference(settings[multiple: "INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad[sdk=iphone*]"], [ + "UIInterfaceOrientationPortrait", "UIInterfaceOrientationPortraitUpsideDown", + "UIInterfaceOrientationLandscapeLeft", "UIInterfaceOrientationLandscapeRight", + ]) + } + + private func describe(schema: PackSchema, root: URL, products: [String]) async throws -> BuildDescription { + let settings = try await BuildSettings(configuration: .debug, triple: "arm64-apple-ios", packagePath: root.path) + let project = XPIF.Project( + id: "package", path: root.appending(path: "Package.swift").path, projectDir: root.path, name: "Package", + targets: products.map { name in + .target(.init(id: .init("PACKAGE-PRODUCT:\(name)"), productType: .packageProduct, + name: "\(name)-product", productName: name)) + } + ) + let pif = XPIF.Container(workspace: .init(id: "workspace", name: "Package", path: root.path, projects: [project])) + return try SwiftPMBuildDescriber(buildSettings: settings, schema: schema).describe(basePIF: pif) + } + + private func copyPhases(_ target: XPIF.BaseTarget) -> [XPIF.CopyFilesBuildPhase] { + target.common.buildPhases.compactMap { phase in + guard case .copyFiles(let phase) = phase else { return nil } + return phase + } + } + + private func temporaryDirectory() throws -> URL { + let directory = FileManager.default.temporaryDirectory.appending(path: UUID().uuidString).resolvingSymlinksInPath() + try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true) + return directory + } + + private func writePlist(_ plist: [String: Any], to url: URL) throws { + try PropertyListSerialization.data(fromPropertyList: plist, format: .xml, options: 0).write(to: url) + } + + private func readPlist(_ settings: XPIF.BuildSettings) throws -> [String: Any] { + let path = try #require(settings[.INFOPLIST_FILE]) + return try #require(PropertyListSerialization.propertyList(from: Data(contentsOf: URL(filePath: path)), + format: nil) as? [String: Any]) + } +} diff --git a/Tests/XToolTests/PackerTests.swift b/Tests/XToolTests/PackerTests.swift deleted file mode 100644 index 5aabda83..00000000 --- a/Tests/XToolTests/PackerTests.swift +++ /dev/null @@ -1,15 +0,0 @@ -import Testing -@testable import PackLib - -@Test func cfBundleSupportedPlatformsFollowsBuildTriple() { - #expect(Packer.cfBundleSupportedPlatforms(for: "arm64-apple-ios") == ["iPhoneOS"]) - #expect(Packer.cfBundleSupportedPlatforms(for: "arm64-apple-ios-simulator") == ["iPhoneSimulator"]) - - #expect(Packer.cfBundleSupportedPlatforms(for: "arm64-apple-ios17.0") == ["iPhoneOS"]) - #expect(Packer.cfBundleSupportedPlatforms(for: "arm64-apple-ios17.0-simulator") == ["iPhoneSimulator"]) - #expect(Packer.cfBundleSupportedPlatforms(for: "x86_64-apple-ios-simulator") == ["iPhoneSimulator"]) - #expect(Packer.cfBundleSupportedPlatforms(for: "arm64-apple-macosx") == ["MacOSX"]) - #expect(Packer.cfBundleSupportedPlatforms(for: "x86_64-apple-macosx") == ["MacOSX"]) - - #expect(Packer.cfBundleSupportedPlatforms(for: "unknown-triple") == ["iPhoneOS"]) -} diff --git a/Tests/XToolTests/SwiftPMBuildDescriberTests.swift b/Tests/XToolTests/SwiftPMBuildDescriberTests.swift new file mode 100644 index 00000000..f3942756 --- /dev/null +++ b/Tests/XToolTests/SwiftPMBuildDescriberTests.swift @@ -0,0 +1,257 @@ +import XCLIUtils +import CustomDump +import Dependencies +import Foundation +@testable import PackLib +import Testing +import XcodeProjects +import XPIF +import XUtils +@testable import XToolSupport + +struct SwiftPMBuildDescriberTests { + @Test(arguments: [nil, "org.example.widget"] as [String?]) + func generatesAppAndExtensionWithSchemaSettings(extensionBundleID: String?) async throws { + let root = try temporaryDirectory() + defer { try? FileManager.default.removeItem(at: root) } + try FileManager.default.createDirectory(at: root.appending(path: "Assets"), withIntermediateDirectories: true) + let absoluteResource = root.appending(path: "Shared.txt").path + try Data().write(to: URL(filePath: absoluteResource)) + try writePlist(["UIDeviceFamily": [2], "CFBundleVersion": "42"], to: root.appending(path: "App.plist")) + try writePlist(["NSExtension": ["NSExtensionPointIdentifier": "com.apple.widgetkit-extension"]], + to: root.appending(path: "Widget.plist")) + let schema = try PackSchema(validating: .init( + version: .v1, + bundleID: "org.example.custom", + product: "App", + infoPath: "App.plist", + entitlementsPath: "App.entitlements", + iconPath: "Icon.png", + resources: ["Assets", "Icon.png", absoluteResource], + extensions: [ + .init(product: "Widget", bundleID: extensionBundleID, infoPath: "Widget.plist", resources: ["Assets"], + entitlementsPath: root.appending(path: "Widget.entitlements").path), + ], + )) + let describer = try await describer(schema: schema, root: root) + let result = try describer.writeXcodeProject(basePIF: pif(root: root, products: ["App", "Widget"])) + #expect(result.bundleID == "org.example.custom") + #expect(result.url == root.appending(path: "xtool/App.xcworkspace")) + let workspace = try XcodeWorkspace(url: result.url) + #expect(workspace.children == [ + .file(.init(base: .container, path: "..")), + .file(.init(base: .group, path: ".xtool-tmp/App.xcodeproj")), + ]) + let projectDir = root.appending(path: "xtool/.xtool-tmp") + let project = try XcodeProject(url: projectDir.appending(path: "App.xcodeproj")).project + let targets = project.targets.compactMap { target -> XCSchema.CommonTargetProperties? in + guard case .native(let properties) = target else { return nil } + return properties + } + #expect(targets.map(\.name) == ["App-App", "Widget-Extension"]) + let app = try #require(targets.first) + let widget = try #require(targets.last) + #expect(app.buildSettings["PRODUCT_BUNDLE_IDENTIFIER"] == .string("org.example.custom")) + #expect(widget.buildSettings["PRODUCT_BUNDLE_IDENTIFIER"] + == .string(extensionBundleID ?? "org.example.custom.Widget")) + #expect(app.buildSettings["TARGETED_DEVICE_FAMILY[sdk=iphone*]"] == .string("2")) + #expect(project.buildSettings["IPHONEOS_DEPLOYMENT_TARGET[config=Debug]"] == .string("16.0")) + #expect(app.buildSettings["CODE_SIGN_ENTITLEMENTS"] == .string("../../App.entitlements")) + #expect(widget.buildSettings["CODE_SIGN_ENTITLEMENTS"] == .string(root.appending(path: "Widget.entitlements").path)) + #expect(widget.buildSettings["APPLICATION_EXTENSION_API_ONLY"] == .string("YES")) + #expect(app.dependencies == [.localTarget(.init(targetName: "Widget-Extension"), [])]) + #expect(app.packageProductTargetMembers.map { $0.packageProduct.productName } == ["App"]) + #expect(widget.packageProductTargetMembers.map { $0.packageProduct.productName } == ["Widget"]) + let appPlist = try readPlist(projectDir.appending(path: "App/Info.plist")) + #expect(appPlist["CFBundleVersion"] as? String == "42") + #expect(appPlist["CFBundleExecutable"] == nil) + expectNoDifference(app.buildSettings["PRODUCT_NAME"], .string("App")) + expectNoDifference(widget.buildSettings["PRODUCT_NAME"], .string("Widget")) + expectNoDifference(app.product, .namePath(.init(components: [.child("Products"), .child("App.app")]))) + expectNoDifference(widget.product, .namePath(.init(components: [.child("Products"), .child("Widget.appex")]))) + #expect(appPlist["CFBundleIconFile"] as? String == "Icon") + #expect(appPlist["UIDeviceFamily"] == nil) + let widgetPlist = try readPlist(projectDir.appending(path: "Widget/Info.plist")) + #expect((widgetPlist["NSExtension"] as? [String: String])?["NSExtensionPointIdentifier"] + == "com.apple.widgetkit-extension") + let projectText = try String(contentsOf: projectDir.appending(path: "App.xcodeproj/project.pbxproj"), encoding: .utf8) + #expect(projectText.contains("Embed App Extensions")) + #expect(projectText.contains("Copy Root Resources")) + #expect(projectText.contains("../../Assets")) + #expect(projectText.contains(absoluteResource)) + // The icon is also listed as a resource; the copy phase should contain it only once. + let appGroup = try #require(project.topLevelReferences.first) + guard case .group(let group) = appGroup else { + Issue.record("Expected the app's group") + return + } + #expect(group.children.count == 5) + } + + @Test func dumpsPIFAndGeneratesProjectForSwiftPackage() async throws { + let root = try temporaryDirectory() + defer { try? FileManager.default.removeItem(at: root) } + try Data(""" + // swift-tools-version:6.0 + import PackageDescription + let package = Package( + name: "Sample", platforms: [.iOS(.v15)], + products: [.library(name: "Sample", targets: ["Sample"]), + .library(name: "DynamicHelper", type: .dynamic, targets: ["Sample"]), + .executable(name: "Tool", targets: ["Tool"])], + targets: [.target(name: "Sample"), .executableTarget(name: "Tool")] + ) + """.utf8).write(to: root.appending(path: "Package.swift")) + try FileManager.default.createDirectory(at: root.appending(path: "Sources/Sample"), withIntermediateDirectories: true) + try Data("public struct Sample {}".utf8).write(to: root.appending(path: "Sources/Sample/Sample.swift")) + try FileManager.default.createDirectory(at: root.appending(path: "Sources/Tool"), withIntermediateDirectories: true) + try Data("print(42)".utf8).write(to: root.appending(path: "Sources/Tool/main.swift")) + let result = try await withDependencies { + $0.out = .liveValue + } operation: { + let describer = try await describer(schema: .default, root: root) + let description = try await describer.describe() + let app = try #require(description.pif.workspace.projects.flatMap(\.targets).first { $0.id == "xtool:main-app" }) + expectNoDifference(app.common.name, "Sample-App") + return try await describer.writeXcodeProject() + } + #expect(result.bundleID == "com.example.Sample") + let project = try XcodeProject(url: root.appending(path: "xtool/.xtool-tmp/Sample.xcodeproj")) + #expect(project.project.targets.count == 1) + let plist = try readPlist(root.appending(path: "xtool/.xtool-tmp/Sample/Info.plist")) + #expect(plist["MinimumOSVersion"] == nil) + #expect(plist["UILaunchScreen"] == nil) + #expect(project.project.buildSettings["IPHONEOS_DEPLOYMENT_TARGET"] == .string("15.0")) + } + + @Test(arguments: [nil, "App"] as [String?]) + func selectsPrimaryProductAndPreservesDynamicVariant(expected: String?) async throws { + let root = try temporaryDirectory() + defer { try? FileManager.default.removeItem(at: root) } + let schema = try PackSchema(validating: .init(version: .v1, orgID: "com.example", product: expected)) + let describer = try await describer(schema: schema, root: root) + var pif = pif(root: root, products: []) + var product = XPIF.Target(id: "PACKAGE-PRODUCT:App", productType: .packageProduct, + name: "App-product", productName: "App") + let variant = XPIF.Target(id: "PACKAGE-PRODUCT:Variant", productType: .framework, + name: "App-product", productName: "AppDynamic") + product.dynamicTargetVariantId = variant.id + // Put the variant first so explicit product selection cannot accidentally pick it. + pif.workspace.projects[0].targets = [.target(variant), .target(product)] + let description = try describer.describe(basePIF: pif) + let targets = description.pif.workspace.projects[0].targets + expectNoDifference(Array(targets.prefix(2)), pif.workspace.projects[0].targets) + let app = try #require(targets.first { $0.id == "xtool:main-app" }) + expectNoDifference(app.common.dependencies.map(\.targetId), [product.id]) + let result = try describer.writeXcodeProject(basePIF: pif) + expectNoDifference(result.bundleID, "com.example.App") + let project = try XcodeProject(url: root.appending(path: "xtool/.xtool-tmp/App.xcodeproj")) + expectNoDifference(project.project.targets.map(\.name), ["App-App"]) + + // Separately declared dynamic libraries remain in the graph as dependencies, but + // must not make app selection ambiguous or be selectable as apps themselves. + let other = XPIF.Target(id: "PACKAGE-PRODUCT:Other", productType: .framework, + name: "Other-product", productName: "Other") + let dylib = XPIF.Target(id: "PACKAGE-PRODUCT:Dylib", productType: .dynamicLibrary, + name: "Dylib-product", productName: "Dylib") + pif.workspace.projects[0].targets.append(contentsOf: [.target(other), .target(dylib)]) + let automatic = try await self.describer(schema: .default, root: root) + let descriptionWithDependencies = try automatic.describe(basePIF: pif) + expectNoDifference(Array(descriptionWithDependencies.pif.workspace.projects[0].targets.prefix(4)), + pif.workspace.projects[0].targets) + expectNoDifference(try automatic.writeXcodeProject(basePIF: pif).bundleID, "com.example.App") + for name in ["Other", "Dylib"] { + let schema = try PackSchema(validating: .init(version: .v1, orgID: "com.example", product: name)) + let explicit = try await self.describer(schema: schema, root: root) + #expect(throws: Console.Error.self) { try explicit.describe(basePIF: pif) } + #expect(throws: Console.Error.self) { try explicit.writeXcodeProject(basePIF: pif) } + } + pif.workspace.projects[0].targets = [.target(other), .target(dylib)] + #expect(throws: Console.Error.self) { try automatic.describe(basePIF: pif) } + #expect(throws: Console.Error.self) { try automatic.writeXcodeProject(basePIF: pif) } + } + + @Test(arguments: [0, 2]) + func rejectsMissingOrAmbiguousProducts(count: Int) async throws { + let root = try temporaryDirectory() + defer { try? FileManager.default.removeItem(at: root) } + let describer = try await describer(schema: .default, root: root) + let pif = pif(root: root, products: (0.. URL { + let directory = FileManager.default.temporaryDirectory.appending(path: UUID().uuidString) + .resolvingSymlinksInPath() + try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true) + return directory + } + + private func describer(schema: PackSchema, root: URL) async throws -> SwiftPMBuildDescriber { + let settings = try await BuildSettings( + configuration: .debug, triple: "arm64-apple-ios", packagePath: root.path + ) + return try SwiftPMBuildDescriber(buildSettings: settings, schema: schema) + } + + private func pif(root: URL, products: [String]) -> XPIF.Container { + var settings = XPIF.BuildSettings() + settings[.IPHONEOS_DEPLOYMENT_TARGET] = "16.0" + let project = XPIF.Project( + id: "package", path: root.appending(path: "Package.swift").path, projectDir: root.path, name: "Package", + buildConfigs: [.init(id: "debug", name: "Debug", settings: settings)], + targets: products.map { name in + .target(.init(id: .init("PACKAGE-PRODUCT:\(name)"), productType: .packageProduct, + name: "\(name)-product", productName: name)) + } + ) + return XPIF.Container(workspace: .init(id: "workspace", name: "Package", path: root.path, projects: [project])) + } + + private func writePlist(_ plist: [String: Any], to url: URL) throws { + try PropertyListSerialization.data(fromPropertyList: plist, format: .xml, options: 0).write(to: url) + } + + private func readPlist(_ url: URL) throws -> [String: Any] { + try #require(PropertyListSerialization.propertyList(from: Data(contentsOf: url), format: nil) as? [String: Any]) + } +} From e9d881c976b8871040f31eae3743f224671f28cf Mon Sep 17 00:00:00 2001 From: Kabir Oberai Date: Fri, 23 Oct 2026 01:05:36 -0400 Subject: [PATCH 03/15] swb: add signing support --- Package.swift | 4 + Sources/XKit/Signer/RCodesignSigner.swift | 42 +- Sources/XToolSupport/BuildCoordinator.swift | 9 + .../XToolSupport/BuildSessionManager.swift | 37 +- .../XToolSupport/EmulatedTools+Codesign.swift | 102 +++++ Sources/XToolSupport/EmulatedTools+Derq.swift | 93 +++++ Sources/XToolSupport/EmulatedTools+Live.swift | 14 +- Tests/XToolTests/CodesignToolTests.swift | 380 ++++++++++++++++++ Tests/XToolTests/DerqToolTests.swift | 56 +++ 9 files changed, 716 insertions(+), 21 deletions(-) create mode 100644 Sources/XToolSupport/EmulatedTools+Codesign.swift create mode 100644 Sources/XToolSupport/EmulatedTools+Derq.swift create mode 100644 Tests/XToolTests/CodesignToolTests.swift create mode 100644 Tests/XToolTests/DerqToolTests.swift diff --git a/Package.swift b/Package.swift index 5662a22e..9753ca82 100644 --- a/Package.swift +++ b/Package.swift @@ -59,6 +59,7 @@ let package = Package( .package(url: "https://github.com/apple/swift-system", from: "1.4.0"), .package(url: "https://github.com/apple/swift-http-types", from: "1.3.1"), .package(url: "https://github.com/apple/swift-certificates", from: "1.0.0"), + .package(url: "https://github.com/apple/swift-asn1.git", from: "1.7.3"), .package(url: "https://github.com/apple/swift-crypto", from: "5.0.0"), .package(url: "https://github.com/apple/swift-argument-parser", from: "1.5.0"), .package(url: "https://github.com/apple/swift-openapi-generator", from: "1.6.0"), @@ -184,6 +185,7 @@ let package = Package( "PackLib", .product(name: "Crypto", package: "swift-crypto"), .product(name: "X509", package: "swift-certificates"), + .product(name: "DependenciesTestSupport", package: "swift-dependencies"), .product(name: "CustomDump", package: "swift-custom-dump"), .product(name: "SwiftBuild", package: "swift-build"), ], @@ -215,6 +217,8 @@ let package = Package( .product(name: "libunxip", package: "unxip"), .product(name: "SWBBuildService", package: "swift-build"), .product(name: "SwiftBuild", package: "swift-build"), + .product(name: "SWBUtil", package: "swift-build"), + .product(name: "SwiftASN1", package: "swift-asn1"), ], cSettings: cSettings, swiftSettings: swiftSettings, diff --git a/Sources/XKit/Signer/RCodesignSigner.swift b/Sources/XKit/Signer/RCodesignSigner.swift index 3e08f7eb..225f3e35 100644 --- a/Sources/XKit/Signer/RCodesignSigner.swift +++ b/Sources/XKit/Signer/RCodesignSigner.swift @@ -3,17 +3,16 @@ import RCodesign public actor RCodesignSigner: Signer { - public enum Error: LocalizedError { + public enum Error: CustomStringConvertible, LocalizedError { case badFilePath - public var errorDescription: String? { + public var description: String { switch self { - case .badFilePath: - return NSLocalizedString( - "signer_impl.error.bad_file_path", value: "A bad file path was provided", comment: "" - ) + case .badFilePath: "A bad file path was provided" } } + + public var errorDescription: String? { description } } public init() {} @@ -47,7 +46,36 @@ public actor RCodesignSigner: Signer { case .adhoc: signingIdentity = .adHoc } - try RCodesign.sign(at: app, identity: signingIdentity, options: .init(entitlements: entitlements, shallow: !deep)) + + var originalPermissions: [(String, Int)] = [] + try updatePermissions(dir: app) { path, permissions in + originalPermissions.append((path, permissions)) + permissions = 0o644 + } + let signResult = Result { + try RCodesign.sign(at: app, identity: signingIdentity, options: .init(entitlements: entitlements, shallow: !deep)) + } + for (path, permissions) in originalPermissions { + try FileManager.default.setAttributes([.posixPermissions: permissions], ofItemAtPath: path) + } + return try signResult.get() + } + + private func updatePermissions(dir: URL, transform: (String, inout Int) throws -> Void) throws { + guard let enumerator = FileManager.default.enumerator( + at: dir, + includingPropertiesForKeys: [.isDirectoryKey] + ) else { throw Error.badFilePath } + for case let file as URL in enumerator { + guard try file.resourceValues(forKeys: [.isDirectoryKey]).isDirectory == false else { continue } + let path = file.path + let attributes = try FileManager.default.attributesOfItem(atPath: path) + guard let permissions = attributes[.posixPermissions] as? Int else { continue } + var newValue = permissions + try transform(path, &newValue) + guard newValue != permissions else { continue } + try FileManager.default.setAttributes([.posixPermissions: newValue], ofItemAtPath: path) + } } public func analyze(executable: URL) throws -> Data? { diff --git a/Sources/XToolSupport/BuildCoordinator.swift b/Sources/XToolSupport/BuildCoordinator.swift index 6f336085..2b70d8d5 100644 --- a/Sources/XToolSupport/BuildCoordinator.swift +++ b/Sources/XToolSupport/BuildCoordinator.swift @@ -48,6 +48,15 @@ public actor BuildCoordinator { // mode, the Xcode targets will use [arm64, x86_64] but the package targets will // only declare [arm64], causing the build to fail overrides["ONLY_ACTIVE_ARCH"] = "YES" + // force ad-hoc signing during the build process. we'll provision & re-sign + // with the proper identity later. + #if os(macOS) + overrides["CODE_SIGN_IDENTITY"] = "-" + #else + // on Linux, host targets (eg macros) should not receive Darwin codesigning flags + overrides["CODE_SIGN_IDENTITY[__destination_platform=YES]"] = "-" + #endif + overrides["AD_HOC_CODE_SIGNING_ALLOWED"] = "YES" // Only override deployment targets when the triple explicitly includes a version. // Otherwise each package retains the deployment targets from its manifest. if let setting = targetInfo.deploymentTargetSettingName, let version = targetInfo.deploymentTarget { diff --git a/Sources/XToolSupport/BuildSessionManager.swift b/Sources/XToolSupport/BuildSessionManager.swift index 5ed89497..bb74dcea 100644 --- a/Sources/XToolSupport/BuildSessionManager.swift +++ b/Sources/XToolSupport/BuildSessionManager.swift @@ -125,7 +125,42 @@ private final class PlanningOperationDelegate: SWBPlanningOperationDelegate, Sen targetGUID: String, provisioningSourceData: SWBProvisioningTaskInputsSourceData ) async -> SWBProvisioningTaskInputs { - SWBProvisioningTaskInputs() + // swiftlint:disable:next line_length + // based on https://github.com/swiftlang/swift-build/blob/02d98b1/Sources/SwiftBuild/ConsoleCommands/SWBServiceConsoleBuildCommand.swift#L491 + guard !provisioningSourceData.signingCertificateIdentifier.isEmpty else { + return SWBProvisioningTaskInputs() + } + let entitlementsDestination = provisioningSourceData.entitlementsDestination + let signedEntitlements = entitlementsDestination == "Signature" + ? provisioningSourceData.productTypeEntitlements + .merging( + provisioningSourceData.projectEntitlements ?? [:], + uniquingKeysWith: { _, new in new } + ) + : [:] + let simulatedEntitlements = entitlementsDestination == "__entitlements" + ? provisioningSourceData.productTypeEntitlements + .merging( + ["application-identifier": .plString(provisioningSourceData.bundleIdentifier)], + uniquingKeysWith: { _, new in new } + ) + .merging( + provisioningSourceData.projectEntitlements ?? [:], + uniquingKeysWith: { _, new in new } + ) + : [:] + return SWBProvisioningTaskInputs( + identityHash: "-", + identityName: "-", + signedEntitlements: signedEntitlements + .merging( + provisioningSourceData.sdkRoot.contains("simulator") + ? ["com.apple.security.get-task-allow": .plBool(true)] + : [:], + uniquingKeysWith: { _, new in new } + ), + simulatedEntitlements: simulatedEntitlements, + ) } func executeExternalTool( diff --git a/Sources/XToolSupport/EmulatedTools+Codesign.swift b/Sources/XToolSupport/EmulatedTools+Codesign.swift new file mode 100644 index 00000000..f35849c1 --- /dev/null +++ b/Sources/XToolSupport/EmulatedTools+Codesign.swift @@ -0,0 +1,102 @@ +import ArgumentParser +import Dependencies +import Foundation +import XCLIUtils +import XKit + +struct CodesignTool: EmulatedTool { + @Dependency(\.signer) private var signer + + var name: String { "codesign" } + + func execute( + commandLine: [String], + workingDirectory: String?, + environment: [String: String] + ) async throws -> EmulatedToolResult { + do { + try await execute(commandLine: commandLine, workingDirectory: workingDirectory) + return .result(status: .exit(0), stdout: Data(), stderr: Data()) + } catch { + return .result(status: .exit(1), stdout: Data(), stderr: Data("codesign: \(error)\n".utf8)) + } + } + + private func execute(commandLine: [String], workingDirectory: String?) async throws { + // Unsupported operations, including `-r- --display`, must fail. SwiftBuild + // probes runtime dylib signatures before/after signing and restores the old + // file when the outputs match; successful empty output would undo signing. + let options = try Options.parse(Array(commandLine.dropFirst())) + guard options.requirements == nil else { + throw Console.Error("Code requirement operations (-r/--requirements) are not supported yet") + } + guard !options.display, options.force, options.sign == "-", options.timestamp == nil || options.timestamp == "none" else { + throw Console.Error(""" + Codesign emulator received unsupported arguments: + \(commandLine.joined(separator: " ")) + Please file a bug report at https://github.com/xtool-org/xtool/issues/new/choose. + """) + } + let preservedMetadata = try options.preservedMetadata() + let directory = URL( + fileURLWithPath: workingDirectory ?? FileManager.default.currentDirectoryPath, + isDirectory: true, + ) + let path = URL(fileURLWithPath: options.path, relativeTo: directory).standardizedFileURL + let entitlements: Data? + if let entitlementsPath = options.entitlements { + let data = try Data(contentsOf: URL(fileURLWithPath: entitlementsPath, relativeTo: directory)) + // rcodesign accepts some ill-formed plists; we should validate first + _ = try PropertyListSerialization.propertyList(from: data, format: nil) + entitlements = data + } else if !preservedMetadata.contains(.entitlements) { + entitlements = try PropertyListSerialization.data(fromPropertyList: [:], format: .xml, options: 0) + } else { + entitlements = nil + } + // The signature's XML entitlement slot must contain XML even when the + // supplied plist is binary. RCodesign handles this conversion and derives + // matching DER, including when importing an older XML-only signature. + try await signer.sign( + app: path, + identity: .adhoc, + entitlementMapping: entitlements.map { [path: $0] } ?? [:], + deep: false, + progress: { _ in } + ) + } + + private struct Options: ParsableArguments { + @Flag var force = false + @Option var sign: String? + @Option(name: [.customShort("r", allowingJoined: true), .long]) var requirements: String? + @Flag(name: [.customShort("d"), .long]) var display = false + @Option var entitlements: String? + @Option var preserveMetadata: String? + @Option var timestamp: String? + // Ad-hoc signing never looks up a keychain identity. + @Option var keychain: String? + @Flag var verbose = false + @Flag var generateEntitlementDer = false + @Argument var path: String + + enum PreservedMetadata: String { + case identifier + case entitlements + case flags + } + + func preservedMetadata() throws -> Set { + guard let preserveMetadata else { return [] } + let mapped = try preserveMetadata + .split(separator: ",", omittingEmptySubsequences: false) + .map { field in + guard let value = PreservedMetadata(rawValue: String(field)) else { + throw Console.Error("Unsupported preserved metadata: \(field)") + } + return value + } + return Set(mapped) + } + } +} diff --git a/Sources/XToolSupport/EmulatedTools+Derq.swift b/Sources/XToolSupport/EmulatedTools+Derq.swift new file mode 100644 index 00000000..f1212e64 --- /dev/null +++ b/Sources/XToolSupport/EmulatedTools+Derq.swift @@ -0,0 +1,93 @@ +import ArgumentParser +import Foundation +import SWBUtil +import SwiftASN1 +import XCLIUtils + +struct DerqTool: EmulatedTool { + var name: String { "derq" } + + func execute( + commandLine: [String], + workingDirectory: String?, + environment: [String: String] + ) async throws -> EmulatedToolResult { + do { + try await execute(commandLine: commandLine, workingDirectory: workingDirectory) + return .result(status: .exit(0), stdout: Data(), stderr: Data()) + } catch { + return .result(status: .exit(1), stdout: Data(), stderr: Data("derq: \(error)\n".utf8)) + } + } + + private func execute( + commandLine: [String], + workingDirectory: String?, + ) async throws { + guard commandLine.count >= 2, commandLine[1] == "query" else { + throw Console.Error("Only `derq query` is supported") + } + let options = try Query.parse(Array(commandLine.dropFirst(2))) + guard options.format == "xml", options.raw else { + throw Console.Error("Only `derq query -f xml -i -o --raw` is supported") + } + let directory = URL( + fileURLWithPath: workingDirectory ?? FileManager.default.currentDirectoryPath, + isDirectory: true, + ) + let input = URL(fileURLWithPath: options.input, relativeTo: directory) + let output = URL(fileURLWithPath: options.output, relativeTo: directory) + let encoded = try DerqEncoder().encodeEntitlements(Data(contentsOf: input)) + try encoded.write(to: output) + } + + private struct Query: ParsableArguments { + @Option(name: .customShort("f")) var format: String + @Option(name: .customShort("i")) var input: String + @Option(name: .customShort("o")) var output: String + @Flag var raw = false + } +} + +struct DerqEncoder { + private static let plistDecoder = PropertyListDecoder() + + func encodeEntitlements(_ data: Data) throws -> Data { + let dictionary = try Self.plistDecoder.decode([String: PropertyListItem].self, from: data) + var serializer = DER.Serializer() + try serializer.appendConstructedNode(identifier: .init(tagWithNumber: 16, tagClass: .application)) { + try $0.serialize(Int64(1)) + try serialize(.plDict(dictionary), into: &$0) + } + return Data(serializer.serializedBytes) + } + + private func serialize(_ value: PropertyListItem, into serializer: inout DER.Serializer) throws { + switch value { + case .plBool(let boolean): + try serializer.serialize(boolean) + case .plInt(let integer): + try serializer.serialize(integer) + case .plString(let string): + try serializer.serialize(ASN1UTF8String(string)) + case .plArray(let array): + try serializer.appendConstructedNode(identifier: .sequence) { serializer in + for element in array { + try serialize(element, into: &serializer) + } + } + case .plDict(let dictionary): + let identifier = ASN1Identifier(tagWithNumber: 16, tagClass: .contextSpecific) + try serializer.appendConstructedNode(identifier: identifier) { serializer in + for (key, value) in dictionary.sorted(by: { $0.key.utf8.lexicographicallyPrecedes($1.key.utf8) }) { + try serializer.appendConstructedNode(identifier: .sequence) { serializer in + try serializer.serialize(ASN1UTF8String(key)) + try serialize(value, into: &serializer) + } + } + } + case .plData, .plDate, .plDouble, .plOpaque: + throw Console.Error("Unsupported DER entitlement value: \(value.typeDisplayString)") + } + } +} diff --git a/Sources/XToolSupport/EmulatedTools+Live.swift b/Sources/XToolSupport/EmulatedTools+Live.swift index b0f1f1de..c5341a68 100644 --- a/Sources/XToolSupport/EmulatedTools+Live.swift +++ b/Sources/XToolSupport/EmulatedTools+Live.swift @@ -16,6 +16,7 @@ extension EmulatedTools: DependencyKey { ObjcopyTool(mode: .bitcodeStrip), ChmodTool(), CodesignTool(), + DerqTool(), ] #endif return tools @@ -109,19 +110,6 @@ private struct ChmodTool: EmulatedTool { } } -private struct CodesignTool: EmulatedTool { - var name: String { "codesign" } - - func execute( - commandLine: [String], - workingDirectory: String?, - environment: [String: String] - ) async throws -> EmulatedToolResult { - // TODO: implement - .result(status: .exit(0), stdout: Data(), stderr: Data()) - } -} - extension Environment { init(_ dictionary: [String: String]) { self = .custom([]).updating( diff --git a/Tests/XToolTests/CodesignToolTests.swift b/Tests/XToolTests/CodesignToolTests.swift new file mode 100644 index 00000000..80d7d828 --- /dev/null +++ b/Tests/XToolTests/CodesignToolTests.swift @@ -0,0 +1,380 @@ +import CustomDump +import ConcurrencyExtras +import Dependencies +import DependenciesTestSupport +import Foundation +import Testing +import XKit +import X509 +@testable import XToolSupport + +@Suite(.dependency(\.signer, DependencyValues.live.signer)) +struct CodesignToolTests { + @Dependency(\.signer) private var signer + + @Test func signerUsesCertificateIdentity() async throws { + let root = try temporaryDirectory() + defer { try? FileManager.default.removeItem(at: root) } + let path = root.appending(path: "Executable") + try machO().write(to: path) + // Use the same RSA key representation as provisioning, with a temporary + // self-signed certificate so this test needs no developer account. + let key = try Keypair().privateKey() + let privateKey = try X509.Certificate.PrivateKey(pemEncoded: String(decoding: key.data, as: UTF8.self)) + let name = try DistinguishedName { CommonName("xtool signing tests") } + let now = Date() + let certificate = try XKit.Certificate(raw: X509.Certificate( + version: .v3, + serialNumber: .init(bytes: [1]), + publicKey: privateKey.publicKey, + notValidBefore: now, + notValidAfter: now + 3600, + issuer: name, + subject: name, + signatureAlgorithm: .sha256WithRSAEncryption, + extensions: .init(), + issuerPrivateKey: privateKey + )) + let progress = LockIsolated<[Double?]>([]) + try await signer.sign(app: path, identity: .real(certificate, key), entitlementMapping: [:], deep: true) { value in + progress.withValue { $0.append(value) } + } + expectNoDifference(progress.value, [nil]) + let slots = try signatureSlots(at: path) + let directory = try #require(slots[0]) + #expect(read32(directory, 12) & 2 == 0) // Not CS_ADHOC. + let cms = try #require(slots[0x10000]) + #expect(cms.count > 8) // The CMS wrapper must contain a signature. + } + + @Test func signerUsesEachBundlesProvisionedEntitlements() async throws { + let root = try temporaryDirectory() + defer { try? FileManager.default.removeItem(at: root) } + let app = root.appending(path: "App.app") + let child = app.appending(path: "PlugIns/Child.appex") + try FileManager.default.createDirectory(at: child, withIntermediateDirectories: true) + var mapping: [URL: Data] = [:] + for bundle in [app, child] { + try machO().write(to: bundle.appending(path: "Executable")) + try PropertyListSerialization.data(fromPropertyList: [ + "CFBundleIdentifier": "org.example.\(bundle.deletingPathExtension().lastPathComponent)", + "CFBundleExecutable": "Executable", "CFBundleVersion": "1", + ], format: .xml, options: 0).write(to: bundle.appending(path: "Info.plist")) + let xml = try PropertyListSerialization.data( + fromPropertyList: ["example": bundle.lastPathComponent], format: .xml, options: 0 + ) + mapping[bundle] = xml + } + try await signer.sign(app: app, identity: .adhoc, entitlementMapping: mapping, deep: true, progress: { _ in }) + for bundle in [app, child] { + let data = try await signer.analyze(executable: bundle.appending(path: "Executable")) + let xml = try #require(data) + let value = try PropertyListSerialization.propertyList(from: xml, format: nil) as? [String: String] + expectNoDifference(value, ["example": bundle.lastPathComponent]) + } + } + + @Test func signerRejectsEntitlementsOutsideTheApp() async throws { + let root = try temporaryDirectory() + defer { try? FileManager.default.removeItem(at: root) } + let app = root.appending(path: "App.app") + let sibling = root.appending(path: "App.app-copy") + // Keep the signing input valid so any error must come from the invalid + // entitlement scope, rather than a missing executable or bundle metadata. + try FileManager.default.createDirectory(at: app, withIntermediateDirectories: true) + let executable = app.appending(path: "Executable") + let original = machO() + try original.write(to: executable) + try PropertyListSerialization.data(fromPropertyList: [ + "CFBundleIdentifier": "org.example.App", + "CFBundleExecutable": "Executable", "CFBundleVersion": "1", + ], format: .xml, options: 0).write(to: app.appending(path: "Info.plist")) + let entitlements = try PropertyListSerialization.data(fromPropertyList: [:], format: .xml, options: 0) + await #expect(throws: (any Error).self) { + try await signer.sign( + app: app, identity: .adhoc, entitlementMapping: [sibling: entitlements], deep: true, progress: { _ in } + ) + } + expectNoDifference(try Data(contentsOf: executable), original) + } + + @Test func signsCodeAndGeneratesDERForExecutables() async throws { + let root = try temporaryDirectory() + defer { try? FileManager.default.removeItem(at: root) } + let entitlements = try PropertyListSerialization.data( + fromPropertyList: ["get-task-allow": true, "example": "<&>\t\n🍎 <"], format: .binary, options: 0 + ) + try entitlements.write(to: root.appending(path: "entitlements.plist")) + for (name, type): (String, UInt32) in [("Executable", 2), ("Library", 6)] { + let path = root.appending(path: name) + try machO(fileType: type).write(to: path) + try await sign(["--entitlements", "entitlements.plist", "--generate-entitlement-der", name], in: root) + let slots = try signatureSlots(at: path) + let directory = try #require(slots[0]) + expectNoDifference(read32(directory, 12), 2) // CS_ADHOC + let xml = Data(try #require(slots[5]).dropFirst(8)) + #expect(xml.starts(with: Data("" + + "a&b<>&"' &lt;" + + " 🍎🙂\t\r\n").utf8) + try await signer.sign(app: path, identity: .adhoc, entitlementMapping: [path: xml], deep: false, progress: { _ in }) + var slots = try signatureSlots(at: path) + let expected = try DerqEncoder().encodeEntitlements(xml) + expectNoDifference(Data(try #require(slots[7]).dropFirst(8)), expected) + + // Model an older signature with only the XML slot. Re-signing must add + // matching DER without changing the existing XML entitlement bytes. + slots.removeValue(forKey: 7) + var signature = Data() + func append32(_ value: UInt32) { withUnsafeBytes(of: value.bigEndian) { signature.append(contentsOf: $0) } } + let headerSize = 12 + slots.count * 8 + append32(0xfade0cc0) + append32(UInt32(headerSize + slots.values.reduce(0) { $0 + $1.count })) + append32(UInt32(slots.count)) + var offset = headerSize + let ordered = slots.sorted { $0.key < $1.key } + for (type, slot) in ordered { + append32(type) + append32(UInt32(offset)) + offset += slot.count + } + for (_, slot) in ordered { signature.append(slot) } + var contents = try Data(contentsOf: path) + contents.replaceSubrange(4096..<(4096 + signature.count), with: signature) + try contents.write(to: path) + try await sign(["--preserve-metadata=identifier,entitlements,flags", "Executable"], in: root) + let preserved = try signatureSlots(at: path) + expectNoDifference(preserved[5], slots[5]) + expectNoDifference(Data(try #require(preserved[7]).dropFirst(8)), expected) + } + + @Test func preservesCopiedProductMetadata() async throws { + let root = try temporaryDirectory() + defer { try? FileManager.default.removeItem(at: root) } + let original = root.appending(path: "Original") + try machO().write(to: original) + try PropertyListSerialization.data(fromPropertyList: ["get-task-allow": true], format: .xml, options: 0) + .write(to: root.appending(path: "entitlements.plist")) + try await sign(["--entitlements", "entitlements.plist", "Original"], in: root) + let originalSlots = try signatureSlots(at: original) + var contents = try Data(contentsOf: original) + // Give the source a restrict flag to ensure re-signing reads metadata + // instead of simply deriving a new identifier and resetting all flags. + let directoryOffset = Int(read32(contents, 4096 + 16)) + contents.replaceSubrange((4096 + directoryOffset + 12)..<(4096 + directoryOffset + 16), + with: [0, 0, 8, 2]) // CS_RESTRICT | CS_ADHOC + let copy = root.appending(path: "Copied") + try contents.write(to: copy) + try await sign(["--preserve-metadata=identifier,entitlements,flags", "Copied"], in: root) + let slots = try signatureSlots(at: copy) + let directory = try #require(slots[0]) + let identifierOffset = Int(read32(directory, 20)) + expectNoDifference(String(decoding: directory.dropFirst(identifierOffset).prefix { $0 != 0 }, as: UTF8.self), + "Original") + expectNoDifference(read32(directory, 12), 0x802) + expectNoDifference(slots[5], originalSlots[5]) + expectNoDifference(slots[7], originalSlots[7]) + // Explicit entitlements take precedence over preserved entitlements. + let empty = try PropertyListSerialization.data(fromPropertyList: [:], format: .xml, options: 0) + try empty.write(to: root.appending(path: "empty.plist")) + try await sign(["--preserve-metadata=identifier,entitlements,flags", + "--entitlements", "empty.plist", "Copied"], in: root) + let replaced = try signatureSlots(at: copy) + expectNoDifference(Data(try #require(replaced[7]).dropFirst(8)), try DerqEncoder().encodeEntitlements(empty)) + } + + @Test func clearsUnrequestedEntitlementsWhenResigning() async throws { + let root = try temporaryDirectory() + defer { try? FileManager.default.removeItem(at: root) } + let executable = root.appending(path: "Executable") + try machO().write(to: executable) + let entitlements = try PropertyListSerialization.data( + fromPropertyList: ["application-identifier": "org.example.App"], format: .xml, options: 0 + ) + try entitlements.write(to: root.appending(path: "entitlements.plist")) + let empty = try PropertyListSerialization.data(fromPropertyList: [:], format: .xml, options: 0) + let emptyDER = try DerqEncoder().encodeEntitlements(empty) + for arguments in [[], ["--preserve-metadata=identifier,flags"]] { + try await sign(["--entitlements", "entitlements.plist", "Executable"], in: root) + // Model an incremental build that no longer supplies --entitlements, + // without relinking or otherwise removing the previous signature. + try await sign(arguments + ["Executable"], in: root) + let slots = try signatureSlots(at: executable) + let xml = Data(try #require(slots[5]).dropFirst(8)) + expectNoDifference(try DerqEncoder().encodeEntitlements(xml), emptyDER) + expectNoDifference(Data(try #require(slots[7]).dropFirst(8)), emptyDER) + } + } + + @Test func signingParentDoesNotModifyNestedCode() async throws { + let root = try temporaryDirectory() + defer { try? FileManager.default.removeItem(at: root) } + let app = root.appending(path: "App.app") + let child = app.appending(path: "PlugIns/Child.appex") + try FileManager.default.createDirectory(at: child, withIntermediateDirectories: true) + for bundle in [app, child] { + try machO().write(to: bundle.appending(path: "Executable")) + try PropertyListSerialization.data(fromPropertyList: [ + "CFBundleIdentifier": "org.example.\(bundle.deletingPathExtension().lastPathComponent)", + "CFBundleExecutable": "Executable", "CFBundleVersion": "1", + ], format: .xml, options: 0).write(to: bundle.appending(path: "Info.plist")) + } + try await sign([child.path], in: root) + let childExecutable = try Data(contentsOf: child.appending(path: "Executable")) + let childSeal = try Data(contentsOf: child.appending(path: "_CodeSignature/CodeResources")) + try await sign([app.path], in: root) + expectNoDifference(try Data(contentsOf: child.appending(path: "Executable")), childExecutable) + expectNoDifference(try Data(contentsOf: child.appending(path: "_CodeSignature/CodeResources")), childSeal) + #expect(FileManager.default.fileExists(atPath: app.appending(path: "_CodeSignature/CodeResources").path)) + let slots = try signatureSlots(at: app.appending(path: "Executable")) + let empty = try PropertyListSerialization.data(fromPropertyList: [:], format: .xml, options: 0) + let emptyDER = try DerqEncoder().encodeEntitlements(empty) + expectNoDifference(try DerqEncoder().encodeEntitlements(Data(try #require(slots[5]).dropFirst(8))), emptyDER) + expectNoDifference(Data(try #require(slots[7]).dropFirst(8)), emptyDER) + } + + @Test func rejectsInvalidXMLBeforeSigning() async throws { + let root = try temporaryDirectory() + defer { try? FileManager.default.removeItem(at: root) } + let path = root.appending(path: "Executable") + let original = machO() + try original.write(to: path) + for value in ["&unknown;", "&", "&#;", "&#x;", "�", "�", "�", + "�", "&#-1;"] { + try original.write(to: path) + let xml = Data("value\(value)".utf8) + try xml.write(to: root.appending(path: "entitlements.plist")) + let result = try await CodesignTool().execute( + commandLine: ["codesign", "--force", "--sign", "-", "--entitlements", "entitlements.plist", path.path], + workingDirectory: root.path, environment: [:] + ) + guard case .result(.exit(let status), _, let stderr) = result else { + Issue.record("Expected a tool exit status") + continue + } + #expect(status != 0, "Invalid XML reference: \(value)") + #expect(!stderr.isEmpty) + expectNoDifference(try Data(contentsOf: path), original) + } + } + + @Test func rejectsUnsupportedOperationsBeforeChangingFiles() async throws { + let root = try temporaryDirectory() + defer { try? FileManager.default.removeItem(at: root) } + let path = root.appending(path: "Executable") + let original = machO() + try original.write(to: path) + for arguments in [ + ["-r-", "--display"], + ["-r", "-", "--display"], + ["--requirements", "-", "--display"], + ["--force", "--sign", "iPhone Developer"], + ["--force", "--sign", "-", "--preserve-metadata=requirements"], + ["--force", "--sign", "-", "--options", "runtime"], + ] { + let result = try await CodesignTool().execute( + commandLine: ["codesign"] + arguments + [path.path], workingDirectory: root.path, environment: [:] + ) + guard case .result(.exit(let status), _, let stderr) = result else { + Issue.record("Expected a tool exit status") + continue + } + #expect(status != 0) + #expect(!stderr.isEmpty) + if arguments.contains("--display") { + #expect(String(decoding: stderr, as: UTF8.self).contains( + "Code requirement operations (-r/--requirements) are not supported yet" + )) + } + expectNoDifference(try Data(contentsOf: path), original) + } + } + + private func sign(_ arguments: [String], in directory: URL) async throws { + let result = try await CodesignTool().execute( + commandLine: ["codesign", "--force", "--sign", "-", "--timestamp=none"] + arguments, + workingDirectory: directory.path, environment: [:] + ) + guard case .result(.exit(let status), _, let stderr) = result else { + Issue.record("Expected a tool exit status") + return + } + try #require(status == 0, "\(String(decoding: stderr, as: UTF8.self))") + } + + private func temporaryDirectory() throws -> URL { + var template = Array(FileManager.default.temporaryDirectory.appending(path: "xtool-codesign-XXXXXX").path.utf8CString) + try #require(mkdtemp(&template) != nil) + let path = String(decoding: template.dropLast().map { UInt8(bitPattern: $0) }, as: UTF8.self) + return URL(fileURLWithPath: path, isDirectory: true) + } + + private func read32(_ data: Data, _ offset: Int) -> UInt32 { + data.withUnsafeBytes { UInt32(bigEndian: $0.loadUnaligned(fromByteOffset: offset, as: UInt32.self)) } + } + + private func signatureSlots(at path: URL) throws -> [UInt32: Data] { + let data = try Data(contentsOf: path) + let base = 4096 + try #require(read32(data, base) == 0xfade0cc0) + var slots: [UInt32: Data] = [:] + for index in 0.. Data { + var data = Data() + func append32(_ value: UInt32) { withUnsafeBytes(of: value.littleEndian) { data.append(contentsOf: $0) } } + func append64(_ value: UInt64) { withUnsafeBytes(of: value.littleEndian) { data.append(contentsOf: $0) } } + for value in [0xfeedfacf, 0x0100000c, 0, fileType, 2, 224, 0, 0] { append32(value) } + for (name, offset, size): (String, UInt64, UInt64) in [("__TEXT", 0, 4096), ("__LINKEDIT", 4096, 0)] { + let isText = name == "__TEXT" + append32(0x19) + append32(isText ? 152 : 72) + data.append(contentsOf: name.utf8) + data.append(Data(repeating: 0, count: 16 - name.utf8.count)) + for value in [0x100000000 + offset, size, offset, size] { append64(value) } + for value: UInt32 in [5, 5, isText ? 1 : 0, 0] { append32(value) } + if isText { + // A real section lets the signer determine where header padding + // ends before inserting LC_CODE_SIGNATURE. The sole instruction + // is arm64 `ret`; the tests only inspect the file, never run it. + for name in ["__text", "__TEXT"] { + data.append(contentsOf: name.utf8) + data.append(Data(repeating: 0, count: 16 - name.utf8.count)) + } + append64(0x100000400) + append64(4) + for value: UInt32 in [1024, 2, 0, 0, 0x80000400, 0, 0, 0] { append32(value) } + } + } + data.append(Data(repeating: 0, count: 4096 - data.count)) + data.replaceSubrange(1024..<1028, with: [0xc0, 0x03, 0x5f, 0xd6]) + return data + } +} diff --git a/Tests/XToolTests/DerqToolTests.swift b/Tests/XToolTests/DerqToolTests.swift new file mode 100644 index 00000000..6902e28a --- /dev/null +++ b/Tests/XToolTests/DerqToolTests.swift @@ -0,0 +1,56 @@ +import CustomDump +import Foundation +import Testing +@testable import XToolSupport + +struct DerqToolTests { + @Test func matchesAppleDEREncoding() throws { + // Captured from `derq query -f xml -i -o --raw`. + // In particular, NSNumber must not turn integer 0/1 into BOOLEANs. + let fixtures: [(Any, String)] = [ + (false, "7011020101b00c300a0c0576616c7565010100"), + (true, "7011020101b00c300a0c0576616c75650101ff"), + (0, "7011020101b00c300a0c0576616c7565020100"), + (1, "7011020101b00c300a0c0576616c7565020101"), + (-1, "7011020101b00c300a0c0576616c75650201ff"), + (127, "7011020101b00c300a0c0576616c756502017f"), + (128, "7012020101b00d300b0c0576616c756502020080"), + (256, "7012020101b00d300b0c0576616c756502020100"), + (-129, "7012020101b00d300b0c0576616c75650202ff7f"), + (Int64.min, "7018020101b01330110c0576616c756502088000000000000000"), + (Int64.max, "7018020101b01330110c0576616c756502087fffffffffffffff"), + ] + for (value, expected) in fixtures { + for format in [PropertyListSerialization.PropertyListFormat.xml, .binary] { + let plist = try PropertyListSerialization.data(fromPropertyList: ["value": value], format: format, options: 0) + expectNoDifference(try hexEncoded(plist), expected) + } + } + } + + @Test func encodesEmptyAndNestedDictionaries() throws { + let empty = try PropertyListSerialization.data(fromPropertyList: [:], format: .xml, options: 0) + expectNoDifference(try hexEncoded(empty), "7005020101b000") + let nested: [String: Any] = ["z": [], "a": ["flag": true], "message": "<&>\t\n🍎"] + for format in [PropertyListSerialization.PropertyListFormat.xml, .binary] { + let plist = try PropertyListSerialization.data(fromPropertyList: nested, format: format, options: 0) + expectNoDifference(try hexEncoded(plist), + "7034020101b02f30100c0161b00b30090c04666c61670101ff" + + "30140c076d6573736167650c093c263e090af09f8d8e30050c017a3000") + } + } + + @Test func rejectsUnsupportedValues() throws { + for value: Any in [Data([1, 2]), Date(timeIntervalSince1970: 0), 1.5, UInt64.max] { + let plist = try PropertyListSerialization.data(fromPropertyList: ["value": value], format: .xml, options: 0) + #expect(throws: (any Error).self) { try DerqEncoder().encodeEntitlements(plist) } + } + let array = try PropertyListSerialization.data(fromPropertyList: [1, 2], format: .xml, options: 0) + #expect(throws: (any Error).self) { try DerqEncoder().encodeEntitlements(array) } + #expect(throws: (any Error).self) { try DerqEncoder().encodeEntitlements(Data("invalid plist".utf8)) } + } + + private func hexEncoded(_ plist: Data) throws -> String { + try DerqEncoder().encodeEntitlements(plist).map { String(format: "%02x", $0) }.joined() + } +} From f6e4b7a1a5da8e277132519ca0234329a9f9dd90 Mon Sep 17 00:00:00 2001 From: Kabir Oberai Date: Thu, 29 Oct 2026 23:18:25 -0400 Subject: [PATCH 04/15] examples: add DemoAudioUnit --- Examples/DemoAudioUnit/Package.swift | 22 ++++++ Examples/DemoAudioUnit/README.md | 5 ++ .../Sources/DemoAudioUnit/App.swift | 78 +++++++++++++++++++ .../DemoAudioUnitExtension/AudioUnit.swift | 75 ++++++++++++++++++ .../Support/DemoAudioUnit.entitlements | 12 +++ .../Support/DemoAudioUnitExtension-Info.plist | 39 ++++++++++ .../DemoAudioUnitExtension.entitlements | 10 +++ Examples/DemoAudioUnit/xtool.yml | 8 ++ Examples/DemoBinary/Package.resolved | 15 ++++ Examples/DemoBinary/Package.swift | 30 +++++++ Examples/DemoBinary/README.md | 5 ++ .../DemoBinary/Sources/DemoBinary/App.swift | 36 +++++++++ Examples/DemoBinary/xtool.yml | 2 + .../ExampleSupport/ExampleSupport.swift | 27 +++++++ 14 files changed, 364 insertions(+) create mode 100644 Examples/DemoAudioUnit/Package.swift create mode 100644 Examples/DemoAudioUnit/README.md create mode 100644 Examples/DemoAudioUnit/Sources/DemoAudioUnit/App.swift create mode 100644 Examples/DemoAudioUnit/Sources/DemoAudioUnitExtension/AudioUnit.swift create mode 100644 Examples/DemoAudioUnit/Support/DemoAudioUnit.entitlements create mode 100644 Examples/DemoAudioUnit/Support/DemoAudioUnitExtension-Info.plist create mode 100644 Examples/DemoAudioUnit/Support/DemoAudioUnitExtension.entitlements create mode 100644 Examples/DemoAudioUnit/xtool.yml create mode 100644 Examples/DemoBinary/Package.resolved create mode 100644 Examples/DemoBinary/Package.swift create mode 100644 Examples/DemoBinary/README.md create mode 100644 Examples/DemoBinary/Sources/DemoBinary/App.swift create mode 100644 Examples/DemoBinary/xtool.yml diff --git a/Examples/DemoAudioUnit/Package.swift b/Examples/DemoAudioUnit/Package.swift new file mode 100644 index 00000000..74dcb13b --- /dev/null +++ b/Examples/DemoAudioUnit/Package.swift @@ -0,0 +1,22 @@ +// swift-tools-version: 6.0 + +import PackageDescription + +let package = Package( + name: "DemoAudioUnit", + platforms: [ + .iOS(.v17), + .macOS(.v14), + ], + products: [ + .library(name: "DemoAudioUnit", targets: ["DemoAudioUnit"]), + .library(name: "DemoAudioUnitExtension", targets: ["DemoAudioUnitExtension"]), + ], + dependencies: [ + .package(path: "../ExampleSupport"), + ], + targets: [ + .target(name: "DemoAudioUnit", dependencies: ["ExampleSupport"]), + .target(name: "DemoAudioUnitExtension"), + ] +) diff --git a/Examples/DemoAudioUnit/README.md b/Examples/DemoAudioUnit/README.md new file mode 100644 index 00000000..1fc7b7aa --- /dev/null +++ b/Examples/DemoAudioUnit/README.md @@ -0,0 +1,5 @@ +# DemoAudioUnit + +This is an end-to-end test of building and running an app with an app extension (AudioUnit Extension). + +We verify that the app can communicate back and forth with the extension, and that they can use a shared app group. diff --git a/Examples/DemoAudioUnit/Sources/DemoAudioUnit/App.swift b/Examples/DemoAudioUnit/Sources/DemoAudioUnit/App.swift new file mode 100644 index 00000000..4f6968ef --- /dev/null +++ b/Examples/DemoAudioUnit/Sources/DemoAudioUnit/App.swift @@ -0,0 +1,78 @@ +import AudioToolbox +import AVFAudio +import ExampleSupport +import SwiftUI + +@main struct DemoApp: App { + var body: some Scene { + WindowGroup { + Text("Testing Audio Unit extension...") + .padding() + .task { + let watchdog = Task { @MainActor in + try await Task.sleep(for: .seconds(15)) + print("Test failed: Audio Unit extension timed out") + exit(1) + } + defer { watchdog.cancel() } + do { + try await Task { @concurrent in try await runTest() }.value + Example.pass() + } catch { + print("Test failed: \(error)") + exit(1) + } + } + } + } +} + +private func runTest() async throws { + let description = AudioComponentDescription( + componentType: kAudioUnitType_Generator, + componentSubType: 0x70696E67, // ping + componentManufacturer: 0x58746F6F, // Xtoo + componentFlags: 0, + componentFlagsMask: 0 + ) + let audioUnit = try await AUAudioUnit.instantiate(with: description, options: .loadOutOfProcess) + let channel = audioUnit.messageChannel(for: "xtool.test") + guard let reply = channel.callAudioUnit?(["message": "ping"]), + reply["message"] as? String == "pong" else { + throw StringError("Audio Unit extension did not reply with pong") + } + guard let pid = reply["pid"] as? Int, + pid > 0, pid != Int(ProcessInfo.processInfo.processIdentifier) else { + throw StringError("Audio Unit did not execute in a separate process") + } + print("Audio Unit extension replied pong from PID \(pid)") + try testAppGroup(channel: channel) +} + +private func testAppGroup(channel: any AUMessageChannel) throws { + // xtool prefixes both the bundle ID and App Group when signing for a device. + let groupID = "group.\(Bundle.main.bundleIdentifier!).data" + guard FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: groupID) != nil, + let defaults = UserDefaults(suiteName: groupID) else { + throw StringError("Host could not access the App Group") + } + // fresh keys ensure a previous run cannot satisfy this test. + let token = UUID().uuidString + let requestKey = "request.\(token)" + let responseKey = "response.\(token)" + defer { + defaults.removeObject(forKey: requestKey) + defaults.removeObject(forKey: responseKey) + } + defaults.set("ping", forKey: requestKey) + let reply = channel.callAudioUnit?([ + "message": "test-app-group", "groupID": groupID, "token": token, + ]) + guard reply?["message"] as? String == "ok" else { + throw StringError("App Group extension test failed: \(reply, default: "nil")") + } + guard defaults.string(forKey: responseKey) == "pong" else { + throw StringError("Host did not read pong from App Group defaults") + } + print("App Group defaults exchanged ping/pong between host and extension") +} diff --git a/Examples/DemoAudioUnit/Sources/DemoAudioUnitExtension/AudioUnit.swift b/Examples/DemoAudioUnit/Sources/DemoAudioUnitExtension/AudioUnit.swift new file mode 100644 index 00000000..885e030f --- /dev/null +++ b/Examples/DemoAudioUnit/Sources/DemoAudioUnitExtension/AudioUnit.swift @@ -0,0 +1,75 @@ +import AudioToolbox +import AVFAudio +import Foundation + +@objc(DemoAudioUnitFactory) +final class AudioUnitFactory: NSObject, AUAudioUnitFactory { + func beginRequest(with context: NSExtensionContext) {} + + func createAudioUnit(with description: AudioComponentDescription) throws -> AUAudioUnit { + try DemoAudioUnit(componentDescription: description) + } +} + +private final class DemoAudioUnit: AUAudioUnit { + private var outputs: AUAudioUnitBusArray! + + override init( + componentDescription: AudioComponentDescription, + options: AudioComponentInstantiationOptions = [] + ) throws { + try super.init(componentDescription: componentDescription, options: options) + let format = AVAudioFormat(standardFormatWithSampleRate: 44_100, channels: 2)! + let bus = try AUAudioUnitBus(format: format) + self.outputs = AUAudioUnitBusArray(audioUnit: self, busType: .output, busses: [bus]) + } + + override var outputBusses: AUAudioUnitBusArray { outputs } + + // A silent generator keeps this a valid Audio Unit, although the execution + // test only instantiates it and exchanges messages; it never starts audio. + override var internalRenderBlock: AUInternalRenderBlock { + { _, _, _, _, output, _, _ in + for buffer in UnsafeMutableAudioBufferListPointer(output) { + if let data = buffer.mData { + memset(data, 0, Int(buffer.mDataByteSize)) + } + } + return noErr + } + } + + override func messageChannel(for channelName: String) -> any AUMessageChannel { + TestMessageChannel() + } +} + +private final class TestMessageChannel: NSObject, AUMessageChannel { + // AudioToolbox installs this callback when connecting the remote channel, + // even though this test only sends requests from the host. + var callHostBlock: CallHostBlock? + + func callAudioUnit(_ message: [AnyHashable: Any]) -> [AnyHashable: Any] { + if message["message"] as? String == "test-app-group" { + return ["message": testAppGroup(message)] + } + return [ + "message": message["message"] as? String == "ping" ? "pong" : "unknown message", + "pid": Int(ProcessInfo.processInfo.processIdentifier), + ] + } + + private func testAppGroup(_ message: [AnyHashable: Any]) -> String { + guard let groupID = message["groupID"] as? String, + let token = message["token"] as? String, + FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: groupID) != nil, + let defaults = UserDefaults(suiteName: groupID) else { + return "Extension could not access the App Group" + } + guard defaults.string(forKey: "request.\(token)") == "ping" else { + return "Extension did not read ping from App Group defaults" + } + defaults.set("pong", forKey: "response.\(token)") + return "ok" + } +} diff --git a/Examples/DemoAudioUnit/Support/DemoAudioUnit.entitlements b/Examples/DemoAudioUnit/Support/DemoAudioUnit.entitlements new file mode 100644 index 00000000..446d84dd --- /dev/null +++ b/Examples/DemoAudioUnit/Support/DemoAudioUnit.entitlements @@ -0,0 +1,12 @@ + + + + + com.apple.security.application-groups + + group.sh.xtool.examples.DemoAudioUnit.data + + inter-app-audio + + + diff --git a/Examples/DemoAudioUnit/Support/DemoAudioUnitExtension-Info.plist b/Examples/DemoAudioUnit/Support/DemoAudioUnitExtension-Info.plist new file mode 100644 index 00000000..a823d064 --- /dev/null +++ b/Examples/DemoAudioUnit/Support/DemoAudioUnitExtension-Info.plist @@ -0,0 +1,39 @@ + + + + + NSExtension + + NSExtensionPointIdentifier + com.apple.AudioUnit + NSExtensionPrincipalClass + DemoAudioUnitFactory + NSExtensionAttributes + + AudioComponents + + + name + xtool: DemoAudioUnit + description + xtool extension execution test + manufacturer + Xtoo + type + augn + subtype + ping + version + 1 + sandboxSafe + + tags + + Generator + + + + + + + diff --git a/Examples/DemoAudioUnit/Support/DemoAudioUnitExtension.entitlements b/Examples/DemoAudioUnit/Support/DemoAudioUnitExtension.entitlements new file mode 100644 index 00000000..607cdfe7 --- /dev/null +++ b/Examples/DemoAudioUnit/Support/DemoAudioUnitExtension.entitlements @@ -0,0 +1,10 @@ + + + + + com.apple.security.application-groups + + group.sh.xtool.examples.DemoAudioUnit.data + + + diff --git a/Examples/DemoAudioUnit/xtool.yml b/Examples/DemoAudioUnit/xtool.yml new file mode 100644 index 00000000..3e669100 --- /dev/null +++ b/Examples/DemoAudioUnit/xtool.yml @@ -0,0 +1,8 @@ +version: 1 +bundleID: sh.xtool.examples.DemoAudioUnit +product: DemoAudioUnit +entitlementsPath: Support/DemoAudioUnit.entitlements +extensions: +- product: DemoAudioUnitExtension + infoPath: Support/DemoAudioUnitExtension-Info.plist + entitlementsPath: Support/DemoAudioUnitExtension.entitlements diff --git a/Examples/DemoBinary/Package.resolved b/Examples/DemoBinary/Package.resolved new file mode 100644 index 00000000..fc5e45ae --- /dev/null +++ b/Examples/DemoBinary/Package.resolved @@ -0,0 +1,15 @@ +{ + "originHash" : "631b6bc3d92a4afe204994d83c6c1aa97d9dd251c57696eb4f5dcdf1999eb3e9", + "pins" : [ + { + "identity" : "openssl", + "kind" : "remoteSourceControl", + "location" : "https://github.com/krzyzanowskim/OpenSSL", + "state" : { + "revision" : "0a6149bff4fb00921ce0ec1811f726b166638095", + "version" : "3.6.5000" + } + } + ], + "version" : 3 +} diff --git a/Examples/DemoBinary/Package.swift b/Examples/DemoBinary/Package.swift new file mode 100644 index 00000000..e245c3dd --- /dev/null +++ b/Examples/DemoBinary/Package.swift @@ -0,0 +1,30 @@ +// swift-tools-version: 6.0 + +import PackageDescription + +let package = Package( + name: "DemoBinary", + platforms: [ + .iOS(.v17), + .macOS(.v14), + ], + products: [ + .library( + name: "DemoBinary", + targets: ["DemoBinary"] + ), + ], + dependencies: [ + .package(path: "../ExampleSupport"), + .package(url: "https://github.com/krzyzanowskim/OpenSSL", from: "3.0.0"), + ], + targets: [ + .target( + name: "DemoBinary", + dependencies: [ + "ExampleSupport", + "OpenSSL", + ] + ), + ] +) diff --git a/Examples/DemoBinary/README.md b/Examples/DemoBinary/README.md new file mode 100644 index 00000000..269d5c89 --- /dev/null +++ b/Examples/DemoBinary/README.md @@ -0,0 +1,5 @@ +# DemoBinary + +This test validates that we can link and embed a binary xcframework, and then use it at runtime. + +We perform the test with [krzyzanowskim/OpenSSL](https://github.com/krzyzanowskim/OpenSSL). diff --git a/Examples/DemoBinary/Sources/DemoBinary/App.swift b/Examples/DemoBinary/Sources/DemoBinary/App.swift new file mode 100644 index 00000000..473bfaaf --- /dev/null +++ b/Examples/DemoBinary/Sources/DemoBinary/App.swift @@ -0,0 +1,36 @@ +import ExampleSupport +import SwiftUI +import OpenSSL + +@main struct DemoApp: App { + public var body: some Scene { + WindowGroup { + ContentView() + } + } +} + +private struct ContentView: View { + @State var counter = 0 + + var body: some View { + Text("Hello, world!") + .padding() + .task { + do { + try await verify() + Example.pass() + } catch { + print("Test failed: \(error)") + exit(1) + } + } + } +} + +private func verify() async throws { + let shaSize = EVP_MD_get_size(EVP_sha256()) * 8 + guard shaSize == 256 else { + throw StringError("OpenSSL sanity check failed: it claims that SHA-256 is \(shaSize) bits (!= 256)") + } +} diff --git a/Examples/DemoBinary/xtool.yml b/Examples/DemoBinary/xtool.yml new file mode 100644 index 00000000..fe559928 --- /dev/null +++ b/Examples/DemoBinary/xtool.yml @@ -0,0 +1,2 @@ +version: 1 +bundleID: sh.xtool.examples.DemoBinary diff --git a/Examples/ExampleSupport/Sources/ExampleSupport/ExampleSupport.swift b/Examples/ExampleSupport/Sources/ExampleSupport/ExampleSupport.swift index dae8aa27..67dd1699 100644 --- a/Examples/ExampleSupport/Sources/ExampleSupport/ExampleSupport.swift +++ b/Examples/ExampleSupport/Sources/ExampleSupport/ExampleSupport.swift @@ -10,3 +10,30 @@ extension Example { } } } + +nonisolated(nonsending) public func withTimeout( + _ timeout: Duration, + _ operation: nonisolated(nonsending) @Sendable @escaping () async throws -> T, +) async throws -> T { + try await withThrowingTaskGroup(of: T.self) { group in + group.addTask { + try await operation() + } + group.addTask { + try await Task.sleep(for: timeout) + throw TimeoutError() + } + let result = try await group.next()! + group.cancelAll() + return result + } +} + +public struct TimeoutError: Error {} + +public struct StringError: Error, CustomStringConvertible { + public let description: String + public init(_ description: String) { + self.description = description + } +} From 2c09ba0bbb09a4acf776f7e20ba6f76881dd048b Mon Sep 17 00:00:00 2001 From: Kabir Oberai Date: Mon, 2 Nov 2026 19:06:34 -0500 Subject: [PATCH 05/15] meta: merge PackLib into XToolSupport --- Package.swift | 14 +++----------- Sources/XCLIUtils/Console.swift | 11 +++-------- Sources/XToolSupport/BuildCoordinator.swift | 1 - Sources/XToolSupport/BuildDescription.swift | 1 - Sources/XToolSupport/BuildSessionManager.swift | 1 - .../{PackLib => XToolSupport}/BuildSettings.swift | 0 Sources/{PackLib => XToolSupport}/DarwinSDK.swift | 0 Sources/XToolSupport/DevCommand.swift | 1 - Sources/XToolSupport/LSPConfig.swift | 1 - Sources/XToolSupport/NewCommand.swift | 1 - .../{PackLib => XToolSupport}/PackSchema.swift | 0 Sources/XToolSupport/ProcessZIPCompressor.swift | 1 - Sources/XToolSupport/SDKCommand.swift | 1 - Sources/XToolSupport/SetupCommand.swift | 1 - .../{PackLib => XToolSupport}/Stdlib+Utils.swift | 0 .../SwiftPMBuildDescriber+Products.swift | 1 - .../SwiftPMBuildDescriber+XcodeProject.swift | 1 - Sources/XToolSupport/SwiftPMBuildDescriber.swift | 1 - Sources/XToolSupport/SwiftPMPIFDumper.swift | 1 - .../{PackLib => XToolSupport}/SwiftVersion.swift | 0 .../{PackLib => XToolSupport}/ToolRegistry.swift | 0 Sources/XUtils/Console.swift | 15 +++++++++++++++ .../XToolTests/BuildCoordinatorTripleTests.swift | 1 - Tests/XToolTests/DarwinSDKTests.swift | 2 +- .../XToolTests/DirectBuildDescriptionTests.swift | 1 - Tests/XToolTests/SwiftPMBuildDescriberTests.swift | 1 - Tests/XToolTests/SwiftVersionTests.swift | 2 +- 27 files changed, 23 insertions(+), 37 deletions(-) rename Sources/{PackLib => XToolSupport}/BuildSettings.swift (100%) rename Sources/{PackLib => XToolSupport}/DarwinSDK.swift (100%) rename Sources/{PackLib => XToolSupport}/PackSchema.swift (100%) rename Sources/{PackLib => XToolSupport}/Stdlib+Utils.swift (100%) rename Sources/{PackLib => XToolSupport}/SwiftVersion.swift (100%) rename Sources/{PackLib => XToolSupport}/ToolRegistry.swift (100%) create mode 100644 Sources/XUtils/Console.swift diff --git a/Package.swift b/Package.swift index 9753ca82..c9a74638 100644 --- a/Package.swift +++ b/Package.swift @@ -182,7 +182,6 @@ let package = Package( "XKit", "XToolSupport", "XCLIUtils", - "PackLib", .product(name: "Crypto", package: "swift-crypto"), .product(name: "X509", package: "swift-certificates"), .product(name: "DependenciesTestSupport", package: "swift-dependencies"), @@ -209,7 +208,7 @@ let package = Package( "SwiftyMobileDevice", "XKit", "XCLIUtils", - "PackLib", + "XUtils", .product(name: "ArgumentParser", package: "swift-argument-parser"), .product(name: "ConcurrencyExtras", package: "swift-concurrency-extras"), .product(name: "SystemPackage", package: "swift-system"), @@ -219,6 +218,8 @@ let package = Package( .product(name: "SwiftBuild", package: "swift-build"), .product(name: "SWBUtil", package: "swift-build"), .product(name: "SwiftASN1", package: "swift-asn1"), + .product(name: "Superutils", package: "xtool-core"), + .product(name: "Yams", package: "Yams"), ], cSettings: cSettings, swiftSettings: swiftSettings, @@ -230,15 +231,6 @@ let package = Package( .product(name: "XcodeProjectFormat", package: "xcode-project-format"), ], ), - .target( - name: "PackLib", - dependencies: [ - "XUtils", - .product(name: "Superutils", package: "xtool-core"), - .product(name: "Yams", package: "Yams"), - ], - swiftSettings: swiftSettings, - ), .executableTarget( name: "xtool", dependencies: [ diff --git a/Sources/XCLIUtils/Console.swift b/Sources/XCLIUtils/Console.swift index ef5abae8..1e9dc8b7 100644 --- a/Sources/XCLIUtils/Console.swift +++ b/Sources/XCLIUtils/Console.swift @@ -2,7 +2,9 @@ import Foundation import XUtils import Dependencies -package enum Console { +package typealias Console = XUtils::Console + +extension Console { package static func prompt(_ message: String) async throws -> String { d.out.print(message, terminator: "", flush: true) guard let line = try await d.input.readLine() else { throw Console.Error("Input ended before a response was received.") } @@ -101,11 +103,4 @@ package enum Console { } } } - - package struct Error: Swift.Error, CustomStringConvertible { - package let description: String - package init(_ description: String) { - self.description = description - } - } } diff --git a/Sources/XToolSupport/BuildCoordinator.swift b/Sources/XToolSupport/BuildCoordinator.swift index 2b70d8d5..3f0a69ca 100644 --- a/Sources/XToolSupport/BuildCoordinator.swift +++ b/Sources/XToolSupport/BuildCoordinator.swift @@ -1,7 +1,6 @@ import XCLIUtils import XPIF import Foundation -import PackLib import XUtils import Subprocess import SwiftBuild diff --git a/Sources/XToolSupport/BuildDescription.swift b/Sources/XToolSupport/BuildDescription.swift index 0ea42d14..2385b63c 100644 --- a/Sources/XToolSupport/BuildDescription.swift +++ b/Sources/XToolSupport/BuildDescription.swift @@ -1,7 +1,6 @@ import XCLIUtils import XUtils import XPIF -import PackLib import Foundation struct BuildDescription { diff --git a/Sources/XToolSupport/BuildSessionManager.swift b/Sources/XToolSupport/BuildSessionManager.swift index bb74dcea..988e79c9 100644 --- a/Sources/XToolSupport/BuildSessionManager.swift +++ b/Sources/XToolSupport/BuildSessionManager.swift @@ -2,7 +2,6 @@ import XCLIUtils import XUtils import XPIF import SwiftBuild -import PackLib import Foundation import Dependencies diff --git a/Sources/PackLib/BuildSettings.swift b/Sources/XToolSupport/BuildSettings.swift similarity index 100% rename from Sources/PackLib/BuildSettings.swift rename to Sources/XToolSupport/BuildSettings.swift diff --git a/Sources/PackLib/DarwinSDK.swift b/Sources/XToolSupport/DarwinSDK.swift similarity index 100% rename from Sources/PackLib/DarwinSDK.swift rename to Sources/XToolSupport/DarwinSDK.swift diff --git a/Sources/XToolSupport/DevCommand.swift b/Sources/XToolSupport/DevCommand.swift index a371d037..7012d5a2 100644 --- a/Sources/XToolSupport/DevCommand.swift +++ b/Sources/XToolSupport/DevCommand.swift @@ -1,7 +1,6 @@ import XCLIUtils import Foundation import ArgumentParser -import PackLib import XKit import Dependencies import XUtils diff --git a/Sources/XToolSupport/LSPConfig.swift b/Sources/XToolSupport/LSPConfig.swift index 914da43c..01d9b061 100644 --- a/Sources/XToolSupport/LSPConfig.swift +++ b/Sources/XToolSupport/LSPConfig.swift @@ -1,6 +1,5 @@ import XUtils import Foundation -import PackLib enum LSPConfig { static let bsp: (String, String) = ( diff --git a/Sources/XToolSupport/NewCommand.swift b/Sources/XToolSupport/NewCommand.swift index 7ab839df..725cc9e6 100644 --- a/Sources/XToolSupport/NewCommand.swift +++ b/Sources/XToolSupport/NewCommand.swift @@ -2,7 +2,6 @@ import XUtils import XCLIUtils import ArgumentParser import Foundation -import PackLib struct NewCommand: AsyncParsableCommand { static let configuration = CommandConfiguration( diff --git a/Sources/PackLib/PackSchema.swift b/Sources/XToolSupport/PackSchema.swift similarity index 100% rename from Sources/PackLib/PackSchema.swift rename to Sources/XToolSupport/PackSchema.swift diff --git a/Sources/XToolSupport/ProcessZIPCompressor.swift b/Sources/XToolSupport/ProcessZIPCompressor.swift index cc04f053..9011e5a5 100644 --- a/Sources/XToolSupport/ProcessZIPCompressor.swift +++ b/Sources/XToolSupport/ProcessZIPCompressor.swift @@ -1,7 +1,6 @@ import Foundation import XKit import Dependencies -import PackLib import Subprocess import XUtils diff --git a/Sources/XToolSupport/SDKCommand.swift b/Sources/XToolSupport/SDKCommand.swift index a4bafbf0..1f6a8a11 100644 --- a/Sources/XToolSupport/SDKCommand.swift +++ b/Sources/XToolSupport/SDKCommand.swift @@ -3,7 +3,6 @@ import Foundation import XKit import ArgumentParser import Dependencies -import PackLib import XUtils import Subprocess diff --git a/Sources/XToolSupport/SetupCommand.swift b/Sources/XToolSupport/SetupCommand.swift index 65749f31..ffb2f92d 100644 --- a/Sources/XToolSupport/SetupCommand.swift +++ b/Sources/XToolSupport/SetupCommand.swift @@ -1,7 +1,6 @@ import ArgumentParser import Foundation import XKit -import PackLib struct SetupCommand: AsyncParsableCommand { static let configuration = CommandConfiguration( diff --git a/Sources/PackLib/Stdlib+Utils.swift b/Sources/XToolSupport/Stdlib+Utils.swift similarity index 100% rename from Sources/PackLib/Stdlib+Utils.swift rename to Sources/XToolSupport/Stdlib+Utils.swift diff --git a/Sources/XToolSupport/SwiftPMBuildDescriber+Products.swift b/Sources/XToolSupport/SwiftPMBuildDescriber+Products.swift index fece2260..1cbfe48d 100644 --- a/Sources/XToolSupport/SwiftPMBuildDescriber+Products.swift +++ b/Sources/XToolSupport/SwiftPMBuildDescriber+Products.swift @@ -1,5 +1,4 @@ import Foundation -import PackLib import XCLIUtils import XPIF import XUtils diff --git a/Sources/XToolSupport/SwiftPMBuildDescriber+XcodeProject.swift b/Sources/XToolSupport/SwiftPMBuildDescriber+XcodeProject.swift index f9cf7cfd..f7a5f3ef 100644 --- a/Sources/XToolSupport/SwiftPMBuildDescriber+XcodeProject.swift +++ b/Sources/XToolSupport/SwiftPMBuildDescriber+XcodeProject.swift @@ -3,7 +3,6 @@ import Foundation import Dependencies import XcodeProjects import XPIF -import PackLib import XUtils extension SwiftPMBuildDescriber { diff --git a/Sources/XToolSupport/SwiftPMBuildDescriber.swift b/Sources/XToolSupport/SwiftPMBuildDescriber.swift index 99f92579..06c0edef 100644 --- a/Sources/XToolSupport/SwiftPMBuildDescriber.swift +++ b/Sources/XToolSupport/SwiftPMBuildDescriber.swift @@ -1,7 +1,6 @@ import XCLIUtils import XPIF import Foundation -import PackLib import XUtils import Dependencies diff --git a/Sources/XToolSupport/SwiftPMPIFDumper.swift b/Sources/XToolSupport/SwiftPMPIFDumper.swift index a78b19e0..2eb01b89 100644 --- a/Sources/XToolSupport/SwiftPMPIFDumper.swift +++ b/Sources/XToolSupport/SwiftPMPIFDumper.swift @@ -1,6 +1,5 @@ import XPIF import Foundation -import PackLib import XUtils import Subprocess import Dependencies diff --git a/Sources/PackLib/SwiftVersion.swift b/Sources/XToolSupport/SwiftVersion.swift similarity index 100% rename from Sources/PackLib/SwiftVersion.swift rename to Sources/XToolSupport/SwiftVersion.swift diff --git a/Sources/PackLib/ToolRegistry.swift b/Sources/XToolSupport/ToolRegistry.swift similarity index 100% rename from Sources/PackLib/ToolRegistry.swift rename to Sources/XToolSupport/ToolRegistry.swift diff --git a/Sources/XUtils/Console.swift b/Sources/XUtils/Console.swift new file mode 100644 index 00000000..77fcf2e1 --- /dev/null +++ b/Sources/XUtils/Console.swift @@ -0,0 +1,15 @@ +import Foundation + +package struct Console { + package struct Error: Swift.Error, CustomStringConvertible, LocalizedError { + package let description: String + + package init(_ description: String) { + self.description = description + } + + package var errorDescription: String? { + description + } + } +} diff --git a/Tests/XToolTests/BuildCoordinatorTripleTests.swift b/Tests/XToolTests/BuildCoordinatorTripleTests.swift index 53832902..cd0d8b3f 100644 --- a/Tests/XToolTests/BuildCoordinatorTripleTests.swift +++ b/Tests/XToolTests/BuildCoordinatorTripleTests.swift @@ -1,7 +1,6 @@ import CustomDump import Dependencies import Foundation -import PackLib import SwiftBuild import Testing import XPIF diff --git a/Tests/XToolTests/DarwinSDKTests.swift b/Tests/XToolTests/DarwinSDKTests.swift index 96c2e4a6..9e87817e 100644 --- a/Tests/XToolTests/DarwinSDKTests.swift +++ b/Tests/XToolTests/DarwinSDKTests.swift @@ -2,7 +2,7 @@ import Dependencies import Foundation import Testing import XUtils -@testable import PackLib +@testable import XToolSupport @Test func swiftPMDirectoryUsesXDGConfigHome() throws { let directory = try DarwinSDK.swiftPMDirectory( diff --git a/Tests/XToolTests/DirectBuildDescriptionTests.swift b/Tests/XToolTests/DirectBuildDescriptionTests.swift index d2061e43..fa6ac420 100644 --- a/Tests/XToolTests/DirectBuildDescriptionTests.swift +++ b/Tests/XToolTests/DirectBuildDescriptionTests.swift @@ -1,6 +1,5 @@ import CustomDump import Foundation -@testable import PackLib import Testing import XCLIUtils import XPIF diff --git a/Tests/XToolTests/SwiftPMBuildDescriberTests.swift b/Tests/XToolTests/SwiftPMBuildDescriberTests.swift index f3942756..242487fe 100644 --- a/Tests/XToolTests/SwiftPMBuildDescriberTests.swift +++ b/Tests/XToolTests/SwiftPMBuildDescriberTests.swift @@ -2,7 +2,6 @@ import XCLIUtils import CustomDump import Dependencies import Foundation -@testable import PackLib import Testing import XcodeProjects import XPIF diff --git a/Tests/XToolTests/SwiftVersionTests.swift b/Tests/XToolTests/SwiftVersionTests.swift index d1634144..f28ddd8c 100644 --- a/Tests/XToolTests/SwiftVersionTests.swift +++ b/Tests/XToolTests/SwiftVersionTests.swift @@ -1,5 +1,5 @@ import Testing -@testable import PackLib +@testable import XToolSupport @Test(arguments: [ ("6.2", SwiftVersion(6, 2)), From 1cc317ab42c38d63ffa8b7cc9f94a24c3850a6ab Mon Sep 17 00:00:00 2001 From: Kabir Oberai Date: Mon, 2 Nov 2026 21:26:56 -0500 Subject: [PATCH 06/15] swb: refactor BuildSettings -> XToolProject, Toolchain --- Sources/XToolSupport/BuildCoordinator.swift | 6 +- Sources/XToolSupport/BuildDescription.swift | 19 +- .../XToolSupport/BuildSessionManager.swift | 2 +- Sources/XToolSupport/BuildSettings.swift | 163 ------------------ Sources/XToolSupport/DevCommand.swift | 46 ++--- Sources/XToolSupport/LSPConfig.swift | 2 +- .../SwiftPMBuildDescriber+Products.swift | 1 + .../SwiftPMBuildDescriber+XcodeProject.swift | 2 +- .../XToolSupport/SwiftPMBuildDescriber.swift | 26 +-- Sources/XToolSupport/SwiftPMPIFDumper.swift | 37 ++-- Sources/XToolSupport/SwiftVersion.swift | 29 ---- Sources/XToolSupport/Toolchain.swift | 102 +++++++++++ Sources/XToolSupport/XToolProject.swift | 92 ++++++++++ .../BuildCoordinatorTripleTests.swift | 9 +- .../DirectBuildDescriptionTests.swift | 21 ++- .../SwiftPMBuildDescriberTests.swift | 34 ++-- 16 files changed, 289 insertions(+), 302 deletions(-) delete mode 100644 Sources/XToolSupport/BuildSettings.swift create mode 100644 Sources/XToolSupport/Toolchain.swift create mode 100644 Sources/XToolSupport/XToolProject.swift diff --git a/Sources/XToolSupport/BuildCoordinator.swift b/Sources/XToolSupport/BuildCoordinator.swift index 3f0a69ca..93a4e618 100644 --- a/Sources/XToolSupport/BuildCoordinator.swift +++ b/Sources/XToolSupport/BuildCoordinator.swift @@ -92,10 +92,10 @@ public actor BuildCoordinator { let developerDir = sessionManager.darwinSDK.developerDirectory.path let swiftResourceDir = "\(developerDir)/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift" overrides["SWIFT_RESOURCE_DIR[__destination_platform=YES]"] = swiftResourceDir - let runtimeResourcePath = try await BuildSettings.runtimeResourcePath() - let hostClangResources = URL(filePath: runtimeResourcePath).appending(path: "clang") + let runtimeResourcePath = try await d.toolchain.runtimeResourcePath + let hostClangResources = runtimeResourcePath.appending("clang") // Compile with the host's intrinsic headers... - overrides["CLANG_RESOURCE_DIR[__destination_platform=YES]"] = hostClangResources.path + overrides["CLANG_RESOURCE_DIR[__destination_platform=YES]"] = hostClangResources.string // ...but at link time, point to Apple's Darwin compiler runtimes (incl OS availability helpers) // from the Xcode toolchain. overrides["CLANG_RESOURCE_DIR_CLANG[__destination_platform=YES]"] = "\(swiftResourceDir)/clang" diff --git a/Sources/XToolSupport/BuildDescription.swift b/Sources/XToolSupport/BuildDescription.swift index 2385b63c..3d9c9e64 100644 --- a/Sources/XToolSupport/BuildDescription.swift +++ b/Sources/XToolSupport/BuildDescription.swift @@ -1,18 +1,21 @@ -import XCLIUtils -import XUtils -import XPIF import Foundation +import XPIF struct BuildDescription { var directory: URL var pif: XPIF.Container var configuration: BuildConfiguration var triple: String +} + +enum BuildConfiguration: String, CaseIterable, Sendable { + case debug + case release - init(pif: XPIF.Container, buildSettings: BuildSettings) { - self.directory = URL(filePath: buildSettings.packagePath) - self.pif = pif - self.configuration = buildSettings.configuration - self.triple = buildSettings.triple + var swiftBuildValue: String { + switch self { + case .debug: "Debug" + case .release: "Release" + } } } diff --git a/Sources/XToolSupport/BuildSessionManager.swift b/Sources/XToolSupport/BuildSessionManager.swift index 988e79c9..40515274 100644 --- a/Sources/XToolSupport/BuildSessionManager.swift +++ b/Sources/XToolSupport/BuildSessionManager.swift @@ -49,7 +49,7 @@ actor BuildSessionManager { #else let (result, diagnostics) = await service.createSession( name: "xtool", - swiftToolchainPath: try await BuildSettings.toolchainPath(), + swiftToolchainPath: try await d.toolchain.toolchainRoot.string, resourceSearchPaths: [], cachePath: nil, inferiorProductsPath: nil, diff --git a/Sources/XToolSupport/BuildSettings.swift b/Sources/XToolSupport/BuildSettings.swift deleted file mode 100644 index 08a5c911..00000000 --- a/Sources/XToolSupport/BuildSettings.swift +++ /dev/null @@ -1,163 +0,0 @@ -import Foundation -import Subprocess -import XUtils -import Superutils - -public struct BuildSettings: Sendable { - private static let customBinDir = - // this is the same option used by SwiftPM itself for dev builds - ProcessInfo.processInfo.environment["SWIFTPM_CUSTOM_BIN_DIR"].map { FilePath($0) } - - public var packagePath: String - public let configuration: BuildConfiguration - public let triple: String - public let customOptions: [String] - - public var sdkEnvironment: [Environment.Key: String?] { - return [ - // xcrun passes an SDKROOT that messes with our sdk configuration - "SDKROOT": nil, - ] - } - - private var configOptions: [String] { - return [ - "--build-system", "swiftbuild", - "--package-path", packagePath, - ] - } - - public init( - configuration: BuildConfiguration, - triple: String, - packagePath: String = ".", - options: [String] = [] - ) async throws { - self.packagePath = packagePath - self.configuration = configuration - self.customOptions = options - self.triple = triple - } - - #if os(macOS) - private static func xcrun(_ arguments: [String]) async throws -> String { - let result = try await Subprocess.run( - .path("/usr/bin/xcrun"), - arguments: .init(arguments), - output: .string(limit: .max) - ).checkSuccess() - return result.standardOutput.trimmingCharacters(in: .whitespacesAndNewlines) - } - - private static let _swiftURL = Task { - try await FilePath(xcrun(["-f", "swift"])) - } - - public static func swiftURL() async throws -> FilePath { - try await _swiftURL.value - } - - private static let _swiftcURL = Task { - try await FilePath(xcrun(["-f", "swiftc"])) - } - - public static func swiftcURL() async throws -> FilePath { - try await _swiftcURL.value - } - #else - public static func swiftURL() async throws -> FilePath { - try await FilePath(ToolRegistry.locate("swift")).orThrow(StringError("Got bad path for swift executable")) - } - - public static func swiftcURL() async throws -> FilePath { - try await FilePath(ToolRegistry.locate("swiftc")).orThrow(StringError("Got bad path for swiftc executable")) - } - #endif - - private static let _runtimeResourcePath = Task { - struct SwiftTargetInfo: Decodable { - struct Paths: Decodable { - var runtimeResourcePath: String - } - var paths: Paths - } - let process = try await Subprocess.run( - .path(try await swiftcURL()), - arguments: ["-print-target-info"], - output: .data(limit: .max) - ).checkSuccess() - let targetInfo = try JSONDecoder().decode( - SwiftTargetInfo.self, - from: process.standardOutput - ) - return targetInfo.paths.runtimeResourcePath - } - - public static func runtimeResourcePath() async throws -> String { - try await _runtimeResourcePath.value - } - - public static func toolchainPath() async throws -> String { - // we can't simply return the parent of `swiftc` because the `swiftc` in PATH - // might be a trampoline (e.g. xcrun or swiftly) - let toolchainResourcePath = try await runtimeResourcePath() - let trimmed: Substring - if toolchainResourcePath.hasSuffix("/usr/lib/swift") { - trimmed = toolchainResourcePath.dropLast("/usr/lib/swift".count) - } else if toolchainResourcePath.hasSuffix("/usr/local/lib/swift") { - trimmed = toolchainResourcePath.dropLast("/usr/local/lib/swift".count) - } else { - throw StringError("Unexpected toolchain resource path: \(toolchainResourcePath)") - } - return trimmed.isEmpty ? "/" : String(trimmed) - } - - public func withPackagePath(_ path: String) -> Self { - var copy = self - copy.packagePath = path - return copy - } - - public func swiftPMInvocation( - forTool tool: String, - arguments: [String], - ) async throws -> Subprocess.Configuration { - let executable: Executable - let baseArguments: [String] - if let customBinDir = Self.customBinDir { - executable = .path(customBinDir.appending("swift-\(tool)")) - baseArguments = [] - } else { - #if os(macOS) - // xcrun/libxcrun (via the /usr/bin/swift trampoline) is very trigger-happy - // to add SDKROOT=.../MacOSX.sdk to our invocations. We avoid this by - // 1) invoking the real swift executable (located with `xcrun -f`) and - // 2) explicitly removing SDKROOT from the env, as it may be inherited - // through a parent process (e.g. `swift run xtool`). - executable = .path(try await Self.swiftURL()) - #else - executable = .name("swift") - #endif - baseArguments = [tool] - } - - return Configuration( - executable: executable, - arguments: .init(baseArguments + configOptions + customOptions + arguments), - environment: .inherit.updating(sdkEnvironment), - platformOptions: .withGracefulShutDown, - ) - } -} - -public enum BuildConfiguration: String, CaseIterable, Sendable { - case debug - case release - - var swiftBuildValue: String { - switch self { - case .debug: "Debug" - case .release: "Release" - } - } -} diff --git a/Sources/XToolSupport/DevCommand.swift b/Sources/XToolSupport/DevCommand.swift index 7012d5a2..74d2f50f 100644 --- a/Sources/XToolSupport/DevCommand.swift +++ b/Sources/XToolSupport/DevCommand.swift @@ -34,49 +34,21 @@ struct PackOperation { var triple: String? var buildOptions = BuildOptions(configuration: .debug) - var xcode = false @discardableResult func run() async throws -> ProjectBuildResult { - // TODO: support running inside a child directory - - let contents = try FileManager.default.contentsOfDirectory(atPath: ".") - guard contents.contains("Package.swift") - || contents.contains(where: { $0.hasPrefix("Package@") && $0.hasSuffix(".swift") }) - else { - throw Console.Error("Could not find Package.swift in this directory.") - } + let project = try await XToolProject.discover(start: URL(filePath: ".")) try await EnsureSDKOperation(quiet: true).run() - Signposter.track("Planning") - - let schema: PackSchema - let configPath = URL(fileURLWithPath: "xtool.yml") - if FileManager.default.fileExists(atPath: configPath.path) { - schema = try await PackSchema(url: configPath) - } else { - schema = .default - d.out.print(""" - warning: Could not locate configuration file '\(configPath.path)'. Using default \ - configuration with 'com.example' organization ID. - """) - } - - await LSPConfig.tryEnsure(using: schema) + await project.tryEnsureLSPConfig() - let buildSettings = try await BuildSettings( + let description = try await project.describe( configuration: buildOptions.configuration, - triple: triple ?? Self.defaultTriple, - options: [] + triple: triple ?? Self.defaultTriple ) - let describer = try SwiftPMBuildDescriber(buildSettings: buildSettings, schema: schema) - if xcode { - return try await describer.writeXcodeProject() - } - - let coordinator = try await BuildCoordinator(description: describer.describe()) + let coordinator = try await BuildCoordinator(description: description) defer { await coordinator.close() } return try await ProjectBuildOperation( buildCoordinator: coordinator, @@ -92,7 +64,13 @@ struct DevXcodeCommand: AsyncParsableCommand { ) func run() async throws { - let result = try await PackOperation(xcode: true).run() + let project = try await XToolProject.discover(start: URL(filePath: ".")) + guard case .swiftPM(let swiftPM) = project else { + throw Console.Error("generate-xcode-project requires a SwiftPM-based project") + } + + let result = try await SwiftPMBuildDescriber(project: swiftPM).writeXcodeProject() + d.out.print("Wrote to \(result.url.path)") } } diff --git a/Sources/XToolSupport/LSPConfig.swift b/Sources/XToolSupport/LSPConfig.swift index 01d9b061..f76eb65d 100644 --- a/Sources/XToolSupport/LSPConfig.swift +++ b/Sources/XToolSupport/LSPConfig.swift @@ -47,7 +47,7 @@ enum LSPConfig { return } - if try await SwiftVersion.current.supportsSwiftBuild { + if try await d.toolchain.swiftVersion.supportsSwiftBuild { try await ensure(primary: bsp, secondary: legacyLSP, quiet: quiet, currentDirectory: directory) } else { try await ensure(primary: legacyLSP, secondary: bsp, quiet: quiet, currentDirectory: directory) diff --git a/Sources/XToolSupport/SwiftPMBuildDescriber+Products.swift b/Sources/XToolSupport/SwiftPMBuildDescriber+Products.swift index 1cbfe48d..7e62cb03 100644 --- a/Sources/XToolSupport/SwiftPMBuildDescriber+Products.swift +++ b/Sources/XToolSupport/SwiftPMBuildDescriber+Products.swift @@ -5,6 +5,7 @@ import XUtils extension SwiftPMBuildDescriber { func bundleProducts(in project: XPIF.Project) throws -> [ProjectProduct] { + let idSpecifier = self.project.base.schema.idSpecifier let appProduct = try libraryProduct(in: project, matching: schema.product) let app = ProjectProduct( packageProduct: appProduct, diff --git a/Sources/XToolSupport/SwiftPMBuildDescriber+XcodeProject.swift b/Sources/XToolSupport/SwiftPMBuildDescriber+XcodeProject.swift index f7a5f3ef..7785beb2 100644 --- a/Sources/XToolSupport/SwiftPMBuildDescriber+XcodeProject.swift +++ b/Sources/XToolSupport/SwiftPMBuildDescriber+XcodeProject.swift @@ -7,7 +7,7 @@ import XUtils extension SwiftPMBuildDescriber { func writeXcodeProject() async throws -> ProjectBuildResult { - try writeXcodeProject(basePIF: await d.swiftPMPIFDumper.dump(buildSettings: buildSettings)) + try await writeXcodeProject(basePIF: basePIF()) } // swiftlint:disable:next function_body_length diff --git a/Sources/XToolSupport/SwiftPMBuildDescriber.swift b/Sources/XToolSupport/SwiftPMBuildDescriber.swift index 06c0edef..68a7ca78 100644 --- a/Sources/XToolSupport/SwiftPMBuildDescriber.swift +++ b/Sources/XToolSupport/SwiftPMBuildDescriber.swift @@ -4,21 +4,23 @@ import Foundation import XUtils import Dependencies -public struct SwiftPMBuildDescriber { - let buildSettings: BuildSettings - let schema: PackSchema - let idSpecifier: PackSchema.IDSpecifier +struct SwiftPMBuildDescriber { + let project: XToolProject.SwiftPM + + var schema: PackSchema { project.base.schema } static let supportedPlatforms = ["macosx", "iphoneos", "iphonesimulator"] - public init(buildSettings: BuildSettings, schema: PackSchema) throws { - self.buildSettings = buildSettings - self.schema = schema - self.idSpecifier = schema.idSpecifier + init(project: XToolProject.SwiftPM) { + self.project = project + } + + func describe() async throws -> XPIF.Container { + try await describe(basePIF: basePIF()) } - func describe() async throws -> BuildDescription { - try describe(basePIF: await d.swiftPMPIFDumper.dump(buildSettings: buildSettings)) + func basePIF() async throws -> XPIF.Container { + try await d.swiftPMPIFDumper.dump(packagePath: project.manifest.deletingLastPathComponent()) } /// Generated dynamic variants remain in the graph, but are not separate manifest products. @@ -31,7 +33,7 @@ public struct SwiftPMBuildDescriber { return targets.filter { $0.id.value.hasPrefix("PACKAGE-PRODUCT:") && !variants.contains($0.id) } } - func describe(basePIF pif: XPIF.Container) throws -> BuildDescription { + func describe(basePIF pif: XPIF.Container) throws -> XPIF.Container { var container = pif #if !os(macOS) container.workspace.projects = container.workspace.projects.map { $0.withoutRelocatableLinkage() } @@ -65,7 +67,7 @@ public struct SwiftPMBuildDescriber { try project.addTarget { _ in target } } container.workspace.projects[mainProjectIndex] = project - return BuildDescription(pif: container, buildSettings: buildSettings) + return container } private func bundleTarget( diff --git a/Sources/XToolSupport/SwiftPMPIFDumper.swift b/Sources/XToolSupport/SwiftPMPIFDumper.swift index 2eb01b89..a997fcaa 100644 --- a/Sources/XToolSupport/SwiftPMPIFDumper.swift +++ b/Sources/XToolSupport/SwiftPMPIFDumper.swift @@ -9,27 +9,26 @@ struct SwiftPMPIFDumper: DependencyKey, Sendable { static var liveValue: Self { Self() } static var testValue: Self { liveValue } - func dump(buildSettings: BuildSettings) async throws -> XPIF.Container { - let dumpPIFInvocation = try await buildSettings.swiftPMInvocation( - forTool: "package", - arguments: ["dump-pif", "--experimental-build-dylibs-as-frameworks"], - ) - let result = try await Subprocess.run( - dumpPIFInvocation, + func dump(packagePath: URL) async throws -> XPIF.Container { + let pif = try await Subprocess.run( + .path(d.toolchain.swiftTool), + arguments: [ + "package", + "--package-path", packagePath.path, + "dump-pif", + "--experimental-build-dylibs-as-frameworks", + ], + environment: .inherit.updating([ + // xcrun passes an SDKROOT that messes with our sdk configuration + "SDKROOT": nil, + ]), + platformOptions: .withGracefulShutDown, input: .none, output: .data(limit: .max), - error: .sequence, - ) { execution in - for try await contents in execution.standardError { - @Dependency(\.out) var output - output.write( - contents.bytes.withUnsafeBytes { Data($0) }, - .stderr, - false - ) - } - } - let pif = try result.checkSuccess().standardOutput + error: .xtoolError(), + ) + .checkSuccess() + .standardOutput Signposter.track("Dumped PIF") diff --git a/Sources/XToolSupport/SwiftVersion.swift b/Sources/XToolSupport/SwiftVersion.swift index 7b5fbe83..d8c16084 100644 --- a/Sources/XToolSupport/SwiftVersion.swift +++ b/Sources/XToolSupport/SwiftVersion.swift @@ -61,32 +61,3 @@ extension SwiftVersion: LosslessStringConvertible, CustomStringConvertible { self.init(numericParts[0], numericParts[1], numericParts[2]) } } - -extension SwiftVersion { - private static let cached = Task { - let outputString: String? - do { - outputString = try await Subprocess.run( - .path(try await BuildSettings.swiftURL()), - arguments: ["--version"], - output: .string(limit: .max) - ) - .checkSuccess() - .standardOutput - } catch { - throw StringError("Failed to obtain Swift version") - } - - let output = outputString ?? "" - guard let version = Self(output) else { - throw StringError("Could not parse Swift version: '\(output)'") - } - return version - } - - public static var current: Self { - get async throws { - try await cached.value - } - } -} diff --git a/Sources/XToolSupport/Toolchain.swift b/Sources/XToolSupport/Toolchain.swift new file mode 100644 index 00000000..b2eac361 --- /dev/null +++ b/Sources/XToolSupport/Toolchain.swift @@ -0,0 +1,102 @@ +import XUtils +import Dependencies +import Foundation +import Subprocess + +public struct Toolchain: Sendable { + public var toolchainRoot: FilePath + public var swiftTool: FilePath + public var swiftVersion: SwiftVersion + public var runtimeResourcePath: FilePath +} + +public struct ToolchainProvider: DependencyKey, Sendable { + public var toolchain: @Sendable () async throws -> Toolchain + + public init(toolchain: @escaping @Sendable () async throws -> Toolchain) { + self.toolchain = toolchain + } + + public static var liveValue: Self { + Self { try await liveToolchain.value } + } + + public static var testValue: Self { + // allow tests to use the live Swift toolchain by default + liveValue + } + + fileprivate static let liveToolchain = Task { + #if os(macOS) + let swiftTool = FilePath( + try await Subprocess.run( + .path("/usr/bin/xcrun"), + arguments: ["-f", "swift"], + output: .string(limit: .max) + ) + .checkSuccess() + .standardOutput + .trimmingCharacters(in: .whitespacesAndNewlines) + ) + #else + let swiftTool = try await Executable.name("swift").resolveExecutablePath(in: .inherit) + #endif + + struct SwiftTargetInfo: Decodable { + var compilerVersion: String + struct Paths: Decodable { + var runtimeResourcePath: String + } + var paths: Paths + } + let targetInfo: SwiftTargetInfo + do { + let process = try await Subprocess.run( + .path(swiftTool), + arguments: ["-print-target-info"], + output: .data(limit: .max) + ).checkSuccess() + targetInfo = try JSONDecoder().decode( + SwiftTargetInfo.self, + from: process.standardOutput + ) + } catch { + throw Console.Error("Failed to query Swift compiler info: \(error)") + } + + let runtimeResourcePath = targetInfo.paths.runtimeResourcePath + let swiftVersion = try SwiftVersion(targetInfo.compilerVersion).orThrow( + Console.Error("Could not parse Swift version: '\(targetInfo.compilerVersion)'") + ) + + let trimmed: Substring + if runtimeResourcePath.hasSuffix("/usr/lib/swift") { + trimmed = runtimeResourcePath.dropLast("/usr/lib/swift".count) + } else if runtimeResourcePath.hasSuffix("/usr/local/lib/swift") { + trimmed = runtimeResourcePath.dropLast("/usr/local/lib/swift".count) + } else { + throw Console.Error("Unexpected toolchain resource path: \(runtimeResourcePath)") + } + let toolchainRoot = FilePath(trimmed.isEmpty ? "/" : String(trimmed)) + + return Toolchain( + toolchainRoot: toolchainRoot, + swiftTool: swiftTool, + swiftVersion: swiftVersion, + runtimeResourcePath: FilePath(runtimeResourcePath), + ) + } +} + +extension DependencyValues { + public var toolchainProvider: ToolchainProvider { + get { self[ToolchainProvider.self] } + set { self[ToolchainProvider.self] = newValue } + } + + public var toolchain: Toolchain { + get async throws { + try await toolchainProvider.toolchain() + } + } +} diff --git a/Sources/XToolSupport/XToolProject.swift b/Sources/XToolSupport/XToolProject.swift new file mode 100644 index 00000000..2b3b393c --- /dev/null +++ b/Sources/XToolSupport/XToolProject.swift @@ -0,0 +1,92 @@ +import Foundation +import XUtils +import XPIF + +enum XToolProject { + struct Base { + let rootDirectory: URL + let schema: PackSchema + } + + struct SwiftPM { + let base: Base + let manifest: URL + } + + case swiftPM(SwiftPM) + + var base: Base { + switch self { + case .swiftPM(let swiftPM): + return swiftPM.base + } + } + + static func discover(start directory: URL) async throws -> XToolProject { + // TODO: support running inside a child directory + let configPath = directory.appendingPathComponent("xtool.yml") + let hasSchema = FileManager.default.fileExists(atPath: configPath.path) + + let rootDirectory = configPath.deletingLastPathComponent() + + let schema: PackSchema = if hasSchema { + try await PackSchema(url: configPath) + } else { + .default + } + let base = Base(rootDirectory: rootDirectory, schema: schema) + let project = try await base.project() + if !hasSchema { + let defaultSchema: String + switch project { + case .swiftPM: + d.out.print(""" + warning: Could not locate configuration file '\(configPath.path)'. Creating a default \ + configuration with 'com.example' organization ID. + """) + defaultSchema = """ + version: 1 + orgID: com.example + """ + } + try Data((defaultSchema + "\n").utf8).write(to: configPath) + } + return project + } + + func tryEnsureLSPConfig(quiet: Bool = false) async { + await LSPConfig.tryEnsure(using: base.schema, quiet: quiet, currentDirectory: base.rootDirectory) + } + + func describe( + configuration: BuildConfiguration, + triple: String + ) async throws -> BuildDescription { + let pif: XPIF.Container + switch self { + case .swiftPM(let swiftPM): + pif = try await SwiftPMBuildDescriber(project: swiftPM).describe() + } + return BuildDescription( + directory: base.rootDirectory, + pif: pif, + configuration: configuration, + triple: triple, + ) + } +} + +extension XToolProject.Base { + fileprivate func project() async throws -> XToolProject { + let contents = try FileManager.default.contentsOfDirectory(at: rootDirectory, includingPropertiesForKeys: nil) + + if let manifest = contents.first(where: { + let name = $0.lastPathComponent + return name == "Package.swift" || (name.hasPrefix("Package@") && name.hasSuffix(".swift")) + }) { + return .swiftPM(.init(base: self, manifest: manifest)) + } + + throw Console.Error("Could not find Package.swift in this directory.") + } +} diff --git a/Tests/XToolTests/BuildCoordinatorTripleTests.swift b/Tests/XToolTests/BuildCoordinatorTripleTests.swift index cd0d8b3f..d0b124f2 100644 --- a/Tests/XToolTests/BuildCoordinatorTripleTests.swift +++ b/Tests/XToolTests/BuildCoordinatorTripleTests.swift @@ -21,7 +21,7 @@ struct BuildCoordinatorTripleTests { triple: String, arch: String, platform: String, variant: String?, deploymentSetting: String, deployment: String ) async throws { let directory = try TemporaryDirectory(name: "BuildCoordinatorTripleTests") - let description = try await description(triple: triple, root: directory.url) + let description = description(triple: triple, root: directory.url) let coordinator = try await withDependencies { $0.out = .liveValue } operation: { @@ -46,15 +46,14 @@ struct BuildCoordinatorTripleTests { @Test(.requiresXcode, arguments: ["arm64", "arm64-apple-ios-invalid-version", "arm64-apple-unrecognized"]) func rejectsInvalidTriples(triple: String) async throws { let directory = try TemporaryDirectory(name: "InvalidTripleTests") - let description = try await description(triple: triple, root: directory.url) + let description = description(triple: triple, root: directory.url) await #expect(throws: (any Error).self) { let coordinator = try await BuildCoordinator(description: description) await coordinator.close() } } - private func description(triple: String, root: URL) async throws -> BuildDescription { - let settings = try await BuildSettings(configuration: .debug, triple: triple) + private func description(triple: String, root: URL) -> BuildDescription { var projectSettings = XPIF.BuildSettings() projectSettings[.SDKROOT] = "auto" projectSettings[.IPHONEOS_DEPLOYMENT_TARGET] = "16.0" @@ -64,7 +63,7 @@ struct BuildCoordinatorTripleTests { buildConfigs: [.init(id: "debug", name: "Debug", settings: projectSettings)] ) let pif = XPIF.Container(workspace: .init(id: "workspace", name: "Package", path: root.path, projects: [project])) - return BuildDescription(pif: pif, buildSettings: settings) + return BuildDescription(directory: root, pif: pif, configuration: .debug, triple: triple) } } #endif diff --git a/Tests/XToolTests/DirectBuildDescriptionTests.swift b/Tests/XToolTests/DirectBuildDescriptionTests.swift index fa6ac420..5997a6f3 100644 --- a/Tests/XToolTests/DirectBuildDescriptionTests.swift +++ b/Tests/XToolTests/DirectBuildDescriptionTests.swift @@ -29,8 +29,8 @@ struct DirectBuildDescriptionTests { extensions: [.init(product: "Widget", bundleID: extensionBundleID, infoPath: "Widget.plist", resources: ["Assets", shared.path])] )) - let description = try await describe(schema: schema, root: root, products: ["App", "Widget"]) - let project = try #require(description.pif.workspace.projects.first) + let description = try describe(schema: schema, root: root, products: ["App", "Widget"]) + let project = try #require(description.workspace.projects.first) let app = try #require(project.targets.first { $0.id == "xtool:main-app" }) let widget = try #require(project.targets.first { $0.id == "xtool:extension:Widget" }) if case .target(let target) = widget { @@ -87,16 +87,16 @@ struct DirectBuildDescriptionTests { #expect(widgetPlist["CFBundleExecutable"] == nil) #expect(widgetPlist["CFBundlePackageType"] == nil) // Verify the build service receives the phases and references through PIF serialization. - let roundTrip = try XPIF.Container(data: description.pif.data()) + let roundTrip = try XPIF.Container(data: description.data()) expectNoDifference(roundTrip.workspace.projects.flatMap(\.targets).map(\.common.buildPhases), - description.pif.workspace.projects.flatMap(\.targets).map(\.common.buildPhases)) + description.workspace.projects.flatMap(\.targets).map(\.common.buildPhases)) } @Test func delegatesPlistDefaultsToSwiftBuild() async throws { let root = try temporaryDirectory() defer { try? FileManager.default.removeItem(at: root) } - let description = try await describe(schema: .default, root: root, products: ["App"]) - let app = try #require(description.pif.workspace.projects.first?.targets.first { $0.id == "xtool:main-app" }) + let description = try describe(schema: .default, root: root, products: ["App"]) + let app = try #require(description.workspace.projects.first?.targets.first { $0.id == "xtool:main-app" }) let settings = try #require(app.common.buildConfigs.first?.settings) #expect(settings[.INFOPLIST_FILE] == nil) expectNoDifference(settings[.GENERATE_INFOPLIST_FILE], "YES") @@ -114,8 +114,7 @@ struct DirectBuildDescriptionTests { ]) } - private func describe(schema: PackSchema, root: URL, products: [String]) async throws -> BuildDescription { - let settings = try await BuildSettings(configuration: .debug, triple: "arm64-apple-ios", packagePath: root.path) + private func describe(schema: PackSchema, root: URL, products: [String]) throws -> XPIF.Container { let project = XPIF.Project( id: "package", path: root.appending(path: "Package.swift").path, projectDir: root.path, name: "Package", targets: products.map { name in @@ -124,7 +123,11 @@ struct DirectBuildDescriptionTests { } ) let pif = XPIF.Container(workspace: .init(id: "workspace", name: "Package", path: root.path, projects: [project])) - return try SwiftPMBuildDescriber(buildSettings: settings, schema: schema).describe(basePIF: pif) + let swiftPM = XToolProject.SwiftPM( + base: .init(rootDirectory: root, schema: schema), + manifest: root.appending(path: "Package.swift") + ) + return try SwiftPMBuildDescriber(project: swiftPM).describe(basePIF: pif) } private func copyPhases(_ target: XPIF.BaseTarget) -> [XPIF.CopyFilesBuildPhase] { diff --git a/Tests/XToolTests/SwiftPMBuildDescriberTests.swift b/Tests/XToolTests/SwiftPMBuildDescriberTests.swift index 242487fe..4e5c3513 100644 --- a/Tests/XToolTests/SwiftPMBuildDescriberTests.swift +++ b/Tests/XToolTests/SwiftPMBuildDescriberTests.swift @@ -32,7 +32,7 @@ struct SwiftPMBuildDescriberTests { entitlementsPath: root.appending(path: "Widget.entitlements").path), ], )) - let describer = try await describer(schema: schema, root: root) + let describer = describer(schema: schema, root: root) let result = try describer.writeXcodeProject(basePIF: pif(root: root, products: ["App", "Widget"])) #expect(result.bundleID == "org.example.custom") #expect(result.url == root.appending(path: "xtool/App.xcworkspace")) @@ -108,9 +108,9 @@ struct SwiftPMBuildDescriberTests { let result = try await withDependencies { $0.out = .liveValue } operation: { - let describer = try await describer(schema: .default, root: root) + let describer = describer(schema: .default, root: root) let description = try await describer.describe() - let app = try #require(description.pif.workspace.projects.flatMap(\.targets).first { $0.id == "xtool:main-app" }) + let app = try #require(description.workspace.projects.flatMap(\.targets).first { $0.id == "xtool:main-app" }) expectNoDifference(app.common.name, "Sample-App") return try await describer.writeXcodeProject() } @@ -128,7 +128,7 @@ struct SwiftPMBuildDescriberTests { let root = try temporaryDirectory() defer { try? FileManager.default.removeItem(at: root) } let schema = try PackSchema(validating: .init(version: .v1, orgID: "com.example", product: expected)) - let describer = try await describer(schema: schema, root: root) + let describer = describer(schema: schema, root: root) var pif = pif(root: root, products: []) var product = XPIF.Target(id: "PACKAGE-PRODUCT:App", productType: .packageProduct, name: "App-product", productName: "App") @@ -138,7 +138,7 @@ struct SwiftPMBuildDescriberTests { // Put the variant first so explicit product selection cannot accidentally pick it. pif.workspace.projects[0].targets = [.target(variant), .target(product)] let description = try describer.describe(basePIF: pif) - let targets = description.pif.workspace.projects[0].targets + let targets = description.workspace.projects[0].targets expectNoDifference(Array(targets.prefix(2)), pif.workspace.projects[0].targets) let app = try #require(targets.first { $0.id == "xtool:main-app" }) expectNoDifference(app.common.dependencies.map(\.targetId), [product.id]) @@ -154,14 +154,14 @@ struct SwiftPMBuildDescriberTests { let dylib = XPIF.Target(id: "PACKAGE-PRODUCT:Dylib", productType: .dynamicLibrary, name: "Dylib-product", productName: "Dylib") pif.workspace.projects[0].targets.append(contentsOf: [.target(other), .target(dylib)]) - let automatic = try await self.describer(schema: .default, root: root) + let automatic = self.describer(schema: .default, root: root) let descriptionWithDependencies = try automatic.describe(basePIF: pif) - expectNoDifference(Array(descriptionWithDependencies.pif.workspace.projects[0].targets.prefix(4)), + expectNoDifference(Array(descriptionWithDependencies.workspace.projects[0].targets.prefix(4)), pif.workspace.projects[0].targets) expectNoDifference(try automatic.writeXcodeProject(basePIF: pif).bundleID, "com.example.App") for name in ["Other", "Dylib"] { let schema = try PackSchema(validating: .init(version: .v1, orgID: "com.example", product: name)) - let explicit = try await self.describer(schema: schema, root: root) + let explicit = self.describer(schema: schema, root: root) #expect(throws: Console.Error.self) { try explicit.describe(basePIF: pif) } #expect(throws: Console.Error.self) { try explicit.writeXcodeProject(basePIF: pif) } } @@ -174,7 +174,7 @@ struct SwiftPMBuildDescriberTests { func rejectsMissingOrAmbiguousProducts(count: Int) async throws { let root = try temporaryDirectory() defer { try? FileManager.default.removeItem(at: root) } - let describer = try await describer(schema: .default, root: root) + let describer = describer(schema: .default, root: root) let pif = pif(root: root, products: (0.. SwiftPMBuildDescriber { - let settings = try await BuildSettings( - configuration: .debug, triple: "arm64-apple-ios", packagePath: root.path - ) - return try SwiftPMBuildDescriber(buildSettings: settings, schema: schema) + private func describer(schema: PackSchema, root: URL) -> SwiftPMBuildDescriber { + SwiftPMBuildDescriber(project: .init( + base: .init(rootDirectory: root, schema: schema), + manifest: root.appending(path: "Package.swift") + )) } private func pif(root: URL, products: [String]) -> XPIF.Container { From 278e4d3af51bcba4f7222a30e73d13fbb90bcfb8 Mon Sep 17 00:00:00 2001 From: Kabir Oberai Date: Wed, 21 Oct 2026 18:21:46 -0400 Subject: [PATCH 07/15] bsp: add SwiftBuild-based bsp --- .../XToolSupport/BSPCommandTransport.swift | 90 +++++ Sources/XToolSupport/DevBSPCommand.swift | 17 +- .../SWBBuildRequest+Indexing.swift | 35 ++ .../XToolSupport/SwiftPMBuildDescriber.swift | 1 + Sources/XToolSupport/XPIF+Utils.swift | 37 ++ Sources/XToolSupport/XTool.swift | 13 +- Sources/XToolSupport/XToolBuildServer.swift | 371 ++++++++++++++++++ .../XToolTests/BSPCommandTransportTests.swift | 120 ++++++ .../BuildServerInitializationTests.swift | 124 ++++++ .../XToolTests/BuildServerTargetsTests.swift | 35 ++ Tests/XToolTests/IndexBuildArenaTests.swift | 55 +++ .../SwiftPMBuildDescriberTests.swift | 14 + 12 files changed, 910 insertions(+), 2 deletions(-) create mode 100644 Sources/XToolSupport/BSPCommandTransport.swift create mode 100644 Sources/XToolSupport/SWBBuildRequest+Indexing.swift create mode 100644 Sources/XToolSupport/XPIF+Utils.swift create mode 100644 Sources/XToolSupport/XToolBuildServer.swift create mode 100644 Tests/XToolTests/BSPCommandTransportTests.swift create mode 100644 Tests/XToolTests/BuildServerInitializationTests.swift create mode 100644 Tests/XToolTests/BuildServerTargetsTests.swift create mode 100644 Tests/XToolTests/IndexBuildArenaTests.swift diff --git a/Sources/XToolSupport/BSPCommandTransport.swift b/Sources/XToolSupport/BSPCommandTransport.swift new file mode 100644 index 00000000..540d96b6 --- /dev/null +++ b/Sources/XToolSupport/BSPCommandTransport.swift @@ -0,0 +1,90 @@ +import ConcurrencyExtras +import Foundation +import BuildServerProtocol +import LanguageServerProtocol +import LanguageServerProtocolTransport +import XCLIUtils +import XUtils + +/// Bridges the descriptor-based JSON-RPC connection to task-scoped command streams. +final class BSPCommandTransport: Sendable { + let connection: JSONRPCConnection + private let inputPipe = Pipe() + private let outputPipe = Pipe() + + // Capture the streams before server diagnostics are redirected to stderr. + private let input = d.input + private let output = d.out + + init() { + connection = JSONRPCConnection( + name: "client", + protocol: MessageRegistry.bspProtocol, + receiveFD: inputPipe.fileHandleForReading, + sendFD: outputPipe.fileHandleForWriting + ) + } + + func run(receiveHandler: any MessageHandler) async throws { + let input = self.input + let output = self.output + let inputHandle = inputPipe.fileHandleForWriting + let outputHandle = outputPipe.fileHandleForReading + let inputQueue = DispatchQueue(label: "bsp-command-input") + let outputQueue = DispatchQueue(label: "bsp-command-output") + let outputTask = Task { + await withCheckedContinuation { continuation in + outputQueue.async { + defer { continuation.resume() } + while true { + // Forward promptly instead of waiting to fill a fixed-size read. + let data = outputHandle.availableData + guard !data.isEmpty else { break } + output.write(data, .stdout, false) + } + } + } + } + let (closed, continuation) = AsyncStream.makeStream(of: Never.self) + connection.start(receiveHandler: receiveHandler, closeHandler: { continuation.finish() }) + let inputError = LockIsolated<(any Error)?>(nil) + let inputTask = Task { + defer { try? inputHandle.close() } + do { + while let data = try await input.read() { + try Task.checkCancellation() + // Pipe writes can block; keep them off the cooperative executor. + try await withCheckedThrowingContinuation { (continuation: CheckedContinuation) in + inputQueue.async { + do { + try inputHandle.write(contentsOf: data) + continuation.resume() + } catch { + continuation.resume(throwing: error) + } + } + } + } + } catch where Task.isCancelled { + // The client exited or the command was cancelled. + } catch { + inputError.setValue(error) + } + } + defer { + connection.close() + inputTask.cancel() + await inputTask.value + await outputTask.value + try? outputHandle.close() + } + try await withTaskCancellationHandler { + try Task.checkCancellation() + for await _ in closed {} + try Task.checkCancellation() + if let error = inputError.value { throw error } + } onCancel: { + self.connection.close() + } + } +} diff --git a/Sources/XToolSupport/DevBSPCommand.swift b/Sources/XToolSupport/DevBSPCommand.swift index afcf9eb6..eb0f4db7 100644 --- a/Sources/XToolSupport/DevBSPCommand.swift +++ b/Sources/XToolSupport/DevBSPCommand.swift @@ -1,5 +1,7 @@ import XCLIUtils import ArgumentParser +import XUtils +import Dependencies struct DevBSPCommand: AsyncParsableCommand { static let configuration = CommandConfiguration( @@ -11,6 +13,19 @@ struct DevBSPCommand: AsyncParsableCommand { var triple: String? func run() async throws { - throw Console.Error("Stub") + let transport = BSPCommandTransport() + // Only BSP frames may be written to the client's stdout. + try await withDependencies { + let output = $0.out + $0.out = CommandOutput { data, _, flush in output.write(data, .stderr, flush) } + } operation: { + let server = XToolBuildServer( + triple: triple ?? PackOperation.defaultTriple, + connectionToClient: transport.connection, + exitHandler: { _ in transport.connection.close() } + ) + defer { await server.close() } + try await transport.run(receiveHandler: server) + } } } diff --git a/Sources/XToolSupport/SWBBuildRequest+Indexing.swift b/Sources/XToolSupport/SWBBuildRequest+Indexing.swift new file mode 100644 index 00000000..4377d3b1 --- /dev/null +++ b/Sources/XToolSupport/SWBBuildRequest+Indexing.swift @@ -0,0 +1,35 @@ +import Foundation +import SwiftBuild + +extension SWBBuildRequest { + /// Index preparation links host tools directly into Products. Normal install builds put + /// symlinks there instead, so the two actions must have separate build arenas. + func withIndexBuildArena() -> Self { + var request = self + request.parameters.arenaInfo = parameters.arenaInfo?.forIndexing() + request.configuredTargets = configuredTargets.map { target in + var target = target + if let parameters = target.parameters { + target.parameters?.arenaInfo = parameters.arenaInfo?.forIndexing() + } + return target + } + return request + } +} + +private extension SWBArenaInfo { + func forIndexing() -> Self { + guard indexRegularBuildProductsPath == nil else { return self } + var arena = self + let indexDirectory = URL(filePath: derivedDataPath).appending(path: "Index.noindex") + let buildDirectory = indexDirectory.appending(path: "Build") + arena.indexRegularBuildProductsPath = buildProductsPath + arena.indexRegularBuildIntermediatesPath = buildIntermediatesPath + arena.buildProductsPath = buildDirectory.appending(path: "Products").path + arena.buildIntermediatesPath = buildDirectory.appending(path: "Intermediates.noindex").path + arena.pchPath = buildDirectory.appending(path: "Intermediates.noindex/PrecompiledHeaders").path + arena.indexPCHPath = indexDirectory.appending(path: "PrecompiledHeaders").path + return arena + } +} diff --git a/Sources/XToolSupport/SwiftPMBuildDescriber.swift b/Sources/XToolSupport/SwiftPMBuildDescriber.swift index 68a7ca78..3cfc986f 100644 --- a/Sources/XToolSupport/SwiftPMBuildDescriber.swift +++ b/Sources/XToolSupport/SwiftPMBuildDescriber.swift @@ -67,6 +67,7 @@ struct SwiftPMBuildDescriber { try project.addTarget { _ in target } } container.workspace.projects[mainProjectIndex] = project + try container.workspace.addUmbrellaTarget(toProjectAt: mainProjectIndex) return container } diff --git a/Sources/XToolSupport/XPIF+Utils.swift b/Sources/XToolSupport/XPIF+Utils.swift new file mode 100644 index 00000000..cbf14e2c --- /dev/null +++ b/Sources/XToolSupport/XPIF+Utils.swift @@ -0,0 +1,37 @@ +import XPIF + +extension XPIF.Container.Workspace { + static let umbrellaTargetGUID: XPIF.GUID = "xtool:UMBRELLA" + + mutating func addUmbrellaTarget(toProjectAt projectIndex: Int) throws { + // SwiftBuild's BSP impl allows for mutable PIF but requires an immutable + // SWBBuildRequest. So we construct an SWBBuildRequest that points to this + // umbrella target. + // + // So if the user adds a new target, although we can't mutate the build + // request, we can add the new target to the umbrella and have it covered. + let umbrellaTarget = XPIF.AggregateTarget( + id: Self.umbrellaTargetGUID, + name: "Umbrella Target", + buildConfigs: ["Debug", "Release"].map { configuration in + XPIF.BuildConfig( + id: "\(Self.umbrellaTargetGUID):\(configuration)", + name: configuration, + settings: XPIF.BuildSettings(), + ) + }, + dependencies: indexingTargets.map { XPIF.TargetDependency(targetId: $0.id) } + ) + try projects[projectIndex].addAggregateTarget { _ in umbrellaTarget } + } + + var indexingTargets: [XPIF.BaseTarget] { + let targets = self.projects.flatMap(\.targets) + let dynamicVariants = Set(targets.compactMap { target -> XPIF.GUID? in + guard case .target(let target) = target else { return nil } + return target.dynamicTargetVariantId + }) + // including the dynamic variants in the umbrella causes duplicate-target collisions + return targets.filter { !dynamicVariants.contains($0.id) } + } +} diff --git a/Sources/XToolSupport/XTool.swift b/Sources/XToolSupport/XTool.swift index f6677872..29625514 100644 --- a/Sources/XToolSupport/XTool.swift +++ b/Sources/XToolSupport/XTool.swift @@ -4,10 +4,11 @@ import XKit import ArgumentParser import XUtils import Dependencies +@_spi(SourceKitLSP) import SKLogging public enum XTool { public static func run(arguments: [String]? = nil) async throws { - Signposter.setup() + configureLogging() TemporaryDirectory.prepare() await XToolCommand.cancellableMain(arguments) } @@ -92,3 +93,13 @@ extension ParsableCommand where Self: SendableMetatype { await task.value } } + +extension XTool { + fileprivate static func configureLogging() { + // SKLogging (for swift-tools-protocols) + LoggingScope.configureDefaultLoggingSubsystem("sh.xtool") + LogConfig.logLevel = .default + // xtool + Signposter.setup() + } +} diff --git a/Sources/XToolSupport/XToolBuildServer.swift b/Sources/XToolSupport/XToolBuildServer.swift new file mode 100644 index 00000000..8d0c2a0a --- /dev/null +++ b/Sources/XToolSupport/XToolBuildServer.swift @@ -0,0 +1,371 @@ +import XPIF +import Foundation +import Subprocess +import SwiftBuild +import BuildServerProtocol +import LanguageServerProtocol +import LanguageServerProtocolTransport +import ToolsProtocolsSwiftExtensions +import XCLIUtils +import XUtils + +// Based on SwiftPMBuildServer.swift in swift-package-manager. +// License: https://github.com/swiftlang/swift-package-manager/blob/main/LICENSE.txt + +actor XToolBuildServer: QueueBasedMessageHandler { + let messageHandlingHelper = QueueBasedMessageHandlerHelper( + signpostLoggingCategory: "xtool-build-server-message-handling", + createLoggingScope: false + ) + let messageHandlingQueue = AsyncQueue() + + private let connectionToUnderlyingBuildServer: LocalConnection + private let connectionFromUnderlyingBuildServer: LocalConnection + private let connectionToClient: any LanguageServerProtocol.Connection + private var coordinator: BuildCoordinator? + private let triple: String + private let exitHandler: @Sendable (Int) -> Void + private var packageRoot: URL? + private var headersByTargetGUID: [String: Set] = [:] + private enum State { + case awaitingInitialize, initializing, initialized, running, shutDown, failed, closed + } + private var state = State.awaitingInitialize + + init( + triple: String, + connectionToClient: any LanguageServerProtocol.Connection, + exitHandler: @escaping @Sendable (Int) -> Void + ) { + self.triple = triple + self.exitHandler = exitHandler + self.connectionToClient = connectionToClient + self.connectionToUnderlyingBuildServer = LocalConnection(receiverName: "underlying-swift-build-server") + self.connectionFromUnderlyingBuildServer = LocalConnection(receiverName: "xtool-build-server") + } + + private func initialize(_ request: InitializeBuildRequest) async throws -> InitializeBuildResponse { + guard state == .awaitingInitialize else { + throw ResponseError.unknown("Build server has already been initialized") + } + guard let directory = request.rootUri.fileURL else { + throw ResponseError.invalidParams("rootUri must be a file URI") + } + state = .initializing + do { + guard try await d.toolchain.swiftVersion.supportsSwiftBuild else { + throw Console.Error("`xtool dev build-server` requires Swift 6.4 or later") + } + let project = try await XToolProject.discover(start: directory) + let description = try await project.describe(configuration: .debug, triple: triple) + let pif = description.pif + let coordinator = try await BuildCoordinator(description: description) + // The transport may have closed while the coordinator was being created. + guard state == .initializing else { + await coordinator.close() + throw CancellationError() + } + self.coordinator = coordinator + packageRoot = URL(filePath: pif.workspace.path) + headersByTargetGUID = Self.headerMapping(in: pif) + var buildRequest = coordinator.buildRequest + buildRequest.configuredTargets = [SWBConfiguredTarget(guid: XPIF.Workspace.umbrellaTargetGUID.value)] + buildRequest = buildRequest.withIndexBuildArena() + let server = SWBBuildServer( + session: coordinator.sessionManager.session, + buildRequest: buildRequest, + connectionToClient: connectionFromUnderlyingBuildServer, + exitHandler: exitHandler + ) + connectionToUnderlyingBuildServer.start(handler: server) + connectionFromUnderlyingBuildServer.start(handler: BuildServerClient(connection: connectionToClient)) + var response = try await connectionToUnderlyingBuildServer.send(request) + guard state == .initializing else { throw CancellationError() } + response.displayName = "xtool Build Server" + state = .initialized + return response + } catch { + if state == .initializing { state = .failed } + throw error + } + } + + func close() async { + guard state != .closed else { return } + state = .closed + connectionToUnderlyingBuildServer.close() + connectionFromUnderlyingBuildServer.close() + await coordinator?.close() + coordinator = nil + } + + func handle(notification: some NotificationType) async { + switch notification { + case is OnBuildInitializedNotification: + guard state == .initialized else { return } + connectionToUnderlyingBuildServer.send(notification) + state = .running + case is OnBuildExitNotification: + guard state != .closed else { return } + state = .shutDown + if packageRoot != nil { + connectionToUnderlyingBuildServer.send(notification) + } else { + exitHandler(0) + } + case is OnWatchedFilesDidChangeNotification: + // The PIF and header mapping are snapshots taken when the server starts. + break + default: + guard state == .running else { return } + connectionToUnderlyingBuildServer.send(notification) + } + } + + func handle( + request: Request, + id: RequestID, + reply: @Sendable @escaping (Result) -> Void + ) async { + let request = RequestAndReply(request, reply: reply) + guard state == .running || request.params is InitializeBuildRequest else { + await request.reply { + throw ResponseError(code: .serverNotInitialized, message: "Build server is not running") + } + return + } + switch request { + case let request as RequestAndReply: + await request.reply { try await initialize(request.params) } + case let request as RequestAndReply: + await request.reply { + let response = try await connectionToUnderlyingBuildServer.send(request.params) + state = .shutDown + return response + } + case let request as RequestAndReply: + await request.reply { + var response: WorkspaceBuildTargetsResponse + do { + response = try await connectionToUnderlyingBuildServer.send(request.params) + } catch { + logUnderlyingError(error) + response = WorkspaceBuildTargetsResponse(targets: []) + } + response.targets.append(BuildTarget( + id: .xtoolPackageManifest, + displayName: "Package Manifest", + tags: [.notBuildable], + languageIds: [.swift], + dependencies: [] + )) + return response + } + case let request as RequestAndReply: + await request.reply { try await sources(request.params) } + case let request as RequestAndReply: + await request.reply { + var params = request.params + params.targets.removeAll { $0 == .xtoolPackageManifest } + guard !params.targets.isEmpty else { return BuildTargetPrepareResponse() } + return try await connectionToUnderlyingBuildServer.send(params) + } + case let request as RequestAndReply: + await request.reply { + if request.params.target == .xtoolPackageManifest { + return try await manifestOptions(request.params) + } + if let guid = request.params.target.xtoolTargetGUID, + let url = request.params.textDocument.uri.fileURL, + headersByTargetGUID[guid]?.contains(where: { + $0.resolvingSymlinksInPath().path == url.resolvingSymlinksInPath().path + }) == true, + let options = try await headerOptions(request.params) { + return options + } + return try await connectionToUnderlyingBuildServer.send(request.params) + } + default: + await request.reply { try await connectionToUnderlyingBuildServer.send(request.params) } + } + } + + private func sources(_ request: BuildTargetSourcesRequest) async throws -> BuildTargetSourcesResponse { + var params = request + params.targets.removeAll { $0 == .xtoolPackageManifest } + var response = BuildTargetSourcesResponse(items: []) + if !params.targets.isEmpty { + do { + response = try await connectionToUnderlyingBuildServer.send(params) + } catch { + guard request.targets.contains(.xtoolPackageManifest) else { throw error } + logUnderlyingError(error) + } + } + for index in response.items.indices { + guard let guid = response.items[index].target.xtoolTargetGUID else { continue } + let existing = Set(response.items[index].sources.map(\.uri)) + for header in (headersByTargetGUID[guid] ?? []).sorted(by: { $0.path < $1.path }) { + let uri = DocumentURI(header) + guard !existing.contains(uri) else { continue } + response.items[index].sources.append(SourceItem( + uri: uri, + kind: .file, + generated: false, + dataKind: .sourceKit, + data: SourceKitSourceItemData(kind: .header).encodeToLSPAny() + )) + } + } + if request.targets.contains(.xtoolPackageManifest) { + guard let packageRoot else { throw ResponseError.unknown("Build server is not initialized") } + let manifests = try FileManager.default.contentsOfDirectory(at: packageRoot, includingPropertiesForKeys: nil) + .filter { Self.isManifest($0.lastPathComponent) } + .sorted { $0.path < $1.path } + response.items.append(SourcesItem( + target: .xtoolPackageManifest, + sources: manifests.map { SourceItem(uri: DocumentURI($0), kind: .file, generated: false) } + )) + } + return response + } + + private func logUnderlyingError(_ error: any Error) { + connectionToClient.send(OnBuildLogMessageNotification( + type: .warning, + message: "Underlying SwiftBuild server reported an error: \(error)" + )) + } + + static func isManifest(_ name: String) -> Bool { + name == "Package.swift" || name.wholeMatch(of: #/^Package@swift-\d+(?:\.\d+)?(?:\.\d+)?\.swift$/#) != nil + } + + private func manifestOptions( + _ request: TextDocumentSourceKitOptionsRequest + ) async throws -> TextDocumentSourceKitOptionsResponse { + guard let packageRoot, let url = request.textDocument.uri.fileURL, + url.deletingLastPathComponent().resolvingSymlinksInPath().path == packageRoot.resolvingSymlinksInPath().path, + Self.isManifest(url.lastPathComponent) else { + throw ResponseError.unknown("Unknown package manifest: \(request.textDocument.uri)") + } + let contents = try String(contentsOf: url, encoding: .utf8) + guard let version = contents.firstMatch(of: #/swift-tools-version:\s*(\d+)\.(\d+)(?:\.(\d+))?/#) else { + throw ResponseError.unknown("Missing swift-tools-version in \(url.path)") + } + let major = Int(version.1) ?? 6 + let languageVersion = major >= 6 ? "6" : major == 5 ? "5" : (Int(version.2) ?? 0) < 2 ? "4" : "4.2" + let toolsVersion = "\(version.1).\(version.2).\(version.3 ?? "0")" + // Manifests run on the host, independently of the app's iOS SDK and triple. + let modules = try await d.toolchain.runtimeResourcePath.appending("pm/ManifestAPI") + var arguments = ["-swift-version", languageVersion, "-I", modules.string] + #if os(macOS) + let sdk = try await Subprocess.run( + .path("/usr/bin/xcrun"), + arguments: ["--sdk", "macosx", "--show-sdk-path"], + output: .string(limit: .max) + ).checkSuccess().standardOutput.trimmingCharacters(in: .whitespacesAndNewlines) + arguments += ["-sdk", sdk] + #endif + arguments += ["-package-description-version", toolsVersion, url.path] + return TextDocumentSourceKitOptionsResponse(compilerArguments: arguments, workingDirectory: packageRoot.path) + } + + private func headerOptions( + _ request: TextDocumentSourceKitOptionsRequest + ) async throws -> TextDocumentSourceKitOptionsResponse? { + let response = try await connectionToUnderlyingBuildServer.send(BuildTargetSourcesRequest(targets: [request.target])) + let source = response.items.flatMap(\.sources).first { + guard $0.kind == .file else { return false } + switch SourceKitSourceItemData(fromLSPAny: $0.data)?.language { + case .c, .cpp, .objective_c, .objective_cpp: return true + case nil: return ["c", "cc", "cp", "cpp", "cxx", "c++", "C", "m", "mm", "M"] + .contains($0.uri.fileURL?.pathExtension ?? "") + default: return false + } + } + guard let source, let sourceURL = source.uri.fileURL, let headerURL = request.textDocument.uri.fileURL else { return nil } + let substitute = TextDocumentSourceKitOptionsRequest( + textDocument: TextDocumentIdentifier(source.uri), + target: request.target, + language: request.language + ) + guard var options = try await connectionToUnderlyingBuildServer.send(substitute), + let index = options.compilerArguments.lastIndex(where: { + $0.hasSuffix(sourceURL.lastPathComponent) && sourceURL.path.hasSuffix($0) + }) else { return nil } + options.compilerArguments[index] = headerURL.path + return options + } + + static func headerMapping(in pif: XPIF.Container) -> [String: Set] { + var result: [String: Set] = [:] + for project in pif.workspace.projects { + var files: [XPIF.GUID: URL] = [:] + func collect(_ reference: XPIF.Reference, parent: URL) { + let common: XPIF.ReferenceCommon + switch reference { + case .file(let file): common = file.common + case .group(let group): common = group.common + } + let base: URL + switch common.pathBase { + case .absolute: base = URL(filePath: "/") + case .groupDir: base = parent + case .buildSetting("SOURCE_ROOT"): base = URL(filePath: project.projectDir) + case .buildSetting: return + } + let url = base.appending(path: common.path) + switch reference { + case .file: files[common.id] = url + case .group(let group): group.subitems.forEach { collect($0, parent: url) } + } + } + collect(.group(project.mainGroup), parent: URL(filePath: project.projectDir)) + for target in project.targets { + for phase in target.common.buildPhases { + guard case .headers(let phase) = phase else { continue } + for file in phase.common.files { + guard case .reference(let id) = file.ref, let url = files[id] else { continue } + result[target.id.value, default: []].insert(url) + } + } + } + } + return result + } +} + +private extension BuildTargetIdentifier { + static let xtoolPackageManifest = BuildTargetIdentifier(uri: URI(URL(string: "xtool://package-manifest")!)) + + var xtoolTargetGUID: String? { + URLComponents(url: uri.arbitrarySchemeURL, resolvingAgainstBaseURL: false)? + .queryItems?.last { $0.name == "targetGUID" }?.value + } +} + +private actor BuildServerClient: QueueBasedMessageHandler { + let messageHandlingHelper = QueueBasedMessageHandlerHelper( + signpostLoggingCategory: "underlying-build-server-message-handling", + createLoggingScope: false + ) + let messageHandlingQueue = AsyncQueue() + let connection: any LanguageServerProtocol.Connection + + init(connection: any LanguageServerProtocol.Connection) { + self.connection = connection + } + + func handle(notification: some NotificationType) async { + connection.send(notification) + } + + func handle( + request: Request, + id: RequestID, + reply: @Sendable @escaping (Result) -> Void + ) async { + await RequestAndReply(request, reply: reply).reply { throw ResponseError.methodNotFound(Request.method) } + } +} diff --git a/Tests/XToolTests/BSPCommandTransportTests.swift b/Tests/XToolTests/BSPCommandTransportTests.swift new file mode 100644 index 00000000..4ff68d41 --- /dev/null +++ b/Tests/XToolTests/BSPCommandTransportTests.swift @@ -0,0 +1,120 @@ +import BuildServerProtocol +import Dependencies +import Foundation +import LanguageServerProtocol +import LanguageServerProtocolTransport +import Testing +import ToolsProtocolsSwiftExtensions +import XCLIUtils +import XUtils +@testable import XToolSupport + +struct BSPCommandTransportTests { + @Test(.timeLimit(.minutes(1))) + func fragmentedInputAndOutputThroughCommandStreams() async throws { + let input = DataChannel() + let (output, continuation) = AsyncStream.makeStream(of: Data.self) + let transport = withDependencies { + $0.input = CommandInput(channel: input) + $0.out.write = { data, stream, _ in + #expect(stream == .stdout) + continuation.yield(data) + } + } operation: { BSPCommandTransport() } + let handler = TestBSPHandler(connection: transport.connection) + let task = Task { try await transport.run(receiveHandler: handler) } + let request = frame(#"{"jsonrpc":"2.0","id":1,"method":"build/shutdown"}"#) + for byte in request { input.write(Data([byte])) } + var received = Data() + for await chunk in output { + received.append(chunk) + guard let separator = received.range(of: Data("\r\n\r\n".utf8)) else { continue } + let header = String(decoding: received[..= length else { continue } + let response = try #require(JSONSerialization.jsonObject(with: body) as? [String: Any]) + #expect(response["id"] as? Int == 1) + #expect(response["result"] != nil) + break + } + input.write(frame(#"{"jsonrpc":"2.0","method":"build/exit"}"#)) + // BSP exit must finish even if the client's input stream remains open. + try await task.value + } + + @Test(.timeLimit(.minutes(1)), arguments: [false, true]) + func cancellationUnblocksInput(beforeStart: Bool) async throws { + let input = DataChannel() + let (reading, continuation) = AsyncStream.makeStream(of: Never.self) + let transport = withDependencies { + $0.input = CommandInput(read: { limit in + continuation.finish() + return try await input.read(upTo: limit) + }) + $0.out.write = { _, _, _ in Issue.record("Unexpected BSP output") } + } operation: { BSPCommandTransport() } + let task = Task { + if beforeStart { withUnsafeCurrentTask { $0?.cancel() } } + try await transport.run(receiveHandler: TestBSPHandler(connection: transport.connection)) + } + if !beforeStart { + for await _ in reading {} + task.cancel() + } + await #expect(throws: CancellationError.self) { try await task.value } + } + + @Test(.timeLimit(.minutes(1))) + func inputEOFAndErrorsCloseConnection() async throws { + for fails in [false, true] { + let transport = withDependencies { + $0.input = CommandInput(read: { _ in + if fails { throw InputError() } + return nil + }) + $0.out.write = { _, _, _ in Issue.record("Unexpected BSP output") } + } operation: { BSPCommandTransport() } + let handler = TestBSPHandler(connection: transport.connection) + if fails { + await #expect(throws: InputError.self) { try await transport.run(receiveHandler: handler) } + } else { + try await transport.run(receiveHandler: handler) + } + } + } + + private struct InputError: Error {} + + private func frame(_ json: String) -> Data { + Data("Content-Length: \(json.utf8.count)\r\n\r\n\(json)".utf8) + } +} + +private actor TestBSPHandler: QueueBasedMessageHandler { + let messageHandlingHelper = QueueBasedMessageHandlerHelper( + signpostLoggingCategory: "test-bsp-handler", + createLoggingScope: false + ) + let messageHandlingQueue = AsyncQueue() + let connection: JSONRPCConnection + + init(connection: JSONRPCConnection) { self.connection = connection } + + func handle(notification: some NotificationType) async { + if notification is OnBuildExitNotification { connection.close() } + } + + func handle( + request: Request, + id: RequestID, + reply: @escaping @Sendable (Result) -> Void + ) async { + await RequestAndReply(request, reply: reply).reply { + guard request is BuildShutdownRequest, let response = VoidResponse() as? Request.Response else { + throw ResponseError.methodNotFound(Request.method) + } + return response + } + } +} diff --git a/Tests/XToolTests/BuildServerInitializationTests.swift b/Tests/XToolTests/BuildServerInitializationTests.swift new file mode 100644 index 00000000..44e6bc5d --- /dev/null +++ b/Tests/XToolTests/BuildServerInitializationTests.swift @@ -0,0 +1,124 @@ +import BuildServerProtocol +import ConcurrencyExtras +import CustomDump +import Dependencies +import Foundation +import LanguageServerProtocol +import LanguageServerProtocolTransport +import Testing +import XUtils +@testable import XToolSupport + +struct BuildServerInitializationTests { + @Test + func uninitializedServerDoesNotCreateACoordinatorAndCanExit() async throws { + let client = LocalConnection(receiverName: "test client") + defer { client.close() } + let exits = LockIsolated<[Int]>([]) + let server = XToolBuildServer(triple: "arm64-apple-ios", connectionToClient: client) { code in + exits.withValue { $0.append(code) } + } + defer { await server.close() } + let connection = LocalConnection(receiverName: "test server", handler: server) + defer { connection.close() } + await server.handle(notification: OnBuildInitializedNotification()) + do { + _ = try await connection.send(WorkspaceBuildTargetsRequest()) + Issue.record("Expected an uninitialized-server error") + } catch let error as ResponseError { + expectNoDifference(error.code, .serverNotInitialized) + } + await server.handle(notification: OnBuildExitNotification()) + expectNoDifference(exits.value, [0]) + await server.close() + } + + @Test + func rejectsNonFileRootURI() async throws { + let client = LocalConnection(receiverName: "test client") + defer { client.close() } + let server = XToolBuildServer(triple: "arm64-apple-ios", connectionToClient: client, exitHandler: { _ in }) + defer { await server.close() } + let connection = LocalConnection(receiverName: "test server", handler: server) + defer { connection.close() } + do { + _ = try await connection.send(initialize(URL(string: "https://example.com/project")!)) + Issue.record("Expected an invalid rootUri error") + } catch let error as ResponseError { + expectNoDifference(error.code, .invalidParams) + } + } + + #if os(macOS) + @Test(.requiresXcode, .timeLimit(.minutes(2))) + func rootURISelectsProjectAndBuildArenaWithoutChangingWorkingDirectory() async throws { + let temporary = try TemporaryDirectory(name: "BSP Root URI") + let originalDirectory = FileManager.default.currentDirectoryPath + try await withDependencies { + $0.out = .liveValue + $0.shutdownRegistry = .liveValue + } operation: { + for name in ["First", "Second"] { + let root = temporary.url.appending(path: "\(name) Package é").resolvingSymlinksInPath() + try makePackage(name: name, at: root) + let client = LocalConnection(receiverName: "test client") + defer { client.close() } + let server = XToolBuildServer(triple: "arm64-apple-ios", connectionToClient: client, exitHandler: { _ in }) + defer { await server.close() } + let connection = LocalConnection(receiverName: "test server", handler: server) + defer { connection.close() } + + let response = try await connection.send(initialize(root)) + expectNoDifference(response.displayName, "xtool Build Server") + await server.handle(notification: OnBuildInitializedNotification()) + _ = try await connection.send(WorkspaceWaitForBuildSystemUpdatesRequest()) + let targets = try await connection.send(WorkspaceBuildTargetsRequest()) + #expect(targets.targets.contains { $0.displayName == name }) + let manifest = BuildTargetIdentifier(uri: URI(URL(string: "xtool://package-manifest")!)) + let sources = try await connection.send(BuildTargetSourcesRequest(targets: [manifest])) + expectNoDifference( + sources.items.flatMap(\.sources).map(\.uri), [DocumentURI(root.appending(path: "Package.swift"))] + ) + let options = try await connection.send(TextDocumentSourceKitOptionsRequest( + textDocument: TextDocumentIdentifier(DocumentURI(root.appending(path: "Package.swift"))), + target: manifest, + language: .swift + )) + expectNoDifference(options?.workingDirectory, root.path) + + let indexData = try #require(SourceKitInitializeBuildResponseData(fromLSPAny: response.data)) + #expect(indexData.indexStorePath?.hasPrefix(root.path + "/") == true) + await #expect(throws: ResponseError.self) { + try await connection.send(initialize(root)) + } + _ = try await connection.send(BuildShutdownRequest()) + expectNoDifference(FileManager.default.currentDirectoryPath, originalDirectory) + } + } + } + + private func makePackage(name: String, at root: URL) throws { + let sources = root.appending(path: "Sources/\(name)") + try FileManager.default.createDirectory(at: sources, withIntermediateDirectories: true) + try Data("public struct Example {}\n".utf8).write(to: sources.appending(path: "Example.swift")) + try Data(""" + // swift-tools-version: 6.0 + import PackageDescription + let package = Package( + name: "\(name)", + platforms: [.iOS(.v17)], + products: [.library(name: "\(name)", targets: ["\(name)"])], + targets: [.target(name: "\(name)")] + ) + """.utf8).write(to: root.appending(path: "Package.swift")) + try Data("version: 1\norgID: org.example\nskipLSP: true\n".utf8).write(to: root.appending(path: "xtool.yml")) + } + #endif + + private func initialize(_ root: URL) -> InitializeBuildRequest { + InitializeBuildRequest( + displayName: "Test", version: "1.0", bspVersion: "2.2.0", rootUri: URI(root), + capabilities: BuildClientCapabilities(languageIds: [.swift]) + ) + } +} diff --git a/Tests/XToolTests/BuildServerTargetsTests.swift b/Tests/XToolTests/BuildServerTargetsTests.swift new file mode 100644 index 00000000..d991dd76 --- /dev/null +++ b/Tests/XToolTests/BuildServerTargetsTests.swift @@ -0,0 +1,35 @@ +import SwiftBuild +import Testing +import XPIF +@testable import XToolSupport + +struct BuildServerTargetsTests { + @Test + func leavesDynamicVariantsToDependencyResolution() { + var support = XPIF.Target( + id: "support", productType: .commonStaticArchive, name: "MacroSupport", productName: "MacroSupport.o" + ) + support.dynamicTargetVariantId = "support-dynamic" + let dynamic = XPIF.Target( + id: "support-dynamic", productType: .framework, name: "MacroSupport", productName: "MacroSupport.framework" + ) + let macro = XPIF.Target( + id: "macro", productType: .hostBuildTool, name: "Macro", productName: "Macro", + dependencies: [XPIF.TargetDependency(targetId: support.id)] + ) + let tests = XPIF.Target( + id: "tests", productType: .unitTest, name: "Tests", productName: "Tests.xctest" + ) + var package = XPIF.Project(id: "package", path: "/tmp/Package", projectDir: "/tmp/Package", name: "Package") + package.targets = [.target(support), .target(macro), .target(tests)] + var variants = XPIF.Project(id: "variants", path: "/tmp/Variants", projectDir: "/tmp/Variants", name: "Variants") + variants.targets = [.target(dynamic)] + let workspace = XPIF.Workspace( + id: "workspace", name: "Workspace", path: "/tmp", projects: [package, variants] + ) + + let targets = workspace.indexingTargets + #expect(targets.map(\.id) == ["support", "macro", "tests"]) + #expect(workspace.projects[1].targets[0].id == dynamic.id) + } +} diff --git a/Tests/XToolTests/IndexBuildArenaTests.swift b/Tests/XToolTests/IndexBuildArenaTests.swift new file mode 100644 index 00000000..692dfd51 --- /dev/null +++ b/Tests/XToolTests/IndexBuildArenaTests.swift @@ -0,0 +1,55 @@ +import SwiftBuild +import Testing +@testable import XToolSupport + +struct IndexBuildArenaTests { + @Test + func separatesIndexProductsFromCachedInstallProducts() throws { + var request = SWBBuildRequest() + request.parameters.arenaInfo = arena(root: "/tmp/DerivedData") + request.parameters.action = "install" + var targetParameters = SWBBuildParameters() + targetParameters.arenaInfo = arena(root: "/tmp/TargetData") + request.configuredTargets = [ + SWBConfiguredTarget(guid: "app"), + SWBConfiguredTarget(guid: "macro", parameters: targetParameters), + ] + + let indexing = request.withIndexBuildArena() + let indexedArena = try #require(indexing.parameters.arenaInfo) + #expect(indexedArena.buildProductsPath == "/tmp/DerivedData/Index.noindex/Build/Products") + #expect(indexedArena.buildIntermediatesPath == "/tmp/DerivedData/Index.noindex/Build/Intermediates.noindex") + #expect(indexedArena.indexRegularBuildProductsPath == "/tmp/DerivedData/Products") + #expect(indexedArena.indexRegularBuildIntermediatesPath == "/tmp/DerivedData/Intermediates.noindex") + #expect(indexedArena.indexDataStoreFolderPath == "/tmp/DerivedData/Index.noindex/DataStore") + #expect(indexedArena.indexEnableDataStore) + #expect(indexing.configuredTargets[0].parameters == nil) + #expect(indexing.configuredTargets[1].parameters?.arenaInfo?.buildProductsPath + == "/tmp/TargetData/Index.noindex/Build/Products") + #expect(request.parameters.arenaInfo?.buildProductsPath == "/tmp/DerivedData/Products") + #expect(request.configuredTargets[1].parameters?.arenaInfo?.buildProductsPath == "/tmp/TargetData/Products") + #expect(indexing.withIndexBuildArena().parameters.arenaInfo?.buildProductsPath == indexedArena.buildProductsPath) + #expect(indexing.withIndexBuildArena().parameters.arenaInfo?.indexRegularBuildProductsPath + == indexedArena.indexRegularBuildProductsPath) + } + + @Test + func leavesUnspecifiedArenasToSwiftBuild() { + let request = SWBBuildRequest().withIndexBuildArena() + #expect(request.parameters.arenaInfo == nil) + } + + private func arena(root: String) -> SWBArenaInfo { + SWBArenaInfo( + derivedDataPath: root, + buildProductsPath: root + "/Products", + buildIntermediatesPath: root + "/Intermediates.noindex", + pchPath: root, + indexRegularBuildProductsPath: nil, + indexRegularBuildIntermediatesPath: nil, + indexPCHPath: root, + indexDataStoreFolderPath: root + "/Index.noindex/DataStore", + indexEnableDataStore: true + ) + } +} diff --git a/Tests/XToolTests/SwiftPMBuildDescriberTests.swift b/Tests/XToolTests/SwiftPMBuildDescriberTests.swift index 4e5c3513..669cf379 100644 --- a/Tests/XToolTests/SwiftPMBuildDescriberTests.swift +++ b/Tests/XToolTests/SwiftPMBuildDescriberTests.swift @@ -33,6 +33,18 @@ struct SwiftPMBuildDescriberTests { ], )) let describer = describer(schema: schema, root: root) + let description = try describer.describe(basePIF: pif(root: root, products: ["App", "Widget"])) + let umbrellaTarget = try #require(description.workspace.projects.flatMap(\.targets).first { + $0.id == "xtool:UMBRELLA" + }) + guard case .aggregate(let aggregate) = umbrellaTarget else { + Issue.record("Expected an aggregate indexing target") + return + } + expectNoDifference(aggregate.common.dependencies.map(\.targetId), [ + "PACKAGE-PRODUCT:App", "PACKAGE-PRODUCT:Widget", "xtool:main-app", "xtool:extension:Widget", + ]) + expectNoDifference(aggregate.common.buildConfigs.map(\.name), ["Debug", "Release"]) let result = try describer.writeXcodeProject(basePIF: pif(root: root, products: ["App", "Widget"])) #expect(result.bundleID == "org.example.custom") #expect(result.url == root.appending(path: "xtool/App.xcworkspace")) @@ -142,6 +154,8 @@ struct SwiftPMBuildDescriberTests { expectNoDifference(Array(targets.prefix(2)), pif.workspace.projects[0].targets) let app = try #require(targets.first { $0.id == "xtool:main-app" }) expectNoDifference(app.common.dependencies.map(\.targetId), [product.id]) + let umbrellaTarget = try #require(targets.first { $0.id == "xtool:UMBRELLA" }) + expectNoDifference(umbrellaTarget.common.dependencies.map(\.targetId), [product.id, app.id]) let result = try describer.writeXcodeProject(basePIF: pif) expectNoDifference(result.bundleID, "com.example.App") let project = try XcodeProject(url: root.appending(path: "xtool/.xtool-tmp/App.xcodeproj")) From 7798105f0e01eb9fc18622a15af779acfd65a87b Mon Sep 17 00:00:00 2001 From: Kabir Oberai Date: Tue, 22 Sep 2026 00:59:18 -0400 Subject: [PATCH 08/15] swb: support Xcode projects --- .bsp/xtool.json | 7 + .../DemoXcode.xcodeproj/project.pbxproj | 213 ++++++++++ .../xcshareddata/xcschemes/DemoXcode.xcscheme | 22 ++ Examples/DemoXcode/Resources/message.txt | 1 + Examples/DemoXcode/Sources/App.swift | 20 + Examples/DemoXcode/xtool.yml | 1 + Examples/test.sh | 4 +- Package.resolved | 2 +- Package.swift | 12 + Sources/XToolSupport/BuildCoordinator.swift | 13 +- Sources/XToolSupport/BuildDescription.swift | 8 + Sources/XToolSupport/DevBSPCommand.swift | 4 + Sources/XToolSupport/DevCommand.swift | 19 +- Sources/XToolSupport/PackSchema.swift | 18 +- .../SwiftPMBuildDescriber+Products.swift | 4 +- .../SwiftPMBuildDescriber+XcodeProject.swift | 8 + Sources/XToolSupport/XToolBuildServer.swift | 5 +- Sources/XToolSupport/XToolProject.swift | 57 ++- .../XToolSupport/XcodeBuildDescriber.swift | 73 ++++ .../XToolSupport/XcodePackagePIFBuilder.swift | 158 ++++++++ .../XcodePackageSDKDefaults.swift | 71 ++++ .../StringEncoding+IANA.swift | 29 ++ .../XcodePIFConversion/XcodeFileType.swift | 168 ++++++++ .../XcodeLoadedProject+References.swift | 126 ++++++ .../XcodeLoadedProject.swift | 216 ++++++++++ .../XcodePIFBuilder+Schemes.swift | 26 ++ .../XcodePIFConversion/XcodePIFBuilder.swift | 146 +++++++ .../XcodePIFConversionError.swift | 10 + .../XcodePIFProjectBuilder+BuildFiles.swift | 35 ++ .../XcodePIFProjectBuilder+Packages.swift | 22 ++ .../XcodePIFProjectBuilder+Targets.swift | 334 ++++++++++++++++ .../XcodePIFProjectBuilder.swift | 86 ++++ .../XcodeProjects+Locations.swift | 68 ++++ .../XcodeSynchronizedFolder+Memberships.swift | 94 +++++ .../XcodeSynchronizedFolder+Routing.swift | 96 +++++ .../XcodeSynchronizedFolder.swift | 193 +++++++++ Sources/XcodeProjects/XcodeScheme.swift | 162 ++++++++ .../Projects/App.xcodeproj/project.xcproj | 108 +++++ .../contents.xcworkspacedata | 7 + .../xcshareddata/xcschemes/Primary.xcscheme | 8 + .../project.xcproj | 147 +++++++ .../Features.xcodeproj/project.xcproj | 167 ++++++++ .../contents.xcworkspacedata | 7 + Tests/XToolTests/Fixtures/Projects/Info.plist | 8 + .../Fixtures/Projects/Inputs.xcfilelist | 1 + .../Projects/LocalPackage/Package.swift | 17 + .../DynamicLibrary/DynamicLibrary.swift | 1 + .../LocalPackage/Sources/Helper/Helper.swift | 1 + .../Sources/Library/Library.swift | 7 + .../LocalPackage/Sources/Library/Message.txt | 1 + .../Tests/LibraryTests/LibraryTests.swift | 4 + .../Projects/Modules/Account/Package.swift | 8 + .../Account/Sources/Account/Account.swift | 1 + .../OtherApp.xcodeproj/project.xcproj | 108 +++++ .../Fixtures/Projects/Outputs.xcfilelist | 1 + .../PIF.xcworkspace/contents.xcworkspacedata | 5 + .../Packages.xcodeproj/project.xcproj | 149 +++++++ .../Resources/en.lproj/Localizable.strings | 1 + .../Resources/fr.lproj/Localizable.strings | 1 + .../Fixtures/Projects/Resources/message.txt | 1 + .../contents.xcworkspacedata | 5 + .../xcshareddata/xcschemes/Secondary.xcscheme | 13 + .../Fixtures/Projects/Settings.xcconfig | 1 + .../Fixtures/Projects/Sources/App.swift | 10 + .../Fixtures/Projects/Sources/Feature.h | 1 + .../Fixtures/Projects/Sources/Feature.swift | 1 + .../Fixtures/Projects/Sources/Other.swift | 1 + .../Projects/Sources/PackageApp.swift | 13 + .../Projects/SyncFiles/Excluded.swift | 1 + .../Fixtures/Projects/SyncFiles/Feature.swift | 1 + .../Projects/SyncFiles/Features.entitlements | 5 + .../Projects/SyncFiles/Nested/message.txt | 1 + .../Fixtures/Projects/SyncFiles/Notes.md | 1 + .../Projects/SyncFiles/module.modulemap | 1 + .../Synchronized.xcodeproj/project.xcproj | 80 ++++ .../contents.xcworkspacedata | 7 + .../Fixtures/simple-xcode-project.pbxproj | 276 +++++++++++++ Tests/XToolTests/PackSchemaTests.swift | 92 +++++ Tests/XToolTests/RequiresXcodeTrait.swift | 27 +- .../SwiftPMBuildDescriberTests.swift | 8 + Tests/XToolTests/TestFixtures.swift | 8 + .../XToolTests/XcodeBuildDescriberTests.swift | 82 ++++ Tests/XToolTests/XcodePIFBuilderTests.swift | 290 ++++++++++++++ Tests/XToolTests/XcodePIFParityTests.swift | 124 ++++++ .../XcodePackagePIFBuilderTests.swift | 170 ++++++++ Tests/XToolTests/XcodeSchemeTests.swift | 221 +++++++++++ .../XcodeSynchronizedFolderTests.swift | 281 +++++++++++++ .../XcodeWorkspacePIFBuilderTests.swift | 372 ++++++++++++++++++ xtool.yml | 2 + 89 files changed, 5366 insertions(+), 19 deletions(-) create mode 100644 .bsp/xtool.json create mode 100644 Examples/DemoXcode/DemoXcode.xcodeproj/project.pbxproj create mode 100644 Examples/DemoXcode/DemoXcode.xcodeproj/xcshareddata/xcschemes/DemoXcode.xcscheme create mode 100644 Examples/DemoXcode/Resources/message.txt create mode 100644 Examples/DemoXcode/Sources/App.swift create mode 100644 Examples/DemoXcode/xtool.yml create mode 100644 Sources/XToolSupport/XcodeBuildDescriber.swift create mode 100644 Sources/XToolSupport/XcodePackagePIFBuilder.swift create mode 100644 Sources/XToolSupport/XcodePackageSDKDefaults.swift create mode 100644 Sources/XcodePIFConversion/StringEncoding+IANA.swift create mode 100644 Sources/XcodePIFConversion/XcodeFileType.swift create mode 100644 Sources/XcodePIFConversion/XcodeLoadedProject+References.swift create mode 100644 Sources/XcodePIFConversion/XcodeLoadedProject.swift create mode 100644 Sources/XcodePIFConversion/XcodePIFBuilder+Schemes.swift create mode 100644 Sources/XcodePIFConversion/XcodePIFBuilder.swift create mode 100644 Sources/XcodePIFConversion/XcodePIFConversionError.swift create mode 100644 Sources/XcodePIFConversion/XcodePIFProjectBuilder+BuildFiles.swift create mode 100644 Sources/XcodePIFConversion/XcodePIFProjectBuilder+Packages.swift create mode 100644 Sources/XcodePIFConversion/XcodePIFProjectBuilder+Targets.swift create mode 100644 Sources/XcodePIFConversion/XcodePIFProjectBuilder.swift create mode 100644 Sources/XcodePIFConversion/XcodeProjects+Locations.swift create mode 100644 Sources/XcodePIFConversion/XcodeSynchronizedFolder+Memberships.swift create mode 100644 Sources/XcodePIFConversion/XcodeSynchronizedFolder+Routing.swift create mode 100644 Sources/XcodePIFConversion/XcodeSynchronizedFolder.swift create mode 100644 Sources/XcodeProjects/XcodeScheme.swift create mode 100644 Tests/XToolTests/Fixtures/Projects/App.xcodeproj/project.xcproj create mode 100644 Tests/XToolTests/Fixtures/Projects/App.xcodeproj/project.xcworkspace/contents.xcworkspacedata create mode 100644 Tests/XToolTests/Fixtures/Projects/App.xcodeproj/xcshareddata/xcschemes/Primary.xcscheme create mode 100644 Tests/XToolTests/Fixtures/Projects/DiscoveredPackages.xcodeproj/project.xcproj create mode 100644 Tests/XToolTests/Fixtures/Projects/Features.xcodeproj/project.xcproj create mode 100644 Tests/XToolTests/Fixtures/Projects/Features.xcodeproj/project.xcworkspace/contents.xcworkspacedata create mode 100644 Tests/XToolTests/Fixtures/Projects/Info.plist create mode 100644 Tests/XToolTests/Fixtures/Projects/Inputs.xcfilelist create mode 100644 Tests/XToolTests/Fixtures/Projects/LocalPackage/Package.swift create mode 100644 Tests/XToolTests/Fixtures/Projects/LocalPackage/Sources/DynamicLibrary/DynamicLibrary.swift create mode 100644 Tests/XToolTests/Fixtures/Projects/LocalPackage/Sources/Helper/Helper.swift create mode 100644 Tests/XToolTests/Fixtures/Projects/LocalPackage/Sources/Library/Library.swift create mode 100644 Tests/XToolTests/Fixtures/Projects/LocalPackage/Sources/Library/Message.txt create mode 100644 Tests/XToolTests/Fixtures/Projects/LocalPackage/Tests/LibraryTests/LibraryTests.swift create mode 100644 Tests/XToolTests/Fixtures/Projects/Modules/Account/Package.swift create mode 100644 Tests/XToolTests/Fixtures/Projects/Modules/Account/Sources/Account/Account.swift create mode 100644 Tests/XToolTests/Fixtures/Projects/OtherApp.xcodeproj/project.xcproj create mode 100644 Tests/XToolTests/Fixtures/Projects/Outputs.xcfilelist create mode 100644 Tests/XToolTests/Fixtures/Projects/PIF.xcworkspace/contents.xcworkspacedata create mode 100644 Tests/XToolTests/Fixtures/Projects/Packages.xcodeproj/project.xcproj create mode 100644 Tests/XToolTests/Fixtures/Projects/Resources/en.lproj/Localizable.strings create mode 100644 Tests/XToolTests/Fixtures/Projects/Resources/fr.lproj/Localizable.strings create mode 100644 Tests/XToolTests/Fixtures/Projects/Resources/message.txt create mode 100644 Tests/XToolTests/Fixtures/Projects/Schemes.xcworkspace/contents.xcworkspacedata create mode 100644 Tests/XToolTests/Fixtures/Projects/Schemes.xcworkspace/xcshareddata/xcschemes/Secondary.xcscheme create mode 100644 Tests/XToolTests/Fixtures/Projects/Settings.xcconfig create mode 100644 Tests/XToolTests/Fixtures/Projects/Sources/App.swift create mode 100644 Tests/XToolTests/Fixtures/Projects/Sources/Feature.h create mode 100644 Tests/XToolTests/Fixtures/Projects/Sources/Feature.swift create mode 100644 Tests/XToolTests/Fixtures/Projects/Sources/Other.swift create mode 100644 Tests/XToolTests/Fixtures/Projects/Sources/PackageApp.swift create mode 100644 Tests/XToolTests/Fixtures/Projects/SyncFiles/Excluded.swift create mode 100644 Tests/XToolTests/Fixtures/Projects/SyncFiles/Feature.swift create mode 100644 Tests/XToolTests/Fixtures/Projects/SyncFiles/Features.entitlements create mode 100644 Tests/XToolTests/Fixtures/Projects/SyncFiles/Nested/message.txt create mode 100644 Tests/XToolTests/Fixtures/Projects/SyncFiles/Notes.md create mode 100644 Tests/XToolTests/Fixtures/Projects/SyncFiles/module.modulemap create mode 100644 Tests/XToolTests/Fixtures/Projects/Synchronized.xcodeproj/project.xcproj create mode 100644 Tests/XToolTests/Fixtures/Projects/Synchronized.xcodeproj/project.xcworkspace/contents.xcworkspacedata create mode 100644 Tests/XToolTests/Fixtures/simple-xcode-project.pbxproj create mode 100644 Tests/XToolTests/PackSchemaTests.swift create mode 100644 Tests/XToolTests/XcodeBuildDescriberTests.swift create mode 100644 Tests/XToolTests/XcodePIFBuilderTests.swift create mode 100644 Tests/XToolTests/XcodePIFParityTests.swift create mode 100644 Tests/XToolTests/XcodePackagePIFBuilderTests.swift create mode 100644 Tests/XToolTests/XcodeSchemeTests.swift create mode 100644 Tests/XToolTests/XcodeSynchronizedFolderTests.swift create mode 100644 Tests/XToolTests/XcodeWorkspacePIFBuilderTests.swift create mode 100644 xtool.yml diff --git a/.bsp/xtool.json b/.bsp/xtool.json new file mode 100644 index 00000000..2ca811e8 --- /dev/null +++ b/.bsp/xtool.json @@ -0,0 +1,7 @@ +{ + "name": "xtool", + "version": "1.0", + "bspVersion": "2.2.0", + "languages": ["c", "cpp", "objective-c", "objective-cpp", "swift"], + "argv": ["/usr/bin/env", "xtool", "dev", "build-server", "--triple", "arm64-apple-macosx"] +} diff --git a/Examples/DemoXcode/DemoXcode.xcodeproj/project.pbxproj b/Examples/DemoXcode/DemoXcode.xcodeproj/project.pbxproj new file mode 100644 index 00000000..96c8ea6f --- /dev/null +++ b/Examples/DemoXcode/DemoXcode.xcodeproj/project.pbxproj @@ -0,0 +1,213 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = {}; + objectVersion = 56; + objects = { + +/* Begin PBXBuildFile section */ + A10000000000000000000001 /* App.swift in Sources */ = {isa = PBXBuildFile; fileRef = A20000000000000000000001 /* App.swift */; }; + A10000000000000000000002 /* ExampleSupport in Frameworks */ = {isa = PBXBuildFile; productRef = AC0000000000000000000001 /* ExampleSupport */; }; + A10000000000000000000003 /* message.txt in Resources */ = {isa = PBXBuildFile; fileRef = A20000000000000000000003 /* message.txt */; }; +/* End PBXBuildFile section */ + +/* Begin PBXFileReference section */ + A20000000000000000000001 /* App.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = App.swift; sourceTree = ""; }; + A20000000000000000000003 /* message.txt */ = {isa = PBXFileReference; lastKnownFileType = text; path = message.txt; sourceTree = ""; }; + A20000000000000000000004 /* DemoXcode.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = DemoXcode.app; sourceTree = BUILT_PRODUCTS_DIR; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + A30000000000000000000001 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = (A10000000000000000000002 /* ExampleSupport in Frameworks */); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + A40000000000000000000001 = { + isa = PBXGroup; + children = ( + A40000000000000000000002 /* Sources */, + A40000000000000000000003 /* Resources */, + A40000000000000000000005 /* Products */, + ); + sourceTree = ""; + }; + A40000000000000000000002 /* Sources */ = { + isa = PBXGroup; + children = (A20000000000000000000001 /* App.swift */); + path = Sources; + sourceTree = ""; + }; + A40000000000000000000003 /* Resources */ = { + isa = PBXGroup; + children = (A20000000000000000000003 /* message.txt */); + path = Resources; + sourceTree = ""; + }; + A40000000000000000000005 /* Products */ = { + isa = PBXGroup; + children = (A20000000000000000000004 /* DemoXcode.app */); + name = Products; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + A50000000000000000000001 /* DemoXcode */ = { + isa = PBXNativeTarget; + buildConfigurationList = A90000000000000000000002 /* Build configuration list for PBXNativeTarget "DemoXcode" */; + buildPhases = ( + A70000000000000000000001 /* Sources */, + A30000000000000000000001 /* Frameworks */, + A60000000000000000000001 /* Resources */, + ); + buildRules = (); + dependencies = (); + name = DemoXcode; + packageProductDependencies = (AC0000000000000000000001 /* ExampleSupport */); + productName = DemoXcode; + productReference = A20000000000000000000004 /* DemoXcode.app */; + productType = "com.apple.product-type.application"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + AA0000000000000000000001 /* Project object */ = { + isa = PBXProject; + attributes = {BuildIndependentTargetsInParallel = YES; LastUpgradeCheck = 1600; }; + buildConfigurationList = A90000000000000000000001 /* Build configuration list for PBXProject "DemoXcode" */; + compatibilityVersion = "Xcode 14.0"; + developmentRegion = en; + hasScannedForEncodings = 0; + knownRegions = (en, Base); + mainGroup = A40000000000000000000001; + packageReferences = (AB0000000000000000000001 /* XCLocalSwiftPackageReference "../ExampleSupport" */); + productRefGroup = A40000000000000000000005 /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = (A50000000000000000000001 /* DemoXcode */); + }; +/* End PBXProject section */ + +/* Begin PBXResourcesBuildPhase section */ + A60000000000000000000001 /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = (A10000000000000000000003 /* message.txt in Resources */); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXResourcesBuildPhase section */ + +/* Begin PBXSourcesBuildPhase section */ + A70000000000000000000001 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + A10000000000000000000001 /* App.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + A80000000000000000000001 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CLANG_ENABLE_MODULES = YES; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_TESTABILITY = YES; + IPHONEOS_DEPLOYMENT_TARGET = 17.0; + MACOSX_DEPLOYMENT_TARGET = 14.0; + SDKROOT = auto; + SDK_VARIANT = auto; + SUPPORTED_PLATFORMS = "macosx iphoneos iphonesimulator"; + SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 6.0; + }; + name = Debug; + }; + A80000000000000000000002 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CLANG_ENABLE_MODULES = YES; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + IPHONEOS_DEPLOYMENT_TARGET = 17.0; + MACOSX_DEPLOYMENT_TARGET = 14.0; + SDKROOT = auto; + SDK_VARIANT = auto; + SUPPORTED_PLATFORMS = "macosx iphoneos iphonesimulator"; + SWIFT_COMPILATION_MODE = wholemodule; + SWIFT_OPTIMIZATION_LEVEL = "-O"; + SWIFT_VERSION = 6.0; + }; + name = Release; + }; + A80000000000000000000003 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + GENERATE_INFOPLIST_FILE = YES; + "INFOPLIST_KEY_UIApplicationSceneManifest_Generation[sdk=iphone*]" = YES; + "INFOPLIST_KEY_UILaunchScreen_Generation[sdk=iphone*]" = YES; + "LD_RUNPATH_SEARCH_PATHS[sdk=iphone*]" = "$(inherited) @executable_path/Frameworks"; + "LD_RUNPATH_SEARCH_PATHS[sdk=macosx*]" = "$(inherited) @executable_path/../Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = sh.xtool.examples.DemoXcode; + PRODUCT_NAME = "$(TARGET_NAME)"; + "TARGETED_DEVICE_FAMILY[sdk=iphone*]" = "1,2"; + }; + name = Debug; + }; + A80000000000000000000004 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + GENERATE_INFOPLIST_FILE = YES; + "INFOPLIST_KEY_UIApplicationSceneManifest_Generation[sdk=iphone*]" = YES; + "INFOPLIST_KEY_UILaunchScreen_Generation[sdk=iphone*]" = YES; + "LD_RUNPATH_SEARCH_PATHS[sdk=iphone*]" = "$(inherited) @executable_path/Frameworks"; + "LD_RUNPATH_SEARCH_PATHS[sdk=macosx*]" = "$(inherited) @executable_path/../Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = sh.xtool.examples.DemoXcode; + PRODUCT_NAME = "$(TARGET_NAME)"; + "TARGETED_DEVICE_FAMILY[sdk=iphone*]" = "1,2"; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + A90000000000000000000001 /* Build configuration list for PBXProject "DemoXcode" */ = { + isa = XCConfigurationList; + buildConfigurations = (A80000000000000000000001 /* Debug */, A80000000000000000000002 /* Release */); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Debug; + }; + A90000000000000000000002 /* Build configuration list for PBXNativeTarget "DemoXcode" */ = { + isa = XCConfigurationList; + buildConfigurations = (A80000000000000000000003 /* Debug */, A80000000000000000000004 /* Release */); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Debug; + }; +/* End XCConfigurationList section */ + +/* Begin XCLocalSwiftPackageReference section */ + AB0000000000000000000001 /* XCLocalSwiftPackageReference "../ExampleSupport" */ = { + isa = XCLocalSwiftPackageReference; + relativePath = ../ExampleSupport; + }; +/* End XCLocalSwiftPackageReference section */ + +/* Begin XCSwiftPackageProductDependency section */ + AC0000000000000000000001 /* ExampleSupport */ = { + isa = XCSwiftPackageProductDependency; + package = AB0000000000000000000001 /* XCLocalSwiftPackageReference "../ExampleSupport" */; + productName = ExampleSupport; + }; +/* End XCSwiftPackageProductDependency section */ + }; + rootObject = AA0000000000000000000001 /* Project object */; +} diff --git a/Examples/DemoXcode/DemoXcode.xcodeproj/xcshareddata/xcschemes/DemoXcode.xcscheme b/Examples/DemoXcode/DemoXcode.xcodeproj/xcshareddata/xcschemes/DemoXcode.xcscheme new file mode 100644 index 00000000..6e682b79 --- /dev/null +++ b/Examples/DemoXcode/DemoXcode.xcodeproj/xcshareddata/xcschemes/DemoXcode.xcscheme @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/Examples/DemoXcode/Resources/message.txt b/Examples/DemoXcode/Resources/message.txt new file mode 100644 index 00000000..8175d7e9 --- /dev/null +++ b/Examples/DemoXcode/Resources/message.txt @@ -0,0 +1 @@ +Hello from Xcode! diff --git a/Examples/DemoXcode/Sources/App.swift b/Examples/DemoXcode/Sources/App.swift new file mode 100644 index 00000000..34a5f024 --- /dev/null +++ b/Examples/DemoXcode/Sources/App.swift @@ -0,0 +1,20 @@ +import ExampleSupport +import Foundation +import SwiftUI + +@main struct DemoApp: App { + var body: some Scene { + WindowGroup { + Text("Hello from an Xcode project!") + .padding() + .task { + guard let url = Bundle.main.url(forResource: "message", withExtension: "txt") else { + fatalError("Missing bundled resource") + } + let message = try? String(contentsOf: url, encoding: .utf8) + precondition(message == "Hello from Xcode!\n") + Example.pass() + } + } + } +} diff --git a/Examples/DemoXcode/xtool.yml b/Examples/DemoXcode/xtool.yml new file mode 100644 index 00000000..b8255184 --- /dev/null +++ b/Examples/DemoXcode/xtool.yml @@ -0,0 +1 @@ +version: 1 diff --git a/Examples/test.sh b/Examples/test.sh index 453444af..99915545 100755 --- a/Examples/test.sh +++ b/Examples/test.sh @@ -34,7 +34,9 @@ elif [[ "$mode" == run ]]; then else demos=() for demo in "$examples_dir"/Demo*/; do - [[ -f "$demo/Package.swift" ]] || continue + projects=("$demo"/*.xcodeproj) + workspaces=("$demo"/*.xcworkspace) + [[ -f "$demo/Package.swift" || ${#projects[@]} -gt 0 || ${#workspaces[@]} -gt 0 ]] || continue demo="${demo%/}" demos+=("${demo##*/}") done diff --git a/Package.resolved b/Package.resolved index 24bb1b16..dd3ef741 100644 --- a/Package.resolved +++ b/Package.resolved @@ -1,5 +1,5 @@ { - "originHash" : "5008a43e53ee72f8c3441b7879d0528a1595547ef65419251a5b65a72a347a0a", + "originHash" : "90974e527d6b13c924b60ede3cbcef4bcc2c4eb28f7963ba8128aa08aa74c2ac", "pins" : [ { "identity" : "aexml", diff --git a/Package.swift b/Package.swift index c9a74638..8c972320 100644 --- a/Package.swift +++ b/Package.swift @@ -37,6 +37,7 @@ let package = Package( ], products: [ .library(name: "XPIF", targets: ["XPIF"]), + .library(name: "XcodePIFConversion", targets: ["XcodePIFConversion"]), .library( name: "XKit", targets: ["XKit"] @@ -99,6 +100,15 @@ let package = Package( exclude: ["LICENSE.txt", "README.md"], swiftSettings: swiftSettings, ), + .target( + name: "XcodePIFConversion", + dependencies: [ + "XPIF", + "XcodeProjects", + .product(name: "OrderedCollections", package: "swift-collections"), + ], + swiftSettings: swiftSettings, + ), .target( name: "CXKit", dependencies: [ @@ -178,6 +188,7 @@ let package = Package( name: "XToolTests", dependencies: [ "XPIF", + "XcodePIFConversion", "XcodeProjects", "XKit", "XToolSupport", @@ -204,6 +215,7 @@ let package = Package( name: "XToolSupport", dependencies: [ "XPIF", + "XcodePIFConversion", "XcodeProjects", "SwiftyMobileDevice", "XKit", diff --git a/Sources/XToolSupport/BuildCoordinator.swift b/Sources/XToolSupport/BuildCoordinator.swift index 93a4e618..0fa6cb7b 100644 --- a/Sources/XToolSupport/BuildCoordinator.swift +++ b/Sources/XToolSupport/BuildCoordinator.swift @@ -9,9 +9,11 @@ public actor BuildCoordinator { let sessionManager: BuildSessionManager let buildRequest: SWBBuildRequest let pifContainer: XPIF.Container + let targetSelection: BuildDescription.TargetSelection? init(description: BuildDescription) async throws { self.pifContainer = description.pif + self.targetSelection = description.targetSelection Signposter.track("Creating BuildSessionManager") @@ -117,7 +119,10 @@ public actor BuildCoordinator { parameters.overrides.synthesized = overridesTable var request = SWBBuildRequest() request.parameters = parameters - request.useImplicitDependencies = true + request.useImplicitDependencies = description.targetSelection?.buildImplicitDependencies ?? true + request.configuredTargets = description.targetSelection?.buildTargets.map { + SWBConfiguredTarget(guid: $0.value) + } ?? [] request.setDerivedDataPath(xtoolDir.appending(path: ".xtool-tmp/DerivedData")) self.buildRequest = request } catch { @@ -131,12 +136,16 @@ public actor BuildCoordinator { } func mainProductGUID() throws -> String { + if let targetSelection { return targetSelection.application.value } let apps = pifContainer.workspace.projects.flatMap(\.targets).compactMap { base -> XPIF.Target? in guard case let .target(target) = base, target.productType == .application else { return nil } return target } guard apps.count == 1, let app = apps.first else { - throw Console.Error("Expected one application target, found \(apps.count).") + throw Console.Error(""" + Expected one application target in Xcode project or workspace, found \(apps.count). \ + Select an application with --scheme or 'scheme' in xtool.yml. + """) } return app.id.value } diff --git a/Sources/XToolSupport/BuildDescription.swift b/Sources/XToolSupport/BuildDescription.swift index 3d9c9e64..2e89d3ee 100644 --- a/Sources/XToolSupport/BuildDescription.swift +++ b/Sources/XToolSupport/BuildDescription.swift @@ -6,6 +6,14 @@ struct BuildDescription { var pif: XPIF.Container var configuration: BuildConfiguration var triple: String + + struct TargetSelection { + var application: XPIF.GUID + var buildTargets: [XPIF.GUID] + var buildImplicitDependencies: Bool + } + + var targetSelection: TargetSelection? } enum BuildConfiguration: String, CaseIterable, Sendable { diff --git a/Sources/XToolSupport/DevBSPCommand.swift b/Sources/XToolSupport/DevBSPCommand.swift index eb0f4db7..235b407f 100644 --- a/Sources/XToolSupport/DevBSPCommand.swift +++ b/Sources/XToolSupport/DevBSPCommand.swift @@ -12,6 +12,9 @@ struct DevBSPCommand: AsyncParsableCommand { @Option var triple: String? + @Option(help: "Xcode scheme to build (overrides xtool.yml)") + var scheme: String? + func run() async throws { let transport = BSPCommandTransport() // Only BSP frames may be written to the client's stdout. @@ -21,6 +24,7 @@ struct DevBSPCommand: AsyncParsableCommand { } operation: { let server = XToolBuildServer( triple: triple ?? PackOperation.defaultTriple, + scheme: scheme, connectionToClient: transport.connection, exitHandler: { _ in transport.connection.close() } ) diff --git a/Sources/XToolSupport/DevCommand.swift b/Sources/XToolSupport/DevCommand.swift index 74d2f50f..853da719 100644 --- a/Sources/XToolSupport/DevCommand.swift +++ b/Sources/XToolSupport/DevCommand.swift @@ -1,4 +1,5 @@ import XCLIUtils +import XPIF import Foundation import ArgumentParser import XKit @@ -18,15 +19,21 @@ struct PackOperation { help: "Verbose output" ) var verbose = false + @Option( + help: "Xcode scheme to build (overrides xtool.yml)" + ) var scheme: String? + init() {} // this is load bearing: synthesized init doesn't work with ArgumentParser init( configuration: BuildConfiguration, verbose: Bool = false, + scheme: String? = nil, ) { self.configuration = configuration self.verbose = verbose + self.scheme = scheme } } @@ -45,14 +52,14 @@ struct PackOperation { let description = try await project.describe( configuration: buildOptions.configuration, - triple: triple ?? Self.defaultTriple + triple: triple ?? Self.defaultTriple, + scheme: buildOptions.scheme ) - let coordinator = try await BuildCoordinator(description: description) defer { await coordinator.close() } return try await ProjectBuildOperation( buildCoordinator: coordinator, - verbose: buildOptions.verbose, + verbose: buildOptions.verbose ).run() } } @@ -80,7 +87,7 @@ struct DevBuildCommand: AsyncParsableCommand { commandName: "build", abstract: "Build iOS app", discussion: """ - This command builds the Swift package iOS app in the current directory + This command builds the Swift package or Xcode iOS app in the current directory """ ) @@ -170,7 +177,7 @@ struct DevRunCommand: AsyncParsableCommand { commandName: "run", abstract: "Build and run iOS app", discussion: """ - This command deploys the Swift package iOS app in the current directory + This command deploys the Swift package or Xcode iOS app in the current directory """ ) @@ -266,7 +273,7 @@ struct DevRunCommand: AsyncParsableCommand { struct DevCommand: AsyncParsableCommand { static let configuration = CommandConfiguration( commandName: "dev", - abstract: "Build and run a Swift package", + abstract: "Build and run a Swift package or Xcode project", subcommands: [ DevXcodeCommand.self, DevBuildCommand.self, diff --git a/Sources/XToolSupport/PackSchema.swift b/Sources/XToolSupport/PackSchema.swift index 7da0e211..1d586281 100644 --- a/Sources/XToolSupport/PackSchema.swift +++ b/Sources/XToolSupport/PackSchema.swift @@ -14,6 +14,12 @@ public struct PackSchemaBase: Codable, Sendable { public var product: String? + /// Path to Package.swift, an Xcode workspace, or an Xcode project, relative to xtool.yml. + public var project: String? + + /// Xcode scheme to select when building an Xcode project or workspace. + public var scheme: String? + public var infoPath: String? public var entitlementsPath: String? @@ -51,7 +57,7 @@ public struct PackSchema: Sendable { } public let base: PackSchemaBase - public let idSpecifier: IDSpecifier + public let idSpecifier: IDSpecifier? public init(validating base: PackSchemaBase) throws { self.base = base @@ -66,7 +72,7 @@ public struct PackSchema: Sendable { case (nil, let orgID?): idSpecifier = .orgID(orgID) case (nil, nil): - throw StringError("xtool.yml: Must specify either orgID or bundleID") + idSpecifier = nil } if let iconPath = base.iconPath { @@ -75,6 +81,14 @@ public struct PackSchema: Sendable { throw StringError("xtool.yml: iconPath should have a 'png' path extension. Got '\(ext)'.") } } + + if let project = base.project { + let url = URL(fileURLWithPath: project) + guard url.lastPathComponent == "Package.swift" + || ["xcworkspace", "xcodeproj"].contains(url.pathExtension) else { + throw StringError("xtool.yml: project must point to Package.swift, an .xcworkspace, or an .xcodeproj.") + } + } } // swiftlint:disable:next force_try diff --git a/Sources/XToolSupport/SwiftPMBuildDescriber+Products.swift b/Sources/XToolSupport/SwiftPMBuildDescriber+Products.swift index 7e62cb03..b4d98974 100644 --- a/Sources/XToolSupport/SwiftPMBuildDescriber+Products.swift +++ b/Sources/XToolSupport/SwiftPMBuildDescriber+Products.swift @@ -5,7 +5,9 @@ import XUtils extension SwiftPMBuildDescriber { func bundleProducts(in project: XPIF.Project) throws -> [ProjectProduct] { - let idSpecifier = self.project.base.schema.idSpecifier + let idSpecifier = try self.project.base.schema.idSpecifier.orThrow( + Console.Error("xtool.yml: Must specify either orgID or bundleID") + ) let appProduct = try libraryProduct(in: project, matching: schema.product) let app = ProjectProduct( packageProduct: appProduct, diff --git a/Sources/XToolSupport/SwiftPMBuildDescriber+XcodeProject.swift b/Sources/XToolSupport/SwiftPMBuildDescriber+XcodeProject.swift index 7785beb2..f8b57316 100644 --- a/Sources/XToolSupport/SwiftPMBuildDescriber+XcodeProject.swift +++ b/Sources/XToolSupport/SwiftPMBuildDescriber+XcodeProject.swift @@ -227,6 +227,14 @@ extension SwiftPMBuildDescriber { let projectURL = projectDir.appending(path: "\(app.product).xcodeproj") let project = XcodeProject(project: schema, url: projectURL) try project.write() + let appReference = XcodeScheme.TargetReference( + projectURL: project.url, + objectID: targetIDs[0].rawValue, + name: app.targetName, + buildableName: "\(app.product).app", + ) + try XcodeScheme(name: app.targetName, buildTargets: [appReference], runnable: appReference) + .write(in: project.url, shared: true) let workspaceURL = xtoolDir.appending(path: "\(app.product).xcworkspace") let workspace = XcodeWorkspace(children: [ diff --git a/Sources/XToolSupport/XToolBuildServer.swift b/Sources/XToolSupport/XToolBuildServer.swift index 8d0c2a0a..4f0f6fa3 100644 --- a/Sources/XToolSupport/XToolBuildServer.swift +++ b/Sources/XToolSupport/XToolBuildServer.swift @@ -24,6 +24,7 @@ actor XToolBuildServer: QueueBasedMessageHandler { private let connectionToClient: any LanguageServerProtocol.Connection private var coordinator: BuildCoordinator? private let triple: String + private let scheme: String? private let exitHandler: @Sendable (Int) -> Void private var packageRoot: URL? private var headersByTargetGUID: [String: Set] = [:] @@ -34,10 +35,12 @@ actor XToolBuildServer: QueueBasedMessageHandler { init( triple: String, + scheme: String? = nil, connectionToClient: any LanguageServerProtocol.Connection, exitHandler: @escaping @Sendable (Int) -> Void ) { self.triple = triple + self.scheme = scheme self.exitHandler = exitHandler self.connectionToClient = connectionToClient self.connectionToUnderlyingBuildServer = LocalConnection(receiverName: "underlying-swift-build-server") @@ -57,7 +60,7 @@ actor XToolBuildServer: QueueBasedMessageHandler { throw Console.Error("`xtool dev build-server` requires Swift 6.4 or later") } let project = try await XToolProject.discover(start: directory) - let description = try await project.describe(configuration: .debug, triple: triple) + let description = try await project.describe(configuration: .debug, triple: triple, scheme: scheme) let pif = description.pif let coordinator = try await BuildCoordinator(description: description) // The transport may have closed while the coordinator was being created. diff --git a/Sources/XToolSupport/XToolProject.swift b/Sources/XToolSupport/XToolProject.swift index 2b3b393c..057fe229 100644 --- a/Sources/XToolSupport/XToolProject.swift +++ b/Sources/XToolSupport/XToolProject.swift @@ -13,12 +13,21 @@ enum XToolProject { let manifest: URL } + struct Xcode { + let base: Base + /// The `.xcworkspace` or `.xcodeproj` to build. + let container: URL + } + case swiftPM(SwiftPM) + case xcode(Xcode) var base: Base { switch self { case .swiftPM(let swiftPM): return swiftPM.base + case .xcode(let xcode): + return xcode.base } } @@ -48,6 +57,10 @@ enum XToolProject { version: 1 orgID: com.example """ + case .xcode: + defaultSchema = """ + version: 1 + """ } try Data((defaultSchema + "\n").utf8).write(to: configPath) } @@ -60,26 +73,66 @@ enum XToolProject { func describe( configuration: BuildConfiguration, - triple: String + triple: String, + scheme: String? = nil ) async throws -> BuildDescription { + let scheme = scheme ?? base.schema.scheme let pif: XPIF.Container + var targetSelection: BuildDescription.TargetSelection? switch self { case .swiftPM(let swiftPM): + guard scheme == nil else { throw Console.Error("Scheme selection requires an Xcode project or workspace.") } pif = try await SwiftPMBuildDescriber(project: swiftPM).describe() + case .xcode(let xcode): + (pif, targetSelection) = try await XcodeBuildDescriber(project: xcode, scheme: scheme).describe() } return BuildDescription( directory: base.rootDirectory, pif: pif, configuration: configuration, triple: triple, + targetSelection: targetSelection, ) } } extension XToolProject.Base { fileprivate func project() async throws -> XToolProject { + if let path = schema.project { + let directory = URL(fileURLWithPath: rootDirectory.path, isDirectory: true) + let url = URL(fileURLWithPath: path, relativeTo: directory).standardizedFileURL + guard FileManager.default.fileExists(atPath: url.path) else { + throw Console.Error("xtool.yml: Could not find project '\(path)'.") + } + if url.lastPathComponent == "Package.swift" { + return .swiftPM(.init(base: self, manifest: url)) + } else { + return .xcode(.init(base: self, container: url)) + } + } + let contents = try FileManager.default.contentsOfDirectory(at: rootDirectory, includingPropertiesForKeys: nil) + let workspaces = contents.filter { $0.pathExtension == "xcworkspace" } + switch workspaces.count { + case 0: + break + case 1: + return .xcode(.init(base: self, container: workspaces[0])) + default: + throw Console.Error("Found multiple Xcode workspaces in this directory.") + } + + let projects = contents.filter { $0.pathExtension == "xcodeproj" } + switch projects.count { + case 0: + break + case 1: + return .xcode(.init(base: self, container: projects[0])) + default: + throw Console.Error("Found multiple Xcode projects in this directory.") + } + if let manifest = contents.first(where: { let name = $0.lastPathComponent return name == "Package.swift" || (name.hasPrefix("Package@") && name.hasSuffix(".swift")) @@ -87,6 +140,6 @@ extension XToolProject.Base { return .swiftPM(.init(base: self, manifest: manifest)) } - throw Console.Error("Could not find Package.swift in this directory.") + throw Console.Error("Could not find Package.swift, an Xcode workspace, or an Xcode project in this directory.") } } diff --git a/Sources/XToolSupport/XcodeBuildDescriber.swift b/Sources/XToolSupport/XcodeBuildDescriber.swift new file mode 100644 index 00000000..4d470c65 --- /dev/null +++ b/Sources/XToolSupport/XcodeBuildDescriber.swift @@ -0,0 +1,73 @@ +import XCLIUtils +import XPIF +import Foundation +import XcodeProjects +import XcodePIFConversion +import XUtils + +struct XcodeBuildDescriber { + let project: XToolProject.Xcode + var scheme: String? + + func describe() async throws -> (pif: XPIF.Container, targetSelection: BuildDescription.TargetSelection?) { + let container = project.container + let builder: XcodePIFBuilder + if container.pathExtension == "xcworkspace" { + builder = try XcodePIFBuilder(workspace: XcodeWorkspace(url: container)) + } else { + builder = try XcodePIFBuilder(project: XcodeProject(url: container)) + } + let packages = try await XcodePackagePIFBuilder( + builder: builder, + rootDirectory: project.base.rootDirectory + ).build() + let workspace = try builder.build(packageProjects: packages) + Signposter.track("Generated Xcode PIF") + let pif = try describe(pif: XPIF.Container(workspace: workspace)) + var targetSelection: BuildDescription.TargetSelection? + if let scheme { + // TODO: swb: use bona fide scheme request + let selection = try builder.scheme(named: scheme) + var seen: Set = [] + let targets = try selection.buildTargets.map { try builder.targetGUID(for: $0) } + .filter { seen.insert($0).inserted } + guard !targets.isEmpty else { throw Console.Error("Scheme '\(scheme)' has no targets enabled for running.") } + let runnable = try selection.runnable.map { try builder.targetGUID(for: $0) } + let apps = workspace.projects.flatMap(\.targets).compactMap { base -> XPIF.Target? in + guard case .target(let target) = base, target.productType == .application, + runnable.map({ $0 == target.id }) ?? targets.contains(target.id) else { return nil } + return target + } + guard apps.count == 1, let app = apps.first else { + throw Console.Error("Expected one application target in scheme '\(scheme)', found \(apps.count).") + } + // A runnable can be built indirectly by an aggregate target. Reject schemes + // that would build other targets and then deploy an unrelated, stale app. + let allTargets = Dictionary(uniqueKeysWithValues: workspace.projects.flatMap(\.targets).map { ($0.id, $0) }) + var pending = targets + var reachable: Set = [] + while let target = pending.popLast() { + guard reachable.insert(target).inserted else { continue } + pending += allTargets[target]?.common.dependencies.map(\.targetId) ?? [] + } + guard reachable.contains(app.id) else { + throw Console.Error("Scheme '\(scheme)' does not build its runnable application for running.") + } + targetSelection = .init( + application: app.id, buildTargets: targets, buildImplicitDependencies: selection.buildImplicitDependencies + ) + } + return (pif, targetSelection) + } + + func describe(pif: XPIF.Container) throws -> XPIF.Container { + var pif = pif + #if !os(macOS) + pif.workspace.projects = pif.workspace.projects.map { $0.withoutRelocatableLinkage() } + #endif + let projectIndex = try pif.workspace.projects.indices.first + .orThrow(Console.Error("Xcode workspace contains no projects.")) + try pif.workspace.addUmbrellaTarget(toProjectAt: projectIndex) + return pif + } +} diff --git a/Sources/XToolSupport/XcodePackagePIFBuilder.swift b/Sources/XToolSupport/XcodePackagePIFBuilder.swift new file mode 100644 index 00000000..87102bde --- /dev/null +++ b/Sources/XToolSupport/XcodePackagePIFBuilder.swift @@ -0,0 +1,158 @@ +import XCLIUtils +import XUtils +import Foundation +import Crypto +import Dependencies +import XcodePIFConversion +import XcodeProjects +import XPIF + +/// Resolves all Xcode package references as one SwiftPM graph. +struct XcodePackagePIFBuilder { + let builder: XcodePIFBuilder + /// Directory under which xtool stores its package resolution scratch space. + let rootDirectory: URL + + func build() async throws -> [XPIF.Project] { + guard !builder.packageReferences.isEmpty else { return [] } + let references = builder.packageReferences + let manifest = try Self.manifest(references: references) + let rootPaths = Self.rootPaths(references) + // SwiftPM's checkout paths appear in PIF. Keep the workspace stable across builds. + let key = SHA256.hash(data: Data(builder.packageWorkspaceURL.path.utf8)) + .map { String(format: "%02x", $0) }.joined() + let directory = rootDirectory.appending(path: "xtool/.xtool-tmp/packages/\(key)") + try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true) + let manifestURL = directory.appending(path: "Package.swift") + if (try? String(contentsOf: manifestURL, encoding: .utf8)) != manifest { + try manifest.write(to: manifestURL, atomically: true, encoding: .utf8) + } + let resolved = builder.packageWorkspaceURL.appending(path: "xcshareddata/swiftpm/Package.resolved") + if let data = try? Data(contentsOf: resolved) { + try data.write(to: directory.appending(path: "Package.resolved"), options: .atomic) + } + @Dependency(\.swiftPMPIFDumper) var dumper + let pif = try await dumper.dump(packagePath: directory) + let defaults = try await XcodePackageSDKDefaults.load() + let allTargets = Dictionary(uniqueKeysWithValues: pif.workspace.projects.flatMap(\.targets).map { ($0.id, $0) }) + var pending = allTargets.values.compactMap { base -> XPIF.GUID? in + guard case .target(let target) = base, target.id.value.hasPrefix("PACKAGE-PRODUCT:"), + ![.unitTest, .uiTest, .swiftpmTestRunner].contains(target.productType) else { return nil } + return target.id + } + var reachable: Set = [] + while let id = pending.popLast() { + guard reachable.insert(id).inserted, let target = allTargets[id] else { continue } + pending += target.common.dependencies.map(\.targetId) + if case .target(let target) = target, let dynamic = target.dynamicTargetVariantId { pending.append(dynamic) } + } + return pif.workspace.projects.filter { + $0.id != "AGGREGATE" && URL(filePath: $0.projectDir).resolvingSymlinksInPath().path + != directory.resolvingSymlinksInPath().path + }.map { project in + var project = project + // SwiftPM CLI includes test products; Xcode includes only the product dependency graph. + project.targets.removeAll { !reachable.contains($0.id) } + // Xcode emits a resource bundle before its first consuming module. The + // SwiftPM CLI emits it afterwards; project target references keep their order. + var emitted: Set = [] + project.targetSerializationOrder = (project.targetSerializationOrder ?? project.targets.map(\.id)) + .filter { reachable.contains($0) }.flatMap { id -> [XPIF.GUID] in + let resources = id.value.hasPrefix("PACKAGE-TARGET:") ? allTargets[id]?.common.dependencies.map(\.targetId) + .filter { $0.value.hasPrefix("PACKAGE-RESOURCE:") } ?? [] : [] + return (resources + [id]).filter { emitted.insert($0).inserted } + } + Self.configureProject(&project, defaults: defaults, + isRoot: rootPaths.contains(URL(filePath: project.projectDir).resolvingSymlinksInPath().path)) + for index in project.targets.indices { + for config in project.targets[index].common.buildConfigs.indices { + var configuration = project.targets[index].common.buildConfigs[config] + // These are standalone CLI documentation and non-Darwin linker settings, + // not settings supplied by Xcode's package PIF integration. + for key in configuration.settings.values.keys where + ["DOCC_EXTRACT_PROJECT_HEADERS_DOCUMENTATION", "SYMBOL_GRAPH_EXTRACTOR_OUTPUT_DIR", + "TAPI_EXTRACT_API_OUTPUT_DIR"].contains(key) + || key.hasPrefix("SWIFT_DISABLE_COMPILATION_FOR_STATIC_LINKING_WHEN_ANY_TARGET_IS_PROMOTED_TO_DYNAMIC[") { + configuration.settings.values.removeValue(forKey: key) + } + if configuration.impartedBuildProperties.settings.values["LD_RUNPATH_SEARCH_PATHS"] + == .array(["$(RPATH_ORIGIN)", "$(inherited)"]) { + configuration.impartedBuildProperties.settings.values.removeValue(forKey: "LD_RUNPATH_SEARCH_PATHS") + } + project.targets[index].common.buildConfigs[config] = configuration + } + } + return project + } + } + + private static func rootPaths(_ references: [XcodePIFBuilder.PackageReference]) -> Set { + Set(references.filter(\.isRoot).compactMap { reference -> String? in + guard case .local(let package) = reference.package.location else { return nil } + return URL(fileURLWithPath: package.path, relativeTo: reference.sourceRoot) + .standardizedFileURL.resolvingSymlinksInPath().path + }) + } + + private static func configureProject(_ project: inout XPIF.Project, defaults: XcodePackageSDKDefaults, isRoot: Bool) { + for index in project.buildConfigs.indices { + defaults.apply(to: &project.buildConfigs[index].settings) + if isRoot { project.buildConfigs[index].settings.values.removeValue(forKey: "BUILD_SERVER_PROTOCOL_TARGET_TAGS") } + } + } + + static func manifest(references: [XcodePIFBuilder.PackageReference]) throws -> String { + var dependencies: [String: String] = [:] + for reference in references { + let identity: String + var arguments: String + switch reference.package.location { + case .local(let local): + let sourceRoot = URL(filePath: reference.sourceRoot.path, directoryHint: .isDirectory) + let url = URL(fileURLWithPath: local.path, relativeTo: sourceRoot) + .standardizedFileURL.resolvingSymlinksInPath() + identity = url.lastPathComponent.lowercased() + arguments = "path: \(String(reflecting: url.path))" + case .remote(let remote): + let component = (remote.repositoryURL as NSString).lastPathComponent + identity = (component.hasSuffix(".git") ? String(component.dropLast(4)) : component).lowercased() + guard let constraint = remote.versionConstraint else { + throw Console.Error("Swift package '\(remote.repositoryURL)' needs a version, branch, or revision requirement.") + } + arguments = "url: \(String(reflecting: remote.repositoryURL)), " + requirement(constraint) + } + if !reference.package.traits.isEmpty { + arguments += ", traits: [\(reference.package.traits.sorted().map { String(reflecting: $0) }.joined(separator: ", "))]" + } + let dependency = ".package(\(arguments))" + if let existing = dependencies[identity], existing != dependency { + throw Console.Error("Conflicting references for Swift package '\(identity)'.") + } + dependencies[identity] = dependency + } + let declarations = dependencies.sorted { $0.key < $1.key }.map { " \($0.value)," }.joined(separator: "\n") + return """ + // swift-tools-version: 6.1 + import PackageDescription + + let package = Package( + name: "XtoolDependencies", + dependencies: [ + \(declarations) + ] + ) + + """ + } + + private static func requirement(_ constraint: XCSchema.SwiftPackageVersionConstraint) -> String { + switch constraint { + case .revision(let value): "revision: \(String(reflecting: value))" + case .branch(let value): "branch: \(String(reflecting: value))" + case .version(let value): "exact: \(String(reflecting: value))" + case .versionRange(let min, let max): "\(String(reflecting: min))..<\(String(reflecting: max))" + case .upToNextMinorVersion(let value): ".upToNextMinor(from: \(String(reflecting: value)))" + case .upToNextMajorVersion(let value): "from: \(String(reflecting: value))" + } + } +} diff --git a/Sources/XToolSupport/XcodePackageSDKDefaults.swift b/Sources/XToolSupport/XcodePackageSDKDefaults.swift new file mode 100644 index 00000000..6eef6d17 --- /dev/null +++ b/Sources/XToolSupport/XcodePackageSDKDefaults.swift @@ -0,0 +1,71 @@ +import Foundation +import XUtils +import XPIF + +/// Xcode supplies platform version mappings and device families to libSwiftPM. +struct XcodePackageSDKDefaults { + var versionMaps: [String: [String: String]] = [:] + var deviceFamilies: Set = [] + + private static let alignedPlatforms: [(String, XPIF.BuildSettings.Platform, XPIF.BuildSettings.SingleValueSetting)] = [ + ("tvOS", .tvOS, .TVOS_DEPLOYMENT_TARGET), + ("watchOS", .watchOS, .WATCHOS_DEPLOYMENT_TARGET), + ("xrOS", .xrOS, .XROS_DEPLOYMENT_TARGET), + ] + + static func load() async throws -> Self { + let developerDirectory: URL? + #if os(macOS) + let resourcePath = try await d.toolchain.runtimeResourcePath + // /Toolchains//usr/lib/swift + developerDirectory = URL(filePath: resourcePath.string) + .deletingLastPathComponent().deletingLastPathComponent().deletingLastPathComponent() + .deletingLastPathComponent().deletingLastPathComponent() + #else + developerDirectory = try DarwinSDK.current()?.developerDirectory + #endif + guard let developerDirectory else { return Self() } + let platforms = try FileManager.default.contentsOfDirectory( + at: developerDirectory.appending(path: "Platforms"), + includingPropertiesForKeys: nil + ) + .filter { $0.pathExtension == "platform" } + var result = Self() + for platform in platforms { + let sdksURL = platform.appending(path: "Developer/SDKs") + let sdks = (try? FileManager.default.contentsOfDirectory(at: sdksURL, includingPropertiesForKeys: nil)) ?? [] + for sdk in sdks where sdk.pathExtension == "sdk" { + guard let data = try? Data(contentsOf: sdk.appending(path: "SDKSettings.plist")), + let plist = try PropertyListSerialization.propertyList(from: data, format: nil) as? [String: Any], + let targets = plist["SupportedTargets"] as? [String: [String: Any]] else { continue } + for (name, versions) in plist["VersionMap"] as? [String: [String: String]] ?? [:] { + result.versionMaps[name] = versions + } + for target in targets.values { + for family in target["DeviceFamilies"] as? [[String: String]] ?? [] { + if let value = family["Identifier"].flatMap(Int.init) { result.deviceFamilies.insert(value) } + } + } + } + } + return result + } + + func apply(to settings: inout XPIF.BuildSettings) { + if let iOS = settings[.IPHONEOS_DEPLOYMENT_TARGET] { + for (family, platform, setting) in Self.alignedPlatforms { + // SwiftPM emits this key even for empty options, but only for declared platforms. + guard settings[.SPECIALIZATION_SDK_OPTIONS, platform] == nil, + let versions = versionMaps["iOS_\(family)"], + let version = versions.keys.filter({ $0.compare(iOS, options: .numeric) != .orderedDescending }) + .max(by: { $0.compare($1, options: .numeric) == .orderedAscending }), + let mapped = versions[version], let current = settings[setting], + mapped.compare(current, options: .numeric) != .orderedAscending else { continue } + settings[setting] = mapped + } + } + if !deviceFamilies.isEmpty { + settings[single: "TARGETED_DEVICE_FAMILY"] = deviceFamilies.sorted().map(String.init).joined(separator: ",") + } + } +} diff --git a/Sources/XcodePIFConversion/StringEncoding+IANA.swift b/Sources/XcodePIFConversion/StringEncoding+IANA.swift new file mode 100644 index 00000000..93a3363f --- /dev/null +++ b/Sources/XcodePIFConversion/StringEncoding+IANA.swift @@ -0,0 +1,29 @@ +import Foundation +#if canImport(Darwin) +import CoreFoundation +#endif + +extension String.Encoding { + var backportedIANAName: String? { + let name: String? + #if canImport(Darwin) + if #available(macOS 26.4, iOS 26.4, tvOS 26.4, watchOS 26.4, visionOS 26.4, *) { + name = self.ianaName + } else { + name = self.legacyIANAName + } + #else + name = self.ianaName + #endif + // Foundation's casing differs between platforms; retain the existing PIF representation. + return name?.lowercased() + } + + #if canImport(Darwin) + private var legacyIANAName: String? { + let cfEncoding = CFStringConvertNSStringEncodingToEncoding(self.rawValue) + guard let charset = CFStringConvertEncodingToIANACharSetName(cfEncoding) else { return nil } + return charset as String + } + #endif +} diff --git a/Sources/XcodePIFConversion/XcodeFileType.swift b/Sources/XcodePIFConversion/XcodeFileType.swift new file mode 100644 index 00000000..808e97b8 --- /dev/null +++ b/Sources/XcodePIFConversion/XcodeFileType.swift @@ -0,0 +1,168 @@ +import Foundation + +/// Xcode's file type identifiers, as detected for files discovered in synchronized folders. +/// +/// The tables mirror what `xcodebuild -dumpPIF` reports. Xcode also inspects file contents +/// (e.g. Mach-O headers, XML plists, shebangs); we only use names, so binaries are typed by +/// their conventional extension and unrecognized files are plain `file`s. +enum XcodeFileType { + static func forFile(named name: String) -> String? { + if let type = fileNames[name] { return type } + return lookup((name as NSString).pathExtension, in: fileExtensions) + } + + static func forDirectory(named name: String) -> String? { + lookup((name as NSString).pathExtension, in: directoryExtensions) + } + + /// The type followed by each of the types it's based on, most specific first. + static func ancestry(of type: String) -> [String] { + var result: [String] = [] + var current: String? = type + while let type = current { + result.append(type) + if let parent = parents[type] { + current = parent + } else if let dot = type.lastIndex(of: ".") { + current = String(type[.. String? { + guard !pathExtension.isEmpty else { return nil } + return table[pathExtension] ?? table[pathExtension.lowercased()] + } + + private static let fileNames: [String: String] = [ + "Makefile": "sourcecode.make", "makefile": "sourcecode.make", + "AppIntents.vocabulary": "text.plist.vocabulary", "project.pbxproj": "text.pbxproject", + ] + + private static let fileExtensions: [String: String] = [ + "a": "archive.ar", "lib": "archive.ar", "asdictionary": "archive.asdictionary", "hqx": "archive.binhex", + "ear": "archive.ear", "gz": "archive.gzip", "jar": "archive.jar", "bin": "archive.macbinary", + "metallib": "archive.metal-library", "ppob": "archive.ppob", "rsrc": "archive.rsrc", "view": "archive.rsrc", + "sit": "archive.stuffit", "tar": "archive.tar", "war": "archive.war", "zip": "archive.zip", + "aiff": "audio.aiff", "cdda": "audio.aiff", "au": "audio.au", "mid": "audio.midi", "midi": "audio.midi", + "mp3": "audio.mp3", "wav": "audio.wav", "instrpkg": "com.apple.instruments.package-definition", + "air": "compiled.air", "class": "compiled.javaclass", "dll": "compiled.mach-o.dylib", + "dylib": "compiled.mach-o.dylib", "o": "compiled.mach-o.objfile", "rcx": "compiled.rcx", + "intentdefinition": "file.intentdefinition", "mlkitmodel": "file.mlmodel", "mlmodel": "file.mlmodel", + "referenceobject": "file.referenceobject", "scnp": "file.scp", "sks": "file.sks", "skybox": "file.skybox", + "storyboard": "file.storyboard", "uicatalog": "file.uicatalog", "xib": "file.xib", "bmp": "image.bmp", + "gif": "image.gif", "icns": "image.icns", "ico": "image.ico", "jpeg": "image.jpeg", "jpg": "image.jpeg", + "pdf": "image.pdf", "pct": "image.pict", "pict": "image.pict", "png": "image.png", "tif": "image.tiff", + "tiff": "image.tiff", "markdown": "net.daringfireball.markdown", "md": "net.daringfireball.markdown", + "mdown": "net.daringfireball.markdown", "text": "net.daringfireball.markdown", "ada": "sourcecode.ada", + "adb": "sourcecode.ada", "ads": "sourcecode.ada", "applescript": "sourcecode.applescript", + "s": "sourcecode.asm", "asm": "sourcecode.asm.asm", "ll": "sourcecode.asm.llvm", "llx": "sourcecode.asm.llvm", + "c": "sourcecode.c.c", "i": "sourcecode.c.c.preprocessed", "h": "sourcecode.c.h", "pch": "sourcecode.c.h", + "m": "sourcecode.c.objc", "mi": "sourcecode.c.objc.preprocessed", "clp": "sourcecode.clips", + "C": "sourcecode.cpp.cpp", "c++": "sourcecode.cpp.cpp", "cc": "sourcecode.cpp.cpp", "cp": "sourcecode.cpp.cpp", + "cpp": "sourcecode.cpp.cpp", "cxx": "sourcecode.cpp.cpp", "tcc": "sourcecode.cpp.cpp", + "ii": "sourcecode.cpp.cpp.preprocessed", "H": "sourcecode.cpp.h", "h++": "sourcecode.cpp.h", + "hh": "sourcecode.cpp.h", "hp": "sourcecode.cpp.h", "hpp": "sourcecode.cpp.h", "hxx": "sourcecode.cpp.h", + "ipp": "sourcecode.cpp.h", "pch++": "sourcecode.cpp.h", "M": "sourcecode.cpp.objcpp", + "mm": "sourcecode.cpp.objcpp", "mii": "sourcecode.cpp.objcpp.preprocessed", "d": "sourcecode.dtrace", + "dylan": "sourcecode.dylan", "lid": "sourcecode.dylan", "exp": "sourcecode.exports", "f": "sourcecode.fortran", + "for": "sourcecode.fortran", "f77": "sourcecode.fortran.f77", "f90": "sourcecode.fortran.f90", + "f95": "sourcecode.fortran.f90", "ctrl": "sourcecode.glsl", "eval": "sourcecode.glsl", + "frag": "sourcecode.glsl", "fragment": "sourcecode.glsl", "fs": "sourcecode.glsl", "fsh": "sourcecode.glsl", + "geom": "sourcecode.glsl", "geometry": "sourcecode.glsl", "gs": "sourcecode.glsl", "gsh": "sourcecode.glsl", + "vert": "sourcecode.glsl", "vertex": "sourcecode.glsl", "vs": "sourcecode.glsl", "vsh": "sourcecode.glsl", + "iig": "sourcecode.iig", "java": "sourcecode.java", "javascript": "sourcecode.javascript", + "js": "sourcecode.javascript", "jscript": "sourcecode.javascript", "l": "sourcecode.lex", + "lm": "sourcecode.lex", "lmm": "sourcecode.lex", "lp": "sourcecode.lex", "lpp": "sourcecode.lex", + "lxx": "sourcecode.lex", "gmk": "sourcecode.make", "mak": "sourcecode.make", "make": "sourcecode.make", + "metal": "sourcecode.metal", "defs": "sourcecode.mig", "mig": "sourcecode.mig", "map": "sourcecode.module-map", + "modulemap": "sourcecode.module-map", "nasm": "sourcecode.nasm", "nqc": "sourcecode.nqc", + "cl": "sourcecode.opencl", "inc": "sourcecode.pascal", "p": "sourcecode.pascal", "pas": "sourcecode.pascal", + "pp": "sourcecode.pascal", "proto": "sourcecode.protobuf", "r": "sourcecode.rez", "rez": "sourcecode.rez", + "swift": "sourcecode.swift", "tbd": "sourcecode.text-based-dylib-definition", "ts": "sourcecode.typescript", + "tsx": "sourcecode.typescript", "y": "sourcecode.yacc", "ym": "sourcecode.yacc", "ymm": "sourcecode.yacc", + "yp": "sourcecode.yacc", "ypp": "sourcecode.yacc", "yxx": "sourcecode.yacc", "yy": "sourcecode.yacc", + "txt": "text", "apinotes": "text.apinotes", "css": "text.css", "htm": "text.html", "html": "text.html", + "jsp": "text.html.other", "rhtml": "text.html.other", "shtml": "text.html.other", "json": "text.json", + "xcstrings": "text.json.xcstrings", "1": "text.man", "pbxproj": "text.pbxproject", "dict": "text.plist", + "privacy": "text.plist.app-privacy", "entitlements": "text.plist.entitlements", + "classdescription": "text.plist.ibClassDescription", "classdescriptions": "text.plist.ibClassDescription", + "pbfilespec": "text.plist.pbfilespec", "pblangspec": "text.plist.pblangspec", + "scriptSuite": "text.plist.scriptSuite", "scriptTerminology": "text.plist.scriptTerminology", + "strings": "text.plist.strings", "stringsdict": "text.plist.stringsdict", + "xcappextensionpoints": "text.plist.xcappextensionpoints", "xcbuildrules": "text.plist.xcbuildrules", + "xclangspec": "text.plist.xclangspec", "xcspec": "text.plist.xcspec", "xcsynspec": "text.plist.xcsynspec", + "xctxtmacro": "text.plist.xctxtmacro", "plist": "text.plist.xml", "rtf": "text.rtf", "csh": "text.script.csh", + "perl": "text.script.perl", "pl": "text.script.perl", "pm": "text.script.perl", "php": "text.script.php", + "php3": "text.script.php", "php4": "text.script.php", "phtml": "text.script.php", "py": "text.script.python", + "rb": "text.script.ruby", "rbw": "text.script.ruby", "sh": "text.script.sh", + "worksheet": "text.script.worksheet", "xcconfig": "text.xcconfig", "xcfilelist": "text.xcfilelist", + "dtd": "text.xml", "xconf": "text.xml", "xhtml": "text.xml", "xmap": "text.xml", "xml": "text.xml", + "xsl": "text.xml", "xslt": "text.xml", "xsp": "text.xml", "dae": "text.xml.dae", + "archivingdescription": "text.xml.ibArchivingDescription", "yaml": "text.yaml", "yml": "text.yaml", + "avi": "video.avi", "mpeg": "video.mpeg", "mpg": "video.mpeg", "qtz": "video.quartz-composer", + "moov": "video.quicktime", "mov": "video.quicktime", "storyboardc": "wrapper.storyboardc", + "xcdatamodel": "wrapper.xcdatamodel", "xcmappingmodel": "wrapper.xcmappingmodel", + ] + + private static let directoryExtensions: [String: String] = [ + "asdictionary": "archive.asdictionary", "hqx": "archive.binhex", "ear": "archive.ear", "gz": "archive.gzip", + "jar": "archive.jar", "bin": "archive.macbinary", "metallib": "archive.metal-library", "ppob": "archive.ppob", + "rsrc": "archive.rsrc", "view": "archive.rsrc", "sit": "archive.stuffit", "tar": "archive.tar", + "war": "archive.war", "zip": "archive.zip", "aiff": "audio.aiff", "cdda": "audio.aiff", "au": "audio.au", + "mid": "audio.midi", "midi": "audio.midi", "mp3": "audio.mp3", "wav": "audio.wav", + "instrdst": "com.apple.instruments.instrdst", "air": "compiled.air", "class": "compiled.javaclass", + "o": "compiled.object-library", "rcx": "compiled.rcx", "playground": "file.playground", "scnp": "file.scp", + "sks": "file.sks", "skybox": "file.skybox", "storyboard": "file.storyboard", "uicatalog": "file.uicatalog", + "xcplaygroundpage": "file.xcplaygroundpage", "xib": "file.xib", "aimodel": "folder.aimodel", + "xcassets": "folder.assetcatalog", "docc": "folder.documentationcatalog", "icon": "folder.iconcomposer.icon", + "iconset": "folder.iconset", "imagecatalog": "folder.imagecatalog", "mlpackage": "folder.mlpackage", + "rkassets": "folder.rkassets", "atlas": "folder.skatlas", "xcstickers": "folder.stickers", "bmp": "image.bmp", + "gif": "image.gif", "icns": "image.icns", "ico": "image.ico", "jpeg": "image.jpeg", "jpg": "image.jpeg", + "pdf": "image.pdf", "pct": "image.pict", "pict": "image.pict", "png": "image.png", "tif": "image.tiff", + "tiff": "image.tiff", "avi": "video.avi", "mpeg": "video.mpeg", "mpg": "video.mpeg", + "qtz": "video.quartz-composer", "moov": "video.quicktime", "mov": "video.quicktime", "fs": "wrapper", + "pluginkit": "wrapper.app-extension", "app": "wrapper.application", "artifactbundle": "wrapper.artifactbundle", + "dext": "wrapper.driver-extension", "dSYM": "wrapper.dsym", "dsym": "wrapper.dsym", + "appex": "wrapper.extensionkit-extension", "framework": "wrapper.framework", "htmld": "wrapper.htmld", + "mpkg": "wrapper.installer-mpkg", "pkg": "wrapper.installer-pkg", "kext": "wrapper.kernel-extension", + "nib": "wrapper.nib", "xcode": "wrapper.pb-project", "xcodeproj": "wrapper.pb-project", + "xctarget": "wrapper.pb-target", "bundle": "wrapper.plug-in", "rtfd": "wrapper.rtfd", + "scnassets": "wrapper.scnassets", "scncache": "wrapper.scncache", "mdimporter": "wrapper.spotlight-importer", + "storyboardc": "wrapper.storyboardc", "systemextension": "wrapper.system-extension", + "xcworkspace": "wrapper.workspace", "xcclassmodel": "wrapper.xcclassmodel", + "xcdatamodel": "wrapper.xcdatamodel", "xcframework": "wrapper.xcframework", + "xcmappingmodel": "wrapper.xcmappingmodel", "xpc": "wrapper.xpc-service", + "xcdatamodeld": "wrapper.xcdatamodeld", + ] + + /// Types whose parent isn't the identifier with its last component dropped (`nil` for roots). + private static let parents: [String: String?] = [ + "folder.rkassets": nil, "archive.ear": "archive.jar", "archive.war": "archive.jar", + "archive.ppob": "archive.rsrc", "archive.jar": "archive.zip", "compiled.object-library": "compiled.mach-o", + "archive": "file", "audio": "file", "compiled": "file", "image": "file", "text": "file", "video": "file", + "wrapper": "folder", "folder.assetcatalog": "folder.abstractassetcatalog", + "folder.iconcomposer.icon": "folder.abstractassetcatalog", "folder.stickers": "folder.abstractassetcatalog", + "folder.imagecatalog": "folder.assetcatalog", "file.intentdefinition": "sourcecode", + "file.mlmodel": "sourcecode", "file.referenceobject": "sourcecode", + "net.daringfireball.markdown": "sourcecode", "sourcecode.asm.llvm": "sourcecode", + "wrapper.xcdatamodel": "sourcecode", "wrapper.xcdatamodeld": "sourcecode", + "wrapper.xcmappingmodel": "sourcecode", "sourcecode.cpp": "sourcecode.c", "sourcecode.iig": "sourcecode.c", + "file.swiftpm-manifest": "sourcecode.swift", "sourcecode": "text", "text.plist.strings": "text", + "text.plist.stringsdict": "text", "text.plist.info": "text.plist.xml", + "text.plist.vocabulary": "text.plist.xml", "com.apple.instruments.package-definition": "text.xml", + "text.apinotes": "text.yaml", "com.apple.instruments.instrdst": "wrapper", "file.playground": "wrapper", + "file.xcplaygroundpage": "wrapper", "folder.abstractassetcatalog": "wrapper", "folder.aimodel": "wrapper", + "folder.documentationcatalog": "wrapper", "folder.iconset": "wrapper", "folder.mlpackage": "wrapper", + "folder.skatlas": "wrapper", "wrapper.extensionkit-extension": "wrapper.app-extension", + "wrapper.application": "wrapper.cfbundle", "wrapper.framework": "wrapper.cfbundle", + "wrapper.plug-in": "wrapper.cfbundle", "wrapper.spotlight-importer": "wrapper.cfbundle", + "wrapper.xpc-service": "wrapper.cfbundle", "wrapper.pb-target": "wrapper.pb-project", + "wrapper.kernel-extension": "wrapper.plug-in", "wrapper.system-extension": "wrapper.plug-in", + "wrapper.driver-extension": "wrapper.system-extension", "wrapper.app-extension": "wrapper.xpc-service", + ] +} diff --git a/Sources/XcodePIFConversion/XcodeLoadedProject+References.swift b/Sources/XcodePIFConversion/XcodeLoadedProject+References.swift new file mode 100644 index 00000000..8eead748 --- /dev/null +++ b/Sources/XcodePIFConversion/XcodeLoadedProject+References.swift @@ -0,0 +1,126 @@ +import Foundation +import XcodeProjects +import XPIF + +extension XcodeLoadedProject.Loader { + mutating func convertReferences( + _ items: [XCSchema.Reference], parentID: XPIF.GUID, names: [String], directory: URL? + ) throws -> [XPIF.Reference] { + try items.enumerated().map { index, item in + let fallback = "\(parentID.value)::REF:\(index)" + let ref: XPIF.Reference + let name: String + let url: URL? + switch item { + case .fileReference(let file): + url = xcodeProject.resolve(file.path, relativeTo: directory) + let id = try identifier(file.objectID, fallback: fallback) + let fileRef = try Self.fileReference(file, id: id) + ref = .file(fileRef) + name = fileRef.name + memberships += file.buildFiles.map { (id, $0) } + case .group(let group): + url = xcodeProject.resolve(group.path, relativeTo: directory) + let id = try identifier(group.objectID, fallback: fallback) + name = group.name + ref = .group(.init( + id: id, path: group.path.path, pathBase: Self.pathBase(group.path.base), name: name, + subitems: try convertReferences(group.children, parentID: id, names: names + [name], directory: url) + )) + case .variantGroup(let group): + url = xcodeProject.resolve(group.path, relativeTo: directory) + let id = try identifier(group.objectID, fallback: fallback) + name = group.name + var children = try convertReferences(group.children.map(XCSchema.Reference.fileReference), + parentID: id, names: names + [name], directory: url) + for index in children.indices { + if case .file(var file) = children[index] { + let components = file.path.components(separatedBy: "/") + file.regionVariantName = components.first(where: { $0.hasSuffix(".lproj") }) + .map { String($0.dropLast(".lproj".count)) } + children[index] = .file(file) + if let entryIndex = references.firstIndex(where: { $0.reference.id == file.id }) { + let entry = references[entryIndex] + references[entryIndex] = .init(names: entry.names, reference: .file(file), url: entry.url) + } + } + } + var converted = XPIF.Group( + id: id, + path: group.path.path, + pathBase: Self.pathBase(group.path.base), + name: name, + subitems: children, + ) + converted.kind = .variantGroup + ref = .group(converted) + memberships += group.buildFiles.map { (id, $0) } + case .versionGroup(let group): + url = xcodeProject.resolve(group.path, relativeTo: directory) + let id = try identifier(group.objectID, fallback: fallback) + name = group.name + var converted = XPIF.Group( + id: id, + path: group.path.path, + pathBase: Self.pathBase(group.path.base), + name: name, + subitems: try convertReferences( + group.children.map(XCSchema.Reference.fileReference), + parentID: id, + names: names + [name], + directory: url, + ) + ) + converted.kind = .versionGroup + ref = .group(converted) + memberships += group.buildFiles.map { (id, $0) } + case .folder(let folder): + guard let directory = xcodeProject.resolve(folder.path, relativeTo: directory) else { + throw ConversionError("Cannot resolve synchronized folder: \(folder.path.path)") + } + url = directory + let id = try identifier(folder.objectID, fallback: fallback) + name = (folder.path.path as NSString).lastPathComponent + let contents = try XcodeSynchronizedFolder( + folder: folder, project: project, identifierPrefix: folder.objectID?.rawValue ?? fallback + ).references(at: directory) + ref = .group(.init( + id: id, path: folder.path.path, pathBase: Self.pathBase(folder.path.base), name: name, + subitems: try convertReferences(contents, parentID: id, names: names + [name], directory: directory) + )) + } + references.append(.init(names: names + [name], reference: ref, url: url)) + return ref + } + } + + static func fileReference(_ file: XCSchema.FileReference, id: XPIF.GUID) throws -> XPIF.FileReference { + var ref = XPIF.FileReference( + id: id, + path: file.path.path, + pathBase: pathBase(file.path.base), + name: (file.path.path as NSString).lastPathComponent, + fileType: file.explicitFileType?.rawValue, + ) + ref.expectedSignature = file.expectedSignature + ref.fileTextEncoding = try file.textEncoding.map { encoding in + guard let name = encoding.rawValue.backportedIANAName else { + throw ConversionError("Unsupported Xcode file text encoding: \(encoding.rawValue.rawValue)") + } + return name + } + return ref + } + + static func pathBase(_ base: XCSchema.FilePath.Base) -> XPIF.Reference.RefPathBase { + switch base { + case .absolute: .absolute + case .group: .groupDir + case .project: .projectDir + case .developer: .buildSetting("DEVELOPER_DIR") + case .buildProducts: .buildDir + case .sdk: .buildSetting("SDKROOT") + case .sourceRoot(let setting): .buildSetting(setting) + } + } +} diff --git a/Sources/XcodePIFConversion/XcodeLoadedProject.swift b/Sources/XcodePIFConversion/XcodeLoadedProject.swift new file mode 100644 index 00000000..f29a3b8d --- /dev/null +++ b/Sources/XcodePIFConversion/XcodeLoadedProject.swift @@ -0,0 +1,216 @@ +import Foundation +import Crypto +import XcodeProjects +import XPIF + +/// Loaded project metadata, before resolving remote projects. +struct XcodeLoadedProject { + let xcodeProject: XcodeProject + let projectURL: URL + let identifierPrefix: String + let mainGroup: XPIF.Group + let references: [ReferenceEntry] + let memberships: [(XPIF.GUID, XCSchema.ProjectBuildFile)] + let products: [XPIF.GUID: XPIF.GUID] + let usedIDs: Set + + var sourceRoot: URL { xcodeProject.sourceRoot } + var project: XCSchema.Project { xcodeProject.project } + var projectID: XPIF.GUID { + .init(identifierPrefix) + } + + struct ReferenceEntry { + let names: [String] + let reference: XPIF.Reference + let url: URL? + } + + init(xcodeProject: XcodeProject, projectURL: URL, baseURL: URL) throws { + let source = baseURL.pathComponents + let destination = projectURL.pathComponents + let shared = zip(source, destination).prefix { $0 == $1 }.count + let relative = Array(repeating: "..", count: source.count - shared) + destination.dropFirst(shared) + var loader = Loader(xcodeProject: xcodeProject, projectURL: projectURL, + identifierPrefix: Self.digest(relative.joined(separator: "/"))) + self = try loader.load() + } + + private init(loader: Loader, mainGroup: XPIF.Group) { + self.xcodeProject = loader.xcodeProject + self.projectURL = loader.projectURL + self.identifierPrefix = loader.identifierPrefix + self.mainGroup = mainGroup + self.references = loader.references + self.memberships = loader.memberships + self.products = loader.products + self.usedIDs = loader.usedIDs + } + + func guid(_ objectID: XCSchema.ObjectID) -> XPIF.GUID { + Self.guid(objectID, prefix: identifierPrefix) + } + + static func digest(_ value: String) -> String { + Insecure.MD5.hash(data: Data(value.utf8)).map { String(format: "%02x", $0) }.joined() + } + + private static func guid(_ objectID: XCSchema.ObjectID, prefix: String) -> XPIF.GUID { + if let discovered = XcodeSynchronizedFolder.pifGUID(objectID, projectPrefix: prefix) { + return discovered + } + return .init(prefix + digest(objectID.rawValue)) + } + + func resolve(_ ref: XCSchema.GroupTreeReference) throws -> XPIF.Reference { + try Self.resolve(ref, references: references, identifierPrefix: identifierPrefix) + } + + func resolve(_ ref: XCSchema.GroupTreeAnchoredReference) throws -> XPIF.Reference { + let anchor = try resolve(ref.anchor) + guard let relativePath = ref.relativePath else { return anchor } + guard var url = references.first(where: { $0.reference.id == anchor.id })?.url else { + throw ConversionError("Cannot resolve Xcode configuration reference: \(ref)") + } + for component in relativePath.components { + switch component { + case .child(let name): url.append(path: name) + case .relative(.current): break + case .relative(.parent): url.deleteLastPathComponent() + } + } + let matches = references.filter { $0.url?.standardizedFileURL == url.standardizedFileURL } + guard matches.count == 1, let match = matches.first else { + throw ConversionError("Missing or ambiguous Xcode configuration reference: \(ref)") + } + return match.reference + } + + private static func resolve( + _ ref: XCSchema.GroupTreeReference, references: [ReferenceEntry], identifierPrefix: String + ) throws -> XPIF.Reference { + let matches: [ReferenceEntry] + switch ref { + case .objectID(let id): matches = references.filter { $0.reference.id == guid(id, prefix: identifierPrefix) } + case .namePath(let path): + var names: [String] = [] + for component in path.components { + switch component { + case .child(let name): names.append(name) + case .relative(.current): break + case .relative(.parent): + guard !names.isEmpty else { throw ConversionError("Reference escapes the project group: \(ref)") } + names.removeLast() + } + } + matches = references.filter { $0.names == names } + } + guard matches.count == 1, let match = matches.first else { + throw ConversionError("Missing or ambiguous Xcode file reference: \(ref)") + } + return match.reference + } + + func resolveTarget(_ name: String) throws -> XCSchema.CommonTargetProperties { + guard let target = project.targets.first(where: { $0.name == name }) else { + throw ConversionError("Unknown Xcode target: \(name)") + } + return target.commonProperties + } + + func phaseID(_ phase: XCSchema.BuildPhase, target: XCSchema.CommonTargetProperties, index: Int) -> XPIF.GUID { + phase.properties.objectID.map(guid) ?? .init("\(guid(target.objectID).value)::PHASE:\(index)") + } + + func resolvePhase(_ ref: XCSchema.ProjectBuildPhaseReference) throws -> XPIF.GUID { + var matches: [XPIF.GUID] = [] + for target in project.targets { + for (index, phase) in target.commonProperties.buildPhases.enumerated() { + let matchesReference: Bool + switch ref { + case .objectID(let id): matchesReference = phase.properties.objectID == id + case .named(let targetRef, let kind, let name): + matchesReference = target.name == targetRef.targetName && phase.kind == kind && phase.properties.name == name + } + if matchesReference { matches.append(phaseID(phase, target: target.commonProperties, index: index)) } + } + } + guard matches.count == 1, let match = matches.first else { + throw ConversionError("Missing or ambiguous Xcode build phase: \(ref)") + } + return match + } + + struct Loader { + let xcodeProject: XcodeProject + let projectURL: URL + let identifierPrefix: String + var sourceRoot: URL { xcodeProject.sourceRoot } + var project: XCSchema.Project { xcodeProject.project } + var references: [ReferenceEntry] = [] + var memberships: [(XPIF.GUID, XCSchema.ProjectBuildFile)] = [] + var products: [XPIF.GUID: XPIF.GUID] = [:] + var usedIDs: Set = [] + + var projectID: XPIF.GUID { + .init(identifierPrefix) + } + + mutating func load() throws -> XcodeLoadedProject { + let targetNames = project.targets.map(\.name) + guard targetNames.allSatisfy({ !$0.isEmpty }), Set(targetNames).count == targetNames.count else { + throw ConversionError("Xcode project contains empty or duplicate target names.") + } + let name = projectURL.deletingPathExtension().lastPathComponent + guard !name.isEmpty, project.configurations.contains(where: { $0.name == project.defaultConfigurationName }) else { + throw ConversionError("Xcode project must have a name and a valid default build configuration.") + } + _ = try identifier(project.objectID, fallback: projectID.value) + let rootID = try identifier(project.rootGroupDebugID, fallback: "\(projectID.value)::MAINGROUP") + let children = try convertReferences(project.topLevelReferences, parentID: rootID, names: [], directory: sourceRoot) + var mainGroup = XPIF.Group(id: rootID, path: "", pathBase: .groupDir, name: name, subitems: children) + references.append(.init(names: [], reference: .group(mainGroup), url: sourceRoot)) + for target in project.targets { + _ = try identifier(target.commonProperties.objectID, fallback: "") + if let product = target.commonProperties.product { + let ref = try resolve(product) + guard case .file = ref else { throw ConversionError("Target product must be a file: \(product)") } + guard products.updateValue(guid(target.commonProperties.objectID), forKey: ref.id) == nil else { + throw ConversionError("Multiple targets use the same product reference: \(product)") + } + } + } + // PIF registers product references on their targets. Including the same references + // in the group tree would register their GUIDs twice. + mainGroup = removingProducts(from: mainGroup) + return XcodeLoadedProject(loader: self, mainGroup: mainGroup) + } + + func guid(_ objectID: XCSchema.ObjectID) -> XPIF.GUID { + XcodeLoadedProject.guid(objectID, prefix: identifierPrefix) + } + + func resolve(_ ref: XCSchema.GroupTreeReference) throws -> XPIF.Reference { + try XcodeLoadedProject.resolve(ref, references: references, identifierPrefix: identifierPrefix) + } + + func removingProducts(from group: XPIF.Group) -> XPIF.Group { + var result = group + result.subitems = group.subitems.compactMap { reference in + switch reference { + case .file(let file): products[file.id] == nil ? reference : nil + case .group(let child): .group(removingProducts(from: child)) + } + } + return result + } + + mutating func identifier(_ objectID: XCSchema.ObjectID?, fallback: String) throws -> XPIF.GUID { + let id = objectID.map(guid) ?? XPIF.GUID(fallback) + guard objectID?.rawValue.isEmpty != true, !id.value.isEmpty, usedIDs.insert(id).inserted else { + throw ConversionError("Empty or duplicate Xcode object identifier: \(id.value)") + } + return id + } + } +} diff --git a/Sources/XcodePIFConversion/XcodePIFBuilder+Schemes.swift b/Sources/XcodePIFConversion/XcodePIFBuilder+Schemes.swift new file mode 100644 index 00000000..e958c3a8 --- /dev/null +++ b/Sources/XcodePIFConversion/XcodePIFBuilder+Schemes.swift @@ -0,0 +1,26 @@ +import Foundation +import XcodeProjects +import XPIF + +extension XcodePIFBuilder { + public func scheme(named name: String) throws -> XcodeScheme { + try XcodeScheme(named: name, containers: [workspaceURL] + projects.values.map(\.projectURL)) + } + + /// Resolve both the project and the target identifier: target names and IDs can repeat across projects. + public func targetGUID(for reference: XcodeScheme.TargetReference) throws -> XPIF.GUID { + guard let project = projects.values.first(where: { $0.projectURL.path == reference.projectURL.path }) else { + throw ConversionError("Scheme references a project outside the workspace: \(reference.projectURL.path).") + } + let matches = project.project.targets.filter { target in + if let objectID = reference.objectID, !objectID.isEmpty { + return target.commonProperties.objectID.rawValue == objectID + } + return target.name == reference.name + } + guard matches.count == 1, let target = matches.first else { + throw ConversionError("Could not resolve scheme target '\(reference.name)' in \(reference.projectURL.path).") + } + return project.guid(target.commonProperties.objectID) + } +} diff --git a/Sources/XcodePIFConversion/XcodePIFBuilder.swift b/Sources/XcodePIFConversion/XcodePIFBuilder.swift new file mode 100644 index 00000000..bb114dbf --- /dev/null +++ b/Sources/XcodePIFConversion/XcodePIFBuilder.swift @@ -0,0 +1,146 @@ +import Foundation +import OrderedCollections +import XcodeProjects +import XPIF + +/// Converts Xcode projects and workspaces into the workspace consumed by Swift Build. +public struct XcodePIFBuilder: Sendable { + enum Input: Sendable { + case project(XcodeProject) + case workspace(XcodeWorkspace) + } + + let workspaceURL: URL + let workspaceName: String + private(set) var projects: OrderedDictionary = [:] + + public init(project: XcodeProject) throws { + self.workspaceURL = project.url.appending(path: "project.xcworkspace") + self.workspaceName = project.url.deletingPathExtension().lastPathComponent + try add(project) + try loadReferences() + } + + public init(workspace: XcodeWorkspace) throws { + self.workspaceURL = workspace.url + self.workspaceName = workspace.url.deletingPathExtension().lastPathComponent + for projectURL in try workspace.projectURLs() { + try load(projectURL) + } + try loadReferences() + } + + private mutating func load(_ url: URL) throws { + let url = url.canonicalized + guard projects[url] == nil else { return } + try add(XcodeProject(url: url)) + } + + private var identifierBaseURL: URL { + let parent = workspaceURL.deletingLastPathComponent() + return parent.pathExtension == "xcodeproj" ? parent.deletingLastPathComponent() : parent + } + + private mutating func add(_ project: XcodeProject) throws { + let key = project.url.canonicalized + guard projects[key] == nil else { return } + projects[key] = try XcodeLoadedProject(xcodeProject: project, projectURL: key, baseURL: identifierBaseURL) + } + + private mutating func loadReferences() throws { + var index = 0 + while index < projects.count { + let (url, project) = projects.elements[index] + for reference in project.references { + guard case .file(let file) = reference.reference, (file.path as NSString).pathExtension == "xcodeproj" else { + continue + } + guard let projectURL = reference.url else { + throw ConversionError("Cannot resolve Xcode project reference: \(file.path) in \(url.path)") + } + try load(projectURL) + } + index += 1 + } + + guard !projects.isEmpty else { throw ConversionError("Xcode workspace contains no projects.") } + } + + public struct PackageReference: Sendable { + public let sourceRoot: URL + public let package: XCSchema.SwiftPackage + /// Packages present in the navigator are roots of Xcode's package graph. + public var isRoot = false + } + + public var packageReferences: [PackageReference] { + var result = projects.values.flatMap { project in + project.project.packages.map { PackageReference(sourceRoot: project.sourceRoot, package: $0) } + } + // Local packages can be file references (including synchronized-folder + // discoveries) without an entry in the project's packageReferences list. + var localIndices: [String: Int] = [:] + for (index, reference) in result.enumerated() { + guard case .local(let package) = reference.package.location else { continue } + let path = URL(fileURLWithPath: package.path, relativeTo: reference.sourceRoot) + .standardizedFileURL.resolvingSymlinksInPath().path + localIndices[path] = index + } + for project in projects.values { + for reference in project.references { + guard case .file = reference.reference, let url = reference.url, + FileManager.default.fileExists(atPath: url.appending(path: "Package.swift").path) else { continue } + let path = url.standardizedFileURL.resolvingSymlinksInPath().path + if let index = localIndices[path] { + result[index].isRoot = true + continue + } + localIndices[path] = result.count + result.append(PackageReference(sourceRoot: project.sourceRoot, + package: .init(location: .local(.init(path: url.path)), traits: []), isRoot: true)) + } + } + return result + } + + public var packageWorkspaceURL: URL { workspaceURL } + + public func build(packageProjects: [XPIF.Project] = []) throws -> XPIF.Workspace { + guard packageReferences.isEmpty || !packageProjects.isEmpty else { + throw ConversionError("Package references must be resolved before generating Xcode PIF.") + } + let converted = try projects.values.sorted { $0.projectURL.path < $1.projectURL.path }.map { project in + var builder = XcodePIFProjectBuilder(loaded: project, packageProjects: packageProjects) + return try builder.buildProject(in: projects) + } + return XPIF.Workspace( + id: .init(XcodeLoadedProject.digest(workspaceName)), + name: workspaceName, + path: workspaceURL.path, + projects: converted + packageProjects, + ) + } +} + +extension XcodePIFProjectBuilder { + func referencedProject( + _ reference: XCSchema.GroupTreeReference, + in projects: OrderedDictionary + ) throws -> XcodeLoadedProject { + let ref = try loaded.resolve(reference) + guard case .file(let file) = ref, (file.path as NSString).pathExtension == "xcodeproj", + let url = loaded.references.first(where: { $0.reference.id == ref.id })?.url, + let project = projects[url.canonicalized] else { + throw ConversionError("Missing or unresolved Xcode project reference: \(reference) in \(loaded.projectURL.path)") + } + return project + } +} + +extension URL { + fileprivate var canonicalized: URL { + // URL equality distinguishes a directory URL's trailing slash. A project loaded + // from the workspace and from a file reference must share one registry entry. + URL(filePath: self.standardizedFileURL.resolvingSymlinksInPath().path, directoryHint: .notDirectory) + } +} diff --git a/Sources/XcodePIFConversion/XcodePIFConversionError.swift b/Sources/XcodePIFConversion/XcodePIFConversionError.swift new file mode 100644 index 00000000..665aa088 --- /dev/null +++ b/Sources/XcodePIFConversion/XcodePIFConversionError.swift @@ -0,0 +1,10 @@ +public struct XcodePIFConversionError: Error, CustomStringConvertible { + public let description: String + + public init(_ description: String) { + self.description = description + } +} + +// for convenience +typealias ConversionError = XcodePIFConversionError diff --git a/Sources/XcodePIFConversion/XcodePIFProjectBuilder+BuildFiles.swift b/Sources/XcodePIFConversion/XcodePIFProjectBuilder+BuildFiles.swift new file mode 100644 index 00000000..08e046dc --- /dev/null +++ b/Sources/XcodePIFConversion/XcodePIFProjectBuilder+BuildFiles.swift @@ -0,0 +1,35 @@ +import XcodeProjects +import XPIF + +extension XcodePIFProjectBuilder { + static func platformFilters(_ filters: Set) -> Set { + Set(filters.map { filter in + switch filter.rawValue { + case "maccatalyst": .init(platform: "ios", environment: "maccatalyst") + case "macos": .init(platform: "macos") + default: .init(platform: filter.rawValue) + } + }) + } + + static func buildFile( + id: XPIF.GUID, ref: XPIF.BuildFile.Ref, properties: XCSchema.BuildFileProperties + ) -> XPIF.BuildFile { + let attributes = properties.attributes + var file = XPIF.BuildFile( + id: id, ref: ref, + headerVisibility: attributes.headerRole.flatMap { .init(rawValue: $0.rawValue) }, + generatedCodeVisibility: attributes.codeGeneration == .skip ? .noCodegen + : attributes.codeGenerationVisibility.flatMap { .init(rawValue: $0.rawValue) }, + platformFilters: platformFilters(properties.platformFilters), + codeSignOnCopy: attributes.codeSignOnCopy, + removeHeadersOnCopy: attributes.headerPreservation == .removeOnCopy + ) + file.additionalCompilerOptions = properties.additionalBuildFlags.map(XPIF.StringList.string) + file.shouldLinkWeakly = attributes.isWeak + file.decompress = attributes.decompress + file.assetTags = Set(properties.assetTags.map(\.rawValue)) + file.migCodegenFiles = attributes.machInterfaceGeneration?.rawValue + return file + } +} diff --git a/Sources/XcodePIFConversion/XcodePIFProjectBuilder+Packages.swift b/Sources/XcodePIFConversion/XcodePIFProjectBuilder+Packages.swift new file mode 100644 index 00000000..2775b475 --- /dev/null +++ b/Sources/XcodePIFConversion/XcodePIFProjectBuilder+Packages.swift @@ -0,0 +1,22 @@ +import Foundation +import XcodeProjects +import XPIF + +extension XcodePIFProjectBuilder { + func packageProduct(_ reference: XCSchema.SwiftPackageProductReference) throws -> XPIF.BaseTarget { + let candidates = packageProjects.filter { project in + guard let name = reference.package?.packageName.lowercased() else { return true } + return project.name.lowercased() == name || project.id.value == "PACKAGE:\(name)" + }.flatMap(\.targets).filter { + $0.id.value.hasPrefix("PACKAGE-PRODUCT:") && $0.common.name == "\(reference.productName)-product" + } + switch candidates.count { + case 0: + throw ConversionError("Missing Swift package product '\(reference.productName)'.") + case 1: + return candidates[0] + default: + throw ConversionError("Found multiple Swift package products with name '\(reference.productName)'.") + } + } +} diff --git a/Sources/XcodePIFConversion/XcodePIFProjectBuilder+Targets.swift b/Sources/XcodePIFConversion/XcodePIFProjectBuilder+Targets.swift new file mode 100644 index 00000000..4fff1ef3 --- /dev/null +++ b/Sources/XcodePIFConversion/XcodePIFProjectBuilder+Targets.swift @@ -0,0 +1,334 @@ +import Foundation +import OrderedCollections +import RegexBuilder +import XcodeProjects +import XPIF + +extension XcodePIFProjectBuilder { + mutating func buildConfigurations( + _ configurations: [XCSchema.Configuration], settings: [String: XCSchema.BuildSetting], ownerID: XPIF.GUID + ) throws -> [XPIF.BuildConfig] { + guard !configurations.isEmpty, Set(configurations.map(\.name)).count == configurations.count else { + throw ConversionError("Xcode project must have distinct, nonempty build configurations.") + } + return try configurations.map { configuration in + let name = configuration.name.rawValue + guard !name.isEmpty else { throw ConversionError("Empty Xcode build configuration name.") } + let id = try identifier(configuration.objectID, fallback: "\(ownerID.value)::CONFIG:\(name)") + var result = XPIF.BuildConfig(id: id, name: name, settings: Self.buildSettings(settings, configuration: name)) + if let file = configuration.file { + guard case .file(let ref) = try loaded.resolve(file) else { + throw ConversionError("Xcode configuration must reference an xcconfig file: \(file)") + } + result.baseConfigurationFileReference = ref.id + } + return result + } + } + + // xcproj stores per-configuration settings using [config=...] conditions. PIF stores + // a settings table per configuration and flattens list values to space-separated strings. + static func buildSettings( + _ settings: [String: XCSchema.BuildSetting], configuration: String + ) -> XPIF.BuildSettings { + var result = XPIF.BuildSettings() + let expression = /\[config=([^\]]*)\]/ + let ordered = settings.sorted { lhs, rhs in + func priority(_ key: String) -> Int { + let matches = key.matches(of: expression) + guard !matches.isEmpty else { return 0 } + let wildcard = matches.contains { match in + match.output.1.contains("*") || match.output.1.contains("?") + } + return wildcard ? 1 : 2 + } + let left = priority(lhs.key) + let right = priority(rhs.key) + return left == right ? lhs.key < rhs.key : left < right + } + for (rawKey, value) in ordered { + let matches = rawKey.matches(of: expression) + let applies = matches.allSatisfy { match in + let pattern = match.output.1.reduce(Regex(verbatim: "")) { prefix, character in + let suffix: Regex = switch character { + case "*": Regex { ZeroOrMore(.any) } + case "?": Regex { CharacterClass.any } + default: Regex(verbatim: String(character)) + } + return Regex { + prefix + suffix + } + } + return configuration.wholeMatch(of: pattern) != nil + } + guard applies else { continue } + let key = rawKey.replacing(expression, with: "") + result.values[key] = switch value { + case .string(let string): .string(string) + case .array(let array): .string(array.joined(separator: " ")) + } + } + return result + } + + mutating func convertTarget( + _ source: XCSchema.Target, projects: OrderedDictionary + ) throws -> XPIF.BaseTarget { + let properties = source.commonProperties + let id = loaded.guid(properties.objectID) + try addPackageMemberships(properties) + var configurations = project.configurations + for specialized in properties.specializedConfigurations { + guard let index = configurations.firstIndex(where: { $0.name == specialized.name }) else { + throw ConversionError("Unknown configuration in target \(properties.name): \(specialized.name.rawValue)") + } + configurations[index] = specialized + } + // Project configuration IDs and xcconfigs belong to the project, not the target. + for index in configurations.indices { + if !properties.specializedConfigurations.contains(where: { $0.name == configurations[index].name }) { + configurations[index].objectID = nil + configurations[index].file = nil + } + } + let configs = try buildConfigurations(configurations, settings: properties.buildSettings, ownerID: id) + let phases = try properties.buildPhases.enumerated().map { try convertPhase($0.element, target: properties, index: $0.offset) } + let rules = try buildRules(properties, ownerID: id) + let dependencies = try targetDependencies(properties.dependencies, projects: projects) + packageDependencies(properties) + var result: XPIF.BaseTarget + switch source { + case .native: + guard let productType = properties.productTypeID else { + throw ConversionError("Missing product type for Xcode target \(properties.name).") + } + let product: XPIF.FileReference + if let reference = properties.product, case .file(let file) = try loaded.resolve(reference) { + product = file + } else { + throw ConversionError("Missing product reference for Xcode target \(properties.name).") + } + var target = XPIF.Target(id: id, productType: .init(rawValue: productType.rawValue), + name: properties.name, productName: product.path) + target.productReference = .init(id: product.id, name: product.path) + target.classPrefix = project.classPrefix + target.predominantSourceCodeLanguage = predominantLanguage(in: phases) + target.provisioningSourceData = configs.map { config in + .init(configurationName: config.name, + provisioningStyle: config.settings[single: "CODE_SIGN_STYLE"] == "Manual" ? .manual : .automatic, + bundleIdentifierFromInfoPlist: bundleIdentifierFromInfoPlist(config)) + } + result = .target(target) + case .aggregate: + var target = XPIF.AggregateTarget(id: id, name: properties.name) + if let prefix = project.classPrefix { target.common.additionalFields["classPrefix"] = .string(prefix) } + target.common.additionalFields["provisioningSourceData"] = .array(configs.map { config in + .object(["configurationName": .string(config.name), "provisioningStyle": .number(0), + "bundleIdentifierFromInfoPlist": .string("")]) + }) + result = .aggregate(target) + case .externalBuildSystem(let external): + guard phases.isEmpty, rules.isEmpty else { + throw ConversionError("External Xcode targets with build phases or build rules are unsupported.") + } + result = .external(.init( + id: id, name: properties.name, toolPath: .string(external.buildToolPath), + arguments: .string(external.buildToolArguments), + workingDirectory: .string(external.buildToolWorkingDirectory ?? "$(PROJECT_DIR)"), + passBuildSettingsInEnvironment: external.passBuildSettingsInEnvironment + )) + } + result.common.buildConfigs = configs + result.common.buildPhases = phases + result.common.buildRules = rules + result.common.dependencies = dependencies + return result + } + + private mutating func addPackageMemberships(_ properties: XCSchema.CommonTargetProperties) throws { + let id = loaded.guid(properties.objectID) + for (index, member) in properties.packageProductTargetMembers.enumerated() { + let reference: XCSchema.ProjectBuildPhaseReference = switch member.buildFile.buildPhase { + case .objectID(let id): .objectID(id) + case .named(let kind, let name): .named(target: .init(targetName: properties.name), kind: kind, name: name) + } + try addMembership(.init(objectID: member.packageProduct.objectID ?? member.buildFile.objectID, buildPhase: reference, + properties: member.buildFile.properties), + ref: .targetProduct(id: packageProduct(member.packageProduct).id), + fallback: "\(id.value)::PACKAGE:\(index)") + } + } + + private func packageDependencies(_ properties: XCSchema.CommonTargetProperties) throws -> [XPIF.TargetDependency] { + let explicit = try Set(properties.dependencies.compactMap { dependency -> XPIF.GUID? in + guard case .package(let reference, _) = dependency else { return nil } + return try packageProduct(reference).id + }) + var seen = explicit + return try properties.packageProductTargetMembers.compactMap { member in + let product = try packageProduct(member.packageProduct) + guard seen.insert(product.id).inserted else { return nil } + var dependency = XPIF.TargetDependency(targetId: product.id) + dependency.additionalFields["name"] = .string(member.packageProduct.productName) + return dependency + } + } + + private func bundleIdentifierFromInfoPlist(_ configuration: XPIF.BuildConfig) -> String { + let projectSettings = Self.buildSettings(project.buildSettings, configuration: configuration.name) + let settings = projectSettings.values.merging(configuration.settings.values) { _, target in target } + if case .string(let path) = settings["INFOPLIST_FILE"], + let filePath = try? XCSchema.FilePath(base: .project, path: path), + let url = loaded.xcodeProject.resolve(filePath, relativeTo: loaded.sourceRoot), + let data = try? Data(contentsOf: url), + let plist = try? PropertyListSerialization.propertyList(from: data, format: nil) as? [String: Any], + let identifier = plist["CFBundleIdentifier"] as? String { + return identifier + } + return settings["GENERATE_INFOPLIST_FILE"] == .string("YES") ? "$(PRODUCT_BUNDLE_IDENTIFIER)" : "" + } + + private func predominantLanguage(in phases: [XPIF.BuildPhase]) -> String? { + let fileTypes = Dictionary(uniqueKeysWithValues: loaded.references.compactMap { entry -> (XPIF.GUID, String)? in + guard case .file(let file) = entry.reference else { return nil } + return (file.id, file.fileType) + }) + // Xcode counts build-file memberships by file type, including headers/copy phases. + var counts: [String: Int] = [:] + for file in phases.flatMap({ $0.common.files }) { + guard case .reference(let id) = file.ref, let type = fileTypes[id] else { continue } + counts[type, default: 0] += 1 + } + let languages = counts.compactMap { type, count -> (name: String, count: Int)? in + let language: String + switch type { + case "sourcecode.swift": language = "Swift" + case "sourcecode.c.c": language = "C" + case "sourcecode.c.objc": language = "Objective-C" + case "sourcecode.cpp.cpp": language = "C-Plus-Plus" + case "sourcecode.c.h", "sourcecode.cpp.h", "sourcecode.cpp.objcpp": language = "Objective-C-Plus-Plus" + case "net.daringfireball.markdown": language = "Markdown" + default: return nil + } + return (language, count) + } + let language = languages.min { + $0.count == $1.count ? $0.name < $1.name : $0.count > $1.count + } + return language.map { "Xcode.SourceCodeLanguage.\($0.name)" } + } + + func targetDependencies( + _ dependencies: [XCSchema.TargetDependency], projects: OrderedDictionary + ) throws -> [XPIF.TargetDependency] { + try dependencies.map { dependency -> XPIF.TargetDependency in + switch dependency { + case .localTarget(let target, let filters): + var result = XPIF.TargetDependency( + targetId: loaded.guid(try loaded.resolveTarget(target.targetName).objectID), + platformFilters: Self.platformFilters(filters) + ) + result.additionalFields["name"] = .string(target.targetName) + return result + case .remoteTarget(let target, let filters): + let remote = try referencedProject(target.project, in: projects) + guard remote.project.targets.contains(where: { $0.commonProperties.objectID == target.targetID }) else { + throw ConversionError("Unknown target \(target.targetID.rawValue) in \(remote.projectURL.path)") + } + var result = XPIF.TargetDependency( + targetId: remote.guid(target.targetID), platformFilters: Self.platformFilters(filters) + ) + let name = remote.project.targets.first { $0.commonProperties.objectID == target.targetID }?.name ?? target.target + let suffix = " (from \(remote.projectURL.lastPathComponent))" + result.additionalFields["name"] = .string(name + suffix) + return result + case .package(let reference, let filters): + var result = XPIF.TargetDependency(targetId: try packageProduct(reference).id, + platformFilters: Self.platformFilters(filters)) + result.additionalFields["name"] = .string(reference.productName) + return result + } + } + } + + mutating func buildRules(_ properties: XCSchema.CommonTargetProperties, ownerID id: XPIF.GUID) throws -> [XPIF.BuildRule] { + try properties.buildRules.enumerated().map { index, rule in + let ruleID = try identifier(rule.objectID, fallback: "\(id.value)::RULE:\(index)") + let input: XPIF.BuildRule.Input = if let pattern = rule.filePatterns { + .filePattern(pattern) + } else { + .fileType(rule.fileType?.rawValue ?? "file") + } + let action: XPIF.BuildRule.Action = if let script = rule.script { + .shellScriptWithFileList( + script, inputPaths: rule.inputFiles, inputFileListPaths: rule.inputFileLists, + outputPaths: rule.outputFiles, outputFileListPaths: rule.outputFileLists, + outputFilesCompilerFlags: rule.outputFilesCompilerFlags.isEmpty ? nil : rule.outputFilesCompilerFlags, + dependencyInfo: rule.dependencyFile.map { .makefile($0) }, runOncePerArchitecture: rule.runOncePerArchitecture + ) + } else { + .compiler(rule.processor) + } + let name = rule.name ?? rule.filePatterns.map { "Files '\($0)' using Script" } ?? "" + return XPIF.BuildRule(id: ruleID, name: name, input: input, action: action) + } + } + + mutating func convertPhase( + _ phase: XCSchema.BuildPhase, target: XCSchema.CommonTargetProperties, index: Int + ) throws -> XPIF.BuildPhase { + let id = try identifier(phase.properties.objectID, fallback: loaded.phaseID(phase, target: target, index: index).value) + let files = phaseFiles[id] ?? [] + switch phase { + case .headers: return .headers(.init(id: id, files: files)) + case .sources: return .sources(.init(id: id, files: files)) + case .frameworks: return .frameworks(.init(id: id, files: files)) + case .resources: return .copyBundleResources(.init(id: id, files: files)) + case .copy(let properties): + return .copyFiles(.init( + common: .init(id: id, files: files), destinationSubfolder: Self.copyDestination(properties.bundleBasePath), + destinationSubpath: properties.relativePath, runOnlyForDeploymentPostprocessing: properties.scope == .install + )) + case .script(let properties): + guard files.isEmpty else { throw ConversionError("Shell script build phases cannot contain build files.") } + var script = XPIF.ShellScriptBuildPhase( + id: id, name: properties.baseProperties.name ?? "Run Script", scriptContents: properties.script, + shellPath: properties.shellPath, inputPaths: properties.inputPaths, outputPaths: properties.outputPaths, + emitEnvironment: properties.logEnvironmentVariables, alwaysOutOfDate: properties.runOnEveryBuild, + runOnlyForDeploymentPostprocessing: properties.scope == .install, + originalObjectID: properties.baseProperties.objectID?.rawValue ?? id.value + ) + script.inputFileListPaths = properties.inputFileListPaths + script.outputFileListPaths = properties.outputFileListPaths + script.dependencyInfo = properties.dependencyFile.map { .makefile($0) } + return .shellScript(script) + case .appleScript, .javaArchive, .rez: + throw ConversionError("Unsupported Xcode build phase: \(phase.kind.rawValue)") + } + } + + // Each case maps one Xcode bundle location to its PIF build setting. + // swiftlint:disable:next cyclomatic_complexity + static func copyDestination(_ base: XCSchema.BundleBasePath?) -> XPIF.CopyFilesBuildPhase.DestinationSubfolder { + switch base { + case nil: .absolute + case .productDir: .builtProductsDir + case .root: .wrapper + case .resourcesDir: .resources + case .frameworksDir: .frameworks + case .sharedFrameworksDir: .sharedFrameworks + case .sharedSupportDir: .sharedSupport + case .javaDir: .javaResources + case .plugInsDir: .plugins + case .pkgInfo: .buildSetting("$(PKGINFO_FILE_PATH)") + case .appleScriptsDir: .buildSetting("$(SCRIPTS_FOLDER_PATH)") + case .privateHeadersDir: .buildSetting("$(PRIVATE_HEADERS_FOLDER_PATH)") + case .headersDir: .buildSetting("$(PUBLIC_HEADERS_FOLDER_PATH)") + case .contentsDir: .buildSetting("$(CONTENTS_FOLDER_PATH)") + case .executablesDir: .buildSetting("$(EXECUTABLE_FOLDER_PATH)") + case .infoPlist: .buildSetting("$(INFOPLIST_PATH)") + case .mainExecutable: .buildSetting("$(EXECUTABLE_PATH)") + case .mainExecutableShallow: .buildSetting("$(EXECUTABLE_NAME)") + } + } +} diff --git a/Sources/XcodePIFConversion/XcodePIFProjectBuilder.swift b/Sources/XcodePIFConversion/XcodePIFProjectBuilder.swift new file mode 100644 index 00000000..f52c5afa --- /dev/null +++ b/Sources/XcodePIFConversion/XcodePIFProjectBuilder.swift @@ -0,0 +1,86 @@ +import Foundation +import OrderedCollections +import XcodeProjects +import XPIF + +struct XcodePIFProjectBuilder { + let loaded: XcodeLoadedProject + let packageProjects: [XPIF.Project] + var project: XCSchema.Project { loaded.project } + var phaseFiles: [XPIF.GUID: [XPIF.BuildFile]] = [:] + var usedIDs: Set + + init(loaded: XcodeLoadedProject, packageProjects: [XPIF.Project] = []) { + self.loaded = loaded + self.packageProjects = packageProjects + self.usedIDs = loaded.usedIDs + } + + mutating func buildProject(in projects: OrderedDictionary) throws -> XPIF.Project { + // Resolve memberships after all referenced projects and their products have been loaded. + for (referenceID, membership) in loaded.memberships { + let ref: XPIF.BuildFile.Ref = if let targetID = loaded.products[referenceID] { + .targetProduct(id: targetID) + } else { + .reference(id: referenceID) + } + try addMembership(membership, ref: ref, fallback: referenceID.value) + } + for (index, product) in project.importedProducts.enumerated() { + let remote = try referencedProject(product.project, in: projects) + guard let targetID = remote.products[remote.guid(product.productID)] else { + throw ConversionError("Unknown product \(product.productID.rawValue) in \(remote.projectURL.path)") + } + for membership in product.buildFiles { + try addMembership(membership, ref: .targetProduct(id: targetID), fallback: "IMPORTED:\(index)") + } + } + let configurations = try buildConfigurations( + project.configurations, settings: project.buildSettings, ownerID: loaded.projectID + ) + var result = XPIF.Project( + id: loaded.projectID, path: loaded.projectURL.path, projectDir: loaded.sourceRoot.path, + name: loaded.projectURL.deletingPathExtension().lastPathComponent, + mainGroup: loaded.mainGroup, buildConfigs: configurations, + targets: try project.targets.map { try convertTarget($0, projects: projects) }, + developmentRegion: project.localizationInfo.development.rawValue + ) + result.defaultConfigurationName = project.defaultConfigurationName.rawValue + let discoveredRegions = loaded.references.compactMap { entry -> String? in + guard case .file(let file) = entry.reference else { return nil } + return file.regionVariantName + } + result.knownRegions = Set(project.localizationInfo.allLanguages.map(\.rawValue)).union(discoveredRegions).sorted() + result.classPrefix = project.classPrefix ?? "" + result.appPreferencesBuildSettings = .init() + return result + } + + mutating func addMembership( + _ membership: XCSchema.ProjectBuildFile, ref: XPIF.BuildFile.Ref, fallback: String + ) throws { + let phaseID = try loaded.resolvePhase(membership.buildPhase) + let buildID = try identifier(membership.objectID, fallback: "\(phaseID.value)::FILE:\(fallback)") + phaseFiles[phaseID, default: []].append(Self.buildFile(id: buildID, ref: ref, properties: membership.properties)) + } + + mutating func identifier(_ objectID: XCSchema.ObjectID?, fallback: String) throws -> XPIF.GUID { + let id = objectID.map(loaded.guid) ?? XPIF.GUID(fallback) + guard objectID?.rawValue.isEmpty != true, !id.value.isEmpty, usedIDs.insert(id).inserted else { + throw ConversionError("Empty or duplicate Xcode object identifier: \(id.value)") + } + return id + } +} + +extension XCSchema.BuildPhase { + var properties: XCSchema.BuildPhaseProperties { + switch self { + case .frameworks(let properties), .headers(let properties), .javaArchive(let properties), + .resources(let properties), .rez(let properties), .sources(let properties): properties + case .appleScript(let properties): properties.baseProperties + case .copy(let properties): properties.baseProperties + case .script(let properties): properties.baseProperties + } + } +} diff --git a/Sources/XcodePIFConversion/XcodeProjects+Locations.swift b/Sources/XcodePIFConversion/XcodeProjects+Locations.swift new file mode 100644 index 00000000..67c05c26 --- /dev/null +++ b/Sources/XcodePIFConversion/XcodeProjects+Locations.swift @@ -0,0 +1,68 @@ +import Foundation +import XcodeProjects + +extension XcodeProject { + /// Resolves a literal file reference. SDK, build-product, and unevaluated macro paths return nil. + func resolve(_ path: XCSchema.FilePath, relativeTo group: URL?) -> URL? { + let sourceRoot = self.sourceRoot + let base: URL? + switch path.base { + case .absolute: base = nil + case .group: base = group + case .project: base = sourceRoot + case .sourceRoot(let setting): + switch setting { + case "SOURCE_ROOT", "SRCROOT", "PROJECT_DIR": base = sourceRoot + default: + if case .string(let value) = project.buildSettings[setting], (value as NSString).isAbsolutePath { + base = URL(filePath: value) + } else { + return nil + } + } + case .developer, .buildProducts, .sdk: return nil + } + var value = (path.path as NSString).expandingTildeInPath + for name in ["SOURCE_ROOT", "SRCROOT", "PROJECT_DIR"] { + value = value.replacingOccurrences(of: "$(\(name))", with: sourceRoot.path) + .replacingOccurrences(of: "${\(name)}", with: sourceRoot.path) + } + guard !value.contains("$("), !value.contains("${") else { return nil } + if (value as NSString).isAbsolutePath { return URL(filePath: value).standardizedFileURL } + return base?.appending(path: value).standardizedFileURL + } +} + +extension XcodeWorkspace { + /// Projects explicitly listed in the workspace, in document order. Groups affect paths, + /// while container-relative locations always start beside the workspace bundle. + func projectURLs() throws -> [URL] { + let container = url.deletingLastPathComponent() + return try projectURLs(in: children, container: container, group: container) + } + + private func projectURLs(in elements: [Element], container: URL, group: URL) throws -> [URL] { + try elements.flatMap { element in + let location = element.location + let resolved: URL + switch location.base { + case .absolute: resolved = URL(filePath: (location.path as NSString).expandingTildeInPath) + case .container, .current: resolved = container.appending(path: location.path) + case .group: resolved = group.appending(path: location.path) + default: throw Errors.unsupportedLocation(location.description) + } + switch element { + case .file: + return resolved.pathExtension == "xcodeproj" ? [resolved.standardizedFileURL.resolvingSymlinksInPath()] : [] + case .group(let child): + return try projectURLs(in: child.children, container: container, group: resolved) + case .fileSystemSynchronizedGroup: + throw Errors.unsupportedLocation("Synchronized workspace group: \(location)") + } + } + } + + enum Errors: Error { + case unsupportedLocation(String) + } +} diff --git a/Sources/XcodePIFConversion/XcodeSynchronizedFolder+Memberships.swift b/Sources/XcodePIFConversion/XcodeSynchronizedFolder+Memberships.swift new file mode 100644 index 00000000..d97fab97 --- /dev/null +++ b/Sources/XcodePIFConversion/XcodeSynchronizedFolder+Memberships.swift @@ -0,0 +1,94 @@ +import XcodeProjects + +extension XcodeSynchronizedFolder { + func buildFiles(for members: [XCSchema.FolderMemberID], fileType: String) throws -> [XCSchema.ProjectBuildFile] { + var result = project.targets.compactMap { target in + buildFile(for: target.commonProperties, members: members, fileType: fileType) + } + for exception in folder.membershipExceptions { + guard case .buildPhase(let exception) = exception, + members.contains(where: exception.commonProperties.membershipExceptions.contains) else { continue } + let phase = try resolvePhase(exception.buildPhase) + if let index = result.firstIndex(where: { $0.buildPhase == phase }) { + Self.apply(exception.commonProperties, members: members, to: &result[index].properties) + } else { + var properties = Self.defaultProperties + Self.apply(exception.commonProperties, members: members, to: &properties) + result.append(.init(objectID: nil, buildPhase: phase, properties: properties)) + } + } + for index in result.indices { + if case .objectID(let phase) = result[index].buildPhase, let path = members.first?.value { + result[index].objectID = .init("\(identifierPrefix)::SYNCHRONIZED:BUILD:\(phase.rawValue):\(path)") + } + } + return result + } + + private func buildFile( + for target: XCSchema.CommonTargetProperties, members: [XCSchema.FolderMemberID], fileType: String + ) -> XCSchema.ProjectBuildFile? { + let targetReference = XCSchema.LocalTargetReference(targetName: target.name) + var includedMembers = folder.targets.contains(targetReference) ? Set(members) : [] + var properties = Self.defaultProperties + for exception in folder.membershipExceptions { + guard case .target(let exception) = exception, exception.target == targetReference else { continue } + let common = exception.commonProperties + let selected = Set(members).intersection(common.membershipExceptions) + switch common.sense { + case .inclusions: includedMembers.formUnion(selected) + case .exclusions: includedMembers.subtract(selected) + } + Self.apply(common, members: members, to: &properties) + for member in members { + if exception.publicHeaders.contains(member) { properties.attributes.headerRole = .public } + if exception.privateHeaders.contains(member) { properties.attributes.headerRole = .private } + if let flags = exception.additionalCompilerFlags[member] { properties.additionalBuildFlags = flags } + } + } + // A localized resource is a single build input as long as any variant belongs to the target. + guard !includedMembers.isEmpty else { return nil } + guard let phase = Self.implicitPhase(for: fileType, in: target) else { return nil } + return .init(objectID: nil, buildPhase: Self.reference(to: phase, target: target.name), properties: properties) + } + + func resolvePhase(_ reference: XCSchema.ProjectBuildPhaseReference) throws -> XCSchema.ProjectBuildPhaseReference { + let matches = project.targets.flatMap { target in + target.commonProperties.buildPhases.compactMap { phase -> XCSchema.ProjectBuildPhaseReference? in + let matches: Bool = switch reference { + case .objectID(let id): phase.properties.objectID == id + case .named(let owner, let kind, let name): + target.name == owner.targetName && phase.kind == kind && phase.properties.name == name + } + return matches ? Self.reference(to: phase, target: target.name) : nil + } + } + guard matches.count == 1, let match = matches.first else { + throw ConversionError("Missing or ambiguous synchronized folder build phase: \(reference)") + } + return match + } + + private static func reference(to phase: XCSchema.BuildPhase, target: String) -> XCSchema.ProjectBuildPhaseReference { + if let id = phase.properties.objectID { return .objectID(id) } + return .named(target: .init(targetName: target), kind: phase.kind, name: phase.properties.name) + } + + private static func apply( + _ common: XCSchema.CommonExceptionSetProperties, members: [XCSchema.FolderMemberID], + to properties: inout XCSchema.BuildFileProperties + ) { + for member in members { + if let filters = common.platformFiltersByFolderMemberID[member] { properties.platformFilters = filters } + if let attributes = common.attributesByFolderMemberID[member] { properties.attributes = attributes } + if let tags = common.assetTagsByFolderMemberID[member] { properties.assetTags = tags } + } + } + + private static var defaultProperties: XCSchema.BuildFileProperties { + .init(platformFilters: [], additionalBuildFlags: nil, assetTags: [], attributes: .init( + headerRole: nil, machInterfaceGeneration: nil, isWeak: false, codeSignOnCopy: false, codeGeneration: .default, + headerPreservation: .keep, decompress: false, codeGenerationVisibility: nil + )) + } +} diff --git a/Sources/XcodePIFConversion/XcodeSynchronizedFolder+Routing.swift b/Sources/XcodePIFConversion/XcodeSynchronizedFolder+Routing.swift new file mode 100644 index 00000000..a0fb004d --- /dev/null +++ b/Sources/XcodePIFConversion/XcodeSynchronizedFolder+Routing.swift @@ -0,0 +1,96 @@ +import XcodeProjects + +/// Picks the phase a synchronized folder member implicitly joins, the way Xcode routes a file added to a target. +/// +/// Each kind of phase accepts or rejects a type based on the most specific entry in its include/exclude lists +/// that matches the type or one of its ancestors. Xcode first tries the target's phases in order, skipping +/// catch-all phases; then the first catch-all phase; then the type's fallback phase, if the target has one. +/// Otherwise the file joins no phase. +extension XcodeSynchronizedFolder { + static func implicitPhase( + for fileType: String, in target: XCSchema.CommonTargetProperties + ) -> XCSchema.BuildPhase? { + let ancestry = XcodeFileType.ancestry(of: fileType) + guard !ancestry.contains(where: projectWrapperTypes.contains) else { return nil } + let phases = target.buildPhases + func isCatchAll(_ index: Int) -> Bool { + switch phases[index].kind { + case .javaArchive: true + // Copy Files only collects files when the target does nothing else but run scripts. + case .copy: phases.indices.allSatisfy { $0 == index || phases[$0].kind == .script } + default: false + } + } + let sources = sourcesIncludes[target.productTypeID?.productTypeID ?? ""] ?? PhaseFilter.sources + for index in phases.indices where !isCatchAll(index) { + let filter: PhaseFilter? = switch phases[index].kind { + case .sources: sources + case .headers: .headers + case .frameworks: .frameworks + case .rez: .rez + case .appleScript: .appleScript + case .resources: .resources + case .copy, .script, .javaArchive: nil + } + if filter?.accepts(ancestry) == true { return phases[index] } + } + if let index = phases.indices.first(where: isCatchAll) { return phases[index] } + if ancestry.contains(where: resourcesFallbackTypes.contains) { + return phases.first { $0.kind == .resources } + } + return nil + } + + private static let projectWrapperTypes: Set = ["wrapper.pb-project", "file.swiftpm-manifest"] + private static let resourcesFallbackTypes: Set = ["archive.rsrc", "archive.metal-library", "file.intentdefinition"] + + /// Product types that restrict what their Sources phase compiles. + private static let sourcesIncludes: [String: PhaseFilter] = [ + "com.apple.product-type.metal-library": .init(includes: ["sourcecode.metal"], excludes: PhaseFilter.sources.excludes), + "com.apple.product-type.instruments-package": .init( + includes: ["com.apple.instruments.package-definition"], excludes: PhaseFilter.sources.excludes + ), + ] +} + +private struct PhaseFilter { + var includes: Set + var excludes: Set = [] + var includesEverything = false + + func accepts(_ ancestry: [String]) -> Bool { + for type in ancestry { + if excludes.contains(type) { return false } + if includes.contains(type) { return true } + } + return includesEverything + } + + static let headers = Self(includes: ["sourcecode.c.h", "sourcecode.cpp.h", "text.apinotes"]) + static let rez = Self(includes: ["sourcecode.rez", "archive.rsrc"]) + static let appleScript = Self(includes: ["sourcecode.applescript", "sourcecode.scpt"]) + static let frameworks = Self(includes: [ + "wrapper.framework", "wrapper.xcframework", "archive.ar", "compiled.mach-o.dylib", "compiled.mach-o.objfile", + "sourcecode.text-based-dylib-definition", "archive.jar", "archive.war", "archive.ear", + ]) + static let sources = Self( + includes: [ + "sourcecode", "text.xml.ibArchivingDescription", "text.xml.ibCodingDescription", "folder.mlpackage", + "folder.documentationcatalog", "file.referenceobject", "folder.fmadapter", "folder.mlasset", "folder.aimodel", + ], + excludes: headers.includes.union(rez.includes).union([ + "sourcecode.scpt", "sourcecode.glsl", "sourcecode.javascript", "sourcecode.typescript", + "net.daringfireball.markdown", "sourcecode.module-map", "sourcecode.text-based-dylib-definition", + ]) + ) + /// Resources takes everything that no specific phase claims, bar a few types that belong nowhere. + static let resources = Self( + includes: ["sourcecode.glsl", "sourcecode.javascript", "net.daringfireball.markdown"], + excludes: sources.includes.union(headers.includes).union(frameworks.includes).union(rez.includes) + .union(appleScript.includes).union([ + "file.playground", "sourcecode.module-map", "sourcecode.text-based-dylib-definition", + "text.plist.entitlements", + ]), + includesEverything: true + ) +} diff --git a/Sources/XcodePIFConversion/XcodeSynchronizedFolder.swift b/Sources/XcodePIFConversion/XcodeSynchronizedFolder.swift new file mode 100644 index 00000000..fa15539d --- /dev/null +++ b/Sources/XcodePIFConversion/XcodeSynchronizedFolder.swift @@ -0,0 +1,193 @@ +import Foundation +import Crypto +import XcodeProjects +import XPIF + +/// Materializes the discovered references before the project loader resolves their memberships. +struct XcodeSynchronizedFolder { + let folder: XCSchema.Folder + let project: XCSchema.Project + let identifierPrefix: String + + func references(at directory: URL) throws -> [XCSchema.Reference] { + for target in folder.targets { + guard project.targets.contains(where: { $0.name == target.targetName }) else { + throw ConversionError("Unknown synchronized folder target: \(target.targetName)") + } + } + for exception in folder.membershipExceptions { + switch exception { + case .target(let exception): + guard project.targets.contains(where: { $0.name == exception.target.targetName }) else { + throw ConversionError("Unknown synchronized folder target: \(exception.target.targetName)") + } + case .buildPhase(let exception): + _ = try resolvePhase(exception.buildPhase) + } + } + guard FileManager.default.fileExists(atPath: directory.path) else { return [] } + return try references(at: directory, relativePath: "", ancestors: []) + } + + private func references(at directory: URL, relativePath: String, ancestors: Set) throws -> [XCSchema.Reference] { + let canonical = URL(filePath: directory.resolvingSymlinksInPath().path, directoryHint: .notDirectory) + guard !ancestors.contains(canonical) else { + throw ConversionError("Cyclic synchronized folder symlink: \(directory.path)") + } + let ancestors = ancestors.union([canonical]) + var result: [XCSchema.Reference] = [] + var localized: [String: [XCSchema.FileReference]] = [:] + for url in try contents(of: directory) { + let name = url.lastPathComponent + let path = relativePath + name + var type = fileType(for: path, isDirectory: isDirectory(url)) + // Xcode treats a discovered Swift package as one opaque reference. Its + // sources belong to the package graph, not the enclosing native target. + if type == "file", isDirectory(url), + FileManager.default.fileExists(atPath: url.appending(path: "Package.swift").path) { + type = "wrapper" + } + if isDirectory(url), !isOpaque(path, type: type) { + if url.pathExtension == "lproj" { + for file in try localizedFiles(at: url, path: path + "/", filePath: name + "/", ancestors: ancestors) { + let logicalPath = String(file.path.path.dropFirst(name.count + 1)) + localized[logicalPath, default: []].append(file) + } + } else { + result.append(.group(.init( + objectID: objectID(path, kind: "GROUP"), name: name, path: try groupPath(name), includeInIndex: nil, + children: try references(at: url, relativePath: path + "/", ancestors: ancestors) + ))) + } + } else if isDirectory(url), type == "wrapper.xcdatamodeld" { + let children = try contents(of: url).filter { $0.pathExtension == "xcdatamodel" }.map { child in + try file(at: child, path: path + "/" + child.lastPathComponent, filePath: child.lastPathComponent, + type: "wrapper.xcdatamodel", buildFiles: []) + } + result.append(.versionGroup(.init( + objectID: objectID(path, kind: "VERSION"), name: name, path: try groupPath(name), currentVersion: nil, + versionedFileType: .init(rawValue: type), includeInIndex: nil, + buildFiles: try buildFiles(for: [.init(value: path)], fileType: type), children: children + ))) + } else { + let type = isDirectory(url) && type == "file" ? "folder" : type + result.append(.fileReference(try file( + at: url, path: path, filePath: name, type: type, + buildFiles: buildFiles(for: [.init(value: path)], fileType: type) + ))) + } + } + // Interface-builder strings belong to their storyboard/xib's variant group. + for key in localized.keys.sorted() where (key as NSString).pathExtension == "strings" { + let stem = (key as NSString).deletingPathExtension + if let interface = [stem + ".storyboard", stem + ".xib"].first(where: { localized[$0] != nil }) { + localized[interface, default: []] += localized.removeValue(forKey: key) ?? [] + } + } + for key in localized.keys.sorted() { + let children = localized[key] ?? [] + let members = children.map { XCSchema.FolderMemberID(value: relativePath + $0.path.path) } + result.append(.variantGroup(.init( + objectID: objectID(relativePath + key, kind: "VARIANT"), name: key, path: try groupPath(""), + includeInIndex: nil, buildFiles: try buildFiles(for: members, fileType: fileType(for: key, isDirectory: false)), + children: children + ))) + } + // Xcode places discovered groups before files, then sorts each category by name like Finder does. + return result.sorted { + func key(_ ref: XCSchema.Reference) -> (Int, String) { + switch ref { + case .group(let group): return (0, group.name) + case .variantGroup(let group): return (1, group.name) + case .versionGroup(let group): return (1, group.name) + case .fileReference(let file): return (1, file.path.path) + case .folder(let folder): return (0, folder.path.path) + } + } + let (lhs, rhs) = (key($0), key($1)) + return lhs.0 != rhs.0 ? lhs.0 < rhs.0 : lhs.1.localizedStandardCompare(rhs.1) == .orderedAscending + } + } + + private func localizedFiles( + at directory: URL, path: String, filePath: String, ancestors: Set + ) throws -> [XCSchema.FileReference] { + let canonical = URL(filePath: directory.resolvingSymlinksInPath().path, directoryHint: .notDirectory) + guard !ancestors.contains(canonical) else { + throw ConversionError("Cyclic synchronized folder symlink: \(directory.path)") + } + return try contents(of: directory).flatMap { url in + let name = url.lastPathComponent + let type = fileType(for: path + name, isDirectory: isDirectory(url)) + if isDirectory(url), !isOpaque(path + name, type: type) { + return try localizedFiles(at: url, path: path + name + "/", filePath: filePath + name + "/", + ancestors: ancestors.union([canonical])) + } + return [try file(at: url, path: path + name, filePath: filePath + name, type: type, buildFiles: [])] + } + } + + private func contents(of directory: URL) throws -> [URL] { + // Like hidden files, Xcode ignores backup files (`name~`). + try FileManager.default.contentsOfDirectory(at: directory, includingPropertiesForKeys: nil, options: .skipsHiddenFiles) + .filter { !$0.lastPathComponent.hasSuffix("~") } + .sorted { $0.lastPathComponent.localizedStandardCompare($1.lastPathComponent) == .orderedAscending } + } + + private func isDirectory(_ url: URL) -> Bool { + var directory = ObjCBool(false) + return FileManager.default.fileExists(atPath: url.path, isDirectory: &directory) && directory.boolValue + } + + private func isOpaque(_ path: String, type: String) -> Bool { + folder.explicitOpaqueFolders.contains(.init(value: path)) || type != "file" + } + + private func fileType(for path: String, isDirectory: Bool) -> String { + if let type = folder.explicitFileTypes[.init(value: path)] { return type.rawValue } + let name = (path as NSString).lastPathComponent + let type = isDirectory ? XcodeFileType.forDirectory(named: name) : XcodeFileType.forFile(named: name) + return type ?? "file" + } + + private func file( + at url: URL, path: String, filePath: String, type: String, buildFiles: [XCSchema.ProjectBuildFile] + ) throws -> XCSchema.FileReference { + .init(objectID: objectID(path, kind: "FILE"), path: try groupPath(filePath), explicitFileType: .init(rawValue: type), + expectedSignature: nil, textEncoding: try textEncoding(at: url, fileType: type), + lineEnding: nil, includeInIndex: nil, buildFiles: buildFiles) + } + + private func textEncoding(at url: URL, fileType: String) throws -> XCSchema.TextEncoding? { + guard fileType == "text.plist.strings" else { return nil } + // Swift Build cannot autodetect strings-file encodings on Linux. + var encoding = String.Encoding.utf8 + _ = try String(contentsOf: url, usedEncoding: &encoding) + return .init(rawValue: encoding == .ascii ? .utf8 : encoding) + } + + private func groupPath(_ path: String) throws -> XCSchema.FilePath { + try .init(base: .group, path: path) + } + + /// Discovered references use SHA-256(project GUID + root object ID + relative path). + /// Build files additionally include the owning phase's PBX ID; variant groups add a suffix. + static func pifGUID(_ objectID: XCSchema.ObjectID, projectPrefix: String) -> XPIF.GUID? { + let parts = objectID.rawValue.components(separatedBy: "::SYNCHRONIZED:") + guard parts.count == 2, let separator = parts[1].firstIndex(of: ":") else { return nil } + let kind = String(parts[1][.. XCSchema.ObjectID { + .init("\(identifierPrefix)::SYNCHRONIZED:\(kind):\(path)") + } +} diff --git a/Sources/XcodeProjects/XcodeScheme.swift b/Sources/XcodeProjects/XcodeScheme.swift new file mode 100644 index 00000000..879af1e8 --- /dev/null +++ b/Sources/XcodeProjects/XcodeScheme.swift @@ -0,0 +1,162 @@ +import Foundation +private import XcodeProj + +/// The target selection of a scheme's Run action. +public struct XcodeScheme: Sendable { + public struct TargetReference: Sendable { + public let projectURL: URL + public let objectID: String? + public let name: String + /// The product's filename, e.g. `App.app`. + public let buildableName: String + + public init(projectURL: URL, objectID: String?, name: String, buildableName: String) { + self.projectURL = projectURL + self.objectID = objectID + self.name = name + self.buildableName = buildableName + } + } + + public let name: String + public let buildTargets: [TargetReference] + public let runnable: TargetReference? + public let buildImplicitDependencies: Bool + + public init( + name: String, + buildTargets: [TargetReference], + runnable: TargetReference?, + buildImplicitDependencies: Bool = true, + ) { + self.name = name + self.buildTargets = buildTargets + self.runnable = runnable + self.buildImplicitDependencies = buildImplicitDependencies + } + + /// Finds shared schemes and the current user's schemes in the supplied containers. + /// Duplicate names are rejected rather than selecting an arbitrary project. + public init(named name: String, containers: [URL]) throws { + guard !name.isEmpty, (name as NSString).lastPathComponent == name else { + throw SchemeError("Invalid Xcode scheme name: '\(name)'.") + } + var matches: [(url: URL, container: URL)] = [] + var visited: Set = [] + for container in containers { + let container = container.standardizedFileURL.resolvingSymlinksInPath() + guard visited.insert(container.path).inserted else { continue } + let directories = [ + "xcuserdata/\(NSUserName()).xcuserdatad/xcschemes", + "xcshareddata/xcschemes", + ] + for directory in directories { + let url = container.appending(path: directory).appending(path: "\(name).xcscheme") + if FileManager.default.fileExists(atPath: url.path) { + matches.append((url, container)) + } + } + } + guard matches.count == 1, let match = matches.first else { + if matches.isEmpty { throw SchemeError("Could not find Xcode scheme '\(name)'.") } + throw SchemeError("Ambiguous Xcode scheme '\(name)': \(matches.map(\.url.path).joined(separator: ", ")).") + } + try self.init(url: match.url, container: match.container) + } + + public init(url: URL, container: URL) throws { + let scheme = try XCScheme(pathString: url.path) + name = scheme.name + let base = Self.referenceBase(for: container) + func reference(_ reference: XCScheme.BuildableReference) throws -> TargetReference { + let prefix = "container:" + guard reference.referencedContainer.hasPrefix(prefix) else { + throw SchemeError("Unsupported scheme container: \(reference.referencedContainer).") + } + let path = String(reference.referencedContainer.dropFirst(prefix.count)) + guard !path.isEmpty else { throw SchemeError("Empty project reference in scheme '\(scheme.name)'.") } + let projectURL = URL(fileURLWithPath: path, relativeTo: base).standardizedFileURL.resolvingSymlinksInPath() + return TargetReference( + projectURL: projectURL, + objectID: reference.blueprintIdentifier, + name: reference.blueprintName, + buildableName: reference.buildableName, + ) + } + buildTargets = try (scheme.buildAction?.buildActionEntries ?? []) + .filter { $0.buildFor.contains(.running) } + .map { try reference($0.buildableReference) } + buildImplicitDependencies = scheme.buildAction?.buildImplicitDependencies ?? true + if let runnable = scheme.launchAction?.runnable { + guard runnable is XCScheme.BuildableProductRunnable, let target = runnable.buildableReference else { + throw SchemeError("Scheme '\(name)' does not launch a target product.") + } + self.runnable = try reference(target) + } else { + runnable = nil + } + } + + /// Writes the scheme into `container`, an `.xcodeproj` or `.xcworkspace`, either as a + /// shared scheme or as one of the current user's schemes. + public func write(in container: URL, shared: Bool) throws { + let base = Self.referenceBase(for: container).resolvingSymlinksInPath() + func reference(_ target: TargetReference) -> XCScheme.BuildableReference { + XCScheme.BuildableReference( + referencedContainer: "container:\(Self.path(of: target.projectURL, relativeTo: base))", + blueprintIdentifier: target.objectID, + buildableName: target.buildableName, + blueprintName: target.name, + ) + } + let runnable = self.runnable.map { XCScheme.BuildableProductRunnable(buildableReference: reference($0)) } + let scheme = XCScheme( + name: name, + lastUpgradeVersion: nil, + version: "1.7", + buildAction: XCScheme.BuildAction( + buildActionEntries: buildTargets.map { + .init( + buildableReference: reference($0), + buildFor: [.running, .testing, .profiling, .archiving, .analyzing], + ) + }, + parallelizeBuild: true, + buildImplicitDependencies: buildImplicitDependencies, + ), + testAction: XCScheme.TestAction(buildConfiguration: "Debug", macroExpansion: nil), + launchAction: XCScheme.LaunchAction(runnable: runnable, buildConfiguration: "Debug"), + profileAction: XCScheme.ProfileAction(runnable: runnable, buildConfiguration: "Release"), + analyzeAction: XCScheme.AnalyzeAction(buildConfiguration: "Debug"), + archiveAction: XCScheme.ArchiveAction(buildConfiguration: "Release", revealArchiveInOrganizer: true), + ) + let directory = container.appending( + path: shared ? "xcshareddata/xcschemes" : "xcuserdata/\(NSUserName()).xcuserdatad/xcschemes", + directoryHint: .isDirectory, + ) + try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true) + // dataRepresentation should never return nil + try (scheme.dataRepresentation() ?? Data()).write(to: directory.appending(path: "\(name).xcscheme")) + } + + /// The directory that a scheme's `container:` references are relative to. + private static func referenceBase(for container: URL) -> URL { + // References in a project's embedded workspace are relative to the project directory. + let parent = container.deletingLastPathComponent() + return parent.pathExtension == "xcodeproj" ? parent.deletingLastPathComponent() : parent + } + + private static func path(of url: URL, relativeTo base: URL) -> String { + let baseComponents = base.standardizedFileURL.pathComponents + let components = url.standardizedFileURL.resolvingSymlinksInPath().pathComponents + let common = zip(baseComponents, components).prefix { $0 == $1 }.count + let parents = Array(repeating: "..", count: baseComponents.count - common) + return (parents + components.dropFirst(common)).joined(separator: "/") + } + + public struct SchemeError: Error, CustomStringConvertible { + public let description: String + + init(_ description: String) { self.description = description } + } +} diff --git a/Tests/XToolTests/Fixtures/Projects/App.xcodeproj/project.xcproj b/Tests/XToolTests/Fixtures/Projects/App.xcodeproj/project.xcproj new file mode 100644 index 00000000..c2e12ae7 --- /dev/null +++ b/Tests/XToolTests/Fixtures/Projects/App.xcodeproj/project.xcproj @@ -0,0 +1,108 @@ +{ + "id": "AA0000000000000000000001", + "root-group-debug-id": "A40000000000000000000001", + "configuration-list-debug-id": "A90000000000000000000001", + "default-configuration": "Debug", + "configurations": [ + { "id": "A80000000000000000000001", "name": "Debug" }, + { "id": "A80000000000000000000002", "name": "Release" }, + ], + "localizations": { + "development": "en", + "supported": [ + "Base", + ], + }, + "imported-products": [ + { + "name": "Features.framework", + "project": "id:47251F93A0778F8438F170D6", + "target": "Features", + "product-id": "FB9EF894175C327470F768D9", + "type": "wrapper.framework", + "target-membership": [ + { "id": "A6A32DBC5618EA39F6DF5B0F", "build-phase": "id:A30000000000000000000001", "platforms": [ "ios" ], "is-weak": true }, + { "id": "7512561D2F685534F3A83998", "build-phase": "id:9A1A94849351517526ADF6B3", "code-sign-on-copy": true, "header-preservation": "remove-on-copy" }, + ], + }, + ], + "files": [ + { "path": "Features.xcodeproj", "id": "47251F93A0778F8438F170D6" }, + { + "kind": "group", + "id": "A40000000000000000000002", + "path": "Sources", + "name": "Sources", + "children": [ + { "path": "App.swift", "id": "A20000000000000000000001", "target-membership": [ { "id": "A10000000000000000000001", "build-phase": "id:A70000000000000000000001" } ] }, + ], + }, { + "kind": "group", + "id": "A40000000000000000000003", + "path": "Resources", + "name": "Resources", + "children": [ + { "path": "message.txt", "id": "A20000000000000000000003", "target-membership": [ { "id": "A10000000000000000000003", "build-phase": "id:A60000000000000000000001" } ] }, + ], + }, { + "kind": "group", + "id": "A40000000000000000000005", + "name": "Products", + "children": [ + { "path": "/App.app", "id": "A20000000000000000000004", "type": "wrapper.application", "index": false }, + ], + }, + ], + "targets": [ + { + "name": "App", + "id": "A50000000000000000000001", + "configuration-list-debug-id": "A90000000000000000000002", + "product": "id:A20000000000000000000004", + "product-type": "application", + "specialized-configurations": [ + { "id": "A80000000000000000000003", "name": "Debug" }, + { "id": "A80000000000000000000004", "name": "Release" }, + ], + "dependencies": [ + { "kind": "remoteTarget", "project": "id:47251F93A0778F8438F170D6", "target": "Features", "target-id": "5697D03DAEF4DE9CF8DB7CB1", "platforms": [ "ios" ] }, + ], + "build-phases": [ + { "kind": "compile-sources", "id": "A70000000000000000000001" }, + { "kind": "frameworks", "id": "A30000000000000000000001" }, + { "kind": "resources", "id": "A60000000000000000000001" }, + { + "kind": "copy", + "id": "9A1A94849351517526ADF6B3", + "name": "Embed Frameworks", + "bundle-base-path": "frameworks-directory", + }, + ], + "build-settings": { + "CODE_SIGN_STYLE": "Automatic", + "GENERATE_INFOPLIST_FILE": "YES", + "INFOPLIST_KEY_UIApplicationSceneManifest_Generation": "YES", + "INFOPLIST_KEY_UILaunchScreen_Generation": "YES", + "LD_RUNPATH_SEARCH_PATHS": "$(inherited) @executable_path/Frameworks", + "PRODUCT_BUNDLE_IDENTIFIER": "sh.xtool.examples.App", + "PRODUCT_NAME": "$(TARGET_NAME)", + "TARGETED_DEVICE_FAMILY": "1,2", + }, + }, + ], + "build-settings": { + "CLANG_ENABLE_MODULES": "YES", + "DEBUG_INFORMATION_FORMAT[config=Debug]": "dwarf", + "DEBUG_INFORMATION_FORMAT[config=Release]": "dwarf-with-dsym", + "ENABLE_TESTABILITY[config=Debug]": "YES", + "IPHONEOS_DEPLOYMENT_TARGET": "17.0", + "SDKROOT": "iphoneos", + "SWIFT_ACTIVE_COMPILATION_CONDITIONS[config=Debug]": "DEBUG", + "SWIFT_COMPILATION_MODE[config=Release]": "wholemodule", + "SWIFT_OPTIMIZATION_LEVEL[config=Debug]": "-Onone", + "SWIFT_OPTIMIZATION_LEVEL[config=Release]": "-O", + "SWIFT_VERSION": "6.0", + }, + "products-group": "id:A40000000000000000000005", + "last-upgrade": "16.0", +} diff --git a/Tests/XToolTests/Fixtures/Projects/App.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/Tests/XToolTests/Fixtures/Projects/App.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 00000000..919434a6 --- /dev/null +++ b/Tests/XToolTests/Fixtures/Projects/App.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/Tests/XToolTests/Fixtures/Projects/App.xcodeproj/xcshareddata/xcschemes/Primary.xcscheme b/Tests/XToolTests/Fixtures/Projects/App.xcodeproj/xcshareddata/xcschemes/Primary.xcscheme new file mode 100644 index 00000000..8abd3517 --- /dev/null +++ b/Tests/XToolTests/Fixtures/Projects/App.xcodeproj/xcshareddata/xcschemes/Primary.xcscheme @@ -0,0 +1,8 @@ + + + + + + + + diff --git a/Tests/XToolTests/Fixtures/Projects/DiscoveredPackages.xcodeproj/project.xcproj b/Tests/XToolTests/Fixtures/Projects/DiscoveredPackages.xcodeproj/project.xcproj new file mode 100644 index 00000000..bb5c8245 --- /dev/null +++ b/Tests/XToolTests/Fixtures/Projects/DiscoveredPackages.xcodeproj/project.xcproj @@ -0,0 +1,147 @@ +{ + "id": "AA0000000000000000000001", + "root-group-debug-id": "A40000000000000000000001", + "configuration-list-debug-id": "A90000000000000000000001", + "default-configuration": "Debug", + "configurations": [ + { + "id": "A80000000000000000000001", + "name": "Debug" + }, + { + "id": "A80000000000000000000002", + "name": "Release" + } + ], + "localizations": { + "development": "en", + "supported": [ + "Base" + ] + }, + "files": [ + { + "kind": "group", + "id": "A40000000000000000000002", + "path": "Sources", + "name": "Sources", + "children": [ + { + "path": "App.swift", + "id": "A20000000000000000000001", + "target-membership": [ + { + "id": "A10000000000000000000001", + "build-phase": "id:A70000000000000000000001" + } + ] + } + ] + }, + { + "kind": "group", + "id": "A40000000000000000000003", + "path": "Resources", + "name": "Resources", + "children": [ + { + "path": "message.txt", + "id": "A20000000000000000000003", + "target-membership": [ + { + "id": "A10000000000000000000003", + "build-phase": "id:A60000000000000000000001" + } + ] + } + ] + }, + { + "kind": "group", + "id": "A40000000000000000000005", + "name": "Products", + "children": [ + { + "path": "/App.app", + "id": "A20000000000000000000004", + "type": "wrapper.application", + "index": false + } + ] + }, + { + "kind": "folder", + "path": "Modules", + "id": "BB0000000000000000000001" + } + ], + "targets": [ + { + "name": "App", + "id": "A50000000000000000000001", + "configuration-list-debug-id": "A90000000000000000000002", + "product": "id:A20000000000000000000004", + "product-type": "application", + "specialized-configurations": [ + { + "id": "A80000000000000000000003", + "name": "Debug" + }, + { + "id": "A80000000000000000000004", + "name": "Release" + } + ], + "dependencies": [], + "build-phases": [ + { + "kind": "compile-sources", + "id": "A70000000000000000000001" + }, + { + "kind": "frameworks", + "id": "A30000000000000000000001" + }, + { + "kind": "resources", + "id": "A60000000000000000000001" + } + ], + "build-settings": { + "CODE_SIGN_STYLE": "Automatic", + "GENERATE_INFOPLIST_FILE": "YES", + "INFOPLIST_KEY_UIApplicationSceneManifest_Generation": "YES", + "INFOPLIST_KEY_UILaunchScreen_Generation": "YES", + "LD_RUNPATH_SEARCH_PATHS": "$(inherited) @executable_path/Frameworks", + "PRODUCT_BUNDLE_IDENTIFIER": "sh.xtool.tests.Packages", + "PRODUCT_NAME": "$(TARGET_NAME)", + "TARGETED_DEVICE_FAMILY": "1,2" + }, + "package-product-members": [ + { + "product-name": "Account", + "id": "AA0000000000000000000099", + "build-phase": { + "id": "AA0000000000000000000098", + "build-phase": "id:A30000000000000000000001" + } + } + ] + } + ], + "build-settings": { + "CLANG_ENABLE_MODULES": "YES", + "DEBUG_INFORMATION_FORMAT[config=Debug]": "dwarf", + "DEBUG_INFORMATION_FORMAT[config=Release]": "dwarf-with-dsym", + "ENABLE_TESTABILITY[config=Debug]": "YES", + "IPHONEOS_DEPLOYMENT_TARGET": "17.0", + "SDKROOT": "iphoneos", + "SWIFT_ACTIVE_COMPILATION_CONDITIONS[config=Debug]": "DEBUG", + "SWIFT_COMPILATION_MODE[config=Release]": "wholemodule", + "SWIFT_OPTIMIZATION_LEVEL[config=Debug]": "-Onone", + "SWIFT_OPTIMIZATION_LEVEL[config=Release]": "-O", + "SWIFT_VERSION": "6.0" + }, + "products-group": "id:A40000000000000000000005", + "last-upgrade": "16.0" +} diff --git a/Tests/XToolTests/Fixtures/Projects/Features.xcodeproj/project.xcproj b/Tests/XToolTests/Fixtures/Projects/Features.xcodeproj/project.xcproj new file mode 100644 index 00000000..42f09195 --- /dev/null +++ b/Tests/XToolTests/Fixtures/Projects/Features.xcodeproj/project.xcproj @@ -0,0 +1,167 @@ +{ + "id": "985959785319747668373CC6", + "root-group-debug-id": "44CB005EE2E65D9CC817B0A0", + "configuration-list-debug-id": "3C05BEEE7303EE3FA7B250FA", + "class-prefix": "PF", + "default-configuration": "Debug", + "configurations": [ + { "id": "433548979C024CC63B5F3B8D", "name": "Debug", "file": "id:87E89ABB4C1C551FE08D355D" }, + { "id": "B800A40FDE6F0A0218D9BCD2", "name": "Release", "file": "id:87E89ABB4C1C551FE08D355D" }, + ], + "localizations": { + "development": "en", + "supported": [ + "Base", + "fr", + ], + }, + "files": [ + { "path": "Sources/Feature.swift", "id": "AE8ED2743925477EAFAD0DFD", "encoding": "utf8", "target-membership": [ { "id": "25B2FEC255F5749BDFF02710", "build-phase": "id:CAF85B0888D78F1F83771A07", "arguments": "-DLOCAL" } ] }, + { "path": "Sources/Other.swift", "id": "17989396AB9542B7A6F19D69", "target-membership": [ { "id": "2431ADF55F8891FF47272B79", "build-phase": "id:CAF85B0888D78F1F83771A07" } ] }, + { + "path": "Sources/Feature.h", + "id": "BA5CAA4285A84D88129280D2", + "target-membership": [ + { "id": "9449AEABF19DE80786D2FE73", "build-phase": "id:194E9FE656A1BCABFA596619", "header-role": "public" }, + { "id": "F187BBC502B22299CD142406", "build-phase": "id:E21F935F11D7E966DBBAE78D" }, + ], + }, { + "kind": "group", + "id": "E89B30AA1DC30A6AD8C34610", + "path": "Resources", + "children": [ + { + "kind": "variant-group", + "id": "F4F877DDF1670B8C39240650", + "name": "Localizable.strings", + "target-membership": [ + { "id": "2748450557125186C299F45B", "build-phase": "id:F77E8395C14206FA0A5A32E1", "asset-tags": [ "fixture" ] }, + ], + "children": [ + { + "path": "en.lproj/Localizable.strings", + "id": "BA118BF7FC9C1AEDC1EDB28A", + }, { + "path": "fr.lproj/Localizable.strings", + "id": "7458199FE97A184002BFD3C4", + "encoding": "utf8", + }, + ], + }, + ], + }, + { "path": "Settings.xcconfig", "id": "87E89ABB4C1C551FE08D355D" }, + { + "kind": "group", + "id": "4EDC8BFAFC6BD936B849F38C", + "name": "Products", + "children": [ + { "path": "/Features.framework", "id": "FB9EF894175C327470F768D9", "index": false }, + ], + }, + ], + "targets": [ + { + "name": "Features", + "id": "5697D03DAEF4DE9CF8DB7CB1", + "configuration-list-debug-id": "A6FB65CCCF5741BC044EAB30", + "product": "id:FB9EF894175C327470F768D9", + "product-type": "framework", + "specialized-configurations": [ + { "id": "D52364EB7F4DAE720159F17A", "name": "Debug" }, + { "id": "D7133DDD9B2C56730E249807", "name": "Release" }, + ], + "build-phases": [ + { "kind": "headers", "id": "194E9FE656A1BCABFA596619" }, + { "kind": "compile-sources", "id": "CAF85B0888D78F1F83771A07" }, + { "kind": "resources", "id": "F77E8395C14206FA0A5A32E1" }, + { + "kind": "copy", + "id": "E21F935F11D7E966DBBAE78D", + "name": "Copy Header", + "bundle-base-path": "build-products-directory", + "relative-path": "include", + }, { + "kind": "script", + "id": "102ACC10E67A297D79F7A044", + "name": "Generate", + "input-paths": [ + "$(SRCROOT)/input", + ], + "input-file-list-paths": [ + "$(SRCROOT)/Inputs.xcfilelist", + ], + "output-paths": [ + "$(DERIVED_FILE_DIR)/output", + ], + "output-file-list-paths": [ + "$(SRCROOT)/Outputs.xcfilelist", + ], + "dependency-file": "$(DERIVED_FILE_DIR)/output.d", + "run-on-every-build": true, + "shell": "/bin/sh", + "script": "echo fixture", + }, + ], + "build-rules": [ + { + "id": "62845F31A2FE00BE3EBAAEB9", + "processor": "com.apple.compilers.proxy.script", + "file-type": "pattern.proxy", + "file-patterns": "*.fixture", + "input-files": [ + "$(INPUT_FILE_PATH)", + ], + "output-files": [ + "$(DERIVED_FILE_DIR)/$(INPUT_FILE_BASE).swift", + ], + "script": "echo rule", + }, + ], + "build-settings": { + "CODE_SIGN_STYLE[config=Debug]": "Automatic", + "CODE_SIGN_STYLE[config=Release]": "Manual", + "GENERATE_INFOPLIST_FILE[config=Debug]": "YES", + "INFOPLIST_FILE[config=Release]": "Info.plist", + "PRODUCT_BUNDLE_IDENTIFIER": "sh.xtool.pif.Features", + "PRODUCT_NAME": "$(TARGET_NAME)", + }, + }, { + "name": "All", + "id": "A52ACE420F2175D08B1577A1", + "configuration-list-debug-id": "91321694432A748261AFE995", + "kind": "aggregate", + "specialized-configurations": [ + { "id": "3752D8080E98983C1F1A5855", "name": "Debug" }, + { "id": "10C619824FAB373D2828DF94", "name": "Release" }, + ], + "dependencies": [ + "Features", + ], + }, { + "name": "External", + "id": "68C114EA9C8C66041E945221", + "configuration-list-debug-id": "FE81CF25B6D7C783551D544C", + "kind": "external-build-system", + "specialized-configurations": [ + { "id": "BAD3340EDE84BFE02446B2DE", "name": "Debug" }, + { "id": "DE07F11AE176D0A9A1D93738", "name": "Release" }, + ], + "build-tool-path": "/usr/bin/make", + "build-tool-arguments": "all", + "build-tool-working-directory": "$(SRCROOT)", + }, + ], + "build-settings": { + "CUSTOM[config=Debug][sdk=iphoneos*]": "device", + "OTHER_SWIFT_FLAGS[config=Debug]": [ + "$(inherited)", + "-DDEBUG", + ], + "SDKROOT": "iphoneos", + "SWIFT_OPTIMIZATION_LEVEL[config=Release]": "-O", + "SWIFT_VERSION": "6.0", + }, + "products-group": "id:4EDC8BFAFC6BD936B849F38C", + "last-upgrade": "16.0", +} diff --git a/Tests/XToolTests/Fixtures/Projects/Features.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/Tests/XToolTests/Fixtures/Projects/Features.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 00000000..919434a6 --- /dev/null +++ b/Tests/XToolTests/Fixtures/Projects/Features.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/Tests/XToolTests/Fixtures/Projects/Info.plist b/Tests/XToolTests/Fixtures/Projects/Info.plist new file mode 100644 index 00000000..88bc0d1f --- /dev/null +++ b/Tests/XToolTests/Fixtures/Projects/Info.plist @@ -0,0 +1,8 @@ + + + + + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER).from-plist + + diff --git a/Tests/XToolTests/Fixtures/Projects/Inputs.xcfilelist b/Tests/XToolTests/Fixtures/Projects/Inputs.xcfilelist new file mode 100644 index 00000000..c2971132 --- /dev/null +++ b/Tests/XToolTests/Fixtures/Projects/Inputs.xcfilelist @@ -0,0 +1 @@ +$(SRCROOT)/input diff --git a/Tests/XToolTests/Fixtures/Projects/LocalPackage/Package.swift b/Tests/XToolTests/Fixtures/Projects/LocalPackage/Package.swift new file mode 100644 index 00000000..d3b18c99 --- /dev/null +++ b/Tests/XToolTests/Fixtures/Projects/LocalPackage/Package.swift @@ -0,0 +1,17 @@ +// swift-tools-version: 6.0 +import PackageDescription + +let package = Package( + name: "LocalPackage", + platforms: [.iOS(.v17)], + products: [ + .library(name: "Library", targets: ["Library"]), + .library(name: "DynamicLibrary", type: .dynamic, targets: ["DynamicLibrary"]), + ], + targets: [ + .target(name: "Library", dependencies: ["Helper"], resources: [.process("Message.txt")]), + .target(name: "Helper"), + .target(name: "DynamicLibrary"), + .testTarget(name: "LibraryTests", dependencies: ["Library"]), + ] +) diff --git a/Tests/XToolTests/Fixtures/Projects/LocalPackage/Sources/DynamicLibrary/DynamicLibrary.swift b/Tests/XToolTests/Fixtures/Projects/LocalPackage/Sources/DynamicLibrary/DynamicLibrary.swift new file mode 100644 index 00000000..ba81e886 --- /dev/null +++ b/Tests/XToolTests/Fixtures/Projects/LocalPackage/Sources/DynamicLibrary/DynamicLibrary.swift @@ -0,0 +1 @@ +public func dynamicMessage() -> String { "Dynamic library" } diff --git a/Tests/XToolTests/Fixtures/Projects/LocalPackage/Sources/Helper/Helper.swift b/Tests/XToolTests/Fixtures/Projects/LocalPackage/Sources/Helper/Helper.swift new file mode 100644 index 00000000..34ed047d --- /dev/null +++ b/Tests/XToolTests/Fixtures/Projects/LocalPackage/Sources/Helper/Helper.swift @@ -0,0 +1 @@ +public func helperMessage() -> String { "Hello, " } diff --git a/Tests/XToolTests/Fixtures/Projects/LocalPackage/Sources/Library/Library.swift b/Tests/XToolTests/Fixtures/Projects/LocalPackage/Sources/Library/Library.swift new file mode 100644 index 00000000..711e348a --- /dev/null +++ b/Tests/XToolTests/Fixtures/Projects/LocalPackage/Sources/Library/Library.swift @@ -0,0 +1,7 @@ +import Foundation +import Helper + +public func libraryMessage() throws -> String { + let url = Bundle.module.url(forResource: "Message", withExtension: "txt")! + return helperMessage() + (try String(contentsOf: url, encoding: .utf8)) +} diff --git a/Tests/XToolTests/Fixtures/Projects/LocalPackage/Sources/Library/Message.txt b/Tests/XToolTests/Fixtures/Projects/LocalPackage/Sources/Library/Message.txt new file mode 100644 index 00000000..01af1ed8 --- /dev/null +++ b/Tests/XToolTests/Fixtures/Projects/LocalPackage/Sources/Library/Message.txt @@ -0,0 +1 @@ +Package fixture diff --git a/Tests/XToolTests/Fixtures/Projects/LocalPackage/Tests/LibraryTests/LibraryTests.swift b/Tests/XToolTests/Fixtures/Projects/LocalPackage/Tests/LibraryTests/LibraryTests.swift new file mode 100644 index 00000000..195fe01d --- /dev/null +++ b/Tests/XToolTests/Fixtures/Projects/LocalPackage/Tests/LibraryTests/LibraryTests.swift @@ -0,0 +1,4 @@ +import Testing +import Library + +@Test func message() throws { #expect(try libraryMessage().contains("Package fixture")) } diff --git a/Tests/XToolTests/Fixtures/Projects/Modules/Account/Package.swift b/Tests/XToolTests/Fixtures/Projects/Modules/Account/Package.swift new file mode 100644 index 00000000..5f06de1c --- /dev/null +++ b/Tests/XToolTests/Fixtures/Projects/Modules/Account/Package.swift @@ -0,0 +1,8 @@ +// swift-tools-version: 6.0 +import PackageDescription + +let package = Package( + name: "Account", + products: [.library(name: "Account", type: .dynamic, targets: ["Account"])], + targets: [.target(name: "Account")] +) diff --git a/Tests/XToolTests/Fixtures/Projects/Modules/Account/Sources/Account/Account.swift b/Tests/XToolTests/Fixtures/Projects/Modules/Account/Sources/Account/Account.swift new file mode 100644 index 00000000..1ba44b9a --- /dev/null +++ b/Tests/XToolTests/Fixtures/Projects/Modules/Account/Sources/Account/Account.swift @@ -0,0 +1 @@ +public struct Account {} diff --git a/Tests/XToolTests/Fixtures/Projects/OtherApp.xcodeproj/project.xcproj b/Tests/XToolTests/Fixtures/Projects/OtherApp.xcodeproj/project.xcproj new file mode 100644 index 00000000..aeaad9c4 --- /dev/null +++ b/Tests/XToolTests/Fixtures/Projects/OtherApp.xcodeproj/project.xcproj @@ -0,0 +1,108 @@ +{ + "id": "AA0000000000000000000001", + "root-group-debug-id": "A40000000000000000000001", + "configuration-list-debug-id": "A90000000000000000000001", + "default-configuration": "Debug", + "configurations": [ + { "id": "A80000000000000000000001", "name": "Debug" }, + { "id": "A80000000000000000000002", "name": "Release" }, + ], + "localizations": { + "development": "en", + "supported": [ + "Base", + ], + }, + "imported-products": [ + { + "name": "Features.framework", + "project": "id:47251F93A0778F8438F170D6", + "target": "Features", + "product-id": "FB9EF894175C327470F768D9", + "type": "wrapper.framework", + "target-membership": [ + { "id": "A6A32DBC5618EA39F6DF5B0F", "build-phase": "id:A30000000000000000000001", "platforms": [ "ios" ], "is-weak": true }, + { "id": "7512561D2F685534F3A83998", "build-phase": "id:9A1A94849351517526ADF6B3", "code-sign-on-copy": true, "header-preservation": "remove-on-copy" }, + ], + }, + ], + "files": [ + { "path": "Features.xcodeproj", "id": "47251F93A0778F8438F170D6" }, + { + "kind": "group", + "id": "A40000000000000000000002", + "path": "Sources", + "name": "Sources", + "children": [ + { "path": "App.swift", "id": "A20000000000000000000001", "target-membership": [ { "id": "A10000000000000000000001", "build-phase": "id:A70000000000000000000001" } ] }, + ], + }, { + "kind": "group", + "id": "A40000000000000000000003", + "path": "Resources", + "name": "Resources", + "children": [ + { "path": "message.txt", "id": "A20000000000000000000003", "target-membership": [ { "id": "A10000000000000000000003", "build-phase": "id:A60000000000000000000001" } ] }, + ], + }, { + "kind": "group", + "id": "A40000000000000000000005", + "name": "Products", + "children": [ + { "path": "/OtherApp.app", "id": "A20000000000000000000004", "type": "wrapper.application", "index": false }, + ], + }, + ], + "targets": [ + { + "name": "App", + "id": "A50000000000000000000001", + "configuration-list-debug-id": "A90000000000000000000002", + "product": "id:A20000000000000000000004", + "product-type": "application", + "specialized-configurations": [ + { "id": "A80000000000000000000003", "name": "Debug" }, + { "id": "A80000000000000000000004", "name": "Release" }, + ], + "dependencies": [ + { "kind": "remoteTarget", "project": "id:47251F93A0778F8438F170D6", "target": "Features", "target-id": "5697D03DAEF4DE9CF8DB7CB1", "platforms": [ "ios" ] }, + ], + "build-phases": [ + { "kind": "compile-sources", "id": "A70000000000000000000001" }, + { "kind": "frameworks", "id": "A30000000000000000000001" }, + { "kind": "resources", "id": "A60000000000000000000001" }, + { + "kind": "copy", + "id": "9A1A94849351517526ADF6B3", + "name": "Embed Frameworks", + "bundle-base-path": "frameworks-directory", + }, + ], + "build-settings": { + "CODE_SIGN_STYLE": "Automatic", + "GENERATE_INFOPLIST_FILE": "YES", + "INFOPLIST_KEY_UIApplicationSceneManifest_Generation": "YES", + "INFOPLIST_KEY_UILaunchScreen_Generation": "YES", + "LD_RUNPATH_SEARCH_PATHS": "$(inherited) @executable_path/Frameworks", + "PRODUCT_BUNDLE_IDENTIFIER": "sh.xtool.examples.OtherApp", + "PRODUCT_NAME": "OtherApp", + "TARGETED_DEVICE_FAMILY": "1,2", + }, + }, + ], + "build-settings": { + "CLANG_ENABLE_MODULES": "YES", + "DEBUG_INFORMATION_FORMAT[config=Debug]": "dwarf", + "DEBUG_INFORMATION_FORMAT[config=Release]": "dwarf-with-dsym", + "ENABLE_TESTABILITY[config=Debug]": "YES", + "IPHONEOS_DEPLOYMENT_TARGET": "17.0", + "SDKROOT": "iphoneos", + "SWIFT_ACTIVE_COMPILATION_CONDITIONS[config=Debug]": "DEBUG", + "SWIFT_COMPILATION_MODE[config=Release]": "wholemodule", + "SWIFT_OPTIMIZATION_LEVEL[config=Debug]": "-Onone", + "SWIFT_OPTIMIZATION_LEVEL[config=Release]": "-O", + "SWIFT_VERSION": "6.0", + }, + "products-group": "id:A40000000000000000000005", + "last-upgrade": "16.0", +} diff --git a/Tests/XToolTests/Fixtures/Projects/Outputs.xcfilelist b/Tests/XToolTests/Fixtures/Projects/Outputs.xcfilelist new file mode 100644 index 00000000..e5ab5799 --- /dev/null +++ b/Tests/XToolTests/Fixtures/Projects/Outputs.xcfilelist @@ -0,0 +1 @@ +$(DERIVED_FILE_DIR)/output diff --git a/Tests/XToolTests/Fixtures/Projects/PIF.xcworkspace/contents.xcworkspacedata b/Tests/XToolTests/Fixtures/Projects/PIF.xcworkspace/contents.xcworkspacedata new file mode 100644 index 00000000..519371b6 --- /dev/null +++ b/Tests/XToolTests/Fixtures/Projects/PIF.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,5 @@ + + + + + diff --git a/Tests/XToolTests/Fixtures/Projects/Packages.xcodeproj/project.xcproj b/Tests/XToolTests/Fixtures/Projects/Packages.xcodeproj/project.xcproj new file mode 100644 index 00000000..336b3de1 --- /dev/null +++ b/Tests/XToolTests/Fixtures/Projects/Packages.xcodeproj/project.xcproj @@ -0,0 +1,149 @@ +{ + "id": "AA0000000000000000000001", + "root-group-debug-id": "A40000000000000000000001", + "configuration-list-debug-id": "A90000000000000000000001", + "default-configuration": "Debug", + "configurations": [ + { + "id": "A80000000000000000000001", + "name": "Debug" + }, + { + "id": "A80000000000000000000002", + "name": "Release" + } + ], + "localizations": { + "development": "en", + "supported": [ + "Base" + ] + }, + "files": [ + { + "kind": "group", + "id": "A40000000000000000000002", + "path": "Sources", + "name": "Sources", + "children": [ + { + "path": "PackageApp.swift", + "id": "A20000000000000000000001", + "target-membership": [ + { + "id": "A10000000000000000000001", + "build-phase": "id:A70000000000000000000001" + } + ] + } + ] + }, + { + "kind": "group", + "id": "A40000000000000000000003", + "path": "Resources", + "name": "Resources", + "children": [ + { + "path": "message.txt", + "id": "A20000000000000000000003", + "target-membership": [ + { + "id": "A10000000000000000000003", + "build-phase": "id:A60000000000000000000001" + } + ] + } + ] + }, + { + "kind": "group", + "id": "A40000000000000000000005", + "name": "Products", + "children": [ + { + "path": "/App.app", + "id": "A20000000000000000000004", + "type": "wrapper.application", + "index": false + } + ] + } + ], + "targets": [ + { + "name": "App", + "id": "A50000000000000000000001", + "configuration-list-debug-id": "A90000000000000000000002", + "product": "id:A20000000000000000000004", + "product-type": "application", + "specialized-configurations": [ + { + "id": "A80000000000000000000003", + "name": "Debug" + }, + { + "id": "A80000000000000000000004", + "name": "Release" + } + ], + "dependencies": [], + "build-phases": [ + { + "kind": "compile-sources", + "id": "A70000000000000000000001" + }, + { + "kind": "frameworks", + "id": "A30000000000000000000001" + }, + { + "kind": "resources", + "id": "A60000000000000000000001" + } + ], + "build-settings": { + "CODE_SIGN_STYLE": "Automatic", + "GENERATE_INFOPLIST_FILE": "YES", + "INFOPLIST_KEY_UIApplicationSceneManifest_Generation": "YES", + "INFOPLIST_KEY_UILaunchScreen_Generation": "YES", + "LD_RUNPATH_SEARCH_PATHS": "$(inherited) @executable_path/Frameworks", + "PRODUCT_BUNDLE_IDENTIFIER": "sh.xtool.tests.Packages", + "PRODUCT_NAME": "$(TARGET_NAME)", + "TARGETED_DEVICE_FAMILY": "1,2" + }, + "package-product-members": [ + { + "package": "LocalPackage", + "product-name": "Library", + "id": "AA0000000000000000000099", + "build-phase": { + "id": "AA0000000000000000000098", + "build-phase": "id:A30000000000000000000001" + } + } + ] + } + ], + "build-settings": { + "CLANG_ENABLE_MODULES": "YES", + "DEBUG_INFORMATION_FORMAT[config=Debug]": "dwarf", + "DEBUG_INFORMATION_FORMAT[config=Release]": "dwarf-with-dsym", + "ENABLE_TESTABILITY[config=Debug]": "YES", + "IPHONEOS_DEPLOYMENT_TARGET": "17.0", + "SDKROOT": "iphoneos", + "SWIFT_ACTIVE_COMPILATION_CONDITIONS[config=Debug]": "DEBUG", + "SWIFT_COMPILATION_MODE[config=Release]": "wholemodule", + "SWIFT_OPTIMIZATION_LEVEL[config=Debug]": "-Onone", + "SWIFT_OPTIMIZATION_LEVEL[config=Release]": "-O", + "SWIFT_VERSION": "6.0" + }, + "products-group": "id:A40000000000000000000005", + "last-upgrade": "16.0", + "packages": [ + { + "kind": "local", + "path": "LocalPackage" + } + ] +} diff --git a/Tests/XToolTests/Fixtures/Projects/Resources/en.lproj/Localizable.strings b/Tests/XToolTests/Fixtures/Projects/Resources/en.lproj/Localizable.strings new file mode 100644 index 00000000..cf0ab243 --- /dev/null +++ b/Tests/XToolTests/Fixtures/Projects/Resources/en.lproj/Localizable.strings @@ -0,0 +1 @@ +"fixture" = "Fixture"; diff --git a/Tests/XToolTests/Fixtures/Projects/Resources/fr.lproj/Localizable.strings b/Tests/XToolTests/Fixtures/Projects/Resources/fr.lproj/Localizable.strings new file mode 100644 index 00000000..cf0ab243 --- /dev/null +++ b/Tests/XToolTests/Fixtures/Projects/Resources/fr.lproj/Localizable.strings @@ -0,0 +1 @@ +"fixture" = "Fixture"; diff --git a/Tests/XToolTests/Fixtures/Projects/Resources/message.txt b/Tests/XToolTests/Fixtures/Projects/Resources/message.txt new file mode 100644 index 00000000..8175d7e9 --- /dev/null +++ b/Tests/XToolTests/Fixtures/Projects/Resources/message.txt @@ -0,0 +1 @@ +Hello from Xcode! diff --git a/Tests/XToolTests/Fixtures/Projects/Schemes.xcworkspace/contents.xcworkspacedata b/Tests/XToolTests/Fixtures/Projects/Schemes.xcworkspace/contents.xcworkspacedata new file mode 100644 index 00000000..4fc1679c --- /dev/null +++ b/Tests/XToolTests/Fixtures/Projects/Schemes.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,5 @@ + + + + + diff --git a/Tests/XToolTests/Fixtures/Projects/Schemes.xcworkspace/xcshareddata/xcschemes/Secondary.xcscheme b/Tests/XToolTests/Fixtures/Projects/Schemes.xcworkspace/xcshareddata/xcschemes/Secondary.xcscheme new file mode 100644 index 00000000..ae2d16ea --- /dev/null +++ b/Tests/XToolTests/Fixtures/Projects/Schemes.xcworkspace/xcshareddata/xcschemes/Secondary.xcscheme @@ -0,0 +1,13 @@ + + + + + + + + + + + + + diff --git a/Tests/XToolTests/Fixtures/Projects/Settings.xcconfig b/Tests/XToolTests/Fixtures/Projects/Settings.xcconfig new file mode 100644 index 00000000..bb0a30d8 --- /dev/null +++ b/Tests/XToolTests/Fixtures/Projects/Settings.xcconfig @@ -0,0 +1 @@ +SWIFT_ACTIVE_COMPILATION_CONDITIONS = $(inherited) FIXTURE diff --git a/Tests/XToolTests/Fixtures/Projects/Sources/App.swift b/Tests/XToolTests/Fixtures/Projects/Sources/App.swift new file mode 100644 index 00000000..d54ab661 --- /dev/null +++ b/Tests/XToolTests/Fixtures/Projects/Sources/App.swift @@ -0,0 +1,10 @@ +import SwiftUI + +@main struct DemoApp: App { + var body: some Scene { + WindowGroup { + Text("Hello from an Xcode project!") + .padding() + } + } +} diff --git a/Tests/XToolTests/Fixtures/Projects/Sources/Feature.h b/Tests/XToolTests/Fixtures/Projects/Sources/Feature.h new file mode 100644 index 00000000..4b76ee11 --- /dev/null +++ b/Tests/XToolTests/Fixtures/Projects/Sources/Feature.h @@ -0,0 +1 @@ +// Header fixture. diff --git a/Tests/XToolTests/Fixtures/Projects/Sources/Feature.swift b/Tests/XToolTests/Fixtures/Projects/Sources/Feature.swift new file mode 100644 index 00000000..2cf40af7 --- /dev/null +++ b/Tests/XToolTests/Fixtures/Projects/Sources/Feature.swift @@ -0,0 +1 @@ +public func feature() -> String { "Fixture" } diff --git a/Tests/XToolTests/Fixtures/Projects/Sources/Other.swift b/Tests/XToolTests/Fixtures/Projects/Sources/Other.swift new file mode 100644 index 00000000..f751d043 --- /dev/null +++ b/Tests/XToolTests/Fixtures/Projects/Sources/Other.swift @@ -0,0 +1 @@ +public let otherFeature = "Fixture" diff --git a/Tests/XToolTests/Fixtures/Projects/Sources/PackageApp.swift b/Tests/XToolTests/Fixtures/Projects/Sources/PackageApp.swift new file mode 100644 index 00000000..b586289c --- /dev/null +++ b/Tests/XToolTests/Fixtures/Projects/Sources/PackageApp.swift @@ -0,0 +1,13 @@ +import UIKit +import Library + +@main +final class PackageApp: UIResponder, UIApplicationDelegate { + func application( + _ application: UIApplication, + didFinishLaunchingWithOptions options: [UIApplication.LaunchOptionsKey: Any]? = nil + ) -> Bool { + print((try? libraryMessage()) ?? "Unable to load package resource") + return true + } +} diff --git a/Tests/XToolTests/Fixtures/Projects/SyncFiles/Excluded.swift b/Tests/XToolTests/Fixtures/Projects/SyncFiles/Excluded.swift new file mode 100644 index 00000000..22b80337 --- /dev/null +++ b/Tests/XToolTests/Fixtures/Projects/SyncFiles/Excluded.swift @@ -0,0 +1 @@ +#error("This file must not be compiled") diff --git a/Tests/XToolTests/Fixtures/Projects/SyncFiles/Feature.swift b/Tests/XToolTests/Fixtures/Projects/SyncFiles/Feature.swift new file mode 100644 index 00000000..50bc2f60 --- /dev/null +++ b/Tests/XToolTests/Fixtures/Projects/SyncFiles/Feature.swift @@ -0,0 +1 @@ +public func feature() -> Int { 42 } diff --git a/Tests/XToolTests/Fixtures/Projects/SyncFiles/Features.entitlements b/Tests/XToolTests/Fixtures/Projects/SyncFiles/Features.entitlements new file mode 100644 index 00000000..0c67376e --- /dev/null +++ b/Tests/XToolTests/Fixtures/Projects/SyncFiles/Features.entitlements @@ -0,0 +1,5 @@ + + + + + diff --git a/Tests/XToolTests/Fixtures/Projects/SyncFiles/Nested/message.txt b/Tests/XToolTests/Fixtures/Projects/SyncFiles/Nested/message.txt new file mode 100644 index 00000000..f5571267 --- /dev/null +++ b/Tests/XToolTests/Fixtures/Projects/SyncFiles/Nested/message.txt @@ -0,0 +1 @@ +Synchronized resource. diff --git a/Tests/XToolTests/Fixtures/Projects/SyncFiles/Notes.md b/Tests/XToolTests/Fixtures/Projects/SyncFiles/Notes.md new file mode 100644 index 00000000..17e0f0de --- /dev/null +++ b/Tests/XToolTests/Fixtures/Projects/SyncFiles/Notes.md @@ -0,0 +1 @@ +# Notes diff --git a/Tests/XToolTests/Fixtures/Projects/SyncFiles/module.modulemap b/Tests/XToolTests/Fixtures/Projects/SyncFiles/module.modulemap new file mode 100644 index 00000000..e01c5301 --- /dev/null +++ b/Tests/XToolTests/Fixtures/Projects/SyncFiles/module.modulemap @@ -0,0 +1 @@ +framework module Features {} diff --git a/Tests/XToolTests/Fixtures/Projects/Synchronized.xcodeproj/project.xcproj b/Tests/XToolTests/Fixtures/Projects/Synchronized.xcodeproj/project.xcproj new file mode 100644 index 00000000..db7db536 --- /dev/null +++ b/Tests/XToolTests/Fixtures/Projects/Synchronized.xcodeproj/project.xcproj @@ -0,0 +1,80 @@ +{ + "id": "985959785319747668373CC6", + "root-group-debug-id": "44CB005EE2E65D9CC817B0A0", + "configuration-list-debug-id": "3C05BEEE7303EE3FA7B250FA", + "class-prefix": "PF", + "default-configuration": "Debug", + "configurations": [ + { "id": "433548979C024CC63B5F3B8D", "name": "Debug", "file": "id:87E89ABB4C1C551FE08D355D" }, + { "id": "B800A40FDE6F0A0218D9BCD2", "name": "Release", "file": "id:87E89ABB4C1C551FE08D355D" }, + ], + "localizations": { + "development": "en", + "supported": [ + "Base", + ], + }, + "files": [ + { + "kind": "folder", + "id": "04FA91A3914FA55ADE36462E", + "path": "SyncFiles", + "target-membership": [ + "Features", + ], + "membership-exceptions": [ + { + "target": "Features", + "exclusions": [ + "Excluded.swift", + ], + }, + ], + }, + { "path": "Settings.xcconfig", "id": "87E89ABB4C1C551FE08D355D" }, + { + "kind": "group", + "id": "4EDC8BFAFC6BD936B849F38C", + "name": "Products", + "children": [ + { "path": "/Features.framework", "id": "FB9EF894175C327470F768D9", "index": false }, + ], + }, + ], + "targets": [ + { + "name": "Features", + "id": "5697D03DAEF4DE9CF8DB7CB1", + "configuration-list-debug-id": "A6FB65CCCF5741BC044EAB30", + "product": "id:FB9EF894175C327470F768D9", + "product-type": "framework", + "specialized-configurations": [ + { "id": "D52364EB7F4DAE720159F17A", "name": "Debug" }, + { "id": "D7133DDD9B2C56730E249807", "name": "Release" }, + ], + "build-phases": [ + { "kind": "compile-sources", "id": "CAF85B0888D78F1F83771A07" }, + { "kind": "resources", "id": "F77E8395C14206FA0A5A32E1" }, + ], + "build-settings": { + "CODE_SIGN_STYLE[config=Debug]": "Automatic", + "CODE_SIGN_STYLE[config=Release]": "Manual", + "GENERATE_INFOPLIST_FILE": "YES", + "PRODUCT_BUNDLE_IDENTIFIER": "sh.xtool.pif.Features", + "PRODUCT_NAME": "$(TARGET_NAME)", + }, + }, + ], + "build-settings": { + "CUSTOM[config=Debug][sdk=iphoneos*]": "device", + "OTHER_SWIFT_FLAGS[config=Debug]": [ + "$(inherited)", + "-DDEBUG", + ], + "SDKROOT": "iphoneos", + "SWIFT_OPTIMIZATION_LEVEL[config=Release]": "-O", + "SWIFT_VERSION": "6.0", + }, + "products-group": "id:4EDC8BFAFC6BD936B849F38C", + "last-upgrade": "16.0", +} diff --git a/Tests/XToolTests/Fixtures/Projects/Synchronized.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/Tests/XToolTests/Fixtures/Projects/Synchronized.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 00000000..919434a6 --- /dev/null +++ b/Tests/XToolTests/Fixtures/Projects/Synchronized.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/Tests/XToolTests/Fixtures/simple-xcode-project.pbxproj b/Tests/XToolTests/Fixtures/simple-xcode-project.pbxproj new file mode 100644 index 00000000..85d8ea23 --- /dev/null +++ b/Tests/XToolTests/Fixtures/simple-xcode-project.pbxproj @@ -0,0 +1,276 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 77; + objects = { + +/* Begin PBXBuildFile section */ + 1E4689B484284F82BCC2D4AA /* App.swift in Sources */ = {isa = PBXBuildFile; fileRef = AF02201919D6052C723EFCCB /* App.swift */; }; +/* End PBXBuildFile section */ + +/* Begin PBXFileReference section */ + 670DC86C0DB18949CDB87DC1 /* Probe.app */ = {isa = PBXFileReference; includeInIndex = 0; lastKnownFileType = wrapper.application; path = Probe.app; sourceTree = BUILT_PRODUCTS_DIR; }; + AF02201919D6052C723EFCCB /* App.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = App.swift; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXGroup section */ + 1ACCD3A56B4BF8334F3423E1 /* Products */ = { + isa = PBXGroup; + children = ( + 670DC86C0DB18949CDB87DC1 /* Probe.app */, + ); + name = Products; + sourceTree = ""; + }; + A6BDC90C598E12E8EFC5D4D8 = { + isa = PBXGroup; + children = ( + AF02201919D6052C723EFCCB /* App.swift */, + 1ACCD3A56B4BF8334F3423E1 /* Products */, + ); + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + 78DE96411CD202404D0EB238 /* Probe */ = { + isa = PBXNativeTarget; + buildConfigurationList = 994AE47A9D06B34933CCECDF /* Build configuration list for PBXNativeTarget "Probe" */; + buildPhases = ( + 12038353AE89A7B6471BFFB5 /* Sources */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = Probe; + packageProductDependencies = ( + ); + productName = Probe; + productReference = 670DC86C0DB18949CDB87DC1 /* Probe.app */; + productType = "com.apple.product-type.application"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + 587D90692CD26D0EA2FE6ACC /* Project object */ = { + isa = PBXProject; + attributes = { + BuildIndependentTargetsInParallel = YES; + LastUpgradeCheck = 1430; + TargetAttributes = { + }; + }; + buildConfigurationList = CF443E36A5351D254050E971 /* Build configuration list for PBXProject "Probe" */; + developmentRegion = en; + hasScannedForEncodings = 0; + knownRegions = ( + Base, + en, + ); + mainGroup = A6BDC90C598E12E8EFC5D4D8; + minimizedProjectReferenceProxies = 1; + preferredProjectObjectVersion = 77; + productRefGroup = 1ACCD3A56B4BF8334F3423E1 /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + 78DE96411CD202404D0EB238 /* Probe */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXSourcesBuildPhase section */ + 12038353AE89A7B6471BFFB5 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 1E4689B484284F82BCC2D4AA /* App.swift in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin XCBuildConfiguration section */ + 1FC4CBA9787F45EC026A86EB /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + CODE_SIGN_IDENTITY = "iPhone Developer"; + GENERATE_INFOPLIST_FILE = YES; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + ); + PRODUCT_BUNDLE_IDENTIFIER = com.example.Probe; + PRODUCT_NAME = CustomApp; + SDKROOT = iphoneos; + SWIFT_VERSION = 6.0; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Release; + }; + 7B27C290F387B2DD52A77DC0 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "$(inherited)", + "DEBUG=1", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 17.0; + MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; + MTL_FAST_MATH = YES; + ONLY_ACTIVE_ARCH = YES; + PRODUCT_NAME = "$(TARGET_NAME)"; + SDKROOT = iphoneos; + SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; + SWIFT_OPTIMIZATION_LEVEL = "-Onone"; + SWIFT_VERSION = 5.0; + }; + name = Debug; + }; + B3ADA2309A2481A6D5D24A7D /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_ANALYZER_NONNULL = YES; + CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_ENABLE_OBJC_WEAK = YES; + CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_COMMA = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_DOCUMENTATION_COMMENTS = YES; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INFINITE_RECURSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; + CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; + CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_QUOTED_INCLUDE_IN_FRAMEWORK_HEADER = YES; + CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; + CLANG_WARN_STRICT_PROTOTYPES = YES; + CLANG_WARN_SUSPICIOUS_MOVE = YES; + CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu11; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 17.0; + MTL_ENABLE_DEBUG_INFO = NO; + MTL_FAST_MATH = YES; + PRODUCT_NAME = "$(TARGET_NAME)"; + SDKROOT = iphoneos; + SWIFT_COMPILATION_MODE = wholemodule; + SWIFT_OPTIMIZATION_LEVEL = "-O"; + SWIFT_VERSION = 5.0; + }; + name = Release; + }; + EB2BA8CAEC40E01A3787EA3D /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + CODE_SIGN_IDENTITY = "iPhone Developer"; + GENERATE_INFOPLIST_FILE = YES; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/Frameworks", + ); + PRODUCT_BUNDLE_IDENTIFIER = com.example.Probe; + PRODUCT_NAME = CustomApp; + SDKROOT = iphoneos; + SWIFT_VERSION = 6.0; + TARGETED_DEVICE_FAMILY = "1,2"; + }; + name = Debug; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + 994AE47A9D06B34933CCECDF /* Build configuration list for PBXNativeTarget "Probe" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + EB2BA8CAEC40E01A3787EA3D /* Debug */, + 1FC4CBA9787F45EC026A86EB /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Debug; + }; + CF443E36A5351D254050E971 /* Build configuration list for PBXProject "Probe" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 7B27C290F387B2DD52A77DC0 /* Debug */, + B3ADA2309A2481A6D5D24A7D /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Debug; + }; +/* End XCConfigurationList section */ + }; + rootObject = 587D90692CD26D0EA2FE6ACC /* Project object */; +} diff --git a/Tests/XToolTests/PackSchemaTests.swift b/Tests/XToolTests/PackSchemaTests.swift new file mode 100644 index 00000000..d42aa032 --- /dev/null +++ b/Tests/XToolTests/PackSchemaTests.swift @@ -0,0 +1,92 @@ +import XCLIUtils +import XUtils +import Foundation +import Testing +@testable import XToolSupport + +struct PackSchemaTests { + @Test(arguments: ["Package.swift", "App.xcworkspace", "App.xcodeproj"], [false, true]) + func selectsConfiguredProject(name: String, absolute: Bool) async throws { + let directory = try temporaryDirectory() + defer { try? FileManager.default.removeItem(at: directory) } + let nested = directory.appending(path: "Nested", directoryHint: .isDirectory) + try FileManager.default.createDirectory(at: nested, withIntermediateDirectories: true) + let project = nested.appending(path: name) + if name == "Package.swift" { + try Data().write(to: project) + } else { + try FileManager.default.createDirectory(at: project, withIntermediateDirectories: true) + } + // Explicit selection takes precedence even when discovery would be ambiguous. + for name in ["Other.xcworkspace", "Another.xcworkspace"] { + try FileManager.default.createDirectory(at: directory.appending(path: name), withIntermediateDirectories: true) + } + let path = absolute ? project.path : "Nested/\(name)" + try Data(""" + version: 1 + orgID: org.example + project: \(path) + skipLSP: true + """.utf8).write(to: directory.appending(path: "xtool.yml")) + + let discovered = try await XToolProject.discover(start: directory) + #expect(discovered.base.schema.project == path) + #expect(discovered.base.schema.skipLSP == true) + switch discovered { + case .swiftPM(let swiftPM): + #expect(name == "Package.swift") + #expect(swiftPM.manifest.resolvingSymlinksInPath().path == project.resolvingSymlinksInPath().path) + case .xcode(let xcode): + #expect(name != "Package.swift") + #expect(xcode.container.resolvingSymlinksInPath().path == project.resolvingSymlinksInPath().path) + } + } + + @Test(arguments: ["", ".", "Sources/App.swift", "Package.txt", "App.xcodeproj/project.pbxproj"]) + func rejectsUnsupportedProjectPaths(project: String) throws { + #expect(throws: StringError.self) { + try PackSchema(validating: .init(version: .v1, orgID: "org.example", project: project)) + } + } + + @Test func rejectsMissingConfiguredProject() async throws { + let directory = try temporaryDirectory() + defer { try? FileManager.default.removeItem(at: directory) } + try Data(""" + version: 1 + orgID: org.example + project: Missing.xcodeproj + """.utf8).write(to: directory.appending(path: "xtool.yml")) + try Data().write(to: directory.appending(path: "Package.swift")) + await #expect(throws: Console.Error.self) { + try await XToolProject.discover(start: directory) + } + } + + @Test func discoversProjectWhenFieldIsOmitted() async throws { + let directory = try temporaryDirectory() + defer { try? FileManager.default.removeItem(at: directory) } + try Data(""" + version: 1 + orgID: org.example + """.utf8).write(to: directory.appending(path: "xtool.yml")) + try Data().write(to: directory.appending(path: "Package.swift")) + + let project = try await XToolProject.discover(start: directory) + #expect(project.base.schema.project == nil) + #expect(project.base.schema.orgID == "org.example") + guard case .swiftPM(let swiftPM) = project else { + Issue.record("Expected a Swift package") + return + } + #expect(swiftPM.manifest.resolvingSymlinksInPath().path + == directory.appending(path: "Package.swift").resolvingSymlinksInPath().path) + } + + private func temporaryDirectory() throws -> URL { + let directory = FileManager.default.temporaryDirectory.appending(path: UUID().uuidString) + .resolvingSymlinksInPath() + try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true) + return directory + } +} diff --git a/Tests/XToolTests/RequiresXcodeTrait.swift b/Tests/XToolTests/RequiresXcodeTrait.swift index 094dc7f5..92466897 100644 --- a/Tests/XToolTests/RequiresXcodeTrait.swift +++ b/Tests/XToolTests/RequiresXcodeTrait.swift @@ -19,7 +19,18 @@ enum TestXcode { @TaskLocal fileprivate static var _current: URL? - fileprivate static let resolved = Task { + fileprivate static let resolved = Task<(url: URL, version: String)?, Error> { + guard let url = try await resolveURL() else { return nil } + let data = try Data(contentsOf: url.appending(path: "Contents/version.plist")) + let version = try PropertyListDecoder().decode(Version.self, from: data) + return (url, version.CFBundleShortVersionString) + } + + private struct Version: Decodable { + let CFBundleShortVersionString: String + } + + private static func resolveURL() async throws -> URL? { if let xcode = ProcessInfo.processInfo.environment["XTL_TEST_XCODE_APP"], !xcode.isEmpty { return URL(filePath: xcode) } @@ -42,15 +53,20 @@ enum TestXcode { } struct RequiresXcodeTrait: TestTrait, SuiteTrait, TestScoping { + var minimumVersion: String? + func provideScope( for test: Test, testCase: Test.Case?, performing function: () async throws -> Void ) async throws { guard let xcode = try? await TestXcode.resolved.value else { - try Test.cancel("Could not resolve Xcode.app path. Set XTL_TEST_XCODE_APP.") + try Test.cancel("Could not resolve Xcode.app path and version. Set XTL_TEST_XCODE_APP.") + } + if let minimumVersion, xcode.version.compare(minimumVersion, options: .numeric) == .orderedAscending { + try Test.cancel("Requires Xcode \(minimumVersion) or later; found \(xcode.version) at \(xcode.url.path).") } - return try await TestXcode.$_current.withValue(xcode) { + return try await TestXcode.$_current.withValue(xcode.url) { try await function() } } @@ -60,4 +76,9 @@ extension Trait where Self == RequiresXcodeTrait { static var requiresXcode: Self { Self() } + + /// Requires the resolved Xcode installation to be at least the given version. + static func requiresXcode(_ minimumVersion: String) -> Self { + Self(minimumVersion: minimumVersion) + } } diff --git a/Tests/XToolTests/SwiftPMBuildDescriberTests.swift b/Tests/XToolTests/SwiftPMBuildDescriberTests.swift index 669cf379..677dc0a5 100644 --- a/Tests/XToolTests/SwiftPMBuildDescriberTests.swift +++ b/Tests/XToolTests/SwiftPMBuildDescriberTests.swift @@ -97,6 +97,14 @@ struct SwiftPMBuildDescriberTests { return } #expect(group.children.count == 5) + let projectURL = projectDir.appending(path: "App.xcodeproj") + let scheme = try XcodeScheme(named: "App-App", containers: [projectURL]) + let runnable = try #require(scheme.runnable) + #expect(runnable.name == "App-App") + #expect(runnable.objectID == app.objectID.rawValue) + #expect(runnable.projectURL == projectURL.resolvingSymlinksInPath()) + #expect(scheme.buildTargets.map(\.name) == ["App-App"]) + #expect(scheme.buildImplicitDependencies) } @Test func dumpsPIFAndGeneratesProjectForSwiftPackage() async throws { diff --git a/Tests/XToolTests/TestFixtures.swift b/Tests/XToolTests/TestFixtures.swift index 47a42a1a..1089bec5 100644 --- a/Tests/XToolTests/TestFixtures.swift +++ b/Tests/XToolTests/TestFixtures.swift @@ -1,5 +1,13 @@ import Foundation +@testable import XToolSupport func fixtureURL(_ path: String) -> URL { URL(filePath: #filePath).deletingLastPathComponent().appending(path: "Fixtures/\(path)") } + +extension XToolProject { + /// An Xcode project whose scratch files live in the current directory rather than next to the fixture. + static func testXcode(_ container: URL, schema: PackSchema = .default) -> Self { + .xcode(.init(base: .init(rootDirectory: URL(filePath: "."), schema: schema), container: container)) + } +} diff --git a/Tests/XToolTests/XcodeBuildDescriberTests.swift b/Tests/XToolTests/XcodeBuildDescriberTests.swift new file mode 100644 index 00000000..0110f4ed --- /dev/null +++ b/Tests/XToolTests/XcodeBuildDescriberTests.swift @@ -0,0 +1,82 @@ +import XCLIUtils +import CustomDump +import XUtils +import XPIF +import Foundation +import Testing +@testable import XToolSupport +import DependenciesTestSupport + +struct XcodeBuildDescriberTests { + @Test func decodesXcodePIFWithoutLosingSettings() throws { + let data = try fixtureData() + var pif = try XPIF.Container(data: data) + #expect(pif.workspace.projects[0].name == "Probe") + #expect(!pif.workspace.projects[0].isPackage) + #expect(pif.workspace.projects[0].targets[0].common.name == "Probe") + let decoded = try XPIF.Container(data: pif.data()) + #expect(decoded.workspace.projects[0].targets.map(\.id) == pif.workspace.projects[0].targets.map(\.id)) + pif.workspace.name = "Changed" + #expect(try XPIF.Container(data: pif.data()).workspace.name == "Changed") + } + + @Test(.dependencies { + $0.out.write = { _, _, _ in } + }) + func detectsProjectsAndPrefersWorkspacesThenProjectsThenSwiftPackages() async throws { + let directory = FileManager.default.temporaryDirectory.appending(path: UUID().uuidString) + try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true) + defer { try? FileManager.default.removeItem(at: directory) } + await #expect(throws: Console.Error.self) { try await XToolProject.discover(start: directory) } + try Data().write(to: directory.appending(path: "Package@swift-6.4.swift")) + guard case .swiftPM = try await XToolProject.discover(start: directory) else { + Issue.record("Expected a Swift package") + return + } + let project = directory.appending(path: "App.xcodeproj") + try FileManager.default.createDirectory(at: project, withIntermediateDirectories: true) + guard case .xcode(let detected) = try await XToolProject.discover(start: directory) else { + Issue.record("Expected an Xcode project") + return + } + #expect(detected.container.resolvingSymlinksInPath().path == project.resolvingSymlinksInPath().path) + try FileManager.default.createDirectory(at: directory.appending(path: "Other.xcodeproj"), withIntermediateDirectories: true) + await #expect(throws: Console.Error.self) { try await XToolProject.discover(start: directory) } + let workspace = directory.appending(path: "App.xcworkspace") + try FileManager.default.createDirectory(at: workspace, withIntermediateDirectories: true) + guard case .xcode(let detectedWorkspace) = try await XToolProject.discover(start: directory) else { + Issue.record("Expected an Xcode workspace") + return + } + #expect(detectedWorkspace.container.resolvingSymlinksInPath().path == workspace.resolvingSymlinksInPath().path) + try FileManager.default.createDirectory(at: directory.appending(path: "Other.xcworkspace"), withIntermediateDirectories: true) + await #expect(throws: Console.Error.self) { try await XToolProject.discover(start: directory) } + } + + @Test func generatesPIFWithoutXcode() async throws { + let directory = FileManager.default.temporaryDirectory.appending(path: UUID().uuidString) + let project = directory.appending(path: "Probe.xcodeproj") + try FileManager.default.createDirectory(at: project, withIntermediateDirectories: true) + defer { try? FileManager.default.removeItem(at: directory) } + let fixture = fixtureURL("simple-xcode-project.pbxproj") + try FileManager.default.copyItem(at: fixture, to: project.appending(path: "project.pbxproj")) + try Data("import UIKit\n".utf8).write(to: directory.appending(path: "App.swift")) + let description = try await XToolProject.testXcode(project).describe(configuration: .debug, triple: "arm64-apple-ios") + #expect(description.configuration == .debug) + #expect(try !description.pif.data().isEmpty) + let targets = description.pif.workspace.projects.flatMap(\.targets) + let umbrella = try #require(targets.first { $0.id == "xtool:UMBRELLA" }) + guard case .aggregate(let aggregate) = umbrella else { + Issue.record("Expected an aggregate umbrella target") + return + } + let app = try #require(targets.first { $0.id != umbrella.id }) + expectNoDifference(aggregate.common.dependencies.map(\.targetId), [app.id]) + expectNoDifference(aggregate.common.buildConfigs.map(\.name), ["Debug", "Release"]) + } + + private func fixtureData() throws -> Data { + let url = fixtureURL("xcode-project.json") + return try Data(contentsOf: url) + } +} diff --git a/Tests/XToolTests/XcodePIFBuilderTests.swift b/Tests/XToolTests/XcodePIFBuilderTests.swift new file mode 100644 index 00000000..e6a6f46e --- /dev/null +++ b/Tests/XToolTests/XcodePIFBuilderTests.swift @@ -0,0 +1,290 @@ +import Foundation +import XcodeProjects +import XPIF +import Testing +@testable import XToolSupport +@testable import XcodePIFConversion + +struct XcodePIFBuilderTests { + let projectURL = URL(filePath: "/tmp/Probe.xcodeproj").resolvingSymlinksInPath() + + @Test func convertsPBXProjectAndProducesStablePIF() throws { + let source = try fixtureProject() + let workspace = try XcodePIFBuilder(project: source).build() + let project = try #require(workspace.projects.first) + #expect(project.id.value == "e199eb287a19a5a0c958424ee41fbfd2") + #expect(project.name == "Probe") + #expect(project.projectDir == projectURL.deletingLastPathComponent().path) + #expect(project.defaultConfigurationName == "Debug") + #expect(project.developmentRegion == "en") + #expect(project.knownRegions == ["Base", "en"]) + guard case .target(let target) = try #require(project.targets.first) else { + Issue.record("Expected a native target") + return + } + let prefix = XcodeLoadedProject.digest("Probe.xcodeproj") + #expect(target.id.value == prefix + XcodeLoadedProject.digest("78DE96411CD202404D0EB238")) + #expect(target.productType == .application) + #expect(target.productReference.id.value == prefix + XcodeLoadedProject.digest("670DC86C0DB18949CDB87DC1")) + #expect(target.buildConfigs.map(\.name) == ["Debug", "Release"]) + #expect(target.buildConfigs.allSatisfy { $0.settings[.PRODUCT_NAME] == "CustomApp" }) + #expect(project.buildConfigs[0].settings[single: "SWIFT_OPTIMIZATION_LEVEL"] == "-Onone") + #expect(project.buildConfigs[1].settings[single: "SWIFT_OPTIMIZATION_LEVEL"] == "-O") + guard case .sources(let phase) = try #require(target.buildPhases.first) else { + Issue.record("Expected a sources phase") + return + } + #expect(phase.files.map(\.ref) == [.reference(id: project.mainGroup.subitems[0].id)]) + let data = try XPIF.Container(workspace: workspace).data() + #expect(try XPIF.Container(data: data).data() == data) + let jsonProject = try XcodeProject( + project: .init(jsonRepresentation: source.project.jsonRepresentation()), url: projectURL, format: .xcproj + ) + let jsonWorkspace = try XcodePIFBuilder(project: jsonProject).build() + #expect(try XPIF.Container(workspace: jsonWorkspace).data() == data) + #expect(try XPIF.Container(workspace: XcodePIFBuilder(project: source).build()).data() == data) + } + + @Test(.requiresXcode) func generatedWorkspaceIsAcceptedByBuildService() async throws { + let workspace = try XcodePIFBuilder(project: fixtureProject()).build() + let description = try BuildDescription( + directory: URL(filePath: "."), + pif: XcodeBuildDescriber(project: .init( + base: .init(rootDirectory: URL(filePath: "."), schema: .default), container: projectURL + )).describe(pif: .init(workspace: workspace)), + configuration: .debug, + triple: "arm64-apple-ios-simulator" + ) + let coordinator = try await BuildCoordinator(description: description) + #expect(try await coordinator.mainProductDetails().output.lastPathComponent == "CustomApp.app") + await coordinator.close() + } + + @Test func mapsConfigurationConditionsAndXCConfigs() throws { + var source = try fixtureProject() + source.project.buildSettings = [ + "FLAGS": .array(["base"]), + "FLAGS[config=Debug][sdk=iphoneos*]": .array(["$(inherited)", "device-debug"]), + "FLAGS[config=Release]": .string("release"), + "OTHER[config=Deb*]": .string("debug"), + "ORDERED[config=Debug][sdk=iphoneos*]": .string("specific"), + "ORDERED[sdk=iphoneos*][config=*]": .string("default"), + ] + source.project.topLevelReferences.append(.fileReference(.init( + objectID: nil, path: "Settings.xcconfig", explicitFileType: nil, expectedSignature: nil, + textEncoding: nil, lineEnding: nil, includeInIndex: nil, buildFiles: [] + ))) + source.project.configurations[0].file = .init( + anchor: .namePath(.init(components: [.child("Settings.xcconfig")])), relativePath: nil + ) + let result = try XcodePIFBuilder(project: source).build().projects[0] + let debug = result.buildConfigs[0] + let release = result.buildConfigs[1] + #expect(debug.settings.values["FLAGS"] == .string("base")) + #expect(debug.settings.values["FLAGS[sdk=iphoneos*]"] == .string("$(inherited) device-debug")) + #expect(debug.settings[single: "OTHER"] == "debug") + #expect(debug.settings[single: "ORDERED[sdk=iphoneos*]"] == "specific") + #expect(release.settings[single: "ORDERED[sdk=iphoneos*]"] == "default") + #expect(release.settings[single: "FLAGS"] == "release") + #expect(release.settings.values["OTHER"] == nil) + #expect(debug.baseConfigurationFileReference == result.mainGroup.subitems.last?.id) + #expect(result.targets[0].common.buildConfigs[0].baseConfigurationFileReference == nil) + } + + @Test(arguments: [ + ("Debug.+(QA)", "Debug.+(QA)", true), + ("Debug.+(QA)", "DebugXYZQA", false), + ("D?bug", "Débug", true), + ("D?bug", "Debugging", false), + ("*Release?", "MyRelease1", true), + ("*Release?", "MyRelease12", false), + ("*", "", true), + ("", "Debug", false), + ]) + func matchesConfigurationGlobs(pattern: String, configuration: String, applies: Bool) { + let settings = XcodePIFProjectBuilder.buildSettings([ + "FLAGS[config=\(pattern)][sdk=iphoneos*][config=*]": .array(["flag"]), + ], configuration: configuration) + #expect(settings.values["FLAGS[sdk=iphoneos*]"] == (applies ? .string("flag") : nil)) + #expect(settings.values.count == (applies ? 1 : 0)) + } + + @Test func resolvesNamedPhasesLocalProductsAndBuildFileAttributes() throws { + var source = try fixtureProject() + var app = source.project.targets[0].commonProperties + let frameworkID = XCSchema.ObjectID("FRAMEWORK") + var framework = app + framework.name = "Support" + framework.objectID = frameworkID + framework.specializedConfigurations = [] + framework.buildPhases = [] + framework.dependencies = [] + framework.product = .namePath(.init(components: [.child("Support.framework")])) + framework.productTypeID = .init(rawValue: "com.apple.product-type.framework") + app.dependencies = [.localTarget(.init(targetName: "Support"), [.init(rawValue: "maccatalyst")])] + app.buildPhases.append(.frameworks(.init(objectID: nil, name: nil))) + app.buildPhases.append(.copy(.init( + objectID: nil, name: "Embed", bundleBasePath: .frameworksDir, relativePath: "", scope: .always + ))) + source.project.targets = [.native(app), .native(framework)] + let properties = XCSchema.BuildFileProperties( + platformFilters: [.init(rawValue: "ios")], additionalBuildFlags: "-DTEST", assetTags: [.init(rawValue: "asset")], + attributes: .init(headerRole: .public, machInterfaceGeneration: .client, isWeak: true, codeSignOnCopy: true, + codeGeneration: .skip, headerPreservation: .removeOnCopy, decompress: true, + codeGenerationVisibility: nil) + ) + source.project.topLevelReferences.append(.fileReference(.init( + objectID: nil, path: "/Support.framework", explicitFileType: nil, expectedSignature: nil, + textEncoding: nil, lineEnding: nil, includeInIndex: nil, buildFiles: [ + .init(objectID: nil, buildPhase: .named(target: .init(targetName: "Probe"), kind: .frameworks, name: nil), + properties: properties), + .init(objectID: nil, buildPhase: .named(target: .init(targetName: "Probe"), kind: .copy, name: "Embed"), + properties: properties), + ] + ))) + let target = try XcodePIFBuilder(project: source).build().projects[0].targets[0] + var dependency = XPIF.TargetDependency( + targetId: .init(XcodeLoadedProject.digest("Probe.xcodeproj") + XcodeLoadedProject.digest("FRAMEWORK")), + platformFilters: [.init(platform: "ios", environment: "maccatalyst")] + ) + dependency.additionalFields["name"] = .string("Support") + #expect(target.common.dependencies == [dependency]) + guard case .frameworks(let phase) = target.common.buildPhases[1], + case .copyFiles(let copy) = target.common.buildPhases[2] else { + Issue.record("Expected link and embed phases") + return + } + let file = try #require(phase.files.first) + #expect(file.ref == .targetProduct(id: dependency.targetId)) + #expect(file.headerVisibility == .public) + #expect(file.generatedCodeVisibility == .noCodegen) + #expect(file.platformFilters == [.init(platform: "ios")]) + #expect(file.additionalCompilerOptions == .string("-DTEST")) + #expect(file.shouldLinkWeakly && file.codeSignOnCopy && file.removeHeadersOnCopy && file.decompress) + #expect(file.assetTags == ["asset"]) + #expect(file.migCodegenFiles == "client") + #expect(copy.destinationSubfolder == .frameworks) + #expect(copy.files.first?.ref == file.ref) + } + + @Test func convertsScriptsAndRules() throws { + var source = try fixtureProject() + var app = source.project.targets[0].commonProperties + app.buildPhases.append(.script(.init( + objectID: nil, name: "Generate", shellPath: "/bin/sh", script: "echo generated", logEnvironmentVariables: true, + inputPaths: ["input"], inputFileListPaths: ["inputs.xcfilelist"], outputPaths: ["output"], + outputFileListPaths: ["outputs.xcfilelist"], dependencyFile: "deps.d", runOnEveryBuild: true, scope: .install + ))) + app.buildRules = [.init( + objectID: nil, processor: "com.apple.compilers.proxy.script", name: "Rule", fileType: nil, + filePatterns: "*.custom", script: "echo rule", inputFiles: ["in"], inputFileLists: [], + outputFiles: ["out"], outputFileLists: [], outputFilesCompilerFlags: ["-DGENERATED"], + dependencyFile: "rule.d", runOncePerArchitecture: true + )] + source.project.targets[0] = .native(app) + let result = try XcodePIFBuilder(project: source).build() + let target = result.projects[0].targets[0] + guard case .shellScript(let script) = target.common.buildPhases[1] else { + Issue.record("Expected a script phase") + return + } + #expect(script.scriptContents == "echo generated") + #expect(script.inputFileListPaths == ["inputs.xcfilelist"]) + #expect(script.outputFileListPaths == ["outputs.xcfilelist"]) + #expect(script.dependencyInfo == .makefile("deps.d")) + #expect(script.alwaysOutOfDate && script.emitEnvironment && script.runOnlyForDeploymentPostprocessing) + #expect(target.common.buildRules.first?.input == .filePattern("*.custom")) + let decoded = try XPIF.Container(data: XPIF.Container(workspace: result).data()) + let decodedTarget = decoded.workspace.projects[0].targets[0] + #expect(decodedTarget.common.buildPhases == target.common.buildPhases) + #expect(decodedTarget.common.buildRules == target.common.buildRules) + } + + @Test func convertsLocalizedResourcesAndFilePathBases() throws { + var source = try fixtureProject() + var app = source.project.targets[0].commonProperties + app.buildPhases.append(.resources(.init(objectID: nil, name: nil))) + source.project.targets[0] = .native(app) + let properties = XCSchema.BuildFileProperties( + platformFilters: [], additionalBuildFlags: nil, assetTags: [], + attributes: .init(headerRole: nil, machInterfaceGeneration: nil, isWeak: false, codeSignOnCopy: false, + codeGeneration: .default, headerPreservation: .keep, decompress: false, codeGenerationVisibility: nil) + ) + let child = XCSchema.FileReference( + objectID: nil, path: "en.lproj/Main.strings", explicitFileType: nil, expectedSignature: "signature", + textEncoding: .init(rawValue: .utf8), lineEnding: nil, includeInIndex: nil, buildFiles: [] + ) + source.project.topLevelReferences.append(.group(.init( + objectID: nil, name: "Resources", path: "/Resources", includeInIndex: nil, + children: [.variantGroup(.init( + objectID: nil, name: "Main.strings", path: "", includeInIndex: nil, + buildFiles: [.init(objectID: nil, + buildPhase: .named(target: .init(targetName: "Probe"), kind: .resources, name: nil), + properties: properties)], children: [child] + ))] + ))) + let project = try XcodePIFBuilder(project: source).build().projects[0] + guard case .group(let resources) = project.mainGroup.subitems[2], + case .group(let variant) = resources.subitems[0], case .file(let file) = variant.subitems[0], + case .copyBundleResources(let phase) = project.targets[0].common.buildPhases[1] else { + Issue.record("Expected a localized resource group and phase") + return + } + #expect(resources.pathBase == .projectDir) + #expect(resources.path == "Resources") + #expect(variant.kind == .variantGroup) + #expect(file.pathBase == .groupDir) + #expect(file.fileTextEncoding == "utf-8") + #expect(file.regionVariantName == "en") + #expect(file.expectedSignature == "signature") + #expect(phase.files.first?.ref == .reference(id: variant.id)) + } + + @Test func convertsAggregateAndExternalTargets() throws { + var source = try fixtureProject() + var aggregate = source.project.targets[0].commonProperties + aggregate.objectID = .init("AGGREGATE") + aggregate.name = "All" + aggregate.product = nil + aggregate.productTypeID = nil + aggregate.buildPhases = [] + aggregate.specializedConfigurations = [] + aggregate.dependencies = [.localTarget(.init(targetName: "Probe"), [])] + var external = aggregate + external.objectID = .init("EXTERNAL") + external.name = "Tool" + external.dependencies = [] + source.project.targets += [ + .aggregate(aggregate), + .externalBuildSystem(.init(commonProperties: external, buildToolPath: "/usr/bin/make", buildToolArguments: "all", + buildToolWorkingDirectory: nil, passBuildSettingsInEnvironment: false)), + ] + let project = try XcodePIFBuilder(project: source).build().projects[0] + guard case .aggregate(let target) = project.targets[1], case .external(let tool) = project.targets[2] else { + Issue.record("Expected aggregate and external targets") + return + } + #expect(target.dependencies.first?.targetId == project.targets[0].id) + #expect(target.buildConfigs.map(\.name) == ["Debug", "Release"]) + #expect(tool.toolPath == .string("/usr/bin/make")) + #expect(tool.arguments == .string("all")) + #expect(tool.workingDirectory == .string("$(PROJECT_DIR)")) + #expect(!tool.passBuildSettingsInEnvironment) + } + + @Test func rejectsUnresolvedAndAmbiguousMemberships() throws { + var source = try fixtureProject() + var app = source.project.targets[0].commonProperties + app.buildPhases = [] + source.project.targets[0] = .native(app) + #expect(throws: XcodePIFConversionError.self) { try XcodePIFBuilder(project: source).build() } + app.buildPhases = [.sources(.init(objectID: .init("SAME"), name: nil)), .sources(.init(objectID: .init("SAME"), name: nil))] + source.project.targets[0] = .native(app) + #expect(throws: XcodePIFConversionError.self) { try XcodePIFBuilder(project: source).build() } + } + + private func fixtureProject() throws -> XcodeProject { + let url = fixtureURL("simple-xcode-project.pbxproj") + return try XcodeProject(project: .init(pbxprojRepresentation: Data(contentsOf: url)), url: projectURL) + } +} diff --git a/Tests/XToolTests/XcodePIFParityTests.swift b/Tests/XToolTests/XcodePIFParityTests.swift new file mode 100644 index 00000000..a0117540 --- /dev/null +++ b/Tests/XToolTests/XcodePIFParityTests.swift @@ -0,0 +1,124 @@ +import CustomDump +import Foundation +import Subprocess +import Testing +import XcodePIFConversion +import XcodeProjects +import XPIF +import XUtils +import Dependencies +@testable import XToolSupport + +struct XcodePIFParityTests { + #if os(macOS) + @Test(.requiresXcode("27.2"), arguments: [ + "Features.xcodeproj", + "Synchronized.xcodeproj", + "App.xcodeproj", + "PIF.xcworkspace", + "Packages.xcodeproj", + "DiscoveredPackages.xcodeproj", + ]) + func matchesXcodebuild(fixture: String) async throws { + let projects = fixtureURL("Projects") + let url = projects.appending(path: fixture).resolvingSymlinksInPath() + let temporary = try TemporaryDirectory(name: "PIFParity") + let dump = temporary.url.appending(path: "xcode.json") + let derivedData = temporary.url.appending(path: "DerivedData") + let result = try await Subprocess.run( + .path(.init(TestXcode.current().appending(path: "Contents/Developer/usr/bin/xcodebuild").path)), + arguments: .init([ + url.pathExtension == "xcworkspace" ? "-workspace" : "-project", url.path, + "-dumpPIF", dump.path, + "-IDECustomDerivedDataLocation=\(derivedData.path)", + "-IDEEnableNewPackagePIFBuilder=YES", + "-IDEApplicationwideBuildSettings={}", "-PBXApplicationwideBuildSettings={}", + ]), + output: .string(limit: .max), error: .string(limit: .max) + ) + try #require(result.terminationStatus.isSuccess, "\(result.standardOutput)\n\(result.standardError)") + let builder = try url.pathExtension == "xcworkspace" + ? XcodePIFBuilder(workspace: XcodeWorkspace(url: url)) + : XcodePIFBuilder(project: XcodeProject(url: url)) + let packages = try await withDependencies { + $0.out.write = { _, _, _ in } + } operation: { + try await XcodePackagePIFBuilder(builder: builder, rootDirectory: URL(filePath: ".")).build() + } + let actual = try XPIF.Container(workspace: builder.build(packageProjects: packages)).data() + let expected = try Data(contentsOf: dump) + expectNoDifference(try canonicalJSON(expected), try canonicalJSON(actual)) + let roundTripped = try XPIF.Container(data: actual).data() + expectNoDifference(try canonicalJSON(actual), try canonicalJSON(roundTripped)) + } + #endif + + @Test func ignoresEnvelopeSignaturesAndResolvesTheirReferences() throws { + let resigned = comparisonFixture.replacingOccurrences(of: "signature-", with: "resigned-") + expectNoDifference(try canonicalJSON(Data(comparisonFixture.utf8)), try canonicalJSON(Data(resigned.utf8))) + } + + @Test(arguments: [ + ("\"name\":\"App\"", "\"name\":\"Changed\""), + ("\"expectedSignature\":\"signed-file\"", "\"expectedSignature\":\"changed\""), + ("\"signature\":\"semantic-value\"", "\"signature\":\"changed\""), + ("[\"signature-a\",\"signature-b\"]", "[\"signature-b\",\"signature-a\"]"), + ("[\"signature-a\",\"signature-b\"]", "[\"signature-a\",\"signature-a\"]"), + ("\"name\":\"App\"", "\"name\":\"App\",\"extra\":false"), + ]) + func preservesNonSignatureDifferences(original: String, replacement: String) throws { + let changed = comparisonFixture.replacingOccurrences(of: original, with: replacement) + #expect(try canonicalJSON(Data(comparisonFixture.utf8)) != canonicalJSON(Data(changed.utf8))) + } + + private var comparisonFixture: String { + """ + [ + {"type":"workspace","signature":"signature-w","contents":{"guid":"w","projects":["signature-p"]}}, + {"type":"project","signature":"signature-p","contents":{"guid":"p","targets":["signature-a","signature-b"]}}, + {"type":"target","signature":"signature-a","contents":{"guid":"a","name":"App", + "expectedSignature":"signed-file","buildSettings":{"signature":"semantic-value"}}}, + {"type":"target","signature":"signature-b","contents":{"guid":"b","name":"Library"}} + ] + """ + } + + /// Compare raw JSON, so decoding through XPIF cannot hide missing or extra fields. + /// Signature references are replaced with GUIDs, preserving graph membership and order. + private func canonicalJSON(_ data: Data) throws -> String { + var objects = try JSONDecoder().decode([[String: XPIF.JSONValue]].self, from: data) + var signatures: [XPIF.JSONValue: XPIF.JSONValue] = [:] + for object in objects { + let signature = try #require(object["signature"]) + guard case .object(let contents) = object["contents"] else { + throw ParityError.invalidContents + } + let guid = try #require(contents["guid"]) + try #require(signatures.updateValue(guid, forKey: signature) == nil, "Duplicate signature") + } + for index in objects.indices { + objects[index].removeValue(forKey: "signature") + guard case .object(var contents) = objects[index]["contents"] else { + throw ParityError.invalidContents + } + let key: String? + switch objects[index]["type"] { + case .string("workspace"): key = "projects" + case .string("project"): key = "targets" + default: key = nil + } + if let key { + guard case .array(let references) = contents[key] else { throw ParityError.invalidContents } + contents[key] = .array(try references.map { try #require(signatures[$0], "Dangling signature reference") }) + } + objects[index]["contents"] = .object(contents) + } + let encoder = JSONEncoder() + encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes] + return String(decoding: try encoder.encode(objects), as: UTF8.self) + } + + private enum ParityError: Error { + case invalidContents + } +} diff --git a/Tests/XToolTests/XcodePackagePIFBuilderTests.swift b/Tests/XToolTests/XcodePackagePIFBuilderTests.swift new file mode 100644 index 00000000..3b469117 --- /dev/null +++ b/Tests/XToolTests/XcodePackagePIFBuilderTests.swift @@ -0,0 +1,170 @@ +import XCLIUtils +import CustomDump +import Dependencies +import DependenciesTestSupport +import Foundation +import Subprocess +import SwiftBuild +import Testing +import XcodeProjects +import XPIF +import XUtils +@testable import XcodePIFConversion +@testable import XToolSupport + +struct XcodePackagePIFBuilderTests { + @Test(.requiresXcode, .dependencies { $0.out.write = { _, _, _ in } }) + func resolvesPackagesDiscoveredInSynchronizedFolders() async throws { + let fixtures = fixtureURL("Projects") + let project = try XcodeProject(url: fixtures.appending(path: "DiscoveredPackages.xcodeproj")) + #expect(project.project.packages.isEmpty) + let builder = try XcodePIFBuilder(project: project) + #expect(builder.packageReferences.count == 1) + let loaded = try #require(builder.projects.values.first) + #expect(!loaded.references.contains { $0.url?.lastPathComponent == "Account.swift" }) + let packages = try await XcodePackagePIFBuilder(builder: builder, rootDirectory: URL(filePath: ".")).build() + let workspace = try builder.build(packageProjects: packages) + let account = try #require(packages.flatMap(\.targets).first { $0.common.name == "Account-product" }) + #expect(workspace.projects[0].targets[0].common.dependencies.contains { $0.targetId == account.id }) + + // An explicit declaration retains its traits and is not duplicated by discovery. + var declared = project + declared.project.packages = [.init(location: .local(.init(path: "Modules/Account")), traits: ["Feature"])] + let references = try XcodePIFBuilder(project: declared).packageReferences + #expect(references.count == 1) + #expect(references[0].package.traits == ["Feature"]) + #expect(references[0].isRoot) + } + + @Test(arguments: [ + (XCSchema.SwiftPackageVersionConstraint.revision("abc123"), "revision: \"abc123\""), + (.branch("feature/\"quoted\""), "branch: \"feature/\\\"quoted\\\"\""), + (.version("1.2.3"), "exact: \"1.2.3\""), + (.versionRange(min: "1.0.0", max: "2.0.0"), "\"1.0.0\"..<\"2.0.0\""), + (.upToNextMinorVersion("1.2.3"), ".upToNextMinor(from: \"1.2.3\")"), + (.upToNextMajorVersion("1.2.3"), "from: \"1.2.3\""), + ]) + func manifestRequirements(constraint: XCSchema.SwiftPackageVersionConstraint, expected: String) throws { + let reference = remote(constraint) + let manifest = try XcodePackagePIFBuilder.manifest(references: [reference]) + expectNoDifference(manifest, """ + // swift-tools-version: 6.1 + import PackageDescription + + let package = Package( + name: "XtoolDependencies", + dependencies: [ + .package(url: "https://example.com/Library.git", \(expected)), + ] + ) + + """) + } + + @Test func deduplicatesReferencesAndResolvesLocalPaths() throws { + let root = URL(filePath: "/tmp/Projects", directoryHint: .isDirectory).resolvingSymlinksInPath() + let first = XcodePIFBuilder.PackageReference(sourceRoot: root, package: .init( + location: .local(.init(path: "Library")), traits: ["Z", "A"] + )) + let second = XcodePIFBuilder.PackageReference(sourceRoot: root.appending(path: "Nested"), package: .init( + location: .local(.init(path: "../Library")), traits: ["A", "Z"] + )) + let manifest = try XcodePackagePIFBuilder.manifest(references: [first, second]) + #expect(manifest.components(separatedBy: ".package(").count == 2) + #expect(manifest.contains("path: \(String(reflecting: root.appending(path: "Library").path))")) + #expect(manifest.contains("traits: [\"A\", \"Z\"]")) + let remote = remote(.version("1.2.3")) + #expect(throws: Console.Error.self) { try XcodePackagePIFBuilder.manifest(references: [first, remote]) } + #expect(throws: Console.Error.self) { + try XcodePackagePIFBuilder.manifest(references: [remote, self.remote(.branch("main"))]) + } + #expect(throws: Console.Error.self) { try XcodePackagePIFBuilder.manifest(references: [self.remote(nil)]) } + } + + @Test(.requiresXcode, .dependencies { $0.out.write = { _, _, _ in } }) + func buildsAppWithPackageResourcesAndTransitiveTarget() async throws { + let temporary = try TemporaryDirectory(name: "PackageBuild") + let fixtures = fixtureURL("Projects") + #if os(macOS) + let triple = "arm64-apple-ios-simulator" + #else + let triple = "arm64-apple-ios" + #endif + let description = try await XToolProject.testXcode(fixtures.appending(path: "Packages.xcodeproj")) + .describe(configuration: .debug, triple: triple) + let package = try #require(description.pif.workspace.projects.first { $0.id == "PACKAGE:localpackage" }) + #expect(package.targets.contains { $0.common.name == "Helper" }) + #expect(!package.targets.contains { $0.common.name.contains("Tests") }) + let coordinator = try await BuildCoordinator(description: description) + do { + var request = await coordinator.buildRequest + let guid = try await coordinator.mainProductGUID() + request.configuredTargets = [SWBConfiguredTarget(guid: guid)] + request.parameters.overrides.synthesized?.set(value: "NO", for: "CODE_SIGNING_ALLOWED") + request.parameters.overrides.synthesized?.set(value: "NO", for: "COMPILER_INDEX_STORE_ENABLE") + let derived = temporary.url.appending(path: "DerivedData") + request.parameters.arenaInfo = SWBArenaInfo( + derivedDataPath: derived.path, buildProductsPath: derived.appending(path: "Products").path, + buildIntermediatesPath: derived.appending(path: "Intermediates").path, pchPath: derived.path, + indexRegularBuildProductsPath: nil, indexRegularBuildIntermediatesPath: nil, + indexPCHPath: derived.path, indexDataStoreFolderPath: nil, indexEnableDataStore: false + ) + let manager = await coordinator.sessionManager + let output = URL(filePath: try await manager.session.evaluateMacroAsString( + "TARGET_BUILD_DIR", level: .target(guid), + buildParameters: request.parameters, overrides: nil + )) + let operation = try await manager.createBuildOperation(request: request) + var errors: [String] = [] + for await event in try await operation.start() { + if case .diagnostic(let diagnostic) = event, diagnostic.kind == .error { errors.append(diagnostic.message) } + } + await operation.waitForCompletion() + #expect(operation.state == .succeeded, "Build errors: \(errors)") + #expect(FileManager.default.fileExists(atPath: output.appending(path: "App.app/App").path)) + let resource = output.appending(path: "App.app/LocalPackage_Library.bundle/Message.txt") + expectNoDifference(try String(contentsOf: resource, encoding: .utf8), "Package fixture\n") + await coordinator.close() + } catch { + await coordinator.close() + throw error + } + } + + @Test(.requiresXcode, .dependencies { $0.out.write = { _, _, _ in } }) + func resolvesGitPackageAtRequestedVersion() async throws { + let temporary = try TemporaryDirectory(name: "GitPackage") + let fixtures = fixtureURL("Projects") + let repository = temporary.url.appending(path: "RemoteLibrary") + try FileManager.default.copyItem(at: fixtures.appending(path: "LocalPackage"), to: repository) + for arguments in [ + ["init", "--initial-branch=main"], ["add", "."], + ["-c", "user.name=Package Test", "-c", "user.email=package@example.com", "commit", "-m", "Fixture"], + ["tag", "1.2.3"], + ] { + _ = try await Subprocess.run( + .name("git"), arguments: .init(["-C", repository.path] + arguments), + output: .discarded, error: .discarded + ).checkSuccess() + } + var project = try XcodeProject(url: fixtures.appending(path: "Packages.xcodeproj")) + .withURL(temporary.url.appending(path: "App.xcodeproj")) + project.project.packages = [.init(location: .remote(.init( + repositoryURL: repository.absoluteString, versionConstraint: .version("1.2.3") + )), traits: [])] + let builder = try XcodePIFBuilder(project: project) + let packages = try await XcodePackagePIFBuilder(builder: builder, rootDirectory: URL(filePath: ".")).build() + let package = try #require(packages.first { $0.id == "PACKAGE:remotelibrary" }) + #expect(package.projectDir.contains("/checkouts/RemoteLibrary")) + let product = try #require(package.targets.first { $0.common.name == "Library-product" }) + let workspace = try builder.build(packageProjects: packages) + #expect(workspace.projects[0].targets[0].common.dependencies.contains { $0.targetId == product.id }) + #expect(FileManager.default.fileExists(atPath: URL(filePath: package.projectDir).appending(path: "Package.swift").path)) + } + + private func remote(_ constraint: XCSchema.SwiftPackageVersionConstraint?) -> XcodePIFBuilder.PackageReference { + .init(sourceRoot: URL(filePath: "/tmp"), package: .init(location: .remote(.init( + repositoryURL: "https://example.com/Library.git", versionConstraint: constraint + )), traits: [])) + } +} diff --git a/Tests/XToolTests/XcodeSchemeTests.swift b/Tests/XToolTests/XcodeSchemeTests.swift new file mode 100644 index 00000000..0b8bb4ea --- /dev/null +++ b/Tests/XToolTests/XcodeSchemeTests.swift @@ -0,0 +1,221 @@ +import XCLIUtils +import ArgumentParser +import CustomDump +import DependenciesTestSupport +import Foundation +import Testing +import XcodeProjects +import XcodePIFConversion +import XPIF +import XUtils +@testable import XToolSupport + +struct XcodeSchemeTests { + @Test(arguments: ["Primary", "Secondary"]) + func selectsTargetsAcrossProjects(scheme: String) async throws { + let description = try await describe(scheme: scheme) + let selection = try #require(description.targetSelection) + let projectName = scheme == "Primary" ? "App" : "OtherApp" + let app = try #require(description.pif.workspace.projects.first { $0.name == projectName }?.targets.first) + expectNoDifference(selection.application, app.id) + expectNoDifference(description.configuration, .debug) // The CLI configuration still takes precedence. + if scheme == "Primary" { + expectNoDifference(selection.buildTargets, [app.id]) + #expect(selection.buildImplicitDependencies) + } else { + let framework = try #require(description.pif.workspace.projects.first { $0.name == "Features" }?.targets.first) + expectNoDifference(selection.buildTargets, [framework.id, app.id]) + #expect(!selection.buildImplicitDependencies) + } + let original = try await describe(scheme: nil) + #expect(original.targetSelection == nil) + expectNoDifference(try description.pif.data(), try original.pif.data()) + let targets = description.pif.workspace.projects.flatMap(\.targets) + let umbrella = try #require(targets.first { $0.id == "xtool:UMBRELLA" }) + expectNoDifference(umbrella.common.dependencies.map(\.targetId), + targets.filter { $0.id != umbrella.id }.map(\.id)) + } + + @Test func findsSchemeForStandaloneProject() async throws { + let description = try await describe(scheme: "Primary", container: "App.xcodeproj") + let selection = try #require(description.targetSelection) + let app = try #require(description.pif.workspace.projects.first { $0.name == "App" }?.targets.first) + expectNoDifference(selection.application, app.id) + } + + @Test func selectsPBXTargetUsingOriginalIdentifier() async throws { + let temporary = try TemporaryDirectory(name: "Schemes") + let root = temporary.url.appending(path: "Projects") + try FileManager.default.copyItem(at: projects(), to: root) + let projectURL = root.appending(path: "App.xcodeproj") + try XcodeProject(url: projectURL).withFormat(.pbxproj).write() + try FileManager.default.removeItem(at: projectURL.appending(path: "project.xcproj")) + let description = try await describe(scheme: "Primary", container: "App.xcodeproj", root: root) + let app = try #require(description.pif.workspace.projects.first { $0.name == "App" }?.targets.first) + expectNoDifference(description.targetSelection?.application, app.id) + } + + @Test(arguments: [true, false]) + func roundTripsWrittenScheme(shared: Bool) async throws { + let temporary = try TemporaryDirectory(name: "Schemes") + let root = temporary.url.appending(path: "Projects") + try FileManager.default.copyItem(at: projects(), to: root) + let workspace = root.appending(path: "Schemes.xcworkspace") + let original = try XcodeScheme(named: "Secondary", containers: [workspace]) + let originalSelection = try #require(try await describe(scheme: "Secondary", root: root).targetSelection) + try FileManager.default.removeItem(at: workspace.appending(path: "xcshareddata/xcschemes/Secondary.xcscheme")) + try original.write(in: workspace, shared: shared) + let written = try XcodeScheme(named: "Secondary", containers: [workspace]) + expectNoDifference(written.buildTargets.map(\.projectURL), original.buildTargets.map(\.projectURL)) + expectNoDifference(written.buildTargets.map(\.objectID), original.buildTargets.map(\.objectID)) + expectNoDifference(written.buildTargets.map(\.buildableName), original.buildTargets.map(\.buildableName)) + expectNoDifference(written.runnable?.objectID, original.runnable?.objectID) + expectNoDifference(written.buildImplicitDependencies, original.buildImplicitDependencies) + let selection = try #require(try await describe(scheme: "Secondary", root: root).targetSelection) + expectNoDifference(selection.application, originalSelection.application) + expectNoDifference(selection.buildTargets, originalSelection.buildTargets) + expectNoDifference(selection.buildImplicitDependencies, originalSelection.buildImplicitDependencies) + } + + @Test func rejectsUnknownScheme() async throws { + await #expect(throws: XcodeScheme.SchemeError.self) { try await describe(scheme: "Missing") } + } + + @Test(arguments: [false, true]) + func discoversUserSchemesAndRejectsDuplicateNames(duplicate: Bool) throws { + let temporary = try TemporaryDirectory(name: "Schemes") + let project = temporary.url.appending(path: "App.xcodeproj") + let source = projects().appending(path: "App.xcodeproj/xcshareddata/xcschemes/Primary.xcscheme") + let userSchemes = project.appending(path: "xcuserdata/\(NSUserName()).xcuserdatad/xcschemes") + try FileManager.default.createDirectory(at: userSchemes, withIntermediateDirectories: true) + try FileManager.default.copyItem(at: source, to: userSchemes.appending(path: "Primary.xcscheme")) + if duplicate { + let other = temporary.url.appending(path: "Other.xcodeproj") + let shared = other.appending(path: "xcshareddata/xcschemes") + try FileManager.default.createDirectory(at: shared, withIntermediateDirectories: true) + try FileManager.default.copyItem(at: source, to: shared.appending(path: "Primary.xcscheme")) + #expect(throws: XcodeScheme.SchemeError.self) { + try XcodeScheme(named: "Primary", containers: [project, other]) + } + } else { + let scheme = try XcodeScheme(named: "Primary", containers: [project, project]) + expectNoDifference(scheme.buildTargets.map(\.projectURL.path), [project.resolvingSymlinksInPath().path]) + } + } + + @Test(arguments: [ + ("A50000000000000000000001", "MISSING"), + ("container:OtherApp.xcodeproj", "container:Missing.xcodeproj"), + ]) + func rejectsUnresolvedTargets(original: String, replacement: String) async throws { + let temporary = try TemporaryDirectory(name: "Schemes") + let root = temporary.url.appending(path: "Projects") + try FileManager.default.copyItem(at: projects(), to: root) + let scheme = root.appending(path: "Schemes.xcworkspace/xcshareddata/xcschemes/Secondary.xcscheme") + let xml = try String(contentsOf: scheme, encoding: .utf8) + try xml.replacingOccurrences(of: original, with: replacement).write(to: scheme, atomically: true, encoding: .utf8) + await #expect(throws: XcodePIFConversionError.self) { + try await describe(scheme: "Secondary", root: root) + } + } + + @Test(arguments: [false, true]) + func rejectsEmptyOrAmbiguousApplicationSelection(empty: Bool) async throws { + let temporary = try TemporaryDirectory(name: "Schemes") + let root = temporary.url.appending(path: "Projects") + try FileManager.default.copyItem(at: projects(), to: root) + let scheme = root.appending(path: "Schemes.xcworkspace/xcshareddata/xcschemes/Secondary.xcscheme") + var xml = try String(contentsOf: scheme, encoding: .utf8) + if empty { + xml = xml.replacingOccurrences(of: "buildForRunning=\"YES\"", with: "buildForRunning=\"NO\"") + } else { + let launch = try #require(xml.range(of: " \n" + xml = xml.replacingOccurrences(of: "buildForRunning=\"NO\"", with: "buildForRunning=\"YES\"") + } + try xml.write(to: scheme, atomically: true, encoding: .utf8) + await #expect(throws: Console.Error.self) { try await describe(scheme: "Secondary", root: root) } + } + + @Test(.dependencies { $0.out.write = { _, _, _ in } }) + func usesConfiguredSchemeAndAllowsCLIOverride() async throws { + let temporary = try TemporaryDirectory(name: "Schemes") + let root = temporary.url.appending(path: "Projects") + try FileManager.default.copyItem(at: projects(), to: root) + try Data(""" + version: 1 + orgID: org.example + project: Schemes.xcworkspace + scheme: Secondary + skipLSP: true + """.utf8).write(to: root.appending(path: "xtool.yml")) + let project = try await XToolProject.discover(start: root) + let configured = try await project.describe(configuration: .debug, triple: triple) + let overridden = try await project.describe(configuration: .debug, triple: triple, scheme: "Primary") + let other = try #require(configured.pif.workspace.projects.first { $0.name == "OtherApp" }?.targets.first) + let app = try #require(configured.pif.workspace.projects.first { $0.name == "App" }?.targets.first) + expectNoDifference(configured.targetSelection?.application, other.id) + expectNoDifference(overridden.targetSelection?.application, app.id) + } + + @Test func rejectsRunnableOutsideBuildSelection() async throws { + let temporary = try TemporaryDirectory(name: "Schemes") + let root = temporary.url.appending(path: "Projects") + try FileManager.default.copyItem(at: projects(), to: root) + let scheme = root.appending(path: "Schemes.xcworkspace/xcshareddata/xcschemes/Secondary.xcscheme") + let xml = try String(contentsOf: scheme, encoding: .utf8) + let launch = try #require(xml.range(of: " URL { + fixtureURL("Projects") + } + + private let triple = "arm64-apple-ios-simulator" + + private func describe( + scheme: String?, container: String = "Schemes.xcworkspace", root: URL? = nil + ) async throws -> BuildDescription { + try await XToolProject.testXcode((root ?? projects()).appending(path: container)) + .describe(configuration: .debug, triple: triple, scheme: scheme) + } +} diff --git a/Tests/XToolTests/XcodeSynchronizedFolderTests.swift b/Tests/XToolTests/XcodeSynchronizedFolderTests.swift new file mode 100644 index 00000000..c303477b --- /dev/null +++ b/Tests/XToolTests/XcodeSynchronizedFolderTests.swift @@ -0,0 +1,281 @@ +import Foundation +import XcodeProjects +import XPIF +import Testing +@testable import XcodePIFConversion + +struct XcodeSynchronizedFolderTests { + @Test(arguments: [XcodeProject.Format.pbxproj, .xcproj]) + func discoversFilesAndAppliesMembershipExceptions(format: XcodeProject.Format) throws { + let directory = try temporaryDirectory() + defer { try? FileManager.default.removeItem(at: directory) } + var source = try project(at: directory) + source.format = format + var folder = synchronizedFolder() + folder.explicitFileTypes = ["Custom.txt": .init(rawValue: "sourcecode.swift")] + folder.explicitOpaqueFolders = ["Opaque"] + folder.membershipExceptions = [ + .target(.init(target: .init(targetName: "Probe"), publicHeaders: ["Public.hpp"], privateHeaders: [], + additionalCompilerFlags: ["Nested/Code.swift": "-DTEST"], commonProperties: common( + sense: .exclusions, members: ["Excluded.swift", "Settings.xcconfig"], + filters: ["Nested/Code.swift": [.init(rawValue: "maccatalyst")]] + ))), + .target(.init(target: .init(targetName: "Extra"), publicHeaders: [], privateHeaders: [], + additionalCompilerFlags: [:], commonProperties: common(sense: .inclusions, members: ["Excluded.swift"]))), + .buildPhase(.init(buildPhase: .named(target: .init(targetName: "Probe"), kind: .copy, name: "Copy"), + commonProperties: common(sense: .inclusions, members: ["Data.json"], + tags: format == .xcproj ? ["Data.json": [.init(rawValue: "asset")]] : [:]))), + ] + source.project.topLevelReferences[0] = .folder(folder) + var extra = source.project.targets[0].commonProperties + extra.name = "Extra" + extra.objectID = .init("EXTRA") + extra.specializedConfigurations = [] + extra.product = nil + extra.buildPhases = [.sources(.init(objectID: nil, name: nil))] + source.project.targets.append(.aggregate(extra)) + source.project.configurations[0].file = .init( + anchor: .objectID(.init("SYNC")), relativePath: .init(components: [.child("Settings.xcconfig")]) + ) + try writeFiles([ + "Nested/Code.swift", "Excluded.swift", "Public.hpp", "Custom.txt", "Data.json", "Settings.xcconfig", + "Opaque/Hidden.swift", "Assets.xcassets/Contents.json", "Models.xcdatamodeld/Model.xcdatamodel/contents", + "Support.framework/Support", "lib.a", ".hidden.swift", "App.entitlements", "module.modulemap", + "Demo.playground/Contents.swift", "Backup.swift~", + ], at: directory) + try source.write() + let result = try XcodePIFBuilder(project: XcodeProject(url: source.url)).build().projects[0] + let files = fileReferences(in: result.mainGroup) + let app = result.targets[0].common + #expect(Set(paths(in: app.buildPhases[0], files: files)) == ["Code.swift", "Custom.txt", "Models.xcdatamodeld"]) + #expect(paths(in: app.buildPhases[1], files: files) == ["Public.hpp"]) + #expect(Set(paths(in: app.buildPhases[2], files: files)) == ["Assets.xcassets", "Data.json", "Opaque"]) + #expect(Set(paths(in: app.buildPhases[3], files: files)) == ["Support.framework", "lib.a"]) + #expect(paths(in: app.buildPhases[4], files: files) == ["Data.json"]) + #expect(paths(in: result.targets[1].common.buildPhases[0], files: files) == ["Excluded.swift"]) + #expect(files.values.allSatisfy { !$0.hasSuffix("Hidden.swift") && !$0.hasSuffix("Contents.json") }) + #expect(!files.values.contains(".hidden.swift") && !files.values.contains("Backup.swift~")) + guard case .sources(let sources) = app.buildPhases[0], case .headers(let headers) = app.buildPhases[1], + case .copyFiles(let copy) = app.buildPhases[4] else { + Issue.record("Missing synchronized folder phases") + return + } + let compiled = try #require(sources.files.first(where: { filePath($0, files: files) == "Code.swift" })) + #expect(compiled.additionalCompilerOptions == .string("-DTEST")) + #expect(compiled.platformFilters == [.init(platform: "ios", environment: "maccatalyst")]) + #expect(headers.files.first?.headerVisibility == .public) + #expect(copy.files.first?.assetTags == (format == .xcproj ? ["asset"] : [])) + #expect(result.buildConfigs[0].baseConfigurationFileReference == files.first(where: { $0.value == "Settings.xcconfig" })?.key) + let data = try XPIF.Container(workspace: XcodePIFBuilder(project: source).build()).data() + #expect(try XPIF.Container(data: data).data() == data) + } + + @Test func groupsLocalizedResourcesAndPreservesDiscoveredIdentifiers() throws { + let directory = try temporaryDirectory() + defer { try? FileManager.default.removeItem(at: directory) } + var source = try project(at: directory) + var folderSource = synchronizedFolder() + folderSource.membershipExceptions = [.target(.init( + target: .init(targetName: "Probe"), publicHeaders: [], privateHeaders: [], additionalCompilerFlags: [:], + commonProperties: common(sense: .exclusions, members: ["en.lproj/Main.strings"]) + ))] + source.project.topLevelReferences[0] = .folder(folderSource) + try writeFiles(["Nested/Code.swift", "Base.lproj/Main.storyboard", "en.lproj/Main.strings", "fr.lproj/Main.strings", + "en.lproj/Localizable.strings", "fr.lproj/Localizable.strings"], at: directory) + let result = try XcodePIFBuilder(project: source).build().projects[0] + guard case .group(let folder) = result.mainGroup.subitems[0] else { + Issue.record("Missing expanded folder") + return + } + #expect(result.knownRegions == ["Base", "en", "fr"]) + let variants = folder.subitems.compactMap { reference -> XPIF.Group? in + guard case .group(let group) = reference, group.kind == .variantGroup else { return nil } + return group + } + #expect(Set(variants.map(\.name)) == ["Main.storyboard", "Localizable.strings"]) + let main = try #require(variants.first(where: { $0.name == "Main.storyboard" })) + let regions = main.subitems.compactMap { reference -> String? in + guard case .file(let file) = reference else { return nil } + return file.regionVariantName + } + #expect(Set(regions) == ["Base", "en", "fr"]) + guard case .copyBundleResources(let resources) = result.targets[0].common.buildPhases[2] else { + Issue.record("Missing resources phase") + return + } + #expect(Set(resources.files.map(\.ref)) == Set(variants.map { .reference(id: $0.id) })) + let originalFiles = fileReferences(in: result.mainGroup) + try writeFiles(["AAA.swift"], at: directory) + let updated = try XcodePIFBuilder(project: source).build().projects[0] + let updatedFiles = fileReferences(in: updated.mainGroup) + #expect(originalFiles.allSatisfy { updatedFiles[$0.key] == $0.value }) + } + + @Test func rejectsUnresolvedMembershipsAndDirectoryCycles() throws { + let directory = try temporaryDirectory() + defer { try? FileManager.default.removeItem(at: directory) } + var source = try project(at: directory) + try writeFiles(["Code.swift"], at: directory) + var folder = synchronizedFolder() + folder.targets = [.init(targetName: "Missing")] + source.project.topLevelReferences[0] = .folder(folder) + #expect(throws: XcodePIFConversionError.self) { try XcodePIFBuilder(project: source).build() } + folder.targets = [.init(targetName: "Probe")] + folder.membershipExceptions = [.buildPhase(.init( + buildPhase: .objectID(.init("MISSING")), commonProperties: common(sense: .inclusions, members: []) + ))] + source.project.topLevelReferences[0] = .folder(folder) + #expect(throws: XcodePIFConversionError.self) { try XcodePIFBuilder(project: source).build() } + source.project.topLevelReferences[0] = .folder(synchronizedFolder()) + try FileManager.default.createSymbolicLink( + at: directory.appending(path: "Files/Loop"), withDestinationURL: directory.appending(path: "Files") + ) + #expect(throws: XcodePIFConversionError.self) { try XcodePIFBuilder(project: source).build() } + } + + @Test func discoversUnattachedFoldersRelativeToGroupsAndSourceRoot() throws { + let directory = try temporaryDirectory() + defer { try? FileManager.default.removeItem(at: directory) } + var source = try project(at: directory) + try FileManager.default.createDirectory(at: source.url, withIntermediateDirectories: true) + try source.project.pbxprojRepresentation(name: "Probe", projectDirPath: "SourceRoot") + .write(to: source.url.appending(path: "project.pbxproj")) + source = try XcodeProject(url: source.url) + var folder = synchronizedFolder() + folder.targets = [] + source.project.topLevelReferences[0] = .group(.init( + objectID: nil, name: "Outer", path: "Parent", includeInIndex: nil, children: [.folder(folder)] + )) + let empty = try XcodePIFBuilder(project: source).build().projects[0] + #expect(fileReferences(in: empty.mainGroup).isEmpty) + try writeFiles(["Nested/Settings.xcconfig", "Code.swift"], at: directory.appending(path: "SourceRoot/Parent")) + source.project.configurations[0].file = .init( + anchor: .objectID(.init("SYNC")), relativePath: .init(components: [.child("Nested"), .child("Settings.xcconfig")]) + ) + let result = try XcodePIFBuilder(project: source).build().projects[0] + let files = fileReferences(in: result.mainGroup) + #expect(Set(files.values) == ["Settings.xcconfig", "Code.swift"]) + #expect(result.buildConfigs[0].baseConfigurationFileReference == files.first(where: { $0.value == "Settings.xcconfig" })?.key) + #expect(paths(in: result.targets[0].common.buildPhases[0], files: files).isEmpty) + } + + // Expectations match `xcodebuild -dumpPIF` for the same type and phase layout. + @Test(arguments: [ + ("text.plist.entitlements", "SR", nil), ("text.plist.entitlements", "C", .copy), + ("sourcecode.module-map", "HSFR", nil), ("file.playground", "SR", nil), ("sourcecode.typescript", "SR", nil), + ("net.daringfireball.markdown", "SR", .resources), ("sourcecode.javascript", "SR", .resources), + ("text.apinotes", "SR", nil), ("text.apinotes", "HSFR", .headers), + ("sourcecode.text-based-dylib-definition", "HSFR", .frameworks), ("sourcecode.text-based-dylib-definition", "SR", nil), + ("file.intentdefinition", "SR", .sources), ("file.intentdefinition", "R", .resources), ("archive.ppob", "R", .resources), + ("archive.ppob", "S", nil), ("wrapper.xcdatamodeld", "SR", .sources), ("sourcecode.swift", "R", nil), + ("sourcecode.swift", "RS", .sources), ("sourcecode.swift", "CX", .copy), ("sourcecode.swift", "CCX", nil), + ("sourcecode.swift", "CR", nil), ("image.png", "CR", .resources), ("image.png", "S", nil), + ("wrapper.pb-project", "C", nil), ("sourcecode.swift", "metal:SR", nil), ("sourcecode.metal", "metal:SR", .sources), + ] as [(String, String, XCSchema.BuildPhase.Kind?)]) + func routesImplicitMembersLikeXcode(fileType: String, layout: String, expected: XCSchema.BuildPhase.Kind?) throws { + var target = try project(at: URL(filePath: "/")).project.targets[0].commonProperties + let parts = layout.split(separator: ":") + if parts.count > 1 { target.productTypeID = .init(abbreviatedRepresentation: "metal-library") } + target.buildPhases = parts.last!.map { letter -> XCSchema.BuildPhase in + switch letter { + case "S": .sources(.init(objectID: nil, name: nil)) + case "H": .headers(.init(objectID: nil, name: nil)) + case "F": .frameworks(.init(objectID: nil, name: nil)) + case "R": .resources(.init(objectID: nil, name: nil)) + case "C": .copy(.init(objectID: nil, name: nil, bundleBasePath: .resourcesDir, relativePath: "", scope: .always)) + default: .script(.init( + objectID: nil, name: "Script", shellPath: "/bin/sh", script: "", logEnvironmentVariables: false, + inputPaths: [], inputFileListPaths: [], outputPaths: [], outputFileListPaths: [], dependencyFile: nil, + runOnEveryBuild: false, scope: .always + )) + } + } + #expect(XcodeSynchronizedFolder.implicitPhase(for: fileType, in: target)?.kind == expected) + } + + @Test func detectsFileTypesLikeXcode() { + #expect(XcodeFileType.forFile(named: "x.h") == "sourcecode.c.h") + #expect(XcodeFileType.forFile(named: "x.H") == "sourcecode.cpp.h") + #expect(XcodeFileType.forFile(named: "X.SWIFT") == "sourcecode.swift") + #expect(XcodeFileType.forFile(named: "App.entitlements") == "text.plist.entitlements") + #expect(XcodeFileType.forFile(named: "Makefile") == "sourcecode.make") + #expect(XcodeFileType.forFile(named: "Demo.playground") == nil) + #expect(XcodeFileType.forFile(named: "data.unknown") == nil) + #expect(XcodeFileType.forDirectory(named: "Demo.playground") == "file.playground") + #expect(XcodeFileType.forDirectory(named: "Assets.xcassets") == "folder.assetcatalog") + #expect(XcodeFileType.forDirectory(named: "Sources.swift") == nil) + #expect(XcodeFileType.ancestry(of: "text.plist.strings") == ["text.plist.strings", "text", "file"]) + } + + private func project(at directory: URL) throws -> XcodeProject { + let fixture = fixtureURL("simple-xcode-project.pbxproj") + var source = try XcodeProject(project: .init(pbxprojRepresentation: Data(contentsOf: fixture)), + url: directory.appending(path: "Probe.xcodeproj")) + source.project.topLevelReferences[0] = .folder(synchronizedFolder()) + var target = source.project.targets[0].commonProperties + target.buildPhases = [ + .sources(.init(objectID: nil, name: nil)), .headers(.init(objectID: nil, name: nil)), + .resources(.init(objectID: nil, name: nil)), .frameworks(.init(objectID: nil, name: nil)), + .copy(.init(objectID: nil, name: "Copy", bundleBasePath: .resourcesDir, relativePath: "", scope: .always)), + ] + source.project.targets[0] = .native(target) + return source + } + + private func synchronizedFolder() -> XCSchema.Folder { + .init(objectID: .init("SYNC"), path: "Files", targets: [.init(targetName: "Probe")], membershipExceptions: [], + explicitFileTypes: [:], explicitOpaqueFolders: [], includeInIndex: nil) + } + + private func common( + sense: XCSchema.ExceptionSetSense, members: Set, + filters: [XCSchema.FolderMemberID: Set] = [:], + tags: [XCSchema.FolderMemberID: Set] = [:] + ) -> XCSchema.CommonExceptionSetProperties { + .init(sense: sense, membershipExceptions: members, platformFiltersByFolderMemberID: filters, + attributesByFolderMemberID: [:], assetTagsByFolderMemberID: tags) + } + + private func writeFiles(_ paths: [String], at directory: URL) throws { + for path in paths { + let url = directory.appending(path: "Files/" + path) + try FileManager.default.createDirectory(at: url.deletingLastPathComponent(), withIntermediateDirectories: true) + try Data().write(to: url) + } + } + + private func temporaryDirectory() throws -> URL { + let directory = FileManager.default.temporaryDirectory.appending(path: UUID().uuidString).resolvingSymlinksInPath() + try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true) + return directory + } + + private func fileReferences(in group: XPIF.Group) -> [XPIF.GUID: String] { + var result: [XPIF.GUID: String] = [:] + for reference in group.subitems { + switch reference { + case .file(let file): result[file.id] = file.path + case .group(let child): + result.merge(fileReferences(in: child)) { _, new in new } + if child.kind == .versionGroup { result[child.id] = child.path } + } + } + return result + } + + private func paths(in phase: XPIF.BuildPhase, files: [XPIF.GUID: String]) -> [String] { + switch phase { + case .sources(let phase): phase.files.compactMap { filePath($0, files: files) } + case .headers(let phase): phase.files.compactMap { filePath($0, files: files) } + case .frameworks(let phase): phase.files.compactMap { filePath($0, files: files) } + case .copyBundleResources(let phase): phase.files.compactMap { filePath($0, files: files) } + case .copyFiles(let phase): phase.files.compactMap { filePath($0, files: files) } + default: [] + } + } + + private func filePath(_ file: XPIF.BuildFile, files: [XPIF.GUID: String]) -> String? { + guard case .reference(let id) = file.ref else { return nil } + return files[id] + } +} diff --git a/Tests/XToolTests/XcodeWorkspacePIFBuilderTests.swift b/Tests/XToolTests/XcodeWorkspacePIFBuilderTests.swift new file mode 100644 index 00000000..f0d61485 --- /dev/null +++ b/Tests/XToolTests/XcodeWorkspacePIFBuilderTests.swift @@ -0,0 +1,372 @@ +import Foundation +import CustomDump +import XcodeProjects +import XPIF +import Testing +import SwiftBuild +@testable import XToolSupport +@testable import XcodePIFConversion + +struct XcodeWorkspacePIFBuilderTests { + @Test(arguments: [XcodeProject.Format.pbxproj, .xcproj]) + func resolvesCrossProjectDependenciesAndProducts(format: XcodeProject.Format) throws { + let directory = try temporaryDirectory() + defer { try? FileManager.default.removeItem(at: directory) } + let url = try writeGraph(at: directory, format: format) + let workspace = try XcodePIFBuilder(workspace: XcodeWorkspace(url: url)).build() + #expect(workspace.name == "App") + #expect(workspace.path == url.path) + #expect(workspace.projects.map(\.name) == ["App", "Support"]) + let app = workspace.projects[0].targets[0] + let support = workspace.projects[1].targets[0] + #expect(app.id != support.id) // The fixtures deliberately reuse the original target and product IDs. + var dependency = XPIF.TargetDependency(targetId: support.id, platformFilters: [.init(platform: "ios")]) + dependency.additionalFields["name"] = .string("Support (from Support.xcodeproj)") + #expect(app.common.dependencies == [dependency]) + guard case .frameworks(let link) = app.common.buildPhases[1], + case .copyFiles(let embed) = app.common.buildPhases[2] else { + Issue.record("Expected link and embed phases") + return + } + #expect(link.files.first?.ref == .targetProduct(id: support.id)) + #expect(link.files.first?.shouldLinkWeakly == true) + #expect(embed.files.first?.ref == .targetProduct(id: support.id)) + #expect(embed.files.first?.codeSignOnCopy == true) + let standalone = try XcodePIFBuilder(project: XcodeProject(url: directory.appending(path: "App.xcodeproj"))).build() + #expect(standalone.name == "App") + #expect(standalone.projects.map(\.id) == workspace.projects.map(\.id)) + let explicitlyNamed = try XcodeWorkspace(url: url).withURL(directory.appending(path: "project.xcworkspace")) + #expect(try XcodePIFBuilder(workspace: explicitlyNamed).build().name == "project") + let data = try XPIF.Container(workspace: workspace).data() + #expect(try XPIF.Container(data: data).data() == data) + #expect(try XPIF.Container(workspace: XcodePIFBuilder(workspace: XcodeWorkspace(url: url)).build()).data() == data) + } + + @Test func loadedProjectsCanBeReusedByIndependentBuilders() throws { + let directory = try temporaryDirectory() + defer { try? FileManager.default.removeItem(at: directory) } + let url = try writeGraph(at: directory, format: .pbxproj) + let graph = try XcodePIFBuilder(workspace: XcodeWorkspace(url: url)) + var workspace = try graph.build() + let loaded = try #require(graph.projects.values.first) + let reservedIDs = loaded.usedIDs + var first = XcodePIFProjectBuilder(loaded: loaded) + workspace.projects[0] = try first.buildProject(in: graph.projects) + let firstData = try XPIF.Container(workspace: workspace).data() + var second = XcodePIFProjectBuilder(loaded: loaded) + workspace.projects[0] = try second.buildProject(in: graph.projects) + #expect(try XPIF.Container(workspace: workspace).data() == firstData) + #expect(loaded.usedIDs == reservedIDs) + #expect(first.usedIDs.count > reservedIDs.count) + #expect(second.usedIDs == first.usedIDs) + } + + @Test func loadsSharedReferencesAndCyclesOnce() throws { + let directory = try temporaryDirectory() + defer { try? FileManager.default.removeItem(at: directory) } + let url = try writeGraph(at: directory, format: .xcproj) + let supportURL = directory.appending(path: "Libraries/Support.xcodeproj") + var support = try XcodeProject(url: supportURL) + support.project.topLevelReferences.append(.fileReference(file(path: "../App.xcodeproj"))) + try support.write() + var workspace = try XcodeWorkspace(url: url) + workspace.children.append(.file(.init(base: .group, path: "Libraries/../Libraries/Support.xcodeproj"))) + let alias = directory.appending(path: "Alias.xcodeproj") + try FileManager.default.createSymbolicLink(at: alias, withDestinationURL: supportURL) + workspace.children.append(.file(.init(base: .absolute, path: alias.path))) + let result = try XcodePIFBuilder(workspace: workspace).build() + #expect(result.projects.count == 2) + let embedded = try XcodeWorkspace(url: directory.appending(path: "App.xcodeproj/project.xcworkspace")) + #expect(try XcodePIFBuilder(workspace: embedded).build().projects.count == 2) + } + + @Test func rejectsMissingProjectsAndStaleForeignIDs() throws { + let directory = try temporaryDirectory() + defer { try? FileManager.default.removeItem(at: directory) } + let url = try writeGraph(at: directory, format: .xcproj) + let appURL = directory.appending(path: "App.xcodeproj") + var app = try XcodeProject(url: appURL) + app.project.importedProducts[0].productID = .init("MISSING_PRODUCT") + #expect(throws: XcodePIFConversionError.self) { try XcodePIFBuilder(project: app).build() } + app = try XcodeProject(url: appURL) + var target = app.project.targets[0].commonProperties + target.dependencies = [.remoteTarget(.init( + project: remoteReference, target: "Support", targetID: .init("MISSING_TARGET") + ), [])] + app.project.targets[0] = .native(target) + #expect(throws: XcodePIFConversionError.self) { try XcodePIFBuilder(project: app).build() } + try FileManager.default.removeItem(at: directory.appending(path: "Libraries/Support.xcodeproj")) + #expect(throws: (any Error).self) { try XcodePIFBuilder(workspace: XcodeWorkspace(url: url)).build() } + } + + @Test(arguments: [XcodeProject.Format.pbxproj, .xcproj]) + func resolvesProjectPathsFromItsRequiredURL(format: XcodeProject.Format) throws { + let directory = try temporaryDirectory() + defer { try? FileManager.default.removeItem(at: directory) } + _ = try writeGraph(at: directory, format: format) + let app = try XcodeProject(url: directory.appending(path: "App.xcodeproj")) + expectNoDifference(app.sourceRoot.path, directory.path) + let result = try XcodePIFBuilder(project: app).build() + #expect(result.projects[0].projectDir == app.url.deletingLastPathComponent().path) + #expect(result.projects[1].projectDir == directory.appending(path: "Libraries").path) + #expect(result.projects.count == 2) + #expect(app.resolve("/Libraries/Support.xcodeproj", relativeTo: nil)?.path + == directory.appending(path: "Libraries/Support.xcodeproj").path) + let copyURL = directory.appending(path: "Exports/App.xcodeproj") + try app.withURL(copyURL).write() + let copy = try XcodeProject(url: copyURL) + expectNoDifference(copy.sourceRoot.path, copyURL.deletingLastPathComponent().path) + #expect(copy.resolve("/App.swift", relativeTo: nil)?.path + == copyURL.deletingLastPathComponent().appending(path: "App.swift").path) + } + + @Test(arguments: ["Sources", "./Sources", "Nested/../Sources"]) + func loadsAndPreservesDistinctSourceRoot(projectDirPath: String) throws { + let directory = try temporaryDirectory() + defer { try? FileManager.default.removeItem(at: directory) } + _ = try writeGraph(at: directory, format: .pbxproj) + let projectURL = directory.appending(path: "App.xcodeproj") + let pbxprojURL = projectURL.appending(path: "project.pbxproj") + let contents = try String(contentsOf: pbxprojURL, encoding: .utf8) + .replacingOccurrences(of: "projectDirPath = \"\";", with: "projectDirPath = \"\(projectDirPath)\";") + try contents.write(to: pbxprojURL, atomically: true, encoding: .utf8) + let root = directory.appending(path: "Sources") + try FileManager.default.createDirectory(at: root, withIntermediateDirectories: true) + try FileManager.default.moveItem(at: directory.appending(path: "Libraries"), to: root.appending(path: "Libraries")) + let app = try XcodeProject(url: projectURL) + #expect(app.sourceRoot.path == root.path) + let result = try XcodePIFBuilder(project: app).build() + #expect(result.projects[0].projectDir == root.path) + #expect(result.projects[1].projectDir == root.appending(path: "Libraries").path) + try app.write() + #expect(try XcodeProject(url: projectURL).sourceRoot.path == root.path) + expectNoDifference(try savedProjectDirPath(at: projectURL), projectDirPath) + let copyURL = directory.appending(path: "Exports/Copy.xcodeproj") + let copy = app.withURL(copyURL) + try copy.write() + #expect(app.url.path == projectURL.path) + let copyRoot = copyURL.deletingLastPathComponent().appending(path: "Sources") + expectNoDifference(copy.sourceRoot.path, copyRoot.path) + expectNoDifference(try XcodeProject(url: copyURL).sourceRoot.path, copyRoot.path) + expectNoDifference(try savedProjectDirPath(at: copyURL), projectDirPath) + let workspaceURL = copyURL.appending(path: "project.xcworkspace") + let workspace = try XcodeWorkspace(url: workspaceURL) + let exportedURL = directory.appending(path: "Exported.xcworkspace") + try workspace.withURL(exportedURL).write() + #expect(workspace.url.path == workspaceURL.path) + #expect(try XcodeWorkspace(url: exportedURL).children == workspace.children) + // Saving and then moving the checkout must keep relative source roots portable. + let destination = try temporaryDirectory() + defer { try? FileManager.default.removeItem(at: destination) } + let checkout = destination.appending(path: "Checkout") + try FileManager.default.moveItem(at: directory, to: checkout) + let relocated = try XcodeProject(url: checkout.appending(path: "App.xcodeproj")) + expectNoDifference(relocated.sourceRoot.path, checkout.appending(path: "Sources").path) + } + + private func savedProjectDirPath(at url: URL) throws -> String { + let data = try Data(contentsOf: url.appending(path: "project.pbxproj")) + let plist = try #require(PropertyListSerialization.propertyList(from: data, format: nil) as? [String: Any]) + let rootObject = try #require(plist["rootObject"] as? String) + let objects = try #require(plist["objects"] as? [String: [String: Any]]) + return try #require(objects[rootObject]?["projectDirPath"] as? String) + } + + @Test(.requiresXcode, arguments: [false, true], [false, true]) + func buildsWorkspaceWithLinkedAndEmbeddedFramework( + implicitDependency: Bool, + synchronizedFolders: Bool + ) async throws { + let directory = try temporaryDirectory() + defer { try? FileManager.default.removeItem(at: directory) } + let url = try writeGraph(at: directory, format: .pbxproj) + if synchronizedFolders { + try synchronizeSources(in: directory.appending(path: "App.xcodeproj")) + try synchronizeSources(in: directory.appending(path: "Libraries/Support.xcodeproj")) + } + if implicitDependency { + let appURL = directory.appending(path: "App.xcodeproj") + var app = try XcodeProject(url: appURL) + var target = app.project.targets[0].commonProperties + target.dependencies = [] + app.project.targets[0] = .native(target) + try app.write() + } + #if os(macOS) + let triple = "arm64-apple-ios-simulator" + #else + let triple = "arm64-apple-ios" + #endif + let description = try await XToolProject.testXcode(url).describe(configuration: .debug, triple: triple) + let coordinator = try await BuildCoordinator(description: description) + do { + let guid = try await coordinator.mainProductGUID() + let details = try await coordinator.mainProductDetails() + #expect(details.output.lastPathComponent == "CustomApp.app") + var request = await coordinator.buildRequest + request.configuredTargets = [ + SWBConfiguredTarget(guid: guid), + ] + request.parameters.overrides.synthesized?.set(value: "NO", for: "CODE_SIGNING_ALLOWED") + request.parameters.overrides.synthesized?.set(value: "NO", for: "COMPILER_INDEX_STORE_ENABLE") + let derived = directory.appending(path: "DerivedData") + request.parameters.arenaInfo = SWBArenaInfo( + derivedDataPath: derived.path, buildProductsPath: derived.appending(path: "Products").path, + buildIntermediatesPath: derived.appending(path: "Intermediates").path, pchPath: derived.path, + indexRegularBuildProductsPath: nil, indexRegularBuildIntermediatesPath: nil, + indexPCHPath: derived.path, indexDataStoreFolderPath: nil, indexEnableDataStore: false + ) + let manager = await coordinator.sessionManager + let output = URL(filePath: try await manager.session.evaluateMacroAsString( + "TARGET_BUILD_DIR", level: .target(guid), + buildParameters: request.parameters, overrides: nil + )) + let operation = try await manager.createBuildOperation(request: request) + var errors: [String] = [] + for await event in try await operation.start() { + if case .diagnostic(let diagnostic) = event, diagnostic.kind == .error { + errors.append(diagnostic.message) + } + } + await operation.waitForCompletion() + #expect(operation.state == .succeeded, "Build errors: \(errors)") + #expect(FileManager.default.fileExists(atPath: output.appending(path: "CustomApp.app/CustomApp").path)) + let embeddedBinary = output.appending(path: "CustomApp.app/Frameworks/Support.framework/Support") + #expect(FileManager.default.fileExists(atPath: embeddedBinary.path)) + if synchronizedFolders { + #expect(FileManager.default.fileExists(atPath: output.appending(path: "CustomApp.app/Resource.txt").path)) + #expect(FileManager.default.fileExists(atPath: output + .appending(path: "CustomApp.app/fr.lproj/Localizable.strings").path)) + #expect(FileManager.default.fileExists(atPath: embeddedBinary.deletingLastPathComponent() + .appending(path: "Resource.txt").path)) + } + await coordinator.close() + } catch { + await coordinator.close() + throw error + } + } + + private var remoteReference: XCSchema.GroupTreeReference { + .namePath(.init(components: [.child("Projects"), .child("Support.xcodeproj")])) + } + + private func synchronizeSources(in url: URL) throws { + var project = try XcodeProject(url: url) + var target = project.project.targets[0].commonProperties + target.buildPhases.append(.resources(.init(objectID: nil, name: nil))) + project.project.targets[0] = .native(target) + project.project.topLevelReferences[0] = .folder(.init( + objectID: nil, path: "Sources", targets: [.init(targetName: target.name)], membershipExceptions: [ + .target(.init(target: .init(targetName: target.name), publicHeaders: [], privateHeaders: [], + additionalCompilerFlags: [:], commonProperties: .init( + sense: .exclusions, membershipExceptions: ["Excluded.swift"], + platformFiltersByFolderMemberID: [:], attributesByFolderMemberID: [:], assetTagsByFolderMemberID: [:] + ))), + ], explicitFileTypes: [:], explicitOpaqueFolders: [], includeInIndex: nil + )) + let directory = url.deletingLastPathComponent() + try FileManager.default.createDirectory(at: directory.appending(path: "Sources/Nested"), withIntermediateDirectories: true) + try FileManager.default.moveItem( + at: directory.appending(path: "App.swift"), to: directory.appending(path: "Sources/Nested/App.swift") + ) + try Data("resource".utf8).write(to: directory.appending(path: "Sources/Resource.txt")) + for language in ["en", "fr"] { + let localized = directory.appending(path: "Sources/\(language).lproj") + try FileManager.default.createDirectory(at: localized, withIntermediateDirectories: true) + let encoding = language == "fr" ? String.Encoding.utf16 : .utf8 + try #require("\"key\" = \"value\";".data(using: encoding)).write(to: localized.appending(path: "Localizable.strings")) + } + try Data("#error(\"Excluded source was compiled\")".utf8).write(to: directory.appending(path: "Sources/Excluded.swift")) + try project.write() + } + + /// Creates a real PBX/xcproj graph, including proxy products, in a location usable by CLI smoke tests. + @discardableResult + func writeGraph(at directory: URL, format: XcodeProject.Format) throws -> URL { + let fixture = fixtureURL("simple-xcode-project.pbxproj") + var app = try XcodeProject(project: .init(pbxprojRepresentation: Data(contentsOf: fixture)), + url: directory.appending(path: "App.xcodeproj"), format: format) + var support = app + support.url = directory.appending(path: "Libraries/Support.xcodeproj") + var library = support.project.targets[0].commonProperties + library.name = "Support" + library.productTypeID = .init(rawValue: "com.apple.product-type.framework") + library.buildSettings = [ + "PRODUCT_NAME": .string("Support"), "DEFINES_MODULE": .string("YES"), "SKIP_INSTALL": .string("YES"), + "PRODUCT_BUNDLE_IDENTIFIER": .string("com.example.Support"), "GENERATE_INFOPLIST_FILE": .string("YES"), + "SWIFT_VERSION": .string("6.0"), "CODE_SIGNING_ALLOWED": .string("NO"), + ] + library.buildPhases = [.sources(.init(objectID: nil, name: nil))] + support.project.targets = [.native(library)] + support.project.productsGroup = nil + support.project.topLevelReferences = [ + .fileReference(file(path: "App.swift", buildFiles: [membership(target: "Support", phase: .sources)])), + .group(.init(objectID: nil, name: "Products", path: "", includeInIndex: nil, children: [ + .fileReference(file(path: "/Support.framework", id: "670DC86C0DB18949CDB87DC1")) + ])), + ] + library.product = .namePath(.init(components: [.child("Products"), .child("Support.framework")])) + support.project.targets = [.native(library)] + var target = app.project.targets[0].commonProperties + target.dependencies = [.remoteTarget(.init( + project: remoteReference, target: "OutdatedName", targetID: library.objectID + ), [.init(rawValue: "ios")])] + target.buildPhases += [.frameworks(.init(objectID: nil, name: nil)), .copy(.init( + objectID: nil, name: "Embed", bundleBasePath: .frameworksDir, relativePath: "", scope: .always + ))] + app.project.targets = [.native(target)] + app.project.topLevelReferences.append(.group(.init( + objectID: nil, name: "Projects", path: "Libraries", includeInIndex: nil, + children: [.fileReference(file(path: "Support.xcodeproj"))] + ))) + app.project.importedProducts = [.init( + project: remoteReference, target: "OutdatedName", productID: .init("670DC86C0DB18949CDB87DC1"), + path: "Support.framework", fileType: .init(rawValue: "wrapper.framework"), buildFiles: [ + membership(target: "Probe", phase: .frameworks, weak: true), + membership(target: "Probe", phase: .copy, name: "Embed", sign: true), + ] + )] + try app.write() + try support.write() + let source = """ + import UIKit + import Support + @main final class App: UIResponder, UIApplicationDelegate { let value = answer() } + """ + try Data(source.utf8).write(to: directory.appending(path: "App.swift")) + try Data("public func answer() -> Int { 42 }\n".utf8).write(to: directory.appending(path: "Libraries/App.swift")) + let workspaceURL = directory.appending(path: "App.xcworkspace") + try XcodeWorkspace(children: [ + .file(.init(base: .group, path: "App.xcodeproj")), + .group(.init(location: .init(base: .container, path: "Libraries"), name: "Libraries", children: [ + .file(.init(base: .group, path: "Support.xcodeproj")), + ])), + ], url: workspaceURL).write() + return workspaceURL + } + + private func membership( + target: String, phase: XCSchema.BuildPhase.Kind, name: String? = nil, weak: Bool = false, sign: Bool = false + ) -> XCSchema.ProjectBuildFile { + .init(objectID: nil, buildPhase: .named(target: .init(targetName: target), kind: phase, name: name), properties: .init( + platformFilters: [], additionalBuildFlags: nil, assetTags: [], attributes: .init( + headerRole: nil, machInterfaceGeneration: nil, isWeak: weak, codeSignOnCopy: sign, + codeGeneration: .default, headerPreservation: .keep, decompress: false, codeGenerationVisibility: nil + ) + )) + } + + private func file( + path: XCSchema.FilePath, id: String? = nil, buildFiles: [XCSchema.ProjectBuildFile] = [] + ) -> XCSchema.FileReference { + .init(objectID: id.map { XCSchema.ObjectID($0) }, path: path, explicitFileType: nil, expectedSignature: nil, + textEncoding: nil, lineEnding: nil, includeInIndex: nil, buildFiles: buildFiles) + } + + private func temporaryDirectory() throws -> URL { + let directory = FileManager.default.temporaryDirectory.appending(path: UUID().uuidString).resolvingSymlinksInPath() + try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true) + return directory + } +} diff --git a/xtool.yml b/xtool.yml new file mode 100644 index 00000000..b801875b --- /dev/null +++ b/xtool.yml @@ -0,0 +1,2 @@ +version: 1 +project: macOS/XToolMac.xcodeproj From cf2fa0643e6778198eacc2ba09e997a04da03876 Mon Sep 17 00:00:00 2001 From: Kabir Oberai Date: Tue, 3 Nov 2026 05:29:31 -0500 Subject: [PATCH 09/15] io: add FileSystemMonitoring --- Package.swift | 13 + .../include/inotify_shims.h | 21 ++ Sources/CFileSystemMonitoring/stub.c | 0 .../FileSystemMonitor+Linux.swift | 296 ++++++++++++++++++ .../FileSystemMonitor+Mac.swift | 104 ++++++ .../FileSystemMonitor.swift | 47 +++ 6 files changed, 481 insertions(+) create mode 100644 Sources/CFileSystemMonitoring/include/inotify_shims.h create mode 100644 Sources/CFileSystemMonitoring/stub.c create mode 100644 Sources/FileSystemMonitoring/FileSystemMonitor+Linux.swift create mode 100644 Sources/FileSystemMonitoring/FileSystemMonitor+Mac.swift create mode 100644 Sources/FileSystemMonitoring/FileSystemMonitor.swift diff --git a/Package.swift b/Package.swift index 8c972320..3f7890c5 100644 --- a/Package.swift +++ b/Package.swift @@ -211,12 +211,25 @@ let package = Package( ], swiftSettings: swiftSettings, ), + .target( + name: "CFileSystemMonitoring", + dependencies: [] + ), + .target( + name: "FileSystemMonitoring", + dependencies: [ + "CFileSystemMonitoring", + "XUtils", + .product(name: "Dependencies", package: "swift-dependencies"), + ] + ), .target( name: "XToolSupport", dependencies: [ "XPIF", "XcodePIFConversion", "XcodeProjects", + "FileSystemMonitoring", "SwiftyMobileDevice", "XKit", "XCLIUtils", diff --git a/Sources/CFileSystemMonitoring/include/inotify_shims.h b/Sources/CFileSystemMonitoring/include/inotify_shims.h new file mode 100644 index 00000000..6e576400 --- /dev/null +++ b/Sources/CFileSystemMonitoring/include/inotify_shims.h @@ -0,0 +1,21 @@ +#ifndef CINOTIFY_SHIMS_H +#define CINOTIFY_SHIMS_H + +#ifdef __linux__ + +#include +#include +#include + +static inline const char *cin_event_name(const struct inotify_event *event) { + if (event->len) + return event->name; + else + return NULL; +} + +static const uint32_t cin_all_events = IN_ALL_EVENTS; + +#endif + +#endif /* CINOTIFY_SHIMS_H */ diff --git a/Sources/CFileSystemMonitoring/stub.c b/Sources/CFileSystemMonitoring/stub.c new file mode 100644 index 00000000..e69de29b diff --git a/Sources/FileSystemMonitoring/FileSystemMonitor+Linux.swift b/Sources/FileSystemMonitoring/FileSystemMonitor+Linux.swift new file mode 100644 index 00000000..199fcb39 --- /dev/null +++ b/Sources/FileSystemMonitoring/FileSystemMonitor+Linux.swift @@ -0,0 +1,296 @@ +// swiftlint:disable all + +#if os(Linux) + +// https://github.com/sersoft-gmbh/swift-inotify + +import Foundation +import CFileSystemMonitoring +import XUtils + +extension FileSystemMonitor { + public static let liveValue = FileSystemMonitor { directory in + let notifier = try Inotifier() + let events = try await notifier.events(for: directory) + .compactMap { $0.path.map { FileSystemChangeEvent(file: $0) } } + .eraseToStream() + return FileSystemEvents { + _ = notifier + return events.makeAsyncIterator() + } + } +} + +/// The notifier object. +final actor Inotifier { + /// An asynchronous sequence of events for a certain file path. + struct PathEvents: AsyncSequence, Sendable { + @usableFromInline + let stream: AsyncStream + + @usableFromInline + init(stream: AsyncStream) { + self.stream = stream + } + + @inlinable + func makeAsyncIterator() -> AsyncStream.AsyncIterator { + stream.makeAsyncIterator() + } + } + + private let fileDescriptor: FileDescriptor + private var streamTask: Task? + private var watches = Dictionary.Continuation>>() + + /// Creates a new instance. + init() throws { + guard case let fd = inotify_init1(0), fd != -1 else { throw Errno(rawValue: errno) } + fileDescriptor = .init(rawValue: fd) + } + + deinit { + streamTask?.cancel() + streamTask = nil + try? fileDescriptor.close() + } + + /// Closes this inotify instance. All further calls to this instance will fail. + func close() throws { + stopStreaming() + try fileDescriptor.close() + } + + /// Returns the asynchronous events sequence for the given file path. + /// - Parameters: + /// - filePath: The file path to watch. + /// - Returns: The asynchronous sequence of events for the given file path. + func events(for filePath: FilePath) throws -> PathEvents { + let wd = filePath.withCString { + inotify_add_watch(fileDescriptor.rawValue, $0, cin_all_events) + } + guard wd != -1 else { throw Errno(rawValue: errno) } + if streamTask == nil { + startStreaming() + } + let stream = AsyncStream { continuation in + let sequenceID = UUID() + watches[wd, default: [:]][sequenceID] = continuation + continuation.onTermination = { [weak self] _ in + Task { [weak self] in + try? await self?.removeWatch(forDescriptor: wd, sequenceID: sequenceID) + } + } + } + return PathEvents(stream: stream) + } + + private func startStreaming(restart: Bool = false) { + assert(restart || streamTask == nil) + if restart { + streamTask?.cancel() + } + streamTask = Task { @concurrent [fileDescriptor, weak self] in + do { + for try await event in FileStream(fileDescriptor: fileDescriptor) { + guard !Task.isCancelled, let self else { return } + await self.handle(event) + } + } catch where Task.isCancelled { + } catch { + // restart + await self?.startStreaming(restart: true) + } + } + } + + private func handle(_ cEvent: inotify_event) { + guard var watchesToNotify = watches[cEvent.wd] else { return } + defer { + if watchesToNotify.isEmpty { + watches.removeValue(forKey: cEvent.wd) + } else { + watches[cEvent.wd] = watchesToNotify + } + } + // FIXME: Deal with connected events using `event.cookie`. + let event = InotifyEvent(cEvent: cEvent) + for (watchID, continuation) in watchesToNotify { + if case .terminated = continuation.yield(event) { + watchesToNotify.removeValue(forKey: watchID) + } + } + } + + private func stopStreaming() { + streamTask?.cancel() + streamTask = nil + } + + private func removeWatch(forDescriptor wd: CInt, sequenceID: UUID) throws { + let status = inotify_rm_watch(fileDescriptor.rawValue, wd) + guard status != -1 else { throw Errno(rawValue: errno) } + guard var watchSequences = watches[wd] else { return } + watchSequences.removeValue(forKey: sequenceID) + guard watchSequences.isEmpty else { return } + watches.removeValue(forKey: wd) + guard watches.isEmpty else { return } + stopStreaming() + } +} + +/// An event sent by inotify. +struct InotifyEvent: Equatable, Sendable { + /// The file path of the event. If nil, the event is not for a file inside of the watch. + let path: FilePath? + /// The flags of the event. + let flags: Flags + + init(cEvent event: inotify_event) { + path = withUnsafePointer(to: event) { + cin_event_name($0).map { FilePath(platformString: $0) } + } + flags = .init(rawValue: event.mask) + } +} + +extension InotifyEvent { + /// A set of flags that can be set on an event. + struct Flags: OptionSet, Hashable, Sendable { + typealias RawValue = UInt32 + + let rawValue: RawValue + + init(rawValue: RawValue) { + self.rawValue = rawValue + } + } +} + +extension InotifyEvent.Flags { + /// File was accessed. + static let accessed = InotifyEvent.Flags(rawValue: numericCast(IN_ACCESS)) + /// File was modified. + static let modified = InotifyEvent.Flags(rawValue: numericCast(IN_MODIFY)) + /// Metadata changed. + static let attributesChanged = InotifyEvent.Flags(rawValue: numericCast(IN_ATTRIB)) + /// A writeable file was closed. + static let writableFileClosed = InotifyEvent.Flags(rawValue: numericCast(IN_CLOSE_WRITE)) + /// A non-writable file was closed. + static let nonWritableFileClosed = InotifyEvent.Flags(rawValue: numericCast(IN_CLOSE_NOWRITE)) + /// File was opened. + static let opened = InotifyEvent.Flags(rawValue: numericCast(IN_OPEN)) + /// File was moved from X. + static let movedFrom = InotifyEvent.Flags(rawValue: numericCast(IN_MOVED_FROM)) + /// File was moved to Y. + static let movedTo = InotifyEvent.Flags(rawValue: numericCast(IN_MOVED_TO)) + /// File was created inside the watched path. + static let fileCreated = InotifyEvent.Flags(rawValue: numericCast(IN_CREATE)) + /// File was deleted inside the watched path. + static let fileDeleted = InotifyEvent.Flags(rawValue: numericCast(IN_DELETE)) + /// The watched path was deleted. + static let selfDeleted = InotifyEvent.Flags(rawValue: numericCast(IN_DELETE_SELF)) + /// The watched path was moved. + static let selfMoved = InotifyEvent.Flags(rawValue: numericCast(IN_MOVE_SELF)) + + /// Event occurred against a directory. + static let isDirectory = InotifyEvent.Flags(rawValue: numericCast(IN_ISDIR)) +} + +/// An async sequence that continously streams the generic `Element` type from a given file. +/// The `Failure` type describes the errors thrown for the sequence. The ``FailureBehavior`` is used to handle errors. +struct FileStream: AsyncSequence { + @usableFromInline + let _stream: AsyncThrowingStream + + /// Creates a new file stream for the given `fileDescriptor`. + /// The `failureBehavior` defines how errors are handled. + /// - Parameters: + /// - fileDescriptor: The file descriptor to stream from. + /// - failureBehavior: How to handle failures of the underlying stream. + init(fileDescriptor: FileDescriptor) { + _stream = .init(Element.self, { Self._gcdImplementation(for: fileDescriptor, using: $0) }) + } + + @inlinable + func makeAsyncIterator() -> AsyncThrowingStream.AsyncIterator { + _stream.makeAsyncIterator() + } +} + +extension FileStream: Sendable where Element: Sendable {} + +extension FileStream { + private static func _gcdImplementation(for fileDescriptor: FileDescriptor, + using cont: AsyncThrowingStream.Continuation) { + let source = _inactiveSource(from: fileDescriptor) { + cont.yield($0) + } onFailure: { + cont.finish(throwing: $0) + } + cont.onTermination = { _ in source.cancel() } + source.activate() + } +} + +fileprivate struct SendableDispatchSource: @unchecked Sendable { + let source: any DispatchSourceRead + + func activate() { + source.activate() + } + + func cancel() { + source.cancel() + } +} + +extension FileStream { + private static func _inactiveSource( + from fileDesc: FileDescriptor, + onElement elementCallback: @escaping @Sendable (sending Element) -> (), + onFailure failureCallback: @escaping @Sendable (any Error) -> () + ) -> SendableDispatchSource { + let unsafeCallback = unsafeBitCast(elementCallback, to: (@Sendable (Element) -> ()).self) + @Sendable + func send(_ value: Element) { + unsafeCallback(value) + } + + let workerQueue = DispatchQueue(label: "de.sersoft.filestreamer.filestream.gcd.worker") + let source = DispatchSource.makeReadSource(fileDescriptor: fileDesc.rawValue, queue: workerQueue) + let rawSize = MemoryLayout.size + let rawSize64 = UInt64(rawSize) + var remainingData: UInt64 = 0 + source.setEventHandler { + do { + remainingData += .init(source.data) + guard case let capacity = remainingData / rawSize64, capacity > 0 else { return } + let buffer = UnsafeMutableBufferPointer.allocate(capacity: .init(capacity)) + defer { buffer.deallocate() } + let bytesRead = try fileDesc.read(into: UnsafeMutableRawBufferPointer(buffer)) + if case let noOfValues = bytesRead / rawSize, noOfValues > 0 { + for value in buffer.prefix(noOfValues) { + send(value) + } + } + let leftOverBytes = bytesRead % rawSize + remainingData -= .init(bytesRead - leftOverBytes) + if leftOverBytes > 0 { + do { + try fileDesc.seek(offset: .init(-leftOverBytes), from: .current) + } catch { + // If we failed to seek, we need to drop the left-over bytes. + remainingData -= .init(leftOverBytes) + throw error // Re-throw to land it in the failureCallback below + } + } + } catch { + failureCallback(error) + } + } + return .init(source: source) + } +} + +#endif diff --git a/Sources/FileSystemMonitoring/FileSystemMonitor+Mac.swift b/Sources/FileSystemMonitoring/FileSystemMonitor+Mac.swift new file mode 100644 index 00000000..ef7d426b --- /dev/null +++ b/Sources/FileSystemMonitoring/FileSystemMonitor+Mac.swift @@ -0,0 +1,104 @@ +// swiftlint:disable all + +#if os(macOS) + +// https://github.com/jgvanwyk/SwiftFileSystemEvents + +import Foundation +import CoreServices.FSEvents +import Dependencies +import XUtils + +extension FileSystemMonitor: DependencyKey { + public static let liveValue = FileSystemMonitor { directory in + let (events, cont) = AsyncStream.makeStream() + let stream = FileSystemEventStream(directoriesToWatch: [directory]) { + cont.yield(.init(file: $0)) + } + let queue = DispatchQueue(label: "fsevents-queue") + stream.setDispatchQueue(queue) + try stream.start() + cont.onTermination = { _ in + stream.invalidate() + } + let onDeinit = OnDeinit { cont.finish() } + return FileSystemEvents { + _ = onDeinit + return events.makeAsyncIterator() + } + } +} + +private final class OnDeinit: Sendable { + let perform: @Sendable () -> Void + init(perform: @Sendable @escaping () -> Void) { + self.perform = perform + } + deinit { perform() } +} + +// MARK: FileSystemEventStream + +/// Register for a stream of notifications of file system events in a list of directories. +final class FileSystemEventStream: @unchecked Sendable { + + private let streamRef: FSEventStreamRef + private let handlerRef: AnyObject + + init( + directoriesToWatch: [FilePath], + handler: @escaping @Sendable (FilePath) -> Void + ) { + self.handlerRef = handler as AnyObject + var context = FSEventStreamContext( + version: 0, + info: Unmanaged.passUnretained(handlerRef).toOpaque(), + retain: nil, + release: nil, + copyDescription: nil + ) + self.streamRef = FSEventStreamCreate( + kCFAllocatorDefault, + Self.callback, + &context, + directoriesToWatch.map { $0.string } as CFArray, + FSEventStreamEventId(kFSEventStreamEventIdSinceNow), + 0, + FSEventStreamCreateFlags(kFSEventStreamCreateFlagFileEvents), + )! + } + + deinit { + FSEventStreamRelease(streamRef) + } + + private static let callback: FSEventStreamCallback = { _, info, numEvents, eventPaths, _, _ in + guard let info = info else { return } + let eventPaths = eventPaths.assumingMemoryBound(to: UnsafePointer.self) + // note: the cast must include @Sendable to match the stored closure's type + guard let handler = Unmanaged.fromOpaque(info).takeUnretainedValue() + as? @Sendable (FilePath) -> Void else { return } + for index in 0.. FileSystemEvents + + public init( + watch: @Sendable @escaping (_ directory: FilePath) async throws -> FileSystemEvents, + ) { + self.watch = watch + } +} + +public struct FileSystemChangeEvent: Sendable, Hashable { + public var file: FilePath + + public init(file: FilePath) { + self.file = file + } +} + +extension FileSystemMonitor: TestDependencyKey { + public static let testValue = FileSystemMonitor( + watch: unimplemented(), + ) +} + +extension DependencyValues { + public var fileSystemMonitor: FileSystemMonitor { + get { self[FileSystemMonitor.self] } + set { self[FileSystemMonitor.self] = newValue } + } +} + +public struct FileSystemEvents: AsyncSequence, Sendable { + private let makeIterator: @Sendable () -> AsyncStream.AsyncIterator + + public init( + makeAsyncIterator: @Sendable @escaping () -> AsyncStream.AsyncIterator + ) { + self.makeIterator = makeAsyncIterator + } + + public func makeAsyncIterator() -> AsyncStream.AsyncIterator { + makeIterator() + } +} From 716a1a921fa08af8fa212524cb87d7b1584f50e5 Mon Sep 17 00:00:00 2001 From: Kabir Oberai Date: Tue, 3 Nov 2026 05:30:13 -0500 Subject: [PATCH 10/15] swb: hot reloading --- Sources/XToolSupport/BuildCoordinator.swift | 143 ++++++++++++- .../XToolSupport/BuildSessionManager.swift | 27 ++- Sources/XToolSupport/DevCommand.swift | 99 +++++++-- Sources/XToolSupport/EmulatedTools+Live.swift | 4 +- .../EmulatedTools+SwiftCompiler.swift | 139 +++++++++++++ Sources/XToolSupport/HotReloader.swift | 190 ++++++++++++++++++ skills/xtool/SKILL.md | 49 +++++ 7 files changed, 627 insertions(+), 24 deletions(-) create mode 100644 Sources/XToolSupport/EmulatedTools+SwiftCompiler.swift create mode 100644 Sources/XToolSupport/HotReloader.swift create mode 100644 skills/xtool/SKILL.md diff --git a/Sources/XToolSupport/BuildCoordinator.swift b/Sources/XToolSupport/BuildCoordinator.swift index 0fa6cb7b..e7ad155b 100644 --- a/Sources/XToolSupport/BuildCoordinator.swift +++ b/Sources/XToolSupport/BuildCoordinator.swift @@ -1,3 +1,4 @@ +import Dependencies import XCLIUtils import XPIF import Foundation @@ -11,13 +12,21 @@ public actor BuildCoordinator { let pifContainer: XPIF.Container let targetSelection: BuildDescription.TargetSelection? - init(description: BuildDescription) async throws { + private var buildDescriptionID: SWBBuildDescriptionID? + + /// - Parameter hotReloadable: Build with the flags that `HotReloader` needs + /// to swap out code in the running app. + init(description: BuildDescription, hotReloadable: Bool = false) async throws { self.pifContainer = description.pif self.targetSelection = description.targetSelection Signposter.track("Creating BuildSessionManager") - let sessionManager = try await BuildSessionManager() + let sessionManager = try await withDependencies { + $0.isHotReloadableBuild = hotReloadable + } operation: { + try await BuildSessionManager() + } self.sessionManager = sessionManager do { @@ -58,6 +67,20 @@ public actor BuildCoordinator { overrides["CODE_SIGN_IDENTITY[__destination_platform=YES]"] = "-" #endif overrides["AD_HOC_CODE_SIGNING_ALLOWED"] = "YES" + if hotReloadable { + // lets us ask for per-file compile commands via generatePreviewInfo + overrides["ENABLE_XOJIT_PREVIEWS"] = "YES" + // mutually exclusive with ENABLE_XOJIT_PREVIEWS + overrides["ENABLE_PREVIEWS"] = "NO" + // XOJIT previews would otherwise split the app into a stub executable + debug dylib + overrides["ENABLE_DEBUG_DYLIB"] = "NO" + // route calls through stubs so that reloaded symbols can be interposed + overrides["OTHER_LDFLAGS"] = "$(inherited) -Xlinker -interposable" + // https://kyleye.top/posts/debugreplaceableview-multiple-type-erasers/ + // https://www.guardsquare.com/blog/behind-swiftui-previews + overrides["SWIFT_ENABLE_OPAQUE_TYPE_ERASURE"] = "YES" + overrides["OTHER_SWIFT_FLAGS"] = "$(inherited) -Xfrontend -enable-private-imports" + } // Only override deployment targets when the triple explicitly includes a version. // Otherwise each package retains the deployment targets from its manifest. if let setting = targetInfo.deploymentTargetSettingName, let version = targetInfo.deploymentTarget { @@ -132,6 +155,10 @@ public actor BuildCoordinator { } func close() async { + if let buildDescriptionID { + self.buildDescriptionID = nil + await sessionManager.session.releaseBuildDescription(id: buildDescriptionID) + } await sessionManager.close() } @@ -177,12 +204,120 @@ public actor BuildCoordinator { ) } - func createBuildOperation() async throws -> SWBBuildOperation { + func mainBuildRequest() throws -> SWBBuildRequest { var request = buildRequest if request.configuredTargets.isEmpty { request.configuredTargets = [SWBConfiguredTarget(guid: try mainProductGUID())] } - return try await sessionManager.createBuildOperation(request: request) + return request + } + + func createBuildOperation() async throws -> SWBBuildOperation { + try await sessionManager.createBuildOperation(request: mainBuildRequest()) + } + + struct SourceFile: Sendable { + var path: String + var language: SWBSourceLanguage? + var target: SWBConfiguredTargetIdentifier + /// The toolchain used to build `target`, if known. + var toolchain: String? + } + + /// The source files of every target in the main build. + func sourceFiles() async throws -> [SourceFile] { + let buildDescription = try await retainedBuildDescription() + let request = try mainBuildRequest() + let targets = try await sessionManager.session.configuredTargets( + buildDescription: buildDescription, + buildRequest: request + ) + let toolchains = Dictionary( + targets.map { ($0.identifier, $0.toolchain?.pathString) }, + uniquingKeysWith: { first, _ in first } + ) + return try await sessionManager.session.sources( + of: targets.map(\.identifier), + buildDescription: buildDescription, + buildRequest: request + ).flatMap { info in + info.sourceFiles.map { + SourceFile( + path: $0.path.pathString, + language: $0.language, + target: info.configuredTarget, + toolchain: toolchains[info.configuredTarget] ?? nil + ) + } + } + } + + struct RecompileCommand: Sendable { + /// The frontend invocation, starting with the frontend executable. + var commandLine: [String] + /// Where `commandLine` writes the object file. + var objectFile: String + /// The SDK and target triple to link `objectFile` against. + var sdk: String + var target: String + } + + /// A frontend command that recompiles `file` in the context of its module in the main build. + /// + /// Requires `hotReloadable`. + func recompileCommand(for file: SourceFile) async throws -> RecompileCommand { + let guid = file.target.targetGUID.rawValue + // XOJIT preview "thunks" are the original file compiled as the primary file of its module. + let infos = try await sessionManager.generatePreviewInfo( + request: mainBuildRequest(), + targetID: guid, + sourceFile: file.path, + thunkVariantSuffix: "xtool" + ) + guard let info = infos.first(where: { !$0.compileCommandLine.isEmpty }) else { + throw Console.Error("Could not determine compile command for \(file.path)") + } + return RecompileCommand( + commandLine: info.compileCommandLine, + objectFile: info.thunkObjectFile, + sdk: try await evaluate("SDKROOT", target: guid, arch: info.architecture), + // what the Swift compiler spec passes as -target + target: try await evaluate("SWIFT_TARGET_TRIPLE", target: guid, arch: info.architecture) + ) + } + + private func evaluate(_ macro: String, target guid: String, arch: String) async throws -> String { + try await sessionManager.session.evaluateMacroAsString( + macro, level: .target(guid), + buildParameters: buildRequest.parameters, + // only set per-arch during task construction + overrides: ["CURRENT_ARCH": arch], + ) + } + + private func retainedBuildDescription() async throws -> SWBBuildDescriptionID { + if let buildDescriptionID { return buildDescriptionID } + // after a build, this is served from Swift Build's build description cache + let operation = try await sessionManager.createRetainedBuildDescriptionOperation( + request: mainBuildRequest() + ) + var id: SWBBuildDescriptionID? + for await event in try await operation.start() { + if case .reportBuildDescription(let info) = event { + id = SWBBuildDescriptionID(info.buildDescriptionID) + } + } + await operation.waitForCompletion() + guard operation.state == .succeeded, let id else { + throw Console.Error("Failed to compute build description") + } + // a concurrent call may have beaten us to it + if let buildDescriptionID { + await sessionManager.session.releaseBuildDescription(id: id) + return buildDescriptionID + } + buildDescriptionID = id + return id } } diff --git a/Sources/XToolSupport/BuildSessionManager.swift b/Sources/XToolSupport/BuildSessionManager.swift index 40515274..583bf3c2 100644 --- a/Sources/XToolSupport/BuildSessionManager.swift +++ b/Sources/XToolSupport/BuildSessionManager.swift @@ -102,9 +102,34 @@ actor BuildSessionManager { func createBuildOperation(request: SWBBuildRequest) async throws -> SWBBuildOperation { try await session.createBuildOperation(request: request, delegate: planningDelegate) } + + /// Plans (but doesn't run) a build. The caller is responsible for releasing the + /// resulting build description with `session.releaseBuildDescription(id:)`. + func createRetainedBuildDescriptionOperation(request: SWBBuildRequest) async throws -> SWBBuildOperation { + try await session.createBuildOperationForBuildDescriptionOnly( + request: request, + delegate: planningDelegate, + retainBuildDescription: true, + ) + } + + func generatePreviewInfo( + request: SWBBuildRequest, + targetID: String, + sourceFile: String, + thunkVariantSuffix: String + ) async throws -> [SWBPreviewInfo] { + try await session.generatePreviewInfo( + for: request, + targetID: targetID, + sourceFile: sourceFile, + thunkVariantSuffix: thunkVariantSuffix, + delegate: planningDelegate, + ) + } } -private final class PlanningOperationDelegate: SWBPlanningOperationDelegate, Sendable { +private final class PlanningOperationDelegate: SWBPlanningOperationDelegate, SWBPreviewDelegate, Sendable { let dependencies = withEscapedDependencies(\.self) func provisioningTaskInputs( diff --git a/Sources/XToolSupport/DevCommand.swift b/Sources/XToolSupport/DevCommand.swift index 853da719..c9e4c638 100644 --- a/Sources/XToolSupport/DevCommand.swift +++ b/Sources/XToolSupport/DevCommand.swift @@ -41,9 +41,17 @@ struct PackOperation { var triple: String? var buildOptions = BuildOptions(configuration: .debug) + var hotReloadable = false @discardableResult func run() async throws -> ProjectBuildResult { + try await run { result, _ in result } + } + + /// Builds the project, then invokes `body` while the build coordinator is still alive. + func run( + _ body: (ProjectBuildResult, BuildCoordinator) async throws -> R + ) async throws -> R { let project = try await XToolProject.discover(start: URL(filePath: ".")) try await EnsureSDKOperation(quiet: true).run() @@ -55,12 +63,16 @@ struct PackOperation { triple: triple ?? Self.defaultTriple, scheme: buildOptions.scheme ) - let coordinator = try await BuildCoordinator(description: description) + let coordinator = try await BuildCoordinator( + description: description, + hotReloadable: hotReloadable + ) defer { await coordinator.close() } - return try await ProjectBuildOperation( + let result = try await ProjectBuildOperation( buildCoordinator: coordinator, verbose: buildOptions.verbose ).run() + return try await body(result, coordinator) } } @@ -184,15 +196,26 @@ struct DevRunCommand: AsyncParsableCommand { @OptionGroup var packOptions: PackOperation.BuildOptions @Flag( - name: .shortAndLong, + name: [.customShort("s"), .customLong("simulator")], help: "Target the iOS Simulator (macOS only)", - ) var simulator = false + ) var explicitSimulator = false @Flag( inversion: .prefixedEnableDisable, help: "Attach console when running on the simulator (default: true)", ) var console: Bool? + @Flag( + name: .shortAndLong, + help: "Hot reload Swift files as they're edited (implies --simulator)", + ) var watch = false + + var simulator: Bool { + explicitSimulator || watch + } + + @Option(help: .hidden) var xloadPath: String? + #if os(macOS) var triple: String? { if simulator { @@ -213,19 +236,39 @@ struct DevRunCommand: AsyncParsableCommand { @OptionGroup var connectionOptions: ConnectionOptions func run() async throws { - let output = try await PackOperation(triple: triple, buildOptions: packOptions).run() + let operation = PackOperation( + triple: triple, + buildOptions: packOptions, + hotReloadable: watch, + ) #if os(macOS) if simulator { - try await SimInstallOperation( - path: output.url, - bundleID: output.bundleID, - console: console != false, - ).run() + if watch { + // keep the coordinator alive: the hot reloader uses it to get compiler args + try await operation.run { output, coordinator in + let hotReloader = try await HotReloader(coordinator: coordinator, xloadPath: xloadPath) + try await SimInstallOperation( + path: output.url, + bundleID: output.bundleID, + console: console != false, + hotReloader: hotReloader, + ).run() + } + } else { + let output = try await operation.run() + try await SimInstallOperation( + path: output.url, + bundleID: output.bundleID, + console: console != false, + ).run() + } return } #endif + let output = try await operation.run() + let token = try AuthToken.saved() let client = try await connectionOptions.client() @@ -267,6 +310,14 @@ struct DevRunCommand: AsyncParsableCommand { if console == true && !simulator { throw ValidationError("`--enable-console` is only supported when targeting the simulator") } + + if watch && packOptions.configuration != .debug { + throw ValidationError("`--watch` is only supported with the debug configuration") + } + + if xloadPath != nil && !watch { + throw ValidationError("`--xload-path` requires `--watch`") + } } } @@ -293,6 +344,7 @@ struct SimInstallOperation { var path: URL var bundleID: String var console: Bool + var hotReloader: HotReloader? // TODO: allow customizing this var simulator = "booted" @@ -321,14 +373,25 @@ struct SimInstallOperation { launchFlags.append("--console-pty") } - try await Subprocess.run( - .path(simctl), - arguments: Arguments(["launch"] + launchFlags + [simulator, bundleID]), - platformOptions: .withGracefulShutDown, - output: .xtoolOutput(), - error: .xtoolError(), - ) - .checkSuccess() + try await withThrowingDiscardingTaskGroup { group in + if let hotReloader { + group.addTask { try await hotReloader.watch() } + } + + try await Subprocess.run( + .path(simctl), + arguments: Arguments(["launch"] + launchFlags + [simulator, bundleID]), + environment: .inherit.updating(hotReloader?.launchEnvironment ?? [:]), + platformOptions: .withGracefulShutDown, + output: .xtoolOutput(), + error: .xtoolError(), + ) + .checkSuccess() + + // With the console attached, `launch` returns once the app exits, so we're done. + // Otherwise it returns immediately, so keep watching until we're interrupted. + if console { group.cancelAll() } + } } } #endif diff --git a/Sources/XToolSupport/EmulatedTools+Live.swift b/Sources/XToolSupport/EmulatedTools+Live.swift index c5341a68..8988d70b 100644 --- a/Sources/XToolSupport/EmulatedTools+Live.swift +++ b/Sources/XToolSupport/EmulatedTools+Live.swift @@ -7,7 +7,9 @@ import XUtils extension EmulatedTools: DependencyKey { static let liveValue = EmulatedTools({ - var tools: [any EmulatedTool] = [] + var tools: [any EmulatedTool] = [ + SwiftCompilerTool(), + ] #if !os(macOS) tools += [ LSRegisterTool(), diff --git a/Sources/XToolSupport/EmulatedTools+SwiftCompiler.swift b/Sources/XToolSupport/EmulatedTools+SwiftCompiler.swift new file mode 100644 index 00000000..bffe0e1c --- /dev/null +++ b/Sources/XToolSupport/EmulatedTools+SwiftCompiler.swift @@ -0,0 +1,139 @@ +import Dependencies +import Foundation +import Subprocess +import XUtils + +/// swift-frontend proxy that applies some fixes to hot-reloadable builds. +/// +/// Only active when `@Dependency(\.isHotReloadableBuild)` is `true`. +struct SwiftCompilerTool: EmulatedTool { + var name: String { "swift-frontend" } + + func execute( + commandLine: [String], + workingDirectory: String?, + environment: [String: String] + ) async throws -> EmulatedToolResult { + guard d.isHotReloadableBuild else { return .deferred } + + // The frontend unconditionally emits some symbols with hidden visibility, notably + // default arguments of public functions. When another file in the same module uses + // these, hot reloading that file fails (the dylib doesn't have access to the symbol). + // Fix this by exposing these symbols. + + let arguments = try Self.expandResponseFiles(Array(commandLine.dropFirst())) + guard arguments.first == "-frontend", arguments.contains("-c") else { return .deferred } + + let result = try await Subprocess.run( + .path(FilePath(commandLine[0])), + arguments: Arguments(Array(commandLine.dropFirst())), + environment: Environment(environment), + workingDirectory: workingDirectory.map { FilePath($0) }, + output: .data(limit: .max), + error: .data(limit: .max), + ) + guard result.terminationStatus.isSuccess else { return result.emulatedToolResult } + + let base = workingDirectory.map { URL(filePath: $0, directoryHint: .isDirectory) } + // one -o per primary file + for (index, argument) in arguments.enumerated() where argument == "-o" { + guard arguments.indices.contains(index + 1) else { continue } + try MachOSymbolExporter.exportHiddenSwiftSymbols( + in: URL(filePath: arguments[index + 1], relativeTo: base) + ) + } + return result.emulatedToolResult + } + + // The driver moves arguments into a response file when they exceed `ARG_MAX`. + private static func expandResponseFiles(_ arguments: [String]) throws -> [String] { + try arguments.flatMap { argument -> [String] in + guard argument.hasPrefix("@") else { return [argument] } + let contents = try String(contentsOfFile: String(argument.dropFirst()), encoding: .utf8) + // one shell-escaped argument per line + return contents.split(separator: "\n").map { line in + guard line.count >= 2, line.hasPrefix("'"), line.hasSuffix("'") else { return String(line) } + return String(line.dropFirst().dropLast()).replacing(#"'\''"#, with: "'") + } + } + } +} + +enum MachOSymbolExporter { + private static let magic64: UInt32 = 0xfeed_facf + private static let symtabCommand: UInt32 = 0x2 + private static let headerSize = 32 + private static let nlistSize = 16 + + // n_type bits + private static let stab: UInt8 = 0xe0 + private static let privateExternal: UInt8 = 0x10 + private static let typeMask: UInt8 = 0x0e + private static let definedInSection: UInt8 = 0x0e + private static let external: UInt8 = 0x01 + + /// Clears `N_PEXT` on defined Swift symbols in the 64-bit Mach-O object at `url`. + /// Leaves other files untouched. + static func exportHiddenSwiftSymbols(in url: URL) throws { + var bytes = [UInt8](try Data(contentsOf: url)) + guard exportHiddenSwiftSymbols(in: &bytes) else { return } + try Data(bytes).write(to: url) + } + + /// Returns whether any symbols were changed. + static func exportHiddenSwiftSymbols(in bytes: inout [UInt8]) -> Bool { + func read(_: T.Type, at offset: Int) -> T? { + guard offset >= 0, offset + MemoryLayout.size <= bytes.count else { return nil } + return bytes.withUnsafeBytes { T(littleEndian: $0.loadUnaligned(fromByteOffset: offset, as: T.self)) } + } + + guard read(UInt32.self, at: 0) == magic64, + let commandCount = read(UInt32.self, at: 16) + else { return false } + + var changed = false + var commandOffset = headerSize + for _ in 0.. 0 + else { break } + defer { commandOffset += Int(commandSize) } + guard command == symtabCommand, + let symbolOffset = read(UInt32.self, at: commandOffset + 8).map(Int.init), + let symbolCount = read(UInt32.self, at: commandOffset + 12).map(Int.init), + let stringOffset = read(UInt32.self, at: commandOffset + 16).map(Int.init) + else { continue } + + for index in 0.. nameOffset + 3, + bytes[nameOffset..<(nameOffset + 3)].elementsEqual("_$s".utf8) + else { continue } + bytes[entry + 4] = type & ~privateExternal + changed = true + } + } + return changed + } +} + +private enum IsHotReloadableBuildKey: DependencyKey { + static let liveValue = false + static let testValue = false +} + +extension DependencyValues { + /// Whether the current build is hot-reloadable + var isHotReloadableBuild: Bool { + get { self[IsHotReloadableBuildKey.self] } + set { self[IsHotReloadableBuildKey.self] = newValue } + } +} diff --git a/Sources/XToolSupport/HotReloader.swift b/Sources/XToolSupport/HotReloader.swift new file mode 100644 index 00000000..5e52ed09 --- /dev/null +++ b/Sources/XToolSupport/HotReloader.swift @@ -0,0 +1,190 @@ +#if os(macOS) + +import Foundation +import Dependencies +import FileSystemMonitoring +import Subprocess +import XCLIUtils +import XKit +import XUtils + +/// Recompiles Swift files as they're edited and hands the results to xload, +/// which interposes the new implementations into the running app. +/// +/// The app must be built with `BuildCoordinator(hotReloadable: true)`. +actor HotReloader { + private struct WatchedFile { + var source: BuildCoordinator.SourceFile + var modificationDate: Date? + } + + private let coordinator: BuildCoordinator + private let xloadPath: String + private let stagingDirectory: URL + private let outputDirectory: URL + + /// Keyed by canonical path. + private var files: [String: WatchedFile] + private var commands: [String: BuildCoordinator.RecompileCommand] = [:] + /// The latest dylib for each file, keyed by canonical path. + private var dylibs: [String: URL] = [:] + private var counter = 0 + + init(coordinator: BuildCoordinator, xloadPath: String? = nil) async throws { + self.coordinator = coordinator + + self.xloadPath = if let xloadPath { + URL(filePath: xloadPath).absoluteURL.path + } else { + try await Self.fetchXLoad(platform: "iphonesimulator").path + } + + let scratch = URL(filePath: "xtool/.xtool-tmp/HotReload").absoluteURL + try? FileManager.default.removeItem(at: scratch) + stagingDirectory = scratch.appending(path: "Staging") + outputDirectory = scratch.appending(path: "Products") + for directory in [stagingDirectory, outputDirectory] { + try FileManager.default.createDirectory(at: directory, withIntermediateDirectories: true) + } + + var files: [String: WatchedFile] = [:] + for source in try await coordinator.sourceFiles() where source.language == .swift { + // if a file belongs to multiple targets, arbitrarily pick the first + guard let path = Self.canonicalPath(source.path), files[path] == nil else { continue } + files[path] = WatchedFile(source: source, modificationDate: Self.modificationDate(path)) + } + self.files = files + } + + /// Environment variables to pass to `simctl launch` to enable hot reloading. + nonisolated var launchEnvironment: [Environment.Key: String] { + [ + "SIMCTL_CHILD_DYLD_INSERT_LIBRARIES": xloadPath, + "SIMCTL_CHILD_XLOAD_WATCH_DIR": outputDirectory.path, + "SIMCTL_CHILD_XLOAD_INTERCEPT": "1", + ] + } + + /// Watches for changes until cancelled. + func watch() async throws { + // TODO: handle files being added or removed + let events = try await d.fileSystemMonitor.watch(FilePath(FileManager.default.currentDirectoryPath)) + d.out.print("Watching for changes...") + for await event in events { + guard let path = Self.canonicalPath(event.file.string), let file = files[path] else { continue } + // a single save often produces multiple events + let modificationDate = Self.modificationDate(path) + guard modificationDate != file.modificationDate else { continue } + files[path]?.modificationDate = modificationDate + + do { + try await reload(path: path, source: file.source) + } catch is CancellationError { + break + } catch { + d.out.print("Failed to reload \(URL(filePath: path).lastPathComponent): \(error)") + } + } + } + + private func reload(path: String, source: BuildCoordinator.SourceFile) async throws { + counter += 1 + + let url = URL(filePath: path) + d.out.print("[Reload #\(counter)] Updating \(url.lastPathComponent)") + let start = ContinuousClock.now + + let command = try await recompileCommand(path: path, source: source) + try await Subprocess.run( + .path(FilePath(command.commandLine[0])), + arguments: Arguments(Array(command.commandLine.dropFirst())), + output: .xtoolOutput(), + error: .xtoolError(), + ) + .checkSuccess() + try MachOSymbolExporter.exportHiddenSwiftSymbols(in: URL(filePath: command.objectFile)) + + let dylibName = "lib\(url.deletingPathExtension().lastPathComponent).\(counter).dylib" + let stagedDylib = stagingDirectory.appending(path: dylibName) + try await Subprocess.run( + .path(swiftc(for: source)), + arguments: [ + command.objectFile, + "-emit-library", + "-sdk", command.sdk, + "-target", command.target, + // resolve references to the rest of the app at load time + "-Xlinker", "-undefined", "-Xlinker", "dynamic_lookup", + "-o", stagedDylib.path, + ], + output: .xtoolOutput(), + error: .xtoolError(), + ) + .checkSuccess() + + let elapsed = (ContinuousClock.now - start) + .formatted(.units(allowed: [.seconds, .milliseconds], width: .narrow)) + d.out.print("[Reload #\(counter)] Built in \(elapsed)") + + // xload loads new dylibs as soon as they appear, so they have to show up atomically + let dylib = outputDirectory.appending(path: dylibName) + try FileManager.default.moveItem(at: stagedDylib, to: dylib) + // Loaded dylibs stay mapped, so the previous version is no longer needed. If xload + // hasn't loaded it yet, it's superseded by this one anyway. + if let previous = dylibs.updateValue(dylib, forKey: path) { + try? FileManager.default.removeItem(at: previous) + } + } + + private func recompileCommand( + path: String, + source: BuildCoordinator.SourceFile + ) async throws -> BuildCoordinator.RecompileCommand { + if let cached = commands[path] { return cached } + let command = try await coordinator.recompileCommand(for: source) + commands[path] = command + return command + } + + private func swiftc(for source: BuildCoordinator.SourceFile) async throws -> FilePath { + if let toolchain = source.toolchain { + let swiftc = FilePath(toolchain).appending("usr/bin/swiftc") + if FileManager.default.isExecutableFile(atPath: swiftc.string) { return swiftc } + } + return try await d.toolchain.toolchainRoot.appending("usr/bin/swiftc") + } + + private static func canonicalPath(_ path: String) -> String? { + guard let resolved = realpath(path, nil) else { return nil } + defer { free(resolved) } + return String(cString: resolved) + } + + private static func modificationDate(_ path: String) -> Date? { + try? FileManager.default.attributesOfItem(atPath: path)[.modificationDate] as? Date + } + + private static func fetchXLoad(platform: String) async throws -> URL { + let version = "0.4.1" + let platformDirectory = d.persistentDirectory.appending(path: "xload/\(platform)") + let local = platformDirectory.appending(path: "\(version)/libXLoad.dylib") + if FileManager.default.fileExists(atPath: local.path) { return local } + + d.out.print("Downloading xload...") + let remote = URL( + string: "https://github.com/xtool-org/xload/releases/download/v\(version)/libXLoad.\(platform).dylib" + )! + let (_, body) = try await d.httpClient.makeRequest(HTTPRequest(url: remote)) + + // clear out old versions + try? FileManager.default.removeItem(at: platformDirectory) + try FileManager.default.createDirectory( + at: local.deletingLastPathComponent(), + withIntermediateDirectories: true + ) + try body.write(to: local, options: .atomic) + return local + } +} + +#endif diff --git a/skills/xtool/SKILL.md b/skills/xtool/SKILL.md new file mode 100644 index 00000000..aeb45d34 --- /dev/null +++ b/skills/xtool/SKILL.md @@ -0,0 +1,49 @@ +--- +name: xtool +description: Build and run Apple apps faster with xtool +--- +# About + +[xtool](https://xtool.sh) is a cross-platform Xcode replacement. It supports building and run apps for iOS and macOS. + +Run `xtool --help` to view the full command line interface. + +# Supported projects + +An xtool project should declare an `xtool.yml` at the top level. At minimum, it should have contents `version: 1`. + +Build commands will automatically discover Xcode/SwiftPM projects located at the same level as the `xtool.yml`, otherwise you can manually specify the project with `project: path/to/{Package.swift,YourProject.xcodeproj,YourWorkspace.xcworkspace}` + +## Xcode + +If the project/workspace has a single scheme, xtool will automatically discover and use it. Otherwise, disambiguate with `scheme: NameOfScheme` in `xtool.yml` + +## SwiftPM + +xtool can also build SwiftPM packages into apps. The package must contain a _library_ product with an `@main` entrypoint. You must also declare `orgID: com.yourdomain` or `bundleID: com.yourdomain.app` in `xtool.yml`. + +# Building and running + +Use `xtool dev build [--configuration debug|release] [--triple ...]` to build (see `--help` for full options) + +Use `xtool dev run [--configuration debug|release] [--simulator]` to build and then run on an iOS device/simulator (triple is inferred). + +When running on the simulator, the command attaches stdout/stderr. + +## Hot reloading SwiftUI + +Use `xtool dev run -w` to build and run on the iOS Simulator with support for hot reloading SwiftUI code. + +While hot reloading is active, xtool will automatically pick up edits to source files and inject the changes into the running app. You'll see output like this (lines with `#` indicate your actions): + +``` +[XLoad] Watching for changes... +# +[Reload #1] Updating FactView.swift +[Reload #1] Built in 352ms +🔥 ✅ Hot reload complete - Rebound 19 symbols, classes [] +``` + +Hot reloading works by interposing symbols at runtime. It works best when editing function/view bodies: SwiftUI views automatically re-render on reload. Adding types works, but changing a type's layout will crash. This is expected: just re-run the app if you change type layout. + +Use hot reloading judiciously when iterating on UI. It's a strictly faster flow than not hot-reloading. From bd5046ac207c3198a825af2484f440e260f04d0e Mon Sep 17 00:00:00 2001 From: Kabir Oberai Date: Sun, 15 Nov 2026 04:02:47 -0500 Subject: [PATCH 11/15] readme: add prerelease callout --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 70f09430..273f7466 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,10 @@ Cross-platform Xcode replacement. Build and deploy iOS apps with SwiftPM on Linux, Windows, and macOS. +> [!IMPORTANT] +> +> You're currently looking at a **beta** v2.x branch. The work here is experimental and pre-release. There are no stability guarantees until a non-prerelease 2.0.0 is tagged. + ## Overview xtool is a cross-platform (Linux/WSL/macOS) tool that replicates Xcode functionality with open standards. From 69d7def3fb6845f2050a8bcc2a6a467e083cccb7 Mon Sep 17 00:00:00 2001 From: Kabir Oberai Date: Sun, 15 Nov 2026 18:48:31 -0500 Subject: [PATCH 12/15] tests: fix XcodeScheme path comparison on Linux --- Tests/XToolTests/SwiftPMBuildDescriberTests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/XToolTests/SwiftPMBuildDescriberTests.swift b/Tests/XToolTests/SwiftPMBuildDescriberTests.swift index 677dc0a5..c1a52d2f 100644 --- a/Tests/XToolTests/SwiftPMBuildDescriberTests.swift +++ b/Tests/XToolTests/SwiftPMBuildDescriberTests.swift @@ -102,7 +102,7 @@ struct SwiftPMBuildDescriberTests { let runnable = try #require(scheme.runnable) #expect(runnable.name == "App-App") #expect(runnable.objectID == app.objectID.rawValue) - #expect(runnable.projectURL == projectURL.resolvingSymlinksInPath()) + #expect(runnable.projectURL.path == projectURL.resolvingSymlinksInPath().path) #expect(scheme.buildTargets.map(\.name) == ["App-App"]) #expect(scheme.buildImplicitDependencies) } From f1f42aada5002fc2340bdde08da6395a937703bd Mon Sep 17 00:00:00 2001 From: Kabir Oberai Date: Sun, 15 Nov 2026 19:05:13 -0500 Subject: [PATCH 13/15] tests: more XcodePIF test fixes --- Tests/XToolTests/XcodePIFBuilderTests.swift | 3 +++ Tests/XToolTests/XcodePackagePIFBuilderTests.swift | 6 ++++++ Tests/XToolTests/XcodeSchemeTests.swift | 3 +++ Tests/XToolTests/XcodeWorkspacePIFBuilderTests.swift | 3 +++ 4 files changed, 15 insertions(+) diff --git a/Tests/XToolTests/XcodePIFBuilderTests.swift b/Tests/XToolTests/XcodePIFBuilderTests.swift index e6a6f46e..7a2af86a 100644 --- a/Tests/XToolTests/XcodePIFBuilderTests.swift +++ b/Tests/XToolTests/XcodePIFBuilderTests.swift @@ -45,6 +45,8 @@ struct XcodePIFBuilderTests { #expect(try XPIF.Container(workspace: XcodePIFBuilder(project: source).build()).data() == data) } + // TODO: needs an installed Darwin SDK on Linux + #if os(macOS) @Test(.requiresXcode) func generatedWorkspaceIsAcceptedByBuildService() async throws { let workspace = try XcodePIFBuilder(project: fixtureProject()).build() let description = try BuildDescription( @@ -59,6 +61,7 @@ struct XcodePIFBuilderTests { #expect(try await coordinator.mainProductDetails().output.lastPathComponent == "CustomApp.app") await coordinator.close() } + #endif @Test func mapsConfigurationConditionsAndXCConfigs() throws { var source = try fixtureProject() diff --git a/Tests/XToolTests/XcodePackagePIFBuilderTests.swift b/Tests/XToolTests/XcodePackagePIFBuilderTests.swift index 3b469117..2b738ad0 100644 --- a/Tests/XToolTests/XcodePackagePIFBuilderTests.swift +++ b/Tests/XToolTests/XcodePackagePIFBuilderTests.swift @@ -13,6 +13,8 @@ import XUtils @testable import XToolSupport struct XcodePackagePIFBuilderTests { + // TODO: needs an installed Darwin SDK on Linux + #if os(macOS) @Test(.requiresXcode, .dependencies { $0.out.write = { _, _, _ in } }) func resolvesPackagesDiscoveredInSynchronizedFolders() async throws { let fixtures = fixtureURL("Projects") @@ -35,6 +37,7 @@ struct XcodePackagePIFBuilderTests { #expect(references[0].package.traits == ["Feature"]) #expect(references[0].isRoot) } + #endif @Test(arguments: [ (XCSchema.SwiftPackageVersionConstraint.revision("abc123"), "revision: \"abc123\""), @@ -81,6 +84,8 @@ struct XcodePackagePIFBuilderTests { #expect(throws: Console.Error.self) { try XcodePackagePIFBuilder.manifest(references: [self.remote(nil)]) } } + // TODO: needs an installed Darwin SDK on Linux + #if os(macOS) @Test(.requiresXcode, .dependencies { $0.out.write = { _, _, _ in } }) func buildsAppWithPackageResourcesAndTransitiveTarget() async throws { let temporary = try TemporaryDirectory(name: "PackageBuild") @@ -161,6 +166,7 @@ struct XcodePackagePIFBuilderTests { #expect(workspace.projects[0].targets[0].common.dependencies.contains { $0.targetId == product.id }) #expect(FileManager.default.fileExists(atPath: URL(filePath: package.projectDir).appending(path: "Package.swift").path)) } + #endif private func remote(_ constraint: XCSchema.SwiftPackageVersionConstraint?) -> XcodePIFBuilder.PackageReference { .init(sourceRoot: URL(filePath: "/tmp"), package: .init(location: .remote(.init( diff --git a/Tests/XToolTests/XcodeSchemeTests.swift b/Tests/XToolTests/XcodeSchemeTests.swift index 0b8bb4ea..fbcb569f 100644 --- a/Tests/XToolTests/XcodeSchemeTests.swift +++ b/Tests/XToolTests/XcodeSchemeTests.swift @@ -187,6 +187,8 @@ struct XcodeSchemeTests { expectNoDifference(try DevBSPCommand.parse(["--scheme", "Secondary"]).scheme, "Secondary") } + // TODO: needs an installed Darwin SDK on Linux + #if os(macOS) @Test(.requiresXcode) func configuresBuildServiceWithSchemeTargets() async throws { let description = try await describe(scheme: "Secondary") let selection = try #require(description.targetSelection) @@ -205,6 +207,7 @@ struct XcodeSchemeTests { throw error } } + #endif private func projects() -> URL { fixtureURL("Projects") diff --git a/Tests/XToolTests/XcodeWorkspacePIFBuilderTests.swift b/Tests/XToolTests/XcodeWorkspacePIFBuilderTests.swift index f0d61485..42321195 100644 --- a/Tests/XToolTests/XcodeWorkspacePIFBuilderTests.swift +++ b/Tests/XToolTests/XcodeWorkspacePIFBuilderTests.swift @@ -172,6 +172,8 @@ struct XcodeWorkspacePIFBuilderTests { return try #require(objects[rootObject]?["projectDirPath"] as? String) } + // TODO: needs an installed Darwin SDK on Linux + #if os(macOS) @Test(.requiresXcode, arguments: [false, true], [false, true]) func buildsWorkspaceWithLinkedAndEmbeddedFramework( implicitDependency: Bool, @@ -246,6 +248,7 @@ struct XcodeWorkspacePIFBuilderTests { throw error } } + #endif private var remoteReference: XCSchema.GroupTreeReference { .namePath(.init(components: [.child("Projects"), .child("Support.xcodeproj")])) From 854767cb2a1f9c579676b81ce344c39ed7376fed Mon Sep 17 00:00:00 2001 From: Kabir Oberai Date: Mon, 16 Nov 2026 02:52:21 -0500 Subject: [PATCH 14/15] swb: fix LC_BUILD_VERSION with swift linker-driver (#452) ## What does this PR do? Works around https://github.com/xtool-org/xtool/issues/351#issuecomment-6910459413 ## How was it tested? Tested against the Examples/ projects on macOS and Linux (Docker) ## AI tool usage How much of the code in this PR was AI-assisted? (check one) - [ ] **0** - No AI was used to write code - [x] **1** - I was assisted by AI in writing code. I reviewed the finished result. - [ ] **2** - I set the AI going and left it to it; nobody has read the result - no review, or AI review only --- Sources/XToolSupport/BuildCoordinator.swift | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/Sources/XToolSupport/BuildCoordinator.swift b/Sources/XToolSupport/BuildCoordinator.swift index e7ad155b..4308bb5f 100644 --- a/Sources/XToolSupport/BuildCoordinator.swift +++ b/Sources/XToolSupport/BuildCoordinator.swift @@ -52,6 +52,8 @@ public actor BuildCoordinator { parameters.activeArchitecture = arch var overrides: [String: String] = [:] + var targetLinkerFlags = "$(inherited)" + var targetSwiftFlags = "$(inherited)" let xtoolDir = description.directory.appending(path: "xtool") // if you have a macOS Xcode project with a package dependency, and build in debug @@ -67,6 +69,13 @@ public actor BuildCoordinator { overrides["CODE_SIGN_IDENTITY[__destination_platform=YES]"] = "-" #endif overrides["AD_HOC_CODE_SIGNING_ALLOWED"] = "YES" + // when LINKER_DRIVER=swiftc, LC_BUILD_VERSION.sdk is not set correctly by default. + // specifically, it invokes the clang linker-driver with `--sysroot ...` but Clang + // only looks at `-isysroot` to determine the SDK version. this primarily affects + // SwiftPM targets since they use LINKER_DRIVER=auto (instead of `=clang`). fix + // by explicitly passing `-isysroot` when the driver is swiftc. + overrides["XTL_LDFLAGS_swiftc"] = #"-Xclang-linker -isysroot -Xclang-linker "$(SDKROOT)""# + targetLinkerFlags += " $(XTL_LDFLAGS_$(LINKER_DRIVER))" if hotReloadable { // lets us ask for per-file compile commands via generatePreviewInfo overrides["ENABLE_XOJIT_PREVIEWS"] = "YES" @@ -75,11 +84,11 @@ public actor BuildCoordinator { // XOJIT previews would otherwise split the app into a stub executable + debug dylib overrides["ENABLE_DEBUG_DYLIB"] = "NO" // route calls through stubs so that reloaded symbols can be interposed - overrides["OTHER_LDFLAGS"] = "$(inherited) -Xlinker -interposable" + targetLinkerFlags += " -Xlinker -interposable" // https://kyleye.top/posts/debugreplaceableview-multiple-type-erasers/ // https://www.guardsquare.com/blog/behind-swiftui-previews overrides["SWIFT_ENABLE_OPAQUE_TYPE_ERASURE"] = "YES" - overrides["OTHER_SWIFT_FLAGS"] = "$(inherited) -Xfrontend -enable-private-imports" + targetSwiftFlags += " -Xfrontend -enable-private-imports" } // Only override deployment targets when the triple explicitly includes a version. // Otherwise each package retains the deployment targets from its manifest. @@ -128,13 +137,15 @@ public actor BuildCoordinator { // Use OpenAppleMacrosServer, which reimplements Apple's proprietary macros. // Also explicitly enable cross-import-overlays: they're on by default in Apple Swift // but off by default in OSS Swift. - overrides["OTHER_SWIFT_FLAGS[__destination_platform=YES]"] = """ - $(inherited) \ + targetSwiftFlags += " " + """ -external-plugin-path "\(bundle)/toolset/plugins#\(bundle)/toolset/bin/OpenAppleMacrosServer" \ -Xfrontend -enable-cross-import-overlays """ #endif + overrides["OTHER_LDFLAGS[__destination_platform=YES]"] = targetLinkerFlags + overrides["OTHER_SWIFT_FLAGS[__destination_platform=YES]"] = targetSwiftFlags + var overridesTable = SWBSettingsTable() for (key, value) in overrides { overridesTable.set(value: value, for: key) From d9ebbb6653146e2af8963daf0e5b3e2113144ee9 Mon Sep 17 00:00:00 2001 From: Kabir Oberai Date: Mon, 16 Nov 2026 02:58:38 -0500 Subject: [PATCH 15/15] sdk: fix bitcode_strip invocation --- Sources/XToolSupport/SDKBuilder.swift | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/Sources/XToolSupport/SDKBuilder.swift b/Sources/XToolSupport/SDKBuilder.swift index f2acff54..bd06a5bf 100644 --- a/Sources/XToolSupport/SDKBuilder.swift +++ b/Sources/XToolSupport/SDKBuilder.swift @@ -76,16 +76,20 @@ struct SDKBuilder { let mode: Mode // bump this when the sdk builder logic changes - static let sdkEpoch = 5 + static let sdkEpoch = 6 // tag from https://github.com/xtool-org/darwin-tools-linux-llvm static let darwinToolsVersion = "1.1.0" // tag from https://github.com/xtool-org/OpenAppleMacros static let oamVersion = "1.4.1" + // most EmulatedTools don't need this because we can intercept their invocation + // at the SwiftBuild/llbuild level. but for some tools we need a physical path + // on disk, which is where this comes in handy. // bump sdk epoch when this changes static let toolsToEmulate = [ "strip", + "bitcode_strip", ] static var currentSDKVersion: String {