1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2021 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import Basics
import Foundation
import PackageGraph
import PackageModel
/// Information about a library from a binary dependency.
public struct LibraryInfo: Equatable {
/// The path to the binary.
public let libraryPath: AbsolutePath
/// The paths to the headers directories.
public let headersPaths: [AbsolutePath]
}
/// Information about an executable from a binary dependency.
public struct ExecutableInfo: Equatable {
/// The tool name
public let name: String
/// The path to the executable.
public let executablePath: AbsolutePath
/// Supported triples, e.g. `x86_64-apple-macosx`
public let supportedTriples: [Triple]
}
extension BinaryModule {
public func parseXCFrameworks(for triple: Triple, fileSystem: FileSystem) throws -> [LibraryInfo] {
// At the moment we return at most a single library.
let metadata = try XCFrameworkMetadata.parse(fileSystem: fileSystem, rootPath: self.artifactPath)
// Filter the libraries that are relevant to the triple.
guard let library = metadata.libraries.first(where: {
$0.platform == triple.os?.asXCFrameworkPlatformString &&
$0.variant == triple.environment?.asXCFrameworkPlatformVariantString &&
$0.architectures.contains(triple.archName)
}) else {
return []
}
// Construct a LibraryInfo for the library.
let libraryDir = self.artifactPath.appending(component: library.libraryIdentifier)
let libraryFile = try AbsolutePath(validating: library.libraryPath, relativeTo: libraryDir)
let headersDirs = try library.headersPath
.map { [try AbsolutePath(validating: $0, relativeTo: libraryDir)] } ?? [] + [libraryDir]
return [LibraryInfo(libraryPath: libraryFile, headersPaths: headersDirs)]
}
public func parseArtifactArchives(for triple: Triple, fileSystem: FileSystem) throws -> [ExecutableInfo] {
// The host triple might contain a version which we don't want to take into account here.
let versionLessTriple = try triple.withoutVersion()
// We return at most a single variant of each artifact.
let metadata = try ArtifactsArchiveMetadata.parse(fileSystem: fileSystem, rootPath: self.artifactPath)
// Currently we filter out everything except executables.
// TODO: Add support for libraries
let executables = metadata.artifacts.filter { $0.value.type == .executable }
// Construct an ExecutableInfo for each matching variant.
return try executables.flatMap { entry in
// Filter supported triples with versionLessTriple and pass into
// ExecutableInfo; empty if non matching triples found.
try entry.value.variants.map {
guard let supportedTriples = $0.supportedTriples else {
throw StringError("No \"supportedTriples\" found in the artifact metadata for \(entry.key) in \(self.artifactPath)")
}
let filteredSupportedTriples = try supportedTriples
.filter { try $0.withoutVersion() == versionLessTriple }
return ExecutableInfo(
name: entry.key,
executablePath: self.artifactPath.appending($0.path),
supportedTriples: filteredSupportedTriples
)
}
}
}
}
extension Triple {
func withoutVersion() throws -> Triple {
if isDarwin() {
let stringWithoutVersion = tripleString(forPlatformVersion: "")
return try Triple(stringWithoutVersion)
} else {
return self
}
}
}
extension Triple.OS {
/// Returns a representation of the receiver that can be compared with platform strings declared in an XCFramework.
fileprivate var asXCFrameworkPlatformString: String? {
switch self {
case .darwin, .linux, .wasi, .win32, .openbsd, .noneOS:
return nil // XCFrameworks do not support any of these platforms today.
case .macosx:
return "macos"
case .ios:
return "ios"
case .tvos:
return "tvos"
case .watchos:
return "watchos"
default:
return nil // XCFrameworks do not support any of these platforms today.
}
}
}
extension Triple.Environment {
fileprivate var asXCFrameworkPlatformVariantString: String? {
switch self {
case .simulator:
return "simulator"
case .macabi:
return "maccatalyst"
default:
return nil // XCFrameworks do not support any of these platform variants today.
}
}
}
|