1 /* 2 * Copyright (C) 2015, The Android Open Source Project * 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 16 #pragma once 17 18 #include <set> 19 #include <string> 20 #include <vector> 21 22 #include <android-base/result.h> 23 #include <android-base/strings.h> 24 25 #include "diagnostics.h" 26 #include "logging.h" 27 28 namespace android { 29 namespace aidl { 30 31 using std::set; 32 using std::string; 33 using std::vector; 34 35 // The oldest SDK version that is supported for each backend. For non-Java backends, these are the 36 // platform SDK version where the support for the backend was added. For Java backend, this is 1. 37 // TODO(b/205065703) switch back to DEFAULT_SDK_VERSION_JAVA = 23 38 constexpr uint32_t DEFAULT_SDK_VERSION_JAVA = 1; 39 constexpr uint32_t DEFAULT_SDK_VERSION_CPP = 23; 40 constexpr uint32_t DEFAULT_SDK_VERSION_NDK = 29; 41 constexpr uint32_t DEFAULT_SDK_VERSION_RUST = 31; 42 43 constexpr uint32_t SDK_VERSION_current = 10000; 44 constexpr uint32_t SDK_VERSION_Tiramisu = 33; 45 constexpr uint32_t SDK_VERSION_UpsideDownCake = 34; 46 47 constexpr uint32_t JAVA_PROPAGATE_VERSION = SDK_VERSION_Tiramisu; 48 49 // A simple wrapper around ostringstream. This is just to make Options class 50 // copiable by the implicit copy constructor. If ostingstream is not wrapped, 51 // the implcit copy constructor is not generated because ostringstream isn't 52 // copiable. This class makes the field copiable by having a copy constructor 53 // that does not copy the underlying stream. 54 class ErrorMessage { 55 public: 56 ErrorMessage() = default; ErrorMessage(const ErrorMessage &)57 ErrorMessage(const ErrorMessage&) {} 58 std::ostringstream stream_; 59 60 template <typename T> 61 ErrorMessage& operator<<(T& t) { 62 stream_ << t; 63 return *this; 64 } 65 66 template <typename T> 67 ErrorMessage& operator<<(const T& t) { 68 stream_ << t; 69 return *this; 70 } 71 72 // for "<< endl" 73 ErrorMessage& operator<<(std::ostream& (*f)(std::ostream&)) { 74 f(stream_); 75 return *this; 76 } 77 }; 78 79 // Handles warning-related options (e.g. -W, -w, ...) 80 class WarningOptions { 81 public: 82 std::vector<const char*> Parse(int argc, const char* const argv[], ErrorMessage& error_message); 83 DiagnosticMapping GetDiagnosticMapping() const; 84 85 private: 86 bool as_errors_ = false; // -Werror 87 bool enable_all_ = false; // -Weverything 88 bool disable_all_ = false; // -w 89 std::set<std::string> enabled_; // -Wfoo 90 std::set<std::string> disabled_; // -Wno-foo 91 std::set<std::string> no_errors_; // -Wno-error=foo 92 }; 93 94 // Options for AIDL 95 // 96 // These are passed all throughout the compiler, but they should not affect the 97 // code which is generated. In order to avoid ODR issues, and also in order to 98 // make sure the language is orthogonal and portable, we should only generate 99 // different things based on the file contents themselves. 100 class Options final { 101 public: 102 enum class Language { UNSPECIFIED, JAVA, CPP, NDK, RUST, CPP_ANALYZER }; 103 104 enum class Task { HELP, COMPILE, PREPROCESS, DUMP_API, CHECK_API, DUMP_MAPPINGS }; 105 106 enum class CheckApiLevel { COMPATIBLE, EQUAL }; 107 108 enum class Stability { UNSPECIFIED, VINTF }; 109 bool StabilityFromString(const std::string& stability, Stability* out_stability); 110 111 Options(int argc, const char* const argv[], Language default_lang = Language::UNSPECIFIED); 112 113 Options PlusImportDir(const std::string& import_dir) const; 114 AsPreviousVersion()115 Options AsPreviousVersion() const { 116 Options copy(*this); 117 copy.as_previous_version_ = true; 118 return copy; 119 } 120 WithoutVersion()121 Options WithoutVersion() const { 122 Options copy(*this); 123 copy.version_ = 0; 124 copy.hash_ = ""; 125 return copy; 126 } 127 WithNoWarnings()128 Options WithNoWarnings() const { 129 Options copy(*this); 130 copy.warning_options_ = WarningOptions(); 131 return copy; 132 } 133 134 static Options From(const string& cmdline); 135 136 static Options From(const vector<string>& args); 137 138 // to print in output files RawArgs()139 std::string RawArgs() const { return raw_args_; } 140 141 // Contain no references to unstructured data types (such as a parcelable that is 142 // implemented in Java). These interfaces aren't inherently stable but they have the 143 // capacity to be stabilized. IsStructured()144 bool IsStructured() const { return structured_; } 145 GetStability()146 Stability GetStability() const { return stability_; } 147 GetMinSdkVersion()148 uint32_t GetMinSdkVersion() const { return min_sdk_version_; } 149 TargetLanguage()150 Language TargetLanguage() const { return language_; } IsCppOutput()151 bool IsCppOutput() const { 152 return language_ == Language::CPP || language_ == Language::NDK || 153 language_ == Language::CPP_ANALYZER; 154 } 155 GetTask()156 Task GetTask() const { return task_; } 157 GetCheckApiLevel()158 CheckApiLevel GetCheckApiLevel() const { return check_api_level_; } 159 ImportDirs()160 const set<string>& ImportDirs() const { 161 if (as_previous_version_) { 162 return previous_import_dirs_; 163 } else { 164 return import_dirs_; 165 } 166 } 167 PreprocessedFiles()168 const vector<string>& PreprocessedFiles() const { return preprocessed_files_; } 169 DependencyFile()170 string DependencyFile() const { 171 return dependency_file_; 172 } 173 AutoDepFile()174 bool AutoDepFile() const { return auto_dep_file_; } 175 GenRpc()176 bool GenRpc() const { return gen_rpc_; } 177 GenTraces()178 bool GenTraces() const { return gen_traces_; } 179 GenTransactionNames()180 bool GenTransactionNames() const { return gen_transaction_names_; } 181 DependencyFileNinja()182 bool DependencyFileNinja() const { return dependency_file_ninja_; } 183 PreviousApiDir()184 const string& PreviousApiDir() const { return previous_api_dir_; } 185 InputFiles()186 const vector<string>& InputFiles() const { return input_files_; } 187 188 // Path to the output file. This is used only when there is only one 189 // output file for the invocation. When there are multiple outputs 190 // (e.g. compile multiple AIDL files), output files are created under 191 // OutputDir(). OutputFile()192 const string& OutputFile() const { return output_file_; } 193 194 // Path to the directory where output file(s) will be generated under. OutputDir()195 const string& OutputDir() const { return output_dir_; } 196 197 // Path to the directory where header file(s) will be generated under. 198 // Only used when TargetLanguage() == Language::CPP OutputHeaderDir()199 const string& OutputHeaderDir() const { return output_header_dir_; } 200 FailOnParcelable()201 bool FailOnParcelable() const { return fail_on_parcelable_; } 202 Version()203 int Version() const { return version_; } 204 PreviousVersion()205 int PreviousVersion() const { 206 AIDL_FATAL_IF(version_ <= 1, "This should only be called on versions greater than 1"); 207 return version_ - 1; 208 } 209 IsLatestUnfrozenVersion()210 bool IsLatestUnfrozenVersion() const { return !PreviousApiDir().empty(); } 211 Hash()212 string Hash() const { return hash_; } 213 PreviousHash()214 string PreviousHash() const { return previous_hash_; } 215 GenLog()216 bool GenLog() const { return gen_log_; } 217 DumpNoLicense()218 bool DumpNoLicense() const { return dump_no_license_; } 219 Ok()220 bool Ok() const { return error_message_.stream_.str().empty(); } 221 GetErrorMessage()222 string GetErrorMessage() const { return error_message_.stream_.str(); } 223 224 string GetUsage() const; 225 GenApiMapping()226 bool GenApiMapping() const { return task_ == Task::DUMP_MAPPINGS; } 227 GetDiagnosticMapping()228 DiagnosticMapping GetDiagnosticMapping() const { return warning_options_.GetDiagnosticMapping(); } 229 230 // The following are for testability, but cannot be influenced on the command line. 231 // Threshold of interface methods to enable outlining of onTransact cases. 232 size_t onTransact_outline_threshold_{275u}; 233 // Number of cases to _not_ outline, if outlining is enabled. 234 size_t onTransact_non_outline_count_{275u}; 235 236 private: 237 Options() = default; 238 239 const string myname_; 240 std::string raw_args_; 241 Language language_ = Language::UNSPECIFIED; 242 Task task_ = Task::COMPILE; 243 CheckApiLevel check_api_level_ = CheckApiLevel::COMPATIBLE; 244 set<string> import_dirs_; 245 set<string> previous_import_dirs_; 246 bool as_previous_version_ = false; 247 vector<string> preprocessed_files_; 248 string dependency_file_; 249 bool gen_rpc_ = false; 250 bool gen_traces_ = false; 251 bool gen_transaction_names_ = false; 252 bool dependency_file_ninja_ = false; 253 string previous_api_dir_; 254 bool structured_ = false; 255 Stability stability_ = Stability::UNSPECIFIED; 256 uint32_t min_sdk_version_ = 0; // invalid version 257 string output_dir_; 258 string output_header_dir_; 259 bool fail_on_parcelable_ = false; 260 bool auto_dep_file_ = false; 261 vector<string> input_files_; 262 string output_file_; 263 int version_ = 0; 264 string hash_ = ""; 265 string previous_hash_ = ""; 266 bool gen_log_ = false; 267 bool dump_no_license_ = false; 268 ErrorMessage error_message_; 269 WarningOptions warning_options_; 270 }; 271 272 std::string to_string(Options::Language language); 273 android::base::Result<uint32_t> MinSdkVersionFromString(const std::string& str); 274 275 } // namespace aidl 276 } // namespace android 277