-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathSearchPathOptions.cpp
224 lines (192 loc) · 8.61 KB
/
SearchPathOptions.cpp
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
//===----------------------- SearchPathOptions.cpp ------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2021 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/SearchPathOptions.h"
#include "swift/Basic/Assertions.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/Support/Errc.h"
using namespace swift;
void ModuleSearchPathLookup::addFilesInPathToLookupTable(
llvm::vfs::FileSystem *FS, StringRef SearchPath, ModuleSearchPathKind Kind,
bool IsSystem, unsigned SearchPathIndex) {
std::error_code Error;
auto entryAlreadyExists = [this](ModuleSearchPathKind Kind,
unsigned SearchPathIndex) -> bool {
return llvm::any_of(LookupTable, [&](const auto &LookupTableEntry) {
return llvm::any_of(
LookupTableEntry.second, [&](ModuleSearchPathPtr ExistingSearchPath) {
return ExistingSearchPath->getKind() == Kind &&
ExistingSearchPath->getIndex() == SearchPathIndex;
});
});
};
assert(!entryAlreadyExists(Kind, SearchPathIndex) &&
"Search path with this kind and index already exists");
ModuleSearchPathPtr TableEntry =
new ModuleSearchPath(SearchPath, Kind, IsSystem, SearchPathIndex);
for (auto Dir = FS->dir_begin(SearchPath, Error);
!Error && Dir != llvm::vfs::directory_iterator(); Dir.increment(Error)) {
StringRef Filename = llvm::sys::path::filename(Dir->path());
LookupTable[Filename].push_back(TableEntry);
}
}
void ModuleSearchPathLookup::rebuildLookupTable(const SearchPathOptions *Opts,
llvm::vfs::FileSystem *FS,
bool IsOSDarwin) {
clearLookupTable();
for (auto Entry : llvm::enumerate(Opts->getImportSearchPaths())) {
addFilesInPathToLookupTable(FS, Entry.value().Path,
ModuleSearchPathKind::Import,
Entry.value().IsSystem, Entry.index());
}
for (auto Entry : llvm::enumerate(Opts->getFrameworkSearchPaths())) {
addFilesInPathToLookupTable(FS, Entry.value().Path, ModuleSearchPathKind::Framework,
Entry.value().IsSystem, Entry.index());
}
// Apple platforms have extra implicit framework search paths:
// $SDKROOT/System/Library/Frameworks/ and $SDKROOT/Library/Frameworks/.
if (IsOSDarwin) {
for (auto Entry : llvm::enumerate(Opts->getDarwinImplicitFrameworkSearchPaths())) {
addFilesInPathToLookupTable(FS, Entry.value(),
ModuleSearchPathKind::DarwinImplicitFramework,
/*isSystem=*/true, Entry.index());
}
}
for (auto Entry : llvm::enumerate(Opts->getRuntimeLibraryImportPaths())) {
addFilesInPathToLookupTable(FS, Entry.value(),
ModuleSearchPathKind::RuntimeLibrary,
/*isSystem=*/true, Entry.index());
}
State.FileSystem = FS;
State.IsOSDarwin = IsOSDarwin;
State.Opts = Opts;
State.IsPopulated = true;
}
static std::string computeSDKPlatformPath(StringRef SDKPath,
llvm::vfs::FileSystem *FS) {
if (SDKPath.empty())
return "";
SmallString<128> platformPath;
if (auto err = FS->getRealPath(SDKPath, platformPath))
llvm::sys::path::append(platformPath, SDKPath);
llvm::sys::path::remove_filename(platformPath); // specific SDK
llvm::sys::path::remove_filename(platformPath); // SDKs
llvm::sys::path::remove_filename(platformPath); // Developer
if (!llvm::sys::path::filename(platformPath).ends_with(".platform"))
return "";
return platformPath.str().str();
}
std::optional<StringRef>
SearchPathOptions::getSDKPlatformPath(llvm::vfs::FileSystem *FS) const {
if (!SDKPlatformPath)
SDKPlatformPath = computeSDKPlatformPath(getSDKPath(), FS);
if (SDKPlatformPath->empty())
return std::nullopt;
return *SDKPlatformPath;
}
void SearchPathOptions::dump(bool isDarwin) const {
llvm::errs() << "Module import search paths:\n";
for (auto Entry : llvm::enumerate(getImportSearchPaths())) {
llvm::errs() << " [" << Entry.index() << "] "
<< (Entry.value().IsSystem ? "(system) " : "(non-system) ")
<< Entry.value().Path << "\n";
}
llvm::errs() << "Framework search paths:\n";
for (auto Entry : llvm::enumerate(getFrameworkSearchPaths())) {
llvm::errs() << " [" << Entry.index() << "] "
<< (Entry.value().IsSystem ? "(system) " : "(non-system) ")
<< Entry.value().Path << "\n";
}
if (isDarwin) {
llvm::errs() << "Darwin implicit framework search paths:\n";
for (auto Entry :
llvm::enumerate(getDarwinImplicitFrameworkSearchPaths())) {
llvm::errs() << " [" << Entry.index() << "] " << Entry.value() << "\n";
}
}
llvm::errs() << "Runtime library import search paths:\n";
for (auto Entry : llvm::enumerate(getRuntimeLibraryImportPaths())) {
llvm::errs() << " [" << Entry.index() << "] " << Entry.value() << "\n";
}
llvm::errs() << "(End of search path lists.)\n";
}
SmallVector<const ModuleSearchPath *, 4>
ModuleSearchPathLookup::searchPathsContainingFile(
const SearchPathOptions *Opts, llvm::ArrayRef<std::string> Filenames,
llvm::vfs::FileSystem *FS, bool IsOSDarwin) {
if (!State.IsPopulated || State.FileSystem != FS ||
State.IsOSDarwin != IsOSDarwin || State.Opts != Opts) {
rebuildLookupTable(Opts, FS, IsOSDarwin);
}
// Gather all search paths that include a file whose name is in Filenames.
// To make sure that we don't include the same search paths twice, keep track
// of which search paths have already been added to Result by their kind and
// Index in ResultIds.
// Note that if a search path is specified twice by including it twice in
// compiler arguments or by specifying it as different kinds (e.g. once as
// import and once as framework search path), these search paths are
// considered different (because they have different indices/kinds and may
// thus still be included twice.
llvm::SmallVector<const ModuleSearchPath *, 4> Result;
llvm::SmallSet<std::pair<ModuleSearchPathKind, unsigned>, 4> ResultIds;
for (auto &Filename : Filenames) {
if (LookupTable.contains(Filename)) {
for (auto &Entry : LookupTable.at(Filename)) {
if (ResultIds.insert(std::make_pair(Entry->getKind(), Entry->getIndex()))
.second) {
Result.push_back(Entry.get());
}
}
}
}
// Make sure we maintain the same search paths order that we had used in
// populateLookupTableIfNecessary after merging results from
// different filenames.
llvm::sort(Result, [](const ModuleSearchPath *Lhs,
const ModuleSearchPath *Rhs) { return *Lhs < *Rhs; });
return Result;
}
/// Loads a VFS YAML file located at \p File using \p BaseFS and adds it to
/// \p OverlayFS. Returns an error if either loading the \p File failed or it
/// is invalid.
static llvm::Error loadAndValidateVFSOverlay(
const std::string &File,
const llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> &BaseFS,
const llvm::IntrusiveRefCntPtr<llvm::vfs::OverlayFileSystem> &OverlayFS) {
auto Buffer = BaseFS->getBufferForFile(File);
if (!Buffer)
return llvm::createFileError(File, Buffer.getError());
auto VFS = llvm::vfs::getVFSFromYAML(std::move(Buffer.get()), nullptr, File);
if (!VFS)
return llvm::createFileError(File, llvm::errc::invalid_argument);
OverlayFS->pushOverlay(std::move(VFS));
return llvm::Error::success();
}
llvm::Expected<llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>>
SearchPathOptions::makeOverlayFileSystem(
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> BaseFS) const {
// TODO: This implementation is different to how Clang reads overlays in.
// Expose a helper in Clang rather than doing this ourselves.
auto OverlayFS =
llvm::makeIntrusiveRefCnt<llvm::vfs::OverlayFileSystem>(BaseFS);
llvm::Error AllErrors = llvm::Error::success();
bool hasOverlays = false;
for (const auto &File : VFSOverlayFiles) {
hasOverlays = true;
if (auto Err = loadAndValidateVFSOverlay(File, BaseFS, OverlayFS))
AllErrors = llvm::joinErrors(std::move(AllErrors), std::move(Err));
}
if (AllErrors)
return std::move(AllErrors);
if (hasOverlays)
return OverlayFS;
return BaseFS;
}