blob: b79c4f1f5a6bf5d5a78a1a717d7cf9e2a7b35d54 [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
Steven Morelandb736f602021-08-09 16:19:08 -070029#include <android-base/logging.h>
Jiyong Park74595c12018-07-23 15:22:50 +090030#include <android-base/strings.h>
Devin Moore7b8d5c92020-03-17 14:14:08 -070031#include "aidl_language.h"
Christopher Wiley4432ccf2015-09-18 18:32:08 -070032
Jiyong Park6f77e0c2018-07-28 16:55:44 +090033using android::base::Split;
Jiyong Park74595c12018-07-23 15:22:50 +090034using android::base::Trim;
Christopher Wileya590de82015-09-15 15:46:28 -070035using std::endl;
Christopher Wiley4427d862015-09-14 11:07:39 -070036using std::string;
Christopher Wiley4427d862015-09-14 11:07:39 -070037
Steven Moreland1c894432020-02-14 11:32:44 -080038#ifndef PLATFORM_SDK_VERSION
39#define PLATFORM_SDK_VERSION "<UNKNOWN>"
40#endif
41
Christopher Wiley4427d862015-09-14 11:07:39 -070042namespace android {
43namespace aidl {
Christopher Wiley4427d862015-09-14 11:07:39 -070044
Jiyong Park74595c12018-07-23 15:22:50 +090045string Options::GetUsage() const {
46 std::ostringstream sstr;
Steven Moreland1c894432020-02-14 11:32:44 -080047 sstr << "AIDL Compiler: built for platform SDK version " << PLATFORM_SDK_VERSION << endl;
Jiyong Park74595c12018-07-23 15:22:50 +090048 sstr << "usage:" << endl
Andrei Homescub62afd92020-05-11 19:24:59 -070049 << myname_ << " --lang={java|cpp|ndk|rust} [OPTION]... INPUT..." << endl
50 << " Generate Java, C++ or Rust files for AIDL file(s)." << endl
Steven Morelandda0654f2018-07-17 12:24:38 -070051 << endl
Jiyong Park74595c12018-07-23 15:22:50 +090052 << myname_ << " --preprocess OUTPUT INPUT..." << endl
53 << " Create an AIDL file having declarations of AIDL file(s)." << endl
Steven Morelandda0654f2018-07-17 12:24:38 -070054 << endl
Jiyong Parke59c3682018-09-11 23:10:25 +090055#ifndef _WIN32
56 << myname_ << " --dumpapi --out=DIR INPUT..." << endl
57 << " Dump API signature of AIDL file(s) to DIR." << endl
Jiyong Park3656c3c2018-08-01 20:02:01 +090058 << endl
Jooyung Hanb8a97772021-01-19 01:27:38 +090059 << myname_ << " --checkapi[={compatible|equal}] OLD_DIR NEW_DIR" << endl
60 << " Check whether NEW_DIR API dump is {compatible|equal} extension " << endl
61 << " of the API dump OLD_DIR. Default: compatible" << endl
Jiyong Parke59c3682018-09-11 23:10:25 +090062#endif
Steven Morelandda0654f2018-07-17 12:24:38 -070063 << endl;
Adam Lesinskiffa16862014-01-23 18:17:42 -080064
Jiyong Park74595c12018-07-23 15:22:50 +090065 // Legacy option formats
66 if (language_ == Options::Language::JAVA) {
67 sstr << myname_ << " [OPTION]... INPUT [OUTPUT]" << endl
68 << " Generate a Java file for an AIDL file." << endl
69 << endl;
70 } else if (language_ == Options::Language::CPP) {
71 sstr << myname_ << " [OPTION]... INPUT HEADER_DIR OUTPUT" << endl
72 << " Generate C++ headers and source for an AIDL file." << endl
73 << endl;
Andrei Homescub62afd92020-05-11 19:24:59 -070074 } else if (language_ == Options::Language::RUST) {
75 sstr << myname_ << " [OPTION]... INPUT [OUTPUT]" << endl
76 << " Generate Rust file for an AIDL file." << endl
77 << endl;
Christopher Wiley4427d862015-09-14 11:07:39 -070078 }
79
Jiyong Park74595c12018-07-23 15:22:50 +090080 sstr << "OPTION:" << endl
81 << " -I DIR, --include=DIR" << endl
82 << " Use DIR as a search path for import statements." << endl
83 << " -p FILE, --preprocessed=FILE" << endl
84 << " Include FILE which is created by --preprocess." << endl
85 << " -d FILE, --dep=FILE" << endl
86 << " Generate dependency file as FILE. Don't use this when" << endl
87 << " there are multiple input files. Use -a then." << endl
88 << " -o DIR, --out=DIR" << endl
89 << " Use DIR as the base output directory for generated files." << endl
90 << " -h DIR, --header_out=DIR" << endl
91 << " Generate C++ headers under DIR." << endl
92 << " -a" << endl
93 << " Generate dependency file next to the output file with the" << endl
94 << " name based on the input file." << endl
95 << " -b" << endl
96 << " Trigger fail when trying to compile a parcelable." << endl
97 << " --ninja" << endl
98 << " Generate dependency file in a format ninja understands." << endl
Steven Moreland6cee3482018-07-18 14:39:58 -070099 << " --structured" << endl
100 << " Whether this interface is defined exclusively in AIDL." << endl
101 << " It is therefore a candidate for stabilization." << endl
Steven Morelanda57d0a62019-07-30 09:41:14 -0700102 << " --stability=<level>" << endl
103 << " The stability requirement of this interface." << endl
Jiyong Park74595c12018-07-23 15:22:50 +0900104 << " -t, --trace" << endl
105 << " Include tracing code for systrace. Note that if either" << endl
106 << " the client or service code is not auto-generated by this" << endl
107 << " tool, that part will not be traced." << endl
108 << " --transaction_names" << endl
109 << " Generate transaction names." << endl
Andrei Onea8714b022019-02-01 18:55:54 +0000110 << " --apimapping" << endl
111 << " Generates a mapping of declared aidl method signatures to" << endl
112 << " the original line number. e.g.: " << endl
113 << " If line 39 of foo/bar/IFoo.aidl contains:"
114 << " void doFoo(int bar, String baz);" << endl
115 << " Then the result would be:" << endl
116 << " foo.bar.Baz|doFoo|int,String,|void" << endl
117 << " foo/bar/IFoo.aidl:39" << endl
Jiyong Park309668e2018-07-28 16:55:44 +0900118 << " -v VER, --version=VER" << endl
119 << " Set the version of the interface and parcelable to VER." << endl
120 << " VER must be an interger greater than 0." << endl
Paul Trautrimb77048c2020-01-21 16:39:32 +0900121 << " --hash=HASH" << endl
122 << " Set the interface hash to HASH." << endl
Jiyong Parkce50e262018-10-29 09:54:20 +0900123 << " --log" << endl
124 << " Information about the transaction, e.g., method name, argument" << endl
125 << " values, execution time, etc., is provided via callback." << endl
Jooyung Han888c5bc2020-12-22 17:28:47 +0900126 << " -Werror" << endl
127 << " Turn warnings into errors." << endl
128 << " -Wno-error=<warning>" << endl
129 << " Turn the specified warning into a warning even if -Werror is specified."
130 << endl
131 << " -W<warning>" << endl
132 << " Enable the specified warning." << endl
133 << " -Wno-<warning>" << endl
134 << " Disable the specified warning." << endl
135 << " -w" << endl
136 << " Disable all diagnostics. -w wins -Weverything" << endl
137 << " -Weverything" << endl
138 << " Enable all diagnostics." << endl
Jiyong Park74595c12018-07-23 15:22:50 +0900139 << " --help" << endl
140 << " Show this help." << endl
141 << endl
142 << "INPUT:" << endl
143 << " An AIDL file." << endl
144 << endl
145 << "OUTPUT:" << endl
146 << " Path to the generated Java or C++ source file. This is ignored when" << endl
147 << " -o or --out is specified or the number of the input files are" << endl
148 << " more than one." << endl
149 << " For Java, if omitted, Java source file is generated at the same" << endl
150 << " place as the input AIDL file," << endl
151 << endl
Christopher Wiley054afbd2015-10-16 17:08:43 -0700152 << "HEADER_DIR:" << endl
Jiyong Park74595c12018-07-23 15:22:50 +0900153 << " Path to where C++ headers are generated." << endl;
154 return sstr.str();
Christopher Wileya590de82015-09-15 15:46:28 -0700155}
156
Jooyung Han9435e9a2021-01-06 10:16:31 +0900157string to_string(Options::Language language) {
Devin Moore7b8d5c92020-03-17 14:14:08 -0700158 switch (language) {
159 case Options::Language::CPP:
160 return "cpp";
161 case Options::Language::JAVA:
162 return "java";
163 case Options::Language::NDK:
164 return "ndk";
Andrei Homescub62afd92020-05-11 19:24:59 -0700165 case Options::Language::RUST:
166 return "rust";
Devin Moore7b8d5c92020-03-17 14:14:08 -0700167 case Options::Language::UNSPECIFIED:
168 return "unspecified";
169 default:
170 AIDL_FATAL(AIDL_LOCATION_HERE)
171 << "Unexpected Options::Language enumerator: " << static_cast<size_t>(language);
172 }
173}
174
Steven Morelanda57d0a62019-07-30 09:41:14 -0700175bool Options::StabilityFromString(const std::string& stability, Stability* out_stability) {
176 if (stability == "vintf") {
177 *out_stability = Stability::VINTF;
178 return true;
179 }
180 return false;
181}
182
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900183Options Options::From(const string& cmdline) {
184 vector<string> args = Split(cmdline, " ");
185 return From(args);
186}
187
188Options Options::From(const vector<string>& args) {
189 Options::Language lang = Options::Language::JAVA;
190 int argc = args.size();
191 if (argc >= 1 && args.at(0) == "aidl-cpp") {
192 lang = Options::Language::CPP;
193 }
194 const char* argv[argc + 1];
195 for (int i = 0; i < argc; i++) {
196 argv[i] = args.at(i).c_str();
197 }
198 argv[argc] = nullptr;
199
200 return Options(argc, argv, lang);
201}
202
Jooyung Han888c5bc2020-12-22 17:28:47 +0900203Options::Options(int argc, const char* const raw_argv[], Options::Language default_lang)
Steven Moreland6514b252021-08-09 15:20:18 -0700204 : myname_(argc >= 1 ? raw_argv[0] : "aidl"), language_(default_lang) {
Jooyung Han888c5bc2020-12-22 17:28:47 +0900205 std::vector<const char*> argv = warning_options_.Parse(argc, raw_argv, error_message_);
206 if (!Ok()) return;
207 argc = argv.size();
208
Jiyong Park74595c12018-07-23 15:22:50 +0900209 bool lang_option_found = false;
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900210 optind = 0;
Jiyong Park74595c12018-07-23 15:22:50 +0900211 while (true) {
212 static struct option long_options[] = {
213 {"lang", required_argument, 0, 'l'},
214 {"preprocess", no_argument, 0, 's'},
Jiyong Parke59c3682018-09-11 23:10:25 +0900215#ifndef _WIN32
Jiyong Park74595c12018-07-23 15:22:50 +0900216 {"dumpapi", no_argument, 0, 'u'},
Jooyung Han252657e2021-02-27 02:51:39 +0900217 {"no_license", no_argument, 0, 'x'},
Jooyung Hanb8a97772021-01-19 01:27:38 +0900218 {"checkapi", optional_argument, 0, 'A'},
Jiyong Parke59c3682018-09-11 23:10:25 +0900219#endif
Andrei Onea8714b022019-02-01 18:55:54 +0000220 {"apimapping", required_argument, 0, 'i'},
Jiyong Park74595c12018-07-23 15:22:50 +0900221 {"include", required_argument, 0, 'I'},
222 {"preprocessed", required_argument, 0, 'p'},
223 {"dep", required_argument, 0, 'd'},
224 {"out", required_argument, 0, 'o'},
225 {"header_out", required_argument, 0, 'h'},
226 {"ninja", no_argument, 0, 'n'},
Steven Morelanda57d0a62019-07-30 09:41:14 -0700227 {"stability", required_argument, 0, 'Y'},
Steven Moreland6cee3482018-07-18 14:39:58 -0700228 {"structured", no_argument, 0, 'S'},
Jiyong Park74595c12018-07-23 15:22:50 +0900229 {"trace", no_argument, 0, 't'},
230 {"transaction_names", no_argument, 0, 'c'},
Jiyong Park309668e2018-07-28 16:55:44 +0900231 {"version", required_argument, 0, 'v'},
Jiyong Parkce50e262018-10-29 09:54:20 +0900232 {"log", no_argument, 0, 'L'},
Paul Trautrimb77048c2020-01-21 16:39:32 +0900233 {"hash", required_argument, 0, 'H'},
Jiyong Park74595c12018-07-23 15:22:50 +0900234 {"help", no_argument, 0, 'e'},
235 {0, 0, 0, 0},
236 };
Jooyung Han888c5bc2020-12-22 17:28:47 +0900237 const int c = getopt_long(argc, const_cast<char* const*>(argv.data()),
Jooyung Han61d78032021-06-11 00:29:18 +0900238 "I:p:d:o:h:abtv:", long_options, nullptr);
Jiyong Park74595c12018-07-23 15:22:50 +0900239 if (c == -1) {
240 // no more options
241 break;
Christopher Wileya590de82015-09-15 15:46:28 -0700242 }
Jiyong Park74595c12018-07-23 15:22:50 +0900243 switch (c) {
244 case 'l':
245 if (language_ == Options::Language::CPP) {
246 // aidl-cpp can't set language. aidl-cpp exists only for backwards
247 // compatibility.
248 error_message_ << "aidl-cpp does not support --lang." << endl;
249 return;
250 } else {
251 lang_option_found = true;
Jiyong Park309668e2018-07-28 16:55:44 +0900252 string lang = Trim(optarg);
Jiyong Park74595c12018-07-23 15:22:50 +0900253 if (lang == "java") {
254 language_ = Options::Language::JAVA;
255 task_ = Options::Task::COMPILE;
256 } else if (lang == "cpp") {
257 language_ = Options::Language::CPP;
258 task_ = Options::Task::COMPILE;
Steven Morelandc26d8142018-09-17 14:25:33 -0700259 } else if (lang == "ndk") {
260 language_ = Options::Language::NDK;
261 task_ = Options::Task::COMPILE;
Andrei Homescub62afd92020-05-11 19:24:59 -0700262 } else if (lang == "rust") {
263 language_ = Options::Language::RUST;
264 task_ = Options::Task::COMPILE;
Jiyong Park74595c12018-07-23 15:22:50 +0900265 } else {
266 error_message_ << "Unsupported language: '" << lang << "'" << endl;
267 return;
268 }
269 }
270 break;
271 case 's':
272 if (task_ != Options::Task::UNSPECIFIED) {
273 task_ = Options::Task::PREPROCESS;
274 }
275 break;
Jiyong Parke59c3682018-09-11 23:10:25 +0900276#ifndef _WIN32
Jiyong Park74595c12018-07-23 15:22:50 +0900277 case 'u':
278 if (task_ != Options::Task::UNSPECIFIED) {
Jiyong Park3656c3c2018-08-01 20:02:01 +0900279 task_ = Options::Task::DUMP_API;
280 }
281 break;
Jooyung Han252657e2021-02-27 02:51:39 +0900282 case 'x':
283 dump_no_license_ = true;
284 break;
Jiyong Park3656c3c2018-08-01 20:02:01 +0900285 case 'A':
286 if (task_ != Options::Task::UNSPECIFIED) {
287 task_ = Options::Task::CHECK_API;
288 // to ensure that all parcelables in the api dumpes are structured
289 structured_ = true;
Jooyung Hanb8a97772021-01-19 01:27:38 +0900290 if (optarg) {
291 if (strcmp(optarg, "compatible") == 0)
292 check_api_level_ = CheckApiLevel::COMPATIBLE;
293 else if (strcmp(optarg, "equal") == 0)
294 check_api_level_ = CheckApiLevel::EQUAL;
295 else {
296 error_message_ << "Unsupported --checkapi level: '" << optarg << "'" << endl;
297 return;
298 }
299 }
Jiyong Park74595c12018-07-23 15:22:50 +0900300 }
301 break;
Jiyong Parke59c3682018-09-11 23:10:25 +0900302#endif
Jiyong Park8c380532018-08-30 14:55:26 +0900303 case 'I': {
304 import_dirs_.emplace(Trim(optarg));
Jiyong Park3c35e392018-08-30 13:10:30 +0900305 break;
Jiyong Park8c380532018-08-30 14:55:26 +0900306 }
Jiyong Park74595c12018-07-23 15:22:50 +0900307 case 'p':
Jiyong Park309668e2018-07-28 16:55:44 +0900308 preprocessed_files_.emplace_back(Trim(optarg));
Jiyong Park74595c12018-07-23 15:22:50 +0900309 break;
310 case 'd':
Jiyong Park309668e2018-07-28 16:55:44 +0900311 dependency_file_ = Trim(optarg);
Jiyong Park74595c12018-07-23 15:22:50 +0900312 break;
313 case 'o':
Jiyong Park309668e2018-07-28 16:55:44 +0900314 output_dir_ = Trim(optarg);
Jiyong Park05463732018-08-09 16:03:02 +0900315 if (output_dir_.back() != OS_PATH_SEPARATOR) {
316 output_dir_.push_back(OS_PATH_SEPARATOR);
317 }
Jiyong Park74595c12018-07-23 15:22:50 +0900318 break;
319 case 'h':
Jiyong Park309668e2018-07-28 16:55:44 +0900320 output_header_dir_ = Trim(optarg);
Jiyong Park05463732018-08-09 16:03:02 +0900321 if (output_header_dir_.back() != OS_PATH_SEPARATOR) {
322 output_header_dir_.push_back(OS_PATH_SEPARATOR);
323 }
Jiyong Park74595c12018-07-23 15:22:50 +0900324 break;
325 case 'n':
326 dependency_file_ninja_ = true;
327 break;
Steven Moreland6cee3482018-07-18 14:39:58 -0700328 case 'S':
329 structured_ = true;
330 break;
Steven Morelanda57d0a62019-07-30 09:41:14 -0700331 case 'Y': {
332 const string stability_str = Trim(optarg);
333 if (!StabilityFromString(stability_str, &stability_)) {
334 error_message_ << "Unrecognized stability level: '" << stability_str
335 << "'. Must be vintf." << endl;
336 return;
337 }
338 break;
339 }
Jiyong Park74595c12018-07-23 15:22:50 +0900340 case 't':
341 gen_traces_ = true;
342 break;
343 case 'a':
344 auto_dep_file_ = true;
345 break;
346 case 'b':
347 fail_on_parcelable_ = true;
348 break;
349 case 'c':
350 gen_transaction_names_ = true;
351 break;
Jiyong Park309668e2018-07-28 16:55:44 +0900352 case 'v': {
353 const string ver_str = Trim(optarg);
354 int ver = atoi(ver_str.c_str());
355 if (ver > 0) {
356 version_ = ver;
357 } else {
358 error_message_ << "Invalid version number: '" << ver_str << "'. "
359 << "Version must be a positive natural number." << endl;
360 return;
361 }
362 break;
363 }
Paul Trautrimb77048c2020-01-21 16:39:32 +0900364 case 'H':
365 hash_ = Trim(optarg);
366 break;
Jiyong Parkce50e262018-10-29 09:54:20 +0900367 case 'L':
368 gen_log_ = true;
369 break;
Jiyong Park74595c12018-07-23 15:22:50 +0900370 case 'e':
371 std::cerr << GetUsage();
Steven Morelandb736f602021-08-09 16:19:08 -0700372 task_ = Task::HELP;
373 CHECK(Ok());
374 return;
Andrei Onea8714b022019-02-01 18:55:54 +0000375 case 'i':
376 output_file_ = Trim(optarg);
377 task_ = Task::DUMP_MAPPINGS;
378 break;
Jiyong Park74595c12018-07-23 15:22:50 +0900379 default:
Steven Morelandb736f602021-08-09 16:19:08 -0700380 error_message_ << GetUsage();
381 CHECK(!Ok());
382 return;
Jiyong Park74595c12018-07-23 15:22:50 +0900383 }
384 } // while
385
386 // Positional arguments
387 if (!lang_option_found && task_ == Options::Task::COMPILE) {
388 // the legacy arguments format
389 if (argc - optind <= 0) {
390 error_message_ << "No input file" << endl;
391 return;
392 }
Andrei Homescub62afd92020-05-11 19:24:59 -0700393 if (language_ == Options::Language::JAVA || language_ == Options::Language::RUST) {
Jiyong Park74595c12018-07-23 15:22:50 +0900394 input_files_.emplace_back(argv[optind++]);
395 if (argc - optind >= 1) {
396 output_file_ = argv[optind++];
Jiyong Park56f73d72019-06-11 12:20:28 +0900397 } else if (output_dir_.empty()) {
398 // when output is omitted and -o option isn't set, the output is by
399 // default set to the input file path with .aidl is replaced to .java.
400 // If -o option is set, the output path is calculated by
Jooyung Hanb3cd63f2021-01-05 13:38:46 +0900401 // GetOutputFilePath which returns "<output_dir>/<package/name>/
Jiyong Park56f73d72019-06-11 12:20:28 +0900402 // <typename>.java"
Jiyong Park74595c12018-07-23 15:22:50 +0900403 output_file_ = input_files_.front();
Steven Moreland4dbadf52018-08-08 17:46:10 -0700404 if (android::base::EndsWith(output_file_, ".aidl")) {
405 output_file_ = output_file_.substr(0, output_file_.length() - strlen(".aidl"));
406 }
Andrei Homescub62afd92020-05-11 19:24:59 -0700407 output_file_ += (language_ == Options::Language::JAVA) ? ".java" : ".rs";
Jiyong Park74595c12018-07-23 15:22:50 +0900408 }
Steven Morelandc26d8142018-09-17 14:25:33 -0700409 } else if (IsCppOutput()) {
Jiyong Park74595c12018-07-23 15:22:50 +0900410 input_files_.emplace_back(argv[optind++]);
411 if (argc - optind < 2) {
412 error_message_ << "No HEADER_DIR or OUTPUT." << endl;
413 return;
414 }
415 output_header_dir_ = argv[optind++];
Jiyong Park05463732018-08-09 16:03:02 +0900416 if (output_header_dir_.back() != OS_PATH_SEPARATOR) {
417 output_header_dir_.push_back(OS_PATH_SEPARATOR);
418 }
Jiyong Park74595c12018-07-23 15:22:50 +0900419 output_file_ = argv[optind++];
420 }
421 if (argc - optind > 0) {
422 error_message_ << "Too many arguments: ";
423 for (int i = optind; i < argc; i++) {
424 error_message_ << " " << argv[i];
425 }
426 error_message_ << endl;
427 }
Jiyong Park74595c12018-07-23 15:22:50 +0900428 } else {
429 // the new arguments format
Jiyong Parke59c3682018-09-11 23:10:25 +0900430 if (task_ == Options::Task::COMPILE || task_ == Options::Task::DUMP_API) {
Jiyong Park74595c12018-07-23 15:22:50 +0900431 if (argc - optind < 1) {
432 error_message_ << "No input file." << endl;
433 return;
434 }
435 } else {
436 if (argc - optind < 2) {
437 error_message_ << "Insufficient arguments. At least 2 required, but "
438 << "got " << (argc - optind) << "." << endl;
439 return;
440 }
Andrei Onea8714b022019-02-01 18:55:54 +0000441 if (task_ != Options::Task::CHECK_API && task_ != Options::Task::DUMP_MAPPINGS) {
Jiyong Park3656c3c2018-08-01 20:02:01 +0900442 output_file_ = argv[optind++];
443 }
Jiyong Park74595c12018-07-23 15:22:50 +0900444 }
445 while (optind < argc) {
446 input_files_.emplace_back(argv[optind++]);
447 }
Christopher Wileya590de82015-09-15 15:46:28 -0700448 }
449
Jiyong Park74595c12018-07-23 15:22:50 +0900450 // filter out invalid combinations
Steven Moreland4dbadf52018-08-08 17:46:10 -0700451 if (lang_option_found) {
Steven Morelandc26d8142018-09-17 14:25:33 -0700452 if (IsCppOutput() && task_ == Options::Task::COMPILE) {
Steven Moreland4dbadf52018-08-08 17:46:10 -0700453 if (output_dir_.empty()) {
454 error_message_ << "Output directory is not set. Set with --out." << endl;
455 return;
456 }
457 if (output_header_dir_.empty()) {
458 error_message_ << "Header output directory is not set. Set with "
459 << "--header_out." << endl;
460 return;
461 }
Jiyong Park74595c12018-07-23 15:22:50 +0900462 }
Steven Moreland4dbadf52018-08-08 17:46:10 -0700463 if (language_ == Options::Language::JAVA && task_ == Options::Task::COMPILE) {
464 if (output_dir_.empty()) {
465 error_message_ << "Output directory is not set. Set with --out." << endl;
466 return;
467 }
468 if (!output_header_dir_.empty()) {
469 error_message_ << "Header output directory is set, which does not make "
470 << "sense for Java." << endl;
471 return;
472 }
Jiyong Park74595c12018-07-23 15:22:50 +0900473 }
Andrei Homescub62afd92020-05-11 19:24:59 -0700474 if (language_ == Options::Language::RUST && task_ == Options::Task::COMPILE) {
475 if (output_dir_.empty()) {
476 error_message_ << "Output directory is not set. Set with --out." << endl;
477 return;
478 }
479 if (!output_header_dir_.empty()) {
480 error_message_ << "Header output directory is set, which does not make "
481 << "sense for Rust." << endl;
482 return;
483 }
484 }
Jiyong Park74595c12018-07-23 15:22:50 +0900485 }
486 if (task_ == Options::Task::COMPILE) {
Jiyong Parke59c3682018-09-11 23:10:25 +0900487 for (const string& input : input_files_) {
488 if (!android::base::EndsWith(input, ".aidl")) {
489 error_message_ << "Expected .aidl file for input but got '" << input << "'" << endl;
490 return;
491 }
492 }
Jiyong Park74595c12018-07-23 15:22:50 +0900493 if (!output_file_.empty() && input_files_.size() > 1) {
494 error_message_ << "Multiple AIDL files can't be compiled to a single "
495 << "output file '" << output_file_ << "'. "
496 << "Use --out=DIR instead for output files." << endl;
497 return;
498 }
499 if (!dependency_file_.empty() && input_files_.size() > 1) {
500 error_message_ << "-d or --dep doesn't work when compiling multiple AIDL "
501 << "files. Use '-a' to generate dependency file next to "
502 << "the output file with the name based on the input "
503 << "file." << endl;
504 return;
505 }
Jeongik Cha37e2ad52019-04-18 13:44:26 +0900506 if (gen_log_ && (language_ != Options::Language::CPP && language_ != Options::Language::NDK)) {
507 error_message_ << "--log is currently supported for either --lang=cpp or --lang=ndk" << endl;
Jiyong Parkce50e262018-10-29 09:54:20 +0900508 return;
509 }
Christopher Wileya590de82015-09-15 15:46:28 -0700510 }
Jiyong Park309668e2018-07-28 16:55:44 +0900511 if (task_ == Options::Task::PREPROCESS) {
512 if (version_ > 0) {
513 error_message_ << "--version should not be used with '--preprocess'." << endl;
514 return;
515 }
516 }
Jiyong Park3656c3c2018-08-01 20:02:01 +0900517 if (task_ == Options::Task::CHECK_API) {
518 if (input_files_.size() != 2) {
519 error_message_ << "--checkapi requires two inputs for comparing, "
520 << "but got " << input_files_.size() << "." << endl;
521 return;
522 }
523 }
Jiyong Parke59c3682018-09-11 23:10:25 +0900524 if (task_ == Options::Task::DUMP_API) {
525 if (output_dir_.empty()) {
Jooyung Han4bb2a152020-10-12 18:11:41 +0900526 error_message_ << "--dumpapi requires output directory. Use --out." << endl;
Jiyong Parke59c3682018-09-11 23:10:25 +0900527 return;
528 }
529 }
Jiyong Park05463732018-08-09 16:03:02 +0900530
Steven Moreland21780812020-09-11 01:29:45 +0000531 AIDL_FATAL_IF(!output_dir_.empty() && output_dir_.back() != OS_PATH_SEPARATOR, output_dir_);
532 AIDL_FATAL_IF(!output_header_dir_.empty() && output_header_dir_.back() != OS_PATH_SEPARATOR,
533 output_header_dir_);
Christopher Wileya590de82015-09-15 15:46:28 -0700534}
535
Jooyung Han888c5bc2020-12-22 17:28:47 +0900536std::vector<const char*> WarningOptions::Parse(int argc, const char* const raw_argv[],
537 ErrorMessage& error_message) {
538 std::vector<const char*> remains;
539 for (int i = 0; i < argc; i++) {
540 auto arg = raw_argv[i];
541 if (strcmp(arg, "-Weverything") == 0) {
542 enable_all_ = true;
543 } else if (strcmp(arg, "-Werror") == 0) {
544 as_errors_ = true;
545 } else if (strcmp(arg, "-w") == 0) {
546 disable_all_ = true;
547 } else if (base::StartsWith(arg, "-Wno-error=")) {
548 no_errors_.insert(arg + strlen("-Wno-error="));
549 } else if (base::StartsWith(arg, "-Wno-")) {
550 disabled_.insert(arg + strlen("-Wno-"));
551 } else if (base::StartsWith(arg, "-W")) {
552 enabled_.insert(arg + strlen("-W"));
553 } else {
554 remains.push_back(arg);
555 }
556 }
557
558 for (const auto& names : {no_errors_, disabled_, enabled_}) {
559 for (const auto& name : names) {
560 if (kAllDiagnostics.count(name) == 0) {
561 error_message << "unknown warning: " << name << "\n";
562 return {};
563 }
564 }
565 }
566
Jooyung Han808a2a02020-12-28 16:46:54 +0900567 return remains;
568}
569
570DiagnosticMapping WarningOptions::GetDiagnosticMapping() const {
571 DiagnosticMapping mapping;
Jooyung Han888c5bc2020-12-22 17:28:47 +0900572 for (const auto& [_, d] : kAllDiagnostics) {
573 bool enabled = d.default_enabled;
574 if (enable_all_ || enabled_.find(d.name) != enabled_.end()) {
575 enabled = true;
576 }
577 if (disable_all_ || disabled_.find(d.name) != disabled_.end()) {
578 enabled = false;
579 }
580
581 DiagnosticSeverity severity = DiagnosticSeverity::DISABLED;
582 if (enabled) {
583 severity = DiagnosticSeverity::WARNING;
584 if (as_errors_ && no_errors_.find(d.name) == no_errors_.end()) {
585 severity = DiagnosticSeverity::ERROR;
586 }
587 }
Jooyung Han808a2a02020-12-28 16:46:54 +0900588 mapping.Severity(d.id, severity);
Jooyung Han888c5bc2020-12-22 17:28:47 +0900589 }
Jooyung Han808a2a02020-12-28 16:46:54 +0900590 return mapping;
Jooyung Han888c5bc2020-12-22 17:28:47 +0900591}
592
Christopher Wiley4427d862015-09-14 11:07:39 -0700593} // namespace aidl
Steven Morelandf4c64df2019-07-29 19:54:04 -0700594} // namespace android