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 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
//===----------- PrintTargetInfoJob.swift - Swift Target Info Job ---------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import protocol TSCBasic.FileSystem
import class Foundation.JSONDecoder
import struct TSCBasic.AbsolutePath
import class TSCBasic.DiagnosticsEngine
/// Swift versions are major.minor.
struct SwiftVersion {
var major: Int
var minor: Int
init?(string: String) {
let components = string.split(
separator: ".", maxSplits: 2, omittingEmptySubsequences: false)
.compactMap { Int($0)}
guard components.count == 2 else { return nil }
self.major = components[0]
self.minor = components[1]
}
init(major: Int, minor: Int) {
self.major = major
self.minor = minor
}
}
extension SwiftVersion: Comparable {
static func < (lhs: SwiftVersion, rhs: SwiftVersion) -> Bool {
(lhs.major, lhs.minor) < (rhs.major, rhs.minor)
}
}
extension SwiftVersion: CustomStringConvertible {
var description: String { "\(major).\(minor)" }
}
extension SwiftVersion: Codable {
func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(description)
}
init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let string = try container.decode(String.self)
guard let version = SwiftVersion(string: string) else {
throw DecodingError.dataCorrupted(.init(
codingPath: decoder.codingPath,
debugDescription: "Invalid Swift version string \(string)"))
}
self = version
}
}
/// Describes information about the target as provided by the Swift frontend.
@dynamicMemberLookup
public struct FrontendTargetInfo: Codable {
struct CompatibilityLibrary: Codable {
enum Filter: String, Codable {
case all
case executable
}
let libraryName: String
let filter: Filter
let forceLoad: Bool?
}
struct Target: Codable {
/// The target triple
let triple: Triple
/// The target triple without any version information.
let unversionedTriple: Triple
/// The triple used for module names.
let moduleTriple: Triple
/// The version of the Swift runtime that is present in the runtime
/// environment of the target.
var swiftRuntimeCompatibilityVersion: SwiftVersion?
/// The set of compatibility libraries that one needs to link against
/// for this particular target.
let compatibilityLibraries: [CompatibilityLibrary]
/// Whether the Swift libraries need to be referenced in their system
/// location (/usr/lib/swift) via rpath.
let librariesRequireRPath: Bool
}
@_spi(Testing) public struct Paths: Codable {
/// The path to the SDK, if provided.
public let sdkPath: TextualVirtualPath?
public let runtimeLibraryPaths: [TextualVirtualPath]
public let runtimeLibraryImportPaths: [TextualVirtualPath]
public let runtimeResourcePath: TextualVirtualPath
}
var compilerVersion: String
var target: Target
var targetVariant: Target?
let paths: Paths
}
// Make members of `FrontendTargetInfo.Paths` accessible on `FrontendTargetInfo`.
extension FrontendTargetInfo {
@_spi(Testing) public subscript<T>(dynamicMember dynamicMember: KeyPath<FrontendTargetInfo.Paths, T>) -> T {
self.paths[keyPath: dynamicMember]
}
}
extension Toolchain {
@_spi(Testing) public func printTargetInfoJob(target: Triple?,
targetVariant: Triple?,
sdkPath: VirtualPath? = nil,
resourceDirPath: VirtualPath? = nil,
runtimeCompatibilityVersion: String? = nil,
requiresInPlaceExecution: Bool = false,
useStaticResourceDir: Bool = false,
swiftCompilerPrefixArgs: [String]) throws -> Job {
var commandLine: [Job.ArgTemplate] = swiftCompilerPrefixArgs.map { Job.ArgTemplate.flag($0) }
commandLine.append(contentsOf: [.flag("-frontend"),
.flag("-print-target-info")])
// If we were given a target, include it. Otherwise, let the frontend
// tell us the host target.
if let target = target {
commandLine += [.flag("-target"), .flag(target.triple)]
}
// If there is a target variant, include that too.
if let targetVariant = targetVariant {
commandLine += [.flag("-target-variant"), .flag(targetVariant.triple)]
}
if let sdkPath = sdkPath {
commandLine += [.flag("-sdk"), .path(sdkPath)]
}
if let resourceDirPath = resourceDirPath {
commandLine += [.flag("-resource-dir"), .path(resourceDirPath)]
}
if let runtimeCompatibilityVersion = runtimeCompatibilityVersion {
commandLine += [
.flag("-runtime-compatibility-version"),
.flag(runtimeCompatibilityVersion)
]
}
if useStaticResourceDir {
commandLine += [.flag("-use-static-resource-dir")]
}
return Job(
moduleName: "",
kind: .printTargetInfo,
tool: try resolvedTool(.swiftCompiler),
commandLine: commandLine,
displayInputs: [],
inputs: [],
primaryInputs: [],
outputs: [.init(file: .standardOutput, type: .jsonTargetInfo)],
requiresInPlaceExecution: requiresInPlaceExecution
)
}
}
extension Driver {
@_spi(Testing) public static func queryTargetInfoInProcess(of toolchain: Toolchain,
fileSystem: FileSystem,
workingDirectory: AbsolutePath?,
invocationCommand: [String]) throws -> FrontendTargetInfo? {
let optionalSwiftScanLibPath = try toolchain.lookupSwiftScanLib()
if let swiftScanLibPath = optionalSwiftScanLibPath,
fileSystem.exists(swiftScanLibPath) {
let libSwiftScanInstance = try SwiftScan(dylib: swiftScanLibPath)
if libSwiftScanInstance.canQueryTargetInfo() {
let cwd = try workingDirectory ?? fileSystem.currentWorkingDirectory ?? fileSystem.tempDirectory
let compilerExecutablePath = try toolchain.resolvedTool(.swiftCompiler).path
let targetInfoData =
try libSwiftScanInstance.queryTargetInfoJSON(workingDirectory: cwd,
compilerExecutablePath: compilerExecutablePath,
invocationCommand: invocationCommand)
do {
return try JSONDecoder().decode(FrontendTargetInfo.self, from: targetInfoData)
} catch let decodingError as DecodingError {
let stringToDecode = String(data: targetInfoData, encoding: .utf8)
let errorDesc: String
switch decodingError {
case let .typeMismatch(type, context):
errorDesc = "type mismatch: \(type), path: \(context.codingPath)"
case let .valueNotFound(type, context):
errorDesc = "value missing: \(type), path: \(context.codingPath)"
case let .keyNotFound(key, context):
errorDesc = "key missing: \(key), path: \(context.codingPath)"
case let .dataCorrupted(context):
errorDesc = "data corrupted at path: \(context.codingPath)"
@unknown default:
errorDesc = "unknown decoding error"
}
throw Error.unableToDecodeFrontendTargetInfo(
stringToDecode,
invocationCommand,
errorDesc)
}
}
}
return nil
}
static func computeTargetInfo(target: Triple?,
targetVariant: Triple?,
sdkPath: VirtualPath? = nil,
resourceDirPath: VirtualPath? = nil,
runtimeCompatibilityVersion: String? = nil,
requiresInPlaceExecution: Bool = false,
useStaticResourceDir: Bool = false,
swiftCompilerPrefixArgs: [String],
toolchain: Toolchain,
fileSystem: FileSystem,
workingDirectory: AbsolutePath?,
diagnosticsEngine: DiagnosticsEngine,
executor: DriverExecutor) throws -> FrontendTargetInfo {
let frontendTargetInfoJob =
try toolchain.printTargetInfoJob(target: target, targetVariant: targetVariant,
sdkPath: sdkPath, resourceDirPath: resourceDirPath,
runtimeCompatibilityVersion: runtimeCompatibilityVersion,
requiresInPlaceExecution: requiresInPlaceExecution,
useStaticResourceDir: useStaticResourceDir,
swiftCompilerPrefixArgs: swiftCompilerPrefixArgs)
var command = try Self.itemizedJobCommand(of: frontendTargetInfoJob,
useResponseFiles: .disabled,
using: executor.resolver)
Self.sanitizeCommandForLibScanInvocation(&command)
// Disable in-process target query due to a race condition in the compiler's current
// build system where libSwiftScan may not be ready when building the Swift standard library.
// do {
// if let targetInfo =
// try Self.queryTargetInfoInProcess(of: toolchain, fileSystem: fileSystem,
// workingDirectory: workingDirectory,
// invocationCommand: command) {
// return targetInfo
// }
// } catch {
// diagnosticsEngine.emit(.remark_inprocess_target_info_query_failed(error.localizedDescription))
// }
// Fallback: Invoke `swift-frontend -print-target-info` and decode the output
return try executor.execute(
job: frontendTargetInfoJob,
capturingJSONOutputAs: FrontendTargetInfo.self,
forceResponseFiles: false,
recordedInputModificationDates: [:])
}
}
|