blob: b18c3c256b122bb5a00a6ea77ed0980e4f90b2e0 [file] [log] [blame]
Josh Gaobf8a2852016-05-27 11:59:09 -07001/*
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 Gaobfb6bae2016-07-15 17:25:21 -070040#include "Arch.h"
Josh Gaobf8a2852016-05-27 11:59:09 -070041#include "DeclarationDatabase.h"
Josh Gaof8592a32016-07-26 18:58:27 -070042#include "Preprocessor.h"
Josh Gaobf8a2852016-05-27 11:59:09 -070043#include "SymbolDatabase.h"
44#include "Utils.h"
45#include "versioner.h"
46
47using namespace std::string_literals;
48using namespace clang;
49using namespace clang::tooling;
50
51bool verbose;
Josh Gaobfb6bae2016-07-15 17:25:21 -070052static bool add_include;
Josh Gaobf8a2852016-05-27 11:59:09 -070053
54class 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 Gaobfb6bae2016-07-15 17:25:21 -070081 command.push_back("-Wno-pragma-once-outside-header");
Josh Gaobf8a2852016-05-27 11:59:09 -070082 command.push_back("-target");
83 command.push_back(arch_targets[type.arch]);
84
Josh Gaobfb6bae2016-07-15 17:25:21 -070085 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 Gaobf8a2852016-05-27 11:59:09 -070092 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
114struct CompilationRequirements {
115 std::vector<std::string> headers;
116 std::vector<std::string> dependencies;
117};
118
Josh Gaobfb6bae2016-07-15 17:25:21 -0700119static CompilationRequirements collectRequirements(const Arch& arch, const std::string& header_dir,
Josh Gaobf8a2852016-05-27 11:59:09 -0700120 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 Gaobfb6bae2016-07-15 17:25:21 -0700156 collect_children(dependency_dir + "/" + to_string(arch));
Josh Gaobf8a2852016-05-27 11:59:09 -0700157 }
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 Gaobfb6bae2016-07-15 17:25:21 -0700178static std::set<CompilationType> generateCompilationTypes(const std::set<Arch> selected_architectures,
179 const std::set<int>& selected_levels) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700180 std::set<CompilationType> result;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700181 for (const auto& arch : selected_architectures) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700182 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 Gaobfb6bae2016-07-15 17:25:21 -0700194static 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 Gaobf8a2852016-05-27 11:59:09 -0700198 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 Gaobfb6bae2016-07-15 17:25:21 -0700204 std::unordered_map<Arch, CompilationRequirements> requirements;
Josh Gaobf8a2852016-05-27 11:59:09 -0700205
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 Gaobfb6bae2016-07-15 17:25:21 -0700215 auto result = std::make_unique<HeaderDatabase>();
Josh Gaobf8a2852016-05-27 11:59:09 -0700216 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 Gaobf8a2852016-05-27 11:59:09 -0700227 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 Gaobfb6bae2016-07-15 17:25:21 -0700239 printf("versioner: compilation failure for %s in %s\n", to_string(type).c_str(),
Josh Gaobf8a2852016-05-27 11:59:09 -0700240 ast->getOriginalSourceFileName().str().c_str());
241 }
242
Josh Gaobfb6bae2016-07-15 17:25:21 -0700243 result->parseAST(type, ast.get());
Josh Gaobf8a2852016-05-27 11:59:09 -0700244 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700245 },
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 Gaobfb6bae2016-07-15 17:25:21 -0700262 return result;
Josh Gaobf8a2852016-05-27 11:59:09 -0700263}
264
Josh Gaobfb6bae2016-07-15 17:25:21 -0700265// 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))
273static 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 Gao566735d2016-08-02 15:07:32 -0700293 decl->dump(cwd, stderr, 2);
Josh Gaobfb6bae2016-07-15 17:25:21 -0700294 return false;
295 }
296
297 if (decl->is_definition && !availability.empty()) {
298 fprintf(stderr, "versioner: inline definition has non-empty versioning information:\n");
Josh Gao566735d2016-08-02 15:07:32 -0700299 decl->dump(cwd, stderr, 2);
Josh Gaobfb6bae2016-07-15 17:25:21 -0700300 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
315static bool sanityCheck(const HeaderDatabase* database) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700316 bool error = false;
Josh Gao958f3b32016-06-03 13:44:00 -0700317 std::string cwd = getWorkingDir() + "/";
318
Josh Gaobfb6bae2016-07-15 17:25:21 -0700319 for (const auto& symbol_it : database->symbols) {
320 if (!checkSymbol(symbol_it.second)) {
321 error = true;
Josh Gaobf8a2852016-05-27 11:59:09 -0700322 }
323 }
324 return !error;
325}
326
327// Check that our symbol availability declarations match the actual NDK
328// platform symbol availability.
329static bool checkVersions(const std::set<CompilationType>& types,
Josh Gaobfb6bae2016-07-15 17:25:21 -0700330 const HeaderDatabase* header_database,
Josh Gaobf8a2852016-05-27 11:59:09 -0700331 const NdkSymbolDatabase& symbol_database) {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700332 std::string cwd = getWorkingDir() + "/";
Josh Gaobf8a2852016-05-27 11:59:09 -0700333 bool failed = false;
334
Josh Gaobfb6bae2016-07-15 17:25:21 -0700335 std::map<Arch, std::set<CompilationType>> arch_types;
Josh Gaobf8a2852016-05-27 11:59:09 -0700336 for (const CompilationType& type : types) {
337 arch_types[type.arch].insert(type);
338 }
339
Josh Gaod67dbf02016-06-02 15:21:14 -0700340 std::set<std::string> completely_unavailable;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700341 std::map<std::string, std::set<CompilationType>> missing_availability;
342 std::map<std::string, std::set<CompilationType>> extra_availability;
Josh Gaod67dbf02016-06-02 15:21:14 -0700343
Josh Gaobfb6bae2016-07-15 17:25:21 -0700344 for (const auto& symbol_it : header_database->symbols) {
345 const auto& symbol_name = symbol_it.first;
346 DeclarationAvailability symbol_availability;
Josh Gaobf8a2852016-05-27 11:59:09 -0700347
Josh Gaobfb6bae2016-07-15 17:25:21 -0700348 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 Gaobf8a2852016-05-27 11:59:09 -0700353 if (platform_availability_it == symbol_database.end()) {
Josh Gaod67dbf02016-06-02 15:21:14 -0700354 completely_unavailable.insert(symbol_name);
Josh Gaobf8a2852016-05-27 11:59:09 -0700355 continue;
356 }
357
358 const auto& platform_availability = platform_availability_it->second;
Josh Gaobf8a2852016-05-27 11:59:09 -0700359
360 for (const CompilationType& type : types) {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700361 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 Gaobf8a2852016-05-27 11:59:09 -0700381 continue;
382 }
383
Josh Gaobfb6bae2016-07-15 17:25:21 -0700384 // 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 Gaobf8a2852016-05-27 11:59:09 -0700388
Josh Gaobfb6bae2016-07-15 17:25:21 -0700389 bool is_available = platform_availability.count(type);
Josh Gaobf8a2852016-05-27 11:59:09 -0700390
Josh Gaobfb6bae2016-07-15 17:25:21 -0700391 if (should_be_available != is_available) {
392 if (is_available) {
393 extra_availability[symbol_name].insert(type);
Josh Gaobf8a2852016-05-27 11:59:09 -0700394 } else {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700395 missing_availability[symbol_name].insert(type);
Josh Gaobf8a2852016-05-27 11:59:09 -0700396 }
397 }
398 }
Josh Gaobfb6bae2016-07-15 17:25:21 -0700399 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700400
Josh Gaobfb6bae2016-07-15 17:25:21 -0700401 for (const auto& it : symbol_database) {
402 const std::string& symbol_name = it.first;
Josh Gaobf8a2852016-05-27 11:59:09 -0700403
Josh Gaobfb6bae2016-07-15 17:25:21 -0700404 bool symbol_error = false;
405 auto missing_it = missing_availability.find(symbol_name);
406 if (missing_it != missing_availability.end()) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700407 printf("%s: declaration marked available but symbol missing in [%s]\n", symbol_name.c_str(),
Josh Gaobfb6bae2016-07-15 17:25:21 -0700408 Join(missing_it->second, ", ").c_str());
409 symbol_error = true;
Josh Gaobf8a2852016-05-27 11:59:09 -0700410 failed = true;
411 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700412
Josh Gaobfb6bae2016-07-15 17:25:21 -0700413 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 Gao958f3b32016-06-03 13:44:00 -0700420 }
421 }
422
Josh Gaobfb6bae2016-07-15 17:25:21 -0700423 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 Gaod67dbf02016-06-02 15:21:14 -0700429 }
Josh Gaod67dbf02016-06-02 15:21:14 -0700430 }
431
Josh Gaobfb6bae2016-07-15 17:25:21 -0700432 // TODO: Verify that function/variable declarations are actually function/variable symbols.
Josh Gaobf8a2852016-05-27 11:59:09 -0700433 return !failed;
434}
435
Josh Gao62aaf8f2016-06-02 14:27:21 -0700436static 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 Gao9b5af7a2016-06-02 14:29:13 -0700443 fprintf(stderr, "Autodetects paths if HEADER_PATH and DEPS_PATH are not specified\n");
Josh Gao62aaf8f2016-06-02 14:27:21 -0700444 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 Gaof8592a32016-07-26 18:58:27 -0700455 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 Gao62aaf8f2016-06-02 14:27:21 -0700459 fprintf(stderr, "Miscellaneous:\n");
Josh Gaobfb6bae2016-07-15 17:25:21 -0700460 fprintf(stderr, " -d\t\tdump function availability\n");
Josh Gao62aaf8f2016-06-02 14:27:21 -0700461 fprintf(stderr, " -h\t\tdisplay this message\n");
462 exit(0);
463 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700464}
465
466int main(int argc, char** argv) {
467 std::string cwd = getWorkingDir() + "/";
468 bool default_args = true;
469 std::string platform_dir;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700470 std::set<Arch> selected_architectures;
Josh Gaobf8a2852016-05-27 11:59:09 -0700471 std::set<int> selected_levels;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700472 bool dump = false;
Josh Gaof8592a32016-07-26 18:58:27 -0700473 std::string preprocessor_output_path;
474 bool force = false;
Josh Gaobf8a2852016-05-27 11:59:09 -0700475
476 int c;
Josh Gaof8592a32016-07-26 18:58:27 -0700477 while ((c = getopt(argc, argv, "a:r:p:vo:fdhi")) != -1) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700478 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 Gaobfb6bae2016-07-15 17:25:21 -0700496 Arch arch = arch_from_string(optarg);
497 selected_architectures.insert(arch);
Josh Gaobf8a2852016-05-27 11:59:09 -0700498 break;
499 }
500
501 case 'p': {
502 if (!platform_dir.empty()) {
503 usage();
504 }
505
506 platform_dir = optarg;
507
Josh Gaof8592a32016-07-26 18:58:27 -0700508 if (platform_dir.empty()) {
509 usage();
510 }
511
Josh Gaobf8a2852016-05-27 11:59:09 -0700512 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 Gaof8592a32016-07-26 18:58:27 -0700526 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 Gaobfb6bae2016-07-15 17:25:21 -0700540 case 'd':
541 dump = true;
542 break;
543
Josh Gao62aaf8f2016-06-02 14:27:21 -0700544 case 'h':
545 usage(true);
546 break;
547
Josh Gaobfb6bae2016-07-15 17:25:21 -0700548 case 'i':
549 // Secret option for tests to -include <android/versioning.h>.
550 add_include = true;
551 break;
552
Josh Gaobf8a2852016-05-27 11:59:09 -0700553 default:
554 usage();
555 break;
556 }
557 }
558
Josh Gao9b5af7a2016-06-02 14:29:13 -0700559 if (argc - optind > 2 || optind > argc) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700560 usage();
561 }
562
Josh Gao9b5af7a2016-06-02 14:29:13 -0700563 std::string header_dir;
564 std::string dependency_dir;
565
Josh Gaobfb6bae2016-07-15 17:25:21 -0700566 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 Gao9b5af7a2016-06-02 14:29:13 -0700572 if (optind == argc) {
573 // Neither HEADER_PATH nor DEPS_PATH were specified, so try to figure them out.
Josh Gaobfb6bae2016-07-15 17:25:21 -0700574 std::string versioner_dir = to_string(top) + "/bionic/tools/versioner";
Josh Gao9b5af7a2016-06-02 14:29:13 -0700575 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 Gaof8592a32016-07-26 18:58:27 -0700581 // Intentional leak.
582 header_dir = realpath(argv[optind], nullptr);
Josh Gao9b5af7a2016-06-02 14:29:13 -0700583
584 if (argc - optind == 2) {
585 dependency_dir = argv[optind + 1];
586 }
587 }
588
Josh Gaobf8a2852016-05-27 11:59:09 -0700589 if (selected_levels.empty()) {
590 selected_levels = supported_levels;
591 }
592
593 if (selected_architectures.empty()) {
594 selected_architectures = supported_archs;
595 }
596
Josh Gaobf8a2852016-05-27 11:59:09 -0700597
598 struct stat st;
Josh Gao9b5af7a2016-06-02 14:29:13 -0700599 if (stat(header_dir.c_str(), &st) != 0) {
600 err(1, "failed to stat '%s'", header_dir.c_str());
Josh Gaobf8a2852016-05-27 11:59:09 -0700601 } else if (!S_ISDIR(st.st_mode)) {
Josh Gao9b5af7a2016-06-02 14:29:13 -0700602 errx(1, "'%s' is not a directory", header_dir.c_str());
Josh Gaobf8a2852016-05-27 11:59:09 -0700603 }
604
605 std::set<CompilationType> compilation_types;
Josh Gaobf8a2852016-05-27 11:59:09 -0700606 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 Gaobfb6bae2016-07-15 17:25:21 -0700617 std::unique_ptr<HeaderDatabase> declaration_database =
618 compileHeaders(compilation_types, header_dir, dependency_dir, &failed);
Josh Gaobf8a2852016-05-27 11:59:09 -0700619
Josh Gaobfb6bae2016-07-15 17:25:21 -0700620 if (dump) {
621 declaration_database->dump(header_dir + "/");
622 } else {
623 if (!sanityCheck(declaration_database.get())) {
624 printf("versioner: sanity check failed\n");
Josh Gaobf8a2852016-05-27 11:59:09 -0700625 failed = true;
626 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700627
Josh Gaobfb6bae2016-07-15 17:25:21 -0700628 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 Gaof8592a32016-07-26 18:58:27 -0700635
636 if (!preprocessor_output_path.empty() && (force || !failed)) {
637 failed = !preprocessHeaders(preprocessor_output_path, header_dir, declaration_database.get());
638 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700639 return failed;
640}