blob: 59d41da78edc33e4bb3000a7054a0fa14281ea25 [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 Gaoacc3d802016-11-09 18:22:44 -080060bool strict;
Josh Gaobf8a2852016-05-27 11:59:09 -070061bool verbose;
Josh Gaoacc3d802016-11-09 18:22:44 -080062bool add_include;
Josh Gao338cf122016-11-09 18:01:41 -080063
64static int getCpuCount();
65static int max_thread_count = getCpuCount();
66
67static int getCpuCount() {
68#if defined(__linux__)
69 cpu_set_t cpu_set;
70 int rc = sched_getaffinity(getpid(), sizeof(cpu_set), &cpu_set);
71 if (rc != 0) {
72 err(1, "sched_getaffinity failed");
73 }
74 return CPU_COUNT(&cpu_set);
75#else
76 return 1;
77#endif
78}
Josh Gaobf8a2852016-05-27 11:59:09 -070079
Josh Gaobfb6bae2016-07-15 17:25:21 -070080static CompilationRequirements collectRequirements(const Arch& arch, const std::string& header_dir,
Josh Gaobf8a2852016-05-27 11:59:09 -070081 const std::string& dependency_dir) {
Josh Gao3091f5a2016-11-16 17:01:57 -080082 std::vector<std::string> headers = collectHeaders(header_dir);
Josh Gaobf8a2852016-05-27 11:59:09 -070083 std::vector<std::string> dependencies = { header_dir };
84 if (!dependency_dir.empty()) {
Yi Kongff6c8de2017-04-20 09:08:11 -070085 auto collect_children = [&dependencies](const std::string& dir_path) {
Josh Gaobf8a2852016-05-27 11:59:09 -070086 DIR* dir = opendir(dir_path.c_str());
87 if (!dir) {
88 err(1, "failed to open dependency directory '%s'", dir_path.c_str());
89 }
90
91 struct dirent* dent;
92 while ((dent = readdir(dir))) {
93 if (dent->d_name[0] == '.') {
94 continue;
95 }
96
97 // TODO: Resolve symlinks.
98 std::string dependency = dir_path + "/" + dent->d_name;
99
100 struct stat st;
101 if (stat(dependency.c_str(), &st) != 0) {
102 err(1, "failed to stat dependency '%s'", dependency.c_str());
103 }
104
105 if (!S_ISDIR(st.st_mode)) {
106 errx(1, "'%s' is not a directory", dependency.c_str());
107 }
108
109 dependencies.push_back(dependency);
110 }
111
112 closedir(dir);
113 };
114
115 collect_children(dependency_dir + "/common");
Josh Gaobfb6bae2016-07-15 17:25:21 -0700116 collect_children(dependency_dir + "/" + to_string(arch));
Josh Gaobf8a2852016-05-27 11:59:09 -0700117 }
118
119 auto new_end = std::remove_if(headers.begin(), headers.end(), [&arch](llvm::StringRef header) {
120 for (const auto& it : header_blacklist) {
121 if (it.second.find(arch) == it.second.end()) {
122 continue;
123 }
124
125 if (header.endswith("/" + it.first)) {
126 return true;
127 }
128 }
129 return false;
130 });
131
132 headers.erase(new_end, headers.end());
133
134 CompilationRequirements result = { .headers = headers, .dependencies = dependencies };
135 return result;
136}
137
Josh Gaobfb6bae2016-07-15 17:25:21 -0700138static std::set<CompilationType> generateCompilationTypes(const std::set<Arch> selected_architectures,
139 const std::set<int>& selected_levels) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700140 std::set<CompilationType> result;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700141 for (const auto& arch : selected_architectures) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700142 int min_api = arch_min_api[arch];
143 for (int api_level : selected_levels) {
144 if (api_level < min_api) {
145 continue;
146 }
Josh Gaoa77b3a92016-08-15 16:39:27 -0700147
148 for (int file_offset_bits : { 32, 64 }) {
149 CompilationType type = {
150 .arch = arch, .api_level = api_level, .file_offset_bits = file_offset_bits
151 };
152 result.insert(type);
153 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700154 }
155 }
156 return result;
157}
158
Josh Gaobfb6bae2016-07-15 17:25:21 -0700159static std::unique_ptr<HeaderDatabase> compileHeaders(const std::set<CompilationType>& types,
160 const std::string& header_dir,
Josh Gao16016df2016-11-07 18:27:16 -0800161 const std::string& dependency_dir) {
162 if (types.empty()) {
163 errx(1, "compileHeaders received no CompilationTypes");
164 }
165
Josh Gao78b8a142016-11-09 01:00:41 -0800166 auto vfs = createCommonVFS(header_dir, dependency_dir, add_include);
167
Josh Gao16016df2016-11-07 18:27:16 -0800168 size_t thread_count = max_thread_count;
169 std::vector<std::thread> threads;
Josh Gaobf8a2852016-05-27 11:59:09 -0700170
171 std::map<CompilationType, HeaderDatabase> header_databases;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700172 std::unordered_map<Arch, CompilationRequirements> requirements;
Josh Gaobf8a2852016-05-27 11:59:09 -0700173
Josh Gao16016df2016-11-07 18:27:16 -0800174 auto result = std::make_unique<HeaderDatabase>();
Josh Gaobf8a2852016-05-27 11:59:09 -0700175 for (const auto& type : types) {
176 if (requirements.count(type.arch) == 0) {
177 requirements[type.arch] = collectRequirements(type.arch, header_dir, dependency_dir);
178 }
179 }
180
Josh Gao78b8a142016-11-09 01:00:41 -0800181 initializeTargetCC1FlagCache(vfs, types, requirements);
Josh Gaob5c49632016-11-08 22:21:31 -0800182
183 std::vector<std::pair<CompilationType, const std::string&>> jobs;
Josh Gao338cf122016-11-09 18:01:41 -0800184 std::atomic<size_t> job_index(0);
Josh Gao16016df2016-11-07 18:27:16 -0800185 for (CompilationType type : types) {
186 CompilationRequirements& req = requirements[type.arch];
187 for (const std::string& header : req.headers) {
Josh Gaob5c49632016-11-08 22:21:31 -0800188 jobs.emplace_back(type, header);
Josh Gaobf8a2852016-05-27 11:59:09 -0700189 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700190 }
191
Josh Gaob5c49632016-11-08 22:21:31 -0800192 thread_count = std::min(thread_count, jobs.size());
193
194 if (thread_count == 1) {
195 for (const auto& job : jobs) {
Josh Gao78b8a142016-11-09 01:00:41 -0800196 compileHeader(vfs, result.get(), job.first, job.second);
Josh Gaob5c49632016-11-08 22:21:31 -0800197 }
198 } else {
199 // Spawn threads.
200 for (size_t i = 0; i < thread_count; ++i) {
Yi Kongff6c8de2017-04-20 09:08:11 -0700201 threads.emplace_back([&jobs, &job_index, &result, vfs]() {
Josh Gao338cf122016-11-09 18:01:41 -0800202 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;
Josh Gao0a284f52016-12-15 13:56:00 -0800384 if (auto missing_it = missing_availability.find(symbol_name);
385 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 Gaoacc3d802016-11-09 18:22:44 -0800392 if (strict) {
Josh Gao0a284f52016-12-15 13:56:00 -0800393 if (auto extra_it = extra_availability.find(symbol_name);
394 extra_it != extra_availability.end()) {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700395 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) {
Josh Gao0a284f52016-12-15 13:56:00 -0800403 if (auto symbol_it = header_database->symbols.find(symbol_name);
404 symbol_it != header_database->symbols.end()) {
405 symbol_it->second.dump(cwd);
406 } else {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700407 errx(1, "failed to find symbol in header database");
408 }
Josh Gaod67dbf02016-06-02 15:21:14 -0700409 }
Josh Gaod67dbf02016-06-02 15:21:14 -0700410 }
411
Josh Gaobfb6bae2016-07-15 17:25:21 -0700412 // TODO: Verify that function/variable declarations are actually function/variable symbols.
Josh Gaobf8a2852016-05-27 11:59:09 -0700413 return !failed;
414}
415
Josh Gao62aaf8f2016-06-02 14:27:21 -0700416static void usage(bool help = false) {
417 fprintf(stderr, "Usage: versioner [OPTION]... [HEADER_PATH] [DEPS_PATH]\n");
418 if (!help) {
419 printf("Try 'versioner -h' for more information.\n");
420 exit(1);
421 } else {
422 fprintf(stderr, "Version headers at HEADER_PATH, with DEPS_PATH/ARCH/* on the include path\n");
Josh Gao9b5af7a2016-06-02 14:29:13 -0700423 fprintf(stderr, "Autodetects paths if HEADER_PATH and DEPS_PATH are not specified\n");
Josh Gao62aaf8f2016-06-02 14:27:21 -0700424 fprintf(stderr, "\n");
425 fprintf(stderr, "Target specification (defaults to all):\n");
426 fprintf(stderr, " -a API_LEVEL\tbuild with specified API level (can be repeated)\n");
427 fprintf(stderr, " \t\tvalid levels are %s\n", Join(supported_levels).c_str());
428 fprintf(stderr, " -r ARCH\tbuild with specified architecture (can be repeated)\n");
429 fprintf(stderr, " \t\tvalid architectures are %s\n", Join(supported_archs).c_str());
430 fprintf(stderr, "\n");
431 fprintf(stderr, "Validation:\n");
432 fprintf(stderr, " -p PATH\tcompare against NDK platform at PATH\n");
Josh Gaoacc3d802016-11-09 18:22:44 -0800433 fprintf(stderr, " -s\t\tenable strict warnings\n");
Josh Gao62aaf8f2016-06-02 14:27:21 -0700434 fprintf(stderr, "\n");
Josh Gaof8592a32016-07-26 18:58:27 -0700435 fprintf(stderr, "Preprocessing:\n");
436 fprintf(stderr, " -o PATH\tpreprocess header files and emit them at PATH\n");
Josh Gaod744a9b2017-04-03 11:24:48 -0700437 fprintf(stderr, " -f\t\tpreprocess header files even if validation fails\n");
Josh Gaof8592a32016-07-26 18:58:27 -0700438 fprintf(stderr, "\n");
Josh Gao62aaf8f2016-06-02 14:27:21 -0700439 fprintf(stderr, "Miscellaneous:\n");
Josh Gaobfb6bae2016-07-15 17:25:21 -0700440 fprintf(stderr, " -d\t\tdump function availability\n");
Josh Gao16016df2016-11-07 18:27:16 -0800441 fprintf(stderr, " -j THREADS\tmaximum number of threads to use\n");
Josh Gaoacc3d802016-11-09 18:22:44 -0800442 fprintf(stderr, " -v\t\tenable verbose logging\n");
Josh Gao62aaf8f2016-06-02 14:27:21 -0700443 fprintf(stderr, " -h\t\tdisplay this message\n");
444 exit(0);
445 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700446}
447
448int main(int argc, char** argv) {
449 std::string cwd = getWorkingDir() + "/";
450 bool default_args = true;
451 std::string platform_dir;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700452 std::set<Arch> selected_architectures;
Josh Gaobf8a2852016-05-27 11:59:09 -0700453 std::set<int> selected_levels;
Josh Gaof8592a32016-07-26 18:58:27 -0700454 std::string preprocessor_output_path;
455 bool force = false;
Josh Gao16016df2016-11-07 18:27:16 -0800456 bool dump = false;
Josh Gaobf8a2852016-05-27 11:59:09 -0700457
458 int c;
Josh Gaoacc3d802016-11-09 18:22:44 -0800459 while ((c = getopt(argc, argv, "a:r:p:so:fdj:vhi")) != -1) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700460 default_args = false;
461 switch (c) {
462 case 'a': {
463 char* end;
464 int api_level = strtol(optarg, &end, 10);
465 if (end == optarg || strlen(end) > 0) {
466 usage();
467 }
468
469 if (supported_levels.count(api_level) == 0) {
470 errx(1, "unsupported API level %d", api_level);
471 }
472
473 selected_levels.insert(api_level);
474 break;
475 }
476
477 case 'r': {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700478 Arch arch = arch_from_string(optarg);
479 selected_architectures.insert(arch);
Josh Gaobf8a2852016-05-27 11:59:09 -0700480 break;
481 }
482
483 case 'p': {
484 if (!platform_dir.empty()) {
485 usage();
486 }
487
488 platform_dir = optarg;
489
Josh Gaof8592a32016-07-26 18:58:27 -0700490 if (platform_dir.empty()) {
491 usage();
492 }
493
Josh Gaobf8a2852016-05-27 11:59:09 -0700494 struct stat st;
495 if (stat(platform_dir.c_str(), &st) != 0) {
496 err(1, "failed to stat platform directory '%s'", platform_dir.c_str());
497 }
498 if (!S_ISDIR(st.st_mode)) {
499 errx(1, "'%s' is not a directory", optarg);
500 }
501 break;
502 }
503
Josh Gaoacc3d802016-11-09 18:22:44 -0800504 case 's':
505 strict = true;
Josh Gaobf8a2852016-05-27 11:59:09 -0700506 break;
507
Josh Gaof8592a32016-07-26 18:58:27 -0700508 case 'o':
509 if (!preprocessor_output_path.empty()) {
510 usage();
511 }
512 preprocessor_output_path = optarg;
513 if (preprocessor_output_path.empty()) {
514 usage();
515 }
516 break;
517
518 case 'f':
519 force = true;
520 break;
521
Josh Gaobfb6bae2016-07-15 17:25:21 -0700522 case 'd':
523 dump = true;
524 break;
525
Josh Gao16016df2016-11-07 18:27:16 -0800526 case 'j':
527 if (!android::base::ParseInt<int>(optarg, &max_thread_count, 1)) {
528 usage();
529 }
530 break;
531
Josh Gaoacc3d802016-11-09 18:22:44 -0800532 case 'v':
533 verbose = true;
534 break;
535
Josh Gao62aaf8f2016-06-02 14:27:21 -0700536 case 'h':
537 usage(true);
538 break;
539
Josh Gaobfb6bae2016-07-15 17:25:21 -0700540 case 'i':
541 // Secret option for tests to -include <android/versioning.h>.
542 add_include = true;
543 break;
544
Josh Gaobf8a2852016-05-27 11:59:09 -0700545 default:
546 usage();
547 break;
548 }
549 }
550
Josh Gao9b5af7a2016-06-02 14:29:13 -0700551 if (argc - optind > 2 || optind > argc) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700552 usage();
553 }
554
Josh Gao9b5af7a2016-06-02 14:29:13 -0700555 std::string header_dir;
556 std::string dependency_dir;
557
Josh Gaobfb6bae2016-07-15 17:25:21 -0700558 const char* top = getenv("ANDROID_BUILD_TOP");
559 if (!top && (optind == argc || add_include)) {
560 fprintf(stderr, "versioner: failed to autodetect bionic paths. Is ANDROID_BUILD_TOP set?\n");
561 usage();
562 }
563
Josh Gao9b5af7a2016-06-02 14:29:13 -0700564 if (optind == argc) {
565 // Neither HEADER_PATH nor DEPS_PATH were specified, so try to figure them out.
Josh Gaobfb6bae2016-07-15 17:25:21 -0700566 std::string versioner_dir = to_string(top) + "/bionic/tools/versioner";
Josh Gao9b5af7a2016-06-02 14:29:13 -0700567 header_dir = versioner_dir + "/current";
568 dependency_dir = versioner_dir + "/dependencies";
569 if (platform_dir.empty()) {
570 platform_dir = versioner_dir + "/platforms";
571 }
572 } else {
Josh Gaof8592a32016-07-26 18:58:27 -0700573 // Intentional leak.
574 header_dir = realpath(argv[optind], nullptr);
Josh Gao9b5af7a2016-06-02 14:29:13 -0700575
576 if (argc - optind == 2) {
577 dependency_dir = argv[optind + 1];
578 }
579 }
580
Josh Gaobf8a2852016-05-27 11:59:09 -0700581 if (selected_levels.empty()) {
582 selected_levels = supported_levels;
583 }
584
585 if (selected_architectures.empty()) {
586 selected_architectures = supported_archs;
587 }
588
Josh Gaobf8a2852016-05-27 11:59:09 -0700589
590 struct stat st;
Josh Gao9b5af7a2016-06-02 14:29:13 -0700591 if (stat(header_dir.c_str(), &st) != 0) {
592 err(1, "failed to stat '%s'", header_dir.c_str());
Josh Gaobf8a2852016-05-27 11:59:09 -0700593 } else if (!S_ISDIR(st.st_mode)) {
Josh Gao9b5af7a2016-06-02 14:29:13 -0700594 errx(1, "'%s' is not a directory", header_dir.c_str());
Josh Gaobf8a2852016-05-27 11:59:09 -0700595 }
596
597 std::set<CompilationType> compilation_types;
Josh Gaobf8a2852016-05-27 11:59:09 -0700598 NdkSymbolDatabase symbol_database;
599
600 compilation_types = generateCompilationTypes(selected_architectures, selected_levels);
601
602 // Do this before compiling so that we can early exit if the platforms don't match what we
603 // expect.
604 if (!platform_dir.empty()) {
605 symbol_database = parsePlatforms(compilation_types, platform_dir);
606 }
607
Josh Gao338cf122016-11-09 18:01:41 -0800608 auto start = std::chrono::high_resolution_clock::now();
Josh Gaobfb6bae2016-07-15 17:25:21 -0700609 std::unique_ptr<HeaderDatabase> declaration_database =
Josh Gao16016df2016-11-07 18:27:16 -0800610 compileHeaders(compilation_types, header_dir, dependency_dir);
Josh Gao338cf122016-11-09 18:01:41 -0800611 auto end = std::chrono::high_resolution_clock::now();
612
613 if (verbose) {
614 auto diff = (end - start) / 1.0ms;
615 printf("Compiled headers for %zu targets in %0.2LFms\n", compilation_types.size(), diff);
616 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700617
Josh Gao16016df2016-11-07 18:27:16 -0800618 bool failed = false;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700619 if (dump) {
620 declaration_database->dump(header_dir + "/");
621 } else {
622 if (!sanityCheck(declaration_database.get())) {
623 printf("versioner: sanity check failed\n");
Josh Gaobf8a2852016-05-27 11:59:09 -0700624 failed = true;
625 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700626
Josh Gaobfb6bae2016-07-15 17:25:21 -0700627 if (!platform_dir.empty()) {
628 if (!checkVersions(compilation_types, declaration_database.get(), symbol_database)) {
629 printf("versioner: version check failed\n");
630 failed = true;
631 }
632 }
633 }
Josh Gaof8592a32016-07-26 18:58:27 -0700634
635 if (!preprocessor_output_path.empty() && (force || !failed)) {
636 failed = !preprocessHeaders(preprocessor_output_path, header_dir, declaration_database.get());
637 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700638 return failed;
639}