blob: 601a06b3e385df5c6724c94c1a322ec9e80e9cfb [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
Jiyong Parke59c3682018-09-11 23:10:25 +090058 << 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
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
Jiyong Park3c35e392018-08-30 13:10:30 +090082 << " -m FILE, --import=FILE" << endl
83 << " Import FILE directly without searching in the search paths." << endl
Jiyong Park74595c12018-07-23 15:22:50 +090084 << " -p FILE, --preprocessed=FILE" << endl
85 << " Include FILE which is created by --preprocess." << endl
86 << " -d FILE, --dep=FILE" << endl
87 << " Generate dependency file as FILE. Don't use this when" << endl
88 << " there are multiple input files. Use -a then." << endl
89 << " -o DIR, --out=DIR" << endl
90 << " Use DIR as the base output directory for generated files." << endl
91 << " -h DIR, --header_out=DIR" << endl
92 << " Generate C++ headers under DIR." << endl
93 << " -a" << endl
94 << " Generate dependency file next to the output file with the" << endl
95 << " name based on the input file." << endl
96 << " -b" << endl
97 << " Trigger fail when trying to compile a parcelable." << endl
98 << " --ninja" << endl
99 << " Generate dependency file in a format ninja understands." << endl
Steven Moreland6cee3482018-07-18 14:39:58 -0700100 << " --structured" << endl
101 << " Whether this interface is defined exclusively in AIDL." << endl
102 << " It is therefore a candidate for stabilization." << endl
Steven Morelanda57d0a62019-07-30 09:41:14 -0700103 << " --stability=<level>" << endl
104 << " The stability requirement of this interface." << endl
Jiyong Park74595c12018-07-23 15:22:50 +0900105 << " -t, --trace" << endl
106 << " Include tracing code for systrace. Note that if either" << endl
107 << " the client or service code is not auto-generated by this" << endl
108 << " tool, that part will not be traced." << endl
109 << " --transaction_names" << endl
110 << " Generate transaction names." << endl
Andrei Onea8714b022019-02-01 18:55:54 +0000111 << " --apimapping" << endl
112 << " Generates a mapping of declared aidl method signatures to" << endl
113 << " the original line number. e.g.: " << endl
114 << " If line 39 of foo/bar/IFoo.aidl contains:"
115 << " void doFoo(int bar, String baz);" << endl
116 << " Then the result would be:" << endl
117 << " foo.bar.Baz|doFoo|int,String,|void" << endl
118 << " foo/bar/IFoo.aidl:39" << endl
Jiyong Park309668e2018-07-28 16:55:44 +0900119 << " -v VER, --version=VER" << endl
120 << " Set the version of the interface and parcelable to VER." << endl
121 << " VER must be an interger greater than 0." << endl
Paul Trautrimb77048c2020-01-21 16:39:32 +0900122 << " --hash=HASH" << endl
123 << " Set the interface hash to HASH." << endl
Jiyong Parkce50e262018-10-29 09:54:20 +0900124 << " --log" << endl
125 << " Information about the transaction, e.g., method name, argument" << endl
126 << " values, execution time, etc., is provided via callback." << endl
Jiyong Park74595c12018-07-23 15:22:50 +0900127 << " --help" << endl
128 << " Show this help." << endl
129 << endl
130 << "INPUT:" << endl
131 << " An AIDL file." << endl
132 << endl
133 << "OUTPUT:" << endl
134 << " Path to the generated Java or C++ source file. This is ignored when" << endl
135 << " -o or --out is specified or the number of the input files are" << endl
136 << " more than one." << endl
137 << " For Java, if omitted, Java source file is generated at the same" << endl
138 << " place as the input AIDL file," << endl
139 << endl
Christopher Wiley054afbd2015-10-16 17:08:43 -0700140 << "HEADER_DIR:" << endl
Jiyong Park74595c12018-07-23 15:22:50 +0900141 << " Path to where C++ headers are generated." << endl;
142 return sstr.str();
Christopher Wileya590de82015-09-15 15:46:28 -0700143}
144
Devin Moore7b8d5c92020-03-17 14:14:08 -0700145const string Options::LanguageToString(Language language) {
146 switch (language) {
147 case Options::Language::CPP:
148 return "cpp";
149 case Options::Language::JAVA:
150 return "java";
151 case Options::Language::NDK:
152 return "ndk";
Andrei Homescub62afd92020-05-11 19:24:59 -0700153 case Options::Language::RUST:
154 return "rust";
Devin Moore7b8d5c92020-03-17 14:14:08 -0700155 case Options::Language::UNSPECIFIED:
156 return "unspecified";
157 default:
158 AIDL_FATAL(AIDL_LOCATION_HERE)
159 << "Unexpected Options::Language enumerator: " << static_cast<size_t>(language);
160 }
161}
162
Steven Morelanda57d0a62019-07-30 09:41:14 -0700163bool Options::StabilityFromString(const std::string& stability, Stability* out_stability) {
164 if (stability == "vintf") {
165 *out_stability = Stability::VINTF;
166 return true;
167 }
168 return false;
169}
170
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900171Options Options::From(const string& cmdline) {
172 vector<string> args = Split(cmdline, " ");
173 return From(args);
174}
175
176Options Options::From(const vector<string>& args) {
177 Options::Language lang = Options::Language::JAVA;
178 int argc = args.size();
179 if (argc >= 1 && args.at(0) == "aidl-cpp") {
180 lang = Options::Language::CPP;
181 }
182 const char* argv[argc + 1];
183 for (int i = 0; i < argc; i++) {
184 argv[i] = args.at(i).c_str();
185 }
186 argv[argc] = nullptr;
187
188 return Options(argc, argv, lang);
189}
190
Jiyong Park74595c12018-07-23 15:22:50 +0900191Options::Options(int argc, const char* const argv[], Options::Language default_lang)
192 : myname_(argv[0]), language_(default_lang) {
193 bool lang_option_found = false;
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900194 optind = 0;
Jiyong Park74595c12018-07-23 15:22:50 +0900195 while (true) {
196 static struct option long_options[] = {
197 {"lang", required_argument, 0, 'l'},
198 {"preprocess", no_argument, 0, 's'},
Jiyong Parke59c3682018-09-11 23:10:25 +0900199#ifndef _WIN32
Jiyong Park74595c12018-07-23 15:22:50 +0900200 {"dumpapi", no_argument, 0, 'u'},
Jiyong Park3656c3c2018-08-01 20:02:01 +0900201 {"checkapi", no_argument, 0, 'A'},
Jiyong Parke59c3682018-09-11 23:10:25 +0900202#endif
Andrei Onea8714b022019-02-01 18:55:54 +0000203 {"apimapping", required_argument, 0, 'i'},
Jiyong Park74595c12018-07-23 15:22:50 +0900204 {"include", required_argument, 0, 'I'},
Jiyong Park3c35e392018-08-30 13:10:30 +0900205 {"import", required_argument, 0, 'm'},
Jiyong Park74595c12018-07-23 15:22:50 +0900206 {"preprocessed", required_argument, 0, 'p'},
207 {"dep", required_argument, 0, 'd'},
208 {"out", required_argument, 0, 'o'},
209 {"header_out", required_argument, 0, 'h'},
210 {"ninja", no_argument, 0, 'n'},
Steven Morelanda57d0a62019-07-30 09:41:14 -0700211 {"stability", required_argument, 0, 'Y'},
Steven Moreland6cee3482018-07-18 14:39:58 -0700212 {"structured", no_argument, 0, 'S'},
Jiyong Park74595c12018-07-23 15:22:50 +0900213 {"trace", no_argument, 0, 't'},
214 {"transaction_names", no_argument, 0, 'c'},
Jiyong Park309668e2018-07-28 16:55:44 +0900215 {"version", required_argument, 0, 'v'},
Jiyong Parkce50e262018-10-29 09:54:20 +0900216 {"log", no_argument, 0, 'L'},
Paul Trautrimb77048c2020-01-21 16:39:32 +0900217 {"hash", required_argument, 0, 'H'},
Jiyong Park74595c12018-07-23 15:22:50 +0900218 {"help", no_argument, 0, 'e'},
219 {0, 0, 0, 0},
220 };
Jiyong Park3c35e392018-08-30 13:10:30 +0900221 const int c = getopt_long(argc, const_cast<char* const*>(argv),
222 "I:m:p:d:o:h:abtv:", long_options, nullptr);
Jiyong Park74595c12018-07-23 15:22:50 +0900223 if (c == -1) {
224 // no more options
225 break;
Christopher Wileya590de82015-09-15 15:46:28 -0700226 }
Jiyong Park74595c12018-07-23 15:22:50 +0900227 switch (c) {
228 case 'l':
229 if (language_ == Options::Language::CPP) {
230 // aidl-cpp can't set language. aidl-cpp exists only for backwards
231 // compatibility.
232 error_message_ << "aidl-cpp does not support --lang." << endl;
233 return;
234 } else {
235 lang_option_found = true;
Jiyong Park309668e2018-07-28 16:55:44 +0900236 string lang = Trim(optarg);
Jiyong Park74595c12018-07-23 15:22:50 +0900237 if (lang == "java") {
238 language_ = Options::Language::JAVA;
239 task_ = Options::Task::COMPILE;
240 } else if (lang == "cpp") {
241 language_ = Options::Language::CPP;
242 task_ = Options::Task::COMPILE;
Steven Morelandc26d8142018-09-17 14:25:33 -0700243 } else if (lang == "ndk") {
244 language_ = Options::Language::NDK;
245 task_ = Options::Task::COMPILE;
Andrei Homescub62afd92020-05-11 19:24:59 -0700246 } else if (lang == "rust") {
247 language_ = Options::Language::RUST;
248 task_ = Options::Task::COMPILE;
Jiyong Park74595c12018-07-23 15:22:50 +0900249 } else {
250 error_message_ << "Unsupported language: '" << lang << "'" << endl;
251 return;
252 }
253 }
254 break;
255 case 's':
256 if (task_ != Options::Task::UNSPECIFIED) {
257 task_ = Options::Task::PREPROCESS;
258 }
259 break;
Jiyong Parke59c3682018-09-11 23:10:25 +0900260#ifndef _WIN32
Jiyong Park74595c12018-07-23 15:22:50 +0900261 case 'u':
262 if (task_ != Options::Task::UNSPECIFIED) {
Jiyong Park3656c3c2018-08-01 20:02:01 +0900263 task_ = Options::Task::DUMP_API;
264 }
265 break;
266 case 'A':
267 if (task_ != Options::Task::UNSPECIFIED) {
268 task_ = Options::Task::CHECK_API;
269 // to ensure that all parcelables in the api dumpes are structured
270 structured_ = true;
Jiyong Park74595c12018-07-23 15:22:50 +0900271 }
272 break;
Jiyong Parke59c3682018-09-11 23:10:25 +0900273#endif
Jiyong Park8c380532018-08-30 14:55:26 +0900274 case 'I': {
275 import_dirs_.emplace(Trim(optarg));
Jiyong Park3c35e392018-08-30 13:10:30 +0900276 break;
Jiyong Park8c380532018-08-30 14:55:26 +0900277 }
278 case 'm': {
279 import_files_.emplace(Trim(optarg));
Jiyong Park74595c12018-07-23 15:22:50 +0900280 break;
Jiyong Park8c380532018-08-30 14:55:26 +0900281 }
Jiyong Park74595c12018-07-23 15:22:50 +0900282 case 'p':
Jiyong Park309668e2018-07-28 16:55:44 +0900283 preprocessed_files_.emplace_back(Trim(optarg));
Jiyong Park74595c12018-07-23 15:22:50 +0900284 break;
285 case 'd':
Jiyong Park309668e2018-07-28 16:55:44 +0900286 dependency_file_ = Trim(optarg);
Jiyong Park74595c12018-07-23 15:22:50 +0900287 break;
288 case 'o':
Jiyong Park309668e2018-07-28 16:55:44 +0900289 output_dir_ = Trim(optarg);
Jiyong Park05463732018-08-09 16:03:02 +0900290 if (output_dir_.back() != OS_PATH_SEPARATOR) {
291 output_dir_.push_back(OS_PATH_SEPARATOR);
292 }
Jiyong Park74595c12018-07-23 15:22:50 +0900293 break;
294 case 'h':
Jiyong Park309668e2018-07-28 16:55:44 +0900295 output_header_dir_ = Trim(optarg);
Jiyong Park05463732018-08-09 16:03:02 +0900296 if (output_header_dir_.back() != OS_PATH_SEPARATOR) {
297 output_header_dir_.push_back(OS_PATH_SEPARATOR);
298 }
Jiyong Park74595c12018-07-23 15:22:50 +0900299 break;
300 case 'n':
301 dependency_file_ninja_ = true;
302 break;
Steven Moreland6cee3482018-07-18 14:39:58 -0700303 case 'S':
304 structured_ = true;
305 break;
Steven Morelanda57d0a62019-07-30 09:41:14 -0700306 case 'Y': {
307 const string stability_str = Trim(optarg);
308 if (!StabilityFromString(stability_str, &stability_)) {
309 error_message_ << "Unrecognized stability level: '" << stability_str
310 << "'. Must be vintf." << endl;
311 return;
312 }
313 break;
314 }
Jiyong Park74595c12018-07-23 15:22:50 +0900315 case 't':
316 gen_traces_ = true;
317 break;
318 case 'a':
319 auto_dep_file_ = true;
320 break;
321 case 'b':
322 fail_on_parcelable_ = true;
323 break;
324 case 'c':
325 gen_transaction_names_ = true;
326 break;
Jiyong Park309668e2018-07-28 16:55:44 +0900327 case 'v': {
328 const string ver_str = Trim(optarg);
329 int ver = atoi(ver_str.c_str());
330 if (ver > 0) {
331 version_ = ver;
332 } else {
333 error_message_ << "Invalid version number: '" << ver_str << "'. "
334 << "Version must be a positive natural number." << endl;
335 return;
336 }
337 break;
338 }
Paul Trautrimb77048c2020-01-21 16:39:32 +0900339 case 'H':
340 hash_ = Trim(optarg);
341 break;
Jiyong Parkce50e262018-10-29 09:54:20 +0900342 case 'L':
343 gen_log_ = true;
344 break;
Jiyong Park74595c12018-07-23 15:22:50 +0900345 case 'e':
346 std::cerr << GetUsage();
347 exit(0);
Andrei Onea8714b022019-02-01 18:55:54 +0000348 case 'i':
349 output_file_ = Trim(optarg);
350 task_ = Task::DUMP_MAPPINGS;
351 break;
Jiyong Park74595c12018-07-23 15:22:50 +0900352 default:
Steven Moreland4dbadf52018-08-08 17:46:10 -0700353 std::cerr << GetUsage();
354 exit(1);
Jiyong Park74595c12018-07-23 15:22:50 +0900355 }
356 } // while
357
358 // Positional arguments
359 if (!lang_option_found && task_ == Options::Task::COMPILE) {
360 // the legacy arguments format
361 if (argc - optind <= 0) {
362 error_message_ << "No input file" << endl;
363 return;
364 }
Andrei Homescub62afd92020-05-11 19:24:59 -0700365 if (language_ == Options::Language::JAVA || language_ == Options::Language::RUST) {
Jiyong Park74595c12018-07-23 15:22:50 +0900366 input_files_.emplace_back(argv[optind++]);
367 if (argc - optind >= 1) {
368 output_file_ = argv[optind++];
Jiyong Park56f73d72019-06-11 12:20:28 +0900369 } else if (output_dir_.empty()) {
370 // when output is omitted and -o option isn't set, the output is by
371 // default set to the input file path with .aidl is replaced to .java.
372 // If -o option is set, the output path is calculated by
373 // generate_outputFileName which returns "<output_dir>/<package/name>/
374 // <typename>.java"
Jiyong Park74595c12018-07-23 15:22:50 +0900375 output_file_ = input_files_.front();
Steven Moreland4dbadf52018-08-08 17:46:10 -0700376 if (android::base::EndsWith(output_file_, ".aidl")) {
377 output_file_ = output_file_.substr(0, output_file_.length() - strlen(".aidl"));
378 }
Andrei Homescub62afd92020-05-11 19:24:59 -0700379 output_file_ += (language_ == Options::Language::JAVA) ? ".java" : ".rs";
Jiyong Park74595c12018-07-23 15:22:50 +0900380 }
Steven Morelandc26d8142018-09-17 14:25:33 -0700381 } else if (IsCppOutput()) {
Jiyong Park74595c12018-07-23 15:22:50 +0900382 input_files_.emplace_back(argv[optind++]);
383 if (argc - optind < 2) {
384 error_message_ << "No HEADER_DIR or OUTPUT." << endl;
385 return;
386 }
387 output_header_dir_ = argv[optind++];
Jiyong Park05463732018-08-09 16:03:02 +0900388 if (output_header_dir_.back() != OS_PATH_SEPARATOR) {
389 output_header_dir_.push_back(OS_PATH_SEPARATOR);
390 }
Jiyong Park74595c12018-07-23 15:22:50 +0900391 output_file_ = argv[optind++];
392 }
393 if (argc - optind > 0) {
394 error_message_ << "Too many arguments: ";
395 for (int i = optind; i < argc; i++) {
396 error_message_ << " " << argv[i];
397 }
398 error_message_ << endl;
399 }
Jiyong Park74595c12018-07-23 15:22:50 +0900400 } else {
401 // the new arguments format
Jiyong Parke59c3682018-09-11 23:10:25 +0900402 if (task_ == Options::Task::COMPILE || task_ == Options::Task::DUMP_API) {
Jiyong Park74595c12018-07-23 15:22:50 +0900403 if (argc - optind < 1) {
404 error_message_ << "No input file." << endl;
405 return;
406 }
407 } else {
408 if (argc - optind < 2) {
409 error_message_ << "Insufficient arguments. At least 2 required, but "
410 << "got " << (argc - optind) << "." << endl;
411 return;
412 }
Andrei Onea8714b022019-02-01 18:55:54 +0000413 if (task_ != Options::Task::CHECK_API && task_ != Options::Task::DUMP_MAPPINGS) {
Jiyong Park3656c3c2018-08-01 20:02:01 +0900414 output_file_ = argv[optind++];
415 }
Jiyong Park74595c12018-07-23 15:22:50 +0900416 }
417 while (optind < argc) {
418 input_files_.emplace_back(argv[optind++]);
419 }
Christopher Wileya590de82015-09-15 15:46:28 -0700420 }
421
Jiyong Park74595c12018-07-23 15:22:50 +0900422 // filter out invalid combinations
Steven Moreland4dbadf52018-08-08 17:46:10 -0700423 if (lang_option_found) {
Steven Morelandc26d8142018-09-17 14:25:33 -0700424 if (IsCppOutput() && task_ == Options::Task::COMPILE) {
Steven Moreland4dbadf52018-08-08 17:46:10 -0700425 if (output_dir_.empty()) {
426 error_message_ << "Output directory is not set. Set with --out." << endl;
427 return;
428 }
429 if (output_header_dir_.empty()) {
430 error_message_ << "Header output directory is not set. Set with "
431 << "--header_out." << endl;
432 return;
433 }
Jiyong Park74595c12018-07-23 15:22:50 +0900434 }
Steven Moreland4dbadf52018-08-08 17:46:10 -0700435 if (language_ == Options::Language::JAVA && task_ == Options::Task::COMPILE) {
436 if (output_dir_.empty()) {
437 error_message_ << "Output directory is not set. Set with --out." << endl;
438 return;
439 }
440 if (!output_header_dir_.empty()) {
441 error_message_ << "Header output directory is set, which does not make "
442 << "sense for Java." << endl;
443 return;
444 }
Jiyong Park74595c12018-07-23 15:22:50 +0900445 }
Andrei Homescub62afd92020-05-11 19:24:59 -0700446 if (language_ == Options::Language::RUST && task_ == Options::Task::COMPILE) {
447 if (output_dir_.empty()) {
448 error_message_ << "Output directory is not set. Set with --out." << endl;
449 return;
450 }
451 if (!output_header_dir_.empty()) {
452 error_message_ << "Header output directory is set, which does not make "
453 << "sense for Rust." << endl;
454 return;
455 }
456 }
Jiyong Park74595c12018-07-23 15:22:50 +0900457 }
458 if (task_ == Options::Task::COMPILE) {
Jiyong Parke59c3682018-09-11 23:10:25 +0900459 for (const string& input : input_files_) {
460 if (!android::base::EndsWith(input, ".aidl")) {
461 error_message_ << "Expected .aidl file for input but got '" << input << "'" << endl;
462 return;
463 }
464 }
Jiyong Park74595c12018-07-23 15:22:50 +0900465 if (!output_file_.empty() && input_files_.size() > 1) {
466 error_message_ << "Multiple AIDL files can't be compiled to a single "
467 << "output file '" << output_file_ << "'. "
468 << "Use --out=DIR instead for output files." << endl;
469 return;
470 }
471 if (!dependency_file_.empty() && input_files_.size() > 1) {
472 error_message_ << "-d or --dep doesn't work when compiling multiple AIDL "
473 << "files. Use '-a' to generate dependency file next to "
474 << "the output file with the name based on the input "
475 << "file." << endl;
476 return;
477 }
Jeongik Cha37e2ad52019-04-18 13:44:26 +0900478 if (gen_log_ && (language_ != Options::Language::CPP && language_ != Options::Language::NDK)) {
479 error_message_ << "--log is currently supported for either --lang=cpp or --lang=ndk" << endl;
Jiyong Parkce50e262018-10-29 09:54:20 +0900480 return;
481 }
Christopher Wileya590de82015-09-15 15:46:28 -0700482 }
Jiyong Park309668e2018-07-28 16:55:44 +0900483 if (task_ == Options::Task::PREPROCESS) {
484 if (version_ > 0) {
485 error_message_ << "--version should not be used with '--preprocess'." << endl;
486 return;
487 }
488 }
Jiyong Park3656c3c2018-08-01 20:02:01 +0900489 if (task_ == Options::Task::CHECK_API) {
490 if (input_files_.size() != 2) {
491 error_message_ << "--checkapi requires two inputs for comparing, "
492 << "but got " << input_files_.size() << "." << endl;
493 return;
494 }
495 }
Jiyong Parke59c3682018-09-11 23:10:25 +0900496 if (task_ == Options::Task::DUMP_API) {
497 if (output_dir_.empty()) {
Jooyung Han4bb2a152020-10-12 18:11:41 +0900498 error_message_ << "--dumpapi requires output directory. Use --out." << endl;
Jiyong Parke59c3682018-09-11 23:10:25 +0900499 return;
500 }
501 }
Jiyong Park05463732018-08-09 16:03:02 +0900502
Steven Moreland21780812020-09-11 01:29:45 +0000503 AIDL_FATAL_IF(!output_dir_.empty() && output_dir_.back() != OS_PATH_SEPARATOR, output_dir_);
504 AIDL_FATAL_IF(!output_header_dir_.empty() && output_header_dir_.back() != OS_PATH_SEPARATOR,
505 output_header_dir_);
Christopher Wileya590de82015-09-15 15:46:28 -0700506}
507
Christopher Wiley4427d862015-09-14 11:07:39 -0700508} // namespace aidl
Steven Morelandf4c64df2019-07-29 19:54:04 -0700509} // namespace android