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