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