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 24 #include "diagnostics.h" 25 26 namespace android { 27 namespace aidl { 28 29 using std::set; 30 using std::string; 31 using std::vector; 32 33 // The oldest SDK version that is supported for each backend. For non-Java backends, these are the 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; 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 41 constexpr uint32_t SDK_VERSION_current = 10000; 42 constexpr uint32_t SDK_VERSION_Tiramisu = 33; 43 44 constexpr uint32_t JAVA_PROPAGATE_VERSION = SDK_VERSION_Tiramisu; 45 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; ErrorMessage(const ErrorMessage &)54 ErrorMessage(const ErrorMessage&) {} 55 std::ostringstream stream_; 56 57 template <typename T> 58 ErrorMessage& operator<<(T& t) { 59 stream_ << t; 60 return *this; 61 } 62 63 template <typename T> 64 ErrorMessage& operator<<(const T& t) { 65 stream_ << t; 66 return *this; 67 } 68 69 // for "<< endl" 70 ErrorMessage& operator<<(std::ostream& (*f)(std::ostream&)) { 71 f(stream_); 72 return *this; 73 } 74 }; 75 76 // Handles warning-related options (e.g. -W, -w, ...) 77 class WarningOptions { 78 public: 79 std::vector<const char*> Parse(int argc, const char* const argv[], ErrorMessage& error_message); 80 DiagnosticMapping GetDiagnosticMapping() const; 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 89 }; 90 91 class Options final { 92 public: 93 enum class Language { UNSPECIFIED, JAVA, CPP, NDK, RUST }; 94 95 enum class Task { HELP, COMPILE, PREPROCESS, DUMP_API, CHECK_API, DUMP_MAPPINGS }; 96 97 enum class CheckApiLevel { COMPATIBLE, EQUAL }; 98 99 enum class Stability { UNSPECIFIED, VINTF }; 100 bool StabilityFromString(const std::string& stability, Stability* out_stability); 101 102 Options(int argc, const char* const argv[], Language default_lang = Language::UNSPECIFIED); 103 PlusImportDir(const std::string & import_dir)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 110 static Options From(const string& cmdline); 111 112 static Options From(const vector<string>& args); 113 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. IsStructured()117 bool IsStructured() const { return structured_; } 118 GetStability()119 Stability GetStability() const { return stability_; } 120 GetMinSdkVersion()121 uint32_t GetMinSdkVersion() const { return min_sdk_version_; } 122 TargetLanguage()123 Language TargetLanguage() const { return language_; } IsCppOutput()124 bool IsCppOutput() const { return language_ == Language::CPP || language_ == Language::NDK; } 125 GetTask()126 Task GetTask() const { return task_; } 127 GetCheckApiLevel()128 CheckApiLevel GetCheckApiLevel() const { return check_api_level_; } 129 ImportDirs()130 const set<string>& ImportDirs() const { return import_dirs_; } 131 PreprocessedFiles()132 const vector<string>& PreprocessedFiles() const { return preprocessed_files_; } 133 DependencyFile()134 string DependencyFile() const { 135 return dependency_file_; 136 } 137 AutoDepFile()138 bool AutoDepFile() const { return auto_dep_file_; } 139 GenRpc()140 bool GenRpc() const { return gen_rpc_; } 141 GenTraces()142 bool GenTraces() const { return gen_traces_; } 143 GenTransactionNames()144 bool GenTransactionNames() const { return gen_transaction_names_; } 145 DependencyFileNinja()146 bool DependencyFileNinja() const { return dependency_file_ninja_; } 147 InputFiles()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(). OutputFile()154 const string& OutputFile() const { return output_file_; } 155 156 // Path to the directory where output file(s) will be generated under. OutputDir()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 OutputHeaderDir()161 const string& OutputHeaderDir() const { return output_header_dir_; } 162 FailOnParcelable()163 bool FailOnParcelable() const { return fail_on_parcelable_; } 164 Version()165 int Version() const { return version_; } 166 Hash()167 string Hash() const { return hash_; } 168 GenLog()169 bool GenLog() const { return gen_log_; } 170 DumpNoLicense()171 bool DumpNoLicense() const { return dump_no_license_; } 172 Ok()173 bool Ok() const { return error_message_.stream_.str().empty(); } 174 GetErrorMessage()175 string GetErrorMessage() const { return error_message_.stream_.str(); } 176 177 string GetUsage() const; 178 GenApiMapping()179 bool GenApiMapping() const { return task_ == Task::DUMP_MAPPINGS; } 180 GetDiagnosticMapping()181 DiagnosticMapping GetDiagnosticMapping() const { return warning_options_.GetDiagnosticMapping(); } 182 183 // The following are for testability, but cannot be influenced on the command line. 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 189 private: 190 Options() = default; 191 192 const string myname_; 193 Language language_ = Language::UNSPECIFIED; 194 Task task_ = Task::COMPILE; 195 CheckApiLevel check_api_level_ = CheckApiLevel::COMPATIBLE; 196 set<string> import_dirs_; 197 vector<string> preprocessed_files_; 198 string dependency_file_; 199 bool gen_rpc_ = false; 200 bool gen_traces_ = false; 201 bool gen_transaction_names_ = false; 202 bool dependency_file_ninja_ = false; 203 bool structured_ = false; 204 Stability stability_ = Stability::UNSPECIFIED; 205 uint32_t min_sdk_version_ = 0; // invalid version 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_; 212 int version_ = 0; 213 string hash_ = ""; 214 bool gen_log_ = false; 215 bool dump_no_license_ = false; 216 ErrorMessage error_message_; 217 WarningOptions warning_options_; 218 }; 219 220 std::string to_string(Options::Language language); 221 android::base::Result<uint32_t> MinSdkVersionFromString(const std::string& str); 222 223 } // namespace aidl 224 } // namespace android 225