Skip to content

Commit 0edc9d3

Browse files
author
David Ungar
authored
Merge pull request #519 from davidungar/just-reorg-2
2 parents b998272 + 3124790 commit 0edc9d3

File tree

2 files changed

+132
-100
lines changed

2 files changed

+132
-100
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//===--------------- OutputFileMapCreator.swift - Swift Testing -----------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
import XCTest
13+
import TSCBasic
14+
15+
import Foundation
16+
17+
struct OutputFileMapCreator {
18+
private let module: String
19+
private let inputPaths: [AbsolutePath]
20+
private let derivedData: AbsolutePath
21+
22+
private init(module: String, inputPaths: [AbsolutePath], derivedData: AbsolutePath) {
23+
self.module = module
24+
self.inputPaths = inputPaths
25+
self.derivedData = derivedData
26+
}
27+
28+
static func write(module: String,
29+
inputPaths: [AbsolutePath],
30+
derivedData: AbsolutePath,
31+
to dst: AbsolutePath) {
32+
let creator = Self(module: module, inputPaths: inputPaths, derivedData: derivedData)
33+
try! localFileSystem.writeFileContents(dst, bytes: ByteString(creator.generateData()))
34+
}
35+
36+
private func generateDict() -> [String: [String: String]] {
37+
let master = ["swift-dependencies": "\(derivedData.pathString)/\(module)-master.swiftdeps"]
38+
func baseNameEntry(_ s: AbsolutePath) -> [String: String] {
39+
[
40+
"dependencies": ".d",
41+
"diagnostics": ".dia",
42+
"llvm-bc": ".bc",
43+
"object": ".o",
44+
"swift-dependencies": ".swiftdeps",
45+
"swiftmodule": "-partial.swiftmodule"
46+
]
47+
.mapValues {"\(derivedData.appending(component: s.basenameWithoutExt))\($0)"}
48+
}
49+
return Dictionary(uniqueKeysWithValues:
50+
inputPaths.map { ("\($0)", baseNameEntry($0)) }
51+
)
52+
.merging(["": master]) {_, _ in fatalError()}
53+
}
54+
55+
private func generateData() -> Data {
56+
let d: [String: [String: String]] = generateDict()
57+
let enc = JSONEncoder()
58+
return try! enc.encode(d)
59+
}
60+
}
61+

Tests/SwiftDriverTests/IncrementalCompilationTests.swift

Lines changed: 71 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import TSCBasic
1515
@_spi(Testing) import SwiftDriver
1616
import SwiftOptions
1717

18+
// MARK: - Baseline: nonincremental
1819
final class NonincrementalCompilationTests: XCTestCase {
1920
func testBuildRecordReading() throws {
2021
let buildRecord = try XCTUnwrap(
@@ -322,10 +323,11 @@ final class NonincrementalCompilationTests: XCTestCase {
322323
}
323324
}
324325

325-
326+
// MARK: - IncrementalCompilation
326327
final class IncrementalCompilationTests: XCTestCase {
327328

328329
var tempDir: AbsolutePath = AbsolutePath("/tmp")
330+
329331
var derivedDataDir: AbsolutePath {
330332
tempDir.appending(component: "derivedData")
331333
}
@@ -360,7 +362,7 @@ final class IncrementalCompilationTests: XCTestCase {
360362
"Finished Extracting autolink information for module \(module)",
361363
]
362364
}
363-
var args: [String] {
365+
var commonArgs: [String] {
364366
[
365367
"swiftc",
366368
"-module-name", module,
@@ -375,17 +377,14 @@ final class IncrementalCompilationTests: XCTestCase {
375377
]
376378
+ inputPathsAndContents.map {$0.0.pathString} .sorted()
377379
}
378-
deinit {
379-
try? localFileSystem.removeFileTree(tempDir)
380-
}
381380

382381
override func setUp() {
383382
self.tempDir = try! withTemporaryDirectory(removeTreeOnDeinit: false) {$0}
384383
try! localFileSystem.createDirectory(derivedDataPath)
385-
writeOutputFileMapData(module: module,
386-
inputPaths: inputPathsAndContents.map {$0.0},
387-
derivedData: derivedDataPath,
388-
to: OFM)
384+
OutputFileMapCreator.write(module: module,
385+
inputPaths: inputPathsAndContents.map {$0.0},
386+
derivedData: derivedDataPath,
387+
to: OFM)
389388
for (base, contents) in baseNamesAndContents {
390389
let filePath = tempDir.appending(component: "\(base).swift")
391390
try! localFileSystem.writeFileContents(filePath) {
@@ -394,6 +393,63 @@ final class IncrementalCompilationTests: XCTestCase {
394393
}
395394
}
396395

396+
deinit {
397+
try? localFileSystem.removeFileTree(tempDir)
398+
}
399+
}
400+
401+
extension IncrementalCompilationTests {
402+
func doABuild(_ message: String,
403+
checkDiagnostics: Bool,
404+
extraArguments: [String],
405+
expectingRemarks texts: [String],
406+
whenAutolinking: [String]) throws {
407+
try doABuild(
408+
message,
409+
checkDiagnostics: checkDiagnostics,
410+
extraArguments: extraArguments,
411+
expecting: texts.map {.remark($0)},
412+
expectingWhenAutolinking: whenAutolinking.map {.remark($0)})
413+
}
414+
415+
func doABuild(_ message: String,
416+
checkDiagnostics: Bool,
417+
extraArguments: [String],
418+
expecting expectations: [Diagnostic.Message],
419+
expectingWhenAutolinking autolinkExpectations: [Diagnostic.Message]) throws {
420+
print("*** starting build \(message) ***", to: &stderrStream); stderrStream.flush()
421+
422+
func doIt(_ driver: inout Driver) {
423+
let jobs = try! driver.planBuild()
424+
try? driver.run(jobs: jobs)
425+
}
426+
427+
let allArgs = try commonArgs + extraArguments + Driver.sdkArgumentsForTesting()
428+
if checkDiagnostics {
429+
try assertDriverDiagnostics(args: allArgs) {driver, verifier in
430+
verifier.forbidUnexpected(.error, .warning, .note, .remark, .ignored)
431+
expectations.forEach {verifier.expect($0)}
432+
if driver.isAutolinkExtractJobNeeded {
433+
autolinkExpectations.forEach {verifier.expect($0)}
434+
}
435+
doIt(&driver)
436+
}
437+
}
438+
else {
439+
let diagnosticEngine = DiagnosticsEngine(handlers: [
440+
{print($0, to: &stderrStream); stderrStream.flush()}
441+
])
442+
var driver = try Driver(args: allArgs, env: ProcessEnv.vars,
443+
diagnosticsEngine: diagnosticEngine,
444+
fileSystem: localFileSystem)
445+
doIt(&driver)
446+
}
447+
print("", to: &stderrStream); stderrStream.flush()
448+
}
449+
}
450+
451+
extension IncrementalCompilationTests {
452+
397453
func testOptionsParsing() throws {
398454
let optionPairs: [(
399455
Option, (IncrementalCompilationState.InitialStateComputer) -> Bool
@@ -413,7 +469,7 @@ final class IncrementalCompilationTests: XCTestCase {
413469
expectingRemarks: [],
414470
whenAutolinking: [])
415471

416-
var driver = try Driver(args: self.args + [
472+
var driver = try Driver(args: self.commonArgs + [
417473
driverOption.spelling,
418474
] + Driver.sdkArgumentsForTesting())
419475
_ = try driver.planBuild()
@@ -422,6 +478,9 @@ final class IncrementalCompilationTests: XCTestCase {
422478
XCTAssertTrue(stateOptionFn(state.moduleDependencyGraph.info))
423479
}
424480
}
481+
}
482+
483+
extension IncrementalCompilationTests {
425484

426485
// FIXME: why does it fail on Linux in CI?
427486
func testIncrementalDiagnostics() throws {
@@ -716,53 +775,6 @@ final class IncrementalCompilationTests: XCTestCase {
716775
XCTAssert(previousContents != newContents, "\(path.pathString) unchanged after write")
717776
XCTAssert(replacement == newContents, "\(path.pathString) failed to write")
718777
}
719-
func doABuild(_ message: String,
720-
checkDiagnostics: Bool,
721-
extraArguments: [String],
722-
expectingRemarks texts: [String],
723-
whenAutolinking: [String]) throws {
724-
try doABuild(
725-
message,
726-
checkDiagnostics: checkDiagnostics,
727-
extraArguments: extraArguments,
728-
expecting: texts.map {.remark($0)},
729-
expectingWhenAutolinking: whenAutolinking.map {.remark($0)})
730-
}
731-
732-
func doABuild(_ message: String,
733-
checkDiagnostics: Bool,
734-
extraArguments: [String],
735-
expecting expectations: [Diagnostic.Message],
736-
expectingWhenAutolinking autolinkExpectations: [Diagnostic.Message]) throws {
737-
print("*** starting build \(message) ***", to: &stderrStream); stderrStream.flush()
738-
739-
func doIt(_ driver: inout Driver) {
740-
let jobs = try! driver.planBuild()
741-
try? driver.run(jobs: jobs)
742-
}
743-
744-
let allArgs = try args + extraArguments + Driver.sdkArgumentsForTesting()
745-
if checkDiagnostics {
746-
try assertDriverDiagnostics(args: allArgs) {driver, verifier in
747-
verifier.forbidUnexpected(.error, .warning, .note, .remark, .ignored)
748-
expectations.forEach {verifier.expect($0)}
749-
if driver.isAutolinkExtractJobNeeded {
750-
autolinkExpectations.forEach {verifier.expect($0)}
751-
}
752-
doIt(&driver)
753-
}
754-
}
755-
else {
756-
let diagnosticEngine = DiagnosticsEngine(handlers: [
757-
{print($0, to: &stderrStream); stderrStream.flush()}
758-
])
759-
var driver = try Driver(args: allArgs, env: ProcessEnv.vars,
760-
diagnosticsEngine: diagnosticEngine,
761-
fileSystem: localFileSystem)
762-
doIt(&driver)
763-
}
764-
print("", to: &stderrStream); stderrStream.flush()
765-
}
766778

767779
/// Ensure that autolink output file goes with .o directory, to not prevent incremental omission of
768780
/// autolink job.
@@ -774,7 +786,7 @@ final class IncrementalCompilationTests: XCTestCase {
774786
env["SWIFT_DRIVER_DSYMUTIL_EXEC"] = "/garbage/dsymutil"
775787

776788
var driver = try! Driver(
777-
args: args
789+
args: commonArgs
778790
+ ["-emit-library", "-target", "x86_64-unknown-linux"],
779791
env: env)
780792
let plannedJobs = try! driver.planBuild()
@@ -788,48 +800,6 @@ final class IncrementalCompilationTests: XCTestCase {
788800
let expected = AbsolutePath(derivedDataPath, "\(module).autolink")
789801
XCTAssertEqual(autoOut.file.absolutePath, expected)
790802
}
791-
792-
private func generateOutputFileMapDict(module: String, inputPaths: [AbsolutePath],
793-
derivedData: AbsolutePath
794-
) -> [String: [String: String]] {
795-
let master = ["swift-dependencies": "\(derivedData.pathString)/\(module)-master.swiftdeps"]
796-
func baseNameEntry(_ s: AbsolutePath) -> [String: String] {
797-
[
798-
"dependencies": ".d",
799-
"diagnostics": ".dia",
800-
"llvm-bc": ".bc",
801-
"object": ".o",
802-
"swift-dependencies": ".swiftdeps",
803-
"swiftmodule": "-partial.swiftmodule"
804-
]
805-
.mapValues {"\(derivedData.appending(component: s.basenameWithoutExt))\($0)"}
806-
}
807-
return Dictionary( uniqueKeysWithValues:
808-
inputPaths.map { ("\($0)", baseNameEntry($0)) }
809-
)
810-
.merging(["": master]) {_, _ in fatalError()}
811-
}
812-
813-
private func generateOutputFileMapData(module: String,
814-
inputPaths: [AbsolutePath],
815-
derivedData: AbsolutePath
816-
) -> Data {
817-
let d: [String: [String: String]] = generateOutputFileMapDict(
818-
module: module,
819-
inputPaths: inputPaths,
820-
derivedData: derivedData)
821-
let enc = JSONEncoder()
822-
return try! enc.encode(d)
823-
}
824-
825-
private func writeOutputFileMapData(module: String,
826-
inputPaths: [AbsolutePath],
827-
derivedData: AbsolutePath,
828-
to dst: AbsolutePath) {
829-
let d: Data = generateOutputFileMapData(module: module, inputPaths: inputPaths,
830-
derivedData: derivedData)
831-
try! localFileSystem.writeFileContents(dst, bytes: ByteString(d))
832-
}
833803
}
834804

835805
class CrossModuleIncrementalBuildTests: XCTestCase {
@@ -935,3 +905,4 @@ class CrossModuleIncrementalBuildTests: XCTestCase {
935905
}
936906
}
937907
}
908+

0 commit comments

Comments
 (0)