blob: b55170941f6761f299fd715f8fec2f84c799911e [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 Gaob50b8c82017-04-27 17:22:52 -070044#include <android-base/file.h>
Josh Gao338cf122016-11-09 18:01:41 -080045#include <android-base/macros.h>
Josh Gao16016df2016-11-07 18:27:16 -080046#include <android-base/parseint.h>
47
Josh Gaobfb6bae2016-07-15 17:25:21 -070048#include "Arch.h"
Josh Gaobf8a2852016-05-27 11:59:09 -070049#include "DeclarationDatabase.h"
Josh Gaob5c49632016-11-08 22:21:31 -080050#include "Driver.h"
Josh Gaof8592a32016-07-26 18:58:27 -070051#include "Preprocessor.h"
Josh Gaobf8a2852016-05-27 11:59:09 -070052#include "SymbolDatabase.h"
53#include "Utils.h"
Josh Gao78b8a142016-11-09 01:00:41 -080054#include "VFS.h"
55
Josh Gaobf8a2852016-05-27 11:59:09 -070056#include "versioner.h"
57
Josh Gao338cf122016-11-09 18:01:41 -080058using namespace std::chrono_literals;
Josh Gaobf8a2852016-05-27 11:59:09 -070059using namespace std::string_literals;
Josh Gaobf8a2852016-05-27 11:59:09 -070060
Josh Gaoacc3d802016-11-09 18:22:44 -080061bool strict;
Josh Gaobf8a2852016-05-27 11:59:09 -070062bool verbose;
Josh Gaoacc3d802016-11-09 18:22:44 -080063bool add_include;
Josh Gao338cf122016-11-09 18:01:41 -080064
65static int getCpuCount();
66static int max_thread_count = getCpuCount();
67
68static int getCpuCount() {
69#if defined(__linux__)
70 cpu_set_t cpu_set;
71 int rc = sched_getaffinity(getpid(), sizeof(cpu_set), &cpu_set);
72 if (rc != 0) {
73 err(1, "sched_getaffinity failed");
74 }
75 return CPU_COUNT(&cpu_set);
76#else
77 return 1;
78#endif
79}
Josh Gaobf8a2852016-05-27 11:59:09 -070080
Josh Gaobfb6bae2016-07-15 17:25:21 -070081static CompilationRequirements collectRequirements(const Arch& arch, const std::string& header_dir,
Josh Gaobf8a2852016-05-27 11:59:09 -070082 const std::string& dependency_dir) {
Josh Gao3091f5a2016-11-16 17:01:57 -080083 std::vector<std::string> headers = collectHeaders(header_dir);
Josh Gaobf8a2852016-05-27 11:59:09 -070084 std::vector<std::string> dependencies = { header_dir };
85 if (!dependency_dir.empty()) {
Yi Kongff6c8de2017-04-20 09:08:11 -070086 auto collect_children = [&dependencies](const std::string& dir_path) {
Josh Gaobf8a2852016-05-27 11:59:09 -070087 DIR* dir = opendir(dir_path.c_str());
88 if (!dir) {
89 err(1, "failed to open dependency directory '%s'", dir_path.c_str());
90 }
91
92 struct dirent* dent;
93 while ((dent = readdir(dir))) {
94 if (dent->d_name[0] == '.') {
95 continue;
96 }
97
98 // TODO: Resolve symlinks.
99 std::string dependency = dir_path + "/" + dent->d_name;
100
101 struct stat st;
102 if (stat(dependency.c_str(), &st) != 0) {
103 err(1, "failed to stat dependency '%s'", dependency.c_str());
104 }
105
106 if (!S_ISDIR(st.st_mode)) {
107 errx(1, "'%s' is not a directory", dependency.c_str());
108 }
109
110 dependencies.push_back(dependency);
111 }
112
113 closedir(dir);
114 };
115
116 collect_children(dependency_dir + "/common");
Josh Gaobfb6bae2016-07-15 17:25:21 -0700117 collect_children(dependency_dir + "/" + to_string(arch));
Josh Gaobf8a2852016-05-27 11:59:09 -0700118 }
119
120 auto new_end = std::remove_if(headers.begin(), headers.end(), [&arch](llvm::StringRef header) {
121 for (const auto& it : header_blacklist) {
122 if (it.second.find(arch) == it.second.end()) {
123 continue;
124 }
125
126 if (header.endswith("/" + it.first)) {
127 return true;
128 }
129 }
130 return false;
131 });
132
133 headers.erase(new_end, headers.end());
134
135 CompilationRequirements result = { .headers = headers, .dependencies = dependencies };
136 return result;
137}
138
Josh Gaobfb6bae2016-07-15 17:25:21 -0700139static std::set<CompilationType> generateCompilationTypes(const std::set<Arch> selected_architectures,
140 const std::set<int>& selected_levels) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700141 std::set<CompilationType> result;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700142 for (const auto& arch : selected_architectures) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700143 int min_api = arch_min_api[arch];
144 for (int api_level : selected_levels) {
145 if (api_level < min_api) {
146 continue;
147 }
Josh Gaoa77b3a92016-08-15 16:39:27 -0700148
149 for (int file_offset_bits : { 32, 64 }) {
150 CompilationType type = {
151 .arch = arch, .api_level = api_level, .file_offset_bits = file_offset_bits
152 };
153 result.insert(type);
154 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700155 }
156 }
157 return result;
158}
159
Josh Gaobfb6bae2016-07-15 17:25:21 -0700160static std::unique_ptr<HeaderDatabase> compileHeaders(const std::set<CompilationType>& types,
161 const std::string& header_dir,
Josh Gao16016df2016-11-07 18:27:16 -0800162 const std::string& dependency_dir) {
163 if (types.empty()) {
164 errx(1, "compileHeaders received no CompilationTypes");
165 }
166
Josh Gao78b8a142016-11-09 01:00:41 -0800167 auto vfs = createCommonVFS(header_dir, dependency_dir, add_include);
168
Josh Gao16016df2016-11-07 18:27:16 -0800169 size_t thread_count = max_thread_count;
170 std::vector<std::thread> threads;
Josh Gaobf8a2852016-05-27 11:59:09 -0700171
172 std::map<CompilationType, HeaderDatabase> header_databases;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700173 std::unordered_map<Arch, CompilationRequirements> requirements;
Josh Gaobf8a2852016-05-27 11:59:09 -0700174
Josh Gao16016df2016-11-07 18:27:16 -0800175 auto result = std::make_unique<HeaderDatabase>();
Josh Gaobf8a2852016-05-27 11:59:09 -0700176 for (const auto& type : types) {
177 if (requirements.count(type.arch) == 0) {
178 requirements[type.arch] = collectRequirements(type.arch, header_dir, dependency_dir);
179 }
180 }
181
Josh Gao78b8a142016-11-09 01:00:41 -0800182 initializeTargetCC1FlagCache(vfs, types, requirements);
Josh Gaob5c49632016-11-08 22:21:31 -0800183
184 std::vector<std::pair<CompilationType, const std::string&>> jobs;
Josh Gao338cf122016-11-09 18:01:41 -0800185 std::atomic<size_t> job_index(0);
Josh Gao16016df2016-11-07 18:27:16 -0800186 for (CompilationType type : types) {
187 CompilationRequirements& req = requirements[type.arch];
188 for (const std::string& header : req.headers) {
Josh Gaob5c49632016-11-08 22:21:31 -0800189 jobs.emplace_back(type, header);
Josh Gaobf8a2852016-05-27 11:59:09 -0700190 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700191 }
192
Josh Gaob5c49632016-11-08 22:21:31 -0800193 thread_count = std::min(thread_count, jobs.size());
194
195 if (thread_count == 1) {
196 for (const auto& job : jobs) {
Josh Gao78b8a142016-11-09 01:00:41 -0800197 compileHeader(vfs, result.get(), job.first, job.second);
Josh Gaob5c49632016-11-08 22:21:31 -0800198 }
199 } else {
200 // Spawn threads.
201 for (size_t i = 0; i < thread_count; ++i) {
Yi Kongff6c8de2017-04-20 09:08:11 -0700202 threads.emplace_back([&jobs, &job_index, &result, vfs]() {
Josh Gao338cf122016-11-09 18:01:41 -0800203 while (true) {
204 size_t idx = job_index++;
205 if (idx >= jobs.size()) {
206 return;
207 }
208
209 const auto& job = jobs[idx];
Josh Gao78b8a142016-11-09 01:00:41 -0800210 compileHeader(vfs, result.get(), job.first, job.second);
Josh Gaob5c49632016-11-08 22:21:31 -0800211 }
212 });
Josh Gaobf8a2852016-05-27 11:59:09 -0700213 }
214
Josh Gaob5c49632016-11-08 22:21:31 -0800215 // Reap them.
216 for (auto& thread : threads) {
217 thread.join();
218 }
219 threads.clear();
Josh Gaobf8a2852016-05-27 11:59:09 -0700220 }
221
Josh Gaobfb6bae2016-07-15 17:25:21 -0700222 return result;
Josh Gaobf8a2852016-05-27 11:59:09 -0700223}
224
Josh Gao1a176de2016-11-04 13:15:11 -0700225static std::set<CompilationType> getCompilationTypes(const Declaration* decl) {
226 std::set<CompilationType> result;
227 for (const auto& it : decl->availability) {
228 result.insert(it.first);
229 }
230 return result;
231}
232
233template<typename T>
234static std::vector<T> Intersection(const std::set<T>& a, const std::set<T>& b) {
235 std::vector<T> intersection;
236 std::set_intersection(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(intersection));
237 return intersection;
238}
239
Josh Gaobfb6bae2016-07-15 17:25:21 -0700240// Perform a sanity check on a symbol's declarations, enforcing the following invariants:
241// 1. At most one inline definition of the function exists.
242// 2. All of the availability declarations for a symbol are compatible.
243// If a function is declared as an inline before a certain version, the inline definition
244// should have no version tag.
245// 3. Each availability type must only be present globally or on a per-arch basis.
246// (e.g. __INTRODUCED_IN_ARM(9) __INTRODUCED_IN_X86(10) __DEPRECATED_IN(11) is fine,
247// but not __INTRODUCED_IN(9) __INTRODUCED_IN_X86(10))
248static bool checkSymbol(const Symbol& symbol) {
249 std::string cwd = getWorkingDir() + "/";
250
Josh Gao1a176de2016-11-04 13:15:11 -0700251 std::unordered_map<const Declaration*, std::set<CompilationType>> inline_definitions;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700252 for (const auto& decl_it : symbol.declarations) {
253 const Declaration* decl = &decl_it.second;
254 if (decl->is_definition) {
Josh Gao1a176de2016-11-04 13:15:11 -0700255 std::set<CompilationType> compilation_types = getCompilationTypes(decl);
256 for (const auto& inline_def_it : inline_definitions) {
257 auto intersection = Intersection(compilation_types, inline_def_it.second);
258 if (!intersection.empty()) {
259 fprintf(stderr, "versioner: conflicting inline definitions:\n");
260 fprintf(stderr, " declarations visible in: %s\n", Join(intersection, ", ").c_str());
261 decl->dump(cwd, stderr, 4);
262 inline_def_it.first->dump(cwd, stderr, 4);
263 return false;
264 }
Josh Gaobfb6bae2016-07-15 17:25:21 -0700265 }
266
Josh Gao1a176de2016-11-04 13:15:11 -0700267 inline_definitions[decl] = std::move(compilation_types);
Josh Gaobfb6bae2016-07-15 17:25:21 -0700268 }
269
270 DeclarationAvailability availability;
271 if (!decl->calculateAvailability(&availability)) {
272 fprintf(stderr, "versioner: failed to calculate availability for declaration:\n");
Josh Gao566735d2016-08-02 15:07:32 -0700273 decl->dump(cwd, stderr, 2);
Josh Gaobfb6bae2016-07-15 17:25:21 -0700274 return false;
275 }
276
277 if (decl->is_definition && !availability.empty()) {
278 fprintf(stderr, "versioner: inline definition has non-empty versioning information:\n");
Josh Gao566735d2016-08-02 15:07:32 -0700279 decl->dump(cwd, stderr, 2);
Josh Gaobfb6bae2016-07-15 17:25:21 -0700280 return false;
281 }
282 }
283
284 DeclarationAvailability availability;
285 if (!symbol.calculateAvailability(&availability)) {
286 fprintf(stderr, "versioner: inconsistent availability for symbol '%s'\n", symbol.name.c_str());
287 symbol.dump(cwd);
288 return false;
289 }
290
291 // TODO: Check invariant #3.
292 return true;
293}
294
295static bool sanityCheck(const HeaderDatabase* database) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700296 bool error = false;
Josh Gao958f3b32016-06-03 13:44:00 -0700297 std::string cwd = getWorkingDir() + "/";
298
Josh Gaobfb6bae2016-07-15 17:25:21 -0700299 for (const auto& symbol_it : database->symbols) {
300 if (!checkSymbol(symbol_it.second)) {
301 error = true;
Josh Gaobf8a2852016-05-27 11:59:09 -0700302 }
303 }
304 return !error;
305}
306
307// Check that our symbol availability declarations match the actual NDK
308// platform symbol availability.
309static bool checkVersions(const std::set<CompilationType>& types,
Josh Gaobfb6bae2016-07-15 17:25:21 -0700310 const HeaderDatabase* header_database,
Josh Gaobf8a2852016-05-27 11:59:09 -0700311 const NdkSymbolDatabase& symbol_database) {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700312 std::string cwd = getWorkingDir() + "/";
Josh Gaobf8a2852016-05-27 11:59:09 -0700313 bool failed = false;
314
Josh Gaobfb6bae2016-07-15 17:25:21 -0700315 std::map<Arch, std::set<CompilationType>> arch_types;
Josh Gaobf8a2852016-05-27 11:59:09 -0700316 for (const CompilationType& type : types) {
317 arch_types[type.arch].insert(type);
318 }
319
Josh Gaod67dbf02016-06-02 15:21:14 -0700320 std::set<std::string> completely_unavailable;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700321 std::map<std::string, std::set<CompilationType>> missing_availability;
322 std::map<std::string, std::set<CompilationType>> extra_availability;
Josh Gaod67dbf02016-06-02 15:21:14 -0700323
Josh Gaobfb6bae2016-07-15 17:25:21 -0700324 for (const auto& symbol_it : header_database->symbols) {
325 const auto& symbol_name = symbol_it.first;
326 DeclarationAvailability symbol_availability;
Josh Gaobf8a2852016-05-27 11:59:09 -0700327
Josh Gaobfb6bae2016-07-15 17:25:21 -0700328 if (!symbol_it.second.calculateAvailability(&symbol_availability)) {
329 errx(1, "failed to calculate symbol availability");
330 }
331
332 const auto platform_availability_it = symbol_database.find(symbol_name);
Josh Gaobf8a2852016-05-27 11:59:09 -0700333 if (platform_availability_it == symbol_database.end()) {
Josh Gaod67dbf02016-06-02 15:21:14 -0700334 completely_unavailable.insert(symbol_name);
Josh Gaobf8a2852016-05-27 11:59:09 -0700335 continue;
336 }
337
338 const auto& platform_availability = platform_availability_it->second;
Josh Gaobf8a2852016-05-27 11:59:09 -0700339
340 for (const CompilationType& type : types) {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700341 bool should_be_available = true;
342 const auto& global_availability = symbol_availability.global_availability;
343 const auto& arch_availability = symbol_availability.arch_availability[type.arch];
344 if (global_availability.introduced != 0 && global_availability.introduced > type.api_level) {
345 should_be_available = false;
346 }
347
348 if (arch_availability.introduced != 0 && arch_availability.introduced > type.api_level) {
349 should_be_available = false;
350 }
351
352 if (global_availability.obsoleted != 0 && global_availability.obsoleted <= type.api_level) {
353 should_be_available = false;
354 }
355
356 if (arch_availability.obsoleted != 0 && arch_availability.obsoleted <= type.api_level) {
357 should_be_available = false;
358 }
359
360 if (arch_availability.future) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700361 continue;
362 }
363
Josh Gaobfb6bae2016-07-15 17:25:21 -0700364 // The function declaration might be (validly) missing for the given CompilationType.
365 if (!symbol_it.second.hasDeclaration(type)) {
366 should_be_available = false;
367 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700368
Josh Gaobfb6bae2016-07-15 17:25:21 -0700369 bool is_available = platform_availability.count(type);
Josh Gaobf8a2852016-05-27 11:59:09 -0700370
Josh Gaobfb6bae2016-07-15 17:25:21 -0700371 if (should_be_available != is_available) {
372 if (is_available) {
373 extra_availability[symbol_name].insert(type);
Josh Gaobf8a2852016-05-27 11:59:09 -0700374 } else {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700375 missing_availability[symbol_name].insert(type);
Josh Gaobf8a2852016-05-27 11:59:09 -0700376 }
377 }
378 }
Josh Gaobfb6bae2016-07-15 17:25:21 -0700379 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700380
Josh Gaobfb6bae2016-07-15 17:25:21 -0700381 for (const auto& it : symbol_database) {
382 const std::string& symbol_name = it.first;
Josh Gaobf8a2852016-05-27 11:59:09 -0700383
Josh Gaobfb6bae2016-07-15 17:25:21 -0700384 bool symbol_error = false;
Josh Gao0a284f52016-12-15 13:56:00 -0800385 if (auto missing_it = missing_availability.find(symbol_name);
386 missing_it != missing_availability.end()) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700387 printf("%s: declaration marked available but symbol missing in [%s]\n", symbol_name.c_str(),
Josh Gaobfb6bae2016-07-15 17:25:21 -0700388 Join(missing_it->second, ", ").c_str());
389 symbol_error = true;
Josh Gaobf8a2852016-05-27 11:59:09 -0700390 failed = true;
391 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700392
Josh Gaoacc3d802016-11-09 18:22:44 -0800393 if (strict) {
Josh Gao0a284f52016-12-15 13:56:00 -0800394 if (auto extra_it = extra_availability.find(symbol_name);
395 extra_it != extra_availability.end()) {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700396 printf("%s: declaration marked unavailable but symbol available in [%s]\n",
397 symbol_name.c_str(), Join(extra_it->second, ", ").c_str());
398 symbol_error = true;
399 failed = true;
Josh Gao958f3b32016-06-03 13:44:00 -0700400 }
401 }
402
Josh Gaobfb6bae2016-07-15 17:25:21 -0700403 if (symbol_error) {
Josh Gao0a284f52016-12-15 13:56:00 -0800404 if (auto symbol_it = header_database->symbols.find(symbol_name);
405 symbol_it != header_database->symbols.end()) {
406 symbol_it->second.dump(cwd);
407 } else {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700408 errx(1, "failed to find symbol in header database");
409 }
Josh Gaod67dbf02016-06-02 15:21:14 -0700410 }
Josh Gaod67dbf02016-06-02 15:21:14 -0700411 }
412
Josh Gaobfb6bae2016-07-15 17:25:21 -0700413 // TODO: Verify that function/variable declarations are actually function/variable symbols.
Josh Gaobf8a2852016-05-27 11:59:09 -0700414 return !failed;
415}
416
Josh Gao62aaf8f2016-06-02 14:27:21 -0700417static void usage(bool help = false) {
418 fprintf(stderr, "Usage: versioner [OPTION]... [HEADER_PATH] [DEPS_PATH]\n");
419 if (!help) {
420 printf("Try 'versioner -h' for more information.\n");
421 exit(1);
422 } else {
423 fprintf(stderr, "Version headers at HEADER_PATH, with DEPS_PATH/ARCH/* on the include path\n");
Josh Gao9b5af7a2016-06-02 14:29:13 -0700424 fprintf(stderr, "Autodetects paths if HEADER_PATH and DEPS_PATH are not specified\n");
Josh Gao62aaf8f2016-06-02 14:27:21 -0700425 fprintf(stderr, "\n");
426 fprintf(stderr, "Target specification (defaults to all):\n");
427 fprintf(stderr, " -a API_LEVEL\tbuild with specified API level (can be repeated)\n");
428 fprintf(stderr, " \t\tvalid levels are %s\n", Join(supported_levels).c_str());
429 fprintf(stderr, " -r ARCH\tbuild with specified architecture (can be repeated)\n");
430 fprintf(stderr, " \t\tvalid architectures are %s\n", Join(supported_archs).c_str());
431 fprintf(stderr, "\n");
432 fprintf(stderr, "Validation:\n");
433 fprintf(stderr, " -p PATH\tcompare against NDK platform at PATH\n");
Josh Gaoacc3d802016-11-09 18:22:44 -0800434 fprintf(stderr, " -s\t\tenable strict warnings\n");
Josh Gao62aaf8f2016-06-02 14:27:21 -0700435 fprintf(stderr, "\n");
Josh Gaof8592a32016-07-26 18:58:27 -0700436 fprintf(stderr, "Preprocessing:\n");
437 fprintf(stderr, " -o PATH\tpreprocess header files and emit them at PATH\n");
Josh Gaod744a9b2017-04-03 11:24:48 -0700438 fprintf(stderr, " -f\t\tpreprocess header files even if validation fails\n");
Josh Gaof8592a32016-07-26 18:58:27 -0700439 fprintf(stderr, "\n");
Josh Gao62aaf8f2016-06-02 14:27:21 -0700440 fprintf(stderr, "Miscellaneous:\n");
Josh Gaobfb6bae2016-07-15 17:25:21 -0700441 fprintf(stderr, " -d\t\tdump function availability\n");
Josh Gao16016df2016-11-07 18:27:16 -0800442 fprintf(stderr, " -j THREADS\tmaximum number of threads to use\n");
Josh Gaoacc3d802016-11-09 18:22:44 -0800443 fprintf(stderr, " -v\t\tenable verbose logging\n");
Josh Gao62aaf8f2016-06-02 14:27:21 -0700444 fprintf(stderr, " -h\t\tdisplay this message\n");
445 exit(0);
446 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700447}
448
449int main(int argc, char** argv) {
450 std::string cwd = getWorkingDir() + "/";
451 bool default_args = true;
452 std::string platform_dir;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700453 std::set<Arch> selected_architectures;
Josh Gaobf8a2852016-05-27 11:59:09 -0700454 std::set<int> selected_levels;
Josh Gaof8592a32016-07-26 18:58:27 -0700455 std::string preprocessor_output_path;
456 bool force = false;
Josh Gao16016df2016-11-07 18:27:16 -0800457 bool dump = false;
Josh Gaobf8a2852016-05-27 11:59:09 -0700458
459 int c;
Josh Gaoacc3d802016-11-09 18:22:44 -0800460 while ((c = getopt(argc, argv, "a:r:p:so:fdj:vhi")) != -1) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700461 default_args = false;
462 switch (c) {
463 case 'a': {
464 char* end;
465 int api_level = strtol(optarg, &end, 10);
466 if (end == optarg || strlen(end) > 0) {
467 usage();
468 }
469
470 if (supported_levels.count(api_level) == 0) {
471 errx(1, "unsupported API level %d", api_level);
472 }
473
474 selected_levels.insert(api_level);
475 break;
476 }
477
478 case 'r': {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700479 Arch arch = arch_from_string(optarg);
480 selected_architectures.insert(arch);
Josh Gaobf8a2852016-05-27 11:59:09 -0700481 break;
482 }
483
484 case 'p': {
485 if (!platform_dir.empty()) {
486 usage();
487 }
488
489 platform_dir = optarg;
490
Josh Gaof8592a32016-07-26 18:58:27 -0700491 if (platform_dir.empty()) {
492 usage();
493 }
494
Josh Gaobf8a2852016-05-27 11:59:09 -0700495 struct stat st;
496 if (stat(platform_dir.c_str(), &st) != 0) {
497 err(1, "failed to stat platform directory '%s'", platform_dir.c_str());
498 }
499 if (!S_ISDIR(st.st_mode)) {
500 errx(1, "'%s' is not a directory", optarg);
501 }
502 break;
503 }
504
Josh Gaoacc3d802016-11-09 18:22:44 -0800505 case 's':
506 strict = true;
Josh Gaobf8a2852016-05-27 11:59:09 -0700507 break;
508
Josh Gaof8592a32016-07-26 18:58:27 -0700509 case 'o':
510 if (!preprocessor_output_path.empty()) {
511 usage();
512 }
513 preprocessor_output_path = optarg;
514 if (preprocessor_output_path.empty()) {
515 usage();
516 }
517 break;
518
519 case 'f':
520 force = true;
521 break;
522
Josh Gaobfb6bae2016-07-15 17:25:21 -0700523 case 'd':
524 dump = true;
525 break;
526
Josh Gao16016df2016-11-07 18:27:16 -0800527 case 'j':
528 if (!android::base::ParseInt<int>(optarg, &max_thread_count, 1)) {
529 usage();
530 }
531 break;
532
Josh Gaoacc3d802016-11-09 18:22:44 -0800533 case 'v':
534 verbose = true;
535 break;
536
Josh Gao62aaf8f2016-06-02 14:27:21 -0700537 case 'h':
538 usage(true);
539 break;
540
Josh Gaobfb6bae2016-07-15 17:25:21 -0700541 case 'i':
542 // Secret option for tests to -include <android/versioning.h>.
543 add_include = true;
544 break;
545
Josh Gaobf8a2852016-05-27 11:59:09 -0700546 default:
547 usage();
548 break;
549 }
550 }
551
Josh Gao9b5af7a2016-06-02 14:29:13 -0700552 if (argc - optind > 2 || optind > argc) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700553 usage();
554 }
555
Josh Gao9b5af7a2016-06-02 14:29:13 -0700556 std::string header_dir;
557 std::string dependency_dir;
558
Josh Gaobfb6bae2016-07-15 17:25:21 -0700559 const char* top = getenv("ANDROID_BUILD_TOP");
560 if (!top && (optind == argc || add_include)) {
561 fprintf(stderr, "versioner: failed to autodetect bionic paths. Is ANDROID_BUILD_TOP set?\n");
562 usage();
563 }
564
Josh Gao9b5af7a2016-06-02 14:29:13 -0700565 if (optind == argc) {
566 // Neither HEADER_PATH nor DEPS_PATH were specified, so try to figure them out.
Josh Gaobfb6bae2016-07-15 17:25:21 -0700567 std::string versioner_dir = to_string(top) + "/bionic/tools/versioner";
Josh Gao9b5af7a2016-06-02 14:29:13 -0700568 header_dir = versioner_dir + "/current";
569 dependency_dir = versioner_dir + "/dependencies";
570 if (platform_dir.empty()) {
571 platform_dir = versioner_dir + "/platforms";
572 }
573 } else {
Josh Gaob50b8c82017-04-27 17:22:52 -0700574 if (!android::base::Realpath(argv[optind], &header_dir)) {
575 err(1, "failed to get realpath for path '%s'", argv[optind]);
576 }
Josh Gao9b5af7a2016-06-02 14:29:13 -0700577
578 if (argc - optind == 2) {
579 dependency_dir = argv[optind + 1];
580 }
581 }
582
Josh Gaobf8a2852016-05-27 11:59:09 -0700583 if (selected_levels.empty()) {
584 selected_levels = supported_levels;
585 }
586
587 if (selected_architectures.empty()) {
588 selected_architectures = supported_archs;
589 }
590
Josh Gaobf8a2852016-05-27 11:59:09 -0700591
592 struct stat st;
Josh Gao9b5af7a2016-06-02 14:29:13 -0700593 if (stat(header_dir.c_str(), &st) != 0) {
594 err(1, "failed to stat '%s'", header_dir.c_str());
Josh Gaobf8a2852016-05-27 11:59:09 -0700595 } else if (!S_ISDIR(st.st_mode)) {
Josh Gao9b5af7a2016-06-02 14:29:13 -0700596 errx(1, "'%s' is not a directory", header_dir.c_str());
Josh Gaobf8a2852016-05-27 11:59:09 -0700597 }
598
599 std::set<CompilationType> compilation_types;
Josh Gaobf8a2852016-05-27 11:59:09 -0700600 NdkSymbolDatabase symbol_database;
601
602 compilation_types = generateCompilationTypes(selected_architectures, selected_levels);
603
604 // Do this before compiling so that we can early exit if the platforms don't match what we
605 // expect.
606 if (!platform_dir.empty()) {
607 symbol_database = parsePlatforms(compilation_types, platform_dir);
608 }
609
Josh Gao338cf122016-11-09 18:01:41 -0800610 auto start = std::chrono::high_resolution_clock::now();
Josh Gaobfb6bae2016-07-15 17:25:21 -0700611 std::unique_ptr<HeaderDatabase> declaration_database =
Josh Gao16016df2016-11-07 18:27:16 -0800612 compileHeaders(compilation_types, header_dir, dependency_dir);
Josh Gao338cf122016-11-09 18:01:41 -0800613 auto end = std::chrono::high_resolution_clock::now();
614
615 if (verbose) {
616 auto diff = (end - start) / 1.0ms;
617 printf("Compiled headers for %zu targets in %0.2LFms\n", compilation_types.size(), diff);
618 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700619
Josh Gao16016df2016-11-07 18:27:16 -0800620 bool failed = false;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700621 if (dump) {
622 declaration_database->dump(header_dir + "/");
623 } else {
624 if (!sanityCheck(declaration_database.get())) {
625 printf("versioner: sanity check failed\n");
Josh Gaobf8a2852016-05-27 11:59:09 -0700626 failed = true;
627 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700628
Josh Gaobfb6bae2016-07-15 17:25:21 -0700629 if (!platform_dir.empty()) {
630 if (!checkVersions(compilation_types, declaration_database.get(), symbol_database)) {
631 printf("versioner: version check failed\n");
632 failed = true;
633 }
634 }
635 }
Josh Gaof8592a32016-07-26 18:58:27 -0700636
637 if (!preprocessor_output_path.empty() && (force || !failed)) {
638 failed = !preprocessHeaders(preprocessor_output_path, header_dir, declaration_database.get());
639 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700640 return failed;
641}