blob: f0a23393e9c13ea95274b4fc3289f83583b997f4 [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"
47#include "versioner.h"
48
49using namespace std::string_literals;
Josh Gaobf8a2852016-05-27 11:59:09 -070050
Josh Gaob5c49632016-11-08 22:21:31 -080051bool add_include;
Josh Gaobf8a2852016-05-27 11:59:09 -070052bool verbose;
Josh Gao16016df2016-11-07 18:27:16 -080053static int max_thread_count = 48;
Josh Gaobf8a2852016-05-27 11:59:09 -070054
Josh Gaobfb6bae2016-07-15 17:25:21 -070055static CompilationRequirements collectRequirements(const Arch& arch, const std::string& header_dir,
Josh Gaobf8a2852016-05-27 11:59:09 -070056 const std::string& dependency_dir) {
57 std::vector<std::string> headers = collectFiles(header_dir);
Josh Gaobf8a2852016-05-27 11:59:09 -070058 std::vector<std::string> dependencies = { header_dir };
59 if (!dependency_dir.empty()) {
Josh Gaob5c49632016-11-08 22:21:31 -080060 auto collect_children = [&dependencies, &dependency_dir](const std::string& dir_path) {
Josh Gaobf8a2852016-05-27 11:59:09 -070061 DIR* dir = opendir(dir_path.c_str());
62 if (!dir) {
63 err(1, "failed to open dependency directory '%s'", dir_path.c_str());
64 }
65
66 struct dirent* dent;
67 while ((dent = readdir(dir))) {
68 if (dent->d_name[0] == '.') {
69 continue;
70 }
71
72 // TODO: Resolve symlinks.
73 std::string dependency = dir_path + "/" + dent->d_name;
74
75 struct stat st;
76 if (stat(dependency.c_str(), &st) != 0) {
77 err(1, "failed to stat dependency '%s'", dependency.c_str());
78 }
79
80 if (!S_ISDIR(st.st_mode)) {
81 errx(1, "'%s' is not a directory", dependency.c_str());
82 }
83
84 dependencies.push_back(dependency);
85 }
86
87 closedir(dir);
88 };
89
90 collect_children(dependency_dir + "/common");
Josh Gaobfb6bae2016-07-15 17:25:21 -070091 collect_children(dependency_dir + "/" + to_string(arch));
Josh Gaobf8a2852016-05-27 11:59:09 -070092 }
93
94 auto new_end = std::remove_if(headers.begin(), headers.end(), [&arch](llvm::StringRef header) {
95 for (const auto& it : header_blacklist) {
96 if (it.second.find(arch) == it.second.end()) {
97 continue;
98 }
99
100 if (header.endswith("/" + it.first)) {
101 return true;
102 }
103 }
104 return false;
105 });
106
107 headers.erase(new_end, headers.end());
108
109 CompilationRequirements result = { .headers = headers, .dependencies = dependencies };
110 return result;
111}
112
Josh Gaobfb6bae2016-07-15 17:25:21 -0700113static std::set<CompilationType> generateCompilationTypes(const std::set<Arch> selected_architectures,
114 const std::set<int>& selected_levels) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700115 std::set<CompilationType> result;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700116 for (const auto& arch : selected_architectures) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700117 int min_api = arch_min_api[arch];
118 for (int api_level : selected_levels) {
119 if (api_level < min_api) {
120 continue;
121 }
Josh Gaoa77b3a92016-08-15 16:39:27 -0700122
123 for (int file_offset_bits : { 32, 64 }) {
124 CompilationType type = {
125 .arch = arch, .api_level = api_level, .file_offset_bits = file_offset_bits
126 };
127 result.insert(type);
128 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700129 }
130 }
131 return result;
132}
133
Josh Gaobfb6bae2016-07-15 17:25:21 -0700134static std::unique_ptr<HeaderDatabase> compileHeaders(const std::set<CompilationType>& types,
135 const std::string& header_dir,
Josh Gao16016df2016-11-07 18:27:16 -0800136 const std::string& dependency_dir) {
137 if (types.empty()) {
138 errx(1, "compileHeaders received no CompilationTypes");
139 }
140
141 size_t thread_count = max_thread_count;
142 std::vector<std::thread> threads;
Josh Gaobf8a2852016-05-27 11:59:09 -0700143
144 std::map<CompilationType, HeaderDatabase> header_databases;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700145 std::unordered_map<Arch, CompilationRequirements> requirements;
Josh Gaobf8a2852016-05-27 11:59:09 -0700146
Josh Gao16016df2016-11-07 18:27:16 -0800147 auto result = std::make_unique<HeaderDatabase>();
Josh Gaobf8a2852016-05-27 11:59:09 -0700148 for (const auto& type : types) {
149 if (requirements.count(type.arch) == 0) {
150 requirements[type.arch] = collectRequirements(type.arch, header_dir, dependency_dir);
151 }
152 }
153
Josh Gaob5c49632016-11-08 22:21:31 -0800154 initializeTargetCC1FlagCache(types, requirements);
155
156 std::vector<std::pair<CompilationType, const std::string&>> jobs;
Josh Gao16016df2016-11-07 18:27:16 -0800157 for (CompilationType type : types) {
158 CompilationRequirements& req = requirements[type.arch];
159 for (const std::string& header : req.headers) {
Josh Gaob5c49632016-11-08 22:21:31 -0800160 jobs.emplace_back(type, header);
Josh Gaobf8a2852016-05-27 11:59:09 -0700161 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700162 }
163
Josh Gaob5c49632016-11-08 22:21:31 -0800164 thread_count = std::min(thread_count, jobs.size());
165
166 if (thread_count == 1) {
167 for (const auto& job : jobs) {
168 compileHeader(result.get(), job.first, job.second);
169 }
170 } else {
171 // Spawn threads.
172 for (size_t i = 0; i < thread_count; ++i) {
173 threads.emplace_back([&jobs, &result, &header_dir, thread_count, i]() {
174 size_t index = i;
175 while (index < jobs.size()) {
176 const auto& job = jobs[index];
177 compileHeader(result.get(), job.first, job.second);
178 index += thread_count;
179 }
180 });
181 }
182
183 // Reap them.
184 for (auto& thread : threads) {
185 thread.join();
186 }
187 threads.clear();
188 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700189
Josh Gaobfb6bae2016-07-15 17:25:21 -0700190 return result;
Josh Gaobf8a2852016-05-27 11:59:09 -0700191}
192
Josh Gaobfb6bae2016-07-15 17:25:21 -0700193// Perform a sanity check on a symbol's declarations, enforcing the following invariants:
194// 1. At most one inline definition of the function exists.
195// 2. All of the availability declarations for a symbol are compatible.
196// If a function is declared as an inline before a certain version, the inline definition
197// should have no version tag.
198// 3. Each availability type must only be present globally or on a per-arch basis.
199// (e.g. __INTRODUCED_IN_ARM(9) __INTRODUCED_IN_X86(10) __DEPRECATED_IN(11) is fine,
200// but not __INTRODUCED_IN(9) __INTRODUCED_IN_X86(10))
201static bool checkSymbol(const Symbol& symbol) {
202 std::string cwd = getWorkingDir() + "/";
203
204 const Declaration* inline_definition = nullptr;
205 for (const auto& decl_it : symbol.declarations) {
206 const Declaration* decl = &decl_it.second;
207 if (decl->is_definition) {
208 if (inline_definition) {
209 fprintf(stderr, "versioner: multiple definitions of symbol %s\n", symbol.name.c_str());
210 symbol.dump(cwd);
211 inline_definition->dump(cwd);
212 return false;
213 }
214
215 inline_definition = decl;
216 }
217
218 DeclarationAvailability availability;
219 if (!decl->calculateAvailability(&availability)) {
220 fprintf(stderr, "versioner: failed to calculate availability for declaration:\n");
Josh Gao566735d2016-08-02 15:07:32 -0700221 decl->dump(cwd, stderr, 2);
Josh Gaobfb6bae2016-07-15 17:25:21 -0700222 return false;
223 }
224
225 if (decl->is_definition && !availability.empty()) {
226 fprintf(stderr, "versioner: inline definition has non-empty versioning information:\n");
Josh Gao566735d2016-08-02 15:07:32 -0700227 decl->dump(cwd, stderr, 2);
Josh Gaobfb6bae2016-07-15 17:25:21 -0700228 return false;
229 }
230 }
231
232 DeclarationAvailability availability;
233 if (!symbol.calculateAvailability(&availability)) {
234 fprintf(stderr, "versioner: inconsistent availability for symbol '%s'\n", symbol.name.c_str());
235 symbol.dump(cwd);
236 return false;
237 }
238
239 // TODO: Check invariant #3.
240 return true;
241}
242
243static bool sanityCheck(const HeaderDatabase* database) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700244 bool error = false;
Josh Gao958f3b32016-06-03 13:44:00 -0700245 std::string cwd = getWorkingDir() + "/";
246
Josh Gaobfb6bae2016-07-15 17:25:21 -0700247 for (const auto& symbol_it : database->symbols) {
248 if (!checkSymbol(symbol_it.second)) {
249 error = true;
Josh Gaobf8a2852016-05-27 11:59:09 -0700250 }
251 }
252 return !error;
253}
254
255// Check that our symbol availability declarations match the actual NDK
256// platform symbol availability.
257static bool checkVersions(const std::set<CompilationType>& types,
Josh Gaobfb6bae2016-07-15 17:25:21 -0700258 const HeaderDatabase* header_database,
Josh Gaobf8a2852016-05-27 11:59:09 -0700259 const NdkSymbolDatabase& symbol_database) {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700260 std::string cwd = getWorkingDir() + "/";
Josh Gaobf8a2852016-05-27 11:59:09 -0700261 bool failed = false;
262
Josh Gaobfb6bae2016-07-15 17:25:21 -0700263 std::map<Arch, std::set<CompilationType>> arch_types;
Josh Gaobf8a2852016-05-27 11:59:09 -0700264 for (const CompilationType& type : types) {
265 arch_types[type.arch].insert(type);
266 }
267
Josh Gaod67dbf02016-06-02 15:21:14 -0700268 std::set<std::string> completely_unavailable;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700269 std::map<std::string, std::set<CompilationType>> missing_availability;
270 std::map<std::string, std::set<CompilationType>> extra_availability;
Josh Gaod67dbf02016-06-02 15:21:14 -0700271
Josh Gaobfb6bae2016-07-15 17:25:21 -0700272 for (const auto& symbol_it : header_database->symbols) {
273 const auto& symbol_name = symbol_it.first;
274 DeclarationAvailability symbol_availability;
Josh Gaobf8a2852016-05-27 11:59:09 -0700275
Josh Gaobfb6bae2016-07-15 17:25:21 -0700276 if (!symbol_it.second.calculateAvailability(&symbol_availability)) {
277 errx(1, "failed to calculate symbol availability");
278 }
279
280 const auto platform_availability_it = symbol_database.find(symbol_name);
Josh Gaobf8a2852016-05-27 11:59:09 -0700281 if (platform_availability_it == symbol_database.end()) {
Josh Gaod67dbf02016-06-02 15:21:14 -0700282 completely_unavailable.insert(symbol_name);
Josh Gaobf8a2852016-05-27 11:59:09 -0700283 continue;
284 }
285
286 const auto& platform_availability = platform_availability_it->second;
Josh Gaobf8a2852016-05-27 11:59:09 -0700287
288 for (const CompilationType& type : types) {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700289 bool should_be_available = true;
290 const auto& global_availability = symbol_availability.global_availability;
291 const auto& arch_availability = symbol_availability.arch_availability[type.arch];
292 if (global_availability.introduced != 0 && global_availability.introduced > type.api_level) {
293 should_be_available = false;
294 }
295
296 if (arch_availability.introduced != 0 && arch_availability.introduced > type.api_level) {
297 should_be_available = false;
298 }
299
300 if (global_availability.obsoleted != 0 && global_availability.obsoleted <= type.api_level) {
301 should_be_available = false;
302 }
303
304 if (arch_availability.obsoleted != 0 && arch_availability.obsoleted <= type.api_level) {
305 should_be_available = false;
306 }
307
308 if (arch_availability.future) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700309 continue;
310 }
311
Josh Gaobfb6bae2016-07-15 17:25:21 -0700312 // The function declaration might be (validly) missing for the given CompilationType.
313 if (!symbol_it.second.hasDeclaration(type)) {
314 should_be_available = false;
315 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700316
Josh Gaobfb6bae2016-07-15 17:25:21 -0700317 bool is_available = platform_availability.count(type);
Josh Gaobf8a2852016-05-27 11:59:09 -0700318
Josh Gaobfb6bae2016-07-15 17:25:21 -0700319 if (should_be_available != is_available) {
320 if (is_available) {
321 extra_availability[symbol_name].insert(type);
Josh Gaobf8a2852016-05-27 11:59:09 -0700322 } else {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700323 missing_availability[symbol_name].insert(type);
Josh Gaobf8a2852016-05-27 11:59:09 -0700324 }
325 }
326 }
Josh Gaobfb6bae2016-07-15 17:25:21 -0700327 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700328
Josh Gaobfb6bae2016-07-15 17:25:21 -0700329 for (const auto& it : symbol_database) {
330 const std::string& symbol_name = it.first;
Josh Gaobf8a2852016-05-27 11:59:09 -0700331
Josh Gaobfb6bae2016-07-15 17:25:21 -0700332 bool symbol_error = false;
333 auto missing_it = missing_availability.find(symbol_name);
334 if (missing_it != missing_availability.end()) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700335 printf("%s: declaration marked available but symbol missing in [%s]\n", symbol_name.c_str(),
Josh Gaobfb6bae2016-07-15 17:25:21 -0700336 Join(missing_it->second, ", ").c_str());
337 symbol_error = true;
Josh Gaobf8a2852016-05-27 11:59:09 -0700338 failed = true;
339 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700340
Josh Gaobfb6bae2016-07-15 17:25:21 -0700341 if (verbose) {
342 auto extra_it = extra_availability.find(symbol_name);
343 if (extra_it != extra_availability.end()) {
344 printf("%s: declaration marked unavailable but symbol available in [%s]\n",
345 symbol_name.c_str(), Join(extra_it->second, ", ").c_str());
346 symbol_error = true;
347 failed = true;
Josh Gao958f3b32016-06-03 13:44:00 -0700348 }
349 }
350
Josh Gaobfb6bae2016-07-15 17:25:21 -0700351 if (symbol_error) {
352 auto symbol_it = header_database->symbols.find(symbol_name);
353 if (symbol_it == header_database->symbols.end()) {
354 errx(1, "failed to find symbol in header database");
355 }
356 symbol_it->second.dump(cwd);
Josh Gaod67dbf02016-06-02 15:21:14 -0700357 }
Josh Gaod67dbf02016-06-02 15:21:14 -0700358 }
359
Josh Gaobfb6bae2016-07-15 17:25:21 -0700360 // TODO: Verify that function/variable declarations are actually function/variable symbols.
Josh Gaobf8a2852016-05-27 11:59:09 -0700361 return !failed;
362}
363
Josh Gao62aaf8f2016-06-02 14:27:21 -0700364static void usage(bool help = false) {
365 fprintf(stderr, "Usage: versioner [OPTION]... [HEADER_PATH] [DEPS_PATH]\n");
366 if (!help) {
367 printf("Try 'versioner -h' for more information.\n");
368 exit(1);
369 } else {
370 fprintf(stderr, "Version headers at HEADER_PATH, with DEPS_PATH/ARCH/* on the include path\n");
Josh Gao9b5af7a2016-06-02 14:29:13 -0700371 fprintf(stderr, "Autodetects paths if HEADER_PATH and DEPS_PATH are not specified\n");
Josh Gao62aaf8f2016-06-02 14:27:21 -0700372 fprintf(stderr, "\n");
373 fprintf(stderr, "Target specification (defaults to all):\n");
374 fprintf(stderr, " -a API_LEVEL\tbuild with specified API level (can be repeated)\n");
375 fprintf(stderr, " \t\tvalid levels are %s\n", Join(supported_levels).c_str());
376 fprintf(stderr, " -r ARCH\tbuild with specified architecture (can be repeated)\n");
377 fprintf(stderr, " \t\tvalid architectures are %s\n", Join(supported_archs).c_str());
378 fprintf(stderr, "\n");
379 fprintf(stderr, "Validation:\n");
380 fprintf(stderr, " -p PATH\tcompare against NDK platform at PATH\n");
381 fprintf(stderr, " -v\t\tenable verbose warnings\n");
382 fprintf(stderr, "\n");
Josh Gaof8592a32016-07-26 18:58:27 -0700383 fprintf(stderr, "Preprocessing:\n");
384 fprintf(stderr, " -o PATH\tpreprocess header files and emit them at PATH\n");
385 fprintf(stderr, " -f\tpreprocess header files even if validation fails\n");
386 fprintf(stderr, "\n");
Josh Gao62aaf8f2016-06-02 14:27:21 -0700387 fprintf(stderr, "Miscellaneous:\n");
Josh Gaobfb6bae2016-07-15 17:25:21 -0700388 fprintf(stderr, " -d\t\tdump function availability\n");
Josh Gao16016df2016-11-07 18:27:16 -0800389 fprintf(stderr, " -j THREADS\tmaximum number of threads to use\n");
Josh Gao62aaf8f2016-06-02 14:27:21 -0700390 fprintf(stderr, " -h\t\tdisplay this message\n");
391 exit(0);
392 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700393}
394
395int main(int argc, char** argv) {
396 std::string cwd = getWorkingDir() + "/";
397 bool default_args = true;
398 std::string platform_dir;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700399 std::set<Arch> selected_architectures;
Josh Gaobf8a2852016-05-27 11:59:09 -0700400 std::set<int> selected_levels;
Josh Gaof8592a32016-07-26 18:58:27 -0700401 std::string preprocessor_output_path;
402 bool force = false;
Josh Gao16016df2016-11-07 18:27:16 -0800403 bool dump = false;
Josh Gaobf8a2852016-05-27 11:59:09 -0700404
405 int c;
Josh Gao16016df2016-11-07 18:27:16 -0800406 while ((c = getopt(argc, argv, "a:r:p:vo:fdj:hi")) != -1) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700407 default_args = false;
408 switch (c) {
409 case 'a': {
410 char* end;
411 int api_level = strtol(optarg, &end, 10);
412 if (end == optarg || strlen(end) > 0) {
413 usage();
414 }
415
416 if (supported_levels.count(api_level) == 0) {
417 errx(1, "unsupported API level %d", api_level);
418 }
419
420 selected_levels.insert(api_level);
421 break;
422 }
423
424 case 'r': {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700425 Arch arch = arch_from_string(optarg);
426 selected_architectures.insert(arch);
Josh Gaobf8a2852016-05-27 11:59:09 -0700427 break;
428 }
429
430 case 'p': {
431 if (!platform_dir.empty()) {
432 usage();
433 }
434
435 platform_dir = optarg;
436
Josh Gaof8592a32016-07-26 18:58:27 -0700437 if (platform_dir.empty()) {
438 usage();
439 }
440
Josh Gaobf8a2852016-05-27 11:59:09 -0700441 struct stat st;
442 if (stat(platform_dir.c_str(), &st) != 0) {
443 err(1, "failed to stat platform directory '%s'", platform_dir.c_str());
444 }
445 if (!S_ISDIR(st.st_mode)) {
446 errx(1, "'%s' is not a directory", optarg);
447 }
448 break;
449 }
450
451 case 'v':
452 verbose = true;
453 break;
454
Josh Gaof8592a32016-07-26 18:58:27 -0700455 case 'o':
456 if (!preprocessor_output_path.empty()) {
457 usage();
458 }
459 preprocessor_output_path = optarg;
460 if (preprocessor_output_path.empty()) {
461 usage();
462 }
463 break;
464
465 case 'f':
466 force = true;
467 break;
468
Josh Gaobfb6bae2016-07-15 17:25:21 -0700469 case 'd':
470 dump = true;
471 break;
472
Josh Gao16016df2016-11-07 18:27:16 -0800473 case 'j':
474 if (!android::base::ParseInt<int>(optarg, &max_thread_count, 1)) {
475 usage();
476 }
477 break;
478
Josh Gao62aaf8f2016-06-02 14:27:21 -0700479 case 'h':
480 usage(true);
481 break;
482
Josh Gaobfb6bae2016-07-15 17:25:21 -0700483 case 'i':
484 // Secret option for tests to -include <android/versioning.h>.
485 add_include = true;
486 break;
487
Josh Gaobf8a2852016-05-27 11:59:09 -0700488 default:
489 usage();
490 break;
491 }
492 }
493
Josh Gao9b5af7a2016-06-02 14:29:13 -0700494 if (argc - optind > 2 || optind > argc) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700495 usage();
496 }
497
Josh Gao9b5af7a2016-06-02 14:29:13 -0700498 std::string header_dir;
499 std::string dependency_dir;
500
Josh Gaobfb6bae2016-07-15 17:25:21 -0700501 const char* top = getenv("ANDROID_BUILD_TOP");
502 if (!top && (optind == argc || add_include)) {
503 fprintf(stderr, "versioner: failed to autodetect bionic paths. Is ANDROID_BUILD_TOP set?\n");
504 usage();
505 }
506
Josh Gao9b5af7a2016-06-02 14:29:13 -0700507 if (optind == argc) {
508 // Neither HEADER_PATH nor DEPS_PATH were specified, so try to figure them out.
Josh Gaobfb6bae2016-07-15 17:25:21 -0700509 std::string versioner_dir = to_string(top) + "/bionic/tools/versioner";
Josh Gao9b5af7a2016-06-02 14:29:13 -0700510 header_dir = versioner_dir + "/current";
511 dependency_dir = versioner_dir + "/dependencies";
512 if (platform_dir.empty()) {
513 platform_dir = versioner_dir + "/platforms";
514 }
515 } else {
Josh Gaof8592a32016-07-26 18:58:27 -0700516 // Intentional leak.
517 header_dir = realpath(argv[optind], nullptr);
Josh Gao9b5af7a2016-06-02 14:29:13 -0700518
519 if (argc - optind == 2) {
520 dependency_dir = argv[optind + 1];
521 }
522 }
523
Josh Gaobf8a2852016-05-27 11:59:09 -0700524 if (selected_levels.empty()) {
525 selected_levels = supported_levels;
526 }
527
528 if (selected_architectures.empty()) {
529 selected_architectures = supported_archs;
530 }
531
Josh Gaobf8a2852016-05-27 11:59:09 -0700532
533 struct stat st;
Josh Gao9b5af7a2016-06-02 14:29:13 -0700534 if (stat(header_dir.c_str(), &st) != 0) {
535 err(1, "failed to stat '%s'", header_dir.c_str());
Josh Gaobf8a2852016-05-27 11:59:09 -0700536 } else if (!S_ISDIR(st.st_mode)) {
Josh Gao9b5af7a2016-06-02 14:29:13 -0700537 errx(1, "'%s' is not a directory", header_dir.c_str());
Josh Gaobf8a2852016-05-27 11:59:09 -0700538 }
539
540 std::set<CompilationType> compilation_types;
Josh Gaobf8a2852016-05-27 11:59:09 -0700541 NdkSymbolDatabase symbol_database;
542
543 compilation_types = generateCompilationTypes(selected_architectures, selected_levels);
544
545 // Do this before compiling so that we can early exit if the platforms don't match what we
546 // expect.
547 if (!platform_dir.empty()) {
548 symbol_database = parsePlatforms(compilation_types, platform_dir);
549 }
550
Josh Gaobfb6bae2016-07-15 17:25:21 -0700551 std::unique_ptr<HeaderDatabase> declaration_database =
Josh Gao16016df2016-11-07 18:27:16 -0800552 compileHeaders(compilation_types, header_dir, dependency_dir);
Josh Gaobf8a2852016-05-27 11:59:09 -0700553
Josh Gao16016df2016-11-07 18:27:16 -0800554 bool failed = false;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700555 if (dump) {
556 declaration_database->dump(header_dir + "/");
557 } else {
558 if (!sanityCheck(declaration_database.get())) {
559 printf("versioner: sanity check failed\n");
Josh Gaobf8a2852016-05-27 11:59:09 -0700560 failed = true;
561 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700562
Josh Gaobfb6bae2016-07-15 17:25:21 -0700563 if (!platform_dir.empty()) {
564 if (!checkVersions(compilation_types, declaration_database.get(), symbol_database)) {
565 printf("versioner: version check failed\n");
566 failed = true;
567 }
568 }
569 }
Josh Gaof8592a32016-07-26 18:58:27 -0700570
571 if (!preprocessor_output_path.empty() && (force || !failed)) {
572 failed = !preprocessHeaders(preprocessor_output_path, header_dir, declaration_database.get());
573 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700574 return failed;
575}