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