Christopher Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015, The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 16 | |
| 17 | #include "options.h" |
Jiyong Park | 0546373 | 2018-08-09 16:03:02 +0900 | [diff] [blame] | 18 | #include "logging.h" |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 19 | #include "os.h" |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 20 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 21 | #include <getopt.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <unistd.h> |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 24 | #include <algorithm> |
Christopher Wiley | a590de8 | 2015-09-15 15:46:28 -0700 | [diff] [blame] | 25 | #include <iostream> |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 26 | #include <sstream> |
| 27 | #include <string> |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 28 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 29 | #include <android-base/strings.h> |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 30 | #include "aidl_language.h" |
Christopher Wiley | 4432ccf | 2015-09-18 18:32:08 -0700 | [diff] [blame] | 31 | |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 32 | using android::base::Split; |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 33 | using android::base::Trim; |
Christopher Wiley | a590de8 | 2015-09-15 15:46:28 -0700 | [diff] [blame] | 34 | using std::endl; |
Christopher Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 35 | using std::string; |
Christopher Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 36 | |
Steven Moreland | 1c89443 | 2020-02-14 11:32:44 -0800 | [diff] [blame] | 37 | #ifndef PLATFORM_SDK_VERSION |
| 38 | #define PLATFORM_SDK_VERSION "<UNKNOWN>" |
| 39 | #endif |
| 40 | |
Christopher Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 41 | namespace android { |
| 42 | namespace aidl { |
Christopher Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 43 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 44 | string Options::GetUsage() const { |
| 45 | std::ostringstream sstr; |
Steven Moreland | 1c89443 | 2020-02-14 11:32:44 -0800 | [diff] [blame] | 46 | sstr << "AIDL Compiler: built for platform SDK version " << PLATFORM_SDK_VERSION << endl; |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 47 | sstr << "usage:" << endl |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 48 | << myname_ << " --lang={java|cpp|ndk|rust} [OPTION]... INPUT..." << endl |
| 49 | << " Generate Java, C++ or Rust files for AIDL file(s)." << endl |
Steven Moreland | da0654f | 2018-07-17 12:24:38 -0700 | [diff] [blame] | 50 | << endl |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 51 | << myname_ << " --preprocess OUTPUT INPUT..." << endl |
| 52 | << " Create an AIDL file having declarations of AIDL file(s)." << endl |
Steven Moreland | da0654f | 2018-07-17 12:24:38 -0700 | [diff] [blame] | 53 | << endl |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 54 | #ifndef _WIN32 |
| 55 | << myname_ << " --dumpapi --out=DIR INPUT..." << endl |
| 56 | << " Dump API signature of AIDL file(s) to DIR." << endl |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 57 | << endl |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 58 | << 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 Moreland | da0654f | 2018-07-17 12:24:38 -0700 | [diff] [blame] | 62 | << endl; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 63 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 64 | // 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 Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 73 | } 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 Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 77 | } |
| 78 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 79 | sstr << "OPTION:" << endl |
| 80 | << " -I DIR, --include=DIR" << endl |
| 81 | << " Use DIR as a search path for import statements." << endl |
Jiyong Park | 3c35e39 | 2018-08-30 13:10:30 +0900 | [diff] [blame] | 82 | << " -m FILE, --import=FILE" << endl |
| 83 | << " Import FILE directly without searching in the search paths." << endl |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 84 | << " -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 Moreland | 6cee348 | 2018-07-18 14:39:58 -0700 | [diff] [blame] | 100 | << " --structured" << endl |
| 101 | << " Whether this interface is defined exclusively in AIDL." << endl |
| 102 | << " It is therefore a candidate for stabilization." << endl |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 103 | << " --stability=<level>" << endl |
| 104 | << " The stability requirement of this interface." << endl |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 105 | << " -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 Onea | 8714b02 | 2019-02-01 18:55:54 +0000 | [diff] [blame] | 111 | << " --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 Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 119 | << " -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 Trautrim | b77048c | 2020-01-21 16:39:32 +0900 | [diff] [blame] | 122 | << " --hash=HASH" << endl |
| 123 | << " Set the interface hash to HASH." << endl |
Jiyong Park | ce50e26 | 2018-10-29 09:54:20 +0900 | [diff] [blame] | 124 | << " --log" << endl |
| 125 | << " Information about the transaction, e.g., method name, argument" << endl |
| 126 | << " values, execution time, etc., is provided via callback." << endl |
Nikita Ioffe | 8954a0e | 2019-06-25 23:36:13 +0100 | [diff] [blame] | 127 | << " --parcelable-to-string" << endl |
| 128 | << " Generates an implementation of toString() for Java parcelables," << endl |
| 129 | << " and ostream& operator << for C++ parcelables." << endl |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 130 | << " --help" << endl |
| 131 | << " Show this help." << endl |
| 132 | << endl |
| 133 | << "INPUT:" << endl |
| 134 | << " An AIDL file." << endl |
| 135 | << endl |
| 136 | << "OUTPUT:" << endl |
| 137 | << " Path to the generated Java or C++ source file. This is ignored when" << endl |
| 138 | << " -o or --out is specified or the number of the input files are" << endl |
| 139 | << " more than one." << endl |
| 140 | << " For Java, if omitted, Java source file is generated at the same" << endl |
| 141 | << " place as the input AIDL file," << endl |
| 142 | << endl |
Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 143 | << "HEADER_DIR:" << endl |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 144 | << " Path to where C++ headers are generated." << endl; |
| 145 | return sstr.str(); |
Christopher Wiley | a590de8 | 2015-09-15 15:46:28 -0700 | [diff] [blame] | 146 | } |
| 147 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 148 | const string Options::LanguageToString(Language language) { |
| 149 | switch (language) { |
| 150 | case Options::Language::CPP: |
| 151 | return "cpp"; |
| 152 | case Options::Language::JAVA: |
| 153 | return "java"; |
| 154 | case Options::Language::NDK: |
| 155 | return "ndk"; |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 156 | case Options::Language::RUST: |
| 157 | return "rust"; |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 158 | case Options::Language::UNSPECIFIED: |
| 159 | return "unspecified"; |
| 160 | default: |
| 161 | AIDL_FATAL(AIDL_LOCATION_HERE) |
| 162 | << "Unexpected Options::Language enumerator: " << static_cast<size_t>(language); |
| 163 | } |
| 164 | } |
| 165 | |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 166 | bool Options::StabilityFromString(const std::string& stability, Stability* out_stability) { |
| 167 | if (stability == "vintf") { |
| 168 | *out_stability = Stability::VINTF; |
| 169 | return true; |
| 170 | } |
| 171 | return false; |
| 172 | } |
| 173 | |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 174 | Options Options::From(const string& cmdline) { |
| 175 | vector<string> args = Split(cmdline, " "); |
| 176 | return From(args); |
| 177 | } |
| 178 | |
| 179 | Options Options::From(const vector<string>& args) { |
| 180 | Options::Language lang = Options::Language::JAVA; |
| 181 | int argc = args.size(); |
| 182 | if (argc >= 1 && args.at(0) == "aidl-cpp") { |
| 183 | lang = Options::Language::CPP; |
| 184 | } |
| 185 | const char* argv[argc + 1]; |
| 186 | for (int i = 0; i < argc; i++) { |
| 187 | argv[i] = args.at(i).c_str(); |
| 188 | } |
| 189 | argv[argc] = nullptr; |
| 190 | |
| 191 | return Options(argc, argv, lang); |
| 192 | } |
| 193 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 194 | Options::Options(int argc, const char* const argv[], Options::Language default_lang) |
| 195 | : myname_(argv[0]), language_(default_lang) { |
| 196 | bool lang_option_found = false; |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 197 | optind = 0; |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 198 | while (true) { |
| 199 | static struct option long_options[] = { |
| 200 | {"lang", required_argument, 0, 'l'}, |
| 201 | {"preprocess", no_argument, 0, 's'}, |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 202 | #ifndef _WIN32 |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 203 | {"dumpapi", no_argument, 0, 'u'}, |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 204 | {"checkapi", no_argument, 0, 'A'}, |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 205 | #endif |
Andrei Onea | 8714b02 | 2019-02-01 18:55:54 +0000 | [diff] [blame] | 206 | {"apimapping", required_argument, 0, 'i'}, |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 207 | {"include", required_argument, 0, 'I'}, |
Jiyong Park | 3c35e39 | 2018-08-30 13:10:30 +0900 | [diff] [blame] | 208 | {"import", required_argument, 0, 'm'}, |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 209 | {"preprocessed", required_argument, 0, 'p'}, |
| 210 | {"dep", required_argument, 0, 'd'}, |
| 211 | {"out", required_argument, 0, 'o'}, |
| 212 | {"header_out", required_argument, 0, 'h'}, |
| 213 | {"ninja", no_argument, 0, 'n'}, |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 214 | {"stability", required_argument, 0, 'Y'}, |
Steven Moreland | 6cee348 | 2018-07-18 14:39:58 -0700 | [diff] [blame] | 215 | {"structured", no_argument, 0, 'S'}, |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 216 | {"trace", no_argument, 0, 't'}, |
| 217 | {"transaction_names", no_argument, 0, 'c'}, |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 218 | {"version", required_argument, 0, 'v'}, |
Jiyong Park | ce50e26 | 2018-10-29 09:54:20 +0900 | [diff] [blame] | 219 | {"log", no_argument, 0, 'L'}, |
Nikita Ioffe | 8954a0e | 2019-06-25 23:36:13 +0100 | [diff] [blame] | 220 | {"parcelable-to-string", no_argument, 0, 'P'}, |
Paul Trautrim | b77048c | 2020-01-21 16:39:32 +0900 | [diff] [blame] | 221 | {"hash", required_argument, 0, 'H'}, |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 222 | {"help", no_argument, 0, 'e'}, |
| 223 | {0, 0, 0, 0}, |
| 224 | }; |
Jiyong Park | 3c35e39 | 2018-08-30 13:10:30 +0900 | [diff] [blame] | 225 | const int c = getopt_long(argc, const_cast<char* const*>(argv), |
| 226 | "I:m:p:d:o:h:abtv:", long_options, nullptr); |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 227 | if (c == -1) { |
| 228 | // no more options |
| 229 | break; |
Christopher Wiley | a590de8 | 2015-09-15 15:46:28 -0700 | [diff] [blame] | 230 | } |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 231 | switch (c) { |
| 232 | case 'l': |
| 233 | if (language_ == Options::Language::CPP) { |
| 234 | // aidl-cpp can't set language. aidl-cpp exists only for backwards |
| 235 | // compatibility. |
| 236 | error_message_ << "aidl-cpp does not support --lang." << endl; |
| 237 | return; |
| 238 | } else { |
| 239 | lang_option_found = true; |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 240 | string lang = Trim(optarg); |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 241 | if (lang == "java") { |
| 242 | language_ = Options::Language::JAVA; |
| 243 | task_ = Options::Task::COMPILE; |
| 244 | } else if (lang == "cpp") { |
| 245 | language_ = Options::Language::CPP; |
| 246 | task_ = Options::Task::COMPILE; |
Steven Moreland | c26d814 | 2018-09-17 14:25:33 -0700 | [diff] [blame] | 247 | } else if (lang == "ndk") { |
| 248 | language_ = Options::Language::NDK; |
| 249 | task_ = Options::Task::COMPILE; |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 250 | } else if (lang == "rust") { |
| 251 | language_ = Options::Language::RUST; |
| 252 | task_ = Options::Task::COMPILE; |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 253 | } else { |
| 254 | error_message_ << "Unsupported language: '" << lang << "'" << endl; |
| 255 | return; |
| 256 | } |
| 257 | } |
| 258 | break; |
| 259 | case 's': |
| 260 | if (task_ != Options::Task::UNSPECIFIED) { |
| 261 | task_ = Options::Task::PREPROCESS; |
| 262 | } |
| 263 | break; |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 264 | #ifndef _WIN32 |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 265 | case 'u': |
| 266 | if (task_ != Options::Task::UNSPECIFIED) { |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 267 | task_ = Options::Task::DUMP_API; |
| 268 | } |
| 269 | break; |
| 270 | case 'A': |
| 271 | if (task_ != Options::Task::UNSPECIFIED) { |
| 272 | task_ = Options::Task::CHECK_API; |
| 273 | // to ensure that all parcelables in the api dumpes are structured |
| 274 | structured_ = true; |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 275 | } |
| 276 | break; |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 277 | #endif |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 278 | case 'I': { |
| 279 | import_dirs_.emplace(Trim(optarg)); |
Jiyong Park | 3c35e39 | 2018-08-30 13:10:30 +0900 | [diff] [blame] | 280 | break; |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 281 | } |
| 282 | case 'm': { |
| 283 | import_files_.emplace(Trim(optarg)); |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 284 | break; |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 285 | } |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 286 | case 'p': |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 287 | preprocessed_files_.emplace_back(Trim(optarg)); |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 288 | break; |
| 289 | case 'd': |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 290 | dependency_file_ = Trim(optarg); |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 291 | break; |
| 292 | case 'o': |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 293 | output_dir_ = Trim(optarg); |
Jiyong Park | 0546373 | 2018-08-09 16:03:02 +0900 | [diff] [blame] | 294 | if (output_dir_.back() != OS_PATH_SEPARATOR) { |
| 295 | output_dir_.push_back(OS_PATH_SEPARATOR); |
| 296 | } |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 297 | break; |
| 298 | case 'h': |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 299 | output_header_dir_ = Trim(optarg); |
Jiyong Park | 0546373 | 2018-08-09 16:03:02 +0900 | [diff] [blame] | 300 | if (output_header_dir_.back() != OS_PATH_SEPARATOR) { |
| 301 | output_header_dir_.push_back(OS_PATH_SEPARATOR); |
| 302 | } |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 303 | break; |
| 304 | case 'n': |
| 305 | dependency_file_ninja_ = true; |
| 306 | break; |
Steven Moreland | 6cee348 | 2018-07-18 14:39:58 -0700 | [diff] [blame] | 307 | case 'S': |
| 308 | structured_ = true; |
| 309 | break; |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 310 | case 'Y': { |
| 311 | const string stability_str = Trim(optarg); |
| 312 | if (!StabilityFromString(stability_str, &stability_)) { |
| 313 | error_message_ << "Unrecognized stability level: '" << stability_str |
| 314 | << "'. Must be vintf." << endl; |
| 315 | return; |
| 316 | } |
| 317 | break; |
| 318 | } |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 319 | case 't': |
| 320 | gen_traces_ = true; |
| 321 | break; |
| 322 | case 'a': |
| 323 | auto_dep_file_ = true; |
| 324 | break; |
| 325 | case 'b': |
| 326 | fail_on_parcelable_ = true; |
| 327 | break; |
| 328 | case 'c': |
| 329 | gen_transaction_names_ = true; |
| 330 | break; |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 331 | case 'v': { |
| 332 | const string ver_str = Trim(optarg); |
| 333 | int ver = atoi(ver_str.c_str()); |
| 334 | if (ver > 0) { |
| 335 | version_ = ver; |
| 336 | } else { |
| 337 | error_message_ << "Invalid version number: '" << ver_str << "'. " |
| 338 | << "Version must be a positive natural number." << endl; |
| 339 | return; |
| 340 | } |
| 341 | break; |
| 342 | } |
Paul Trautrim | b77048c | 2020-01-21 16:39:32 +0900 | [diff] [blame] | 343 | case 'H': |
| 344 | hash_ = Trim(optarg); |
| 345 | break; |
Jiyong Park | ce50e26 | 2018-10-29 09:54:20 +0900 | [diff] [blame] | 346 | case 'L': |
| 347 | gen_log_ = true; |
| 348 | break; |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 349 | case 'e': |
| 350 | std::cerr << GetUsage(); |
| 351 | exit(0); |
Andrei Onea | 8714b02 | 2019-02-01 18:55:54 +0000 | [diff] [blame] | 352 | case 'i': |
| 353 | output_file_ = Trim(optarg); |
| 354 | task_ = Task::DUMP_MAPPINGS; |
| 355 | break; |
Nikita Ioffe | 8954a0e | 2019-06-25 23:36:13 +0100 | [diff] [blame] | 356 | case 'P': |
| 357 | gen_parcelable_to_string_ = true; |
| 358 | break; |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 359 | default: |
Steven Moreland | 4dbadf5 | 2018-08-08 17:46:10 -0700 | [diff] [blame] | 360 | std::cerr << GetUsage(); |
| 361 | exit(1); |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 362 | } |
| 363 | } // while |
| 364 | |
| 365 | // Positional arguments |
| 366 | if (!lang_option_found && task_ == Options::Task::COMPILE) { |
| 367 | // the legacy arguments format |
| 368 | if (argc - optind <= 0) { |
| 369 | error_message_ << "No input file" << endl; |
| 370 | return; |
| 371 | } |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 372 | if (language_ == Options::Language::JAVA || language_ == Options::Language::RUST) { |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 373 | input_files_.emplace_back(argv[optind++]); |
| 374 | if (argc - optind >= 1) { |
| 375 | output_file_ = argv[optind++]; |
Jiyong Park | 56f73d7 | 2019-06-11 12:20:28 +0900 | [diff] [blame] | 376 | } else if (output_dir_.empty()) { |
| 377 | // when output is omitted and -o option isn't set, the output is by |
| 378 | // default set to the input file path with .aidl is replaced to .java. |
| 379 | // If -o option is set, the output path is calculated by |
| 380 | // generate_outputFileName which returns "<output_dir>/<package/name>/ |
| 381 | // <typename>.java" |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 382 | output_file_ = input_files_.front(); |
Steven Moreland | 4dbadf5 | 2018-08-08 17:46:10 -0700 | [diff] [blame] | 383 | if (android::base::EndsWith(output_file_, ".aidl")) { |
| 384 | output_file_ = output_file_.substr(0, output_file_.length() - strlen(".aidl")); |
| 385 | } |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 386 | output_file_ += (language_ == Options::Language::JAVA) ? ".java" : ".rs"; |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 387 | } |
Steven Moreland | c26d814 | 2018-09-17 14:25:33 -0700 | [diff] [blame] | 388 | } else if (IsCppOutput()) { |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 389 | input_files_.emplace_back(argv[optind++]); |
| 390 | if (argc - optind < 2) { |
| 391 | error_message_ << "No HEADER_DIR or OUTPUT." << endl; |
| 392 | return; |
| 393 | } |
| 394 | output_header_dir_ = argv[optind++]; |
Jiyong Park | 0546373 | 2018-08-09 16:03:02 +0900 | [diff] [blame] | 395 | if (output_header_dir_.back() != OS_PATH_SEPARATOR) { |
| 396 | output_header_dir_.push_back(OS_PATH_SEPARATOR); |
| 397 | } |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 398 | output_file_ = argv[optind++]; |
| 399 | } |
| 400 | if (argc - optind > 0) { |
| 401 | error_message_ << "Too many arguments: "; |
| 402 | for (int i = optind; i < argc; i++) { |
| 403 | error_message_ << " " << argv[i]; |
| 404 | } |
| 405 | error_message_ << endl; |
| 406 | } |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 407 | } else { |
| 408 | // the new arguments format |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 409 | if (task_ == Options::Task::COMPILE || task_ == Options::Task::DUMP_API) { |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 410 | if (argc - optind < 1) { |
| 411 | error_message_ << "No input file." << endl; |
| 412 | return; |
| 413 | } |
| 414 | } else { |
| 415 | if (argc - optind < 2) { |
| 416 | error_message_ << "Insufficient arguments. At least 2 required, but " |
| 417 | << "got " << (argc - optind) << "." << endl; |
| 418 | return; |
| 419 | } |
Andrei Onea | 8714b02 | 2019-02-01 18:55:54 +0000 | [diff] [blame] | 420 | if (task_ != Options::Task::CHECK_API && task_ != Options::Task::DUMP_MAPPINGS) { |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 421 | output_file_ = argv[optind++]; |
| 422 | } |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 423 | } |
| 424 | while (optind < argc) { |
| 425 | input_files_.emplace_back(argv[optind++]); |
| 426 | } |
Christopher Wiley | a590de8 | 2015-09-15 15:46:28 -0700 | [diff] [blame] | 427 | } |
| 428 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 429 | // filter out invalid combinations |
Steven Moreland | 4dbadf5 | 2018-08-08 17:46:10 -0700 | [diff] [blame] | 430 | if (lang_option_found) { |
Steven Moreland | c26d814 | 2018-09-17 14:25:33 -0700 | [diff] [blame] | 431 | if (IsCppOutput() && task_ == Options::Task::COMPILE) { |
Steven Moreland | 4dbadf5 | 2018-08-08 17:46:10 -0700 | [diff] [blame] | 432 | if (output_dir_.empty()) { |
| 433 | error_message_ << "Output directory is not set. Set with --out." << endl; |
| 434 | return; |
| 435 | } |
| 436 | if (output_header_dir_.empty()) { |
| 437 | error_message_ << "Header output directory is not set. Set with " |
| 438 | << "--header_out." << endl; |
| 439 | return; |
| 440 | } |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 441 | } |
Steven Moreland | 4dbadf5 | 2018-08-08 17:46:10 -0700 | [diff] [blame] | 442 | if (language_ == Options::Language::JAVA && task_ == Options::Task::COMPILE) { |
| 443 | if (output_dir_.empty()) { |
| 444 | error_message_ << "Output directory is not set. Set with --out." << endl; |
| 445 | return; |
| 446 | } |
| 447 | if (!output_header_dir_.empty()) { |
| 448 | error_message_ << "Header output directory is set, which does not make " |
| 449 | << "sense for Java." << endl; |
| 450 | return; |
| 451 | } |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 452 | } |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 453 | if (language_ == Options::Language::RUST && task_ == Options::Task::COMPILE) { |
| 454 | if (output_dir_.empty()) { |
| 455 | error_message_ << "Output directory is not set. Set with --out." << endl; |
| 456 | return; |
| 457 | } |
| 458 | if (!output_header_dir_.empty()) { |
| 459 | error_message_ << "Header output directory is set, which does not make " |
| 460 | << "sense for Rust." << endl; |
| 461 | return; |
| 462 | } |
| 463 | } |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 464 | } |
| 465 | if (task_ == Options::Task::COMPILE) { |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 466 | for (const string& input : input_files_) { |
| 467 | if (!android::base::EndsWith(input, ".aidl")) { |
| 468 | error_message_ << "Expected .aidl file for input but got '" << input << "'" << endl; |
| 469 | return; |
| 470 | } |
| 471 | } |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 472 | if (!output_file_.empty() && input_files_.size() > 1) { |
| 473 | error_message_ << "Multiple AIDL files can't be compiled to a single " |
| 474 | << "output file '" << output_file_ << "'. " |
| 475 | << "Use --out=DIR instead for output files." << endl; |
| 476 | return; |
| 477 | } |
| 478 | if (!dependency_file_.empty() && input_files_.size() > 1) { |
| 479 | error_message_ << "-d or --dep doesn't work when compiling multiple AIDL " |
| 480 | << "files. Use '-a' to generate dependency file next to " |
| 481 | << "the output file with the name based on the input " |
| 482 | << "file." << endl; |
| 483 | return; |
| 484 | } |
Jeongik Cha | 37e2ad5 | 2019-04-18 13:44:26 +0900 | [diff] [blame] | 485 | if (gen_log_ && (language_ != Options::Language::CPP && language_ != Options::Language::NDK)) { |
| 486 | error_message_ << "--log is currently supported for either --lang=cpp or --lang=ndk" << endl; |
Jiyong Park | ce50e26 | 2018-10-29 09:54:20 +0900 | [diff] [blame] | 487 | return; |
| 488 | } |
Christopher Wiley | a590de8 | 2015-09-15 15:46:28 -0700 | [diff] [blame] | 489 | } |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 490 | if (task_ == Options::Task::PREPROCESS) { |
| 491 | if (version_ > 0) { |
| 492 | error_message_ << "--version should not be used with '--preprocess'." << endl; |
| 493 | return; |
| 494 | } |
| 495 | } |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 496 | if (task_ == Options::Task::CHECK_API) { |
| 497 | if (input_files_.size() != 2) { |
| 498 | error_message_ << "--checkapi requires two inputs for comparing, " |
| 499 | << "but got " << input_files_.size() << "." << endl; |
| 500 | return; |
| 501 | } |
| 502 | } |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 503 | if (task_ == Options::Task::DUMP_API) { |
| 504 | if (output_dir_.empty()) { |
Jooyung Han | 4bb2a15 | 2020-10-12 18:11:41 +0900 | [diff] [blame^] | 505 | error_message_ << "--dumpapi requires output directory. Use --out." << endl; |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 506 | return; |
| 507 | } |
| 508 | } |
Jiyong Park | 0546373 | 2018-08-09 16:03:02 +0900 | [diff] [blame] | 509 | |
Steven Moreland | 2178081 | 2020-09-11 01:29:45 +0000 | [diff] [blame] | 510 | AIDL_FATAL_IF(!output_dir_.empty() && output_dir_.back() != OS_PATH_SEPARATOR, output_dir_); |
| 511 | AIDL_FATAL_IF(!output_header_dir_.empty() && output_header_dir_.back() != OS_PATH_SEPARATOR, |
| 512 | output_header_dir_); |
Christopher Wiley | a590de8 | 2015-09-15 15:46:28 -0700 | [diff] [blame] | 513 | } |
| 514 | |
Christopher Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 515 | } // namespace aidl |
Steven Moreland | f4c64df | 2019-07-29 19:54:04 -0700 | [diff] [blame] | 516 | } // namespace android |