blob: daf6f0bec95ded6b0ea2316050b86221794e17de [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");
Josh Gaobb966282016-09-14 14:21:56 -070080 command.push_back("-Wall");
81 command.push_back("-Wextra");
82 command.push_back("-Werror");
83 command.push_back("-Wno-unused-macros");
84 command.push_back("-Wno-unused-function");
85 command.push_back("-Wno-unused-variable");
Josh Gaobf8a2852016-05-27 11:59:09 -070086 command.push_back("-Wno-unknown-attributes");
Josh Gaobfb6bae2016-07-15 17:25:21 -070087 command.push_back("-Wno-pragma-once-outside-header");
Josh Gaobf8a2852016-05-27 11:59:09 -070088 command.push_back("-target");
89 command.push_back(arch_targets[type.arch]);
90
Josh Gaobfb6bae2016-07-15 17:25:21 -070091 if (add_include) {
92 const char* top = getenv("ANDROID_BUILD_TOP");
93 std::string header_path = to_string(top) + "/bionic/libc/include/android/versioning.h";
94 command.push_back("-include");
95 command.push_back(std::move(header_path));
96 }
97
Josh Gaoa77b3a92016-08-15 16:39:27 -070098 command.push_back("-D_FILE_OFFSET_BITS="s + std::to_string(type.file_offset_bits));
99
Josh Gaobf8a2852016-05-27 11:59:09 -0700100 return CompileCommand(cwd, filename, command);
101 }
102
103 std::vector<CompileCommand> getAllCompileCommands() const override {
104 std::vector<CompileCommand> commands;
105 for (const std::string& file : headers) {
106 commands.push_back(generateCompileCommand(file));
107 }
108 return commands;
109 }
110
111 std::vector<CompileCommand> getCompileCommands(StringRef file) const override {
112 std::vector<CompileCommand> commands;
113 commands.push_back(generateCompileCommand(file));
114 return commands;
115 }
116
117 std::vector<std::string> getAllFiles() const override {
118 return headers;
119 }
120};
121
122struct CompilationRequirements {
123 std::vector<std::string> headers;
124 std::vector<std::string> dependencies;
125};
126
Josh Gaobfb6bae2016-07-15 17:25:21 -0700127static CompilationRequirements collectRequirements(const Arch& arch, const std::string& header_dir,
Josh Gaobf8a2852016-05-27 11:59:09 -0700128 const std::string& dependency_dir) {
129 std::vector<std::string> headers = collectFiles(header_dir);
130
131 std::vector<std::string> dependencies = { header_dir };
132 if (!dependency_dir.empty()) {
133 auto collect_children = [&dependencies](const std::string& dir_path) {
134 DIR* dir = opendir(dir_path.c_str());
135 if (!dir) {
136 err(1, "failed to open dependency directory '%s'", dir_path.c_str());
137 }
138
139 struct dirent* dent;
140 while ((dent = readdir(dir))) {
141 if (dent->d_name[0] == '.') {
142 continue;
143 }
144
145 // TODO: Resolve symlinks.
146 std::string dependency = dir_path + "/" + dent->d_name;
147
148 struct stat st;
149 if (stat(dependency.c_str(), &st) != 0) {
150 err(1, "failed to stat dependency '%s'", dependency.c_str());
151 }
152
153 if (!S_ISDIR(st.st_mode)) {
154 errx(1, "'%s' is not a directory", dependency.c_str());
155 }
156
157 dependencies.push_back(dependency);
158 }
159
160 closedir(dir);
161 };
162
163 collect_children(dependency_dir + "/common");
Josh Gaobfb6bae2016-07-15 17:25:21 -0700164 collect_children(dependency_dir + "/" + to_string(arch));
Josh Gaobf8a2852016-05-27 11:59:09 -0700165 }
166
167 auto new_end = std::remove_if(headers.begin(), headers.end(), [&arch](llvm::StringRef header) {
168 for (const auto& it : header_blacklist) {
169 if (it.second.find(arch) == it.second.end()) {
170 continue;
171 }
172
173 if (header.endswith("/" + it.first)) {
174 return true;
175 }
176 }
177 return false;
178 });
179
180 headers.erase(new_end, headers.end());
181
182 CompilationRequirements result = { .headers = headers, .dependencies = dependencies };
183 return result;
184}
185
Josh Gaobfb6bae2016-07-15 17:25:21 -0700186static std::set<CompilationType> generateCompilationTypes(const std::set<Arch> selected_architectures,
187 const std::set<int>& selected_levels) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700188 std::set<CompilationType> result;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700189 for (const auto& arch : selected_architectures) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700190 int min_api = arch_min_api[arch];
191 for (int api_level : selected_levels) {
192 if (api_level < min_api) {
193 continue;
194 }
Josh Gaoa77b3a92016-08-15 16:39:27 -0700195
196 for (int file_offset_bits : { 32, 64 }) {
197 CompilationType type = {
198 .arch = arch, .api_level = api_level, .file_offset_bits = file_offset_bits
199 };
200 result.insert(type);
201 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700202 }
203 }
204 return result;
205}
206
Josh Gaobfb6bae2016-07-15 17:25:21 -0700207static std::unique_ptr<HeaderDatabase> compileHeaders(const std::set<CompilationType>& types,
208 const std::string& header_dir,
209 const std::string& dependency_dir,
210 bool* failed) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700211 constexpr size_t thread_count = 8;
212 size_t threads_created = 0;
213 std::mutex mutex;
214 std::vector<std::thread> threads(thread_count);
215
216 std::map<CompilationType, HeaderDatabase> header_databases;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700217 std::unordered_map<Arch, CompilationRequirements> requirements;
Josh Gaobf8a2852016-05-27 11:59:09 -0700218
219 std::string cwd = getWorkingDir();
220 bool errors = false;
221
222 for (const auto& type : types) {
223 if (requirements.count(type.arch) == 0) {
224 requirements[type.arch] = collectRequirements(type.arch, header_dir, dependency_dir);
225 }
226 }
227
Josh Gaobfb6bae2016-07-15 17:25:21 -0700228 auto result = std::make_unique<HeaderDatabase>();
Josh Gaobf8a2852016-05-27 11:59:09 -0700229 for (const auto& type : types) {
230 size_t thread_id = threads_created++;
231 if (thread_id >= thread_count) {
232 thread_id = thread_id % thread_count;
233 threads[thread_id].join();
234 }
235
236 threads[thread_id] = std::thread(
237 [&](CompilationType type) {
238 const auto& req = requirements[type.arch];
239
Josh Gaobf8a2852016-05-27 11:59:09 -0700240 HeaderCompilationDatabase compilation_database(type, cwd, req.headers, req.dependencies);
241
242 ClangTool tool(compilation_database, req.headers);
243
244 clang::DiagnosticOptions diagnostic_options;
245 std::vector<std::unique_ptr<ASTUnit>> asts;
246 tool.buildASTs(asts);
247 for (const auto& ast : asts) {
248 clang::DiagnosticsEngine& diagnostics_engine = ast->getDiagnostics();
249 if (diagnostics_engine.getNumWarnings() || diagnostics_engine.hasErrorOccurred()) {
250 std::unique_lock<std::mutex> l(mutex);
251 errors = true;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700252 printf("versioner: compilation failure for %s in %s\n", to_string(type).c_str(),
Josh Gaobf8a2852016-05-27 11:59:09 -0700253 ast->getOriginalSourceFileName().str().c_str());
254 }
255
Josh Gaobfb6bae2016-07-15 17:25:21 -0700256 result->parseAST(type, ast.get());
Josh Gaobf8a2852016-05-27 11:59:09 -0700257 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700258 },
259 type);
260 }
261
262 if (threads_created < thread_count) {
263 threads.resize(threads_created);
264 }
265
266 for (auto& thread : threads) {
267 thread.join();
268 }
269
270 if (errors) {
271 printf("versioner: compilation generated warnings or errors\n");
272 *failed = errors;
273 }
274
Josh Gaobfb6bae2016-07-15 17:25:21 -0700275 return result;
Josh Gaobf8a2852016-05-27 11:59:09 -0700276}
277
Josh Gaobfb6bae2016-07-15 17:25:21 -0700278// Perform a sanity check on a symbol's declarations, enforcing the following invariants:
279// 1. At most one inline definition of the function exists.
280// 2. All of the availability declarations for a symbol are compatible.
281// If a function is declared as an inline before a certain version, the inline definition
282// should have no version tag.
283// 3. Each availability type must only be present globally or on a per-arch basis.
284// (e.g. __INTRODUCED_IN_ARM(9) __INTRODUCED_IN_X86(10) __DEPRECATED_IN(11) is fine,
285// but not __INTRODUCED_IN(9) __INTRODUCED_IN_X86(10))
286static bool checkSymbol(const Symbol& symbol) {
287 std::string cwd = getWorkingDir() + "/";
288
289 const Declaration* inline_definition = nullptr;
290 for (const auto& decl_it : symbol.declarations) {
291 const Declaration* decl = &decl_it.second;
292 if (decl->is_definition) {
293 if (inline_definition) {
294 fprintf(stderr, "versioner: multiple definitions of symbol %s\n", symbol.name.c_str());
295 symbol.dump(cwd);
296 inline_definition->dump(cwd);
297 return false;
298 }
299
300 inline_definition = decl;
301 }
302
303 DeclarationAvailability availability;
304 if (!decl->calculateAvailability(&availability)) {
305 fprintf(stderr, "versioner: failed to calculate availability for declaration:\n");
Josh 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 if (decl->is_definition && !availability.empty()) {
311 fprintf(stderr, "versioner: inline definition has non-empty versioning information:\n");
Josh Gao566735d2016-08-02 15:07:32 -0700312 decl->dump(cwd, stderr, 2);
Josh Gaobfb6bae2016-07-15 17:25:21 -0700313 return false;
314 }
315 }
316
317 DeclarationAvailability availability;
318 if (!symbol.calculateAvailability(&availability)) {
319 fprintf(stderr, "versioner: inconsistent availability for symbol '%s'\n", symbol.name.c_str());
320 symbol.dump(cwd);
321 return false;
322 }
323
324 // TODO: Check invariant #3.
325 return true;
326}
327
328static bool sanityCheck(const HeaderDatabase* database) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700329 bool error = false;
Josh Gao958f3b32016-06-03 13:44:00 -0700330 std::string cwd = getWorkingDir() + "/";
331
Josh Gaobfb6bae2016-07-15 17:25:21 -0700332 for (const auto& symbol_it : database->symbols) {
333 if (!checkSymbol(symbol_it.second)) {
334 error = true;
Josh Gaobf8a2852016-05-27 11:59:09 -0700335 }
336 }
337 return !error;
338}
339
340// Check that our symbol availability declarations match the actual NDK
341// platform symbol availability.
342static bool checkVersions(const std::set<CompilationType>& types,
Josh Gaobfb6bae2016-07-15 17:25:21 -0700343 const HeaderDatabase* header_database,
Josh Gaobf8a2852016-05-27 11:59:09 -0700344 const NdkSymbolDatabase& symbol_database) {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700345 std::string cwd = getWorkingDir() + "/";
Josh Gaobf8a2852016-05-27 11:59:09 -0700346 bool failed = false;
347
Josh Gaobfb6bae2016-07-15 17:25:21 -0700348 std::map<Arch, std::set<CompilationType>> arch_types;
Josh Gaobf8a2852016-05-27 11:59:09 -0700349 for (const CompilationType& type : types) {
350 arch_types[type.arch].insert(type);
351 }
352
Josh Gaod67dbf02016-06-02 15:21:14 -0700353 std::set<std::string> completely_unavailable;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700354 std::map<std::string, std::set<CompilationType>> missing_availability;
355 std::map<std::string, std::set<CompilationType>> extra_availability;
Josh Gaod67dbf02016-06-02 15:21:14 -0700356
Josh Gaobfb6bae2016-07-15 17:25:21 -0700357 for (const auto& symbol_it : header_database->symbols) {
358 const auto& symbol_name = symbol_it.first;
359 DeclarationAvailability symbol_availability;
Josh Gaobf8a2852016-05-27 11:59:09 -0700360
Josh Gaobfb6bae2016-07-15 17:25:21 -0700361 if (!symbol_it.second.calculateAvailability(&symbol_availability)) {
362 errx(1, "failed to calculate symbol availability");
363 }
364
365 const auto platform_availability_it = symbol_database.find(symbol_name);
Josh Gaobf8a2852016-05-27 11:59:09 -0700366 if (platform_availability_it == symbol_database.end()) {
Josh Gaod67dbf02016-06-02 15:21:14 -0700367 completely_unavailable.insert(symbol_name);
Josh Gaobf8a2852016-05-27 11:59:09 -0700368 continue;
369 }
370
371 const auto& platform_availability = platform_availability_it->second;
Josh Gaobf8a2852016-05-27 11:59:09 -0700372
373 for (const CompilationType& type : types) {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700374 bool should_be_available = true;
375 const auto& global_availability = symbol_availability.global_availability;
376 const auto& arch_availability = symbol_availability.arch_availability[type.arch];
377 if (global_availability.introduced != 0 && global_availability.introduced > type.api_level) {
378 should_be_available = false;
379 }
380
381 if (arch_availability.introduced != 0 && arch_availability.introduced > type.api_level) {
382 should_be_available = false;
383 }
384
385 if (global_availability.obsoleted != 0 && global_availability.obsoleted <= type.api_level) {
386 should_be_available = false;
387 }
388
389 if (arch_availability.obsoleted != 0 && arch_availability.obsoleted <= type.api_level) {
390 should_be_available = false;
391 }
392
393 if (arch_availability.future) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700394 continue;
395 }
396
Josh Gaobfb6bae2016-07-15 17:25:21 -0700397 // The function declaration might be (validly) missing for the given CompilationType.
398 if (!symbol_it.second.hasDeclaration(type)) {
399 should_be_available = false;
400 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700401
Josh Gaobfb6bae2016-07-15 17:25:21 -0700402 bool is_available = platform_availability.count(type);
Josh Gaobf8a2852016-05-27 11:59:09 -0700403
Josh Gaobfb6bae2016-07-15 17:25:21 -0700404 if (should_be_available != is_available) {
405 if (is_available) {
406 extra_availability[symbol_name].insert(type);
Josh Gaobf8a2852016-05-27 11:59:09 -0700407 } else {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700408 missing_availability[symbol_name].insert(type);
Josh Gaobf8a2852016-05-27 11:59:09 -0700409 }
410 }
411 }
Josh Gaobfb6bae2016-07-15 17:25:21 -0700412 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700413
Josh Gaobfb6bae2016-07-15 17:25:21 -0700414 for (const auto& it : symbol_database) {
415 const std::string& symbol_name = it.first;
Josh Gaobf8a2852016-05-27 11:59:09 -0700416
Josh Gaobfb6bae2016-07-15 17:25:21 -0700417 bool symbol_error = false;
418 auto missing_it = missing_availability.find(symbol_name);
419 if (missing_it != missing_availability.end()) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700420 printf("%s: declaration marked available but symbol missing in [%s]\n", symbol_name.c_str(),
Josh Gaobfb6bae2016-07-15 17:25:21 -0700421 Join(missing_it->second, ", ").c_str());
422 symbol_error = true;
Josh Gaobf8a2852016-05-27 11:59:09 -0700423 failed = true;
424 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700425
Josh Gaobfb6bae2016-07-15 17:25:21 -0700426 if (verbose) {
427 auto extra_it = extra_availability.find(symbol_name);
428 if (extra_it != extra_availability.end()) {
429 printf("%s: declaration marked unavailable but symbol available in [%s]\n",
430 symbol_name.c_str(), Join(extra_it->second, ", ").c_str());
431 symbol_error = true;
432 failed = true;
Josh Gao958f3b32016-06-03 13:44:00 -0700433 }
434 }
435
Josh Gaobfb6bae2016-07-15 17:25:21 -0700436 if (symbol_error) {
437 auto symbol_it = header_database->symbols.find(symbol_name);
438 if (symbol_it == header_database->symbols.end()) {
439 errx(1, "failed to find symbol in header database");
440 }
441 symbol_it->second.dump(cwd);
Josh Gaod67dbf02016-06-02 15:21:14 -0700442 }
Josh Gaod67dbf02016-06-02 15:21:14 -0700443 }
444
Josh Gaobfb6bae2016-07-15 17:25:21 -0700445 // TODO: Verify that function/variable declarations are actually function/variable symbols.
Josh Gaobf8a2852016-05-27 11:59:09 -0700446 return !failed;
447}
448
Josh Gao62aaf8f2016-06-02 14:27:21 -0700449static void usage(bool help = false) {
450 fprintf(stderr, "Usage: versioner [OPTION]... [HEADER_PATH] [DEPS_PATH]\n");
451 if (!help) {
452 printf("Try 'versioner -h' for more information.\n");
453 exit(1);
454 } else {
455 fprintf(stderr, "Version headers at HEADER_PATH, with DEPS_PATH/ARCH/* on the include path\n");
Josh Gao9b5af7a2016-06-02 14:29:13 -0700456 fprintf(stderr, "Autodetects paths if HEADER_PATH and DEPS_PATH are not specified\n");
Josh Gao62aaf8f2016-06-02 14:27:21 -0700457 fprintf(stderr, "\n");
458 fprintf(stderr, "Target specification (defaults to all):\n");
459 fprintf(stderr, " -a API_LEVEL\tbuild with specified API level (can be repeated)\n");
460 fprintf(stderr, " \t\tvalid levels are %s\n", Join(supported_levels).c_str());
461 fprintf(stderr, " -r ARCH\tbuild with specified architecture (can be repeated)\n");
462 fprintf(stderr, " \t\tvalid architectures are %s\n", Join(supported_archs).c_str());
463 fprintf(stderr, "\n");
464 fprintf(stderr, "Validation:\n");
465 fprintf(stderr, " -p PATH\tcompare against NDK platform at PATH\n");
466 fprintf(stderr, " -v\t\tenable verbose warnings\n");
467 fprintf(stderr, "\n");
Josh Gaof8592a32016-07-26 18:58:27 -0700468 fprintf(stderr, "Preprocessing:\n");
469 fprintf(stderr, " -o PATH\tpreprocess header files and emit them at PATH\n");
470 fprintf(stderr, " -f\tpreprocess header files even if validation fails\n");
471 fprintf(stderr, "\n");
Josh Gao62aaf8f2016-06-02 14:27:21 -0700472 fprintf(stderr, "Miscellaneous:\n");
Josh Gaobfb6bae2016-07-15 17:25:21 -0700473 fprintf(stderr, " -d\t\tdump function availability\n");
Josh Gao62aaf8f2016-06-02 14:27:21 -0700474 fprintf(stderr, " -h\t\tdisplay this message\n");
475 exit(0);
476 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700477}
478
479int main(int argc, char** argv) {
480 std::string cwd = getWorkingDir() + "/";
481 bool default_args = true;
482 std::string platform_dir;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700483 std::set<Arch> selected_architectures;
Josh Gaobf8a2852016-05-27 11:59:09 -0700484 std::set<int> selected_levels;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700485 bool dump = false;
Josh Gaof8592a32016-07-26 18:58:27 -0700486 std::string preprocessor_output_path;
487 bool force = false;
Josh Gaobf8a2852016-05-27 11:59:09 -0700488
489 int c;
Josh Gaof8592a32016-07-26 18:58:27 -0700490 while ((c = getopt(argc, argv, "a:r:p:vo:fdhi")) != -1) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700491 default_args = false;
492 switch (c) {
493 case 'a': {
494 char* end;
495 int api_level = strtol(optarg, &end, 10);
496 if (end == optarg || strlen(end) > 0) {
497 usage();
498 }
499
500 if (supported_levels.count(api_level) == 0) {
501 errx(1, "unsupported API level %d", api_level);
502 }
503
504 selected_levels.insert(api_level);
505 break;
506 }
507
508 case 'r': {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700509 Arch arch = arch_from_string(optarg);
510 selected_architectures.insert(arch);
Josh Gaobf8a2852016-05-27 11:59:09 -0700511 break;
512 }
513
514 case 'p': {
515 if (!platform_dir.empty()) {
516 usage();
517 }
518
519 platform_dir = optarg;
520
Josh Gaof8592a32016-07-26 18:58:27 -0700521 if (platform_dir.empty()) {
522 usage();
523 }
524
Josh Gaobf8a2852016-05-27 11:59:09 -0700525 struct stat st;
526 if (stat(platform_dir.c_str(), &st) != 0) {
527 err(1, "failed to stat platform directory '%s'", platform_dir.c_str());
528 }
529 if (!S_ISDIR(st.st_mode)) {
530 errx(1, "'%s' is not a directory", optarg);
531 }
532 break;
533 }
534
535 case 'v':
536 verbose = true;
537 break;
538
Josh Gaof8592a32016-07-26 18:58:27 -0700539 case 'o':
540 if (!preprocessor_output_path.empty()) {
541 usage();
542 }
543 preprocessor_output_path = optarg;
544 if (preprocessor_output_path.empty()) {
545 usage();
546 }
547 break;
548
549 case 'f':
550 force = true;
551 break;
552
Josh Gaobfb6bae2016-07-15 17:25:21 -0700553 case 'd':
554 dump = true;
555 break;
556
Josh Gao62aaf8f2016-06-02 14:27:21 -0700557 case 'h':
558 usage(true);
559 break;
560
Josh Gaobfb6bae2016-07-15 17:25:21 -0700561 case 'i':
562 // Secret option for tests to -include <android/versioning.h>.
563 add_include = true;
564 break;
565
Josh Gaobf8a2852016-05-27 11:59:09 -0700566 default:
567 usage();
568 break;
569 }
570 }
571
Josh Gao9b5af7a2016-06-02 14:29:13 -0700572 if (argc - optind > 2 || optind > argc) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700573 usage();
574 }
575
Josh Gao9b5af7a2016-06-02 14:29:13 -0700576 std::string header_dir;
577 std::string dependency_dir;
578
Josh Gaobfb6bae2016-07-15 17:25:21 -0700579 const char* top = getenv("ANDROID_BUILD_TOP");
580 if (!top && (optind == argc || add_include)) {
581 fprintf(stderr, "versioner: failed to autodetect bionic paths. Is ANDROID_BUILD_TOP set?\n");
582 usage();
583 }
584
Josh Gao9b5af7a2016-06-02 14:29:13 -0700585 if (optind == argc) {
586 // Neither HEADER_PATH nor DEPS_PATH were specified, so try to figure them out.
Josh Gaobfb6bae2016-07-15 17:25:21 -0700587 std::string versioner_dir = to_string(top) + "/bionic/tools/versioner";
Josh Gao9b5af7a2016-06-02 14:29:13 -0700588 header_dir = versioner_dir + "/current";
589 dependency_dir = versioner_dir + "/dependencies";
590 if (platform_dir.empty()) {
591 platform_dir = versioner_dir + "/platforms";
592 }
593 } else {
Josh Gaof8592a32016-07-26 18:58:27 -0700594 // Intentional leak.
595 header_dir = realpath(argv[optind], nullptr);
Josh Gao9b5af7a2016-06-02 14:29:13 -0700596
597 if (argc - optind == 2) {
598 dependency_dir = argv[optind + 1];
599 }
600 }
601
Josh Gaobf8a2852016-05-27 11:59:09 -0700602 if (selected_levels.empty()) {
603 selected_levels = supported_levels;
604 }
605
606 if (selected_architectures.empty()) {
607 selected_architectures = supported_archs;
608 }
609
Josh Gaobf8a2852016-05-27 11:59:09 -0700610
611 struct stat st;
Josh Gao9b5af7a2016-06-02 14:29:13 -0700612 if (stat(header_dir.c_str(), &st) != 0) {
613 err(1, "failed to stat '%s'", header_dir.c_str());
Josh Gaobf8a2852016-05-27 11:59:09 -0700614 } else if (!S_ISDIR(st.st_mode)) {
Josh Gao9b5af7a2016-06-02 14:29:13 -0700615 errx(1, "'%s' is not a directory", header_dir.c_str());
Josh Gaobf8a2852016-05-27 11:59:09 -0700616 }
617
618 std::set<CompilationType> compilation_types;
Josh Gaobf8a2852016-05-27 11:59:09 -0700619 NdkSymbolDatabase symbol_database;
620
621 compilation_types = generateCompilationTypes(selected_architectures, selected_levels);
622
623 // Do this before compiling so that we can early exit if the platforms don't match what we
624 // expect.
625 if (!platform_dir.empty()) {
626 symbol_database = parsePlatforms(compilation_types, platform_dir);
627 }
628
629 bool failed = false;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700630 std::unique_ptr<HeaderDatabase> declaration_database =
631 compileHeaders(compilation_types, header_dir, dependency_dir, &failed);
Josh Gaobf8a2852016-05-27 11:59:09 -0700632
Josh Gaobfb6bae2016-07-15 17:25:21 -0700633 if (dump) {
634 declaration_database->dump(header_dir + "/");
635 } else {
636 if (!sanityCheck(declaration_database.get())) {
637 printf("versioner: sanity check failed\n");
Josh Gaobf8a2852016-05-27 11:59:09 -0700638 failed = true;
639 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700640
Josh Gaobfb6bae2016-07-15 17:25:21 -0700641 if (!platform_dir.empty()) {
642 if (!checkVersions(compilation_types, declaration_database.get(), symbol_database)) {
643 printf("versioner: version check failed\n");
644 failed = true;
645 }
646 }
647 }
Josh Gaof8592a32016-07-26 18:58:27 -0700648
649 if (!preprocessor_output_path.empty() && (force || !failed)) {
650 failed = !preprocessHeaders(preprocessor_output_path, header_dir, declaration_database.get());
651 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700652 return failed;
653}