Skip to content

Add support for loading supported features from share/swift/features.json #845

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 22, 2021
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
13 changes: 12 additions & 1 deletion Sources/SwiftDriver/Driver/Driver.swift
Original file line number Diff line number Diff line change
Expand Up @@ -330,9 +330,15 @@ public struct Driver {
/// A collection of all the flags the selected toolchain's `swift-frontend` supports
public let supportedFrontendFlags: Set<String>

/// A collection of all the features the selected toolchain's `swift-frontend` supports
public let supportedFrontendFeatures: Set<String>

/// A global queue for emitting non-interrupted messages into stderr
public static let stdErrQueue = DispatchQueue(label: "org.swift.driver.emit-to-stderr")

enum KnownCompilerFeature: String {
case emit_abi_descriptor = "emit-abi-descriptor"
}

lazy var sdkPath: VirtualPath? = {
guard let rawSdkPath = frontendTargetInfo.sdkPath?.path else {
Expand Down Expand Up @@ -364,6 +370,10 @@ public struct Driver {
}
}

func isFeatureSupported(_ feature: KnownCompilerFeature) -> Bool {
return supportedFrontendFeatures.contains(feature.rawValue)
}

/// Handler for emitting diagnostics to stderr.
public static let stderrDiagnosticsHandler: DiagnosticsEngine.DiagnosticsHandler = { diagnostic in
stdErrQueue.sync {
Expand Down Expand Up @@ -606,11 +616,12 @@ public struct Driver {
outputFileMap: outputFileMap)

self.supportedFrontendFlags =
try Self.computeSupportedCompilerFeatures(of: self.toolchain, hostTriple: self.hostTriple,
try Self.computeSupportedCompilerArgs(of: self.toolchain, hostTriple: self.hostTriple,
parsedOptions: &self.parsedOptions,
diagnosticsEngine: diagnosticEngine,
fileSystem: fileSystem, executor: executor,
env: env)
self.supportedFrontendFeatures = try Self.computeSupportedCompilerFeatures(of: self.toolchain, env: env)

self.enabledSanitizers = try Self.parseSanitizerArgValues(
&parsedOptions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ public extension Driver {
.appending(component: libScanner)
}

fileprivate static func getRootPath(of toolchain: Toolchain, env: [String: String])
static func getRootPath(of toolchain: Toolchain, env: [String: String])
throws -> AbsolutePath {
if let overrideString = env["SWIFT_DRIVER_SWIFT_SCAN_TOOLCHAIN_PATH"] {
return try AbsolutePath(validating: overrideString)
Expand Down
8 changes: 6 additions & 2 deletions Sources/SwiftDriver/Jobs/EmitModuleJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,13 @@ extension Driver {
commandLine.appendFlag(.parseAsLibrary)
}

let outputPath = VirtualPath.lookup(moduleOutputPath)
commandLine.appendFlag(.o)
commandLine.appendPath(VirtualPath.lookup(moduleOutputPath))

commandLine.appendPath(outputPath)
if isFeatureSupported(.emit_abi_descriptor) {
commandLine.appendFlag(.emitAbiDescriptorPath)
commandLine.appendPath(outputPath.replacingExtension(with: .jsonABIBaseline))
}
return Job(
moduleName: moduleOutputInfo.name,
kind: .emitModule,
Expand Down
23 changes: 22 additions & 1 deletion Sources/SwiftDriver/Jobs/EmitSupportedFeaturesJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

import TSCBasic
import SwiftOptions
import Foundation

/// Describes information about the compiler's supported arguments and features
@_spi(Testing) public struct SupportedCompilerFeatures: Codable {
Expand Down Expand Up @@ -52,7 +53,7 @@ extension Toolchain {
}

extension Driver {
static func computeSupportedCompilerFeatures(of toolchain: Toolchain, hostTriple: Triple,
static func computeSupportedCompilerArgs(of toolchain: Toolchain, hostTriple: Triple,
parsedOptions: inout ParsedOptions,
diagnosticsEngine: DiagnosticsEngine,
fileSystem: FileSystem,
Expand Down Expand Up @@ -85,4 +86,24 @@ extension Driver {
recordedInputModificationDates: [:]).SupportedArguments
return Set(decodedSupportedFlagList)
}

static func computeSupportedCompilerFeatures(of toolchain: Toolchain,
env: [String: String]) throws -> Set<String> {
struct FeatureInfo: Codable {
var name: String
}
struct FeatureList: Codable {
var features: [FeatureInfo]
}
let jsonPath = try getRootPath(of: toolchain, env: env)
.appending(component: "share")
.appending(component: "swift")
.appending(component: "features.json")
guard localFileSystem.exists(jsonPath) else {
return Set<String>()
}
let content = try localFileSystem.readFileContents(jsonPath)
let result = try JSONDecoder().decode(FeatureList.self, from: Data(content.contents))
return Set(result.features.map {$0.name})
}
}
7 changes: 6 additions & 1 deletion Sources/SwiftDriver/Jobs/MergeModuleJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,14 @@ extension Driver {
try commandLine.appendLast(.disableIncrementalImports, from: &parsedOptions)
}

let outputPath = VirtualPath.lookup(moduleOutputInfo.output!.outputPath)
commandLine.appendFlag(.o)
commandLine.appendPath(VirtualPath.lookup(moduleOutputInfo.output!.outputPath))
commandLine.appendPath(outputPath)

if isFeatureSupported(.emit_abi_descriptor) {
commandLine.appendFlag(.emitAbiDescriptorPath)
commandLine.appendPath(outputPath.replacingExtension(with: .jsonABIBaseline))
}
return Job(
moduleName: moduleOutputInfo.name,
kind: .mergeModule,
Expand Down
6 changes: 6 additions & 0 deletions Tests/SwiftDriverTests/SwiftDriverTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5015,6 +5015,12 @@ final class SwiftDriverTests: XCTestCase {
}
}

func testSupportedFeatureJson() throws {
let driver = try Driver(args: ["swiftc", "-emit-module", "foo.swift"])
XCTAssertFalse(driver.supportedFrontendFeatures.isEmpty)
XCTAssertTrue(driver.supportedFrontendFeatures.contains("experimental-skip-all-function-bodies"))
}

func testFilelist() throws {
do {
var driver = try Driver(args: ["swiftc", "-emit-module", "./a.swift", "./b.swift", "./c.swift", "-module-name", "main", "-target", "x86_64-apple-macosx10.9", "-driver-filelist-threshold=0", "-no-emit-module-separately"])
Expand Down