blob: a4dafba718cd19d604a356d16acaf5bc7e08f268 [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>
Devin Moore7b8d5c92020-03-17 14:14:08 -070030#include "aidl_language.h"
Christopher Wiley4432ccf2015-09-18 18:32:08 -070031
Jiyong Park6f77e0c2018-07-28 16:55:44 +090032using android::base::Split;
Jiyong Park74595c12018-07-23 15:22:50 +090033using android::base::Trim;
Christopher Wileya590de82015-09-15 15:46:28 -070034using std::endl;
Christopher Wiley4427d862015-09-14 11:07:39 -070035using std::string;
Christopher Wiley4427d862015-09-14 11:07:39 -070036
Steven Moreland1c894432020-02-14 11:32:44 -080037#ifndef PLATFORM_SDK_VERSION
38#define PLATFORM_SDK_VERSION "<UNKNOWN>"
39#endif
40
Christopher Wiley4427d862015-09-14 11:07:39 -070041namespace android {
42namespace aidl {
Christopher Wiley4427d862015-09-14 11:07:39 -070043
Jiyong Park74595c12018-07-23 15:22:50 +090044string Options::GetUsage() const {
45 std::ostringstream sstr;
Steven Moreland1c894432020-02-14 11:32:44 -080046 sstr << "AIDL Compiler: built for platform SDK version " << PLATFORM_SDK_VERSION << endl;
Jiyong Park74595c12018-07-23 15:22:50 +090047 sstr << "usage:" << endl
Andrei Homescub62afd92020-05-11 19:24:59 -070048 << myname_ << " --lang={java|cpp|ndk|rust} [OPTION]... INPUT..." << endl
49 << " Generate Java, C++ or Rust files for AIDL file(s)." << endl
Steven Morelandda0654f2018-07-17 12:24:38 -070050 << endl
Jiyong Park74595c12018-07-23 15:22:50 +090051 << myname_ << " --preprocess OUTPUT INPUT..." << endl
52 << " Create an AIDL file having declarations of AIDL file(s)." << endl
Steven Morelandda0654f2018-07-17 12:24:38 -070053 << endl
Jiyong Parke59c3682018-09-11 23:10:25 +090054#ifndef _WIN32
55 << myname_ << " --dumpapi --out=DIR INPUT..." << endl
56 << " Dump API signature of AIDL file(s) to DIR." << endl
Jiyong Park3656c3c2018-08-01 20:02:01 +090057 << endl
Jooyung Hanb8a97772021-01-19 01:27:38 +090058 << myname_ << " --checkapi[={compatible|equal}] OLD_DIR NEW_DIR" << endl
59 << " Check whether NEW_DIR API dump is {compatible|equal} extension " << endl
60 << " of the API dump OLD_DIR. Default: compatible" << endl
Jiyong Parke59c3682018-09-11 23:10:25 +090061#endif
Steven Morelandda0654f2018-07-17 12:24:38 -070062 << endl;
Adam Lesinskiffa16862014-01-23 18:17:42 -080063
Jiyong Park74595c12018-07-23 15:22:50 +090064 // 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;
Andrei Homescub62afd92020-05-11 19:24:59 -070073 } else if (language_ == Options::Language::RUST) {
74 sstr << myname_ << " [OPTION]... INPUT [OUTPUT]" << endl
75 << " Generate Rust file for an AIDL file." << endl
76 << endl;
Christopher Wiley4427d862015-09-14 11:07:39 -070077 }
78
Jiyong Park74595c12018-07-23 15:22:50 +090079 sstr << "OPTION:" << endl
80 << " -I DIR, --include=DIR" << endl
81 << " Use DIR as a search path for import statements." << endl
82 << " -p FILE, --preprocessed=FILE" << endl
83 << " Include FILE which is created by --preprocess." << endl
84 << " -d FILE, --dep=FILE" << endl
85 << " Generate dependency file as FILE. Don't use this when" << endl
86 << " there are multiple input files. Use -a then." << endl
87 << " -o DIR, --out=DIR" << endl
88 << " Use DIR as the base output directory for generated files." << endl
89 << " -h DIR, --header_out=DIR" << endl
90 << " Generate C++ headers under DIR." << endl
91 << " -a" << endl
92 << " Generate dependency file next to the output file with the" << endl
93 << " name based on the input file." << endl
94 << " -b" << endl
95 << " Trigger fail when trying to compile a parcelable." << endl
96 << " --ninja" << endl
97 << " Generate dependency file in a format ninja understands." << endl
Steven Moreland6cee3482018-07-18 14:39:58 -070098 << " --structured" << endl
99 << " Whether this interface is defined exclusively in AIDL." << endl
100 << " It is therefore a candidate for stabilization." << endl
Steven Morelanda57d0a62019-07-30 09:41:14 -0700101 << " --stability=<level>" << endl
102 << " The stability requirement of this interface." << endl
Jiyong Park74595c12018-07-23 15:22:50 +0900103 << " -t, --trace" << endl
104 << " Include tracing code for systrace. Note that if either" << endl
105 << " the client or service code is not auto-generated by this" << endl
106 << " tool, that part will not be traced." << endl
107 << " --transaction_names" << endl
108 << " Generate transaction names." << endl
Andrei Onea8714b022019-02-01 18:55:54 +0000109 << " --apimapping" << endl
110 << " Generates a mapping of declared aidl method signatures to" << endl
111 << " the original line number. e.g.: " << endl
112 << " If line 39 of foo/bar/IFoo.aidl contains:"
113 << " void doFoo(int bar, String baz);" << endl
114 << " Then the result would be:" << endl
115 << " foo.bar.Baz|doFoo|int,String,|void" << endl
116 << " foo/bar/IFoo.aidl:39" << endl
Jiyong Park309668e2018-07-28 16:55:44 +0900117 << " -v VER, --version=VER" << endl
118 << " Set the version of the interface and parcelable to VER." << endl
119 << " VER must be an interger greater than 0." << endl
Paul Trautrimb77048c2020-01-21 16:39:32 +0900120 << " --hash=HASH" << endl
121 << " Set the interface hash to HASH." << endl
Jiyong Parkce50e262018-10-29 09:54:20 +0900122 << " --log" << endl
123 << " Information about the transaction, e.g., method name, argument" << endl
124 << " values, execution time, etc., is provided via callback." << endl
Jooyung Han888c5bc2020-12-22 17:28:47 +0900125 << " -Werror" << endl
126 << " Turn warnings into errors." << endl
127 << " -Wno-error=<warning>" << endl
128 << " Turn the specified warning into a warning even if -Werror is specified."
129 << endl
130 << " -W<warning>" << endl
131 << " Enable the specified warning." << endl
132 << " -Wno-<warning>" << endl
133 << " Disable the specified warning." << endl
134 << " -w" << endl
135 << " Disable all diagnostics. -w wins -Weverything" << endl
136 << " -Weverything" << endl
137 << " Enable all diagnostics." << endl
Jiyong Park74595c12018-07-23 15:22:50 +0900138 << " --help" << endl
139 << " Show this help." << endl
140 << endl
141 << "INPUT:" << endl
142 << " An AIDL file." << endl
143 << endl
144 << "OUTPUT:" << endl
145 << " Path to the generated Java or C++ source file. This is ignored when" << endl
146 << " -o or --out is specified or the number of the input files are" << endl
147 << " more than one." << endl
148 << " For Java, if omitted, Java source file is generated at the same" << endl
149 << " place as the input AIDL file," << endl
150 << endl
Christopher Wiley054afbd2015-10-16 17:08:43 -0700151 << "HEADER_DIR:" << endl
Jiyong Park74595c12018-07-23 15:22:50 +0900152 << " Path to where C++ headers are generated." << endl;
153 return sstr.str();
Christopher Wileya590de82015-09-15 15:46:28 -0700154}
155
Jooyung Han9435e9a2021-01-06 10:16:31 +0900156string to_string(Options::Language language) {
Devin Moore7b8d5c92020-03-17 14:14:08 -0700157 switch (language) {
158 case Options::Language::CPP:
159 return "cpp";
160 case Options::Language::JAVA:
161 return "java";
162 case Options::Language::NDK:
163 return "ndk";
Andrei Homescub62afd92020-05-11 19:24:59 -0700164 case Options::Language::RUST:
165 return "rust";
Devin Moore7b8d5c92020-03-17 14:14:08 -0700166 case Options::Language::UNSPECIFIED:
167 return "unspecified";
168 default:
169 AIDL_FATAL(AIDL_LOCATION_HERE)
170 << "Unexpected Options::Language enumerator: " << static_cast<size_t>(language);
171 }
172}
173
Steven Morelanda57d0a62019-07-30 09:41:14 -0700174bool Options::StabilityFromString(const std::string& stability, Stability* out_stability) {
175 if (stability == "vintf") {
176 *out_stability = Stability::VINTF;
177 return true;
178 }
179 return false;
180}
181
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900182Options Options::From(const string& cmdline) {
183 vector<string> args = Split(cmdline, " ");
184 return From(args);
185}
186
187Options Options::From(const vector<string>& args) {
188 Options::Language lang = Options::Language::JAVA;
189 int argc = args.size();
190 if (argc >= 1 && args.at(0) == "aidl-cpp") {
191 lang = Options::Language::CPP;
192 }
193 const char* argv[argc + 1];
194 for (int i = 0; i < argc; i++) {
195 argv[i] = args.at(i).c_str();
196 }
197 argv[argc] = nullptr;
198
199 return Options(argc, argv, lang);
200}
201
Jooyung Han888c5bc2020-12-22 17:28:47 +0900202Options::Options(int argc, const char* const raw_argv[], Options::Language default_lang)
Steven Moreland6514b252021-08-09 15:20:18 -0700203 : myname_(argc >= 1 ? raw_argv[0] : "aidl"), language_(default_lang) {
Jooyung Han888c5bc2020-12-22 17:28:47 +0900204 std::vector<const char*> argv = warning_options_.Parse(argc, raw_argv, error_message_);
205 if (!Ok()) return;
206 argc = argv.size();
207
Jiyong Park74595c12018-07-23 15:22:50 +0900208 bool lang_option_found = false;
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900209 optind = 0;
Jiyong Park74595c12018-07-23 15:22:50 +0900210 while (true) {
211 static struct option long_options[] = {
212 {"lang", required_argument, 0, 'l'},
213 {"preprocess", no_argument, 0, 's'},
Jiyong Parke59c3682018-09-11 23:10:25 +0900214#ifndef _WIN32
Jiyong Park74595c12018-07-23 15:22:50 +0900215 {"dumpapi", no_argument, 0, 'u'},
Jooyung Han252657e2021-02-27 02:51:39 +0900216 {"no_license", no_argument, 0, 'x'},
Jooyung Hanb8a97772021-01-19 01:27:38 +0900217 {"checkapi", optional_argument, 0, 'A'},
Jiyong Parke59c3682018-09-11 23:10:25 +0900218#endif
Andrei Onea8714b022019-02-01 18:55:54 +0000219 {"apimapping", required_argument, 0, 'i'},
Jiyong Park74595c12018-07-23 15:22:50 +0900220 {"include", required_argument, 0, 'I'},
221 {"preprocessed", required_argument, 0, 'p'},
222 {"dep", required_argument, 0, 'd'},
223 {"out", required_argument, 0, 'o'},
224 {"header_out", required_argument, 0, 'h'},
225 {"ninja", no_argument, 0, 'n'},
Steven Morelanda57d0a62019-07-30 09:41:14 -0700226 {"stability", required_argument, 0, 'Y'},
Steven Moreland6cee3482018-07-18 14:39:58 -0700227 {"structured", no_argument, 0, 'S'},
Jiyong Park74595c12018-07-23 15:22:50 +0900228 {"trace", no_argument, 0, 't'},
229 {"transaction_names", no_argument, 0, 'c'},
Jiyong Park309668e2018-07-28 16:55:44 +0900230 {"version", required_argument, 0, 'v'},
Jiyong Parkce50e262018-10-29 09:54:20 +0900231 {"log", no_argument, 0, 'L'},
Paul Trautrimb77048c2020-01-21 16:39:32 +0900232 {"hash", required_argument, 0, 'H'},
Jiyong Park74595c12018-07-23 15:22:50 +0900233 {"help", no_argument, 0, 'e'},
234 {0, 0, 0, 0},
235 };
Jooyung Han888c5bc2020-12-22 17:28:47 +0900236 const int c = getopt_long(argc, const_cast<char* const*>(argv.data()),
Jooyung Han61d78032021-06-11 00:29:18 +0900237 "I:p:d:o:h:abtv:", long_options, nullptr);
Jiyong Park74595c12018-07-23 15:22:50 +0900238 if (c == -1) {
239 // no more options
240 break;
Christopher Wileya590de82015-09-15 15:46:28 -0700241 }
Jiyong Park74595c12018-07-23 15:22:50 +0900242 switch (c) {
243 case 'l':
244 if (language_ == Options::Language::CPP) {
245 // aidl-cpp can't set language. aidl-cpp exists only for backwards
246 // compatibility.
247 error_message_ << "aidl-cpp does not support --lang." << endl;
248 return;
249 } else {
250 lang_option_found = true;
Jiyong Park309668e2018-07-28 16:55:44 +0900251 string lang = Trim(optarg);
Jiyong Park74595c12018-07-23 15:22:50 +0900252 if (lang == "java") {
253 language_ = Options::Language::JAVA;
254 task_ = Options::Task::COMPILE;
255 } else if (lang == "cpp") {
256 language_ = Options::Language::CPP;
257 task_ = Options::Task::COMPILE;
Steven Morelandc26d8142018-09-17 14:25:33 -0700258 } else if (lang == "ndk") {
259 language_ = Options::Language::NDK;
260 task_ = Options::Task::COMPILE;
Andrei Homescub62afd92020-05-11 19:24:59 -0700261 } else if (lang == "rust") {
262 language_ = Options::Language::RUST;
263 task_ = Options::Task::COMPILE;
Jiyong Park74595c12018-07-23 15:22:50 +0900264 } else {
265 error_message_ << "Unsupported language: '" << lang << "'" << endl;
266 return;
267 }
268 }
269 break;
270 case 's':
271 if (task_ != Options::Task::UNSPECIFIED) {
272 task_ = Options::Task::PREPROCESS;
273 }
274 break;
Jiyong Parke59c3682018-09-11 23:10:25 +0900275#ifndef _WIN32
Jiyong Park74595c12018-07-23 15:22:50 +0900276 case 'u':
277 if (task_ != Options::Task::UNSPECIFIED) {
Jiyong Park3656c3c2018-08-01 20:02:01 +0900278 task_ = Options::Task::DUMP_API;
279 }
280 break;
Jooyung Han252657e2021-02-27 02:51:39 +0900281 case 'x':
282 dump_no_license_ = true;
283 break;
Jiyong Park3656c3c2018-08-01 20:02:01 +0900284 case 'A':
285 if (task_ != Options::Task::UNSPECIFIED) {
286 task_ = Options::Task::CHECK_API;
287 // to ensure that all parcelables in the api dumpes are structured
288 structured_ = true;
Jooyung Hanb8a97772021-01-19 01:27:38 +0900289 if (optarg) {
290 if (strcmp(optarg, "compatible") == 0)
291 check_api_level_ = CheckApiLevel::COMPATIBLE;
292 else if (strcmp(optarg, "equal") == 0)
293 check_api_level_ = CheckApiLevel::EQUAL;
294 else {
295 error_message_ << "Unsupported --checkapi level: '" << optarg << "'" << endl;
296 return;
297 }
298 }
Jiyong Park74595c12018-07-23 15:22:50 +0900299 }
300 break;
Jiyong Parke59c3682018-09-11 23:10:25 +0900301#endif
Jiyong Park8c380532018-08-30 14:55:26 +0900302 case 'I': {
303 import_dirs_.emplace(Trim(optarg));
Jiyong Park3c35e392018-08-30 13:10:30 +0900304 break;
Jiyong Park8c380532018-08-30 14:55:26 +0900305 }
Jiyong Park74595c12018-07-23 15:22:50 +0900306 case 'p':
Jiyong Park309668e2018-07-28 16:55:44 +0900307 preprocessed_files_.emplace_back(Trim(optarg));
Jiyong Park74595c12018-07-23 15:22:50 +0900308 break;
309 case 'd':
Jiyong Park309668e2018-07-28 16:55:44 +0900310 dependency_file_ = Trim(optarg);
Jiyong Park74595c12018-07-23 15:22:50 +0900311 break;
312 case 'o':
Jiyong Park309668e2018-07-28 16:55:44 +0900313 output_dir_ = Trim(optarg);
Jiyong Park05463732018-08-09 16:03:02 +0900314 if (output_dir_.back() != OS_PATH_SEPARATOR) {
315 output_dir_.push_back(OS_PATH_SEPARATOR);
316 }
Jiyong Park74595c12018-07-23 15:22:50 +0900317 break;
318 case 'h':
Jiyong Park309668e2018-07-28 16:55:44 +0900319 output_header_dir_ = Trim(optarg);
Jiyong Park05463732018-08-09 16:03:02 +0900320 if (output_header_dir_.back() != OS_PATH_SEPARATOR) {
321 output_header_dir_.push_back(OS_PATH_SEPARATOR);
322 }
Jiyong Park74595c12018-07-23 15:22:50 +0900323 break;
324 case 'n':
325 dependency_file_ninja_ = true;
326 break;
Steven Moreland6cee3482018-07-18 14:39:58 -0700327 case 'S':
328 structured_ = true;
329 break;
Steven Morelanda57d0a62019-07-30 09:41:14 -0700330 case 'Y': {
331 const string stability_str = Trim(optarg);
332 if (!StabilityFromString(stability_str, &stability_)) {
333 error_message_ << "Unrecognized stability level: '" << stability_str
334 << "'. Must be vintf." << endl;
335 return;
336 }
337 break;
338 }
Jiyong Park74595c12018-07-23 15:22:50 +0900339 case 't':
340 gen_traces_ = true;
341 break;
342 case 'a':
343 auto_dep_file_ = true;
344 break;
345 case 'b':
346 fail_on_parcelable_ = true;
347 break;
348 case 'c':
349 gen_transaction_names_ = true;
350 break;
Jiyong Park309668e2018-07-28 16:55:44 +0900351 case 'v': {
352 const string ver_str = Trim(optarg);
353 int ver = atoi(ver_str.c_str());
354 if (ver > 0) {
355 version_ = ver;
356 } else {
357 error_message_ << "Invalid version number: '" << ver_str << "'. "
358 << "Version must be a positive natural number." << endl;
359 return;
360 }
361 break;
362 }
Paul Trautrimb77048c2020-01-21 16:39:32 +0900363 case 'H':
364 hash_ = Trim(optarg);
365 break;
Jiyong Parkce50e262018-10-29 09:54:20 +0900366 case 'L':
367 gen_log_ = true;
368 break;
Jiyong Park74595c12018-07-23 15:22:50 +0900369 case 'e':
370 std::cerr << GetUsage();
371 exit(0);
Andrei Onea8714b022019-02-01 18:55:54 +0000372 case 'i':
373 output_file_ = Trim(optarg);
374 task_ = Task::DUMP_MAPPINGS;
375 break;
Jiyong Park74595c12018-07-23 15:22:50 +0900376 default:
Steven Moreland4dbadf52018-08-08 17:46:10 -0700377 std::cerr << GetUsage();
378 exit(1);
Jiyong Park74595c12018-07-23 15:22:50 +0900379 }
380 } // while
381
382 // Positional arguments
383 if (!lang_option_found && task_ == Options::Task::COMPILE) {
384 // the legacy arguments format
385 if (argc - optind <= 0) {
386 error_message_ << "No input file" << endl;
387 return;
388 }
Andrei Homescub62afd92020-05-11 19:24:59 -0700389 if (language_ == Options::Language::JAVA || language_ == Options::Language::RUST) {
Jiyong Park74595c12018-07-23 15:22:50 +0900390 input_files_.emplace_back(argv[optind++]);
391 if (argc - optind >= 1) {
392 output_file_ = argv[optind++];
Jiyong Park56f73d72019-06-11 12:20:28 +0900393 } else if (output_dir_.empty()) {
394 // when output is omitted and -o option isn't set, the output is by
395 // default set to the input file path with .aidl is replaced to .java.
396 // If -o option is set, the output path is calculated by
Jooyung Hanb3cd63f2021-01-05 13:38:46 +0900397 // GetOutputFilePath which returns "<output_dir>/<package/name>/
Jiyong Park56f73d72019-06-11 12:20:28 +0900398 // <typename>.java"
Jiyong Park74595c12018-07-23 15:22:50 +0900399 output_file_ = input_files_.front();
Steven Moreland4dbadf52018-08-08 17:46:10 -0700400 if (android::base::EndsWith(output_file_, ".aidl")) {
401 output_file_ = output_file_.substr(0, output_file_.length() - strlen(".aidl"));
402 }
Andrei Homescub62afd92020-05-11 19:24:59 -0700403 output_file_ += (language_ == Options::Language::JAVA) ? ".java" : ".rs";
Jiyong Park74595c12018-07-23 15:22:50 +0900404 }
Steven Morelandc26d8142018-09-17 14:25:33 -0700405 } else if (IsCppOutput()) {
Jiyong Park74595c12018-07-23 15:22:50 +0900406 input_files_.emplace_back(argv[optind++]);
407 if (argc - optind < 2) {
408 error_message_ << "No HEADER_DIR or OUTPUT." << endl;
409 return;
410 }
411 output_header_dir_ = argv[optind++];
Jiyong Park05463732018-08-09 16:03:02 +0900412 if (output_header_dir_.back() != OS_PATH_SEPARATOR) {
413 output_header_dir_.push_back(OS_PATH_SEPARATOR);
414 }
Jiyong Park74595c12018-07-23 15:22:50 +0900415 output_file_ = argv[optind++];
416 }
417 if (argc - optind > 0) {
418 error_message_ << "Too many arguments: ";
419 for (int i = optind; i < argc; i++) {
420 error_message_ << " " << argv[i];
421 }
422 error_message_ << endl;
423 }
Jiyong Park74595c12018-07-23 15:22:50 +0900424 } else {
425 // the new arguments format
Jiyong Parke59c3682018-09-11 23:10:25 +0900426 if (task_ == Options::Task::COMPILE || task_ == Options::Task::DUMP_API) {
Jiyong Park74595c12018-07-23 15:22:50 +0900427 if (argc - optind < 1) {
428 error_message_ << "No input file." << endl;
429 return;
430 }
431 } else {
432 if (argc - optind < 2) {
433 error_message_ << "Insufficient arguments. At least 2 required, but "
434 << "got " << (argc - optind) << "." << endl;
435 return;
436 }
Andrei Onea8714b022019-02-01 18:55:54 +0000437 if (task_ != Options::Task::CHECK_API && task_ != Options::Task::DUMP_MAPPINGS) {
Jiyong Park3656c3c2018-08-01 20:02:01 +0900438 output_file_ = argv[optind++];
439 }
Jiyong Park74595c12018-07-23 15:22:50 +0900440 }
441 while (optind < argc) {
442 input_files_.emplace_back(argv[optind++]);
443 }
Christopher Wileya590de82015-09-15 15:46:28 -0700444 }
445
Jiyong Park74595c12018-07-23 15:22:50 +0900446 // filter out invalid combinations
Steven Moreland4dbadf52018-08-08 17:46:10 -0700447 if (lang_option_found) {
Steven Morelandc26d8142018-09-17 14:25:33 -0700448 if (IsCppOutput() && task_ == Options::Task::COMPILE) {
Steven Moreland4dbadf52018-08-08 17:46:10 -0700449 if (output_dir_.empty()) {
450 error_message_ << "Output directory is not set. Set with --out." << endl;
451 return;
452 }
453 if (output_header_dir_.empty()) {
454 error_message_ << "Header output directory is not set. Set with "
455 << "--header_out." << endl;
456 return;
457 }
Jiyong Park74595c12018-07-23 15:22:50 +0900458 }
Steven Moreland4dbadf52018-08-08 17:46:10 -0700459 if (language_ == Options::Language::JAVA && task_ == Options::Task::COMPILE) {
460 if (output_dir_.empty()) {
461 error_message_ << "Output directory is not set. Set with --out." << endl;
462 return;
463 }
464 if (!output_header_dir_.empty()) {
465 error_message_ << "Header output directory is set, which does not make "
466 << "sense for Java." << endl;
467 return;
468 }
Jiyong Park74595c12018-07-23 15:22:50 +0900469 }
Andrei Homescub62afd92020-05-11 19:24:59 -0700470 if (language_ == Options::Language::RUST && task_ == Options::Task::COMPILE) {
471 if (output_dir_.empty()) {
472 error_message_ << "Output directory is not set. Set with --out." << endl;
473 return;
474 }
475 if (!output_header_dir_.empty()) {
476 error_message_ << "Header output directory is set, which does not make "
477 << "sense for Rust." << endl;
478 return;
479 }
480 }
Jiyong Park74595c12018-07-23 15:22:50 +0900481 }
482 if (task_ == Options::Task::COMPILE) {
Jiyong Parke59c3682018-09-11 23:10:25 +0900483 for (const string& input : input_files_) {
484 if (!android::base::EndsWith(input, ".aidl")) {
485 error_message_ << "Expected .aidl file for input but got '" << input << "'" << endl;
486 return;
487 }
488 }
Jiyong Park74595c12018-07-23 15:22:50 +0900489 if (!output_file_.empty() && input_files_.size() > 1) {
490 error_message_ << "Multiple AIDL files can't be compiled to a single "
491 << "output file '" << output_file_ << "'. "
492 << "Use --out=DIR instead for output files." << endl;
493 return;
494 }
495 if (!dependency_file_.empty() && input_files_.size() > 1) {
496 error_message_ << "-d or --dep doesn't work when compiling multiple AIDL "
497 << "files. Use '-a' to generate dependency file next to "
498 << "the output file with the name based on the input "
499 << "file." << endl;
500 return;
501 }
Jeongik Cha37e2ad52019-04-18 13:44:26 +0900502 if (gen_log_ && (language_ != Options::Language::CPP && language_ != Options::Language::NDK)) {
503 error_message_ << "--log is currently supported for either --lang=cpp or --lang=ndk" << endl;
Jiyong Parkce50e262018-10-29 09:54:20 +0900504 return;
505 }
Christopher Wileya590de82015-09-15 15:46:28 -0700506 }
Jiyong Park309668e2018-07-28 16:55:44 +0900507 if (task_ == Options::Task::PREPROCESS) {
508 if (version_ > 0) {
509 error_message_ << "--version should not be used with '--preprocess'." << endl;
510 return;
511 }
512 }
Jiyong Park3656c3c2018-08-01 20:02:01 +0900513 if (task_ == Options::Task::CHECK_API) {
514 if (input_files_.size() != 2) {
515 error_message_ << "--checkapi requires two inputs for comparing, "
516 << "but got " << input_files_.size() << "." << endl;
517 return;
518 }
519 }
Jiyong Parke59c3682018-09-11 23:10:25 +0900520 if (task_ == Options::Task::DUMP_API) {
521 if (output_dir_.empty()) {
Jooyung Han4bb2a152020-10-12 18:11:41 +0900522 error_message_ << "--dumpapi requires output directory. Use --out." << endl;
Jiyong Parke59c3682018-09-11 23:10:25 +0900523 return;
524 }
525 }
Jiyong Park05463732018-08-09 16:03:02 +0900526
Steven Moreland21780812020-09-11 01:29:45 +0000527 AIDL_FATAL_IF(!output_dir_.empty() && output_dir_.back() != OS_PATH_SEPARATOR, output_dir_);
528 AIDL_FATAL_IF(!output_header_dir_.empty() && output_header_dir_.back() != OS_PATH_SEPARATOR,
529 output_header_dir_);
Christopher Wileya590de82015-09-15 15:46:28 -0700530}
531
Jooyung Han888c5bc2020-12-22 17:28:47 +0900532std::vector<const char*> WarningOptions::Parse(int argc, const char* const raw_argv[],
533 ErrorMessage& error_message) {
534 std::vector<const char*> remains;
535 for (int i = 0; i < argc; i++) {
536 auto arg = raw_argv[i];
537 if (strcmp(arg, "-Weverything") == 0) {
538 enable_all_ = true;
539 } else if (strcmp(arg, "-Werror") == 0) {
540 as_errors_ = true;
541 } else if (strcmp(arg, "-w") == 0) {
542 disable_all_ = true;
543 } else if (base::StartsWith(arg, "-Wno-error=")) {
544 no_errors_.insert(arg + strlen("-Wno-error="));
545 } else if (base::StartsWith(arg, "-Wno-")) {
546 disabled_.insert(arg + strlen("-Wno-"));
547 } else if (base::StartsWith(arg, "-W")) {
548 enabled_.insert(arg + strlen("-W"));
549 } else {
550 remains.push_back(arg);
551 }
552 }
553
554 for (const auto& names : {no_errors_, disabled_, enabled_}) {
555 for (const auto& name : names) {
556 if (kAllDiagnostics.count(name) == 0) {
557 error_message << "unknown warning: " << name << "\n";
558 return {};
559 }
560 }
561 }
562
Jooyung Han808a2a02020-12-28 16:46:54 +0900563 return remains;
564}
565
566DiagnosticMapping WarningOptions::GetDiagnosticMapping() const {
567 DiagnosticMapping mapping;
Jooyung Han888c5bc2020-12-22 17:28:47 +0900568 for (const auto& [_, d] : kAllDiagnostics) {
569 bool enabled = d.default_enabled;
570 if (enable_all_ || enabled_.find(d.name) != enabled_.end()) {
571 enabled = true;
572 }
573 if (disable_all_ || disabled_.find(d.name) != disabled_.end()) {
574 enabled = false;
575 }
576
577 DiagnosticSeverity severity = DiagnosticSeverity::DISABLED;
578 if (enabled) {
579 severity = DiagnosticSeverity::WARNING;
580 if (as_errors_ && no_errors_.find(d.name) == no_errors_.end()) {
581 severity = DiagnosticSeverity::ERROR;
582 }
583 }
Jooyung Han808a2a02020-12-28 16:46:54 +0900584 mapping.Severity(d.id, severity);
Jooyung Han888c5bc2020-12-22 17:28:47 +0900585 }
Jooyung Han808a2a02020-12-28 16:46:54 +0900586 return mapping;
Jooyung Han888c5bc2020-12-22 17:28:47 +0900587}
588
Christopher Wiley4427d862015-09-14 11:07:39 -0700589} // namespace aidl
Steven Morelandf4c64df2019-07-29 19:54:04 -0700590} // namespace android