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 | #ifndef COORDINATOR_H_ |
| 18 | |
| 19 | #define COORDINATOR_H_ |
| 20 | |
| 21 | #include <android-base/macros.h> |
Steven Moreland | 47792c4 | 2017-09-20 10:03:20 -0700 | [diff] [blame] | 22 | #include <hidl-util/FQName.h> |
Steven Moreland | f78c44d | 2017-09-25 18:41:45 -0700 | [diff] [blame] | 23 | #include <hidl-util/Formatter.h> |
| 24 | #include <utils/Errors.h> |
Steven Moreland | d537ab0 | 2016-09-12 10:32:01 -0700 | [diff] [blame] | 25 | #include <map> |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 26 | #include <set> |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 27 | #include <string> |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 28 | #include <vector> |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 29 | |
| 30 | namespace android { |
| 31 | |
| 32 | struct AST; |
Andreas Huber | fd4afab | 2016-08-03 13:02:57 -0700 | [diff] [blame] | 33 | struct Type; |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 34 | |
| 35 | struct Coordinator { |
Steven Moreland | 47792c4 | 2017-09-20 10:03:20 -0700 | [diff] [blame] | 36 | Coordinator() {}; |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 37 | |
Steven Moreland | 89a9ebb | 2017-12-04 10:18:00 -0800 | [diff] [blame] | 38 | const std::string& getRootPath() const; |
Steven Moreland | 47792c4 | 2017-09-20 10:03:20 -0700 | [diff] [blame] | 39 | void setRootPath(const std::string &rootPath); |
Steven Moreland | 6d3d3c8 | 2018-02-08 11:49:34 -0800 | [diff] [blame] | 40 | void setOutputPath(const std::string& outputPath); |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 41 | |
Steven Moreland | 37c57ee | 2017-09-25 19:08:56 -0700 | [diff] [blame] | 42 | void setVerbose(bool value); |
Andreas Huber | 308d8a2 | 2017-11-06 14:46:52 -0800 | [diff] [blame] | 43 | bool isVerbose() const; |
Steven Moreland | 37c57ee | 2017-09-25 19:08:56 -0700 | [diff] [blame] | 44 | |
Steven Moreland | 394af5c | 2018-02-09 14:41:46 -0800 | [diff] [blame] | 45 | void setDepFile(const std::string& depFile); |
| 46 | |
Steven Moreland | 89a9ebb | 2017-12-04 10:18:00 -0800 | [diff] [blame] | 47 | const std::string& getOwner() const; |
| 48 | void setOwner(const std::string& owner); |
| 49 | |
Steven Moreland | 25c8166 | 2017-05-12 14:57:36 -0700 | [diff] [blame] | 50 | // adds path only if it doesn't exist |
Steven Moreland | 47792c4 | 2017-09-20 10:03:20 -0700 | [diff] [blame] | 51 | status_t addPackagePath(const std::string& root, const std::string& path, std::string* error); |
| 52 | // adds path if it hasn't already been added |
Steven Moreland | 25c8166 | 2017-05-12 14:57:36 -0700 | [diff] [blame] | 53 | void addDefaultPackagePath(const std::string& root, const std::string& path); |
| 54 | |
Steven Moreland | f78c44d | 2017-09-25 18:41:45 -0700 | [diff] [blame] | 55 | enum class Location { |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 56 | STANDARD_OUT, |
Steven Moreland | 6d3d3c8 | 2018-02-08 11:49:34 -0800 | [diff] [blame] | 57 | DIRECT, // mOutputPath + file name |
| 58 | PACKAGE_ROOT, // e.x. mRootPath + /nfc/1.0/Android.bp |
| 59 | GEN_OUTPUT, // e.x. mOutputPath + /android/hardware/foo/1.0/*.cpp |
| 60 | GEN_SANITIZED, // e.x. mOutputPath + /android/hardware/foo/V1_0/*.cpp |
Steven Moreland | f78c44d | 2017-09-25 18:41:45 -0700 | [diff] [blame] | 61 | }; |
| 62 | |
Steven Moreland | 255c9a1 | 2018-02-26 13:10:27 -0800 | [diff] [blame] | 63 | status_t getFilepath(const FQName& fqName, Location location, const std::string& fileName, |
| 64 | std::string* path) const; |
Steven Moreland | f78c44d | 2017-09-25 18:41:45 -0700 | [diff] [blame] | 65 | |
Steven Moreland | 6d3d3c8 | 2018-02-08 11:49:34 -0800 | [diff] [blame] | 66 | Formatter getFormatter(const FQName& fqName, Location location, |
Steven Moreland | f78c44d | 2017-09-25 18:41:45 -0700 | [diff] [blame] | 67 | const std::string& fileName) const; |
| 68 | |
Steven Moreland | 37c57ee | 2017-09-25 19:08:56 -0700 | [diff] [blame] | 69 | // must be called before file access |
| 70 | void onFileAccess(const std::string& path, const std::string& mode) const; |
| 71 | |
Steven Moreland | 394af5c | 2018-02-09 14:41:46 -0800 | [diff] [blame] | 72 | status_t writeDepFile(const std::string& forFile) const; |
| 73 | |
Steven Moreland | c59326e | 2017-06-20 15:19:30 -0700 | [diff] [blame] | 74 | enum class Enforce { |
| 75 | FULL, // default |
| 76 | NO_HASH, // only for use with -Lhash |
| 77 | NONE, // only for use during enforcement |
| 78 | }; |
| 79 | |
Andreas Huber | 39fa718 | 2016-08-19 14:27:33 -0700 | [diff] [blame] | 80 | // Attempts to parse the interface/types referred to by fqName. |
| 81 | // Parsing an interface also parses the associated package's types.hal |
| 82 | // file if it exists. |
| 83 | // If "parsedASTs" is non-NULL, successfully parsed ASTs are inserted |
| 84 | // into the set. |
Yifan Hong | f619fc7 | 2017-04-07 15:40:06 -0700 | [diff] [blame] | 85 | // If !enforce, enforceRestrictionsOnPackage won't be run. |
Steven Moreland | c59326e | 2017-06-20 15:19:30 -0700 | [diff] [blame] | 86 | AST* parse(const FQName& fqName, std::set<AST*>* parsedASTs = nullptr, |
| 87 | Enforce enforcement = Enforce::FULL) const; |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 88 | |
Steven Moreland | 71f0913 | 2018-02-20 12:24:30 -0800 | [diff] [blame] | 89 | // Same as parse, but it distinguishes between "missing file" and "could not parse AST" |
| 90 | // return OK, out *ast: |
| 91 | // 0xdeadbeef -> successfully parsed |
| 92 | // nullptr -> file not present |
| 93 | // return !OK |
| 94 | // could not parse AST and file exists |
| 95 | status_t parseOptional(const FQName& fqName, AST** ast, std::set<AST*>* parsedASTs = nullptr, |
| 96 | Enforce enforcement = Enforce::FULL) const; |
| 97 | |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 98 | // Given package-root paths of ["hardware/interfaces", |
| 99 | // "vendor/<something>/interfaces"], package roots of |
| 100 | // ["android.hardware", "vendor.<something>.hardware"], and a |
| 101 | // FQName of "android.hardware.nfc@1.0::INfc, then getPackagePath() |
Yifan Hong | 97288ac | 2016-12-12 16:03:51 -0800 | [diff] [blame] | 102 | // will return "hardware/interfaces/nfc/1.0" (if sanitized = false) |
| 103 | // or "hardware/interfaces/nfc/V1_0" (if sanitized = true). |
Steven Moreland | 255c9a1 | 2018-02-26 13:10:27 -0800 | [diff] [blame] | 104 | status_t getPackagePath(const FQName& fqName, bool relative, bool sanitized, |
| 105 | std::string* path) const; |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 106 | |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 107 | // Given package roots of ["android.hardware", |
| 108 | // "vendor.<something>.hardware"] and a FQName of |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 109 | // "android.hardware.nfc@1.0::INfc, then getPackageRoot() will |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 110 | // return "android.hardware". |
Steven Moreland | 255c9a1 | 2018-02-26 13:10:27 -0800 | [diff] [blame] | 111 | status_t getPackageRoot(const FQName& fqName, std::string* root) const; |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 112 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 113 | status_t getPackageInterfaceFiles( |
| 114 | const FQName &package, |
| 115 | std::vector<std::string> *fileNames) const; |
| 116 | |
Steven Moreland | aa18683 | 2016-09-26 13:51:43 -0700 | [diff] [blame] | 117 | status_t appendPackageInterfacesToVector( |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 118 | const FQName &package, |
| 119 | std::vector<FQName> *packageInterfaces) const; |
| 120 | |
Steven Moreland | 87b26d7 | 2017-10-11 09:35:42 -0700 | [diff] [blame] | 121 | status_t isTypesOnlyPackage(const FQName& package, bool* result) const; |
| 122 | |
Steven Moreland | 40b8635 | 2018-02-01 16:03:30 -0800 | [diff] [blame] | 123 | // Returns types which are imported/defined but not referenced in code |
| 124 | status_t addUnreferencedTypes(const std::vector<FQName>& packageInterfaces, |
| 125 | std::set<FQName>* unreferencedDefinitions, |
Steven Moreland | b65e5d9 | 2018-02-08 12:44:51 -0800 | [diff] [blame] | 126 | std::set<FQName>* unreferencedImports) const; |
Steven Moreland | 40b8635 | 2018-02-01 16:03:30 -0800 | [diff] [blame] | 127 | |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 128 | // Enforce a set of restrictions on a set of packages. These include: |
| 129 | // - minor version upgrades |
| 130 | // "packages" contains names like "android.hardware.nfc@1.1". |
Steven Moreland | 218625a | 2017-04-18 22:31:50 -0700 | [diff] [blame] | 131 | // - hashing restrictions |
Steven Moreland | c59326e | 2017-06-20 15:19:30 -0700 | [diff] [blame] | 132 | status_t enforceRestrictionsOnPackage(const FQName& fqName, |
| 133 | Enforce enforcement = Enforce::FULL) const; |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 134 | |
Steven Moreland | 0057ad5 | 2017-09-25 19:11:19 -0700 | [diff] [blame] | 135 | private: |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 136 | static bool MakeParentHierarchy(const std::string &path); |
Andreas Huber | e61e3f7 | 2016-08-03 10:22:03 -0700 | [diff] [blame] | 137 | |
Steven Moreland | 2d3ff5c | 2018-01-18 17:22:45 -0800 | [diff] [blame] | 138 | enum class HashStatus { |
| 139 | ERROR, |
| 140 | UNFROZEN, |
| 141 | FROZEN, |
| 142 | CHANGED, // frozen but changed |
| 143 | }; |
| 144 | HashStatus checkHash(const FQName& fqName) const; |
| 145 | status_t getUnfrozenDependencies(const FQName& fqName, std::set<FQName>* result) const; |
| 146 | |
Steven Moreland | 47792c4 | 2017-09-20 10:03:20 -0700 | [diff] [blame] | 147 | // indicates that packages in "android.hardware" will be looked up in hardware/interfaces |
| 148 | struct PackageRoot { |
| 149 | std::string path; // e.x. hardware/interfaces |
| 150 | FQName root; // e.x. android.hardware@0.0 |
| 151 | }; |
Steven Moreland | 18d9af1 | 2017-10-03 08:52:05 -0700 | [diff] [blame] | 152 | |
Steven Moreland | 255c9a1 | 2018-02-26 13:10:27 -0800 | [diff] [blame] | 153 | // nullptr if it doesn't exist |
| 154 | const PackageRoot* findPackageRoot(const FQName& fqName) const; |
Steven Moreland | 18d9af1 | 2017-10-03 08:52:05 -0700 | [diff] [blame] | 155 | |
| 156 | // Given package-root paths of ["hardware/interfaces", |
| 157 | // "vendor/<something>/interfaces"], package roots of |
| 158 | // ["android.hardware", "vendor.<something>.hardware"], and a |
| 159 | // FQName of "android.hardware.nfc@1.0::INfc, then getPackageRootPath() |
| 160 | // will return "hardware/interfaces". |
Steven Moreland | 255c9a1 | 2018-02-26 13:10:27 -0800 | [diff] [blame] | 161 | status_t getPackageRootPath(const FQName& fqName, std::string* path) const; |
Steven Moreland | 18d9af1 | 2017-10-03 08:52:05 -0700 | [diff] [blame] | 162 | |
| 163 | // Given an FQName of "android.hardware.nfc@1.0::INfc", return |
| 164 | // "android/hardware/". |
Steven Moreland | 255c9a1 | 2018-02-26 13:10:27 -0800 | [diff] [blame] | 165 | status_t convertPackageRootToPath(const FQName& fqName, std::string* path) const; |
Steven Moreland | 18d9af1 | 2017-10-03 08:52:05 -0700 | [diff] [blame] | 166 | |
Steven Moreland | 47792c4 | 2017-09-20 10:03:20 -0700 | [diff] [blame] | 167 | std::vector<PackageRoot> mPackageRoots; |
Steven Moreland | 6d3d3c8 | 2018-02-08 11:49:34 -0800 | [diff] [blame] | 168 | std::string mRootPath; // root of android source tree (to locate package roots) |
| 169 | std::string mOutputPath; // root of output directory |
Steven Moreland | 394af5c | 2018-02-09 14:41:46 -0800 | [diff] [blame] | 170 | std::string mDepFile; // location to write depfile |
Steven Moreland | 89a9ebb | 2017-12-04 10:18:00 -0800 | [diff] [blame] | 171 | |
| 172 | // hidl-gen options |
Steven Moreland | 75fb19b | 2018-02-09 11:22:43 -0800 | [diff] [blame] | 173 | bool mVerbose = false; |
Steven Moreland | 89a9ebb | 2017-12-04 10:18:00 -0800 | [diff] [blame] | 174 | std::string mOwner; |
Steven Moreland | 37c57ee | 2017-09-25 19:08:56 -0700 | [diff] [blame] | 175 | |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 176 | // cache to parse(). |
Steven Moreland | 28b9b53 | 2017-05-12 17:02:58 -0700 | [diff] [blame] | 177 | mutable std::map<FQName, AST *> mCache; |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 178 | |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 179 | // cache to enforceRestrictionsOnPackage(). |
Steven Moreland | 28b9b53 | 2017-05-12 17:02:58 -0700 | [diff] [blame] | 180 | mutable std::set<FQName> mPackagesEnforced; |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 181 | |
Steven Moreland | 394af5c | 2018-02-09 14:41:46 -0800 | [diff] [blame] | 182 | mutable std::set<std::string> mReadFiles; |
| 183 | |
Steven Moreland | 7a36804 | 2017-08-28 12:47:24 -0700 | [diff] [blame] | 184 | // Returns the given path if it is absolute, otherwise it returns |
| 185 | // the path relative to mRootPath |
| 186 | std::string makeAbsolute(const std::string& string) const; |
Steven Moreland | b90d327 | 2017-05-26 10:02:39 -0700 | [diff] [blame] | 187 | |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 188 | // Rules of enforceRestrictionsOnPackage are listed below. |
Steven Moreland | 51b02f4 | 2018-03-14 15:43:29 -0700 | [diff] [blame] | 189 | status_t enforceMinorVersionUprevs(const FQName& fqName, Enforce enforcement) const; |
Steven Moreland | 28b9b53 | 2017-05-12 17:02:58 -0700 | [diff] [blame] | 190 | status_t enforceHashes(const FQName &fqName) const; |
Yifan Hong | 78b38d1 | 2017-02-13 18:14:46 +0000 | [diff] [blame] | 191 | |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 192 | DISALLOW_COPY_AND_ASSIGN(Coordinator); |
| 193 | }; |
| 194 | |
| 195 | } // namespace android |
| 196 | |
| 197 | #endif // COORDINATOR_H_ |