1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
|
//===---------- ScanningLoaders.cpp - Compute module dependencies ---------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
#include "swift/AST/ASTContext.h"
#include "swift/AST/DiagnosticEngine.h"
#include "swift/AST/DiagnosticSuppression.h"
#include "swift/AST/DiagnosticsFrontend.h"
#include "swift/AST/DiagnosticsSema.h"
#include "swift/AST/ModuleDependencies.h"
#include "swift/AST/ModuleLoader.h"
#include "swift/AST/SourceFile.h"
#include "swift/AST/TypeCheckRequests.h"
#include "swift/Basic/FileTypes.h"
#include "swift/Basic/PrettyStackTrace.h"
#include "swift/Frontend/ModuleInterfaceLoader.h"
#include "swift/Serialization/ScanningLoaders.h"
#include "swift/Serialization/SerializedModuleLoader.h"
#include "swift/Subsystems.h"
#include "llvm/ADT/IntrusiveRefCntPtr.h"
#include "llvm/ADT/SetOperations.h"
#include "llvm/CAS/CachingOnDiskFileSystem.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/PrefixMapper.h"
#include "llvm/Support/Threading.h"
#include "llvm/Support/VirtualFileSystem.h"
#include <algorithm>
#include <system_error>
using namespace swift;
std::error_code SwiftModuleScanner::findModuleFilesInDirectory(
ImportPath::Element ModuleID, const SerializedModuleBaseName &BaseName,
SmallVectorImpl<char> *ModuleInterfacePath,
SmallVectorImpl<char> *ModuleInterfaceSourcePath,
std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer,
std::unique_ptr<llvm::MemoryBuffer> *ModuleSourceInfoBuffer,
bool skipBuildingInterface, bool IsFramework,
bool isTestableDependencyLookup) {
using namespace llvm::sys;
auto &fs = *Ctx.SourceMgr.getFileSystem();
auto ModPath = BaseName.getName(file_types::TY_SwiftModuleFile);
auto InPath = BaseName.findInterfacePath(fs, Ctx);
if (LoadMode == ModuleLoadingMode::OnlySerialized || !InPath) {
if (fs.exists(ModPath)) {
// The module file will be loaded directly.
auto dependencies =
scanModuleFile(ModPath, IsFramework, isTestableDependencyLookup);
if (dependencies) {
this->dependencies = std::move(dependencies.get());
return std::error_code();
}
return dependencies.getError();
}
return std::make_error_code(std::errc::no_such_file_or_directory);
}
assert(InPath);
auto dependencies =
scanInterfaceFile(*InPath, IsFramework, isTestableDependencyLookup);
if (dependencies) {
this->dependencies = std::move(dependencies.get());
return std::error_code();
}
return dependencies.getError();
}
bool PlaceholderSwiftModuleScanner::findModule(
ImportPath::Element moduleID, SmallVectorImpl<char> *moduleInterfacePath,
SmallVectorImpl<char> *moduleInterfaceSourcePath,
std::unique_ptr<llvm::MemoryBuffer> *moduleBuffer,
std::unique_ptr<llvm::MemoryBuffer> *moduleDocBuffer,
std::unique_ptr<llvm::MemoryBuffer> *moduleSourceInfoBuffer,
bool skipBuildingInterface, bool isTestableDependencyLookup,
bool &isFramework, bool &isSystemModule) {
StringRef moduleName = Ctx.getRealModuleName(moduleID.Item).str();
auto it = PlaceholderDependencyModuleMap.find(moduleName);
if (it == PlaceholderDependencyModuleMap.end()) {
return false;
}
auto &moduleInfo = it->getValue();
auto dependencies = ModuleDependencyInfo::forPlaceholderSwiftModuleStub(
moduleInfo.modulePath,
moduleInfo.moduleDocPath.has_value() ? moduleInfo.moduleDocPath.value()
: "",
moduleInfo.moduleSourceInfoPath.has_value()
? moduleInfo.moduleSourceInfoPath.value()
: "");
this->dependencies = std::move(dependencies);
return true;
}
void PlaceholderSwiftModuleScanner::parsePlaceholderModuleMap(
StringRef fileName) {
ExplicitModuleMapParser parser(Allocator);
llvm::StringMap<ExplicitClangModuleInputInfo> ClangDependencyModuleMap;
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> fileBufOrErr =
llvm::MemoryBuffer::getFile(fileName);
if (!fileBufOrErr) {
Ctx.Diags.diagnose(SourceLoc(), diag::explicit_swift_module_map_missing,
fileName);
return;
}
auto result = parser.parseSwiftExplicitModuleMap(
(*fileBufOrErr)->getMemBufferRef(), PlaceholderDependencyModuleMap,
ClangDependencyModuleMap);
if (result == std::errc::invalid_argument) {
Ctx.Diags.diagnose(SourceLoc(),
diag::placeholder_dependency_module_map_corrupted,
fileName);
} else if (result == std::errc::no_such_file_or_directory) {
Ctx.Diags.diagnose(
SourceLoc(), diag::placeholder_dependency_module_map_missing, fileName);
}
}
static std::vector<std::string> getCompiledCandidates(ASTContext &ctx,
StringRef moduleName,
StringRef interfacePath) {
return ctx.getModuleInterfaceChecker()
->getCompiledModuleCandidatesForInterface(moduleName.str(),
interfacePath);
}
llvm::ErrorOr<ModuleDependencyInfo>
SwiftModuleScanner::scanInterfaceFile(Twine moduleInterfacePath,
bool isFramework, bool isTestableImport) {
// Create a module filename.
// FIXME: Query the module interface loader to determine an appropriate
// name for the module, which includes an appropriate hash.
auto newExt = file_types::getExtension(file_types::TY_SwiftModuleFile);
auto realModuleName = Ctx.getRealModuleName(moduleName);
StringRef sdkPath = Ctx.SearchPathOpts.getSDKPath();
llvm::SmallString<32> modulePath = realModuleName.str();
llvm::sys::path::replace_extension(modulePath, newExt);
std::optional<ModuleDependencyInfo> Result;
std::error_code code = astDelegate.runInSubContext(
realModuleName.str(), moduleInterfacePath.str(), sdkPath,
StringRef(), SourceLoc(),
[&](ASTContext &Ctx, ModuleDecl *mainMod, ArrayRef<StringRef> BaseArgs,
ArrayRef<StringRef> PCMArgs, StringRef Hash) {
assert(mainMod);
std::string InPath = moduleInterfacePath.str();
auto compiledCandidates =
getCompiledCandidates(Ctx, realModuleName.str(), InPath);
if (!compiledCandidates.empty() &&
Ctx.SearchPathOpts.ScannerModuleValidation) {
assert(compiledCandidates.size() == 1 &&
"Should only have 1 candidate module");
auto BinaryDep = scanModuleFile(compiledCandidates[0], isFramework,
isTestableImport);
if (BinaryDep) {
Result = *BinaryDep;
return std::error_code();
}
// If return no such file, just fallback to use interface.
if (BinaryDep.getError() != std::errc::no_such_file_or_directory)
return BinaryDep.getError();
}
std::vector<std::string> Args(BaseArgs.begin(), BaseArgs.end());
// Add explicit Swift dependency compilation flags
Args.push_back("-explicit-interface-module-build");
Args.push_back("-disable-implicit-swift-modules");
// Handle clang arguments. For caching build, all arguments are passed
// with `-direct-clang-cc1-module-build`.
if (Ctx.ClangImporterOpts.ClangImporterDirectCC1Scan) {
Args.push_back("-direct-clang-cc1-module-build");
auto *importer =
static_cast<ClangImporter *>(Ctx.getClangModuleLoader());
for (auto &Arg : importer->getSwiftExplicitModuleDirectCC1Args()) {
Args.push_back("-Xcc");
Args.push_back(Arg);
}
} else {
Args.push_back("-Xcc");
Args.push_back("-fno-implicit-modules");
Args.push_back("-Xcc");
Args.push_back("-fno-implicit-module-maps");
}
for (const auto &candidate : compiledCandidates) {
Args.push_back("-candidate-module-file");
Args.push_back(candidate);
}
// Compute the output path and add it to the command line
SmallString<128> outputPathBase(moduleOutputPath);
llvm::sys::path::append(
outputPathBase,
moduleName.str() + "-" + Hash + "." +
file_types::getExtension(file_types::TY_SwiftModuleFile));
Args.push_back("-o");
Args.push_back(outputPathBase.str().str());
// Open the interface file.
auto &fs = *Ctx.SourceMgr.getFileSystem();
auto interfaceBuf = fs.getBufferForFile(moduleInterfacePath);
if (!interfaceBuf) {
return interfaceBuf.getError();
}
// Create a source file.
unsigned bufferID =
Ctx.SourceMgr.addNewSourceBuffer(std::move(interfaceBuf.get()));
auto moduleDecl = ModuleDecl::create(realModuleName, Ctx);
SourceFile::ParsingOptions parsingOpts;
auto sourceFile = new (Ctx) SourceFile(
*moduleDecl, SourceFileKind::Interface, bufferID, parsingOpts);
moduleDecl->addAuxiliaryFile(*sourceFile);
std::vector<StringRef> ArgsRefs(Args.begin(), Args.end());
Result = ModuleDependencyInfo::forSwiftInterfaceModule(
outputPathBase.str().str(), InPath, compiledCandidates, ArgsRefs,
PCMArgs, Hash, isFramework, {}, /*module-cache-key*/ "");
if (Ctx.CASOpts.EnableCaching) {
std::vector<std::string> clangDependencyFiles;
auto clangImporter =
static_cast<ClangImporter *>(Ctx.getClangModuleLoader());
clangImporter->addClangInvovcationDependencies(clangDependencyFiles);
llvm::for_each(clangDependencyFiles, [&](std::string &file) {
Result->addAuxiliaryFile(file);
});
}
// Walk the source file to find the import declarations.
llvm::StringSet<> alreadyAddedModules;
Result->addModuleImports(*sourceFile, alreadyAddedModules,
&Ctx.SourceMgr);
// Collect implicitly imported modules in case they are not explicitly
// printed in the interface file, e.g. SwiftOnoneSupport.
auto &imInfo = mainMod->getImplicitImportInfo();
for (auto import : imInfo.AdditionalUnloadedImports) {
Result->addModuleImport(import.module.getModulePath(),
&alreadyAddedModules, &Ctx.SourceMgr);
}
return std::error_code();
});
if (code) {
return code;
}
return *Result;
}
ModuleDependencyVector SerializedModuleLoaderBase::getModuleDependencies(
Identifier moduleName, StringRef moduleOutputPath,
llvm::IntrusiveRefCntPtr<llvm::cas::CachingOnDiskFileSystem> CacheFS,
const llvm::DenseSet<clang::tooling::dependencies::ModuleID>
&alreadySeenClangModules,
clang::tooling::dependencies::DependencyScanningTool &clangScanningTool,
InterfaceSubContextDelegate &delegate, llvm::TreePathPrefixMapper *mapper,
bool isTestableDependencyLookup) {
ImportPath::Module::Builder builder(moduleName);
auto modulePath = builder.get();
auto moduleId = modulePath.front().Item;
// Do not load interface module if it is testable import.
ModuleLoadingMode MLM =
isTestableDependencyLookup ? ModuleLoadingMode::OnlySerialized : LoadMode;
// Instantiate dependency scanning "loaders".
SmallVector<std::unique_ptr<SwiftModuleScanner>, 2> scanners;
// Placeholder dependencies must be resolved first, to prevent the
// ModuleDependencyScanner from first discovering artifacts of a previous
// build. Such artifacts are captured as compiledModuleCandidates in the
// dependency graph of the placeholder dependency module itself.
// FIXME: submodules?
scanners.push_back(std::make_unique<PlaceholderSwiftModuleScanner>(
Ctx, MLM, moduleId, Ctx.SearchPathOpts.PlaceholderDependencyModuleMap,
delegate, moduleOutputPath));
scanners.push_back(std::make_unique<SwiftModuleScanner>(
Ctx, MLM, moduleId, delegate, moduleOutputPath,
SwiftModuleScanner::MDS_plain));
// Check whether there is a module with this name that we can import.
assert(isa<PlaceholderSwiftModuleScanner>(scanners[0].get()) &&
"Expected PlaceholderSwiftModuleScanner as the first dependency "
"scanner loader.");
for (auto &scanner : scanners) {
if (scanner->canImportModule(modulePath, SourceLoc(), nullptr,
isTestableDependencyLookup)) {
ModuleDependencyVector moduleDependnecies;
moduleDependnecies.push_back(
std::make_pair(ModuleDependencyID{moduleName.str().str(),
scanner->dependencies->getKind()},
*(scanner->dependencies)));
return moduleDependnecies;
}
}
return {};
}
|