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 <sstream>
20 #include <string>
21 #include <vector>
22 
23 namespace android {
24 namespace aidl {
25 
26 using std::set;
27 using std::string;
28 using std::vector;
29 
30 // A simple wrapper around ostringstream. This is just to make Options class
31 // copiable by the implicit copy constructor. If ostingstream is not wrapped,
32 // the implcit copy constructor is not generated because ostringstream isn't
33 // copiable. This class makes the field copiable by having a copy constructor
34 // that does not copy the underlying stream.
35 class ErrorMessage {
36  public:
37   ErrorMessage() = default;
ErrorMessage(const ErrorMessage &)38   ErrorMessage(const ErrorMessage&) {}
39   std::ostringstream stream_;
40 
41   template <typename T>
42   ErrorMessage& operator<<(T& t) {
43     stream_ << t;
44     return *this;
45   }
46 
47   template <typename T>
48   ErrorMessage& operator<<(const T& t) {
49     stream_ << t;
50     return *this;
51   }
52 
53   // for "<< endl"
54   ErrorMessage& operator<<(std::ostream& (*f)(std::ostream&)) {
55     f(stream_);
56     return *this;
57   }
58 };
59 
60 class Options final {
61  public:
62   enum class Language { UNSPECIFIED, JAVA, CPP, NDK };
63 
64   enum class Task { UNSPECIFIED, COMPILE, PREPROCESS, DUMP_API, CHECK_API, DUMP_MAPPINGS };
65 
66   enum class Stability { UNSPECIFIED, VINTF };
67   bool StabilityFromString(const std::string& stability, Stability* out_stability);
68 
69   Options(int argc, const char* const argv[], Language default_lang = Language::UNSPECIFIED);
70 
71   static Options From(const string& cmdline);
72 
73   static Options From(const vector<string>& args);
74 
75   // Contain no references to unstructured data types (such as a parcelable that is
76   // implemented in Java). These interfaces aren't inherently stable but they have the
77   // capacity to be stabilized.
IsStructured()78   bool IsStructured() const { return structured_; }
79 
GetStability()80   Stability GetStability() const { return stability_; }
81 
TargetLanguage()82   Language TargetLanguage() const { return language_; }
IsCppOutput()83   bool IsCppOutput() const { return language_ == Language::CPP || language_ == Language::NDK; }
84 
GetTask()85   Task GetTask() const { return task_; }
86 
ImportDirs()87   const set<string>& ImportDirs() const { return import_dirs_; }
88 
ImportFiles()89   const set<string>& ImportFiles() const { return import_files_; }
90 
PreprocessedFiles()91   const vector<string>& PreprocessedFiles() const { return preprocessed_files_; }
92 
DependencyFile()93   string DependencyFile() const {
94     return dependency_file_;
95   }
96 
AutoDepFile()97   bool AutoDepFile() const { return auto_dep_file_; }
98 
GenTraces()99   bool GenTraces() const { return gen_traces_; }
100 
GenTransactionNames()101   bool GenTransactionNames() const { return gen_transaction_names_; }
102 
DependencyFileNinja()103   bool DependencyFileNinja() const { return dependency_file_ninja_; }
104 
InputFiles()105   const vector<string>& InputFiles() const { return input_files_; }
106 
107   // Path to the output file. This is used only when there is only one
108   // output file for the invocation. When there are multiple outputs
109   // (e.g. compile multiple AIDL files), output files are created under
110   // OutputDir().
OutputFile()111   const string& OutputFile() const { return output_file_; }
112 
113   // Path to the directory where output file(s) will be generated under.
OutputDir()114   const string& OutputDir() const { return output_dir_; }
115 
116   // Path to the directory where header file(s) will be generated under.
117   // Only used when TargetLanguage() == Language::CPP
OutputHeaderDir()118   const string& OutputHeaderDir() const { return output_header_dir_; }
119 
FailOnParcelable()120   bool FailOnParcelable() const { return fail_on_parcelable_; }
121 
Version()122   int Version() const { return version_; }
123 
Hash()124   string Hash() const { return hash_; }
125 
GenLog()126   bool GenLog() const { return gen_log_; }
127 
GenParcelableToString()128   bool GenParcelableToString() const { return gen_parcelable_to_string_; }
129 
Ok()130   bool Ok() const { return error_message_.stream_.str().empty(); }
131 
GetErrorMessage()132   string GetErrorMessage() const { return error_message_.stream_.str(); }
133 
134   string GetUsage() const;
135 
GenApiMapping()136   bool GenApiMapping() const { return task_ == Task::DUMP_MAPPINGS; }
137 
138   static const string LanguageToString(Language language);
139 
140   // The following are for testability, but cannot be influenced on the command line.
141   // Threshold of interface methods to enable outlining of onTransact cases.
142   size_t onTransact_outline_threshold_{275u};
143   // Number of cases to _not_ outline, if outlining is enabled.
144   size_t onTransact_non_outline_count_{275u};
145 
146  private:
147   Options() = default;
148 
149   const string myname_;
150   Language language_ = Language::UNSPECIFIED;
151   Task task_ = Task::COMPILE;
152   set<string> import_dirs_;
153   set<string> import_files_;
154   vector<string> preprocessed_files_;
155   string dependency_file_;
156   bool gen_traces_ = false;
157   bool gen_transaction_names_ = false;
158   bool dependency_file_ninja_ = false;
159   bool structured_ = false;
160   Stability stability_ = Stability::UNSPECIFIED;
161   string output_dir_;
162   string output_header_dir_;
163   bool fail_on_parcelable_ = false;
164   bool auto_dep_file_ = false;
165   vector<string> input_files_;
166   string output_file_;
167   int version_ = 0;
168   string hash_ = "";
169   bool gen_log_ = false;
170   bool gen_parcelable_to_string_ = false;
171   ErrorMessage error_message_;
172 };
173 
174 }  // namespace aidl
175 }  // namespace android
176