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 |
Steven Moreland | baa7064 | 2019-10-25 16:33:10 -0700 | [diff] [blame] | 48 | << myname_ << " --lang={java|cpp|ndk} [OPTION]... INPUT..." << endl |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 49 | << " Generate Java or C++ 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; |
Christopher Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 73 | } |
| 74 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 75 | sstr << "OPTION:" << endl |
| 76 | << " -I DIR, --include=DIR" << endl |
| 77 | << " Use DIR as a search path for import statements." << endl |
Jiyong Park | 3c35e39 | 2018-08-30 13:10:30 +0900 | [diff] [blame] | 78 | << " -m FILE, --import=FILE" << endl |
| 79 | << " Import FILE directly without searching in the search paths." << endl |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 80 | << " -p FILE, --preprocessed=FILE" << endl |
| 81 | << " Include FILE which is created by --preprocess." << endl |
| 82 | << " -d FILE, --dep=FILE" << endl |
| 83 | << " Generate dependency file as FILE. Don't use this when" << endl |
| 84 | << " there are multiple input files. Use -a then." << endl |
| 85 | << " -o DIR, --out=DIR" << endl |
| 86 | << " Use DIR as the base output directory for generated files." << endl |
| 87 | << " -h DIR, --header_out=DIR" << endl |
| 88 | << " Generate C++ headers under DIR." << endl |
| 89 | << " -a" << endl |
| 90 | << " Generate dependency file next to the output file with the" << endl |
| 91 | << " name based on the input file." << endl |
| 92 | << " -b" << endl |
| 93 | << " Trigger fail when trying to compile a parcelable." << endl |
| 94 | << " --ninja" << endl |
| 95 | << " Generate dependency file in a format ninja understands." << endl |
Steven Moreland | 6cee348 | 2018-07-18 14:39:58 -0700 | [diff] [blame] | 96 | << " --structured" << endl |
| 97 | << " Whether this interface is defined exclusively in AIDL." << endl |
| 98 | << " It is therefore a candidate for stabilization." << endl |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 99 | << " --stability=<level>" << endl |
| 100 | << " The stability requirement of this interface." << endl |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 101 | << " -t, --trace" << endl |
| 102 | << " Include tracing code for systrace. Note that if either" << endl |
| 103 | << " the client or service code is not auto-generated by this" << endl |
| 104 | << " tool, that part will not be traced." << endl |
| 105 | << " --transaction_names" << endl |
| 106 | << " Generate transaction names." << endl |
Andrei Onea | 8714b02 | 2019-02-01 18:55:54 +0000 | [diff] [blame] | 107 | << " --apimapping" << endl |
| 108 | << " Generates a mapping of declared aidl method signatures to" << endl |
| 109 | << " the original line number. e.g.: " << endl |
| 110 | << " If line 39 of foo/bar/IFoo.aidl contains:" |
| 111 | << " void doFoo(int bar, String baz);" << endl |
| 112 | << " Then the result would be:" << endl |
| 113 | << " foo.bar.Baz|doFoo|int,String,|void" << endl |
| 114 | << " foo/bar/IFoo.aidl:39" << endl |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 115 | << " -v VER, --version=VER" << endl |
| 116 | << " Set the version of the interface and parcelable to VER." << endl |
| 117 | << " VER must be an interger greater than 0." << endl |
Paul Trautrim | b77048c | 2020-01-21 16:39:32 +0900 | [diff] [blame] | 118 | << " --hash=HASH" << endl |
| 119 | << " Set the interface hash to HASH." << endl |
Jiyong Park | ce50e26 | 2018-10-29 09:54:20 +0900 | [diff] [blame] | 120 | << " --log" << endl |
| 121 | << " Information about the transaction, e.g., method name, argument" << endl |
| 122 | << " values, execution time, etc., is provided via callback." << endl |
Nikita Ioffe | 8954a0e | 2019-06-25 23:36:13 +0100 | [diff] [blame] | 123 | << " --parcelable-to-string" << endl |
| 124 | << " Generates an implementation of toString() for Java parcelables," << endl |
| 125 | << " and ostream& operator << for C++ parcelables." << endl |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 126 | << " --help" << endl |
| 127 | << " Show this help." << endl |
| 128 | << endl |
| 129 | << "INPUT:" << endl |
| 130 | << " An AIDL file." << endl |
| 131 | << endl |
| 132 | << "OUTPUT:" << endl |
| 133 | << " Path to the generated Java or C++ source file. This is ignored when" << endl |
| 134 | << " -o or --out is specified or the number of the input files are" << endl |
| 135 | << " more than one." << endl |
| 136 | << " For Java, if omitted, Java source file is generated at the same" << endl |
| 137 | << " place as the input AIDL file," << endl |
| 138 | << endl |
Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 139 | << "HEADER_DIR:" << endl |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 140 | << " Path to where C++ headers are generated." << endl; |
| 141 | return sstr.str(); |
Christopher Wiley | a590de8 | 2015-09-15 15:46:28 -0700 | [diff] [blame] | 142 | } |
| 143 | |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 144 | const string Options::LanguageToString(Language language) { |
| 145 | switch (language) { |
| 146 | case Options::Language::CPP: |
| 147 | return "cpp"; |
| 148 | case Options::Language::JAVA: |
| 149 | return "java"; |
| 150 | case Options::Language::NDK: |
| 151 | return "ndk"; |
| 152 | case Options::Language::UNSPECIFIED: |
| 153 | return "unspecified"; |
| 154 | default: |
| 155 | AIDL_FATAL(AIDL_LOCATION_HERE) |
| 156 | << "Unexpected Options::Language enumerator: " << static_cast<size_t>(language); |
| 157 | } |
| 158 | } |
| 159 | |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 160 | bool Options::StabilityFromString(const std::string& stability, Stability* out_stability) { |
| 161 | if (stability == "vintf") { |
| 162 | *out_stability = Stability::VINTF; |
| 163 | return true; |
| 164 | } |
| 165 | return false; |
| 166 | } |
| 167 | |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 168 | Options Options::From(const string& cmdline) { |
| 169 | vector<string> args = Split(cmdline, " "); |
| 170 | return From(args); |
| 171 | } |
| 172 | |
| 173 | Options Options::From(const vector<string>& args) { |
| 174 | Options::Language lang = Options::Language::JAVA; |
| 175 | int argc = args.size(); |
| 176 | if (argc >= 1 && args.at(0) == "aidl-cpp") { |
| 177 | lang = Options::Language::CPP; |
| 178 | } |
| 179 | const char* argv[argc + 1]; |
| 180 | for (int i = 0; i < argc; i++) { |
| 181 | argv[i] = args.at(i).c_str(); |
| 182 | } |
| 183 | argv[argc] = nullptr; |
| 184 | |
| 185 | return Options(argc, argv, lang); |
| 186 | } |
| 187 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 188 | Options::Options(int argc, const char* const argv[], Options::Language default_lang) |
| 189 | : myname_(argv[0]), language_(default_lang) { |
| 190 | bool lang_option_found = false; |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 191 | optind = 0; |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 192 | while (true) { |
| 193 | static struct option long_options[] = { |
| 194 | {"lang", required_argument, 0, 'l'}, |
| 195 | {"preprocess", no_argument, 0, 's'}, |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 196 | #ifndef _WIN32 |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 197 | {"dumpapi", no_argument, 0, 'u'}, |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 198 | {"checkapi", no_argument, 0, 'A'}, |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 199 | #endif |
Andrei Onea | 8714b02 | 2019-02-01 18:55:54 +0000 | [diff] [blame] | 200 | {"apimapping", required_argument, 0, 'i'}, |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 201 | {"include", required_argument, 0, 'I'}, |
Jiyong Park | 3c35e39 | 2018-08-30 13:10:30 +0900 | [diff] [blame] | 202 | {"import", required_argument, 0, 'm'}, |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 203 | {"preprocessed", required_argument, 0, 'p'}, |
| 204 | {"dep", required_argument, 0, 'd'}, |
| 205 | {"out", required_argument, 0, 'o'}, |
| 206 | {"header_out", required_argument, 0, 'h'}, |
| 207 | {"ninja", no_argument, 0, 'n'}, |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 208 | {"stability", required_argument, 0, 'Y'}, |
Steven Moreland | 6cee348 | 2018-07-18 14:39:58 -0700 | [diff] [blame] | 209 | {"structured", no_argument, 0, 'S'}, |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 210 | {"trace", no_argument, 0, 't'}, |
| 211 | {"transaction_names", no_argument, 0, 'c'}, |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 212 | {"version", required_argument, 0, 'v'}, |
Jiyong Park | ce50e26 | 2018-10-29 09:54:20 +0900 | [diff] [blame] | 213 | {"log", no_argument, 0, 'L'}, |
Nikita Ioffe | 8954a0e | 2019-06-25 23:36:13 +0100 | [diff] [blame] | 214 | {"parcelable-to-string", no_argument, 0, 'P'}, |
Paul Trautrim | b77048c | 2020-01-21 16:39:32 +0900 | [diff] [blame] | 215 | {"hash", required_argument, 0, 'H'}, |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 216 | {"help", no_argument, 0, 'e'}, |
| 217 | {0, 0, 0, 0}, |
| 218 | }; |
Jiyong Park | 3c35e39 | 2018-08-30 13:10:30 +0900 | [diff] [blame] | 219 | const int c = getopt_long(argc, const_cast<char* const*>(argv), |
| 220 | "I:m:p:d:o:h:abtv:", long_options, nullptr); |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 221 | if (c == -1) { |
| 222 | // no more options |
| 223 | break; |
Christopher Wiley | a590de8 | 2015-09-15 15:46:28 -0700 | [diff] [blame] | 224 | } |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 225 | switch (c) { |
| 226 | case 'l': |
| 227 | if (language_ == Options::Language::CPP) { |
| 228 | // aidl-cpp can't set language. aidl-cpp exists only for backwards |
| 229 | // compatibility. |
| 230 | error_message_ << "aidl-cpp does not support --lang." << endl; |
| 231 | return; |
| 232 | } else { |
| 233 | lang_option_found = true; |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 234 | string lang = Trim(optarg); |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 235 | if (lang == "java") { |
| 236 | language_ = Options::Language::JAVA; |
| 237 | task_ = Options::Task::COMPILE; |
| 238 | } else if (lang == "cpp") { |
| 239 | language_ = Options::Language::CPP; |
| 240 | task_ = Options::Task::COMPILE; |
Steven Moreland | c26d814 | 2018-09-17 14:25:33 -0700 | [diff] [blame] | 241 | } else if (lang == "ndk") { |
| 242 | language_ = Options::Language::NDK; |
| 243 | task_ = Options::Task::COMPILE; |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 244 | } else { |
| 245 | error_message_ << "Unsupported language: '" << lang << "'" << endl; |
| 246 | return; |
| 247 | } |
| 248 | } |
| 249 | break; |
| 250 | case 's': |
| 251 | if (task_ != Options::Task::UNSPECIFIED) { |
| 252 | task_ = Options::Task::PREPROCESS; |
| 253 | } |
| 254 | break; |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 255 | #ifndef _WIN32 |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 256 | case 'u': |
| 257 | if (task_ != Options::Task::UNSPECIFIED) { |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 258 | task_ = Options::Task::DUMP_API; |
| 259 | } |
| 260 | break; |
| 261 | case 'A': |
| 262 | if (task_ != Options::Task::UNSPECIFIED) { |
| 263 | task_ = Options::Task::CHECK_API; |
| 264 | // to ensure that all parcelables in the api dumpes are structured |
| 265 | structured_ = true; |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 266 | } |
| 267 | break; |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 268 | #endif |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 269 | case 'I': { |
| 270 | import_dirs_.emplace(Trim(optarg)); |
Jiyong Park | 3c35e39 | 2018-08-30 13:10:30 +0900 | [diff] [blame] | 271 | break; |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 272 | } |
| 273 | case 'm': { |
| 274 | import_files_.emplace(Trim(optarg)); |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 275 | break; |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 276 | } |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 277 | case 'p': |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 278 | preprocessed_files_.emplace_back(Trim(optarg)); |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 279 | break; |
| 280 | case 'd': |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 281 | dependency_file_ = Trim(optarg); |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 282 | break; |
| 283 | case 'o': |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 284 | output_dir_ = Trim(optarg); |
Jiyong Park | 0546373 | 2018-08-09 16:03:02 +0900 | [diff] [blame] | 285 | if (output_dir_.back() != OS_PATH_SEPARATOR) { |
| 286 | output_dir_.push_back(OS_PATH_SEPARATOR); |
| 287 | } |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 288 | break; |
| 289 | case 'h': |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 290 | output_header_dir_ = Trim(optarg); |
Jiyong Park | 0546373 | 2018-08-09 16:03:02 +0900 | [diff] [blame] | 291 | if (output_header_dir_.back() != OS_PATH_SEPARATOR) { |
| 292 | output_header_dir_.push_back(OS_PATH_SEPARATOR); |
| 293 | } |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 294 | break; |
| 295 | case 'n': |
| 296 | dependency_file_ninja_ = true; |
| 297 | break; |
Steven Moreland | 6cee348 | 2018-07-18 14:39:58 -0700 | [diff] [blame] | 298 | case 'S': |
| 299 | structured_ = true; |
| 300 | break; |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 301 | case 'Y': { |
| 302 | const string stability_str = Trim(optarg); |
| 303 | if (!StabilityFromString(stability_str, &stability_)) { |
| 304 | error_message_ << "Unrecognized stability level: '" << stability_str |
| 305 | << "'. Must be vintf." << endl; |
| 306 | return; |
| 307 | } |
| 308 | break; |
| 309 | } |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 310 | case 't': |
| 311 | gen_traces_ = true; |
| 312 | break; |
| 313 | case 'a': |
| 314 | auto_dep_file_ = true; |
| 315 | break; |
| 316 | case 'b': |
| 317 | fail_on_parcelable_ = true; |
| 318 | break; |
| 319 | case 'c': |
| 320 | gen_transaction_names_ = true; |
| 321 | break; |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 322 | case 'v': { |
| 323 | const string ver_str = Trim(optarg); |
| 324 | int ver = atoi(ver_str.c_str()); |
| 325 | if (ver > 0) { |
| 326 | version_ = ver; |
| 327 | } else { |
| 328 | error_message_ << "Invalid version number: '" << ver_str << "'. " |
| 329 | << "Version must be a positive natural number." << endl; |
| 330 | return; |
| 331 | } |
| 332 | break; |
| 333 | } |
Paul Trautrim | b77048c | 2020-01-21 16:39:32 +0900 | [diff] [blame] | 334 | case 'H': |
| 335 | hash_ = Trim(optarg); |
| 336 | break; |
Jiyong Park | ce50e26 | 2018-10-29 09:54:20 +0900 | [diff] [blame] | 337 | case 'L': |
| 338 | gen_log_ = true; |
| 339 | break; |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 340 | case 'e': |
| 341 | std::cerr << GetUsage(); |
| 342 | exit(0); |
Andrei Onea | 8714b02 | 2019-02-01 18:55:54 +0000 | [diff] [blame] | 343 | case 'i': |
| 344 | output_file_ = Trim(optarg); |
| 345 | task_ = Task::DUMP_MAPPINGS; |
| 346 | break; |
Nikita Ioffe | 8954a0e | 2019-06-25 23:36:13 +0100 | [diff] [blame] | 347 | case 'P': |
| 348 | gen_parcelable_to_string_ = true; |
| 349 | break; |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 350 | default: |
Steven Moreland | 4dbadf5 | 2018-08-08 17:46:10 -0700 | [diff] [blame] | 351 | std::cerr << GetUsage(); |
| 352 | exit(1); |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 353 | } |
| 354 | } // while |
| 355 | |
| 356 | // Positional arguments |
| 357 | if (!lang_option_found && task_ == Options::Task::COMPILE) { |
| 358 | // the legacy arguments format |
| 359 | if (argc - optind <= 0) { |
| 360 | error_message_ << "No input file" << endl; |
| 361 | return; |
| 362 | } |
| 363 | if (language_ == Options::Language::JAVA) { |
| 364 | input_files_.emplace_back(argv[optind++]); |
| 365 | if (argc - optind >= 1) { |
| 366 | output_file_ = argv[optind++]; |
Jiyong Park | 56f73d7 | 2019-06-11 12:20:28 +0900 | [diff] [blame] | 367 | } else if (output_dir_.empty()) { |
| 368 | // when output is omitted and -o option isn't set, the output is by |
| 369 | // default set to the input file path with .aidl is replaced to .java. |
| 370 | // If -o option is set, the output path is calculated by |
| 371 | // generate_outputFileName which returns "<output_dir>/<package/name>/ |
| 372 | // <typename>.java" |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 373 | output_file_ = input_files_.front(); |
Steven Moreland | 4dbadf5 | 2018-08-08 17:46:10 -0700 | [diff] [blame] | 374 | if (android::base::EndsWith(output_file_, ".aidl")) { |
| 375 | output_file_ = output_file_.substr(0, output_file_.length() - strlen(".aidl")); |
| 376 | } |
| 377 | output_file_ += ".java"; |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 378 | } |
Steven Moreland | c26d814 | 2018-09-17 14:25:33 -0700 | [diff] [blame] | 379 | } else if (IsCppOutput()) { |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 380 | input_files_.emplace_back(argv[optind++]); |
| 381 | if (argc - optind < 2) { |
| 382 | error_message_ << "No HEADER_DIR or OUTPUT." << endl; |
| 383 | return; |
| 384 | } |
| 385 | output_header_dir_ = argv[optind++]; |
Jiyong Park | 0546373 | 2018-08-09 16:03:02 +0900 | [diff] [blame] | 386 | if (output_header_dir_.back() != OS_PATH_SEPARATOR) { |
| 387 | output_header_dir_.push_back(OS_PATH_SEPARATOR); |
| 388 | } |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 389 | output_file_ = argv[optind++]; |
| 390 | } |
| 391 | if (argc - optind > 0) { |
| 392 | error_message_ << "Too many arguments: "; |
| 393 | for (int i = optind; i < argc; i++) { |
| 394 | error_message_ << " " << argv[i]; |
| 395 | } |
| 396 | error_message_ << endl; |
| 397 | } |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 398 | } else { |
| 399 | // the new arguments format |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 400 | if (task_ == Options::Task::COMPILE || task_ == Options::Task::DUMP_API) { |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 401 | if (argc - optind < 1) { |
| 402 | error_message_ << "No input file." << endl; |
| 403 | return; |
| 404 | } |
| 405 | } else { |
| 406 | if (argc - optind < 2) { |
| 407 | error_message_ << "Insufficient arguments. At least 2 required, but " |
| 408 | << "got " << (argc - optind) << "." << endl; |
| 409 | return; |
| 410 | } |
Andrei Onea | 8714b02 | 2019-02-01 18:55:54 +0000 | [diff] [blame] | 411 | if (task_ != Options::Task::CHECK_API && task_ != Options::Task::DUMP_MAPPINGS) { |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 412 | output_file_ = argv[optind++]; |
| 413 | } |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 414 | } |
| 415 | while (optind < argc) { |
| 416 | input_files_.emplace_back(argv[optind++]); |
| 417 | } |
Christopher Wiley | a590de8 | 2015-09-15 15:46:28 -0700 | [diff] [blame] | 418 | } |
| 419 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 420 | // filter out invalid combinations |
Steven Moreland | 4dbadf5 | 2018-08-08 17:46:10 -0700 | [diff] [blame] | 421 | if (lang_option_found) { |
Steven Moreland | c26d814 | 2018-09-17 14:25:33 -0700 | [diff] [blame] | 422 | if (IsCppOutput() && task_ == Options::Task::COMPILE) { |
Steven Moreland | 4dbadf5 | 2018-08-08 17:46:10 -0700 | [diff] [blame] | 423 | if (output_dir_.empty()) { |
| 424 | error_message_ << "Output directory is not set. Set with --out." << endl; |
| 425 | return; |
| 426 | } |
| 427 | if (output_header_dir_.empty()) { |
| 428 | error_message_ << "Header output directory is not set. Set with " |
| 429 | << "--header_out." << endl; |
| 430 | return; |
| 431 | } |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 432 | } |
Steven Moreland | 4dbadf5 | 2018-08-08 17:46:10 -0700 | [diff] [blame] | 433 | if (language_ == Options::Language::JAVA && task_ == Options::Task::COMPILE) { |
| 434 | if (output_dir_.empty()) { |
| 435 | error_message_ << "Output directory is not set. Set with --out." << endl; |
| 436 | return; |
| 437 | } |
| 438 | if (!output_header_dir_.empty()) { |
| 439 | error_message_ << "Header output directory is set, which does not make " |
| 440 | << "sense for Java." << endl; |
| 441 | return; |
| 442 | } |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 443 | } |
| 444 | } |
| 445 | if (task_ == Options::Task::COMPILE) { |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 446 | for (const string& input : input_files_) { |
| 447 | if (!android::base::EndsWith(input, ".aidl")) { |
| 448 | error_message_ << "Expected .aidl file for input but got '" << input << "'" << endl; |
| 449 | return; |
| 450 | } |
| 451 | } |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 452 | if (!output_file_.empty() && input_files_.size() > 1) { |
| 453 | error_message_ << "Multiple AIDL files can't be compiled to a single " |
| 454 | << "output file '" << output_file_ << "'. " |
| 455 | << "Use --out=DIR instead for output files." << endl; |
| 456 | return; |
| 457 | } |
| 458 | if (!dependency_file_.empty() && input_files_.size() > 1) { |
| 459 | error_message_ << "-d or --dep doesn't work when compiling multiple AIDL " |
| 460 | << "files. Use '-a' to generate dependency file next to " |
| 461 | << "the output file with the name based on the input " |
| 462 | << "file." << endl; |
| 463 | return; |
| 464 | } |
Jeongik Cha | 37e2ad5 | 2019-04-18 13:44:26 +0900 | [diff] [blame] | 465 | if (gen_log_ && (language_ != Options::Language::CPP && language_ != Options::Language::NDK)) { |
| 466 | 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] | 467 | return; |
| 468 | } |
Christopher Wiley | a590de8 | 2015-09-15 15:46:28 -0700 | [diff] [blame] | 469 | } |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 470 | if (task_ == Options::Task::PREPROCESS) { |
| 471 | if (version_ > 0) { |
| 472 | error_message_ << "--version should not be used with '--preprocess'." << endl; |
| 473 | return; |
| 474 | } |
| 475 | } |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 476 | if (task_ == Options::Task::CHECK_API) { |
| 477 | if (input_files_.size() != 2) { |
| 478 | error_message_ << "--checkapi requires two inputs for comparing, " |
| 479 | << "but got " << input_files_.size() << "." << endl; |
| 480 | return; |
| 481 | } |
| 482 | } |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 483 | if (task_ == Options::Task::DUMP_API) { |
| 484 | if (output_dir_.empty()) { |
| 485 | error_message_ << "--dump_api requires output directory. Use --out." << endl; |
| 486 | return; |
| 487 | } |
| 488 | } |
Jiyong Park | 0546373 | 2018-08-09 16:03:02 +0900 | [diff] [blame] | 489 | |
| 490 | CHECK(output_dir_.empty() || output_dir_.back() == OS_PATH_SEPARATOR); |
| 491 | CHECK(output_header_dir_.empty() || output_header_dir_.back() == OS_PATH_SEPARATOR); |
Christopher Wiley | a590de8 | 2015-09-15 15:46:28 -0700 | [diff] [blame] | 492 | } |
| 493 | |
Christopher Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 494 | } // namespace aidl |
Steven Moreland | f4c64df | 2019-07-29 19:54:04 -0700 | [diff] [blame] | 495 | } // namespace android |