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