Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ extension SwiftSDKGenerator {
return toolsetJSONPath
}

/// Generates `swift-sdk.json` metadata file.
func generateSwiftSDKMetadata(
toolsetPath: FilePath,
sdkDirPath: FilePath,
Expand All @@ -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"
)

Expand Down Expand Up @@ -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")
Expand All @@ -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,
Expand Down
64 changes: 1 addition & 63 deletions Sources/SwiftSDKGenerator/Serialization/SwiftSDKMetadata.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
}