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
|
//===- DirectoryScanner.cpp -----------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "clang/InstallAPI/DirectoryScanner.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/TextAPI/DylibReader.h"
using namespace llvm;
using namespace llvm::MachO;
namespace clang::installapi {
HeaderSeq DirectoryScanner::getHeaders(ArrayRef<Library> Libraries) {
HeaderSeq Headers;
for (const Library &Lib : Libraries)
llvm::append_range(Headers, Lib.Headers);
return Headers;
}
llvm::Error DirectoryScanner::scan(StringRef Directory) {
if (Mode == ScanMode::ScanFrameworks)
return scanForFrameworks(Directory);
return scanForUnwrappedLibraries(Directory);
}
llvm::Error DirectoryScanner::scanForUnwrappedLibraries(StringRef Directory) {
// Check some known sub-directory locations.
auto GetDirectory = [&](const char *Sub) -> OptionalDirectoryEntryRef {
SmallString<PATH_MAX> Path(Directory);
sys::path::append(Path, Sub);
return FM.getOptionalDirectoryRef(Path);
};
auto DirPublic = GetDirectory("usr/include");
auto DirPrivate = GetDirectory("usr/local/include");
if (!DirPublic && !DirPrivate) {
std::error_code ec = std::make_error_code(std::errc::not_a_directory);
return createStringError(ec,
"cannot find any public (usr/include) or private "
"(usr/local/include) header directory");
}
Library &Lib = getOrCreateLibrary(Directory, Libraries);
Lib.IsUnwrappedDylib = true;
if (DirPublic)
if (Error Err = scanHeaders(DirPublic->getName(), Lib, HeaderType::Public,
Directory))
return Err;
if (DirPrivate)
if (Error Err = scanHeaders(DirPrivate->getName(), Lib, HeaderType::Private,
Directory))
return Err;
return Error::success();
}
static bool isFramework(StringRef Path) {
while (Path.back() == '/')
Path = Path.slice(0, Path.size() - 1);
return llvm::StringSwitch<bool>(llvm::sys::path::extension(Path))
.Case(".framework", true)
.Default(false);
}
Library &
DirectoryScanner::getOrCreateLibrary(StringRef Path,
std::vector<Library> &Libs) const {
if (Path.consume_front(RootPath) && Path.empty())
Path = "/";
auto LibIt =
find_if(Libs, [Path](const Library &L) { return L.getPath() == Path; });
if (LibIt != Libs.end())
return *LibIt;
Libs.emplace_back(Path);
return Libs.back();
}
Error DirectoryScanner::scanHeaders(StringRef Path, Library &Lib,
HeaderType Type, StringRef BasePath,
StringRef ParentPath) const {
std::error_code ec;
auto &FS = FM.getVirtualFileSystem();
PathSeq SubDirectories;
for (vfs::directory_iterator i = FS.dir_begin(Path, ec), ie; i != ie;
i.increment(ec)) {
StringRef HeaderPath = i->path();
if (ec)
return createStringError(ec, "unable to read: " + HeaderPath);
if (sys::fs::is_symlink_file(HeaderPath))
continue;
// Ignore tmp files from unifdef.
const StringRef Filename = sys::path::filename(HeaderPath);
if (Filename.starts_with("."))
continue;
// If it is a directory, remember the subdirectory.
if (FM.getOptionalDirectoryRef(HeaderPath))
SubDirectories.push_back(HeaderPath.str());
if (!isHeaderFile(HeaderPath))
continue;
// Skip files that do not exist. This usually happens for broken symlinks.
if (FS.status(HeaderPath) == std::errc::no_such_file_or_directory)
continue;
auto IncludeName = createIncludeHeaderName(HeaderPath);
Lib.addHeaderFile(HeaderPath, Type,
IncludeName.has_value() ? IncludeName.value() : "");
}
// Go through the subdirectories.
// Sort the sub-directory first since different file systems might have
// different traverse order.
llvm::sort(SubDirectories);
if (ParentPath.empty())
ParentPath = Path;
for (const StringRef Dir : SubDirectories)
return scanHeaders(Dir, Lib, Type, BasePath, ParentPath);
return Error::success();
}
llvm::Error
DirectoryScanner::scanMultipleFrameworks(StringRef Directory,
std::vector<Library> &Libs) const {
std::error_code ec;
auto &FS = FM.getVirtualFileSystem();
for (vfs::directory_iterator i = FS.dir_begin(Directory, ec), ie; i != ie;
i.increment(ec)) {
StringRef Curr = i->path();
// Skip files that do not exist. This usually happens for broken symlinks.
if (ec == std::errc::no_such_file_or_directory) {
ec.clear();
continue;
}
if (ec)
return createStringError(ec, Curr);
if (sys::fs::is_symlink_file(Curr))
continue;
if (isFramework(Curr)) {
if (!FM.getOptionalDirectoryRef(Curr))
continue;
Library &Framework = getOrCreateLibrary(Curr, Libs);
if (Error Err = scanFrameworkDirectory(Curr, Framework))
return Err;
}
}
return Error::success();
}
llvm::Error
DirectoryScanner::scanSubFrameworksDirectory(StringRef Directory,
std::vector<Library> &Libs) const {
if (FM.getOptionalDirectoryRef(Directory))
return scanMultipleFrameworks(Directory, Libs);
std::error_code ec = std::make_error_code(std::errc::not_a_directory);
return createStringError(ec, Directory);
}
/// FIXME: How to handle versions? For now scan them separately as independent
/// frameworks.
llvm::Error
DirectoryScanner::scanFrameworkVersionsDirectory(StringRef Path,
Library &Lib) const {
std::error_code ec;
auto &FS = FM.getVirtualFileSystem();
for (vfs::directory_iterator i = FS.dir_begin(Path, ec), ie; i != ie;
i.increment(ec)) {
const StringRef Curr = i->path();
// Skip files that do not exist. This usually happens for broken symlinks.
if (ec == std::errc::no_such_file_or_directory) {
ec.clear();
continue;
}
if (ec)
return createStringError(ec, Curr);
if (sys::fs::is_symlink_file(Curr))
continue;
// Each version should be a framework directory.
if (!FM.getOptionalDirectoryRef(Curr))
continue;
Library &VersionedFramework =
getOrCreateLibrary(Curr, Lib.FrameworkVersions);
if (Error Err = scanFrameworkDirectory(Curr, VersionedFramework))
return Err;
}
return Error::success();
}
llvm::Error DirectoryScanner::scanFrameworkDirectory(StringRef Path,
Library &Framework) const {
// If the framework is inside Kernel or IOKit, scan headers in the different
// directories separately.
Framework.IsUnwrappedDylib =
Path.contains("Kernel.framework") || Path.contains("IOKit.framework");
// Unfortunately we cannot identify symlinks in the VFS. We assume that if
// there is a Versions directory, then we have symlinks and directly proceed
// to the Versions folder.
std::error_code ec;
auto &FS = FM.getVirtualFileSystem();
for (vfs::directory_iterator i = FS.dir_begin(Path, ec), ie; i != ie;
i.increment(ec)) {
StringRef Curr = i->path();
// Skip files that do not exist. This usually happens for broken symlinks.
if (ec == std::errc::no_such_file_or_directory) {
ec.clear();
continue;
}
if (ec)
return createStringError(ec, Curr);
if (sys::fs::is_symlink_file(Curr))
continue;
StringRef FileName = sys::path::filename(Curr);
// Scan all "public" headers.
if (FileName.contains("Headers")) {
if (Error Err = scanHeaders(Curr, Framework, HeaderType::Public, Curr))
return Err;
continue;
}
// Scan all "private" headers.
if (FileName.contains("PrivateHeaders")) {
if (Error Err = scanHeaders(Curr, Framework, HeaderType::Private, Curr))
return Err;
continue;
}
// Scan sub frameworks.
if (FileName.contains("Frameworks")) {
if (Error Err = scanSubFrameworksDirectory(Curr, Framework.SubFrameworks))
return Err;
continue;
}
// Check for versioned frameworks.
if (FileName.contains("Versions")) {
if (Error Err = scanFrameworkVersionsDirectory(Curr, Framework))
return Err;
continue;
}
}
return Error::success();
}
llvm::Error DirectoryScanner::scanForFrameworks(StringRef Directory) {
RootPath = "";
// Expect a certain directory structure and naming convention to find
// frameworks.
static const char *SubDirectories[] = {"System/Library/Frameworks/",
"System/Library/PrivateFrameworks/"};
// Check if the directory is already a framework.
if (isFramework(Directory)) {
Library &Framework = getOrCreateLibrary(Directory, Libraries);
if (Error Err = scanFrameworkDirectory(Directory, Framework))
return Err;
return Error::success();
}
// Check known sub-directory locations.
for (const auto *SubDir : SubDirectories) {
SmallString<PATH_MAX> Path(Directory);
sys::path::append(Path, SubDir);
if (Error Err = scanMultipleFrameworks(Path, Libraries))
return Err;
}
return Error::success();
}
} // namespace clang::installapi
|