Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 1 | /* |
Elliott Hughes | dfb74c5 | 2016-10-24 12:53:17 -0700 | [diff] [blame] | 2 | * Copyright (C) 2016 The Android Open Source Project |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 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 | * |
Elliott Hughes | dfb74c5 | 2016-10-24 12:53:17 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 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 <dirent.h> |
| 18 | #include <err.h> |
| 19 | #include <limits.h> |
| 20 | #include <stdio.h> |
Josh Gao | d2ab9ff | 2017-07-28 12:53:36 -0700 | [diff] [blame] | 21 | #include <stdlib.h> |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 22 | #include <sys/stat.h> |
| 23 | #include <sys/types.h> |
| 24 | #include <unistd.h> |
| 25 | |
Josh Gao | 338cf12 | 2016-11-09 18:01:41 -0800 | [diff] [blame] | 26 | #if defined(__linux__) |
| 27 | #include <sched.h> |
| 28 | #endif |
| 29 | |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 30 | #include <atomic> |
Josh Gao | 338cf12 | 2016-11-09 18:01:41 -0800 | [diff] [blame] | 31 | #include <chrono> |
Josh Gao | 16016df | 2016-11-07 18:27:16 -0800 | [diff] [blame] | 32 | #include <functional> |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 33 | #include <iostream> |
| 34 | #include <map> |
| 35 | #include <memory> |
Josh Gao | c166113 | 2017-12-12 11:51:32 -0800 | [diff] [blame^] | 36 | #include <optional> |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 37 | #include <set> |
| 38 | #include <sstream> |
| 39 | #include <string> |
Josh Gao | ab25d0b | 2017-08-10 10:50:33 -0700 | [diff] [blame] | 40 | #include <string_view> |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 41 | #include <thread> |
| 42 | #include <unordered_map> |
| 43 | #include <vector> |
| 44 | |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 45 | #include <llvm/ADT/StringRef.h> |
| 46 | |
Josh Gao | b50b8c8 | 2017-04-27 17:22:52 -0700 | [diff] [blame] | 47 | #include <android-base/file.h> |
Josh Gao | 338cf12 | 2016-11-09 18:01:41 -0800 | [diff] [blame] | 48 | #include <android-base/macros.h> |
Josh Gao | 16016df | 2016-11-07 18:27:16 -0800 | [diff] [blame] | 49 | #include <android-base/parseint.h> |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 50 | #include <android-base/strings.h> |
Josh Gao | 16016df | 2016-11-07 18:27:16 -0800 | [diff] [blame] | 51 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 52 | #include "Arch.h" |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 53 | #include "DeclarationDatabase.h" |
Josh Gao | b5c4963 | 2016-11-08 22:21:31 -0800 | [diff] [blame] | 54 | #include "Driver.h" |
Josh Gao | f8592a3 | 2016-07-26 18:58:27 -0700 | [diff] [blame] | 55 | #include "Preprocessor.h" |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 56 | #include "SymbolDatabase.h" |
| 57 | #include "Utils.h" |
Josh Gao | 78b8a14 | 2016-11-09 01:00:41 -0800 | [diff] [blame] | 58 | #include "VFS.h" |
| 59 | |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 60 | #include "versioner.h" |
| 61 | |
Josh Gao | 338cf12 | 2016-11-09 18:01:41 -0800 | [diff] [blame] | 62 | using namespace std::chrono_literals; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 63 | using namespace std::string_literals; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 64 | |
Josh Gao | acc3d80 | 2016-11-09 18:22:44 -0800 | [diff] [blame] | 65 | bool strict; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 66 | bool verbose; |
Josh Gao | acc3d80 | 2016-11-09 18:22:44 -0800 | [diff] [blame] | 67 | bool add_include; |
Josh Gao | 338cf12 | 2016-11-09 18:01:41 -0800 | [diff] [blame] | 68 | |
| 69 | static int getCpuCount(); |
| 70 | static int max_thread_count = getCpuCount(); |
| 71 | |
| 72 | static int getCpuCount() { |
| 73 | #if defined(__linux__) |
| 74 | cpu_set_t cpu_set; |
| 75 | int rc = sched_getaffinity(getpid(), sizeof(cpu_set), &cpu_set); |
| 76 | if (rc != 0) { |
| 77 | err(1, "sched_getaffinity failed"); |
| 78 | } |
| 79 | return CPU_COUNT(&cpu_set); |
| 80 | #else |
| 81 | return 1; |
| 82 | #endif |
| 83 | } |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 84 | |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 85 | namespace { |
| 86 | struct HeaderLocationInformation { |
Josh Gao | ab25d0b | 2017-08-10 10:50:33 -0700 | [diff] [blame] | 87 | std::string header_path; |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 88 | std::string dependency_dir; |
| 89 | // Absolute paths to ignore all children -- including subdirectories -- of. |
| 90 | std::unordered_set<std::string> ignored_directories; |
| 91 | }; |
| 92 | } |
| 93 | |
Josh Gao | ab25d0b | 2017-08-10 10:50:33 -0700 | [diff] [blame] | 94 | static bool is_dir(const std::string& path) { |
| 95 | struct stat st; |
| 96 | return stat(path.c_str(), &st) == 0 && S_ISDIR(st.st_mode); |
| 97 | } |
| 98 | |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 99 | static CompilationRequirements collectRequirements(const Arch& arch, |
| 100 | const HeaderLocationInformation& location) { |
| 101 | std::vector<std::string> headers = |
Josh Gao | ab25d0b | 2017-08-10 10:50:33 -0700 | [diff] [blame] | 102 | collectHeaders(location.header_path, location.ignored_directories); |
| 103 | std::vector<std::string> dependencies; |
| 104 | |
| 105 | if (is_dir(location.header_path)) { |
| 106 | dependencies.emplace_back(location.header_path); |
| 107 | } |
| 108 | |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 109 | if (!location.dependency_dir.empty()) { |
Yi Kong | ff6c8de | 2017-04-20 09:08:11 -0700 | [diff] [blame] | 110 | auto collect_children = [&dependencies](const std::string& dir_path) { |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 111 | DIR* dir = opendir(dir_path.c_str()); |
| 112 | if (!dir) { |
| 113 | err(1, "failed to open dependency directory '%s'", dir_path.c_str()); |
| 114 | } |
| 115 | |
| 116 | struct dirent* dent; |
| 117 | while ((dent = readdir(dir))) { |
| 118 | if (dent->d_name[0] == '.') { |
| 119 | continue; |
| 120 | } |
| 121 | |
| 122 | // TODO: Resolve symlinks. |
| 123 | std::string dependency = dir_path + "/" + dent->d_name; |
| 124 | |
| 125 | struct stat st; |
| 126 | if (stat(dependency.c_str(), &st) != 0) { |
| 127 | err(1, "failed to stat dependency '%s'", dependency.c_str()); |
| 128 | } |
| 129 | |
| 130 | if (!S_ISDIR(st.st_mode)) { |
| 131 | errx(1, "'%s' is not a directory", dependency.c_str()); |
| 132 | } |
| 133 | |
| 134 | dependencies.push_back(dependency); |
| 135 | } |
| 136 | |
| 137 | closedir(dir); |
| 138 | }; |
| 139 | |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 140 | collect_children(location.dependency_dir + "/common"); |
| 141 | collect_children(location.dependency_dir + "/" + to_string(arch)); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | auto new_end = std::remove_if(headers.begin(), headers.end(), [&arch](llvm::StringRef header) { |
| 145 | for (const auto& it : header_blacklist) { |
| 146 | if (it.second.find(arch) == it.second.end()) { |
| 147 | continue; |
| 148 | } |
| 149 | |
| 150 | if (header.endswith("/" + it.first)) { |
| 151 | return true; |
| 152 | } |
| 153 | } |
| 154 | return false; |
| 155 | }); |
| 156 | |
| 157 | headers.erase(new_end, headers.end()); |
| 158 | |
| 159 | CompilationRequirements result = { .headers = headers, .dependencies = dependencies }; |
| 160 | return result; |
| 161 | } |
| 162 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 163 | static std::set<CompilationType> generateCompilationTypes(const std::set<Arch> selected_architectures, |
| 164 | const std::set<int>& selected_levels) { |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 165 | std::set<CompilationType> result; |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 166 | for (const auto& arch : selected_architectures) { |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 167 | int min_api = arch_min_api[arch]; |
| 168 | for (int api_level : selected_levels) { |
| 169 | if (api_level < min_api) { |
| 170 | continue; |
| 171 | } |
Josh Gao | a77b3a9 | 2016-08-15 16:39:27 -0700 | [diff] [blame] | 172 | |
| 173 | for (int file_offset_bits : { 32, 64 }) { |
Josh Gao | ab25d0b | 2017-08-10 10:50:33 -0700 | [diff] [blame] | 174 | for (bool cpp : { true, false }) { |
| 175 | CompilationType type = { |
| 176 | .arch = arch, .cpp = cpp, .api_level = api_level, .file_offset_bits = file_offset_bits |
| 177 | }; |
| 178 | result.insert(type); |
| 179 | } |
Josh Gao | a77b3a9 | 2016-08-15 16:39:27 -0700 | [diff] [blame] | 180 | } |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 181 | } |
| 182 | } |
| 183 | return result; |
| 184 | } |
| 185 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 186 | static std::unique_ptr<HeaderDatabase> compileHeaders(const std::set<CompilationType>& types, |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 187 | const HeaderLocationInformation& location) { |
Josh Gao | 16016df | 2016-11-07 18:27:16 -0800 | [diff] [blame] | 188 | if (types.empty()) { |
| 189 | errx(1, "compileHeaders received no CompilationTypes"); |
| 190 | } |
| 191 | |
Josh Gao | ab25d0b | 2017-08-10 10:50:33 -0700 | [diff] [blame] | 192 | auto vfs = createCommonVFS(location.header_path, location.dependency_dir, add_include); |
Josh Gao | 78b8a14 | 2016-11-09 01:00:41 -0800 | [diff] [blame] | 193 | |
Josh Gao | 16016df | 2016-11-07 18:27:16 -0800 | [diff] [blame] | 194 | size_t thread_count = max_thread_count; |
| 195 | std::vector<std::thread> threads; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 196 | |
| 197 | std::map<CompilationType, HeaderDatabase> header_databases; |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 198 | std::unordered_map<Arch, CompilationRequirements> requirements; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 199 | |
Josh Gao | 16016df | 2016-11-07 18:27:16 -0800 | [diff] [blame] | 200 | auto result = std::make_unique<HeaderDatabase>(); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 201 | for (const auto& type : types) { |
| 202 | if (requirements.count(type.arch) == 0) { |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 203 | requirements[type.arch] = collectRequirements(type.arch, location); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | |
Josh Gao | 78b8a14 | 2016-11-09 01:00:41 -0800 | [diff] [blame] | 207 | initializeTargetCC1FlagCache(vfs, types, requirements); |
Josh Gao | b5c4963 | 2016-11-08 22:21:31 -0800 | [diff] [blame] | 208 | |
| 209 | std::vector<std::pair<CompilationType, const std::string&>> jobs; |
Josh Gao | 338cf12 | 2016-11-09 18:01:41 -0800 | [diff] [blame] | 210 | std::atomic<size_t> job_index(0); |
Josh Gao | 16016df | 2016-11-07 18:27:16 -0800 | [diff] [blame] | 211 | for (CompilationType type : types) { |
| 212 | CompilationRequirements& req = requirements[type.arch]; |
| 213 | for (const std::string& header : req.headers) { |
Josh Gao | b5c4963 | 2016-11-08 22:21:31 -0800 | [diff] [blame] | 214 | jobs.emplace_back(type, header); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 215 | } |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Josh Gao | d2ab9ff | 2017-07-28 12:53:36 -0700 | [diff] [blame] | 218 | // Dup an empty file to stdin, so that we can use `clang -include a.h -` instead of `clang a.h`, |
| 219 | // since some warnings don't get generated in files that are compiled directly. |
| 220 | FILE* empty_file = tmpfile(); |
| 221 | if (!empty_file) { |
| 222 | err(1, "failed to create temporary file"); |
| 223 | } |
| 224 | |
| 225 | int empty_file_fd = fileno(empty_file); |
| 226 | if (empty_file_fd == -1) { |
| 227 | errx(1, "fileno failed on tmpfile"); |
| 228 | } |
| 229 | |
| 230 | dup2(empty_file_fd, STDIN_FILENO); |
| 231 | fclose(empty_file); |
| 232 | |
Josh Gao | b5c4963 | 2016-11-08 22:21:31 -0800 | [diff] [blame] | 233 | thread_count = std::min(thread_count, jobs.size()); |
| 234 | |
| 235 | if (thread_count == 1) { |
| 236 | for (const auto& job : jobs) { |
Josh Gao | 78b8a14 | 2016-11-09 01:00:41 -0800 | [diff] [blame] | 237 | compileHeader(vfs, result.get(), job.first, job.second); |
Josh Gao | b5c4963 | 2016-11-08 22:21:31 -0800 | [diff] [blame] | 238 | } |
| 239 | } else { |
| 240 | // Spawn threads. |
| 241 | for (size_t i = 0; i < thread_count; ++i) { |
Yi Kong | ff6c8de | 2017-04-20 09:08:11 -0700 | [diff] [blame] | 242 | threads.emplace_back([&jobs, &job_index, &result, vfs]() { |
Josh Gao | 338cf12 | 2016-11-09 18:01:41 -0800 | [diff] [blame] | 243 | while (true) { |
| 244 | size_t idx = job_index++; |
| 245 | if (idx >= jobs.size()) { |
| 246 | return; |
| 247 | } |
| 248 | |
| 249 | const auto& job = jobs[idx]; |
Josh Gao | 78b8a14 | 2016-11-09 01:00:41 -0800 | [diff] [blame] | 250 | compileHeader(vfs, result.get(), job.first, job.second); |
Josh Gao | b5c4963 | 2016-11-08 22:21:31 -0800 | [diff] [blame] | 251 | } |
| 252 | }); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Josh Gao | b5c4963 | 2016-11-08 22:21:31 -0800 | [diff] [blame] | 255 | // Reap them. |
| 256 | for (auto& thread : threads) { |
| 257 | thread.join(); |
| 258 | } |
| 259 | threads.clear(); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 262 | return result; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Josh Gao | 1a176de | 2016-11-04 13:15:11 -0700 | [diff] [blame] | 265 | static std::set<CompilationType> getCompilationTypes(const Declaration* decl) { |
| 266 | std::set<CompilationType> result; |
| 267 | for (const auto& it : decl->availability) { |
| 268 | result.insert(it.first); |
| 269 | } |
| 270 | return result; |
| 271 | } |
| 272 | |
| 273 | template<typename T> |
| 274 | static std::vector<T> Intersection(const std::set<T>& a, const std::set<T>& b) { |
| 275 | std::vector<T> intersection; |
| 276 | std::set_intersection(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(intersection)); |
| 277 | return intersection; |
| 278 | } |
| 279 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 280 | // Perform a sanity check on a symbol's declarations, enforcing the following invariants: |
| 281 | // 1. At most one inline definition of the function exists. |
| 282 | // 2. All of the availability declarations for a symbol are compatible. |
| 283 | // If a function is declared as an inline before a certain version, the inline definition |
| 284 | // should have no version tag. |
| 285 | // 3. Each availability type must only be present globally or on a per-arch basis. |
| 286 | // (e.g. __INTRODUCED_IN_ARM(9) __INTRODUCED_IN_X86(10) __DEPRECATED_IN(11) is fine, |
| 287 | // but not __INTRODUCED_IN(9) __INTRODUCED_IN_X86(10)) |
| 288 | static bool checkSymbol(const Symbol& symbol) { |
| 289 | std::string cwd = getWorkingDir() + "/"; |
| 290 | |
Josh Gao | 1a176de | 2016-11-04 13:15:11 -0700 | [diff] [blame] | 291 | std::unordered_map<const Declaration*, std::set<CompilationType>> inline_definitions; |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 292 | for (const auto& decl_it : symbol.declarations) { |
| 293 | const Declaration* decl = &decl_it.second; |
| 294 | if (decl->is_definition) { |
Josh Gao | 1a176de | 2016-11-04 13:15:11 -0700 | [diff] [blame] | 295 | std::set<CompilationType> compilation_types = getCompilationTypes(decl); |
| 296 | for (const auto& inline_def_it : inline_definitions) { |
| 297 | auto intersection = Intersection(compilation_types, inline_def_it.second); |
| 298 | if (!intersection.empty()) { |
Josh Gao | ab25d0b | 2017-08-10 10:50:33 -0700 | [diff] [blame] | 299 | fprintf(stderr, "versioner: conflicting inline definitions for symbol %s:\n", |
| 300 | symbol.name.c_str()); |
Josh Gao | 1a176de | 2016-11-04 13:15:11 -0700 | [diff] [blame] | 301 | fprintf(stderr, " declarations visible in: %s\n", Join(intersection, ", ").c_str()); |
| 302 | decl->dump(cwd, stderr, 4); |
| 303 | inline_def_it.first->dump(cwd, stderr, 4); |
| 304 | return false; |
| 305 | } |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Josh Gao | 1a176de | 2016-11-04 13:15:11 -0700 | [diff] [blame] | 308 | inline_definitions[decl] = std::move(compilation_types); |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | DeclarationAvailability availability; |
| 312 | if (!decl->calculateAvailability(&availability)) { |
| 313 | fprintf(stderr, "versioner: failed to calculate availability for declaration:\n"); |
Josh Gao | 566735d | 2016-08-02 15:07:32 -0700 | [diff] [blame] | 314 | decl->dump(cwd, stderr, 2); |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 315 | return false; |
| 316 | } |
| 317 | |
| 318 | if (decl->is_definition && !availability.empty()) { |
| 319 | fprintf(stderr, "versioner: inline definition has non-empty versioning information:\n"); |
Josh Gao | 566735d | 2016-08-02 15:07:32 -0700 | [diff] [blame] | 320 | decl->dump(cwd, stderr, 2); |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 321 | return false; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | DeclarationAvailability availability; |
| 326 | if (!symbol.calculateAvailability(&availability)) { |
| 327 | fprintf(stderr, "versioner: inconsistent availability for symbol '%s'\n", symbol.name.c_str()); |
| 328 | symbol.dump(cwd); |
| 329 | return false; |
| 330 | } |
| 331 | |
| 332 | // TODO: Check invariant #3. |
| 333 | return true; |
| 334 | } |
| 335 | |
| 336 | static bool sanityCheck(const HeaderDatabase* database) { |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 337 | bool error = false; |
Josh Gao | 958f3b3 | 2016-06-03 13:44:00 -0700 | [diff] [blame] | 338 | std::string cwd = getWorkingDir() + "/"; |
| 339 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 340 | for (const auto& symbol_it : database->symbols) { |
| 341 | if (!checkSymbol(symbol_it.second)) { |
| 342 | error = true; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 343 | } |
| 344 | } |
| 345 | return !error; |
| 346 | } |
| 347 | |
| 348 | // Check that our symbol availability declarations match the actual NDK |
| 349 | // platform symbol availability. |
| 350 | static bool checkVersions(const std::set<CompilationType>& types, |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 351 | const HeaderDatabase* header_database, |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 352 | const NdkSymbolDatabase& symbol_database) { |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 353 | std::string cwd = getWorkingDir() + "/"; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 354 | bool failed = false; |
| 355 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 356 | std::map<Arch, std::set<CompilationType>> arch_types; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 357 | for (const CompilationType& type : types) { |
| 358 | arch_types[type.arch].insert(type); |
| 359 | } |
| 360 | |
Josh Gao | d67dbf0 | 2016-06-02 15:21:14 -0700 | [diff] [blame] | 361 | std::set<std::string> completely_unavailable; |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 362 | std::map<std::string, std::set<CompilationType>> missing_availability; |
| 363 | std::map<std::string, std::set<CompilationType>> extra_availability; |
Josh Gao | d67dbf0 | 2016-06-02 15:21:14 -0700 | [diff] [blame] | 364 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 365 | for (const auto& symbol_it : header_database->symbols) { |
| 366 | const auto& symbol_name = symbol_it.first; |
| 367 | DeclarationAvailability symbol_availability; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 368 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 369 | if (!symbol_it.second.calculateAvailability(&symbol_availability)) { |
| 370 | errx(1, "failed to calculate symbol availability"); |
| 371 | } |
| 372 | |
| 373 | const auto platform_availability_it = symbol_database.find(symbol_name); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 374 | if (platform_availability_it == symbol_database.end()) { |
Josh Gao | d67dbf0 | 2016-06-02 15:21:14 -0700 | [diff] [blame] | 375 | completely_unavailable.insert(symbol_name); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 376 | continue; |
| 377 | } |
| 378 | |
| 379 | const auto& platform_availability = platform_availability_it->second; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 380 | |
| 381 | for (const CompilationType& type : types) { |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 382 | bool should_be_available = true; |
| 383 | const auto& global_availability = symbol_availability.global_availability; |
| 384 | const auto& arch_availability = symbol_availability.arch_availability[type.arch]; |
| 385 | if (global_availability.introduced != 0 && global_availability.introduced > type.api_level) { |
| 386 | should_be_available = false; |
| 387 | } |
| 388 | |
| 389 | if (arch_availability.introduced != 0 && arch_availability.introduced > type.api_level) { |
| 390 | should_be_available = false; |
| 391 | } |
| 392 | |
| 393 | if (global_availability.obsoleted != 0 && global_availability.obsoleted <= type.api_level) { |
| 394 | should_be_available = false; |
| 395 | } |
| 396 | |
| 397 | if (arch_availability.obsoleted != 0 && arch_availability.obsoleted <= type.api_level) { |
| 398 | should_be_available = false; |
| 399 | } |
| 400 | |
| 401 | if (arch_availability.future) { |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 402 | continue; |
| 403 | } |
| 404 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 405 | // The function declaration might be (validly) missing for the given CompilationType. |
| 406 | if (!symbol_it.second.hasDeclaration(type)) { |
| 407 | should_be_available = false; |
| 408 | } |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 409 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 410 | bool is_available = platform_availability.count(type); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 411 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 412 | if (should_be_available != is_available) { |
| 413 | if (is_available) { |
| 414 | extra_availability[symbol_name].insert(type); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 415 | } else { |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 416 | missing_availability[symbol_name].insert(type); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 417 | } |
| 418 | } |
| 419 | } |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 420 | } |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 421 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 422 | for (const auto& it : symbol_database) { |
| 423 | const std::string& symbol_name = it.first; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 424 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 425 | bool symbol_error = false; |
Josh Gao | 0a284f5 | 2016-12-15 13:56:00 -0800 | [diff] [blame] | 426 | if (auto missing_it = missing_availability.find(symbol_name); |
| 427 | missing_it != missing_availability.end()) { |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 428 | printf("%s: declaration marked available but symbol missing in [%s]\n", symbol_name.c_str(), |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 429 | Join(missing_it->second, ", ").c_str()); |
| 430 | symbol_error = true; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 431 | failed = true; |
| 432 | } |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 433 | |
Josh Gao | acc3d80 | 2016-11-09 18:22:44 -0800 | [diff] [blame] | 434 | if (strict) { |
Josh Gao | 0a284f5 | 2016-12-15 13:56:00 -0800 | [diff] [blame] | 435 | if (auto extra_it = extra_availability.find(symbol_name); |
| 436 | extra_it != extra_availability.end()) { |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 437 | printf("%s: declaration marked unavailable but symbol available in [%s]\n", |
| 438 | symbol_name.c_str(), Join(extra_it->second, ", ").c_str()); |
| 439 | symbol_error = true; |
| 440 | failed = true; |
Josh Gao | 958f3b3 | 2016-06-03 13:44:00 -0700 | [diff] [blame] | 441 | } |
| 442 | } |
| 443 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 444 | if (symbol_error) { |
Josh Gao | 0a284f5 | 2016-12-15 13:56:00 -0800 | [diff] [blame] | 445 | if (auto symbol_it = header_database->symbols.find(symbol_name); |
| 446 | symbol_it != header_database->symbols.end()) { |
| 447 | symbol_it->second.dump(cwd); |
| 448 | } else { |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 449 | errx(1, "failed to find symbol in header database"); |
| 450 | } |
Josh Gao | d67dbf0 | 2016-06-02 15:21:14 -0700 | [diff] [blame] | 451 | } |
Josh Gao | d67dbf0 | 2016-06-02 15:21:14 -0700 | [diff] [blame] | 452 | } |
| 453 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 454 | // TODO: Verify that function/variable declarations are actually function/variable symbols. |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 455 | return !failed; |
| 456 | } |
| 457 | |
Josh Gao | 62aaf8f | 2016-06-02 14:27:21 -0700 | [diff] [blame] | 458 | static void usage(bool help = false) { |
| 459 | fprintf(stderr, "Usage: versioner [OPTION]... [HEADER_PATH] [DEPS_PATH]\n"); |
| 460 | if (!help) { |
| 461 | printf("Try 'versioner -h' for more information.\n"); |
| 462 | exit(1); |
| 463 | } else { |
| 464 | fprintf(stderr, "Version headers at HEADER_PATH, with DEPS_PATH/ARCH/* on the include path\n"); |
Josh Gao | 9b5af7a | 2016-06-02 14:29:13 -0700 | [diff] [blame] | 465 | fprintf(stderr, "Autodetects paths if HEADER_PATH and DEPS_PATH are not specified\n"); |
Josh Gao | 62aaf8f | 2016-06-02 14:27:21 -0700 | [diff] [blame] | 466 | fprintf(stderr, "\n"); |
| 467 | fprintf(stderr, "Target specification (defaults to all):\n"); |
| 468 | fprintf(stderr, " -a API_LEVEL\tbuild with specified API level (can be repeated)\n"); |
Josh Gao | 35aa213 | 2017-10-24 17:05:49 -0700 | [diff] [blame] | 469 | fprintf(stderr, " \t\tdefaults to %s\n", Join(default_levels).c_str()); |
Josh Gao | 62aaf8f | 2016-06-02 14:27:21 -0700 | [diff] [blame] | 470 | fprintf(stderr, " -r ARCH\tbuild with specified architecture (can be repeated)\n"); |
| 471 | fprintf(stderr, " \t\tvalid architectures are %s\n", Join(supported_archs).c_str()); |
| 472 | fprintf(stderr, "\n"); |
| 473 | fprintf(stderr, "Validation:\n"); |
| 474 | fprintf(stderr, " -p PATH\tcompare against NDK platform at PATH\n"); |
Josh Gao | acc3d80 | 2016-11-09 18:22:44 -0800 | [diff] [blame] | 475 | fprintf(stderr, " -s\t\tenable strict warnings\n"); |
Josh Gao | 62aaf8f | 2016-06-02 14:27:21 -0700 | [diff] [blame] | 476 | fprintf(stderr, "\n"); |
Josh Gao | f8592a3 | 2016-07-26 18:58:27 -0700 | [diff] [blame] | 477 | fprintf(stderr, "Preprocessing:\n"); |
| 478 | fprintf(stderr, " -o PATH\tpreprocess header files and emit them at PATH\n"); |
Josh Gao | d744a9b | 2017-04-03 11:24:48 -0700 | [diff] [blame] | 479 | fprintf(stderr, " -f\t\tpreprocess header files even if validation fails\n"); |
Josh Gao | f8592a3 | 2016-07-26 18:58:27 -0700 | [diff] [blame] | 480 | fprintf(stderr, "\n"); |
Josh Gao | 62aaf8f | 2016-06-02 14:27:21 -0700 | [diff] [blame] | 481 | fprintf(stderr, "Miscellaneous:\n"); |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 482 | fprintf(stderr, " -F\t\tdo not ignore FORTIFY headers by default\n"); |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 483 | fprintf(stderr, " -d\t\tdump function availability\n"); |
Josh Gao | 16016df | 2016-11-07 18:27:16 -0800 | [diff] [blame] | 484 | fprintf(stderr, " -j THREADS\tmaximum number of threads to use\n"); |
Josh Gao | acc3d80 | 2016-11-09 18:22:44 -0800 | [diff] [blame] | 485 | fprintf(stderr, " -v\t\tenable verbose logging\n"); |
Josh Gao | 62aaf8f | 2016-06-02 14:27:21 -0700 | [diff] [blame] | 486 | fprintf(stderr, " -h\t\tdisplay this message\n"); |
| 487 | exit(0); |
| 488 | } |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 489 | } |
| 490 | |
Andreas Gampe | d10d3ee | 2017-04-28 19:32:13 -0700 | [diff] [blame] | 491 | // versioner uses a prebuilt version of clang, which is not up-to-date wrt/ |
| 492 | // container annotations. So disable container overflow checking. b/37775238 |
| 493 | extern "C" const char* __asan_default_options() { |
| 494 | return "detect_container_overflow=0"; |
| 495 | } |
| 496 | |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 497 | int main(int argc, char** argv) { |
| 498 | std::string cwd = getWorkingDir() + "/"; |
| 499 | bool default_args = true; |
| 500 | std::string platform_dir; |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 501 | std::set<Arch> selected_architectures; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 502 | std::set<int> selected_levels; |
Josh Gao | f8592a3 | 2016-07-26 18:58:27 -0700 | [diff] [blame] | 503 | std::string preprocessor_output_path; |
| 504 | bool force = false; |
Josh Gao | 16016df | 2016-11-07 18:27:16 -0800 | [diff] [blame] | 505 | bool dump = false; |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 506 | bool ignore_fortify_headers = true; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 507 | |
| 508 | int c; |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 509 | while ((c = getopt(argc, argv, "a:r:p:so:fdj:vhFi")) != -1) { |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 510 | default_args = false; |
| 511 | switch (c) { |
| 512 | case 'a': { |
| 513 | char* end; |
| 514 | int api_level = strtol(optarg, &end, 10); |
| 515 | if (end == optarg || strlen(end) > 0) { |
| 516 | usage(); |
| 517 | } |
| 518 | |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 519 | selected_levels.insert(api_level); |
| 520 | break; |
| 521 | } |
| 522 | |
| 523 | case 'r': { |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 524 | Arch arch = arch_from_string(optarg); |
| 525 | selected_architectures.insert(arch); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 526 | break; |
| 527 | } |
| 528 | |
| 529 | case 'p': { |
| 530 | if (!platform_dir.empty()) { |
| 531 | usage(); |
| 532 | } |
| 533 | |
| 534 | platform_dir = optarg; |
| 535 | |
Josh Gao | f8592a3 | 2016-07-26 18:58:27 -0700 | [diff] [blame] | 536 | if (platform_dir.empty()) { |
| 537 | usage(); |
| 538 | } |
| 539 | |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 540 | struct stat st; |
| 541 | if (stat(platform_dir.c_str(), &st) != 0) { |
| 542 | err(1, "failed to stat platform directory '%s'", platform_dir.c_str()); |
| 543 | } |
Josh Gao | ab25d0b | 2017-08-10 10:50:33 -0700 | [diff] [blame] | 544 | if (!S_ISDIR(st.st_mode) && !S_ISREG(st.st_mode)) { |
| 545 | errx(1, "'%s' is not a file or directory", optarg); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 546 | } |
| 547 | break; |
| 548 | } |
| 549 | |
Josh Gao | acc3d80 | 2016-11-09 18:22:44 -0800 | [diff] [blame] | 550 | case 's': |
| 551 | strict = true; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 552 | break; |
| 553 | |
Josh Gao | f8592a3 | 2016-07-26 18:58:27 -0700 | [diff] [blame] | 554 | case 'o': |
| 555 | if (!preprocessor_output_path.empty()) { |
| 556 | usage(); |
| 557 | } |
| 558 | preprocessor_output_path = optarg; |
| 559 | if (preprocessor_output_path.empty()) { |
| 560 | usage(); |
| 561 | } |
| 562 | break; |
| 563 | |
| 564 | case 'f': |
| 565 | force = true; |
| 566 | break; |
| 567 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 568 | case 'd': |
| 569 | dump = true; |
| 570 | break; |
| 571 | |
Josh Gao | 16016df | 2016-11-07 18:27:16 -0800 | [diff] [blame] | 572 | case 'j': |
| 573 | if (!android::base::ParseInt<int>(optarg, &max_thread_count, 1)) { |
| 574 | usage(); |
| 575 | } |
| 576 | break; |
| 577 | |
Josh Gao | acc3d80 | 2016-11-09 18:22:44 -0800 | [diff] [blame] | 578 | case 'v': |
| 579 | verbose = true; |
| 580 | break; |
| 581 | |
Josh Gao | 62aaf8f | 2016-06-02 14:27:21 -0700 | [diff] [blame] | 582 | case 'h': |
| 583 | usage(true); |
| 584 | break; |
| 585 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 586 | case 'i': |
| 587 | // Secret option for tests to -include <android/versioning.h>. |
| 588 | add_include = true; |
| 589 | break; |
| 590 | |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 591 | case 'F': |
| 592 | ignore_fortify_headers = false; |
| 593 | break; |
| 594 | |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 595 | default: |
| 596 | usage(); |
| 597 | break; |
| 598 | } |
| 599 | } |
| 600 | |
Josh Gao | 9b5af7a | 2016-06-02 14:29:13 -0700 | [diff] [blame] | 601 | if (argc - optind > 2 || optind > argc) { |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 602 | usage(); |
| 603 | } |
| 604 | |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 605 | HeaderLocationInformation location; |
Josh Gao | 9b5af7a | 2016-06-02 14:29:13 -0700 | [diff] [blame] | 606 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 607 | const char* top = getenv("ANDROID_BUILD_TOP"); |
| 608 | if (!top && (optind == argc || add_include)) { |
| 609 | fprintf(stderr, "versioner: failed to autodetect bionic paths. Is ANDROID_BUILD_TOP set?\n"); |
| 610 | usage(); |
| 611 | } |
| 612 | |
Josh Gao | 9b5af7a | 2016-06-02 14:29:13 -0700 | [diff] [blame] | 613 | if (optind == argc) { |
| 614 | // Neither HEADER_PATH nor DEPS_PATH were specified, so try to figure them out. |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 615 | std::string versioner_dir = to_string(top) + "/bionic/tools/versioner"; |
Josh Gao | ab25d0b | 2017-08-10 10:50:33 -0700 | [diff] [blame] | 616 | location.header_path = versioner_dir + "/current"; |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 617 | location.dependency_dir = versioner_dir + "/dependencies"; |
Josh Gao | 9b5af7a | 2016-06-02 14:29:13 -0700 | [diff] [blame] | 618 | } else { |
Josh Gao | ab25d0b | 2017-08-10 10:50:33 -0700 | [diff] [blame] | 619 | if (!android::base::Realpath(argv[optind], &location.header_path)) { |
Josh Gao | b50b8c8 | 2017-04-27 17:22:52 -0700 | [diff] [blame] | 620 | err(1, "failed to get realpath for path '%s'", argv[optind]); |
| 621 | } |
Josh Gao | 9b5af7a | 2016-06-02 14:29:13 -0700 | [diff] [blame] | 622 | |
| 623 | if (argc - optind == 2) { |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 624 | location.dependency_dir = argv[optind + 1]; |
Josh Gao | 9b5af7a | 2016-06-02 14:29:13 -0700 | [diff] [blame] | 625 | } |
| 626 | } |
| 627 | |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 628 | // Every file that lives in bits/fortify is logically a part of a header outside of bits/fortify. |
| 629 | // This makes the files there impossible to build on their own. |
| 630 | if (ignore_fortify_headers) { |
Josh Gao | ab25d0b | 2017-08-10 10:50:33 -0700 | [diff] [blame] | 631 | std::string fortify_path = location.header_path; |
| 632 | if (!android::base::EndsWith(location.header_path, "/")) { |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 633 | fortify_path += '/'; |
| 634 | } |
| 635 | fortify_path += "bits/fortify"; |
| 636 | location.ignored_directories.insert(std::move(fortify_path)); |
| 637 | } |
| 638 | |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 639 | if (selected_levels.empty()) { |
Josh Gao | 35aa213 | 2017-10-24 17:05:49 -0700 | [diff] [blame] | 640 | selected_levels = default_levels; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | if (selected_architectures.empty()) { |
| 644 | selected_architectures = supported_archs; |
| 645 | } |
| 646 | |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 647 | |
| 648 | struct stat st; |
Josh Gao | ab25d0b | 2017-08-10 10:50:33 -0700 | [diff] [blame] | 649 | if (const char *path = location.header_path.c_str(); stat(path, &st) != 0) { |
| 650 | err(1, "failed to stat '%s'", path); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | std::set<CompilationType> compilation_types; |
Josh Gao | c166113 | 2017-12-12 11:51:32 -0800 | [diff] [blame^] | 654 | std::optional<NdkSymbolDatabase> symbol_database; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 655 | |
| 656 | compilation_types = generateCompilationTypes(selected_architectures, selected_levels); |
| 657 | |
Josh Gao | 338cf12 | 2016-11-09 18:01:41 -0800 | [diff] [blame] | 658 | auto start = std::chrono::high_resolution_clock::now(); |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 659 | std::unique_ptr<HeaderDatabase> declaration_database = |
George Burgess IV | b97049c | 2017-07-24 15:05:05 -0700 | [diff] [blame] | 660 | compileHeaders(compilation_types, location); |
Josh Gao | 338cf12 | 2016-11-09 18:01:41 -0800 | [diff] [blame] | 661 | auto end = std::chrono::high_resolution_clock::now(); |
| 662 | |
| 663 | if (verbose) { |
| 664 | auto diff = (end - start) / 1.0ms; |
| 665 | printf("Compiled headers for %zu targets in %0.2LFms\n", compilation_types.size(), diff); |
| 666 | } |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 667 | |
Josh Gao | 16016df | 2016-11-07 18:27:16 -0800 | [diff] [blame] | 668 | bool failed = false; |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 669 | if (dump) { |
Josh Gao | ab25d0b | 2017-08-10 10:50:33 -0700 | [diff] [blame] | 670 | declaration_database->dump(location.header_path + "/"); |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 671 | } else { |
| 672 | if (!sanityCheck(declaration_database.get())) { |
| 673 | printf("versioner: sanity check failed\n"); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 674 | failed = true; |
| 675 | } |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 676 | |
Josh Gao | c166113 | 2017-12-12 11:51:32 -0800 | [diff] [blame^] | 677 | if (symbol_database) { |
| 678 | if (!checkVersions(compilation_types, declaration_database.get(), *symbol_database)) { |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 679 | printf("versioner: version check failed\n"); |
| 680 | failed = true; |
| 681 | } |
| 682 | } |
| 683 | } |
Josh Gao | f8592a3 | 2016-07-26 18:58:27 -0700 | [diff] [blame] | 684 | |
| 685 | if (!preprocessor_output_path.empty() && (force || !failed)) { |
Josh Gao | ab25d0b | 2017-08-10 10:50:33 -0700 | [diff] [blame] | 686 | failed = !preprocessHeaders(preprocessor_output_path, location.header_path, |
| 687 | declaration_database.get()); |
Josh Gao | f8592a3 | 2016-07-26 18:58:27 -0700 | [diff] [blame] | 688 | } |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 689 | return failed; |
| 690 | } |