Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 1 | /* |
Elliott Hughes | dfb74c5 | 2016-10-24 12:53:17 -0700 | [diff] [blame] | 2 | * Copyright (C) 2016 The Android Open Source Project |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 3 | * |
| 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 Hughes | dfb74c5 | 2016-10-24 12:53:17 -0700 | [diff] [blame] | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 9 | * |
| 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 Gao | 16016df | 2016-11-07 18:27:16 -0800 | [diff] [blame] | 26 | #include <functional> |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 27 | #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 Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 37 | #include <llvm/ADT/StringRef.h> |
| 38 | |
Josh Gao | 16016df | 2016-11-07 18:27:16 -0800 | [diff] [blame] | 39 | #include <android-base/parseint.h> |
| 40 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 41 | #include "Arch.h" |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 42 | #include "DeclarationDatabase.h" |
Josh Gao | b5c4963 | 2016-11-08 22:21:31 -0800 | [diff] [blame] | 43 | #include "Driver.h" |
Josh Gao | f8592a3 | 2016-07-26 18:58:27 -0700 | [diff] [blame] | 44 | #include "Preprocessor.h" |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 45 | #include "SymbolDatabase.h" |
| 46 | #include "Utils.h" |
Josh Gao | 78b8a14 | 2016-11-09 01:00:41 -0800 | [diff] [blame^] | 47 | #include "VFS.h" |
| 48 | |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 49 | #include "versioner.h" |
| 50 | |
| 51 | using namespace std::string_literals; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 52 | |
Josh Gao | b5c4963 | 2016-11-08 22:21:31 -0800 | [diff] [blame] | 53 | bool add_include; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 54 | bool verbose; |
Josh Gao | 16016df | 2016-11-07 18:27:16 -0800 | [diff] [blame] | 55 | static int max_thread_count = 48; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 56 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 57 | static CompilationRequirements collectRequirements(const Arch& arch, const std::string& header_dir, |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 58 | const std::string& dependency_dir) { |
| 59 | std::vector<std::string> headers = collectFiles(header_dir); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 60 | std::vector<std::string> dependencies = { header_dir }; |
| 61 | if (!dependency_dir.empty()) { |
Josh Gao | b5c4963 | 2016-11-08 22:21:31 -0800 | [diff] [blame] | 62 | auto collect_children = [&dependencies, &dependency_dir](const std::string& dir_path) { |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 63 | 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 Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 93 | collect_children(dependency_dir + "/" + to_string(arch)); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 94 | } |
| 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 Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 115 | static std::set<CompilationType> generateCompilationTypes(const std::set<Arch> selected_architectures, |
| 116 | const std::set<int>& selected_levels) { |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 117 | std::set<CompilationType> result; |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 118 | for (const auto& arch : selected_architectures) { |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 119 | int min_api = arch_min_api[arch]; |
| 120 | for (int api_level : selected_levels) { |
| 121 | if (api_level < min_api) { |
| 122 | continue; |
| 123 | } |
Josh Gao | a77b3a9 | 2016-08-15 16:39:27 -0700 | [diff] [blame] | 124 | |
| 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 Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 131 | } |
| 132 | } |
| 133 | return result; |
| 134 | } |
| 135 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 136 | static std::unique_ptr<HeaderDatabase> compileHeaders(const std::set<CompilationType>& types, |
| 137 | const std::string& header_dir, |
Josh Gao | 16016df | 2016-11-07 18:27:16 -0800 | [diff] [blame] | 138 | const std::string& dependency_dir) { |
| 139 | if (types.empty()) { |
| 140 | errx(1, "compileHeaders received no CompilationTypes"); |
| 141 | } |
| 142 | |
Josh Gao | 78b8a14 | 2016-11-09 01:00:41 -0800 | [diff] [blame^] | 143 | auto vfs = createCommonVFS(header_dir, dependency_dir, add_include); |
| 144 | |
Josh Gao | 16016df | 2016-11-07 18:27:16 -0800 | [diff] [blame] | 145 | size_t thread_count = max_thread_count; |
| 146 | std::vector<std::thread> threads; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 147 | |
| 148 | std::map<CompilationType, HeaderDatabase> header_databases; |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 149 | std::unordered_map<Arch, CompilationRequirements> requirements; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 150 | |
Josh Gao | 16016df | 2016-11-07 18:27:16 -0800 | [diff] [blame] | 151 | auto result = std::make_unique<HeaderDatabase>(); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 152 | 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 Gao | 78b8a14 | 2016-11-09 01:00:41 -0800 | [diff] [blame^] | 158 | initializeTargetCC1FlagCache(vfs, types, requirements); |
Josh Gao | b5c4963 | 2016-11-08 22:21:31 -0800 | [diff] [blame] | 159 | |
| 160 | std::vector<std::pair<CompilationType, const std::string&>> jobs; |
Josh Gao | 16016df | 2016-11-07 18:27:16 -0800 | [diff] [blame] | 161 | for (CompilationType type : types) { |
| 162 | CompilationRequirements& req = requirements[type.arch]; |
| 163 | for (const std::string& header : req.headers) { |
Josh Gao | b5c4963 | 2016-11-08 22:21:31 -0800 | [diff] [blame] | 164 | jobs.emplace_back(type, header); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 165 | } |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 166 | } |
| 167 | |
Josh Gao | b5c4963 | 2016-11-08 22:21:31 -0800 | [diff] [blame] | 168 | thread_count = std::min(thread_count, jobs.size()); |
| 169 | |
| 170 | if (thread_count == 1) { |
| 171 | for (const auto& job : jobs) { |
Josh Gao | 78b8a14 | 2016-11-09 01:00:41 -0800 | [diff] [blame^] | 172 | compileHeader(vfs, result.get(), job.first, job.second); |
Josh Gao | b5c4963 | 2016-11-08 22:21:31 -0800 | [diff] [blame] | 173 | } |
| 174 | } else { |
| 175 | // Spawn threads. |
| 176 | for (size_t i = 0; i < thread_count; ++i) { |
Josh Gao | 78b8a14 | 2016-11-09 01:00:41 -0800 | [diff] [blame^] | 177 | threads.emplace_back([&jobs, &result, &header_dir, vfs, thread_count, i]() { |
Josh Gao | b5c4963 | 2016-11-08 22:21:31 -0800 | [diff] [blame] | 178 | size_t index = i; |
| 179 | while (index < jobs.size()) { |
| 180 | const auto& job = jobs[index]; |
Josh Gao | 78b8a14 | 2016-11-09 01:00:41 -0800 | [diff] [blame^] | 181 | compileHeader(vfs, result.get(), job.first, job.second); |
Josh Gao | b5c4963 | 2016-11-08 22:21:31 -0800 | [diff] [blame] | 182 | index += thread_count; |
| 183 | } |
| 184 | }); |
| 185 | } |
| 186 | |
| 187 | // Reap them. |
| 188 | for (auto& thread : threads) { |
| 189 | thread.join(); |
| 190 | } |
| 191 | threads.clear(); |
| 192 | } |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 193 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 194 | return result; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 197 | // Perform a sanity check on a symbol's declarations, enforcing the following invariants: |
| 198 | // 1. At most one inline definition of the function exists. |
| 199 | // 2. All of the availability declarations for a symbol are compatible. |
| 200 | // If a function is declared as an inline before a certain version, the inline definition |
| 201 | // should have no version tag. |
| 202 | // 3. Each availability type must only be present globally or on a per-arch basis. |
| 203 | // (e.g. __INTRODUCED_IN_ARM(9) __INTRODUCED_IN_X86(10) __DEPRECATED_IN(11) is fine, |
| 204 | // but not __INTRODUCED_IN(9) __INTRODUCED_IN_X86(10)) |
| 205 | static bool checkSymbol(const Symbol& symbol) { |
| 206 | std::string cwd = getWorkingDir() + "/"; |
| 207 | |
| 208 | const Declaration* inline_definition = nullptr; |
| 209 | for (const auto& decl_it : symbol.declarations) { |
| 210 | const Declaration* decl = &decl_it.second; |
| 211 | if (decl->is_definition) { |
| 212 | if (inline_definition) { |
| 213 | fprintf(stderr, "versioner: multiple definitions of symbol %s\n", symbol.name.c_str()); |
| 214 | symbol.dump(cwd); |
| 215 | inline_definition->dump(cwd); |
| 216 | return false; |
| 217 | } |
| 218 | |
| 219 | inline_definition = decl; |
| 220 | } |
| 221 | |
| 222 | DeclarationAvailability availability; |
| 223 | if (!decl->calculateAvailability(&availability)) { |
| 224 | fprintf(stderr, "versioner: failed to calculate availability for declaration:\n"); |
Josh Gao | 566735d | 2016-08-02 15:07:32 -0700 | [diff] [blame] | 225 | decl->dump(cwd, stderr, 2); |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 226 | return false; |
| 227 | } |
| 228 | |
| 229 | if (decl->is_definition && !availability.empty()) { |
| 230 | fprintf(stderr, "versioner: inline definition has non-empty versioning information:\n"); |
Josh Gao | 566735d | 2016-08-02 15:07:32 -0700 | [diff] [blame] | 231 | decl->dump(cwd, stderr, 2); |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 232 | return false; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | DeclarationAvailability availability; |
| 237 | if (!symbol.calculateAvailability(&availability)) { |
| 238 | fprintf(stderr, "versioner: inconsistent availability for symbol '%s'\n", symbol.name.c_str()); |
| 239 | symbol.dump(cwd); |
| 240 | return false; |
| 241 | } |
| 242 | |
| 243 | // TODO: Check invariant #3. |
| 244 | return true; |
| 245 | } |
| 246 | |
| 247 | static bool sanityCheck(const HeaderDatabase* database) { |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 248 | bool error = false; |
Josh Gao | 958f3b3 | 2016-06-03 13:44:00 -0700 | [diff] [blame] | 249 | std::string cwd = getWorkingDir() + "/"; |
| 250 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 251 | for (const auto& symbol_it : database->symbols) { |
| 252 | if (!checkSymbol(symbol_it.second)) { |
| 253 | error = true; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | return !error; |
| 257 | } |
| 258 | |
| 259 | // Check that our symbol availability declarations match the actual NDK |
| 260 | // platform symbol availability. |
| 261 | static bool checkVersions(const std::set<CompilationType>& types, |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 262 | const HeaderDatabase* header_database, |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 263 | const NdkSymbolDatabase& symbol_database) { |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 264 | std::string cwd = getWorkingDir() + "/"; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 265 | bool failed = false; |
| 266 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 267 | std::map<Arch, std::set<CompilationType>> arch_types; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 268 | for (const CompilationType& type : types) { |
| 269 | arch_types[type.arch].insert(type); |
| 270 | } |
| 271 | |
Josh Gao | d67dbf0 | 2016-06-02 15:21:14 -0700 | [diff] [blame] | 272 | std::set<std::string> completely_unavailable; |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 273 | std::map<std::string, std::set<CompilationType>> missing_availability; |
| 274 | std::map<std::string, std::set<CompilationType>> extra_availability; |
Josh Gao | d67dbf0 | 2016-06-02 15:21:14 -0700 | [diff] [blame] | 275 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 276 | for (const auto& symbol_it : header_database->symbols) { |
| 277 | const auto& symbol_name = symbol_it.first; |
| 278 | DeclarationAvailability symbol_availability; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 279 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 280 | if (!symbol_it.second.calculateAvailability(&symbol_availability)) { |
| 281 | errx(1, "failed to calculate symbol availability"); |
| 282 | } |
| 283 | |
| 284 | const auto platform_availability_it = symbol_database.find(symbol_name); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 285 | if (platform_availability_it == symbol_database.end()) { |
Josh Gao | d67dbf0 | 2016-06-02 15:21:14 -0700 | [diff] [blame] | 286 | completely_unavailable.insert(symbol_name); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 287 | continue; |
| 288 | } |
| 289 | |
| 290 | const auto& platform_availability = platform_availability_it->second; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 291 | |
| 292 | for (const CompilationType& type : types) { |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 293 | bool should_be_available = true; |
| 294 | const auto& global_availability = symbol_availability.global_availability; |
| 295 | const auto& arch_availability = symbol_availability.arch_availability[type.arch]; |
| 296 | if (global_availability.introduced != 0 && global_availability.introduced > type.api_level) { |
| 297 | should_be_available = false; |
| 298 | } |
| 299 | |
| 300 | if (arch_availability.introduced != 0 && arch_availability.introduced > type.api_level) { |
| 301 | should_be_available = false; |
| 302 | } |
| 303 | |
| 304 | if (global_availability.obsoleted != 0 && global_availability.obsoleted <= type.api_level) { |
| 305 | should_be_available = false; |
| 306 | } |
| 307 | |
| 308 | if (arch_availability.obsoleted != 0 && arch_availability.obsoleted <= type.api_level) { |
| 309 | should_be_available = false; |
| 310 | } |
| 311 | |
| 312 | if (arch_availability.future) { |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 313 | continue; |
| 314 | } |
| 315 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 316 | // The function declaration might be (validly) missing for the given CompilationType. |
| 317 | if (!symbol_it.second.hasDeclaration(type)) { |
| 318 | should_be_available = false; |
| 319 | } |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 320 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 321 | bool is_available = platform_availability.count(type); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 322 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 323 | if (should_be_available != is_available) { |
| 324 | if (is_available) { |
| 325 | extra_availability[symbol_name].insert(type); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 326 | } else { |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 327 | missing_availability[symbol_name].insert(type); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 328 | } |
| 329 | } |
| 330 | } |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 331 | } |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 332 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 333 | for (const auto& it : symbol_database) { |
| 334 | const std::string& symbol_name = it.first; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 335 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 336 | bool symbol_error = false; |
| 337 | auto missing_it = missing_availability.find(symbol_name); |
| 338 | if (missing_it != missing_availability.end()) { |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 339 | printf("%s: declaration marked available but symbol missing in [%s]\n", symbol_name.c_str(), |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 340 | Join(missing_it->second, ", ").c_str()); |
| 341 | symbol_error = true; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 342 | failed = true; |
| 343 | } |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 344 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 345 | if (verbose) { |
| 346 | auto extra_it = extra_availability.find(symbol_name); |
| 347 | if (extra_it != extra_availability.end()) { |
| 348 | printf("%s: declaration marked unavailable but symbol available in [%s]\n", |
| 349 | symbol_name.c_str(), Join(extra_it->second, ", ").c_str()); |
| 350 | symbol_error = true; |
| 351 | failed = true; |
Josh Gao | 958f3b3 | 2016-06-03 13:44:00 -0700 | [diff] [blame] | 352 | } |
| 353 | } |
| 354 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 355 | if (symbol_error) { |
| 356 | auto symbol_it = header_database->symbols.find(symbol_name); |
| 357 | if (symbol_it == header_database->symbols.end()) { |
| 358 | errx(1, "failed to find symbol in header database"); |
| 359 | } |
| 360 | symbol_it->second.dump(cwd); |
Josh Gao | d67dbf0 | 2016-06-02 15:21:14 -0700 | [diff] [blame] | 361 | } |
Josh Gao | d67dbf0 | 2016-06-02 15:21:14 -0700 | [diff] [blame] | 362 | } |
| 363 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 364 | // TODO: Verify that function/variable declarations are actually function/variable symbols. |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 365 | return !failed; |
| 366 | } |
| 367 | |
Josh Gao | 62aaf8f | 2016-06-02 14:27:21 -0700 | [diff] [blame] | 368 | static void usage(bool help = false) { |
| 369 | fprintf(stderr, "Usage: versioner [OPTION]... [HEADER_PATH] [DEPS_PATH]\n"); |
| 370 | if (!help) { |
| 371 | printf("Try 'versioner -h' for more information.\n"); |
| 372 | exit(1); |
| 373 | } else { |
| 374 | fprintf(stderr, "Version headers at HEADER_PATH, with DEPS_PATH/ARCH/* on the include path\n"); |
Josh Gao | 9b5af7a | 2016-06-02 14:29:13 -0700 | [diff] [blame] | 375 | fprintf(stderr, "Autodetects paths if HEADER_PATH and DEPS_PATH are not specified\n"); |
Josh Gao | 62aaf8f | 2016-06-02 14:27:21 -0700 | [diff] [blame] | 376 | fprintf(stderr, "\n"); |
| 377 | fprintf(stderr, "Target specification (defaults to all):\n"); |
| 378 | fprintf(stderr, " -a API_LEVEL\tbuild with specified API level (can be repeated)\n"); |
| 379 | fprintf(stderr, " \t\tvalid levels are %s\n", Join(supported_levels).c_str()); |
| 380 | fprintf(stderr, " -r ARCH\tbuild with specified architecture (can be repeated)\n"); |
| 381 | fprintf(stderr, " \t\tvalid architectures are %s\n", Join(supported_archs).c_str()); |
| 382 | fprintf(stderr, "\n"); |
| 383 | fprintf(stderr, "Validation:\n"); |
| 384 | fprintf(stderr, " -p PATH\tcompare against NDK platform at PATH\n"); |
| 385 | fprintf(stderr, " -v\t\tenable verbose warnings\n"); |
| 386 | fprintf(stderr, "\n"); |
Josh Gao | f8592a3 | 2016-07-26 18:58:27 -0700 | [diff] [blame] | 387 | fprintf(stderr, "Preprocessing:\n"); |
| 388 | fprintf(stderr, " -o PATH\tpreprocess header files and emit them at PATH\n"); |
| 389 | fprintf(stderr, " -f\tpreprocess header files even if validation fails\n"); |
| 390 | fprintf(stderr, "\n"); |
Josh Gao | 62aaf8f | 2016-06-02 14:27:21 -0700 | [diff] [blame] | 391 | fprintf(stderr, "Miscellaneous:\n"); |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 392 | fprintf(stderr, " -d\t\tdump function availability\n"); |
Josh Gao | 16016df | 2016-11-07 18:27:16 -0800 | [diff] [blame] | 393 | fprintf(stderr, " -j THREADS\tmaximum number of threads to use\n"); |
Josh Gao | 62aaf8f | 2016-06-02 14:27:21 -0700 | [diff] [blame] | 394 | fprintf(stderr, " -h\t\tdisplay this message\n"); |
| 395 | exit(0); |
| 396 | } |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | int main(int argc, char** argv) { |
| 400 | std::string cwd = getWorkingDir() + "/"; |
| 401 | bool default_args = true; |
| 402 | std::string platform_dir; |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 403 | std::set<Arch> selected_architectures; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 404 | std::set<int> selected_levels; |
Josh Gao | f8592a3 | 2016-07-26 18:58:27 -0700 | [diff] [blame] | 405 | std::string preprocessor_output_path; |
| 406 | bool force = false; |
Josh Gao | 16016df | 2016-11-07 18:27:16 -0800 | [diff] [blame] | 407 | bool dump = false; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 408 | |
| 409 | int c; |
Josh Gao | 16016df | 2016-11-07 18:27:16 -0800 | [diff] [blame] | 410 | while ((c = getopt(argc, argv, "a:r:p:vo:fdj:hi")) != -1) { |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 411 | default_args = false; |
| 412 | switch (c) { |
| 413 | case 'a': { |
| 414 | char* end; |
| 415 | int api_level = strtol(optarg, &end, 10); |
| 416 | if (end == optarg || strlen(end) > 0) { |
| 417 | usage(); |
| 418 | } |
| 419 | |
| 420 | if (supported_levels.count(api_level) == 0) { |
| 421 | errx(1, "unsupported API level %d", api_level); |
| 422 | } |
| 423 | |
| 424 | selected_levels.insert(api_level); |
| 425 | break; |
| 426 | } |
| 427 | |
| 428 | case 'r': { |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 429 | Arch arch = arch_from_string(optarg); |
| 430 | selected_architectures.insert(arch); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 431 | break; |
| 432 | } |
| 433 | |
| 434 | case 'p': { |
| 435 | if (!platform_dir.empty()) { |
| 436 | usage(); |
| 437 | } |
| 438 | |
| 439 | platform_dir = optarg; |
| 440 | |
Josh Gao | f8592a3 | 2016-07-26 18:58:27 -0700 | [diff] [blame] | 441 | if (platform_dir.empty()) { |
| 442 | usage(); |
| 443 | } |
| 444 | |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 445 | struct stat st; |
| 446 | if (stat(platform_dir.c_str(), &st) != 0) { |
| 447 | err(1, "failed to stat platform directory '%s'", platform_dir.c_str()); |
| 448 | } |
| 449 | if (!S_ISDIR(st.st_mode)) { |
| 450 | errx(1, "'%s' is not a directory", optarg); |
| 451 | } |
| 452 | break; |
| 453 | } |
| 454 | |
| 455 | case 'v': |
| 456 | verbose = true; |
| 457 | break; |
| 458 | |
Josh Gao | f8592a3 | 2016-07-26 18:58:27 -0700 | [diff] [blame] | 459 | case 'o': |
| 460 | if (!preprocessor_output_path.empty()) { |
| 461 | usage(); |
| 462 | } |
| 463 | preprocessor_output_path = optarg; |
| 464 | if (preprocessor_output_path.empty()) { |
| 465 | usage(); |
| 466 | } |
| 467 | break; |
| 468 | |
| 469 | case 'f': |
| 470 | force = true; |
| 471 | break; |
| 472 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 473 | case 'd': |
| 474 | dump = true; |
| 475 | break; |
| 476 | |
Josh Gao | 16016df | 2016-11-07 18:27:16 -0800 | [diff] [blame] | 477 | case 'j': |
| 478 | if (!android::base::ParseInt<int>(optarg, &max_thread_count, 1)) { |
| 479 | usage(); |
| 480 | } |
| 481 | break; |
| 482 | |
Josh Gao | 62aaf8f | 2016-06-02 14:27:21 -0700 | [diff] [blame] | 483 | case 'h': |
| 484 | usage(true); |
| 485 | break; |
| 486 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 487 | case 'i': |
| 488 | // Secret option for tests to -include <android/versioning.h>. |
| 489 | add_include = true; |
| 490 | break; |
| 491 | |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 492 | default: |
| 493 | usage(); |
| 494 | break; |
| 495 | } |
| 496 | } |
| 497 | |
Josh Gao | 9b5af7a | 2016-06-02 14:29:13 -0700 | [diff] [blame] | 498 | if (argc - optind > 2 || optind > argc) { |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 499 | usage(); |
| 500 | } |
| 501 | |
Josh Gao | 9b5af7a | 2016-06-02 14:29:13 -0700 | [diff] [blame] | 502 | std::string header_dir; |
| 503 | std::string dependency_dir; |
| 504 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 505 | const char* top = getenv("ANDROID_BUILD_TOP"); |
| 506 | if (!top && (optind == argc || add_include)) { |
| 507 | fprintf(stderr, "versioner: failed to autodetect bionic paths. Is ANDROID_BUILD_TOP set?\n"); |
| 508 | usage(); |
| 509 | } |
| 510 | |
Josh Gao | 9b5af7a | 2016-06-02 14:29:13 -0700 | [diff] [blame] | 511 | if (optind == argc) { |
| 512 | // Neither HEADER_PATH nor DEPS_PATH were specified, so try to figure them out. |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 513 | std::string versioner_dir = to_string(top) + "/bionic/tools/versioner"; |
Josh Gao | 9b5af7a | 2016-06-02 14:29:13 -0700 | [diff] [blame] | 514 | header_dir = versioner_dir + "/current"; |
| 515 | dependency_dir = versioner_dir + "/dependencies"; |
| 516 | if (platform_dir.empty()) { |
| 517 | platform_dir = versioner_dir + "/platforms"; |
| 518 | } |
| 519 | } else { |
Josh Gao | f8592a3 | 2016-07-26 18:58:27 -0700 | [diff] [blame] | 520 | // Intentional leak. |
| 521 | header_dir = realpath(argv[optind], nullptr); |
Josh Gao | 9b5af7a | 2016-06-02 14:29:13 -0700 | [diff] [blame] | 522 | |
| 523 | if (argc - optind == 2) { |
| 524 | dependency_dir = argv[optind + 1]; |
| 525 | } |
| 526 | } |
| 527 | |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 528 | if (selected_levels.empty()) { |
| 529 | selected_levels = supported_levels; |
| 530 | } |
| 531 | |
| 532 | if (selected_architectures.empty()) { |
| 533 | selected_architectures = supported_archs; |
| 534 | } |
| 535 | |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 536 | |
| 537 | struct stat st; |
Josh Gao | 9b5af7a | 2016-06-02 14:29:13 -0700 | [diff] [blame] | 538 | if (stat(header_dir.c_str(), &st) != 0) { |
| 539 | err(1, "failed to stat '%s'", header_dir.c_str()); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 540 | } else if (!S_ISDIR(st.st_mode)) { |
Josh Gao | 9b5af7a | 2016-06-02 14:29:13 -0700 | [diff] [blame] | 541 | errx(1, "'%s' is not a directory", header_dir.c_str()); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | std::set<CompilationType> compilation_types; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 545 | NdkSymbolDatabase symbol_database; |
| 546 | |
| 547 | compilation_types = generateCompilationTypes(selected_architectures, selected_levels); |
| 548 | |
| 549 | // Do this before compiling so that we can early exit if the platforms don't match what we |
| 550 | // expect. |
| 551 | if (!platform_dir.empty()) { |
| 552 | symbol_database = parsePlatforms(compilation_types, platform_dir); |
| 553 | } |
| 554 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 555 | std::unique_ptr<HeaderDatabase> declaration_database = |
Josh Gao | 16016df | 2016-11-07 18:27:16 -0800 | [diff] [blame] | 556 | compileHeaders(compilation_types, header_dir, dependency_dir); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 557 | |
Josh Gao | 16016df | 2016-11-07 18:27:16 -0800 | [diff] [blame] | 558 | bool failed = false; |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 559 | if (dump) { |
| 560 | declaration_database->dump(header_dir + "/"); |
| 561 | } else { |
| 562 | if (!sanityCheck(declaration_database.get())) { |
| 563 | printf("versioner: sanity check failed\n"); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 564 | failed = true; |
| 565 | } |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 566 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 567 | if (!platform_dir.empty()) { |
| 568 | if (!checkVersions(compilation_types, declaration_database.get(), symbol_database)) { |
| 569 | printf("versioner: version check failed\n"); |
| 570 | failed = true; |
| 571 | } |
| 572 | } |
| 573 | } |
Josh Gao | f8592a3 | 2016-07-26 18:58:27 -0700 | [diff] [blame] | 574 | |
| 575 | if (!preprocessor_output_path.empty() && (force || !failed)) { |
| 576 | failed = !preprocessHeaders(preprocessor_output_path, header_dir, declaration_database.get()); |
| 577 | } |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 578 | return failed; |
| 579 | } |