From 1f395ec1a84f1cd13c22e2e08fc2a860a6004268 Mon Sep 17 00:00:00 2001 From: Xi Ge Date: Tue, 11 Apr 2023 12:09:55 -0700 Subject: [PATCH] Frontend: infer default blocklists to use when the explicit paths aren't given by swift-driver Although swift-driver always passes down these blocklist for the compiler to consume, some frontend tools, like ABI checker, are invoked by the build system directly. Therefore, we need to teach the compiler to infer these blocklist files like prebuilt module cache. --- include/swift/Frontend/Frontend.h | 3 +++ lib/Frontend/CompilerInvocation.cpp | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/include/swift/Frontend/Frontend.h b/include/swift/Frontend/Frontend.h index 9669e98d8488b..058c47bbab91a 100644 --- a/include/swift/Frontend/Frontend.h +++ b/include/swift/Frontend/Frontend.h @@ -235,6 +235,9 @@ class CompilerInvocation { /// options have been parsed. void setDefaultPrebuiltCacheIfNecessary(); + /// If we haven't explicitly passed -blocklist-paths, set it to the default value. + void setDefaultBlocklistsIfNecessary(); + /// Computes the runtime resource path relative to the given Swift /// executable. static void computeRuntimeResourcePathFromExecutablePath( diff --git a/lib/Frontend/CompilerInvocation.cpp b/lib/Frontend/CompilerInvocation.cpp index f855e8defcbbf..6856112b062bc 100644 --- a/lib/Frontend/CompilerInvocation.cpp +++ b/lib/Frontend/CompilerInvocation.cpp @@ -159,6 +159,31 @@ void CompilerInvocation::setDefaultPrebuiltCacheIfNecessary() { (llvm::Twine(pair.first) + "preferred-interfaces" + pair.second).str(); } +void CompilerInvocation::setDefaultBlocklistsIfNecessary() { + if (!LangOpts.BlocklistConfigFilePaths.empty()) + return; + if (SearchPathOpts.RuntimeResourcePath.empty()) + return; + // XcodeDefault.xctoolchain/usr/lib/swift + SmallString<64> blocklistDir{SearchPathOpts.RuntimeResourcePath}; + // XcodeDefault.xctoolchain/usr/lib + llvm::sys::path::remove_filename(blocklistDir); + // XcodeDefault.xctoolchain/usr + llvm::sys::path::remove_filename(blocklistDir); + // XcodeDefault.xctoolchain/usr/local/lib/swift/blocklists + llvm::sys::path::append(blocklistDir, "local", "lib", "swift", "blocklists"); + std::error_code EC; + if (llvm::sys::fs::is_directory(blocklistDir)) { + for (llvm::sys::fs::directory_iterator F(blocklistDir, EC), FE; + F != FE; F.increment(EC)) { + StringRef ext = llvm::sys::path::extension(F->path()); + if (ext == "yml" || ext == "yaml") { + LangOpts.BlocklistConfigFilePaths.push_back(F->path()); + } + } + } +} + static void updateRuntimeLibraryPaths(SearchPathOptions &SearchPathOpts, llvm::Triple &Triple) { llvm::SmallString<128> LibPath(SearchPathOpts.RuntimeResourcePath); @@ -2870,6 +2895,7 @@ bool CompilerInvocation::parseArgs( updateRuntimeLibraryPaths(SearchPathOpts, LangOpts.Target); setDefaultPrebuiltCacheIfNecessary(); + setDefaultBlocklistsIfNecessary(); // Now that we've parsed everything, setup some inter-option-dependent state. setIRGenOutputOptsFromFrontendOptions(IRGenOpts, FrontendOpts);