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