diff --git a/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Entrypoint.swift b/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Entrypoint.swift index eecdd99..e43d51a 100644 --- a/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Entrypoint.swift +++ b/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Entrypoint.swift @@ -34,7 +34,7 @@ extension Triple.Arch { } extension SwiftSDKGenerator { - package func run(recipe: SwiftSDKRecipe) async throws { + package func run(recipe: some SwiftSDKRecipe) async throws { try await withQueryEngine(OSFileSystem(), self.logger, cacheLocation: self.engineCachePath) { engine in let httpClientType: HTTPClientProtocol.Type @@ -79,7 +79,8 @@ extension SwiftSDKGenerator { try await generateArtifactBundleManifest( hostTriples: swiftSDKProduct.hostTriples, - artifacts: artifacts + artifacts: artifacts, + shouldUseFullPaths: recipe.shouldSupportEmbeddedSwift ) // Extra spaces added for readability for the user diff --git a/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Metadata.swift b/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Metadata.swift index fbb056b..b6fe42d 100644 --- a/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Metadata.swift +++ b/Sources/SwiftSDKGenerator/Generator/SwiftSDKGenerator+Metadata.swift @@ -49,6 +49,7 @@ extension SwiftSDKGenerator { return toolsetJSONPath } + /// Generates `swift-sdk.json` metadata file. func generateSwiftSDKMetadata( toolsetPath: FilePath, sdkDirPath: FilePath, @@ -57,7 +58,7 @@ extension SwiftSDKGenerator { ) throws -> FilePath { logger.info("Generating Swift SDK metadata JSON file...") - let destinationJSONPath = pathsConfiguration.swiftSDKRootPath.appending( + let swiftSDKMetadataPath = pathsConfiguration.swiftSDKRootPath.appending( "\(isForEmbeddedSwift ? "embedded-" : "")swift-sdk.json" ) @@ -95,14 +96,18 @@ extension SwiftSDKGenerator { ) try writeFile( - at: destinationJSONPath, + at: swiftSDKMetadataPath, encoder.encode(metadata) ) - return destinationJSONPath + return swiftSDKMetadataPath } - func generateArtifactBundleManifest(hostTriples: [Triple]?, artifacts: [String: FilePath]) throws { + func generateArtifactBundleManifest( + hostTriples: [Triple]?, + artifacts: [String: FilePath], + shouldUseFullPaths: Bool + ) throws { logger.info("Generating .artifactbundle info JSON file...") let artifactBundleManifestPath = pathsConfiguration.artifactBundlePath.appending("info.json") @@ -116,6 +121,9 @@ extension SwiftSDKGenerator { var relativePath = $0 let prefixRemoved = relativePath.removePrefix(pathsConfiguration.artifactBundlePath) assert(prefixRemoved) + if !shouldUseFullPaths { + relativePath.removeLastComponent() + } return .init( type: .swiftSDK, diff --git a/Sources/SwiftSDKGenerator/Serialization/SwiftSDKMetadata.swift b/Sources/SwiftSDKGenerator/Serialization/SwiftSDKMetadata.swift index ad855ea..d1cccde 100644 --- a/Sources/SwiftSDKGenerator/Serialization/SwiftSDKMetadata.swift +++ b/Sources/SwiftSDKGenerator/Serialization/SwiftSDKMetadata.swift @@ -10,68 +10,6 @@ // //===----------------------------------------------------------------------===// -struct DestinationV1: Encodable { - enum CodingKeys: String, CodingKey { - case version - case sdk - case toolchainBinDir = "toolchain-bin-dir" - case target - case extraCCFlags = "extra-cc-flags" - case extraSwiftCFlags = "extra-swiftc-flags" - case extraCPPFlags = "extra-cpp-flags" - } - - let version = 1 - let sdk: String - let toolchainBinDir: String - let target: String - let extraCCFlags: [String] - let extraSwiftCFlags: [String] - let extraCPPFlags: [String] -} - -struct DestinationV2: Encodable { - let version = 2 - - let sdkRootDir: String - let toolchainBinDir: String - let hostTriples: [String] - let targetTriples: [String] - let extraCCFlags: [String] - let extraSwiftCFlags: [String] - let extraCXXFlags: [String] - let extraLinkerFlags: [String] -} - -/// Represents v3 schema of `destination.json` files used for cross-compilation. -struct DestinationV3: Encodable { - struct TripleProperties: Encodable { - /// Path relative to `destination.json` containing SDK root. - let sdkRootPath: String - - /// Path relative to `destination.json` containing Swift resources for dynamic linking. - var swiftResourcesPath: String? - - /// Path relative to `destination.json` containing Swift resources for static linking. - var swiftStaticResourcesPath: String? - - /// Array of paths relative to `destination.json` containing headers. - var includeSearchPaths: [String]? - - /// Array of paths relative to `destination.json` containing libraries. - var librarySearchPaths: [String]? - - /// Array of paths relative to `destination.json` containing toolset files. - let toolsetPaths: [String]? - } - - /// Version of the schema used when serializing the destination file. - let schemaVersion = "3.0" - - /// Mapping of triple strings to corresponding properties of such target triple. - let runTimeTriples: [String: TripleProperties] -} - /// Represents v4 schema of `swift-sdk.json` (previously `destination.json`) files used for cross-compilation. package struct SwiftSDKMetadataV4: Encodable { package struct TripleProperties: Encodable { @@ -94,7 +32,7 @@ package struct SwiftSDKMetadataV4: Encodable { var toolsetPaths: [String]? } - /// Version of the schema used when serializing the destination file. + /// Version of the schema used when serializing the Swift SDK metadata file. let schemaVersion = "4.0" /// Mapping of triple strings to corresponding properties of such target triple. diff --git a/Tests/SwiftSDKGeneratorTests/Generator/SwiftSDKGenerator+MetadataTests.swift b/Tests/SwiftSDKGeneratorTests/Generator/SwiftSDKGenerator+MetadataTests.swift index 5074efd..fb03e3e 100644 --- a/Tests/SwiftSDKGeneratorTests/Generator/SwiftSDKGenerator+MetadataTests.swift +++ b/Tests/SwiftSDKGeneratorTests/Generator/SwiftSDKGenerator+MetadataTests.swift @@ -76,28 +76,38 @@ final class SwiftSDKGeneratorMetadataTests: XCTestCase { try await sdk.createDirectoryIfNeeded(at: sdk.pathsConfiguration.artifactBundlePath) - // Generate bundle metadata - try await sdk.generateArtifactBundleManifest( - hostTriples: [sdk.targetTriple], - artifacts: ["foo": sdk.pathsConfiguration.artifactBundlePath.appending("bar.json")] - ) + for shouldUseFullPaths in [true, false] { + // Generate bundle metadata + try await sdk.generateArtifactBundleManifest( + hostTriples: [sdk.targetTriple], + artifacts: ["foo": sdk.pathsConfiguration.artifactBundlePath.appending("foo").appending("bar.json")], + shouldUseFullPaths: shouldUseFullPaths + ) - // Make sure the file exists - let archiveMetadataFile = await sdk.pathsConfiguration.artifactBundlePath.appending("info.json") - fileExists = await sdk.doesFileExist(at: archiveMetadataFile) - XCTAssertTrue(fileExists) + // Make sure the file exists + let archiveMetadataFile = await sdk.pathsConfiguration.artifactBundlePath.appending("info.json") + fileExists = await sdk.doesFileExist(at: archiveMetadataFile) + XCTAssertTrue(fileExists) - // Read back file, make sure it contains the expected data - let data = try await sdk.readFile(at: archiveMetadataFile) - let decodedMetadata = try JSONDecoder().decode(ArtifactsArchiveMetadata.self, from: data) - XCTAssertEqual(decodedMetadata.artifacts.count, 1) - for (id, artifact) in decodedMetadata.artifacts { - XCTAssertEqual(id, "foo") - XCTAssertEqual(artifact.variants, [.init(path: "bar.json", supportedTriples: [testCase.targetTriple.triple])]) - } + // Read back file, make sure it contains the expected data + let data = try await sdk.readFile(at: archiveMetadataFile) + let decodedMetadata = try JSONDecoder().decode(ArtifactsArchiveMetadata.self, from: data) + XCTAssertEqual(decodedMetadata.artifacts.count, 1) + let variant: ArtifactsArchiveMetadata.Variant + if shouldUseFullPaths { + variant = .init(path: "foo/bar.json", supportedTriples: [testCase.targetTriple.triple]) + } else { + variant = .init(path: "foo", supportedTriples: [testCase.targetTriple.triple]) + } - // Cleanup - try await sdk.removeFile(at: archiveMetadataFile) + for (id, artifact) in decodedMetadata.artifacts { + XCTAssertEqual(id, "foo") + XCTAssertEqual(artifact.variants, [variant]) + } + + // Cleanup + try await sdk.removeFile(at: archiveMetadataFile) + } } } }