mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
ModuleLoader: refactor computePrebuiltModulePath to facilitate dependencies scanner's invocation, NFC
This commit is contained in:
@@ -10,6 +10,7 @@
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "swift/Basic/Platform.h"
|
||||
#include "swift/Serialization/SerializedModuleLoader.h"
|
||||
#include "swift/AST/ASTContext.h"
|
||||
#include "swift/AST/DiagnosticSuppression.h"
|
||||
@@ -21,6 +22,92 @@ using namespace swift;
|
||||
using llvm::ErrorOr;
|
||||
|
||||
namespace {
|
||||
static Optional<StringRef>
|
||||
computePrebuiltModulePathDefault(ASTContext &ctx,
|
||||
StringRef interfacePath,
|
||||
StringRef prebuiltCacheDir,
|
||||
StringRef moduleName,
|
||||
StringRef modulePath,
|
||||
llvm::SmallString<256> &scratch) {
|
||||
namespace path = llvm::sys::path;
|
||||
StringRef sdkPath = ctx.SearchPathOpts.SDKPath;
|
||||
auto &fs = *ctx.SourceMgr.getFileSystem();
|
||||
// Check if the interface file comes from the SDK
|
||||
if (sdkPath.empty() || !hasPrefix(path::begin(interfacePath),
|
||||
path::end(interfacePath),
|
||||
path::begin(sdkPath),
|
||||
path::end(sdkPath)))
|
||||
return None;
|
||||
|
||||
// Assemble the expected path: $PREBUILT_CACHE/Foo.swiftmodule or
|
||||
// $PREBUILT_CACHE/Foo.swiftmodule/arch.swiftmodule. Note that there's no
|
||||
// cache key here.
|
||||
scratch = prebuiltCacheDir;
|
||||
|
||||
// FIXME: Would it be possible to only have architecture-specific names
|
||||
// here? Then we could skip this check.
|
||||
StringRef inParentDirName =
|
||||
path::filename(path::parent_path(interfacePath));
|
||||
if (path::extension(inParentDirName) == ".swiftmodule") {
|
||||
assert(path::stem(inParentDirName) == moduleName);
|
||||
path::append(scratch, inParentDirName);
|
||||
}
|
||||
path::append(scratch, path::filename(modulePath));
|
||||
|
||||
// If there isn't a file at this location, skip returning a path.
|
||||
if (!fs.exists(scratch))
|
||||
return None;
|
||||
|
||||
return scratch.str();
|
||||
}
|
||||
|
||||
/// Hack to deal with build systems (including the Swift standard library, at
|
||||
/// the time of this comment) that aren't yet using target-specific names for
|
||||
/// multi-target swiftmodules, in case the prebuilt cache is.
|
||||
static Optional<StringRef>
|
||||
computeFallbackPrebuiltModulePath(ASTContext &ctx,
|
||||
StringRef interfacePath,
|
||||
StringRef prebuiltCacheDir,
|
||||
StringRef moduleName,
|
||||
StringRef modulePath,
|
||||
llvm::SmallString<256> &scratch) {
|
||||
namespace path = llvm::sys::path;
|
||||
StringRef sdkPath = ctx.SearchPathOpts.SDKPath;
|
||||
auto &fs = *ctx.SourceMgr.getFileSystem();
|
||||
|
||||
// Check if the interface file comes from the SDK
|
||||
if (sdkPath.empty() || !hasPrefix(path::begin(interfacePath),
|
||||
path::end(interfacePath),
|
||||
path::begin(sdkPath),
|
||||
path::end(sdkPath)))
|
||||
return None;
|
||||
|
||||
// If the module isn't target-specific, there's no fallback path.
|
||||
StringRef inParentDirName =
|
||||
path::filename(path::parent_path(interfacePath));
|
||||
if (path::extension(inParentDirName) != ".swiftmodule")
|
||||
return None;
|
||||
|
||||
// If the interface is already using the target-specific name, there's
|
||||
// nothing else to try.
|
||||
auto normalizedTarget = getTargetSpecificModuleTriple(ctx.LangOpts.Target);
|
||||
if (path::stem(modulePath) == normalizedTarget.str())
|
||||
return None;
|
||||
|
||||
// Assemble the expected path:
|
||||
// $PREBUILT_CACHE/Foo.swiftmodule/target.swiftmodule. Note that there's no
|
||||
// cache key here.
|
||||
scratch = prebuiltCacheDir;
|
||||
path::append(scratch, inParentDirName);
|
||||
path::append(scratch, normalizedTarget.str());
|
||||
scratch += ".swiftmodule";
|
||||
|
||||
// If there isn't a file at this location, skip returning a path.
|
||||
if (!fs.exists(scratch))
|
||||
return None;
|
||||
|
||||
return scratch.str();
|
||||
}
|
||||
|
||||
/// A module "loader" that looks for .swiftinterface and .swiftmodule files
|
||||
/// for the purpose of determining dependencies, but does not attempt to
|
||||
@@ -94,6 +181,28 @@ public:
|
||||
};
|
||||
}
|
||||
|
||||
Optional<StringRef>
|
||||
swift::computePrebuiltModulePath(ASTContext &ctx,
|
||||
StringRef interfacePath,
|
||||
StringRef prebuiltCacheDir,
|
||||
StringRef moduleName,
|
||||
llvm::SmallString<256> &scratch) {
|
||||
llvm::SmallString<64> modulePath = llvm::sys::path::filename(interfacePath);
|
||||
llvm::sys::path::replace_extension(modulePath,
|
||||
file_types::getExtension(file_types::TY_SwiftModuleFile));
|
||||
auto defaultPath =
|
||||
computePrebuiltModulePathDefault(ctx, interfacePath, prebuiltCacheDir,
|
||||
moduleName, modulePath, scratch);
|
||||
if (defaultPath.hasValue())
|
||||
return defaultPath;
|
||||
else
|
||||
return computeFallbackPrebuiltModulePath(ctx, interfacePath,
|
||||
prebuiltCacheDir,
|
||||
moduleName,
|
||||
modulePath,
|
||||
scratch);
|
||||
}
|
||||
|
||||
ErrorOr<ModuleDependencies> ModuleDependencyScanner::scanInterfaceFile(
|
||||
Twine moduleInterfacePath) {
|
||||
// Create a module filename.
|
||||
|
||||
Reference in New Issue
Block a user