Christopher Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 1 | /* |
Christopher Wiley | b1bbdf8 | 2016-04-21 11:43:45 -0700 | [diff] [blame] | 2 | * Copyright (C) 2015, The Android Open Source Project * |
Christopher Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | |
Steven Moreland | 9fccf58 | 2018-08-27 20:36:27 -0700 | [diff] [blame] | 16 | #pragma once |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 17 | |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 18 | #include <set> |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 19 | #include <string> |
| 20 | #include <vector> |
| 21 | |
Jooyung Han | 74406c5 | 2021-11-06 06:17:36 +0900 | [diff] [blame] | 22 | #include <android-base/result.h> |
| 23 | |
Jooyung Han | 888c5bc | 2020-12-22 17:28:47 +0900 | [diff] [blame] | 24 | #include "diagnostics.h" |
| 25 | |
Christopher Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 26 | namespace android { |
| 27 | namespace aidl { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 28 | |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 29 | using std::set; |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 30 | using std::string; |
| 31 | using std::vector; |
| 32 | |
Jiyong Park | 223274e | 2021-09-24 14:14:12 +0900 | [diff] [blame] | 33 | // The oldest SDK version that is supported for each backend. For non-Java backends, these are the |
Jooyung Han | d11f9f5 | 2021-11-04 11:34:57 +0900 | [diff] [blame] | 34 | // platform SDK version where the support for the backend was added. For Java backend, this is 1. |
| 35 | // TODO(b/205065703) switch back to DEFAULT_SDK_VERSION_JAVA = 23 |
| 36 | constexpr uint32_t DEFAULT_SDK_VERSION_JAVA = 1; |
Jiyong Park | 223274e | 2021-09-24 14:14:12 +0900 | [diff] [blame] | 37 | constexpr uint32_t DEFAULT_SDK_VERSION_CPP = 23; |
| 38 | constexpr uint32_t DEFAULT_SDK_VERSION_NDK = 29; |
| 39 | constexpr uint32_t DEFAULT_SDK_VERSION_RUST = 31; |
| 40 | |
Jooyung Han | 6edc7ec | 2021-12-23 11:57:49 +0900 | [diff] [blame] | 41 | constexpr uint32_t SDK_VERSION_current = 10000; |
Steven Moreland | 4de616b | 2022-04-15 22:51:07 +0000 | [diff] [blame] | 42 | constexpr uint32_t SDK_VERSION_Tiramisu = 33; |
Jooyung Han | 6edc7ec | 2021-12-23 11:57:49 +0900 | [diff] [blame] | 43 | |
Steven Moreland | aad0e4b | 2022-02-04 18:52:44 +0000 | [diff] [blame] | 44 | constexpr uint32_t JAVA_PROPAGATE_VERSION = SDK_VERSION_Tiramisu; |
| 45 | |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 46 | // A simple wrapper around ostringstream. This is just to make Options class |
| 47 | // copiable by the implicit copy constructor. If ostingstream is not wrapped, |
| 48 | // the implcit copy constructor is not generated because ostringstream isn't |
| 49 | // copiable. This class makes the field copiable by having a copy constructor |
| 50 | // that does not copy the underlying stream. |
| 51 | class ErrorMessage { |
| 52 | public: |
| 53 | ErrorMessage() = default; |
| 54 | ErrorMessage(const ErrorMessage&) {} |
| 55 | std::ostringstream stream_; |
Jiyong Park | e1d0268 | 2018-08-01 12:18:02 +0900 | [diff] [blame] | 56 | |
| 57 | template <typename T> |
| 58 | ErrorMessage& operator<<(T& t) { |
| 59 | stream_ << t; |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 60 | return *this; |
| 61 | } |
Jiyong Park | e1d0268 | 2018-08-01 12:18:02 +0900 | [diff] [blame] | 62 | |
| 63 | template <typename T> |
| 64 | ErrorMessage& operator<<(const T& t) { |
| 65 | stream_ << t; |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 66 | return *this; |
| 67 | } |
Jiyong Park | e1d0268 | 2018-08-01 12:18:02 +0900 | [diff] [blame] | 68 | |
| 69 | // for "<< endl" |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 70 | ErrorMessage& operator<<(std::ostream& (*f)(std::ostream&)) { |
| 71 | f(stream_); |
| 72 | return *this; |
| 73 | } |
| 74 | }; |
| 75 | |
Jooyung Han | 808a2a0 | 2020-12-28 16:46:54 +0900 | [diff] [blame] | 76 | // Handles warning-related options (e.g. -W, -w, ...) |
Jooyung Han | 888c5bc | 2020-12-22 17:28:47 +0900 | [diff] [blame] | 77 | class WarningOptions { |
| 78 | public: |
| 79 | std::vector<const char*> Parse(int argc, const char* const argv[], ErrorMessage& error_message); |
Jooyung Han | 808a2a0 | 2020-12-28 16:46:54 +0900 | [diff] [blame] | 80 | DiagnosticMapping GetDiagnosticMapping() const; |
Jooyung Han | 888c5bc | 2020-12-22 17:28:47 +0900 | [diff] [blame] | 81 | |
| 82 | private: |
| 83 | bool as_errors_ = false; // -Werror |
| 84 | bool enable_all_ = false; // -Weverything |
| 85 | bool disable_all_ = false; // -w |
| 86 | std::set<std::string> enabled_; // -Wfoo |
| 87 | std::set<std::string> disabled_; // -Wno-foo |
| 88 | std::set<std::string> no_errors_; // -Wno-error=foo |
Jooyung Han | 888c5bc | 2020-12-22 17:28:47 +0900 | [diff] [blame] | 89 | }; |
| 90 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 91 | class Options final { |
Steven Moreland | da0654f | 2018-07-17 12:24:38 -0700 | [diff] [blame] | 92 | public: |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 93 | enum class Language { UNSPECIFIED, JAVA, CPP, NDK, RUST }; |
Steven Moreland | da0654f | 2018-07-17 12:24:38 -0700 | [diff] [blame] | 94 | |
Steven Moreland | 481f593 | 2021-08-09 17:31:05 -0700 | [diff] [blame] | 95 | enum class Task { HELP, COMPILE, PREPROCESS, DUMP_API, CHECK_API, DUMP_MAPPINGS }; |
Steven Moreland | da0654f | 2018-07-17 12:24:38 -0700 | [diff] [blame] | 96 | |
Jooyung Han | b8a9777 | 2021-01-19 01:27:38 +0900 | [diff] [blame] | 97 | enum class CheckApiLevel { COMPATIBLE, EQUAL }; |
| 98 | |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 99 | enum class Stability { UNSPECIFIED, VINTF }; |
| 100 | bool StabilityFromString(const std::string& stability, Stability* out_stability); |
| 101 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 102 | Options(int argc, const char* const argv[], Language default_lang = Language::UNSPECIFIED); |
Steven Moreland | da0654f | 2018-07-17 12:24:38 -0700 | [diff] [blame] | 103 | |
Jooyung Han | 8e3b72c | 2021-05-22 02:54:37 +0900 | [diff] [blame] | 104 | Options PlusImportDir(const std::string& import_dir) const { |
| 105 | Options copy(*this); |
| 106 | copy.import_dirs_.insert(import_dir); |
| 107 | return copy; |
| 108 | } |
| 109 | |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 110 | static Options From(const string& cmdline); |
| 111 | |
| 112 | static Options From(const vector<string>& args); |
| 113 | |
Steven Moreland | 6cee348 | 2018-07-18 14:39:58 -0700 | [diff] [blame] | 114 | // Contain no references to unstructured data types (such as a parcelable that is |
| 115 | // implemented in Java). These interfaces aren't inherently stable but they have the |
| 116 | // capacity to be stabilized. |
| 117 | bool IsStructured() const { return structured_; } |
| 118 | |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 119 | Stability GetStability() const { return stability_; } |
| 120 | |
Jiyong Park | 223274e | 2021-09-24 14:14:12 +0900 | [diff] [blame] | 121 | uint32_t GetMinSdkVersion() const { return min_sdk_version_; } |
| 122 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 123 | Language TargetLanguage() const { return language_; } |
Steven Moreland | c26d814 | 2018-09-17 14:25:33 -0700 | [diff] [blame] | 124 | bool IsCppOutput() const { return language_ == Language::CPP || language_ == Language::NDK; } |
Steven Moreland | da0654f | 2018-07-17 12:24:38 -0700 | [diff] [blame] | 125 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 126 | Task GetTask() const { return task_; } |
Steven Moreland | da0654f | 2018-07-17 12:24:38 -0700 | [diff] [blame] | 127 | |
Jooyung Han | b8a9777 | 2021-01-19 01:27:38 +0900 | [diff] [blame] | 128 | CheckApiLevel GetCheckApiLevel() const { return check_api_level_; } |
| 129 | |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 130 | const set<string>& ImportDirs() const { return import_dirs_; } |
Jiyong Park | 3c35e39 | 2018-08-30 13:10:30 +0900 | [diff] [blame] | 131 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 132 | const vector<string>& PreprocessedFiles() const { return preprocessed_files_; } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 133 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 134 | string DependencyFile() const { |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 135 | return dependency_file_; |
| 136 | } |
Christopher Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 137 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 138 | bool AutoDepFile() const { return auto_dep_file_; } |
Christopher Wiley | ef4132c | 2015-11-05 15:47:40 -0800 | [diff] [blame] | 139 | |
Steven Moreland | 4693108 | 2022-02-04 18:24:18 +0000 | [diff] [blame] | 140 | bool GenRpc() const { return gen_rpc_; } |
| 141 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 142 | bool GenTraces() const { return gen_traces_; } |
Steven Moreland | da0654f | 2018-07-17 12:24:38 -0700 | [diff] [blame] | 143 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 144 | bool GenTransactionNames() const { return gen_transaction_names_; } |
Christopher Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 145 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 146 | bool DependencyFileNinja() const { return dependency_file_ninja_; } |
| 147 | |
| 148 | const vector<string>& InputFiles() const { return input_files_; } |
| 149 | |
| 150 | // Path to the output file. This is used only when there is only one |
| 151 | // output file for the invocation. When there are multiple outputs |
| 152 | // (e.g. compile multiple AIDL files), output files are created under |
| 153 | // OutputDir(). |
| 154 | const string& OutputFile() const { return output_file_; } |
| 155 | |
| 156 | // Path to the directory where output file(s) will be generated under. |
| 157 | const string& OutputDir() const { return output_dir_; } |
| 158 | |
| 159 | // Path to the directory where header file(s) will be generated under. |
| 160 | // Only used when TargetLanguage() == Language::CPP |
| 161 | const string& OutputHeaderDir() const { return output_header_dir_; } |
| 162 | |
| 163 | bool FailOnParcelable() const { return fail_on_parcelable_; } |
| 164 | |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 165 | int Version() const { return version_; } |
| 166 | |
Paul Trautrim | b77048c | 2020-01-21 16:39:32 +0900 | [diff] [blame] | 167 | string Hash() const { return hash_; } |
| 168 | |
Jiyong Park | ce50e26 | 2018-10-29 09:54:20 +0900 | [diff] [blame] | 169 | bool GenLog() const { return gen_log_; } |
| 170 | |
Jooyung Han | 252657e | 2021-02-27 02:51:39 +0900 | [diff] [blame] | 171 | bool DumpNoLicense() const { return dump_no_license_; } |
| 172 | |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 173 | bool Ok() const { return error_message_.stream_.str().empty(); } |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 174 | |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 175 | string GetErrorMessage() const { return error_message_.stream_.str(); } |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 176 | |
| 177 | string GetUsage() const; |
Andreas Gampe | e9c816e | 2018-03-14 09:05:48 -0700 | [diff] [blame] | 178 | |
Andrei Onea | 8714b02 | 2019-02-01 18:55:54 +0000 | [diff] [blame] | 179 | bool GenApiMapping() const { return task_ == Task::DUMP_MAPPINGS; } |
| 180 | |
Jooyung Han | 808a2a0 | 2020-12-28 16:46:54 +0900 | [diff] [blame] | 181 | DiagnosticMapping GetDiagnosticMapping() const { return warning_options_.GetDiagnosticMapping(); } |
Jooyung Han | 888c5bc | 2020-12-22 17:28:47 +0900 | [diff] [blame] | 182 | |
Steven Moreland | da0654f | 2018-07-17 12:24:38 -0700 | [diff] [blame] | 183 | // The following are for testability, but cannot be influenced on the command line. |
Andreas Gampe | e9c816e | 2018-03-14 09:05:48 -0700 | [diff] [blame] | 184 | // Threshold of interface methods to enable outlining of onTransact cases. |
| 185 | size_t onTransact_outline_threshold_{275u}; |
| 186 | // Number of cases to _not_ outline, if outlining is enabled. |
| 187 | size_t onTransact_non_outline_count_{275u}; |
| 188 | |
Christopher Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 189 | private: |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 190 | Options() = default; |
Christopher Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 191 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 192 | const string myname_; |
| 193 | Language language_ = Language::UNSPECIFIED; |
| 194 | Task task_ = Task::COMPILE; |
Jooyung Han | b8a9777 | 2021-01-19 01:27:38 +0900 | [diff] [blame] | 195 | CheckApiLevel check_api_level_ = CheckApiLevel::COMPATIBLE; |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 196 | set<string> import_dirs_; |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 197 | vector<string> preprocessed_files_; |
| 198 | string dependency_file_; |
Steven Moreland | 4693108 | 2022-02-04 18:24:18 +0000 | [diff] [blame] | 199 | bool gen_rpc_ = false; |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 200 | bool gen_traces_ = false; |
| 201 | bool gen_transaction_names_ = false; |
| 202 | bool dependency_file_ninja_ = false; |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 203 | bool structured_ = false; |
| 204 | Stability stability_ = Stability::UNSPECIFIED; |
Jiyong Park | 223274e | 2021-09-24 14:14:12 +0900 | [diff] [blame] | 205 | uint32_t min_sdk_version_ = 0; // invalid version |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 206 | string output_dir_; |
| 207 | string output_header_dir_; |
| 208 | bool fail_on_parcelable_ = false; |
| 209 | bool auto_dep_file_ = false; |
| 210 | vector<string> input_files_; |
| 211 | string output_file_; |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 212 | int version_ = 0; |
Paul Trautrim | b77048c | 2020-01-21 16:39:32 +0900 | [diff] [blame] | 213 | string hash_ = ""; |
Jiyong Park | ce50e26 | 2018-10-29 09:54:20 +0900 | [diff] [blame] | 214 | bool gen_log_ = false; |
Jooyung Han | 252657e | 2021-02-27 02:51:39 +0900 | [diff] [blame] | 215 | bool dump_no_license_ = false; |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 216 | ErrorMessage error_message_; |
Jooyung Han | 888c5bc | 2020-12-22 17:28:47 +0900 | [diff] [blame] | 217 | WarningOptions warning_options_; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 218 | }; |
| 219 | |
Jooyung Han | 9435e9a | 2021-01-06 10:16:31 +0900 | [diff] [blame] | 220 | std::string to_string(Options::Language language); |
Jooyung Han | 74406c5 | 2021-11-06 06:17:36 +0900 | [diff] [blame] | 221 | android::base::Result<uint32_t> MinSdkVersionFromString(const std::string& str); |
Jooyung Han | 9435e9a | 2021-01-06 10:16:31 +0900 | [diff] [blame] | 222 | |
Christopher Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 223 | } // namespace aidl |
Steven Moreland | f4c64df | 2019-07-29 19:54:04 -0700 | [diff] [blame] | 224 | } // namespace android |