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 |
Jooyung Han | b8a9777 | 2021-01-19 01:27:38 +0900 | [diff] [blame] | 58 | << myname_ << " --checkapi[={compatible|equal}] OLD_DIR NEW_DIR" << endl |
| 59 | << " Check whether NEW_DIR API dump is {compatible|equal} extension " << endl |
| 60 | << " of the API dump OLD_DIR. Default: compatible" << endl |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 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 |
Jooyung Han | 888c5bc | 2020-12-22 17:28:47 +0900 | [diff] [blame] | 127 | << " -Werror" << endl |
| 128 | << " Turn warnings into errors." << endl |
| 129 | << " -Wno-error=<warning>" << endl |
| 130 | << " Turn the specified warning into a warning even if -Werror is specified." |
| 131 | << endl |
| 132 | << " -W<warning>" << endl |
| 133 | << " Enable the specified warning." << endl |
| 134 | << " -Wno-<warning>" << endl |
| 135 | << " Disable the specified warning." << endl |
| 136 | << " -w" << endl |
| 137 | << " Disable all diagnostics. -w wins -Weverything" << endl |
| 138 | << " -Weverything" << endl |
| 139 | << " Enable all diagnostics." << endl |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 140 | << " --help" << endl |
| 141 | << " Show this help." << endl |
| 142 | << endl |
| 143 | << "INPUT:" << endl |
| 144 | << " An AIDL file." << endl |
| 145 | << endl |
| 146 | << "OUTPUT:" << endl |
| 147 | << " Path to the generated Java or C++ source file. This is ignored when" << endl |
| 148 | << " -o or --out is specified or the number of the input files are" << endl |
| 149 | << " more than one." << endl |
| 150 | << " For Java, if omitted, Java source file is generated at the same" << endl |
| 151 | << " place as the input AIDL file," << endl |
| 152 | << endl |
Christopher Wiley | 054afbd | 2015-10-16 17:08:43 -0700 | [diff] [blame] | 153 | << "HEADER_DIR:" << endl |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 154 | << " Path to where C++ headers are generated." << endl; |
| 155 | return sstr.str(); |
Christopher Wiley | a590de8 | 2015-09-15 15:46:28 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Jooyung Han | 9435e9a | 2021-01-06 10:16:31 +0900 | [diff] [blame] | 158 | string to_string(Options::Language language) { |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 159 | switch (language) { |
| 160 | case Options::Language::CPP: |
| 161 | return "cpp"; |
| 162 | case Options::Language::JAVA: |
| 163 | return "java"; |
| 164 | case Options::Language::NDK: |
| 165 | return "ndk"; |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 166 | case Options::Language::RUST: |
| 167 | return "rust"; |
Devin Moore | 7b8d5c9 | 2020-03-17 14:14:08 -0700 | [diff] [blame] | 168 | case Options::Language::UNSPECIFIED: |
| 169 | return "unspecified"; |
| 170 | default: |
| 171 | AIDL_FATAL(AIDL_LOCATION_HERE) |
| 172 | << "Unexpected Options::Language enumerator: " << static_cast<size_t>(language); |
| 173 | } |
| 174 | } |
| 175 | |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 176 | bool Options::StabilityFromString(const std::string& stability, Stability* out_stability) { |
| 177 | if (stability == "vintf") { |
| 178 | *out_stability = Stability::VINTF; |
| 179 | return true; |
| 180 | } |
| 181 | return false; |
| 182 | } |
| 183 | |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 184 | Options Options::From(const string& cmdline) { |
| 185 | vector<string> args = Split(cmdline, " "); |
| 186 | return From(args); |
| 187 | } |
| 188 | |
| 189 | Options Options::From(const vector<string>& args) { |
| 190 | Options::Language lang = Options::Language::JAVA; |
| 191 | int argc = args.size(); |
| 192 | if (argc >= 1 && args.at(0) == "aidl-cpp") { |
| 193 | lang = Options::Language::CPP; |
| 194 | } |
| 195 | const char* argv[argc + 1]; |
| 196 | for (int i = 0; i < argc; i++) { |
| 197 | argv[i] = args.at(i).c_str(); |
| 198 | } |
| 199 | argv[argc] = nullptr; |
| 200 | |
| 201 | return Options(argc, argv, lang); |
| 202 | } |
| 203 | |
Jooyung Han | 888c5bc | 2020-12-22 17:28:47 +0900 | [diff] [blame] | 204 | Options::Options(int argc, const char* const raw_argv[], Options::Language default_lang) |
| 205 | : myname_(raw_argv[0]), language_(default_lang) { |
| 206 | std::vector<const char*> argv = warning_options_.Parse(argc, raw_argv, error_message_); |
| 207 | if (!Ok()) return; |
| 208 | argc = argv.size(); |
| 209 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 210 | bool lang_option_found = false; |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 211 | optind = 0; |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 212 | while (true) { |
| 213 | static struct option long_options[] = { |
| 214 | {"lang", required_argument, 0, 'l'}, |
| 215 | {"preprocess", no_argument, 0, 's'}, |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 216 | #ifndef _WIN32 |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 217 | {"dumpapi", no_argument, 0, 'u'}, |
Jooyung Han | 252657e | 2021-02-27 02:51:39 +0900 | [diff] [blame^] | 218 | {"no_license", no_argument, 0, 'x'}, |
Jooyung Han | b8a9777 | 2021-01-19 01:27:38 +0900 | [diff] [blame] | 219 | {"checkapi", optional_argument, 0, 'A'}, |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 220 | #endif |
Andrei Onea | 8714b02 | 2019-02-01 18:55:54 +0000 | [diff] [blame] | 221 | {"apimapping", required_argument, 0, 'i'}, |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 222 | {"include", required_argument, 0, 'I'}, |
Jiyong Park | 3c35e39 | 2018-08-30 13:10:30 +0900 | [diff] [blame] | 223 | {"import", required_argument, 0, 'm'}, |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 224 | {"preprocessed", required_argument, 0, 'p'}, |
| 225 | {"dep", required_argument, 0, 'd'}, |
| 226 | {"out", required_argument, 0, 'o'}, |
| 227 | {"header_out", required_argument, 0, 'h'}, |
| 228 | {"ninja", no_argument, 0, 'n'}, |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 229 | {"stability", required_argument, 0, 'Y'}, |
Steven Moreland | 6cee348 | 2018-07-18 14:39:58 -0700 | [diff] [blame] | 230 | {"structured", no_argument, 0, 'S'}, |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 231 | {"trace", no_argument, 0, 't'}, |
| 232 | {"transaction_names", no_argument, 0, 'c'}, |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 233 | {"version", required_argument, 0, 'v'}, |
Jiyong Park | ce50e26 | 2018-10-29 09:54:20 +0900 | [diff] [blame] | 234 | {"log", no_argument, 0, 'L'}, |
Paul Trautrim | b77048c | 2020-01-21 16:39:32 +0900 | [diff] [blame] | 235 | {"hash", required_argument, 0, 'H'}, |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 236 | {"help", no_argument, 0, 'e'}, |
| 237 | {0, 0, 0, 0}, |
| 238 | }; |
Jooyung Han | 888c5bc | 2020-12-22 17:28:47 +0900 | [diff] [blame] | 239 | const int c = getopt_long(argc, const_cast<char* const*>(argv.data()), |
Jiyong Park | 3c35e39 | 2018-08-30 13:10:30 +0900 | [diff] [blame] | 240 | "I:m:p:d:o:h:abtv:", long_options, nullptr); |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 241 | if (c == -1) { |
| 242 | // no more options |
| 243 | break; |
Christopher Wiley | a590de8 | 2015-09-15 15:46:28 -0700 | [diff] [blame] | 244 | } |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 245 | switch (c) { |
| 246 | case 'l': |
| 247 | if (language_ == Options::Language::CPP) { |
| 248 | // aidl-cpp can't set language. aidl-cpp exists only for backwards |
| 249 | // compatibility. |
| 250 | error_message_ << "aidl-cpp does not support --lang." << endl; |
| 251 | return; |
| 252 | } else { |
| 253 | lang_option_found = true; |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 254 | string lang = Trim(optarg); |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 255 | if (lang == "java") { |
| 256 | language_ = Options::Language::JAVA; |
| 257 | task_ = Options::Task::COMPILE; |
| 258 | } else if (lang == "cpp") { |
| 259 | language_ = Options::Language::CPP; |
| 260 | task_ = Options::Task::COMPILE; |
Steven Moreland | c26d814 | 2018-09-17 14:25:33 -0700 | [diff] [blame] | 261 | } else if (lang == "ndk") { |
| 262 | language_ = Options::Language::NDK; |
| 263 | task_ = Options::Task::COMPILE; |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 264 | } else if (lang == "rust") { |
| 265 | language_ = Options::Language::RUST; |
| 266 | task_ = Options::Task::COMPILE; |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 267 | } else { |
| 268 | error_message_ << "Unsupported language: '" << lang << "'" << endl; |
| 269 | return; |
| 270 | } |
| 271 | } |
| 272 | break; |
| 273 | case 's': |
| 274 | if (task_ != Options::Task::UNSPECIFIED) { |
| 275 | task_ = Options::Task::PREPROCESS; |
| 276 | } |
| 277 | break; |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 278 | #ifndef _WIN32 |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 279 | case 'u': |
| 280 | if (task_ != Options::Task::UNSPECIFIED) { |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 281 | task_ = Options::Task::DUMP_API; |
| 282 | } |
| 283 | break; |
Jooyung Han | 252657e | 2021-02-27 02:51:39 +0900 | [diff] [blame^] | 284 | case 'x': |
| 285 | dump_no_license_ = true; |
| 286 | break; |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 287 | case 'A': |
| 288 | if (task_ != Options::Task::UNSPECIFIED) { |
| 289 | task_ = Options::Task::CHECK_API; |
| 290 | // to ensure that all parcelables in the api dumpes are structured |
| 291 | structured_ = true; |
Jooyung Han | b8a9777 | 2021-01-19 01:27:38 +0900 | [diff] [blame] | 292 | if (optarg) { |
| 293 | if (strcmp(optarg, "compatible") == 0) |
| 294 | check_api_level_ = CheckApiLevel::COMPATIBLE; |
| 295 | else if (strcmp(optarg, "equal") == 0) |
| 296 | check_api_level_ = CheckApiLevel::EQUAL; |
| 297 | else { |
| 298 | error_message_ << "Unsupported --checkapi level: '" << optarg << "'" << endl; |
| 299 | return; |
| 300 | } |
| 301 | } |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 302 | } |
| 303 | break; |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 304 | #endif |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 305 | case 'I': { |
| 306 | import_dirs_.emplace(Trim(optarg)); |
Jiyong Park | 3c35e39 | 2018-08-30 13:10:30 +0900 | [diff] [blame] | 307 | break; |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 308 | } |
| 309 | case 'm': { |
| 310 | import_files_.emplace(Trim(optarg)); |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 311 | break; |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 312 | } |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 313 | case 'p': |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 314 | preprocessed_files_.emplace_back(Trim(optarg)); |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 315 | break; |
| 316 | case 'd': |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 317 | dependency_file_ = Trim(optarg); |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 318 | break; |
| 319 | case 'o': |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 320 | output_dir_ = Trim(optarg); |
Jiyong Park | 0546373 | 2018-08-09 16:03:02 +0900 | [diff] [blame] | 321 | if (output_dir_.back() != OS_PATH_SEPARATOR) { |
| 322 | output_dir_.push_back(OS_PATH_SEPARATOR); |
| 323 | } |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 324 | break; |
| 325 | case 'h': |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 326 | output_header_dir_ = Trim(optarg); |
Jiyong Park | 0546373 | 2018-08-09 16:03:02 +0900 | [diff] [blame] | 327 | if (output_header_dir_.back() != OS_PATH_SEPARATOR) { |
| 328 | output_header_dir_.push_back(OS_PATH_SEPARATOR); |
| 329 | } |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 330 | break; |
| 331 | case 'n': |
| 332 | dependency_file_ninja_ = true; |
| 333 | break; |
Steven Moreland | 6cee348 | 2018-07-18 14:39:58 -0700 | [diff] [blame] | 334 | case 'S': |
| 335 | structured_ = true; |
| 336 | break; |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 337 | case 'Y': { |
| 338 | const string stability_str = Trim(optarg); |
| 339 | if (!StabilityFromString(stability_str, &stability_)) { |
| 340 | error_message_ << "Unrecognized stability level: '" << stability_str |
| 341 | << "'. Must be vintf." << endl; |
| 342 | return; |
| 343 | } |
| 344 | break; |
| 345 | } |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 346 | case 't': |
| 347 | gen_traces_ = true; |
| 348 | break; |
| 349 | case 'a': |
| 350 | auto_dep_file_ = true; |
| 351 | break; |
| 352 | case 'b': |
| 353 | fail_on_parcelable_ = true; |
| 354 | break; |
| 355 | case 'c': |
| 356 | gen_transaction_names_ = true; |
| 357 | break; |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 358 | case 'v': { |
| 359 | const string ver_str = Trim(optarg); |
| 360 | int ver = atoi(ver_str.c_str()); |
| 361 | if (ver > 0) { |
| 362 | version_ = ver; |
| 363 | } else { |
| 364 | error_message_ << "Invalid version number: '" << ver_str << "'. " |
| 365 | << "Version must be a positive natural number." << endl; |
| 366 | return; |
| 367 | } |
| 368 | break; |
| 369 | } |
Paul Trautrim | b77048c | 2020-01-21 16:39:32 +0900 | [diff] [blame] | 370 | case 'H': |
| 371 | hash_ = Trim(optarg); |
| 372 | break; |
Jiyong Park | ce50e26 | 2018-10-29 09:54:20 +0900 | [diff] [blame] | 373 | case 'L': |
| 374 | gen_log_ = true; |
| 375 | break; |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 376 | case 'e': |
| 377 | std::cerr << GetUsage(); |
| 378 | exit(0); |
Andrei Onea | 8714b02 | 2019-02-01 18:55:54 +0000 | [diff] [blame] | 379 | case 'i': |
| 380 | output_file_ = Trim(optarg); |
| 381 | task_ = Task::DUMP_MAPPINGS; |
| 382 | break; |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 383 | default: |
Steven Moreland | 4dbadf5 | 2018-08-08 17:46:10 -0700 | [diff] [blame] | 384 | std::cerr << GetUsage(); |
| 385 | exit(1); |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 386 | } |
| 387 | } // while |
| 388 | |
| 389 | // Positional arguments |
| 390 | if (!lang_option_found && task_ == Options::Task::COMPILE) { |
| 391 | // the legacy arguments format |
| 392 | if (argc - optind <= 0) { |
| 393 | error_message_ << "No input file" << endl; |
| 394 | return; |
| 395 | } |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 396 | if (language_ == Options::Language::JAVA || language_ == Options::Language::RUST) { |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 397 | input_files_.emplace_back(argv[optind++]); |
| 398 | if (argc - optind >= 1) { |
| 399 | output_file_ = argv[optind++]; |
Jiyong Park | 56f73d7 | 2019-06-11 12:20:28 +0900 | [diff] [blame] | 400 | } else if (output_dir_.empty()) { |
| 401 | // when output is omitted and -o option isn't set, the output is by |
| 402 | // default set to the input file path with .aidl is replaced to .java. |
| 403 | // If -o option is set, the output path is calculated by |
Jooyung Han | b3cd63f | 2021-01-05 13:38:46 +0900 | [diff] [blame] | 404 | // GetOutputFilePath which returns "<output_dir>/<package/name>/ |
Jiyong Park | 56f73d7 | 2019-06-11 12:20:28 +0900 | [diff] [blame] | 405 | // <typename>.java" |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 406 | output_file_ = input_files_.front(); |
Steven Moreland | 4dbadf5 | 2018-08-08 17:46:10 -0700 | [diff] [blame] | 407 | if (android::base::EndsWith(output_file_, ".aidl")) { |
| 408 | output_file_ = output_file_.substr(0, output_file_.length() - strlen(".aidl")); |
| 409 | } |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 410 | output_file_ += (language_ == Options::Language::JAVA) ? ".java" : ".rs"; |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 411 | } |
Steven Moreland | c26d814 | 2018-09-17 14:25:33 -0700 | [diff] [blame] | 412 | } else if (IsCppOutput()) { |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 413 | input_files_.emplace_back(argv[optind++]); |
| 414 | if (argc - optind < 2) { |
| 415 | error_message_ << "No HEADER_DIR or OUTPUT." << endl; |
| 416 | return; |
| 417 | } |
| 418 | output_header_dir_ = argv[optind++]; |
Jiyong Park | 0546373 | 2018-08-09 16:03:02 +0900 | [diff] [blame] | 419 | if (output_header_dir_.back() != OS_PATH_SEPARATOR) { |
| 420 | output_header_dir_.push_back(OS_PATH_SEPARATOR); |
| 421 | } |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 422 | output_file_ = argv[optind++]; |
| 423 | } |
| 424 | if (argc - optind > 0) { |
| 425 | error_message_ << "Too many arguments: "; |
| 426 | for (int i = optind; i < argc; i++) { |
| 427 | error_message_ << " " << argv[i]; |
| 428 | } |
| 429 | error_message_ << endl; |
| 430 | } |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 431 | } else { |
| 432 | // the new arguments format |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 433 | if (task_ == Options::Task::COMPILE || task_ == Options::Task::DUMP_API) { |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 434 | if (argc - optind < 1) { |
| 435 | error_message_ << "No input file." << endl; |
| 436 | return; |
| 437 | } |
| 438 | } else { |
| 439 | if (argc - optind < 2) { |
| 440 | error_message_ << "Insufficient arguments. At least 2 required, but " |
| 441 | << "got " << (argc - optind) << "." << endl; |
| 442 | return; |
| 443 | } |
Andrei Onea | 8714b02 | 2019-02-01 18:55:54 +0000 | [diff] [blame] | 444 | if (task_ != Options::Task::CHECK_API && task_ != Options::Task::DUMP_MAPPINGS) { |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 445 | output_file_ = argv[optind++]; |
| 446 | } |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 447 | } |
| 448 | while (optind < argc) { |
| 449 | input_files_.emplace_back(argv[optind++]); |
| 450 | } |
Christopher Wiley | a590de8 | 2015-09-15 15:46:28 -0700 | [diff] [blame] | 451 | } |
| 452 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 453 | // filter out invalid combinations |
Steven Moreland | 4dbadf5 | 2018-08-08 17:46:10 -0700 | [diff] [blame] | 454 | if (lang_option_found) { |
Steven Moreland | c26d814 | 2018-09-17 14:25:33 -0700 | [diff] [blame] | 455 | if (IsCppOutput() && task_ == Options::Task::COMPILE) { |
Steven Moreland | 4dbadf5 | 2018-08-08 17:46:10 -0700 | [diff] [blame] | 456 | if (output_dir_.empty()) { |
| 457 | error_message_ << "Output directory is not set. Set with --out." << endl; |
| 458 | return; |
| 459 | } |
| 460 | if (output_header_dir_.empty()) { |
| 461 | error_message_ << "Header output directory is not set. Set with " |
| 462 | << "--header_out." << endl; |
| 463 | return; |
| 464 | } |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 465 | } |
Steven Moreland | 4dbadf5 | 2018-08-08 17:46:10 -0700 | [diff] [blame] | 466 | if (language_ == Options::Language::JAVA && task_ == Options::Task::COMPILE) { |
| 467 | if (output_dir_.empty()) { |
| 468 | error_message_ << "Output directory is not set. Set with --out." << endl; |
| 469 | return; |
| 470 | } |
| 471 | if (!output_header_dir_.empty()) { |
| 472 | error_message_ << "Header output directory is set, which does not make " |
| 473 | << "sense for Java." << endl; |
| 474 | return; |
| 475 | } |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 476 | } |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 477 | if (language_ == Options::Language::RUST && task_ == Options::Task::COMPILE) { |
| 478 | if (output_dir_.empty()) { |
| 479 | error_message_ << "Output directory is not set. Set with --out." << endl; |
| 480 | return; |
| 481 | } |
| 482 | if (!output_header_dir_.empty()) { |
| 483 | error_message_ << "Header output directory is set, which does not make " |
| 484 | << "sense for Rust." << endl; |
| 485 | return; |
| 486 | } |
| 487 | } |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 488 | } |
| 489 | if (task_ == Options::Task::COMPILE) { |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 490 | for (const string& input : input_files_) { |
| 491 | if (!android::base::EndsWith(input, ".aidl")) { |
| 492 | error_message_ << "Expected .aidl file for input but got '" << input << "'" << endl; |
| 493 | return; |
| 494 | } |
| 495 | } |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 496 | if (!output_file_.empty() && input_files_.size() > 1) { |
| 497 | error_message_ << "Multiple AIDL files can't be compiled to a single " |
| 498 | << "output file '" << output_file_ << "'. " |
| 499 | << "Use --out=DIR instead for output files." << endl; |
| 500 | return; |
| 501 | } |
| 502 | if (!dependency_file_.empty() && input_files_.size() > 1) { |
| 503 | error_message_ << "-d or --dep doesn't work when compiling multiple AIDL " |
| 504 | << "files. Use '-a' to generate dependency file next to " |
| 505 | << "the output file with the name based on the input " |
| 506 | << "file." << endl; |
| 507 | return; |
| 508 | } |
Jeongik Cha | 37e2ad5 | 2019-04-18 13:44:26 +0900 | [diff] [blame] | 509 | if (gen_log_ && (language_ != Options::Language::CPP && language_ != Options::Language::NDK)) { |
| 510 | 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] | 511 | return; |
| 512 | } |
Christopher Wiley | a590de8 | 2015-09-15 15:46:28 -0700 | [diff] [blame] | 513 | } |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 514 | if (task_ == Options::Task::PREPROCESS) { |
| 515 | if (version_ > 0) { |
| 516 | error_message_ << "--version should not be used with '--preprocess'." << endl; |
| 517 | return; |
| 518 | } |
| 519 | } |
Jiyong Park | 3656c3c | 2018-08-01 20:02:01 +0900 | [diff] [blame] | 520 | if (task_ == Options::Task::CHECK_API) { |
| 521 | if (input_files_.size() != 2) { |
| 522 | error_message_ << "--checkapi requires two inputs for comparing, " |
| 523 | << "but got " << input_files_.size() << "." << endl; |
| 524 | return; |
| 525 | } |
| 526 | } |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 527 | if (task_ == Options::Task::DUMP_API) { |
| 528 | if (output_dir_.empty()) { |
Jooyung Han | 4bb2a15 | 2020-10-12 18:11:41 +0900 | [diff] [blame] | 529 | error_message_ << "--dumpapi requires output directory. Use --out." << endl; |
Jiyong Park | e59c368 | 2018-09-11 23:10:25 +0900 | [diff] [blame] | 530 | return; |
| 531 | } |
| 532 | } |
Jiyong Park | 0546373 | 2018-08-09 16:03:02 +0900 | [diff] [blame] | 533 | |
Steven Moreland | 2178081 | 2020-09-11 01:29:45 +0000 | [diff] [blame] | 534 | AIDL_FATAL_IF(!output_dir_.empty() && output_dir_.back() != OS_PATH_SEPARATOR, output_dir_); |
| 535 | AIDL_FATAL_IF(!output_header_dir_.empty() && output_header_dir_.back() != OS_PATH_SEPARATOR, |
| 536 | output_header_dir_); |
Christopher Wiley | a590de8 | 2015-09-15 15:46:28 -0700 | [diff] [blame] | 537 | } |
| 538 | |
Jooyung Han | 888c5bc | 2020-12-22 17:28:47 +0900 | [diff] [blame] | 539 | std::vector<const char*> WarningOptions::Parse(int argc, const char* const raw_argv[], |
| 540 | ErrorMessage& error_message) { |
| 541 | std::vector<const char*> remains; |
| 542 | for (int i = 0; i < argc; i++) { |
| 543 | auto arg = raw_argv[i]; |
| 544 | if (strcmp(arg, "-Weverything") == 0) { |
| 545 | enable_all_ = true; |
| 546 | } else if (strcmp(arg, "-Werror") == 0) { |
| 547 | as_errors_ = true; |
| 548 | } else if (strcmp(arg, "-w") == 0) { |
| 549 | disable_all_ = true; |
| 550 | } else if (base::StartsWith(arg, "-Wno-error=")) { |
| 551 | no_errors_.insert(arg + strlen("-Wno-error=")); |
| 552 | } else if (base::StartsWith(arg, "-Wno-")) { |
| 553 | disabled_.insert(arg + strlen("-Wno-")); |
| 554 | } else if (base::StartsWith(arg, "-W")) { |
| 555 | enabled_.insert(arg + strlen("-W")); |
| 556 | } else { |
| 557 | remains.push_back(arg); |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | for (const auto& names : {no_errors_, disabled_, enabled_}) { |
| 562 | for (const auto& name : names) { |
| 563 | if (kAllDiagnostics.count(name) == 0) { |
| 564 | error_message << "unknown warning: " << name << "\n"; |
| 565 | return {}; |
| 566 | } |
| 567 | } |
| 568 | } |
| 569 | |
Jooyung Han | 808a2a0 | 2020-12-28 16:46:54 +0900 | [diff] [blame] | 570 | return remains; |
| 571 | } |
| 572 | |
| 573 | DiagnosticMapping WarningOptions::GetDiagnosticMapping() const { |
| 574 | DiagnosticMapping mapping; |
Jooyung Han | 888c5bc | 2020-12-22 17:28:47 +0900 | [diff] [blame] | 575 | for (const auto& [_, d] : kAllDiagnostics) { |
| 576 | bool enabled = d.default_enabled; |
| 577 | if (enable_all_ || enabled_.find(d.name) != enabled_.end()) { |
| 578 | enabled = true; |
| 579 | } |
| 580 | if (disable_all_ || disabled_.find(d.name) != disabled_.end()) { |
| 581 | enabled = false; |
| 582 | } |
| 583 | |
| 584 | DiagnosticSeverity severity = DiagnosticSeverity::DISABLED; |
| 585 | if (enabled) { |
| 586 | severity = DiagnosticSeverity::WARNING; |
| 587 | if (as_errors_ && no_errors_.find(d.name) == no_errors_.end()) { |
| 588 | severity = DiagnosticSeverity::ERROR; |
| 589 | } |
| 590 | } |
Jooyung Han | 808a2a0 | 2020-12-28 16:46:54 +0900 | [diff] [blame] | 591 | mapping.Severity(d.id, severity); |
Jooyung Han | 888c5bc | 2020-12-22 17:28:47 +0900 | [diff] [blame] | 592 | } |
Jooyung Han | 808a2a0 | 2020-12-28 16:46:54 +0900 | [diff] [blame] | 593 | return mapping; |
Jooyung Han | 888c5bc | 2020-12-22 17:28:47 +0900 | [diff] [blame] | 594 | } |
| 595 | |
Christopher Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 596 | } // namespace aidl |
Steven Moreland | f4c64df | 2019-07-29 19:54:04 -0700 | [diff] [blame] | 597 | } // namespace android |