From bfa356b3ecc546631af5dcbe30e2684a52abdf4d Mon Sep 17 00:00:00 2001 From: Steven Wu Date: Wed, 7 Jun 2023 10:49:49 -0700 Subject: [PATCH 1/2] Address review feedback --- .../swift-c/DependencyScan/DependencyScan.h | 2 +- include/swift/AST/ModuleDependencies.h | 153 +++++++----------- lib/AST/ModuleDependencies.cpp | 33 ++-- .../ClangModuleDependencyScanner.cpp | 21 ++- .../ModuleDependencyCacheSerialization.cpp | 46 +++--- lib/DependencyScan/ScanDependencies.cpp | 49 +++--- test/CAS/Inputs/SwiftDepsExtractor.py | 32 ++-- test/CAS/cas-explicit-module-map.swift | 26 +++ 8 files changed, 180 insertions(+), 182 deletions(-) diff --git a/include/swift-c/DependencyScan/DependencyScan.h b/include/swift-c/DependencyScan/DependencyScan.h index ba6bc4a179194..998c8309bf352 100644 --- a/include/swift-c/DependencyScan/DependencyScan.h +++ b/include/swift-c/DependencyScan/DependencyScan.h @@ -25,7 +25,7 @@ /// SWIFTSCAN_VERSION_MINOR should increase when there are API additions. /// SWIFTSCAN_VERSION_MAJOR is intended for "major" source/ABI breaking changes. #define SWIFTSCAN_VERSION_MAJOR 0 -#define SWIFTSCAN_VERSION_MINOR 3 +#define SWIFTSCAN_VERSION_MINOR 4 SWIFTSCAN_BEGIN_DECLS diff --git a/include/swift/AST/ModuleDependencies.h b/include/swift/AST/ModuleDependencies.h index 048bce85768cd..98dbc1de8fda6 100644 --- a/include/swift/AST/ModuleDependencies.h +++ b/include/swift/AST/ModuleDependencies.h @@ -106,8 +106,10 @@ class ModuleDependencyInfoStorageBase { const ModuleDependencyKind dependencyKind; ModuleDependencyInfoStorageBase(ModuleDependencyKind dependencyKind, + StringRef moduleCacheKey = "", bool resolved = false) - : dependencyKind(dependencyKind), resolved(resolved) { } + : dependencyKind(dependencyKind), moduleCacheKey(moduleCacheKey.str()), + resolved(resolved) {} virtual ModuleDependencyInfoStorageBase *clone() const = 0; @@ -124,18 +126,19 @@ class ModuleDependencyInfoStorageBase { /// The set of modules on which this module depends, resolved /// to Module IDs, qualified by module kind: Swift, Clang, etc. std::vector resolvedModuleDependencies; + + /// The cache key for the produced module. + std::string moduleCacheKey; + bool resolved; }; struct CommonSwiftTextualModuleDependencyDetails { CommonSwiftTextualModuleDependencyDetails( ArrayRef extraPCMArgs, ArrayRef buildCommandLine, - ArrayRef bridgingHeaderBuildCommandLine, const std::string &CASFileSystemRootID) : extraPCMArgs(extraPCMArgs.begin(), extraPCMArgs.end()), buildCommandLine(buildCommandLine.begin(), buildCommandLine.end()), - bridgingHeaderBuildCommandLine(bridgingHeaderBuildCommandLine.begin(), - bridgingHeaderBuildCommandLine.end()), CASFileSystemRootID(CASFileSystemRootID) {} /// To build a PCM to be used by this Swift module, we need to append these @@ -160,14 +163,11 @@ struct CommonSwiftTextualModuleDependencyDetails { /// interface. std::vector buildCommandLine; - /// The Swift frontend invocation arguments to build bridging header. - std::vector bridgingHeaderBuildCommandLine; - /// CASID for the Root of CASFS. Empty if CAS is not used. std::string CASFileSystemRootID; /// CASID for the Root of bridgingHeaderClangIncludeTree. Empty if not used. - std::string bridgingHeaderIncludeTreeRoot; + std::string CASBridgingHeaderIncludeTreeRootID; }; /// Describes the dependencies of a Swift module described by an Swift interface file. @@ -194,28 +194,21 @@ class SwiftInterfaceModuleDependenciesStorage : /// Details common to Swift textual (interface or source) modules CommonSwiftTextualModuleDependencyDetails textualModuleDetails; - /// The cache key for the produced module. - std::string moduleCacheKey; - SwiftInterfaceModuleDependenciesStorage( const std::string &moduleOutputPath, const std::string &swiftInterfaceFile, ArrayRef compiledModuleCandidates, - ArrayRef buildCommandLine, - ArrayRef extraPCMArgs, - StringRef contextHash, - bool isFramework, - const std::string &RootID, - const std::string &ModuleCacheKey - ) : ModuleDependencyInfoStorageBase(ModuleDependencyKind::SwiftInterface), - moduleOutputPath(moduleOutputPath), - swiftInterfaceFile(swiftInterfaceFile), - compiledModuleCandidates(compiledModuleCandidates.begin(), - compiledModuleCandidates.end()), - contextHash(contextHash), isFramework(isFramework), - textualModuleDetails(extraPCMArgs, buildCommandLine, {}, RootID), - moduleCacheKey(ModuleCacheKey) - {} + ArrayRef buildCommandLine, ArrayRef extraPCMArgs, + StringRef contextHash, bool isFramework, const std::string &RootID, + const std::string &moduleCacheKey) + : ModuleDependencyInfoStorageBase(ModuleDependencyKind::SwiftInterface, + moduleCacheKey), + moduleOutputPath(moduleOutputPath), + swiftInterfaceFile(swiftInterfaceFile), + compiledModuleCandidates(compiledModuleCandidates.begin(), + compiledModuleCandidates.end()), + contextHash(contextHash), isFramework(isFramework), + textualModuleDetails(extraPCMArgs, buildCommandLine, RootID) {} ModuleDependencyInfoStorageBase *clone() const override { return new SwiftInterfaceModuleDependenciesStorage(*this); @@ -228,10 +221,6 @@ class SwiftInterfaceModuleDependenciesStorage : void updateCommandLine(const std::vector &newCommandLine) { textualModuleDetails.buildCommandLine = newCommandLine; } - - void updateModuleCacheKey(const std::string &Key) { - moduleCacheKey = Key; - } }; /// Describes the dependencies of a Swift module @@ -250,14 +239,18 @@ class SwiftSourceModuleDependenciesStorage : /// Collection of module imports that were detected to be `@Testable` llvm::StringSet<> testableImports; + /// The Swift frontend invocation arguments to build bridging header. + std::vector bridgingHeaderBuildCommandLine; + SwiftSourceModuleDependenciesStorage( const std::string &RootID, ArrayRef buildCommandLine, ArrayRef bridgingHeaderBuildCommandLine, ArrayRef extraPCMArgs) : ModuleDependencyInfoStorageBase(ModuleDependencyKind::SwiftSource), - textualModuleDetails(extraPCMArgs, buildCommandLine, - bridgingHeaderBuildCommandLine, RootID), - testableImports(llvm::StringSet<>()) {} + textualModuleDetails(extraPCMArgs, buildCommandLine, RootID), + testableImports(llvm::StringSet<>()), + bridgingHeaderBuildCommandLine(bridgingHeaderBuildCommandLine.begin(), + bridgingHeaderBuildCommandLine.end()) {} ModuleDependencyInfoStorageBase *clone() const override { return new SwiftSourceModuleDependenciesStorage(*this); @@ -273,7 +266,7 @@ class SwiftSourceModuleDependenciesStorage : void updateBridgingHeaderCommandLine( const std::vector &newCommandLine) { - textualModuleDetails.bridgingHeaderBuildCommandLine = newCommandLine; + bridgingHeaderBuildCommandLine = newCommandLine; } void addTestableImport(ImportPath::Module module) { @@ -290,11 +283,11 @@ class SwiftBinaryModuleDependencyStorage : public ModuleDependencyInfoStorageBas const std::string &moduleDocPath, const std::string &sourceInfoPath, const bool isFramework, - const std::string ModuleCacheKey) - : ModuleDependencyInfoStorageBase(ModuleDependencyKind::SwiftBinary), + const std::string &moduleCacheKey) + : ModuleDependencyInfoStorageBase(ModuleDependencyKind::SwiftBinary, + moduleCacheKey), compiledModulePath(compiledModulePath), moduleDocPath(moduleDocPath), - sourceInfoPath(sourceInfoPath), isFramework(isFramework), - moduleCacheKey(ModuleCacheKey) {} + sourceInfoPath(sourceInfoPath), isFramework(isFramework) {} ModuleDependencyInfoStorageBase *clone() const override { return new SwiftBinaryModuleDependencyStorage(*this); @@ -312,16 +305,9 @@ class SwiftBinaryModuleDependencyStorage : public ModuleDependencyInfoStorageBas /// A flag that indicates this dependency is a framework const bool isFramework; - /// The cache key for the produced module. - std::string moduleCacheKey; - static bool classof(const ModuleDependencyInfoStorageBase *base) { return base->dependencyKind == ModuleDependencyKind::SwiftBinary; } - - void updateModuleCacheKey(const std::string &Key) { - moduleCacheKey = Key; - } }; /// Describes the dependencies of a Clang module. @@ -339,7 +325,7 @@ class ClangModuleDependencyStorage : public ModuleDependencyInfoStorageBase { const std::string contextHash; /// Partial (Clang) command line that can be used to build this module. - std::vector nonPathCommandLine; + std::vector buildCommandLine; /// The file dependencies const std::vector fileDependencies; @@ -352,31 +338,24 @@ class ClangModuleDependencyStorage : public ModuleDependencyInfoStorageBase { std::string CASFileSystemRootID; /// CASID for the Root of ClangIncludeTree. Empty if not used. - std::string clangIncludeTreeRoot; - - /// The cache key for the produced module. - std::string moduleCacheKey; - - ClangModuleDependencyStorage( - const std::string &pcmOutputPath, - const std::string &moduleMapFile, - const std::string &contextHash, - const std::vector &nonPathCommandLine, - const std::vector &fileDependencies, - const std::vector &capturedPCMArgs, - const std::string &CASFileSystemRootID, - const std::string &clangIncludeTreeRoot, - const std::string &ModuleCacheKey - ) : ModuleDependencyInfoStorageBase(ModuleDependencyKind::Clang), - pcmOutputPath(pcmOutputPath), - moduleMapFile(moduleMapFile), - contextHash(contextHash), - nonPathCommandLine(nonPathCommandLine), - fileDependencies(fileDependencies), - capturedPCMArgs(capturedPCMArgs), - CASFileSystemRootID(CASFileSystemRootID), - clangIncludeTreeRoot(clangIncludeTreeRoot), - moduleCacheKey(ModuleCacheKey) {} + std::string CASClangIncludeTreeRootID; + + ClangModuleDependencyStorage(const std::string &pcmOutputPath, + const std::string &moduleMapFile, + const std::string &contextHash, + const std::vector &buildCommandLine, + const std::vector &fileDependencies, + const std::vector &capturedPCMArgs, + const std::string &CASFileSystemRootID, + const std::string &clangIncludeTreeRoot, + const std::string &moduleCacheKey) + : ModuleDependencyInfoStorageBase(ModuleDependencyKind::Clang, + moduleCacheKey), + pcmOutputPath(pcmOutputPath), moduleMapFile(moduleMapFile), + contextHash(contextHash), buildCommandLine(buildCommandLine), + fileDependencies(fileDependencies), capturedPCMArgs(capturedPCMArgs), + CASFileSystemRootID(CASFileSystemRootID), + CASClangIncludeTreeRootID(clangIncludeTreeRoot) {} ModuleDependencyInfoStorageBase *clone() const override { return new ClangModuleDependencyStorage(*this); @@ -387,12 +366,7 @@ class ClangModuleDependencyStorage : public ModuleDependencyInfoStorageBase { } void updateCommandLine(const std::vector &newCommandLine) { - nonPathCommandLine = newCommandLine; - } - - void updateModuleCacheKey(const std::string &Key) { - assert(moduleCacheKey.empty()); - moduleCacheKey = Key; + buildCommandLine = newCommandLine; } }; @@ -543,28 +517,11 @@ class ModuleDependencyInfo { std::string getModuleCacheKey() const { assert(storage->resolved); - if (auto *dep = getAsSwiftInterfaceModule()) - return dep->moduleCacheKey; - else if (auto *dep = getAsSwiftBinaryModule()) - return dep->moduleCacheKey; - else if (auto *dep = getAsClangModule()) - return dep->moduleCacheKey; - - llvm_unreachable("Unexpected type"); + return storage->moduleCacheKey; } void updateModuleCacheKey(const std::string &key) { - if (isSwiftInterfaceModule()) - return cast(storage.get()) - ->updateModuleCacheKey(key); - else if (isSwiftBinaryModule()) - return cast(storage.get()) - ->updateModuleCacheKey(key); - else if (isClangModule()) - return cast(storage.get()) - ->updateModuleCacheKey(key); - - llvm_unreachable("Unexpected type"); + storage->moduleCacheKey = key; } /// Resolve a dependency's set of `imports` with qualified Module IDs @@ -590,7 +547,7 @@ class ModuleDependencyInfo { std::vector getCommandline() const { if (auto *detail = getAsClangModule()) - return detail->nonPathCommandLine; + return detail->buildCommandLine; else if (auto *detail = getAsSwiftInterfaceModule()) return detail->textualModuleDetails.buildCommandLine; else if (auto *detail = getAsSwiftSourceModule()) @@ -613,7 +570,7 @@ class ModuleDependencyInfo { std::vector getBridgingHeaderCommandline() const { if (auto *detail = getAsSwiftSourceModule()) - return detail->textualModuleDetails.bridgingHeaderBuildCommandLine; + return detail->bridgingHeaderBuildCommandLine; return {}; } diff --git a/lib/AST/ModuleDependencies.cpp b/lib/AST/ModuleDependencies.cpp index fb5c3c72aee9a..c3d3a59f590c2 100644 --- a/lib/AST/ModuleDependencies.cpp +++ b/lib/AST/ModuleDependencies.cpp @@ -215,7 +215,7 @@ Optional ModuleDependencyInfo::getClangIncludeTree() const { switch (getKind()) { case swift::ModuleDependencyKind::Clang: { auto clangModuleStorage = cast(storage.get()); - Root = clangModuleStorage->clangIncludeTreeRoot; + Root = clangModuleStorage->CASClangIncludeTreeRootID; break; } default: @@ -235,14 +235,14 @@ ModuleDependencyInfo::getBridgingHeaderIncludeTree() const { auto swiftInterfaceStorage = cast(storage.get()); Root = swiftInterfaceStorage->textualModuleDetails - .bridgingHeaderIncludeTreeRoot; + .CASBridgingHeaderIncludeTreeRootID; break; } case swift::ModuleDependencyKind::SwiftSource: { auto swiftSourceStorage = cast(storage.get()); - Root = - swiftSourceStorage->textualModuleDetails.bridgingHeaderIncludeTreeRoot; + Root = swiftSourceStorage->textualModuleDetails + .CASBridgingHeaderIncludeTreeRootID; break; } default: @@ -330,15 +330,15 @@ void ModuleDependencyInfo::addBridgingHeaderIncludeTree(StringRef ID) { case swift::ModuleDependencyKind::SwiftInterface: { auto swiftInterfaceStorage = cast(storage.get()); - swiftInterfaceStorage->textualModuleDetails.bridgingHeaderIncludeTreeRoot = - ID.str(); + swiftInterfaceStorage->textualModuleDetails + .CASBridgingHeaderIncludeTreeRootID = ID.str(); break; } case swift::ModuleDependencyKind::SwiftSource: { auto swiftSourceStorage = cast(storage.get()); - swiftSourceStorage->textualModuleDetails.bridgingHeaderIncludeTreeRoot = - ID.str(); + swiftSourceStorage->textualModuleDetails + .CASBridgingHeaderIncludeTreeRootID = ID.str(); break; } default: @@ -435,14 +435,15 @@ void SwiftDependencyScanningService::setupCachingDependencyScanningService( SDKSettingPath.size()); // Add Legacy layout file (maybe just hard code instead of searching). - StringRef RuntimeLibPath = - Instance.getInvocation().getSearchPathOptions().RuntimeLibraryPaths[0]; - auto &FS = Instance.getFileSystem(); - std::error_code EC; - for (auto F = FS.dir_begin(RuntimeLibPath, EC); - !EC && F != llvm::vfs::directory_iterator(); F.increment(EC)) { - if (F->path().endswith(".yaml")) - CommonDependencyFiles.emplace_back(F->path().str()); + for (auto RuntimeLibPath : + Instance.getInvocation().getSearchPathOptions().RuntimeLibraryPaths) { + auto &FS = Instance.getFileSystem(); + std::error_code EC; + for (auto F = FS.dir_begin(RuntimeLibPath, EC); + !EC && F != llvm::vfs::directory_iterator(); F.increment(EC)) { + if (F->path().endswith(".yaml")) + CommonDependencyFiles.emplace_back(F->path().str()); + } } auto CachingFS = diff --git a/lib/ClangImporter/ClangModuleDependencyScanner.cpp b/lib/ClangImporter/ClangModuleDependencyScanner.cpp index bee9092c22c9d..0855acffb882a 100644 --- a/lib/ClangImporter/ClangModuleDependencyScanner.cpp +++ b/lib/ClangImporter/ClangModuleDependencyScanner.cpp @@ -187,18 +187,6 @@ void ClangImporter::recordModuleDependencies( swiftArgs.push_back("-vfsoverlay"); swiftArgs.push_back(overlay); } - } else { - // HACK: find the -ivfsoverlay option from clang scanner and pass to - // swift. - bool addOption = false; - for (auto &arg : clangModuleDep.BuildArguments) { - if (addOption) { - swiftArgs.push_back("-vfsoverlay"); - swiftArgs.push_back(arg); - addOption = false; - } else if (arg == "-ivfsoverlay") - addOption = true; - } } // Add args reported by the scanner. @@ -224,6 +212,15 @@ void ClangImporter::recordModuleDependencies( // invocation, not swift invocation. depsInvocation.getFrontendOpts().ModuleCacheKeys.clear(); + // FIXME: workaround for rdar://105684525: find the -ivfsoverlay option + // from clang scanner and pass to swift. + for (auto overlay : depsInvocation.getHeaderSearchOpts().VFSOverlayFiles) { + if (llvm::is_contained(ctx.SearchPathOpts.VFSOverlayFiles, overlay)) + continue; + swiftArgs.push_back("-vfsoverlay"); + swiftArgs.push_back(overlay); + } + llvm::BumpPtrAllocator allocator; llvm::StringSaver saver(allocator); clangArgs.clear(); diff --git a/lib/DependencyScan/ModuleDependencyCacheSerialization.cpp b/lib/DependencyScan/ModuleDependencyCacheSerialization.cpp index 63fff992acbe9..184f356ef78c4 100644 --- a/lib/DependencyScan/ModuleDependencyCacheSerialization.cpp +++ b/lib/DependencyScan/ModuleDependencyCacheSerialization.cpp @@ -918,20 +918,22 @@ void ModuleDependenciesCacheSerializer::writeModuleInfo(ModuleDependencyID modul : 0; SwiftInterfaceModuleDetailsLayout::emitRecord( Out, ScratchRecord, AbbrCodes[SwiftInterfaceModuleDetailsLayout::Code], - outputModulePathFileId, - swiftInterfaceFileId, - getArrayID(moduleID, ModuleIdentifierArrayKind::CompiledModuleCandidates), + outputModulePathFileId, swiftInterfaceFileId, + getArrayID(moduleID, + ModuleIdentifierArrayKind::CompiledModuleCandidates), getArrayID(moduleID, ModuleIdentifierArrayKind::BuildCommandLine), getArrayID(moduleID, ModuleIdentifierArrayKind::ExtraPCMArgs), - getIdentifier(swiftTextDeps->contextHash), - swiftTextDeps->isFramework, + getIdentifier(swiftTextDeps->contextHash), swiftTextDeps->isFramework, bridgingHeaderFileId, getArrayID(moduleID, ModuleIdentifierArrayKind::SourceFiles), getArrayID(moduleID, ModuleIdentifierArrayKind::BridgingSourceFiles), - getArrayID(moduleID, ModuleIdentifierArrayKind::BridgingModuleDependencies), - getArrayID(moduleID, ModuleIdentifierArrayKind::SwiftOverlayDependencyIDs), + getArrayID(moduleID, + ModuleIdentifierArrayKind::BridgingModuleDependencies), + getArrayID(moduleID, + ModuleIdentifierArrayKind::SwiftOverlayDependencyIDs), getIdentifier(swiftTextDeps->textualModuleDetails.CASFileSystemRootID), - getIdentifier(swiftTextDeps->textualModuleDetails.bridgingHeaderIncludeTreeRoot), + getIdentifier(swiftTextDeps->textualModuleDetails + .CASBridgingHeaderIncludeTreeRootID), getIdentifier(swiftTextDeps->moduleCacheKey)); break; } @@ -951,12 +953,17 @@ void ModuleDependenciesCacheSerializer::writeModuleInfo(ModuleDependencyID modul bridgingHeaderFileId, getArrayID(moduleID, ModuleIdentifierArrayKind::SourceFiles), getArrayID(moduleID, ModuleIdentifierArrayKind::BridgingSourceFiles), - getArrayID(moduleID, ModuleIdentifierArrayKind::BridgingModuleDependencies), - getArrayID(moduleID, ModuleIdentifierArrayKind::SwiftOverlayDependencyIDs), - getIdentifier(swiftSourceDeps->textualModuleDetails.CASFileSystemRootID), - getIdentifier(swiftSourceDeps->textualModuleDetails.bridgingHeaderIncludeTreeRoot), + getArrayID(moduleID, + ModuleIdentifierArrayKind::BridgingModuleDependencies), + getArrayID(moduleID, + ModuleIdentifierArrayKind::SwiftOverlayDependencyIDs), + getIdentifier( + swiftSourceDeps->textualModuleDetails.CASFileSystemRootID), + getIdentifier(swiftSourceDeps->textualModuleDetails + .CASBridgingHeaderIncludeTreeRootID), getArrayID(moduleID, ModuleIdentifierArrayKind::BuildCommandLine), - getArrayID(moduleID, ModuleIdentifierArrayKind::BridgingHeaderBuildCommandLine)); + getArrayID(moduleID, + ModuleIdentifierArrayKind::BridgingHeaderBuildCommandLine)); break; } case swift::ModuleDependencyKind::SwiftBinary: { @@ -998,7 +1005,7 @@ void ModuleDependenciesCacheSerializer::writeModuleInfo(ModuleDependencyID modul getArrayID(moduleID, ModuleIdentifierArrayKind::FileDependencies), getArrayID(moduleID, ModuleIdentifierArrayKind::CapturedPCMArgs), getIdentifier(clangDeps->CASFileSystemRootID), - getIdentifier(clangDeps->clangIncludeTreeRoot), + getIdentifier(clangDeps->CASClangIncludeTreeRootID), getIdentifier(clangDeps->moduleCacheKey)); break; @@ -1133,8 +1140,8 @@ void ModuleDependenciesCacheSerializer::collectStringsAndArrays( moduleID, ModuleIdentifierArrayKind::SwiftOverlayDependencyIDs, swiftTextDeps->textualModuleDetails.swiftOverlayDependencies); addIdentifier(swiftTextDeps->textualModuleDetails.CASFileSystemRootID); - addIdentifier( - swiftTextDeps->textualModuleDetails.bridgingHeaderIncludeTreeRoot); + addIdentifier(swiftTextDeps->textualModuleDetails + .CASBridgingHeaderIncludeTreeRootID); addIdentifier(swiftTextDeps->moduleCacheKey); break; } @@ -1180,8 +1187,7 @@ void ModuleDependenciesCacheSerializer::collectStringsAndArrays( swiftSourceDeps->textualModuleDetails.buildCommandLine); addStringArray( moduleID, ModuleIdentifierArrayKind::BridgingHeaderBuildCommandLine, - swiftSourceDeps->textualModuleDetails - .bridgingHeaderBuildCommandLine); + swiftSourceDeps->bridgingHeaderBuildCommandLine); addIdentifier( swiftSourceDeps->textualModuleDetails.CASFileSystemRootID); break; @@ -1193,13 +1199,13 @@ void ModuleDependenciesCacheSerializer::collectStringsAndArrays( addIdentifier(clangDeps->moduleMapFile); addIdentifier(clangDeps->contextHash); addStringArray(moduleID, ModuleIdentifierArrayKind::NonPathCommandLine, - clangDeps->nonPathCommandLine); + clangDeps->buildCommandLine); addStringArray(moduleID, ModuleIdentifierArrayKind::FileDependencies, clangDeps->fileDependencies); addStringArray(moduleID, ModuleIdentifierArrayKind::CapturedPCMArgs, clangDeps->capturedPCMArgs); addIdentifier(clangDeps->CASFileSystemRootID); - addIdentifier(clangDeps->clangIncludeTreeRoot); + addIdentifier(clangDeps->CASClangIncludeTreeRootID); addIdentifier(clangDeps->moduleCacheKey); break; } diff --git a/lib/DependencyScan/ScanDependencies.cpp b/lib/DependencyScan/ScanDependencies.cpp index d98120d30fa11..2feaba20cbce0 100644 --- a/lib/DependencyScan/ScanDependencies.cpp +++ b/lib/DependencyScan/ScanDependencies.cpp @@ -378,8 +378,8 @@ static llvm::Error resolveExplicitModuleInputs( case swift::ModuleDependencyKind::SwiftSource: { auto sourceDepDetails = depInfo->getAsSwiftSourceModule(); assert(sourceDepDetails && "Expected source dependency"); - if (sourceDepDetails->textualModuleDetails.bridgingHeaderIncludeTreeRoot - .empty()) { + if (sourceDepDetails->textualModuleDetails + .CASBridgingHeaderIncludeTreeRootID.empty()) { if (!sourceDepDetails->textualModuleDetails.bridgingSourceFiles .empty()) { if (auto tracker = @@ -396,7 +396,7 @@ static llvm::Error resolveExplicitModuleInputs( } } else includeTrees.push_back(sourceDepDetails->textualModuleDetails - .bridgingHeaderIncludeTreeRoot); + .CASBridgingHeaderIncludeTreeRootID); break; } default: @@ -1338,16 +1338,20 @@ generateFullDependencyGraph(CompilerInstance &instance, moduleInterfacePath, create_set(swiftTextualDeps->compiledModuleCandidates), bridgingHeaderPath, - create_set(swiftTextualDeps->textualModuleDetails.bridgingSourceFiles), - create_set(swiftTextualDeps->textualModuleDetails.bridgingModuleDependencies), + create_set( + swiftTextualDeps->textualModuleDetails.bridgingSourceFiles), + create_set(swiftTextualDeps->textualModuleDetails + .bridgingModuleDependencies), create_set(bridgedOverlayDependencyNames), create_set(swiftTextualDeps->textualModuleDetails.buildCommandLine), - create_set(swiftTextualDeps->textualModuleDetails.bridgingHeaderBuildCommandLine), + /*bridgingHeaderBuildCommand*/ create_set({}), create_set(swiftTextualDeps->textualModuleDetails.extraPCMArgs), create_clone(swiftTextualDeps->contextHash.c_str()), swiftTextualDeps->isFramework, - create_clone(swiftTextualDeps->textualModuleDetails.CASFileSystemRootID.c_str()), - create_clone(swiftTextualDeps->textualModuleDetails.bridgingHeaderIncludeTreeRoot.c_str()), + create_clone(swiftTextualDeps->textualModuleDetails + .CASFileSystemRootID.c_str()), + create_clone(swiftTextualDeps->textualModuleDetails + .CASBridgingHeaderIncludeTreeRootID.c_str()), create_clone(swiftTextualDeps->moduleCacheKey.c_str())}; } else if (swiftSourceDeps) { swiftscan_string_ref_t moduleInterfacePath = create_null(); @@ -1366,17 +1370,23 @@ generateFullDependencyGraph(CompilerInstance &instance, moduleInterfacePath, create_empty_set(), bridgingHeaderPath, - create_set(swiftSourceDeps->textualModuleDetails.bridgingSourceFiles), - create_set(swiftSourceDeps->textualModuleDetails.bridgingModuleDependencies), + create_set( + swiftSourceDeps->textualModuleDetails.bridgingSourceFiles), + create_set(swiftSourceDeps->textualModuleDetails + .bridgingModuleDependencies), create_set(bridgedOverlayDependencyNames), create_set(swiftSourceDeps->textualModuleDetails.buildCommandLine), - create_set(swiftSourceDeps->textualModuleDetails.bridgingHeaderBuildCommandLine), + create_set(swiftSourceDeps->bridgingHeaderBuildCommandLine), create_set(swiftSourceDeps->textualModuleDetails.extraPCMArgs), - /*contextHash*/create_null(), - /*isFramework*/false, - /*CASFS*/create_clone(swiftSourceDeps->textualModuleDetails.CASFileSystemRootID.c_str()), - /*IncludeTree*/create_clone(swiftSourceDeps->textualModuleDetails.bridgingHeaderIncludeTreeRoot.c_str()), - /*CacheKey*/create_clone("")}; + /*contextHash*/ create_null(), + /*isFramework*/ false, + /*CASFS*/ + create_clone(swiftSourceDeps->textualModuleDetails + .CASFileSystemRootID.c_str()), + /*IncludeTree*/ + create_clone(swiftSourceDeps->textualModuleDetails + .CASBridgingHeaderIncludeTreeRootID.c_str()), + /*CacheKey*/ create_clone("")}; } else if (swiftPlaceholderDeps) { details->kind = SWIFTSCAN_DEPENDENCY_INFO_SWIFT_PLACEHOLDER; details->swift_placeholder_details = { @@ -1397,12 +1407,11 @@ generateFullDependencyGraph(CompilerInstance &instance, details->clang_details = { create_clone(clangDeps->moduleMapFile.c_str()), create_clone(clangDeps->contextHash.c_str()), - create_set(clangDeps->nonPathCommandLine), + create_set(clangDeps->buildCommandLine), create_set(clangDeps->capturedPCMArgs), create_clone(clangDeps->CASFileSystemRootID.c_str()), - create_clone(clangDeps->clangIncludeTreeRoot.c_str()), - create_clone(clangDeps->moduleCacheKey.c_str()) - }; + create_clone(clangDeps->CASClangIncludeTreeRootID.c_str()), + create_clone(clangDeps->moduleCacheKey.c_str())}; } return details; }; diff --git a/test/CAS/Inputs/SwiftDepsExtractor.py b/test/CAS/Inputs/SwiftDepsExtractor.py index a06df5b6e0add..9666127c2ed86 100755 --- a/test/CAS/Inputs/SwiftDepsExtractor.py +++ b/test/CAS/Inputs/SwiftDepsExtractor.py @@ -1,32 +1,34 @@ #!/usr/bin/env python3 +# +# Usage: SwiftDepsExtractor.py file.json ModuleName Key import json import sys -## SwiftDepsExtractor.py file.json ModuleName Key input_json = sys.argv[1] module_name = sys.argv[2] key = sys.argv[3] -mode = "swift" +mode = 'swift' -if module_name.startswith("clang:"): - mode = "clang" +if module_name.startswith('clang:'): + mode = 'clang' module_name = module_name[6:] -elif module_name.startswith("swiftPrebuiltExternal:"): - mode = "swiftPrebuiltExternal" +elif module_name.startswith('swiftPrebuiltExternal:'): + mode = 'swiftPrebuiltExternal' module_name = module_name[22:] with open(input_json, 'r') as file: deps = json.load(file) - extract_next = False - for module in deps['modules']: - if extract_next: - if key in module.keys(): - print(module[key]) - break - print(module['details'][mode][key]) + module_names = deps['modules'][::2] + module_details = deps['modules'][1::2] + for name, detail in zip(module_names, module_details): + if name.get(mode, '') != module_name: + continue + + if key in detail.keys(): + print(detail[key]) break - if module.get(mode, '') == module_name: - extract_next = True + print(detail['details'][mode][key]) + break diff --git a/test/CAS/cas-explicit-module-map.swift b/test/CAS/cas-explicit-module-map.swift index f54aad1e0aae3..fec0dd188d2ec 100644 --- a/test/CAS/cas-explicit-module-map.swift +++ b/test/CAS/cas-explicit-module-map.swift @@ -1,6 +1,8 @@ // RUN: %empty-directory(%t) // RUN: mkdir -p %t/cas // RUN: split-file %s %t +// RUN: %target-swift-emit-pcm -module-cache-path %t/clang-module-cache -module-name SwiftShims %swift-lib-dir/swift/shims/module.modulemap -o %t/SwiftShims.pcm +// RUN: %target-swift-emit-pcm -module-cache-path %t/clang-module-cache -module-name _SwiftConcurrencyShims %swift-lib-dir/swift/shims/module.modulemap -o %t/_SwiftConcurrencyShims.pcm // RUN: %target-swift-frontend -emit-module -module-cache-path %t/clang-module-cache %t/A.swift -o %t/A.swiftmodule -swift-version 5 // RUN: %target-swift-frontend -emit-module -module-cache-path %t/clang-module-cache %t/B.swift -o %t/B.swiftmodule -I %t -swift-version 5 // RUN: %target-swift-frontend -scan-dependencies -module-cache-path %t/clang-module-cache %t/Test.swift -o %t/deps.json -I %t -swift-version 5 -enable-cas -cas-path %t/cas @@ -30,6 +32,14 @@ // RUN: llvm-cas --cas %t/cas --make-node --data %t/kind.blob @%t/String.key @%t/schema.casid > %t/String.casid // RUN: llvm-cas --cas %t/cas --put-cache-key @%t/String.key @%t/String.casid +// RUN: llvm-cas --cas %t/cas --make-blob --data %t/SwiftShims.pcm | tr -d '\n' > %t/Shims.key +// RUN: llvm-cas --cas %t/cas --make-node --data %t/kind.blob @%t/Shims.key @%t/schema.casid > %t/Shims.casid +// RUN: llvm-cas --cas %t/cas --put-cache-key @%t/Shims.key @%t/Shims.casid + +// RUN: llvm-cas --cas %t/cas --make-blob --data %t/_SwiftConcurrencyShims.pcm | tr -d '\n' > %t/ConcurrencyShims.key +// RUN: llvm-cas --cas %t/cas --make-node --data %t/kind.blob @%t/ConcurrencyShims.key @%t/schema.casid > %t/ConcurrencyShims.casid +// RUN: llvm-cas --cas %t/cas --put-cache-key @%t/ConcurrencyShims.key @%t/ConcurrencyShims.casid + // RUN: echo "[{" > %/t/map.json // RUN: echo "\"moduleName\": \"A\"," >> %/t/map.json // RUN: echo "\"modulePath\": \"A.swiftmodule\"," >> %/t/map.json @@ -77,6 +87,22 @@ // RUN: cat %t/String.key >> %/t/map.json // RUN: echo "\"," >> %/t/map.json // RUN: echo "\"isFramework\": false" >> %/t/map.json +// RUN: echo "}," >> %/t/map.json +// RUN: echo "{" >> %/t/map.json +// RUN: echo "\"moduleName\": \"SwiftShims\"," >> %/t/map.json +// RUN: echo "\"clangModulePath\": \"SwiftShims.pcm\"," >> %/t/map.json +// RUN: echo -n "\"clangModuleCacheKey\": \"" >> %/t/map.json +// RUN: cat %t/Shims.key >> %/t/map.json +// RUN: echo "\"," >> %/t/map.json +// RUN: echo "\"isFramework\": false" >> %/t/map.json +// RUN: echo "}," >> %/t/map.json +// RUN: echo "{" >> %/t/map.json +// RUN: echo "\"moduleName\": \"_SwiftConcurrencyShims\"," >> %/t/map.json +// RUN: echo "\"clangModulePath\": \"_SwiftConcurrency.pcm\"," >> %/t/map.json +// RUN: echo -n "\"clangModuleCacheKey\": \"" >> %/t/map.json +// RUN: cat %t/ConcurrencyShims.key >> %/t/map.json +// RUN: echo "\"," >> %/t/map.json +// RUN: echo "\"isFramework\": false" >> %/t/map.json // RUN: echo "}]" >> %/t/map.json // RUN: llvm-cas --cas %t/cas --make-blob --data %t/map.json > %t/map.casid From 56617acdaaddf51983c4fb9d02b4761926f91308 Mon Sep 17 00:00:00 2001 From: Steven Wu Date: Fri, 9 Jun 2023 09:44:01 -0700 Subject: [PATCH 2/2] [ScanDependency] Fix dependency output json format Remove a trailing common that makes the JSON output from `-scan-dependencies` to be invalide JSON format. --- lib/DependencyScan/ScanDependencies.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/DependencyScan/ScanDependencies.cpp b/lib/DependencyScan/ScanDependencies.cpp index 2feaba20cbce0..a3a09efb75b30 100644 --- a/lib/DependencyScan/ScanDependencies.cpp +++ b/lib/DependencyScan/ScanDependencies.cpp @@ -1111,7 +1111,7 @@ static void writeJSON(llvm::raw_ostream &out, if (hasOverlayDependencies) { writeDependencies(out, swiftTextualDeps->swift_overlay_module_dependencies, "swiftOverlayDependencies", 5, - /*trailingComma=*/true); + /*trailingComma=*/false); } } else if (swiftPlaceholderDeps) { out << "\"swiftPlaceholder\": {\n";