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