blob: cd6d367b2e86dafe321432558ea4c26b57f6c820 [file] [log] [blame]
Josh Gao78b8a142016-11-09 01:00:41 -08001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <err.h>
18#include <fcntl.h>
19#include <fts.h>
20#include <sys/stat.h>
21#include <sys/types.h>
22#include <unistd.h>
23
24#include <memory>
25#include <string>
26
27#include <android-base/unique_fd.h>
28#include <clang/Basic/VirtualFileSystem.h>
29#include <llvm/ADT/IntrusiveRefCntPtr.h>
30#include <llvm/Support/MemoryBuffer.h>
31
32#include "Utils.h"
33
34using android::base::unique_fd;
35using namespace clang::vfs;
36
37static void addDirectoryToVFS(InMemoryFileSystem* vfs, const std::string& path) {
38 char* paths[] = { const_cast<char*>(path.c_str()), nullptr };
39 FTS* fts = fts_open(paths, FTS_COMFOLLOW | FTS_LOGICAL | FTS_NOCHDIR, nullptr);
40 if (!fts) {
41 err(1, "failed to open directory %s", path.c_str());
42 }
43
44 while (FTSENT* ent = fts_read(fts)) {
45 if ((ent->fts_info & FTS_F) == 0) {
46 continue;
47 }
48
49 const char* file_path = ent->fts_accpath;
50 unique_fd fd(open(file_path, O_RDONLY | O_CLOEXEC));
51 if (fd == -1) {
52 err(1, "failed to open header '%s'", file_path);
53 }
54
55 auto buffer_opt = llvm::MemoryBuffer::getOpenFileSlice(fd, file_path, -1, 0);
56 if (!buffer_opt) {
57 errx(1, "failed to map header '%s'", file_path);
58 }
59
60 if (!vfs->addFile(file_path, ent->fts_statp->st_mtim.tv_sec, std::move(buffer_opt.get()))) {
61 errx(1, "failed to add file '%s'", file_path);
62 }
63 }
64
65 fts_close(fts);
66}
67
68llvm::IntrusiveRefCntPtr<FileSystem> createCommonVFS(const std::string& header_dir,
69 const std::string& dependency_dir,
70 bool add_versioning_header) {
71 auto vfs = std::make_unique<InMemoryFileSystem>();
72 addDirectoryToVFS(vfs.get(), header_dir);
73 if (!dependency_dir.empty()) {
74 addDirectoryToVFS(vfs.get(), dependency_dir);
75 }
76
77 if (add_versioning_header) {
78 const char* top = getenv("ANDROID_BUILD_TOP");
79 if (!top) {
80 errx(1, "-i passed, but ANDROID_BUILD_TOP is unset");
81 }
82
83 std::string header_path = std::string(top) + "/bionic/libc/include/android/versioning.h";
84 auto buffer_opt = llvm::MemoryBuffer::getFile(header_path);
85 if (!buffer_opt) {
86 err(1, "failed to open %s", header_path.c_str());
87 }
88 vfs->addFile("android/versioning.h", 0, std::move(buffer_opt.get()));
89 }
90
91 return llvm::IntrusiveRefCntPtr<FileSystem>(vfs.release());
92}