Skip to content

Commit 7c7ec33

Browse files
committed
Added functionality to -register-module-dependency flag
This flag tells the driver to include the module in the dependency scanning, but not load it. Added test case for the flag.
1 parent b77c2db commit 7c7ec33

File tree

2 files changed

+76
-0
lines changed

2 files changed

+76
-0
lines changed

Sources/SwiftDriver/ExplicitModuleBuilds/ModuleDependencyScanning.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,15 @@ public extension Driver {
145145
commandLine.appendFlag(.dependencyScanCacheRemarks)
146146
}
147147

148+
// enabling scanning dependencies and building the module without including it in the compilation
149+
if (parsedOptions.contains(.registerModuleDependency) ){
150+
if let moduleNameArg = parsedOptions.getLastArgument(.registerModuleDependency) {
151+
let moduleName = moduleNameArg.asSingle
152+
commandLine.appendFlag(.importModule)
153+
commandLine.appendFlag(moduleName)
154+
}
155+
}
156+
148157
if shouldAttemptIncrementalCompilation &&
149158
parsedOptions.contains(.incrementalDependencyScan) {
150159
if let serializationPath = buildRecordInfo?.dependencyScanSerializedResultPath {

Tests/SwiftDriverTests/ExplicitModuleBuildTests.swift

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,73 @@ final class ExplicitModuleBuildTests: XCTestCase {
803803
}
804804
}
805805

806+
func testRegisterModuleDependencyFlag() throws {
807+
let (stdlibPath, shimsPath, _, _) = try getDriverArtifactsForScanning()
808+
try withTemporaryDirectory { path in
809+
try localFileSystem.changeCurrentWorkingDirectory(to: path)
810+
let moduleCachePath = path.appending(component: "ModuleCache")
811+
try localFileSystem.createDirectory(moduleCachePath)
812+
let main = path.appending(component: "testRegisterModuleDependency.swift")
813+
// Note: We're NOT importing module E in the source code
814+
try localFileSystem.writeFileContents(main, bytes:
815+
"""
816+
import A;
817+
"""
818+
)
819+
let cHeadersPath: AbsolutePath =
820+
try testInputsPath.appending(component: "ExplicitModuleBuilds")
821+
.appending(component: "CHeaders")
822+
let swiftModuleInterfacesPath: AbsolutePath =
823+
try testInputsPath.appending(component: "ExplicitModuleBuilds")
824+
.appending(component: "Swift")
825+
let sdkArgumentsForTesting = (try? Driver.sdkArgumentsForTesting()) ?? []
826+
827+
var driver = try Driver(args: ["swiftc",
828+
"-I", cHeadersPath.nativePathString(escaped: true),
829+
"-I", swiftModuleInterfacesPath.nativePathString(escaped: true),
830+
"-I", stdlibPath.nativePathString(escaped: true),
831+
"-I", shimsPath.nativePathString(escaped: true),
832+
"-explicit-module-build",
833+
"-module-cache-path", moduleCachePath.nativePathString(escaped: true),
834+
"-working-directory", path.nativePathString(escaped: true),
835+
"-disable-implicit-concurrency-module-import",
836+
"-disable-implicit-string-processing-module-import",
837+
"-register-module-dependency", "E",
838+
"-emit-loaded-module-trace",
839+
main.nativePathString(escaped: true)] + sdkArgumentsForTesting)
840+
//guard driver.isFrontendArgSupported(.registerModuleDependency) else {
841+
// throw XCTSkip("Compiler does not support -register-module-dependency")
842+
//}
843+
let dependencyGraph = try driver.scanModuleDependencies()
844+
let jobs = try driver.planBuild()
845+
// E SHOULD be in the dependency graph (registered for scanning)
846+
XCTAssertTrue(dependencyGraph.modules.keys.contains(.swift("E")),
847+
"Module E should be in dependency graph when registered via -register-module-dependency")
848+
// Checking that registered module compiled
849+
let moduleEJobs = jobs.filter { job in
850+
job.outputs.contains { output in
851+
output.file.basename.contains("E") && output.file.extension == "swiftmodule"
852+
}
853+
}
854+
XCTAssertFalse(moduleEJobs.isEmpty,
855+
"Module E should have a build job when registered via -register-module-dependency")
856+
// Checking that registered module is not loaded for the main compilation
857+
try driver.run(jobs: jobs)
858+
XCTAssertFalse(driver.diagnosticEngine.hasErrors)
859+
// Checking the output given by the -emit-loaded-module-trace flag
860+
let traceFile = path.appending(component: "testRegisterModuleDependency.trace.json")
861+
XCTAssertTrue(localFileSystem.exists(traceFile), "Module trace file should exist")
862+
let traceData = try localFileSystem.readFileContents(traceFile)
863+
let traceJSON = try traceData.withData { data in
864+
try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any]
865+
}
866+
let jsonString = String(decoding: traceData.contents, as: UTF8.self)
867+
XCTAssertFalse(jsonString.contains("\"name\":\"E\""),
868+
"Module E should not be loaded in the final compilation since it's not imported")
869+
870+
}
871+
}
872+
806873
// Ensure that (even when not in '-incremental' mode) up-to-date module dependencies
807874
// do not get re-built
808875
func testExplicitModuleBuildIncrementalEndToEnd() throws {

0 commit comments

Comments
 (0)