blob: 2f0656cd9b6b600198de851b80d7b1218a922e82 [file] [log] [blame]
Josh Gaobf8a2852016-05-27 11:59:09 -07001/*
Elliott Hughesdfb74c52016-10-24 12:53:17 -07002 * Copyright (C) 2016 The Android Open Source Project
Josh Gaobf8a2852016-05-27 11:59:09 -07003 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
Elliott Hughesdfb74c52016-10-24 12:53:17 -07008 * http://www.apache.org/licenses/LICENSE-2.0
Josh Gaobf8a2852016-05-27 11:59:09 -07009 *
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");
Josh Gaoac3e5642016-09-15 14:16:15 -070083 command.push_back("-Wundef");
Josh Gaobb966282016-09-14 14:21:56 -070084 command.push_back("-Wno-unused-macros");
85 command.push_back("-Wno-unused-function");
86 command.push_back("-Wno-unused-variable");
Josh Gaobf8a2852016-05-27 11:59:09 -070087 command.push_back("-Wno-unknown-attributes");
Josh Gaobfb6bae2016-07-15 17:25:21 -070088 command.push_back("-Wno-pragma-once-outside-header");
Josh Gaobf8a2852016-05-27 11:59:09 -070089 command.push_back("-target");
90 command.push_back(arch_targets[type.arch]);
91
Josh Gaobfb6bae2016-07-15 17:25:21 -070092 if (add_include) {
93 const char* top = getenv("ANDROID_BUILD_TOP");
94 std::string header_path = to_string(top) + "/bionic/libc/include/android/versioning.h";
95 command.push_back("-include");
96 command.push_back(std::move(header_path));
97 }
98
Josh Gaoa77b3a92016-08-15 16:39:27 -070099 command.push_back("-D_FILE_OFFSET_BITS="s + std::to_string(type.file_offset_bits));
100
Josh Gaobf8a2852016-05-27 11:59:09 -0700101 return CompileCommand(cwd, filename, command);
102 }
103
104 std::vector<CompileCommand> getAllCompileCommands() const override {
105 std::vector<CompileCommand> commands;
106 for (const std::string& file : headers) {
107 commands.push_back(generateCompileCommand(file));
108 }
109 return commands;
110 }
111
112 std::vector<CompileCommand> getCompileCommands(StringRef file) const override {
113 std::vector<CompileCommand> commands;
114 commands.push_back(generateCompileCommand(file));
115 return commands;
116 }
117
118 std::vector<std::string> getAllFiles() const override {
119 return headers;
120 }
121};
122
123struct CompilationRequirements {
124 std::vector<std::string> headers;
125 std::vector<std::string> dependencies;
126};
127
Josh Gaobfb6bae2016-07-15 17:25:21 -0700128static CompilationRequirements collectRequirements(const Arch& arch, const std::string& header_dir,
Josh Gaobf8a2852016-05-27 11:59:09 -0700129 const std::string& dependency_dir) {
130 std::vector<std::string> headers = collectFiles(header_dir);
131
132 std::vector<std::string> dependencies = { header_dir };
133 if (!dependency_dir.empty()) {
134 auto collect_children = [&dependencies](const std::string& dir_path) {
135 DIR* dir = opendir(dir_path.c_str());
136 if (!dir) {
137 err(1, "failed to open dependency directory '%s'", dir_path.c_str());
138 }
139
140 struct dirent* dent;
141 while ((dent = readdir(dir))) {
142 if (dent->d_name[0] == '.') {
143 continue;
144 }
145
146 // TODO: Resolve symlinks.
147 std::string dependency = dir_path + "/" + dent->d_name;
148
149 struct stat st;
150 if (stat(dependency.c_str(), &st) != 0) {
151 err(1, "failed to stat dependency '%s'", dependency.c_str());
152 }
153
154 if (!S_ISDIR(st.st_mode)) {
155 errx(1, "'%s' is not a directory", dependency.c_str());
156 }
157
158 dependencies.push_back(dependency);
159 }
160
161 closedir(dir);
162 };
163
164 collect_children(dependency_dir + "/common");
Josh Gaobfb6bae2016-07-15 17:25:21 -0700165 collect_children(dependency_dir + "/" + to_string(arch));
Josh Gaobf8a2852016-05-27 11:59:09 -0700166 }
167
168 auto new_end = std::remove_if(headers.begin(), headers.end(), [&arch](llvm::StringRef header) {
169 for (const auto& it : header_blacklist) {
170 if (it.second.find(arch) == it.second.end()) {
171 continue;
172 }
173
174 if (header.endswith("/" + it.first)) {
175 return true;
176 }
177 }
178 return false;
179 });
180
181 headers.erase(new_end, headers.end());
182
183 CompilationRequirements result = { .headers = headers, .dependencies = dependencies };
184 return result;
185}
186
Josh Gaobfb6bae2016-07-15 17:25:21 -0700187static std::set<CompilationType> generateCompilationTypes(const std::set<Arch> selected_architectures,
188 const std::set<int>& selected_levels) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700189 std::set<CompilationType> result;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700190 for (const auto& arch : selected_architectures) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700191 int min_api = arch_min_api[arch];
192 for (int api_level : selected_levels) {
193 if (api_level < min_api) {
194 continue;
195 }
Josh Gaoa77b3a92016-08-15 16:39:27 -0700196
197 for (int file_offset_bits : { 32, 64 }) {
198 CompilationType type = {
199 .arch = arch, .api_level = api_level, .file_offset_bits = file_offset_bits
200 };
201 result.insert(type);
202 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700203 }
204 }
205 return result;
206}
207
Josh Gaobfb6bae2016-07-15 17:25:21 -0700208static std::unique_ptr<HeaderDatabase> compileHeaders(const std::set<CompilationType>& types,
209 const std::string& header_dir,
210 const std::string& dependency_dir,
211 bool* failed) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700212 constexpr size_t thread_count = 8;
213 size_t threads_created = 0;
214 std::mutex mutex;
215 std::vector<std::thread> threads(thread_count);
216
217 std::map<CompilationType, HeaderDatabase> header_databases;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700218 std::unordered_map<Arch, CompilationRequirements> requirements;
Josh Gaobf8a2852016-05-27 11:59:09 -0700219
220 std::string cwd = getWorkingDir();
221 bool errors = false;
222
223 for (const auto& type : types) {
224 if (requirements.count(type.arch) == 0) {
225 requirements[type.arch] = collectRequirements(type.arch, header_dir, dependency_dir);
226 }
227 }
228
Josh Gaobfb6bae2016-07-15 17:25:21 -0700229 auto result = std::make_unique<HeaderDatabase>();
Josh Gaobf8a2852016-05-27 11:59:09 -0700230 for (const auto& type : types) {
231 size_t thread_id = threads_created++;
232 if (thread_id >= thread_count) {
233 thread_id = thread_id % thread_count;
234 threads[thread_id].join();
235 }
236
237 threads[thread_id] = std::thread(
238 [&](CompilationType type) {
239 const auto& req = requirements[type.arch];
240
Josh Gaobf8a2852016-05-27 11:59:09 -0700241 HeaderCompilationDatabase compilation_database(type, cwd, req.headers, req.dependencies);
242
243 ClangTool tool(compilation_database, req.headers);
244
245 clang::DiagnosticOptions diagnostic_options;
246 std::vector<std::unique_ptr<ASTUnit>> asts;
247 tool.buildASTs(asts);
248 for (const auto& ast : asts) {
249 clang::DiagnosticsEngine& diagnostics_engine = ast->getDiagnostics();
250 if (diagnostics_engine.getNumWarnings() || diagnostics_engine.hasErrorOccurred()) {
251 std::unique_lock<std::mutex> l(mutex);
252 errors = true;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700253 printf("versioner: compilation failure for %s in %s\n", to_string(type).c_str(),
Josh Gaobf8a2852016-05-27 11:59:09 -0700254 ast->getOriginalSourceFileName().str().c_str());
255 }
256
Josh Gaobfb6bae2016-07-15 17:25:21 -0700257 result->parseAST(type, ast.get());
Josh Gaobf8a2852016-05-27 11:59:09 -0700258 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700259 },
260 type);
261 }
262
263 if (threads_created < thread_count) {
264 threads.resize(threads_created);
265 }
266
267 for (auto& thread : threads) {
268 thread.join();
269 }
270
271 if (errors) {
272 printf("versioner: compilation generated warnings or errors\n");
273 *failed = errors;
274 }
275
Josh Gaobfb6bae2016-07-15 17:25:21 -0700276 return result;
Josh Gaobf8a2852016-05-27 11:59:09 -0700277}
278
Josh Gao1a176de2016-11-04 13:15:11 -0700279static std::set<CompilationType> getCompilationTypes(const Declaration* decl) {
280 std::set<CompilationType> result;
281 for (const auto& it : decl->availability) {
282 result.insert(it.first);
283 }
284 return result;
285}
286
287template<typename T>
288static std::vector<T> Intersection(const std::set<T>& a, const std::set<T>& b) {
289 std::vector<T> intersection;
290 std::set_intersection(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(intersection));
291 return intersection;
292}
293
Josh Gaobfb6bae2016-07-15 17:25:21 -0700294// Perform a sanity check on a symbol's declarations, enforcing the following invariants:
295// 1. At most one inline definition of the function exists.
296// 2. All of the availability declarations for a symbol are compatible.
297// If a function is declared as an inline before a certain version, the inline definition
298// should have no version tag.
299// 3. Each availability type must only be present globally or on a per-arch basis.
300// (e.g. __INTRODUCED_IN_ARM(9) __INTRODUCED_IN_X86(10) __DEPRECATED_IN(11) is fine,
301// but not __INTRODUCED_IN(9) __INTRODUCED_IN_X86(10))
302static bool checkSymbol(const Symbol& symbol) {
303 std::string cwd = getWorkingDir() + "/";
304
Josh Gao1a176de2016-11-04 13:15:11 -0700305 std::unordered_map<const Declaration*, std::set<CompilationType>> inline_definitions;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700306 for (const auto& decl_it : symbol.declarations) {
307 const Declaration* decl = &decl_it.second;
308 if (decl->is_definition) {
Josh Gao1a176de2016-11-04 13:15:11 -0700309 std::set<CompilationType> compilation_types = getCompilationTypes(decl);
310 for (const auto& inline_def_it : inline_definitions) {
311 auto intersection = Intersection(compilation_types, inline_def_it.second);
312 if (!intersection.empty()) {
313 fprintf(stderr, "versioner: conflicting inline definitions:\n");
314 fprintf(stderr, " declarations visible in: %s\n", Join(intersection, ", ").c_str());
315 decl->dump(cwd, stderr, 4);
316 inline_def_it.first->dump(cwd, stderr, 4);
317 return false;
318 }
Josh Gaobfb6bae2016-07-15 17:25:21 -0700319 }
320
Josh Gao1a176de2016-11-04 13:15:11 -0700321 inline_definitions[decl] = std::move(compilation_types);
Josh Gaobfb6bae2016-07-15 17:25:21 -0700322 }
323
324 DeclarationAvailability availability;
325 if (!decl->calculateAvailability(&availability)) {
326 fprintf(stderr, "versioner: failed to calculate availability for declaration:\n");
Josh Gao566735d2016-08-02 15:07:32 -0700327 decl->dump(cwd, stderr, 2);
Josh Gaobfb6bae2016-07-15 17:25:21 -0700328 return false;
329 }
330
331 if (decl->is_definition && !availability.empty()) {
332 fprintf(stderr, "versioner: inline definition has non-empty versioning information:\n");
Josh Gao566735d2016-08-02 15:07:32 -0700333 decl->dump(cwd, stderr, 2);
Josh Gaobfb6bae2016-07-15 17:25:21 -0700334 return false;
335 }
336 }
337
338 DeclarationAvailability availability;
339 if (!symbol.calculateAvailability(&availability)) {
340 fprintf(stderr, "versioner: inconsistent availability for symbol '%s'\n", symbol.name.c_str());
341 symbol.dump(cwd);
342 return false;
343 }
344
345 // TODO: Check invariant #3.
346 return true;
347}
348
349static bool sanityCheck(const HeaderDatabase* database) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700350 bool error = false;
Josh Gao958f3b32016-06-03 13:44:00 -0700351 std::string cwd = getWorkingDir() + "/";
352
Josh Gaobfb6bae2016-07-15 17:25:21 -0700353 for (const auto& symbol_it : database->symbols) {
354 if (!checkSymbol(symbol_it.second)) {
355 error = true;
Josh Gaobf8a2852016-05-27 11:59:09 -0700356 }
357 }
358 return !error;
359}
360
361// Check that our symbol availability declarations match the actual NDK
362// platform symbol availability.
363static bool checkVersions(const std::set<CompilationType>& types,
Josh Gaobfb6bae2016-07-15 17:25:21 -0700364 const HeaderDatabase* header_database,
Josh Gaobf8a2852016-05-27 11:59:09 -0700365 const NdkSymbolDatabase& symbol_database) {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700366 std::string cwd = getWorkingDir() + "/";
Josh Gaobf8a2852016-05-27 11:59:09 -0700367 bool failed = false;
368
Josh Gaobfb6bae2016-07-15 17:25:21 -0700369 std::map<Arch, std::set<CompilationType>> arch_types;
Josh Gaobf8a2852016-05-27 11:59:09 -0700370 for (const CompilationType& type : types) {
371 arch_types[type.arch].insert(type);
372 }
373
Josh Gaod67dbf02016-06-02 15:21:14 -0700374 std::set<std::string> completely_unavailable;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700375 std::map<std::string, std::set<CompilationType>> missing_availability;
376 std::map<std::string, std::set<CompilationType>> extra_availability;
Josh Gaod67dbf02016-06-02 15:21:14 -0700377
Josh Gaobfb6bae2016-07-15 17:25:21 -0700378 for (const auto& symbol_it : header_database->symbols) {
379 const auto& symbol_name = symbol_it.first;
380 DeclarationAvailability symbol_availability;
Josh Gaobf8a2852016-05-27 11:59:09 -0700381
Josh Gaobfb6bae2016-07-15 17:25:21 -0700382 if (!symbol_it.second.calculateAvailability(&symbol_availability)) {
383 errx(1, "failed to calculate symbol availability");
384 }
385
386 const auto platform_availability_it = symbol_database.find(symbol_name);
Josh Gaobf8a2852016-05-27 11:59:09 -0700387 if (platform_availability_it == symbol_database.end()) {
Josh Gaod67dbf02016-06-02 15:21:14 -0700388 completely_unavailable.insert(symbol_name);
Josh Gaobf8a2852016-05-27 11:59:09 -0700389 continue;
390 }
391
392 const auto& platform_availability = platform_availability_it->second;
Josh Gaobf8a2852016-05-27 11:59:09 -0700393
394 for (const CompilationType& type : types) {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700395 bool should_be_available = true;
396 const auto& global_availability = symbol_availability.global_availability;
397 const auto& arch_availability = symbol_availability.arch_availability[type.arch];
398 if (global_availability.introduced != 0 && global_availability.introduced > type.api_level) {
399 should_be_available = false;
400 }
401
402 if (arch_availability.introduced != 0 && arch_availability.introduced > type.api_level) {
403 should_be_available = false;
404 }
405
406 if (global_availability.obsoleted != 0 && global_availability.obsoleted <= type.api_level) {
407 should_be_available = false;
408 }
409
410 if (arch_availability.obsoleted != 0 && arch_availability.obsoleted <= type.api_level) {
411 should_be_available = false;
412 }
413
414 if (arch_availability.future) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700415 continue;
416 }
417
Josh Gaobfb6bae2016-07-15 17:25:21 -0700418 // The function declaration might be (validly) missing for the given CompilationType.
419 if (!symbol_it.second.hasDeclaration(type)) {
420 should_be_available = false;
421 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700422
Josh Gaobfb6bae2016-07-15 17:25:21 -0700423 bool is_available = platform_availability.count(type);
Josh Gaobf8a2852016-05-27 11:59:09 -0700424
Josh Gaobfb6bae2016-07-15 17:25:21 -0700425 if (should_be_available != is_available) {
426 if (is_available) {
427 extra_availability[symbol_name].insert(type);
Josh Gaobf8a2852016-05-27 11:59:09 -0700428 } else {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700429 missing_availability[symbol_name].insert(type);
Josh Gaobf8a2852016-05-27 11:59:09 -0700430 }
431 }
432 }
Josh Gaobfb6bae2016-07-15 17:25:21 -0700433 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700434
Josh Gaobfb6bae2016-07-15 17:25:21 -0700435 for (const auto& it : symbol_database) {
436 const std::string& symbol_name = it.first;
Josh Gaobf8a2852016-05-27 11:59:09 -0700437
Josh Gaobfb6bae2016-07-15 17:25:21 -0700438 bool symbol_error = false;
439 auto missing_it = missing_availability.find(symbol_name);
440 if (missing_it != missing_availability.end()) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700441 printf("%s: declaration marked available but symbol missing in [%s]\n", symbol_name.c_str(),
Josh Gaobfb6bae2016-07-15 17:25:21 -0700442 Join(missing_it->second, ", ").c_str());
443 symbol_error = true;
Josh Gaobf8a2852016-05-27 11:59:09 -0700444 failed = true;
445 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700446
Josh Gaobfb6bae2016-07-15 17:25:21 -0700447 if (verbose) {
448 auto extra_it = extra_availability.find(symbol_name);
449 if (extra_it != extra_availability.end()) {
450 printf("%s: declaration marked unavailable but symbol available in [%s]\n",
451 symbol_name.c_str(), Join(extra_it->second, ", ").c_str());
452 symbol_error = true;
453 failed = true;
Josh Gao958f3b32016-06-03 13:44:00 -0700454 }
455 }
456
Josh Gaobfb6bae2016-07-15 17:25:21 -0700457 if (symbol_error) {
458 auto symbol_it = header_database->symbols.find(symbol_name);
459 if (symbol_it == header_database->symbols.end()) {
460 errx(1, "failed to find symbol in header database");
461 }
462 symbol_it->second.dump(cwd);
Josh Gaod67dbf02016-06-02 15:21:14 -0700463 }
Josh Gaod67dbf02016-06-02 15:21:14 -0700464 }
465
Josh Gaobfb6bae2016-07-15 17:25:21 -0700466 // TODO: Verify that function/variable declarations are actually function/variable symbols.
Josh Gaobf8a2852016-05-27 11:59:09 -0700467 return !failed;
468}
469
Josh Gao62aaf8f2016-06-02 14:27:21 -0700470static void usage(bool help = false) {
471 fprintf(stderr, "Usage: versioner [OPTION]... [HEADER_PATH] [DEPS_PATH]\n");
472 if (!help) {
473 printf("Try 'versioner -h' for more information.\n");
474 exit(1);
475 } else {
476 fprintf(stderr, "Version headers at HEADER_PATH, with DEPS_PATH/ARCH/* on the include path\n");
Josh Gao9b5af7a2016-06-02 14:29:13 -0700477 fprintf(stderr, "Autodetects paths if HEADER_PATH and DEPS_PATH are not specified\n");
Josh Gao62aaf8f2016-06-02 14:27:21 -0700478 fprintf(stderr, "\n");
479 fprintf(stderr, "Target specification (defaults to all):\n");
480 fprintf(stderr, " -a API_LEVEL\tbuild with specified API level (can be repeated)\n");
481 fprintf(stderr, " \t\tvalid levels are %s\n", Join(supported_levels).c_str());
482 fprintf(stderr, " -r ARCH\tbuild with specified architecture (can be repeated)\n");
483 fprintf(stderr, " \t\tvalid architectures are %s\n", Join(supported_archs).c_str());
484 fprintf(stderr, "\n");
485 fprintf(stderr, "Validation:\n");
486 fprintf(stderr, " -p PATH\tcompare against NDK platform at PATH\n");
487 fprintf(stderr, " -v\t\tenable verbose warnings\n");
488 fprintf(stderr, "\n");
Josh Gaof8592a32016-07-26 18:58:27 -0700489 fprintf(stderr, "Preprocessing:\n");
490 fprintf(stderr, " -o PATH\tpreprocess header files and emit them at PATH\n");
491 fprintf(stderr, " -f\tpreprocess header files even if validation fails\n");
492 fprintf(stderr, "\n");
Josh Gao62aaf8f2016-06-02 14:27:21 -0700493 fprintf(stderr, "Miscellaneous:\n");
Josh Gaobfb6bae2016-07-15 17:25:21 -0700494 fprintf(stderr, " -d\t\tdump function availability\n");
Josh Gao62aaf8f2016-06-02 14:27:21 -0700495 fprintf(stderr, " -h\t\tdisplay this message\n");
496 exit(0);
497 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700498}
499
500int main(int argc, char** argv) {
501 std::string cwd = getWorkingDir() + "/";
502 bool default_args = true;
503 std::string platform_dir;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700504 std::set<Arch> selected_architectures;
Josh Gaobf8a2852016-05-27 11:59:09 -0700505 std::set<int> selected_levels;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700506 bool dump = false;
Josh Gaof8592a32016-07-26 18:58:27 -0700507 std::string preprocessor_output_path;
508 bool force = false;
Josh Gaobf8a2852016-05-27 11:59:09 -0700509
510 int c;
Josh Gaof8592a32016-07-26 18:58:27 -0700511 while ((c = getopt(argc, argv, "a:r:p:vo:fdhi")) != -1) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700512 default_args = false;
513 switch (c) {
514 case 'a': {
515 char* end;
516 int api_level = strtol(optarg, &end, 10);
517 if (end == optarg || strlen(end) > 0) {
518 usage();
519 }
520
521 if (supported_levels.count(api_level) == 0) {
522 errx(1, "unsupported API level %d", api_level);
523 }
524
525 selected_levels.insert(api_level);
526 break;
527 }
528
529 case 'r': {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700530 Arch arch = arch_from_string(optarg);
531 selected_architectures.insert(arch);
Josh Gaobf8a2852016-05-27 11:59:09 -0700532 break;
533 }
534
535 case 'p': {
536 if (!platform_dir.empty()) {
537 usage();
538 }
539
540 platform_dir = optarg;
541
Josh Gaof8592a32016-07-26 18:58:27 -0700542 if (platform_dir.empty()) {
543 usage();
544 }
545
Josh Gaobf8a2852016-05-27 11:59:09 -0700546 struct stat st;
547 if (stat(platform_dir.c_str(), &st) != 0) {
548 err(1, "failed to stat platform directory '%s'", platform_dir.c_str());
549 }
550 if (!S_ISDIR(st.st_mode)) {
551 errx(1, "'%s' is not a directory", optarg);
552 }
553 break;
554 }
555
556 case 'v':
557 verbose = true;
558 break;
559
Josh Gaof8592a32016-07-26 18:58:27 -0700560 case 'o':
561 if (!preprocessor_output_path.empty()) {
562 usage();
563 }
564 preprocessor_output_path = optarg;
565 if (preprocessor_output_path.empty()) {
566 usage();
567 }
568 break;
569
570 case 'f':
571 force = true;
572 break;
573
Josh Gaobfb6bae2016-07-15 17:25:21 -0700574 case 'd':
575 dump = true;
576 break;
577
Josh Gao62aaf8f2016-06-02 14:27:21 -0700578 case 'h':
579 usage(true);
580 break;
581
Josh Gaobfb6bae2016-07-15 17:25:21 -0700582 case 'i':
583 // Secret option for tests to -include <android/versioning.h>.
584 add_include = true;
585 break;
586
Josh Gaobf8a2852016-05-27 11:59:09 -0700587 default:
588 usage();
589 break;
590 }
591 }
592
Josh Gao9b5af7a2016-06-02 14:29:13 -0700593 if (argc - optind > 2 || optind > argc) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700594 usage();
595 }
596
Josh Gao9b5af7a2016-06-02 14:29:13 -0700597 std::string header_dir;
598 std::string dependency_dir;
599
Josh Gaobfb6bae2016-07-15 17:25:21 -0700600 const char* top = getenv("ANDROID_BUILD_TOP");
601 if (!top && (optind == argc || add_include)) {
602 fprintf(stderr, "versioner: failed to autodetect bionic paths. Is ANDROID_BUILD_TOP set?\n");
603 usage();
604 }
605
Josh Gao9b5af7a2016-06-02 14:29:13 -0700606 if (optind == argc) {
607 // Neither HEADER_PATH nor DEPS_PATH were specified, so try to figure them out.
Josh Gaobfb6bae2016-07-15 17:25:21 -0700608 std::string versioner_dir = to_string(top) + "/bionic/tools/versioner";
Josh Gao9b5af7a2016-06-02 14:29:13 -0700609 header_dir = versioner_dir + "/current";
610 dependency_dir = versioner_dir + "/dependencies";
611 if (platform_dir.empty()) {
612 platform_dir = versioner_dir + "/platforms";
613 }
614 } else {
Josh Gaof8592a32016-07-26 18:58:27 -0700615 // Intentional leak.
616 header_dir = realpath(argv[optind], nullptr);
Josh Gao9b5af7a2016-06-02 14:29:13 -0700617
618 if (argc - optind == 2) {
619 dependency_dir = argv[optind + 1];
620 }
621 }
622
Josh Gaobf8a2852016-05-27 11:59:09 -0700623 if (selected_levels.empty()) {
624 selected_levels = supported_levels;
625 }
626
627 if (selected_architectures.empty()) {
628 selected_architectures = supported_archs;
629 }
630
Josh Gaobf8a2852016-05-27 11:59:09 -0700631
632 struct stat st;
Josh Gao9b5af7a2016-06-02 14:29:13 -0700633 if (stat(header_dir.c_str(), &st) != 0) {
634 err(1, "failed to stat '%s'", header_dir.c_str());
Josh Gaobf8a2852016-05-27 11:59:09 -0700635 } else if (!S_ISDIR(st.st_mode)) {
Josh Gao9b5af7a2016-06-02 14:29:13 -0700636 errx(1, "'%s' is not a directory", header_dir.c_str());
Josh Gaobf8a2852016-05-27 11:59:09 -0700637 }
638
639 std::set<CompilationType> compilation_types;
Josh Gaobf8a2852016-05-27 11:59:09 -0700640 NdkSymbolDatabase symbol_database;
641
642 compilation_types = generateCompilationTypes(selected_architectures, selected_levels);
643
644 // Do this before compiling so that we can early exit if the platforms don't match what we
645 // expect.
646 if (!platform_dir.empty()) {
647 symbol_database = parsePlatforms(compilation_types, platform_dir);
648 }
649
650 bool failed = false;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700651 std::unique_ptr<HeaderDatabase> declaration_database =
652 compileHeaders(compilation_types, header_dir, dependency_dir, &failed);
Josh Gaobf8a2852016-05-27 11:59:09 -0700653
Josh Gaobfb6bae2016-07-15 17:25:21 -0700654 if (dump) {
655 declaration_database->dump(header_dir + "/");
656 } else {
657 if (!sanityCheck(declaration_database.get())) {
658 printf("versioner: sanity check failed\n");
Josh Gaobf8a2852016-05-27 11:59:09 -0700659 failed = true;
660 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700661
Josh Gaobfb6bae2016-07-15 17:25:21 -0700662 if (!platform_dir.empty()) {
663 if (!checkVersions(compilation_types, declaration_database.get(), symbol_database)) {
664 printf("versioner: version check failed\n");
665 failed = true;
666 }
667 }
668 }
Josh Gaof8592a32016-07-26 18:58:27 -0700669
670 if (!preprocessor_output_path.empty() && (force || !failed)) {
671 failed = !preprocessHeaders(preprocessor_output_path, header_dir, declaration_database.get());
672 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700673 return failed;
674}