1 // Copyright (C) 2017 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "utils/header_abi_util.h"
16
17 #include <llvm/Support/FileSystem.h>
18 #include <llvm/Support/Path.h>
19 #include <llvm/Support/raw_ostream.h>
20
21 #include <set>
22 #include <string>
23 #include <vector>
24
25
26 namespace header_checker {
27 namespace utils {
28
29
ShouldSkipFile(llvm::StringRef & file_name)30 static bool ShouldSkipFile(llvm::StringRef &file_name) {
31 // Ignore swap files, hidden files, and hidden directories. Do not recurse
32 // into hidden directories either. We should also not look at source files.
33 // Many projects include source files in their exports.
34 return (file_name.empty() || file_name.startswith(".") ||
35 file_name.endswith(".swp") || file_name.endswith(".swo") ||
36 file_name.endswith("#") || file_name.endswith(".cpp") ||
37 file_name.endswith(".cc") || file_name.endswith(".c"));
38 }
39
GetCwd()40 std::string GetCwd() {
41 llvm::SmallString<256> cwd;
42 if (llvm::sys::fs::current_path(cwd)) {
43 llvm::errs() << "ERROR: Failed to get current working directory\n";
44 ::exit(1);
45 }
46 return cwd.c_str();
47 }
48
NormalizePath(const std::string & path,const std::string & root_dir)49 std::string NormalizePath(const std::string &path,
50 const std::string &root_dir) {
51 llvm::SmallString<256> norm_path(path);
52 if (llvm::sys::fs::make_absolute(norm_path)) {
53 return "";
54 }
55 llvm::sys::path::remove_dots(norm_path, /* remove_dot_dot = */ true);
56 // Convert /cwd/path to /path.
57 if (llvm::sys::path::replace_path_prefix(norm_path, root_dir, "")) {
58 // Convert /path to path.
59 return llvm::sys::path::relative_path(norm_path.str()).str();
60 }
61 return std::string(norm_path);
62 }
63
CollectExportedHeaderSet(const std::string & dir_name,std::set<std::string> * exported_headers,const std::string & root_dir)64 static bool CollectExportedHeaderSet(const std::string &dir_name,
65 std::set<std::string> *exported_headers,
66 const std::string &root_dir) {
67 std::error_code ec;
68 llvm::sys::fs::recursive_directory_iterator walker(dir_name, ec);
69 // Default construction - end of directory.
70 llvm::sys::fs::recursive_directory_iterator end;
71 for ( ; walker != end; walker.increment(ec)) {
72 if (ec) {
73 llvm::errs() << "Failed to walk directory: " << dir_name << ": "
74 << ec.message() << "\n";
75 return false;
76 }
77
78 const std::string &file_path = walker->path();
79
80 llvm::StringRef file_name(llvm::sys::path::filename(file_path));
81 // Ignore swap files and hidden files / dirs. Do not recurse into them too.
82 // We should also not look at source files. Many projects include source
83 // files in their exports.
84 if (ShouldSkipFile(file_name)) {
85 walker.no_push();
86 continue;
87 }
88
89 llvm::ErrorOr<llvm::sys::fs::basic_file_status> status = walker->status();
90 if (!status) {
91 llvm::errs() << "Failed to stat file: " << file_path << "\n";
92 return false;
93 }
94
95 if ((status->type() != llvm::sys::fs::file_type::symlink_file) &&
96 (status->type() != llvm::sys::fs::file_type::regular_file)) {
97 // Ignore non regular files, except symlinks.
98 continue;
99 }
100
101 exported_headers->insert(NormalizePath(file_path, root_dir));
102 }
103 return true;
104 }
105
106 std::set<std::string>
CollectAllExportedHeaders(const std::vector<std::string> & exported_header_dirs,const std::string & root_dir)107 CollectAllExportedHeaders(const std::vector<std::string> &exported_header_dirs,
108 const std::string &root_dir) {
109 std::set<std::string> exported_headers;
110 for (auto &&dir : exported_header_dirs) {
111 if (!CollectExportedHeaderSet(dir, &exported_headers, root_dir)) {
112 llvm::errs() << "Couldn't collect exported headers\n";
113 ::exit(1);
114 }
115 }
116 return exported_headers;
117 }
118
119 } // namespace utils
120 } // namespace header_checker
121