Skip to content

Commit e79384c

Browse files
committed
fixup
1 parent 4473957 commit e79384c

File tree

8 files changed

+244
-186
lines changed

8 files changed

+244
-186
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
This source file is part of the Swift.org open source project
3+
4+
Copyright (c) 2021 Apple Inc. and the Swift project authors
5+
Licensed under Apache License v2.0 with Runtime Library Exception
6+
7+
See http://swift.org/LICENSE.txt for license information
8+
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
9+
*/
10+
11+
import Foundation
12+
import PackageModel
13+
import SPMBuildCore
14+
import TSCBasic
15+
import TSCUtility
16+
17+
public struct ArtifactsArchiveMetadata: Equatable {
18+
public let schemaVersion: String
19+
public let artifacts: [String: Artifact]
20+
21+
public init(schemaVersion: String, artifacts: [String: Artifact]) {
22+
self.schemaVersion = schemaVersion
23+
self.artifacts = artifacts
24+
}
25+
26+
public struct Artifact: Equatable {
27+
let type: ArtifactType
28+
let version: String
29+
let variants: [Variant]
30+
31+
public init(type: ArtifactsArchiveMetadata.ArtifactType, version: String, variants: [Variant]) {
32+
self.type = type
33+
self.version = version
34+
self.variants = variants
35+
}
36+
}
37+
38+
// In the future we are likely to extend the ArtifactsArchive file format to carry other types of artifacts beyond executables.
39+
// Additional fields may be required to support these new artifact types e.g. headers path for libraries.
40+
public enum ArtifactType: String, RawRepresentable, Decodable {
41+
case executable
42+
}
43+
44+
public struct Variant: Equatable {
45+
let path: String
46+
let supportedTriplets: [Triple]
47+
48+
public init(path: String, supportedTriplets: [Triple]) {
49+
self.path = path
50+
self.supportedTriplets = supportedTriplets
51+
}
52+
}
53+
}
54+
55+
extension ArtifactsArchiveMetadata {
56+
public static func parse(fileSystem: FileSystem, rootPath: AbsolutePath) throws -> ArtifactsArchiveMetadata {
57+
let path = rootPath.appending(component: "info.json")
58+
guard fileSystem.exists(path) else {
59+
throw StringError("ArtifactsArchive info.json not found at '\(rootPath)'")
60+
}
61+
62+
do {
63+
let bytes = try fileSystem.readFileContents(path)
64+
return try bytes.withData { data in
65+
let decoder = JSONDecoder.makeWithDefaults()
66+
return try decoder.decode(ArtifactsArchiveMetadata.self, from: data)
67+
}
68+
} catch {
69+
throw StringError("failed parsing ArtifactsArchive info.json at '\(path)': \(error)")
70+
}
71+
}
72+
}
73+
74+
extension ArtifactsArchiveMetadata: Decodable {
75+
enum CodingKeys: String, CodingKey {
76+
case schemaVersion
77+
case artifacts = "availableArtifacts"
78+
}
79+
}
80+
81+
extension ArtifactsArchiveMetadata.Artifact: Decodable {
82+
enum CodingKeys: String, CodingKey {
83+
case type
84+
case version
85+
case variants
86+
case supportedTriplets
87+
}
88+
89+
public init(from decoder: Decoder) throws {
90+
let container = try decoder.container(keyedBy: CodingKeys.self)
91+
self.type = try container.decode(ArtifactsArchiveMetadata.ArtifactType.self, forKey: .type)
92+
self.version = try container.decode(String.self, forKey: .version)
93+
self.variants = try container.decode([ArtifactsArchiveMetadata.Variant].self, forKey: .variants)
94+
}
95+
}
96+
97+
extension ArtifactsArchiveMetadata.Variant: Decodable {
98+
enum CodingKeys: String, CodingKey {
99+
case path
100+
case supportedTriplets
101+
}
102+
103+
public init(from decoder: Decoder) throws {
104+
let container = try decoder.container(keyedBy: CodingKeys.self)
105+
self.supportedTriplets = try container.decode([String].self, forKey: .supportedTriplets).map { try Triple($0) }
106+
self.path = try container.decode(String.self, forKey: .path)
107+
}
108+
}
109+

Sources/Build/BuildPlan.swift

Lines changed: 44 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,10 +1298,10 @@ public class BuildPlan {
12981298
private var pkgConfigCache = [SystemLibraryTarget: (cFlags: [String], libs: [String])]()
12991299

13001300
/// Cache for library information.
1301-
private var externalLibrariesCache = [BinaryTarget: LibraryInfo]()
1301+
private var externalLibrariesCache = [BinaryTarget: [LibraryInfo]]()
13021302

13031303
/// Cache for tools information.
1304-
private var externalToolsCache = [BinaryTarget: [ToolInfo]]()
1304+
private var externalExecutablesCache = [BinaryTarget: [ExecutableInfo]]()
13051305

13061306
private static func makeTestManifestTargets(
13071307
_ buildParameters: BuildParameters,
@@ -1640,11 +1640,13 @@ public class BuildPlan {
16401640
}
16411641
switch binaryTarget.kind {
16421642
case .xcframework:
1643-
let library = try self.parseXCFramework(for: binaryTarget)
1644-
libraryBinaryPaths.insert(library.binaryPath)
1645-
case .toolsArchive:
1646-
let tools = try self.parseToolsArchive(for: binaryTarget)
1647-
tools.forEach { availableTools[$0.name] = $0.binaryPath }
1643+
let libraries = try self.parseXCFramework(for: binaryTarget)
1644+
libraries.forEach { library in
1645+
libraryBinaryPaths.insert(library.libraryPath)
1646+
}
1647+
case .artifactsArchive:
1648+
let tools = try self.parseArtifactsArchive(for: binaryTarget)
1649+
tools.forEach { availableTools[$0.name] = $0.executablePath }
16481650
}
16491651
case .extension:
16501652
continue
@@ -1694,11 +1696,13 @@ public class BuildPlan {
16941696
clangTarget.additionalFlags += pkgConfig(for: target).cFlags
16951697
case let target as BinaryTarget:
16961698
if case .xcframework = target.kind {
1697-
let library = try self.parseXCFramework(for: target)
1698-
if let headersPath = library.headersPath {
1699-
clangTarget.additionalFlags += ["-I", headersPath.pathString]
1699+
let libraries = try self.parseXCFramework(for: target)
1700+
libraries.forEach { library in
1701+
if let headersPath = library.headersPath {
1702+
clangTarget.additionalFlags += ["-I", headersPath.pathString]
1703+
}
1704+
clangTarget.libraryBinaryPaths.insert(library.libraryPath)
17001705
}
1701-
clangTarget.libraryBinaryPaths.insert(library.binaryPath)
17021706
}
17031707
default: continue
17041708
}
@@ -1729,11 +1733,13 @@ public class BuildPlan {
17291733
swiftTarget.additionalFlags += pkgConfig(for: target).cFlags
17301734
case let target as BinaryTarget:
17311735
if case .xcframework = target.kind {
1732-
let library = try self.parseXCFramework(for: target)
1733-
if let headersPath = library.headersPath {
1734-
swiftTarget.additionalFlags += ["-Xcc", "-I", "-Xcc", headersPath.pathString]
1736+
let libraries = try self.parseXCFramework(for: target)
1737+
libraries.forEach { library in
1738+
if let headersPath = library.headersPath {
1739+
swiftTarget.additionalFlags += ["-Xcc", "-I", "-Xcc", headersPath.pathString]
1740+
}
1741+
swiftTarget.libraryBinaryPaths.insert(library.libraryPath)
17351742
}
1736-
swiftTarget.libraryBinaryPaths.insert(library.binaryPath)
17371743
}
17381744
default:
17391745
break
@@ -1853,7 +1859,7 @@ public class BuildPlan {
18531859
}
18541860

18551861
/// Extracts the library information from an XCFramework.
1856-
private func parseXCFramework(for target: BinaryTarget) throws -> LibraryInfo {
1862+
private func parseXCFramework(for target: BinaryTarget) throws -> [LibraryInfo] {
18571863
try self.externalLibrariesCache.memoize(key: target) {
18581864
let metadata = try XCFrameworkMetadata.parse(fileSystem: self.fileSystem, rootPath: target.artifactPath)
18591865

@@ -1868,26 +1874,30 @@ public class BuildPlan {
18681874
}
18691875

18701876
let libraryDirectory = target.artifactPath.appending(component: library.libraryIdentifier)
1871-
let binaryPath = libraryDirectory.appending(component: library.libraryPath)
1877+
let libraryPath = libraryDirectory.appending(component: library.libraryPath)
18721878
let headersPath = library.headersPath.map({ libraryDirectory.appending(component: $0) })
18731879

1874-
return LibraryInfo(binaryPath: binaryPath, headersPath: headersPath)
1880+
return [LibraryInfo(libraryPath: libraryPath, headersPath: headersPath)]
18751881
}
18761882
}
18771883

1878-
/// Extracts the executables info from an executablesArchive
1879-
private func parseToolsArchive(for target: BinaryTarget) throws -> [ToolInfo] {
1880-
try self.externalToolsCache.memoize(key: target) {
1881-
let metadata = try ToolsArchiveMetadata.parse(fileSystem: self.fileSystem, rootPath: target.artifactPath)
1884+
/// Extracts the artifacts from an artifactsArchive
1885+
private func parseArtifactsArchive(for target: BinaryTarget) throws -> [ExecutableInfo] {
1886+
try self.externalExecutablesCache.memoize(key: target) {
1887+
let metadata = try ArtifactsArchiveMetadata.parse(fileSystem: self.fileSystem, rootPath: target.artifactPath)
18821888

1883-
// filter the tools that are relevant to the triple
1884-
let supportedTools = metadata.tools.filter { $0.value.contains(where: { $0.supportedTriplets.contains(buildParameters.triple) }) }
1885-
// flatten the tools for each access
1886-
return supportedTools.reduce(into: [ToolInfo](), { partial, entry in
1887-
let tools = entry.value.map {
1888-
ToolInfo(name: entry.key, binaryPath: target.artifactPath.appending(RelativePath($0.path)))
1889+
// filter the artifacts that are relevant to the triple
1890+
// FIXME: this filter needs to become more sophisticated
1891+
let supportedArtifacts = metadata.artifacts.filter { $0.value.variants.contains(where: { $0.supportedTriplets.contains(buildParameters.triple) }) }
1892+
// TODO: add support for libraries
1893+
let executables = supportedArtifacts.filter { $0.value.type == .executable }
1894+
1895+
// flatten the results for easy access
1896+
return executables.reduce(into: [ExecutableInfo](), { partial, entry in
1897+
let executables = entry.value.variants.map {
1898+
ExecutableInfo(name: entry.key, executablePath: target.artifactPath.appending(RelativePath($0.path)))
18891899
}
1890-
partial.append(contentsOf: tools)
1900+
partial.append(contentsOf: executables)
18911901
})
18921902
}
18931903
}
@@ -1896,18 +1906,19 @@ public class BuildPlan {
18961906
/// Information about a library from a binary dependency.
18971907
private struct LibraryInfo: Equatable {
18981908
/// The path to the binary.
1899-
let binaryPath: AbsolutePath
1909+
let libraryPath: AbsolutePath
19001910

19011911
/// The path to the headers directory, if one exists.
19021912
let headersPath: AbsolutePath?
19031913
}
19041914

19051915
/// Information about an executable from a binary dependency.
1906-
private struct ToolInfo: Equatable {
1916+
private struct ExecutableInfo: Equatable {
19071917
/// The tool name
19081918
let name: String
1909-
/// The path to the binary.
1910-
let binaryPath: AbsolutePath
1919+
1920+
/// The path to the executable.
1921+
let executablePath: AbsolutePath
19111922
}
19121923

19131924
private extension Diagnostic.Message {

Sources/Build/ToolsArchiveMetadata.swift

Lines changed: 0 additions & 74 deletions
This file was deleted.

Sources/PackageModel/Target.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -517,14 +517,14 @@ public final class BinaryTarget: Target {
517517

518518
public enum Kind: String, RawRepresentable, Codable, CaseIterable {
519519
case xcframework
520-
case toolsArchive
520+
case artifactsArchive
521521

522522
public var fileExtension: String {
523523
switch self {
524524
case .xcframework:
525525
return "xcframework"
526-
case .toolsArchive:
527-
return "toar"
526+
case .artifactsArchive:
527+
return "arar"
528528
}
529529
}
530530

0 commit comments

Comments
 (0)