Skip to content

Commit f563677

Browse files
authored
Merge pull request #1260 from apple/revert-1253-RefactorLibSwiftScanLookup
Revert "[Dependency Scanning] Move computation of the path for libSwiftScan to the toolchain"
2 parents e5f8393 + 43bd743 commit f563677

File tree

8 files changed

+90
-102
lines changed

8 files changed

+90
-102
lines changed

Sources/SwiftDriver/Driver/Driver.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -650,11 +650,11 @@ public struct Driver {
650650
outputFileMap: outputFileMap)
651651

652652
self.supportedFrontendFlags =
653-
try Self.computeSupportedCompilerArgs(of: self.toolchain,
654-
parsedOptions: &self.parsedOptions,
655-
diagnosticsEngine: diagnosticEngine,
656-
fileSystem: fileSystem,
657-
executor: executor)
653+
try Self.computeSupportedCompilerArgs(of: self.toolchain, hostTriple: self.hostTriple,
654+
parsedOptions: &self.parsedOptions,
655+
diagnosticsEngine: diagnosticEngine,
656+
fileSystem: fileSystem, executor: executor,
657+
env: env)
658658
let supportedFrontendFlagsLocal = self.supportedFrontendFlags
659659
self.savedUnknownDriverFlagsForSwiftFrontend = try self.parsedOptions.saveUnknownFlags {
660660
Driver.isOptionFound($0, allOpts: supportedFrontendFlagsLocal)

Sources/SwiftDriver/Driver/WindowsExtensions.swift

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,27 +20,3 @@ internal func executableName(_ name: String) -> String {
2020
return name
2121
#endif
2222
}
23-
24-
@_spi(Testing) public func sharedLibraryName(_ name: String) -> String {
25-
#if canImport(Darwin)
26-
let ext = ".dylib"
27-
#elseif os(Windows)
28-
let ext = ".dll"
29-
#else
30-
let ext = ".so"
31-
#endif
32-
return name + ext
33-
}
34-
35-
// FIXME: This can be subtly wrong, we should rather
36-
// try to get the client to provide this info or move to a better
37-
// path convention for where we keep compiler support libraries
38-
internal var compilerHostSupportLibraryOSComponent : String {
39-
#if canImport(Darwin)
40-
return "macosx"
41-
#elseif os(Windows)
42-
return "windows"
43-
#else
44-
return "linux"
45-
#endif
46-
}

Sources/SwiftDriver/ExplicitModuleBuilds/ModuleDependencyScanning.swift

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ public extension Driver {
221221
// attempt to fallback to using `swift-frontend -scan-dependencies` invocations for dependency
222222
// scanning.
223223
var fallbackToFrontend = parsedOptions.hasArgument(.driverScanDependenciesNonLib)
224-
let scanLibPath = try toolchain.lookupSwiftScanLib()
224+
let scanLibPath = try Self.getScanLibPath(of: toolchain, hostTriple: hostTriple, env: env)
225225
if try interModuleDependencyOracle
226226
.verifyOrCreateScannerInstance(fileSystem: fileSystem,
227227
swiftScanLibPath: scanLibPath) == false {
@@ -490,9 +490,46 @@ public extension Driver {
490490
useResponseFiles: useResponseFiles)
491491
return args
492492
}
493+
}
494+
495+
@_spi(Testing) public extension Driver {
496+
static func getScanLibPath(of toolchain: Toolchain, hostTriple: Triple,
497+
env: [String: String]) throws -> AbsolutePath {
498+
if hostTriple.isWindows {
499+
// no matter if we are in a build tree or an installed tree, the layout is
500+
// always: `bin/_InternalSwiftScan.dll`
501+
return try getRootPath(of: toolchain, env: env)
502+
.appending(component: "bin")
503+
.appending(component: "_InternalSwiftScan.dll")
504+
}
505+
506+
let sharedLibExt: String
507+
if hostTriple.isMacOSX {
508+
sharedLibExt = ".dylib"
509+
} else {
510+
sharedLibExt = ".so"
511+
}
512+
let libScanner = "lib_InternalSwiftScan\(sharedLibExt)"
513+
// We first look into position in toolchain
514+
let libPath
515+
= try getRootPath(of: toolchain, env: env).appending(component: "lib")
516+
.appending(component: "swift")
517+
.appending(component: hostTriple.osNameUnversioned)
518+
.appending(component: libScanner)
519+
if localFileSystem.exists(libPath) {
520+
return libPath
521+
}
522+
// In case we are using a compiler from the build dir, we should also try
523+
// this path.
524+
return try getRootPath(of: toolchain, env: env).appending(component: "lib")
525+
.appending(component: libScanner)
526+
}
493527

494528
static func getRootPath(of toolchain: Toolchain, env: [String: String])
495529
throws -> AbsolutePath {
530+
if let overrideString = env["SWIFT_DRIVER_SWIFT_SCAN_TOOLCHAIN_PATH"] {
531+
return try AbsolutePath(validating: overrideString)
532+
}
496533
return try toolchain.getToolPath(.swiftCompiler)
497534
.parentDirectory // bin
498535
.parentDirectory // toolchain root

Sources/SwiftDriver/Jobs/EmitSupportedFeaturesJob.swift

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,14 @@ extension Toolchain {
5757
}
5858

5959
extension Driver {
60-
61-
static func computeSupportedCompilerArgs(of toolchain: Toolchain,
60+
static func computeSupportedCompilerArgs(of toolchain: Toolchain, hostTriple: Triple,
6261
parsedOptions: inout ParsedOptions,
6362
diagnosticsEngine: DiagnosticsEngine,
6463
fileSystem: FileSystem,
65-
executor: DriverExecutor)
64+
executor: DriverExecutor, env: [String: String])
6665
throws -> Set<String> {
67-
if let supportedArgs =
68-
try querySupportedCompilerArgsInProcess(of: toolchain, fileSystem: fileSystem) {
66+
if let supportedArgs = try querySupportedCompilerArgsInProcess(of: toolchain, hostTriple: hostTriple,
67+
fileSystem: fileSystem, env: env) {
6968
return supportedArgs
7069
}
7170

@@ -85,9 +84,13 @@ extension Driver {
8584
}
8685

8786
static func querySupportedCompilerArgsInProcess(of toolchain: Toolchain,
88-
fileSystem: FileSystem)
87+
hostTriple: Triple,
88+
fileSystem: FileSystem,
89+
env: [String: String])
8990
throws -> Set<String>? {
90-
let swiftScanLibPath = try toolchain.lookupSwiftScanLib()
91+
let swiftScanLibPath = try Self.getScanLibPath(of: toolchain,
92+
hostTriple: hostTriple,
93+
env: env)
9194
if fileSystem.exists(swiftScanLibPath) {
9295
let libSwiftScanInstance = try SwiftScan(dylib: swiftScanLibPath)
9396
if libSwiftScanInstance.canQuerySupportedArguments() {

Sources/SwiftDriver/SwiftScan/SwiftScan.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -317,10 +317,14 @@ internal final class SwiftScan {
317317
@_spi(Testing) public extension Driver {
318318
func querySupportedArgumentsForTest() throws -> Set<String>? {
319319
// If a capable libSwiftScan is found, manually ensure we can get the supported arguments
320-
let scanLibPath = try toolchain.lookupSwiftScanLib()
321-
let libSwiftScanInstance = try SwiftScan(dylib: scanLibPath)
322-
if libSwiftScanInstance.canQuerySupportedArguments() {
323-
return try libSwiftScanInstance.querySupportedArguments()
320+
let scanLibPath = try Self.getScanLibPath(of: toolchain,
321+
hostTriple: hostTriple,
322+
env: env)
323+
if fileSystem.exists(scanLibPath) {
324+
let libSwiftScanInstance = try SwiftScan(dylib: scanLibPath)
325+
if libSwiftScanInstance.canQuerySupportedArguments() {
326+
return try libSwiftScanInstance.querySupportedArguments()
327+
}
324328
}
325329
return nil
326330
}

Sources/SwiftDriver/Toolchains/Toolchain.swift

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -245,46 +245,6 @@ extension Toolchain {
245245
}
246246
}
247247

248-
/// Looks for the executable in the `SWIFT_DRIVER_SWIFTSCAN_LIB` environment variable, if found nothing,
249-
/// looks in the `lib` relative to the compiler executable.
250-
/// TODO: If the driver needs to lookup other shared libraries, this is simple to generalize
251-
@_spi(Testing) public func lookupSwiftScanLib() throws -> AbsolutePath {
252-
#if os(Windows)
253-
// no matter if we are in a build tree or an installed tree, the layout is
254-
// always: `bin/_InternalSwiftScan.dll`
255-
return try toolchain.getToolPath(.swiftCompiler)
256-
.parentDirectory // bin
257-
.appending(component: "_InternalSwiftScan.dll")
258-
#else
259-
let libraryName = sharedLibraryName("lib_InternalSwiftScan")
260-
if let overrideString = env["SWIFT_DRIVER_SWIFTSCAN_LIB"],
261-
let path = try? AbsolutePath(validating: overrideString) {
262-
return path
263-
} else {
264-
let compilerPath = try getToolPath(.swiftCompiler)
265-
let toolchainRootPath = compilerPath.parentDirectory // bin
266-
.parentDirectory // toolchain root
267-
268-
let searchPaths = [toolchainRootPath.appending(component: "lib")
269-
.appending(component: "swift")
270-
.appending(component: compilerHostSupportLibraryOSComponent),
271-
toolchainRootPath.appending(component: "lib")
272-
.appending(component: "swift")
273-
.appending(component: "host"),
274-
// In case we are using a compiler from the build dir, we should also try
275-
// this path.
276-
toolchainRootPath.appending(component: "lib")]
277-
for libraryPath in searchPaths.map({ $0.appending(component: libraryName) }) {
278-
if fileSystem.isFile(libraryPath) {
279-
return libraryPath
280-
}
281-
}
282-
}
283-
284-
throw ToolchainError.unableToFind(tool: libraryName)
285-
#endif
286-
}
287-
288248
private func xcrunFind(executable: String) throws -> AbsolutePath {
289249
let xcrun = "xcrun"
290250
guard lookupExecutablePath(filename: xcrun, searchPaths: searchPaths) != nil else {

Tests/SwiftDriverTests/ExplicitModuleBuildTests.swift

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ final class ExplicitModuleBuildTests: XCTestCase {
648648
}
649649

650650
func testModuleAliasingWithImportPrescan() throws {
651-
let (_, _, toolchain, _) = try getDriverArtifactsForScanning()
651+
let (_, _, toolchain, hostTriple) = try getDriverArtifactsForScanning()
652652

653653
let dummyDriver = try Driver(args: ["swiftc", "-module-name", "dummyDriverCheck", "test.swift"])
654654
guard dummyDriver.isFrontendArgSupported(.moduleAlias) else {
@@ -658,7 +658,9 @@ final class ExplicitModuleBuildTests: XCTestCase {
658658
// The dependency oracle wraps an instance of libSwiftScan and ensures thread safety across
659659
// queries.
660660
let dependencyOracle = InterModuleDependencyOracle()
661-
let scanLibPath = try toolchain.lookupSwiftScanLib()
661+
let scanLibPath = try Driver.getScanLibPath(of: toolchain,
662+
hostTriple: hostTriple,
663+
env: ProcessEnv.vars)
662664
guard try dependencyOracle
663665
.verifyOrCreateScannerInstance(fileSystem: localFileSystem,
664666
swiftScanLibPath: scanLibPath) else {
@@ -838,7 +840,9 @@ final class ExplicitModuleBuildTests: XCTestCase {
838840

839841
// 2. Run a dependency scan to find the just-built module
840842
let dependencyOracle = InterModuleDependencyOracle()
841-
let scanLibPath = try toolchain.lookupSwiftScanLib()
843+
let scanLibPath = try Driver.getScanLibPath(of: toolchain,
844+
hostTriple: hostTriple,
845+
env: ProcessEnv.vars)
842846
guard try dependencyOracle
843847
.verifyOrCreateScannerInstance(fileSystem: localFileSystem,
844848
swiftScanLibPath: scanLibPath) else {
@@ -935,12 +939,14 @@ final class ExplicitModuleBuildTests: XCTestCase {
935939

936940
/// Test the libSwiftScan dependency scanning (import-prescan).
937941
func testDependencyImportPrescan() throws {
938-
let (stdLibPath, shimsPath, toolchain, _) = try getDriverArtifactsForScanning()
942+
let (stdLibPath, shimsPath, toolchain, hostTriple) = try getDriverArtifactsForScanning()
939943

940944
// The dependency oracle wraps an instance of libSwiftScan and ensures thread safety across
941945
// queries.
942946
let dependencyOracle = InterModuleDependencyOracle()
943-
let scanLibPath = try toolchain.lookupSwiftScanLib()
947+
let scanLibPath = try Driver.getScanLibPath(of: toolchain,
948+
hostTriple: hostTriple,
949+
env: ProcessEnv.vars)
944950
guard try dependencyOracle
945951
.verifyOrCreateScannerInstance(fileSystem: localFileSystem,
946952
swiftScanLibPath: scanLibPath) else {
@@ -1014,12 +1020,14 @@ final class ExplicitModuleBuildTests: XCTestCase {
10141020
}
10151021

10161022
func testDependencyScanningFailure() throws {
1017-
let (stdlibPath, shimsPath, toolchain, _) = try getDriverArtifactsForScanning()
1023+
let (stdlibPath, shimsPath, toolchain, hostTriple) = try getDriverArtifactsForScanning()
10181024

10191025
// The dependency oracle wraps an instance of libSwiftScan and ensures thread safety across
10201026
// queries.
10211027
let dependencyOracle = InterModuleDependencyOracle()
1022-
let scanLibPath = try toolchain.lookupSwiftScanLib()
1028+
let scanLibPath = try Driver.getScanLibPath(of: toolchain,
1029+
hostTriple: hostTriple,
1030+
env: ProcessEnv.vars)
10231031
guard try dependencyOracle
10241032
.verifyOrCreateScannerInstance(fileSystem: localFileSystem,
10251033
swiftScanLibPath: scanLibPath) else {
@@ -1090,7 +1098,9 @@ final class ExplicitModuleBuildTests: XCTestCase {
10901098
// The dependency oracle wraps an instance of libSwiftScan and ensures thread safety across
10911099
// queries.
10921100
let dependencyOracle = InterModuleDependencyOracle()
1093-
let scanLibPath = try toolchain.lookupSwiftScanLib()
1101+
let scanLibPath = try Driver.getScanLibPath(of: toolchain,
1102+
hostTriple: hostTriple,
1103+
env: ProcessEnv.vars)
10941104
guard try dependencyOracle
10951105
.verifyOrCreateScannerInstance(fileSystem: localFileSystem,
10961106
swiftScanLibPath: scanLibPath) else {
@@ -1283,9 +1293,11 @@ final class ExplicitModuleBuildTests: XCTestCase {
12831293
}
12841294

12851295
func testDependencyGraphDotSerialization() throws {
1286-
let (stdlibPath, shimsPath, toolchain, _) = try getDriverArtifactsForScanning()
1296+
let (stdlibPath, shimsPath, toolchain, hostTriple) = try getDriverArtifactsForScanning()
12871297
let dependencyOracle = InterModuleDependencyOracle()
1288-
let scanLibPath = try toolchain.lookupSwiftScanLib()
1298+
let scanLibPath = try Driver.getScanLibPath(of: toolchain,
1299+
hostTriple: hostTriple,
1300+
env: ProcessEnv.vars)
12891301
guard try dependencyOracle
12901302
.verifyOrCreateScannerInstance(fileSystem: localFileSystem,
12911303
swiftScanLibPath: scanLibPath) else {
@@ -1346,7 +1358,7 @@ final class ExplicitModuleBuildTests: XCTestCase {
13461358

13471359
/// Test the libSwiftScan dependency scanning.
13481360
func testDependencyScanReuseCache() throws {
1349-
let (stdlibPath, shimsPath, toolchain, _) = try getDriverArtifactsForScanning()
1361+
let (stdlibPath, shimsPath, toolchain, hostTriple) = try getDriverArtifactsForScanning()
13501362
try withTemporaryDirectory { path in
13511363
let cacheSavePath = path.appending(component: "saved.moddepcache")
13521364
let main = path.appending(component: "testDependencyScanning.swift")
@@ -1382,7 +1394,9 @@ final class ExplicitModuleBuildTests: XCTestCase {
13821394
scannerCommand.removeFirst()
13831395
}
13841396

1385-
let scanLibPath = try toolchain.lookupSwiftScanLib()
1397+
let scanLibPath = try Driver.getScanLibPath(of: toolchain,
1398+
hostTriple: hostTriple,
1399+
env: ProcessEnv.vars)
13861400
// Run the first scan and serialize the cache contents.
13871401
let firstDependencyOracle = InterModuleDependencyOracle()
13881402
guard try firstDependencyOracle

Tests/SwiftDriverTests/SwiftDriverTests.swift

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6685,8 +6685,6 @@ final class SwiftDriverTests: XCTestCase {
66856685
let PATH = "PATH"
66866686
#endif
66876687
let SWIFT_FRONTEND_EXEC = "SWIFT_DRIVER_SWIFT_FRONTEND_EXEC"
6688-
let SWIFT_SCANNER_LIB = "SWIFT_DRIVER_SWIFTSCAN_LIB"
6689-
66906688

66916689
// Reset the environment to ensure tool resolution is exactly run against PATH.
66926690
var driver = try Driver(args: ["swiftc", "-print-target-info"], env: [PATH: ProcessEnv.path!])
@@ -6697,7 +6695,6 @@ final class SwiftDriverTests: XCTestCase {
66976695

66986696
try withTemporaryDirectory { toolsDirectory in
66996697
let customSwiftFrontend = toolsDirectory.appending(component: executableName("swift-frontend"))
6700-
let customSwiftScan = toolsDirectory.appending(component: sharedLibraryName("lib_InternalSwiftScan"))
67016698
try localFileSystem.createSymbolicLink(customSwiftFrontend, pointingAt: defaultSwiftFrontend, relative: false)
67026699

67036700
try withTemporaryDirectory { tempDirectory in
@@ -6710,9 +6707,7 @@ final class SwiftDriverTests: XCTestCase {
67106707
// test if SWIFT_DRIVER_TOOLNAME_EXEC is respected
67116708
do {
67126709
var driver = try Driver(args: ["swiftc", "-print-target-info"],
6713-
env: [PATH: ProcessEnv.path!,
6714-
SWIFT_FRONTEND_EXEC: customSwiftFrontend.pathString,
6715-
SWIFT_SCANNER_LIB: customSwiftScan.pathString])
6710+
env: [PATH: ProcessEnv.path!, SWIFT_FRONTEND_EXEC: customSwiftFrontend.pathString])
67166711
let jobs = try driver.planBuild()
67176712
XCTAssertEqual(jobs.count, 1)
67186713
XCTAssertEqual(jobs.first!.tool.name, customSwiftFrontend.pathString)
@@ -6721,16 +6716,15 @@ final class SwiftDriverTests: XCTestCase {
67216716
// test if tools directory is respected
67226717
do {
67236718
var driver = try Driver(args: ["swiftc", "-print-target-info", "-tools-directory", toolsDirectory.pathString],
6724-
env: [PATH: ProcessEnv.path!, SWIFT_SCANNER_LIB: customSwiftScan.pathString])
6719+
env: [PATH: ProcessEnv.path!])
67256720
let jobs = try driver.planBuild()
67266721
XCTAssertEqual(jobs.count, 1)
67276722
XCTAssertEqual(jobs.first!.tool.name, customSwiftFrontend.pathString)
67286723
}
67296724

67306725
// test if current working directory is searched before PATH
67316726
do {
6732-
var driver = try Driver(args: ["swiftc", "-print-target-info"],
6733-
env: [PATH: toolsDirectory.pathString, SWIFT_SCANNER_LIB: customSwiftScan.pathString])
6727+
var driver = try Driver(args: ["swiftc", "-print-target-info"], env: [PATH: toolsDirectory.pathString])
67346728
let jobs = try driver.planBuild()
67356729
XCTAssertEqual(jobs.count, 1)
67366730
XCTAssertEqual(jobs.first!.tool.name, anotherSwiftFrontend.pathString)

0 commit comments

Comments
 (0)