Skip to content

Commit 72f32d7

Browse files
[CAS] Support clang include tree
Using clang include tree for dependency scanning and building for all clang modules and PCHs.
1 parent b95d3b6 commit 72f32d7

26 files changed

+804
-165
lines changed

include/swift-c/DependencyScan/DependencyScan.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ SWIFTSCAN_PUBLIC swiftscan_string_set_t *
139139
swiftscan_swift_textual_detail_get_command_line(
140140
swiftscan_module_details_t details);
141141

142+
SWIFTSCAN_PUBLIC swiftscan_string_set_t *
143+
swiftscan_swift_textual_detail_get_bridging_pch_command_line(
144+
swiftscan_module_details_t details);
145+
142146
SWIFTSCAN_PUBLIC swiftscan_string_set_t *
143147
swiftscan_swift_textual_detail_get_extra_pcm_args(
144148
swiftscan_module_details_t details);

include/swift/AST/ModuleDependencies.h

Lines changed: 62 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@
2626
#include "llvm/ADT/IntrusiveRefCntPtr.h"
2727
#include "llvm/ADT/Optional.h"
2828
#include "llvm/ADT/StringSet.h"
29+
#include "llvm/CAS/CASProvidingFileSystem.h"
2930
#include "llvm/CAS/CASReference.h"
3031
#include "llvm/CAS/CachingOnDiskFileSystem.h"
32+
#include "llvm/CAS/ObjectStore.h"
3133
#include "llvm/Support/Mutex.h"
3234
#include "llvm/Support/Error.h"
3335
#include "llvm/Support/VirtualFileSystem.h"
@@ -128,9 +130,12 @@ class ModuleDependencyInfoStorageBase {
128130
struct CommonSwiftTextualModuleDependencyDetails {
129131
CommonSwiftTextualModuleDependencyDetails(
130132
ArrayRef<StringRef> extraPCMArgs, ArrayRef<StringRef> buildCommandLine,
133+
ArrayRef<StringRef> bridgingHeaderBuildCommandLine,
131134
const std::string &CASFileSystemRootID)
132135
: extraPCMArgs(extraPCMArgs.begin(), extraPCMArgs.end()),
133136
buildCommandLine(buildCommandLine.begin(), buildCommandLine.end()),
137+
bridgingHeaderBuildCommandLine(bridgingHeaderBuildCommandLine.begin(),
138+
bridgingHeaderBuildCommandLine.end()),
134139
CASFileSystemRootID(CASFileSystemRootID) {}
135140

136141
/// To build a PCM to be used by this Swift module, we need to append these
@@ -155,8 +160,14 @@ struct CommonSwiftTextualModuleDependencyDetails {
155160
/// interface.
156161
std::vector<std::string> buildCommandLine;
157162

163+
/// The Swift frontend invocation arguments to build bridging header.
164+
std::vector<std::string> bridgingHeaderBuildCommandLine;
165+
158166
/// CASID for the Root of CASFS. Empty if CAS is not used.
159167
std::string CASFileSystemRootID;
168+
169+
/// CASID for the Root of bridgingHeaderClangIncludeTree. Empty if not used.
170+
std::string bridgingHeaderIncludeTreeRoot;
160171
};
161172

162173
/// Describes the dependencies of a Swift module described by an Swift interface file.
@@ -202,7 +213,7 @@ class SwiftInterfaceModuleDependenciesStorage :
202213
compiledModuleCandidates(compiledModuleCandidates.begin(),
203214
compiledModuleCandidates.end()),
204215
contextHash(contextHash), isFramework(isFramework),
205-
textualModuleDetails(extraPCMArgs, buildCommandLine, RootID),
216+
textualModuleDetails(extraPCMArgs, buildCommandLine, {}, RootID),
206217
moduleCacheKey(ModuleCacheKey)
207218
{}
208219

@@ -218,10 +229,6 @@ class SwiftInterfaceModuleDependenciesStorage :
218229
textualModuleDetails.buildCommandLine = newCommandLine;
219230
}
220231

221-
void updateCASFileSystemID(const std::string &ID) {
222-
textualModuleDetails.CASFileSystemRootID = ID;
223-
}
224-
225232
void updateModuleCacheKey(const std::string &Key) {
226233
moduleCacheKey = Key;
227234
}
@@ -243,11 +250,13 @@ class SwiftSourceModuleDependenciesStorage :
243250
/// Collection of module imports that were detected to be `@Testable`
244251
llvm::StringSet<> testableImports;
245252

246-
SwiftSourceModuleDependenciesStorage(const std::string &RootID,
247-
ArrayRef<StringRef> buildCommandLine,
248-
ArrayRef<StringRef> extraPCMArgs)
253+
SwiftSourceModuleDependenciesStorage(
254+
const std::string &RootID, ArrayRef<StringRef> buildCommandLine,
255+
ArrayRef<StringRef> bridgingHeaderBuildCommandLine,
256+
ArrayRef<StringRef> extraPCMArgs)
249257
: ModuleDependencyInfoStorageBase(ModuleDependencyKind::SwiftSource),
250-
textualModuleDetails(extraPCMArgs, buildCommandLine, RootID),
258+
textualModuleDetails(extraPCMArgs, buildCommandLine,
259+
bridgingHeaderBuildCommandLine, RootID),
251260
testableImports(llvm::StringSet<>()) {}
252261

253262
ModuleDependencyInfoStorageBase *clone() const override {
@@ -262,8 +271,9 @@ class SwiftSourceModuleDependenciesStorage :
262271
textualModuleDetails.buildCommandLine = newCommandLine;
263272
}
264273

265-
void updateCASFileSystemID(const std::string &ID) {
266-
textualModuleDetails.CASFileSystemRootID = ID;
274+
void updateBridgingHeaderCommandLine(
275+
const std::vector<std::string> &newCommandLine) {
276+
textualModuleDetails.bridgingHeaderBuildCommandLine = newCommandLine;
267277
}
268278

269279
void addTestableImport(ImportPath::Module module) {
@@ -341,6 +351,9 @@ class ClangModuleDependencyStorage : public ModuleDependencyInfoStorageBase {
341351
/// CASID for the Root of CASFS. Empty if CAS is not used.
342352
std::string CASFileSystemRootID;
343353

354+
/// CASID for the Root of ClangIncludeTree. Empty if not used.
355+
std::string clangIncludeTreeRoot;
356+
344357
/// The cache key for the produced module.
345358
std::string moduleCacheKey;
346359

@@ -352,6 +365,7 @@ class ClangModuleDependencyStorage : public ModuleDependencyInfoStorageBase {
352365
const std::vector<std::string> &fileDependencies,
353366
const std::vector<std::string> &capturedPCMArgs,
354367
const std::string &CASFileSystemRootID,
368+
const std::string &clangIncludeTreeRoot,
355369
const std::string &ModuleCacheKey
356370
) : ModuleDependencyInfoStorageBase(ModuleDependencyKind::Clang),
357371
pcmOutputPath(pcmOutputPath),
@@ -361,6 +375,7 @@ class ClangModuleDependencyStorage : public ModuleDependencyInfoStorageBase {
361375
fileDependencies(fileDependencies),
362376
capturedPCMArgs(capturedPCMArgs),
363377
CASFileSystemRootID(CASFileSystemRootID),
378+
clangIncludeTreeRoot(clangIncludeTreeRoot),
364379
moduleCacheKey(ModuleCacheKey) {}
365380

366381
ModuleDependencyInfoStorageBase *clone() const override {
@@ -473,10 +488,12 @@ class ModuleDependencyInfo {
473488
static ModuleDependencyInfo
474489
forSwiftSourceModule(const std::string &CASFileSystemRootID,
475490
ArrayRef<StringRef> buildCommands,
491+
ArrayRef<StringRef> bridgingHeaderBuildCommands,
476492
ArrayRef<StringRef> extraPCMArgs) {
477493
return ModuleDependencyInfo(
478494
std::make_unique<SwiftSourceModuleDependenciesStorage>(
479-
CASFileSystemRootID, buildCommands, extraPCMArgs));
495+
CASFileSystemRootID, buildCommands, bridgingHeaderBuildCommands,
496+
extraPCMArgs));
480497
}
481498

482499
/// Describe the module dependencies for a Clang module that can be
@@ -489,12 +506,13 @@ class ModuleDependencyInfo {
489506
const std::vector<std::string> &fileDependencies,
490507
const std::vector<std::string> &capturedPCMArgs,
491508
const std::string &CASFileSystemRootID,
509+
const std::string &clangIncludeTreeRoot,
492510
const std::string &moduleCacheKey) {
493511
return ModuleDependencyInfo(
494512
std::make_unique<ClangModuleDependencyStorage>(
495513
pcmOutputPath, moduleMapFile, contextHash,
496514
nonPathCommandLine, fileDependencies, capturedPCMArgs,
497-
CASFileSystemRootID, moduleCacheKey));
515+
CASFileSystemRootID, clangIncludeTreeRoot, moduleCacheKey));
498516
}
499517

500518
/// Describe a placeholder dependency swift module.
@@ -593,13 +611,17 @@ class ModuleDependencyInfo {
593611
llvm_unreachable("Unexpected type");
594612
}
595613

596-
void updateCASFileSystemID(const std::string &ID) {
597-
if (isSwiftInterfaceModule())
598-
return cast<SwiftInterfaceModuleDependenciesStorage>(storage.get())
599-
->updateCASFileSystemID(ID);
600-
else if (isSwiftSourceModule())
614+
std::vector<std::string> getBridgingHeaderCommandline() const {
615+
if (auto *detail = getAsSwiftSourceModule())
616+
return detail->textualModuleDetails.bridgingHeaderBuildCommandLine;
617+
return {};
618+
}
619+
620+
void updateBridgingHeaderCommandLine(
621+
const std::vector<std::string> &newCommandLine) {
622+
if (isSwiftSourceModule())
601623
return cast<SwiftSourceModuleDependenciesStorage>(storage.get())
602-
->updateCASFileSystemID(ID);
624+
->updateBridgingHeaderCommandLine(newCommandLine);
603625
llvm_unreachable("Unexpected type");
604626
}
605627

@@ -695,6 +717,12 @@ class ModuleDependencyInfo {
695717
/// Get CAS Filesystem RootID.
696718
Optional<std::string> getCASFSRootID() const;
697719

720+
/// Get Clang Include Tree ID.
721+
Optional<std::string> getClangIncludeTree() const;
722+
723+
/// Get bridging header Include Tree ID.
724+
Optional<std::string> getBridgingHeaderIncludeTree() const;
725+
698726
/// Get module output path.
699727
std::string getModuleOutputPath() const;
700728

@@ -711,6 +739,9 @@ class ModuleDependencyInfo {
711739
void addBridgingModuleDependency(StringRef module,
712740
llvm::StringSet<> &alreadyAddedModules);
713741

742+
/// Add bridging header include tree.
743+
void addBridgingHeaderIncludeTree(StringRef ID);
744+
714745
/// Collect a map from a secondary module name to a list of cross-import
715746
/// overlays, when this current module serves as the primary module.
716747
llvm::StringMap<llvm::SmallSetVector<Identifier, 4>>
@@ -773,6 +804,12 @@ class SwiftDependencyScanningService {
773804
/// CachingOnDiskFileSystem for dependency tracking.
774805
llvm::IntrusiveRefCntPtr<llvm::cas::CachingOnDiskFileSystem> CacheFS;
775806

807+
/// If use clang include tree.
808+
bool UseClangIncludeTree = false;
809+
810+
/// CAS ObjectStore Instance.
811+
std::shared_ptr<llvm::cas::ObjectStore> CAS;
812+
776813
/// The common dependencies that is needed for every swift compiler instance.
777814
std::vector<std::string> CommonDependencyFiles;
778815

@@ -826,7 +863,7 @@ class SwiftDependencyScanningService {
826863
return getCacheForScanningContextHash(scanningContextHash)->alreadySeenClangModules;
827864
}
828865

829-
bool usingCachingFS() const { return (bool)CacheFS; }
866+
bool usingCachingFS() const { return !UseClangIncludeTree && (bool)CacheFS; }
830867

831868
llvm::cas::CachingOnDiskFileSystem &getSharedCachingFS() const {
832869
assert(CacheFS && "Expect CachingOnDiskFileSystem");
@@ -841,9 +878,13 @@ class SwiftDependencyScanningService {
841878
}
842879

843880
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> getClangScanningFS() const {
844-
if (CacheFS)
881+
if (usingCachingFS())
845882
return CacheFS->createProxyFS();
846883

884+
if (UseClangIncludeTree)
885+
return llvm::cas::createCASProvidingFileSystem(
886+
CAS, llvm::vfs::createPhysicalFileSystem());
887+
847888
return llvm::vfs::createPhysicalFileSystem();
848889
}
849890

include/swift/Basic/LangOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -890,6 +890,9 @@ namespace swift {
890890
/// built and provided to the compiler invocation.
891891
bool DisableImplicitClangModules = false;
892892

893+
/// Enable ClangIncludeTree for explicit module builds.
894+
bool UseClangIncludeTree = false;
895+
893896
/// Return a hash code of any components from these options that should
894897
/// contribute to a Swift Bridging PCH hash.
895898
llvm::hash_code getPCHHashComponents() const {

include/swift/ClangImporter/ClangImporter.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ namespace clang {
5454
namespace tooling {
5555
namespace dependencies {
5656
struct ModuleDeps;
57+
struct TranslationUnitDeps;
5758
using ModuleDepsGraph = std::vector<ModuleDeps>;
5859
}
5960
}
@@ -431,6 +432,10 @@ class ClangImporter final : public ClangModuleLoader {
431432
ModuleDependenciesCache &cache,
432433
const clang::tooling::dependencies::ModuleDepsGraph &clangDependencies);
433434

435+
void recordBridgingHeaderOptions(
436+
ModuleDependencyInfo &MDI,
437+
const clang::tooling::dependencies::TranslationUnitDeps &deps);
438+
434439
Optional<const ModuleDependencyInfo*> getModuleDependencies(
435440
StringRef moduleName, ModuleDependenciesCache &cache,
436441
InterfaceSubContextDelegate &delegate,

include/swift/DependencyScan/DependencyScanImpl.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ typedef struct {
8686
/// Options to the compile command required to build this module interface
8787
swiftscan_string_set_t *command_line;
8888

89+
/// Options to the compile command required to build bridging header.
90+
swiftscan_string_set_t *bridging_pch_command_line;
91+
8992
/// To build a PCM to be used by this Swift module, we need to append these
9093
/// arguments to the generic PCM build arguments reported from the dependency
9194
/// graph.
@@ -100,6 +103,9 @@ typedef struct {
100103
/// The CASID for CASFileSystemRoot
101104
swiftscan_string_ref_t cas_fs_root_id;
102105

106+
/// The CASID for bridging header include tree
107+
swiftscan_string_ref_t bridging_header_include_tree;
108+
103109
/// ModuleCacheKey
104110
swiftscan_string_ref_t module_cache_key;
105111
} swiftscan_swift_textual_details_t;
@@ -152,6 +158,9 @@ typedef struct {
152158
/// The CASID for CASFileSystemRoot
153159
swiftscan_string_ref_t cas_fs_root_id;
154160

161+
/// The CASID for CASFileSystemRoot
162+
swiftscan_string_ref_t clang_include_tree;
163+
155164
/// ModuleCacheKey
156165
swiftscan_string_ref_t module_cache_key;
157166
} swiftscan_clang_details_t;

include/swift/DependencyScan/SerializedModuleDependencyCacheFormat.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ using SwiftInterfaceModuleDetailsLayout =
142142
FileIDArrayIDField, // bridgingModuleDependencies
143143
DependencyIDArrayIDField, // swiftOverlayDependencies
144144
IdentifierIDField, // CASFileSystemRootID
145+
IdentifierIDField, // bridgingHeaderIncludeTree
145146
IdentifierIDField // moduleCacheKey
146147
>;
147148

@@ -154,7 +155,9 @@ using SwiftSourceModuleDetailsLayout =
154155
FileIDArrayIDField, // bridgingModuleDependencies
155156
DependencyIDArrayIDField, // swiftOverlayDependencies
156157
IdentifierIDField, // CASFileSystemRootID
157-
FlagIDArrayIDField // buildCommandLine
158+
IdentifierIDField, // bridgingHeaderIncludeTree
159+
FlagIDArrayIDField, // buildCommandLine
160+
FlagIDArrayIDField // bridgingHeaderBuildCommandLine
158161
>;
159162

160163
using SwiftBinaryModuleDetailsLayout =
@@ -182,6 +185,7 @@ using ClangModuleDetailsLayout =
182185
FileIDArrayIDField, // fileDependencies
183186
FlagIDArrayIDField, // capturedPCMArgs
184187
IdentifierIDField, // CASFileSystemRootID
188+
IdentifierIDField, // clangIncludeTreeRoot
185189
IdentifierIDField // moduleCacheKey
186190
>;
187191
} // namespace graph_block

include/swift/Frontend/CachingUtils.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "llvm/CAS/ActionCache.h"
2020
#include "llvm/CAS/ObjectStore.h"
2121
#include "llvm/CAS/CASReference.h"
22+
#include "llvm/Support/VirtualFileSystem.h"
2223
#include "llvm/Support/VirtualOutputBackend.h"
2324
#include <memory>
2425

@@ -54,6 +55,10 @@ llvm::Error storeCachedCompilerOutput(llvm::cas::ObjectStore &CAS,
5455
StringRef CorrespondingInput,
5556
file_types::ID OutputKind);
5657

58+
llvm::Expected<llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>>
59+
createCASFileSystem(llvm::cas::ObjectStore &CAS, ArrayRef<std::string> FSRoots,
60+
ArrayRef<std::string> IncludeTreeRoots);
61+
5762
namespace cas {
5863
/// Helper class to manage CAS/Caching from libSwiftScan C APIs.
5964
class CachingTool {

include/swift/Frontend/FrontendOptions.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,10 @@ class FrontendOptions {
129129
std::string CASPath;
130130

131131
/// CASFS Root.
132-
std::string CASFSRootID;
132+
std::vector<std::string> CASFSRootIDs;
133+
134+
/// Clang Include Trees.
135+
std::vector<std::string> ClangIncludeTrees;
133136

134137
/// Number of retry opening an input file if the previous opening returns
135138
/// bad file descriptor error.

include/swift/Option/Options.td

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1833,6 +1833,10 @@ def bridging_header_pch_key : Separate<["-"], "bridging-header-pch-key">,
18331833
Flags<[FrontendOption, HelpHidden, ArgumentIsPath]>,
18341834
HelpText<"Cache Key for bridging header pch">;
18351835

1836+
def clang_include_tree: Flag<["-"], "clang-include-tree">,
1837+
Flags<[FrontendOption, NoDriverOption]>,
1838+
HelpText<"Use clang include tree">;
1839+
18361840

18371841
// END ONLY SUPPORTED IN NEW DRIVER
18381842

@@ -1844,6 +1848,10 @@ def cas_fs: Separate<["-"], "cas-fs">,
18441848
Flags<[FrontendOption, NoDriverOption]>,
18451849
HelpText<"Root CASID for CAS FileSystem">, MetaVarName<"<cas-id>">;
18461850

1851+
def clang_include_tree_root: Separate<["-"], "clang-include-tree-root">,
1852+
Flags<[FrontendOption, NoDriverOption]>,
1853+
HelpText<"Clang Include Tree CASID">, MetaVarName<"<cas-id>">;
1854+
18471855
def load_plugin_library:
18481856
Separate<["-"], "load-plugin-library">,
18491857
Flags<[FrontendOption, DoesNotAffectIncrementalBuild, ArgumentIsPath]>,

0 commit comments

Comments
 (0)