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 Options(int argc, const char* const argv[], Language default_lang = Language::UNSPECIFIED); 67 68 static Options From(const string& cmdline); 69 70 static Options From(const vector<string>& args); 71 72 // Contain no references to unstructured data types (such as a parcelable that is 73 // implemented in Java). These interfaces aren't inherently stable but they have the 74 // capacity to be stabilized. IsStructured()75 bool IsStructured() const { return structured_; } 76 TargetLanguage()77 Language TargetLanguage() const { return language_; } IsCppOutput()78 bool IsCppOutput() const { return language_ == Language::CPP || language_ == Language::NDK; } 79 GetTask()80 Task GetTask() const { return task_; } 81 ImportDirs()82 const set<string>& ImportDirs() const { return import_dirs_; } 83 ImportFiles()84 const set<string>& ImportFiles() const { return import_files_; } 85 PreprocessedFiles()86 const vector<string>& PreprocessedFiles() const { return preprocessed_files_; } 87 DependencyFile()88 string DependencyFile() const { 89 return dependency_file_; 90 } 91 AutoDepFile()92 bool AutoDepFile() const { return auto_dep_file_; } 93 GenTraces()94 bool GenTraces() const { return gen_traces_; } 95 GenTransactionNames()96 bool GenTransactionNames() const { return gen_transaction_names_; } 97 DependencyFileNinja()98 bool DependencyFileNinja() const { return dependency_file_ninja_; } 99 InputFiles()100 const vector<string>& InputFiles() const { return input_files_; } 101 102 // Path to the output file. This is used only when there is only one 103 // output file for the invocation. When there are multiple outputs 104 // (e.g. compile multiple AIDL files), output files are created under 105 // OutputDir(). OutputFile()106 const string& OutputFile() const { return output_file_; } 107 108 // Path to the directory where output file(s) will be generated under. OutputDir()109 const string& OutputDir() const { return output_dir_; } 110 111 // Path to the directory where header file(s) will be generated under. 112 // Only used when TargetLanguage() == Language::CPP OutputHeaderDir()113 const string& OutputHeaderDir() const { return output_header_dir_; } 114 FailOnParcelable()115 bool FailOnParcelable() const { return fail_on_parcelable_; } 116 Version()117 int Version() const { return version_; } 118 GenLog()119 bool GenLog() const { return gen_log_; } 120 Ok()121 bool Ok() const { return error_message_.stream_.str().empty(); } 122 GetErrorMessage()123 string GetErrorMessage() const { return error_message_.stream_.str(); } 124 125 string GetUsage() const; 126 GenApiMapping()127 bool GenApiMapping() const { return task_ == Task::DUMP_MAPPINGS; } 128 129 // The following are for testability, but cannot be influenced on the command line. 130 // Threshold of interface methods to enable outlining of onTransact cases. 131 size_t onTransact_outline_threshold_{275u}; 132 // Number of cases to _not_ outline, if outlining is enabled. 133 size_t onTransact_non_outline_count_{275u}; 134 135 private: 136 Options() = default; 137 138 const string myname_; 139 bool structured_ = false; 140 Language language_ = Language::UNSPECIFIED; 141 Task task_ = Task::COMPILE; 142 set<string> import_dirs_; 143 set<string> import_files_; 144 vector<string> preprocessed_files_; 145 string dependency_file_; 146 bool gen_traces_ = false; 147 bool gen_transaction_names_ = false; 148 bool dependency_file_ninja_ = false; 149 string output_dir_; 150 string output_header_dir_; 151 bool fail_on_parcelable_ = false; 152 bool auto_dep_file_ = false; 153 vector<string> input_files_; 154 string output_file_; 155 int version_ = 0; 156 bool gen_log_ = false; 157 ErrorMessage error_message_; 158 }; 159 160 } // namespace android 161 } // namespace aidl 162