blob: 1b1710911c10c9e1c31e48537abab889e804f3b6 [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"
42#include "SymbolDatabase.h"
43#include "Utils.h"
44#include "versioner.h"
45
46using namespace std::string_literals;
47using namespace clang;
48using namespace clang::tooling;
49
50bool verbose;
Josh Gaobfb6bae2016-07-15 17:25:21 -070051static bool add_include;
Josh Gaobf8a2852016-05-27 11:59:09 -070052
53class HeaderCompilationDatabase : public CompilationDatabase {
54 CompilationType type;
55 std::string cwd;
56 std::vector<std::string> headers;
57 std::vector<std::string> include_dirs;
58
59 public:
60 HeaderCompilationDatabase(CompilationType type, std::string cwd, std::vector<std::string> headers,
61 std::vector<std::string> include_dirs)
62 : type(type),
63 cwd(std::move(cwd)),
64 headers(std::move(headers)),
65 include_dirs(std::move(include_dirs)) {
66 }
67
68 CompileCommand generateCompileCommand(const std::string& filename) const {
69 std::vector<std::string> command = { "clang-tool", filename, "-nostdlibinc" };
70 for (const auto& dir : include_dirs) {
71 command.push_back("-isystem");
72 command.push_back(dir);
73 }
74 command.push_back("-std=c11");
75 command.push_back("-DANDROID");
76 command.push_back("-D__ANDROID_API__="s + std::to_string(type.api_level));
77 command.push_back("-D_FORTIFY_SOURCE=2");
78 command.push_back("-D_GNU_SOURCE");
79 command.push_back("-Wno-unknown-attributes");
Josh Gaobfb6bae2016-07-15 17:25:21 -070080 command.push_back("-Wno-pragma-once-outside-header");
Josh Gaobf8a2852016-05-27 11:59:09 -070081 command.push_back("-target");
82 command.push_back(arch_targets[type.arch]);
83
Josh Gaobfb6bae2016-07-15 17:25:21 -070084 if (add_include) {
85 const char* top = getenv("ANDROID_BUILD_TOP");
86 std::string header_path = to_string(top) + "/bionic/libc/include/android/versioning.h";
87 command.push_back("-include");
88 command.push_back(std::move(header_path));
89 }
90
Josh Gaobf8a2852016-05-27 11:59:09 -070091 return CompileCommand(cwd, filename, command);
92 }
93
94 std::vector<CompileCommand> getAllCompileCommands() const override {
95 std::vector<CompileCommand> commands;
96 for (const std::string& file : headers) {
97 commands.push_back(generateCompileCommand(file));
98 }
99 return commands;
100 }
101
102 std::vector<CompileCommand> getCompileCommands(StringRef file) const override {
103 std::vector<CompileCommand> commands;
104 commands.push_back(generateCompileCommand(file));
105 return commands;
106 }
107
108 std::vector<std::string> getAllFiles() const override {
109 return headers;
110 }
111};
112
113struct CompilationRequirements {
114 std::vector<std::string> headers;
115 std::vector<std::string> dependencies;
116};
117
Josh Gaobfb6bae2016-07-15 17:25:21 -0700118static CompilationRequirements collectRequirements(const Arch& arch, const std::string& header_dir,
Josh Gaobf8a2852016-05-27 11:59:09 -0700119 const std::string& dependency_dir) {
120 std::vector<std::string> headers = collectFiles(header_dir);
121
122 std::vector<std::string> dependencies = { header_dir };
123 if (!dependency_dir.empty()) {
124 auto collect_children = [&dependencies](const std::string& dir_path) {
125 DIR* dir = opendir(dir_path.c_str());
126 if (!dir) {
127 err(1, "failed to open dependency directory '%s'", dir_path.c_str());
128 }
129
130 struct dirent* dent;
131 while ((dent = readdir(dir))) {
132 if (dent->d_name[0] == '.') {
133 continue;
134 }
135
136 // TODO: Resolve symlinks.
137 std::string dependency = dir_path + "/" + dent->d_name;
138
139 struct stat st;
140 if (stat(dependency.c_str(), &st) != 0) {
141 err(1, "failed to stat dependency '%s'", dependency.c_str());
142 }
143
144 if (!S_ISDIR(st.st_mode)) {
145 errx(1, "'%s' is not a directory", dependency.c_str());
146 }
147
148 dependencies.push_back(dependency);
149 }
150
151 closedir(dir);
152 };
153
154 collect_children(dependency_dir + "/common");
Josh Gaobfb6bae2016-07-15 17:25:21 -0700155 collect_children(dependency_dir + "/" + to_string(arch));
Josh Gaobf8a2852016-05-27 11:59:09 -0700156 }
157
158 auto new_end = std::remove_if(headers.begin(), headers.end(), [&arch](llvm::StringRef header) {
159 for (const auto& it : header_blacklist) {
160 if (it.second.find(arch) == it.second.end()) {
161 continue;
162 }
163
164 if (header.endswith("/" + it.first)) {
165 return true;
166 }
167 }
168 return false;
169 });
170
171 headers.erase(new_end, headers.end());
172
173 CompilationRequirements result = { .headers = headers, .dependencies = dependencies };
174 return result;
175}
176
Josh Gaobfb6bae2016-07-15 17:25:21 -0700177static std::set<CompilationType> generateCompilationTypes(const std::set<Arch> selected_architectures,
178 const std::set<int>& selected_levels) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700179 std::set<CompilationType> result;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700180 for (const auto& arch : selected_architectures) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700181 int min_api = arch_min_api[arch];
182 for (int api_level : selected_levels) {
183 if (api_level < min_api) {
184 continue;
185 }
186 CompilationType type = { .arch = arch, .api_level = api_level };
187 result.insert(type);
188 }
189 }
190 return result;
191}
192
Josh Gaobfb6bae2016-07-15 17:25:21 -0700193static std::unique_ptr<HeaderDatabase> compileHeaders(const std::set<CompilationType>& types,
194 const std::string& header_dir,
195 const std::string& dependency_dir,
196 bool* failed) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700197 constexpr size_t thread_count = 8;
198 size_t threads_created = 0;
199 std::mutex mutex;
200 std::vector<std::thread> threads(thread_count);
201
202 std::map<CompilationType, HeaderDatabase> header_databases;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700203 std::unordered_map<Arch, CompilationRequirements> requirements;
Josh Gaobf8a2852016-05-27 11:59:09 -0700204
205 std::string cwd = getWorkingDir();
206 bool errors = false;
207
208 for (const auto& type : types) {
209 if (requirements.count(type.arch) == 0) {
210 requirements[type.arch] = collectRequirements(type.arch, header_dir, dependency_dir);
211 }
212 }
213
Josh Gaobfb6bae2016-07-15 17:25:21 -0700214 auto result = std::make_unique<HeaderDatabase>();
Josh Gaobf8a2852016-05-27 11:59:09 -0700215 for (const auto& type : types) {
216 size_t thread_id = threads_created++;
217 if (thread_id >= thread_count) {
218 thread_id = thread_id % thread_count;
219 threads[thread_id].join();
220 }
221
222 threads[thread_id] = std::thread(
223 [&](CompilationType type) {
224 const auto& req = requirements[type.arch];
225
Josh Gaobf8a2852016-05-27 11:59:09 -0700226 HeaderCompilationDatabase compilation_database(type, cwd, req.headers, req.dependencies);
227
228 ClangTool tool(compilation_database, req.headers);
229
230 clang::DiagnosticOptions diagnostic_options;
231 std::vector<std::unique_ptr<ASTUnit>> asts;
232 tool.buildASTs(asts);
233 for (const auto& ast : asts) {
234 clang::DiagnosticsEngine& diagnostics_engine = ast->getDiagnostics();
235 if (diagnostics_engine.getNumWarnings() || diagnostics_engine.hasErrorOccurred()) {
236 std::unique_lock<std::mutex> l(mutex);
237 errors = true;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700238 printf("versioner: compilation failure for %s in %s\n", to_string(type).c_str(),
Josh Gaobf8a2852016-05-27 11:59:09 -0700239 ast->getOriginalSourceFileName().str().c_str());
240 }
241
Josh Gaobfb6bae2016-07-15 17:25:21 -0700242 result->parseAST(type, ast.get());
Josh Gaobf8a2852016-05-27 11:59:09 -0700243 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700244 },
245 type);
246 }
247
248 if (threads_created < thread_count) {
249 threads.resize(threads_created);
250 }
251
252 for (auto& thread : threads) {
253 thread.join();
254 }
255
256 if (errors) {
257 printf("versioner: compilation generated warnings or errors\n");
258 *failed = errors;
259 }
260
Josh Gaobfb6bae2016-07-15 17:25:21 -0700261 return result;
Josh Gaobf8a2852016-05-27 11:59:09 -0700262}
263
Josh Gaobfb6bae2016-07-15 17:25:21 -0700264// Perform a sanity check on a symbol's declarations, enforcing the following invariants:
265// 1. At most one inline definition of the function exists.
266// 2. All of the availability declarations for a symbol are compatible.
267// If a function is declared as an inline before a certain version, the inline definition
268// should have no version tag.
269// 3. Each availability type must only be present globally or on a per-arch basis.
270// (e.g. __INTRODUCED_IN_ARM(9) __INTRODUCED_IN_X86(10) __DEPRECATED_IN(11) is fine,
271// but not __INTRODUCED_IN(9) __INTRODUCED_IN_X86(10))
272static bool checkSymbol(const Symbol& symbol) {
273 std::string cwd = getWorkingDir() + "/";
274
275 const Declaration* inline_definition = nullptr;
276 for (const auto& decl_it : symbol.declarations) {
277 const Declaration* decl = &decl_it.second;
278 if (decl->is_definition) {
279 if (inline_definition) {
280 fprintf(stderr, "versioner: multiple definitions of symbol %s\n", symbol.name.c_str());
281 symbol.dump(cwd);
282 inline_definition->dump(cwd);
283 return false;
284 }
285
286 inline_definition = decl;
287 }
288
289 DeclarationAvailability availability;
290 if (!decl->calculateAvailability(&availability)) {
291 fprintf(stderr, "versioner: failed to calculate availability for declaration:\n");
Josh Gao566735d2016-08-02 15:07:32 -0700292 decl->dump(cwd, stderr, 2);
Josh Gaobfb6bae2016-07-15 17:25:21 -0700293 return false;
294 }
295
296 if (decl->is_definition && !availability.empty()) {
297 fprintf(stderr, "versioner: inline definition has non-empty versioning information:\n");
Josh Gao566735d2016-08-02 15:07:32 -0700298 decl->dump(cwd, stderr, 2);
Josh Gaobfb6bae2016-07-15 17:25:21 -0700299 return false;
300 }
301 }
302
303 DeclarationAvailability availability;
304 if (!symbol.calculateAvailability(&availability)) {
305 fprintf(stderr, "versioner: inconsistent availability for symbol '%s'\n", symbol.name.c_str());
306 symbol.dump(cwd);
307 return false;
308 }
309
310 // TODO: Check invariant #3.
311 return true;
312}
313
314static bool sanityCheck(const HeaderDatabase* database) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700315 bool error = false;
Josh Gao958f3b32016-06-03 13:44:00 -0700316 std::string cwd = getWorkingDir() + "/";
317
Josh Gaobfb6bae2016-07-15 17:25:21 -0700318 for (const auto& symbol_it : database->symbols) {
319 if (!checkSymbol(symbol_it.second)) {
320 error = true;
Josh Gaobf8a2852016-05-27 11:59:09 -0700321 }
322 }
323 return !error;
324}
325
326// Check that our symbol availability declarations match the actual NDK
327// platform symbol availability.
328static bool checkVersions(const std::set<CompilationType>& types,
Josh Gaobfb6bae2016-07-15 17:25:21 -0700329 const HeaderDatabase* header_database,
Josh Gaobf8a2852016-05-27 11:59:09 -0700330 const NdkSymbolDatabase& symbol_database) {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700331 std::string cwd = getWorkingDir() + "/";
Josh Gaobf8a2852016-05-27 11:59:09 -0700332 bool failed = false;
333
Josh Gaobfb6bae2016-07-15 17:25:21 -0700334 std::map<Arch, std::set<CompilationType>> arch_types;
Josh Gaobf8a2852016-05-27 11:59:09 -0700335 for (const CompilationType& type : types) {
336 arch_types[type.arch].insert(type);
337 }
338
Josh Gaod67dbf02016-06-02 15:21:14 -0700339 std::set<std::string> completely_unavailable;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700340 std::map<std::string, std::set<CompilationType>> missing_availability;
341 std::map<std::string, std::set<CompilationType>> extra_availability;
Josh Gaod67dbf02016-06-02 15:21:14 -0700342
Josh Gaobfb6bae2016-07-15 17:25:21 -0700343 for (const auto& symbol_it : header_database->symbols) {
344 const auto& symbol_name = symbol_it.first;
345 DeclarationAvailability symbol_availability;
Josh Gaobf8a2852016-05-27 11:59:09 -0700346
Josh Gaobfb6bae2016-07-15 17:25:21 -0700347 if (!symbol_it.second.calculateAvailability(&symbol_availability)) {
348 errx(1, "failed to calculate symbol availability");
349 }
350
351 const auto platform_availability_it = symbol_database.find(symbol_name);
Josh Gaobf8a2852016-05-27 11:59:09 -0700352 if (platform_availability_it == symbol_database.end()) {
Josh Gaod67dbf02016-06-02 15:21:14 -0700353 completely_unavailable.insert(symbol_name);
Josh Gaobf8a2852016-05-27 11:59:09 -0700354 continue;
355 }
356
357 const auto& platform_availability = platform_availability_it->second;
Josh Gaobf8a2852016-05-27 11:59:09 -0700358
359 for (const CompilationType& type : types) {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700360 bool should_be_available = true;
361 const auto& global_availability = symbol_availability.global_availability;
362 const auto& arch_availability = symbol_availability.arch_availability[type.arch];
363 if (global_availability.introduced != 0 && global_availability.introduced > type.api_level) {
364 should_be_available = false;
365 }
366
367 if (arch_availability.introduced != 0 && arch_availability.introduced > type.api_level) {
368 should_be_available = false;
369 }
370
371 if (global_availability.obsoleted != 0 && global_availability.obsoleted <= type.api_level) {
372 should_be_available = false;
373 }
374
375 if (arch_availability.obsoleted != 0 && arch_availability.obsoleted <= type.api_level) {
376 should_be_available = false;
377 }
378
379 if (arch_availability.future) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700380 continue;
381 }
382
Josh Gaobfb6bae2016-07-15 17:25:21 -0700383 // The function declaration might be (validly) missing for the given CompilationType.
384 if (!symbol_it.second.hasDeclaration(type)) {
385 should_be_available = false;
386 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700387
Josh Gaobfb6bae2016-07-15 17:25:21 -0700388 bool is_available = platform_availability.count(type);
Josh Gaobf8a2852016-05-27 11:59:09 -0700389
Josh Gaobfb6bae2016-07-15 17:25:21 -0700390 if (should_be_available != is_available) {
391 if (is_available) {
392 extra_availability[symbol_name].insert(type);
Josh Gaobf8a2852016-05-27 11:59:09 -0700393 } else {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700394 missing_availability[symbol_name].insert(type);
Josh Gaobf8a2852016-05-27 11:59:09 -0700395 }
396 }
397 }
Josh Gaobfb6bae2016-07-15 17:25:21 -0700398 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700399
Josh Gaobfb6bae2016-07-15 17:25:21 -0700400 for (const auto& it : symbol_database) {
401 const std::string& symbol_name = it.first;
Josh Gaobf8a2852016-05-27 11:59:09 -0700402
Josh Gaobfb6bae2016-07-15 17:25:21 -0700403 bool symbol_error = false;
404 auto missing_it = missing_availability.find(symbol_name);
405 if (missing_it != missing_availability.end()) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700406 printf("%s: declaration marked available but symbol missing in [%s]\n", symbol_name.c_str(),
Josh Gaobfb6bae2016-07-15 17:25:21 -0700407 Join(missing_it->second, ", ").c_str());
408 symbol_error = true;
Josh Gaobf8a2852016-05-27 11:59:09 -0700409 failed = true;
410 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700411
Josh Gaobfb6bae2016-07-15 17:25:21 -0700412 if (verbose) {
413 auto extra_it = extra_availability.find(symbol_name);
414 if (extra_it != extra_availability.end()) {
415 printf("%s: declaration marked unavailable but symbol available in [%s]\n",
416 symbol_name.c_str(), Join(extra_it->second, ", ").c_str());
417 symbol_error = true;
418 failed = true;
Josh Gao958f3b32016-06-03 13:44:00 -0700419 }
420 }
421
Josh Gaobfb6bae2016-07-15 17:25:21 -0700422 if (symbol_error) {
423 auto symbol_it = header_database->symbols.find(symbol_name);
424 if (symbol_it == header_database->symbols.end()) {
425 errx(1, "failed to find symbol in header database");
426 }
427 symbol_it->second.dump(cwd);
Josh Gaod67dbf02016-06-02 15:21:14 -0700428 }
Josh Gaod67dbf02016-06-02 15:21:14 -0700429 }
430
Josh Gaobfb6bae2016-07-15 17:25:21 -0700431 // TODO: Verify that function/variable declarations are actually function/variable symbols.
Josh Gaobf8a2852016-05-27 11:59:09 -0700432 return !failed;
433}
434
Josh Gao62aaf8f2016-06-02 14:27:21 -0700435static void usage(bool help = false) {
436 fprintf(stderr, "Usage: versioner [OPTION]... [HEADER_PATH] [DEPS_PATH]\n");
437 if (!help) {
438 printf("Try 'versioner -h' for more information.\n");
439 exit(1);
440 } else {
441 fprintf(stderr, "Version headers at HEADER_PATH, with DEPS_PATH/ARCH/* on the include path\n");
Josh Gao9b5af7a2016-06-02 14:29:13 -0700442 fprintf(stderr, "Autodetects paths if HEADER_PATH and DEPS_PATH are not specified\n");
Josh Gao62aaf8f2016-06-02 14:27:21 -0700443 fprintf(stderr, "\n");
444 fprintf(stderr, "Target specification (defaults to all):\n");
445 fprintf(stderr, " -a API_LEVEL\tbuild with specified API level (can be repeated)\n");
446 fprintf(stderr, " \t\tvalid levels are %s\n", Join(supported_levels).c_str());
447 fprintf(stderr, " -r ARCH\tbuild with specified architecture (can be repeated)\n");
448 fprintf(stderr, " \t\tvalid architectures are %s\n", Join(supported_archs).c_str());
449 fprintf(stderr, "\n");
450 fprintf(stderr, "Validation:\n");
451 fprintf(stderr, " -p PATH\tcompare against NDK platform at PATH\n");
452 fprintf(stderr, " -v\t\tenable verbose warnings\n");
453 fprintf(stderr, "\n");
454 fprintf(stderr, "Miscellaneous:\n");
Josh Gaobfb6bae2016-07-15 17:25:21 -0700455 fprintf(stderr, " -d\t\tdump function availability\n");
Josh Gao62aaf8f2016-06-02 14:27:21 -0700456 fprintf(stderr, " -h\t\tdisplay this message\n");
457 exit(0);
458 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700459}
460
461int main(int argc, char** argv) {
462 std::string cwd = getWorkingDir() + "/";
463 bool default_args = true;
464 std::string platform_dir;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700465 std::set<Arch> selected_architectures;
Josh Gaobf8a2852016-05-27 11:59:09 -0700466 std::set<int> selected_levels;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700467 bool dump = false;
Josh Gaobf8a2852016-05-27 11:59:09 -0700468
469 int c;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700470 while ((c = getopt(argc, argv, "a:r:p:vdhi")) != -1) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700471 default_args = false;
472 switch (c) {
473 case 'a': {
474 char* end;
475 int api_level = strtol(optarg, &end, 10);
476 if (end == optarg || strlen(end) > 0) {
477 usage();
478 }
479
480 if (supported_levels.count(api_level) == 0) {
481 errx(1, "unsupported API level %d", api_level);
482 }
483
484 selected_levels.insert(api_level);
485 break;
486 }
487
488 case 'r': {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700489 Arch arch = arch_from_string(optarg);
490 selected_architectures.insert(arch);
Josh Gaobf8a2852016-05-27 11:59:09 -0700491 break;
492 }
493
494 case 'p': {
495 if (!platform_dir.empty()) {
496 usage();
497 }
498
499 platform_dir = optarg;
500
501 struct stat st;
502 if (stat(platform_dir.c_str(), &st) != 0) {
503 err(1, "failed to stat platform directory '%s'", platform_dir.c_str());
504 }
505 if (!S_ISDIR(st.st_mode)) {
506 errx(1, "'%s' is not a directory", optarg);
507 }
508 break;
509 }
510
511 case 'v':
512 verbose = true;
513 break;
514
Josh Gaobfb6bae2016-07-15 17:25:21 -0700515 case 'd':
516 dump = true;
517 break;
518
Josh Gao62aaf8f2016-06-02 14:27:21 -0700519 case 'h':
520 usage(true);
521 break;
522
Josh Gaobfb6bae2016-07-15 17:25:21 -0700523 case 'i':
524 // Secret option for tests to -include <android/versioning.h>.
525 add_include = true;
526 break;
527
Josh Gaobf8a2852016-05-27 11:59:09 -0700528 default:
529 usage();
530 break;
531 }
532 }
533
Josh Gao9b5af7a2016-06-02 14:29:13 -0700534 if (argc - optind > 2 || optind > argc) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700535 usage();
536 }
537
Josh Gao9b5af7a2016-06-02 14:29:13 -0700538 std::string header_dir;
539 std::string dependency_dir;
540
Josh Gaobfb6bae2016-07-15 17:25:21 -0700541 const char* top = getenv("ANDROID_BUILD_TOP");
542 if (!top && (optind == argc || add_include)) {
543 fprintf(stderr, "versioner: failed to autodetect bionic paths. Is ANDROID_BUILD_TOP set?\n");
544 usage();
545 }
546
Josh Gao9b5af7a2016-06-02 14:29:13 -0700547 if (optind == argc) {
548 // Neither HEADER_PATH nor DEPS_PATH were specified, so try to figure them out.
Josh Gaobfb6bae2016-07-15 17:25:21 -0700549 std::string versioner_dir = to_string(top) + "/bionic/tools/versioner";
Josh Gao9b5af7a2016-06-02 14:29:13 -0700550 header_dir = versioner_dir + "/current";
551 dependency_dir = versioner_dir + "/dependencies";
552 if (platform_dir.empty()) {
553 platform_dir = versioner_dir + "/platforms";
554 }
555 } else {
556 header_dir = argv[optind];
557
558 if (argc - optind == 2) {
559 dependency_dir = argv[optind + 1];
560 }
561 }
562
Josh Gaobf8a2852016-05-27 11:59:09 -0700563 if (selected_levels.empty()) {
564 selected_levels = supported_levels;
565 }
566
567 if (selected_architectures.empty()) {
568 selected_architectures = supported_archs;
569 }
570
Josh Gaobf8a2852016-05-27 11:59:09 -0700571
572 struct stat st;
Josh Gao9b5af7a2016-06-02 14:29:13 -0700573 if (stat(header_dir.c_str(), &st) != 0) {
574 err(1, "failed to stat '%s'", header_dir.c_str());
Josh Gaobf8a2852016-05-27 11:59:09 -0700575 } else if (!S_ISDIR(st.st_mode)) {
Josh Gao9b5af7a2016-06-02 14:29:13 -0700576 errx(1, "'%s' is not a directory", header_dir.c_str());
Josh Gaobf8a2852016-05-27 11:59:09 -0700577 }
578
579 std::set<CompilationType> compilation_types;
Josh Gaobf8a2852016-05-27 11:59:09 -0700580 NdkSymbolDatabase symbol_database;
581
582 compilation_types = generateCompilationTypes(selected_architectures, selected_levels);
583
584 // Do this before compiling so that we can early exit if the platforms don't match what we
585 // expect.
586 if (!platform_dir.empty()) {
587 symbol_database = parsePlatforms(compilation_types, platform_dir);
588 }
589
590 bool failed = false;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700591 std::unique_ptr<HeaderDatabase> declaration_database =
592 compileHeaders(compilation_types, header_dir, dependency_dir, &failed);
Josh Gaobf8a2852016-05-27 11:59:09 -0700593
Josh Gaobfb6bae2016-07-15 17:25:21 -0700594 if (dump) {
595 declaration_database->dump(header_dir + "/");
596 } else {
597 if (!sanityCheck(declaration_database.get())) {
598 printf("versioner: sanity check failed\n");
Josh Gaobf8a2852016-05-27 11:59:09 -0700599 failed = true;
600 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700601
Josh Gaobfb6bae2016-07-15 17:25:21 -0700602 if (!platform_dir.empty()) {
603 if (!checkVersions(compilation_types, declaration_database.get(), symbol_database)) {
604 printf("versioner: version check failed\n");
605 failed = true;
606 }
607 }
608 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700609 return failed;
610}