Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <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 | |
| 25 | #include <atomic> |
| 26 | #include <iostream> |
| 27 | #include <map> |
| 28 | #include <memory> |
| 29 | #include <set> |
| 30 | #include <sstream> |
| 31 | #include <string> |
| 32 | #include <thread> |
| 33 | #include <unordered_map> |
| 34 | #include <vector> |
| 35 | |
| 36 | #include <clang/Frontend/TextDiagnosticPrinter.h> |
| 37 | #include <clang/Tooling/Tooling.h> |
| 38 | #include <llvm/ADT/StringRef.h> |
| 39 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 40 | #include "Arch.h" |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 41 | #include "DeclarationDatabase.h" |
| 42 | #include "SymbolDatabase.h" |
| 43 | #include "Utils.h" |
| 44 | #include "versioner.h" |
| 45 | |
| 46 | using namespace std::string_literals; |
| 47 | using namespace clang; |
| 48 | using namespace clang::tooling; |
| 49 | |
| 50 | bool verbose; |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 51 | static bool add_include; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 52 | |
| 53 | class HeaderCompilationDatabase : public CompilationDatabase { |
| 54 | CompilationType type; |
| 55 | std::string cwd; |
| 56 | std::vector<std::string> headers; |
| 57 | std::vector<std::string> include_dirs; |
| 58 | |
| 59 | public: |
| 60 | HeaderCompilationDatabase(CompilationType type, std::string cwd, std::vector<std::string> headers, |
| 61 | std::vector<std::string> include_dirs) |
| 62 | : type(type), |
| 63 | cwd(std::move(cwd)), |
| 64 | headers(std::move(headers)), |
| 65 | include_dirs(std::move(include_dirs)) { |
| 66 | } |
| 67 | |
| 68 | CompileCommand generateCompileCommand(const std::string& filename) const { |
| 69 | std::vector<std::string> command = { "clang-tool", filename, "-nostdlibinc" }; |
| 70 | for (const auto& dir : include_dirs) { |
| 71 | command.push_back("-isystem"); |
| 72 | command.push_back(dir); |
| 73 | } |
| 74 | command.push_back("-std=c11"); |
| 75 | command.push_back("-DANDROID"); |
| 76 | command.push_back("-D__ANDROID_API__="s + std::to_string(type.api_level)); |
| 77 | command.push_back("-D_FORTIFY_SOURCE=2"); |
| 78 | command.push_back("-D_GNU_SOURCE"); |
| 79 | command.push_back("-Wno-unknown-attributes"); |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 80 | command.push_back("-Wno-pragma-once-outside-header"); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 81 | command.push_back("-target"); |
| 82 | command.push_back(arch_targets[type.arch]); |
| 83 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 84 | if (add_include) { |
| 85 | const char* top = getenv("ANDROID_BUILD_TOP"); |
| 86 | std::string header_path = to_string(top) + "/bionic/libc/include/android/versioning.h"; |
| 87 | command.push_back("-include"); |
| 88 | command.push_back(std::move(header_path)); |
| 89 | } |
| 90 | |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 91 | return CompileCommand(cwd, filename, command); |
| 92 | } |
| 93 | |
| 94 | std::vector<CompileCommand> getAllCompileCommands() const override { |
| 95 | std::vector<CompileCommand> commands; |
| 96 | for (const std::string& file : headers) { |
| 97 | commands.push_back(generateCompileCommand(file)); |
| 98 | } |
| 99 | return commands; |
| 100 | } |
| 101 | |
| 102 | std::vector<CompileCommand> getCompileCommands(StringRef file) const override { |
| 103 | std::vector<CompileCommand> commands; |
| 104 | commands.push_back(generateCompileCommand(file)); |
| 105 | return commands; |
| 106 | } |
| 107 | |
| 108 | std::vector<std::string> getAllFiles() const override { |
| 109 | return headers; |
| 110 | } |
| 111 | }; |
| 112 | |
| 113 | struct CompilationRequirements { |
| 114 | std::vector<std::string> headers; |
| 115 | std::vector<std::string> dependencies; |
| 116 | }; |
| 117 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 118 | static CompilationRequirements collectRequirements(const Arch& arch, const std::string& header_dir, |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 119 | const std::string& dependency_dir) { |
| 120 | std::vector<std::string> headers = collectFiles(header_dir); |
| 121 | |
| 122 | std::vector<std::string> dependencies = { header_dir }; |
| 123 | if (!dependency_dir.empty()) { |
| 124 | auto collect_children = [&dependencies](const std::string& dir_path) { |
| 125 | DIR* dir = opendir(dir_path.c_str()); |
| 126 | if (!dir) { |
| 127 | err(1, "failed to open dependency directory '%s'", dir_path.c_str()); |
| 128 | } |
| 129 | |
| 130 | struct dirent* dent; |
| 131 | while ((dent = readdir(dir))) { |
| 132 | if (dent->d_name[0] == '.') { |
| 133 | continue; |
| 134 | } |
| 135 | |
| 136 | // TODO: Resolve symlinks. |
| 137 | std::string dependency = dir_path + "/" + dent->d_name; |
| 138 | |
| 139 | struct stat st; |
| 140 | if (stat(dependency.c_str(), &st) != 0) { |
| 141 | err(1, "failed to stat dependency '%s'", dependency.c_str()); |
| 142 | } |
| 143 | |
| 144 | if (!S_ISDIR(st.st_mode)) { |
| 145 | errx(1, "'%s' is not a directory", dependency.c_str()); |
| 146 | } |
| 147 | |
| 148 | dependencies.push_back(dependency); |
| 149 | } |
| 150 | |
| 151 | closedir(dir); |
| 152 | }; |
| 153 | |
| 154 | collect_children(dependency_dir + "/common"); |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 155 | collect_children(dependency_dir + "/" + to_string(arch)); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | auto new_end = std::remove_if(headers.begin(), headers.end(), [&arch](llvm::StringRef header) { |
| 159 | for (const auto& it : header_blacklist) { |
| 160 | if (it.second.find(arch) == it.second.end()) { |
| 161 | continue; |
| 162 | } |
| 163 | |
| 164 | if (header.endswith("/" + it.first)) { |
| 165 | return true; |
| 166 | } |
| 167 | } |
| 168 | return false; |
| 169 | }); |
| 170 | |
| 171 | headers.erase(new_end, headers.end()); |
| 172 | |
| 173 | CompilationRequirements result = { .headers = headers, .dependencies = dependencies }; |
| 174 | return result; |
| 175 | } |
| 176 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 177 | static std::set<CompilationType> generateCompilationTypes(const std::set<Arch> selected_architectures, |
| 178 | const std::set<int>& selected_levels) { |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 179 | std::set<CompilationType> result; |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 180 | for (const auto& arch : selected_architectures) { |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 181 | int min_api = arch_min_api[arch]; |
| 182 | for (int api_level : selected_levels) { |
| 183 | if (api_level < min_api) { |
| 184 | continue; |
| 185 | } |
| 186 | CompilationType type = { .arch = arch, .api_level = api_level }; |
| 187 | result.insert(type); |
| 188 | } |
| 189 | } |
| 190 | return result; |
| 191 | } |
| 192 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 193 | static std::unique_ptr<HeaderDatabase> compileHeaders(const std::set<CompilationType>& types, |
| 194 | const std::string& header_dir, |
| 195 | const std::string& dependency_dir, |
| 196 | bool* failed) { |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 197 | constexpr size_t thread_count = 8; |
| 198 | size_t threads_created = 0; |
| 199 | std::mutex mutex; |
| 200 | std::vector<std::thread> threads(thread_count); |
| 201 | |
| 202 | std::map<CompilationType, HeaderDatabase> header_databases; |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 203 | std::unordered_map<Arch, CompilationRequirements> requirements; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 204 | |
| 205 | std::string cwd = getWorkingDir(); |
| 206 | bool errors = false; |
| 207 | |
| 208 | for (const auto& type : types) { |
| 209 | if (requirements.count(type.arch) == 0) { |
| 210 | requirements[type.arch] = collectRequirements(type.arch, header_dir, dependency_dir); |
| 211 | } |
| 212 | } |
| 213 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 214 | auto result = std::make_unique<HeaderDatabase>(); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 215 | for (const auto& type : types) { |
| 216 | size_t thread_id = threads_created++; |
| 217 | if (thread_id >= thread_count) { |
| 218 | thread_id = thread_id % thread_count; |
| 219 | threads[thread_id].join(); |
| 220 | } |
| 221 | |
| 222 | threads[thread_id] = std::thread( |
| 223 | [&](CompilationType type) { |
| 224 | const auto& req = requirements[type.arch]; |
| 225 | |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 226 | HeaderCompilationDatabase compilation_database(type, cwd, req.headers, req.dependencies); |
| 227 | |
| 228 | ClangTool tool(compilation_database, req.headers); |
| 229 | |
| 230 | clang::DiagnosticOptions diagnostic_options; |
| 231 | std::vector<std::unique_ptr<ASTUnit>> asts; |
| 232 | tool.buildASTs(asts); |
| 233 | for (const auto& ast : asts) { |
| 234 | clang::DiagnosticsEngine& diagnostics_engine = ast->getDiagnostics(); |
| 235 | if (diagnostics_engine.getNumWarnings() || diagnostics_engine.hasErrorOccurred()) { |
| 236 | std::unique_lock<std::mutex> l(mutex); |
| 237 | errors = true; |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 238 | printf("versioner: compilation failure for %s in %s\n", to_string(type).c_str(), |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 239 | ast->getOriginalSourceFileName().str().c_str()); |
| 240 | } |
| 241 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 242 | result->parseAST(type, ast.get()); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 243 | } |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 244 | }, |
| 245 | type); |
| 246 | } |
| 247 | |
| 248 | if (threads_created < thread_count) { |
| 249 | threads.resize(threads_created); |
| 250 | } |
| 251 | |
| 252 | for (auto& thread : threads) { |
| 253 | thread.join(); |
| 254 | } |
| 255 | |
| 256 | if (errors) { |
| 257 | printf("versioner: compilation generated warnings or errors\n"); |
| 258 | *failed = errors; |
| 259 | } |
| 260 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 261 | return result; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 264 | // Perform a sanity check on a symbol's declarations, enforcing the following invariants: |
| 265 | // 1. At most one inline definition of the function exists. |
| 266 | // 2. All of the availability declarations for a symbol are compatible. |
| 267 | // If a function is declared as an inline before a certain version, the inline definition |
| 268 | // should have no version tag. |
| 269 | // 3. Each availability type must only be present globally or on a per-arch basis. |
| 270 | // (e.g. __INTRODUCED_IN_ARM(9) __INTRODUCED_IN_X86(10) __DEPRECATED_IN(11) is fine, |
| 271 | // but not __INTRODUCED_IN(9) __INTRODUCED_IN_X86(10)) |
| 272 | static bool checkSymbol(const Symbol& symbol) { |
| 273 | std::string cwd = getWorkingDir() + "/"; |
| 274 | |
| 275 | const Declaration* inline_definition = nullptr; |
| 276 | for (const auto& decl_it : symbol.declarations) { |
| 277 | const Declaration* decl = &decl_it.second; |
| 278 | if (decl->is_definition) { |
| 279 | if (inline_definition) { |
| 280 | fprintf(stderr, "versioner: multiple definitions of symbol %s\n", symbol.name.c_str()); |
| 281 | symbol.dump(cwd); |
| 282 | inline_definition->dump(cwd); |
| 283 | return false; |
| 284 | } |
| 285 | |
| 286 | inline_definition = decl; |
| 287 | } |
| 288 | |
| 289 | DeclarationAvailability availability; |
| 290 | if (!decl->calculateAvailability(&availability)) { |
| 291 | fprintf(stderr, "versioner: failed to calculate availability for declaration:\n"); |
Josh Gao | 566735d | 2016-08-02 15:07:32 -0700 | [diff] [blame^] | 292 | decl->dump(cwd, stderr, 2); |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 293 | return false; |
| 294 | } |
| 295 | |
| 296 | if (decl->is_definition && !availability.empty()) { |
| 297 | fprintf(stderr, "versioner: inline definition has non-empty versioning information:\n"); |
Josh Gao | 566735d | 2016-08-02 15:07:32 -0700 | [diff] [blame^] | 298 | decl->dump(cwd, stderr, 2); |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 299 | return false; |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | DeclarationAvailability availability; |
| 304 | if (!symbol.calculateAvailability(&availability)) { |
| 305 | fprintf(stderr, "versioner: inconsistent availability for symbol '%s'\n", symbol.name.c_str()); |
| 306 | symbol.dump(cwd); |
| 307 | return false; |
| 308 | } |
| 309 | |
| 310 | // TODO: Check invariant #3. |
| 311 | return true; |
| 312 | } |
| 313 | |
| 314 | static bool sanityCheck(const HeaderDatabase* database) { |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 315 | bool error = false; |
Josh Gao | 958f3b3 | 2016-06-03 13:44:00 -0700 | [diff] [blame] | 316 | std::string cwd = getWorkingDir() + "/"; |
| 317 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 318 | for (const auto& symbol_it : database->symbols) { |
| 319 | if (!checkSymbol(symbol_it.second)) { |
| 320 | error = true; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 321 | } |
| 322 | } |
| 323 | return !error; |
| 324 | } |
| 325 | |
| 326 | // Check that our symbol availability declarations match the actual NDK |
| 327 | // platform symbol availability. |
| 328 | static bool checkVersions(const std::set<CompilationType>& types, |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 329 | const HeaderDatabase* header_database, |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 330 | const NdkSymbolDatabase& symbol_database) { |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 331 | std::string cwd = getWorkingDir() + "/"; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 332 | bool failed = false; |
| 333 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 334 | std::map<Arch, std::set<CompilationType>> arch_types; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 335 | for (const CompilationType& type : types) { |
| 336 | arch_types[type.arch].insert(type); |
| 337 | } |
| 338 | |
Josh Gao | d67dbf0 | 2016-06-02 15:21:14 -0700 | [diff] [blame] | 339 | std::set<std::string> completely_unavailable; |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 340 | std::map<std::string, std::set<CompilationType>> missing_availability; |
| 341 | std::map<std::string, std::set<CompilationType>> extra_availability; |
Josh Gao | d67dbf0 | 2016-06-02 15:21:14 -0700 | [diff] [blame] | 342 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 343 | for (const auto& symbol_it : header_database->symbols) { |
| 344 | const auto& symbol_name = symbol_it.first; |
| 345 | DeclarationAvailability symbol_availability; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 346 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 347 | if (!symbol_it.second.calculateAvailability(&symbol_availability)) { |
| 348 | errx(1, "failed to calculate symbol availability"); |
| 349 | } |
| 350 | |
| 351 | const auto platform_availability_it = symbol_database.find(symbol_name); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 352 | if (platform_availability_it == symbol_database.end()) { |
Josh Gao | d67dbf0 | 2016-06-02 15:21:14 -0700 | [diff] [blame] | 353 | completely_unavailable.insert(symbol_name); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 354 | continue; |
| 355 | } |
| 356 | |
| 357 | const auto& platform_availability = platform_availability_it->second; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 358 | |
| 359 | for (const CompilationType& type : types) { |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 360 | bool should_be_available = true; |
| 361 | const auto& global_availability = symbol_availability.global_availability; |
| 362 | const auto& arch_availability = symbol_availability.arch_availability[type.arch]; |
| 363 | if (global_availability.introduced != 0 && global_availability.introduced > type.api_level) { |
| 364 | should_be_available = false; |
| 365 | } |
| 366 | |
| 367 | if (arch_availability.introduced != 0 && arch_availability.introduced > type.api_level) { |
| 368 | should_be_available = false; |
| 369 | } |
| 370 | |
| 371 | if (global_availability.obsoleted != 0 && global_availability.obsoleted <= type.api_level) { |
| 372 | should_be_available = false; |
| 373 | } |
| 374 | |
| 375 | if (arch_availability.obsoleted != 0 && arch_availability.obsoleted <= type.api_level) { |
| 376 | should_be_available = false; |
| 377 | } |
| 378 | |
| 379 | if (arch_availability.future) { |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 380 | continue; |
| 381 | } |
| 382 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 383 | // The function declaration might be (validly) missing for the given CompilationType. |
| 384 | if (!symbol_it.second.hasDeclaration(type)) { |
| 385 | should_be_available = false; |
| 386 | } |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 387 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 388 | bool is_available = platform_availability.count(type); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 389 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 390 | if (should_be_available != is_available) { |
| 391 | if (is_available) { |
| 392 | extra_availability[symbol_name].insert(type); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 393 | } else { |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 394 | missing_availability[symbol_name].insert(type); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 395 | } |
| 396 | } |
| 397 | } |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 398 | } |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 399 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 400 | for (const auto& it : symbol_database) { |
| 401 | const std::string& symbol_name = it.first; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 402 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 403 | bool symbol_error = false; |
| 404 | auto missing_it = missing_availability.find(symbol_name); |
| 405 | if (missing_it != missing_availability.end()) { |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 406 | 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] | 407 | Join(missing_it->second, ", ").c_str()); |
| 408 | symbol_error = true; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 409 | failed = true; |
| 410 | } |
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 (verbose) { |
| 413 | auto extra_it = extra_availability.find(symbol_name); |
| 414 | if (extra_it != extra_availability.end()) { |
| 415 | printf("%s: declaration marked unavailable but symbol available in [%s]\n", |
| 416 | symbol_name.c_str(), Join(extra_it->second, ", ").c_str()); |
| 417 | symbol_error = true; |
| 418 | failed = true; |
Josh Gao | 958f3b3 | 2016-06-03 13:44:00 -0700 | [diff] [blame] | 419 | } |
| 420 | } |
| 421 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 422 | if (symbol_error) { |
| 423 | auto symbol_it = header_database->symbols.find(symbol_name); |
| 424 | if (symbol_it == header_database->symbols.end()) { |
| 425 | errx(1, "failed to find symbol in header database"); |
| 426 | } |
| 427 | symbol_it->second.dump(cwd); |
Josh Gao | d67dbf0 | 2016-06-02 15:21:14 -0700 | [diff] [blame] | 428 | } |
Josh Gao | d67dbf0 | 2016-06-02 15:21:14 -0700 | [diff] [blame] | 429 | } |
| 430 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 431 | // TODO: Verify that function/variable declarations are actually function/variable symbols. |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 432 | return !failed; |
| 433 | } |
| 434 | |
Josh Gao | 62aaf8f | 2016-06-02 14:27:21 -0700 | [diff] [blame] | 435 | static void usage(bool help = false) { |
| 436 | fprintf(stderr, "Usage: versioner [OPTION]... [HEADER_PATH] [DEPS_PATH]\n"); |
| 437 | if (!help) { |
| 438 | printf("Try 'versioner -h' for more information.\n"); |
| 439 | exit(1); |
| 440 | } else { |
| 441 | 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] | 442 | 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] | 443 | fprintf(stderr, "\n"); |
| 444 | fprintf(stderr, "Target specification (defaults to all):\n"); |
| 445 | fprintf(stderr, " -a API_LEVEL\tbuild with specified API level (can be repeated)\n"); |
| 446 | fprintf(stderr, " \t\tvalid levels are %s\n", Join(supported_levels).c_str()); |
| 447 | fprintf(stderr, " -r ARCH\tbuild with specified architecture (can be repeated)\n"); |
| 448 | fprintf(stderr, " \t\tvalid architectures are %s\n", Join(supported_archs).c_str()); |
| 449 | fprintf(stderr, "\n"); |
| 450 | fprintf(stderr, "Validation:\n"); |
| 451 | fprintf(stderr, " -p PATH\tcompare against NDK platform at PATH\n"); |
| 452 | fprintf(stderr, " -v\t\tenable verbose warnings\n"); |
| 453 | fprintf(stderr, "\n"); |
| 454 | fprintf(stderr, "Miscellaneous:\n"); |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 455 | fprintf(stderr, " -d\t\tdump function availability\n"); |
Josh Gao | 62aaf8f | 2016-06-02 14:27:21 -0700 | [diff] [blame] | 456 | fprintf(stderr, " -h\t\tdisplay this message\n"); |
| 457 | exit(0); |
| 458 | } |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | int main(int argc, char** argv) { |
| 462 | std::string cwd = getWorkingDir() + "/"; |
| 463 | bool default_args = true; |
| 464 | std::string platform_dir; |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 465 | std::set<Arch> selected_architectures; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 466 | std::set<int> selected_levels; |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 467 | bool dump = false; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 468 | |
| 469 | int c; |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 470 | while ((c = getopt(argc, argv, "a:r:p:vdhi")) != -1) { |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 471 | default_args = false; |
| 472 | switch (c) { |
| 473 | case 'a': { |
| 474 | char* end; |
| 475 | int api_level = strtol(optarg, &end, 10); |
| 476 | if (end == optarg || strlen(end) > 0) { |
| 477 | usage(); |
| 478 | } |
| 479 | |
| 480 | if (supported_levels.count(api_level) == 0) { |
| 481 | errx(1, "unsupported API level %d", api_level); |
| 482 | } |
| 483 | |
| 484 | selected_levels.insert(api_level); |
| 485 | break; |
| 486 | } |
| 487 | |
| 488 | case 'r': { |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 489 | Arch arch = arch_from_string(optarg); |
| 490 | selected_architectures.insert(arch); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 491 | break; |
| 492 | } |
| 493 | |
| 494 | case 'p': { |
| 495 | if (!platform_dir.empty()) { |
| 496 | usage(); |
| 497 | } |
| 498 | |
| 499 | platform_dir = optarg; |
| 500 | |
| 501 | struct stat st; |
| 502 | if (stat(platform_dir.c_str(), &st) != 0) { |
| 503 | err(1, "failed to stat platform directory '%s'", platform_dir.c_str()); |
| 504 | } |
| 505 | if (!S_ISDIR(st.st_mode)) { |
| 506 | errx(1, "'%s' is not a directory", optarg); |
| 507 | } |
| 508 | break; |
| 509 | } |
| 510 | |
| 511 | case 'v': |
| 512 | verbose = true; |
| 513 | break; |
| 514 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 515 | case 'd': |
| 516 | dump = true; |
| 517 | break; |
| 518 | |
Josh Gao | 62aaf8f | 2016-06-02 14:27:21 -0700 | [diff] [blame] | 519 | case 'h': |
| 520 | usage(true); |
| 521 | break; |
| 522 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 523 | case 'i': |
| 524 | // Secret option for tests to -include <android/versioning.h>. |
| 525 | add_include = true; |
| 526 | break; |
| 527 | |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 528 | default: |
| 529 | usage(); |
| 530 | break; |
| 531 | } |
| 532 | } |
| 533 | |
Josh Gao | 9b5af7a | 2016-06-02 14:29:13 -0700 | [diff] [blame] | 534 | if (argc - optind > 2 || optind > argc) { |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 535 | usage(); |
| 536 | } |
| 537 | |
Josh Gao | 9b5af7a | 2016-06-02 14:29:13 -0700 | [diff] [blame] | 538 | std::string header_dir; |
| 539 | std::string dependency_dir; |
| 540 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 541 | const char* top = getenv("ANDROID_BUILD_TOP"); |
| 542 | if (!top && (optind == argc || add_include)) { |
| 543 | fprintf(stderr, "versioner: failed to autodetect bionic paths. Is ANDROID_BUILD_TOP set?\n"); |
| 544 | usage(); |
| 545 | } |
| 546 | |
Josh Gao | 9b5af7a | 2016-06-02 14:29:13 -0700 | [diff] [blame] | 547 | if (optind == argc) { |
| 548 | // 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] | 549 | std::string versioner_dir = to_string(top) + "/bionic/tools/versioner"; |
Josh Gao | 9b5af7a | 2016-06-02 14:29:13 -0700 | [diff] [blame] | 550 | header_dir = versioner_dir + "/current"; |
| 551 | dependency_dir = versioner_dir + "/dependencies"; |
| 552 | if (platform_dir.empty()) { |
| 553 | platform_dir = versioner_dir + "/platforms"; |
| 554 | } |
| 555 | } else { |
| 556 | header_dir = argv[optind]; |
| 557 | |
| 558 | if (argc - optind == 2) { |
| 559 | dependency_dir = argv[optind + 1]; |
| 560 | } |
| 561 | } |
| 562 | |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 563 | if (selected_levels.empty()) { |
| 564 | selected_levels = supported_levels; |
| 565 | } |
| 566 | |
| 567 | if (selected_architectures.empty()) { |
| 568 | selected_architectures = supported_archs; |
| 569 | } |
| 570 | |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 571 | |
| 572 | struct stat st; |
Josh Gao | 9b5af7a | 2016-06-02 14:29:13 -0700 | [diff] [blame] | 573 | if (stat(header_dir.c_str(), &st) != 0) { |
| 574 | err(1, "failed to stat '%s'", header_dir.c_str()); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 575 | } else if (!S_ISDIR(st.st_mode)) { |
Josh Gao | 9b5af7a | 2016-06-02 14:29:13 -0700 | [diff] [blame] | 576 | errx(1, "'%s' is not a directory", header_dir.c_str()); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | std::set<CompilationType> compilation_types; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 580 | NdkSymbolDatabase symbol_database; |
| 581 | |
| 582 | compilation_types = generateCompilationTypes(selected_architectures, selected_levels); |
| 583 | |
| 584 | // Do this before compiling so that we can early exit if the platforms don't match what we |
| 585 | // expect. |
| 586 | if (!platform_dir.empty()) { |
| 587 | symbol_database = parsePlatforms(compilation_types, platform_dir); |
| 588 | } |
| 589 | |
| 590 | bool failed = false; |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 591 | std::unique_ptr<HeaderDatabase> declaration_database = |
| 592 | compileHeaders(compilation_types, header_dir, dependency_dir, &failed); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 593 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 594 | if (dump) { |
| 595 | declaration_database->dump(header_dir + "/"); |
| 596 | } else { |
| 597 | if (!sanityCheck(declaration_database.get())) { |
| 598 | printf("versioner: sanity check failed\n"); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 599 | failed = true; |
| 600 | } |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 601 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 602 | if (!platform_dir.empty()) { |
| 603 | if (!checkVersions(compilation_types, declaration_database.get(), symbol_database)) { |
| 604 | printf("versioner: version check failed\n"); |
| 605 | failed = true; |
| 606 | } |
| 607 | } |
| 608 | } |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 609 | return failed; |
| 610 | } |