Andreas Huber | 1aec397 | 2016-08-26 09:26:32 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 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 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 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 | |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 17 | #include "Coordinator.h" |
| 18 | |
Iliyan Malchev | a72e0d2 | 2016-09-09 11:03:08 -0700 | [diff] [blame] | 19 | #include <dirent.h> |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 20 | #include <sys/stat.h> |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 21 | |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 22 | #include <algorithm> |
| 23 | #include <iterator> |
| 24 | |
| 25 | #include <android-base/logging.h> |
Steven Moreland | 5bdfa70 | 2017-04-18 23:20:39 -0700 | [diff] [blame] | 26 | #include <hidl-hash/Hash.h> |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 27 | #include <hidl-util/StringHelper.h> |
Steven Moreland | cbff561 | 2017-10-11 17:01:54 -0700 | [diff] [blame] | 28 | #include <iostream> |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 29 | |
| 30 | #include "AST.h" |
| 31 | #include "Interface.h" |
Steven Moreland | 06a088b | 2018-02-20 12:34:47 -0800 | [diff] [blame] | 32 | #include "hidl-gen_l.h" |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 33 | |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 34 | static bool existdir(const char *name) { |
| 35 | DIR *dir = opendir(name); |
Yi Kong | 56758da | 2018-07-24 16:21:37 -0700 | [diff] [blame] | 36 | if (dir == nullptr) { |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 37 | return false; |
| 38 | } |
| 39 | closedir(dir); |
| 40 | return true; |
| 41 | } |
| 42 | |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 43 | namespace android { |
| 44 | |
Steven Moreland | 47792c4 | 2017-09-20 10:03:20 -0700 | [diff] [blame] | 45 | const std::string &Coordinator::getRootPath() const { |
| 46 | return mRootPath; |
Andreas Huber | dc98133 | 2016-07-29 15:46:54 -0700 | [diff] [blame] | 47 | } |
| 48 | |
Steven Moreland | 47792c4 | 2017-09-20 10:03:20 -0700 | [diff] [blame] | 49 | void Coordinator::setRootPath(const std::string &rootPath) { |
| 50 | mRootPath = rootPath; |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 51 | |
Steven Moreland | 47792c4 | 2017-09-20 10:03:20 -0700 | [diff] [blame] | 52 | if (!mRootPath.empty() && !StringHelper::EndsWith(mRootPath, "/")) { |
| 53 | mRootPath += "/"; |
Steven Moreland | 25c8166 | 2017-05-12 14:57:36 -0700 | [diff] [blame] | 54 | } |
| 55 | } |
| 56 | |
Steven Moreland | 6d3d3c8 | 2018-02-08 11:49:34 -0800 | [diff] [blame] | 57 | void Coordinator::setOutputPath(const std::string& outputPath) { |
| 58 | mOutputPath = outputPath; |
| 59 | } |
| 60 | |
Steven Moreland | 37c57ee | 2017-09-25 19:08:56 -0700 | [diff] [blame] | 61 | void Coordinator::setVerbose(bool verbose) { |
| 62 | mVerbose = verbose; |
| 63 | } |
| 64 | |
Andreas Huber | 308d8a2 | 2017-11-06 14:46:52 -0800 | [diff] [blame] | 65 | bool Coordinator::isVerbose() const { |
| 66 | return mVerbose; |
| 67 | } |
| 68 | |
Steven Moreland | 394af5c | 2018-02-09 14:41:46 -0800 | [diff] [blame] | 69 | void Coordinator::setDepFile(const std::string& depFile) { |
| 70 | mDepFile = depFile; |
| 71 | } |
| 72 | |
Steven Moreland | 89a9ebb | 2017-12-04 10:18:00 -0800 | [diff] [blame] | 73 | const std::string& Coordinator::getOwner() const { |
| 74 | return mOwner; |
| 75 | } |
| 76 | void Coordinator::setOwner(const std::string& owner) { |
| 77 | mOwner = owner; |
| 78 | } |
| 79 | |
Steven Moreland | 47792c4 | 2017-09-20 10:03:20 -0700 | [diff] [blame] | 80 | status_t Coordinator::addPackagePath(const std::string& root, const std::string& path, std::string* error) { |
| 81 | FQName package = FQName(root, "0.0", ""); |
| 82 | for (const PackageRoot &packageRoot : mPackageRoots) { |
| 83 | if (packageRoot.root.inPackage(root) || package.inPackage(packageRoot.root.package())) { |
| 84 | if (error != nullptr) { |
| 85 | *error = "ERROR: conflicting package roots " + |
| 86 | packageRoot.root.package() + |
| 87 | " and " + |
| 88 | root; |
| 89 | } |
| 90 | |
| 91 | return UNKNOWN_ERROR; |
| 92 | } |
| 93 | } |
| 94 | |
Steven Moreland | 1a85fd9 | 2017-09-28 16:31:24 -0700 | [diff] [blame] | 95 | mPackageRoots.push_back({path, package}); |
Steven Moreland | 47792c4 | 2017-09-20 10:03:20 -0700 | [diff] [blame] | 96 | return OK; |
| 97 | } |
| 98 | void Coordinator::addDefaultPackagePath(const std::string& root, const std::string& path) { |
| 99 | addPackagePath(root, path, nullptr /* error */); |
| 100 | } |
| 101 | |
Steven Moreland | 6d3d3c8 | 2018-02-08 11:49:34 -0800 | [diff] [blame] | 102 | Formatter Coordinator::getFormatter(const FQName& fqName, Location location, |
| 103 | const std::string& fileName) const { |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 104 | if (location == Location::STANDARD_OUT) { |
| 105 | return Formatter(stdout); |
| 106 | } |
| 107 | |
Steven Moreland | 255c9a1 | 2018-02-26 13:10:27 -0800 | [diff] [blame] | 108 | std::string filepath; |
| 109 | status_t err = getFilepath(fqName, location, fileName, &filepath); |
| 110 | if (err != OK) { |
| 111 | return Formatter::invalid(); |
| 112 | } |
Steven Moreland | f78c44d | 2017-09-25 18:41:45 -0700 | [diff] [blame] | 113 | |
Steven Moreland | 37c57ee | 2017-09-25 19:08:56 -0700 | [diff] [blame] | 114 | onFileAccess(filepath, "w"); |
| 115 | |
Steven Moreland | f78c44d | 2017-09-25 18:41:45 -0700 | [diff] [blame] | 116 | if (!Coordinator::MakeParentHierarchy(filepath)) { |
| 117 | fprintf(stderr, "ERROR: could not make directories for %s.\n", filepath.c_str()); |
| 118 | return Formatter::invalid(); |
| 119 | } |
| 120 | |
| 121 | FILE* file = fopen(filepath.c_str(), "w"); |
| 122 | |
| 123 | if (file == nullptr) { |
| 124 | fprintf(stderr, "ERROR: could not open file %s: %d\n", filepath.c_str(), errno); |
| 125 | return Formatter::invalid(); |
| 126 | } |
| 127 | |
| 128 | return Formatter(file); |
| 129 | } |
| 130 | |
Steven Moreland | 255c9a1 | 2018-02-26 13:10:27 -0800 | [diff] [blame] | 131 | status_t Coordinator::getFilepath(const FQName& fqName, Location location, |
| 132 | const std::string& fileName, std::string* path) const { |
| 133 | status_t err; |
| 134 | std::string packagePath; |
| 135 | std::string packageRootPath; |
Steven Moreland | f78c44d | 2017-09-25 18:41:45 -0700 | [diff] [blame] | 136 | |
| 137 | switch (location) { |
| 138 | case Location::DIRECT: { /* nothing */ |
Steven Moreland | 255c9a1 | 2018-02-26 13:10:27 -0800 | [diff] [blame] | 139 | *path = mOutputPath + fileName; |
Steven Moreland | f78c44d | 2017-09-25 18:41:45 -0700 | [diff] [blame] | 140 | } break; |
| 141 | case Location::PACKAGE_ROOT: { |
Steven Moreland | 255c9a1 | 2018-02-26 13:10:27 -0800 | [diff] [blame] | 142 | err = getPackagePath(fqName, false /* relative */, false /* sanitized */, &packagePath); |
| 143 | if (err != OK) return err; |
| 144 | |
| 145 | *path = mOutputPath + packagePath + fileName; |
Steven Moreland | f78c44d | 2017-09-25 18:41:45 -0700 | [diff] [blame] | 146 | } break; |
| 147 | case Location::GEN_OUTPUT: { |
Steven Moreland | 255c9a1 | 2018-02-26 13:10:27 -0800 | [diff] [blame] | 148 | err = convertPackageRootToPath(fqName, &packageRootPath); |
| 149 | if (err != OK) return err; |
| 150 | err = getPackagePath(fqName, true /* relative */, false /* sanitized */, &packagePath); |
| 151 | if (err != OK) return err; |
| 152 | |
| 153 | *path = mOutputPath + packageRootPath + packagePath + fileName; |
Steven Moreland | f78c44d | 2017-09-25 18:41:45 -0700 | [diff] [blame] | 154 | } break; |
| 155 | case Location::GEN_SANITIZED: { |
Steven Moreland | 255c9a1 | 2018-02-26 13:10:27 -0800 | [diff] [blame] | 156 | err = convertPackageRootToPath(fqName, &packageRootPath); |
| 157 | if (err != OK) return err; |
| 158 | err = getPackagePath(fqName, true /* relative */, true /* sanitized */, &packagePath); |
| 159 | if (err != OK) return err; |
| 160 | |
| 161 | *path = mOutputPath + packageRootPath + packagePath + fileName; |
Steven Moreland | f78c44d | 2017-09-25 18:41:45 -0700 | [diff] [blame] | 162 | } break; |
| 163 | default: { CHECK(false) << "Invalid location: " << static_cast<size_t>(location); } |
| 164 | } |
| 165 | |
Steven Moreland | 255c9a1 | 2018-02-26 13:10:27 -0800 | [diff] [blame] | 166 | return OK; |
Steven Moreland | f78c44d | 2017-09-25 18:41:45 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Steven Moreland | 37c57ee | 2017-09-25 19:08:56 -0700 | [diff] [blame] | 169 | void Coordinator::onFileAccess(const std::string& path, const std::string& mode) const { |
Steven Moreland | 394af5c | 2018-02-09 14:41:46 -0800 | [diff] [blame] | 170 | if (mode == "r") { |
| 171 | // This is a global list. It's not cleared when a second fqname is processed for |
| 172 | // two reasons: |
| 173 | // 1). If there is a bug in hidl-gen, the dependencies on the first project from |
| 174 | // the second would be required to recover correctly when the bug is fixed. |
| 175 | // 2). This option is never used in Android builds. |
| 176 | mReadFiles.insert(StringHelper::LTrim(path, mRootPath)); |
| 177 | } |
| 178 | |
Steven Moreland | 37c57ee | 2017-09-25 19:08:56 -0700 | [diff] [blame] | 179 | if (!mVerbose) { |
| 180 | return; |
| 181 | } |
| 182 | |
| 183 | fprintf(stderr, |
| 184 | "VERBOSE: file access %s %s\n", path.c_str(), mode.c_str()); |
| 185 | } |
| 186 | |
Steven Moreland | 394af5c | 2018-02-09 14:41:46 -0800 | [diff] [blame] | 187 | status_t Coordinator::writeDepFile(const std::string& forFile) const { |
| 188 | // No dep file requested |
| 189 | if (mDepFile.empty()) return OK; |
| 190 | |
| 191 | onFileAccess(mDepFile, "w"); |
| 192 | |
| 193 | FILE* file = fopen(mDepFile.c_str(), "w"); |
| 194 | if (file == nullptr) { |
| 195 | fprintf(stderr, "ERROR: could not open dep file at %s.\n", mDepFile.c_str()); |
| 196 | return UNKNOWN_ERROR; |
| 197 | } |
| 198 | |
| 199 | Formatter out(file, 2 /* spacesPerIndent */); |
| 200 | out << StringHelper::LTrim(forFile, mOutputPath) << ": \\\n"; |
| 201 | out.indent([&] { |
| 202 | for (const std::string& file : mReadFiles) { |
| 203 | out << StringHelper::LTrim(file, mRootPath) << " \\\n"; |
| 204 | } |
| 205 | }); |
| 206 | return OK; |
| 207 | } |
Steven Moreland | 37c57ee | 2017-09-25 19:08:56 -0700 | [diff] [blame] | 208 | |
Steven Moreland | c59326e | 2017-06-20 15:19:30 -0700 | [diff] [blame] | 209 | AST* Coordinator::parse(const FQName& fqName, std::set<AST*>* parsedASTs, |
| 210 | Enforce enforcement) const { |
Steven Moreland | 71f0913 | 2018-02-20 12:24:30 -0800 | [diff] [blame] | 211 | AST* ret; |
| 212 | status_t err = parseOptional(fqName, &ret, parsedASTs, enforcement); |
| 213 | if (err != OK) CHECK(ret == nullptr); // internal consistency |
| 214 | |
| 215 | // only in a handful of places do we want to distinguish between |
| 216 | // a missing file and a bad AST. Everywhere else, we just want to |
| 217 | // throw an error if we expect an AST to be present but it is not. |
| 218 | return ret; |
| 219 | } |
| 220 | |
| 221 | status_t Coordinator::parseOptional(const FQName& fqName, AST** ast, std::set<AST*>* parsedASTs, |
| 222 | Enforce enforcement) const { |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 223 | CHECK(fqName.isFullyQualified()); |
| 224 | |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 225 | auto it = mCache.find(fqName); |
| 226 | if (it != mCache.end()) { |
Steven Moreland | 71f0913 | 2018-02-20 12:24:30 -0800 | [diff] [blame] | 227 | *ast = (*it).second; |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 228 | |
Steven Moreland | 71f0913 | 2018-02-20 12:24:30 -0800 | [diff] [blame] | 229 | if (*ast != nullptr && parsedASTs != nullptr) { |
| 230 | parsedASTs->insert(*ast); |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 231 | } |
| 232 | |
Steven Moreland | 71f0913 | 2018-02-20 12:24:30 -0800 | [diff] [blame] | 233 | if (*ast == nullptr) { |
| 234 | // circular import OR that AST has errors in it |
| 235 | return UNKNOWN_ERROR; |
| 236 | } |
| 237 | |
| 238 | return OK; |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 241 | // Add this to the cache immediately, so we can discover circular imports. |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 242 | mCache[fqName] = nullptr; |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 243 | |
| 244 | AST *typesAST = nullptr; |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 245 | |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 246 | if (fqName.name() != "types") { |
| 247 | // Any interface file implicitly imports its package's types.hal. |
Yifan Hong | fece6ec | 2017-01-12 17:04:04 -0800 | [diff] [blame] | 248 | FQName typesName = fqName.getTypesForPackage(); |
Steven Moreland | a407b7d | 2017-08-25 16:56:39 -0700 | [diff] [blame] | 249 | // Do not enforce on imports. Do not add imports' imports to this AST. |
Steven Moreland | 71f0913 | 2018-02-20 12:24:30 -0800 | [diff] [blame] | 250 | status_t err = parseOptional(typesName, &typesAST, nullptr, Enforce::NONE); |
| 251 | if (err != OK) return err; |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 252 | |
| 253 | // fall through. |
| 254 | } |
| 255 | |
Steven Moreland | 255c9a1 | 2018-02-26 13:10:27 -0800 | [diff] [blame] | 256 | std::string packagePath; |
| 257 | status_t err = |
| 258 | getPackagePath(fqName, false /* relative */, false /* sanitized */, &packagePath); |
| 259 | if (err != OK) return err; |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 260 | |
Steven Moreland | 255c9a1 | 2018-02-26 13:10:27 -0800 | [diff] [blame] | 261 | const std::string path = makeAbsolute(packagePath + fqName.name() + ".hal"); |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 262 | |
Steven Moreland | 71f0913 | 2018-02-20 12:24:30 -0800 | [diff] [blame] | 263 | *ast = new AST(this, &Hash::getHash(path)); |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 264 | |
Yi Kong | 56758da | 2018-07-24 16:21:37 -0700 | [diff] [blame] | 265 | if (typesAST != nullptr) { |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 266 | // If types.hal for this AST's package existed, make it's defined |
| 267 | // types available to the (about to be parsed) AST right away. |
Steven Moreland | 71f0913 | 2018-02-20 12:24:30 -0800 | [diff] [blame] | 268 | (*ast)->addImportedAST(typesAST); |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Steven Moreland | 161afb2 | 2018-02-20 14:22:44 -0800 | [diff] [blame] | 271 | std::unique_ptr<FILE, std::function<void(FILE*)>> file(fopen(path.c_str(), "rb"), fclose); |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 272 | |
Steven Moreland | 71f0913 | 2018-02-20 12:24:30 -0800 | [diff] [blame] | 273 | if (file == nullptr) { |
| 274 | mCache.erase(fqName); // nullptr in cache is used to find circular imports |
| 275 | delete *ast; |
| 276 | *ast = nullptr; |
| 277 | return OK; // File does not exist, nullptr AST* == file doesn't exist. |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 278 | } |
| 279 | |
Steven Moreland | 3d84699 | 2018-02-14 17:24:57 -0800 | [diff] [blame] | 280 | onFileAccess(path, "r"); |
| 281 | |
Steven Moreland | 71f0913 | 2018-02-20 12:24:30 -0800 | [diff] [blame] | 282 | // parse file takes ownership of file |
Steven Moreland | 161afb2 | 2018-02-20 14:22:44 -0800 | [diff] [blame] | 283 | if (parseFile(*ast, std::move(file)) != OK || (*ast)->postParse() != OK) { |
Steven Moreland | 71f0913 | 2018-02-20 12:24:30 -0800 | [diff] [blame] | 284 | delete *ast; |
| 285 | *ast = nullptr; |
| 286 | return UNKNOWN_ERROR; |
| 287 | } |
| 288 | |
Steven Moreland | 71f0913 | 2018-02-20 12:24:30 -0800 | [diff] [blame] | 289 | if ((*ast)->package().package() != fqName.package() || |
| 290 | (*ast)->package().version() != fqName.version()) { |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 291 | fprintf(stderr, |
| 292 | "ERROR: File at '%s' does not match expected package and/or " |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 293 | "version.\n", |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 294 | path.c_str()); |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 295 | |
| 296 | err = UNKNOWN_ERROR; |
| 297 | } else { |
Steven Moreland | 71f0913 | 2018-02-20 12:24:30 -0800 | [diff] [blame] | 298 | if ((*ast)->isInterface()) { |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 299 | if (fqName.name() == "types") { |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 300 | fprintf(stderr, |
| 301 | "ERROR: File at '%s' declares an interface '%s' " |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 302 | "instead of the expected types common to the package.\n", |
Steven Moreland | 71f0913 | 2018-02-20 12:24:30 -0800 | [diff] [blame] | 303 | path.c_str(), (*ast)->getInterface()->localName().c_str()); |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 304 | |
| 305 | err = UNKNOWN_ERROR; |
Steven Moreland | 71f0913 | 2018-02-20 12:24:30 -0800 | [diff] [blame] | 306 | } else if ((*ast)->getInterface()->localName() != fqName.name()) { |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 307 | fprintf(stderr, |
| 308 | "ERROR: File at '%s' does not declare interface type " |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 309 | "'%s'.\n", |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 310 | path.c_str(), |
| 311 | fqName.name().c_str()); |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 312 | |
| 313 | err = UNKNOWN_ERROR; |
| 314 | } |
| 315 | } else if (fqName.name() != "types") { |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 316 | fprintf(stderr, |
| 317 | "ERROR: File at '%s' declares types rather than the " |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 318 | "expected interface type '%s'.\n", |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 319 | path.c_str(), |
| 320 | fqName.name().c_str()); |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 321 | |
| 322 | err = UNKNOWN_ERROR; |
Steven Moreland | b47a262 | 2018-07-11 09:04:25 -0700 | [diff] [blame] | 323 | } else if ((*ast)->definesInterfaces()) { |
Andreas Huber | 7c5ddfb | 2016-09-29 13:45:22 -0700 | [diff] [blame] | 324 | fprintf(stderr, |
| 325 | "ERROR: types.hal file at '%s' declares at least one " |
| 326 | "interface type.\n", |
| 327 | path.c_str()); |
| 328 | |
| 329 | err = UNKNOWN_ERROR; |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | |
| 333 | if (err != OK) { |
Steven Moreland | 71f0913 | 2018-02-20 12:24:30 -0800 | [diff] [blame] | 334 | delete *ast; |
| 335 | *ast = nullptr; |
| 336 | return err; |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 337 | } |
| 338 | |
Steven Moreland | 71f0913 | 2018-02-20 12:24:30 -0800 | [diff] [blame] | 339 | if (parsedASTs != nullptr) { |
| 340 | parsedASTs->insert(*ast); |
| 341 | } |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 342 | |
Yifan Hong | fe07bff | 2017-02-15 14:55:48 -0800 | [diff] [blame] | 343 | // put it into the cache now, so that enforceRestrictionsOnPackage can |
| 344 | // parse fqName. |
Steven Moreland | 71f0913 | 2018-02-20 12:24:30 -0800 | [diff] [blame] | 345 | mCache[fqName] = *ast; |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 346 | |
Steven Moreland | c59326e | 2017-06-20 15:19:30 -0700 | [diff] [blame] | 347 | // For each .hal file that hidl-gen parses, the whole package will be checked. |
| 348 | err = enforceRestrictionsOnPackage(fqName, enforcement); |
| 349 | if (err != OK) { |
| 350 | mCache[fqName] = nullptr; |
Steven Moreland | 71f0913 | 2018-02-20 12:24:30 -0800 | [diff] [blame] | 351 | delete *ast; |
| 352 | *ast = nullptr; |
| 353 | return err; |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Steven Moreland | 71f0913 | 2018-02-20 12:24:30 -0800 | [diff] [blame] | 356 | return OK; |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 357 | } |
| 358 | |
Steven Moreland | 255c9a1 | 2018-02-26 13:10:27 -0800 | [diff] [blame] | 359 | const Coordinator::PackageRoot* Coordinator::findPackageRoot(const FQName& fqName) const { |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 360 | CHECK(!fqName.package().empty()); |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 361 | |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 362 | // Find the right package prefix and path for this FQName. For |
| 363 | // example, if FQName is "android.hardware.nfc@1.0::INfc", and the |
| 364 | // prefix:root is set to [ "android.hardware:hardware/interfaces", |
| 365 | // "vendor.qcom.hardware:vendor/qcom"], then we will identify the |
| 366 | // prefix "android.hardware" and the package root |
| 367 | // "hardware/interfaces". |
| 368 | |
Steven Moreland | 62709d7 | 2017-01-18 10:14:51 -0800 | [diff] [blame] | 369 | auto ret = mPackageRoots.end(); |
Steven Moreland | 47792c4 | 2017-09-20 10:03:20 -0700 | [diff] [blame] | 370 | for (auto it = mPackageRoots.begin(); it != mPackageRoots.end(); it++) { |
| 371 | if (!fqName.inPackage(it->root.package())) { |
Steven Moreland | 62709d7 | 2017-01-18 10:14:51 -0800 | [diff] [blame] | 372 | continue; |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 373 | } |
Steven Moreland | 62709d7 | 2017-01-18 10:14:51 -0800 | [diff] [blame] | 374 | |
Steven Moreland | 255c9a1 | 2018-02-26 13:10:27 -0800 | [diff] [blame] | 375 | if (ret != mPackageRoots.end()) { |
| 376 | std::cerr << "ERROR: Multiple package roots found for " << fqName.string() << " (" |
| 377 | << it->root.package() << " and " << ret->root.package() << ")\n"; |
| 378 | return nullptr; |
| 379 | } |
Steven Moreland | 62709d7 | 2017-01-18 10:14:51 -0800 | [diff] [blame] | 380 | |
| 381 | ret = it; |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 382 | } |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 383 | |
Steven Moreland | 255c9a1 | 2018-02-26 13:10:27 -0800 | [diff] [blame] | 384 | if (ret == mPackageRoots.end()) { |
| 385 | std::cerr << "ERROR: Package root not specified for " << fqName.string() << "\n"; |
| 386 | return nullptr; |
| 387 | } |
| 388 | |
| 389 | return &(*ret); |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 390 | } |
| 391 | |
Steven Moreland | 7a36804 | 2017-08-28 12:47:24 -0700 | [diff] [blame] | 392 | std::string Coordinator::makeAbsolute(const std::string& path) const { |
| 393 | if (StringHelper::StartsWith(path, "/") || mRootPath.empty()) { |
| 394 | return path; |
Steven Moreland | b90d327 | 2017-05-26 10:02:39 -0700 | [diff] [blame] | 395 | } |
| 396 | |
Steven Moreland | 47792c4 | 2017-09-20 10:03:20 -0700 | [diff] [blame] | 397 | return mRootPath + path; |
Steven Moreland | b90d327 | 2017-05-26 10:02:39 -0700 | [diff] [blame] | 398 | } |
| 399 | |
Steven Moreland | 255c9a1 | 2018-02-26 13:10:27 -0800 | [diff] [blame] | 400 | status_t Coordinator::getPackageRoot(const FQName& fqName, std::string* root) const { |
| 401 | const PackageRoot* packageRoot = findPackageRoot(fqName); |
| 402 | if (root == nullptr) { |
| 403 | return UNKNOWN_ERROR; |
| 404 | } |
| 405 | *root = packageRoot->root.package(); |
| 406 | return OK; |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 407 | } |
| 408 | |
Steven Moreland | 255c9a1 | 2018-02-26 13:10:27 -0800 | [diff] [blame] | 409 | status_t Coordinator::getPackageRootPath(const FQName& fqName, std::string* path) const { |
| 410 | const PackageRoot* packageRoot = findPackageRoot(fqName); |
| 411 | if (packageRoot == nullptr) { |
| 412 | return UNKNOWN_ERROR; |
| 413 | } |
| 414 | *path = packageRoot->path; |
| 415 | return OK; |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 416 | } |
| 417 | |
Steven Moreland | 255c9a1 | 2018-02-26 13:10:27 -0800 | [diff] [blame] | 418 | status_t Coordinator::getPackagePath(const FQName& fqName, bool relative, bool sanitized, |
| 419 | std::string* path) const { |
| 420 | const PackageRoot* packageRoot = findPackageRoot(fqName); |
| 421 | if (packageRoot == nullptr) return UNKNOWN_ERROR; |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 422 | |
Steven Moreland | a938738 | 2017-09-18 15:09:36 -0700 | [diff] [blame] | 423 | // Given FQName of "android.hardware.nfc.test@1.0::IFoo" and a prefix |
Steven Moreland | 8f52c8a | 2017-09-19 08:45:53 -0700 | [diff] [blame] | 424 | // "android.hardware", the suffix is "nfc.test". |
Steven Moreland | 255c9a1 | 2018-02-26 13:10:27 -0800 | [diff] [blame] | 425 | std::string suffix = StringHelper::LTrim(fqName.package(), packageRoot->root.package()); |
Steven Moreland | 80ba174 | 2017-09-26 09:49:18 -0700 | [diff] [blame] | 426 | suffix = StringHelper::LTrim(suffix, "."); |
| 427 | |
Steven Moreland | a938738 | 2017-09-18 15:09:36 -0700 | [diff] [blame] | 428 | std::vector<std::string> suffixComponents; |
| 429 | StringHelper::SplitString(suffix, '.', &suffixComponents); |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 430 | |
Steven Moreland | a938738 | 2017-09-18 15:09:36 -0700 | [diff] [blame] | 431 | std::vector<std::string> components; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 432 | if (!relative) { |
Steven Moreland | 255c9a1 | 2018-02-26 13:10:27 -0800 | [diff] [blame] | 433 | components.push_back(StringHelper::RTrimAll(packageRoot->path, "/")); |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 434 | } |
Steven Moreland | a938738 | 2017-09-18 15:09:36 -0700 | [diff] [blame] | 435 | components.insert(components.end(), suffixComponents.begin(), suffixComponents.end()); |
| 436 | components.push_back(sanitized ? fqName.sanitizedVersion() : fqName.version()); |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 437 | |
Steven Moreland | 255c9a1 | 2018-02-26 13:10:27 -0800 | [diff] [blame] | 438 | *path = StringHelper::JoinStrings(components, "/") + "/"; |
| 439 | return OK; |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 440 | } |
| 441 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 442 | status_t Coordinator::getPackageInterfaceFiles( |
| 443 | const FQName &package, |
| 444 | std::vector<std::string> *fileNames) const { |
Steven Moreland | e7d3210 | 2018-12-03 17:03:36 -0800 | [diff] [blame] | 445 | if (fileNames) fileNames->clear(); |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 446 | |
Steven Moreland | 255c9a1 | 2018-02-26 13:10:27 -0800 | [diff] [blame] | 447 | std::string packagePath; |
| 448 | status_t err = |
| 449 | getPackagePath(package, false /* relative */, false /* sanitized */, &packagePath); |
| 450 | if (err != OK) return err; |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 451 | |
Steven Moreland | 255c9a1 | 2018-02-26 13:10:27 -0800 | [diff] [blame] | 452 | const std::string path = makeAbsolute(packagePath); |
Steven Moreland | e7d3210 | 2018-12-03 17:03:36 -0800 | [diff] [blame] | 453 | std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(path.c_str()), closedir); |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 454 | |
Yi Kong | 56758da | 2018-07-24 16:21:37 -0700 | [diff] [blame] | 455 | if (dir == nullptr) { |
Steven Moreland | 255c9a1 | 2018-02-26 13:10:27 -0800 | [diff] [blame] | 456 | fprintf(stderr, "ERROR: Could not open package path %s for package %s:\n%s\n", |
| 457 | packagePath.c_str(), package.string().c_str(), path.c_str()); |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 458 | return -errno; |
| 459 | } |
| 460 | |
Steven Moreland | e7d3210 | 2018-12-03 17:03:36 -0800 | [diff] [blame] | 461 | if (fileNames == nullptr) { |
| 462 | return OK; |
| 463 | } |
| 464 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 465 | struct dirent *ent; |
Steven Moreland | e7d3210 | 2018-12-03 17:03:36 -0800 | [diff] [blame] | 466 | while ((ent = readdir(dir.get())) != nullptr) { |
Iris Chang | 1a92c58 | 2018-03-27 18:40:24 +0800 | [diff] [blame] | 467 | // filesystems may not support d_type and return DT_UNKNOWN |
| 468 | if (ent->d_type == DT_UNKNOWN) { |
| 469 | struct stat sb; |
| 470 | const auto filename = packagePath + std::string(ent->d_name); |
| 471 | if (stat(filename.c_str(), &sb) == -1) { |
| 472 | fprintf(stderr, "ERROR: Could not stat %s\n", filename.c_str()); |
| 473 | return -errno; |
| 474 | } |
| 475 | if ((sb.st_mode & S_IFMT) != S_IFREG) { |
| 476 | continue; |
| 477 | } |
| 478 | } else if (ent->d_type != DT_REG) { |
| 479 | continue; |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 480 | } |
| 481 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 482 | const auto suffix = ".hal"; |
| 483 | const auto suffix_len = std::strlen(suffix); |
| 484 | const auto d_namelen = strlen(ent->d_name); |
Andreas Huber | e61e3f7 | 2016-08-03 10:22:03 -0700 | [diff] [blame] | 485 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 486 | if (d_namelen < suffix_len |
| 487 | || strcmp(ent->d_name + d_namelen - suffix_len, suffix)) { |
| 488 | continue; |
Andreas Huber | e61e3f7 | 2016-08-03 10:22:03 -0700 | [diff] [blame] | 489 | } |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 490 | |
| 491 | fileNames->push_back(std::string(ent->d_name, d_namelen - suffix_len)); |
| 492 | } |
| 493 | |
Iliyan Malchev | 5e8bcfa | 2016-09-09 16:19:57 -0700 | [diff] [blame] | 494 | std::sort(fileNames->begin(), fileNames->end(), |
| 495 | [](const std::string& lhs, const std::string& rhs) -> bool { |
| 496 | if (lhs == "types") { |
| 497 | return true; |
| 498 | } |
| 499 | if (rhs == "types") { |
| 500 | return false; |
| 501 | } |
| 502 | return lhs < rhs; |
| 503 | }); |
| 504 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 505 | return OK; |
| 506 | } |
| 507 | |
Steven Moreland | aa18683 | 2016-09-26 13:51:43 -0700 | [diff] [blame] | 508 | status_t Coordinator::appendPackageInterfacesToVector( |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 509 | const FQName &package, |
| 510 | std::vector<FQName> *packageInterfaces) const { |
| 511 | packageInterfaces->clear(); |
| 512 | |
| 513 | std::vector<std::string> fileNames; |
| 514 | status_t err = getPackageInterfaceFiles(package, &fileNames); |
| 515 | |
| 516 | if (err != OK) { |
| 517 | return err; |
| 518 | } |
| 519 | |
| 520 | for (const auto &fileName : fileNames) { |
Steven Moreland | e1b157e | 2018-03-06 14:18:32 -0800 | [diff] [blame] | 521 | FQName subFQName(package.package(), package.version(), fileName); |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 522 | packageInterfaces->push_back(subFQName); |
Andreas Huber | e61e3f7 | 2016-08-03 10:22:03 -0700 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | return OK; |
| 526 | } |
| 527 | |
Steven Moreland | 255c9a1 | 2018-02-26 13:10:27 -0800 | [diff] [blame] | 528 | status_t Coordinator::convertPackageRootToPath(const FQName& fqName, std::string* path) const { |
| 529 | std::string packageRoot; |
| 530 | status_t err = getPackageRoot(fqName, &packageRoot); |
| 531 | if (err != OK) return err; |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 532 | |
| 533 | if (*(packageRoot.end()--) != '.') { |
| 534 | packageRoot += '.'; |
| 535 | } |
| 536 | |
| 537 | std::replace(packageRoot.begin(), packageRoot.end(), '.', '/'); |
| 538 | |
Steven Moreland | 255c9a1 | 2018-02-26 13:10:27 -0800 | [diff] [blame] | 539 | *path = packageRoot; // now converted to a path |
| 540 | return OK; |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 541 | } |
| 542 | |
Steven Moreland | 87b26d7 | 2017-10-11 09:35:42 -0700 | [diff] [blame] | 543 | status_t Coordinator::isTypesOnlyPackage(const FQName& package, bool* result) const { |
| 544 | std::vector<FQName> packageInterfaces; |
| 545 | |
| 546 | status_t err = appendPackageInterfacesToVector(package, &packageInterfaces); |
| 547 | |
| 548 | if (err != OK) { |
| 549 | *result = false; |
| 550 | return err; |
| 551 | } |
| 552 | |
| 553 | *result = packageInterfaces.size() == 1 && packageInterfaces[0].name() == "types"; |
| 554 | return OK; |
| 555 | } |
| 556 | |
Steven Moreland | 40b8635 | 2018-02-01 16:03:30 -0800 | [diff] [blame] | 557 | status_t Coordinator::addUnreferencedTypes(const std::vector<FQName>& packageInterfaces, |
| 558 | std::set<FQName>* unreferencedDefinitions, |
Steven Moreland | b65e5d9 | 2018-02-08 12:44:51 -0800 | [diff] [blame] | 559 | std::set<FQName>* unreferencedImports) const { |
Steven Moreland | 40b8635 | 2018-02-01 16:03:30 -0800 | [diff] [blame] | 560 | CHECK(unreferencedDefinitions != nullptr); |
| 561 | CHECK(unreferencedImports != nullptr); |
| 562 | |
| 563 | std::set<FQName> packageDefinedTypes; |
| 564 | std::set<FQName> packageReferencedTypes; |
| 565 | std::set<FQName> packageImportedTypes; |
| 566 | std::set<FQName> typesDefinedTypes; // only types.hal types |
| 567 | |
| 568 | for (const auto& fqName : packageInterfaces) { |
| 569 | AST* ast = parse(fqName); |
| 570 | if (!ast) { |
| 571 | std::cerr << "ERROR: Could not parse " << fqName.string() << ". Aborting." << std::endl; |
| 572 | |
| 573 | return UNKNOWN_ERROR; |
| 574 | } |
| 575 | |
| 576 | ast->addDefinedTypes(&packageDefinedTypes); |
| 577 | ast->addReferencedTypes(&packageReferencedTypes); |
| 578 | ast->getAllImportedNamesGranular(&packageImportedTypes); |
| 579 | |
| 580 | if (fqName.name() == "types") { |
| 581 | ast->addDefinedTypes(&typesDefinedTypes); |
| 582 | } |
| 583 | } |
| 584 | |
| 585 | #if 0 |
| 586 | for (const auto &fqName : packageDefinedTypes) { |
| 587 | std::cout << "VERBOSE: DEFINED " << fqName.string() << std::endl; |
| 588 | } |
| 589 | |
| 590 | for (const auto &fqName : packageImportedTypes) { |
| 591 | std::cout << "VERBOSE: IMPORTED " << fqName.string() << std::endl; |
| 592 | } |
| 593 | |
| 594 | for (const auto &fqName : packageReferencedTypes) { |
| 595 | std::cout << "VERBOSE: REFERENCED " << fqName.string() << std::endl; |
| 596 | } |
| 597 | |
| 598 | for (const auto &fqName : typesDefinedTypes) { |
| 599 | std::cout << "VERBOSE: DEFINED in types.hal " << fqName.string() << std::endl; |
| 600 | } |
| 601 | #endif |
| 602 | |
| 603 | for (const auto& fqName : packageReferencedTypes) { |
| 604 | packageDefinedTypes.erase(fqName); |
| 605 | packageImportedTypes.erase(fqName); |
| 606 | } |
| 607 | |
| 608 | // A package implicitly imports its own types.hal, only track them in one set. |
| 609 | for (const auto& fqName : typesDefinedTypes) { |
| 610 | packageImportedTypes.erase(fqName); |
| 611 | } |
| 612 | |
| 613 | // defined but not referenced |
| 614 | unreferencedDefinitions->insert(packageDefinedTypes.begin(), packageDefinedTypes.end()); |
| 615 | // imported but not referenced |
| 616 | unreferencedImports->insert(packageImportedTypes.begin(), packageImportedTypes.end()); |
| 617 | return OK; |
| 618 | } |
| 619 | |
Steven Moreland | c59326e | 2017-06-20 15:19:30 -0700 | [diff] [blame] | 620 | status_t Coordinator::enforceRestrictionsOnPackage(const FQName& fqName, |
| 621 | Enforce enforcement) const { |
| 622 | CHECK(enforcement == Enforce::FULL || enforcement == Enforce::NO_HASH || |
| 623 | enforcement == Enforce::NONE); |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 624 | |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 625 | // need fqName to be something like android.hardware.foo@1.0. |
| 626 | // name and valueName is ignored. |
| 627 | if (fqName.package().empty() || fqName.version().empty()) { |
Steven Moreland | cbff561 | 2017-10-11 17:01:54 -0700 | [diff] [blame] | 628 | std::cerr << "ERROR: Cannot enforce restrictions on package " << fqName.string() |
| 629 | << ": package or version is missing." << std::endl; |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 630 | return BAD_VALUE; |
| 631 | } |
Steven Moreland | c59326e | 2017-06-20 15:19:30 -0700 | [diff] [blame] | 632 | |
| 633 | if (enforcement == Enforce::NONE) { |
| 634 | return OK; |
| 635 | } |
| 636 | |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 637 | FQName package = fqName.getPackageAndVersion(); |
| 638 | // look up cache. |
| 639 | if (mPackagesEnforced.find(package) != mPackagesEnforced.end()) { |
| 640 | return OK; |
| 641 | } |
| 642 | |
| 643 | // enforce all rules. |
Steven Moreland | 218625a | 2017-04-18 22:31:50 -0700 | [diff] [blame] | 644 | status_t err; |
| 645 | |
Steven Moreland | 51b02f4 | 2018-03-14 15:43:29 -0700 | [diff] [blame] | 646 | err = enforceMinorVersionUprevs(package, enforcement); |
Steven Moreland | 218625a | 2017-04-18 22:31:50 -0700 | [diff] [blame] | 647 | if (err != OK) { |
| 648 | return err; |
| 649 | } |
| 650 | |
Steven Moreland | c59326e | 2017-06-20 15:19:30 -0700 | [diff] [blame] | 651 | if (enforcement != Enforce::NO_HASH) { |
| 652 | err = enforceHashes(package); |
| 653 | if (err != OK) { |
| 654 | return err; |
| 655 | } |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | // cache it so that it won't need to be enforced again. |
| 659 | mPackagesEnforced.insert(package); |
| 660 | return OK; |
| 661 | } |
| 662 | |
Steven Moreland | 51b02f4 | 2018-03-14 15:43:29 -0700 | [diff] [blame] | 663 | status_t Coordinator::enforceMinorVersionUprevs(const FQName& currentPackage, |
| 664 | Enforce enforcement) const { |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 665 | if(!currentPackage.hasVersion()) { |
Steven Moreland | cbff561 | 2017-10-11 17:01:54 -0700 | [diff] [blame] | 666 | std::cerr << "ERROR: Cannot enforce minor version uprevs for " << currentPackage.string() |
| 667 | << ": missing version." << std::endl; |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 668 | return UNKNOWN_ERROR; |
| 669 | } |
| 670 | |
| 671 | if (currentPackage.getPackageMinorVersion() == 0) { |
| 672 | return OK; // ignore for @x.0 |
| 673 | } |
| 674 | |
| 675 | bool hasPrevPackage = false; |
Steven Moreland | ad79f56 | 2017-05-11 14:36:54 -0700 | [diff] [blame] | 676 | FQName prevPackage = currentPackage; |
| 677 | while (prevPackage.getPackageMinorVersion() > 0) { |
| 678 | prevPackage = prevPackage.downRev(); |
Steven Moreland | 255c9a1 | 2018-02-26 13:10:27 -0800 | [diff] [blame] | 679 | |
| 680 | std::string prevPackagePath; |
| 681 | status_t err = getPackagePath(prevPackage, false /* relative */, false /* sanitized */, |
| 682 | &prevPackagePath); |
| 683 | if (err != OK) return err; |
| 684 | |
| 685 | if (existdir(makeAbsolute(prevPackagePath).c_str())) { |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 686 | hasPrevPackage = true; |
| 687 | break; |
| 688 | } |
| 689 | } |
| 690 | if (!hasPrevPackage) { |
| 691 | // no @x.z, where z < y, exist. |
| 692 | return OK; |
| 693 | } |
| 694 | |
Steven Moreland | ad79f56 | 2017-05-11 14:36:54 -0700 | [diff] [blame] | 695 | if (prevPackage != currentPackage.downRev()) { |
Steven Moreland | cbff561 | 2017-10-11 17:01:54 -0700 | [diff] [blame] | 696 | std::cerr << "ERROR: Cannot enforce minor version uprevs for " << currentPackage.string() |
| 697 | << ": Found package " << prevPackage.string() << " but missing " |
| 698 | << currentPackage.downRev().string() << "; you cannot skip a minor version." |
| 699 | << std::endl; |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 700 | return UNKNOWN_ERROR; |
| 701 | } |
| 702 | |
Steven Moreland | 87b26d7 | 2017-10-11 09:35:42 -0700 | [diff] [blame] | 703 | bool prevIsTypesOnly; |
| 704 | status_t err = isTypesOnlyPackage(prevPackage, &prevIsTypesOnly); |
| 705 | if (err != OK) return err; |
| 706 | |
| 707 | if (prevIsTypesOnly) { |
| 708 | // A types only package can be extended in any way. |
| 709 | return OK; |
| 710 | } |
| 711 | |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 712 | std::vector<FQName> packageInterfaces; |
| 713 | err = appendPackageInterfacesToVector(currentPackage, &packageInterfaces); |
| 714 | if (err != OK) { |
| 715 | return err; |
| 716 | } |
| 717 | |
Yifan Hong | fe07bff | 2017-02-15 14:55:48 -0800 | [diff] [blame] | 718 | bool extendedInterface = false; |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 719 | for (const FQName ¤tFQName : packageInterfaces) { |
| 720 | if (currentFQName.name() == "types") { |
| 721 | continue; // ignore types.hal |
| 722 | } |
Steven Moreland | ad79f56 | 2017-05-11 14:36:54 -0700 | [diff] [blame] | 723 | |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 724 | const Interface *iface = nullptr; |
Steven Moreland | 51b02f4 | 2018-03-14 15:43:29 -0700 | [diff] [blame] | 725 | AST* currentAST = parse(currentFQName, nullptr /* parsedASTs */, enforcement); |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 726 | if (currentAST != nullptr) { |
| 727 | iface = currentAST->getInterface(); |
| 728 | } |
| 729 | if (iface == nullptr) { |
Yifan Hong | fe07bff | 2017-02-15 14:55:48 -0800 | [diff] [blame] | 730 | if (currentAST == nullptr) { |
Steven Moreland | cbff561 | 2017-10-11 17:01:54 -0700 | [diff] [blame] | 731 | std::cerr << "WARNING: Skipping " << currentFQName.string() |
| 732 | << " because it could not be found or parsed" |
| 733 | << " or " << currentPackage.string() << " doesn't pass all requirements." |
| 734 | << std::endl; |
Yifan Hong | fe07bff | 2017-02-15 14:55:48 -0800 | [diff] [blame] | 735 | } else { |
Steven Moreland | cbff561 | 2017-10-11 17:01:54 -0700 | [diff] [blame] | 736 | std::cerr << "WARNING: Skipping " << currentFQName.string() |
| 737 | << " because the file might contain more than one interface." |
| 738 | << std::endl; |
Yifan Hong | fe07bff | 2017-02-15 14:55:48 -0800 | [diff] [blame] | 739 | } |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 740 | continue; |
| 741 | } |
| 742 | |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 743 | if (iface->superType() == nullptr) { |
Steven Moreland | ad79f56 | 2017-05-11 14:36:54 -0700 | [diff] [blame] | 744 | CHECK(iface->isIBase()); |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 745 | continue; |
| 746 | } |
| 747 | |
Steven Moreland | ad79f56 | 2017-05-11 14:36:54 -0700 | [diff] [blame] | 748 | // Assume that currentFQName == android.hardware.foo@2.2::IFoo. |
| 749 | FQName lastFQName(prevPackage.package(), prevPackage.version(), |
| 750 | currentFQName.name()); |
| 751 | AST *lastAST = parse(lastFQName); |
| 752 | |
| 753 | for (; lastFQName.getPackageMinorVersion() > 0 && |
| 754 | (lastAST == nullptr || lastAST->getInterface() == nullptr) |
| 755 | ; lastFQName = lastFQName.downRev(), lastAST = parse(lastFQName)) { |
| 756 | // nothing |
| 757 | } |
| 758 | |
| 759 | // Then lastFQName == android.hardware.foo@2.1::IFoo or |
| 760 | // lastFQName == android.hardware.foo@2.0::IFoo if 2.1 doesn't exist. |
| 761 | |
| 762 | bool lastFQNameExists = lastAST != nullptr && lastAST->getInterface() != nullptr; |
| 763 | |
Steven Moreland | 8775715 | 2017-11-09 08:57:59 -0800 | [diff] [blame] | 764 | if (!lastFQNameExists) { |
| 765 | continue; |
| 766 | } |
| 767 | |
| 768 | if (iface->superType()->fqName() != lastFQName) { |
Steven Moreland | cbff561 | 2017-10-11 17:01:54 -0700 | [diff] [blame] | 769 | std::cerr << "ERROR: Cannot enforce minor version uprevs for " |
| 770 | << currentPackage.string() << ": " << iface->fqName().string() << " extends " |
| 771 | << iface->superType()->fqName().string() |
| 772 | << ", which is not allowed. It must extend " << lastFQName.string() |
| 773 | << std::endl; |
Steven Moreland | ad79f56 | 2017-05-11 14:36:54 -0700 | [diff] [blame] | 774 | return UNKNOWN_ERROR; |
| 775 | } |
| 776 | |
| 777 | // at least one interface must extend the previous version |
Steven Moreland | 8775715 | 2017-11-09 08:57:59 -0800 | [diff] [blame] | 778 | // @2.0::IFoo does not work. It must be @2.1::IFoo for at least one interface. |
Steven Moreland | ad79f56 | 2017-05-11 14:36:54 -0700 | [diff] [blame] | 779 | if (lastFQName.getPackageAndVersion() == prevPackage.getPackageAndVersion()) { |
| 780 | extendedInterface = true; |
| 781 | } |
| 782 | |
Steven Moreland | cbff561 | 2017-10-11 17:01:54 -0700 | [diff] [blame] | 783 | if (mVerbose) { |
| 784 | std::cout << "VERBOSE: EnforceMinorVersionUprevs: " << currentFQName.string() |
| 785 | << " passes." << std::endl; |
| 786 | } |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 787 | } |
| 788 | |
Yifan Hong | fe07bff | 2017-02-15 14:55:48 -0800 | [diff] [blame] | 789 | if (!extendedInterface) { |
| 790 | // No interface extends the interface with the same name in @x.(y-1). |
Steven Moreland | cbff561 | 2017-10-11 17:01:54 -0700 | [diff] [blame] | 791 | std::cerr << "ERROR: " << currentPackage.string() |
| 792 | << " doesn't pass minor version uprev requirement. " |
| 793 | << "Requires at least one interface to extend an interface with the same name " |
| 794 | << "from " << prevPackage.string() << "." << std::endl; |
Yifan Hong | fe07bff | 2017-02-15 14:55:48 -0800 | [diff] [blame] | 795 | return UNKNOWN_ERROR; |
| 796 | } |
| 797 | |
| 798 | return OK; |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 799 | } |
| 800 | |
Steven Moreland | 2d3ff5c | 2018-01-18 17:22:45 -0800 | [diff] [blame] | 801 | Coordinator::HashStatus Coordinator::checkHash(const FQName& fqName) const { |
| 802 | AST* ast = parse(fqName); |
| 803 | if (ast == nullptr) return HashStatus::ERROR; |
| 804 | |
Steven Moreland | 255c9a1 | 2018-02-26 13:10:27 -0800 | [diff] [blame] | 805 | std::string rootPath; |
| 806 | status_t err = getPackageRootPath(fqName, &rootPath); |
| 807 | if (err != OK) return HashStatus::ERROR; |
| 808 | |
| 809 | std::string hashPath = makeAbsolute(rootPath) + "/current.txt"; |
Steven Moreland | 2d3ff5c | 2018-01-18 17:22:45 -0800 | [diff] [blame] | 810 | std::string error; |
Steven Moreland | 566b0e9 | 2018-02-16 13:51:33 -0800 | [diff] [blame] | 811 | bool fileExists; |
| 812 | std::vector<std::string> frozen = |
| 813 | Hash::lookupHash(hashPath, fqName.string(), &error, &fileExists); |
| 814 | if (fileExists) onFileAccess(hashPath, "r"); |
Steven Moreland | 2d3ff5c | 2018-01-18 17:22:45 -0800 | [diff] [blame] | 815 | |
| 816 | if (error.size() > 0) { |
| 817 | std::cerr << "ERROR: " << error << std::endl; |
| 818 | return HashStatus::ERROR; |
| 819 | } |
| 820 | |
| 821 | // hash not defined, interface not frozen |
| 822 | if (frozen.size() == 0) { |
| 823 | // This ensures that it can be detected. |
| 824 | Hash::clearHash(ast->getFilename()); |
| 825 | |
| 826 | return HashStatus::UNFROZEN; |
| 827 | } |
| 828 | |
Steven Moreland | 04dea8d | 2018-02-06 13:11:24 -0800 | [diff] [blame] | 829 | std::string currentHash = ast->getFileHash()->hexString(); |
Steven Moreland | 2d3ff5c | 2018-01-18 17:22:45 -0800 | [diff] [blame] | 830 | |
| 831 | if (std::find(frozen.begin(), frozen.end(), currentHash) == frozen.end()) { |
| 832 | std::cerr << "ERROR: " << fqName.string() << " has hash " << currentHash |
| 833 | << " which does not match hash on record. This interface has " |
| 834 | << "been frozen. Do not change it!" << std::endl; |
| 835 | return HashStatus::CHANGED; |
| 836 | } |
| 837 | |
| 838 | return HashStatus::FROZEN; |
| 839 | } |
| 840 | |
| 841 | status_t Coordinator::getUnfrozenDependencies(const FQName& fqName, |
| 842 | std::set<FQName>* result) const { |
| 843 | CHECK(result != nullptr); |
| 844 | |
| 845 | AST* ast = parse(fqName); |
| 846 | if (ast == nullptr) return UNKNOWN_ERROR; |
| 847 | |
| 848 | std::set<FQName> imported; |
| 849 | ast->getImportedPackages(&imported); |
| 850 | |
| 851 | // no circular dependency is already guaranteed by parsing |
| 852 | // indirect dependencies will be checked when the imported interface frozen checks are done |
| 853 | for (const FQName& importedPackage : imported) { |
| 854 | std::vector<FQName> packageInterfaces; |
| 855 | status_t err = appendPackageInterfacesToVector(importedPackage, &packageInterfaces); |
| 856 | if (err != OK) { |
| 857 | return err; |
| 858 | } |
| 859 | |
| 860 | for (const FQName& importedName : packageInterfaces) { |
| 861 | HashStatus status = checkHash(importedName); |
Steven Moreland | fc32bf2 | 2019-02-11 21:02:10 -0800 | [diff] [blame] | 862 | switch (status) { |
| 863 | case HashStatus::CHANGED: |
| 864 | case HashStatus::ERROR: |
| 865 | return UNKNOWN_ERROR; |
| 866 | case HashStatus::FROZEN: |
| 867 | continue; |
| 868 | case HashStatus::UNFROZEN: |
| 869 | result->insert(importedName); |
| 870 | continue; |
| 871 | default: |
| 872 | LOG(FATAL) << static_cast<uint64_t>(status); |
Steven Moreland | 2d3ff5c | 2018-01-18 17:22:45 -0800 | [diff] [blame] | 873 | } |
| 874 | } |
| 875 | } |
| 876 | |
| 877 | return OK; |
| 878 | } |
| 879 | |
| 880 | status_t Coordinator::enforceHashes(const FQName& currentPackage) const { |
Steven Moreland | 218625a | 2017-04-18 22:31:50 -0700 | [diff] [blame] | 881 | std::vector<FQName> packageInterfaces; |
Steven Moreland | 2d3ff5c | 2018-01-18 17:22:45 -0800 | [diff] [blame] | 882 | status_t err = appendPackageInterfacesToVector(currentPackage, &packageInterfaces); |
Steven Moreland | 218625a | 2017-04-18 22:31:50 -0700 | [diff] [blame] | 883 | if (err != OK) { |
| 884 | return err; |
| 885 | } |
| 886 | |
Steven Moreland | 2d3ff5c | 2018-01-18 17:22:45 -0800 | [diff] [blame] | 887 | for (const FQName& currentFQName : packageInterfaces) { |
| 888 | HashStatus status = checkHash(currentFQName); |
Steven Moreland | fc32bf2 | 2019-02-11 21:02:10 -0800 | [diff] [blame] | 889 | switch (status) { |
| 890 | case HashStatus::CHANGED: |
| 891 | case HashStatus::ERROR: |
Steven Moreland | 2d3ff5c | 2018-01-18 17:22:45 -0800 | [diff] [blame] | 892 | return UNKNOWN_ERROR; |
Steven Moreland | fc32bf2 | 2019-02-11 21:02:10 -0800 | [diff] [blame] | 893 | case HashStatus::FROZEN: { |
| 894 | std::set<FQName> unfrozenDependencies; |
| 895 | err = getUnfrozenDependencies(currentFQName, &unfrozenDependencies); |
| 896 | if (err != OK) return err; |
Steven Moreland | 218625a | 2017-04-18 22:31:50 -0700 | [diff] [blame] | 897 | |
Steven Moreland | fc32bf2 | 2019-02-11 21:02:10 -0800 | [diff] [blame] | 898 | if (!unfrozenDependencies.empty()) { |
| 899 | std::cerr << "ERROR: Frozen interface " << currentFQName.string() |
| 900 | << " cannot depend on unfrozen thing(s):" << std::endl; |
| 901 | for (const FQName& name : unfrozenDependencies) { |
| 902 | std::cerr << " (unfrozen) " << name.string() << std::endl; |
| 903 | } |
| 904 | return UNKNOWN_ERROR; |
| 905 | } |
| 906 | } |
| 907 | continue; |
| 908 | case HashStatus::UNFROZEN: |
| 909 | continue; |
| 910 | default: |
| 911 | LOG(FATAL) << static_cast<uint64_t>(status); |
| 912 | } |
Steven Moreland | 218625a | 2017-04-18 22:31:50 -0700 | [diff] [blame] | 913 | } |
| 914 | |
| 915 | return err; |
| 916 | } |
| 917 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 918 | bool Coordinator::MakeParentHierarchy(const std::string &path) { |
| 919 | static const mode_t kMode = 0755; |
| 920 | |
| 921 | size_t start = 1; // Ignore leading '/' |
| 922 | size_t slashPos; |
Chih-Hung Hsieh | 8c90cc5 | 2017-08-03 14:51:13 -0700 | [diff] [blame] | 923 | while ((slashPos = path.find('/', start)) != std::string::npos) { |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 924 | std::string partial = path.substr(0, slashPos); |
| 925 | |
| 926 | struct stat st; |
| 927 | if (stat(partial.c_str(), &st) < 0) { |
| 928 | if (errno != ENOENT) { |
| 929 | return false; |
| 930 | } |
| 931 | |
| 932 | int res = mkdir(partial.c_str(), kMode); |
| 933 | if (res < 0) { |
| 934 | return false; |
| 935 | } |
| 936 | } else if (!S_ISDIR(st.st_mode)) { |
| 937 | return false; |
| 938 | } |
| 939 | |
| 940 | start = slashPos + 1; |
| 941 | } |
| 942 | |
| 943 | return true; |
| 944 | } |
| 945 | |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 946 | } // namespace android |
| 947 | |