blob: 8b5f16ea3bad5bcfc1fd0cc685ef794ef525de9f [file] [log] [blame]
Christopher Wiley4427d862015-09-14 11:07:39 -07001/*
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 */
Adam Lesinskiffa16862014-01-23 18:17:42 -080016
17#include "options.h"
Jiyong Park05463732018-08-09 16:03:02 +090018#include "logging.h"
Jiyong Park6f77e0c2018-07-28 16:55:44 +090019#include "os.h"
Adam Lesinskiffa16862014-01-23 18:17:42 -080020
Jiyong Park74595c12018-07-23 15:22:50 +090021#include <getopt.h>
22#include <stdlib.h>
23#include <unistd.h>
Jiyong Park8c380532018-08-30 14:55:26 +090024#include <algorithm>
Christopher Wileya590de82015-09-15 15:46:28 -070025#include <iostream>
Jiyong Park74595c12018-07-23 15:22:50 +090026#include <sstream>
27#include <string>
Adam Lesinskiffa16862014-01-23 18:17:42 -080028
Jiyong Park74595c12018-07-23 15:22:50 +090029#include <android-base/strings.h>
Christopher Wiley4432ccf2015-09-18 18:32:08 -070030
Jiyong Park6f77e0c2018-07-28 16:55:44 +090031using android::base::Split;
Jiyong Park74595c12018-07-23 15:22:50 +090032using android::base::Trim;
Christopher Wileya590de82015-09-15 15:46:28 -070033using std::endl;
Christopher Wiley4427d862015-09-14 11:07:39 -070034using std::string;
Christopher Wiley4427d862015-09-14 11:07:39 -070035
36namespace android {
37namespace aidl {
Christopher Wiley4427d862015-09-14 11:07:39 -070038
Jiyong Park74595c12018-07-23 15:22:50 +090039string 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
Steven Morelandda0654f2018-07-17 12:24:38 -070044 << endl
Jiyong Park74595c12018-07-23 15:22:50 +090045 << myname_ << " --preprocess OUTPUT INPUT..." << endl
46 << " Create an AIDL file having declarations of AIDL file(s)." << endl
Steven Morelandda0654f2018-07-17 12:24:38 -070047 << endl
Jiyong Parke59c3682018-09-11 23:10:25 +090048#ifndef _WIN32
49 << myname_ << " --dumpapi --out=DIR INPUT..." << endl
50 << " Dump API signature of AIDL file(s) to DIR." << endl
Jiyong Park3656c3c2018-08-01 20:02:01 +090051 << endl
Jiyong Parke59c3682018-09-11 23:10:25 +090052 << 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
Steven Morelandda0654f2018-07-17 12:24:38 -070056 << endl;
Adam Lesinskiffa16862014-01-23 18:17:42 -080057
Jiyong Park74595c12018-07-23 15:22:50 +090058 // 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;
Christopher Wiley4427d862015-09-14 11:07:39 -070067 }
68
Jiyong Park74595c12018-07-23 15:22:50 +090069 sstr << "OPTION:" << endl
70 << " -I DIR, --include=DIR" << endl
71 << " Use DIR as a search path for import statements." << endl
Jiyong Park3c35e392018-08-30 13:10:30 +090072 << " -m FILE, --import=FILE" << endl
73 << " Import FILE directly without searching in the search paths." << endl
Jiyong Park74595c12018-07-23 15:22:50 +090074 << " -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
Steven Moreland6cee3482018-07-18 14:39:58 -070090 << " --structured" << endl
91 << " Whether this interface is defined exclusively in AIDL." << endl
92 << " It is therefore a candidate for stabilization." << endl
Jiyong Park74595c12018-07-23 15:22:50 +090093 << " -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
Jiyong Park309668e2018-07-28 16:55:44 +090099 << " -v VER, --version=VER" << endl
100 << " Set the version of the interface and parcelable to VER." << endl
101 << " VER must be an interger greater than 0." << endl
Jiyong Parkce50e262018-10-29 09:54:20 +0900102 << " --log" << endl
103 << " Information about the transaction, e.g., method name, argument" << endl
104 << " values, execution time, etc., is provided via callback." << endl
Jiyong Park74595c12018-07-23 15:22:50 +0900105 << " --help" << endl
106 << " Show this help." << endl
107 << endl
108 << "INPUT:" << endl
109 << " An AIDL file." << endl
110 << endl
111 << "OUTPUT:" << endl
112 << " Path to the generated Java or C++ source file. This is ignored when" << endl
113 << " -o or --out is specified or the number of the input files are" << endl
114 << " more than one." << endl
115 << " For Java, if omitted, Java source file is generated at the same" << endl
116 << " place as the input AIDL file," << endl
117 << endl
Christopher Wiley054afbd2015-10-16 17:08:43 -0700118 << "HEADER_DIR:" << endl
Jiyong Park74595c12018-07-23 15:22:50 +0900119 << " Path to where C++ headers are generated." << endl;
120 return sstr.str();
Christopher Wileya590de82015-09-15 15:46:28 -0700121}
122
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900123Options Options::From(const string& cmdline) {
124 vector<string> args = Split(cmdline, " ");
125 return From(args);
126}
127
128Options Options::From(const vector<string>& args) {
129 Options::Language lang = Options::Language::JAVA;
130 int argc = args.size();
131 if (argc >= 1 && args.at(0) == "aidl-cpp") {
132 lang = Options::Language::CPP;
133 }
134 const char* argv[argc + 1];
135 for (int i = 0; i < argc; i++) {
136 argv[i] = args.at(i).c_str();
137 }
138 argv[argc] = nullptr;
139
140 return Options(argc, argv, lang);
141}
142
Jiyong Park74595c12018-07-23 15:22:50 +0900143Options::Options(int argc, const char* const argv[], Options::Language default_lang)
144 : myname_(argv[0]), language_(default_lang) {
145 bool lang_option_found = false;
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900146 optind = 0;
Jiyong Park74595c12018-07-23 15:22:50 +0900147 while (true) {
148 static struct option long_options[] = {
149 {"lang", required_argument, 0, 'l'},
150 {"preprocess", no_argument, 0, 's'},
Jiyong Parke59c3682018-09-11 23:10:25 +0900151#ifndef _WIN32
Jiyong Park74595c12018-07-23 15:22:50 +0900152 {"dumpapi", no_argument, 0, 'u'},
Jiyong Park3656c3c2018-08-01 20:02:01 +0900153 {"checkapi", no_argument, 0, 'A'},
Jiyong Parke59c3682018-09-11 23:10:25 +0900154#endif
Jiyong Park74595c12018-07-23 15:22:50 +0900155 {"include", required_argument, 0, 'I'},
Jiyong Park3c35e392018-08-30 13:10:30 +0900156 {"import", required_argument, 0, 'm'},
Jiyong Park74595c12018-07-23 15:22:50 +0900157 {"preprocessed", required_argument, 0, 'p'},
158 {"dep", required_argument, 0, 'd'},
159 {"out", required_argument, 0, 'o'},
160 {"header_out", required_argument, 0, 'h'},
161 {"ninja", no_argument, 0, 'n'},
Steven Moreland6cee3482018-07-18 14:39:58 -0700162 {"structured", no_argument, 0, 'S'},
Jiyong Park74595c12018-07-23 15:22:50 +0900163 {"trace", no_argument, 0, 't'},
164 {"transaction_names", no_argument, 0, 'c'},
Jiyong Park309668e2018-07-28 16:55:44 +0900165 {"version", required_argument, 0, 'v'},
Jiyong Parkce50e262018-10-29 09:54:20 +0900166 {"log", no_argument, 0, 'L'},
Jiyong Park74595c12018-07-23 15:22:50 +0900167 {"help", no_argument, 0, 'e'},
168 {0, 0, 0, 0},
169 };
Jiyong Park3c35e392018-08-30 13:10:30 +0900170 const int c = getopt_long(argc, const_cast<char* const*>(argv),
171 "I:m:p:d:o:h:abtv:", long_options, nullptr);
Jiyong Park74595c12018-07-23 15:22:50 +0900172 if (c == -1) {
173 // no more options
174 break;
Christopher Wileya590de82015-09-15 15:46:28 -0700175 }
Jiyong Park74595c12018-07-23 15:22:50 +0900176 switch (c) {
177 case 'l':
178 if (language_ == Options::Language::CPP) {
179 // aidl-cpp can't set language. aidl-cpp exists only for backwards
180 // compatibility.
181 error_message_ << "aidl-cpp does not support --lang." << endl;
182 return;
183 } else {
184 lang_option_found = true;
Jiyong Park309668e2018-07-28 16:55:44 +0900185 string lang = Trim(optarg);
Jiyong Park74595c12018-07-23 15:22:50 +0900186 if (lang == "java") {
187 language_ = Options::Language::JAVA;
188 task_ = Options::Task::COMPILE;
189 } else if (lang == "cpp") {
190 language_ = Options::Language::CPP;
191 task_ = Options::Task::COMPILE;
Steven Morelandc26d8142018-09-17 14:25:33 -0700192 } else if (lang == "ndk") {
193 language_ = Options::Language::NDK;
194 task_ = Options::Task::COMPILE;
Jiyong Park74595c12018-07-23 15:22:50 +0900195 } else {
196 error_message_ << "Unsupported language: '" << lang << "'" << endl;
197 return;
198 }
199 }
200 break;
201 case 's':
202 if (task_ != Options::Task::UNSPECIFIED) {
203 task_ = Options::Task::PREPROCESS;
204 }
205 break;
Jiyong Parke59c3682018-09-11 23:10:25 +0900206#ifndef _WIN32
Jiyong Park74595c12018-07-23 15:22:50 +0900207 case 'u':
208 if (task_ != Options::Task::UNSPECIFIED) {
Jiyong Park3656c3c2018-08-01 20:02:01 +0900209 task_ = Options::Task::DUMP_API;
210 }
211 break;
212 case 'A':
213 if (task_ != Options::Task::UNSPECIFIED) {
214 task_ = Options::Task::CHECK_API;
215 // to ensure that all parcelables in the api dumpes are structured
216 structured_ = true;
Jiyong Park74595c12018-07-23 15:22:50 +0900217 }
218 break;
Jiyong Parke59c3682018-09-11 23:10:25 +0900219#endif
Jiyong Park8c380532018-08-30 14:55:26 +0900220 case 'I': {
221 import_dirs_.emplace(Trim(optarg));
Jiyong Park3c35e392018-08-30 13:10:30 +0900222 break;
Jiyong Park8c380532018-08-30 14:55:26 +0900223 }
224 case 'm': {
225 import_files_.emplace(Trim(optarg));
Jiyong Park74595c12018-07-23 15:22:50 +0900226 break;
Jiyong Park8c380532018-08-30 14:55:26 +0900227 }
Jiyong Park74595c12018-07-23 15:22:50 +0900228 case 'p':
Jiyong Park309668e2018-07-28 16:55:44 +0900229 preprocessed_files_.emplace_back(Trim(optarg));
Jiyong Park74595c12018-07-23 15:22:50 +0900230 break;
231 case 'd':
Jiyong Park309668e2018-07-28 16:55:44 +0900232 dependency_file_ = Trim(optarg);
Jiyong Park74595c12018-07-23 15:22:50 +0900233 break;
234 case 'o':
Jiyong Park309668e2018-07-28 16:55:44 +0900235 output_dir_ = Trim(optarg);
Jiyong Park05463732018-08-09 16:03:02 +0900236 if (output_dir_.back() != OS_PATH_SEPARATOR) {
237 output_dir_.push_back(OS_PATH_SEPARATOR);
238 }
Jiyong Park74595c12018-07-23 15:22:50 +0900239 break;
240 case 'h':
Jiyong Park309668e2018-07-28 16:55:44 +0900241 output_header_dir_ = Trim(optarg);
Jiyong Park05463732018-08-09 16:03:02 +0900242 if (output_header_dir_.back() != OS_PATH_SEPARATOR) {
243 output_header_dir_.push_back(OS_PATH_SEPARATOR);
244 }
Jiyong Park74595c12018-07-23 15:22:50 +0900245 break;
246 case 'n':
247 dependency_file_ninja_ = true;
248 break;
Steven Moreland6cee3482018-07-18 14:39:58 -0700249 case 'S':
250 structured_ = true;
251 break;
Jiyong Park74595c12018-07-23 15:22:50 +0900252 case 't':
253 gen_traces_ = true;
254 break;
255 case 'a':
256 auto_dep_file_ = true;
257 break;
258 case 'b':
259 fail_on_parcelable_ = true;
260 break;
261 case 'c':
262 gen_transaction_names_ = true;
263 break;
Jiyong Park309668e2018-07-28 16:55:44 +0900264 case 'v': {
265 const string ver_str = Trim(optarg);
266 int ver = atoi(ver_str.c_str());
267 if (ver > 0) {
268 version_ = ver;
269 } else {
270 error_message_ << "Invalid version number: '" << ver_str << "'. "
271 << "Version must be a positive natural number." << endl;
272 return;
273 }
274 break;
275 }
Jiyong Parkce50e262018-10-29 09:54:20 +0900276 case 'L':
277 gen_log_ = true;
278 break;
Jiyong Park74595c12018-07-23 15:22:50 +0900279 case 'e':
280 std::cerr << GetUsage();
281 exit(0);
282 default:
Steven Moreland4dbadf52018-08-08 17:46:10 -0700283 std::cerr << GetUsage();
284 exit(1);
Jiyong Park74595c12018-07-23 15:22:50 +0900285 }
286 } // while
287
288 // Positional arguments
289 if (!lang_option_found && task_ == Options::Task::COMPILE) {
290 // the legacy arguments format
291 if (argc - optind <= 0) {
292 error_message_ << "No input file" << endl;
293 return;
294 }
295 if (language_ == Options::Language::JAVA) {
296 input_files_.emplace_back(argv[optind++]);
297 if (argc - optind >= 1) {
298 output_file_ = argv[optind++];
299 } else {
300 // when output is omitted, output is by default set to the input
301 // file path with .aidl is replaced to .java.
302 output_file_ = input_files_.front();
Steven Moreland4dbadf52018-08-08 17:46:10 -0700303 if (android::base::EndsWith(output_file_, ".aidl")) {
304 output_file_ = output_file_.substr(0, output_file_.length() - strlen(".aidl"));
305 }
306 output_file_ += ".java";
307
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900308 if (!output_dir_.empty()) {
Jiyong Park05463732018-08-09 16:03:02 +0900309 output_file_ = output_dir_ + output_file_;
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900310 }
Jiyong Park74595c12018-07-23 15:22:50 +0900311 }
Steven Morelandc26d8142018-09-17 14:25:33 -0700312 } else if (IsCppOutput()) {
Jiyong Park74595c12018-07-23 15:22:50 +0900313 input_files_.emplace_back(argv[optind++]);
314 if (argc - optind < 2) {
315 error_message_ << "No HEADER_DIR or OUTPUT." << endl;
316 return;
317 }
318 output_header_dir_ = argv[optind++];
Jiyong Park05463732018-08-09 16:03:02 +0900319 if (output_header_dir_.back() != OS_PATH_SEPARATOR) {
320 output_header_dir_.push_back(OS_PATH_SEPARATOR);
321 }
Jiyong Park74595c12018-07-23 15:22:50 +0900322 output_file_ = argv[optind++];
323 }
324 if (argc - optind > 0) {
325 error_message_ << "Too many arguments: ";
326 for (int i = optind; i < argc; i++) {
327 error_message_ << " " << argv[i];
328 }
329 error_message_ << endl;
330 }
Jiyong Park74595c12018-07-23 15:22:50 +0900331 } else {
332 // the new arguments format
Jiyong Parke59c3682018-09-11 23:10:25 +0900333 if (task_ == Options::Task::COMPILE || task_ == Options::Task::DUMP_API) {
Jiyong Park74595c12018-07-23 15:22:50 +0900334 if (argc - optind < 1) {
335 error_message_ << "No input file." << endl;
336 return;
337 }
338 } else {
339 if (argc - optind < 2) {
340 error_message_ << "Insufficient arguments. At least 2 required, but "
341 << "got " << (argc - optind) << "." << endl;
342 return;
343 }
Jiyong Park3656c3c2018-08-01 20:02:01 +0900344 if (task_ != Options::Task::CHECK_API) {
345 output_file_ = argv[optind++];
346 }
Jiyong Park74595c12018-07-23 15:22:50 +0900347 }
348 while (optind < argc) {
349 input_files_.emplace_back(argv[optind++]);
350 }
Christopher Wileya590de82015-09-15 15:46:28 -0700351 }
352
Jiyong Park74595c12018-07-23 15:22:50 +0900353 // filter out invalid combinations
Steven Moreland4dbadf52018-08-08 17:46:10 -0700354 if (lang_option_found) {
Steven Morelandc26d8142018-09-17 14:25:33 -0700355 if (IsCppOutput() && task_ == Options::Task::COMPILE) {
Steven Moreland4dbadf52018-08-08 17:46:10 -0700356 if (output_dir_.empty()) {
357 error_message_ << "Output directory is not set. Set with --out." << endl;
358 return;
359 }
360 if (output_header_dir_.empty()) {
361 error_message_ << "Header output directory is not set. Set with "
362 << "--header_out." << endl;
363 return;
364 }
Jiyong Park74595c12018-07-23 15:22:50 +0900365 }
Steven Moreland4dbadf52018-08-08 17:46:10 -0700366 if (language_ == Options::Language::JAVA && task_ == Options::Task::COMPILE) {
367 if (output_dir_.empty()) {
368 error_message_ << "Output directory is not set. Set with --out." << endl;
369 return;
370 }
371 if (!output_header_dir_.empty()) {
372 error_message_ << "Header output directory is set, which does not make "
373 << "sense for Java." << endl;
374 return;
375 }
Jiyong Park74595c12018-07-23 15:22:50 +0900376 }
377 }
378 if (task_ == Options::Task::COMPILE) {
Jiyong Parke59c3682018-09-11 23:10:25 +0900379 for (const string& input : input_files_) {
380 if (!android::base::EndsWith(input, ".aidl")) {
381 error_message_ << "Expected .aidl file for input but got '" << input << "'" << endl;
382 return;
383 }
384 }
Jiyong Park74595c12018-07-23 15:22:50 +0900385 if (!output_file_.empty() && input_files_.size() > 1) {
386 error_message_ << "Multiple AIDL files can't be compiled to a single "
387 << "output file '" << output_file_ << "'. "
388 << "Use --out=DIR instead for output files." << endl;
389 return;
390 }
391 if (!dependency_file_.empty() && input_files_.size() > 1) {
392 error_message_ << "-d or --dep doesn't work when compiling multiple AIDL "
393 << "files. Use '-a' to generate dependency file next to "
394 << "the output file with the name based on the input "
395 << "file." << endl;
396 return;
397 }
Jiyong Parkce50e262018-10-29 09:54:20 +0900398 if (gen_log_ && language_ != Options::Language::CPP) {
399 error_message_ << "--log is currently supported only for --lang=cpp" << endl;
400 return;
401 }
Christopher Wileya590de82015-09-15 15:46:28 -0700402 }
Jiyong Park309668e2018-07-28 16:55:44 +0900403 if (task_ == Options::Task::PREPROCESS) {
404 if (version_ > 0) {
405 error_message_ << "--version should not be used with '--preprocess'." << endl;
406 return;
407 }
408 }
Jiyong Park3656c3c2018-08-01 20:02:01 +0900409 if (task_ == Options::Task::CHECK_API) {
410 if (input_files_.size() != 2) {
411 error_message_ << "--checkapi requires two inputs for comparing, "
412 << "but got " << input_files_.size() << "." << endl;
413 return;
414 }
415 }
Jiyong Parke59c3682018-09-11 23:10:25 +0900416 if (task_ == Options::Task::DUMP_API) {
417 if (output_dir_.empty()) {
418 error_message_ << "--dump_api requires output directory. Use --out." << endl;
419 return;
420 }
421 }
Jiyong Park05463732018-08-09 16:03:02 +0900422
423 CHECK(output_dir_.empty() || output_dir_.back() == OS_PATH_SEPARATOR);
424 CHECK(output_header_dir_.empty() || output_header_dir_.back() == OS_PATH_SEPARATOR);
Christopher Wileya590de82015-09-15 15:46:28 -0700425}
426
Christopher Wiley4427d862015-09-14 11:07:39 -0700427} // namespace android
428} // namespace aidl