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 | |
| 19 | #include "AST.h" |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 20 | |
| 21 | #include <android-base/logging.h> |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 22 | #include <iterator> |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 23 | #include <sys/dir.h> |
| 24 | #include <sys/stat.h> |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 25 | |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 26 | extern android::status_t parseFile(android::AST *ast); |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 27 | |
| 28 | namespace android { |
| 29 | |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 30 | Coordinator::Coordinator( |
| 31 | const std::vector<std::string> &packageRootPaths, |
| 32 | const std::vector<std::string> &packageRoots) |
| 33 | : mPackageRootPaths(packageRootPaths), |
| 34 | mPackageRoots(packageRoots) { |
| 35 | // empty |
Andreas Huber | dc98133 | 2016-07-29 15:46:54 -0700 | [diff] [blame] | 36 | } |
| 37 | |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 38 | Coordinator::~Coordinator() { |
| 39 | // empty |
| 40 | } |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 41 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 42 | AST *Coordinator::parse(const FQName &fqName, std::set<AST *> *parsedASTs) { |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 43 | CHECK(fqName.isFullyQualified()); |
| 44 | |
Andreas Huber | 6e584b7 | 2016-07-29 16:14:07 -0700 | [diff] [blame] | 45 | // LOG(INFO) << "parsing " << fqName.string(); |
| 46 | |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 47 | ssize_t index = mCache.indexOfKey(fqName); |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 48 | if (index >= 0) { |
| 49 | AST *ast = mCache.valueAt(index); |
| 50 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 51 | if (ast != nullptr && parsedASTs != nullptr) { |
| 52 | parsedASTs->insert(ast); |
| 53 | } |
| 54 | |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 55 | return ast; |
| 56 | } |
| 57 | |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 58 | // Add this to the cache immediately, so we can discover circular imports. |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 59 | mCache.add(fqName, nullptr); |
| 60 | |
| 61 | AST *typesAST = nullptr; |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 62 | |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 63 | if (fqName.name() != "types") { |
| 64 | // Any interface file implicitly imports its package's types.hal. |
| 65 | FQName typesName(fqName.package(), fqName.version(), "types"); |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 66 | typesAST = parse(typesName, parsedASTs); |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 67 | |
| 68 | // fall through. |
| 69 | } |
| 70 | |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 71 | std::string path = getPackagePath(fqName); |
| 72 | |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 73 | path.append(fqName.name()); |
| 74 | path.append(".hal"); |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 75 | |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 76 | AST *ast = new AST(this, path); |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 77 | |
| 78 | if (typesAST != NULL) { |
| 79 | // If types.hal for this AST's package existed, make it's defined |
| 80 | // types available to the (about to be parsed) AST right away. |
| 81 | ast->addImportedAST(typesAST); |
| 82 | } |
| 83 | |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 84 | status_t err = parseFile(ast); |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 85 | |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 86 | if (err != OK) { |
Andreas Huber | 6e584b7 | 2016-07-29 16:14:07 -0700 | [diff] [blame] | 87 | // LOG(ERROR) << "parsing '" << path << "' FAILED."; |
| 88 | |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 89 | delete ast; |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 90 | ast = nullptr; |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 91 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 92 | return nullptr; |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 95 | if (ast->package().package() != fqName.package() |
| 96 | || ast->package().version() != fqName.version()) { |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 97 | fprintf(stderr, |
| 98 | "ERROR: File at '%s' does not match expected package and/or " |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 99 | "version.\n", |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 100 | path.c_str()); |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 101 | |
| 102 | err = UNKNOWN_ERROR; |
| 103 | } else { |
| 104 | std::string ifaceName; |
| 105 | if (ast->isInterface(&ifaceName)) { |
| 106 | if (fqName.name() == "types") { |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 107 | fprintf(stderr, |
| 108 | "ERROR: File at '%s' declares an interface '%s' " |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 109 | "instead of the expected types common to the package.\n", |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 110 | path.c_str(), |
| 111 | ifaceName.c_str()); |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 112 | |
| 113 | err = UNKNOWN_ERROR; |
| 114 | } else if (ifaceName != fqName.name()) { |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 115 | fprintf(stderr, |
| 116 | "ERROR: File at '%s' does not declare interface type " |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 117 | "'%s'.\n", |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 118 | path.c_str(), |
| 119 | fqName.name().c_str()); |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 120 | |
| 121 | err = UNKNOWN_ERROR; |
| 122 | } |
| 123 | } else if (fqName.name() != "types") { |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 124 | fprintf(stderr, |
| 125 | "ERROR: File at '%s' declares types rather than the " |
Andreas Huber | 0d0f9a2 | 2016-08-17 10:26:11 -0700 | [diff] [blame] | 126 | "expected interface type '%s'.\n", |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 127 | path.c_str(), |
| 128 | fqName.name().c_str()); |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 129 | |
| 130 | err = UNKNOWN_ERROR; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | if (err != OK) { |
| 135 | delete ast; |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 136 | ast = nullptr; |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 137 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 138 | return nullptr; |
Andreas Huber | a2723d2 | 2016-07-29 15:36:07 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 141 | if (parsedASTs != nullptr) { parsedASTs->insert(ast); } |
| 142 | |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 143 | mCache.add(fqName, ast); |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 144 | |
| 145 | return ast; |
| 146 | } |
| 147 | |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 148 | std::vector<std::string>::const_iterator |
| 149 | Coordinator::findPackageRoot(const FQName &fqName) const { |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 150 | CHECK(!fqName.package().empty()); |
| 151 | CHECK(!fqName.version().empty()); |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 152 | |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 153 | // Find the right package prefix and path for this FQName. For |
| 154 | // example, if FQName is "android.hardware.nfc@1.0::INfc", and the |
| 155 | // prefix:root is set to [ "android.hardware:hardware/interfaces", |
| 156 | // "vendor.qcom.hardware:vendor/qcom"], then we will identify the |
| 157 | // prefix "android.hardware" and the package root |
| 158 | // "hardware/interfaces". |
| 159 | |
| 160 | // TODO: This now returns on the first match. Throw an error if |
| 161 | // there are multiple hits. |
| 162 | auto it = mPackageRoots.begin(); |
| 163 | for (; it != mPackageRoots.end(); it++) { |
| 164 | if (fqName.package().find(*it) != std::string::npos) { |
| 165 | break; |
| 166 | } |
| 167 | } |
| 168 | CHECK(it != mPackageRoots.end()); |
| 169 | |
| 170 | return it; |
| 171 | } |
| 172 | |
| 173 | std::string Coordinator::getPackageRoot(const FQName &fqName) const { |
| 174 | auto it = findPackageRoot(fqName); |
| 175 | auto prefix = *it; |
| 176 | return prefix; |
| 177 | } |
| 178 | |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 179 | std::string Coordinator::getPackageRootPath(const FQName &fqName) const { |
| 180 | auto it = findPackageRoot(fqName); |
| 181 | auto root = mPackageRootPaths[std::distance(mPackageRoots.begin(), it)]; |
| 182 | return root; |
| 183 | } |
| 184 | |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 185 | std::string Coordinator::getPackagePath( |
| 186 | const FQName &fqName, bool relative) const { |
| 187 | |
| 188 | auto it = findPackageRoot(fqName); |
| 189 | auto prefix = *it; |
| 190 | auto root = mPackageRootPaths[std::distance(mPackageRoots.begin(), it)]; |
| 191 | |
| 192 | // Make sure the prefix ends on a '.' and the root path on a '/' |
| 193 | if ((*--prefix.end()) != '.') { |
| 194 | prefix += '.'; |
| 195 | } |
| 196 | |
| 197 | if ((*--root.end()) != '/') { |
| 198 | root += '/'; |
| 199 | } |
| 200 | |
| 201 | // Given FQName of "android.hardware.nfc@1.0::IFoo" and a prefix |
| 202 | // "android.hardware.", the suffix is "nfc@1.0::IFoo". |
| 203 | const std::string packageSuffix = fqName.package().substr(prefix.length()); |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 204 | |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 205 | std::string packagePath; |
| 206 | if (!relative) { |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 207 | packagePath = root; |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 208 | } |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 209 | |
| 210 | size_t startPos = 0; |
| 211 | size_t dotPos; |
| 212 | while ((dotPos = packageSuffix.find('.', startPos)) != std::string::npos) { |
| 213 | packagePath.append(packageSuffix.substr(startPos, dotPos - startPos)); |
| 214 | packagePath.append("/"); |
| 215 | |
| 216 | startPos = dotPos + 1; |
| 217 | } |
| 218 | CHECK_LT(startPos + 1, packageSuffix.length()); |
| 219 | packagePath.append(packageSuffix.substr(startPos)); |
| 220 | packagePath.append("/"); |
| 221 | |
| 222 | CHECK_EQ(fqName.version().find('@'), 0u); |
| 223 | packagePath.append(fqName.version().substr(1)); |
| 224 | packagePath.append("/"); |
| 225 | |
| 226 | return packagePath; |
| 227 | } |
| 228 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 229 | status_t Coordinator::getPackageInterfaceFiles( |
| 230 | const FQName &package, |
| 231 | std::vector<std::string> *fileNames) const { |
| 232 | fileNames->clear(); |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 233 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 234 | const std::string packagePath = getPackagePath(package); |
| 235 | |
| 236 | DIR *dir = opendir(packagePath.c_str()); |
| 237 | |
| 238 | if (dir == NULL) { |
Steven Moreland | 261370a | 2016-08-29 15:36:59 -0700 | [diff] [blame^] | 239 | LOG(ERROR) << "Could not open package path: " << packagePath; |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 240 | return -errno; |
| 241 | } |
| 242 | |
| 243 | struct dirent *ent; |
| 244 | while ((ent = readdir(dir)) != NULL) { |
| 245 | if (ent->d_type != DT_REG) { |
Andreas Huber | 6cb08cf | 2016-08-03 15:44:51 -0700 | [diff] [blame] | 246 | continue; |
| 247 | } |
| 248 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 249 | const auto suffix = ".hal"; |
| 250 | const auto suffix_len = std::strlen(suffix); |
| 251 | const auto d_namelen = strlen(ent->d_name); |
Andreas Huber | e61e3f7 | 2016-08-03 10:22:03 -0700 | [diff] [blame] | 252 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 253 | if (d_namelen < suffix_len |
| 254 | || strcmp(ent->d_name + d_namelen - suffix_len, suffix)) { |
| 255 | continue; |
Andreas Huber | e61e3f7 | 2016-08-03 10:22:03 -0700 | [diff] [blame] | 256 | } |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 257 | |
| 258 | fileNames->push_back(std::string(ent->d_name, d_namelen - suffix_len)); |
| 259 | } |
| 260 | |
| 261 | closedir(dir); |
| 262 | dir = NULL; |
| 263 | |
| 264 | return OK; |
| 265 | } |
| 266 | |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 267 | status_t Coordinator::appendPackageInterfacesToSet( |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 268 | const FQName &package, |
| 269 | std::vector<FQName> *packageInterfaces) const { |
| 270 | packageInterfaces->clear(); |
| 271 | |
| 272 | std::vector<std::string> fileNames; |
| 273 | status_t err = getPackageInterfaceFiles(package, &fileNames); |
| 274 | |
| 275 | if (err != OK) { |
| 276 | return err; |
| 277 | } |
| 278 | |
| 279 | for (const auto &fileName : fileNames) { |
| 280 | FQName subFQName( |
| 281 | package.package() + package.version() + "::" + fileName); |
| 282 | |
| 283 | if (!subFQName.isValid()) { |
| 284 | LOG(WARNING) |
| 285 | << "Whole-package import encountered invalid filename '" |
| 286 | << fileName |
| 287 | << "' in package " |
| 288 | << package.package() |
| 289 | << package.version(); |
| 290 | |
| 291 | continue; |
| 292 | } |
| 293 | |
| 294 | packageInterfaces->push_back(subFQName); |
Andreas Huber | e61e3f7 | 2016-08-03 10:22:03 -0700 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | return OK; |
| 298 | } |
| 299 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 300 | std::string Coordinator::convertPackageRootToPath(const FQName &fqName) const { |
| 301 | std::string packageRoot = getPackageRoot(fqName); |
| 302 | |
| 303 | if (*(packageRoot.end()--) != '.') { |
| 304 | packageRoot += '.'; |
| 305 | } |
| 306 | |
| 307 | std::replace(packageRoot.begin(), packageRoot.end(), '.', '/'); |
| 308 | |
| 309 | return packageRoot; // now converted to a path |
| 310 | } |
| 311 | |
| 312 | // static |
| 313 | bool Coordinator::MakeParentHierarchy(const std::string &path) { |
| 314 | static const mode_t kMode = 0755; |
| 315 | |
| 316 | size_t start = 1; // Ignore leading '/' |
| 317 | size_t slashPos; |
| 318 | while ((slashPos = path.find("/", start)) != std::string::npos) { |
| 319 | std::string partial = path.substr(0, slashPos); |
| 320 | |
| 321 | struct stat st; |
| 322 | if (stat(partial.c_str(), &st) < 0) { |
| 323 | if (errno != ENOENT) { |
| 324 | return false; |
| 325 | } |
| 326 | |
| 327 | int res = mkdir(partial.c_str(), kMode); |
| 328 | if (res < 0) { |
| 329 | return false; |
| 330 | } |
| 331 | } else if (!S_ISDIR(st.st_mode)) { |
| 332 | return false; |
| 333 | } |
| 334 | |
| 335 | start = slashPos + 1; |
| 336 | } |
| 337 | |
| 338 | return true; |
| 339 | } |
| 340 | |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 341 | } // namespace android |
| 342 | |