blob: bb09bea9e44ed10847472c28132dd197547f4e4a [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>
George Burgess IVb97049c2017-07-24 15:05:05 -070047#include <android-base/strings.h>
Josh Gao16016df2016-11-07 18:27:16 -080048
Josh Gaobfb6bae2016-07-15 17:25:21 -070049#include "Arch.h"
Josh Gaobf8a2852016-05-27 11:59:09 -070050#include "DeclarationDatabase.h"
Josh Gaob5c49632016-11-08 22:21:31 -080051#include "Driver.h"
Josh Gaof8592a32016-07-26 18:58:27 -070052#include "Preprocessor.h"
Josh Gaobf8a2852016-05-27 11:59:09 -070053#include "SymbolDatabase.h"
54#include "Utils.h"
Josh Gao78b8a142016-11-09 01:00:41 -080055#include "VFS.h"
56
Josh Gaobf8a2852016-05-27 11:59:09 -070057#include "versioner.h"
58
Josh Gao338cf122016-11-09 18:01:41 -080059using namespace std::chrono_literals;
Josh Gaobf8a2852016-05-27 11:59:09 -070060using namespace std::string_literals;
Josh Gaobf8a2852016-05-27 11:59:09 -070061
Josh Gaoacc3d802016-11-09 18:22:44 -080062bool strict;
Josh Gaobf8a2852016-05-27 11:59:09 -070063bool verbose;
Josh Gaoacc3d802016-11-09 18:22:44 -080064bool add_include;
Josh Gao338cf122016-11-09 18:01:41 -080065
66static int getCpuCount();
67static int max_thread_count = getCpuCount();
68
69static int getCpuCount() {
70#if defined(__linux__)
71 cpu_set_t cpu_set;
72 int rc = sched_getaffinity(getpid(), sizeof(cpu_set), &cpu_set);
73 if (rc != 0) {
74 err(1, "sched_getaffinity failed");
75 }
76 return CPU_COUNT(&cpu_set);
77#else
78 return 1;
79#endif
80}
Josh Gaobf8a2852016-05-27 11:59:09 -070081
George Burgess IVb97049c2017-07-24 15:05:05 -070082namespace {
83struct HeaderLocationInformation {
84 std::string header_dir;
85 std::string dependency_dir;
86 // Absolute paths to ignore all children -- including subdirectories -- of.
87 std::unordered_set<std::string> ignored_directories;
88};
89}
90
91static CompilationRequirements collectRequirements(const Arch& arch,
92 const HeaderLocationInformation& location) {
93 std::vector<std::string> headers =
94 collectHeaders(location.header_dir, location.ignored_directories);
95 std::vector<std::string> dependencies = { location.header_dir };
96 if (!location.dependency_dir.empty()) {
Yi Kongff6c8de2017-04-20 09:08:11 -070097 auto collect_children = [&dependencies](const std::string& dir_path) {
Josh Gaobf8a2852016-05-27 11:59:09 -070098 DIR* dir = opendir(dir_path.c_str());
99 if (!dir) {
100 err(1, "failed to open dependency directory '%s'", dir_path.c_str());
101 }
102
103 struct dirent* dent;
104 while ((dent = readdir(dir))) {
105 if (dent->d_name[0] == '.') {
106 continue;
107 }
108
109 // TODO: Resolve symlinks.
110 std::string dependency = dir_path + "/" + dent->d_name;
111
112 struct stat st;
113 if (stat(dependency.c_str(), &st) != 0) {
114 err(1, "failed to stat dependency '%s'", dependency.c_str());
115 }
116
117 if (!S_ISDIR(st.st_mode)) {
118 errx(1, "'%s' is not a directory", dependency.c_str());
119 }
120
121 dependencies.push_back(dependency);
122 }
123
124 closedir(dir);
125 };
126
George Burgess IVb97049c2017-07-24 15:05:05 -0700127 collect_children(location.dependency_dir + "/common");
128 collect_children(location.dependency_dir + "/" + to_string(arch));
Josh Gaobf8a2852016-05-27 11:59:09 -0700129 }
130
131 auto new_end = std::remove_if(headers.begin(), headers.end(), [&arch](llvm::StringRef header) {
132 for (const auto& it : header_blacklist) {
133 if (it.second.find(arch) == it.second.end()) {
134 continue;
135 }
136
137 if (header.endswith("/" + it.first)) {
138 return true;
139 }
140 }
141 return false;
142 });
143
144 headers.erase(new_end, headers.end());
145
146 CompilationRequirements result = { .headers = headers, .dependencies = dependencies };
147 return result;
148}
149
Josh Gaobfb6bae2016-07-15 17:25:21 -0700150static std::set<CompilationType> generateCompilationTypes(const std::set<Arch> selected_architectures,
151 const std::set<int>& selected_levels) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700152 std::set<CompilationType> result;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700153 for (const auto& arch : selected_architectures) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700154 int min_api = arch_min_api[arch];
155 for (int api_level : selected_levels) {
156 if (api_level < min_api) {
157 continue;
158 }
Josh Gaoa77b3a92016-08-15 16:39:27 -0700159
160 for (int file_offset_bits : { 32, 64 }) {
161 CompilationType type = {
162 .arch = arch, .api_level = api_level, .file_offset_bits = file_offset_bits
163 };
164 result.insert(type);
165 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700166 }
167 }
168 return result;
169}
170
Josh Gaobfb6bae2016-07-15 17:25:21 -0700171static std::unique_ptr<HeaderDatabase> compileHeaders(const std::set<CompilationType>& types,
George Burgess IVb97049c2017-07-24 15:05:05 -0700172 const HeaderLocationInformation& location) {
Josh Gao16016df2016-11-07 18:27:16 -0800173 if (types.empty()) {
174 errx(1, "compileHeaders received no CompilationTypes");
175 }
176
George Burgess IVb97049c2017-07-24 15:05:05 -0700177 auto vfs = createCommonVFS(location.header_dir, location.dependency_dir, add_include);
Josh Gao78b8a142016-11-09 01:00:41 -0800178
Josh Gao16016df2016-11-07 18:27:16 -0800179 size_t thread_count = max_thread_count;
180 std::vector<std::thread> threads;
Josh Gaobf8a2852016-05-27 11:59:09 -0700181
182 std::map<CompilationType, HeaderDatabase> header_databases;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700183 std::unordered_map<Arch, CompilationRequirements> requirements;
Josh Gaobf8a2852016-05-27 11:59:09 -0700184
Josh Gao16016df2016-11-07 18:27:16 -0800185 auto result = std::make_unique<HeaderDatabase>();
Josh Gaobf8a2852016-05-27 11:59:09 -0700186 for (const auto& type : types) {
187 if (requirements.count(type.arch) == 0) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700188 requirements[type.arch] = collectRequirements(type.arch, location);
Josh Gaobf8a2852016-05-27 11:59:09 -0700189 }
190 }
191
Josh Gao78b8a142016-11-09 01:00:41 -0800192 initializeTargetCC1FlagCache(vfs, types, requirements);
Josh Gaob5c49632016-11-08 22:21:31 -0800193
194 std::vector<std::pair<CompilationType, const std::string&>> jobs;
Josh Gao338cf122016-11-09 18:01:41 -0800195 std::atomic<size_t> job_index(0);
Josh Gao16016df2016-11-07 18:27:16 -0800196 for (CompilationType type : types) {
197 CompilationRequirements& req = requirements[type.arch];
198 for (const std::string& header : req.headers) {
Josh Gaob5c49632016-11-08 22:21:31 -0800199 jobs.emplace_back(type, header);
Josh Gaobf8a2852016-05-27 11:59:09 -0700200 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700201 }
202
Josh Gaob5c49632016-11-08 22:21:31 -0800203 thread_count = std::min(thread_count, jobs.size());
204
205 if (thread_count == 1) {
206 for (const auto& job : jobs) {
Josh Gao78b8a142016-11-09 01:00:41 -0800207 compileHeader(vfs, result.get(), job.first, job.second);
Josh Gaob5c49632016-11-08 22:21:31 -0800208 }
209 } else {
210 // Spawn threads.
211 for (size_t i = 0; i < thread_count; ++i) {
Yi Kongff6c8de2017-04-20 09:08:11 -0700212 threads.emplace_back([&jobs, &job_index, &result, vfs]() {
Josh Gao338cf122016-11-09 18:01:41 -0800213 while (true) {
214 size_t idx = job_index++;
215 if (idx >= jobs.size()) {
216 return;
217 }
218
219 const auto& job = jobs[idx];
Josh Gao78b8a142016-11-09 01:00:41 -0800220 compileHeader(vfs, result.get(), job.first, job.second);
Josh Gaob5c49632016-11-08 22:21:31 -0800221 }
222 });
Josh Gaobf8a2852016-05-27 11:59:09 -0700223 }
224
Josh Gaob5c49632016-11-08 22:21:31 -0800225 // Reap them.
226 for (auto& thread : threads) {
227 thread.join();
228 }
229 threads.clear();
Josh Gaobf8a2852016-05-27 11:59:09 -0700230 }
231
Josh Gaobfb6bae2016-07-15 17:25:21 -0700232 return result;
Josh Gaobf8a2852016-05-27 11:59:09 -0700233}
234
Josh Gao1a176de2016-11-04 13:15:11 -0700235static std::set<CompilationType> getCompilationTypes(const Declaration* decl) {
236 std::set<CompilationType> result;
237 for (const auto& it : decl->availability) {
238 result.insert(it.first);
239 }
240 return result;
241}
242
243template<typename T>
244static std::vector<T> Intersection(const std::set<T>& a, const std::set<T>& b) {
245 std::vector<T> intersection;
246 std::set_intersection(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(intersection));
247 return intersection;
248}
249
Josh Gaobfb6bae2016-07-15 17:25:21 -0700250// Perform a sanity check on a symbol's declarations, enforcing the following invariants:
251// 1. At most one inline definition of the function exists.
252// 2. All of the availability declarations for a symbol are compatible.
253// If a function is declared as an inline before a certain version, the inline definition
254// should have no version tag.
255// 3. Each availability type must only be present globally or on a per-arch basis.
256// (e.g. __INTRODUCED_IN_ARM(9) __INTRODUCED_IN_X86(10) __DEPRECATED_IN(11) is fine,
257// but not __INTRODUCED_IN(9) __INTRODUCED_IN_X86(10))
258static bool checkSymbol(const Symbol& symbol) {
259 std::string cwd = getWorkingDir() + "/";
260
Josh Gao1a176de2016-11-04 13:15:11 -0700261 std::unordered_map<const Declaration*, std::set<CompilationType>> inline_definitions;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700262 for (const auto& decl_it : symbol.declarations) {
263 const Declaration* decl = &decl_it.second;
264 if (decl->is_definition) {
Josh Gao1a176de2016-11-04 13:15:11 -0700265 std::set<CompilationType> compilation_types = getCompilationTypes(decl);
266 for (const auto& inline_def_it : inline_definitions) {
267 auto intersection = Intersection(compilation_types, inline_def_it.second);
268 if (!intersection.empty()) {
269 fprintf(stderr, "versioner: conflicting inline definitions:\n");
270 fprintf(stderr, " declarations visible in: %s\n", Join(intersection, ", ").c_str());
271 decl->dump(cwd, stderr, 4);
272 inline_def_it.first->dump(cwd, stderr, 4);
273 return false;
274 }
Josh Gaobfb6bae2016-07-15 17:25:21 -0700275 }
276
Josh Gao1a176de2016-11-04 13:15:11 -0700277 inline_definitions[decl] = std::move(compilation_types);
Josh Gaobfb6bae2016-07-15 17:25:21 -0700278 }
279
280 DeclarationAvailability availability;
281 if (!decl->calculateAvailability(&availability)) {
282 fprintf(stderr, "versioner: failed to calculate availability for declaration:\n");
Josh Gao566735d2016-08-02 15:07:32 -0700283 decl->dump(cwd, stderr, 2);
Josh Gaobfb6bae2016-07-15 17:25:21 -0700284 return false;
285 }
286
287 if (decl->is_definition && !availability.empty()) {
288 fprintf(stderr, "versioner: inline definition has non-empty versioning information:\n");
Josh Gao566735d2016-08-02 15:07:32 -0700289 decl->dump(cwd, stderr, 2);
Josh Gaobfb6bae2016-07-15 17:25:21 -0700290 return false;
291 }
292 }
293
294 DeclarationAvailability availability;
295 if (!symbol.calculateAvailability(&availability)) {
296 fprintf(stderr, "versioner: inconsistent availability for symbol '%s'\n", symbol.name.c_str());
297 symbol.dump(cwd);
298 return false;
299 }
300
301 // TODO: Check invariant #3.
302 return true;
303}
304
305static bool sanityCheck(const HeaderDatabase* database) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700306 bool error = false;
Josh Gao958f3b32016-06-03 13:44:00 -0700307 std::string cwd = getWorkingDir() + "/";
308
Josh Gaobfb6bae2016-07-15 17:25:21 -0700309 for (const auto& symbol_it : database->symbols) {
310 if (!checkSymbol(symbol_it.second)) {
311 error = true;
Josh Gaobf8a2852016-05-27 11:59:09 -0700312 }
313 }
314 return !error;
315}
316
317// Check that our symbol availability declarations match the actual NDK
318// platform symbol availability.
319static bool checkVersions(const std::set<CompilationType>& types,
Josh Gaobfb6bae2016-07-15 17:25:21 -0700320 const HeaderDatabase* header_database,
Josh Gaobf8a2852016-05-27 11:59:09 -0700321 const NdkSymbolDatabase& symbol_database) {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700322 std::string cwd = getWorkingDir() + "/";
Josh Gaobf8a2852016-05-27 11:59:09 -0700323 bool failed = false;
324
Josh Gaobfb6bae2016-07-15 17:25:21 -0700325 std::map<Arch, std::set<CompilationType>> arch_types;
Josh Gaobf8a2852016-05-27 11:59:09 -0700326 for (const CompilationType& type : types) {
327 arch_types[type.arch].insert(type);
328 }
329
Josh Gaod67dbf02016-06-02 15:21:14 -0700330 std::set<std::string> completely_unavailable;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700331 std::map<std::string, std::set<CompilationType>> missing_availability;
332 std::map<std::string, std::set<CompilationType>> extra_availability;
Josh Gaod67dbf02016-06-02 15:21:14 -0700333
Josh Gaobfb6bae2016-07-15 17:25:21 -0700334 for (const auto& symbol_it : header_database->symbols) {
335 const auto& symbol_name = symbol_it.first;
336 DeclarationAvailability symbol_availability;
Josh Gaobf8a2852016-05-27 11:59:09 -0700337
Josh Gaobfb6bae2016-07-15 17:25:21 -0700338 if (!symbol_it.second.calculateAvailability(&symbol_availability)) {
339 errx(1, "failed to calculate symbol availability");
340 }
341
342 const auto platform_availability_it = symbol_database.find(symbol_name);
Josh Gaobf8a2852016-05-27 11:59:09 -0700343 if (platform_availability_it == symbol_database.end()) {
Josh Gaod67dbf02016-06-02 15:21:14 -0700344 completely_unavailable.insert(symbol_name);
Josh Gaobf8a2852016-05-27 11:59:09 -0700345 continue;
346 }
347
348 const auto& platform_availability = platform_availability_it->second;
Josh Gaobf8a2852016-05-27 11:59:09 -0700349
350 for (const CompilationType& type : types) {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700351 bool should_be_available = true;
352 const auto& global_availability = symbol_availability.global_availability;
353 const auto& arch_availability = symbol_availability.arch_availability[type.arch];
354 if (global_availability.introduced != 0 && global_availability.introduced > type.api_level) {
355 should_be_available = false;
356 }
357
358 if (arch_availability.introduced != 0 && arch_availability.introduced > type.api_level) {
359 should_be_available = false;
360 }
361
362 if (global_availability.obsoleted != 0 && global_availability.obsoleted <= type.api_level) {
363 should_be_available = false;
364 }
365
366 if (arch_availability.obsoleted != 0 && arch_availability.obsoleted <= type.api_level) {
367 should_be_available = false;
368 }
369
370 if (arch_availability.future) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700371 continue;
372 }
373
Josh Gaobfb6bae2016-07-15 17:25:21 -0700374 // The function declaration might be (validly) missing for the given CompilationType.
375 if (!symbol_it.second.hasDeclaration(type)) {
376 should_be_available = false;
377 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700378
Josh Gaobfb6bae2016-07-15 17:25:21 -0700379 bool is_available = platform_availability.count(type);
Josh Gaobf8a2852016-05-27 11:59:09 -0700380
Josh Gaobfb6bae2016-07-15 17:25:21 -0700381 if (should_be_available != is_available) {
382 if (is_available) {
383 extra_availability[symbol_name].insert(type);
Josh Gaobf8a2852016-05-27 11:59:09 -0700384 } else {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700385 missing_availability[symbol_name].insert(type);
Josh Gaobf8a2852016-05-27 11:59:09 -0700386 }
387 }
388 }
Josh Gaobfb6bae2016-07-15 17:25:21 -0700389 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700390
Josh Gaobfb6bae2016-07-15 17:25:21 -0700391 for (const auto& it : symbol_database) {
392 const std::string& symbol_name = it.first;
Josh Gaobf8a2852016-05-27 11:59:09 -0700393
Josh Gaobfb6bae2016-07-15 17:25:21 -0700394 bool symbol_error = false;
Josh Gao0a284f52016-12-15 13:56:00 -0800395 if (auto missing_it = missing_availability.find(symbol_name);
396 missing_it != missing_availability.end()) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700397 printf("%s: declaration marked available but symbol missing in [%s]\n", symbol_name.c_str(),
Josh Gaobfb6bae2016-07-15 17:25:21 -0700398 Join(missing_it->second, ", ").c_str());
399 symbol_error = true;
Josh Gaobf8a2852016-05-27 11:59:09 -0700400 failed = true;
401 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700402
Josh Gaoacc3d802016-11-09 18:22:44 -0800403 if (strict) {
Josh Gao0a284f52016-12-15 13:56:00 -0800404 if (auto extra_it = extra_availability.find(symbol_name);
405 extra_it != extra_availability.end()) {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700406 printf("%s: declaration marked unavailable but symbol available in [%s]\n",
407 symbol_name.c_str(), Join(extra_it->second, ", ").c_str());
408 symbol_error = true;
409 failed = true;
Josh Gao958f3b32016-06-03 13:44:00 -0700410 }
411 }
412
Josh Gaobfb6bae2016-07-15 17:25:21 -0700413 if (symbol_error) {
Josh Gao0a284f52016-12-15 13:56:00 -0800414 if (auto symbol_it = header_database->symbols.find(symbol_name);
415 symbol_it != header_database->symbols.end()) {
416 symbol_it->second.dump(cwd);
417 } else {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700418 errx(1, "failed to find symbol in header database");
419 }
Josh Gaod67dbf02016-06-02 15:21:14 -0700420 }
Josh Gaod67dbf02016-06-02 15:21:14 -0700421 }
422
Josh Gaobfb6bae2016-07-15 17:25:21 -0700423 // TODO: Verify that function/variable declarations are actually function/variable symbols.
Josh Gaobf8a2852016-05-27 11:59:09 -0700424 return !failed;
425}
426
Josh Gao62aaf8f2016-06-02 14:27:21 -0700427static void usage(bool help = false) {
428 fprintf(stderr, "Usage: versioner [OPTION]... [HEADER_PATH] [DEPS_PATH]\n");
429 if (!help) {
430 printf("Try 'versioner -h' for more information.\n");
431 exit(1);
432 } else {
433 fprintf(stderr, "Version headers at HEADER_PATH, with DEPS_PATH/ARCH/* on the include path\n");
Josh Gao9b5af7a2016-06-02 14:29:13 -0700434 fprintf(stderr, "Autodetects paths if HEADER_PATH and DEPS_PATH are not specified\n");
Josh Gao62aaf8f2016-06-02 14:27:21 -0700435 fprintf(stderr, "\n");
436 fprintf(stderr, "Target specification (defaults to all):\n");
437 fprintf(stderr, " -a API_LEVEL\tbuild with specified API level (can be repeated)\n");
438 fprintf(stderr, " \t\tvalid levels are %s\n", Join(supported_levels).c_str());
439 fprintf(stderr, " -r ARCH\tbuild with specified architecture (can be repeated)\n");
440 fprintf(stderr, " \t\tvalid architectures are %s\n", Join(supported_archs).c_str());
441 fprintf(stderr, "\n");
442 fprintf(stderr, "Validation:\n");
443 fprintf(stderr, " -p PATH\tcompare against NDK platform at PATH\n");
Josh Gaoacc3d802016-11-09 18:22:44 -0800444 fprintf(stderr, " -s\t\tenable strict warnings\n");
Josh Gao62aaf8f2016-06-02 14:27:21 -0700445 fprintf(stderr, "\n");
Josh Gaof8592a32016-07-26 18:58:27 -0700446 fprintf(stderr, "Preprocessing:\n");
447 fprintf(stderr, " -o PATH\tpreprocess header files and emit them at PATH\n");
Josh Gaod744a9b2017-04-03 11:24:48 -0700448 fprintf(stderr, " -f\t\tpreprocess header files even if validation fails\n");
Josh Gaof8592a32016-07-26 18:58:27 -0700449 fprintf(stderr, "\n");
Josh Gao62aaf8f2016-06-02 14:27:21 -0700450 fprintf(stderr, "Miscellaneous:\n");
George Burgess IVb97049c2017-07-24 15:05:05 -0700451 fprintf(stderr, " -F\t\tdo not ignore FORTIFY headers by default\n");
Josh Gaobfb6bae2016-07-15 17:25:21 -0700452 fprintf(stderr, " -d\t\tdump function availability\n");
Josh Gao16016df2016-11-07 18:27:16 -0800453 fprintf(stderr, " -j THREADS\tmaximum number of threads to use\n");
Josh Gaoacc3d802016-11-09 18:22:44 -0800454 fprintf(stderr, " -v\t\tenable verbose logging\n");
Josh Gao62aaf8f2016-06-02 14:27:21 -0700455 fprintf(stderr, " -h\t\tdisplay this message\n");
456 exit(0);
457 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700458}
459
Andreas Gamped10d3ee2017-04-28 19:32:13 -0700460// versioner uses a prebuilt version of clang, which is not up-to-date wrt/
461// container annotations. So disable container overflow checking. b/37775238
462extern "C" const char* __asan_default_options() {
463 return "detect_container_overflow=0";
464}
465
Josh Gaobf8a2852016-05-27 11:59:09 -0700466int main(int argc, char** argv) {
467 std::string cwd = getWorkingDir() + "/";
468 bool default_args = true;
469 std::string platform_dir;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700470 std::set<Arch> selected_architectures;
Josh Gaobf8a2852016-05-27 11:59:09 -0700471 std::set<int> selected_levels;
Josh Gaof8592a32016-07-26 18:58:27 -0700472 std::string preprocessor_output_path;
473 bool force = false;
Josh Gao16016df2016-11-07 18:27:16 -0800474 bool dump = false;
George Burgess IVb97049c2017-07-24 15:05:05 -0700475 bool ignore_fortify_headers = true;
Josh Gaobf8a2852016-05-27 11:59:09 -0700476
477 int c;
George Burgess IVb97049c2017-07-24 15:05:05 -0700478 while ((c = getopt(argc, argv, "a:r:p:so:fdj:vhFi")) != -1) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700479 default_args = false;
480 switch (c) {
481 case 'a': {
482 char* end;
483 int api_level = strtol(optarg, &end, 10);
484 if (end == optarg || strlen(end) > 0) {
485 usage();
486 }
487
488 if (supported_levels.count(api_level) == 0) {
489 errx(1, "unsupported API level %d", api_level);
490 }
491
492 selected_levels.insert(api_level);
493 break;
494 }
495
496 case 'r': {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700497 Arch arch = arch_from_string(optarg);
498 selected_architectures.insert(arch);
Josh Gaobf8a2852016-05-27 11:59:09 -0700499 break;
500 }
501
502 case 'p': {
503 if (!platform_dir.empty()) {
504 usage();
505 }
506
507 platform_dir = optarg;
508
Josh Gaof8592a32016-07-26 18:58:27 -0700509 if (platform_dir.empty()) {
510 usage();
511 }
512
Josh Gaobf8a2852016-05-27 11:59:09 -0700513 struct stat st;
514 if (stat(platform_dir.c_str(), &st) != 0) {
515 err(1, "failed to stat platform directory '%s'", platform_dir.c_str());
516 }
517 if (!S_ISDIR(st.st_mode)) {
518 errx(1, "'%s' is not a directory", optarg);
519 }
520 break;
521 }
522
Josh Gaoacc3d802016-11-09 18:22:44 -0800523 case 's':
524 strict = true;
Josh Gaobf8a2852016-05-27 11:59:09 -0700525 break;
526
Josh Gaof8592a32016-07-26 18:58:27 -0700527 case 'o':
528 if (!preprocessor_output_path.empty()) {
529 usage();
530 }
531 preprocessor_output_path = optarg;
532 if (preprocessor_output_path.empty()) {
533 usage();
534 }
535 break;
536
537 case 'f':
538 force = true;
539 break;
540
Josh Gaobfb6bae2016-07-15 17:25:21 -0700541 case 'd':
542 dump = true;
543 break;
544
Josh Gao16016df2016-11-07 18:27:16 -0800545 case 'j':
546 if (!android::base::ParseInt<int>(optarg, &max_thread_count, 1)) {
547 usage();
548 }
549 break;
550
Josh Gaoacc3d802016-11-09 18:22:44 -0800551 case 'v':
552 verbose = true;
553 break;
554
Josh Gao62aaf8f2016-06-02 14:27:21 -0700555 case 'h':
556 usage(true);
557 break;
558
Josh Gaobfb6bae2016-07-15 17:25:21 -0700559 case 'i':
560 // Secret option for tests to -include <android/versioning.h>.
561 add_include = true;
562 break;
563
George Burgess IVb97049c2017-07-24 15:05:05 -0700564 case 'F':
565 ignore_fortify_headers = false;
566 break;
567
Josh Gaobf8a2852016-05-27 11:59:09 -0700568 default:
569 usage();
570 break;
571 }
572 }
573
Josh Gao9b5af7a2016-06-02 14:29:13 -0700574 if (argc - optind > 2 || optind > argc) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700575 usage();
576 }
577
George Burgess IVb97049c2017-07-24 15:05:05 -0700578 HeaderLocationInformation location;
Josh Gao9b5af7a2016-06-02 14:29:13 -0700579
Josh Gaobfb6bae2016-07-15 17:25:21 -0700580 const char* top = getenv("ANDROID_BUILD_TOP");
581 if (!top && (optind == argc || add_include)) {
582 fprintf(stderr, "versioner: failed to autodetect bionic paths. Is ANDROID_BUILD_TOP set?\n");
583 usage();
584 }
585
Josh Gao9b5af7a2016-06-02 14:29:13 -0700586 if (optind == argc) {
587 // Neither HEADER_PATH nor DEPS_PATH were specified, so try to figure them out.
Josh Gaobfb6bae2016-07-15 17:25:21 -0700588 std::string versioner_dir = to_string(top) + "/bionic/tools/versioner";
George Burgess IVb97049c2017-07-24 15:05:05 -0700589 location.header_dir = versioner_dir + "/current";
590 location.dependency_dir = versioner_dir + "/dependencies";
Josh Gao9b5af7a2016-06-02 14:29:13 -0700591 if (platform_dir.empty()) {
592 platform_dir = versioner_dir + "/platforms";
593 }
594 } else {
George Burgess IVb97049c2017-07-24 15:05:05 -0700595 if (!android::base::Realpath(argv[optind], &location.header_dir)) {
Josh Gaob50b8c82017-04-27 17:22:52 -0700596 err(1, "failed to get realpath for path '%s'", argv[optind]);
597 }
Josh Gao9b5af7a2016-06-02 14:29:13 -0700598
599 if (argc - optind == 2) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700600 location.dependency_dir = argv[optind + 1];
Josh Gao9b5af7a2016-06-02 14:29:13 -0700601 }
602 }
603
George Burgess IVb97049c2017-07-24 15:05:05 -0700604 // Every file that lives in bits/fortify is logically a part of a header outside of bits/fortify.
605 // This makes the files there impossible to build on their own.
606 if (ignore_fortify_headers) {
607 std::string fortify_path = location.header_dir;
608 if (!android::base::EndsWith(location.header_dir, "/")) {
609 fortify_path += '/';
610 }
611 fortify_path += "bits/fortify";
612 location.ignored_directories.insert(std::move(fortify_path));
613 }
614
Josh Gaobf8a2852016-05-27 11:59:09 -0700615 if (selected_levels.empty()) {
616 selected_levels = supported_levels;
617 }
618
619 if (selected_architectures.empty()) {
620 selected_architectures = supported_archs;
621 }
622
Josh Gaobf8a2852016-05-27 11:59:09 -0700623
624 struct stat st;
George Burgess IVb97049c2017-07-24 15:05:05 -0700625 if (const char *dir = location.header_dir.c_str(); stat(dir, &st) != 0) {
626 err(1, "failed to stat '%s'", dir);
Josh Gaobf8a2852016-05-27 11:59:09 -0700627 } else if (!S_ISDIR(st.st_mode)) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700628 errx(1, "'%s' is not a directory", dir);
Josh Gaobf8a2852016-05-27 11:59:09 -0700629 }
630
631 std::set<CompilationType> compilation_types;
Josh Gaobf8a2852016-05-27 11:59:09 -0700632 NdkSymbolDatabase symbol_database;
633
634 compilation_types = generateCompilationTypes(selected_architectures, selected_levels);
635
636 // Do this before compiling so that we can early exit if the platforms don't match what we
637 // expect.
638 if (!platform_dir.empty()) {
639 symbol_database = parsePlatforms(compilation_types, platform_dir);
640 }
641
Josh Gao338cf122016-11-09 18:01:41 -0800642 auto start = std::chrono::high_resolution_clock::now();
Josh Gaobfb6bae2016-07-15 17:25:21 -0700643 std::unique_ptr<HeaderDatabase> declaration_database =
George Burgess IVb97049c2017-07-24 15:05:05 -0700644 compileHeaders(compilation_types, location);
Josh Gao338cf122016-11-09 18:01:41 -0800645 auto end = std::chrono::high_resolution_clock::now();
646
647 if (verbose) {
648 auto diff = (end - start) / 1.0ms;
649 printf("Compiled headers for %zu targets in %0.2LFms\n", compilation_types.size(), diff);
650 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700651
Josh Gao16016df2016-11-07 18:27:16 -0800652 bool failed = false;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700653 if (dump) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700654 declaration_database->dump(location.header_dir + "/");
Josh Gaobfb6bae2016-07-15 17:25:21 -0700655 } else {
656 if (!sanityCheck(declaration_database.get())) {
657 printf("versioner: sanity check failed\n");
Josh Gaobf8a2852016-05-27 11:59:09 -0700658 failed = true;
659 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700660
Josh Gaobfb6bae2016-07-15 17:25:21 -0700661 if (!platform_dir.empty()) {
662 if (!checkVersions(compilation_types, declaration_database.get(), symbol_database)) {
663 printf("versioner: version check failed\n");
664 failed = true;
665 }
666 }
667 }
Josh Gaof8592a32016-07-26 18:58:27 -0700668
669 if (!preprocessor_output_path.empty() && (force || !failed)) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700670 failed = !preprocessHeaders(preprocessor_output_path, location.header_dir, declaration_database.get());
Josh Gaof8592a32016-07-26 18:58:27 -0700671 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700672 return failed;
673}