Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 1 | #include "AST.h" |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 2 | #include "Coordinator.h" |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 3 | #include "Formatter.h" |
Andreas Huber | 84f89de | 2016-07-28 15:39:51 -0700 | [diff] [blame] | 4 | #include "FQName.h" |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 5 | |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 6 | #include <android-base/logging.h> |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 7 | #include <set> |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 8 | #include <stdio.h> |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 9 | #include <string> |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 10 | #include <unistd.h> |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 11 | #include <vector> |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 12 | |
| 13 | using namespace android; |
| 14 | |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 15 | struct OutputHandler { |
| 16 | std::string mKey; |
| 17 | bool mNeedsOutputDir; |
| 18 | enum ValRes { |
| 19 | FAILED, |
| 20 | PASS_PACKAGE, |
| 21 | PASS_FULL |
| 22 | }; |
| 23 | ValRes (*validate)(const FQName &); |
| 24 | status_t (*generate)(const FQName &fqName, |
| 25 | const char *hidl_gen, |
| 26 | Coordinator *coordinator, |
| 27 | const std::string &outputDir); |
| 28 | }; |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 29 | |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 30 | static status_t generateSourcesForFile( |
| 31 | const FQName &fqName, |
| 32 | const char *, |
| 33 | Coordinator *coordinator, |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 34 | const std::string &outputDir, |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 35 | const std::string &lang) { |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 36 | CHECK(fqName.isFullyQualified()); |
| 37 | |
| 38 | AST *ast = coordinator->parse(fqName); |
| 39 | |
| 40 | if (ast == NULL) { |
| 41 | fprintf(stderr, |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 42 | "ERROR: Could not parse %s. Aborting.\n", |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 43 | fqName.string().c_str()); |
| 44 | |
| 45 | return UNKNOWN_ERROR; |
| 46 | } |
| 47 | |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 48 | if (lang == "c++") { |
| 49 | return ast->generateCpp(outputDir); |
| 50 | } |
| 51 | if (lang == "java") { |
| 52 | return ast->generateJava(outputDir); |
| 53 | } |
| 54 | if (lang == "vts") { |
| 55 | return ast->generateVts(outputDir); |
| 56 | } |
| 57 | // Unknown language. |
| 58 | return UNKNOWN_ERROR; |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | static status_t generateSourcesForPackage( |
| 62 | const FQName &packageFQName, |
| 63 | const char *hidl_gen, |
| 64 | Coordinator *coordinator, |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 65 | const std::string &outputDir, |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 66 | const std::string &lang) { |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 67 | CHECK(packageFQName.isValid() && |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 68 | !packageFQName.isFullyQualified() && |
| 69 | packageFQName.name().empty()); |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 70 | |
| 71 | std::vector<FQName> packageInterfaces; |
| 72 | |
| 73 | status_t err = |
| 74 | coordinator->appendPackageInterfacesToSet(packageFQName, |
| 75 | &packageInterfaces); |
| 76 | |
| 77 | if (err != OK) { |
| 78 | return err; |
| 79 | } |
| 80 | |
| 81 | for (const auto &fqName : packageInterfaces) { |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 82 | err = generateSourcesForFile( |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 83 | fqName, hidl_gen, coordinator, outputDir, lang); |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 84 | if (err != OK) { |
| 85 | return err; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | return OK; |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 90 | } |
| 91 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 92 | static std::string makeLibraryName(const FQName &packageFQName) { |
Iliyan Malchev | e2c595a | 2016-08-07 21:20:04 -0700 | [diff] [blame] | 93 | return packageFQName.string(); |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 94 | } |
| 95 | |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 96 | static status_t generateMakefileForPackage( |
| 97 | const FQName &packageFQName, |
Iliyan Malchev | b66c399 | 2016-08-07 21:18:13 -0700 | [diff] [blame] | 98 | const char *hidl_gen, |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 99 | Coordinator *coordinator, |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 100 | const std::string &) { |
| 101 | |
| 102 | CHECK(packageFQName.isValid() && |
| 103 | !packageFQName.isFullyQualified() && |
| 104 | packageFQName.name().empty()); |
| 105 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 106 | std::vector<FQName> packageInterfaces; |
| 107 | |
| 108 | status_t err = |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 109 | coordinator->appendPackageInterfacesToSet(packageFQName, |
| 110 | &packageInterfaces); |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 111 | |
| 112 | if (err != OK) { |
| 113 | return err; |
| 114 | } |
| 115 | |
| 116 | std::set<FQName> importedPackages; |
| 117 | for (const auto &fqName : packageInterfaces) { |
| 118 | AST *ast = coordinator->parse(fqName); |
| 119 | |
| 120 | if (ast == NULL) { |
| 121 | fprintf(stderr, |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 122 | "ERROR: Could not parse %s. Aborting.\n", |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 123 | fqName.string().c_str()); |
| 124 | |
| 125 | return UNKNOWN_ERROR; |
| 126 | } |
| 127 | |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 128 | ast->getImportedPackages(&importedPackages); |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | std::string path = |
| 132 | coordinator->getPackagePath(packageFQName, false /* relative */); |
| 133 | |
| 134 | path.append("Android.mk"); |
| 135 | |
| 136 | CHECK(Coordinator::MakeParentHierarchy(path)); |
| 137 | FILE *file = fopen(path.c_str(), "w"); |
| 138 | |
| 139 | if (file == NULL) { |
| 140 | return -errno; |
| 141 | } |
| 142 | |
| 143 | const std::string libraryName = makeLibraryName(packageFQName); |
| 144 | |
| 145 | Formatter out(file); |
| 146 | |
| 147 | out << "LOCAL_PATH := $(call my-dir)\n" |
| 148 | << "include $(CLEAR_VARS)\n\n" |
| 149 | << "LOCAL_MODULE := " |
| 150 | << libraryName |
| 151 | << "\n" |
| 152 | << "LOCAL_MODULE_CLASS := SHARED_LIBRARIES\n\n" |
| 153 | << "intermediates := $(local-generated-sources-dir)\n\n" |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 154 | << "HIDL := $(HOST_OUT_EXECUTABLES)/" |
| 155 | << hidl_gen << "$(HOST_EXECUTABLE_SUFFIX)"; |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 156 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 157 | for (const auto &fqName : packageInterfaces) { |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 158 | |
| 159 | out << "\n" |
| 160 | << "\n#" |
| 161 | << "\n# Build " << fqName.name() << ".hal" |
| 162 | << "\n#"; |
| 163 | out << "\nGEN := $(intermediates)/" |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 164 | << coordinator->convertPackageRootToPath(packageFQName) |
| 165 | << coordinator->getPackagePath(packageFQName, true /* relative */); |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 166 | if (fqName.name() == "types") { |
| 167 | out << "types.cpp"; |
| 168 | } else { |
| 169 | out << fqName.name().substr(1) << "All.cpp"; |
| 170 | } |
| 171 | |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 172 | out << "\n$(GEN): $(HIDL)"; |
| 173 | out << "\n$(GEN): PRIVATE_HIDL := $(HIDL)"; |
| 174 | out << "\n$(GEN): PRIVATE_DEPS := $(LOCAL_PATH)/" |
| 175 | << fqName.name() << ".hal"; |
| 176 | out << "\n$(GEN): PRIVATE_OUTPUT_DIR := $(intermediates)" |
| 177 | << "\n$(GEN): PRIVATE_CUSTOM_TOOL = \\"; |
| 178 | out.indent(); |
| 179 | out.indent(); |
| 180 | out << "\n$(PRIVATE_HIDL) -o $(PRIVATE_OUTPUT_DIR) \\" |
| 181 | << "\n-Lc++ -r" |
| 182 | << coordinator->getPackageRoot(packageFQName) << ":" |
| 183 | << coordinator->getPackageRootPath(packageFQName) << "\\"; |
| 184 | out << "\n" |
| 185 | << packageFQName.string() |
| 186 | << "::$(patsubst %.hal,%,$(notdir $(PRIVATE_DEPS)))" |
| 187 | << "\n"; |
| 188 | out.unindent(); |
| 189 | out.unindent(); |
| 190 | |
| 191 | out << "\n$(GEN): $(LOCAL_PATH)/" << fqName.name() << ".hal"; |
| 192 | out << "\n\t$(transform-generated-source)"; |
| 193 | out << "\nLOCAL_GENERATED_SOURCES += $(GEN)"; |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 196 | out << "\n" |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 197 | << "\nLOCAL_EXPORT_C_INCLUDE_DIRS := $(intermediates)" |
| 198 | << "\nLOCAL_SHARED_LIBRARIES := \\"; |
| 199 | out.indent(); |
Martijn Coenen | 7473fab | 2016-08-19 14:05:40 +0200 | [diff] [blame] | 200 | out << "\nlibhidl \\" |
| 201 | << "\nlibhwbinder \\" |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 202 | << "\nlibutils \\"; |
| 203 | |
| 204 | for (const auto &importedPackage : importedPackages) { |
| 205 | out << "\n" << makeLibraryName(importedPackage) << " \\"; |
| 206 | } |
| 207 | out << "\n"; |
| 208 | out.unindent(); |
| 209 | |
| 210 | out << "\ninclude $(BUILD_SHARED_LIBRARY)\n"; |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 211 | |
| 212 | return OK; |
| 213 | } |
| 214 | |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 215 | OutputHandler::ValRes validateForMakefile(const FQName &fqName) { |
| 216 | if (fqName.package().empty()) { |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 217 | fprintf(stderr, "ERROR: Expecting package name\n"); |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 218 | return OutputHandler::FAILED; |
| 219 | } |
| 220 | |
| 221 | if (fqName.version().empty()) { |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 222 | fprintf(stderr, "ERROR: Expecting package version\n"); |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 223 | return OutputHandler::FAILED; |
| 224 | } |
| 225 | |
| 226 | if (!fqName.name().empty()) { |
| 227 | fprintf(stderr, |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 228 | "ERROR: Expecting only package name and version.\n"); |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 229 | return OutputHandler::FAILED; |
| 230 | } |
| 231 | |
| 232 | return OutputHandler::PASS_PACKAGE; |
| 233 | } |
| 234 | |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 235 | OutputHandler::ValRes validateForSource(const FQName &fqName) { |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 236 | if (fqName.package().empty()) { |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 237 | fprintf(stderr, "ERROR: Expecting package name\n"); |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 238 | return OutputHandler::FAILED; |
| 239 | } |
| 240 | |
| 241 | if (fqName.version().empty()) { |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 242 | fprintf(stderr, "ERROR: Expecting package version\n"); |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 243 | return OutputHandler::FAILED; |
| 244 | } |
| 245 | |
| 246 | return fqName.name().empty() ? |
| 247 | OutputHandler::PASS_PACKAGE : |
| 248 | OutputHandler::PASS_FULL; |
| 249 | } |
| 250 | |
| 251 | static std::vector<OutputHandler> formats = { |
| 252 | {"c++", |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 253 | true /* mNeedsOutputDir */, |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 254 | validateForSource, |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 255 | [](const FQName &fqName, |
| 256 | const char *hidl_gen, Coordinator *coordinator, |
| 257 | const std::string &outputDir) -> status_t { |
| 258 | if (fqName.isFullyQualified()) { |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 259 | return generateSourcesForFile(fqName, |
| 260 | hidl_gen, |
| 261 | coordinator, |
| 262 | outputDir, "c++"); |
| 263 | } else { |
| 264 | return generateSourcesForPackage(fqName, |
| 265 | hidl_gen, |
| 266 | coordinator, |
| 267 | outputDir, "c++"); |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 268 | } |
| 269 | } |
| 270 | }, |
| 271 | |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 272 | {"java", |
| 273 | true /* mNeedsOutputDir */, |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 274 | validateForSource, |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 275 | [](const FQName &fqName, |
| 276 | const char *hidl_gen, Coordinator *coordinator, |
| 277 | const std::string &outputDir) -> status_t { |
| 278 | if (fqName.isFullyQualified()) { |
| 279 | return generateSourcesForFile(fqName, |
| 280 | hidl_gen, |
| 281 | coordinator, |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 282 | outputDir, "java"); |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 283 | } |
| 284 | else { |
| 285 | return generateSourcesForPackage(fqName, |
| 286 | hidl_gen, |
| 287 | coordinator, |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 288 | outputDir, "java"); |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | }, |
| 292 | |
Zhuoyao Zhang | 5158db4 | 2016-08-10 10:25:20 -0700 | [diff] [blame] | 293 | {"vts", |
| 294 | true, |
| 295 | validateForSource, |
| 296 | [](const FQName &fqName, |
| 297 | const char * hidl_gen, |
| 298 | Coordinator *coordinator, |
| 299 | const std::string &outputDir) -> status_t { |
| 300 | if (fqName.isFullyQualified()) { |
| 301 | return generateSourcesForFile(fqName, |
| 302 | hidl_gen, |
| 303 | coordinator, |
| 304 | outputDir, "vts"); |
| 305 | } else { |
| 306 | return generateSourcesForPackage(fqName, |
| 307 | hidl_gen, |
| 308 | coordinator, |
| 309 | outputDir, "vts"); |
| 310 | } |
| 311 | } |
| 312 | }, |
| 313 | |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 314 | {"makefile", |
Andreas Huber | 2831d51 | 2016-08-15 09:33:47 -0700 | [diff] [blame] | 315 | false /* mNeedsOutputDir */, |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 316 | validateForMakefile, |
| 317 | generateMakefileForPackage, |
| 318 | }, |
| 319 | }; |
| 320 | |
| 321 | static void usage(const char *me) { |
| 322 | fprintf(stderr, |
| 323 | "usage: %s -o output-path -L <language> (-r interface-root)+ fqname+\n", |
| 324 | me); |
| 325 | |
| 326 | fprintf(stderr, " -o output path\n"); |
| 327 | |
| 328 | fprintf(stderr, " -L <language> (one of"); |
| 329 | for (auto &e : formats) { |
| 330 | fprintf(stderr, " %s", e.mKey.c_str()); |
| 331 | } |
| 332 | fprintf(stderr, ")\n"); |
| 333 | |
| 334 | fprintf(stderr, |
| 335 | " -r package:path root " |
| 336 | "(e.g., android.hardware:hardware/interfaces)\n"); |
| 337 | } |
| 338 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 339 | int main(int argc, char **argv) { |
| 340 | std::string outputDir; |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 341 | std::vector<std::string> packageRootPaths; |
| 342 | std::vector<std::string> packageRoots; |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 343 | |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 344 | const char *me = argv[0]; |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 345 | OutputHandler *outputFormat = nullptr; |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 346 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 347 | int res; |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 348 | while ((res = getopt(argc, argv, "ho:r:L:")) >= 0) { |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 349 | switch (res) { |
| 350 | case 'o': |
| 351 | { |
| 352 | outputDir = optarg; |
| 353 | break; |
| 354 | } |
| 355 | |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 356 | case 'r': |
| 357 | { |
| 358 | std::string val(optarg); |
| 359 | auto index = val.find_first_of(':'); |
| 360 | CHECK(index != std::string::npos); |
| 361 | |
| 362 | auto package = val.substr(0, index); |
| 363 | auto path = val.substr(index + 1); |
| 364 | packageRootPaths.push_back(path); |
| 365 | packageRoots.push_back(package); |
| 366 | break; |
| 367 | } |
| 368 | |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 369 | case 'L': |
| 370 | { |
| 371 | CHECK(outputFormat == nullptr); // only one -L option |
| 372 | for (auto &e : formats) { |
| 373 | if (e.mKey == optarg) { |
| 374 | outputFormat = &e; |
| 375 | break; |
| 376 | } |
| 377 | } |
| 378 | CHECK(outputFormat != nullptr); |
| 379 | break; |
| 380 | } |
| 381 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 382 | case '?': |
| 383 | case 'h': |
| 384 | default: |
| 385 | { |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 386 | usage(me); |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 387 | exit(1); |
| 388 | break; |
| 389 | } |
| 390 | } |
| 391 | } |
| 392 | |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 393 | if (outputFormat == nullptr) { |
| 394 | usage(me); |
| 395 | exit(1); |
| 396 | } |
| 397 | |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 398 | argc -= optind; |
| 399 | argv += optind; |
| 400 | |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 401 | if (packageRootPaths.empty()) { |
| 402 | // Pick reasonable defaults. |
| 403 | |
| 404 | packageRoots.push_back("android.hardware"); |
| 405 | |
| 406 | const char *TOP = getenv("TOP"); |
| 407 | CHECK(TOP != NULL); |
| 408 | |
| 409 | std::string path = TOP; |
| 410 | path.append("/hardware/interfaces"); |
| 411 | |
| 412 | packageRootPaths.push_back(path); |
| 413 | } |
| 414 | |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 415 | // Valid options are now in argv[0] .. argv[argc - 1]. |
| 416 | |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 417 | if (!outputFormat->mNeedsOutputDir) { |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 418 | outputDir.clear(); // Unused. |
| 419 | } else if (outputDir.empty()) { |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 420 | usage(me); |
Andreas Huber | b82318c | 2016-08-02 14:45:54 -0700 | [diff] [blame] | 421 | exit(1); |
| 422 | } else { |
| 423 | const size_t len = outputDir.size(); |
| 424 | if (outputDir[len - 1] != '/') { |
| 425 | outputDir += "/"; |
| 426 | } |
| 427 | } |
| 428 | |
Andreas Huber | dca261f | 2016-08-04 13:47:51 -0700 | [diff] [blame] | 429 | Coordinator coordinator(packageRootPaths, packageRoots); |
Andreas Huber | 5345ec2 | 2016-07-29 13:33:27 -0700 | [diff] [blame] | 430 | |
Andreas Huber | 737080b | 2016-08-02 15:38:04 -0700 | [diff] [blame] | 431 | for (int i = 0; i < argc; ++i) { |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 432 | FQName fqName(argv[i]); |
Andreas Huber | 68f2459 | 2016-07-29 14:53:48 -0700 | [diff] [blame] | 433 | |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 434 | if (!fqName.isValid()) { |
Andreas Huber | e61e3f7 | 2016-08-03 10:22:03 -0700 | [diff] [blame] | 435 | fprintf(stderr, |
Andreas Huber | 70a59e1 | 2016-08-16 12:57:01 -0700 | [diff] [blame] | 436 | "ERROR: Invalid fully-qualified name.\n"); |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 437 | exit(1); |
| 438 | } |
Andreas Huber | 881227d | 2016-08-02 14:20:21 -0700 | [diff] [blame] | 439 | |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 440 | OutputHandler::ValRes valid = outputFormat->validate(fqName); |
| 441 | if (valid == OutputHandler::FAILED) { |
Andreas Huber | e61e3f7 | 2016-08-03 10:22:03 -0700 | [diff] [blame] | 442 | exit(1); |
| 443 | } |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 444 | |
| 445 | status_t err = |
Iliyan Malchev | 5bb1402 | 2016-08-09 15:04:39 -0700 | [diff] [blame] | 446 | outputFormat->generate(fqName, me, &coordinator, outputDir); |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 447 | |
| 448 | if (err != OK) { |
| 449 | break; |
| 450 | } |
Andreas Huber | eb1081f | 2016-07-28 13:13:24 -0700 | [diff] [blame] | 451 | } |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 452 | |
Andreas Huber | d2943e1 | 2016-08-05 11:59:31 -0700 | [diff] [blame] | 453 | return 0; |
Andreas Huber | c9410c7 | 2016-07-28 12:18:40 -0700 | [diff] [blame] | 454 | } |