blob: 83b402760fbe980579103066388c255892e2dd3d [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>
Josh Gaod2ab9ff2017-07-28 12:53:36 -070021#include <stdlib.h>
Josh Gaobf8a2852016-05-27 11:59:09 -070022#include <sys/stat.h>
23#include <sys/types.h>
24#include <unistd.h>
25
Josh Gao338cf122016-11-09 18:01:41 -080026#if defined(__linux__)
27#include <sched.h>
28#endif
29
Josh Gaobf8a2852016-05-27 11:59:09 -070030#include <atomic>
Josh Gao338cf122016-11-09 18:01:41 -080031#include <chrono>
Josh Gao16016df2016-11-07 18:27:16 -080032#include <functional>
Josh Gaobf8a2852016-05-27 11:59:09 -070033#include <iostream>
34#include <map>
35#include <memory>
36#include <set>
37#include <sstream>
38#include <string>
39#include <thread>
40#include <unordered_map>
41#include <vector>
42
Josh Gaobf8a2852016-05-27 11:59:09 -070043#include <llvm/ADT/StringRef.h>
44
Josh Gaob50b8c82017-04-27 17:22:52 -070045#include <android-base/file.h>
Josh Gao338cf122016-11-09 18:01:41 -080046#include <android-base/macros.h>
Josh Gao16016df2016-11-07 18:27:16 -080047#include <android-base/parseint.h>
George Burgess IVb97049c2017-07-24 15:05:05 -070048#include <android-base/strings.h>
Josh Gao16016df2016-11-07 18:27:16 -080049
Josh Gaobfb6bae2016-07-15 17:25:21 -070050#include "Arch.h"
Josh Gaobf8a2852016-05-27 11:59:09 -070051#include "DeclarationDatabase.h"
Josh Gaob5c49632016-11-08 22:21:31 -080052#include "Driver.h"
Josh Gaof8592a32016-07-26 18:58:27 -070053#include "Preprocessor.h"
Josh Gaobf8a2852016-05-27 11:59:09 -070054#include "SymbolDatabase.h"
55#include "Utils.h"
Josh Gao78b8a142016-11-09 01:00:41 -080056#include "VFS.h"
57
Josh Gaobf8a2852016-05-27 11:59:09 -070058#include "versioner.h"
59
Josh Gao338cf122016-11-09 18:01:41 -080060using namespace std::chrono_literals;
Josh Gaobf8a2852016-05-27 11:59:09 -070061using namespace std::string_literals;
Josh Gaobf8a2852016-05-27 11:59:09 -070062
Josh Gaoacc3d802016-11-09 18:22:44 -080063bool strict;
Josh Gaobf8a2852016-05-27 11:59:09 -070064bool verbose;
Josh Gaoacc3d802016-11-09 18:22:44 -080065bool add_include;
Josh Gao338cf122016-11-09 18:01:41 -080066
67static int getCpuCount();
68static int max_thread_count = getCpuCount();
69
70static int getCpuCount() {
71#if defined(__linux__)
72 cpu_set_t cpu_set;
73 int rc = sched_getaffinity(getpid(), sizeof(cpu_set), &cpu_set);
74 if (rc != 0) {
75 err(1, "sched_getaffinity failed");
76 }
77 return CPU_COUNT(&cpu_set);
78#else
79 return 1;
80#endif
81}
Josh Gaobf8a2852016-05-27 11:59:09 -070082
George Burgess IVb97049c2017-07-24 15:05:05 -070083namespace {
84struct HeaderLocationInformation {
85 std::string header_dir;
86 std::string dependency_dir;
87 // Absolute paths to ignore all children -- including subdirectories -- of.
88 std::unordered_set<std::string> ignored_directories;
89};
90}
91
92static CompilationRequirements collectRequirements(const Arch& arch,
93 const HeaderLocationInformation& location) {
94 std::vector<std::string> headers =
95 collectHeaders(location.header_dir, location.ignored_directories);
96 std::vector<std::string> dependencies = { location.header_dir };
97 if (!location.dependency_dir.empty()) {
Yi Kongff6c8de2017-04-20 09:08:11 -070098 auto collect_children = [&dependencies](const std::string& dir_path) {
Josh Gaobf8a2852016-05-27 11:59:09 -070099 DIR* dir = opendir(dir_path.c_str());
100 if (!dir) {
101 err(1, "failed to open dependency directory '%s'", dir_path.c_str());
102 }
103
104 struct dirent* dent;
105 while ((dent = readdir(dir))) {
106 if (dent->d_name[0] == '.') {
107 continue;
108 }
109
110 // TODO: Resolve symlinks.
111 std::string dependency = dir_path + "/" + dent->d_name;
112
113 struct stat st;
114 if (stat(dependency.c_str(), &st) != 0) {
115 err(1, "failed to stat dependency '%s'", dependency.c_str());
116 }
117
118 if (!S_ISDIR(st.st_mode)) {
119 errx(1, "'%s' is not a directory", dependency.c_str());
120 }
121
122 dependencies.push_back(dependency);
123 }
124
125 closedir(dir);
126 };
127
George Burgess IVb97049c2017-07-24 15:05:05 -0700128 collect_children(location.dependency_dir + "/common");
129 collect_children(location.dependency_dir + "/" + to_string(arch));
Josh Gaobf8a2852016-05-27 11:59:09 -0700130 }
131
132 auto new_end = std::remove_if(headers.begin(), headers.end(), [&arch](llvm::StringRef header) {
133 for (const auto& it : header_blacklist) {
134 if (it.second.find(arch) == it.second.end()) {
135 continue;
136 }
137
138 if (header.endswith("/" + it.first)) {
139 return true;
140 }
141 }
142 return false;
143 });
144
145 headers.erase(new_end, headers.end());
146
147 CompilationRequirements result = { .headers = headers, .dependencies = dependencies };
148 return result;
149}
150
Josh Gaobfb6bae2016-07-15 17:25:21 -0700151static std::set<CompilationType> generateCompilationTypes(const std::set<Arch> selected_architectures,
152 const std::set<int>& selected_levels) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700153 std::set<CompilationType> result;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700154 for (const auto& arch : selected_architectures) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700155 int min_api = arch_min_api[arch];
156 for (int api_level : selected_levels) {
157 if (api_level < min_api) {
158 continue;
159 }
Josh Gaoa77b3a92016-08-15 16:39:27 -0700160
161 for (int file_offset_bits : { 32, 64 }) {
162 CompilationType type = {
163 .arch = arch, .api_level = api_level, .file_offset_bits = file_offset_bits
164 };
165 result.insert(type);
166 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700167 }
168 }
169 return result;
170}
171
Josh Gaobfb6bae2016-07-15 17:25:21 -0700172static std::unique_ptr<HeaderDatabase> compileHeaders(const std::set<CompilationType>& types,
George Burgess IVb97049c2017-07-24 15:05:05 -0700173 const HeaderLocationInformation& location) {
Josh Gao16016df2016-11-07 18:27:16 -0800174 if (types.empty()) {
175 errx(1, "compileHeaders received no CompilationTypes");
176 }
177
George Burgess IVb97049c2017-07-24 15:05:05 -0700178 auto vfs = createCommonVFS(location.header_dir, location.dependency_dir, add_include);
Josh Gao78b8a142016-11-09 01:00:41 -0800179
Josh Gao16016df2016-11-07 18:27:16 -0800180 size_t thread_count = max_thread_count;
181 std::vector<std::thread> threads;
Josh Gaobf8a2852016-05-27 11:59:09 -0700182
183 std::map<CompilationType, HeaderDatabase> header_databases;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700184 std::unordered_map<Arch, CompilationRequirements> requirements;
Josh Gaobf8a2852016-05-27 11:59:09 -0700185
Josh Gao16016df2016-11-07 18:27:16 -0800186 auto result = std::make_unique<HeaderDatabase>();
Josh Gaobf8a2852016-05-27 11:59:09 -0700187 for (const auto& type : types) {
188 if (requirements.count(type.arch) == 0) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700189 requirements[type.arch] = collectRequirements(type.arch, location);
Josh Gaobf8a2852016-05-27 11:59:09 -0700190 }
191 }
192
Josh Gao78b8a142016-11-09 01:00:41 -0800193 initializeTargetCC1FlagCache(vfs, types, requirements);
Josh Gaob5c49632016-11-08 22:21:31 -0800194
195 std::vector<std::pair<CompilationType, const std::string&>> jobs;
Josh Gao338cf122016-11-09 18:01:41 -0800196 std::atomic<size_t> job_index(0);
Josh Gao16016df2016-11-07 18:27:16 -0800197 for (CompilationType type : types) {
198 CompilationRequirements& req = requirements[type.arch];
199 for (const std::string& header : req.headers) {
Josh Gaob5c49632016-11-08 22:21:31 -0800200 jobs.emplace_back(type, header);
Josh Gaobf8a2852016-05-27 11:59:09 -0700201 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700202 }
203
Josh Gaod2ab9ff2017-07-28 12:53:36 -0700204 // Dup an empty file to stdin, so that we can use `clang -include a.h -` instead of `clang a.h`,
205 // since some warnings don't get generated in files that are compiled directly.
206 FILE* empty_file = tmpfile();
207 if (!empty_file) {
208 err(1, "failed to create temporary file");
209 }
210
211 int empty_file_fd = fileno(empty_file);
212 if (empty_file_fd == -1) {
213 errx(1, "fileno failed on tmpfile");
214 }
215
216 dup2(empty_file_fd, STDIN_FILENO);
217 fclose(empty_file);
218
Josh Gaob5c49632016-11-08 22:21:31 -0800219 thread_count = std::min(thread_count, jobs.size());
220
221 if (thread_count == 1) {
222 for (const auto& job : jobs) {
Josh Gao78b8a142016-11-09 01:00:41 -0800223 compileHeader(vfs, result.get(), job.first, job.second);
Josh Gaob5c49632016-11-08 22:21:31 -0800224 }
225 } else {
226 // Spawn threads.
227 for (size_t i = 0; i < thread_count; ++i) {
Yi Kongff6c8de2017-04-20 09:08:11 -0700228 threads.emplace_back([&jobs, &job_index, &result, vfs]() {
Josh Gao338cf122016-11-09 18:01:41 -0800229 while (true) {
230 size_t idx = job_index++;
231 if (idx >= jobs.size()) {
232 return;
233 }
234
235 const auto& job = jobs[idx];
Josh Gao78b8a142016-11-09 01:00:41 -0800236 compileHeader(vfs, result.get(), job.first, job.second);
Josh Gaob5c49632016-11-08 22:21:31 -0800237 }
238 });
Josh Gaobf8a2852016-05-27 11:59:09 -0700239 }
240
Josh Gaob5c49632016-11-08 22:21:31 -0800241 // Reap them.
242 for (auto& thread : threads) {
243 thread.join();
244 }
245 threads.clear();
Josh Gaobf8a2852016-05-27 11:59:09 -0700246 }
247
Josh Gaobfb6bae2016-07-15 17:25:21 -0700248 return result;
Josh Gaobf8a2852016-05-27 11:59:09 -0700249}
250
Josh Gao1a176de2016-11-04 13:15:11 -0700251static std::set<CompilationType> getCompilationTypes(const Declaration* decl) {
252 std::set<CompilationType> result;
253 for (const auto& it : decl->availability) {
254 result.insert(it.first);
255 }
256 return result;
257}
258
259template<typename T>
260static std::vector<T> Intersection(const std::set<T>& a, const std::set<T>& b) {
261 std::vector<T> intersection;
262 std::set_intersection(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(intersection));
263 return intersection;
264}
265
Josh Gaobfb6bae2016-07-15 17:25:21 -0700266// Perform a sanity check on a symbol's declarations, enforcing the following invariants:
267// 1. At most one inline definition of the function exists.
268// 2. All of the availability declarations for a symbol are compatible.
269// If a function is declared as an inline before a certain version, the inline definition
270// should have no version tag.
271// 3. Each availability type must only be present globally or on a per-arch basis.
272// (e.g. __INTRODUCED_IN_ARM(9) __INTRODUCED_IN_X86(10) __DEPRECATED_IN(11) is fine,
273// but not __INTRODUCED_IN(9) __INTRODUCED_IN_X86(10))
274static bool checkSymbol(const Symbol& symbol) {
275 std::string cwd = getWorkingDir() + "/";
276
Josh Gao1a176de2016-11-04 13:15:11 -0700277 std::unordered_map<const Declaration*, std::set<CompilationType>> inline_definitions;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700278 for (const auto& decl_it : symbol.declarations) {
279 const Declaration* decl = &decl_it.second;
280 if (decl->is_definition) {
Josh Gao1a176de2016-11-04 13:15:11 -0700281 std::set<CompilationType> compilation_types = getCompilationTypes(decl);
282 for (const auto& inline_def_it : inline_definitions) {
283 auto intersection = Intersection(compilation_types, inline_def_it.second);
284 if (!intersection.empty()) {
285 fprintf(stderr, "versioner: conflicting inline definitions:\n");
286 fprintf(stderr, " declarations visible in: %s\n", Join(intersection, ", ").c_str());
287 decl->dump(cwd, stderr, 4);
288 inline_def_it.first->dump(cwd, stderr, 4);
289 return false;
290 }
Josh Gaobfb6bae2016-07-15 17:25:21 -0700291 }
292
Josh Gao1a176de2016-11-04 13:15:11 -0700293 inline_definitions[decl] = std::move(compilation_types);
Josh Gaobfb6bae2016-07-15 17:25:21 -0700294 }
295
296 DeclarationAvailability availability;
297 if (!decl->calculateAvailability(&availability)) {
298 fprintf(stderr, "versioner: failed to calculate availability for declaration:\n");
Josh Gao566735d2016-08-02 15:07:32 -0700299 decl->dump(cwd, stderr, 2);
Josh Gaobfb6bae2016-07-15 17:25:21 -0700300 return false;
301 }
302
303 if (decl->is_definition && !availability.empty()) {
304 fprintf(stderr, "versioner: inline definition has non-empty versioning information:\n");
Josh Gao566735d2016-08-02 15:07:32 -0700305 decl->dump(cwd, stderr, 2);
Josh Gaobfb6bae2016-07-15 17:25:21 -0700306 return false;
307 }
308 }
309
310 DeclarationAvailability availability;
311 if (!symbol.calculateAvailability(&availability)) {
312 fprintf(stderr, "versioner: inconsistent availability for symbol '%s'\n", symbol.name.c_str());
313 symbol.dump(cwd);
314 return false;
315 }
316
317 // TODO: Check invariant #3.
318 return true;
319}
320
321static bool sanityCheck(const HeaderDatabase* database) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700322 bool error = false;
Josh Gao958f3b32016-06-03 13:44:00 -0700323 std::string cwd = getWorkingDir() + "/";
324
Josh Gaobfb6bae2016-07-15 17:25:21 -0700325 for (const auto& symbol_it : database->symbols) {
326 if (!checkSymbol(symbol_it.second)) {
327 error = true;
Josh Gaobf8a2852016-05-27 11:59:09 -0700328 }
329 }
330 return !error;
331}
332
333// Check that our symbol availability declarations match the actual NDK
334// platform symbol availability.
335static bool checkVersions(const std::set<CompilationType>& types,
Josh Gaobfb6bae2016-07-15 17:25:21 -0700336 const HeaderDatabase* header_database,
Josh Gaobf8a2852016-05-27 11:59:09 -0700337 const NdkSymbolDatabase& symbol_database) {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700338 std::string cwd = getWorkingDir() + "/";
Josh Gaobf8a2852016-05-27 11:59:09 -0700339 bool failed = false;
340
Josh Gaobfb6bae2016-07-15 17:25:21 -0700341 std::map<Arch, std::set<CompilationType>> arch_types;
Josh Gaobf8a2852016-05-27 11:59:09 -0700342 for (const CompilationType& type : types) {
343 arch_types[type.arch].insert(type);
344 }
345
Josh Gaod67dbf02016-06-02 15:21:14 -0700346 std::set<std::string> completely_unavailable;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700347 std::map<std::string, std::set<CompilationType>> missing_availability;
348 std::map<std::string, std::set<CompilationType>> extra_availability;
Josh Gaod67dbf02016-06-02 15:21:14 -0700349
Josh Gaobfb6bae2016-07-15 17:25:21 -0700350 for (const auto& symbol_it : header_database->symbols) {
351 const auto& symbol_name = symbol_it.first;
352 DeclarationAvailability symbol_availability;
Josh Gaobf8a2852016-05-27 11:59:09 -0700353
Josh Gaobfb6bae2016-07-15 17:25:21 -0700354 if (!symbol_it.second.calculateAvailability(&symbol_availability)) {
355 errx(1, "failed to calculate symbol availability");
356 }
357
358 const auto platform_availability_it = symbol_database.find(symbol_name);
Josh Gaobf8a2852016-05-27 11:59:09 -0700359 if (platform_availability_it == symbol_database.end()) {
Josh Gaod67dbf02016-06-02 15:21:14 -0700360 completely_unavailable.insert(symbol_name);
Josh Gaobf8a2852016-05-27 11:59:09 -0700361 continue;
362 }
363
364 const auto& platform_availability = platform_availability_it->second;
Josh Gaobf8a2852016-05-27 11:59:09 -0700365
366 for (const CompilationType& type : types) {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700367 bool should_be_available = true;
368 const auto& global_availability = symbol_availability.global_availability;
369 const auto& arch_availability = symbol_availability.arch_availability[type.arch];
370 if (global_availability.introduced != 0 && global_availability.introduced > type.api_level) {
371 should_be_available = false;
372 }
373
374 if (arch_availability.introduced != 0 && arch_availability.introduced > type.api_level) {
375 should_be_available = false;
376 }
377
378 if (global_availability.obsoleted != 0 && global_availability.obsoleted <= type.api_level) {
379 should_be_available = false;
380 }
381
382 if (arch_availability.obsoleted != 0 && arch_availability.obsoleted <= type.api_level) {
383 should_be_available = false;
384 }
385
386 if (arch_availability.future) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700387 continue;
388 }
389
Josh Gaobfb6bae2016-07-15 17:25:21 -0700390 // The function declaration might be (validly) missing for the given CompilationType.
391 if (!symbol_it.second.hasDeclaration(type)) {
392 should_be_available = false;
393 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700394
Josh Gaobfb6bae2016-07-15 17:25:21 -0700395 bool is_available = platform_availability.count(type);
Josh Gaobf8a2852016-05-27 11:59:09 -0700396
Josh Gaobfb6bae2016-07-15 17:25:21 -0700397 if (should_be_available != is_available) {
398 if (is_available) {
399 extra_availability[symbol_name].insert(type);
Josh Gaobf8a2852016-05-27 11:59:09 -0700400 } else {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700401 missing_availability[symbol_name].insert(type);
Josh Gaobf8a2852016-05-27 11:59:09 -0700402 }
403 }
404 }
Josh Gaobfb6bae2016-07-15 17:25:21 -0700405 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700406
Josh Gaobfb6bae2016-07-15 17:25:21 -0700407 for (const auto& it : symbol_database) {
408 const std::string& symbol_name = it.first;
Josh Gaobf8a2852016-05-27 11:59:09 -0700409
Josh Gaobfb6bae2016-07-15 17:25:21 -0700410 bool symbol_error = false;
Josh Gao0a284f52016-12-15 13:56:00 -0800411 if (auto missing_it = missing_availability.find(symbol_name);
412 missing_it != missing_availability.end()) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700413 printf("%s: declaration marked available but symbol missing in [%s]\n", symbol_name.c_str(),
Josh Gaobfb6bae2016-07-15 17:25:21 -0700414 Join(missing_it->second, ", ").c_str());
415 symbol_error = true;
Josh Gaobf8a2852016-05-27 11:59:09 -0700416 failed = true;
417 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700418
Josh Gaoacc3d802016-11-09 18:22:44 -0800419 if (strict) {
Josh Gao0a284f52016-12-15 13:56:00 -0800420 if (auto extra_it = extra_availability.find(symbol_name);
421 extra_it != extra_availability.end()) {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700422 printf("%s: declaration marked unavailable but symbol available in [%s]\n",
423 symbol_name.c_str(), Join(extra_it->second, ", ").c_str());
424 symbol_error = true;
425 failed = true;
Josh Gao958f3b32016-06-03 13:44:00 -0700426 }
427 }
428
Josh Gaobfb6bae2016-07-15 17:25:21 -0700429 if (symbol_error) {
Josh Gao0a284f52016-12-15 13:56:00 -0800430 if (auto symbol_it = header_database->symbols.find(symbol_name);
431 symbol_it != header_database->symbols.end()) {
432 symbol_it->second.dump(cwd);
433 } else {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700434 errx(1, "failed to find symbol in header database");
435 }
Josh Gaod67dbf02016-06-02 15:21:14 -0700436 }
Josh Gaod67dbf02016-06-02 15:21:14 -0700437 }
438
Josh Gaobfb6bae2016-07-15 17:25:21 -0700439 // TODO: Verify that function/variable declarations are actually function/variable symbols.
Josh Gaobf8a2852016-05-27 11:59:09 -0700440 return !failed;
441}
442
Josh Gao62aaf8f2016-06-02 14:27:21 -0700443static void usage(bool help = false) {
444 fprintf(stderr, "Usage: versioner [OPTION]... [HEADER_PATH] [DEPS_PATH]\n");
445 if (!help) {
446 printf("Try 'versioner -h' for more information.\n");
447 exit(1);
448 } else {
449 fprintf(stderr, "Version headers at HEADER_PATH, with DEPS_PATH/ARCH/* on the include path\n");
Josh Gao9b5af7a2016-06-02 14:29:13 -0700450 fprintf(stderr, "Autodetects paths if HEADER_PATH and DEPS_PATH are not specified\n");
Josh Gao62aaf8f2016-06-02 14:27:21 -0700451 fprintf(stderr, "\n");
452 fprintf(stderr, "Target specification (defaults to all):\n");
453 fprintf(stderr, " -a API_LEVEL\tbuild with specified API level (can be repeated)\n");
Josh Gao35aa2132017-10-24 17:05:49 -0700454 fprintf(stderr, " \t\tdefaults to %s\n", Join(default_levels).c_str());
Josh Gao62aaf8f2016-06-02 14:27:21 -0700455 fprintf(stderr, " -r ARCH\tbuild with specified architecture (can be repeated)\n");
456 fprintf(stderr, " \t\tvalid architectures are %s\n", Join(supported_archs).c_str());
457 fprintf(stderr, "\n");
458 fprintf(stderr, "Validation:\n");
459 fprintf(stderr, " -p PATH\tcompare against NDK platform at PATH\n");
Josh Gaoacc3d802016-11-09 18:22:44 -0800460 fprintf(stderr, " -s\t\tenable strict warnings\n");
Josh Gao62aaf8f2016-06-02 14:27:21 -0700461 fprintf(stderr, "\n");
Josh Gaof8592a32016-07-26 18:58:27 -0700462 fprintf(stderr, "Preprocessing:\n");
463 fprintf(stderr, " -o PATH\tpreprocess header files and emit them at PATH\n");
Josh Gaod744a9b2017-04-03 11:24:48 -0700464 fprintf(stderr, " -f\t\tpreprocess header files even if validation fails\n");
Josh Gaof8592a32016-07-26 18:58:27 -0700465 fprintf(stderr, "\n");
Josh Gao62aaf8f2016-06-02 14:27:21 -0700466 fprintf(stderr, "Miscellaneous:\n");
George Burgess IVb97049c2017-07-24 15:05:05 -0700467 fprintf(stderr, " -F\t\tdo not ignore FORTIFY headers by default\n");
Josh Gaobfb6bae2016-07-15 17:25:21 -0700468 fprintf(stderr, " -d\t\tdump function availability\n");
Josh Gao16016df2016-11-07 18:27:16 -0800469 fprintf(stderr, " -j THREADS\tmaximum number of threads to use\n");
Josh Gaoacc3d802016-11-09 18:22:44 -0800470 fprintf(stderr, " -v\t\tenable verbose logging\n");
Josh Gao62aaf8f2016-06-02 14:27:21 -0700471 fprintf(stderr, " -h\t\tdisplay this message\n");
472 exit(0);
473 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700474}
475
Andreas Gamped10d3ee2017-04-28 19:32:13 -0700476// versioner uses a prebuilt version of clang, which is not up-to-date wrt/
477// container annotations. So disable container overflow checking. b/37775238
478extern "C" const char* __asan_default_options() {
479 return "detect_container_overflow=0";
480}
481
Josh Gaobf8a2852016-05-27 11:59:09 -0700482int main(int argc, char** argv) {
483 std::string cwd = getWorkingDir() + "/";
484 bool default_args = true;
485 std::string platform_dir;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700486 std::set<Arch> selected_architectures;
Josh Gaobf8a2852016-05-27 11:59:09 -0700487 std::set<int> selected_levels;
Josh Gaof8592a32016-07-26 18:58:27 -0700488 std::string preprocessor_output_path;
489 bool force = false;
Josh Gao16016df2016-11-07 18:27:16 -0800490 bool dump = false;
George Burgess IVb97049c2017-07-24 15:05:05 -0700491 bool ignore_fortify_headers = true;
Josh Gaobf8a2852016-05-27 11:59:09 -0700492
493 int c;
George Burgess IVb97049c2017-07-24 15:05:05 -0700494 while ((c = getopt(argc, argv, "a:r:p:so:fdj:vhFi")) != -1) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700495 default_args = false;
496 switch (c) {
497 case 'a': {
498 char* end;
499 int api_level = strtol(optarg, &end, 10);
500 if (end == optarg || strlen(end) > 0) {
501 usage();
502 }
503
Josh Gaobf8a2852016-05-27 11:59:09 -0700504 selected_levels.insert(api_level);
505 break;
506 }
507
508 case 'r': {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700509 Arch arch = arch_from_string(optarg);
510 selected_architectures.insert(arch);
Josh Gaobf8a2852016-05-27 11:59:09 -0700511 break;
512 }
513
514 case 'p': {
515 if (!platform_dir.empty()) {
516 usage();
517 }
518
519 platform_dir = optarg;
520
Josh Gaof8592a32016-07-26 18:58:27 -0700521 if (platform_dir.empty()) {
522 usage();
523 }
524
Josh Gaobf8a2852016-05-27 11:59:09 -0700525 struct stat st;
526 if (stat(platform_dir.c_str(), &st) != 0) {
527 err(1, "failed to stat platform directory '%s'", platform_dir.c_str());
528 }
529 if (!S_ISDIR(st.st_mode)) {
530 errx(1, "'%s' is not a directory", optarg);
531 }
532 break;
533 }
534
Josh Gaoacc3d802016-11-09 18:22:44 -0800535 case 's':
536 strict = true;
Josh Gaobf8a2852016-05-27 11:59:09 -0700537 break;
538
Josh Gaof8592a32016-07-26 18:58:27 -0700539 case 'o':
540 if (!preprocessor_output_path.empty()) {
541 usage();
542 }
543 preprocessor_output_path = optarg;
544 if (preprocessor_output_path.empty()) {
545 usage();
546 }
547 break;
548
549 case 'f':
550 force = true;
551 break;
552
Josh Gaobfb6bae2016-07-15 17:25:21 -0700553 case 'd':
554 dump = true;
555 break;
556
Josh Gao16016df2016-11-07 18:27:16 -0800557 case 'j':
558 if (!android::base::ParseInt<int>(optarg, &max_thread_count, 1)) {
559 usage();
560 }
561 break;
562
Josh Gaoacc3d802016-11-09 18:22:44 -0800563 case 'v':
564 verbose = true;
565 break;
566
Josh Gao62aaf8f2016-06-02 14:27:21 -0700567 case 'h':
568 usage(true);
569 break;
570
Josh Gaobfb6bae2016-07-15 17:25:21 -0700571 case 'i':
572 // Secret option for tests to -include <android/versioning.h>.
573 add_include = true;
574 break;
575
George Burgess IVb97049c2017-07-24 15:05:05 -0700576 case 'F':
577 ignore_fortify_headers = false;
578 break;
579
Josh Gaobf8a2852016-05-27 11:59:09 -0700580 default:
581 usage();
582 break;
583 }
584 }
585
Josh Gao9b5af7a2016-06-02 14:29:13 -0700586 if (argc - optind > 2 || optind > argc) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700587 usage();
588 }
589
George Burgess IVb97049c2017-07-24 15:05:05 -0700590 HeaderLocationInformation location;
Josh Gao9b5af7a2016-06-02 14:29:13 -0700591
Josh Gaobfb6bae2016-07-15 17:25:21 -0700592 const char* top = getenv("ANDROID_BUILD_TOP");
593 if (!top && (optind == argc || add_include)) {
594 fprintf(stderr, "versioner: failed to autodetect bionic paths. Is ANDROID_BUILD_TOP set?\n");
595 usage();
596 }
597
Josh Gao9b5af7a2016-06-02 14:29:13 -0700598 if (optind == argc) {
599 // Neither HEADER_PATH nor DEPS_PATH were specified, so try to figure them out.
Josh Gaobfb6bae2016-07-15 17:25:21 -0700600 std::string versioner_dir = to_string(top) + "/bionic/tools/versioner";
George Burgess IVb97049c2017-07-24 15:05:05 -0700601 location.header_dir = versioner_dir + "/current";
602 location.dependency_dir = versioner_dir + "/dependencies";
Josh Gao9b5af7a2016-06-02 14:29:13 -0700603 if (platform_dir.empty()) {
604 platform_dir = versioner_dir + "/platforms";
605 }
606 } else {
George Burgess IVb97049c2017-07-24 15:05:05 -0700607 if (!android::base::Realpath(argv[optind], &location.header_dir)) {
Josh Gaob50b8c82017-04-27 17:22:52 -0700608 err(1, "failed to get realpath for path '%s'", argv[optind]);
609 }
Josh Gao9b5af7a2016-06-02 14:29:13 -0700610
611 if (argc - optind == 2) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700612 location.dependency_dir = argv[optind + 1];
Josh Gao9b5af7a2016-06-02 14:29:13 -0700613 }
614 }
615
George Burgess IVb97049c2017-07-24 15:05:05 -0700616 // Every file that lives in bits/fortify is logically a part of a header outside of bits/fortify.
617 // This makes the files there impossible to build on their own.
618 if (ignore_fortify_headers) {
619 std::string fortify_path = location.header_dir;
620 if (!android::base::EndsWith(location.header_dir, "/")) {
621 fortify_path += '/';
622 }
623 fortify_path += "bits/fortify";
624 location.ignored_directories.insert(std::move(fortify_path));
625 }
626
Josh Gaobf8a2852016-05-27 11:59:09 -0700627 if (selected_levels.empty()) {
Josh Gao35aa2132017-10-24 17:05:49 -0700628 selected_levels = default_levels;
Josh Gaobf8a2852016-05-27 11:59:09 -0700629 }
630
631 if (selected_architectures.empty()) {
632 selected_architectures = supported_archs;
633 }
634
Josh Gaobf8a2852016-05-27 11:59:09 -0700635
636 struct stat st;
George Burgess IVb97049c2017-07-24 15:05:05 -0700637 if (const char *dir = location.header_dir.c_str(); stat(dir, &st) != 0) {
638 err(1, "failed to stat '%s'", dir);
Josh Gaobf8a2852016-05-27 11:59:09 -0700639 } else if (!S_ISDIR(st.st_mode)) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700640 errx(1, "'%s' is not a directory", dir);
Josh Gaobf8a2852016-05-27 11:59:09 -0700641 }
642
643 std::set<CompilationType> compilation_types;
Josh Gaobf8a2852016-05-27 11:59:09 -0700644 NdkSymbolDatabase symbol_database;
645
646 compilation_types = generateCompilationTypes(selected_architectures, selected_levels);
647
648 // Do this before compiling so that we can early exit if the platforms don't match what we
649 // expect.
650 if (!platform_dir.empty()) {
651 symbol_database = parsePlatforms(compilation_types, platform_dir);
652 }
653
Josh Gao338cf122016-11-09 18:01:41 -0800654 auto start = std::chrono::high_resolution_clock::now();
Josh Gaobfb6bae2016-07-15 17:25:21 -0700655 std::unique_ptr<HeaderDatabase> declaration_database =
George Burgess IVb97049c2017-07-24 15:05:05 -0700656 compileHeaders(compilation_types, location);
Josh Gao338cf122016-11-09 18:01:41 -0800657 auto end = std::chrono::high_resolution_clock::now();
658
659 if (verbose) {
660 auto diff = (end - start) / 1.0ms;
661 printf("Compiled headers for %zu targets in %0.2LFms\n", compilation_types.size(), diff);
662 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700663
Josh Gao16016df2016-11-07 18:27:16 -0800664 bool failed = false;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700665 if (dump) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700666 declaration_database->dump(location.header_dir + "/");
Josh Gaobfb6bae2016-07-15 17:25:21 -0700667 } else {
668 if (!sanityCheck(declaration_database.get())) {
669 printf("versioner: sanity check failed\n");
Josh Gaobf8a2852016-05-27 11:59:09 -0700670 failed = true;
671 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700672
Josh Gaobfb6bae2016-07-15 17:25:21 -0700673 if (!platform_dir.empty()) {
674 if (!checkVersions(compilation_types, declaration_database.get(), symbol_database)) {
675 printf("versioner: version check failed\n");
676 failed = true;
677 }
678 }
679 }
Josh Gaof8592a32016-07-26 18:58:27 -0700680
681 if (!preprocessor_output_path.empty() && (force || !failed)) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700682 failed = !preprocessHeaders(preprocessor_output_path, location.header_dir, declaration_database.get());
Josh Gaof8592a32016-07-26 18:58:27 -0700683 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700684 return failed;
685}