blob: 432fd66aa875afc1dd3e4931c7b49763d577c6d2 [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 Gaoa77b3a92016-08-15 16:39:27 -070092 command.push_back("-D_FILE_OFFSET_BITS="s + std::to_string(type.file_offset_bits));
93
Josh Gaobf8a2852016-05-27 11:59:09 -070094 return CompileCommand(cwd, filename, command);
95 }
96
97 std::vector<CompileCommand> getAllCompileCommands() const override {
98 std::vector<CompileCommand> commands;
99 for (const std::string& file : headers) {
100 commands.push_back(generateCompileCommand(file));
101 }
102 return commands;
103 }
104
105 std::vector<CompileCommand> getCompileCommands(StringRef file) const override {
106 std::vector<CompileCommand> commands;
107 commands.push_back(generateCompileCommand(file));
108 return commands;
109 }
110
111 std::vector<std::string> getAllFiles() const override {
112 return headers;
113 }
114};
115
116struct CompilationRequirements {
117 std::vector<std::string> headers;
118 std::vector<std::string> dependencies;
119};
120
Josh Gaobfb6bae2016-07-15 17:25:21 -0700121static CompilationRequirements collectRequirements(const Arch& arch, const std::string& header_dir,
Josh Gaobf8a2852016-05-27 11:59:09 -0700122 const std::string& dependency_dir) {
123 std::vector<std::string> headers = collectFiles(header_dir);
124
125 std::vector<std::string> dependencies = { header_dir };
126 if (!dependency_dir.empty()) {
127 auto collect_children = [&dependencies](const std::string& dir_path) {
128 DIR* dir = opendir(dir_path.c_str());
129 if (!dir) {
130 err(1, "failed to open dependency directory '%s'", dir_path.c_str());
131 }
132
133 struct dirent* dent;
134 while ((dent = readdir(dir))) {
135 if (dent->d_name[0] == '.') {
136 continue;
137 }
138
139 // TODO: Resolve symlinks.
140 std::string dependency = dir_path + "/" + dent->d_name;
141
142 struct stat st;
143 if (stat(dependency.c_str(), &st) != 0) {
144 err(1, "failed to stat dependency '%s'", dependency.c_str());
145 }
146
147 if (!S_ISDIR(st.st_mode)) {
148 errx(1, "'%s' is not a directory", dependency.c_str());
149 }
150
151 dependencies.push_back(dependency);
152 }
153
154 closedir(dir);
155 };
156
157 collect_children(dependency_dir + "/common");
Josh Gaobfb6bae2016-07-15 17:25:21 -0700158 collect_children(dependency_dir + "/" + to_string(arch));
Josh Gaobf8a2852016-05-27 11:59:09 -0700159 }
160
161 auto new_end = std::remove_if(headers.begin(), headers.end(), [&arch](llvm::StringRef header) {
162 for (const auto& it : header_blacklist) {
163 if (it.second.find(arch) == it.second.end()) {
164 continue;
165 }
166
167 if (header.endswith("/" + it.first)) {
168 return true;
169 }
170 }
171 return false;
172 });
173
174 headers.erase(new_end, headers.end());
175
176 CompilationRequirements result = { .headers = headers, .dependencies = dependencies };
177 return result;
178}
179
Josh Gaobfb6bae2016-07-15 17:25:21 -0700180static std::set<CompilationType> generateCompilationTypes(const std::set<Arch> selected_architectures,
181 const std::set<int>& selected_levels) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700182 std::set<CompilationType> result;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700183 for (const auto& arch : selected_architectures) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700184 int min_api = arch_min_api[arch];
185 for (int api_level : selected_levels) {
186 if (api_level < min_api) {
187 continue;
188 }
Josh Gaoa77b3a92016-08-15 16:39:27 -0700189
190 for (int file_offset_bits : { 32, 64 }) {
191 CompilationType type = {
192 .arch = arch, .api_level = api_level, .file_offset_bits = file_offset_bits
193 };
194 result.insert(type);
195 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700196 }
197 }
198 return result;
199}
200
Josh Gaobfb6bae2016-07-15 17:25:21 -0700201static std::unique_ptr<HeaderDatabase> compileHeaders(const std::set<CompilationType>& types,
202 const std::string& header_dir,
203 const std::string& dependency_dir,
204 bool* failed) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700205 constexpr size_t thread_count = 8;
206 size_t threads_created = 0;
207 std::mutex mutex;
208 std::vector<std::thread> threads(thread_count);
209
210 std::map<CompilationType, HeaderDatabase> header_databases;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700211 std::unordered_map<Arch, CompilationRequirements> requirements;
Josh Gaobf8a2852016-05-27 11:59:09 -0700212
213 std::string cwd = getWorkingDir();
214 bool errors = false;
215
216 for (const auto& type : types) {
217 if (requirements.count(type.arch) == 0) {
218 requirements[type.arch] = collectRequirements(type.arch, header_dir, dependency_dir);
219 }
220 }
221
Josh Gaobfb6bae2016-07-15 17:25:21 -0700222 auto result = std::make_unique<HeaderDatabase>();
Josh Gaobf8a2852016-05-27 11:59:09 -0700223 for (const auto& type : types) {
224 size_t thread_id = threads_created++;
225 if (thread_id >= thread_count) {
226 thread_id = thread_id % thread_count;
227 threads[thread_id].join();
228 }
229
230 threads[thread_id] = std::thread(
231 [&](CompilationType type) {
232 const auto& req = requirements[type.arch];
233
Josh Gaobf8a2852016-05-27 11:59:09 -0700234 HeaderCompilationDatabase compilation_database(type, cwd, req.headers, req.dependencies);
235
236 ClangTool tool(compilation_database, req.headers);
237
238 clang::DiagnosticOptions diagnostic_options;
239 std::vector<std::unique_ptr<ASTUnit>> asts;
240 tool.buildASTs(asts);
241 for (const auto& ast : asts) {
242 clang::DiagnosticsEngine& diagnostics_engine = ast->getDiagnostics();
243 if (diagnostics_engine.getNumWarnings() || diagnostics_engine.hasErrorOccurred()) {
244 std::unique_lock<std::mutex> l(mutex);
245 errors = true;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700246 printf("versioner: compilation failure for %s in %s\n", to_string(type).c_str(),
Josh Gaobf8a2852016-05-27 11:59:09 -0700247 ast->getOriginalSourceFileName().str().c_str());
248 }
249
Josh Gaobfb6bae2016-07-15 17:25:21 -0700250 result->parseAST(type, ast.get());
Josh Gaobf8a2852016-05-27 11:59:09 -0700251 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700252 },
253 type);
254 }
255
256 if (threads_created < thread_count) {
257 threads.resize(threads_created);
258 }
259
260 for (auto& thread : threads) {
261 thread.join();
262 }
263
264 if (errors) {
265 printf("versioner: compilation generated warnings or errors\n");
266 *failed = errors;
267 }
268
Josh Gaobfb6bae2016-07-15 17:25:21 -0700269 return result;
Josh Gaobf8a2852016-05-27 11:59:09 -0700270}
271
Josh Gaobfb6bae2016-07-15 17:25:21 -0700272// Perform a sanity check on a symbol's declarations, enforcing the following invariants:
273// 1. At most one inline definition of the function exists.
274// 2. All of the availability declarations for a symbol are compatible.
275// If a function is declared as an inline before a certain version, the inline definition
276// should have no version tag.
277// 3. Each availability type must only be present globally or on a per-arch basis.
278// (e.g. __INTRODUCED_IN_ARM(9) __INTRODUCED_IN_X86(10) __DEPRECATED_IN(11) is fine,
279// but not __INTRODUCED_IN(9) __INTRODUCED_IN_X86(10))
280static bool checkSymbol(const Symbol& symbol) {
281 std::string cwd = getWorkingDir() + "/";
282
283 const Declaration* inline_definition = nullptr;
284 for (const auto& decl_it : symbol.declarations) {
285 const Declaration* decl = &decl_it.second;
286 if (decl->is_definition) {
287 if (inline_definition) {
288 fprintf(stderr, "versioner: multiple definitions of symbol %s\n", symbol.name.c_str());
289 symbol.dump(cwd);
290 inline_definition->dump(cwd);
291 return false;
292 }
293
294 inline_definition = decl;
295 }
296
297 DeclarationAvailability availability;
298 if (!decl->calculateAvailability(&availability)) {
299 fprintf(stderr, "versioner: failed to calculate availability for declaration:\n");
Josh Gao566735d2016-08-02 15:07:32 -0700300 decl->dump(cwd, stderr, 2);
Josh Gaobfb6bae2016-07-15 17:25:21 -0700301 return false;
302 }
303
304 if (decl->is_definition && !availability.empty()) {
305 fprintf(stderr, "versioner: inline definition has non-empty versioning information:\n");
Josh Gao566735d2016-08-02 15:07:32 -0700306 decl->dump(cwd, stderr, 2);
Josh Gaobfb6bae2016-07-15 17:25:21 -0700307 return false;
308 }
309 }
310
311 DeclarationAvailability availability;
312 if (!symbol.calculateAvailability(&availability)) {
313 fprintf(stderr, "versioner: inconsistent availability for symbol '%s'\n", symbol.name.c_str());
314 symbol.dump(cwd);
315 return false;
316 }
317
318 // TODO: Check invariant #3.
319 return true;
320}
321
322static bool sanityCheck(const HeaderDatabase* database) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700323 bool error = false;
Josh Gao958f3b32016-06-03 13:44:00 -0700324 std::string cwd = getWorkingDir() + "/";
325
Josh Gaobfb6bae2016-07-15 17:25:21 -0700326 for (const auto& symbol_it : database->symbols) {
327 if (!checkSymbol(symbol_it.second)) {
328 error = true;
Josh Gaobf8a2852016-05-27 11:59:09 -0700329 }
330 }
331 return !error;
332}
333
334// Check that our symbol availability declarations match the actual NDK
335// platform symbol availability.
336static bool checkVersions(const std::set<CompilationType>& types,
Josh Gaobfb6bae2016-07-15 17:25:21 -0700337 const HeaderDatabase* header_database,
Josh Gaobf8a2852016-05-27 11:59:09 -0700338 const NdkSymbolDatabase& symbol_database) {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700339 std::string cwd = getWorkingDir() + "/";
Josh Gaobf8a2852016-05-27 11:59:09 -0700340 bool failed = false;
341
Josh Gaobfb6bae2016-07-15 17:25:21 -0700342 std::map<Arch, std::set<CompilationType>> arch_types;
Josh Gaobf8a2852016-05-27 11:59:09 -0700343 for (const CompilationType& type : types) {
344 arch_types[type.arch].insert(type);
345 }
346
Josh Gaod67dbf02016-06-02 15:21:14 -0700347 std::set<std::string> completely_unavailable;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700348 std::map<std::string, std::set<CompilationType>> missing_availability;
349 std::map<std::string, std::set<CompilationType>> extra_availability;
Josh Gaod67dbf02016-06-02 15:21:14 -0700350
Josh Gaobfb6bae2016-07-15 17:25:21 -0700351 for (const auto& symbol_it : header_database->symbols) {
352 const auto& symbol_name = symbol_it.first;
353 DeclarationAvailability symbol_availability;
Josh Gaobf8a2852016-05-27 11:59:09 -0700354
Josh Gaobfb6bae2016-07-15 17:25:21 -0700355 if (!symbol_it.second.calculateAvailability(&symbol_availability)) {
356 errx(1, "failed to calculate symbol availability");
357 }
358
359 const auto platform_availability_it = symbol_database.find(symbol_name);
Josh Gaobf8a2852016-05-27 11:59:09 -0700360 if (platform_availability_it == symbol_database.end()) {
Josh Gaod67dbf02016-06-02 15:21:14 -0700361 completely_unavailable.insert(symbol_name);
Josh Gaobf8a2852016-05-27 11:59:09 -0700362 continue;
363 }
364
365 const auto& platform_availability = platform_availability_it->second;
Josh Gaobf8a2852016-05-27 11:59:09 -0700366
367 for (const CompilationType& type : types) {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700368 bool should_be_available = true;
369 const auto& global_availability = symbol_availability.global_availability;
370 const auto& arch_availability = symbol_availability.arch_availability[type.arch];
371 if (global_availability.introduced != 0 && global_availability.introduced > type.api_level) {
372 should_be_available = false;
373 }
374
375 if (arch_availability.introduced != 0 && arch_availability.introduced > type.api_level) {
376 should_be_available = false;
377 }
378
379 if (global_availability.obsoleted != 0 && global_availability.obsoleted <= type.api_level) {
380 should_be_available = false;
381 }
382
383 if (arch_availability.obsoleted != 0 && arch_availability.obsoleted <= type.api_level) {
384 should_be_available = false;
385 }
386
387 if (arch_availability.future) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700388 continue;
389 }
390
Josh Gaobfb6bae2016-07-15 17:25:21 -0700391 // The function declaration might be (validly) missing for the given CompilationType.
392 if (!symbol_it.second.hasDeclaration(type)) {
393 should_be_available = false;
394 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700395
Josh Gaobfb6bae2016-07-15 17:25:21 -0700396 bool is_available = platform_availability.count(type);
Josh Gaobf8a2852016-05-27 11:59:09 -0700397
Josh Gaobfb6bae2016-07-15 17:25:21 -0700398 if (should_be_available != is_available) {
399 if (is_available) {
400 extra_availability[symbol_name].insert(type);
Josh Gaobf8a2852016-05-27 11:59:09 -0700401 } else {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700402 missing_availability[symbol_name].insert(type);
Josh Gaobf8a2852016-05-27 11:59:09 -0700403 }
404 }
405 }
Josh Gaobfb6bae2016-07-15 17:25:21 -0700406 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700407
Josh Gaobfb6bae2016-07-15 17:25:21 -0700408 for (const auto& it : symbol_database) {
409 const std::string& symbol_name = it.first;
Josh Gaobf8a2852016-05-27 11:59:09 -0700410
Josh Gaobfb6bae2016-07-15 17:25:21 -0700411 bool symbol_error = false;
412 auto missing_it = missing_availability.find(symbol_name);
413 if (missing_it != missing_availability.end()) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700414 printf("%s: declaration marked available but symbol missing in [%s]\n", symbol_name.c_str(),
Josh Gaobfb6bae2016-07-15 17:25:21 -0700415 Join(missing_it->second, ", ").c_str());
416 symbol_error = true;
Josh Gaobf8a2852016-05-27 11:59:09 -0700417 failed = true;
418 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700419
Josh Gaobfb6bae2016-07-15 17:25:21 -0700420 if (verbose) {
421 auto extra_it = extra_availability.find(symbol_name);
422 if (extra_it != extra_availability.end()) {
423 printf("%s: declaration marked unavailable but symbol available in [%s]\n",
424 symbol_name.c_str(), Join(extra_it->second, ", ").c_str());
425 symbol_error = true;
426 failed = true;
Josh Gao958f3b32016-06-03 13:44:00 -0700427 }
428 }
429
Josh Gaobfb6bae2016-07-15 17:25:21 -0700430 if (symbol_error) {
431 auto symbol_it = header_database->symbols.find(symbol_name);
432 if (symbol_it == header_database->symbols.end()) {
433 errx(1, "failed to find symbol in header database");
434 }
435 symbol_it->second.dump(cwd);
Josh Gaod67dbf02016-06-02 15:21:14 -0700436 }
Josh Gaod67dbf02016-06-02 15:21:14 -0700437 }
438
Josh Gaobfb6bae2016-07-15 17:25:21 -0700439 // TODO: Verify that function/variable declarations are actually function/variable symbols.
Josh Gaobf8a2852016-05-27 11:59:09 -0700440 return !failed;
441}
442
Josh Gao62aaf8f2016-06-02 14:27:21 -0700443static void usage(bool help = false) {
444 fprintf(stderr, "Usage: versioner [OPTION]... [HEADER_PATH] [DEPS_PATH]\n");
445 if (!help) {
446 printf("Try 'versioner -h' for more information.\n");
447 exit(1);
448 } else {
449 fprintf(stderr, "Version headers at HEADER_PATH, with DEPS_PATH/ARCH/* on the include path\n");
Josh Gao9b5af7a2016-06-02 14:29:13 -0700450 fprintf(stderr, "Autodetects paths if HEADER_PATH and DEPS_PATH are not specified\n");
Josh Gao62aaf8f2016-06-02 14:27:21 -0700451 fprintf(stderr, "\n");
452 fprintf(stderr, "Target specification (defaults to all):\n");
453 fprintf(stderr, " -a API_LEVEL\tbuild with specified API level (can be repeated)\n");
454 fprintf(stderr, " \t\tvalid levels are %s\n", Join(supported_levels).c_str());
455 fprintf(stderr, " -r ARCH\tbuild with specified architecture (can be repeated)\n");
456 fprintf(stderr, " \t\tvalid architectures are %s\n", Join(supported_archs).c_str());
457 fprintf(stderr, "\n");
458 fprintf(stderr, "Validation:\n");
459 fprintf(stderr, " -p PATH\tcompare against NDK platform at PATH\n");
460 fprintf(stderr, " -v\t\tenable verbose warnings\n");
461 fprintf(stderr, "\n");
Josh Gaof8592a32016-07-26 18:58:27 -0700462 fprintf(stderr, "Preprocessing:\n");
463 fprintf(stderr, " -o PATH\tpreprocess header files and emit them at PATH\n");
464 fprintf(stderr, " -f\tpreprocess header files even if validation fails\n");
465 fprintf(stderr, "\n");
Josh Gao62aaf8f2016-06-02 14:27:21 -0700466 fprintf(stderr, "Miscellaneous:\n");
Josh Gaobfb6bae2016-07-15 17:25:21 -0700467 fprintf(stderr, " -d\t\tdump function availability\n");
Josh Gao62aaf8f2016-06-02 14:27:21 -0700468 fprintf(stderr, " -h\t\tdisplay this message\n");
469 exit(0);
470 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700471}
472
473int main(int argc, char** argv) {
474 std::string cwd = getWorkingDir() + "/";
475 bool default_args = true;
476 std::string platform_dir;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700477 std::set<Arch> selected_architectures;
Josh Gaobf8a2852016-05-27 11:59:09 -0700478 std::set<int> selected_levels;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700479 bool dump = false;
Josh Gaof8592a32016-07-26 18:58:27 -0700480 std::string preprocessor_output_path;
481 bool force = false;
Josh Gaobf8a2852016-05-27 11:59:09 -0700482
483 int c;
Josh Gaof8592a32016-07-26 18:58:27 -0700484 while ((c = getopt(argc, argv, "a:r:p:vo:fdhi")) != -1) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700485 default_args = false;
486 switch (c) {
487 case 'a': {
488 char* end;
489 int api_level = strtol(optarg, &end, 10);
490 if (end == optarg || strlen(end) > 0) {
491 usage();
492 }
493
494 if (supported_levels.count(api_level) == 0) {
495 errx(1, "unsupported API level %d", api_level);
496 }
497
498 selected_levels.insert(api_level);
499 break;
500 }
501
502 case 'r': {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700503 Arch arch = arch_from_string(optarg);
504 selected_architectures.insert(arch);
Josh Gaobf8a2852016-05-27 11:59:09 -0700505 break;
506 }
507
508 case 'p': {
509 if (!platform_dir.empty()) {
510 usage();
511 }
512
513 platform_dir = optarg;
514
Josh Gaof8592a32016-07-26 18:58:27 -0700515 if (platform_dir.empty()) {
516 usage();
517 }
518
Josh Gaobf8a2852016-05-27 11:59:09 -0700519 struct stat st;
520 if (stat(platform_dir.c_str(), &st) != 0) {
521 err(1, "failed to stat platform directory '%s'", platform_dir.c_str());
522 }
523 if (!S_ISDIR(st.st_mode)) {
524 errx(1, "'%s' is not a directory", optarg);
525 }
526 break;
527 }
528
529 case 'v':
530 verbose = true;
531 break;
532
Josh Gaof8592a32016-07-26 18:58:27 -0700533 case 'o':
534 if (!preprocessor_output_path.empty()) {
535 usage();
536 }
537 preprocessor_output_path = optarg;
538 if (preprocessor_output_path.empty()) {
539 usage();
540 }
541 break;
542
543 case 'f':
544 force = true;
545 break;
546
Josh Gaobfb6bae2016-07-15 17:25:21 -0700547 case 'd':
548 dump = true;
549 break;
550
Josh Gao62aaf8f2016-06-02 14:27:21 -0700551 case 'h':
552 usage(true);
553 break;
554
Josh Gaobfb6bae2016-07-15 17:25:21 -0700555 case 'i':
556 // Secret option for tests to -include <android/versioning.h>.
557 add_include = true;
558 break;
559
Josh Gaobf8a2852016-05-27 11:59:09 -0700560 default:
561 usage();
562 break;
563 }
564 }
565
Josh Gao9b5af7a2016-06-02 14:29:13 -0700566 if (argc - optind > 2 || optind > argc) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700567 usage();
568 }
569
Josh Gao9b5af7a2016-06-02 14:29:13 -0700570 std::string header_dir;
571 std::string dependency_dir;
572
Josh Gaobfb6bae2016-07-15 17:25:21 -0700573 const char* top = getenv("ANDROID_BUILD_TOP");
574 if (!top && (optind == argc || add_include)) {
575 fprintf(stderr, "versioner: failed to autodetect bionic paths. Is ANDROID_BUILD_TOP set?\n");
576 usage();
577 }
578
Josh Gao9b5af7a2016-06-02 14:29:13 -0700579 if (optind == argc) {
580 // Neither HEADER_PATH nor DEPS_PATH were specified, so try to figure them out.
Josh Gaobfb6bae2016-07-15 17:25:21 -0700581 std::string versioner_dir = to_string(top) + "/bionic/tools/versioner";
Josh Gao9b5af7a2016-06-02 14:29:13 -0700582 header_dir = versioner_dir + "/current";
583 dependency_dir = versioner_dir + "/dependencies";
584 if (platform_dir.empty()) {
585 platform_dir = versioner_dir + "/platforms";
586 }
587 } else {
Josh Gaof8592a32016-07-26 18:58:27 -0700588 // Intentional leak.
589 header_dir = realpath(argv[optind], nullptr);
Josh Gao9b5af7a2016-06-02 14:29:13 -0700590
591 if (argc - optind == 2) {
592 dependency_dir = argv[optind + 1];
593 }
594 }
595
Josh Gaobf8a2852016-05-27 11:59:09 -0700596 if (selected_levels.empty()) {
597 selected_levels = supported_levels;
598 }
599
600 if (selected_architectures.empty()) {
601 selected_architectures = supported_archs;
602 }
603
Josh Gaobf8a2852016-05-27 11:59:09 -0700604
605 struct stat st;
Josh Gao9b5af7a2016-06-02 14:29:13 -0700606 if (stat(header_dir.c_str(), &st) != 0) {
607 err(1, "failed to stat '%s'", header_dir.c_str());
Josh Gaobf8a2852016-05-27 11:59:09 -0700608 } else if (!S_ISDIR(st.st_mode)) {
Josh Gao9b5af7a2016-06-02 14:29:13 -0700609 errx(1, "'%s' is not a directory", header_dir.c_str());
Josh Gaobf8a2852016-05-27 11:59:09 -0700610 }
611
612 std::set<CompilationType> compilation_types;
Josh Gaobf8a2852016-05-27 11:59:09 -0700613 NdkSymbolDatabase symbol_database;
614
615 compilation_types = generateCompilationTypes(selected_architectures, selected_levels);
616
617 // Do this before compiling so that we can early exit if the platforms don't match what we
618 // expect.
619 if (!platform_dir.empty()) {
620 symbol_database = parsePlatforms(compilation_types, platform_dir);
621 }
622
623 bool failed = false;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700624 std::unique_ptr<HeaderDatabase> declaration_database =
625 compileHeaders(compilation_types, header_dir, dependency_dir, &failed);
Josh Gaobf8a2852016-05-27 11:59:09 -0700626
Josh Gaobfb6bae2016-07-15 17:25:21 -0700627 if (dump) {
628 declaration_database->dump(header_dir + "/");
629 } else {
630 if (!sanityCheck(declaration_database.get())) {
631 printf("versioner: sanity check failed\n");
Josh Gaobf8a2852016-05-27 11:59:09 -0700632 failed = true;
633 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700634
Josh Gaobfb6bae2016-07-15 17:25:21 -0700635 if (!platform_dir.empty()) {
636 if (!checkVersions(compilation_types, declaration_database.get(), symbol_database)) {
637 printf("versioner: version check failed\n");
638 failed = true;
639 }
640 }
641 }
Josh Gaof8592a32016-07-26 18:58:27 -0700642
643 if (!preprocessor_output_path.empty() && (force || !failed)) {
644 failed = !preprocessHeaders(preprocessor_output_path, header_dir, declaration_database.get());
645 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700646 return failed;
647}