Skip to content

Commit 1f2eda3

Browse files
authored
Move down the parsing of binary artifacts such as XCFrameworks and ArtifactArchives to SPMBuildCore (which makes sense anyway, but in particular so that plugins can have access to it). (#3349)
1 parent 93900f5 commit 1f2eda3

File tree

8 files changed

+89
-72
lines changed

8 files changed

+89
-72
lines changed

Sources/Build/BuildPlan.swift

Lines changed: 2 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1967,66 +1967,18 @@ public class BuildPlan {
19671967
/// Extracts the library information from an XCFramework.
19681968
private func parseXCFramework(for target: BinaryTarget) throws -> [LibraryInfo] {
19691969
try self.externalLibrariesCache.memoize(key: target) {
1970-
let metadata = try XCFrameworkMetadata.parse(fileSystem: self.fileSystem, rootPath: target.artifactPath)
1971-
1972-
// Check that it supports the target platform and architecture.
1973-
guard let library = metadata.libraries.first(where: {
1974-
$0.platform == buildParameters.triple.os.asXCFrameworkPlatformString && $0.architectures.contains(buildParameters.triple.arch.rawValue)
1975-
}) else {
1976-
throw StringError("""
1977-
artifact '\(target.name)' does not support the target platform and architecture \
1978-
('\(buildParameters.triple)')
1979-
""")
1980-
}
1981-
1982-
let libraryDirectory = target.artifactPath.appending(component: library.libraryIdentifier)
1983-
let libraryPath = libraryDirectory.appending(RelativePath(library.libraryPath))
1984-
let headersPath = library.headersPath.map({ libraryDirectory.appending(RelativePath($0)) })
1985-
1986-
return [LibraryInfo(libraryPath: libraryPath, headersPath: headersPath)]
1970+
return try target.parseXCFrameworks(for: self.buildParameters, fileSystem: self.fileSystem)
19871971
}
19881972
}
19891973

19901974
/// Extracts the artifacts from an artifactsArchive
19911975
private func parseArtifactsArchive(for target: BinaryTarget) throws -> [ExecutableInfo] {
19921976
try self.externalExecutablesCache.memoize(key: target) {
1993-
let metadata = try ArtifactsArchiveMetadata.parse(fileSystem: self.fileSystem, rootPath: target.artifactPath)
1994-
1995-
// filter the artifacts that are relevant to the triple
1996-
// FIXME: this filter needs to become more sophisticated
1997-
let supportedArtifacts = metadata.artifacts.filter { $0.value.variants.contains(where: { $0.supportedTriples.contains(buildParameters.triple) }) }
1998-
// TODO: add support for libraries
1999-
let executables = supportedArtifacts.filter { $0.value.type == .executable }
2000-
2001-
// flatten the results for easy access
2002-
return executables.reduce(into: [ExecutableInfo](), { partial, entry in
2003-
let executables = entry.value.variants.map {
2004-
ExecutableInfo(name: entry.key, executablePath: target.artifactPath.appending(RelativePath($0.path)))
2005-
}
2006-
partial.append(contentsOf: executables)
2007-
})
1977+
return try target.parseArtifactArchives(for: self.buildParameters, fileSystem: self.fileSystem)
20081978
}
20091979
}
20101980
}
20111981

2012-
/// Information about a library from a binary dependency.
2013-
private struct LibraryInfo: Equatable {
2014-
/// The path to the binary.
2015-
let libraryPath: AbsolutePath
2016-
2017-
/// The path to the headers directory, if one exists.
2018-
let headersPath: AbsolutePath?
2019-
}
2020-
2021-
/// Information about an executable from a binary dependency.
2022-
private struct ExecutableInfo: Equatable {
2023-
/// The tool name
2024-
let name: String
2025-
2026-
/// The path to the executable.
2027-
let executablePath: AbsolutePath
2028-
}
2029-
20301982
private extension Diagnostic.Message {
20311983
static var swiftBackDeployError: Diagnostic.Message {
20321984
.warning("Swift compiler no longer supports statically linking the Swift libraries. They're included in the OS by default starting with macOS Mojave 10.14.4 beta 3. For macOS Mojave 10.14.3 and earlier, there's an optional Swift library package that can be downloaded from \"More Downloads\" for Apple Developers at https://developer.apple.com/download/more/")
@@ -2108,18 +2060,6 @@ private func generateResourceInfoPlist(
21082060
return true
21092061
}
21102062

2111-
fileprivate extension Triple.OS {
2112-
/// Returns a representation of the receiver that can be compared with platform strings declared in an XCFramework.
2113-
var asXCFrameworkPlatformString: String? {
2114-
switch self {
2115-
case .darwin, .linux, .wasi, .windows:
2116-
return nil // XCFrameworks do not support any of these platforms today.
2117-
case .macOS:
2118-
return "macos"
2119-
}
2120-
}
2121-
}
2122-
21232063
fileprivate extension Triple {
21242064
var isSupportingStaticStdlib: Bool {
21252065
isLinux() || arch == .wasm32

Sources/Build/CMakeLists.txt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
# This source file is part of the Swift.org open source project
22
#
3-
# Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
3+
# Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
44
# Licensed under Apache License v2.0 with Runtime Library Exception
55
#
66
# See http://swift.org/LICENSE.txt for license information
77
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
88

99
add_library(Build
10-
ArtifactsArchiveMetadata.swift
1110
BuildOperationBuildSystemDelegateHandler.swift
1211
BuildOperation.swift
1312
BuildPlan.swift
1413
ManifestBuilder.swift
1514
SPMSwiftDriverExecutor.swift
16-
SwiftCompilerOutputParser.swift
17-
XCFrameworkMetadata.swift)
15+
SwiftCompilerOutputParser.swift)
1816
target_link_libraries(Build PUBLIC
1917
TSCBasic
2018
Basics

Sources/Build/ArtifactsArchiveMetadata.swift renamed to Sources/SPMBuildCore/ArtifactsArchiveMetadata.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
import Foundation
1212
import PackageModel
13-
import SPMBuildCore
1413
import TSCBasic
1514
import TSCUtility
1615

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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 Basics
13+
import PackageModel
14+
import PackageGraph
15+
import TSCBasic
16+
import TSCUtility
17+
18+
19+
/// Information about a library from a binary dependency.
20+
public struct LibraryInfo: Equatable {
21+
/// The path to the binary.
22+
public let libraryPath: AbsolutePath
23+
24+
/// The path to the headers directory, if one exists.
25+
public let headersPath: AbsolutePath?
26+
}
27+
28+
29+
/// Information about an executable from a binary dependency.
30+
public struct ExecutableInfo: Equatable {
31+
/// The tool name
32+
public let name: String
33+
34+
/// The path to the executable.
35+
public let executablePath: AbsolutePath
36+
}
37+
38+
39+
extension BinaryTarget {
40+
41+
public func parseXCFrameworks(for buildParameters: BuildParameters, fileSystem: FileSystem) throws -> [LibraryInfo] {
42+
let metadata = try XCFrameworkMetadata.parse(fileSystem: fileSystem, rootPath: self.artifactPath)
43+
guard let library = metadata.libraries.first(where: {
44+
$0.platform == buildParameters.triple.os.asXCFrameworkPlatformString &&
45+
$0.architectures.contains(buildParameters.triple.arch.rawValue)
46+
}) else {
47+
return []
48+
}
49+
let libraryDir = self.artifactPath.appending(component: library.libraryIdentifier)
50+
let libraryFile = libraryDir.appending(RelativePath(library.libraryPath))
51+
let headersDir = library.headersPath.map({ libraryDir.appending(RelativePath($0)) })
52+
return [LibraryInfo(libraryPath: libraryFile, headersPath: headersDir)]
53+
}
54+
55+
public func parseArtifactArchives(for buildParameters: BuildParameters, fileSystem: FileSystem) throws -> [ExecutableInfo] {
56+
let metadata = try ArtifactsArchiveMetadata.parse(fileSystem: fileSystem, rootPath: self.artifactPath)
57+
// filter the artifacts that are relevant to the triple
58+
// FIXME: this filter needs to become more sophisticated
59+
let supportedArtifacts = metadata.artifacts.filter { $0.value.variants.contains(where: { $0.supportedTriples.contains(buildParameters.triple) }) }
60+
// TODO: add support for libraries
61+
let executables = supportedArtifacts.filter { $0.value.type == .executable }
62+
return executables.flatMap { entry in
63+
entry.value.variants.map{ ExecutableInfo(name: entry.key, executablePath: self.artifactPath.appending(RelativePath($0.path))) }
64+
}
65+
}
66+
}
67+
68+
fileprivate extension Triple.OS {
69+
/// Returns a representation of the receiver that can be compared with platform strings declared in an XCFramework.
70+
var asXCFrameworkPlatformString: String? {
71+
switch self {
72+
case .darwin, .linux, .wasi, .windows:
73+
return nil // XCFrameworks do not support any of these platforms today.
74+
case .macOS:
75+
return "macos"
76+
}
77+
}
78+
}

Sources/SPMBuildCore/CMakeLists.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
# See http://swift.org/CONTRIBUTORS.txt for Swift project authors
88

99
add_library(SPMBuildCore
10+
ArtifactsArchiveMetadata.swift
11+
BinaryTarget+Extensions.swift
1012
BuildParameters.swift
1113
BuildSystem.swift
1214
BuildSystemCommand.swift
@@ -15,7 +17,8 @@ add_library(SPMBuildCore
1517
PluginInvocation.swift
1618
PrebuildCommandResult.swift
1719
Sanitizers.swift
18-
Toolchain.swift)
20+
Toolchain.swift
21+
XCFrameworkMetadata.swift)
1922
# NOTE(compnerd) workaround for CMake not setting up include flags yet
2023
set_target_properties(SPMBuildCore PROPERTIES
2124
INTERFACE_INCLUDE_DIRECTORIES ${CMAKE_Swift_MODULE_DIRECTORY})

Sources/Build/XCFrameworkMetadata.swift renamed to Sources/SPMBuildCore/XCFrameworkMetadata.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
import Foundation
1212
import PackageModel
13-
import SPMBuildCore
1413
import TSCBasic
1514
import TSCUtility
1615

Tests/BuildTests/ArtifactsArchiveMetadataTests.swift renamed to Tests/SPMBuildCoreTests/ArtifactsArchiveMetadataTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
/*
22
This source file is part of the Swift.org open source project
33

4-
Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
4+
Copyright (c) 2014 - 2021 Apple Inc. and the Swift project authors
55
Licensed under Apache License v2.0 with Runtime Library Exception
66

77
See http://swift.org/LICENSE.txt for license information
88
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
99
*/
1010

11-
import Build
11+
import SPMBuildCore
1212
import TSCBasic
1313
import TSCUtility
1414
import XCTest

Tests/BuildTests/XCFrameworkMetadataTests.swift renamed to Tests/SPMBuildCoreTests/XCFrameworkMetadataTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
99
*/
1010

11-
import Build
11+
import SPMBuildCore
1212
import TSCBasic
1313
import XCTest
1414

0 commit comments

Comments
 (0)