[BrdigingHeader] Auto bridging header chaining

Add ability to automatically chaining the bridging headers discovered from all
dependencies module when doing swift caching build. This will eliminate all
implicit bridging header imports from the build and make the bridging header
importing behavior much more reliable, while keep the compatibility at maximum.

For example, if the current module A depends on module B and C, and both B and
C are binary modules that uses bridging header, when building module A,
dependency scanner will construct a new header that chains three bridging
headers together with the option to build a PCH from it. This will make all
importing errors more obvious while improving the performance.
This commit is contained in:
Steven Wu
2025-01-06 15:52:32 -08:00
parent 8534f29ef5
commit 9d59044bb1
44 changed files with 1094 additions and 275 deletions

View File

@@ -171,7 +171,7 @@ bool ModuleDependenciesCacheDeserializer::readMetadata(StringRef scannerContextH
if (majorVersion != MODULE_DEPENDENCY_CACHE_FORMAT_VERSION_MAJOR ||
minorVersion != MODULE_DEPENDENCY_CACHE_FORMAT_VERSION_MINOR)
return true;
std::string readScannerContextHash = BlobData.str();
if (readScannerContextHash != scannerContextHash)
return true;
@@ -286,8 +286,7 @@ bool ModuleDependenciesCacheDeserializer::readGraph(
auto bridgingSourceFiles = getStringArray(bridgingSourceFilesArrayID);
if (!bridgingSourceFiles)
llvm::report_fatal_error("Bad bridging source files");
for (const auto &file : *bridgingSourceFiles)
moduleDep.addHeaderSourceFile(file);
moduleDep.setHeaderSourceFiles(*bridgingSourceFiles);
// Add bridging module dependencies
auto bridgingModuleDeps =
@@ -609,13 +608,15 @@ bool ModuleDependenciesCacheDeserializer::readGraph(
unsigned bridgingHeaderFileID, sourceFilesArrayID,
bridgingSourceFilesArrayID, bridgingModuleDependenciesArrayID,
CASFileSystemRootID, bridgingHeaderIncludeTreeID,
buildCommandLineArrayID, bridgingHeaderBuildCommandLineArrayID;
buildCommandLineArrayID, bridgingHeaderBuildCommandLineArrayID,
chainedBridgingHeaderPathID, chainedBridgingHeaderContentID;
SwiftSourceModuleDetailsLayout::readRecord(
Scratch, bridgingHeaderFileID,
sourceFilesArrayID, bridgingSourceFilesArrayID,
bridgingModuleDependenciesArrayID, CASFileSystemRootID,
bridgingHeaderIncludeTreeID, buildCommandLineArrayID,
bridgingHeaderBuildCommandLineArrayID);
bridgingHeaderBuildCommandLineArrayID, chainedBridgingHeaderPathID,
chainedBridgingHeaderContentID);
auto rootFileSystemID = getIdentifier(CASFileSystemRootID);
if (!rootFileSystemID)
@@ -646,6 +647,15 @@ bool ModuleDependenciesCacheDeserializer::readGraph(
for (const auto &file : *sourceFiles)
moduleDep.addSourceFile(file);
// Add chained bridging header.
auto chainedBridgingHeaderPath =
getIdentifier(chainedBridgingHeaderPathID);
auto chainedBridgingHeaderContent =
getIdentifier(chainedBridgingHeaderContentID);
if (chainedBridgingHeaderPath && chainedBridgingHeaderContent)
moduleDep.setChainedBridgingHeaderBuffer(*chainedBridgingHeaderPath,
*chainedBridgingHeaderContent);
addCommonDependencyInfo(moduleDep);
addSwiftCommonDependencyInfo(moduleDep);
addSwiftTextualDependencyInfo(
@@ -724,8 +734,7 @@ bool ModuleDependenciesCacheDeserializer::readGraph(
if (!headerImportsSourceFiles)
llvm::report_fatal_error(
"Bad binary direct dependencies: no header import source files");
for (const auto &depSource : *headerImportsSourceFiles)
moduleDep.addHeaderSourceFile(depSource);
moduleDep.setHeaderSourceFiles(*headerImportsSourceFiles);
cache.recordDependency(currentModuleName, std::move(moduleDep));
hasCurrentModule = false;
@@ -1537,7 +1546,9 @@ void ModuleDependenciesCacheSerializer::writeModuleInfo(
ModuleIdentifierArrayKind::BuildCommandLine),
getIdentifierArrayID(
moduleID,
ModuleIdentifierArrayKind::BridgingHeaderBuildCommandLine));
ModuleIdentifierArrayKind::BridgingHeaderBuildCommandLine),
getIdentifier(swiftSourceDeps->chainedBridgingHeaderPath),
getIdentifier(swiftSourceDeps->chainedBridgingHeaderContent));
break;
}
case swift::ModuleDependencyKind::SwiftBinary: {
@@ -1859,6 +1870,8 @@ void ModuleDependenciesCacheSerializer::collectStringsAndArrays(
swiftSourceDeps->bridgingHeaderBuildCommandLine);
addIdentifier(
swiftSourceDeps->textualModuleDetails.CASFileSystemRootID);
addIdentifier(swiftSourceDeps->chainedBridgingHeaderPath);
addIdentifier(swiftSourceDeps->chainedBridgingHeaderContent);
break;
}
case swift::ModuleDependencyKind::Clang: {