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