• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 constexpr uint32_t SDK_VERSION_UpsideDownCake = 34;
44 
45 constexpr uint32_t JAVA_PROPAGATE_VERSION = SDK_VERSION_Tiramisu;
46 
47 // A simple wrapper around ostringstream. This is just to make Options class
48 // copiable by the implicit copy constructor. If ostingstream is not wrapped,
49 // the implcit copy constructor is not generated because ostringstream isn't
50 // copiable. This class makes the field copiable by having a copy constructor
51 // that does not copy the underlying stream.
52 class ErrorMessage {
53  public:
54   ErrorMessage() = default;
ErrorMessage(const ErrorMessage &)55   ErrorMessage(const ErrorMessage&) {}
56   std::ostringstream stream_;
57 
58   template <typename T>
59   ErrorMessage& operator<<(T& t) {
60     stream_ << t;
61     return *this;
62   }
63 
64   template <typename T>
65   ErrorMessage& operator<<(const T& t) {
66     stream_ << t;
67     return *this;
68   }
69 
70   // for "<< endl"
71   ErrorMessage& operator<<(std::ostream& (*f)(std::ostream&)) {
72     f(stream_);
73     return *this;
74   }
75 };
76 
77 // Handles warning-related options (e.g. -W, -w, ...)
78 class WarningOptions {
79  public:
80   std::vector<const char*> Parse(int argc, const char* const argv[], ErrorMessage& error_message);
81   DiagnosticMapping GetDiagnosticMapping() const;
82 
83  private:
84   bool as_errors_ = false;           // -Werror
85   bool enable_all_ = false;          // -Weverything
86   bool disable_all_ = false;         // -w
87   std::set<std::string> enabled_;    // -Wfoo
88   std::set<std::string> disabled_;   // -Wno-foo
89   std::set<std::string> no_errors_;  // -Wno-error=foo
90 };
91 
92 // Options for AIDL
93 //
94 // These are passed all throughout the compiler, but they should not affect the
95 // code which is generated. In order to avoid ODR issues, and also in order to
96 // make sure the language is orthogonal and portable, we should only generate
97 // different things based on the file contents themselves.
98 class Options final {
99  public:
100   enum class Language { UNSPECIFIED, JAVA, CPP, NDK, RUST, CPP_ANALYZER };
101 
102   enum class Task { HELP, COMPILE, PREPROCESS, DUMP_API, CHECK_API, DUMP_MAPPINGS };
103 
104   enum class CheckApiLevel { COMPATIBLE, EQUAL };
105 
106   enum class Stability { UNSPECIFIED, VINTF };
107   bool StabilityFromString(const std::string& stability, Stability* out_stability);
108 
109   Options(int argc, const char* const argv[], Language default_lang = Language::UNSPECIFIED);
110 
PlusImportDir(const std::string & import_dir)111   Options PlusImportDir(const std::string& import_dir) const {
112     Options copy(*this);
113     copy.import_dirs_.insert(import_dir);
114     return copy;
115   }
116 
117   static Options From(const string& cmdline);
118 
119   static Options From(const vector<string>& args);
120 
121   // Contain no references to unstructured data types (such as a parcelable that is
122   // implemented in Java). These interfaces aren't inherently stable but they have the
123   // capacity to be stabilized.
IsStructured()124   bool IsStructured() const { return structured_; }
125 
GetStability()126   Stability GetStability() const { return stability_; }
127 
GetMinSdkVersion()128   uint32_t GetMinSdkVersion() const { return min_sdk_version_; }
129 
TargetLanguage()130   Language TargetLanguage() const { return language_; }
IsCppOutput()131   bool IsCppOutput() const {
132     return language_ == Language::CPP || language_ == Language::NDK ||
133            language_ == Language::CPP_ANALYZER;
134   }
135 
GetTask()136   Task GetTask() const { return task_; }
137 
GetCheckApiLevel()138   CheckApiLevel GetCheckApiLevel() const { return check_api_level_; }
139 
ImportDirs()140   const set<string>& ImportDirs() const { return import_dirs_; }
141 
PreprocessedFiles()142   const vector<string>& PreprocessedFiles() const { return preprocessed_files_; }
143 
DependencyFile()144   string DependencyFile() const {
145     return dependency_file_;
146   }
147 
AutoDepFile()148   bool AutoDepFile() const { return auto_dep_file_; }
149 
GenRpc()150   bool GenRpc() const { return gen_rpc_; }
151 
GenTraces()152   bool GenTraces() const { return gen_traces_; }
153 
GenTransactionNames()154   bool GenTransactionNames() const { return gen_transaction_names_; }
155 
DependencyFileNinja()156   bool DependencyFileNinja() const { return dependency_file_ninja_; }
157 
InputFiles()158   const vector<string>& InputFiles() const { return input_files_; }
159 
160   // Path to the output file. This is used only when there is only one
161   // output file for the invocation. When there are multiple outputs
162   // (e.g. compile multiple AIDL files), output files are created under
163   // OutputDir().
OutputFile()164   const string& OutputFile() const { return output_file_; }
165 
166   // Path to the directory where output file(s) will be generated under.
OutputDir()167   const string& OutputDir() const { return output_dir_; }
168 
169   // Path to the directory where header file(s) will be generated under.
170   // Only used when TargetLanguage() == Language::CPP
OutputHeaderDir()171   const string& OutputHeaderDir() const { return output_header_dir_; }
172 
FailOnParcelable()173   bool FailOnParcelable() const { return fail_on_parcelable_; }
174 
Version()175   int Version() const { return version_; }
176 
Hash()177   string Hash() const { return hash_; }
178 
GenLog()179   bool GenLog() const { return gen_log_; }
180 
DumpNoLicense()181   bool DumpNoLicense() const { return dump_no_license_; }
182 
Ok()183   bool Ok() const { return error_message_.stream_.str().empty(); }
184 
GetErrorMessage()185   string GetErrorMessage() const { return error_message_.stream_.str(); }
186 
187   string GetUsage() const;
188 
GenApiMapping()189   bool GenApiMapping() const { return task_ == Task::DUMP_MAPPINGS; }
190 
GetDiagnosticMapping()191   DiagnosticMapping GetDiagnosticMapping() const { return warning_options_.GetDiagnosticMapping(); }
192 
193   // The following are for testability, but cannot be influenced on the command line.
194   // Threshold of interface methods to enable outlining of onTransact cases.
195   size_t onTransact_outline_threshold_{275u};
196   // Number of cases to _not_ outline, if outlining is enabled.
197   size_t onTransact_non_outline_count_{275u};
198 
199  private:
200   Options() = default;
201 
202   const string myname_;
203   Language language_ = Language::UNSPECIFIED;
204   Task task_ = Task::COMPILE;
205   CheckApiLevel check_api_level_ = CheckApiLevel::COMPATIBLE;
206   set<string> import_dirs_;
207   vector<string> preprocessed_files_;
208   string dependency_file_;
209   bool gen_rpc_ = false;
210   bool gen_traces_ = false;
211   bool gen_transaction_names_ = false;
212   bool dependency_file_ninja_ = false;
213   bool structured_ = false;
214   Stability stability_ = Stability::UNSPECIFIED;
215   uint32_t min_sdk_version_ = 0;  // invalid version
216   string output_dir_;
217   string output_header_dir_;
218   bool fail_on_parcelable_ = false;
219   bool auto_dep_file_ = false;
220   vector<string> input_files_;
221   string output_file_;
222   int version_ = 0;
223   string hash_ = "";
224   bool gen_log_ = false;
225   bool dump_no_license_ = false;
226   ErrorMessage error_message_;
227   WarningOptions warning_options_;
228 };
229 
230 std::string to_string(Options::Language language);
231 android::base::Result<uint32_t> MinSdkVersionFromString(const std::string& str);
232 
233 }  // namespace aidl
234 }  // namespace android
235