Skip to content

Commit 591b825

Browse files
committed
Refactor Path Computations
* Document paths * Use build directory terminology that is consistent with XCBuild * Fixup the executable path computation for Windows
1 parent 411b765 commit 591b825

File tree

2 files changed

+92
-48
lines changed

2 files changed

+92
-48
lines changed

Tests/IncrementalTestFramework/Context.swift

Lines changed: 71 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -42,47 +42,90 @@ struct Context: CustomStringConvertible {
4242
file: file, line: line)
4343
}
4444

45-
/// Each module has its own directory under the root
46-
private func modulePath(for module: Module) -> AbsolutePath {
47-
rootDir.appending(component: module.name)
48-
}
49-
func derivedDataPath(for module: Module) -> AbsolutePath {
50-
modulePath(for: module).appending(component: "\(module.name)DD")
51-
}
52-
func sourceDir(for module: Module) -> AbsolutePath {
53-
modulePath(for: module)
45+
var description: String {
46+
"Incremental imports \(incrementalImports)"
5447
}
55-
func swiftFilePath(for source: Source, in module: Module) -> AbsolutePath {
56-
sourceDir(for: module).appending(component: "\(source.name).swift")
48+
49+
func failMessage(_ step: Step) -> String {
50+
"\(description), in step \(stepIndex), \(step.whatIsBuilt)"
5751
}
58-
func objFilePath(for source: Source, in module: Module) -> AbsolutePath {
59-
derivedDataPath(for: module).appending(component: "\(source.name).o")
52+
53+
func fail(_ msg: String, _ step: Step) {
54+
XCTFail("\(msg) \(failMessage(step))")
6055
}
61-
func allObjFilePaths(in module: Module) -> [AbsolutePath] {
62-
module.sources.map {objFilePath(for: $0, in: module)}
56+
}
57+
58+
// MARK: Paths
59+
60+
extension Context {
61+
/// Computes the directory containing the given module's build products.
62+
///
63+
/// - Parameter module: The module.
64+
/// - Returns: An absolute path to the build root - relative to the root
65+
/// directory of this test context.
66+
func buildRoot(for module: Module) -> AbsolutePath {
67+
self.rootDir.appending(component: "\(module.name)-buildroot")
6368
}
64-
func allImportedObjFilePaths(in module: Module) -> [AbsolutePath] {
65-
module.imports.flatMap(allObjFilePaths(in:))
69+
70+
/// Computes the directory containing the given module's source files.
71+
///
72+
/// - Parameter module: The module.
73+
/// - Returns: An absolute path to the build root - relative to the root
74+
/// directory of this test context.
75+
func sourceRoot(for module: Module) -> AbsolutePath {
76+
self.rootDir.appending(component: "\(module.name)-srcroot")
6677
}
78+
79+
/// Computes the path to the output file map for the given module.
80+
///
81+
/// - Parameter module: The module.
82+
/// - Returns: An absolute path to the output file map - relative to the root
83+
/// directory of this test context.
6784
func outputFileMapPath(for module: Module) -> AbsolutePath {
68-
derivedDataPath(for: module).appending(component: "OFM.json")
85+
self.buildRoot(for: module).appending(component: "OFM")
6986
}
87+
88+
/// Computes the path to the `.swiftmodule` file for the given module.
89+
///
90+
/// - Parameter module: The module.
91+
/// - Returns: An absolute path to the swiftmodule file - relative to the root
92+
/// directory of this test context.
7093
func swiftmodulePath(for module: Module) -> AbsolutePath {
71-
derivedDataPath(for: module).appending(component: "\(module.name).swiftmodule")
72-
}
73-
func executablePath(for module: Module) -> AbsolutePath {
74-
derivedDataPath(for: module).appending(component: "a.out")
94+
self.buildRoot(for: module).appending(component: "\(module.name).swiftmodule")
7595
}
7696

77-
var description: String {
78-
"Incremental imports \(incrementalImports)"
97+
/// Computes the path to the `.swift` file for the given module.
98+
///
99+
/// - Parameter source: The name of the swift file.
100+
/// - Parameter module: The module.
101+
/// - Returns: An absolute path to the swift file - relative to the root
102+
/// directory of this test context.
103+
func swiftFilePath(for source: Source, in module: Module) -> AbsolutePath {
104+
self.sourceRoot(for: module).appending(component: "\(source.name).swift")
79105
}
80106

81-
func failMessage(_ step: Step) -> String {
82-
"\(description), in step \(stepIndex), \(step.whatIsBuilt)"
107+
/// Computes the path to the `.o` file for the given module.
108+
///
109+
/// - Parameter source: The name of the swift file.
110+
/// - Parameter module: The module.
111+
/// - Returns: An absolute path to the object file - relative to the root
112+
/// directory of this test context.
113+
func objectFilePath(for source: Source, in module: Module) -> AbsolutePath {
114+
self.buildRoot(for: module).appending(component: "\(source.name).o")
83115
}
84116

85-
func fail(_ msg: String, _ step: Step) {
86-
XCTFail("\(msg) \(failMessage(step))")
117+
/// Computes the path to the executable file for the given module.
118+
///
119+
/// - Parameter module: The module.
120+
/// - Returns: An absolute path to the executable file - relative to the root
121+
/// directory of this test context.
122+
func executablePath(for module: Module) -> AbsolutePath {
123+
#if os(Windows)
124+
return self.buildRoot(for: module).appending(component: "a.exe")
125+
#else
126+
return self.buildRoot(for: module).appending(component: "a.out")
127+
#endif
87128
}
88129
}
130+
131+

Tests/IncrementalTestFramework/Module.swift

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ extension Module {
8686
/// Since the source directory may have been previously populated by a module with the same name but
8787
/// different `sources`, old ones must be removed, new ones added, and changed ones changes.
8888
private func createOrRemoveSources(adding addOns: [AddOn], in context: Context) throws {
89-
let dir = context.sourceDir(for: self)
89+
let dir = context.sourceRoot(for: self)
9090
func createSourceDir() throws {
9191
if !localFileSystem.exists(dir) {
9292
try localFileSystem.createDirectory(dir, recursive: true)
@@ -111,7 +111,7 @@ extension Module {
111111
}
112112

113113
private func createDerivedDataDirIfMissing(in context: Context) throws {
114-
let dir = context.derivedDataPath(for: self)
114+
let dir = context.buildRoot(for: self)
115115
if !localFileSystem.exists(dir) {
116116
try localFileSystem.createDirectory(dir, recursive: true)
117117
}
@@ -121,7 +121,7 @@ extension Module {
121121
OutputFileMapCreator.write(
122122
module: name,
123123
inputPaths: sources.map {context.swiftFilePath(for: $0, in: self)},
124-
derivedData: context.derivedDataPath(for: self),
124+
derivedData: context.buildRoot(for: self),
125125
to: context.outputFileMapPath(for: self))
126126
}
127127
}
@@ -155,24 +155,28 @@ extension Module {
155155
"-driver-show-incremental",
156156
"-driver-show-job-lifecycle"]
157157

158-
var libraryArgs: [String] {
159-
[
160-
"-c",
161-
"-parse-as-library",
162-
"-emit-module-path", context.swiftmodulePath(for: self).pathString,
163-
]
164-
}
165-
var appArgs: [String] {
158+
var searchPaths: [String] {
166159
let swiftModules = self.imports.map {
167160
context.swiftmodulePath(for: $0).parentDirectory.pathString
168161
}
169162
return swiftModules.flatMap { ["-I", $0, "-F", $0] }
170163
+ ["-o", context.executablePath(for: self).pathString]
171164
}
172165

166+
var libraryArgs: [String] {
167+
[
168+
"-c",
169+
"-parse-as-library",
170+
"-emit-module-path", context.swiftmodulePath(for: self).pathString,
171+
] + searchPaths
172+
}
173+
173174
var importedObjs: [String] {
174-
context.allImportedObjFilePaths(in: self).map {$0.pathString}
175-
.flatMap { ["-Xlinker", $0]}
175+
self.imports.flatMap { `import` in
176+
`import`.sources.map { source in
177+
context.objectFilePath(for: source, in: `import`).pathString
178+
}
179+
} .flatMap { ["-Xlinker", $0] }
176180
}
177181

178182
var incrementalImportsArgs: [String] {
@@ -184,19 +188,16 @@ extension Module {
184188
}
185189
}
186190

187-
guard let sdkArguments = try Driver.sdkArgumentsForTesting()
188-
else {
189-
throw XCTSkip("Not supported on this platform")
190-
}
191+
let sdkArguments = try? Driver.sdkArgumentsForTesting()
191192

192193
let interestingArgs = [
193-
[ "-module-name", self.name],
194+
["-module-name", self.name],
194195
["-output-file-map", context.outputFileMapPath(for: self).pathString],
195196
incrementalImportsArgs,
196-
self.product == .library ? libraryArgs : appArgs,
197+
self.product == .library ? libraryArgs : searchPaths,
197198
sources.map { context.swiftFilePath(for: $0, in: self).pathString },
198199
importedObjs,
199-
sdkArguments,
200+
sdkArguments ?? [],
200201
].joined()
201202

202203
if context.verbose {

0 commit comments

Comments
 (0)