blob: 86349e174b7e6a99d36ac90ac79cfcfd2d32f0fd [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
25#include <atomic>
Josh Gao16016df2016-11-07 18:27:16 -080026#include <functional>
Josh Gaobf8a2852016-05-27 11:59:09 -070027#include <iostream>
28#include <map>
29#include <memory>
30#include <set>
31#include <sstream>
32#include <string>
33#include <thread>
34#include <unordered_map>
35#include <vector>
36
Josh Gaobf8a2852016-05-27 11:59:09 -070037#include <llvm/ADT/StringRef.h>
38
Josh Gao16016df2016-11-07 18:27:16 -080039#include <android-base/parseint.h>
40
Josh Gaobfb6bae2016-07-15 17:25:21 -070041#include "Arch.h"
Josh Gaobf8a2852016-05-27 11:59:09 -070042#include "DeclarationDatabase.h"
Josh Gaob5c49632016-11-08 22:21:31 -080043#include "Driver.h"
Josh Gaof8592a32016-07-26 18:58:27 -070044#include "Preprocessor.h"
Josh Gaobf8a2852016-05-27 11:59:09 -070045#include "SymbolDatabase.h"
46#include "Utils.h"
Josh Gao78b8a142016-11-09 01:00:41 -080047#include "VFS.h"
48
Josh Gaobf8a2852016-05-27 11:59:09 -070049#include "versioner.h"
50
51using namespace std::string_literals;
Josh Gaobf8a2852016-05-27 11:59:09 -070052
Josh Gaob5c49632016-11-08 22:21:31 -080053bool add_include;
Josh Gaobf8a2852016-05-27 11:59:09 -070054bool verbose;
Josh Gao16016df2016-11-07 18:27:16 -080055static int max_thread_count = 48;
Josh Gaobf8a2852016-05-27 11:59:09 -070056
Josh Gaobfb6bae2016-07-15 17:25:21 -070057static CompilationRequirements collectRequirements(const Arch& arch, const std::string& header_dir,
Josh Gaobf8a2852016-05-27 11:59:09 -070058 const std::string& dependency_dir) {
59 std::vector<std::string> headers = collectFiles(header_dir);
Josh Gaobf8a2852016-05-27 11:59:09 -070060 std::vector<std::string> dependencies = { header_dir };
61 if (!dependency_dir.empty()) {
Josh Gaob5c49632016-11-08 22:21:31 -080062 auto collect_children = [&dependencies, &dependency_dir](const std::string& dir_path) {
Josh Gaobf8a2852016-05-27 11:59:09 -070063 DIR* dir = opendir(dir_path.c_str());
64 if (!dir) {
65 err(1, "failed to open dependency directory '%s'", dir_path.c_str());
66 }
67
68 struct dirent* dent;
69 while ((dent = readdir(dir))) {
70 if (dent->d_name[0] == '.') {
71 continue;
72 }
73
74 // TODO: Resolve symlinks.
75 std::string dependency = dir_path + "/" + dent->d_name;
76
77 struct stat st;
78 if (stat(dependency.c_str(), &st) != 0) {
79 err(1, "failed to stat dependency '%s'", dependency.c_str());
80 }
81
82 if (!S_ISDIR(st.st_mode)) {
83 errx(1, "'%s' is not a directory", dependency.c_str());
84 }
85
86 dependencies.push_back(dependency);
87 }
88
89 closedir(dir);
90 };
91
92 collect_children(dependency_dir + "/common");
Josh Gaobfb6bae2016-07-15 17:25:21 -070093 collect_children(dependency_dir + "/" + to_string(arch));
Josh Gaobf8a2852016-05-27 11:59:09 -070094 }
95
96 auto new_end = std::remove_if(headers.begin(), headers.end(), [&arch](llvm::StringRef header) {
97 for (const auto& it : header_blacklist) {
98 if (it.second.find(arch) == it.second.end()) {
99 continue;
100 }
101
102 if (header.endswith("/" + it.first)) {
103 return true;
104 }
105 }
106 return false;
107 });
108
109 headers.erase(new_end, headers.end());
110
111 CompilationRequirements result = { .headers = headers, .dependencies = dependencies };
112 return result;
113}
114
Josh Gaobfb6bae2016-07-15 17:25:21 -0700115static std::set<CompilationType> generateCompilationTypes(const std::set<Arch> selected_architectures,
116 const std::set<int>& selected_levels) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700117 std::set<CompilationType> result;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700118 for (const auto& arch : selected_architectures) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700119 int min_api = arch_min_api[arch];
120 for (int api_level : selected_levels) {
121 if (api_level < min_api) {
122 continue;
123 }
Josh Gaoa77b3a92016-08-15 16:39:27 -0700124
125 for (int file_offset_bits : { 32, 64 }) {
126 CompilationType type = {
127 .arch = arch, .api_level = api_level, .file_offset_bits = file_offset_bits
128 };
129 result.insert(type);
130 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700131 }
132 }
133 return result;
134}
135
Josh Gaobfb6bae2016-07-15 17:25:21 -0700136static std::unique_ptr<HeaderDatabase> compileHeaders(const std::set<CompilationType>& types,
137 const std::string& header_dir,
Josh Gao16016df2016-11-07 18:27:16 -0800138 const std::string& dependency_dir) {
139 if (types.empty()) {
140 errx(1, "compileHeaders received no CompilationTypes");
141 }
142
Josh Gao78b8a142016-11-09 01:00:41 -0800143 auto vfs = createCommonVFS(header_dir, dependency_dir, add_include);
144
Josh Gao16016df2016-11-07 18:27:16 -0800145 size_t thread_count = max_thread_count;
146 std::vector<std::thread> threads;
Josh Gaobf8a2852016-05-27 11:59:09 -0700147
148 std::map<CompilationType, HeaderDatabase> header_databases;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700149 std::unordered_map<Arch, CompilationRequirements> requirements;
Josh Gaobf8a2852016-05-27 11:59:09 -0700150
Josh Gao16016df2016-11-07 18:27:16 -0800151 auto result = std::make_unique<HeaderDatabase>();
Josh Gaobf8a2852016-05-27 11:59:09 -0700152 for (const auto& type : types) {
153 if (requirements.count(type.arch) == 0) {
154 requirements[type.arch] = collectRequirements(type.arch, header_dir, dependency_dir);
155 }
156 }
157
Josh Gao78b8a142016-11-09 01:00:41 -0800158 initializeTargetCC1FlagCache(vfs, types, requirements);
Josh Gaob5c49632016-11-08 22:21:31 -0800159
160 std::vector<std::pair<CompilationType, const std::string&>> jobs;
Josh Gao16016df2016-11-07 18:27:16 -0800161 for (CompilationType type : types) {
162 CompilationRequirements& req = requirements[type.arch];
163 for (const std::string& header : req.headers) {
Josh Gaob5c49632016-11-08 22:21:31 -0800164 jobs.emplace_back(type, header);
Josh Gaobf8a2852016-05-27 11:59:09 -0700165 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700166 }
167
Josh Gaob5c49632016-11-08 22:21:31 -0800168 thread_count = std::min(thread_count, jobs.size());
169
170 if (thread_count == 1) {
171 for (const auto& job : jobs) {
Josh Gao78b8a142016-11-09 01:00:41 -0800172 compileHeader(vfs, result.get(), job.first, job.second);
Josh Gaob5c49632016-11-08 22:21:31 -0800173 }
174 } else {
175 // Spawn threads.
176 for (size_t i = 0; i < thread_count; ++i) {
Josh Gao78b8a142016-11-09 01:00:41 -0800177 threads.emplace_back([&jobs, &result, &header_dir, vfs, thread_count, i]() {
Josh Gaob5c49632016-11-08 22:21:31 -0800178 size_t index = i;
179 while (index < jobs.size()) {
180 const auto& job = jobs[index];
Josh Gao78b8a142016-11-09 01:00:41 -0800181 compileHeader(vfs, result.get(), job.first, job.second);
Josh Gaob5c49632016-11-08 22:21:31 -0800182 index += thread_count;
183 }
184 });
Josh Gaobf8a2852016-05-27 11:59:09 -0700185 }
186
Josh Gaob5c49632016-11-08 22:21:31 -0800187 // Reap them.
188 for (auto& thread : threads) {
189 thread.join();
190 }
191 threads.clear();
Josh Gaobf8a2852016-05-27 11:59:09 -0700192 }
193
Josh Gaobfb6bae2016-07-15 17:25:21 -0700194 return result;
Josh Gaobf8a2852016-05-27 11:59:09 -0700195}
196
Josh Gao1a176de2016-11-04 13:15:11 -0700197static std::set<CompilationType> getCompilationTypes(const Declaration* decl) {
198 std::set<CompilationType> result;
199 for (const auto& it : decl->availability) {
200 result.insert(it.first);
201 }
202 return result;
203}
204
205template<typename T>
206static std::vector<T> Intersection(const std::set<T>& a, const std::set<T>& b) {
207 std::vector<T> intersection;
208 std::set_intersection(a.begin(), a.end(), b.begin(), b.end(), std::back_inserter(intersection));
209 return intersection;
210}
211
Josh Gaobfb6bae2016-07-15 17:25:21 -0700212// Perform a sanity check on a symbol's declarations, enforcing the following invariants:
213// 1. At most one inline definition of the function exists.
214// 2. All of the availability declarations for a symbol are compatible.
215// If a function is declared as an inline before a certain version, the inline definition
216// should have no version tag.
217// 3. Each availability type must only be present globally or on a per-arch basis.
218// (e.g. __INTRODUCED_IN_ARM(9) __INTRODUCED_IN_X86(10) __DEPRECATED_IN(11) is fine,
219// but not __INTRODUCED_IN(9) __INTRODUCED_IN_X86(10))
220static bool checkSymbol(const Symbol& symbol) {
221 std::string cwd = getWorkingDir() + "/";
222
Josh Gao1a176de2016-11-04 13:15:11 -0700223 std::unordered_map<const Declaration*, std::set<CompilationType>> inline_definitions;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700224 for (const auto& decl_it : symbol.declarations) {
225 const Declaration* decl = &decl_it.second;
226 if (decl->is_definition) {
Josh Gao1a176de2016-11-04 13:15:11 -0700227 std::set<CompilationType> compilation_types = getCompilationTypes(decl);
228 for (const auto& inline_def_it : inline_definitions) {
229 auto intersection = Intersection(compilation_types, inline_def_it.second);
230 if (!intersection.empty()) {
231 fprintf(stderr, "versioner: conflicting inline definitions:\n");
232 fprintf(stderr, " declarations visible in: %s\n", Join(intersection, ", ").c_str());
233 decl->dump(cwd, stderr, 4);
234 inline_def_it.first->dump(cwd, stderr, 4);
235 return false;
236 }
Josh Gaobfb6bae2016-07-15 17:25:21 -0700237 }
238
Josh Gao1a176de2016-11-04 13:15:11 -0700239 inline_definitions[decl] = std::move(compilation_types);
Josh Gaobfb6bae2016-07-15 17:25:21 -0700240 }
241
242 DeclarationAvailability availability;
243 if (!decl->calculateAvailability(&availability)) {
244 fprintf(stderr, "versioner: failed to calculate availability for declaration:\n");
Josh Gao566735d2016-08-02 15:07:32 -0700245 decl->dump(cwd, stderr, 2);
Josh Gaobfb6bae2016-07-15 17:25:21 -0700246 return false;
247 }
248
249 if (decl->is_definition && !availability.empty()) {
250 fprintf(stderr, "versioner: inline definition has non-empty versioning information:\n");
Josh Gao566735d2016-08-02 15:07:32 -0700251 decl->dump(cwd, stderr, 2);
Josh Gaobfb6bae2016-07-15 17:25:21 -0700252 return false;
253 }
254 }
255
256 DeclarationAvailability availability;
257 if (!symbol.calculateAvailability(&availability)) {
258 fprintf(stderr, "versioner: inconsistent availability for symbol '%s'\n", symbol.name.c_str());
259 symbol.dump(cwd);
260 return false;
261 }
262
263 // TODO: Check invariant #3.
264 return true;
265}
266
267static bool sanityCheck(const HeaderDatabase* database) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700268 bool error = false;
Josh Gao958f3b32016-06-03 13:44:00 -0700269 std::string cwd = getWorkingDir() + "/";
270
Josh Gaobfb6bae2016-07-15 17:25:21 -0700271 for (const auto& symbol_it : database->symbols) {
272 if (!checkSymbol(symbol_it.second)) {
273 error = true;
Josh Gaobf8a2852016-05-27 11:59:09 -0700274 }
275 }
276 return !error;
277}
278
279// Check that our symbol availability declarations match the actual NDK
280// platform symbol availability.
281static bool checkVersions(const std::set<CompilationType>& types,
Josh Gaobfb6bae2016-07-15 17:25:21 -0700282 const HeaderDatabase* header_database,
Josh Gaobf8a2852016-05-27 11:59:09 -0700283 const NdkSymbolDatabase& symbol_database) {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700284 std::string cwd = getWorkingDir() + "/";
Josh Gaobf8a2852016-05-27 11:59:09 -0700285 bool failed = false;
286
Josh Gaobfb6bae2016-07-15 17:25:21 -0700287 std::map<Arch, std::set<CompilationType>> arch_types;
Josh Gaobf8a2852016-05-27 11:59:09 -0700288 for (const CompilationType& type : types) {
289 arch_types[type.arch].insert(type);
290 }
291
Josh Gaod67dbf02016-06-02 15:21:14 -0700292 std::set<std::string> completely_unavailable;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700293 std::map<std::string, std::set<CompilationType>> missing_availability;
294 std::map<std::string, std::set<CompilationType>> extra_availability;
Josh Gaod67dbf02016-06-02 15:21:14 -0700295
Josh Gaobfb6bae2016-07-15 17:25:21 -0700296 for (const auto& symbol_it : header_database->symbols) {
297 const auto& symbol_name = symbol_it.first;
298 DeclarationAvailability symbol_availability;
Josh Gaobf8a2852016-05-27 11:59:09 -0700299
Josh Gaobfb6bae2016-07-15 17:25:21 -0700300 if (!symbol_it.second.calculateAvailability(&symbol_availability)) {
301 errx(1, "failed to calculate symbol availability");
302 }
303
304 const auto platform_availability_it = symbol_database.find(symbol_name);
Josh Gaobf8a2852016-05-27 11:59:09 -0700305 if (platform_availability_it == symbol_database.end()) {
Josh Gaod67dbf02016-06-02 15:21:14 -0700306 completely_unavailable.insert(symbol_name);
Josh Gaobf8a2852016-05-27 11:59:09 -0700307 continue;
308 }
309
310 const auto& platform_availability = platform_availability_it->second;
Josh Gaobf8a2852016-05-27 11:59:09 -0700311
312 for (const CompilationType& type : types) {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700313 bool should_be_available = true;
314 const auto& global_availability = symbol_availability.global_availability;
315 const auto& arch_availability = symbol_availability.arch_availability[type.arch];
316 if (global_availability.introduced != 0 && global_availability.introduced > type.api_level) {
317 should_be_available = false;
318 }
319
320 if (arch_availability.introduced != 0 && arch_availability.introduced > type.api_level) {
321 should_be_available = false;
322 }
323
324 if (global_availability.obsoleted != 0 && global_availability.obsoleted <= type.api_level) {
325 should_be_available = false;
326 }
327
328 if (arch_availability.obsoleted != 0 && arch_availability.obsoleted <= type.api_level) {
329 should_be_available = false;
330 }
331
332 if (arch_availability.future) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700333 continue;
334 }
335
Josh Gaobfb6bae2016-07-15 17:25:21 -0700336 // The function declaration might be (validly) missing for the given CompilationType.
337 if (!symbol_it.second.hasDeclaration(type)) {
338 should_be_available = false;
339 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700340
Josh Gaobfb6bae2016-07-15 17:25:21 -0700341 bool is_available = platform_availability.count(type);
Josh Gaobf8a2852016-05-27 11:59:09 -0700342
Josh Gaobfb6bae2016-07-15 17:25:21 -0700343 if (should_be_available != is_available) {
344 if (is_available) {
345 extra_availability[symbol_name].insert(type);
Josh Gaobf8a2852016-05-27 11:59:09 -0700346 } else {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700347 missing_availability[symbol_name].insert(type);
Josh Gaobf8a2852016-05-27 11:59:09 -0700348 }
349 }
350 }
Josh Gaobfb6bae2016-07-15 17:25:21 -0700351 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700352
Josh Gaobfb6bae2016-07-15 17:25:21 -0700353 for (const auto& it : symbol_database) {
354 const std::string& symbol_name = it.first;
Josh Gaobf8a2852016-05-27 11:59:09 -0700355
Josh Gaobfb6bae2016-07-15 17:25:21 -0700356 bool symbol_error = false;
357 auto missing_it = missing_availability.find(symbol_name);
358 if (missing_it != missing_availability.end()) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700359 printf("%s: declaration marked available but symbol missing in [%s]\n", symbol_name.c_str(),
Josh Gaobfb6bae2016-07-15 17:25:21 -0700360 Join(missing_it->second, ", ").c_str());
361 symbol_error = true;
Josh Gaobf8a2852016-05-27 11:59:09 -0700362 failed = true;
363 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700364
Josh Gaobfb6bae2016-07-15 17:25:21 -0700365 if (verbose) {
366 auto extra_it = extra_availability.find(symbol_name);
367 if (extra_it != extra_availability.end()) {
368 printf("%s: declaration marked unavailable but symbol available in [%s]\n",
369 symbol_name.c_str(), Join(extra_it->second, ", ").c_str());
370 symbol_error = true;
371 failed = true;
Josh Gao958f3b32016-06-03 13:44:00 -0700372 }
373 }
374
Josh Gaobfb6bae2016-07-15 17:25:21 -0700375 if (symbol_error) {
376 auto symbol_it = header_database->symbols.find(symbol_name);
377 if (symbol_it == header_database->symbols.end()) {
378 errx(1, "failed to find symbol in header database");
379 }
380 symbol_it->second.dump(cwd);
Josh Gaod67dbf02016-06-02 15:21:14 -0700381 }
Josh Gaod67dbf02016-06-02 15:21:14 -0700382 }
383
Josh Gaobfb6bae2016-07-15 17:25:21 -0700384 // TODO: Verify that function/variable declarations are actually function/variable symbols.
Josh Gaobf8a2852016-05-27 11:59:09 -0700385 return !failed;
386}
387
Josh Gao62aaf8f2016-06-02 14:27:21 -0700388static void usage(bool help = false) {
389 fprintf(stderr, "Usage: versioner [OPTION]... [HEADER_PATH] [DEPS_PATH]\n");
390 if (!help) {
391 printf("Try 'versioner -h' for more information.\n");
392 exit(1);
393 } else {
394 fprintf(stderr, "Version headers at HEADER_PATH, with DEPS_PATH/ARCH/* on the include path\n");
Josh Gao9b5af7a2016-06-02 14:29:13 -0700395 fprintf(stderr, "Autodetects paths if HEADER_PATH and DEPS_PATH are not specified\n");
Josh Gao62aaf8f2016-06-02 14:27:21 -0700396 fprintf(stderr, "\n");
397 fprintf(stderr, "Target specification (defaults to all):\n");
398 fprintf(stderr, " -a API_LEVEL\tbuild with specified API level (can be repeated)\n");
399 fprintf(stderr, " \t\tvalid levels are %s\n", Join(supported_levels).c_str());
400 fprintf(stderr, " -r ARCH\tbuild with specified architecture (can be repeated)\n");
401 fprintf(stderr, " \t\tvalid architectures are %s\n", Join(supported_archs).c_str());
402 fprintf(stderr, "\n");
403 fprintf(stderr, "Validation:\n");
404 fprintf(stderr, " -p PATH\tcompare against NDK platform at PATH\n");
405 fprintf(stderr, " -v\t\tenable verbose warnings\n");
406 fprintf(stderr, "\n");
Josh Gaof8592a32016-07-26 18:58:27 -0700407 fprintf(stderr, "Preprocessing:\n");
408 fprintf(stderr, " -o PATH\tpreprocess header files and emit them at PATH\n");
409 fprintf(stderr, " -f\tpreprocess header files even if validation fails\n");
410 fprintf(stderr, "\n");
Josh Gao62aaf8f2016-06-02 14:27:21 -0700411 fprintf(stderr, "Miscellaneous:\n");
Josh Gaobfb6bae2016-07-15 17:25:21 -0700412 fprintf(stderr, " -d\t\tdump function availability\n");
Josh Gao16016df2016-11-07 18:27:16 -0800413 fprintf(stderr, " -j THREADS\tmaximum number of threads to use\n");
Josh Gao62aaf8f2016-06-02 14:27:21 -0700414 fprintf(stderr, " -h\t\tdisplay this message\n");
415 exit(0);
416 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700417}
418
419int main(int argc, char** argv) {
420 std::string cwd = getWorkingDir() + "/";
421 bool default_args = true;
422 std::string platform_dir;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700423 std::set<Arch> selected_architectures;
Josh Gaobf8a2852016-05-27 11:59:09 -0700424 std::set<int> selected_levels;
Josh Gaof8592a32016-07-26 18:58:27 -0700425 std::string preprocessor_output_path;
426 bool force = false;
Josh Gao16016df2016-11-07 18:27:16 -0800427 bool dump = false;
Josh Gaobf8a2852016-05-27 11:59:09 -0700428
429 int c;
Josh Gao16016df2016-11-07 18:27:16 -0800430 while ((c = getopt(argc, argv, "a:r:p:vo:fdj:hi")) != -1) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700431 default_args = false;
432 switch (c) {
433 case 'a': {
434 char* end;
435 int api_level = strtol(optarg, &end, 10);
436 if (end == optarg || strlen(end) > 0) {
437 usage();
438 }
439
440 if (supported_levels.count(api_level) == 0) {
441 errx(1, "unsupported API level %d", api_level);
442 }
443
444 selected_levels.insert(api_level);
445 break;
446 }
447
448 case 'r': {
Josh Gaobfb6bae2016-07-15 17:25:21 -0700449 Arch arch = arch_from_string(optarg);
450 selected_architectures.insert(arch);
Josh Gaobf8a2852016-05-27 11:59:09 -0700451 break;
452 }
453
454 case 'p': {
455 if (!platform_dir.empty()) {
456 usage();
457 }
458
459 platform_dir = optarg;
460
Josh Gaof8592a32016-07-26 18:58:27 -0700461 if (platform_dir.empty()) {
462 usage();
463 }
464
Josh Gaobf8a2852016-05-27 11:59:09 -0700465 struct stat st;
466 if (stat(platform_dir.c_str(), &st) != 0) {
467 err(1, "failed to stat platform directory '%s'", platform_dir.c_str());
468 }
469 if (!S_ISDIR(st.st_mode)) {
470 errx(1, "'%s' is not a directory", optarg);
471 }
472 break;
473 }
474
475 case 'v':
476 verbose = true;
477 break;
478
Josh Gaof8592a32016-07-26 18:58:27 -0700479 case 'o':
480 if (!preprocessor_output_path.empty()) {
481 usage();
482 }
483 preprocessor_output_path = optarg;
484 if (preprocessor_output_path.empty()) {
485 usage();
486 }
487 break;
488
489 case 'f':
490 force = true;
491 break;
492
Josh Gaobfb6bae2016-07-15 17:25:21 -0700493 case 'd':
494 dump = true;
495 break;
496
Josh Gao16016df2016-11-07 18:27:16 -0800497 case 'j':
498 if (!android::base::ParseInt<int>(optarg, &max_thread_count, 1)) {
499 usage();
500 }
501 break;
502
Josh Gao62aaf8f2016-06-02 14:27:21 -0700503 case 'h':
504 usage(true);
505 break;
506
Josh Gaobfb6bae2016-07-15 17:25:21 -0700507 case 'i':
508 // Secret option for tests to -include <android/versioning.h>.
509 add_include = true;
510 break;
511
Josh Gaobf8a2852016-05-27 11:59:09 -0700512 default:
513 usage();
514 break;
515 }
516 }
517
Josh Gao9b5af7a2016-06-02 14:29:13 -0700518 if (argc - optind > 2 || optind > argc) {
Josh Gaobf8a2852016-05-27 11:59:09 -0700519 usage();
520 }
521
Josh Gao9b5af7a2016-06-02 14:29:13 -0700522 std::string header_dir;
523 std::string dependency_dir;
524
Josh Gaobfb6bae2016-07-15 17:25:21 -0700525 const char* top = getenv("ANDROID_BUILD_TOP");
526 if (!top && (optind == argc || add_include)) {
527 fprintf(stderr, "versioner: failed to autodetect bionic paths. Is ANDROID_BUILD_TOP set?\n");
528 usage();
529 }
530
Josh Gao9b5af7a2016-06-02 14:29:13 -0700531 if (optind == argc) {
532 // Neither HEADER_PATH nor DEPS_PATH were specified, so try to figure them out.
Josh Gaobfb6bae2016-07-15 17:25:21 -0700533 std::string versioner_dir = to_string(top) + "/bionic/tools/versioner";
Josh Gao9b5af7a2016-06-02 14:29:13 -0700534 header_dir = versioner_dir + "/current";
535 dependency_dir = versioner_dir + "/dependencies";
536 if (platform_dir.empty()) {
537 platform_dir = versioner_dir + "/platforms";
538 }
539 } else {
Josh Gaof8592a32016-07-26 18:58:27 -0700540 // Intentional leak.
541 header_dir = realpath(argv[optind], nullptr);
Josh Gao9b5af7a2016-06-02 14:29:13 -0700542
543 if (argc - optind == 2) {
544 dependency_dir = argv[optind + 1];
545 }
546 }
547
Josh Gaobf8a2852016-05-27 11:59:09 -0700548 if (selected_levels.empty()) {
549 selected_levels = supported_levels;
550 }
551
552 if (selected_architectures.empty()) {
553 selected_architectures = supported_archs;
554 }
555
Josh Gaobf8a2852016-05-27 11:59:09 -0700556
557 struct stat st;
Josh Gao9b5af7a2016-06-02 14:29:13 -0700558 if (stat(header_dir.c_str(), &st) != 0) {
559 err(1, "failed to stat '%s'", header_dir.c_str());
Josh Gaobf8a2852016-05-27 11:59:09 -0700560 } else if (!S_ISDIR(st.st_mode)) {
Josh Gao9b5af7a2016-06-02 14:29:13 -0700561 errx(1, "'%s' is not a directory", header_dir.c_str());
Josh Gaobf8a2852016-05-27 11:59:09 -0700562 }
563
564 std::set<CompilationType> compilation_types;
Josh Gaobf8a2852016-05-27 11:59:09 -0700565 NdkSymbolDatabase symbol_database;
566
567 compilation_types = generateCompilationTypes(selected_architectures, selected_levels);
568
569 // Do this before compiling so that we can early exit if the platforms don't match what we
570 // expect.
571 if (!platform_dir.empty()) {
572 symbol_database = parsePlatforms(compilation_types, platform_dir);
573 }
574
Josh Gaobfb6bae2016-07-15 17:25:21 -0700575 std::unique_ptr<HeaderDatabase> declaration_database =
Josh Gao16016df2016-11-07 18:27:16 -0800576 compileHeaders(compilation_types, header_dir, dependency_dir);
Josh Gaobf8a2852016-05-27 11:59:09 -0700577
Josh Gao16016df2016-11-07 18:27:16 -0800578 bool failed = false;
Josh Gaobfb6bae2016-07-15 17:25:21 -0700579 if (dump) {
580 declaration_database->dump(header_dir + "/");
581 } else {
582 if (!sanityCheck(declaration_database.get())) {
583 printf("versioner: sanity check failed\n");
Josh Gaobf8a2852016-05-27 11:59:09 -0700584 failed = true;
585 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700586
Josh Gaobfb6bae2016-07-15 17:25:21 -0700587 if (!platform_dir.empty()) {
588 if (!checkVersions(compilation_types, declaration_database.get(), symbol_database)) {
589 printf("versioner: version check failed\n");
590 failed = true;
591 }
592 }
593 }
Josh Gaof8592a32016-07-26 18:58:27 -0700594
595 if (!preprocessor_output_path.empty() && (force || !failed)) {
596 failed = !preprocessHeaders(preprocessor_output_path, header_dir, declaration_database.get());
597 }
Josh Gaobf8a2852016-05-27 11:59:09 -0700598 return failed;
599}