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 | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 17 | #include "AST.h" |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 18 | #include "Coordinator.h" |
Steven Moreland | 55df071 | 2019-01-16 14:37:11 -0800 | [diff] [blame] | 19 | #include "Interface.h" |
Andreas Huber | 0fa9e39 | 2016-08-31 09:05:44 -0700 | [diff] [blame] | 20 | #include "Scope.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 21 | |
Andreas Huber | 308d8a2 | 2017-11-06 14:46:52 -0800 | [diff] [blame] | 22 | #include <android-base/logging.h> |
Steven Moreland | 5bdfa70 | 2017-04-18 23:20:39 -0700 | [diff] [blame] | 23 | #include <hidl-hash/Hash.h> |
Steven Moreland | 7ae3d54 | 2017-01-18 16:46:01 -0800 | [diff] [blame] | 24 | #include <hidl-util/FQName.h> |
Steven Moreland | a815398 | 2017-11-28 15:22:28 -0800 | [diff] [blame] | 25 | #include <hidl-util/Formatter.h> |
Steven Moreland | d177b12 | 2016-12-12 09:15:37 -0800 | [diff] [blame] | 26 | #include <hidl-util/StringHelper.h> |
Steven Moreland | a815398 | 2017-11-28 15:22:28 -0800 | [diff] [blame] | 27 | #include <stdio.h> |
| 28 | #include <sys/stat.h> |
| 29 | #include <unistd.h> |
Andreas Huber | 308d8a2 | 2017-11-06 14:46:52 -0800 | [diff] [blame] | 30 | #include <iostream> |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 31 | #include <set> |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 32 | #include <string> |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 33 | #include <vector> |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 34 | |
| 35 | using namespace android; |
| 36 | |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 37 | enum class OutputMode { |
| 38 | NEEDS_DIR, // -o output option expects a directory |
| 39 | NEEDS_FILE, // -o output option expects a file |
| 40 | NEEDS_SRC, // for changes inside the source tree itself |
| 41 | NOT_NEEDED // does not create files |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 42 | }; |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 43 | |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 44 | enum class GenerationGranularity { |
| 45 | PER_PACKAGE, // Files generated for each package |
| 46 | PER_FILE, // Files generated for each hal file |
| 47 | PER_TYPE, // Files generated for each hal file + each type in HAL files |
| 48 | }; |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 49 | |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 50 | // Represents a file that is generated by an -L option for an FQName |
| 51 | struct FileGenerator { |
| 52 | using ShouldGenerateFunction = std::function<bool(const FQName& fqName)>; |
| 53 | using FileNameForFQName = std::function<std::string(const FQName& fqName)>; |
| 54 | using GenerationFunction = std::function<status_t(Formatter& out, const FQName& fqName, |
| 55 | const Coordinator* coordinator)>; |
| 56 | |
| 57 | ShouldGenerateFunction mShouldGenerateForFqName; // If generate function applies to this target |
| 58 | FileNameForFQName mFileNameForFqName; // Target -> filename |
| 59 | GenerationFunction mGenerationFunction; // Function to generate output for file |
| 60 | |
Steven Moreland | 394af5c | 2018-02-09 14:41:46 -0800 | [diff] [blame] | 61 | std::string getFileName(const FQName& fqName) const { |
| 62 | return mFileNameForFqName ? mFileNameForFqName(fqName) : ""; |
| 63 | } |
| 64 | |
Steven Moreland | 255c9a1 | 2018-02-26 13:10:27 -0800 | [diff] [blame] | 65 | status_t getOutputFile(const FQName& fqName, const Coordinator* coordinator, |
| 66 | Coordinator::Location location, std::string* file) const { |
Steven Moreland | 394af5c | 2018-02-09 14:41:46 -0800 | [diff] [blame] | 67 | if (!mShouldGenerateForFqName(fqName)) { |
Steven Moreland | 255c9a1 | 2018-02-26 13:10:27 -0800 | [diff] [blame] | 68 | return OK; |
Steven Moreland | 394af5c | 2018-02-09 14:41:46 -0800 | [diff] [blame] | 69 | } |
| 70 | |
Steven Moreland | 255c9a1 | 2018-02-26 13:10:27 -0800 | [diff] [blame] | 71 | return coordinator->getFilepath(fqName, location, getFileName(fqName), file); |
Steven Moreland | 394af5c | 2018-02-09 14:41:46 -0800 | [diff] [blame] | 72 | } |
| 73 | |
Steven Moreland | 255c9a1 | 2018-02-26 13:10:27 -0800 | [diff] [blame] | 74 | status_t appendOutputFiles(const FQName& fqName, const Coordinator* coordinator, |
| 75 | Coordinator::Location location, |
| 76 | std::vector<std::string>* outputFiles) const { |
Steven Moreland | 394af5c | 2018-02-09 14:41:46 -0800 | [diff] [blame] | 77 | if (location == Coordinator::Location::STANDARD_OUT) { |
Steven Moreland | 255c9a1 | 2018-02-26 13:10:27 -0800 | [diff] [blame] | 78 | return OK; |
Steven Moreland | 394af5c | 2018-02-09 14:41:46 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | if (mShouldGenerateForFqName(fqName)) { |
Steven Moreland | 255c9a1 | 2018-02-26 13:10:27 -0800 | [diff] [blame] | 82 | std::string fileName; |
| 83 | status_t err = getOutputFile(fqName, coordinator, location, &fileName); |
| 84 | if (err != OK) return err; |
Steven Moreland | 394af5c | 2018-02-09 14:41:46 -0800 | [diff] [blame] | 85 | |
| 86 | if (!fileName.empty()) { |
| 87 | outputFiles->push_back(fileName); |
| 88 | } |
| 89 | } |
Steven Moreland | 255c9a1 | 2018-02-26 13:10:27 -0800 | [diff] [blame] | 90 | return OK; |
Steven Moreland | 394af5c | 2018-02-09 14:41:46 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 93 | status_t generate(const FQName& fqName, const Coordinator* coordinator, |
| 94 | Coordinator::Location location) const { |
| 95 | CHECK(mShouldGenerateForFqName != nullptr); |
| 96 | CHECK(mGenerationFunction != nullptr); |
| 97 | |
| 98 | if (!mShouldGenerateForFqName(fqName)) { |
| 99 | return OK; |
| 100 | } |
| 101 | |
Steven Moreland | 394af5c | 2018-02-09 14:41:46 -0800 | [diff] [blame] | 102 | Formatter out = coordinator->getFormatter(fqName, location, getFileName(fqName)); |
Steven Moreland | 255c9a1 | 2018-02-26 13:10:27 -0800 | [diff] [blame] | 103 | if (!out.isValid()) { |
| 104 | return UNKNOWN_ERROR; |
| 105 | } |
| 106 | |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 107 | return mGenerationFunction(out, fqName, coordinator); |
| 108 | } |
| 109 | |
| 110 | // Helper methods for filling out this struct |
| 111 | static bool generateForTypes(const FQName& fqName) { |
| 112 | const auto names = fqName.names(); |
| 113 | return names.size() > 0 && names[0] == "types"; |
| 114 | } |
| 115 | static bool generateForInterfaces(const FQName& fqName) { return !generateForTypes(fqName); } |
| 116 | static bool alwaysGenerate(const FQName&) { return true; } |
| 117 | }; |
| 118 | |
| 119 | // Represents a -L option, takes a fqName and generates files |
| 120 | struct OutputHandler { |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 121 | using ValidationFunction = std::function<bool( |
| 122 | const FQName& fqName, const Coordinator* coordinator, const std::string& language)>; |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 123 | |
| 124 | std::string mKey; // -L in Android.bp |
| 125 | std::string mDescription; // for display in help menu |
| 126 | OutputMode mOutputMode; // how this option interacts with -o |
| 127 | Coordinator::Location mLocation; // how to compute location relative to the output directory |
Steven Moreland | 394af5c | 2018-02-09 14:41:46 -0800 | [diff] [blame] | 128 | GenerationGranularity mGenerationGranularity; // what to run generate function on |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 129 | ValidationFunction mValidate; // if a given fqName is allowed for this option |
| 130 | std::vector<FileGenerator> mGenerateFunctions; // run for each target at this granularity |
| 131 | |
| 132 | const std::string& name() const { return mKey; } |
| 133 | const std::string& description() const { return mDescription; } |
| 134 | |
| 135 | status_t generate(const FQName& fqName, const Coordinator* coordinator) const; |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 136 | status_t validate(const FQName& fqName, const Coordinator* coordinator, |
| 137 | const std::string& language) const { |
| 138 | return mValidate(fqName, coordinator, language); |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 139 | } |
Steven Moreland | 394af5c | 2018-02-09 14:41:46 -0800 | [diff] [blame] | 140 | |
| 141 | status_t writeDepFile(const FQName& fqName, const Coordinator* coordinator) const; |
| 142 | |
| 143 | private: |
| 144 | status_t appendTargets(const FQName& fqName, const Coordinator* coordinator, |
| 145 | std::vector<FQName>* targets) const; |
| 146 | status_t appendOutputFiles(const FQName& fqName, const Coordinator* coordinator, |
| 147 | std::vector<std::string>* outputFiles) const; |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 148 | }; |
| 149 | |
| 150 | // Helper method for GenerationGranularity::PER_TYPE |
| 151 | // IFoo -> IFoo, types.hal (containing Bar, Baz) -> types.Bar, types.Baz |
| 152 | static status_t appendPerTypeTargets(const FQName& fqName, const Coordinator* coordinator, |
| 153 | std::vector<FQName>* exportedPackageInterfaces) { |
| 154 | CHECK(fqName.isFullyQualified()); |
| 155 | if (fqName.name() != "types") { |
| 156 | exportedPackageInterfaces->push_back(fqName); |
| 157 | return OK; |
| 158 | } |
| 159 | |
| 160 | AST* typesAST = coordinator->parse(fqName); |
| 161 | if (typesAST == nullptr) { |
| 162 | fprintf(stderr, "ERROR: Could not parse %s. Aborting.\n", fqName.string().c_str()); |
| 163 | return UNKNOWN_ERROR; |
| 164 | } |
| 165 | |
| 166 | std::vector<NamedType*> rootTypes = typesAST->getRootScope()->getSubTypes(); |
| 167 | for (const NamedType* rootType : rootTypes) { |
| 168 | if (rootType->isTypeDef()) continue; |
| 169 | |
| 170 | FQName rootTypeName(fqName.package(), fqName.version(), "types." + rootType->localName()); |
| 171 | exportedPackageInterfaces->push_back(rootTypeName); |
| 172 | } |
| 173 | return OK; |
| 174 | } |
| 175 | |
Steven Moreland | 394af5c | 2018-02-09 14:41:46 -0800 | [diff] [blame] | 176 | status_t OutputHandler::appendTargets(const FQName& fqName, const Coordinator* coordinator, |
| 177 | std::vector<FQName>* targets) const { |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 178 | switch (mGenerationGranularity) { |
| 179 | case GenerationGranularity::PER_PACKAGE: { |
Steven Moreland | 394af5c | 2018-02-09 14:41:46 -0800 | [diff] [blame] | 180 | targets->push_back(fqName.getPackageAndVersion()); |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 181 | } break; |
| 182 | case GenerationGranularity::PER_FILE: { |
| 183 | if (fqName.isFullyQualified()) { |
Steven Moreland | 394af5c | 2018-02-09 14:41:46 -0800 | [diff] [blame] | 184 | targets->push_back(fqName); |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 185 | break; |
| 186 | } |
Steven Moreland | 394af5c | 2018-02-09 14:41:46 -0800 | [diff] [blame] | 187 | status_t err = coordinator->appendPackageInterfacesToVector(fqName, targets); |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 188 | if (err != OK) return err; |
| 189 | } break; |
| 190 | case GenerationGranularity::PER_TYPE: { |
| 191 | if (fqName.isFullyQualified()) { |
Steven Moreland | 394af5c | 2018-02-09 14:41:46 -0800 | [diff] [blame] | 192 | status_t err = appendPerTypeTargets(fqName, coordinator, targets); |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 193 | if (err != OK) return err; |
| 194 | } |
| 195 | |
| 196 | std::vector<FQName> packageInterfaces; |
| 197 | status_t err = coordinator->appendPackageInterfacesToVector(fqName, &packageInterfaces); |
| 198 | if (err != OK) return err; |
| 199 | for (const FQName& packageInterface : packageInterfaces) { |
Steven Moreland | 394af5c | 2018-02-09 14:41:46 -0800 | [diff] [blame] | 200 | err = appendPerTypeTargets(packageInterface, coordinator, targets); |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 201 | if (err != OK) return err; |
| 202 | } |
| 203 | } break; |
| 204 | default: |
| 205 | CHECK(!"Should be here"); |
| 206 | } |
| 207 | |
Steven Moreland | 394af5c | 2018-02-09 14:41:46 -0800 | [diff] [blame] | 208 | return OK; |
| 209 | } |
| 210 | |
| 211 | status_t OutputHandler::generate(const FQName& fqName, const Coordinator* coordinator) const { |
| 212 | std::vector<FQName> targets; |
| 213 | status_t err = appendTargets(fqName, coordinator, &targets); |
| 214 | if (err != OK) return err; |
| 215 | |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 216 | for (const FQName& fqName : targets) { |
| 217 | for (const FileGenerator& file : mGenerateFunctions) { |
| 218 | status_t err = file.generate(fqName, coordinator, mLocation); |
| 219 | if (err != OK) return err; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | return OK; |
| 224 | } |
| 225 | |
Steven Moreland | 394af5c | 2018-02-09 14:41:46 -0800 | [diff] [blame] | 226 | status_t OutputHandler::appendOutputFiles(const FQName& fqName, const Coordinator* coordinator, |
| 227 | std::vector<std::string>* outputFiles) const { |
| 228 | std::vector<FQName> targets; |
| 229 | status_t err = appendTargets(fqName, coordinator, &targets); |
| 230 | if (err != OK) return err; |
| 231 | |
| 232 | for (const FQName& fqName : targets) { |
| 233 | for (const FileGenerator& file : mGenerateFunctions) { |
Steven Moreland | 255c9a1 | 2018-02-26 13:10:27 -0800 | [diff] [blame] | 234 | err = file.appendOutputFiles(fqName, coordinator, mLocation, outputFiles); |
| 235 | if (err != OK) return err; |
Steven Moreland | 394af5c | 2018-02-09 14:41:46 -0800 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | |
| 239 | return OK; |
| 240 | } |
| 241 | |
| 242 | status_t OutputHandler::writeDepFile(const FQName& fqName, const Coordinator* coordinator) const { |
| 243 | std::vector<std::string> outputFiles; |
| 244 | status_t err = appendOutputFiles(fqName, coordinator, &outputFiles); |
| 245 | if (err != OK) return err; |
| 246 | |
| 247 | // No need for dep files |
| 248 | if (outputFiles.empty()) { |
| 249 | return OK; |
| 250 | } |
| 251 | |
| 252 | // Depfiles in Android for genrules should be for the 'main file'. Because hidl-gen doesn't have |
| 253 | // a main file for most targets, we are just outputting a depfile for one single file only. |
| 254 | const std::string forFile = outputFiles[0]; |
| 255 | |
| 256 | return coordinator->writeDepFile(forFile); |
| 257 | } |
| 258 | |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 259 | // Use an AST function as a OutputHandler GenerationFunction |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 260 | static FileGenerator::GenerationFunction astGenerationFunction(void (AST::*generate)(Formatter&) |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 261 | const = nullptr) { |
| 262 | return [generate](Formatter& out, const FQName& fqName, |
| 263 | const Coordinator* coordinator) -> status_t { |
| 264 | AST* ast = coordinator->parse(fqName); |
| 265 | if (ast == nullptr) { |
| 266 | fprintf(stderr, "ERROR: Could not parse %s. Aborting.\n", fqName.string().c_str()); |
| 267 | return UNKNOWN_ERROR; |
| 268 | } |
| 269 | |
| 270 | if (generate == nullptr) return OK; // just parsing AST |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 271 | (ast->*generate)(out); |
| 272 | |
| 273 | return OK; |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 274 | }; |
| 275 | } |
| 276 | |
| 277 | // Common pattern: single file for package or standard out |
| 278 | static FileGenerator singleFileGenerator( |
| 279 | const std::string& fileName, const FileGenerator::GenerationFunction& generationFunction) { |
| 280 | return { |
| 281 | FileGenerator::alwaysGenerate, [fileName](const FQName&) { return fileName; }, |
| 282 | generationFunction, |
| 283 | }; |
| 284 | } |
| 285 | |
| 286 | static status_t generateJavaForPackage(Formatter& out, const FQName& fqName, |
| 287 | const Coordinator* coordinator) { |
| 288 | AST* ast; |
Andreas Huber | d29724f | 2016-09-14 09:33:13 -0700 | [diff] [blame] | 289 | std::string limitToType; |
Andreas Huber | 0fa9e39 | 2016-08-31 09:05:44 -0700 | [diff] [blame] | 290 | |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 291 | // Required for legacy -Lmakefile files |
Andreas Huber | 0fa9e39 | 2016-08-31 09:05:44 -0700 | [diff] [blame] | 292 | if (fqName.name().find("types.") == 0) { |
Andreas Huber | d29724f | 2016-09-14 09:33:13 -0700 | [diff] [blame] | 293 | limitToType = fqName.name().substr(strlen("types.")); |
Andreas Huber | 0fa9e39 | 2016-08-31 09:05:44 -0700 | [diff] [blame] | 294 | |
Yifan Hong | fece6ec | 2017-01-12 17:04:04 -0800 | [diff] [blame] | 295 | FQName typesName = fqName.getTypesForPackage(); |
Andreas Huber | 0fa9e39 | 2016-08-31 09:05:44 -0700 | [diff] [blame] | 296 | ast = coordinator->parse(typesName); |
| 297 | } else { |
| 298 | ast = coordinator->parse(fqName); |
| 299 | } |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 300 | if (ast == nullptr) { |
| 301 | fprintf(stderr, "ERROR: Could not parse %s. Aborting.\n", fqName.string().c_str()); |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 302 | return UNKNOWN_ERROR; |
| 303 | } |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 304 | ast->generateJava(out, limitToType); |
| 305 | return OK; |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 306 | }; |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 307 | |
Steven Moreland | b65e5d9 | 2018-02-08 12:44:51 -0800 | [diff] [blame] | 308 | static status_t dumpDefinedButUnreferencedTypeNames(const FQName& packageFQName, |
| 309 | const Coordinator* coordinator) { |
| 310 | std::vector<FQName> packageInterfaces; |
| 311 | status_t err = coordinator->appendPackageInterfacesToVector(packageFQName, &packageInterfaces); |
| 312 | if (err != OK) return err; |
| 313 | |
Steven Moreland | 40b8635 | 2018-02-01 16:03:30 -0800 | [diff] [blame] | 314 | std::set<FQName> unreferencedDefinitions; |
| 315 | std::set<FQName> unreferencedImports; |
Steven Moreland | b65e5d9 | 2018-02-08 12:44:51 -0800 | [diff] [blame] | 316 | err = coordinator->addUnreferencedTypes(packageInterfaces, &unreferencedDefinitions, |
| 317 | &unreferencedImports); |
| 318 | if (err != OK) return err; |
Andreas Huber | 308d8a2 | 2017-11-06 14:46:52 -0800 | [diff] [blame] | 319 | |
Steven Moreland | 40b8635 | 2018-02-01 16:03:30 -0800 | [diff] [blame] | 320 | for (const auto& fqName : unreferencedDefinitions) { |
Andreas Huber | 308d8a2 | 2017-11-06 14:46:52 -0800 | [diff] [blame] | 321 | std::cerr |
| 322 | << "VERBOSE: DEFINED-BUT-NOT-REFERENCED " |
| 323 | << fqName.string() |
| 324 | << std::endl; |
| 325 | } |
| 326 | |
Steven Moreland | 40b8635 | 2018-02-01 16:03:30 -0800 | [diff] [blame] | 327 | for (const auto& fqName : unreferencedImports) { |
Andreas Huber | 4f4b382 | 2017-11-15 13:18:50 -0800 | [diff] [blame] | 328 | std::cerr |
| 329 | << "VERBOSE: IMPORTED-BUT-NOT-REFERENCED " |
| 330 | << fqName.string() |
| 331 | << std::endl; |
| 332 | } |
| 333 | |
Andreas Huber | 308d8a2 | 2017-11-06 14:46:52 -0800 | [diff] [blame] | 334 | return OK; |
| 335 | } |
| 336 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 337 | static std::string makeLibraryName(const FQName &packageFQName) { |
Iliyan Malchev | e2c595a | 2016-08-07 21:20:04 -0700 | [diff] [blame] | 338 | return packageFQName.string(); |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 339 | } |
Steven Moreland | 5715fed | 2017-01-16 11:06:47 -0800 | [diff] [blame] | 340 | |
Steven Moreland | e6d7f09 | 2018-02-08 13:25:45 -0800 | [diff] [blame] | 341 | static status_t isPackageJavaCompatible(const FQName& packageFQName, const Coordinator* coordinator, |
| 342 | bool* compatible) { |
Andreas Huber | 75ae95d | 2016-10-12 16:08:26 -0700 | [diff] [blame] | 343 | std::vector<FQName> todo; |
| 344 | status_t err = |
| 345 | coordinator->appendPackageInterfacesToVector(packageFQName, &todo); |
| 346 | |
| 347 | if (err != OK) { |
| 348 | return err; |
| 349 | } |
| 350 | |
| 351 | std::set<FQName> seen; |
| 352 | for (const auto &iface : todo) { |
| 353 | seen.insert(iface); |
| 354 | } |
| 355 | |
| 356 | // Form the transitive closure of all imported interfaces (and types.hal-s) |
| 357 | // If any one of them is not java compatible, this package isn't either. |
| 358 | while (!todo.empty()) { |
| 359 | const FQName fqName = todo.back(); |
| 360 | todo.pop_back(); |
| 361 | |
| 362 | AST *ast = coordinator->parse(fqName); |
| 363 | |
| 364 | if (ast == nullptr) { |
| 365 | return UNKNOWN_ERROR; |
| 366 | } |
| 367 | |
| 368 | if (!ast->isJavaCompatible()) { |
| 369 | *compatible = false; |
| 370 | return OK; |
| 371 | } |
| 372 | |
| 373 | std::set<FQName> importedPackages; |
| 374 | ast->getImportedPackages(&importedPackages); |
| 375 | |
| 376 | for (const auto &package : importedPackages) { |
| 377 | std::vector<FQName> packageInterfaces; |
| 378 | status_t err = coordinator->appendPackageInterfacesToVector( |
| 379 | package, &packageInterfaces); |
| 380 | |
| 381 | if (err != OK) { |
| 382 | return err; |
| 383 | } |
| 384 | |
| 385 | for (const auto &iface : packageInterfaces) { |
| 386 | if (seen.find(iface) != seen.end()) { |
| 387 | continue; |
| 388 | } |
| 389 | |
| 390 | todo.push_back(iface); |
| 391 | seen.insert(iface); |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | *compatible = true; |
| 397 | return OK; |
| 398 | } |
| 399 | |
Andreas Huber | 5a9fe2c | 2016-10-13 15:31:10 -0700 | [diff] [blame] | 400 | static bool packageNeedsJavaCode( |
| 401 | const std::vector<FQName> &packageInterfaces, AST *typesAST) { |
Steven Moreland | adc2502 | 2017-10-11 12:42:19 -0700 | [diff] [blame] | 402 | if (packageInterfaces.size() == 0) { |
| 403 | return false; |
| 404 | } |
| 405 | |
Andreas Huber | 5a9fe2c | 2016-10-13 15:31:10 -0700 | [diff] [blame] | 406 | // If there is more than just a types.hal file to this package we'll |
| 407 | // definitely need to generate Java code. |
| 408 | if (packageInterfaces.size() > 1 |
| 409 | || packageInterfaces[0].name() != "types") { |
| 410 | return true; |
| 411 | } |
| 412 | |
| 413 | CHECK(typesAST != nullptr); |
| 414 | |
| 415 | // We'll have to generate Java code if types.hal contains any non-typedef |
| 416 | // type declarations. |
| 417 | |
Timur Iskhakov | cb0ba52 | 2017-07-17 20:01:37 -0700 | [diff] [blame] | 418 | Scope* rootScope = typesAST->getRootScope(); |
Andreas Huber | 5a9fe2c | 2016-10-13 15:31:10 -0700 | [diff] [blame] | 419 | std::vector<NamedType *> subTypes = rootScope->getSubTypes(); |
| 420 | |
| 421 | for (const auto &subType : subTypes) { |
| 422 | if (!subType->isTypeDef()) { |
| 423 | return true; |
| 424 | } |
| 425 | } |
| 426 | |
| 427 | return false; |
| 428 | } |
| 429 | |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 430 | bool validateIsPackage(const FQName& fqName, const Coordinator*, |
| 431 | const std::string& /* language */) { |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 432 | if (fqName.package().empty()) { |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 433 | fprintf(stderr, "ERROR: Expecting package name\n"); |
Steven Moreland | 05951b3 | 2017-05-12 17:22:22 -0700 | [diff] [blame] | 434 | return false; |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | if (fqName.version().empty()) { |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 438 | fprintf(stderr, "ERROR: Expecting package version\n"); |
Steven Moreland | 05951b3 | 2017-05-12 17:22:22 -0700 | [diff] [blame] | 439 | return false; |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | if (!fqName.name().empty()) { |
| 443 | fprintf(stderr, |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 444 | "ERROR: Expecting only package name and version.\n"); |
Steven Moreland | 05951b3 | 2017-05-12 17:22:22 -0700 | [diff] [blame] | 445 | return false; |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 446 | } |
| 447 | |
Steven Moreland | 05951b3 | 2017-05-12 17:22:22 -0700 | [diff] [blame] | 448 | return true; |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 449 | } |
| 450 | |
Steven Moreland | f7f2a9a | 2017-07-21 18:05:38 -0700 | [diff] [blame] | 451 | bool isHidlTransportPackage(const FQName& fqName) { |
Steven Moreland | e1b157e | 2018-03-06 14:18:32 -0800 | [diff] [blame] | 452 | return fqName.package() == gIBaseFqName.package() || |
| 453 | fqName.package() == gIManagerFqName.package(); |
Steven Moreland | 77cf764 | 2017-06-15 14:49:39 -0700 | [diff] [blame] | 454 | } |
| 455 | |
Steven Moreland | c7946c9 | 2017-08-08 14:48:32 -0700 | [diff] [blame] | 456 | bool isSystemProcessSupportedPackage(const FQName& fqName) { |
| 457 | // Technically, so is hidl IBase + IServiceManager, but |
| 458 | // these are part of libhidltransport. |
Steven Moreland | 3206c00 | 2019-02-11 19:51:24 -0800 | [diff] [blame] | 459 | return fqName.inPackage("android.hardware.graphics.common") || |
| 460 | fqName.inPackage("android.hardware.graphics.mapper") || |
Steven Moreland | c7946c9 | 2017-08-08 14:48:32 -0700 | [diff] [blame] | 461 | fqName.string() == "android.hardware.renderscript@1.0" || |
Howard Chen | 01dfc6d | 2017-11-30 15:37:12 +0800 | [diff] [blame] | 462 | fqName.string() == "android.hidl.memory.token@1.0" || |
Steven Moreland | 31aa250 | 2018-11-26 12:23:43 -0800 | [diff] [blame] | 463 | fqName.string() == "android.hidl.memory@1.0" || |
| 464 | fqName.string() == "android.hidl.safe_union@1.0"; |
Steven Moreland | c7946c9 | 2017-08-08 14:48:32 -0700 | [diff] [blame] | 465 | } |
| 466 | |
Steven Moreland | 14cf311 | 2018-11-29 12:23:49 -0800 | [diff] [blame] | 467 | bool isCoreAndroidPackage(const FQName& package) { |
Steven Moreland | 5edf528 | 2017-07-20 12:56:58 -0700 | [diff] [blame] | 468 | return package.inPackage("android.hidl") || |
| 469 | package.inPackage("android.system") || |
| 470 | package.inPackage("android.frameworks") || |
| 471 | package.inPackage("android.hardware"); |
| 472 | } |
| 473 | |
Steven Moreland | a815398 | 2017-11-28 15:22:28 -0800 | [diff] [blame] | 474 | // TODO(b/69862859): remove special case |
Steven Moreland | 255c9a1 | 2018-02-26 13:10:27 -0800 | [diff] [blame] | 475 | status_t isTestPackage(const FQName& fqName, const Coordinator* coordinator, bool* isTestPackage) { |
Steven Moreland | a815398 | 2017-11-28 15:22:28 -0800 | [diff] [blame] | 476 | const auto fileExists = [](const std::string& file) { |
| 477 | struct stat buf; |
| 478 | return stat(file.c_str(), &buf) == 0; |
| 479 | }; |
| 480 | |
Steven Moreland | 255c9a1 | 2018-02-26 13:10:27 -0800 | [diff] [blame] | 481 | std::string path; |
| 482 | status_t err = coordinator->getFilepath(fqName, Coordinator::Location::PACKAGE_ROOT, |
| 483 | ".hidl_for_test", &path); |
| 484 | if (err != OK) return err; |
| 485 | |
Steven Moreland | a815398 | 2017-11-28 15:22:28 -0800 | [diff] [blame] | 486 | const bool exists = fileExists(path); |
| 487 | |
| 488 | if (exists) { |
| 489 | coordinator->onFileAccess(path, "r"); |
| 490 | } |
| 491 | |
Steven Moreland | 255c9a1 | 2018-02-26 13:10:27 -0800 | [diff] [blame] | 492 | *isTestPackage = exists; |
| 493 | return OK; |
Steven Moreland | a815398 | 2017-11-28 15:22:28 -0800 | [diff] [blame] | 494 | } |
| 495 | |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 496 | static status_t generateAdapterMainSource(Formatter& out, const FQName& packageFQName, |
Steven Moreland | e6d7f09 | 2018-02-08 13:25:45 -0800 | [diff] [blame] | 497 | const Coordinator* coordinator) { |
Steven Moreland | 9a6da7a | 2017-09-15 16:21:24 -0700 | [diff] [blame] | 498 | std::vector<FQName> packageInterfaces; |
| 499 | status_t err = |
| 500 | coordinator->appendPackageInterfacesToVector(packageFQName, |
| 501 | &packageInterfaces); |
| 502 | if (err != OK) { |
| 503 | return err; |
| 504 | } |
| 505 | |
| 506 | out << "#include <hidladapter/HidlBinderAdapter.h>\n"; |
| 507 | |
| 508 | for (auto &interface : packageInterfaces) { |
| 509 | if (interface.name() == "types") { |
| 510 | continue; |
| 511 | } |
| 512 | AST::generateCppPackageInclude(out, interface, interface.getInterfaceAdapterName()); |
| 513 | } |
| 514 | |
| 515 | out << "int main(int argc, char** argv) "; |
| 516 | out.block([&] { |
| 517 | out << "return ::android::hardware::adapterMain<\n"; |
| 518 | out.indent(); |
| 519 | for (auto &interface : packageInterfaces) { |
| 520 | if (interface.name() == "types") { |
| 521 | continue; |
| 522 | } |
| 523 | out << interface.getInterfaceAdapterFqName().cppName(); |
| 524 | |
| 525 | if (&interface != &packageInterfaces.back()) { |
| 526 | out << ",\n"; |
| 527 | } |
| 528 | } |
| 529 | out << ">(\"" << packageFQName.string() << "\", argc, argv);\n"; |
| 530 | out.unindent(); |
| 531 | }).endl(); |
| 532 | return OK; |
| 533 | } |
| 534 | |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 535 | static status_t generateAndroidBpForPackage(Formatter& out, const FQName& packageFQName, |
Steven Moreland | e6d7f09 | 2018-02-08 13:25:45 -0800 | [diff] [blame] | 536 | const Coordinator* coordinator) { |
Steven Moreland | 7773089 | 2018-03-06 14:39:41 -0800 | [diff] [blame] | 537 | CHECK(!packageFQName.isFullyQualified() && packageFQName.name().empty()); |
Steven Moreland | 40069d5 | 2017-10-03 09:36:51 -0700 | [diff] [blame] | 538 | |
| 539 | std::vector<FQName> packageInterfaces; |
| 540 | |
| 541 | status_t err = coordinator->appendPackageInterfacesToVector(packageFQName, &packageInterfaces); |
| 542 | |
| 543 | if (err != OK) { |
| 544 | return err; |
| 545 | } |
| 546 | |
| 547 | std::set<FQName> importedPackagesHierarchy; |
Steven Moreland | 887b84c | 2017-10-09 18:40:58 -0700 | [diff] [blame] | 548 | std::vector<const Type *> exportedTypes; |
Steven Moreland | 40069d5 | 2017-10-03 09:36:51 -0700 | [diff] [blame] | 549 | AST* typesAST = nullptr; |
| 550 | |
| 551 | for (const auto& fqName : packageInterfaces) { |
| 552 | AST* ast = coordinator->parse(fqName); |
| 553 | |
Yi Kong | 56758da | 2018-07-24 16:21:37 -0700 | [diff] [blame] | 554 | if (ast == nullptr) { |
Steven Moreland | 40069d5 | 2017-10-03 09:36:51 -0700 | [diff] [blame] | 555 | fprintf(stderr, "ERROR: Could not parse %s. Aborting.\n", fqName.string().c_str()); |
| 556 | |
| 557 | return UNKNOWN_ERROR; |
| 558 | } |
| 559 | |
| 560 | if (fqName.name() == "types") { |
| 561 | typesAST = ast; |
| 562 | } |
| 563 | |
| 564 | ast->getImportedPackagesHierarchy(&importedPackagesHierarchy); |
Steven Moreland | 887b84c | 2017-10-09 18:40:58 -0700 | [diff] [blame] | 565 | ast->appendToExportedTypesVector(&exportedTypes); |
Steven Moreland | 40069d5 | 2017-10-03 09:36:51 -0700 | [diff] [blame] | 566 | } |
| 567 | |
Steven Moreland | 313a013 | 2017-11-03 14:23:14 -0700 | [diff] [blame] | 568 | bool needsJavaCode = packageNeedsJavaCode(packageInterfaces, typesAST); |
| 569 | |
| 570 | bool genJavaConstants = needsJavaCode && !exportedTypes.empty(); |
Steven Moreland | 887b84c | 2017-10-09 18:40:58 -0700 | [diff] [blame] | 571 | |
| 572 | bool isJavaCompatible; |
| 573 | err = isPackageJavaCompatible(packageFQName, coordinator, &isJavaCompatible); |
| 574 | if (err != OK) return err; |
Steven Moreland | 313a013 | 2017-11-03 14:23:14 -0700 | [diff] [blame] | 575 | bool genJavaLibrary = needsJavaCode && isJavaCompatible; |
| 576 | |
Steven Moreland | 255c9a1 | 2018-02-26 13:10:27 -0800 | [diff] [blame] | 577 | bool generateForTest; |
| 578 | err = isTestPackage(packageFQName, coordinator, &generateForTest); |
| 579 | if (err != OK) return err; |
| 580 | |
Steven Moreland | 14cf311 | 2018-11-29 12:23:49 -0800 | [diff] [blame] | 581 | bool isCoreAndroid = isCoreAndroidPackage(packageFQName); |
| 582 | |
| 583 | bool isVndk = !generateForTest && isCoreAndroid; |
Steven Moreland | 313a013 | 2017-11-03 14:23:14 -0700 | [diff] [blame] | 584 | bool isVndkSp = isVndk && isSystemProcessSupportedPackage(packageFQName); |
Steven Moreland | 887b84c | 2017-10-09 18:40:58 -0700 | [diff] [blame] | 585 | |
Steven Moreland | 14cf311 | 2018-11-29 12:23:49 -0800 | [diff] [blame] | 586 | // Currently, all platform-provided interfaces are in the VNDK, so if it isn't in the VNDK, it |
| 587 | // is device specific and so should be put in the product partition. |
| 588 | bool isProduct = !isCoreAndroid; |
| 589 | |
Steven Moreland | 255c9a1 | 2018-02-26 13:10:27 -0800 | [diff] [blame] | 590 | std::string packageRoot; |
| 591 | err = coordinator->getPackageRoot(packageFQName, &packageRoot); |
| 592 | if (err != OK) return err; |
| 593 | |
Steven Moreland | 313a013 | 2017-11-03 14:23:14 -0700 | [diff] [blame] | 594 | out << "// This file is autogenerated by hidl-gen -Landroidbp.\n\n"; |
Steven Moreland | 40069d5 | 2017-10-03 09:36:51 -0700 | [diff] [blame] | 595 | |
Steven Moreland | 313a013 | 2017-11-03 14:23:14 -0700 | [diff] [blame] | 596 | out << "hidl_interface "; |
Steven Moreland | 40069d5 | 2017-10-03 09:36:51 -0700 | [diff] [blame] | 597 | out.block([&] { |
Steven Moreland | 313a013 | 2017-11-03 14:23:14 -0700 | [diff] [blame] | 598 | out << "name: \"" << makeLibraryName(packageFQName) << "\",\n"; |
Steven Moreland | 89a9ebb | 2017-12-04 10:18:00 -0800 | [diff] [blame] | 599 | if (!coordinator->getOwner().empty()) { |
| 600 | out << "owner: \"" << coordinator->getOwner() << "\",\n"; |
| 601 | } |
Steven Moreland | 255c9a1 | 2018-02-26 13:10:27 -0800 | [diff] [blame] | 602 | out << "root: \"" << packageRoot << "\",\n"; |
Steven Moreland | 313a013 | 2017-11-03 14:23:14 -0700 | [diff] [blame] | 603 | if (isVndk) { |
| 604 | out << "vndk: "; |
| 605 | out.block([&]() { |
| 606 | out << "enabled: true,\n"; |
| 607 | if (isVndkSp) { |
| 608 | out << "support_system_process: true,\n"; |
| 609 | } |
| 610 | }) << ",\n"; |
| 611 | } |
Steven Moreland | 14cf311 | 2018-11-29 12:23:49 -0800 | [diff] [blame] | 612 | if (isProduct) { |
| 613 | out << "product_specific: true,\n"; |
| 614 | } |
Steven Moreland | 313a013 | 2017-11-03 14:23:14 -0700 | [diff] [blame] | 615 | (out << "srcs: [\n").indent([&] { |
Steven Moreland | 40069d5 | 2017-10-03 09:36:51 -0700 | [diff] [blame] | 616 | for (const auto& fqName : packageInterfaces) { |
| 617 | out << "\"" << fqName.name() << ".hal\",\n"; |
| 618 | } |
Steven Moreland | 313a013 | 2017-11-03 14:23:14 -0700 | [diff] [blame] | 619 | }) << "],\n"; |
| 620 | if (!importedPackagesHierarchy.empty()) { |
| 621 | (out << "interfaces: [\n").indent([&] { |
| 622 | for (const auto& fqName : importedPackagesHierarchy) { |
| 623 | out << "\"" << fqName.string() << "\",\n"; |
| 624 | } |
| 625 | }) << "],\n"; |
| 626 | } |
| 627 | if (typesAST != nullptr) { |
| 628 | (out << "types: [\n").indent([&] { |
| 629 | std::vector<NamedType *> subTypes = typesAST->getRootScope()->getSubTypes(); |
| 630 | std::sort( |
| 631 | subTypes.begin(), |
| 632 | subTypes.end(), |
| 633 | [](const NamedType *a, const NamedType *b) -> bool { |
| 634 | return a->fqName() < b->fqName(); |
| 635 | }); |
| 636 | |
| 637 | for (const auto &type : subTypes) { |
| 638 | if (type->isTypeDef()) { |
| 639 | continue; |
| 640 | } |
| 641 | |
| 642 | out << "\"" << type->localName() << "\",\n"; |
| 643 | } |
| 644 | }) << "],\n"; |
| 645 | } |
| 646 | // Explicity call this out for developers. |
| 647 | out << "gen_java: " << (genJavaLibrary ? "true" : "false") << ",\n"; |
| 648 | if (genJavaConstants) { |
| 649 | out << "gen_java_constants: true,\n"; |
| 650 | } |
Steven Moreland | 40069d5 | 2017-10-03 09:36:51 -0700 | [diff] [blame] | 651 | }).endl().endl(); |
| 652 | |
Steven Moreland | 40069d5 | 2017-10-03 09:36:51 -0700 | [diff] [blame] | 653 | return OK; |
| 654 | } |
| 655 | |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 656 | static status_t generateAndroidBpImplForPackage(Formatter& out, const FQName& packageFQName, |
Steven Moreland | e6d7f09 | 2018-02-08 13:25:45 -0800 | [diff] [blame] | 657 | const Coordinator* coordinator) { |
Iliyan Malchev | 4923f93 | 2016-09-09 13:04:59 -0700 | [diff] [blame] | 658 | const std::string libraryName = makeLibraryName(packageFQName) + "-impl"; |
Steven Moreland | 197d56c | 2016-09-09 10:03:58 -0700 | [diff] [blame] | 659 | |
| 660 | std::vector<FQName> packageInterfaces; |
| 661 | |
| 662 | status_t err = |
Steven Moreland | aa18683 | 2016-09-26 13:51:43 -0700 | [diff] [blame] | 663 | coordinator->appendPackageInterfacesToVector(packageFQName, |
| 664 | &packageInterfaces); |
Steven Moreland | 197d56c | 2016-09-09 10:03:58 -0700 | [diff] [blame] | 665 | |
| 666 | if (err != OK) { |
| 667 | return err; |
| 668 | } |
| 669 | |
| 670 | std::set<FQName> importedPackages; |
| 671 | |
| 672 | for (const auto &fqName : packageInterfaces) { |
| 673 | AST *ast = coordinator->parse(fqName); |
| 674 | |
Yi Kong | 56758da | 2018-07-24 16:21:37 -0700 | [diff] [blame] | 675 | if (ast == nullptr) { |
Steven Moreland | 197d56c | 2016-09-09 10:03:58 -0700 | [diff] [blame] | 676 | fprintf(stderr, |
| 677 | "ERROR: Could not parse %s. Aborting.\n", |
| 678 | fqName.string().c_str()); |
| 679 | |
| 680 | return UNKNOWN_ERROR; |
| 681 | } |
| 682 | |
| 683 | ast->getImportedPackages(&importedPackages); |
| 684 | } |
| 685 | |
Steven Moreland | 7fa8426 | 2018-09-26 17:12:14 -0700 | [diff] [blame] | 686 | out << "// FIXME: your file license if you have one\n\n"; |
Yifan Hong | 958ee46 | 2016-12-06 17:09:51 -0800 | [diff] [blame] | 687 | out << "cc_library_shared {\n"; |
Yifan Hong | 33223ca | 2016-12-13 15:07:35 -0800 | [diff] [blame] | 688 | out.indent([&] { |
Steven Moreland | 92f1f45 | 2018-01-04 17:55:27 -0800 | [diff] [blame] | 689 | out << "// FIXME: this should only be -impl for a passthrough hal.\n" |
| 690 | << "// In most cases, to convert this to a binderized implementation, you should:\n" |
| 691 | << "// - change '-impl' to '-service' here and make it a cc_binary instead of a\n" |
| 692 | << "// cc_library_shared.\n" |
| 693 | << "// - add a *.rc file for this module.\n" |
| 694 | << "// - delete HIDL_FETCH_I* functions.\n" |
| 695 | << "// - call configureRpcThreadpool and registerAsService on the instance.\n" |
| 696 | << "// You may also want to append '-impl/-service' with a specific identifier like\n" |
| 697 | << "// '-vendor' or '-<hardware identifier>' etc to distinguish it.\n"; |
Steven Moreland | 89a9ebb | 2017-12-04 10:18:00 -0800 | [diff] [blame] | 698 | out << "name: \"" << libraryName << "\",\n"; |
| 699 | if (!coordinator->getOwner().empty()) { |
| 700 | out << "owner: \"" << coordinator->getOwner() << "\",\n"; |
| 701 | } |
Steven Moreland | 92f1f45 | 2018-01-04 17:55:27 -0800 | [diff] [blame] | 702 | out << "relative_install_path: \"hw\",\n"; |
| 703 | if (coordinator->getOwner().empty()) { |
| 704 | out << "// FIXME: this should be 'vendor: true' for modules that will eventually be\n" |
| 705 | "// on AOSP.\n"; |
| 706 | } |
| 707 | out << "proprietary: true,\n"; |
| 708 | out << "srcs: [\n"; |
Yifan Hong | 33223ca | 2016-12-13 15:07:35 -0800 | [diff] [blame] | 709 | out.indent([&] { |
Yifan Hong | 958ee46 | 2016-12-06 17:09:51 -0800 | [diff] [blame] | 710 | for (const auto &fqName : packageInterfaces) { |
| 711 | if (fqName.name() == "types") { |
| 712 | continue; |
| 713 | } |
| 714 | out << "\"" << fqName.getInterfaceBaseName() << ".cpp\",\n"; |
| 715 | } |
| 716 | }); |
| 717 | out << "],\n" |
| 718 | << "shared_libs: [\n"; |
Yifan Hong | 33223ca | 2016-12-13 15:07:35 -0800 | [diff] [blame] | 719 | out.indent([&] { |
Yifan Hong | 958ee46 | 2016-12-06 17:09:51 -0800 | [diff] [blame] | 720 | out << "\"libhidlbase\",\n" |
| 721 | << "\"libhidltransport\",\n" |
Yifan Hong | 958ee46 | 2016-12-06 17:09:51 -0800 | [diff] [blame] | 722 | << "\"libutils\",\n" |
| 723 | << "\"" << makeLibraryName(packageFQName) << "\",\n"; |
Yifan Hong | e0f7d69 | 2016-10-20 17:51:33 -0700 | [diff] [blame] | 724 | |
Yifan Hong | 958ee46 | 2016-12-06 17:09:51 -0800 | [diff] [blame] | 725 | for (const auto &importedPackage : importedPackages) { |
Steven Moreland | dadd184 | 2017-05-09 13:24:54 -0700 | [diff] [blame] | 726 | if (isHidlTransportPackage(importedPackage)) { |
| 727 | continue; |
| 728 | } |
| 729 | |
Yifan Hong | 958ee46 | 2016-12-06 17:09:51 -0800 | [diff] [blame] | 730 | out << "\"" << makeLibraryName(importedPackage) << "\",\n"; |
| 731 | } |
| 732 | }); |
| 733 | out << "],\n"; |
| 734 | }); |
| 735 | out << "}\n"; |
Steven Moreland | 197d56c | 2016-09-09 10:03:58 -0700 | [diff] [blame] | 736 | |
| 737 | return OK; |
| 738 | } |
| 739 | |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 740 | bool validateForSource(const FQName& fqName, const Coordinator* coordinator, |
| 741 | const std::string& language) { |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 742 | if (fqName.package().empty()) { |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 743 | fprintf(stderr, "ERROR: Expecting package name\n"); |
Steven Moreland | 05951b3 | 2017-05-12 17:22:22 -0700 | [diff] [blame] | 744 | return false; |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 745 | } |
| 746 | |
| 747 | if (fqName.version().empty()) { |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 748 | fprintf(stderr, "ERROR: Expecting package version\n"); |
Steven Moreland | 05951b3 | 2017-05-12 17:22:22 -0700 | [diff] [blame] | 749 | return false; |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 750 | } |
| 751 | |
Andreas Huber | 0fa9e39 | 2016-08-31 09:05:44 -0700 | [diff] [blame] | 752 | const std::string &name = fqName.name(); |
| 753 | if (!name.empty()) { |
| 754 | if (name.find('.') == std::string::npos) { |
Steven Moreland | 05951b3 | 2017-05-12 17:22:22 -0700 | [diff] [blame] | 755 | return true; |
Andreas Huber | 0fa9e39 | 2016-08-31 09:05:44 -0700 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | if (language != "java" || name.find("types.") != 0) { |
| 759 | // When generating java sources for "types.hal", output can be |
| 760 | // constrained to just one of the top-level types declared |
| 761 | // by using the extended syntax |
| 762 | // android.hardware.Foo@1.0::types.TopLevelTypeName. |
| 763 | // In all other cases (different language, not 'types') the dot |
| 764 | // notation in the name is illegal in this context. |
Steven Moreland | 05951b3 | 2017-05-12 17:22:22 -0700 | [diff] [blame] | 765 | return false; |
Andreas Huber | 0fa9e39 | 2016-08-31 09:05:44 -0700 | [diff] [blame] | 766 | } |
| 767 | |
Steven Moreland | 05951b3 | 2017-05-12 17:22:22 -0700 | [diff] [blame] | 768 | return true; |
Andreas Huber | 0fa9e39 | 2016-08-31 09:05:44 -0700 | [diff] [blame] | 769 | } |
| 770 | |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 771 | if (language == "java") { |
| 772 | bool isJavaCompatible; |
| 773 | status_t err = isPackageJavaCompatible(fqName, coordinator, &isJavaCompatible); |
| 774 | if (err != OK) return false; |
| 775 | |
| 776 | if (!isJavaCompatible) { |
| 777 | fprintf(stderr, |
| 778 | "ERROR: %s is not Java compatible. The Java backend" |
| 779 | " does NOT support union types nor native handles. " |
| 780 | "In addition, vectors of arrays are limited to at most " |
| 781 | "one-dimensional arrays and vectors of {vectors,interfaces} are" |
| 782 | " not supported.\n", |
| 783 | fqName.string().c_str()); |
| 784 | return false; |
| 785 | } |
| 786 | } |
| 787 | |
Steven Moreland | 05951b3 | 2017-05-12 17:22:22 -0700 | [diff] [blame] | 788 | return true; |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 789 | } |
| 790 | |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 791 | FileGenerator::GenerationFunction generateExportHeaderForPackage(bool forJava) { |
| 792 | return [forJava](Formatter& out, const FQName& packageFQName, |
| 793 | const Coordinator* coordinator) -> status_t { |
Steven Moreland | 7773089 | 2018-03-06 14:39:41 -0800 | [diff] [blame] | 794 | CHECK(!packageFQName.package().empty() && !packageFQName.version().empty() && |
| 795 | packageFQName.name().empty()); |
Andreas Huber | 019d21d | 2016-10-03 12:59:47 -0700 | [diff] [blame] | 796 | |
Steven Moreland | f47912d | 2017-05-12 16:25:44 -0700 | [diff] [blame] | 797 | std::vector<FQName> packageInterfaces; |
Andreas Huber | 019d21d | 2016-10-03 12:59:47 -0700 | [diff] [blame] | 798 | |
Steven Moreland | f47912d | 2017-05-12 16:25:44 -0700 | [diff] [blame] | 799 | status_t err = coordinator->appendPackageInterfacesToVector( |
| 800 | packageFQName, &packageInterfaces); |
Andreas Huber | 019d21d | 2016-10-03 12:59:47 -0700 | [diff] [blame] | 801 | |
Steven Moreland | f47912d | 2017-05-12 16:25:44 -0700 | [diff] [blame] | 802 | if (err != OK) { |
| 803 | return err; |
Andreas Huber | 019d21d | 2016-10-03 12:59:47 -0700 | [diff] [blame] | 804 | } |
| 805 | |
Steven Moreland | f47912d | 2017-05-12 16:25:44 -0700 | [diff] [blame] | 806 | std::vector<const Type *> exportedTypes; |
Andreas Huber | 019d21d | 2016-10-03 12:59:47 -0700 | [diff] [blame] | 807 | |
Steven Moreland | f47912d | 2017-05-12 16:25:44 -0700 | [diff] [blame] | 808 | for (const auto &fqName : packageInterfaces) { |
| 809 | AST *ast = coordinator->parse(fqName); |
| 810 | |
Yi Kong | 56758da | 2018-07-24 16:21:37 -0700 | [diff] [blame] | 811 | if (ast == nullptr) { |
Steven Moreland | f47912d | 2017-05-12 16:25:44 -0700 | [diff] [blame] | 812 | fprintf(stderr, |
| 813 | "ERROR: Could not parse %s. Aborting.\n", |
| 814 | fqName.string().c_str()); |
| 815 | |
| 816 | return UNKNOWN_ERROR; |
| 817 | } |
| 818 | |
| 819 | ast->appendToExportedTypesVector(&exportedTypes); |
| 820 | } |
| 821 | |
| 822 | if (exportedTypes.empty()) { |
| 823 | return OK; |
| 824 | } |
| 825 | |
Steven Moreland | a885d25 | 2017-09-25 18:44:43 -0700 | [diff] [blame] | 826 | if (!out.isValid()) { |
| 827 | return UNKNOWN_ERROR; |
Steven Moreland | f47912d | 2017-05-12 16:25:44 -0700 | [diff] [blame] | 828 | } |
| 829 | |
Steven Moreland | 255c9a1 | 2018-02-26 13:10:27 -0800 | [diff] [blame] | 830 | std::string packagePath; |
| 831 | err = coordinator->getPackagePath(packageFQName, false /* relative */, |
| 832 | false /* sanitized */, &packagePath); |
| 833 | if (err != OK) return err; |
| 834 | |
Steven Moreland | f47912d | 2017-05-12 16:25:44 -0700 | [diff] [blame] | 835 | out << "// This file is autogenerated by hidl-gen. Do not edit manually.\n" |
| 836 | << "// Source: " << packageFQName.string() << "\n" |
Steven Moreland | 255c9a1 | 2018-02-26 13:10:27 -0800 | [diff] [blame] | 837 | << "// Location: " << packagePath << "\n\n"; |
Steven Moreland | f47912d | 2017-05-12 16:25:44 -0700 | [diff] [blame] | 838 | |
| 839 | std::string guard; |
| 840 | if (forJava) { |
| 841 | out << "package " << packageFQName.javaPackage() << ";\n\n"; |
| 842 | out << "public class Constants {\n"; |
| 843 | out.indent(); |
| 844 | } else { |
| 845 | guard = "HIDL_GENERATED_"; |
| 846 | guard += StringHelper::Uppercase(packageFQName.tokenName()); |
| 847 | guard += "_"; |
| 848 | guard += "EXPORTED_CONSTANTS_H_"; |
| 849 | |
| 850 | out << "#ifndef " |
| 851 | << guard |
| 852 | << "\n#define " |
| 853 | << guard |
| 854 | << "\n\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n\n"; |
| 855 | } |
| 856 | |
| 857 | for (const auto &type : exportedTypes) { |
| 858 | type->emitExportedHeader(out, forJava); |
| 859 | } |
| 860 | |
| 861 | if (forJava) { |
| 862 | out.unindent(); |
| 863 | out << "}\n"; |
| 864 | } else { |
| 865 | out << "#ifdef __cplusplus\n}\n#endif\n\n#endif // " |
| 866 | << guard |
| 867 | << "\n"; |
| 868 | } |
| 869 | |
Andreas Huber | 019d21d | 2016-10-03 12:59:47 -0700 | [diff] [blame] | 870 | return OK; |
Steven Moreland | f47912d | 2017-05-12 16:25:44 -0700 | [diff] [blame] | 871 | }; |
Andreas Huber | 019d21d | 2016-10-03 12:59:47 -0700 | [diff] [blame] | 872 | } |
| 873 | |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 874 | static status_t generateHashOutput(Formatter& out, const FQName& fqName, |
| 875 | const Coordinator* coordinator) { |
| 876 | CHECK(fqName.isFullyQualified()); |
Steven Moreland | f2e4469 | 2017-04-18 20:19:09 -0700 | [diff] [blame] | 877 | |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 878 | AST* ast = coordinator->parse(fqName, {} /* parsed */, |
| 879 | Coordinator::Enforce::NO_HASH /* enforcement */); |
| 880 | |
Yi Kong | 56758da | 2018-07-24 16:21:37 -0700 | [diff] [blame] | 881 | if (ast == nullptr) { |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 882 | fprintf(stderr, "ERROR: Could not parse %s. Aborting.\n", fqName.string().c_str()); |
| 883 | |
| 884 | return UNKNOWN_ERROR; |
Steven Moreland | f2e4469 | 2017-04-18 20:19:09 -0700 | [diff] [blame] | 885 | } |
| 886 | |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 887 | out << Hash::getHash(ast->getFilename()).hexString() << " " << fqName.string() << "\n"; |
Steven Moreland | f2e4469 | 2017-04-18 20:19:09 -0700 | [diff] [blame] | 888 | |
| 889 | return OK; |
| 890 | } |
| 891 | |
Steven Moreland | 55df071 | 2019-01-16 14:37:11 -0800 | [diff] [blame] | 892 | static status_t generateFunctionCount(Formatter& out, const FQName& fqName, |
| 893 | const Coordinator* coordinator) { |
| 894 | CHECK(fqName.isFullyQualified()); |
| 895 | |
| 896 | AST* ast = coordinator->parse(fqName, {} /* parsed */, |
| 897 | Coordinator::Enforce::NO_HASH /* enforcement */); |
| 898 | |
| 899 | if (ast == nullptr) { |
| 900 | fprintf(stderr, "ERROR: Could not parse %s. Aborting.\n", fqName.string().c_str()); |
| 901 | return UNKNOWN_ERROR; |
| 902 | } |
| 903 | |
| 904 | const Interface* interface = ast->getInterface(); |
| 905 | if (interface == nullptr) { |
| 906 | fprintf(stderr, "ERROR: Function count requires interface: %s.\n", fqName.string().c_str()); |
| 907 | return UNKNOWN_ERROR; |
| 908 | } |
| 909 | |
| 910 | // This is wrong for android.hidl.base@1.0::IBase, but in that case, it doesn't matter. |
| 911 | // This is just the number of APIs that are added. |
| 912 | out << fqName.string() << " " << interface->userDefinedMethods().size() << "\n"; |
| 913 | |
| 914 | return OK; |
| 915 | } |
| 916 | |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 917 | template <typename T> |
| 918 | std::vector<T> operator+(const std::vector<T>& lhs, const std::vector<T>& rhs) { |
| 919 | std::vector<T> ret; |
| 920 | ret.reserve(lhs.size() + rhs.size()); |
| 921 | ret.insert(ret.begin(), lhs.begin(), lhs.end()); |
| 922 | ret.insert(ret.end(), rhs.begin(), rhs.end()); |
| 923 | return ret; |
| 924 | } |
| 925 | |
Steven Moreland | 0241a84 | 2017-10-06 10:26:07 -0700 | [diff] [blame] | 926 | // clang-format off |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 927 | static const std::vector<FileGenerator> kCppHeaderFormats = { |
| 928 | { |
| 929 | FileGenerator::alwaysGenerate, |
| 930 | [](const FQName& fqName) { return fqName.name() + ".h"; }, |
| 931 | astGenerationFunction(&AST::generateInterfaceHeader), |
Steven Moreland | 3b1ce26 | 2017-04-21 14:19:59 -0700 | [diff] [blame] | 932 | }, |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 933 | { |
| 934 | FileGenerator::alwaysGenerate, |
| 935 | [](const FQName& fqName) { |
| 936 | return fqName.isInterfaceName() ? fqName.getInterfaceHwName() + ".h" : "hwtypes.h"; |
| 937 | }, |
| 938 | astGenerationFunction(&AST::generateHwBinderHeader), |
| 939 | }, |
| 940 | { |
| 941 | FileGenerator::generateForInterfaces, |
| 942 | [](const FQName& fqName) { return fqName.getInterfaceStubName() + ".h"; }, |
| 943 | astGenerationFunction(&AST::generateStubHeader), |
| 944 | }, |
| 945 | { |
| 946 | FileGenerator::generateForInterfaces, |
| 947 | [](const FQName& fqName) { return fqName.getInterfaceProxyName() + ".h"; }, |
| 948 | astGenerationFunction(&AST::generateProxyHeader), |
| 949 | }, |
| 950 | { |
| 951 | FileGenerator::generateForInterfaces, |
| 952 | [](const FQName& fqName) { return fqName.getInterfacePassthroughName() + ".h"; }, |
| 953 | astGenerationFunction(&AST::generatePassthroughHeader), |
| 954 | }, |
| 955 | }; |
Steven Moreland | 3b1ce26 | 2017-04-21 14:19:59 -0700 | [diff] [blame] | 956 | |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 957 | static const std::vector<FileGenerator> kCppSourceFormats = { |
| 958 | { |
| 959 | FileGenerator::alwaysGenerate, |
| 960 | [](const FQName& fqName) { |
| 961 | return fqName.isInterfaceName() ? fqName.getInterfaceBaseName() + "All.cpp" : "types.cpp"; |
| 962 | }, |
| 963 | astGenerationFunction(&AST::generateCppSource), |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 964 | }, |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 965 | }; |
Steven Moreland | 1cbf036 | 2017-05-09 14:32:53 -0700 | [diff] [blame] | 966 | |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 967 | static const std::vector<FileGenerator> kCppImplHeaderFormats = { |
| 968 | { |
| 969 | FileGenerator::generateForInterfaces, |
| 970 | [](const FQName& fqName) { return fqName.getInterfaceBaseName() + ".h"; }, |
| 971 | astGenerationFunction(&AST::generateCppImplHeader), |
Andreas Huber | 019d21d | 2016-10-03 12:59:47 -0700 | [diff] [blame] | 972 | }, |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 973 | }; |
Andreas Huber | 019d21d | 2016-10-03 12:59:47 -0700 | [diff] [blame] | 974 | |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 975 | static const std::vector<FileGenerator> kCppImplSourceFormats = { |
| 976 | { |
| 977 | FileGenerator::generateForInterfaces, |
| 978 | [](const FQName& fqName) { return fqName.getInterfaceBaseName() + ".cpp"; }, |
| 979 | astGenerationFunction(&AST::generateCppImplSource), |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 980 | }, |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 981 | }; |
Steven Moreland | 9c38761 | 2016-09-07 09:54:26 -0700 | [diff] [blame] | 982 | |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 983 | static const std::vector<FileGenerator> kCppAdapterHeaderFormats = { |
| 984 | { |
| 985 | FileGenerator::alwaysGenerate, |
| 986 | [](const FQName& fqName) { |
| 987 | return fqName.isInterfaceName() ? fqName.getInterfaceAdapterName() + ".h" : "Atypes.h"; |
| 988 | }, |
| 989 | astGenerationFunction(&AST::generateCppAdapterHeader), |
Steven Moreland | 9a6da7a | 2017-09-15 16:21:24 -0700 | [diff] [blame] | 990 | }, |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 991 | }; |
Steven Moreland | 9a6da7a | 2017-09-15 16:21:24 -0700 | [diff] [blame] | 992 | |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 993 | static const std::vector<FileGenerator> kCppAdapterSourceFormats = { |
| 994 | { |
| 995 | FileGenerator::alwaysGenerate, |
| 996 | [](const FQName& fqName) { |
| 997 | return fqName.isInterfaceName() ? fqName.getInterfaceAdapterName() + ".cpp" : "Atypes.cpp"; |
| 998 | }, |
| 999 | astGenerationFunction(&AST::generateCppAdapterSource), |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 1000 | }, |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 1001 | }; |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 1002 | |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 1003 | static const std::vector<OutputHandler> kFormats = { |
| 1004 | { |
| 1005 | "check", |
| 1006 | "Parses the interface to see if valid but doesn't write any files.", |
| 1007 | OutputMode::NOT_NEEDED, |
| 1008 | Coordinator::Location::STANDARD_OUT, |
| 1009 | GenerationGranularity::PER_FILE, |
| 1010 | validateForSource, |
| 1011 | { |
| 1012 | { |
| 1013 | FileGenerator::alwaysGenerate, |
| 1014 | nullptr /* filename for fqname */, |
| 1015 | astGenerationFunction(), |
| 1016 | }, |
| 1017 | }, |
Andreas Huber | 1c50727 | 2016-10-05 14:33:21 -0700 | [diff] [blame] | 1018 | }, |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 1019 | { |
| 1020 | "c++", |
| 1021 | "(internal) (deprecated) Generates C++ interface files for talking to HIDL interfaces.", |
| 1022 | OutputMode::NEEDS_DIR, |
| 1023 | Coordinator::Location::GEN_OUTPUT, |
| 1024 | GenerationGranularity::PER_FILE, |
| 1025 | validateForSource, |
| 1026 | kCppHeaderFormats + kCppSourceFormats, |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 1027 | }, |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 1028 | { |
| 1029 | "c++-headers", |
| 1030 | "(internal) Generates C++ headers for interface files for talking to HIDL interfaces.", |
| 1031 | OutputMode::NEEDS_DIR, |
| 1032 | Coordinator::Location::GEN_OUTPUT, |
| 1033 | GenerationGranularity::PER_FILE, |
| 1034 | validateForSource, |
| 1035 | kCppHeaderFormats, |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 1036 | }, |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 1037 | { |
| 1038 | "c++-sources", |
| 1039 | "(internal) Generates C++ sources for interface files for talking to HIDL interfaces.", |
| 1040 | OutputMode::NEEDS_DIR, |
| 1041 | Coordinator::Location::GEN_OUTPUT, |
| 1042 | GenerationGranularity::PER_FILE, |
| 1043 | validateForSource, |
| 1044 | kCppSourceFormats, |
Dan Willemsen | 676abdc | 2016-09-28 19:42:22 -0700 | [diff] [blame] | 1045 | }, |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 1046 | { |
| 1047 | "export-header", |
| 1048 | "Generates a header file from @export enumerations to help maintain legacy code.", |
| 1049 | OutputMode::NEEDS_FILE, |
| 1050 | Coordinator::Location::DIRECT, |
| 1051 | GenerationGranularity::PER_PACKAGE, |
| 1052 | validateIsPackage, |
| 1053 | {singleFileGenerator("", generateExportHeaderForPackage(false /* forJava */))} |
Steven Moreland | f2e4469 | 2017-04-18 20:19:09 -0700 | [diff] [blame] | 1054 | }, |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 1055 | { |
| 1056 | "c++-impl", |
| 1057 | "Generates boilerplate implementation of a hidl interface in C++ (for convenience).", |
| 1058 | OutputMode::NEEDS_DIR, |
| 1059 | Coordinator::Location::DIRECT, |
| 1060 | GenerationGranularity::PER_FILE, |
| 1061 | validateForSource, |
| 1062 | kCppImplHeaderFormats + kCppImplSourceFormats, |
| 1063 | }, |
| 1064 | { |
| 1065 | "c++-impl-headers", |
Alessio Balsini | 56a0c83 | 2019-01-10 12:53:01 +0000 | [diff] [blame] | 1066 | "c++-impl but headers only.", |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 1067 | OutputMode::NEEDS_DIR, |
| 1068 | Coordinator::Location::DIRECT, |
| 1069 | GenerationGranularity::PER_FILE, |
| 1070 | validateForSource, |
| 1071 | kCppImplHeaderFormats, |
| 1072 | }, |
| 1073 | { |
| 1074 | "c++-impl-sources", |
Alessio Balsini | 56a0c83 | 2019-01-10 12:53:01 +0000 | [diff] [blame] | 1075 | "c++-impl but sources only.", |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 1076 | OutputMode::NEEDS_DIR, |
| 1077 | Coordinator::Location::DIRECT, |
| 1078 | GenerationGranularity::PER_FILE, |
| 1079 | validateForSource, |
| 1080 | kCppImplSourceFormats, |
| 1081 | }, |
| 1082 | { |
| 1083 | "c++-adapter", |
| 1084 | "Takes a x.(y+n) interface and mocks an x.y interface.", |
| 1085 | OutputMode::NEEDS_DIR, |
| 1086 | Coordinator::Location::GEN_OUTPUT, |
| 1087 | GenerationGranularity::PER_FILE, |
| 1088 | validateForSource, |
| 1089 | kCppAdapterHeaderFormats + kCppAdapterSourceFormats, |
| 1090 | }, |
| 1091 | { |
| 1092 | "c++-adapter-headers", |
Alessio Balsini | 56a0c83 | 2019-01-10 12:53:01 +0000 | [diff] [blame] | 1093 | "c++-adapter but helper headers only.", |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 1094 | OutputMode::NEEDS_DIR, |
| 1095 | Coordinator::Location::GEN_OUTPUT, |
| 1096 | GenerationGranularity::PER_FILE, |
| 1097 | validateForSource, |
| 1098 | kCppAdapterHeaderFormats, |
| 1099 | }, |
| 1100 | { |
| 1101 | "c++-adapter-sources", |
Alessio Balsini | 56a0c83 | 2019-01-10 12:53:01 +0000 | [diff] [blame] | 1102 | "c++-adapter but helper sources only.", |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 1103 | OutputMode::NEEDS_DIR, |
| 1104 | Coordinator::Location::GEN_OUTPUT, |
| 1105 | GenerationGranularity::PER_FILE, |
| 1106 | validateForSource, |
| 1107 | kCppAdapterSourceFormats, |
| 1108 | }, |
| 1109 | { |
| 1110 | "c++-adapter-main", |
Alessio Balsini | 56a0c83 | 2019-01-10 12:53:01 +0000 | [diff] [blame] | 1111 | "c++-adapter but the adapter binary source only.", |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 1112 | OutputMode::NEEDS_DIR, |
| 1113 | Coordinator::Location::DIRECT, |
| 1114 | GenerationGranularity::PER_PACKAGE, |
| 1115 | validateIsPackage, |
| 1116 | {singleFileGenerator("main.cpp", generateAdapterMainSource)}, |
| 1117 | }, |
| 1118 | { |
| 1119 | "java", |
| 1120 | "(internal) Generates Java library for talking to HIDL interfaces in Java.", |
| 1121 | OutputMode::NEEDS_DIR, |
| 1122 | Coordinator::Location::GEN_SANITIZED, |
| 1123 | GenerationGranularity::PER_TYPE, |
| 1124 | validateForSource, |
| 1125 | { |
| 1126 | { |
| 1127 | FileGenerator::alwaysGenerate, |
| 1128 | [](const FQName& fqName) { |
| 1129 | return StringHelper::LTrim(fqName.name(), "types.") + ".java"; |
| 1130 | }, |
| 1131 | generateJavaForPackage, |
| 1132 | }, |
| 1133 | } |
| 1134 | }, |
| 1135 | { |
| 1136 | "java-constants", |
| 1137 | "(internal) Like export-header but for Java (always created by -Lmakefile if @export exists).", |
| 1138 | OutputMode::NEEDS_DIR, |
| 1139 | Coordinator::Location::GEN_SANITIZED, |
| 1140 | GenerationGranularity::PER_PACKAGE, |
| 1141 | validateIsPackage, |
| 1142 | {singleFileGenerator("Constants.java", generateExportHeaderForPackage(true /* forJava */))} |
| 1143 | }, |
| 1144 | { |
| 1145 | "vts", |
| 1146 | "(internal) Generates vts proto files for use in vtsd.", |
| 1147 | OutputMode::NEEDS_DIR, |
| 1148 | Coordinator::Location::GEN_OUTPUT, |
| 1149 | GenerationGranularity::PER_FILE, |
| 1150 | validateForSource, |
| 1151 | { |
| 1152 | { |
| 1153 | FileGenerator::alwaysGenerate, |
| 1154 | [](const FQName& fqName) { |
| 1155 | return fqName.isInterfaceName() ? fqName.getInterfaceBaseName() + ".vts" : "types.vts"; |
| 1156 | }, |
| 1157 | astGenerationFunction(&AST::generateVts), |
| 1158 | }, |
| 1159 | } |
| 1160 | }, |
| 1161 | { |
| 1162 | "makefile", |
| 1163 | "(removed) Used to generate makefiles for -Ljava and -Ljava-constants.", |
| 1164 | OutputMode::NEEDS_SRC, |
| 1165 | Coordinator::Location::PACKAGE_ROOT, |
| 1166 | GenerationGranularity::PER_PACKAGE, |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 1167 | [](const FQName &, const Coordinator*, const std::string &) { |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 1168 | fprintf(stderr, "ERROR: makefile output is not supported. Use -Landroidbp for all build file generation.\n"); |
| 1169 | return false; |
| 1170 | }, |
| 1171 | {}, |
| 1172 | }, |
| 1173 | { |
| 1174 | "androidbp", |
| 1175 | "(internal) Generates Soong bp files for -Lc++-headers, -Lc++-sources, -Ljava, -Ljava-constants, and -Lc++-adapter.", |
| 1176 | OutputMode::NEEDS_SRC, |
| 1177 | Coordinator::Location::PACKAGE_ROOT, |
| 1178 | GenerationGranularity::PER_PACKAGE, |
| 1179 | validateIsPackage, |
| 1180 | {singleFileGenerator("Android.bp", generateAndroidBpForPackage)}, |
| 1181 | }, |
| 1182 | { |
| 1183 | "androidbp-impl", |
| 1184 | "Generates boilerplate bp files for implementation created with -Lc++-impl.", |
| 1185 | OutputMode::NEEDS_DIR, |
| 1186 | Coordinator::Location::DIRECT, |
| 1187 | GenerationGranularity::PER_PACKAGE, |
| 1188 | validateIsPackage, |
| 1189 | {singleFileGenerator("Android.bp", generateAndroidBpImplForPackage)}, |
| 1190 | }, |
| 1191 | { |
| 1192 | "hash", |
| 1193 | "Prints hashes of interface in `current.txt` format to standard out.", |
| 1194 | OutputMode::NOT_NEEDED, |
| 1195 | Coordinator::Location::STANDARD_OUT, |
| 1196 | GenerationGranularity::PER_FILE, |
| 1197 | validateForSource, |
| 1198 | { |
| 1199 | { |
| 1200 | FileGenerator::alwaysGenerate, |
| 1201 | nullptr /* file name for fqName */, |
| 1202 | generateHashOutput, |
| 1203 | }, |
| 1204 | } |
Steven Moreland | f2e4469 | 2017-04-18 20:19:09 -0700 | [diff] [blame] | 1205 | }, |
Yifan Hong | e401011 | 2018-10-05 11:44:15 -0700 | [diff] [blame] | 1206 | { |
Steven Moreland | 55df071 | 2019-01-16 14:37:11 -0800 | [diff] [blame] | 1207 | "function-count", |
| 1208 | "Prints the total number of functions added by the package or interface.", |
| 1209 | OutputMode::NOT_NEEDED, |
| 1210 | Coordinator::Location::STANDARD_OUT, |
| 1211 | GenerationGranularity::PER_FILE, |
| 1212 | validateForSource, |
| 1213 | { |
| 1214 | { |
| 1215 | FileGenerator::generateForInterfaces, |
| 1216 | nullptr /* file name for fqName */, |
| 1217 | generateFunctionCount, |
| 1218 | }, |
| 1219 | } |
| 1220 | }, |
| 1221 | { |
Yifan Hong | e401011 | 2018-10-05 11:44:15 -0700 | [diff] [blame] | 1222 | "dependencies", |
| 1223 | "Prints all depended types.", |
| 1224 | OutputMode::NOT_NEEDED, |
| 1225 | Coordinator::Location::STANDARD_OUT, |
| 1226 | GenerationGranularity::PER_FILE, |
| 1227 | validateForSource, |
| 1228 | { |
| 1229 | { |
| 1230 | FileGenerator::alwaysGenerate, |
| 1231 | nullptr /* file name for fqName */, |
| 1232 | astGenerationFunction(&AST::generateDependencies), |
| 1233 | }, |
| 1234 | }, |
| 1235 | }, |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 1236 | }; |
Steven Moreland | 0241a84 | 2017-10-06 10:26:07 -0700 | [diff] [blame] | 1237 | // clang-format on |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 1238 | |
| 1239 | static void usage(const char *me) { |
| 1240 | fprintf(stderr, |
Steven Moreland | 89a9ebb | 2017-12-04 10:18:00 -0800 | [diff] [blame] | 1241 | "usage: %s [-p <root path>] -o <output path> -L <language> [-O <owner>] (-r <interface " |
Yifan Hong | 5418c11 | 2018-10-09 17:30:22 -0700 | [diff] [blame] | 1242 | "root>)+ [-R] [-v] [-d <depfile>] FQNAME...\n\n", |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 1243 | me); |
| 1244 | |
Steven Moreland | a12df11 | 2018-02-15 14:32:46 -0800 | [diff] [blame] | 1245 | fprintf(stderr, |
| 1246 | "Process FQNAME, PACKAGE(.SUBPACKAGE)*@[0-9]+.[0-9]+(::TYPE)?, to create output.\n\n"); |
| 1247 | |
Steven Moreland | 97e5233 | 2017-05-12 16:47:19 -0700 | [diff] [blame] | 1248 | fprintf(stderr, " -h: Prints this menu.\n"); |
| 1249 | fprintf(stderr, " -L <language>: The following options are available:\n"); |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 1250 | for (auto& e : kFormats) { |
Steven Moreland | 97e5233 | 2017-05-12 16:47:19 -0700 | [diff] [blame] | 1251 | fprintf(stderr, " %-16s: %s\n", e.name().c_str(), e.description().c_str()); |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 1252 | } |
Steven Moreland | 89a9ebb | 2017-12-04 10:18:00 -0800 | [diff] [blame] | 1253 | fprintf(stderr, " -O <owner>: The owner of the module for -Landroidbp(-impl)?.\n"); |
Steven Moreland | 97e5233 | 2017-05-12 16:47:19 -0700 | [diff] [blame] | 1254 | fprintf(stderr, " -o <output path>: Location to output files.\n"); |
| 1255 | fprintf(stderr, " -p <root path>: Android build root, defaults to $ANDROID_BUILD_TOP or pwd.\n"); |
Alessio Balsini | 56a0c83 | 2019-01-10 12:53:01 +0000 | [diff] [blame] | 1256 | fprintf(stderr, " -R: Do not add default package roots if not specified in -r.\n"); |
Steven Moreland | 97e5233 | 2017-05-12 16:47:19 -0700 | [diff] [blame] | 1257 | fprintf(stderr, " -r <package:path root>: E.g., android.hardware:hardware/interfaces.\n"); |
Steven Moreland | a838ceb | 2018-02-06 13:19:23 -0800 | [diff] [blame] | 1258 | fprintf(stderr, " -v: verbose output.\n"); |
Steven Moreland | 394af5c | 2018-02-09 14:41:46 -0800 | [diff] [blame] | 1259 | fprintf(stderr, " -d <depfile>: location of depfile to write to.\n"); |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 1260 | } |
| 1261 | |
Andreas Gampe | c4ce926 | 2017-04-27 21:03:20 -0700 | [diff] [blame] | 1262 | // hidl is intentionally leaky. Turn off LeakSanitizer by default. |
| 1263 | extern "C" const char *__asan_default_options() { |
Evgenii Stepanov | a3ee97c | 2017-08-16 14:59:29 -0700 | [diff] [blame] | 1264 | return "detect_leaks=0"; |
Andreas Gampe | c4ce926 | 2017-04-27 21:03:20 -0700 | [diff] [blame] | 1265 | } |
| 1266 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 1267 | int main(int argc, char **argv) { |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 1268 | const char *me = argv[0]; |
Steven Moreland | 3db99f2 | 2017-05-11 16:21:46 -0700 | [diff] [blame] | 1269 | if (argc == 1) { |
| 1270 | usage(me); |
| 1271 | exit(1); |
| 1272 | } |
| 1273 | |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 1274 | const OutputHandler* outputFormat = nullptr; |
Steven Moreland | 47792c4 | 2017-09-20 10:03:20 -0700 | [diff] [blame] | 1275 | Coordinator coordinator; |
| 1276 | std::string outputPath; |
Yifan Hong | 5418c11 | 2018-10-09 17:30:22 -0700 | [diff] [blame] | 1277 | bool suppressDefaultPackagePaths = false; |
Steven Moreland | 47792c4 | 2017-09-20 10:03:20 -0700 | [diff] [blame] | 1278 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 1279 | int res; |
Yifan Hong | 5418c11 | 2018-10-09 17:30:22 -0700 | [diff] [blame] | 1280 | while ((res = getopt(argc, argv, "hp:o:O:r:L:vd:R")) >= 0) { |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 1281 | switch (res) { |
Steven Moreland | 394af5c | 2018-02-09 14:41:46 -0800 | [diff] [blame] | 1282 | case 'p': { |
Steven Moreland | 0d93b14 | 2018-01-04 17:26:30 -0800 | [diff] [blame] | 1283 | if (!coordinator.getRootPath().empty()) { |
| 1284 | fprintf(stderr, "ERROR: -p <root path> can only be specified once.\n"); |
| 1285 | exit(1); |
| 1286 | } |
Steven Moreland | 47792c4 | 2017-09-20 10:03:20 -0700 | [diff] [blame] | 1287 | coordinator.setRootPath(optarg); |
Steven Moreland | f7fa068 | 2017-05-11 16:14:55 -0700 | [diff] [blame] | 1288 | break; |
| 1289 | } |
| 1290 | |
Steven Moreland | 394af5c | 2018-02-09 14:41:46 -0800 | [diff] [blame] | 1291 | case 'v': { |
Steven Moreland | 37c57ee | 2017-09-25 19:08:56 -0700 | [diff] [blame] | 1292 | coordinator.setVerbose(true); |
| 1293 | break; |
| 1294 | } |
| 1295 | |
Steven Moreland | 394af5c | 2018-02-09 14:41:46 -0800 | [diff] [blame] | 1296 | case 'd': { |
| 1297 | coordinator.setDepFile(optarg); |
| 1298 | break; |
| 1299 | } |
| 1300 | |
| 1301 | case 'o': { |
Steven Moreland | 0d93b14 | 2018-01-04 17:26:30 -0800 | [diff] [blame] | 1302 | if (!outputPath.empty()) { |
| 1303 | fprintf(stderr, "ERROR: -o <output path> can only be specified once.\n"); |
| 1304 | exit(1); |
| 1305 | } |
Andreas Huber | 019d21d | 2016-10-03 12:59:47 -0700 | [diff] [blame] | 1306 | outputPath = optarg; |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 1307 | break; |
| 1308 | } |
| 1309 | |
Steven Moreland | 89a9ebb | 2017-12-04 10:18:00 -0800 | [diff] [blame] | 1310 | case 'O': { |
| 1311 | if (!coordinator.getOwner().empty()) { |
| 1312 | fprintf(stderr, "ERROR: -O <owner> can only be specified once.\n"); |
| 1313 | exit(1); |
| 1314 | } |
| 1315 | coordinator.setOwner(optarg); |
| 1316 | break; |
| 1317 | } |
| 1318 | |
Steven Moreland | 394af5c | 2018-02-09 14:41:46 -0800 | [diff] [blame] | 1319 | case 'r': { |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 1320 | std::string val(optarg); |
| 1321 | auto index = val.find_first_of(':'); |
Steven Moreland | 4ff7420 | 2017-04-21 14:24:47 -0700 | [diff] [blame] | 1322 | if (index == std::string::npos) { |
| 1323 | fprintf(stderr, "ERROR: -r option must contain ':': %s\n", val.c_str()); |
| 1324 | exit(1); |
| 1325 | } |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 1326 | |
Steven Moreland | 13594d1 | 2017-09-28 23:27:50 +0000 | [diff] [blame] | 1327 | auto root = val.substr(0, index); |
| 1328 | auto path = val.substr(index + 1); |
Steven Moreland | 47792c4 | 2017-09-20 10:03:20 -0700 | [diff] [blame] | 1329 | |
| 1330 | std::string error; |
| 1331 | status_t err = coordinator.addPackagePath(root, path, &error); |
| 1332 | if (err != OK) { |
| 1333 | fprintf(stderr, "%s\n", error.c_str()); |
| 1334 | exit(1); |
| 1335 | } |
| 1336 | |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 1337 | break; |
| 1338 | } |
| 1339 | |
Yifan Hong | 5418c11 | 2018-10-09 17:30:22 -0700 | [diff] [blame] | 1340 | case 'R': { |
| 1341 | suppressDefaultPackagePaths = true; |
| 1342 | break; |
| 1343 | } |
| 1344 | |
Steven Moreland | 394af5c | 2018-02-09 14:41:46 -0800 | [diff] [blame] | 1345 | case 'L': { |
Steven Moreland | dd58384 | 2017-04-19 13:09:57 -0700 | [diff] [blame] | 1346 | if (outputFormat != nullptr) { |
| 1347 | fprintf(stderr, |
| 1348 | "ERROR: only one -L option allowed. \"%s\" already specified.\n", |
| 1349 | outputFormat->name().c_str()); |
| 1350 | exit(1); |
| 1351 | } |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 1352 | for (auto& e : kFormats) { |
Steven Moreland | dd58384 | 2017-04-19 13:09:57 -0700 | [diff] [blame] | 1353 | if (e.name() == optarg) { |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 1354 | outputFormat = &e; |
| 1355 | break; |
| 1356 | } |
| 1357 | } |
Steven Moreland | dd58384 | 2017-04-19 13:09:57 -0700 | [diff] [blame] | 1358 | if (outputFormat == nullptr) { |
| 1359 | fprintf(stderr, |
| 1360 | "ERROR: unrecognized -L option: \"%s\".\n", |
| 1361 | optarg); |
| 1362 | exit(1); |
| 1363 | } |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 1364 | break; |
| 1365 | } |
| 1366 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 1367 | case '?': |
| 1368 | case 'h': |
Steven Moreland | 394af5c | 2018-02-09 14:41:46 -0800 | [diff] [blame] | 1369 | default: { |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 1370 | usage(me); |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 1371 | exit(1); |
| 1372 | break; |
| 1373 | } |
| 1374 | } |
| 1375 | } |
| 1376 | |
Steven Moreland | 0d93b14 | 2018-01-04 17:26:30 -0800 | [diff] [blame] | 1377 | if (coordinator.getRootPath().empty()) { |
| 1378 | const char* ANDROID_BUILD_TOP = getenv("ANDROID_BUILD_TOP"); |
| 1379 | if (ANDROID_BUILD_TOP != nullptr) { |
| 1380 | coordinator.setRootPath(ANDROID_BUILD_TOP); |
| 1381 | } |
| 1382 | } |
| 1383 | |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 1384 | if (outputFormat == nullptr) { |
Steven Moreland | dd58384 | 2017-04-19 13:09:57 -0700 | [diff] [blame] | 1385 | fprintf(stderr, |
| 1386 | "ERROR: no -L option provided.\n"); |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 1387 | exit(1); |
| 1388 | } |
| 1389 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 1390 | argc -= optind; |
| 1391 | argv += optind; |
| 1392 | |
Steven Moreland | 3effa83 | 2017-06-16 16:17:41 -0700 | [diff] [blame] | 1393 | if (argc == 0) { |
| 1394 | fprintf(stderr, "ERROR: no fqname specified.\n"); |
| 1395 | usage(me); |
| 1396 | exit(1); |
| 1397 | } |
| 1398 | |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 1399 | // Valid options are now in argv[0] .. argv[argc - 1]. |
| 1400 | |
Andreas Huber | 019d21d | 2016-10-03 12:59:47 -0700 | [diff] [blame] | 1401 | switch (outputFormat->mOutputMode) { |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 1402 | case OutputMode::NEEDS_DIR: |
| 1403 | case OutputMode::NEEDS_FILE: { |
Andreas Huber | 019d21d | 2016-10-03 12:59:47 -0700 | [diff] [blame] | 1404 | if (outputPath.empty()) { |
| 1405 | usage(me); |
| 1406 | exit(1); |
| 1407 | } |
| 1408 | |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 1409 | if (outputFormat->mOutputMode == OutputMode::NEEDS_DIR) { |
Howard Chen | 30f6b49 | 2017-08-24 16:50:12 +0800 | [diff] [blame] | 1410 | if (outputPath.back() != '/') { |
Andreas Huber | 019d21d | 2016-10-03 12:59:47 -0700 | [diff] [blame] | 1411 | outputPath += "/"; |
| 1412 | } |
| 1413 | } |
| 1414 | break; |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 1415 | } |
Steven Moreland | 5abcf01 | 2018-02-08 18:50:18 -0800 | [diff] [blame] | 1416 | case OutputMode::NEEDS_SRC: { |
Steven Moreland | a171b56 | 2017-05-12 15:18:03 -0700 | [diff] [blame] | 1417 | if (outputPath.empty()) { |
Steven Moreland | 47792c4 | 2017-09-20 10:03:20 -0700 | [diff] [blame] | 1418 | outputPath = coordinator.getRootPath(); |
Steven Moreland | a171b56 | 2017-05-12 15:18:03 -0700 | [diff] [blame] | 1419 | } |
Howard Chen | 30f6b49 | 2017-08-24 16:50:12 +0800 | [diff] [blame] | 1420 | if (outputPath.back() != '/') { |
| 1421 | outputPath += "/"; |
| 1422 | } |
Steven Moreland | a171b56 | 2017-05-12 15:18:03 -0700 | [diff] [blame] | 1423 | |
| 1424 | break; |
| 1425 | } |
Andreas Huber | 019d21d | 2016-10-03 12:59:47 -0700 | [diff] [blame] | 1426 | |
| 1427 | default: |
| 1428 | outputPath.clear(); // Unused. |
| 1429 | break; |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 1430 | } |
| 1431 | |
Steven Moreland | 6d3d3c8 | 2018-02-08 11:49:34 -0800 | [diff] [blame] | 1432 | coordinator.setOutputPath(outputPath); |
| 1433 | |
Yifan Hong | 5418c11 | 2018-10-09 17:30:22 -0700 | [diff] [blame] | 1434 | if (!suppressDefaultPackagePaths) { |
| 1435 | coordinator.addDefaultPackagePath("android.hardware", "hardware/interfaces"); |
| 1436 | coordinator.addDefaultPackagePath("android.hidl", "system/libhidl/transport"); |
| 1437 | coordinator.addDefaultPackagePath("android.frameworks", "frameworks/hardware/interfaces"); |
| 1438 | coordinator.addDefaultPackagePath("android.system", "system/hardware/interfaces"); |
| 1439 | } |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 1440 | |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 1441 | for (int i = 0; i < argc; ++i) { |
Steven Moreland | e7d3210 | 2018-12-03 17:03:36 -0800 | [diff] [blame] | 1442 | const char* arg = argv[i]; |
| 1443 | |
Steven Moreland | e1b157e | 2018-03-06 14:18:32 -0800 | [diff] [blame] | 1444 | FQName fqName; |
Steven Moreland | e7d3210 | 2018-12-03 17:03:36 -0800 | [diff] [blame] | 1445 | if (!FQName::parse(arg, &fqName)) { |
| 1446 | fprintf(stderr, "ERROR: Invalid fully-qualified name as argument: %s.\n", arg); |
| 1447 | exit(1); |
| 1448 | } |
| 1449 | |
| 1450 | if (coordinator.getPackageInterfaceFiles(fqName, nullptr /*fileNames*/) != OK) { |
| 1451 | fprintf(stderr, "ERROR: Could not get sources for %s.\n", arg); |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 1452 | exit(1); |
| 1453 | } |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 1454 | |
Steven Moreland | b65e5d9 | 2018-02-08 12:44:51 -0800 | [diff] [blame] | 1455 | // Dump extra verbose output |
| 1456 | if (coordinator.isVerbose()) { |
| 1457 | status_t err = |
| 1458 | dumpDefinedButUnreferencedTypeNames(fqName.getPackageAndVersion(), &coordinator); |
| 1459 | if (err != OK) return err; |
| 1460 | } |
| 1461 | |
Steven Moreland | 6ec9eb9 | 2018-02-16 14:21:49 -0800 | [diff] [blame] | 1462 | if (!outputFormat->validate(fqName, &coordinator, outputFormat->name())) { |
Steven Moreland | f2e4469 | 2017-04-18 20:19:09 -0700 | [diff] [blame] | 1463 | fprintf(stderr, |
| 1464 | "ERROR: output handler failed.\n"); |
Andreas Huber | e61e3f7 | 2016-08-03 10:22:03 -0700 | [diff] [blame] | 1465 | exit(1); |
| 1466 | } |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 1467 | |
Steven Moreland | 1125cf3 | 2018-02-08 13:15:27 -0800 | [diff] [blame] | 1468 | status_t err = outputFormat->generate(fqName, &coordinator); |
Steven Moreland | 394af5c | 2018-02-09 14:41:46 -0800 | [diff] [blame] | 1469 | if (err != OK) exit(1); |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 1470 | |
Steven Moreland | 394af5c | 2018-02-09 14:41:46 -0800 | [diff] [blame] | 1471 | err = outputFormat->writeDepFile(fqName, &coordinator); |
| 1472 | if (err != OK) exit(1); |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 1473 | } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 1474 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 1475 | return 0; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 1476 | } |