blob: 08f0fdf695b0091d529e3ab79d3b875820d5e2cc [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
Steven Moreland9fccf582018-08-27 20:36:27 -070016#pragma once
Adam Lesinskiffa16862014-01-23 18:17:42 -080017
Jooyung Han888c5bc2020-12-22 17:28:47 +090018#include <map>
Jiyong Park8c380532018-08-30 14:55:26 +090019#include <set>
Jiyong Park74595c12018-07-23 15:22:50 +090020#include <sstream>
Adam Lesinskiffa16862014-01-23 18:17:42 -080021#include <string>
22#include <vector>
23
Jooyung Han888c5bc2020-12-22 17:28:47 +090024#include "diagnostics.h"
25
Christopher Wiley4427d862015-09-14 11:07:39 -070026namespace android {
27namespace aidl {
Adam Lesinskiffa16862014-01-23 18:17:42 -080028
Jiyong Park8c380532018-08-30 14:55:26 +090029using std::set;
Jiyong Park1d2df7d2018-07-23 15:22:50 +090030using std::string;
31using std::vector;
32
Jiyong Park6f77e0c2018-07-28 16:55:44 +090033// A simple wrapper around ostringstream. This is just to make Options class
34// copiable by the implicit copy constructor. If ostingstream is not wrapped,
35// the implcit copy constructor is not generated because ostringstream isn't
36// copiable. This class makes the field copiable by having a copy constructor
37// that does not copy the underlying stream.
38class ErrorMessage {
39 public:
40 ErrorMessage() = default;
41 ErrorMessage(const ErrorMessage&) {}
42 std::ostringstream stream_;
Jiyong Parke1d02682018-08-01 12:18:02 +090043
44 template <typename T>
45 ErrorMessage& operator<<(T& t) {
46 stream_ << t;
Jiyong Park6f77e0c2018-07-28 16:55:44 +090047 return *this;
48 }
Jiyong Parke1d02682018-08-01 12:18:02 +090049
50 template <typename T>
51 ErrorMessage& operator<<(const T& t) {
52 stream_ << t;
Jiyong Park6f77e0c2018-07-28 16:55:44 +090053 return *this;
54 }
Jiyong Parke1d02682018-08-01 12:18:02 +090055
56 // for "<< endl"
Jiyong Park6f77e0c2018-07-28 16:55:44 +090057 ErrorMessage& operator<<(std::ostream& (*f)(std::ostream&)) {
58 f(stream_);
59 return *this;
60 }
61};
62
Jooyung Han888c5bc2020-12-22 17:28:47 +090063class WarningOptions {
64 public:
65 std::vector<const char*> Parse(int argc, const char* const argv[], ErrorMessage& error_message);
66 DiagnosticSeverity Severity(DiagnosticID id) const;
67
68 private:
69 bool as_errors_ = false; // -Werror
70 bool enable_all_ = false; // -Weverything
71 bool disable_all_ = false; // -w
72 std::set<std::string> enabled_; // -Wfoo
73 std::set<std::string> disabled_; // -Wno-foo
74 std::set<std::string> no_errors_; // -Wno-error=foo
75
76 struct Mapping {
77 std::string name;
78 DiagnosticSeverity severity;
79 };
80 std::map<DiagnosticID, Mapping> mapping_;
81};
82
Jiyong Park74595c12018-07-23 15:22:50 +090083class Options final {
Steven Morelandda0654f2018-07-17 12:24:38 -070084 public:
Andrei Homescub62afd92020-05-11 19:24:59 -070085 enum class Language { UNSPECIFIED, JAVA, CPP, NDK, RUST };
Steven Morelandda0654f2018-07-17 12:24:38 -070086
Andrei Onea8714b022019-02-01 18:55:54 +000087 enum class Task { UNSPECIFIED, COMPILE, PREPROCESS, DUMP_API, CHECK_API, DUMP_MAPPINGS };
Steven Morelandda0654f2018-07-17 12:24:38 -070088
Steven Morelanda57d0a62019-07-30 09:41:14 -070089 enum class Stability { UNSPECIFIED, VINTF };
90 bool StabilityFromString(const std::string& stability, Stability* out_stability);
91
Jiyong Park74595c12018-07-23 15:22:50 +090092 Options(int argc, const char* const argv[], Language default_lang = Language::UNSPECIFIED);
Steven Morelandda0654f2018-07-17 12:24:38 -070093
Jiyong Park6f77e0c2018-07-28 16:55:44 +090094 static Options From(const string& cmdline);
95
96 static Options From(const vector<string>& args);
97
Steven Moreland6cee3482018-07-18 14:39:58 -070098 // Contain no references to unstructured data types (such as a parcelable that is
99 // implemented in Java). These interfaces aren't inherently stable but they have the
100 // capacity to be stabilized.
101 bool IsStructured() const { return structured_; }
102
Steven Morelanda57d0a62019-07-30 09:41:14 -0700103 Stability GetStability() const { return stability_; }
104
Jiyong Park74595c12018-07-23 15:22:50 +0900105 Language TargetLanguage() const { return language_; }
Steven Morelandc26d8142018-09-17 14:25:33 -0700106 bool IsCppOutput() const { return language_ == Language::CPP || language_ == Language::NDK; }
Steven Morelandda0654f2018-07-17 12:24:38 -0700107
Jiyong Park74595c12018-07-23 15:22:50 +0900108 Task GetTask() const { return task_; }
Steven Morelandda0654f2018-07-17 12:24:38 -0700109
Jiyong Park8c380532018-08-30 14:55:26 +0900110 const set<string>& ImportDirs() const { return import_dirs_; }
Jiyong Park3c35e392018-08-30 13:10:30 +0900111
Jiyong Park8c380532018-08-30 14:55:26 +0900112 const set<string>& ImportFiles() const { return import_files_; }
Steven Morelandda0654f2018-07-17 12:24:38 -0700113
Jiyong Park74595c12018-07-23 15:22:50 +0900114 const vector<string>& PreprocessedFiles() const { return preprocessed_files_; }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800115
Jiyong Park74595c12018-07-23 15:22:50 +0900116 string DependencyFile() const {
Jiyong Park74595c12018-07-23 15:22:50 +0900117 return dependency_file_;
118 }
Christopher Wiley4427d862015-09-14 11:07:39 -0700119
Jiyong Park74595c12018-07-23 15:22:50 +0900120 bool AutoDepFile() const { return auto_dep_file_; }
Christopher Wileyef4132c2015-11-05 15:47:40 -0800121
Jiyong Park74595c12018-07-23 15:22:50 +0900122 bool GenTraces() const { return gen_traces_; }
Steven Morelandda0654f2018-07-17 12:24:38 -0700123
Jiyong Park74595c12018-07-23 15:22:50 +0900124 bool GenTransactionNames() const { return gen_transaction_names_; }
Christopher Wiley4427d862015-09-14 11:07:39 -0700125
Jiyong Park74595c12018-07-23 15:22:50 +0900126 bool DependencyFileNinja() const { return dependency_file_ninja_; }
127
128 const vector<string>& InputFiles() const { return input_files_; }
129
130 // Path to the output file. This is used only when there is only one
131 // output file for the invocation. When there are multiple outputs
132 // (e.g. compile multiple AIDL files), output files are created under
133 // OutputDir().
134 const string& OutputFile() const { return output_file_; }
135
136 // Path to the directory where output file(s) will be generated under.
137 const string& OutputDir() const { return output_dir_; }
138
139 // Path to the directory where header file(s) will be generated under.
140 // Only used when TargetLanguage() == Language::CPP
141 const string& OutputHeaderDir() const { return output_header_dir_; }
142
143 bool FailOnParcelable() const { return fail_on_parcelable_; }
144
Jiyong Park309668e2018-07-28 16:55:44 +0900145 int Version() const { return version_; }
146
Paul Trautrimb77048c2020-01-21 16:39:32 +0900147 string Hash() const { return hash_; }
148
Jiyong Parkce50e262018-10-29 09:54:20 +0900149 bool GenLog() const { return gen_log_; }
150
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900151 bool Ok() const { return error_message_.stream_.str().empty(); }
Jiyong Park74595c12018-07-23 15:22:50 +0900152
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900153 string GetErrorMessage() const { return error_message_.stream_.str(); }
Jiyong Park74595c12018-07-23 15:22:50 +0900154
155 string GetUsage() const;
Andreas Gampee9c816e2018-03-14 09:05:48 -0700156
Andrei Onea8714b022019-02-01 18:55:54 +0000157 bool GenApiMapping() const { return task_ == Task::DUMP_MAPPINGS; }
158
Devin Moore7b8d5c92020-03-17 14:14:08 -0700159 static const string LanguageToString(Language language);
160
Jooyung Han888c5bc2020-12-22 17:28:47 +0900161 const WarningOptions& GetWarningOptions() const { return warning_options_; }
162
Steven Morelandda0654f2018-07-17 12:24:38 -0700163 // The following are for testability, but cannot be influenced on the command line.
Andreas Gampee9c816e2018-03-14 09:05:48 -0700164 // Threshold of interface methods to enable outlining of onTransact cases.
165 size_t onTransact_outline_threshold_{275u};
166 // Number of cases to _not_ outline, if outlining is enabled.
167 size_t onTransact_non_outline_count_{275u};
168
Christopher Wiley4427d862015-09-14 11:07:39 -0700169 private:
Jiyong Park74595c12018-07-23 15:22:50 +0900170 Options() = default;
Christopher Wiley4427d862015-09-14 11:07:39 -0700171
Jiyong Park74595c12018-07-23 15:22:50 +0900172 const string myname_;
173 Language language_ = Language::UNSPECIFIED;
174 Task task_ = Task::COMPILE;
Jiyong Park8c380532018-08-30 14:55:26 +0900175 set<string> import_dirs_;
176 set<string> import_files_;
Jiyong Park74595c12018-07-23 15:22:50 +0900177 vector<string> preprocessed_files_;
178 string dependency_file_;
179 bool gen_traces_ = false;
180 bool gen_transaction_names_ = false;
181 bool dependency_file_ninja_ = false;
Steven Morelanda57d0a62019-07-30 09:41:14 -0700182 bool structured_ = false;
183 Stability stability_ = Stability::UNSPECIFIED;
Jiyong Park74595c12018-07-23 15:22:50 +0900184 string output_dir_;
185 string output_header_dir_;
186 bool fail_on_parcelable_ = false;
187 bool auto_dep_file_ = false;
188 vector<string> input_files_;
189 string output_file_;
Jiyong Park309668e2018-07-28 16:55:44 +0900190 int version_ = 0;
Paul Trautrimb77048c2020-01-21 16:39:32 +0900191 string hash_ = "";
Jiyong Parkce50e262018-10-29 09:54:20 +0900192 bool gen_log_ = false;
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900193 ErrorMessage error_message_;
Jooyung Han888c5bc2020-12-22 17:28:47 +0900194 WarningOptions warning_options_;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800195};
196
Christopher Wiley4427d862015-09-14 11:07:39 -0700197} // namespace aidl
Steven Morelandf4c64df2019-07-29 19:54:04 -0700198} // namespace android