Skip to content
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
12 changes: 10 additions & 2 deletions Sources/SwiftDriver/Jobs/AutolinkExtractJob.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,20 @@ extension Driver {
}

mutating func autolinkExtractJob(inputs: [TypedVirtualPath]) throws -> Job? {
guard inputs.count > 0 && isAutolinkExtractJobNeeded else {
guard let firstInput = inputs.first, isAutolinkExtractJobNeeded else {
return nil
}

var commandLine = [Job.ArgTemplate]()
let output = VirtualPath.temporary(RelativePath("\(moduleOutputInfo.name).autolink"))
// Put output in same place as first .o, following legacy driver.
// (See `constructInvocation(const AutolinkExtractJobAction` in `UnixToolChains.cpp`.)
let outputBasename = "\(moduleOutputInfo.name).autolink"
let dir = firstInput.file.parentDirectory
// Go through a bit of extra rigmarole to keep the "./" out of the name for
// the sake of the tests.
let output: VirtualPath = dir == .temporary(RelativePath("."))
? .temporary(RelativePath(outputBasename))
: dir.appending(component: outputBasename)

commandLine.append(contentsOf: inputs.map { .path($0.file) })
commandLine.appendFlag(.o)
Expand Down
44 changes: 35 additions & 9 deletions Tests/SwiftDriverTests/IncrementalCompilationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -190,17 +190,18 @@ final class NonincrementalCompilationTests: XCTestCase {

XCTAssertEqual(try! buildRecord.inputInfos[VirtualPath(path: file2 )]!.status,
.needsCascadingBuild)
XCTAssert(try! isCloseEnough(buildRecord.inputInfos[VirtualPath(path: file2 )]!
.previousModTime.legacyDriverSecsAndNanos,
[1570318778, 0]))
XCTAssertEqual(try! buildRecord.inputInfos[VirtualPath(path: gazorp)]!.status,
XCTAssert(try! isCloseEnough(
XCTUnwrap(buildRecord.inputInfos[VirtualPath(path: file2 )])
.previousModTime.legacyDriverSecsAndNanos,
[1570318778, 0]))
XCTAssertEqual(try! XCTUnwrap(buildRecord.inputInfos[VirtualPath(path: gazorp)]).status,
.needsNonCascadingBuild)
XCTAssertEqual(try! buildRecord.inputInfos[VirtualPath(path: gazorp)]!
XCTAssertEqual(try! XCTUnwrap(buildRecord.inputInfos[VirtualPath(path: gazorp)])
.previousModTime.legacyDriverSecsAndNanos,
[0, 0])
XCTAssertEqual(try! buildRecord.inputInfos[VirtualPath(path: main )]!.status,
XCTAssertEqual(try! XCTUnwrap(buildRecord.inputInfos[VirtualPath(path: main )]).status,
.upToDate)
XCTAssert(try! isCloseEnough( buildRecord.inputInfos[VirtualPath(path: main )]!
XCTAssert(try! isCloseEnough( XCTUnwrap(buildRecord.inputInfos[VirtualPath(path: main )])
.previousModTime.legacyDriverSecsAndNanos,
[1570083660, 0]))

Expand Down Expand Up @@ -488,13 +489,13 @@ final class IncrementalCompilationTests: XCTestCase {

func touch(_ name: String) {
print("*** touching \(name) ***", to: &stderrStream); stderrStream.flush()
let (path, contents) = inputPathsAndContents.filter {$0.0.pathString.contains(name)}.first!
let (path, contents) = try! XCTUnwrap(inputPathsAndContents.filter {$0.0.pathString.contains(name)}.first)
try! localFileSystem.writeFileContents(path) { $0 <<< contents }
}

private func replace(contentsOf name: String, with replacement: String ) {
print("*** replacing \(name) ***", to: &stderrStream); stderrStream.flush()
let path = inputPathsAndContents.filter {$0.0.pathString.contains("/" + name + ".swift")}.first!.0
let path = try! XCTUnwrap(inputPathsAndContents.filter {$0.0.pathString.contains("/" + name + ".swift")}.first).0
let previousContents = try! localFileSystem.readFileContents(path).cString
try! localFileSystem.writeFileContents(path) { $0 <<< replacement }
let newContents = try! localFileSystem.readFileContents(path).cString
Expand Down Expand Up @@ -545,6 +546,31 @@ final class IncrementalCompilationTests: XCTestCase {
print("", to: &stderrStream); stderrStream.flush()
}

/// Ensure that autolink output file goes with .o directory, to not prevent incremental omission of
/// autolink job.
/// Much of the code below is taking from testLinking(), but uses the output file map code here.
func testAutolinkOutputPath() {
var env = ProcessEnv.vars
env["SWIFT_DRIVER_TESTS_ENABLE_EXEC_PATH_FALLBACK"] = "1"
env["SWIFT_DRIVER_SWIFT_AUTOLINK_EXTRACT_EXEC"] = "/garbage/swift-autolink-extract"
env["SWIFT_DRIVER_DSYMUTIL_EXEC"] = "/garbage/dsymutil"

var driver = try! Driver(
args: args
+ ["-emit-library", "-target", "x86_64-unknown-linux"],
env: env)
let plannedJobs = try! driver.planBuild()
let autolinkExtractJob = try! XCTUnwrap(
plannedJobs
.filter { $0.kind == .autolinkExtract }
.first)
Comment on lines +565 to +566
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is test code so it doesn't really matter, but I like when people point things like this out to me.

You can use first(where:) https://developer.apple.com/documentation/swift/array/1848165-first to avoid iterating the whole array in cases like this, instead of doing filter then first.

let autoOuts = autolinkExtractJob.outputs.filter {$0.type == .autolink}
XCTAssertEqual(autoOuts.count, 1)
let autoOut = autoOuts[0]
let expected = AbsolutePath(derivedDataPath, "\(module).autolink")
XCTAssertEqual(autoOut.file.absolutePath, expected)
}

private func generateOutputFileMapDict(module: String, inputPaths: [AbsolutePath],
derivedData: AbsolutePath
) -> [String: [String: String]] {
Expand Down