blob: 473f1f920433910cf241b38e899cfc45ebfb3113 [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>
Josh Gaoab25d0b2017-08-10 10:50:33 -070039#include <string_view>
Josh Gaobf8a2852016-05-27 11:59:09 -070040#include <thread>
41#include <unordered_map>
42#include <vector>
43
Josh Gaobf8a2852016-05-27 11:59:09 -070044#include <llvm/ADT/StringRef.h>
45
Josh Gaob50b8c82017-04-27 17:22:52 -070046#include <android-base/file.h>
Josh Gao338cf122016-11-09 18:01:41 -080047#include <android-base/macros.h>
Josh Gao16016df2016-11-07 18:27:16 -080048#include <android-base/parseint.h>
George Burgess IVb97049c2017-07-24 15:05:05 -070049#include <android-base/strings.h>
Josh Gao16016df2016-11-07 18:27:16 -080050
Josh Gaobfb6bae2016-07-15 17:25:21 -070051#include "Arch.h"
Josh Gaobf8a2852016-05-27 11:59:09 -070052#include "DeclarationDatabase.h"
Josh Gaob5c49632016-11-08 22:21:31 -080053#include "Driver.h"
Josh Gaof8592a32016-07-26 18:58:27 -070054#include "Preprocessor.h"
Josh Gaobf8a2852016-05-27 11:59:09 -070055#include "SymbolDatabase.h"
56#include "Utils.h"
Josh Gao78b8a142016-11-09 01:00:41 -080057#include "VFS.h"
58
Josh Gaobf8a2852016-05-27 11:59:09 -070059#include "versioner.h"
60
Josh Gao338cf122016-11-09 18:01:41 -080061using namespace std::chrono_literals;
Josh Gaobf8a2852016-05-27 11:59:09 -070062using namespace std::string_literals;
Josh Gaobf8a2852016-05-27 11:59:09 -070063
Josh Gaoacc3d802016-11-09 18:22:44 -080064bool strict;
Josh Gaobf8a2852016-05-27 11:59:09 -070065bool verbose;
Josh Gaoacc3d802016-11-09 18:22:44 -080066bool add_include;
Josh Gao338cf122016-11-09 18:01:41 -080067
68static int getCpuCount();
69static int max_thread_count = getCpuCount();
70
71static int getCpuCount() {
72#if defined(__linux__)
73 cpu_set_t cpu_set;
74 int rc = sched_getaffinity(getpid(), sizeof(cpu_set), &cpu_set);
75 if (rc != 0) {
76 err(1, "sched_getaffinity failed");
77 }
78 return CPU_COUNT(&cpu_set);
79#else
80 return 1;
81#endif
82}
Josh Gaobf8a2852016-05-27 11:59:09 -070083
George Burgess IVb97049c2017-07-24 15:05:05 -070084namespace {
85struct HeaderLocationInformation {
Josh Gaoab25d0b2017-08-10 10:50:33 -070086 std::string header_path;
George Burgess IVb97049c2017-07-24 15:05:05 -070087 std::string dependency_dir;
88 // Absolute paths to ignore all children -- including subdirectories -- of.
89 std::unordered_set<std::string> ignored_directories;
90};
91}
92
Josh Gaoab25d0b2017-08-10 10:50:33 -070093static bool is_dir(const std::string& path) {
94 struct stat st;
95 return stat(path.c_str(), &st) == 0 && S_ISDIR(st.st_mode);
96}
97
George Burgess IVb97049c2017-07-24 15:05:05 -070098static CompilationRequirements collectRequirements(const Arch& arch,
99 const HeaderLocationInformation& location) {
100 std::vector<std::string> headers =
Josh Gaoab25d0b2017-08-10 10:50:33 -0700101 collectHeaders(location.header_path, location.ignored_directories);
102 std::vector<std::string> dependencies;
103
104 if (is_dir(location.header_path)) {
105 dependencies.emplace_back(location.header_path);
106 }
107
George Burgess IVb97049c2017-07-24 15:05:05 -0700108 if (!location.dependency_dir.empty()) {
Yi Kongff6c8de2017-04-20 09:08:11 -0700109 auto collect_children = [&dependencies](const std::string& dir_path) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700110 DIR* dir = opendir(dir_path.c_str());
111 if (!dir) {
112 err(1, "failed to open dependency directory '%s'", dir_path.c_str());
113 }
114
115 struct dirent* dent;
116 while ((dent = readdir(dir))) {
117 if (dent->d_name[0] == '.') {
118 continue;
119 }
120
121 // TODO: Resolve symlinks.
122 std::string dependency = dir_path + "/" + dent->d_name;
123
124 struct stat st;
125 if (stat(dependency.c_str(), &st) != 0) {
126 err(1, "failed to stat dependency '%s'", dependency.c_str());
127 }
128
129 if (!S_ISDIR(st.st_mode)) {
130 errx(1, "'%s' is not a directory", dependency.c_str());
131 }
132
133 dependencies.push_back(dependency);
134 }
135
136 closedir(dir);
137 };
138
George Burgess IVb97049c2017-07-24 15:05:05 -0700139 collect_children(location.dependency_dir + "/common");
140 collect_children(location.dependency_dir + "/" + to_string(arch));
Josh Gaobf8a2852016-05-27 11:59:09 -0700141 }
142
143 auto new_end = std::remove_if(headers.begin(), headers.end(), [&arch](llvm::StringRef header) {
144 for (const auto& it : header_blacklist) {
145 if (it.second.find(arch) == it.second.end()) {
146 continue;
147 }
148
149 if (header.endswith("/" + it.first)) {
150 return true;
151 }
152 }
153 return false;
154 });
155
156 headers.erase(new_end, headers.end());
157
158 CompilationRequirements result = { .headers = headers, .dependencies = dependencies };
159 return result;
160}
161
Josh Gaobfb6bae2016-07-15 17:25:21 -0700162static std::set<CompilationType> generateCompilationTypes(const std::set<Arch> selected_architectures,
163 const std::set<int>& selected_levels) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700164 std::set<CompilationType> result;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700165 for (const auto& arch : selected_architectures) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700166 int min_api = arch_min_api[arch];
167 for (int api_level : selected_levels) {
168 if (api_level < min_api) {
169 continue;
170 }
Josh Gaoa77b3a92016-08-15 16:39:27 -0700171
172 for (int file_offset_bits : { 32, 64 }) {
Josh Gaoab25d0b2017-08-10 10:50:33 -0700173 for (bool cpp : { true, false }) {
174 CompilationType type = {
175 .arch = arch, .cpp = cpp, .api_level = api_level, .file_offset_bits = file_offset_bits
176 };
177 result.insert(type);
178 }
Josh Gaoa77b3a92016-08-15 16:39:27 -0700179 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700180 }
181 }
182 return result;
183}
184
Josh Gaobfb6bae2016-07-15 17:25:21 -0700185static std::unique_ptr<HeaderDatabase> compileHeaders(const std::set<CompilationType>& types,
George Burgess IVb97049c2017-07-24 15:05:05 -0700186 const HeaderLocationInformation& location) {
Josh Gao16016df2016-11-07 18:27:16 -0800187 if (types.empty()) {
188 errx(1, "compileHeaders received no CompilationTypes");
189 }
190
Josh Gaoab25d0b2017-08-10 10:50:33 -0700191 auto vfs = createCommonVFS(location.header_path, location.dependency_dir, add_include);
Josh Gao78b8a142016-11-09 01:00:41 -0800192
Josh Gao16016df2016-11-07 18:27:16 -0800193 size_t thread_count = max_thread_count;
194 std::vector<std::thread> threads;
Josh Gaobf8a2852016-05-27 11:59:09 -0700195
196 std::map<CompilationType, HeaderDatabase> header_databases;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700197 std::unordered_map<Arch, CompilationRequirements> requirements;
Josh Gaobf8a2852016-05-27 11:59:09 -0700198
Josh Gao16016df2016-11-07 18:27:16 -0800199 auto result = std::make_unique<HeaderDatabase>();
Josh Gaobf8a2852016-05-27 11:59:09 -0700200 for (const auto& type : types) {
201 if (requirements.count(type.arch) == 0) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700202 requirements[type.arch] = collectRequirements(type.arch, location);
Josh Gaobf8a2852016-05-27 11:59:09 -0700203 }
204 }
205
Josh Gao78b8a142016-11-09 01:00:41 -0800206 initializeTargetCC1FlagCache(vfs, types, requirements);
Josh Gaob5c49632016-11-08 22:21:31 -0800207
208 std::vector<std::pair<CompilationType, const std::string&>> jobs;
Josh Gao338cf122016-11-09 18:01:41 -0800209 std::atomic<size_t> job_index(0);
Josh Gao16016df2016-11-07 18:27:16 -0800210 for (CompilationType type : types) {
211 CompilationRequirements& req = requirements[type.arch];
212 for (const std::string& header : req.headers) {
Josh Gaob5c49632016-11-08 22:21:31 -0800213 jobs.emplace_back(type, header);
Josh Gaobf8a2852016-05-27 11:59:09 -0700214 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700215 }
216
Josh Gaod2ab9ff2017-07-28 12:53:36 -0700217 // Dup an empty file to stdin, so that we can use `clang -include a.h -` instead of `clang a.h`,
218 // since some warnings don't get generated in files that are compiled directly.
219 FILE* empty_file = tmpfile();
220 if (!empty_file) {
221 err(1, "failed to create temporary file");
222 }
223
224 int empty_file_fd = fileno(empty_file);
225 if (empty_file_fd == -1) {
226 errx(1, "fileno failed on tmpfile");
227 }
228
229 dup2(empty_file_fd, STDIN_FILENO);
230 fclose(empty_file);
231
Josh Gaob5c49632016-11-08 22:21:31 -0800232 thread_count = std::min(thread_count, jobs.size());
233
234 if (thread_count == 1) {
235 for (const auto& job : jobs) {
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 } else {
239 // Spawn threads.
240 for (size_t i = 0; i < thread_count; ++i) {
Yi Kongff6c8de2017-04-20 09:08:11 -0700241 threads.emplace_back([&jobs, &job_index, &result, vfs]() {
Josh Gao338cf122016-11-09 18:01:41 -0800242 while (true) {
243 size_t idx = job_index++;
244 if (idx >= jobs.size()) {
245 return;
246 }
247
248 const auto& job = jobs[idx];
Josh Gao78b8a142016-11-09 01:00:41 -0800249 compileHeader(vfs, result.get(), job.first, job.second);
Josh Gaob5c49632016-11-08 22:21:31 -0800250 }
251 });
Josh Gaobf8a2852016-05-27 11:59:09 -0700252 }
253
Josh Gaob5c49632016-11-08 22:21:31 -0800254 // Reap them.
255 for (auto& thread : threads) {
256 thread.join();
257 }
258 threads.clear();
Josh Gaobf8a2852016-05-27 11:59:09 -0700259 }
260
Josh Gaobfb6bae2016-07-15 17:25:21 -0700261 return result;
Josh Gaobf8a2852016-05-27 11:59:09 -0700262}
263
Josh Gao1a176de2016-11-04 13:15:11 -0700264static std::set<CompilationType> getCompilationTypes(const Declaration* decl) {
265 std::set<CompilationType> result;
266 for (const auto& it : decl->availability) {
267 result.insert(it.first);
268 }
269 return result;
270}
271
272template<typename T>
273static std::vector<T> Intersection(const std::set<T>& a, const std::set<T>& b) {
274 std::vector<T> intersection;
275 std::set_intersection(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(intersection));
276 return intersection;
277}
278
Josh Gaobfb6bae2016-07-15 17:25:21 -0700279// Perform a sanity check on a symbol's declarations, enforcing the following invariants:
Logan Chienc88331b2019-12-20 10:27:45 -0800280// 1. At most one inline definition of the function exists (overloaded inline functions for
281// _FORTIFY_SOURCE do not count because they are usually introduced to intercept the original
282// functions or usually have enable_if attributes).
Josh Gaobfb6bae2016-07-15 17:25:21 -0700283// 2. All of the availability declarations for a symbol are compatible.
284// If a function is declared as an inline before a certain version, the inline definition
285// should have no version tag.
286// 3. Each availability type must only be present globally or on a per-arch basis.
287// (e.g. __INTRODUCED_IN_ARM(9) __INTRODUCED_IN_X86(10) __DEPRECATED_IN(11) is fine,
288// but not __INTRODUCED_IN(9) __INTRODUCED_IN_X86(10))
289static bool checkSymbol(const Symbol& symbol) {
290 std::string cwd = getWorkingDir() + "/";
291
Josh Gao1a176de2016-11-04 13:15:11 -0700292 std::unordered_map<const Declaration*, std::set<CompilationType>> inline_definitions;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700293 for (const auto& decl_it : symbol.declarations) {
294 const Declaration* decl = &decl_it.second;
Logan Chienc88331b2019-12-20 10:27:45 -0800295 if (decl->is_definition && !decl->fortify_inline) {
Josh Gao1a176de2016-11-04 13:15:11 -0700296 std::set<CompilationType> compilation_types = getCompilationTypes(decl);
297 for (const auto& inline_def_it : inline_definitions) {
298 auto intersection = Intersection(compilation_types, inline_def_it.second);
299 if (!intersection.empty()) {
Josh Gaoab25d0b2017-08-10 10:50:33 -0700300 fprintf(stderr, "versioner: conflicting inline definitions for symbol %s:\n",
301 symbol.name.c_str());
Josh Gao1a176de2016-11-04 13:15:11 -0700302 fprintf(stderr, " declarations visible in: %s\n", Join(intersection, ", ").c_str());
303 decl->dump(cwd, stderr, 4);
304 inline_def_it.first->dump(cwd, stderr, 4);
305 return false;
306 }
Josh Gaobfb6bae2016-07-15 17:25:21 -0700307 }
308
Josh Gao1a176de2016-11-04 13:15:11 -0700309 inline_definitions[decl] = std::move(compilation_types);
Josh Gaobfb6bae2016-07-15 17:25:21 -0700310 }
311
312 DeclarationAvailability availability;
313 if (!decl->calculateAvailability(&availability)) {
314 fprintf(stderr, "versioner: failed to calculate availability for declaration:\n");
Josh Gao566735d2016-08-02 15:07:32 -0700315 decl->dump(cwd, stderr, 2);
Josh Gaobfb6bae2016-07-15 17:25:21 -0700316 return false;
317 }
318
319 if (decl->is_definition && !availability.empty()) {
320 fprintf(stderr, "versioner: inline definition has non-empty versioning information:\n");
Josh Gao566735d2016-08-02 15:07:32 -0700321 decl->dump(cwd, stderr, 2);
Josh Gaobfb6bae2016-07-15 17:25:21 -0700322 return false;
323 }
324 }
325
326 DeclarationAvailability availability;
327 if (!symbol.calculateAvailability(&availability)) {
328 fprintf(stderr, "versioner: inconsistent availability for symbol '%s'\n", symbol.name.c_str());
329 symbol.dump(cwd);
330 return false;
331 }
332
333 // TODO: Check invariant #3.
334 return true;
335}
336
337static bool sanityCheck(const HeaderDatabase* database) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700338 bool error = false;
Josh Gao958f3b32016-06-03 13:44:00 -0700339 std::string cwd = getWorkingDir() + "/";
340
Josh Gaobfb6bae2016-07-15 17:25:21 -0700341 for (const auto& symbol_it : database->symbols) {
342 if (!checkSymbol(symbol_it.second)) {
343 error = true;
Josh Gaobf8a2852016-05-27 11:59:09 -0700344 }
345 }
346 return !error;
347}
348
349// Check that our symbol availability declarations match the actual NDK
350// platform symbol availability.
351static bool checkVersions(const std::set<CompilationType>& types,
Josh Gaobfb6bae2016-07-15 17:25:21 -0700352 const HeaderDatabase* header_database,
Josh Gaobf8a2852016-05-27 11:59:09 -0700353 const NdkSymbolDatabase& symbol_database) {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700354 std::string cwd = getWorkingDir() + "/";
Josh Gaobf8a2852016-05-27 11:59:09 -0700355 bool failed = false;
356
Josh Gaobfb6bae2016-07-15 17:25:21 -0700357 std::map<Arch, std::set<CompilationType>> arch_types;
Josh Gaobf8a2852016-05-27 11:59:09 -0700358 for (const CompilationType& type : types) {
359 arch_types[type.arch].insert(type);
360 }
361
Josh Gaod67dbf02016-06-02 15:21:14 -0700362 std::set<std::string> completely_unavailable;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700363 std::map<std::string, std::set<CompilationType>> missing_availability;
364 std::map<std::string, std::set<CompilationType>> extra_availability;
Josh Gaod67dbf02016-06-02 15:21:14 -0700365
Josh Gaobfb6bae2016-07-15 17:25:21 -0700366 for (const auto& symbol_it : header_database->symbols) {
367 const auto& symbol_name = symbol_it.first;
368 DeclarationAvailability symbol_availability;
Josh Gaobf8a2852016-05-27 11:59:09 -0700369
Josh Gaobfb6bae2016-07-15 17:25:21 -0700370 if (!symbol_it.second.calculateAvailability(&symbol_availability)) {
371 errx(1, "failed to calculate symbol availability");
372 }
373
374 const auto platform_availability_it = symbol_database.find(symbol_name);
Josh Gaobf8a2852016-05-27 11:59:09 -0700375 if (platform_availability_it == symbol_database.end()) {
Josh Gaod67dbf02016-06-02 15:21:14 -0700376 completely_unavailable.insert(symbol_name);
Josh Gaobf8a2852016-05-27 11:59:09 -0700377 continue;
378 }
379
380 const auto& platform_availability = platform_availability_it->second;
Josh Gaobf8a2852016-05-27 11:59:09 -0700381
382 for (const CompilationType& type : types) {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700383 bool should_be_available = true;
384 const auto& global_availability = symbol_availability.global_availability;
385 const auto& arch_availability = symbol_availability.arch_availability[type.arch];
386 if (global_availability.introduced != 0 && global_availability.introduced > type.api_level) {
387 should_be_available = false;
388 }
389
390 if (arch_availability.introduced != 0 && arch_availability.introduced > type.api_level) {
391 should_be_available = false;
392 }
393
394 if (global_availability.obsoleted != 0 && global_availability.obsoleted <= type.api_level) {
395 should_be_available = false;
396 }
397
398 if (arch_availability.obsoleted != 0 && arch_availability.obsoleted <= type.api_level) {
399 should_be_available = false;
400 }
401
Josh Gaobfb6bae2016-07-15 17:25:21 -0700402 // The function declaration might be (validly) missing for the given CompilationType.
403 if (!symbol_it.second.hasDeclaration(type)) {
404 should_be_available = false;
405 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700406
Josh Gaobfb6bae2016-07-15 17:25:21 -0700407 bool is_available = platform_availability.count(type);
Josh Gaobf8a2852016-05-27 11:59:09 -0700408
Josh Gaobfb6bae2016-07-15 17:25:21 -0700409 if (should_be_available != is_available) {
410 if (is_available) {
411 extra_availability[symbol_name].insert(type);
Josh Gaobf8a2852016-05-27 11:59:09 -0700412 } else {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700413 missing_availability[symbol_name].insert(type);
Josh Gaobf8a2852016-05-27 11:59:09 -0700414 }
415 }
416 }
Josh Gaobfb6bae2016-07-15 17:25:21 -0700417 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700418
Josh Gaobfb6bae2016-07-15 17:25:21 -0700419 for (const auto& it : symbol_database) {
420 const std::string& symbol_name = it.first;
Josh Gaobf8a2852016-05-27 11:59:09 -0700421
Josh Gaobfb6bae2016-07-15 17:25:21 -0700422 bool symbol_error = false;
Josh Gao0a284f52016-12-15 13:56:00 -0800423 if (auto missing_it = missing_availability.find(symbol_name);
424 missing_it != missing_availability.end()) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700425 printf("%s: declaration marked available but symbol missing in [%s]\n", symbol_name.c_str(),
Josh Gaobfb6bae2016-07-15 17:25:21 -0700426 Join(missing_it->second, ", ").c_str());
427 symbol_error = true;
Josh Gaobf8a2852016-05-27 11:59:09 -0700428 failed = true;
429 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700430
Josh Gaoacc3d802016-11-09 18:22:44 -0800431 if (strict) {
Josh Gao0a284f52016-12-15 13:56:00 -0800432 if (auto extra_it = extra_availability.find(symbol_name);
433 extra_it != extra_availability.end()) {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700434 printf("%s: declaration marked unavailable but symbol available in [%s]\n",
435 symbol_name.c_str(), Join(extra_it->second, ", ").c_str());
436 symbol_error = true;
437 failed = true;
Josh Gao958f3b32016-06-03 13:44:00 -0700438 }
439 }
440
Josh Gaobfb6bae2016-07-15 17:25:21 -0700441 if (symbol_error) {
Josh Gao0a284f52016-12-15 13:56:00 -0800442 if (auto symbol_it = header_database->symbols.find(symbol_name);
443 symbol_it != header_database->symbols.end()) {
444 symbol_it->second.dump(cwd);
445 } else {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700446 errx(1, "failed to find symbol in header database");
447 }
Josh Gaod67dbf02016-06-02 15:21:14 -0700448 }
Josh Gaod67dbf02016-06-02 15:21:14 -0700449 }
450
Josh Gaobfb6bae2016-07-15 17:25:21 -0700451 // TODO: Verify that function/variable declarations are actually function/variable symbols.
Josh Gaobf8a2852016-05-27 11:59:09 -0700452 return !failed;
453}
454
Josh Gao62aaf8f2016-06-02 14:27:21 -0700455static void usage(bool help = false) {
456 fprintf(stderr, "Usage: versioner [OPTION]... [HEADER_PATH] [DEPS_PATH]\n");
457 if (!help) {
458 printf("Try 'versioner -h' for more information.\n");
459 exit(1);
460 } else {
461 fprintf(stderr, "Version headers at HEADER_PATH, with DEPS_PATH/ARCH/* on the include path\n");
Josh Gao9b5af7a2016-06-02 14:29:13 -0700462 fprintf(stderr, "Autodetects paths if HEADER_PATH and DEPS_PATH are not specified\n");
Josh Gao62aaf8f2016-06-02 14:27:21 -0700463 fprintf(stderr, "\n");
464 fprintf(stderr, "Target specification (defaults to all):\n");
465 fprintf(stderr, " -a API_LEVEL\tbuild with specified API level (can be repeated)\n");
Josh Gao35aa2132017-10-24 17:05:49 -0700466 fprintf(stderr, " \t\tdefaults to %s\n", Join(default_levels).c_str());
Josh Gao62aaf8f2016-06-02 14:27:21 -0700467 fprintf(stderr, " -r ARCH\tbuild with specified architecture (can be repeated)\n");
468 fprintf(stderr, " \t\tvalid architectures are %s\n", Join(supported_archs).c_str());
469 fprintf(stderr, "\n");
470 fprintf(stderr, "Validation:\n");
471 fprintf(stderr, " -p PATH\tcompare against NDK platform at PATH\n");
Josh Gaoacc3d802016-11-09 18:22:44 -0800472 fprintf(stderr, " -s\t\tenable strict warnings\n");
Josh Gao62aaf8f2016-06-02 14:27:21 -0700473 fprintf(stderr, "\n");
Josh Gaof8592a32016-07-26 18:58:27 -0700474 fprintf(stderr, "Preprocessing:\n");
475 fprintf(stderr, " -o PATH\tpreprocess header files and emit them at PATH\n");
Josh Gaod744a9b2017-04-03 11:24:48 -0700476 fprintf(stderr, " -f\t\tpreprocess header files even if validation fails\n");
Josh Gaof8592a32016-07-26 18:58:27 -0700477 fprintf(stderr, "\n");
Josh Gao62aaf8f2016-06-02 14:27:21 -0700478 fprintf(stderr, "Miscellaneous:\n");
George Burgess IVb97049c2017-07-24 15:05:05 -0700479 fprintf(stderr, " -F\t\tdo not ignore FORTIFY headers by default\n");
Josh Gaobfb6bae2016-07-15 17:25:21 -0700480 fprintf(stderr, " -d\t\tdump function availability\n");
Josh Gao16016df2016-11-07 18:27:16 -0800481 fprintf(stderr, " -j THREADS\tmaximum number of threads to use\n");
Josh Gaoacc3d802016-11-09 18:22:44 -0800482 fprintf(stderr, " -v\t\tenable verbose logging\n");
Josh Gao62aaf8f2016-06-02 14:27:21 -0700483 fprintf(stderr, " -h\t\tdisplay this message\n");
484 exit(0);
485 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700486}
487
Andreas Gamped10d3ee2017-04-28 19:32:13 -0700488// versioner uses a prebuilt version of clang, which is not up-to-date wrt/
489// container annotations. So disable container overflow checking. b/37775238
490extern "C" const char* __asan_default_options() {
491 return "detect_container_overflow=0";
492}
493
Josh Gaobf8a2852016-05-27 11:59:09 -0700494int main(int argc, char** argv) {
495 std::string cwd = getWorkingDir() + "/";
Josh Gaobf8a2852016-05-27 11:59:09 -0700496 std::string platform_dir;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700497 std::set<Arch> selected_architectures;
Josh Gaobf8a2852016-05-27 11:59:09 -0700498 std::set<int> selected_levels;
Josh Gaof8592a32016-07-26 18:58:27 -0700499 std::string preprocessor_output_path;
500 bool force = false;
Josh Gao16016df2016-11-07 18:27:16 -0800501 bool dump = false;
George Burgess IVb97049c2017-07-24 15:05:05 -0700502 bool ignore_fortify_headers = true;
Josh Gaobf8a2852016-05-27 11:59:09 -0700503
504 int c;
George Burgess IVb97049c2017-07-24 15:05:05 -0700505 while ((c = getopt(argc, argv, "a:r:p:so:fdj:vhFi")) != -1) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700506 switch (c) {
507 case 'a': {
508 char* end;
509 int api_level = strtol(optarg, &end, 10);
510 if (end == optarg || strlen(end) > 0) {
511 usage();
512 }
513
Josh Gaobf8a2852016-05-27 11:59:09 -0700514 selected_levels.insert(api_level);
515 break;
516 }
517
518 case 'r': {
Logan Chien9c123232018-10-23 10:47:17 +0800519 std::optional<Arch> arch = arch_from_string(optarg);
520 if (!arch) {
521 errx(1, "unknown architecture '%s'", optarg);
522 }
523 selected_architectures.insert(*arch);
Josh Gaobf8a2852016-05-27 11:59:09 -0700524 break;
525 }
526
527 case 'p': {
528 if (!platform_dir.empty()) {
529 usage();
530 }
531
532 platform_dir = optarg;
533
Josh Gaof8592a32016-07-26 18:58:27 -0700534 if (platform_dir.empty()) {
535 usage();
536 }
537
Josh Gaobf8a2852016-05-27 11:59:09 -0700538 struct stat st;
539 if (stat(platform_dir.c_str(), &st) != 0) {
540 err(1, "failed to stat platform directory '%s'", platform_dir.c_str());
541 }
Josh Gaoab25d0b2017-08-10 10:50:33 -0700542 if (!S_ISDIR(st.st_mode) && !S_ISREG(st.st_mode)) {
543 errx(1, "'%s' is not a file or directory", optarg);
Josh Gaobf8a2852016-05-27 11:59:09 -0700544 }
545 break;
546 }
547
Josh Gaoacc3d802016-11-09 18:22:44 -0800548 case 's':
549 strict = true;
Josh Gaobf8a2852016-05-27 11:59:09 -0700550 break;
551
Josh Gaof8592a32016-07-26 18:58:27 -0700552 case 'o':
553 if (!preprocessor_output_path.empty()) {
554 usage();
555 }
556 preprocessor_output_path = optarg;
557 if (preprocessor_output_path.empty()) {
558 usage();
559 }
560 break;
561
562 case 'f':
563 force = true;
564 break;
565
Josh Gaobfb6bae2016-07-15 17:25:21 -0700566 case 'd':
567 dump = true;
568 break;
569
Josh Gao16016df2016-11-07 18:27:16 -0800570 case 'j':
571 if (!android::base::ParseInt<int>(optarg, &max_thread_count, 1)) {
572 usage();
573 }
574 break;
575
Josh Gaoacc3d802016-11-09 18:22:44 -0800576 case 'v':
577 verbose = true;
578 break;
579
Josh Gao62aaf8f2016-06-02 14:27:21 -0700580 case 'h':
581 usage(true);
582 break;
583
Josh Gaobfb6bae2016-07-15 17:25:21 -0700584 case 'i':
585 // Secret option for tests to -include <android/versioning.h>.
586 add_include = true;
587 break;
588
George Burgess IVb97049c2017-07-24 15:05:05 -0700589 case 'F':
590 ignore_fortify_headers = false;
591 break;
592
Josh Gaobf8a2852016-05-27 11:59:09 -0700593 default:
594 usage();
595 break;
596 }
597 }
598
Josh Gao9b5af7a2016-06-02 14:29:13 -0700599 if (argc - optind > 2 || optind > argc) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700600 usage();
601 }
602
George Burgess IVb97049c2017-07-24 15:05:05 -0700603 HeaderLocationInformation location;
Josh Gao9b5af7a2016-06-02 14:29:13 -0700604
Josh Gaobfb6bae2016-07-15 17:25:21 -0700605 const char* top = getenv("ANDROID_BUILD_TOP");
606 if (!top && (optind == argc || add_include)) {
607 fprintf(stderr, "versioner: failed to autodetect bionic paths. Is ANDROID_BUILD_TOP set?\n");
608 usage();
609 }
610
Josh Gao9b5af7a2016-06-02 14:29:13 -0700611 if (optind == argc) {
612 // Neither HEADER_PATH nor DEPS_PATH were specified, so try to figure them out.
Josh Gaobfb6bae2016-07-15 17:25:21 -0700613 std::string versioner_dir = to_string(top) + "/bionic/tools/versioner";
Josh Gaoab25d0b2017-08-10 10:50:33 -0700614 location.header_path = versioner_dir + "/current";
George Burgess IVb97049c2017-07-24 15:05:05 -0700615 location.dependency_dir = versioner_dir + "/dependencies";
Logan Chien9c123232018-10-23 10:47:17 +0800616 if (platform_dir.empty()) {
617 platform_dir = versioner_dir + "/platforms";
618 }
Josh Gao9b5af7a2016-06-02 14:29:13 -0700619 } else {
Josh Gaoab25d0b2017-08-10 10:50:33 -0700620 if (!android::base::Realpath(argv[optind], &location.header_path)) {
Josh Gaob50b8c82017-04-27 17:22:52 -0700621 err(1, "failed to get realpath for path '%s'", argv[optind]);
622 }
Josh Gao9b5af7a2016-06-02 14:29:13 -0700623
624 if (argc - optind == 2) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700625 location.dependency_dir = argv[optind + 1];
Josh Gao9b5af7a2016-06-02 14:29:13 -0700626 }
627 }
628
George Burgess IVb97049c2017-07-24 15:05:05 -0700629 // Every file that lives in bits/fortify is logically a part of a header outside of bits/fortify.
630 // This makes the files there impossible to build on their own.
631 if (ignore_fortify_headers) {
Josh Gaoab25d0b2017-08-10 10:50:33 -0700632 std::string fortify_path = location.header_path;
633 if (!android::base::EndsWith(location.header_path, "/")) {
George Burgess IVb97049c2017-07-24 15:05:05 -0700634 fortify_path += '/';
635 }
636 fortify_path += "bits/fortify";
637 location.ignored_directories.insert(std::move(fortify_path));
638 }
639
Josh Gaobf8a2852016-05-27 11:59:09 -0700640 if (selected_levels.empty()) {
Josh Gao35aa2132017-10-24 17:05:49 -0700641 selected_levels = default_levels;
Josh Gaobf8a2852016-05-27 11:59:09 -0700642 }
643
644 if (selected_architectures.empty()) {
645 selected_architectures = supported_archs;
646 }
647
Josh Gaobf8a2852016-05-27 11:59:09 -0700648
649 struct stat st;
Josh Gaoab25d0b2017-08-10 10:50:33 -0700650 if (const char *path = location.header_path.c_str(); stat(path, &st) != 0) {
651 err(1, "failed to stat '%s'", path);
Josh Gaobf8a2852016-05-27 11:59:09 -0700652 }
653
654 std::set<CompilationType> compilation_types;
Josh Gaoc1661132017-12-12 11:51:32 -0800655 std::optional<NdkSymbolDatabase> symbol_database;
Josh Gaobf8a2852016-05-27 11:59:09 -0700656
657 compilation_types = generateCompilationTypes(selected_architectures, selected_levels);
658
Logan Chien9c123232018-10-23 10:47:17 +0800659 // Do this before compiling so that we can early exit if the platforms don't match what we
660 // expect.
661 if (!platform_dir.empty()) {
662 symbol_database = parsePlatforms(compilation_types, platform_dir);
663 }
664
Josh Gao338cf122016-11-09 18:01:41 -0800665 auto start = std::chrono::high_resolution_clock::now();
Josh Gaobfb6bae2016-07-15 17:25:21 -0700666 std::unique_ptr<HeaderDatabase> declaration_database =
George Burgess IVb97049c2017-07-24 15:05:05 -0700667 compileHeaders(compilation_types, location);
Josh Gao338cf122016-11-09 18:01:41 -0800668 auto end = std::chrono::high_resolution_clock::now();
669
670 if (verbose) {
671 auto diff = (end - start) / 1.0ms;
672 printf("Compiled headers for %zu targets in %0.2LFms\n", compilation_types.size(), diff);
673 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700674
Josh Gao16016df2016-11-07 18:27:16 -0800675 bool failed = false;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700676 if (dump) {
Josh Gaoab25d0b2017-08-10 10:50:33 -0700677 declaration_database->dump(location.header_path + "/");
Josh Gaobfb6bae2016-07-15 17:25:21 -0700678 } else {
679 if (!sanityCheck(declaration_database.get())) {
680 printf("versioner: sanity check failed\n");
Josh Gaobf8a2852016-05-27 11:59:09 -0700681 failed = true;
682 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700683
Josh Gaoc1661132017-12-12 11:51:32 -0800684 if (symbol_database) {
685 if (!checkVersions(compilation_types, declaration_database.get(), *symbol_database)) {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700686 printf("versioner: version check failed\n");
687 failed = true;
688 }
689 }
690 }
Josh Gaof8592a32016-07-26 18:58:27 -0700691
692 if (!preprocessor_output_path.empty() && (force || !failed)) {
Josh Gaoab25d0b2017-08-10 10:50:33 -0700693 failed = !preprocessHeaders(preprocessor_output_path, location.header_path,
694 declaration_database.get());
Josh Gaof8592a32016-07-26 18:58:27 -0700695 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700696 return failed;
697}