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 | |
| 40 | #include "DeclarationDatabase.h" |
| 41 | #include "SymbolDatabase.h" |
| 42 | #include "Utils.h" |
| 43 | #include "versioner.h" |
| 44 | |
| 45 | using namespace std::string_literals; |
| 46 | using namespace clang; |
| 47 | using namespace clang::tooling; |
| 48 | |
| 49 | bool verbose; |
| 50 | |
| 51 | class HeaderCompilationDatabase : public CompilationDatabase { |
| 52 | CompilationType type; |
| 53 | std::string cwd; |
| 54 | std::vector<std::string> headers; |
| 55 | std::vector<std::string> include_dirs; |
| 56 | |
| 57 | public: |
| 58 | HeaderCompilationDatabase(CompilationType type, std::string cwd, std::vector<std::string> headers, |
| 59 | std::vector<std::string> include_dirs) |
| 60 | : type(type), |
| 61 | cwd(std::move(cwd)), |
| 62 | headers(std::move(headers)), |
| 63 | include_dirs(std::move(include_dirs)) { |
| 64 | } |
| 65 | |
| 66 | CompileCommand generateCompileCommand(const std::string& filename) const { |
| 67 | std::vector<std::string> command = { "clang-tool", filename, "-nostdlibinc" }; |
| 68 | for (const auto& dir : include_dirs) { |
| 69 | command.push_back("-isystem"); |
| 70 | command.push_back(dir); |
| 71 | } |
| 72 | command.push_back("-std=c11"); |
| 73 | command.push_back("-DANDROID"); |
| 74 | command.push_back("-D__ANDROID_API__="s + std::to_string(type.api_level)); |
| 75 | command.push_back("-D_FORTIFY_SOURCE=2"); |
| 76 | command.push_back("-D_GNU_SOURCE"); |
| 77 | command.push_back("-Wno-unknown-attributes"); |
| 78 | command.push_back("-target"); |
| 79 | command.push_back(arch_targets[type.arch]); |
| 80 | |
| 81 | return CompileCommand(cwd, filename, command); |
| 82 | } |
| 83 | |
| 84 | std::vector<CompileCommand> getAllCompileCommands() const override { |
| 85 | std::vector<CompileCommand> commands; |
| 86 | for (const std::string& file : headers) { |
| 87 | commands.push_back(generateCompileCommand(file)); |
| 88 | } |
| 89 | return commands; |
| 90 | } |
| 91 | |
| 92 | std::vector<CompileCommand> getCompileCommands(StringRef file) const override { |
| 93 | std::vector<CompileCommand> commands; |
| 94 | commands.push_back(generateCompileCommand(file)); |
| 95 | return commands; |
| 96 | } |
| 97 | |
| 98 | std::vector<std::string> getAllFiles() const override { |
| 99 | return headers; |
| 100 | } |
| 101 | }; |
| 102 | |
| 103 | struct CompilationRequirements { |
| 104 | std::vector<std::string> headers; |
| 105 | std::vector<std::string> dependencies; |
| 106 | }; |
| 107 | |
| 108 | static CompilationRequirements collectRequirements(const std::string& arch, |
| 109 | const std::string& header_dir, |
| 110 | const std::string& dependency_dir) { |
| 111 | std::vector<std::string> headers = collectFiles(header_dir); |
| 112 | |
| 113 | std::vector<std::string> dependencies = { header_dir }; |
| 114 | if (!dependency_dir.empty()) { |
| 115 | auto collect_children = [&dependencies](const std::string& dir_path) { |
| 116 | DIR* dir = opendir(dir_path.c_str()); |
| 117 | if (!dir) { |
| 118 | err(1, "failed to open dependency directory '%s'", dir_path.c_str()); |
| 119 | } |
| 120 | |
| 121 | struct dirent* dent; |
| 122 | while ((dent = readdir(dir))) { |
| 123 | if (dent->d_name[0] == '.') { |
| 124 | continue; |
| 125 | } |
| 126 | |
| 127 | // TODO: Resolve symlinks. |
| 128 | std::string dependency = dir_path + "/" + dent->d_name; |
| 129 | |
| 130 | struct stat st; |
| 131 | if (stat(dependency.c_str(), &st) != 0) { |
| 132 | err(1, "failed to stat dependency '%s'", dependency.c_str()); |
| 133 | } |
| 134 | |
| 135 | if (!S_ISDIR(st.st_mode)) { |
| 136 | errx(1, "'%s' is not a directory", dependency.c_str()); |
| 137 | } |
| 138 | |
| 139 | dependencies.push_back(dependency); |
| 140 | } |
| 141 | |
| 142 | closedir(dir); |
| 143 | }; |
| 144 | |
| 145 | collect_children(dependency_dir + "/common"); |
| 146 | collect_children(dependency_dir + "/" + arch); |
| 147 | } |
| 148 | |
| 149 | auto new_end = std::remove_if(headers.begin(), headers.end(), [&arch](llvm::StringRef header) { |
| 150 | for (const auto& it : header_blacklist) { |
| 151 | if (it.second.find(arch) == it.second.end()) { |
| 152 | continue; |
| 153 | } |
| 154 | |
| 155 | if (header.endswith("/" + it.first)) { |
| 156 | return true; |
| 157 | } |
| 158 | } |
| 159 | return false; |
| 160 | }); |
| 161 | |
| 162 | headers.erase(new_end, headers.end()); |
| 163 | |
| 164 | CompilationRequirements result = { .headers = headers, .dependencies = dependencies }; |
| 165 | return result; |
| 166 | } |
| 167 | |
| 168 | static std::set<CompilationType> generateCompilationTypes( |
| 169 | const std::set<std::string> selected_architectures, const std::set<int>& selected_levels) { |
| 170 | std::set<CompilationType> result; |
| 171 | for (const std::string& arch : selected_architectures) { |
| 172 | int min_api = arch_min_api[arch]; |
| 173 | for (int api_level : selected_levels) { |
| 174 | if (api_level < min_api) { |
| 175 | continue; |
| 176 | } |
| 177 | CompilationType type = { .arch = arch, .api_level = api_level }; |
| 178 | result.insert(type); |
| 179 | } |
| 180 | } |
| 181 | return result; |
| 182 | } |
| 183 | |
| 184 | using DeclarationDatabase = std::map<std::string, std::map<CompilationType, Declaration>>; |
| 185 | |
| 186 | static DeclarationDatabase transposeHeaderDatabases( |
| 187 | const std::map<CompilationType, HeaderDatabase>& original) { |
| 188 | DeclarationDatabase result; |
| 189 | for (const auto& outer : original) { |
| 190 | const CompilationType& type = outer.first; |
| 191 | for (const auto& inner : outer.second.declarations) { |
| 192 | const std::string& symbol_name = inner.first; |
| 193 | result[symbol_name][type] = inner.second; |
| 194 | } |
| 195 | } |
| 196 | return result; |
| 197 | } |
| 198 | |
| 199 | static DeclarationDatabase compileHeaders(const std::set<CompilationType>& types, |
| 200 | const std::string& header_dir, |
| 201 | const std::string& dependency_dir, bool* failed) { |
| 202 | constexpr size_t thread_count = 8; |
| 203 | size_t threads_created = 0; |
| 204 | std::mutex mutex; |
| 205 | std::vector<std::thread> threads(thread_count); |
| 206 | |
| 207 | std::map<CompilationType, HeaderDatabase> header_databases; |
| 208 | std::unordered_map<std::string, CompilationRequirements> requirements; |
| 209 | |
| 210 | std::string cwd = getWorkingDir(); |
| 211 | bool errors = false; |
| 212 | |
| 213 | for (const auto& type : types) { |
| 214 | if (requirements.count(type.arch) == 0) { |
| 215 | requirements[type.arch] = collectRequirements(type.arch, header_dir, dependency_dir); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | for (const auto& type : types) { |
| 220 | size_t thread_id = threads_created++; |
| 221 | if (thread_id >= thread_count) { |
| 222 | thread_id = thread_id % thread_count; |
| 223 | threads[thread_id].join(); |
| 224 | } |
| 225 | |
| 226 | threads[thread_id] = std::thread( |
| 227 | [&](CompilationType type) { |
| 228 | const auto& req = requirements[type.arch]; |
| 229 | |
| 230 | HeaderDatabase database; |
| 231 | HeaderCompilationDatabase compilation_database(type, cwd, req.headers, req.dependencies); |
| 232 | |
| 233 | ClangTool tool(compilation_database, req.headers); |
| 234 | |
| 235 | clang::DiagnosticOptions diagnostic_options; |
| 236 | std::vector<std::unique_ptr<ASTUnit>> asts; |
| 237 | tool.buildASTs(asts); |
| 238 | for (const auto& ast : asts) { |
| 239 | clang::DiagnosticsEngine& diagnostics_engine = ast->getDiagnostics(); |
| 240 | if (diagnostics_engine.getNumWarnings() || diagnostics_engine.hasErrorOccurred()) { |
| 241 | std::unique_lock<std::mutex> l(mutex); |
| 242 | errors = true; |
| 243 | printf("versioner: compilation failure for %s in %s\n", type.describe().c_str(), |
| 244 | ast->getOriginalSourceFileName().str().c_str()); |
| 245 | } |
| 246 | |
| 247 | database.parseAST(ast.get()); |
| 248 | } |
| 249 | |
| 250 | std::unique_lock<std::mutex> l(mutex); |
| 251 | header_databases[type] = database; |
| 252 | }, |
| 253 | type); |
| 254 | } |
| 255 | |
| 256 | if (threads_created < thread_count) { |
| 257 | threads.resize(threads_created); |
| 258 | } |
| 259 | |
| 260 | for (auto& thread : threads) { |
| 261 | thread.join(); |
| 262 | } |
| 263 | |
| 264 | if (errors) { |
| 265 | printf("versioner: compilation generated warnings or errors\n"); |
| 266 | *failed = errors; |
| 267 | } |
| 268 | |
| 269 | return transposeHeaderDatabases(header_databases); |
| 270 | } |
| 271 | |
| 272 | static bool sanityCheck(const std::set<CompilationType>& types, |
| 273 | const DeclarationDatabase& database) { |
| 274 | bool error = false; |
Josh Gao | 958f3b3 | 2016-06-03 13:44:00 -0700 | [diff] [blame^] | 275 | std::string cwd = getWorkingDir() + "/"; |
| 276 | |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 277 | for (auto outer : database) { |
| 278 | const std::string& symbol_name = outer.first; |
| 279 | CompilationType last_type; |
| 280 | DeclarationAvailability last_availability; |
| 281 | |
| 282 | // Rely on std::set being sorted to loop through the types by architecture. |
| 283 | for (const CompilationType& type : types) { |
| 284 | auto inner = outer.second.find(type); |
| 285 | if (inner == outer.second.end()) { |
| 286 | // TODO: Check for holes. |
| 287 | continue; |
| 288 | } |
| 289 | |
| 290 | const Declaration& declaration = inner->second; |
| 291 | bool found_availability = false; |
| 292 | bool availability_mismatch = false; |
| 293 | DeclarationAvailability current_availability; |
| 294 | |
Josh Gao | 958f3b3 | 2016-06-03 13:44:00 -0700 | [diff] [blame^] | 295 | // Ensure that all of the availability declarations for this symbol match. |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 296 | for (const DeclarationLocation& location : declaration.locations) { |
| 297 | if (!found_availability) { |
| 298 | found_availability = true; |
| 299 | current_availability = location.availability; |
| 300 | continue; |
| 301 | } |
| 302 | |
| 303 | if (current_availability != location.availability) { |
| 304 | availability_mismatch = true; |
| 305 | error = true; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | if (availability_mismatch) { |
| 310 | printf("%s: availability mismatch for %s\n", symbol_name.c_str(), type.describe().c_str()); |
Josh Gao | 958f3b3 | 2016-06-03 13:44:00 -0700 | [diff] [blame^] | 311 | declaration.dump(cwd); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | if (type.arch != last_type.arch) { |
| 315 | last_type = type; |
| 316 | last_availability = current_availability; |
| 317 | continue; |
| 318 | } |
| 319 | |
Josh Gao | 958f3b3 | 2016-06-03 13:44:00 -0700 | [diff] [blame^] | 320 | // Ensure that availability declarations are consistent across API levels for a given arch. |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 321 | if (last_availability != current_availability) { |
| 322 | error = true; |
Josh Gao | 173e7c0 | 2016-06-03 13:38:00 -0700 | [diff] [blame] | 323 | printf("%s: availability mismatch between %s and %s: [%s] before, [%s] after\n", |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 324 | symbol_name.c_str(), last_type.describe().c_str(), type.describe().c_str(), |
| 325 | last_availability.describe().c_str(), current_availability.describe().c_str()); |
| 326 | } |
| 327 | |
Josh Gao | 958f3b3 | 2016-06-03 13:44:00 -0700 | [diff] [blame^] | 328 | // Ensure that at most one inline definition of a function exists. |
| 329 | std::set<DeclarationLocation> inline_definitions; |
| 330 | |
| 331 | for (const DeclarationLocation& location : declaration.locations) { |
| 332 | if (location.is_definition) { |
| 333 | inline_definitions.insert(location); |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | if (inline_definitions.size() > 1) { |
| 338 | error = true; |
| 339 | printf("%s: multiple inline definitions found:\n", symbol_name.c_str()); |
| 340 | for (const DeclarationLocation& location : declaration.locations) { |
| 341 | location.dump(cwd); |
| 342 | } |
| 343 | } |
| 344 | |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 345 | last_type = type; |
| 346 | } |
| 347 | } |
| 348 | return !error; |
| 349 | } |
| 350 | |
| 351 | // Check that our symbol availability declarations match the actual NDK |
| 352 | // platform symbol availability. |
| 353 | static bool checkVersions(const std::set<CompilationType>& types, |
| 354 | const DeclarationDatabase& declaration_database, |
| 355 | const NdkSymbolDatabase& symbol_database) { |
| 356 | bool failed = false; |
| 357 | |
| 358 | std::map<std::string, std::set<CompilationType>> arch_types; |
| 359 | for (const CompilationType& type : types) { |
| 360 | arch_types[type.arch].insert(type); |
| 361 | } |
| 362 | |
Josh Gao | d67dbf0 | 2016-06-02 15:21:14 -0700 | [diff] [blame] | 363 | std::set<std::string> completely_unavailable; |
| 364 | |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 365 | for (const auto& outer : declaration_database) { |
| 366 | const std::string& symbol_name = outer.first; |
| 367 | const auto& compilations = outer.second; |
| 368 | |
| 369 | auto platform_availability_it = symbol_database.find(symbol_name); |
| 370 | if (platform_availability_it == symbol_database.end()) { |
Josh Gao | d67dbf0 | 2016-06-02 15:21:14 -0700 | [diff] [blame] | 371 | completely_unavailable.insert(symbol_name); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 372 | continue; |
| 373 | } |
| 374 | |
| 375 | const auto& platform_availability = platform_availability_it->second; |
| 376 | std::set<CompilationType> missing_symbol; |
| 377 | std::set<CompilationType> missing_decl; |
| 378 | |
| 379 | for (const CompilationType& type : types) { |
| 380 | auto it = compilations.find(type); |
| 381 | if (it == compilations.end()) { |
| 382 | missing_decl.insert(type); |
| 383 | continue; |
| 384 | } |
| 385 | |
| 386 | const Declaration& declaration = it->second; |
| 387 | |
| 388 | // sanityCheck ensured that the availability declarations for a given arch match. |
| 389 | DeclarationAvailability availability = declaration.locations.begin()->availability; |
| 390 | int api_level = type.api_level; |
| 391 | |
| 392 | int introduced = std::max(0, availability.introduced); |
| 393 | int obsoleted = availability.obsoleted == 0 ? INT_MAX : availability.obsoleted; |
| 394 | bool decl_available = api_level >= introduced && api_level < obsoleted; |
| 395 | |
| 396 | auto symbol_availability_it = platform_availability.find(type); |
| 397 | bool symbol_available = symbol_availability_it != platform_availability.end(); |
| 398 | if (decl_available) { |
| 399 | if (!symbol_available) { |
Josh Gao | 958f3b3 | 2016-06-03 13:44:00 -0700 | [diff] [blame^] | 400 | // Ensure that either it exists in the platform, or an inline definition is visible. |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 401 | if (!declaration.hasDefinition()) { |
| 402 | missing_symbol.insert(type); |
| 403 | continue; |
| 404 | } |
| 405 | } else { |
Josh Gao | 958f3b3 | 2016-06-03 13:44:00 -0700 | [diff] [blame^] | 406 | // Ensure that symbols declared as functions/variables actually are. |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 407 | switch (declaration.type()) { |
| 408 | case DeclarationType::inconsistent: |
| 409 | printf("%s: inconsistent declaration type\n", symbol_name.c_str()); |
| 410 | declaration.dump(); |
| 411 | exit(1); |
| 412 | |
| 413 | case DeclarationType::variable: |
| 414 | if (symbol_availability_it->second != NdkSymbolType::variable) { |
| 415 | printf("%s: declared as variable, exists in platform as function\n", |
| 416 | symbol_name.c_str()); |
| 417 | failed = true; |
| 418 | } |
| 419 | break; |
| 420 | |
| 421 | case DeclarationType::function: |
| 422 | if (symbol_availability_it->second != NdkSymbolType::function) { |
| 423 | printf("%s: declared as function, exists in platform as variable\n", |
| 424 | symbol_name.c_str()); |
| 425 | failed = true; |
| 426 | } |
| 427 | break; |
| 428 | } |
| 429 | } |
| 430 | } else { |
Josh Gao | 958f3b3 | 2016-06-03 13:44:00 -0700 | [diff] [blame^] | 431 | // Ensure that it's not available in the platform. |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 432 | if (symbol_availability_it != platform_availability.end()) { |
| 433 | printf("%s: symbol should be unavailable in %s (declared with availability %s)\n", |
| 434 | symbol_name.c_str(), type.describe().c_str(), availability.describe().c_str()); |
| 435 | failed = true; |
| 436 | } |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | // Allow declarations to be missing from an entire architecture. |
| 441 | for (const auto& arch_type : arch_types) { |
| 442 | const std::string& arch = arch_type.first; |
| 443 | bool found_all = true; |
| 444 | for (const auto& type : arch_type.second) { |
| 445 | if (missing_decl.find(type) == missing_decl.end()) { |
| 446 | found_all = false; |
| 447 | break; |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | if (!found_all) { |
| 452 | continue; |
| 453 | } |
| 454 | |
| 455 | for (auto it = missing_decl.begin(); it != missing_decl.end();) { |
| 456 | if (it->arch == arch) { |
| 457 | it = missing_decl.erase(it); |
| 458 | } else { |
| 459 | ++it; |
| 460 | } |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | auto types_to_string = [](const std::set<CompilationType>& types) { |
| 465 | std::string result; |
| 466 | for (const CompilationType& type : types) { |
| 467 | result += type.describe(); |
| 468 | result += ", "; |
| 469 | } |
| 470 | result.resize(result.length() - 2); |
| 471 | return result; |
| 472 | }; |
| 473 | |
| 474 | if (!missing_decl.empty()) { |
| 475 | printf("%s: declaration missing in %s\n", symbol_name.c_str(), |
| 476 | types_to_string(missing_decl).c_str()); |
| 477 | failed = true; |
| 478 | } |
| 479 | |
| 480 | if (!missing_symbol.empty()) { |
| 481 | printf("%s: declaration marked available but symbol missing in [%s]\n", symbol_name.c_str(), |
| 482 | types_to_string(missing_symbol).c_str()); |
| 483 | failed = true; |
| 484 | } |
| 485 | } |
| 486 | |
Josh Gao | d67dbf0 | 2016-06-02 15:21:14 -0700 | [diff] [blame] | 487 | for (const std::string& symbol_name : completely_unavailable) { |
| 488 | // This currently has some false positives (mostly functions that come from crtbegin). |
| 489 | // Therefore, only report these declarations when running with verbose for now. |
| 490 | if (!verbose) { |
| 491 | break; |
| 492 | } |
| 493 | |
Josh Gao | 958f3b3 | 2016-06-03 13:44:00 -0700 | [diff] [blame^] | 494 | bool found_inline_definition = false; |
| 495 | bool future = false; |
| 496 | |
Josh Gao | d67dbf0 | 2016-06-02 15:21:14 -0700 | [diff] [blame] | 497 | auto symbol_it = declaration_database.find(symbol_name); |
Josh Gao | 958f3b3 | 2016-06-03 13:44:00 -0700 | [diff] [blame^] | 498 | |
| 499 | // Ignore inline functions and functions that are tagged as __INTRODUCED_IN_FUTURE. |
| 500 | // Ensure that all of the declarations of that function satisfy that. |
| 501 | for (const auto& declaration_pair : symbol_it->second) { |
| 502 | const Declaration& declaration = declaration_pair.second; |
| 503 | DeclarationAvailability availability = declaration.locations.begin()->availability; |
| 504 | |
| 505 | if (availability.introduced >= 10000) { |
| 506 | future = true; |
| 507 | } |
| 508 | |
| 509 | if (declaration.hasDefinition()) { |
| 510 | found_inline_definition = true; |
| 511 | } |
| 512 | } |
| 513 | |
| 514 | if (future || found_inline_definition) { |
Josh Gao | d67dbf0 | 2016-06-02 15:21:14 -0700 | [diff] [blame] | 515 | continue; |
| 516 | } |
| 517 | |
| 518 | printf("%s: not available in any platform\n", symbol_name.c_str()); |
| 519 | failed = true; |
| 520 | } |
| 521 | |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 522 | return !failed; |
| 523 | } |
| 524 | |
Josh Gao | 62aaf8f | 2016-06-02 14:27:21 -0700 | [diff] [blame] | 525 | static void usage(bool help = false) { |
| 526 | fprintf(stderr, "Usage: versioner [OPTION]... [HEADER_PATH] [DEPS_PATH]\n"); |
| 527 | if (!help) { |
| 528 | printf("Try 'versioner -h' for more information.\n"); |
| 529 | exit(1); |
| 530 | } else { |
| 531 | 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] | 532 | 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] | 533 | fprintf(stderr, "\n"); |
| 534 | fprintf(stderr, "Target specification (defaults to all):\n"); |
| 535 | fprintf(stderr, " -a API_LEVEL\tbuild with specified API level (can be repeated)\n"); |
| 536 | fprintf(stderr, " \t\tvalid levels are %s\n", Join(supported_levels).c_str()); |
| 537 | fprintf(stderr, " -r ARCH\tbuild with specified architecture (can be repeated)\n"); |
| 538 | fprintf(stderr, " \t\tvalid architectures are %s\n", Join(supported_archs).c_str()); |
| 539 | fprintf(stderr, "\n"); |
| 540 | fprintf(stderr, "Validation:\n"); |
| 541 | fprintf(stderr, " -p PATH\tcompare against NDK platform at PATH\n"); |
| 542 | fprintf(stderr, " -v\t\tenable verbose warnings\n"); |
| 543 | fprintf(stderr, "\n"); |
| 544 | fprintf(stderr, "Miscellaneous:\n"); |
| 545 | fprintf(stderr, " -h\t\tdisplay this message\n"); |
| 546 | exit(0); |
| 547 | } |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 548 | } |
| 549 | |
| 550 | int main(int argc, char** argv) { |
| 551 | std::string cwd = getWorkingDir() + "/"; |
| 552 | bool default_args = true; |
| 553 | std::string platform_dir; |
| 554 | std::set<std::string> selected_architectures; |
| 555 | std::set<int> selected_levels; |
| 556 | |
| 557 | int c; |
Josh Gao | 62aaf8f | 2016-06-02 14:27:21 -0700 | [diff] [blame] | 558 | while ((c = getopt(argc, argv, "a:r:p:vh")) != -1) { |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 559 | default_args = false; |
| 560 | switch (c) { |
| 561 | case 'a': { |
| 562 | char* end; |
| 563 | int api_level = strtol(optarg, &end, 10); |
| 564 | if (end == optarg || strlen(end) > 0) { |
| 565 | usage(); |
| 566 | } |
| 567 | |
| 568 | if (supported_levels.count(api_level) == 0) { |
| 569 | errx(1, "unsupported API level %d", api_level); |
| 570 | } |
| 571 | |
| 572 | selected_levels.insert(api_level); |
| 573 | break; |
| 574 | } |
| 575 | |
| 576 | case 'r': { |
| 577 | if (supported_archs.count(optarg) == 0) { |
| 578 | errx(1, "unsupported architecture: %s", optarg); |
| 579 | } |
| 580 | selected_architectures.insert(optarg); |
| 581 | break; |
| 582 | } |
| 583 | |
| 584 | case 'p': { |
| 585 | if (!platform_dir.empty()) { |
| 586 | usage(); |
| 587 | } |
| 588 | |
| 589 | platform_dir = optarg; |
| 590 | |
| 591 | struct stat st; |
| 592 | if (stat(platform_dir.c_str(), &st) != 0) { |
| 593 | err(1, "failed to stat platform directory '%s'", platform_dir.c_str()); |
| 594 | } |
| 595 | if (!S_ISDIR(st.st_mode)) { |
| 596 | errx(1, "'%s' is not a directory", optarg); |
| 597 | } |
| 598 | break; |
| 599 | } |
| 600 | |
| 601 | case 'v': |
| 602 | verbose = true; |
| 603 | break; |
| 604 | |
Josh Gao | 62aaf8f | 2016-06-02 14:27:21 -0700 | [diff] [blame] | 605 | case 'h': |
| 606 | usage(true); |
| 607 | break; |
| 608 | |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 609 | default: |
| 610 | usage(); |
| 611 | break; |
| 612 | } |
| 613 | } |
| 614 | |
Josh Gao | 9b5af7a | 2016-06-02 14:29:13 -0700 | [diff] [blame] | 615 | if (argc - optind > 2 || optind > argc) { |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 616 | usage(); |
| 617 | } |
| 618 | |
Josh Gao | 9b5af7a | 2016-06-02 14:29:13 -0700 | [diff] [blame] | 619 | std::string header_dir; |
| 620 | std::string dependency_dir; |
| 621 | |
| 622 | if (optind == argc) { |
| 623 | // Neither HEADER_PATH nor DEPS_PATH were specified, so try to figure them out. |
| 624 | const char* top = getenv("ANDROID_BUILD_TOP"); |
| 625 | if (!top) { |
| 626 | fprintf(stderr, "versioner: failed to autodetect bionic paths. Is ANDROID_BUILD_TOP set?\n"); |
| 627 | usage(); |
| 628 | } |
| 629 | |
| 630 | std::string versioner_dir = std::to_string(top) + "/bionic/tools/versioner"; |
| 631 | header_dir = versioner_dir + "/current"; |
| 632 | dependency_dir = versioner_dir + "/dependencies"; |
| 633 | if (platform_dir.empty()) { |
| 634 | platform_dir = versioner_dir + "/platforms"; |
| 635 | } |
| 636 | } else { |
| 637 | header_dir = argv[optind]; |
| 638 | |
| 639 | if (argc - optind == 2) { |
| 640 | dependency_dir = argv[optind + 1]; |
| 641 | } |
| 642 | } |
| 643 | |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 644 | if (selected_levels.empty()) { |
| 645 | selected_levels = supported_levels; |
| 646 | } |
| 647 | |
| 648 | if (selected_architectures.empty()) { |
| 649 | selected_architectures = supported_archs; |
| 650 | } |
| 651 | |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 652 | |
| 653 | struct stat st; |
Josh Gao | 9b5af7a | 2016-06-02 14:29:13 -0700 | [diff] [blame] | 654 | if (stat(header_dir.c_str(), &st) != 0) { |
| 655 | err(1, "failed to stat '%s'", header_dir.c_str()); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 656 | } else if (!S_ISDIR(st.st_mode)) { |
Josh Gao | 9b5af7a | 2016-06-02 14:29:13 -0700 | [diff] [blame] | 657 | errx(1, "'%s' is not a directory", header_dir.c_str()); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | std::set<CompilationType> compilation_types; |
| 661 | DeclarationDatabase declaration_database; |
| 662 | NdkSymbolDatabase symbol_database; |
| 663 | |
| 664 | compilation_types = generateCompilationTypes(selected_architectures, selected_levels); |
| 665 | |
| 666 | // Do this before compiling so that we can early exit if the platforms don't match what we |
| 667 | // expect. |
| 668 | if (!platform_dir.empty()) { |
| 669 | symbol_database = parsePlatforms(compilation_types, platform_dir); |
| 670 | } |
| 671 | |
| 672 | bool failed = false; |
Josh Gao | 9b5af7a | 2016-06-02 14:29:13 -0700 | [diff] [blame] | 673 | declaration_database = compileHeaders(compilation_types, header_dir, dependency_dir, &failed); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 674 | |
| 675 | if (!sanityCheck(compilation_types, declaration_database)) { |
| 676 | printf("versioner: sanity check failed\n"); |
| 677 | failed = true; |
| 678 | } |
| 679 | |
| 680 | if (!platform_dir.empty()) { |
| 681 | if (!checkVersions(compilation_types, declaration_database, symbol_database)) { |
| 682 | printf("versioner: version check failed\n"); |
| 683 | failed = true; |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | return failed; |
| 688 | } |