blob: 5bff57dd9db2fd9edadf3c17c96051edf070c12f [file] [log] [blame]
Christopher Wiley4427d862015-09-14 11:07:39 -07001/*
Christopher Wileyb1bbdf82016-04-21 11:43:45 -07002 * Copyright (C) 2015, The Android Open Source Project *
Christopher Wiley4427d862015-09-14 11:07:39 -07003 * 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
Christopher Wiley89e35862015-08-30 10:57:07 -070016#ifndef AIDL_OPTIONS_H_
17#define AIDL_OPTIONS_H_
Adam Lesinskiffa16862014-01-23 18:17:42 -080018
Jiyong Park74595c12018-07-23 15:22:50 +090019#include <sstream>
Adam Lesinskiffa16862014-01-23 18:17:42 -080020#include <string>
21#include <vector>
22
Christopher Wiley4427d862015-09-14 11:07:39 -070023namespace android {
24namespace aidl {
Adam Lesinskiffa16862014-01-23 18:17:42 -080025
Jiyong Park1d2df7d2018-07-23 15:22:50 +090026using std::string;
27using std::vector;
28
Jiyong Park6f77e0c2018-07-28 16:55:44 +090029// A simple wrapper around ostringstream. This is just to make Options class
30// copiable by the implicit copy constructor. If ostingstream is not wrapped,
31// the implcit copy constructor is not generated because ostringstream isn't
32// copiable. This class makes the field copiable by having a copy constructor
33// that does not copy the underlying stream.
34class ErrorMessage {
35 public:
36 ErrorMessage() = default;
37 ErrorMessage(const ErrorMessage&) {}
38 std::ostringstream stream_;
Jiyong Parke1d02682018-08-01 12:18:02 +090039
40 template <typename T>
41 ErrorMessage& operator<<(T& t) {
42 stream_ << t;
Jiyong Park6f77e0c2018-07-28 16:55:44 +090043 return *this;
44 }
Jiyong Parke1d02682018-08-01 12:18:02 +090045
46 template <typename T>
47 ErrorMessage& operator<<(const T& t) {
48 stream_ << t;
Jiyong Park6f77e0c2018-07-28 16:55:44 +090049 return *this;
50 }
Jiyong Parke1d02682018-08-01 12:18:02 +090051
52 // for "<< endl"
Jiyong Park6f77e0c2018-07-28 16:55:44 +090053 ErrorMessage& operator<<(std::ostream& (*f)(std::ostream&)) {
54 f(stream_);
55 return *this;
56 }
57};
58
Jiyong Park74595c12018-07-23 15:22:50 +090059class Options final {
Steven Morelandda0654f2018-07-17 12:24:38 -070060 public:
Jiyong Park74595c12018-07-23 15:22:50 +090061 enum class Language { UNSPECIFIED, JAVA, CPP };
Steven Morelandda0654f2018-07-17 12:24:38 -070062
Jiyong Park3656c3c2018-08-01 20:02:01 +090063 enum class Task { UNSPECIFIED, COMPILE, PREPROCESS, DUMP_API, CHECK_API };
Steven Morelandda0654f2018-07-17 12:24:38 -070064
Jiyong Park74595c12018-07-23 15:22:50 +090065 Options(int argc, const char* const argv[], Language default_lang = Language::UNSPECIFIED);
Steven Morelandda0654f2018-07-17 12:24:38 -070066
Jiyong Park6f77e0c2018-07-28 16:55:44 +090067 static Options From(const string& cmdline);
68
69 static Options From(const vector<string>& args);
70
Steven Moreland6cee3482018-07-18 14:39:58 -070071 // Contain no references to unstructured data types (such as a parcelable that is
72 // implemented in Java). These interfaces aren't inherently stable but they have the
73 // capacity to be stabilized.
74 bool IsStructured() const { return structured_; }
75
Jiyong Park74595c12018-07-23 15:22:50 +090076 Language TargetLanguage() const { return language_; }
Steven Morelandda0654f2018-07-17 12:24:38 -070077
Jiyong Park74595c12018-07-23 15:22:50 +090078 Task GetTask() const { return task_; }
Steven Morelandda0654f2018-07-17 12:24:38 -070079
Jiyong Park74595c12018-07-23 15:22:50 +090080 const vector<string>& ImportPaths() const { return import_paths_; }
Steven Morelandda0654f2018-07-17 12:24:38 -070081
Jiyong Park74595c12018-07-23 15:22:50 +090082 const vector<string>& PreprocessedFiles() const { return preprocessed_files_; }
Adam Lesinskiffa16862014-01-23 18:17:42 -080083
Jiyong Park74595c12018-07-23 15:22:50 +090084 string DependencyFile() const {
Jiyong Park74595c12018-07-23 15:22:50 +090085 return dependency_file_;
86 }
Christopher Wiley4427d862015-09-14 11:07:39 -070087
Jiyong Park74595c12018-07-23 15:22:50 +090088 bool AutoDepFile() const { return auto_dep_file_; }
Christopher Wileyef4132c2015-11-05 15:47:40 -080089
Jiyong Park74595c12018-07-23 15:22:50 +090090 bool GenTraces() const { return gen_traces_; }
Steven Morelandda0654f2018-07-17 12:24:38 -070091
Jiyong Park74595c12018-07-23 15:22:50 +090092 bool GenTransactionNames() const { return gen_transaction_names_; }
Christopher Wiley4427d862015-09-14 11:07:39 -070093
Jiyong Park74595c12018-07-23 15:22:50 +090094 bool DependencyFileNinja() const { return dependency_file_ninja_; }
95
96 const vector<string>& InputFiles() const { return input_files_; }
97
98 // Path to the output file. This is used only when there is only one
99 // output file for the invocation. When there are multiple outputs
100 // (e.g. compile multiple AIDL files), output files are created under
101 // OutputDir().
102 const string& OutputFile() const { return output_file_; }
103
104 // Path to the directory where output file(s) will be generated under.
105 const string& OutputDir() const { return output_dir_; }
106
107 // Path to the directory where header file(s) will be generated under.
108 // Only used when TargetLanguage() == Language::CPP
109 const string& OutputHeaderDir() const { return output_header_dir_; }
110
111 bool FailOnParcelable() const { return fail_on_parcelable_; }
112
Jiyong Park309668e2018-07-28 16:55:44 +0900113 int Version() const { return version_; }
114
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900115 bool Ok() const { return error_message_.stream_.str().empty(); }
Jiyong Park74595c12018-07-23 15:22:50 +0900116
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900117 string GetErrorMessage() const { return error_message_.stream_.str(); }
Jiyong Park74595c12018-07-23 15:22:50 +0900118
119 string GetUsage() const;
Andreas Gampee9c816e2018-03-14 09:05:48 -0700120
Steven Morelandda0654f2018-07-17 12:24:38 -0700121 // The following are for testability, but cannot be influenced on the command line.
Andreas Gampee9c816e2018-03-14 09:05:48 -0700122 // Threshold of interface methods to enable outlining of onTransact cases.
123 size_t onTransact_outline_threshold_{275u};
124 // Number of cases to _not_ outline, if outlining is enabled.
125 size_t onTransact_non_outline_count_{275u};
126
Christopher Wiley4427d862015-09-14 11:07:39 -0700127 private:
Jiyong Park74595c12018-07-23 15:22:50 +0900128 Options() = default;
Christopher Wiley4427d862015-09-14 11:07:39 -0700129
Jiyong Park74595c12018-07-23 15:22:50 +0900130 const string myname_;
Steven Moreland6cee3482018-07-18 14:39:58 -0700131 bool structured_ = false;
Jiyong Park74595c12018-07-23 15:22:50 +0900132 Language language_ = Language::UNSPECIFIED;
133 Task task_ = Task::COMPILE;
134 vector<string> import_paths_;
135 vector<string> preprocessed_files_;
136 string dependency_file_;
137 bool gen_traces_ = false;
138 bool gen_transaction_names_ = false;
139 bool dependency_file_ninja_ = false;
140 string output_dir_;
141 string output_header_dir_;
142 bool fail_on_parcelable_ = false;
143 bool auto_dep_file_ = false;
144 vector<string> input_files_;
145 string output_file_;
Jiyong Park309668e2018-07-28 16:55:44 +0900146 int version_ = 0;
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900147 ErrorMessage error_message_;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800148};
149
Christopher Wiley4427d862015-09-14 11:07:39 -0700150} // namespace android
151} // namespace aidl
Adam Lesinskiffa16862014-01-23 18:17:42 -0800152
Christopher Wiley89e35862015-08-30 10:57:07 -0700153#endif // AIDL_OPTIONS_H_