blob: 5aaf47ae1940cbedfc424bf20eec370879d7ca7f [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 Gaobf8a2852016-05-27 11:59:09 -070037#include <llvm/ADT/StringRef.h>
38
Josh Gao16016df2016-11-07 18:27:16 -080039#include <android-base/parseint.h>
40
Josh Gaobfb6bae2016-07-15 17:25:21 -070041#include "Arch.h"
Josh Gaobf8a2852016-05-27 11:59:09 -070042#include "DeclarationDatabase.h"
Josh Gaob5c49632016-11-08 22:21:31 -080043#include "Driver.h"
Josh Gaof8592a32016-07-26 18:58:27 -070044#include "Preprocessor.h"
Josh Gaobf8a2852016-05-27 11:59:09 -070045#include "SymbolDatabase.h"
46#include "Utils.h"
Josh Gao78b8a142016-11-09 01:00:41 -080047#include "VFS.h"
48
Josh Gaobf8a2852016-05-27 11:59:09 -070049#include "versioner.h"
50
51using namespace std::string_literals;
Josh Gaobf8a2852016-05-27 11:59:09 -070052
Josh Gaob5c49632016-11-08 22:21:31 -080053bool add_include;
Josh Gaobf8a2852016-05-27 11:59:09 -070054bool verbose;
Josh Gao16016df2016-11-07 18:27:16 -080055static int max_thread_count = 48;
Josh Gaobf8a2852016-05-27 11:59:09 -070056
Josh Gaobfb6bae2016-07-15 17:25:21 -070057static CompilationRequirements collectRequirements(const Arch& arch, const std::string& header_dir,
Josh Gaobf8a2852016-05-27 11:59:09 -070058 const std::string& dependency_dir) {
59 std::vector<std::string> headers = collectFiles(header_dir);
Josh Gaobf8a2852016-05-27 11:59:09 -070060 std::vector<std::string> dependencies = { header_dir };
61 if (!dependency_dir.empty()) {
Josh Gaob5c49632016-11-08 22:21:31 -080062 auto collect_children = [&dependencies, &dependency_dir](const std::string& dir_path) {
Josh Gaobf8a2852016-05-27 11:59:09 -070063 DIR* dir = opendir(dir_path.c_str());
64 if (!dir) {
65 err(1, "failed to open dependency directory '%s'", dir_path.c_str());
66 }
67
68 struct dirent* dent;
69 while ((dent = readdir(dir))) {
70 if (dent->d_name[0] == '.') {
71 continue;
72 }
73
74 // TODO: Resolve symlinks.
75 std::string dependency = dir_path + "/" + dent->d_name;
76
77 struct stat st;
78 if (stat(dependency.c_str(), &st) != 0) {
79 err(1, "failed to stat dependency '%s'", dependency.c_str());
80 }
81
82 if (!S_ISDIR(st.st_mode)) {
83 errx(1, "'%s' is not a directory", dependency.c_str());
84 }
85
86 dependencies.push_back(dependency);
87 }
88
89 closedir(dir);
90 };
91
92 collect_children(dependency_dir + "/common");
Josh Gaobfb6bae2016-07-15 17:25:21 -070093 collect_children(dependency_dir + "/" + to_string(arch));
Josh Gaobf8a2852016-05-27 11:59:09 -070094 }
95
96 auto new_end = std::remove_if(headers.begin(), headers.end(), [&arch](llvm::StringRef header) {
97 for (const auto& it : header_blacklist) {
98 if (it.second.find(arch) == it.second.end()) {
99 continue;
100 }
101
102 if (header.endswith("/" + it.first)) {
103 return true;
104 }
105 }
106 return false;
107 });
108
109 headers.erase(new_end, headers.end());
110
111 CompilationRequirements result = { .headers = headers, .dependencies = dependencies };
112 return result;
113}
114
Josh Gaobfb6bae2016-07-15 17:25:21 -0700115static std::set<CompilationType> generateCompilationTypes(const std::set<Arch> selected_architectures,
116 const std::set<int>& selected_levels) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700117 std::set<CompilationType> result;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700118 for (const auto& arch : selected_architectures) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700119 int min_api = arch_min_api[arch];
120 for (int api_level : selected_levels) {
121 if (api_level < min_api) {
122 continue;
123 }
Josh Gaoa77b3a92016-08-15 16:39:27 -0700124
125 for (int file_offset_bits : { 32, 64 }) {
126 CompilationType type = {
127 .arch = arch, .api_level = api_level, .file_offset_bits = file_offset_bits
128 };
129 result.insert(type);
130 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700131 }
132 }
133 return result;
134}
135
Josh Gaobfb6bae2016-07-15 17:25:21 -0700136static std::unique_ptr<HeaderDatabase> compileHeaders(const std::set<CompilationType>& types,
137 const std::string& header_dir,
Josh Gao16016df2016-11-07 18:27:16 -0800138 const std::string& dependency_dir) {
139 if (types.empty()) {
140 errx(1, "compileHeaders received no CompilationTypes");
141 }
142
Josh Gao78b8a142016-11-09 01:00:41 -0800143 auto vfs = createCommonVFS(header_dir, dependency_dir, add_include);
144
Josh Gao16016df2016-11-07 18:27:16 -0800145 size_t thread_count = max_thread_count;
146 std::vector<std::thread> threads;
Josh Gaobf8a2852016-05-27 11:59:09 -0700147
148 std::map<CompilationType, HeaderDatabase> header_databases;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700149 std::unordered_map<Arch, CompilationRequirements> requirements;
Josh Gaobf8a2852016-05-27 11:59:09 -0700150
Josh Gao16016df2016-11-07 18:27:16 -0800151 auto result = std::make_unique<HeaderDatabase>();
Josh Gaobf8a2852016-05-27 11:59:09 -0700152 for (const auto& type : types) {
153 if (requirements.count(type.arch) == 0) {
154 requirements[type.arch] = collectRequirements(type.arch, header_dir, dependency_dir);
155 }
156 }
157
Josh Gao78b8a142016-11-09 01:00:41 -0800158 initializeTargetCC1FlagCache(vfs, types, requirements);
Josh Gaob5c49632016-11-08 22:21:31 -0800159
160 std::vector<std::pair<CompilationType, const std::string&>> jobs;
Josh Gao16016df2016-11-07 18:27:16 -0800161 for (CompilationType type : types) {
162 CompilationRequirements& req = requirements[type.arch];
163 for (const std::string& header : req.headers) {
Josh Gaob5c49632016-11-08 22:21:31 -0800164 jobs.emplace_back(type, header);
Josh Gaobf8a2852016-05-27 11:59:09 -0700165 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700166 }
167
Josh Gaob5c49632016-11-08 22:21:31 -0800168 thread_count = std::min(thread_count, jobs.size());
169
170 if (thread_count == 1) {
171 for (const auto& job : jobs) {
Josh Gao78b8a142016-11-09 01:00:41 -0800172 compileHeader(vfs, result.get(), job.first, job.second);
Josh Gaob5c49632016-11-08 22:21:31 -0800173 }
174 } else {
175 // Spawn threads.
176 for (size_t i = 0; i < thread_count; ++i) {
Josh Gao78b8a142016-11-09 01:00:41 -0800177 threads.emplace_back([&jobs, &result, &header_dir, vfs, thread_count, i]() {
Josh Gaob5c49632016-11-08 22:21:31 -0800178 size_t index = i;
179 while (index < jobs.size()) {
180 const auto& job = jobs[index];
Josh Gao78b8a142016-11-09 01:00:41 -0800181 compileHeader(vfs, result.get(), job.first, job.second);
Josh Gaob5c49632016-11-08 22:21:31 -0800182 index += thread_count;
183 }
184 });
185 }
186
187 // Reap them.
188 for (auto& thread : threads) {
189 thread.join();
190 }
191 threads.clear();
192 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700193
Josh Gaobfb6bae2016-07-15 17:25:21 -0700194 return result;
Josh Gaobf8a2852016-05-27 11:59:09 -0700195}
196
Josh Gaobfb6bae2016-07-15 17:25:21 -0700197// Perform a sanity check on a symbol's declarations, enforcing the following invariants:
198// 1. At most one inline definition of the function exists.
199// 2. All of the availability declarations for a symbol are compatible.
200// If a function is declared as an inline before a certain version, the inline definition
201// should have no version tag.
202// 3. Each availability type must only be present globally or on a per-arch basis.
203// (e.g. __INTRODUCED_IN_ARM(9) __INTRODUCED_IN_X86(10) __DEPRECATED_IN(11) is fine,
204// but not __INTRODUCED_IN(9) __INTRODUCED_IN_X86(10))
205static bool checkSymbol(const Symbol& symbol) {
206 std::string cwd = getWorkingDir() + "/";
207
208 const Declaration* inline_definition = nullptr;
209 for (const auto& decl_it : symbol.declarations) {
210 const Declaration* decl = &decl_it.second;
211 if (decl->is_definition) {
212 if (inline_definition) {
213 fprintf(stderr, "versioner: multiple definitions of symbol %s\n", symbol.name.c_str());
214 symbol.dump(cwd);
215 inline_definition->dump(cwd);
216 return false;
217 }
218
219 inline_definition = decl;
220 }
221
222 DeclarationAvailability availability;
223 if (!decl->calculateAvailability(&availability)) {
224 fprintf(stderr, "versioner: failed to calculate availability for declaration:\n");
Josh Gao566735d2016-08-02 15:07:32 -0700225 decl->dump(cwd, stderr, 2);
Josh Gaobfb6bae2016-07-15 17:25:21 -0700226 return false;
227 }
228
229 if (decl->is_definition && !availability.empty()) {
230 fprintf(stderr, "versioner: inline definition has non-empty versioning information:\n");
Josh Gao566735d2016-08-02 15:07:32 -0700231 decl->dump(cwd, stderr, 2);
Josh Gaobfb6bae2016-07-15 17:25:21 -0700232 return false;
233 }
234 }
235
236 DeclarationAvailability availability;
237 if (!symbol.calculateAvailability(&availability)) {
238 fprintf(stderr, "versioner: inconsistent availability for symbol '%s'\n", symbol.name.c_str());
239 symbol.dump(cwd);
240 return false;
241 }
242
243 // TODO: Check invariant #3.
244 return true;
245}
246
247static bool sanityCheck(const HeaderDatabase* database) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700248 bool error = false;
Josh Gao958f3b32016-06-03 13:44:00 -0700249 std::string cwd = getWorkingDir() + "/";
250
Josh Gaobfb6bae2016-07-15 17:25:21 -0700251 for (const auto& symbol_it : database->symbols) {
252 if (!checkSymbol(symbol_it.second)) {
253 error = true;
Josh Gaobf8a2852016-05-27 11:59:09 -0700254 }
255 }
256 return !error;
257}
258
259// Check that our symbol availability declarations match the actual NDK
260// platform symbol availability.
261static bool checkVersions(const std::set<CompilationType>& types,
Josh Gaobfb6bae2016-07-15 17:25:21 -0700262 const HeaderDatabase* header_database,
Josh Gaobf8a2852016-05-27 11:59:09 -0700263 const NdkSymbolDatabase& symbol_database) {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700264 std::string cwd = getWorkingDir() + "/";
Josh Gaobf8a2852016-05-27 11:59:09 -0700265 bool failed = false;
266
Josh Gaobfb6bae2016-07-15 17:25:21 -0700267 std::map<Arch, std::set<CompilationType>> arch_types;
Josh Gaobf8a2852016-05-27 11:59:09 -0700268 for (const CompilationType& type : types) {
269 arch_types[type.arch].insert(type);
270 }
271
Josh Gaod67dbf02016-06-02 15:21:14 -0700272 std::set<std::string> completely_unavailable;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700273 std::map<std::string, std::set<CompilationType>> missing_availability;
274 std::map<std::string, std::set<CompilationType>> extra_availability;
Josh Gaod67dbf02016-06-02 15:21:14 -0700275
Josh Gaobfb6bae2016-07-15 17:25:21 -0700276 for (const auto& symbol_it : header_database->symbols) {
277 const auto& symbol_name = symbol_it.first;
278 DeclarationAvailability symbol_availability;
Josh Gaobf8a2852016-05-27 11:59:09 -0700279
Josh Gaobfb6bae2016-07-15 17:25:21 -0700280 if (!symbol_it.second.calculateAvailability(&symbol_availability)) {
281 errx(1, "failed to calculate symbol availability");
282 }
283
284 const auto platform_availability_it = symbol_database.find(symbol_name);
Josh Gaobf8a2852016-05-27 11:59:09 -0700285 if (platform_availability_it == symbol_database.end()) {
Josh Gaod67dbf02016-06-02 15:21:14 -0700286 completely_unavailable.insert(symbol_name);
Josh Gaobf8a2852016-05-27 11:59:09 -0700287 continue;
288 }
289
290 const auto& platform_availability = platform_availability_it->second;
Josh Gaobf8a2852016-05-27 11:59:09 -0700291
292 for (const CompilationType& type : types) {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700293 bool should_be_available = true;
294 const auto& global_availability = symbol_availability.global_availability;
295 const auto& arch_availability = symbol_availability.arch_availability[type.arch];
296 if (global_availability.introduced != 0 && global_availability.introduced > type.api_level) {
297 should_be_available = false;
298 }
299
300 if (arch_availability.introduced != 0 && arch_availability.introduced > type.api_level) {
301 should_be_available = false;
302 }
303
304 if (global_availability.obsoleted != 0 && global_availability.obsoleted <= type.api_level) {
305 should_be_available = false;
306 }
307
308 if (arch_availability.obsoleted != 0 && arch_availability.obsoleted <= type.api_level) {
309 should_be_available = false;
310 }
311
312 if (arch_availability.future) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700313 continue;
314 }
315
Josh Gaobfb6bae2016-07-15 17:25:21 -0700316 // The function declaration might be (validly) missing for the given CompilationType.
317 if (!symbol_it.second.hasDeclaration(type)) {
318 should_be_available = false;
319 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700320
Josh Gaobfb6bae2016-07-15 17:25:21 -0700321 bool is_available = platform_availability.count(type);
Josh Gaobf8a2852016-05-27 11:59:09 -0700322
Josh Gaobfb6bae2016-07-15 17:25:21 -0700323 if (should_be_available != is_available) {
324 if (is_available) {
325 extra_availability[symbol_name].insert(type);
Josh Gaobf8a2852016-05-27 11:59:09 -0700326 } else {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700327 missing_availability[symbol_name].insert(type);
Josh Gaobf8a2852016-05-27 11:59:09 -0700328 }
329 }
330 }
Josh Gaobfb6bae2016-07-15 17:25:21 -0700331 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700332
Josh Gaobfb6bae2016-07-15 17:25:21 -0700333 for (const auto& it : symbol_database) {
334 const std::string& symbol_name = it.first;
Josh Gaobf8a2852016-05-27 11:59:09 -0700335
Josh Gaobfb6bae2016-07-15 17:25:21 -0700336 bool symbol_error = false;
337 auto missing_it = missing_availability.find(symbol_name);
338 if (missing_it != missing_availability.end()) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700339 printf("%s: declaration marked available but symbol missing in [%s]\n", symbol_name.c_str(),
Josh Gaobfb6bae2016-07-15 17:25:21 -0700340 Join(missing_it->second, ", ").c_str());
341 symbol_error = true;
Josh Gaobf8a2852016-05-27 11:59:09 -0700342 failed = true;
343 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700344
Josh Gaobfb6bae2016-07-15 17:25:21 -0700345 if (verbose) {
346 auto extra_it = extra_availability.find(symbol_name);
347 if (extra_it != extra_availability.end()) {
348 printf("%s: declaration marked unavailable but symbol available in [%s]\n",
349 symbol_name.c_str(), Join(extra_it->second, ", ").c_str());
350 symbol_error = true;
351 failed = true;
Josh Gao958f3b32016-06-03 13:44:00 -0700352 }
353 }
354
Josh Gaobfb6bae2016-07-15 17:25:21 -0700355 if (symbol_error) {
356 auto symbol_it = header_database->symbols.find(symbol_name);
357 if (symbol_it == header_database->symbols.end()) {
358 errx(1, "failed to find symbol in header database");
359 }
360 symbol_it->second.dump(cwd);
Josh Gaod67dbf02016-06-02 15:21:14 -0700361 }
Josh Gaod67dbf02016-06-02 15:21:14 -0700362 }
363
Josh Gaobfb6bae2016-07-15 17:25:21 -0700364 // TODO: Verify that function/variable declarations are actually function/variable symbols.
Josh Gaobf8a2852016-05-27 11:59:09 -0700365 return !failed;
366}
367
Josh Gao62aaf8f2016-06-02 14:27:21 -0700368static void usage(bool help = false) {
369 fprintf(stderr, "Usage: versioner [OPTION]... [HEADER_PATH] [DEPS_PATH]\n");
370 if (!help) {
371 printf("Try 'versioner -h' for more information.\n");
372 exit(1);
373 } else {
374 fprintf(stderr, "Version headers at HEADER_PATH, with DEPS_PATH/ARCH/* on the include path\n");
Josh Gao9b5af7a2016-06-02 14:29:13 -0700375 fprintf(stderr, "Autodetects paths if HEADER_PATH and DEPS_PATH are not specified\n");
Josh Gao62aaf8f2016-06-02 14:27:21 -0700376 fprintf(stderr, "\n");
377 fprintf(stderr, "Target specification (defaults to all):\n");
378 fprintf(stderr, " -a API_LEVEL\tbuild with specified API level (can be repeated)\n");
379 fprintf(stderr, " \t\tvalid levels are %s\n", Join(supported_levels).c_str());
380 fprintf(stderr, " -r ARCH\tbuild with specified architecture (can be repeated)\n");
381 fprintf(stderr, " \t\tvalid architectures are %s\n", Join(supported_archs).c_str());
382 fprintf(stderr, "\n");
383 fprintf(stderr, "Validation:\n");
384 fprintf(stderr, " -p PATH\tcompare against NDK platform at PATH\n");
385 fprintf(stderr, " -v\t\tenable verbose warnings\n");
386 fprintf(stderr, "\n");
Josh Gaof8592a32016-07-26 18:58:27 -0700387 fprintf(stderr, "Preprocessing:\n");
388 fprintf(stderr, " -o PATH\tpreprocess header files and emit them at PATH\n");
389 fprintf(stderr, " -f\tpreprocess header files even if validation fails\n");
390 fprintf(stderr, "\n");
Josh Gao62aaf8f2016-06-02 14:27:21 -0700391 fprintf(stderr, "Miscellaneous:\n");
Josh Gaobfb6bae2016-07-15 17:25:21 -0700392 fprintf(stderr, " -d\t\tdump function availability\n");
Josh Gao16016df2016-11-07 18:27:16 -0800393 fprintf(stderr, " -j THREADS\tmaximum number of threads to use\n");
Josh Gao62aaf8f2016-06-02 14:27:21 -0700394 fprintf(stderr, " -h\t\tdisplay this message\n");
395 exit(0);
396 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700397}
398
399int main(int argc, char** argv) {
400 std::string cwd = getWorkingDir() + "/";
401 bool default_args = true;
402 std::string platform_dir;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700403 std::set<Arch> selected_architectures;
Josh Gaobf8a2852016-05-27 11:59:09 -0700404 std::set<int> selected_levels;
Josh Gaof8592a32016-07-26 18:58:27 -0700405 std::string preprocessor_output_path;
406 bool force = false;
Josh Gao16016df2016-11-07 18:27:16 -0800407 bool dump = false;
Josh Gaobf8a2852016-05-27 11:59:09 -0700408
409 int c;
Josh Gao16016df2016-11-07 18:27:16 -0800410 while ((c = getopt(argc, argv, "a:r:p:vo:fdj:hi")) != -1) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700411 default_args = false;
412 switch (c) {
413 case 'a': {
414 char* end;
415 int api_level = strtol(optarg, &end, 10);
416 if (end == optarg || strlen(end) > 0) {
417 usage();
418 }
419
420 if (supported_levels.count(api_level) == 0) {
421 errx(1, "unsupported API level %d", api_level);
422 }
423
424 selected_levels.insert(api_level);
425 break;
426 }
427
428 case 'r': {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700429 Arch arch = arch_from_string(optarg);
430 selected_architectures.insert(arch);
Josh Gaobf8a2852016-05-27 11:59:09 -0700431 break;
432 }
433
434 case 'p': {
435 if (!platform_dir.empty()) {
436 usage();
437 }
438
439 platform_dir = optarg;
440
Josh Gaof8592a32016-07-26 18:58:27 -0700441 if (platform_dir.empty()) {
442 usage();
443 }
444
Josh Gaobf8a2852016-05-27 11:59:09 -0700445 struct stat st;
446 if (stat(platform_dir.c_str(), &st) != 0) {
447 err(1, "failed to stat platform directory '%s'", platform_dir.c_str());
448 }
449 if (!S_ISDIR(st.st_mode)) {
450 errx(1, "'%s' is not a directory", optarg);
451 }
452 break;
453 }
454
455 case 'v':
456 verbose = true;
457 break;
458
Josh Gaof8592a32016-07-26 18:58:27 -0700459 case 'o':
460 if (!preprocessor_output_path.empty()) {
461 usage();
462 }
463 preprocessor_output_path = optarg;
464 if (preprocessor_output_path.empty()) {
465 usage();
466 }
467 break;
468
469 case 'f':
470 force = true;
471 break;
472
Josh Gaobfb6bae2016-07-15 17:25:21 -0700473 case 'd':
474 dump = true;
475 break;
476
Josh Gao16016df2016-11-07 18:27:16 -0800477 case 'j':
478 if (!android::base::ParseInt<int>(optarg, &max_thread_count, 1)) {
479 usage();
480 }
481 break;
482
Josh Gao62aaf8f2016-06-02 14:27:21 -0700483 case 'h':
484 usage(true);
485 break;
486
Josh Gaobfb6bae2016-07-15 17:25:21 -0700487 case 'i':
488 // Secret option for tests to -include <android/versioning.h>.
489 add_include = true;
490 break;
491
Josh Gaobf8a2852016-05-27 11:59:09 -0700492 default:
493 usage();
494 break;
495 }
496 }
497
Josh Gao9b5af7a2016-06-02 14:29:13 -0700498 if (argc - optind > 2 || optind > argc) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700499 usage();
500 }
501
Josh Gao9b5af7a2016-06-02 14:29:13 -0700502 std::string header_dir;
503 std::string dependency_dir;
504
Josh Gaobfb6bae2016-07-15 17:25:21 -0700505 const char* top = getenv("ANDROID_BUILD_TOP");
506 if (!top && (optind == argc || add_include)) {
507 fprintf(stderr, "versioner: failed to autodetect bionic paths. Is ANDROID_BUILD_TOP set?\n");
508 usage();
509 }
510
Josh Gao9b5af7a2016-06-02 14:29:13 -0700511 if (optind == argc) {
512 // Neither HEADER_PATH nor DEPS_PATH were specified, so try to figure them out.
Josh Gaobfb6bae2016-07-15 17:25:21 -0700513 std::string versioner_dir = to_string(top) + "/bionic/tools/versioner";
Josh Gao9b5af7a2016-06-02 14:29:13 -0700514 header_dir = versioner_dir + "/current";
515 dependency_dir = versioner_dir + "/dependencies";
516 if (platform_dir.empty()) {
517 platform_dir = versioner_dir + "/platforms";
518 }
519 } else {
Josh Gaof8592a32016-07-26 18:58:27 -0700520 // Intentional leak.
521 header_dir = realpath(argv[optind], nullptr);
Josh Gao9b5af7a2016-06-02 14:29:13 -0700522
523 if (argc - optind == 2) {
524 dependency_dir = argv[optind + 1];
525 }
526 }
527
Josh Gaobf8a2852016-05-27 11:59:09 -0700528 if (selected_levels.empty()) {
529 selected_levels = supported_levels;
530 }
531
532 if (selected_architectures.empty()) {
533 selected_architectures = supported_archs;
534 }
535
Josh Gaobf8a2852016-05-27 11:59:09 -0700536
537 struct stat st;
Josh Gao9b5af7a2016-06-02 14:29:13 -0700538 if (stat(header_dir.c_str(), &st) != 0) {
539 err(1, "failed to stat '%s'", header_dir.c_str());
Josh Gaobf8a2852016-05-27 11:59:09 -0700540 } else if (!S_ISDIR(st.st_mode)) {
Josh Gao9b5af7a2016-06-02 14:29:13 -0700541 errx(1, "'%s' is not a directory", header_dir.c_str());
Josh Gaobf8a2852016-05-27 11:59:09 -0700542 }
543
544 std::set<CompilationType> compilation_types;
Josh Gaobf8a2852016-05-27 11:59:09 -0700545 NdkSymbolDatabase symbol_database;
546
547 compilation_types = generateCompilationTypes(selected_architectures, selected_levels);
548
549 // Do this before compiling so that we can early exit if the platforms don't match what we
550 // expect.
551 if (!platform_dir.empty()) {
552 symbol_database = parsePlatforms(compilation_types, platform_dir);
553 }
554
Josh Gaobfb6bae2016-07-15 17:25:21 -0700555 std::unique_ptr<HeaderDatabase> declaration_database =
Josh Gao16016df2016-11-07 18:27:16 -0800556 compileHeaders(compilation_types, header_dir, dependency_dir);
Josh Gaobf8a2852016-05-27 11:59:09 -0700557
Josh Gao16016df2016-11-07 18:27:16 -0800558 bool failed = false;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700559 if (dump) {
560 declaration_database->dump(header_dir + "/");
561 } else {
562 if (!sanityCheck(declaration_database.get())) {
563 printf("versioner: sanity check failed\n");
Josh Gaobf8a2852016-05-27 11:59:09 -0700564 failed = true;
565 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700566
Josh Gaobfb6bae2016-07-15 17:25:21 -0700567 if (!platform_dir.empty()) {
568 if (!checkVersions(compilation_types, declaration_database.get(), symbol_database)) {
569 printf("versioner: version check failed\n");
570 failed = true;
571 }
572 }
573 }
Josh Gaof8592a32016-07-26 18:58:27 -0700574
575 if (!preprocessor_output_path.empty() && (force || !failed)) {
576 failed = !preprocessHeaders(preprocessor_output_path, header_dir, declaration_database.get());
577 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700578 return failed;
579}