blob: 807ef16087dd3733bec6d7a2e5f25e3ed2099f61 [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>
Josh Gao16016df2016-11-07 18:27:16 -080026#include <functional>
Josh Gaobf8a2852016-05-27 11:59:09 -070027#include <iostream>
28#include <map>
29#include <memory>
30#include <set>
31#include <sstream>
32#include <string>
33#include <thread>
34#include <unordered_map>
35#include <vector>
36
Josh Gao16016df2016-11-07 18:27:16 -080037#include <clang/AST/ASTConsumer.h>
38#include <clang/Basic/TargetInfo.h>
39#include <clang/Driver/Compilation.h>
40#include <clang/Driver/Driver.h>
41#include <clang/Frontend/CompilerInstance.h>
42#include <clang/Frontend/CompilerInvocation.h>
43#include <clang/Frontend/FrontendAction.h>
44#include <clang/Frontend/FrontendActions.h>
Josh Gaobf8a2852016-05-27 11:59:09 -070045#include <clang/Frontend/TextDiagnosticPrinter.h>
Josh Gao16016df2016-11-07 18:27:16 -080046#include <clang/Frontend/Utils.h>
47#include <clang/FrontendTool/Utils.h>
Josh Gaobf8a2852016-05-27 11:59:09 -070048#include <clang/Tooling/Tooling.h>
49#include <llvm/ADT/StringRef.h>
50
Josh Gao16016df2016-11-07 18:27:16 -080051#include <android-base/parseint.h>
52
Josh Gaobfb6bae2016-07-15 17:25:21 -070053#include "Arch.h"
Josh Gaobf8a2852016-05-27 11:59:09 -070054#include "DeclarationDatabase.h"
Josh Gaof8592a32016-07-26 18:58:27 -070055#include "Preprocessor.h"
Josh Gaobf8a2852016-05-27 11:59:09 -070056#include "SymbolDatabase.h"
57#include "Utils.h"
58#include "versioner.h"
59
60using namespace std::string_literals;
61using namespace clang;
Josh Gao16016df2016-11-07 18:27:16 -080062using namespace tooling;
Josh Gaobf8a2852016-05-27 11:59:09 -070063
64bool verbose;
Josh Gaobfb6bae2016-07-15 17:25:21 -070065static bool add_include;
Josh Gao16016df2016-11-07 18:27:16 -080066static int max_thread_count = 48;
Josh Gaobf8a2852016-05-27 11:59:09 -070067
Josh Gao16016df2016-11-07 18:27:16 -080068static std::vector<std::string> generateCompileCommand(CompilationType& type,
69 const std::string& filename,
70 const std::string& header_dir,
71 const std::vector<std::string>& include_dirs) {
72 std::vector<std::string> cmd = { "versioner" };
73 cmd.push_back("-std=c11");
Josh Gaobf8a2852016-05-27 11:59:09 -070074
Josh Gao16016df2016-11-07 18:27:16 -080075 cmd.push_back("-Wall");
76 cmd.push_back("-Wextra");
77 cmd.push_back("-Werror");
78 cmd.push_back("-Wundef");
79 cmd.push_back("-Wno-unused-macros");
80 cmd.push_back("-Wno-unused-function");
81 cmd.push_back("-Wno-unused-variable");
82 cmd.push_back("-Wno-unknown-attributes");
83 cmd.push_back("-Wno-pragma-once-outside-header");
84
85 cmd.push_back("-target");
86 cmd.push_back(arch_targets[type.arch]);
87
88 cmd.push_back("-DANDROID");
89 cmd.push_back("-D__ANDROID_API__="s + std::to_string(type.api_level));
90 cmd.push_back("-D_FORTIFY_SOURCE=2");
91 cmd.push_back("-D_GNU_SOURCE");
92 cmd.push_back("-D_FILE_OFFSET_BITS="s + std::to_string(type.file_offset_bits));
93
94 cmd.push_back("-nostdinc");
95 std::string header_path;
96 if (add_include) {
97 const char* top = getenv("ANDROID_BUILD_TOP");
98 header_path = to_string(top) + "/bionic/libc/include/android/versioning.h";
99 cmd.push_back("-include");
100 cmd.push_back(header_path);
101 }
102
103 for (const auto& dir : include_dirs) {
104 cmd.push_back("-isystem");
105 cmd.push_back(dir);
106 }
107
108 cmd.push_back(filename);
109
110 return cmd;
111}
112
113class VersionerASTConsumer : public clang::ASTConsumer {
Josh Gaobf8a2852016-05-27 11:59:09 -0700114 public:
Josh Gao16016df2016-11-07 18:27:16 -0800115 HeaderDatabase* header_database;
116 CompilationType type;
117
118 VersionerASTConsumer(HeaderDatabase* header_database, CompilationType type)
119 : header_database(header_database), type(type) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700120 }
121
Josh Gao16016df2016-11-07 18:27:16 -0800122 virtual void HandleTranslationUnit(ASTContext& ctx) override {
123 header_database->parseAST(type, ctx);
Josh Gaobf8a2852016-05-27 11:59:09 -0700124 }
125};
126
Josh Gao16016df2016-11-07 18:27:16 -0800127class VersionerASTAction : public clang::ASTFrontendAction {
128 public:
129 HeaderDatabase* header_database;
130 CompilationType type;
131
132 VersionerASTAction(HeaderDatabase* header_database, CompilationType type)
133 : header_database(header_database), type(type) {
134 }
135
136 virtual std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance& Compiler,
137 llvm::StringRef InFile) override {
138 return std::make_unique<VersionerASTConsumer>(header_database, type);
139 }
140};
141
142static void compileHeader(HeaderDatabase* header_database, CompilationType type,
143 const std::string& filename, const std::string& header_dir,
144 const std::vector<std::string>& include_dirs) {
145 DiagnosticOptions diagnostic_options;
146 auto diagnostic_printer = new TextDiagnosticPrinter(llvm::errs(), &diagnostic_options);
147
148 IntrusiveRefCntPtr<DiagnosticIDs> diagnostic_ids(new DiagnosticIDs());
149 IntrusiveRefCntPtr<DiagnosticsEngine> diags(
150 new DiagnosticsEngine(diagnostic_ids, &diagnostic_options, diagnostic_printer, false));
151 driver::Driver Driver("versioner", llvm::sys::getDefaultTargetTriple(), *diags);
152
153 std::vector<std::string> cmd = generateCompileCommand(type, filename, header_dir, include_dirs);
154 llvm::SmallVector<const char*, 32> Args;
155
156 for (const std::string& str : cmd) {
157 Args.push_back(str.c_str());
158 }
159
160 std::unique_ptr<driver::Compilation> Compilation(Driver.BuildCompilation(Args));
161
162 const driver::Command &Cmd = llvm::cast<driver::Command>(*Compilation->getJobs().begin());
163 const driver::ArgStringList &CCArgs = Cmd.getArguments();
164
165 auto invocation = std::make_unique<CompilerInvocation>();
166 if (!CompilerInvocation::CreateFromArgs(
167 *invocation.get(), const_cast<const char**>(CCArgs.data()),
168 const_cast<const char**>(CCArgs.data()) + CCArgs.size(), *diags)) {
169 errx(1, "failed to create CompilerInvocation");
170 }
171
172 clang::CompilerInstance Compiler;
173 Compiler.setInvocation(invocation.release());
174 Compiler.setDiagnostics(diags.get());
175
176 VersionerASTAction versioner_action(header_database, type);
177 if (!Compiler.ExecuteAction(versioner_action)) {
178 errx(1, "compilation generated warnings or errors");
179 }
180
181 if (diags->getNumWarnings() || diags->hasErrorOccurred()) {
182 errx(1, "compilation generated warnings or errors");
183 }
184}
185
Josh Gaobf8a2852016-05-27 11:59:09 -0700186struct CompilationRequirements {
187 std::vector<std::string> headers;
188 std::vector<std::string> dependencies;
189};
190
Josh Gaobfb6bae2016-07-15 17:25:21 -0700191static CompilationRequirements collectRequirements(const Arch& arch, const std::string& header_dir,
Josh Gaobf8a2852016-05-27 11:59:09 -0700192 const std::string& dependency_dir) {
193 std::vector<std::string> headers = collectFiles(header_dir);
194
195 std::vector<std::string> dependencies = { header_dir };
196 if (!dependency_dir.empty()) {
197 auto collect_children = [&dependencies](const std::string& dir_path) {
198 DIR* dir = opendir(dir_path.c_str());
199 if (!dir) {
200 err(1, "failed to open dependency directory '%s'", dir_path.c_str());
201 }
202
203 struct dirent* dent;
204 while ((dent = readdir(dir))) {
205 if (dent->d_name[0] == '.') {
206 continue;
207 }
208
209 // TODO: Resolve symlinks.
210 std::string dependency = dir_path + "/" + dent->d_name;
211
212 struct stat st;
213 if (stat(dependency.c_str(), &st) != 0) {
214 err(1, "failed to stat dependency '%s'", dependency.c_str());
215 }
216
217 if (!S_ISDIR(st.st_mode)) {
218 errx(1, "'%s' is not a directory", dependency.c_str());
219 }
220
221 dependencies.push_back(dependency);
222 }
223
224 closedir(dir);
225 };
226
227 collect_children(dependency_dir + "/common");
Josh Gaobfb6bae2016-07-15 17:25:21 -0700228 collect_children(dependency_dir + "/" + to_string(arch));
Josh Gaobf8a2852016-05-27 11:59:09 -0700229 }
230
231 auto new_end = std::remove_if(headers.begin(), headers.end(), [&arch](llvm::StringRef header) {
232 for (const auto& it : header_blacklist) {
233 if (it.second.find(arch) == it.second.end()) {
234 continue;
235 }
236
237 if (header.endswith("/" + it.first)) {
238 return true;
239 }
240 }
241 return false;
242 });
243
244 headers.erase(new_end, headers.end());
245
246 CompilationRequirements result = { .headers = headers, .dependencies = dependencies };
247 return result;
248}
249
Josh Gaobfb6bae2016-07-15 17:25:21 -0700250static std::set<CompilationType> generateCompilationTypes(const std::set<Arch> selected_architectures,
251 const std::set<int>& selected_levels) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700252 std::set<CompilationType> result;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700253 for (const auto& arch : selected_architectures) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700254 int min_api = arch_min_api[arch];
255 for (int api_level : selected_levels) {
256 if (api_level < min_api) {
257 continue;
258 }
Josh Gaoa77b3a92016-08-15 16:39:27 -0700259
260 for (int file_offset_bits : { 32, 64 }) {
261 CompilationType type = {
262 .arch = arch, .api_level = api_level, .file_offset_bits = file_offset_bits
263 };
264 result.insert(type);
265 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700266 }
267 }
268 return result;
269}
270
Josh Gao16016df2016-11-07 18:27:16 -0800271struct Job {
272 Job(CompilationType type, const std::string& header, const std::vector<std::string>& dependencies)
273 : type(type), header(header), dependencies(dependencies) {
274 }
275 CompilationType type;
276 const std::string& header;
277 const std::vector<std::string>& dependencies;
278};
279
Josh Gaobfb6bae2016-07-15 17:25:21 -0700280static std::unique_ptr<HeaderDatabase> compileHeaders(const std::set<CompilationType>& types,
281 const std::string& header_dir,
Josh Gao16016df2016-11-07 18:27:16 -0800282 const std::string& dependency_dir) {
283 if (types.empty()) {
284 errx(1, "compileHeaders received no CompilationTypes");
285 }
286
287 size_t thread_count = max_thread_count;
288 std::vector<std::thread> threads;
289 std::vector<Job> jobs;
Josh Gaobf8a2852016-05-27 11:59:09 -0700290
291 std::map<CompilationType, HeaderDatabase> header_databases;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700292 std::unordered_map<Arch, CompilationRequirements> requirements;
Josh Gaobf8a2852016-05-27 11:59:09 -0700293
294 std::string cwd = getWorkingDir();
Josh Gao16016df2016-11-07 18:27:16 -0800295
296 auto result = std::make_unique<HeaderDatabase>();
297 auto spawn_threads = [&]() {
298 thread_count = std::min(thread_count, jobs.size());
299 for (size_t i = 0; i < thread_count; ++i) {
300 threads.emplace_back([&jobs, &result, &header_dir, thread_count, i]() {
301 size_t index = i;
302 while (index < jobs.size()) {
303 const auto& job = jobs[index];
304 compileHeader(result.get(), job.type, job.header, header_dir, job.dependencies);
305 index += thread_count;
306 }
307 });
308 }
309 };
310 auto reap_threads = [&]() {
311 for (auto& thread : threads) {
312 thread.join();
313 }
314 threads.clear();
315 };
Josh Gaobf8a2852016-05-27 11:59:09 -0700316
317 for (const auto& type : types) {
318 if (requirements.count(type.arch) == 0) {
319 requirements[type.arch] = collectRequirements(type.arch, header_dir, dependency_dir);
320 }
321 }
322
Josh Gao16016df2016-11-07 18:27:16 -0800323 for (CompilationType type : types) {
324 CompilationRequirements& req = requirements[type.arch];
325 for (const std::string& header : req.headers) {
326 jobs.emplace_back(type, header, req.dependencies);
Josh Gaobf8a2852016-05-27 11:59:09 -0700327 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700328 }
329
Josh Gao16016df2016-11-07 18:27:16 -0800330 spawn_threads();
331 reap_threads();
Josh Gaobf8a2852016-05-27 11:59:09 -0700332
Josh Gaobfb6bae2016-07-15 17:25:21 -0700333 return result;
Josh Gaobf8a2852016-05-27 11:59:09 -0700334}
335
Josh Gaobfb6bae2016-07-15 17:25:21 -0700336// Perform a sanity check on a symbol's declarations, enforcing the following invariants:
337// 1. At most one inline definition of the function exists.
338// 2. All of the availability declarations for a symbol are compatible.
339// If a function is declared as an inline before a certain version, the inline definition
340// should have no version tag.
341// 3. Each availability type must only be present globally or on a per-arch basis.
342// (e.g. __INTRODUCED_IN_ARM(9) __INTRODUCED_IN_X86(10) __DEPRECATED_IN(11) is fine,
343// but not __INTRODUCED_IN(9) __INTRODUCED_IN_X86(10))
344static bool checkSymbol(const Symbol& symbol) {
345 std::string cwd = getWorkingDir() + "/";
346
347 const Declaration* inline_definition = nullptr;
348 for (const auto& decl_it : symbol.declarations) {
349 const Declaration* decl = &decl_it.second;
350 if (decl->is_definition) {
351 if (inline_definition) {
352 fprintf(stderr, "versioner: multiple definitions of symbol %s\n", symbol.name.c_str());
353 symbol.dump(cwd);
354 inline_definition->dump(cwd);
355 return false;
356 }
357
358 inline_definition = decl;
359 }
360
361 DeclarationAvailability availability;
362 if (!decl->calculateAvailability(&availability)) {
363 fprintf(stderr, "versioner: failed to calculate availability for declaration:\n");
Josh Gao566735d2016-08-02 15:07:32 -0700364 decl->dump(cwd, stderr, 2);
Josh Gaobfb6bae2016-07-15 17:25:21 -0700365 return false;
366 }
367
368 if (decl->is_definition && !availability.empty()) {
369 fprintf(stderr, "versioner: inline definition has non-empty versioning information:\n");
Josh Gao566735d2016-08-02 15:07:32 -0700370 decl->dump(cwd, stderr, 2);
Josh Gaobfb6bae2016-07-15 17:25:21 -0700371 return false;
372 }
373 }
374
375 DeclarationAvailability availability;
376 if (!symbol.calculateAvailability(&availability)) {
377 fprintf(stderr, "versioner: inconsistent availability for symbol '%s'\n", symbol.name.c_str());
378 symbol.dump(cwd);
379 return false;
380 }
381
382 // TODO: Check invariant #3.
383 return true;
384}
385
386static bool sanityCheck(const HeaderDatabase* database) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700387 bool error = false;
Josh Gao958f3b32016-06-03 13:44:00 -0700388 std::string cwd = getWorkingDir() + "/";
389
Josh Gaobfb6bae2016-07-15 17:25:21 -0700390 for (const auto& symbol_it : database->symbols) {
391 if (!checkSymbol(symbol_it.second)) {
392 error = true;
Josh Gaobf8a2852016-05-27 11:59:09 -0700393 }
394 }
395 return !error;
396}
397
398// Check that our symbol availability declarations match the actual NDK
399// platform symbol availability.
400static bool checkVersions(const std::set<CompilationType>& types,
Josh Gaobfb6bae2016-07-15 17:25:21 -0700401 const HeaderDatabase* header_database,
Josh Gaobf8a2852016-05-27 11:59:09 -0700402 const NdkSymbolDatabase& symbol_database) {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700403 std::string cwd = getWorkingDir() + "/";
Josh Gaobf8a2852016-05-27 11:59:09 -0700404 bool failed = false;
405
Josh Gaobfb6bae2016-07-15 17:25:21 -0700406 std::map<Arch, std::set<CompilationType>> arch_types;
Josh Gaobf8a2852016-05-27 11:59:09 -0700407 for (const CompilationType& type : types) {
408 arch_types[type.arch].insert(type);
409 }
410
Josh Gaod67dbf02016-06-02 15:21:14 -0700411 std::set<std::string> completely_unavailable;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700412 std::map<std::string, std::set<CompilationType>> missing_availability;
413 std::map<std::string, std::set<CompilationType>> extra_availability;
Josh Gaod67dbf02016-06-02 15:21:14 -0700414
Josh Gaobfb6bae2016-07-15 17:25:21 -0700415 for (const auto& symbol_it : header_database->symbols) {
416 const auto& symbol_name = symbol_it.first;
417 DeclarationAvailability symbol_availability;
Josh Gaobf8a2852016-05-27 11:59:09 -0700418
Josh Gaobfb6bae2016-07-15 17:25:21 -0700419 if (!symbol_it.second.calculateAvailability(&symbol_availability)) {
420 errx(1, "failed to calculate symbol availability");
421 }
422
423 const auto platform_availability_it = symbol_database.find(symbol_name);
Josh Gaobf8a2852016-05-27 11:59:09 -0700424 if (platform_availability_it == symbol_database.end()) {
Josh Gaod67dbf02016-06-02 15:21:14 -0700425 completely_unavailable.insert(symbol_name);
Josh Gaobf8a2852016-05-27 11:59:09 -0700426 continue;
427 }
428
429 const auto& platform_availability = platform_availability_it->second;
Josh Gaobf8a2852016-05-27 11:59:09 -0700430
431 for (const CompilationType& type : types) {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700432 bool should_be_available = true;
433 const auto& global_availability = symbol_availability.global_availability;
434 const auto& arch_availability = symbol_availability.arch_availability[type.arch];
435 if (global_availability.introduced != 0 && global_availability.introduced > type.api_level) {
436 should_be_available = false;
437 }
438
439 if (arch_availability.introduced != 0 && arch_availability.introduced > type.api_level) {
440 should_be_available = false;
441 }
442
443 if (global_availability.obsoleted != 0 && global_availability.obsoleted <= type.api_level) {
444 should_be_available = false;
445 }
446
447 if (arch_availability.obsoleted != 0 && arch_availability.obsoleted <= type.api_level) {
448 should_be_available = false;
449 }
450
451 if (arch_availability.future) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700452 continue;
453 }
454
Josh Gaobfb6bae2016-07-15 17:25:21 -0700455 // The function declaration might be (validly) missing for the given CompilationType.
456 if (!symbol_it.second.hasDeclaration(type)) {
457 should_be_available = false;
458 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700459
Josh Gaobfb6bae2016-07-15 17:25:21 -0700460 bool is_available = platform_availability.count(type);
Josh Gaobf8a2852016-05-27 11:59:09 -0700461
Josh Gaobfb6bae2016-07-15 17:25:21 -0700462 if (should_be_available != is_available) {
463 if (is_available) {
464 extra_availability[symbol_name].insert(type);
Josh Gaobf8a2852016-05-27 11:59:09 -0700465 } else {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700466 missing_availability[symbol_name].insert(type);
Josh Gaobf8a2852016-05-27 11:59:09 -0700467 }
468 }
469 }
Josh Gaobfb6bae2016-07-15 17:25:21 -0700470 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700471
Josh Gaobfb6bae2016-07-15 17:25:21 -0700472 for (const auto& it : symbol_database) {
473 const std::string& symbol_name = it.first;
Josh Gaobf8a2852016-05-27 11:59:09 -0700474
Josh Gaobfb6bae2016-07-15 17:25:21 -0700475 bool symbol_error = false;
476 auto missing_it = missing_availability.find(symbol_name);
477 if (missing_it != missing_availability.end()) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700478 printf("%s: declaration marked available but symbol missing in [%s]\n", symbol_name.c_str(),
Josh Gaobfb6bae2016-07-15 17:25:21 -0700479 Join(missing_it->second, ", ").c_str());
480 symbol_error = true;
Josh Gaobf8a2852016-05-27 11:59:09 -0700481 failed = true;
482 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700483
Josh Gaobfb6bae2016-07-15 17:25:21 -0700484 if (verbose) {
485 auto extra_it = extra_availability.find(symbol_name);
486 if (extra_it != extra_availability.end()) {
487 printf("%s: declaration marked unavailable but symbol available in [%s]\n",
488 symbol_name.c_str(), Join(extra_it->second, ", ").c_str());
489 symbol_error = true;
490 failed = true;
Josh Gao958f3b32016-06-03 13:44:00 -0700491 }
492 }
493
Josh Gaobfb6bae2016-07-15 17:25:21 -0700494 if (symbol_error) {
495 auto symbol_it = header_database->symbols.find(symbol_name);
496 if (symbol_it == header_database->symbols.end()) {
497 errx(1, "failed to find symbol in header database");
498 }
499 symbol_it->second.dump(cwd);
Josh Gaod67dbf02016-06-02 15:21:14 -0700500 }
Josh Gaod67dbf02016-06-02 15:21:14 -0700501 }
502
Josh Gaobfb6bae2016-07-15 17:25:21 -0700503 // TODO: Verify that function/variable declarations are actually function/variable symbols.
Josh Gaobf8a2852016-05-27 11:59:09 -0700504 return !failed;
505}
506
Josh Gao62aaf8f2016-06-02 14:27:21 -0700507static void usage(bool help = false) {
508 fprintf(stderr, "Usage: versioner [OPTION]... [HEADER_PATH] [DEPS_PATH]\n");
509 if (!help) {
510 printf("Try 'versioner -h' for more information.\n");
511 exit(1);
512 } else {
513 fprintf(stderr, "Version headers at HEADER_PATH, with DEPS_PATH/ARCH/* on the include path\n");
Josh Gao9b5af7a2016-06-02 14:29:13 -0700514 fprintf(stderr, "Autodetects paths if HEADER_PATH and DEPS_PATH are not specified\n");
Josh Gao62aaf8f2016-06-02 14:27:21 -0700515 fprintf(stderr, "\n");
516 fprintf(stderr, "Target specification (defaults to all):\n");
517 fprintf(stderr, " -a API_LEVEL\tbuild with specified API level (can be repeated)\n");
518 fprintf(stderr, " \t\tvalid levels are %s\n", Join(supported_levels).c_str());
519 fprintf(stderr, " -r ARCH\tbuild with specified architecture (can be repeated)\n");
520 fprintf(stderr, " \t\tvalid architectures are %s\n", Join(supported_archs).c_str());
521 fprintf(stderr, "\n");
522 fprintf(stderr, "Validation:\n");
523 fprintf(stderr, " -p PATH\tcompare against NDK platform at PATH\n");
524 fprintf(stderr, " -v\t\tenable verbose warnings\n");
525 fprintf(stderr, "\n");
Josh Gaof8592a32016-07-26 18:58:27 -0700526 fprintf(stderr, "Preprocessing:\n");
527 fprintf(stderr, " -o PATH\tpreprocess header files and emit them at PATH\n");
528 fprintf(stderr, " -f\tpreprocess header files even if validation fails\n");
529 fprintf(stderr, "\n");
Josh Gao62aaf8f2016-06-02 14:27:21 -0700530 fprintf(stderr, "Miscellaneous:\n");
Josh Gaobfb6bae2016-07-15 17:25:21 -0700531 fprintf(stderr, " -d\t\tdump function availability\n");
Josh Gao16016df2016-11-07 18:27:16 -0800532 fprintf(stderr, " -j THREADS\tmaximum number of threads to use\n");
Josh Gao62aaf8f2016-06-02 14:27:21 -0700533 fprintf(stderr, " -h\t\tdisplay this message\n");
534 exit(0);
535 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700536}
537
538int main(int argc, char** argv) {
539 std::string cwd = getWorkingDir() + "/";
540 bool default_args = true;
541 std::string platform_dir;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700542 std::set<Arch> selected_architectures;
Josh Gaobf8a2852016-05-27 11:59:09 -0700543 std::set<int> selected_levels;
Josh Gaof8592a32016-07-26 18:58:27 -0700544 std::string preprocessor_output_path;
545 bool force = false;
Josh Gao16016df2016-11-07 18:27:16 -0800546 bool dump = false;
Josh Gaobf8a2852016-05-27 11:59:09 -0700547
548 int c;
Josh Gao16016df2016-11-07 18:27:16 -0800549 while ((c = getopt(argc, argv, "a:r:p:vo:fdj:hi")) != -1) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700550 default_args = false;
551 switch (c) {
552 case 'a': {
553 char* end;
554 int api_level = strtol(optarg, &end, 10);
555 if (end == optarg || strlen(end) > 0) {
556 usage();
557 }
558
559 if (supported_levels.count(api_level) == 0) {
560 errx(1, "unsupported API level %d", api_level);
561 }
562
563 selected_levels.insert(api_level);
564 break;
565 }
566
567 case 'r': {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700568 Arch arch = arch_from_string(optarg);
569 selected_architectures.insert(arch);
Josh Gaobf8a2852016-05-27 11:59:09 -0700570 break;
571 }
572
573 case 'p': {
574 if (!platform_dir.empty()) {
575 usage();
576 }
577
578 platform_dir = optarg;
579
Josh Gaof8592a32016-07-26 18:58:27 -0700580 if (platform_dir.empty()) {
581 usage();
582 }
583
Josh Gaobf8a2852016-05-27 11:59:09 -0700584 struct stat st;
585 if (stat(platform_dir.c_str(), &st) != 0) {
586 err(1, "failed to stat platform directory '%s'", platform_dir.c_str());
587 }
588 if (!S_ISDIR(st.st_mode)) {
589 errx(1, "'%s' is not a directory", optarg);
590 }
591 break;
592 }
593
594 case 'v':
595 verbose = true;
596 break;
597
Josh Gaof8592a32016-07-26 18:58:27 -0700598 case 'o':
599 if (!preprocessor_output_path.empty()) {
600 usage();
601 }
602 preprocessor_output_path = optarg;
603 if (preprocessor_output_path.empty()) {
604 usage();
605 }
606 break;
607
608 case 'f':
609 force = true;
610 break;
611
Josh Gaobfb6bae2016-07-15 17:25:21 -0700612 case 'd':
613 dump = true;
614 break;
615
Josh Gao16016df2016-11-07 18:27:16 -0800616 case 'j':
617 if (!android::base::ParseInt<int>(optarg, &max_thread_count, 1)) {
618 usage();
619 }
620 break;
621
Josh Gao62aaf8f2016-06-02 14:27:21 -0700622 case 'h':
623 usage(true);
624 break;
625
Josh Gaobfb6bae2016-07-15 17:25:21 -0700626 case 'i':
627 // Secret option for tests to -include <android/versioning.h>.
628 add_include = true;
629 break;
630
Josh Gaobf8a2852016-05-27 11:59:09 -0700631 default:
632 usage();
633 break;
634 }
635 }
636
Josh Gao9b5af7a2016-06-02 14:29:13 -0700637 if (argc - optind > 2 || optind > argc) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700638 usage();
639 }
640
Josh Gao9b5af7a2016-06-02 14:29:13 -0700641 std::string header_dir;
642 std::string dependency_dir;
643
Josh Gaobfb6bae2016-07-15 17:25:21 -0700644 const char* top = getenv("ANDROID_BUILD_TOP");
645 if (!top && (optind == argc || add_include)) {
646 fprintf(stderr, "versioner: failed to autodetect bionic paths. Is ANDROID_BUILD_TOP set?\n");
647 usage();
648 }
649
Josh Gao9b5af7a2016-06-02 14:29:13 -0700650 if (optind == argc) {
651 // Neither HEADER_PATH nor DEPS_PATH were specified, so try to figure them out.
Josh Gaobfb6bae2016-07-15 17:25:21 -0700652 std::string versioner_dir = to_string(top) + "/bionic/tools/versioner";
Josh Gao9b5af7a2016-06-02 14:29:13 -0700653 header_dir = versioner_dir + "/current";
654 dependency_dir = versioner_dir + "/dependencies";
655 if (platform_dir.empty()) {
656 platform_dir = versioner_dir + "/platforms";
657 }
658 } else {
Josh Gaof8592a32016-07-26 18:58:27 -0700659 // Intentional leak.
660 header_dir = realpath(argv[optind], nullptr);
Josh Gao9b5af7a2016-06-02 14:29:13 -0700661
662 if (argc - optind == 2) {
663 dependency_dir = argv[optind + 1];
664 }
665 }
666
Josh Gaobf8a2852016-05-27 11:59:09 -0700667 if (selected_levels.empty()) {
668 selected_levels = supported_levels;
669 }
670
671 if (selected_architectures.empty()) {
672 selected_architectures = supported_archs;
673 }
674
Josh Gaobf8a2852016-05-27 11:59:09 -0700675
676 struct stat st;
Josh Gao9b5af7a2016-06-02 14:29:13 -0700677 if (stat(header_dir.c_str(), &st) != 0) {
678 err(1, "failed to stat '%s'", header_dir.c_str());
Josh Gaobf8a2852016-05-27 11:59:09 -0700679 } else if (!S_ISDIR(st.st_mode)) {
Josh Gao9b5af7a2016-06-02 14:29:13 -0700680 errx(1, "'%s' is not a directory", header_dir.c_str());
Josh Gaobf8a2852016-05-27 11:59:09 -0700681 }
682
683 std::set<CompilationType> compilation_types;
Josh Gaobf8a2852016-05-27 11:59:09 -0700684 NdkSymbolDatabase symbol_database;
685
686 compilation_types = generateCompilationTypes(selected_architectures, selected_levels);
687
688 // Do this before compiling so that we can early exit if the platforms don't match what we
689 // expect.
690 if (!platform_dir.empty()) {
691 symbol_database = parsePlatforms(compilation_types, platform_dir);
692 }
693
Josh Gaobfb6bae2016-07-15 17:25:21 -0700694 std::unique_ptr<HeaderDatabase> declaration_database =
Josh Gao16016df2016-11-07 18:27:16 -0800695 compileHeaders(compilation_types, header_dir, dependency_dir);
Josh Gaobf8a2852016-05-27 11:59:09 -0700696
Josh Gao16016df2016-11-07 18:27:16 -0800697 bool failed = false;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700698 if (dump) {
699 declaration_database->dump(header_dir + "/");
700 } else {
701 if (!sanityCheck(declaration_database.get())) {
702 printf("versioner: sanity check failed\n");
Josh Gaobf8a2852016-05-27 11:59:09 -0700703 failed = true;
704 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700705
Josh Gaobfb6bae2016-07-15 17:25:21 -0700706 if (!platform_dir.empty()) {
707 if (!checkVersions(compilation_types, declaration_database.get(), symbol_database)) {
708 printf("versioner: version check failed\n");
709 failed = true;
710 }
711 }
712 }
Josh Gaof8592a32016-07-26 18:58:27 -0700713
714 if (!preprocessor_output_path.empty() && (force || !failed)) {
715 failed = !preprocessHeaders(preprocessor_output_path, header_dir, declaration_database.get());
716 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700717 return failed;
718}