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