blob: 74650d53d7c3a41c74391fd08c3ecda1eb240f06 [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 Gaobfb6bae2016-07-15 17:25:21 -0700279// Perform a sanity check on a symbol's declarations, enforcing the following invariants:
280// 1. At most one inline definition of the function exists.
281// 2. All of the availability declarations for a symbol are compatible.
282// If a function is declared as an inline before a certain version, the inline definition
283// should have no version tag.
284// 3. Each availability type must only be present globally or on a per-arch basis.
285// (e.g. __INTRODUCED_IN_ARM(9) __INTRODUCED_IN_X86(10) __DEPRECATED_IN(11) is fine,
286// but not __INTRODUCED_IN(9) __INTRODUCED_IN_X86(10))
287static bool checkSymbol(const Symbol& symbol) {
288 std::string cwd = getWorkingDir() + "/";
289
290 const Declaration* inline_definition = nullptr;
291 for (const auto& decl_it : symbol.declarations) {
292 const Declaration* decl = &decl_it.second;
293 if (decl->is_definition) {
294 if (inline_definition) {
295 fprintf(stderr, "versioner: multiple definitions of symbol %s\n", symbol.name.c_str());
296 symbol.dump(cwd);
297 inline_definition->dump(cwd);
298 return false;
299 }
300
301 inline_definition = decl;
302 }
303
304 DeclarationAvailability availability;
305 if (!decl->calculateAvailability(&availability)) {
306 fprintf(stderr, "versioner: failed to calculate availability for declaration:\n");
Josh Gao566735d2016-08-02 15:07:32 -0700307 decl->dump(cwd, stderr, 2);
Josh Gaobfb6bae2016-07-15 17:25:21 -0700308 return false;
309 }
310
311 if (decl->is_definition && !availability.empty()) {
312 fprintf(stderr, "versioner: inline definition has non-empty versioning information:\n");
Josh Gao566735d2016-08-02 15:07:32 -0700313 decl->dump(cwd, stderr, 2);
Josh Gaobfb6bae2016-07-15 17:25:21 -0700314 return false;
315 }
316 }
317
318 DeclarationAvailability availability;
319 if (!symbol.calculateAvailability(&availability)) {
320 fprintf(stderr, "versioner: inconsistent availability for symbol '%s'\n", symbol.name.c_str());
321 symbol.dump(cwd);
322 return false;
323 }
324
325 // TODO: Check invariant #3.
326 return true;
327}
328
329static bool sanityCheck(const HeaderDatabase* database) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700330 bool error = false;
Josh Gao958f3b32016-06-03 13:44:00 -0700331 std::string cwd = getWorkingDir() + "/";
332
Josh Gaobfb6bae2016-07-15 17:25:21 -0700333 for (const auto& symbol_it : database->symbols) {
334 if (!checkSymbol(symbol_it.second)) {
335 error = true;
Josh Gaobf8a2852016-05-27 11:59:09 -0700336 }
337 }
338 return !error;
339}
340
341// Check that our symbol availability declarations match the actual NDK
342// platform symbol availability.
343static bool checkVersions(const std::set<CompilationType>& types,
Josh Gaobfb6bae2016-07-15 17:25:21 -0700344 const HeaderDatabase* header_database,
Josh Gaobf8a2852016-05-27 11:59:09 -0700345 const NdkSymbolDatabase& symbol_database) {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700346 std::string cwd = getWorkingDir() + "/";
Josh Gaobf8a2852016-05-27 11:59:09 -0700347 bool failed = false;
348
Josh Gaobfb6bae2016-07-15 17:25:21 -0700349 std::map<Arch, std::set<CompilationType>> arch_types;
Josh Gaobf8a2852016-05-27 11:59:09 -0700350 for (const CompilationType& type : types) {
351 arch_types[type.arch].insert(type);
352 }
353
Josh Gaod67dbf02016-06-02 15:21:14 -0700354 std::set<std::string> completely_unavailable;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700355 std::map<std::string, std::set<CompilationType>> missing_availability;
356 std::map<std::string, std::set<CompilationType>> extra_availability;
Josh Gaod67dbf02016-06-02 15:21:14 -0700357
Josh Gaobfb6bae2016-07-15 17:25:21 -0700358 for (const auto& symbol_it : header_database->symbols) {
359 const auto& symbol_name = symbol_it.first;
360 DeclarationAvailability symbol_availability;
Josh Gaobf8a2852016-05-27 11:59:09 -0700361
Josh Gaobfb6bae2016-07-15 17:25:21 -0700362 if (!symbol_it.second.calculateAvailability(&symbol_availability)) {
363 errx(1, "failed to calculate symbol availability");
364 }
365
366 const auto platform_availability_it = symbol_database.find(symbol_name);
Josh Gaobf8a2852016-05-27 11:59:09 -0700367 if (platform_availability_it == symbol_database.end()) {
Josh Gaod67dbf02016-06-02 15:21:14 -0700368 completely_unavailable.insert(symbol_name);
Josh Gaobf8a2852016-05-27 11:59:09 -0700369 continue;
370 }
371
372 const auto& platform_availability = platform_availability_it->second;
Josh Gaobf8a2852016-05-27 11:59:09 -0700373
374 for (const CompilationType& type : types) {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700375 bool should_be_available = true;
376 const auto& global_availability = symbol_availability.global_availability;
377 const auto& arch_availability = symbol_availability.arch_availability[type.arch];
378 if (global_availability.introduced != 0 && global_availability.introduced > type.api_level) {
379 should_be_available = false;
380 }
381
382 if (arch_availability.introduced != 0 && arch_availability.introduced > type.api_level) {
383 should_be_available = false;
384 }
385
386 if (global_availability.obsoleted != 0 && global_availability.obsoleted <= type.api_level) {
387 should_be_available = false;
388 }
389
390 if (arch_availability.obsoleted != 0 && arch_availability.obsoleted <= type.api_level) {
391 should_be_available = false;
392 }
393
394 if (arch_availability.future) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700395 continue;
396 }
397
Josh Gaobfb6bae2016-07-15 17:25:21 -0700398 // The function declaration might be (validly) missing for the given CompilationType.
399 if (!symbol_it.second.hasDeclaration(type)) {
400 should_be_available = false;
401 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700402
Josh Gaobfb6bae2016-07-15 17:25:21 -0700403 bool is_available = platform_availability.count(type);
Josh Gaobf8a2852016-05-27 11:59:09 -0700404
Josh Gaobfb6bae2016-07-15 17:25:21 -0700405 if (should_be_available != is_available) {
406 if (is_available) {
407 extra_availability[symbol_name].insert(type);
Josh Gaobf8a2852016-05-27 11:59:09 -0700408 } else {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700409 missing_availability[symbol_name].insert(type);
Josh Gaobf8a2852016-05-27 11:59:09 -0700410 }
411 }
412 }
Josh Gaobfb6bae2016-07-15 17:25:21 -0700413 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700414
Josh Gaobfb6bae2016-07-15 17:25:21 -0700415 for (const auto& it : symbol_database) {
416 const std::string& symbol_name = it.first;
Josh Gaobf8a2852016-05-27 11:59:09 -0700417
Josh Gaobfb6bae2016-07-15 17:25:21 -0700418 bool symbol_error = false;
419 auto missing_it = missing_availability.find(symbol_name);
420 if (missing_it != missing_availability.end()) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700421 printf("%s: declaration marked available but symbol missing in [%s]\n", symbol_name.c_str(),
Josh Gaobfb6bae2016-07-15 17:25:21 -0700422 Join(missing_it->second, ", ").c_str());
423 symbol_error = true;
Josh Gaobf8a2852016-05-27 11:59:09 -0700424 failed = true;
425 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700426
Josh Gaobfb6bae2016-07-15 17:25:21 -0700427 if (verbose) {
428 auto extra_it = extra_availability.find(symbol_name);
429 if (extra_it != extra_availability.end()) {
430 printf("%s: declaration marked unavailable but symbol available in [%s]\n",
431 symbol_name.c_str(), Join(extra_it->second, ", ").c_str());
432 symbol_error = true;
433 failed = true;
Josh Gao958f3b32016-06-03 13:44:00 -0700434 }
435 }
436
Josh Gaobfb6bae2016-07-15 17:25:21 -0700437 if (symbol_error) {
438 auto symbol_it = header_database->symbols.find(symbol_name);
439 if (symbol_it == header_database->symbols.end()) {
440 errx(1, "failed to find symbol in header database");
441 }
442 symbol_it->second.dump(cwd);
Josh Gaod67dbf02016-06-02 15:21:14 -0700443 }
Josh Gaod67dbf02016-06-02 15:21:14 -0700444 }
445
Josh Gaobfb6bae2016-07-15 17:25:21 -0700446 // TODO: Verify that function/variable declarations are actually function/variable symbols.
Josh Gaobf8a2852016-05-27 11:59:09 -0700447 return !failed;
448}
449
Josh Gao62aaf8f2016-06-02 14:27:21 -0700450static void usage(bool help = false) {
451 fprintf(stderr, "Usage: versioner [OPTION]... [HEADER_PATH] [DEPS_PATH]\n");
452 if (!help) {
453 printf("Try 'versioner -h' for more information.\n");
454 exit(1);
455 } else {
456 fprintf(stderr, "Version headers at HEADER_PATH, with DEPS_PATH/ARCH/* on the include path\n");
Josh Gao9b5af7a2016-06-02 14:29:13 -0700457 fprintf(stderr, "Autodetects paths if HEADER_PATH and DEPS_PATH are not specified\n");
Josh Gao62aaf8f2016-06-02 14:27:21 -0700458 fprintf(stderr, "\n");
459 fprintf(stderr, "Target specification (defaults to all):\n");
460 fprintf(stderr, " -a API_LEVEL\tbuild with specified API level (can be repeated)\n");
461 fprintf(stderr, " \t\tvalid levels are %s\n", Join(supported_levels).c_str());
462 fprintf(stderr, " -r ARCH\tbuild with specified architecture (can be repeated)\n");
463 fprintf(stderr, " \t\tvalid architectures are %s\n", Join(supported_archs).c_str());
464 fprintf(stderr, "\n");
465 fprintf(stderr, "Validation:\n");
466 fprintf(stderr, " -p PATH\tcompare against NDK platform at PATH\n");
467 fprintf(stderr, " -v\t\tenable verbose warnings\n");
468 fprintf(stderr, "\n");
Josh Gaof8592a32016-07-26 18:58:27 -0700469 fprintf(stderr, "Preprocessing:\n");
470 fprintf(stderr, " -o PATH\tpreprocess header files and emit them at PATH\n");
471 fprintf(stderr, " -f\tpreprocess header files even if validation fails\n");
472 fprintf(stderr, "\n");
Josh Gao62aaf8f2016-06-02 14:27:21 -0700473 fprintf(stderr, "Miscellaneous:\n");
Josh Gaobfb6bae2016-07-15 17:25:21 -0700474 fprintf(stderr, " -d\t\tdump function availability\n");
Josh Gao62aaf8f2016-06-02 14:27:21 -0700475 fprintf(stderr, " -h\t\tdisplay this message\n");
476 exit(0);
477 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700478}
479
480int main(int argc, char** argv) {
481 std::string cwd = getWorkingDir() + "/";
482 bool default_args = true;
483 std::string platform_dir;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700484 std::set<Arch> selected_architectures;
Josh Gaobf8a2852016-05-27 11:59:09 -0700485 std::set<int> selected_levels;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700486 bool dump = false;
Josh Gaof8592a32016-07-26 18:58:27 -0700487 std::string preprocessor_output_path;
488 bool force = false;
Josh Gaobf8a2852016-05-27 11:59:09 -0700489
490 int c;
Josh Gaof8592a32016-07-26 18:58:27 -0700491 while ((c = getopt(argc, argv, "a:r:p:vo:fdhi")) != -1) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700492 default_args = false;
493 switch (c) {
494 case 'a': {
495 char* end;
496 int api_level = strtol(optarg, &end, 10);
497 if (end == optarg || strlen(end) > 0) {
498 usage();
499 }
500
501 if (supported_levels.count(api_level) == 0) {
502 errx(1, "unsupported API level %d", api_level);
503 }
504
505 selected_levels.insert(api_level);
506 break;
507 }
508
509 case 'r': {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700510 Arch arch = arch_from_string(optarg);
511 selected_architectures.insert(arch);
Josh Gaobf8a2852016-05-27 11:59:09 -0700512 break;
513 }
514
515 case 'p': {
516 if (!platform_dir.empty()) {
517 usage();
518 }
519
520 platform_dir = optarg;
521
Josh Gaof8592a32016-07-26 18:58:27 -0700522 if (platform_dir.empty()) {
523 usage();
524 }
525
Josh Gaobf8a2852016-05-27 11:59:09 -0700526 struct stat st;
527 if (stat(platform_dir.c_str(), &st) != 0) {
528 err(1, "failed to stat platform directory '%s'", platform_dir.c_str());
529 }
530 if (!S_ISDIR(st.st_mode)) {
531 errx(1, "'%s' is not a directory", optarg);
532 }
533 break;
534 }
535
536 case 'v':
537 verbose = true;
538 break;
539
Josh Gaof8592a32016-07-26 18:58:27 -0700540 case 'o':
541 if (!preprocessor_output_path.empty()) {
542 usage();
543 }
544 preprocessor_output_path = optarg;
545 if (preprocessor_output_path.empty()) {
546 usage();
547 }
548 break;
549
550 case 'f':
551 force = true;
552 break;
553
Josh Gaobfb6bae2016-07-15 17:25:21 -0700554 case 'd':
555 dump = true;
556 break;
557
Josh Gao62aaf8f2016-06-02 14:27:21 -0700558 case 'h':
559 usage(true);
560 break;
561
Josh Gaobfb6bae2016-07-15 17:25:21 -0700562 case 'i':
563 // Secret option for tests to -include <android/versioning.h>.
564 add_include = true;
565 break;
566
Josh Gaobf8a2852016-05-27 11:59:09 -0700567 default:
568 usage();
569 break;
570 }
571 }
572
Josh Gao9b5af7a2016-06-02 14:29:13 -0700573 if (argc - optind > 2 || optind > argc) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700574 usage();
575 }
576
Josh Gao9b5af7a2016-06-02 14:29:13 -0700577 std::string header_dir;
578 std::string dependency_dir;
579
Josh Gaobfb6bae2016-07-15 17:25:21 -0700580 const char* top = getenv("ANDROID_BUILD_TOP");
581 if (!top && (optind == argc || add_include)) {
582 fprintf(stderr, "versioner: failed to autodetect bionic paths. Is ANDROID_BUILD_TOP set?\n");
583 usage();
584 }
585
Josh Gao9b5af7a2016-06-02 14:29:13 -0700586 if (optind == argc) {
587 // Neither HEADER_PATH nor DEPS_PATH were specified, so try to figure them out.
Josh Gaobfb6bae2016-07-15 17:25:21 -0700588 std::string versioner_dir = to_string(top) + "/bionic/tools/versioner";
Josh Gao9b5af7a2016-06-02 14:29:13 -0700589 header_dir = versioner_dir + "/current";
590 dependency_dir = versioner_dir + "/dependencies";
591 if (platform_dir.empty()) {
592 platform_dir = versioner_dir + "/platforms";
593 }
594 } else {
Josh Gaof8592a32016-07-26 18:58:27 -0700595 // Intentional leak.
596 header_dir = realpath(argv[optind], nullptr);
Josh Gao9b5af7a2016-06-02 14:29:13 -0700597
598 if (argc - optind == 2) {
599 dependency_dir = argv[optind + 1];
600 }
601 }
602
Josh Gaobf8a2852016-05-27 11:59:09 -0700603 if (selected_levels.empty()) {
604 selected_levels = supported_levels;
605 }
606
607 if (selected_architectures.empty()) {
608 selected_architectures = supported_archs;
609 }
610
Josh Gaobf8a2852016-05-27 11:59:09 -0700611
612 struct stat st;
Josh Gao9b5af7a2016-06-02 14:29:13 -0700613 if (stat(header_dir.c_str(), &st) != 0) {
614 err(1, "failed to stat '%s'", header_dir.c_str());
Josh Gaobf8a2852016-05-27 11:59:09 -0700615 } else if (!S_ISDIR(st.st_mode)) {
Josh Gao9b5af7a2016-06-02 14:29:13 -0700616 errx(1, "'%s' is not a directory", header_dir.c_str());
Josh Gaobf8a2852016-05-27 11:59:09 -0700617 }
618
619 std::set<CompilationType> compilation_types;
Josh Gaobf8a2852016-05-27 11:59:09 -0700620 NdkSymbolDatabase symbol_database;
621
622 compilation_types = generateCompilationTypes(selected_architectures, selected_levels);
623
624 // Do this before compiling so that we can early exit if the platforms don't match what we
625 // expect.
626 if (!platform_dir.empty()) {
627 symbol_database = parsePlatforms(compilation_types, platform_dir);
628 }
629
630 bool failed = false;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700631 std::unique_ptr<HeaderDatabase> declaration_database =
632 compileHeaders(compilation_types, header_dir, dependency_dir, &failed);
Josh Gaobf8a2852016-05-27 11:59:09 -0700633
Josh Gaobfb6bae2016-07-15 17:25:21 -0700634 if (dump) {
635 declaration_database->dump(header_dir + "/");
636 } else {
637 if (!sanityCheck(declaration_database.get())) {
638 printf("versioner: sanity check failed\n");
Josh Gaobf8a2852016-05-27 11:59:09 -0700639 failed = true;
640 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700641
Josh Gaobfb6bae2016-07-15 17:25:21 -0700642 if (!platform_dir.empty()) {
643 if (!checkVersions(compilation_types, declaration_database.get(), symbol_database)) {
644 printf("versioner: version check failed\n");
645 failed = true;
646 }
647 }
648 }
Josh Gaof8592a32016-07-26 18:58:27 -0700649
650 if (!preprocessor_output_path.empty() && (force || !failed)) {
651 failed = !preprocessHeaders(preprocessor_output_path, header_dir, declaration_database.get());
652 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700653 return failed;
654}