1 /*
2 * Copyright (C) 2015, 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
17 #include "options.h"
18 #include "logging.h"
19 #include "os.h"
20
21 #include <getopt.h>
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <algorithm>
25 #include <iostream>
26 #include <sstream>
27 #include <string>
28
29 #include <android-base/strings.h>
30 #include "aidl_language.h"
31
32 using android::base::Split;
33 using android::base::Trim;
34 using std::endl;
35 using std::string;
36
37 #ifndef PLATFORM_SDK_VERSION
38 #define PLATFORM_SDK_VERSION "<UNKNOWN>"
39 #endif
40
41 namespace android {
42 namespace aidl {
43
GetUsage() const44 string Options::GetUsage() const {
45 std::ostringstream sstr;
46 sstr << "AIDL Compiler: built for platform SDK version " << PLATFORM_SDK_VERSION << endl;
47 sstr << "usage:" << endl
48 << myname_ << " --lang={java|cpp|ndk} [OPTION]... INPUT..." << endl
49 << " Generate Java or C++ files for AIDL file(s)." << endl
50 << endl
51 << myname_ << " --preprocess OUTPUT INPUT..." << endl
52 << " Create an AIDL file having declarations of AIDL file(s)." << endl
53 << endl
54 #ifndef _WIN32
55 << myname_ << " --dumpapi --out=DIR INPUT..." << endl
56 << " Dump API signature of AIDL file(s) to DIR." << endl
57 << endl
58 << myname_ << " --checkapi OLD_DIR NEW_DIR" << endl
59 << " Checkes whether API dump NEW_DIR is backwards compatible extension " << endl
60 << " of the API dump OLD_DIR." << endl
61 #endif
62 << endl;
63
64 // Legacy option formats
65 if (language_ == Options::Language::JAVA) {
66 sstr << myname_ << " [OPTION]... INPUT [OUTPUT]" << endl
67 << " Generate a Java file for an AIDL file." << endl
68 << endl;
69 } else if (language_ == Options::Language::CPP) {
70 sstr << myname_ << " [OPTION]... INPUT HEADER_DIR OUTPUT" << endl
71 << " Generate C++ headers and source for an AIDL file." << endl
72 << endl;
73 }
74
75 sstr << "OPTION:" << endl
76 << " -I DIR, --include=DIR" << endl
77 << " Use DIR as a search path for import statements." << endl
78 << " -m FILE, --import=FILE" << endl
79 << " Import FILE directly without searching in the search paths." << endl
80 << " -p FILE, --preprocessed=FILE" << endl
81 << " Include FILE which is created by --preprocess." << endl
82 << " -d FILE, --dep=FILE" << endl
83 << " Generate dependency file as FILE. Don't use this when" << endl
84 << " there are multiple input files. Use -a then." << endl
85 << " -o DIR, --out=DIR" << endl
86 << " Use DIR as the base output directory for generated files." << endl
87 << " -h DIR, --header_out=DIR" << endl
88 << " Generate C++ headers under DIR." << endl
89 << " -a" << endl
90 << " Generate dependency file next to the output file with the" << endl
91 << " name based on the input file." << endl
92 << " -b" << endl
93 << " Trigger fail when trying to compile a parcelable." << endl
94 << " --ninja" << endl
95 << " Generate dependency file in a format ninja understands." << endl
96 << " --structured" << endl
97 << " Whether this interface is defined exclusively in AIDL." << endl
98 << " It is therefore a candidate for stabilization." << endl
99 << " --stability=<level>" << endl
100 << " The stability requirement of this interface." << endl
101 << " -t, --trace" << endl
102 << " Include tracing code for systrace. Note that if either" << endl
103 << " the client or service code is not auto-generated by this" << endl
104 << " tool, that part will not be traced." << endl
105 << " --transaction_names" << endl
106 << " Generate transaction names." << endl
107 << " --apimapping" << endl
108 << " Generates a mapping of declared aidl method signatures to" << endl
109 << " the original line number. e.g.: " << endl
110 << " If line 39 of foo/bar/IFoo.aidl contains:"
111 << " void doFoo(int bar, String baz);" << endl
112 << " Then the result would be:" << endl
113 << " foo.bar.Baz|doFoo|int,String,|void" << endl
114 << " foo/bar/IFoo.aidl:39" << endl
115 << " -v VER, --version=VER" << endl
116 << " Set the version of the interface and parcelable to VER." << endl
117 << " VER must be an interger greater than 0." << endl
118 << " --hash=HASH" << endl
119 << " Set the interface hash to HASH." << endl
120 << " --log" << endl
121 << " Information about the transaction, e.g., method name, argument" << endl
122 << " values, execution time, etc., is provided via callback." << endl
123 << " --parcelable-to-string" << endl
124 << " Generates an implementation of toString() for Java parcelables," << endl
125 << " and ostream& operator << for C++ parcelables." << endl
126 << " --help" << endl
127 << " Show this help." << endl
128 << endl
129 << "INPUT:" << endl
130 << " An AIDL file." << endl
131 << endl
132 << "OUTPUT:" << endl
133 << " Path to the generated Java or C++ source file. This is ignored when" << endl
134 << " -o or --out is specified or the number of the input files are" << endl
135 << " more than one." << endl
136 << " For Java, if omitted, Java source file is generated at the same" << endl
137 << " place as the input AIDL file," << endl
138 << endl
139 << "HEADER_DIR:" << endl
140 << " Path to where C++ headers are generated." << endl;
141 return sstr.str();
142 }
143
LanguageToString(Language language)144 const string Options::LanguageToString(Language language) {
145 switch (language) {
146 case Options::Language::CPP:
147 return "cpp";
148 case Options::Language::JAVA:
149 return "java";
150 case Options::Language::NDK:
151 return "ndk";
152 case Options::Language::UNSPECIFIED:
153 return "unspecified";
154 default:
155 AIDL_FATAL(AIDL_LOCATION_HERE)
156 << "Unexpected Options::Language enumerator: " << static_cast<size_t>(language);
157 }
158 }
159
StabilityFromString(const std::string & stability,Stability * out_stability)160 bool Options::StabilityFromString(const std::string& stability, Stability* out_stability) {
161 if (stability == "vintf") {
162 *out_stability = Stability::VINTF;
163 return true;
164 }
165 return false;
166 }
167
From(const string & cmdline)168 Options Options::From(const string& cmdline) {
169 vector<string> args = Split(cmdline, " ");
170 return From(args);
171 }
172
From(const vector<string> & args)173 Options Options::From(const vector<string>& args) {
174 Options::Language lang = Options::Language::JAVA;
175 int argc = args.size();
176 if (argc >= 1 && args.at(0) == "aidl-cpp") {
177 lang = Options::Language::CPP;
178 }
179 const char* argv[argc + 1];
180 for (int i = 0; i < argc; i++) {
181 argv[i] = args.at(i).c_str();
182 }
183 argv[argc] = nullptr;
184
185 return Options(argc, argv, lang);
186 }
187
Options(int argc,const char * const argv[],Options::Language default_lang)188 Options::Options(int argc, const char* const argv[], Options::Language default_lang)
189 : myname_(argv[0]), language_(default_lang) {
190 bool lang_option_found = false;
191 optind = 0;
192 while (true) {
193 static struct option long_options[] = {
194 {"lang", required_argument, 0, 'l'},
195 {"preprocess", no_argument, 0, 's'},
196 #ifndef _WIN32
197 {"dumpapi", no_argument, 0, 'u'},
198 {"checkapi", no_argument, 0, 'A'},
199 #endif
200 {"apimapping", required_argument, 0, 'i'},
201 {"include", required_argument, 0, 'I'},
202 {"import", required_argument, 0, 'm'},
203 {"preprocessed", required_argument, 0, 'p'},
204 {"dep", required_argument, 0, 'd'},
205 {"out", required_argument, 0, 'o'},
206 {"header_out", required_argument, 0, 'h'},
207 {"ninja", no_argument, 0, 'n'},
208 {"stability", required_argument, 0, 'Y'},
209 {"structured", no_argument, 0, 'S'},
210 {"trace", no_argument, 0, 't'},
211 {"transaction_names", no_argument, 0, 'c'},
212 {"version", required_argument, 0, 'v'},
213 {"log", no_argument, 0, 'L'},
214 {"parcelable-to-string", no_argument, 0, 'P'},
215 {"hash", required_argument, 0, 'H'},
216 {"help", no_argument, 0, 'e'},
217 {0, 0, 0, 0},
218 };
219 const int c = getopt_long(argc, const_cast<char* const*>(argv),
220 "I:m:p:d:o:h:abtv:", long_options, nullptr);
221 if (c == -1) {
222 // no more options
223 break;
224 }
225 switch (c) {
226 case 'l':
227 if (language_ == Options::Language::CPP) {
228 // aidl-cpp can't set language. aidl-cpp exists only for backwards
229 // compatibility.
230 error_message_ << "aidl-cpp does not support --lang." << endl;
231 return;
232 } else {
233 lang_option_found = true;
234 string lang = Trim(optarg);
235 if (lang == "java") {
236 language_ = Options::Language::JAVA;
237 task_ = Options::Task::COMPILE;
238 } else if (lang == "cpp") {
239 language_ = Options::Language::CPP;
240 task_ = Options::Task::COMPILE;
241 } else if (lang == "ndk") {
242 language_ = Options::Language::NDK;
243 task_ = Options::Task::COMPILE;
244 } else {
245 error_message_ << "Unsupported language: '" << lang << "'" << endl;
246 return;
247 }
248 }
249 break;
250 case 's':
251 if (task_ != Options::Task::UNSPECIFIED) {
252 task_ = Options::Task::PREPROCESS;
253 }
254 break;
255 #ifndef _WIN32
256 case 'u':
257 if (task_ != Options::Task::UNSPECIFIED) {
258 task_ = Options::Task::DUMP_API;
259 }
260 break;
261 case 'A':
262 if (task_ != Options::Task::UNSPECIFIED) {
263 task_ = Options::Task::CHECK_API;
264 // to ensure that all parcelables in the api dumpes are structured
265 structured_ = true;
266 }
267 break;
268 #endif
269 case 'I': {
270 import_dirs_.emplace(Trim(optarg));
271 break;
272 }
273 case 'm': {
274 import_files_.emplace(Trim(optarg));
275 break;
276 }
277 case 'p':
278 preprocessed_files_.emplace_back(Trim(optarg));
279 break;
280 case 'd':
281 dependency_file_ = Trim(optarg);
282 break;
283 case 'o':
284 output_dir_ = Trim(optarg);
285 if (output_dir_.back() != OS_PATH_SEPARATOR) {
286 output_dir_.push_back(OS_PATH_SEPARATOR);
287 }
288 break;
289 case 'h':
290 output_header_dir_ = Trim(optarg);
291 if (output_header_dir_.back() != OS_PATH_SEPARATOR) {
292 output_header_dir_.push_back(OS_PATH_SEPARATOR);
293 }
294 break;
295 case 'n':
296 dependency_file_ninja_ = true;
297 break;
298 case 'S':
299 structured_ = true;
300 break;
301 case 'Y': {
302 const string stability_str = Trim(optarg);
303 if (!StabilityFromString(stability_str, &stability_)) {
304 error_message_ << "Unrecognized stability level: '" << stability_str
305 << "'. Must be vintf." << endl;
306 return;
307 }
308 break;
309 }
310 case 't':
311 gen_traces_ = true;
312 break;
313 case 'a':
314 auto_dep_file_ = true;
315 break;
316 case 'b':
317 fail_on_parcelable_ = true;
318 break;
319 case 'c':
320 gen_transaction_names_ = true;
321 break;
322 case 'v': {
323 const string ver_str = Trim(optarg);
324 int ver = atoi(ver_str.c_str());
325 if (ver > 0) {
326 version_ = ver;
327 } else {
328 error_message_ << "Invalid version number: '" << ver_str << "'. "
329 << "Version must be a positive natural number." << endl;
330 return;
331 }
332 break;
333 }
334 case 'H':
335 hash_ = Trim(optarg);
336 break;
337 case 'L':
338 gen_log_ = true;
339 break;
340 case 'e':
341 std::cerr << GetUsage();
342 exit(0);
343 case 'i':
344 output_file_ = Trim(optarg);
345 task_ = Task::DUMP_MAPPINGS;
346 break;
347 case 'P':
348 gen_parcelable_to_string_ = true;
349 break;
350 default:
351 std::cerr << GetUsage();
352 exit(1);
353 }
354 } // while
355
356 // Positional arguments
357 if (!lang_option_found && task_ == Options::Task::COMPILE) {
358 // the legacy arguments format
359 if (argc - optind <= 0) {
360 error_message_ << "No input file" << endl;
361 return;
362 }
363 if (language_ == Options::Language::JAVA) {
364 input_files_.emplace_back(argv[optind++]);
365 if (argc - optind >= 1) {
366 output_file_ = argv[optind++];
367 } else if (output_dir_.empty()) {
368 // when output is omitted and -o option isn't set, the output is by
369 // default set to the input file path with .aidl is replaced to .java.
370 // If -o option is set, the output path is calculated by
371 // generate_outputFileName which returns "<output_dir>/<package/name>/
372 // <typename>.java"
373 output_file_ = input_files_.front();
374 if (android::base::EndsWith(output_file_, ".aidl")) {
375 output_file_ = output_file_.substr(0, output_file_.length() - strlen(".aidl"));
376 }
377 output_file_ += ".java";
378 }
379 } else if (IsCppOutput()) {
380 input_files_.emplace_back(argv[optind++]);
381 if (argc - optind < 2) {
382 error_message_ << "No HEADER_DIR or OUTPUT." << endl;
383 return;
384 }
385 output_header_dir_ = argv[optind++];
386 if (output_header_dir_.back() != OS_PATH_SEPARATOR) {
387 output_header_dir_.push_back(OS_PATH_SEPARATOR);
388 }
389 output_file_ = argv[optind++];
390 }
391 if (argc - optind > 0) {
392 error_message_ << "Too many arguments: ";
393 for (int i = optind; i < argc; i++) {
394 error_message_ << " " << argv[i];
395 }
396 error_message_ << endl;
397 }
398 } else {
399 // the new arguments format
400 if (task_ == Options::Task::COMPILE || task_ == Options::Task::DUMP_API) {
401 if (argc - optind < 1) {
402 error_message_ << "No input file." << endl;
403 return;
404 }
405 } else {
406 if (argc - optind < 2) {
407 error_message_ << "Insufficient arguments. At least 2 required, but "
408 << "got " << (argc - optind) << "." << endl;
409 return;
410 }
411 if (task_ != Options::Task::CHECK_API && task_ != Options::Task::DUMP_MAPPINGS) {
412 output_file_ = argv[optind++];
413 }
414 }
415 while (optind < argc) {
416 input_files_.emplace_back(argv[optind++]);
417 }
418 }
419
420 // filter out invalid combinations
421 if (lang_option_found) {
422 if (IsCppOutput() && task_ == Options::Task::COMPILE) {
423 if (output_dir_.empty()) {
424 error_message_ << "Output directory is not set. Set with --out." << endl;
425 return;
426 }
427 if (output_header_dir_.empty()) {
428 error_message_ << "Header output directory is not set. Set with "
429 << "--header_out." << endl;
430 return;
431 }
432 }
433 if (language_ == Options::Language::JAVA && task_ == Options::Task::COMPILE) {
434 if (output_dir_.empty()) {
435 error_message_ << "Output directory is not set. Set with --out." << endl;
436 return;
437 }
438 if (!output_header_dir_.empty()) {
439 error_message_ << "Header output directory is set, which does not make "
440 << "sense for Java." << endl;
441 return;
442 }
443 }
444 }
445 if (task_ == Options::Task::COMPILE) {
446 for (const string& input : input_files_) {
447 if (!android::base::EndsWith(input, ".aidl")) {
448 error_message_ << "Expected .aidl file for input but got '" << input << "'" << endl;
449 return;
450 }
451 }
452 if (!output_file_.empty() && input_files_.size() > 1) {
453 error_message_ << "Multiple AIDL files can't be compiled to a single "
454 << "output file '" << output_file_ << "'. "
455 << "Use --out=DIR instead for output files." << endl;
456 return;
457 }
458 if (!dependency_file_.empty() && input_files_.size() > 1) {
459 error_message_ << "-d or --dep doesn't work when compiling multiple AIDL "
460 << "files. Use '-a' to generate dependency file next to "
461 << "the output file with the name based on the input "
462 << "file." << endl;
463 return;
464 }
465 if (gen_log_ && (language_ != Options::Language::CPP && language_ != Options::Language::NDK)) {
466 error_message_ << "--log is currently supported for either --lang=cpp or --lang=ndk" << endl;
467 return;
468 }
469 }
470 if (task_ == Options::Task::PREPROCESS) {
471 if (version_ > 0) {
472 error_message_ << "--version should not be used with '--preprocess'." << endl;
473 return;
474 }
475 }
476 if (task_ == Options::Task::CHECK_API) {
477 if (input_files_.size() != 2) {
478 error_message_ << "--checkapi requires two inputs for comparing, "
479 << "but got " << input_files_.size() << "." << endl;
480 return;
481 }
482 }
483 if (task_ == Options::Task::DUMP_API) {
484 if (output_dir_.empty()) {
485 error_message_ << "--dump_api requires output directory. Use --out." << endl;
486 return;
487 }
488 }
489
490 CHECK(output_dir_.empty() || output_dir_.back() == OS_PATH_SEPARATOR);
491 CHECK(output_header_dir_.empty() || output_header_dir_.back() == OS_PATH_SEPARATOR);
492 }
493
494 } // namespace aidl
495 } // namespace android
496