@@ -40,11 +40,22 @@ using namespace clang::tooling::dependencies;
4040static std::string
4141moduleCacheRelativeLookupModuleOutput (const ModuleDeps &MD, ModuleOutputKind MOK,
4242 const StringRef moduleCachePath,
43- const StringRef stableModuleCachePath) {
43+ const StringRef stableModuleCachePath,
44+ const StringRef runtimeResourcePath) {
4445 llvm::SmallString<128 > outputPath (moduleCachePath);
4546 if (MD.IsInStableDirectories )
4647 outputPath = stableModuleCachePath;
4748
49+ // FIXME: This is a hack to treat Clang modules defined in the compiler's
50+ // own resource directory as stable, when they are not reported as such
51+ // by the Clang scanner.
52+ if (!runtimeResourcePath.empty () &&
53+ hasPrefix (llvm::sys::path::begin (MD.ClangModuleMapFile ),
54+ llvm::sys::path::end (MD.ClangModuleMapFile ),
55+ llvm::sys::path::begin (runtimeResourcePath),
56+ llvm::sys::path::end (runtimeResourcePath)))
57+ outputPath = stableModuleCachePath;
58+
4859 llvm::sys::path::append (outputPath, MD.ID .ModuleName + " -" + MD.ID .ContextHash );
4960 switch (MOK) {
5061 case ModuleOutputKind::ModuleFile:
@@ -174,7 +185,7 @@ ModuleDependencyVector ClangImporter::bridgeClangModuleDependencies(
174185
175186 auto pcmPath = moduleCacheRelativeLookupModuleOutput (
176187 clangModuleDep, ModuleOutputKind::ModuleFile, moduleOutputPath,
177- stableModuleOutputPath);
188+ stableModuleOutputPath, ctx. SearchPathOpts . RuntimeResourcePath );
178189 swiftArgs.push_back (" -o" );
179190 swiftArgs.push_back (pcmPath);
180191
@@ -427,10 +438,11 @@ ClangImporter::getModuleDependencies(Identifier moduleName,
427438 }
428439 std::string workingDir = *optionalWorkingDir;
429440 auto lookupModuleOutput =
430- [moduleOutputPath, sdkModuleOutputPath]
441+ [moduleOutputPath, sdkModuleOutputPath, &ctx ]
431442 (const ModuleDeps &MD, ModuleOutputKind MOK) -> std::string {
432443 return moduleCacheRelativeLookupModuleOutput (MD, MOK, moduleOutputPath,
433- sdkModuleOutputPath);
444+ sdkModuleOutputPath,
445+ ctx.SearchPathOpts .RuntimeResourcePath );
434446 };
435447
436448 auto clangModuleDependencies =
@@ -485,11 +497,12 @@ bool ClangImporter::getHeaderDependencies(
485497 auto moduleOutputPath = cache.getModuleOutputPath ();
486498 auto sdkModuleOutputPath = cache.getSDKModuleOutputPath ();
487499 auto lookupModuleOutput =
488- [moduleOutputPath, sdkModuleOutputPath]
500+ [moduleOutputPath, sdkModuleOutputPath, &ctx ]
489501 (const ModuleDeps &MD, ModuleOutputKind MOK) -> std::string {
490502 return moduleCacheRelativeLookupModuleOutput (MD, MOK,
491503 moduleOutputPath,
492- sdkModuleOutputPath);
504+ sdkModuleOutputPath,
505+ ctx.SearchPathOpts .RuntimeResourcePath );
493506 };
494507 auto dependencies = clangScanningTool.getTranslationUnitDependencies (
495508 commandLineArgs, workingDir, cache.getAlreadySeenClangModules (),
0 commit comments