blob: b46847b08c909909317d02fd58bc3f336baca4b5 [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
Josh Gao338cf122016-11-09 18:01:41 -080025#if defined(__linux__)
26#include <sched.h>
27#endif
28
Josh Gaobf8a2852016-05-27 11:59:09 -070029#include <atomic>
Josh Gao338cf122016-11-09 18:01:41 -080030#include <chrono>
Josh Gao16016df2016-11-07 18:27:16 -080031#include <functional>
Josh Gaobf8a2852016-05-27 11:59:09 -070032#include <iostream>
33#include <map>
34#include <memory>
35#include <set>
36#include <sstream>
37#include <string>
38#include <thread>
39#include <unordered_map>
40#include <vector>
41
Josh Gaobf8a2852016-05-27 11:59:09 -070042#include <llvm/ADT/StringRef.h>
43
Josh Gao338cf122016-11-09 18:01:41 -080044#include <android-base/macros.h>
Josh Gao16016df2016-11-07 18:27:16 -080045#include <android-base/parseint.h>
46
Josh Gaobfb6bae2016-07-15 17:25:21 -070047#include "Arch.h"
Josh Gaobf8a2852016-05-27 11:59:09 -070048#include "DeclarationDatabase.h"
Josh Gaob5c49632016-11-08 22:21:31 -080049#include "Driver.h"
Josh Gaof8592a32016-07-26 18:58:27 -070050#include "Preprocessor.h"
Josh Gaobf8a2852016-05-27 11:59:09 -070051#include "SymbolDatabase.h"
52#include "Utils.h"
Josh Gao78b8a142016-11-09 01:00:41 -080053#include "VFS.h"
54
Josh Gaobf8a2852016-05-27 11:59:09 -070055#include "versioner.h"
56
Josh Gao338cf122016-11-09 18:01:41 -080057using namespace std::chrono_literals;
Josh Gaobf8a2852016-05-27 11:59:09 -070058using namespace std::string_literals;
Josh Gaobf8a2852016-05-27 11:59:09 -070059
Josh Gaob5c49632016-11-08 22:21:31 -080060bool add_include;
Josh Gaobf8a2852016-05-27 11:59:09 -070061bool verbose;
Josh Gao338cf122016-11-09 18:01:41 -080062
63static int getCpuCount();
64static int max_thread_count = getCpuCount();
65
66static int getCpuCount() {
67#if defined(__linux__)
68 cpu_set_t cpu_set;
69 int rc = sched_getaffinity(getpid(), sizeof(cpu_set), &cpu_set);
70 if (rc != 0) {
71 err(1, "sched_getaffinity failed");
72 }
73 return CPU_COUNT(&cpu_set);
74#else
75 return 1;
76#endif
77}
Josh Gaobf8a2852016-05-27 11:59:09 -070078
Josh Gaobfb6bae2016-07-15 17:25:21 -070079static CompilationRequirements collectRequirements(const Arch& arch, const std::string& header_dir,
Josh Gaobf8a2852016-05-27 11:59:09 -070080 const std::string& dependency_dir) {
81 std::vector<std::string> headers = collectFiles(header_dir);
Josh Gaobf8a2852016-05-27 11:59:09 -070082 std::vector<std::string> dependencies = { header_dir };
83 if (!dependency_dir.empty()) {
Josh Gaob5c49632016-11-08 22:21:31 -080084 auto collect_children = [&dependencies, &dependency_dir](const std::string& dir_path) {
Josh Gaobf8a2852016-05-27 11:59:09 -070085 DIR* dir = opendir(dir_path.c_str());
86 if (!dir) {
87 err(1, "failed to open dependency directory '%s'", dir_path.c_str());
88 }
89
90 struct dirent* dent;
91 while ((dent = readdir(dir))) {
92 if (dent->d_name[0] == '.') {
93 continue;
94 }
95
96 // TODO: Resolve symlinks.
97 std::string dependency = dir_path + "/" + dent->d_name;
98
99 struct stat st;
100 if (stat(dependency.c_str(), &st) != 0) {
101 err(1, "failed to stat dependency '%s'", dependency.c_str());
102 }
103
104 if (!S_ISDIR(st.st_mode)) {
105 errx(1, "'%s' is not a directory", dependency.c_str());
106 }
107
108 dependencies.push_back(dependency);
109 }
110
111 closedir(dir);
112 };
113
114 collect_children(dependency_dir + "/common");
Josh Gaobfb6bae2016-07-15 17:25:21 -0700115 collect_children(dependency_dir + "/" + to_string(arch));
Josh Gaobf8a2852016-05-27 11:59:09 -0700116 }
117
118 auto new_end = std::remove_if(headers.begin(), headers.end(), [&arch](llvm::StringRef header) {
119 for (const auto& it : header_blacklist) {
120 if (it.second.find(arch) == it.second.end()) {
121 continue;
122 }
123
124 if (header.endswith("/" + it.first)) {
125 return true;
126 }
127 }
128 return false;
129 });
130
131 headers.erase(new_end, headers.end());
132
133 CompilationRequirements result = { .headers = headers, .dependencies = dependencies };
134 return result;
135}
136
Josh Gaobfb6bae2016-07-15 17:25:21 -0700137static std::set<CompilationType> generateCompilationTypes(const std::set<Arch> selected_architectures,
138 const std::set<int>& selected_levels) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700139 std::set<CompilationType> result;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700140 for (const auto& arch : selected_architectures) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700141 int min_api = arch_min_api[arch];
142 for (int api_level : selected_levels) {
143 if (api_level < min_api) {
144 continue;
145 }
Josh Gaoa77b3a92016-08-15 16:39:27 -0700146
147 for (int file_offset_bits : { 32, 64 }) {
148 CompilationType type = {
149 .arch = arch, .api_level = api_level, .file_offset_bits = file_offset_bits
150 };
151 result.insert(type);
152 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700153 }
154 }
155 return result;
156}
157
Josh Gaobfb6bae2016-07-15 17:25:21 -0700158static std::unique_ptr<HeaderDatabase> compileHeaders(const std::set<CompilationType>& types,
159 const std::string& header_dir,
Josh Gao16016df2016-11-07 18:27:16 -0800160 const std::string& dependency_dir) {
161 if (types.empty()) {
162 errx(1, "compileHeaders received no CompilationTypes");
163 }
164
Josh Gao78b8a142016-11-09 01:00:41 -0800165 auto vfs = createCommonVFS(header_dir, dependency_dir, add_include);
166
Josh Gao16016df2016-11-07 18:27:16 -0800167 size_t thread_count = max_thread_count;
168 std::vector<std::thread> threads;
Josh Gaobf8a2852016-05-27 11:59:09 -0700169
170 std::map<CompilationType, HeaderDatabase> header_databases;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700171 std::unordered_map<Arch, CompilationRequirements> requirements;
Josh Gaobf8a2852016-05-27 11:59:09 -0700172
Josh Gao16016df2016-11-07 18:27:16 -0800173 auto result = std::make_unique<HeaderDatabase>();
Josh Gaobf8a2852016-05-27 11:59:09 -0700174 for (const auto& type : types) {
175 if (requirements.count(type.arch) == 0) {
176 requirements[type.arch] = collectRequirements(type.arch, header_dir, dependency_dir);
177 }
178 }
179
Josh Gao78b8a142016-11-09 01:00:41 -0800180 initializeTargetCC1FlagCache(vfs, types, requirements);
Josh Gaob5c49632016-11-08 22:21:31 -0800181
182 std::vector<std::pair<CompilationType, const std::string&>> jobs;
Josh Gao338cf122016-11-09 18:01:41 -0800183 std::atomic<size_t> job_index(0);
Josh Gao16016df2016-11-07 18:27:16 -0800184 for (CompilationType type : types) {
185 CompilationRequirements& req = requirements[type.arch];
186 for (const std::string& header : req.headers) {
Josh Gaob5c49632016-11-08 22:21:31 -0800187 jobs.emplace_back(type, header);
Josh Gaobf8a2852016-05-27 11:59:09 -0700188 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700189 }
190
Josh Gaob5c49632016-11-08 22:21:31 -0800191 thread_count = std::min(thread_count, jobs.size());
192
193 if (thread_count == 1) {
194 for (const auto& job : jobs) {
Josh Gao78b8a142016-11-09 01:00:41 -0800195 compileHeader(vfs, result.get(), job.first, job.second);
Josh Gaob5c49632016-11-08 22:21:31 -0800196 }
197 } else {
198 // Spawn threads.
Josh Gao338cf122016-11-09 18:01:41 -0800199 size_t cpu_count = getCpuCount();
Josh Gaob5c49632016-11-08 22:21:31 -0800200 for (size_t i = 0; i < thread_count; ++i) {
Josh Gao338cf122016-11-09 18:01:41 -0800201 threads.emplace_back([&jobs, &job_index, &result, &header_dir, vfs, cpu_count, i]() {
202 while (true) {
203 size_t idx = job_index++;
204 if (idx >= jobs.size()) {
205 return;
206 }
207
208 const auto& job = jobs[idx];
Josh Gao78b8a142016-11-09 01:00:41 -0800209 compileHeader(vfs, result.get(), job.first, job.second);
Josh Gaob5c49632016-11-08 22:21:31 -0800210 }
211 });
Josh Gaobf8a2852016-05-27 11:59:09 -0700212 }
213
Josh Gaob5c49632016-11-08 22:21:31 -0800214 // Reap them.
215 for (auto& thread : threads) {
216 thread.join();
217 }
218 threads.clear();
Josh Gaobf8a2852016-05-27 11:59:09 -0700219 }
220
Josh Gaobfb6bae2016-07-15 17:25:21 -0700221 return result;
Josh Gaobf8a2852016-05-27 11:59:09 -0700222}
223
Josh Gao1a176de2016-11-04 13:15:11 -0700224static std::set<CompilationType> getCompilationTypes(const Declaration* decl) {
225 std::set<CompilationType> result;
226 for (const auto& it : decl->availability) {
227 result.insert(it.first);
228 }
229 return result;
230}
231
232template<typename T>
233static std::vector<T> Intersection(const std::set<T>& a, const std::set<T>& b) {
234 std::vector<T> intersection;
235 std::set_intersection(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(intersection));
236 return intersection;
237}
238
Josh Gaobfb6bae2016-07-15 17:25:21 -0700239// Perform a sanity check on a symbol's declarations, enforcing the following invariants:
240// 1. At most one inline definition of the function exists.
241// 2. All of the availability declarations for a symbol are compatible.
242// If a function is declared as an inline before a certain version, the inline definition
243// should have no version tag.
244// 3. Each availability type must only be present globally or on a per-arch basis.
245// (e.g. __INTRODUCED_IN_ARM(9) __INTRODUCED_IN_X86(10) __DEPRECATED_IN(11) is fine,
246// but not __INTRODUCED_IN(9) __INTRODUCED_IN_X86(10))
247static bool checkSymbol(const Symbol& symbol) {
248 std::string cwd = getWorkingDir() + "/";
249
Josh Gao1a176de2016-11-04 13:15:11 -0700250 std::unordered_map<const Declaration*, std::set<CompilationType>> inline_definitions;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700251 for (const auto& decl_it : symbol.declarations) {
252 const Declaration* decl = &decl_it.second;
253 if (decl->is_definition) {
Josh Gao1a176de2016-11-04 13:15:11 -0700254 std::set<CompilationType> compilation_types = getCompilationTypes(decl);
255 for (const auto& inline_def_it : inline_definitions) {
256 auto intersection = Intersection(compilation_types, inline_def_it.second);
257 if (!intersection.empty()) {
258 fprintf(stderr, "versioner: conflicting inline definitions:\n");
259 fprintf(stderr, " declarations visible in: %s\n", Join(intersection, ", ").c_str());
260 decl->dump(cwd, stderr, 4);
261 inline_def_it.first->dump(cwd, stderr, 4);
262 return false;
263 }
Josh Gaobfb6bae2016-07-15 17:25:21 -0700264 }
265
Josh Gao1a176de2016-11-04 13:15:11 -0700266 inline_definitions[decl] = std::move(compilation_types);
Josh Gaobfb6bae2016-07-15 17:25:21 -0700267 }
268
269 DeclarationAvailability availability;
270 if (!decl->calculateAvailability(&availability)) {
271 fprintf(stderr, "versioner: failed to calculate availability for declaration:\n");
Josh Gao566735d2016-08-02 15:07:32 -0700272 decl->dump(cwd, stderr, 2);
Josh Gaobfb6bae2016-07-15 17:25:21 -0700273 return false;
274 }
275
276 if (decl->is_definition && !availability.empty()) {
277 fprintf(stderr, "versioner: inline definition has non-empty versioning information:\n");
Josh Gao566735d2016-08-02 15:07:32 -0700278 decl->dump(cwd, stderr, 2);
Josh Gaobfb6bae2016-07-15 17:25:21 -0700279 return false;
280 }
281 }
282
283 DeclarationAvailability availability;
284 if (!symbol.calculateAvailability(&availability)) {
285 fprintf(stderr, "versioner: inconsistent availability for symbol '%s'\n", symbol.name.c_str());
286 symbol.dump(cwd);
287 return false;
288 }
289
290 // TODO: Check invariant #3.
291 return true;
292}
293
294static bool sanityCheck(const HeaderDatabase* database) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700295 bool error = false;
Josh Gao958f3b32016-06-03 13:44:00 -0700296 std::string cwd = getWorkingDir() + "/";
297
Josh Gaobfb6bae2016-07-15 17:25:21 -0700298 for (const auto& symbol_it : database->symbols) {
299 if (!checkSymbol(symbol_it.second)) {
300 error = true;
Josh Gaobf8a2852016-05-27 11:59:09 -0700301 }
302 }
303 return !error;
304}
305
306// Check that our symbol availability declarations match the actual NDK
307// platform symbol availability.
308static bool checkVersions(const std::set<CompilationType>& types,
Josh Gaobfb6bae2016-07-15 17:25:21 -0700309 const HeaderDatabase* header_database,
Josh Gaobf8a2852016-05-27 11:59:09 -0700310 const NdkSymbolDatabase& symbol_database) {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700311 std::string cwd = getWorkingDir() + "/";
Josh Gaobf8a2852016-05-27 11:59:09 -0700312 bool failed = false;
313
Josh Gaobfb6bae2016-07-15 17:25:21 -0700314 std::map<Arch, std::set<CompilationType>> arch_types;
Josh Gaobf8a2852016-05-27 11:59:09 -0700315 for (const CompilationType& type : types) {
316 arch_types[type.arch].insert(type);
317 }
318
Josh Gaod67dbf02016-06-02 15:21:14 -0700319 std::set<std::string> completely_unavailable;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700320 std::map<std::string, std::set<CompilationType>> missing_availability;
321 std::map<std::string, std::set<CompilationType>> extra_availability;
Josh Gaod67dbf02016-06-02 15:21:14 -0700322
Josh Gaobfb6bae2016-07-15 17:25:21 -0700323 for (const auto& symbol_it : header_database->symbols) {
324 const auto& symbol_name = symbol_it.first;
325 DeclarationAvailability symbol_availability;
Josh Gaobf8a2852016-05-27 11:59:09 -0700326
Josh Gaobfb6bae2016-07-15 17:25:21 -0700327 if (!symbol_it.second.calculateAvailability(&symbol_availability)) {
328 errx(1, "failed to calculate symbol availability");
329 }
330
331 const auto platform_availability_it = symbol_database.find(symbol_name);
Josh Gaobf8a2852016-05-27 11:59:09 -0700332 if (platform_availability_it == symbol_database.end()) {
Josh Gaod67dbf02016-06-02 15:21:14 -0700333 completely_unavailable.insert(symbol_name);
Josh Gaobf8a2852016-05-27 11:59:09 -0700334 continue;
335 }
336
337 const auto& platform_availability = platform_availability_it->second;
Josh Gaobf8a2852016-05-27 11:59:09 -0700338
339 for (const CompilationType& type : types) {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700340 bool should_be_available = true;
341 const auto& global_availability = symbol_availability.global_availability;
342 const auto& arch_availability = symbol_availability.arch_availability[type.arch];
343 if (global_availability.introduced != 0 && global_availability.introduced > type.api_level) {
344 should_be_available = false;
345 }
346
347 if (arch_availability.introduced != 0 && arch_availability.introduced > type.api_level) {
348 should_be_available = false;
349 }
350
351 if (global_availability.obsoleted != 0 && global_availability.obsoleted <= type.api_level) {
352 should_be_available = false;
353 }
354
355 if (arch_availability.obsoleted != 0 && arch_availability.obsoleted <= type.api_level) {
356 should_be_available = false;
357 }
358
359 if (arch_availability.future) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700360 continue;
361 }
362
Josh Gaobfb6bae2016-07-15 17:25:21 -0700363 // The function declaration might be (validly) missing for the given CompilationType.
364 if (!symbol_it.second.hasDeclaration(type)) {
365 should_be_available = false;
366 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700367
Josh Gaobfb6bae2016-07-15 17:25:21 -0700368 bool is_available = platform_availability.count(type);
Josh Gaobf8a2852016-05-27 11:59:09 -0700369
Josh Gaobfb6bae2016-07-15 17:25:21 -0700370 if (should_be_available != is_available) {
371 if (is_available) {
372 extra_availability[symbol_name].insert(type);
Josh Gaobf8a2852016-05-27 11:59:09 -0700373 } else {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700374 missing_availability[symbol_name].insert(type);
Josh Gaobf8a2852016-05-27 11:59:09 -0700375 }
376 }
377 }
Josh Gaobfb6bae2016-07-15 17:25:21 -0700378 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700379
Josh Gaobfb6bae2016-07-15 17:25:21 -0700380 for (const auto& it : symbol_database) {
381 const std::string& symbol_name = it.first;
Josh Gaobf8a2852016-05-27 11:59:09 -0700382
Josh Gaobfb6bae2016-07-15 17:25:21 -0700383 bool symbol_error = false;
384 auto missing_it = missing_availability.find(symbol_name);
385 if (missing_it != missing_availability.end()) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700386 printf("%s: declaration marked available but symbol missing in [%s]\n", symbol_name.c_str(),
Josh Gaobfb6bae2016-07-15 17:25:21 -0700387 Join(missing_it->second, ", ").c_str());
388 symbol_error = true;
Josh Gaobf8a2852016-05-27 11:59:09 -0700389 failed = true;
390 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700391
Josh Gaobfb6bae2016-07-15 17:25:21 -0700392 if (verbose) {
393 auto extra_it = extra_availability.find(symbol_name);
394 if (extra_it != extra_availability.end()) {
395 printf("%s: declaration marked unavailable but symbol available in [%s]\n",
396 symbol_name.c_str(), Join(extra_it->second, ", ").c_str());
397 symbol_error = true;
398 failed = true;
Josh Gao958f3b32016-06-03 13:44:00 -0700399 }
400 }
401
Josh Gaobfb6bae2016-07-15 17:25:21 -0700402 if (symbol_error) {
403 auto symbol_it = header_database->symbols.find(symbol_name);
404 if (symbol_it == header_database->symbols.end()) {
405 errx(1, "failed to find symbol in header database");
406 }
407 symbol_it->second.dump(cwd);
Josh Gaod67dbf02016-06-02 15:21:14 -0700408 }
Josh Gaod67dbf02016-06-02 15:21:14 -0700409 }
410
Josh Gaobfb6bae2016-07-15 17:25:21 -0700411 // TODO: Verify that function/variable declarations are actually function/variable symbols.
Josh Gaobf8a2852016-05-27 11:59:09 -0700412 return !failed;
413}
414
Josh Gao62aaf8f2016-06-02 14:27:21 -0700415static void usage(bool help = false) {
416 fprintf(stderr, "Usage: versioner [OPTION]... [HEADER_PATH] [DEPS_PATH]\n");
417 if (!help) {
418 printf("Try 'versioner -h' for more information.\n");
419 exit(1);
420 } else {
421 fprintf(stderr, "Version headers at HEADER_PATH, with DEPS_PATH/ARCH/* on the include path\n");
Josh Gao9b5af7a2016-06-02 14:29:13 -0700422 fprintf(stderr, "Autodetects paths if HEADER_PATH and DEPS_PATH are not specified\n");
Josh Gao62aaf8f2016-06-02 14:27:21 -0700423 fprintf(stderr, "\n");
424 fprintf(stderr, "Target specification (defaults to all):\n");
425 fprintf(stderr, " -a API_LEVEL\tbuild with specified API level (can be repeated)\n");
426 fprintf(stderr, " \t\tvalid levels are %s\n", Join(supported_levels).c_str());
427 fprintf(stderr, " -r ARCH\tbuild with specified architecture (can be repeated)\n");
428 fprintf(stderr, " \t\tvalid architectures are %s\n", Join(supported_archs).c_str());
429 fprintf(stderr, "\n");
430 fprintf(stderr, "Validation:\n");
431 fprintf(stderr, " -p PATH\tcompare against NDK platform at PATH\n");
432 fprintf(stderr, " -v\t\tenable verbose warnings\n");
433 fprintf(stderr, "\n");
Josh Gaof8592a32016-07-26 18:58:27 -0700434 fprintf(stderr, "Preprocessing:\n");
435 fprintf(stderr, " -o PATH\tpreprocess header files and emit them at PATH\n");
436 fprintf(stderr, " -f\tpreprocess header files even if validation fails\n");
437 fprintf(stderr, "\n");
Josh Gao62aaf8f2016-06-02 14:27:21 -0700438 fprintf(stderr, "Miscellaneous:\n");
Josh Gaobfb6bae2016-07-15 17:25:21 -0700439 fprintf(stderr, " -d\t\tdump function availability\n");
Josh Gao16016df2016-11-07 18:27:16 -0800440 fprintf(stderr, " -j THREADS\tmaximum number of threads to use\n");
Josh Gao62aaf8f2016-06-02 14:27:21 -0700441 fprintf(stderr, " -h\t\tdisplay this message\n");
442 exit(0);
443 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700444}
445
446int main(int argc, char** argv) {
447 std::string cwd = getWorkingDir() + "/";
448 bool default_args = true;
449 std::string platform_dir;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700450 std::set<Arch> selected_architectures;
Josh Gaobf8a2852016-05-27 11:59:09 -0700451 std::set<int> selected_levels;
Josh Gaof8592a32016-07-26 18:58:27 -0700452 std::string preprocessor_output_path;
453 bool force = false;
Josh Gao16016df2016-11-07 18:27:16 -0800454 bool dump = false;
Josh Gaobf8a2852016-05-27 11:59:09 -0700455
456 int c;
Josh Gao16016df2016-11-07 18:27:16 -0800457 while ((c = getopt(argc, argv, "a:r:p:vo:fdj:hi")) != -1) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700458 default_args = false;
459 switch (c) {
460 case 'a': {
461 char* end;
462 int api_level = strtol(optarg, &end, 10);
463 if (end == optarg || strlen(end) > 0) {
464 usage();
465 }
466
467 if (supported_levels.count(api_level) == 0) {
468 errx(1, "unsupported API level %d", api_level);
469 }
470
471 selected_levels.insert(api_level);
472 break;
473 }
474
475 case 'r': {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700476 Arch arch = arch_from_string(optarg);
477 selected_architectures.insert(arch);
Josh Gaobf8a2852016-05-27 11:59:09 -0700478 break;
479 }
480
481 case 'p': {
482 if (!platform_dir.empty()) {
483 usage();
484 }
485
486 platform_dir = optarg;
487
Josh Gaof8592a32016-07-26 18:58:27 -0700488 if (platform_dir.empty()) {
489 usage();
490 }
491
Josh Gaobf8a2852016-05-27 11:59:09 -0700492 struct stat st;
493 if (stat(platform_dir.c_str(), &st) != 0) {
494 err(1, "failed to stat platform directory '%s'", platform_dir.c_str());
495 }
496 if (!S_ISDIR(st.st_mode)) {
497 errx(1, "'%s' is not a directory", optarg);
498 }
499 break;
500 }
501
502 case 'v':
503 verbose = true;
504 break;
505
Josh Gaof8592a32016-07-26 18:58:27 -0700506 case 'o':
507 if (!preprocessor_output_path.empty()) {
508 usage();
509 }
510 preprocessor_output_path = optarg;
511 if (preprocessor_output_path.empty()) {
512 usage();
513 }
514 break;
515
516 case 'f':
517 force = true;
518 break;
519
Josh Gaobfb6bae2016-07-15 17:25:21 -0700520 case 'd':
521 dump = true;
522 break;
523
Josh Gao16016df2016-11-07 18:27:16 -0800524 case 'j':
525 if (!android::base::ParseInt<int>(optarg, &max_thread_count, 1)) {
526 usage();
527 }
528 break;
529
Josh Gao62aaf8f2016-06-02 14:27:21 -0700530 case 'h':
531 usage(true);
532 break;
533
Josh Gaobfb6bae2016-07-15 17:25:21 -0700534 case 'i':
535 // Secret option for tests to -include <android/versioning.h>.
536 add_include = true;
537 break;
538
Josh Gaobf8a2852016-05-27 11:59:09 -0700539 default:
540 usage();
541 break;
542 }
543 }
544
Josh Gao9b5af7a2016-06-02 14:29:13 -0700545 if (argc - optind > 2 || optind > argc) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700546 usage();
547 }
548
Josh Gao9b5af7a2016-06-02 14:29:13 -0700549 std::string header_dir;
550 std::string dependency_dir;
551
Josh Gaobfb6bae2016-07-15 17:25:21 -0700552 const char* top = getenv("ANDROID_BUILD_TOP");
553 if (!top && (optind == argc || add_include)) {
554 fprintf(stderr, "versioner: failed to autodetect bionic paths. Is ANDROID_BUILD_TOP set?\n");
555 usage();
556 }
557
Josh Gao9b5af7a2016-06-02 14:29:13 -0700558 if (optind == argc) {
559 // Neither HEADER_PATH nor DEPS_PATH were specified, so try to figure them out.
Josh Gaobfb6bae2016-07-15 17:25:21 -0700560 std::string versioner_dir = to_string(top) + "/bionic/tools/versioner";
Josh Gao9b5af7a2016-06-02 14:29:13 -0700561 header_dir = versioner_dir + "/current";
562 dependency_dir = versioner_dir + "/dependencies";
563 if (platform_dir.empty()) {
564 platform_dir = versioner_dir + "/platforms";
565 }
566 } else {
Josh Gaof8592a32016-07-26 18:58:27 -0700567 // Intentional leak.
568 header_dir = realpath(argv[optind], nullptr);
Josh Gao9b5af7a2016-06-02 14:29:13 -0700569
570 if (argc - optind == 2) {
571 dependency_dir = argv[optind + 1];
572 }
573 }
574
Josh Gaobf8a2852016-05-27 11:59:09 -0700575 if (selected_levels.empty()) {
576 selected_levels = supported_levels;
577 }
578
579 if (selected_architectures.empty()) {
580 selected_architectures = supported_archs;
581 }
582
Josh Gaobf8a2852016-05-27 11:59:09 -0700583
584 struct stat st;
Josh Gao9b5af7a2016-06-02 14:29:13 -0700585 if (stat(header_dir.c_str(), &st) != 0) {
586 err(1, "failed to stat '%s'", header_dir.c_str());
Josh Gaobf8a2852016-05-27 11:59:09 -0700587 } else if (!S_ISDIR(st.st_mode)) {
Josh Gao9b5af7a2016-06-02 14:29:13 -0700588 errx(1, "'%s' is not a directory", header_dir.c_str());
Josh Gaobf8a2852016-05-27 11:59:09 -0700589 }
590
591 std::set<CompilationType> compilation_types;
Josh Gaobf8a2852016-05-27 11:59:09 -0700592 NdkSymbolDatabase symbol_database;
593
594 compilation_types = generateCompilationTypes(selected_architectures, selected_levels);
595
596 // Do this before compiling so that we can early exit if the platforms don't match what we
597 // expect.
598 if (!platform_dir.empty()) {
599 symbol_database = parsePlatforms(compilation_types, platform_dir);
600 }
601
Josh Gao338cf122016-11-09 18:01:41 -0800602 auto start = std::chrono::high_resolution_clock::now();
Josh Gaobfb6bae2016-07-15 17:25:21 -0700603 std::unique_ptr<HeaderDatabase> declaration_database =
Josh Gao16016df2016-11-07 18:27:16 -0800604 compileHeaders(compilation_types, header_dir, dependency_dir);
Josh Gao338cf122016-11-09 18:01:41 -0800605 auto end = std::chrono::high_resolution_clock::now();
606
607 if (verbose) {
608 auto diff = (end - start) / 1.0ms;
609 printf("Compiled headers for %zu targets in %0.2LFms\n", compilation_types.size(), diff);
610 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700611
Josh Gao16016df2016-11-07 18:27:16 -0800612 bool failed = false;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700613 if (dump) {
614 declaration_database->dump(header_dir + "/");
615 } else {
616 if (!sanityCheck(declaration_database.get())) {
617 printf("versioner: sanity check failed\n");
Josh Gaobf8a2852016-05-27 11:59:09 -0700618 failed = true;
619 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700620
Josh Gaobfb6bae2016-07-15 17:25:21 -0700621 if (!platform_dir.empty()) {
622 if (!checkVersions(compilation_types, declaration_database.get(), symbol_database)) {
623 printf("versioner: version check failed\n");
624 failed = true;
625 }
626 }
627 }
Josh Gaof8592a32016-07-26 18:58:27 -0700628
629 if (!preprocessor_output_path.empty() && (force || !failed)) {
630 failed = !preprocessHeaders(preprocessor_output_path, header_dir, declaration_database.get());
631 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700632 return failed;
633}