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 | }); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Josh Gao | b5c4963 | 2016-11-08 22:21:31 -0800 | [diff] [blame] | 187 | // Reap them. |
| 188 | for (auto& thread : threads) { |
| 189 | thread.join(); |
| 190 | } |
| 191 | threads.clear(); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 192 | } |
| 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 | 1a176de | 2016-11-04 13:15:11 -0700 | [diff] [blame] | 197 | static 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 | |
| 205 | template<typename T> |
| 206 | static 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 Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 212 | // 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)) |
| 220 | static bool checkSymbol(const Symbol& symbol) { |
| 221 | std::string cwd = getWorkingDir() + "/"; |
| 222 | |
Josh Gao | 1a176de | 2016-11-04 13:15:11 -0700 | [diff] [blame] | 223 | std::unordered_map<const Declaration*, std::set<CompilationType>> inline_definitions; |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 224 | for (const auto& decl_it : symbol.declarations) { |
| 225 | const Declaration* decl = &decl_it.second; |
| 226 | if (decl->is_definition) { |
Josh Gao | 1a176de | 2016-11-04 13:15:11 -0700 | [diff] [blame] | 227 | 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 Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 237 | } |
| 238 | |
Josh Gao | 1a176de | 2016-11-04 13:15:11 -0700 | [diff] [blame] | 239 | inline_definitions[decl] = std::move(compilation_types); |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | DeclarationAvailability availability; |
| 243 | if (!decl->calculateAvailability(&availability)) { |
| 244 | fprintf(stderr, "versioner: failed to calculate availability for declaration:\n"); |
Josh Gao | 566735d | 2016-08-02 15:07:32 -0700 | [diff] [blame] | 245 | decl->dump(cwd, stderr, 2); |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 246 | return false; |
| 247 | } |
| 248 | |
| 249 | if (decl->is_definition && !availability.empty()) { |
| 250 | fprintf(stderr, "versioner: inline definition has non-empty versioning information:\n"); |
Josh Gao | 566735d | 2016-08-02 15:07:32 -0700 | [diff] [blame] | 251 | decl->dump(cwd, stderr, 2); |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 252 | 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 | |
| 267 | static bool sanityCheck(const HeaderDatabase* database) { |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 268 | bool error = false; |
Josh Gao | 958f3b3 | 2016-06-03 13:44:00 -0700 | [diff] [blame] | 269 | std::string cwd = getWorkingDir() + "/"; |
| 270 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 271 | for (const auto& symbol_it : database->symbols) { |
| 272 | if (!checkSymbol(symbol_it.second)) { |
| 273 | error = true; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | return !error; |
| 277 | } |
| 278 | |
| 279 | // Check that our symbol availability declarations match the actual NDK |
| 280 | // platform symbol availability. |
| 281 | static bool checkVersions(const std::set<CompilationType>& types, |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 282 | const HeaderDatabase* header_database, |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 283 | const NdkSymbolDatabase& symbol_database) { |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 284 | std::string cwd = getWorkingDir() + "/"; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 285 | bool failed = false; |
| 286 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 287 | std::map<Arch, std::set<CompilationType>> arch_types; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 288 | for (const CompilationType& type : types) { |
| 289 | arch_types[type.arch].insert(type); |
| 290 | } |
| 291 | |
Josh Gao | d67dbf0 | 2016-06-02 15:21:14 -0700 | [diff] [blame] | 292 | std::set<std::string> completely_unavailable; |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 293 | std::map<std::string, std::set<CompilationType>> missing_availability; |
| 294 | std::map<std::string, std::set<CompilationType>> extra_availability; |
Josh Gao | d67dbf0 | 2016-06-02 15:21:14 -0700 | [diff] [blame] | 295 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 296 | for (const auto& symbol_it : header_database->symbols) { |
| 297 | const auto& symbol_name = symbol_it.first; |
| 298 | DeclarationAvailability symbol_availability; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 299 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 300 | 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 Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 305 | if (platform_availability_it == symbol_database.end()) { |
Josh Gao | d67dbf0 | 2016-06-02 15:21:14 -0700 | [diff] [blame] | 306 | completely_unavailable.insert(symbol_name); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 307 | continue; |
| 308 | } |
| 309 | |
| 310 | const auto& platform_availability = platform_availability_it->second; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 311 | |
| 312 | for (const CompilationType& type : types) { |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 313 | 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 Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 333 | continue; |
| 334 | } |
| 335 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 336 | // 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 Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 340 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 341 | bool is_available = platform_availability.count(type); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 342 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 343 | if (should_be_available != is_available) { |
| 344 | if (is_available) { |
| 345 | extra_availability[symbol_name].insert(type); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 346 | } else { |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 347 | missing_availability[symbol_name].insert(type); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | } |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 351 | } |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 352 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 353 | for (const auto& it : symbol_database) { |
| 354 | const std::string& symbol_name = it.first; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 355 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 356 | bool symbol_error = false; |
| 357 | auto missing_it = missing_availability.find(symbol_name); |
| 358 | if (missing_it != missing_availability.end()) { |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 359 | 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] | 360 | Join(missing_it->second, ", ").c_str()); |
| 361 | symbol_error = true; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 362 | failed = true; |
| 363 | } |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 364 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 365 | 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 Gao | 958f3b3 | 2016-06-03 13:44:00 -0700 | [diff] [blame] | 372 | } |
| 373 | } |
| 374 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 375 | 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 Gao | d67dbf0 | 2016-06-02 15:21:14 -0700 | [diff] [blame] | 381 | } |
Josh Gao | d67dbf0 | 2016-06-02 15:21:14 -0700 | [diff] [blame] | 382 | } |
| 383 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 384 | // TODO: Verify that function/variable declarations are actually function/variable symbols. |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 385 | return !failed; |
| 386 | } |
| 387 | |
Josh Gao | 62aaf8f | 2016-06-02 14:27:21 -0700 | [diff] [blame] | 388 | static 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 Gao | 9b5af7a | 2016-06-02 14:29:13 -0700 | [diff] [blame] | 395 | 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] | 396 | 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 Gao | f8592a3 | 2016-07-26 18:58:27 -0700 | [diff] [blame] | 407 | 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 Gao | 62aaf8f | 2016-06-02 14:27:21 -0700 | [diff] [blame] | 411 | fprintf(stderr, "Miscellaneous:\n"); |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 412 | fprintf(stderr, " -d\t\tdump function availability\n"); |
Josh Gao | 16016df | 2016-11-07 18:27:16 -0800 | [diff] [blame] | 413 | fprintf(stderr, " -j THREADS\tmaximum number of threads to use\n"); |
Josh Gao | 62aaf8f | 2016-06-02 14:27:21 -0700 | [diff] [blame] | 414 | fprintf(stderr, " -h\t\tdisplay this message\n"); |
| 415 | exit(0); |
| 416 | } |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 417 | } |
| 418 | |
| 419 | int main(int argc, char** argv) { |
| 420 | std::string cwd = getWorkingDir() + "/"; |
| 421 | bool default_args = true; |
| 422 | std::string platform_dir; |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 423 | std::set<Arch> selected_architectures; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 424 | std::set<int> selected_levels; |
Josh Gao | f8592a3 | 2016-07-26 18:58:27 -0700 | [diff] [blame] | 425 | std::string preprocessor_output_path; |
| 426 | bool force = false; |
Josh Gao | 16016df | 2016-11-07 18:27:16 -0800 | [diff] [blame] | 427 | bool dump = false; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 428 | |
| 429 | int c; |
Josh Gao | 16016df | 2016-11-07 18:27:16 -0800 | [diff] [blame] | 430 | while ((c = getopt(argc, argv, "a:r:p:vo:fdj:hi")) != -1) { |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 431 | 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 Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 449 | Arch arch = arch_from_string(optarg); |
| 450 | selected_architectures.insert(arch); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 451 | break; |
| 452 | } |
| 453 | |
| 454 | case 'p': { |
| 455 | if (!platform_dir.empty()) { |
| 456 | usage(); |
| 457 | } |
| 458 | |
| 459 | platform_dir = optarg; |
| 460 | |
Josh Gao | f8592a3 | 2016-07-26 18:58:27 -0700 | [diff] [blame] | 461 | if (platform_dir.empty()) { |
| 462 | usage(); |
| 463 | } |
| 464 | |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 465 | 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 Gao | f8592a3 | 2016-07-26 18:58:27 -0700 | [diff] [blame] | 479 | 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 Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 493 | case 'd': |
| 494 | dump = true; |
| 495 | break; |
| 496 | |
Josh Gao | 16016df | 2016-11-07 18:27:16 -0800 | [diff] [blame] | 497 | case 'j': |
| 498 | if (!android::base::ParseInt<int>(optarg, &max_thread_count, 1)) { |
| 499 | usage(); |
| 500 | } |
| 501 | break; |
| 502 | |
Josh Gao | 62aaf8f | 2016-06-02 14:27:21 -0700 | [diff] [blame] | 503 | case 'h': |
| 504 | usage(true); |
| 505 | break; |
| 506 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 507 | case 'i': |
| 508 | // Secret option for tests to -include <android/versioning.h>. |
| 509 | add_include = true; |
| 510 | break; |
| 511 | |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 512 | default: |
| 513 | usage(); |
| 514 | break; |
| 515 | } |
| 516 | } |
| 517 | |
Josh Gao | 9b5af7a | 2016-06-02 14:29:13 -0700 | [diff] [blame] | 518 | if (argc - optind > 2 || optind > argc) { |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 519 | usage(); |
| 520 | } |
| 521 | |
Josh Gao | 9b5af7a | 2016-06-02 14:29:13 -0700 | [diff] [blame] | 522 | std::string header_dir; |
| 523 | std::string dependency_dir; |
| 524 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 525 | 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 Gao | 9b5af7a | 2016-06-02 14:29:13 -0700 | [diff] [blame] | 531 | if (optind == argc) { |
| 532 | // 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] | 533 | std::string versioner_dir = to_string(top) + "/bionic/tools/versioner"; |
Josh Gao | 9b5af7a | 2016-06-02 14:29:13 -0700 | [diff] [blame] | 534 | 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 Gao | f8592a3 | 2016-07-26 18:58:27 -0700 | [diff] [blame] | 540 | // Intentional leak. |
| 541 | header_dir = realpath(argv[optind], nullptr); |
Josh Gao | 9b5af7a | 2016-06-02 14:29:13 -0700 | [diff] [blame] | 542 | |
| 543 | if (argc - optind == 2) { |
| 544 | dependency_dir = argv[optind + 1]; |
| 545 | } |
| 546 | } |
| 547 | |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 548 | if (selected_levels.empty()) { |
| 549 | selected_levels = supported_levels; |
| 550 | } |
| 551 | |
| 552 | if (selected_architectures.empty()) { |
| 553 | selected_architectures = supported_archs; |
| 554 | } |
| 555 | |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 556 | |
| 557 | struct stat st; |
Josh Gao | 9b5af7a | 2016-06-02 14:29:13 -0700 | [diff] [blame] | 558 | if (stat(header_dir.c_str(), &st) != 0) { |
| 559 | err(1, "failed to stat '%s'", header_dir.c_str()); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 560 | } else if (!S_ISDIR(st.st_mode)) { |
Josh Gao | 9b5af7a | 2016-06-02 14:29:13 -0700 | [diff] [blame] | 561 | errx(1, "'%s' is not a directory", header_dir.c_str()); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 562 | } |
| 563 | |
| 564 | std::set<CompilationType> compilation_types; |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 565 | 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 Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 575 | std::unique_ptr<HeaderDatabase> declaration_database = |
Josh Gao | 16016df | 2016-11-07 18:27:16 -0800 | [diff] [blame] | 576 | compileHeaders(compilation_types, header_dir, dependency_dir); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 577 | |
Josh Gao | 16016df | 2016-11-07 18:27:16 -0800 | [diff] [blame] | 578 | bool failed = false; |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 579 | if (dump) { |
| 580 | declaration_database->dump(header_dir + "/"); |
| 581 | } else { |
| 582 | if (!sanityCheck(declaration_database.get())) { |
| 583 | printf("versioner: sanity check failed\n"); |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 584 | failed = true; |
| 585 | } |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 586 | |
Josh Gao | bfb6bae | 2016-07-15 17:25:21 -0700 | [diff] [blame] | 587 | 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 Gao | f8592a3 | 2016-07-26 18:58:27 -0700 | [diff] [blame] | 594 | |
| 595 | if (!preprocessor_output_path.empty() && (force || !failed)) { |
| 596 | failed = !preprocessHeaders(preprocessor_output_path, header_dir, declaration_database.get()); |
| 597 | } |
Josh Gao | bf8a285 | 2016-05-27 11:59:09 -0700 | [diff] [blame] | 598 | return failed; |
| 599 | } |