blob: 064b805521dcd56c0d8de55738719759148bd593 [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
Jiyong Park8c380532018-08-30 14:55:26 +090018#include <set>
Adam Lesinskiffa16862014-01-23 18:17:42 -080019#include <string>
20#include <vector>
21
Jooyung Han74406c52021-11-06 06:17:36 +090022#include <android-base/result.h>
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 Park223274e2021-09-24 14:14:12 +090033// The oldest SDK version that is supported for each backend. For non-Java backends, these are the
Jooyung Hand11f9f52021-11-04 11:34:57 +090034// 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
36constexpr uint32_t DEFAULT_SDK_VERSION_JAVA = 1;
Jiyong Park223274e2021-09-24 14:14:12 +090037constexpr uint32_t DEFAULT_SDK_VERSION_CPP = 23;
38constexpr uint32_t DEFAULT_SDK_VERSION_NDK = 29;
39constexpr uint32_t DEFAULT_SDK_VERSION_RUST = 31;
40
Jooyung Han6edc7ec2021-12-23 11:57:49 +090041constexpr uint32_t SDK_VERSION_current = 10000;
42constexpr uint32_t SDK_VERSION_Tiramisu = SDK_VERSION_current;
43
Jiyong Park6f77e0c2018-07-28 16:55:44 +090044// A simple wrapper around ostringstream. This is just to make Options class
45// copiable by the implicit copy constructor. If ostingstream is not wrapped,
46// the implcit copy constructor is not generated because ostringstream isn't
47// copiable. This class makes the field copiable by having a copy constructor
48// that does not copy the underlying stream.
49class ErrorMessage {
50 public:
51 ErrorMessage() = default;
52 ErrorMessage(const ErrorMessage&) {}
53 std::ostringstream stream_;
Jiyong Parke1d02682018-08-01 12:18:02 +090054
55 template <typename T>
56 ErrorMessage& operator<<(T& t) {
57 stream_ << t;
Jiyong Park6f77e0c2018-07-28 16:55:44 +090058 return *this;
59 }
Jiyong Parke1d02682018-08-01 12:18:02 +090060
61 template <typename T>
62 ErrorMessage& operator<<(const T& t) {
63 stream_ << t;
Jiyong Park6f77e0c2018-07-28 16:55:44 +090064 return *this;
65 }
Jiyong Parke1d02682018-08-01 12:18:02 +090066
67 // for "<< endl"
Jiyong Park6f77e0c2018-07-28 16:55:44 +090068 ErrorMessage& operator<<(std::ostream& (*f)(std::ostream&)) {
69 f(stream_);
70 return *this;
71 }
72};
73
Jooyung Han808a2a02020-12-28 16:46:54 +090074// Handles warning-related options (e.g. -W, -w, ...)
Jooyung Han888c5bc2020-12-22 17:28:47 +090075class WarningOptions {
76 public:
77 std::vector<const char*> Parse(int argc, const char* const argv[], ErrorMessage& error_message);
Jooyung Han808a2a02020-12-28 16:46:54 +090078 DiagnosticMapping GetDiagnosticMapping() const;
Jooyung Han888c5bc2020-12-22 17:28:47 +090079
80 private:
81 bool as_errors_ = false; // -Werror
82 bool enable_all_ = false; // -Weverything
83 bool disable_all_ = false; // -w
84 std::set<std::string> enabled_; // -Wfoo
85 std::set<std::string> disabled_; // -Wno-foo
86 std::set<std::string> no_errors_; // -Wno-error=foo
Jooyung Han888c5bc2020-12-22 17:28:47 +090087};
88
Jiyong Park74595c12018-07-23 15:22:50 +090089class Options final {
Steven Morelandda0654f2018-07-17 12:24:38 -070090 public:
Andrei Homescub62afd92020-05-11 19:24:59 -070091 enum class Language { UNSPECIFIED, JAVA, CPP, NDK, RUST };
Steven Morelandda0654f2018-07-17 12:24:38 -070092
Steven Moreland481f5932021-08-09 17:31:05 -070093 enum class Task { HELP, COMPILE, PREPROCESS, DUMP_API, CHECK_API, DUMP_MAPPINGS };
Steven Morelandda0654f2018-07-17 12:24:38 -070094
Jooyung Hanb8a97772021-01-19 01:27:38 +090095 enum class CheckApiLevel { COMPATIBLE, EQUAL };
96
Steven Morelanda57d0a62019-07-30 09:41:14 -070097 enum class Stability { UNSPECIFIED, VINTF };
98 bool StabilityFromString(const std::string& stability, Stability* out_stability);
99
Jiyong Park74595c12018-07-23 15:22:50 +0900100 Options(int argc, const char* const argv[], Language default_lang = Language::UNSPECIFIED);
Steven Morelandda0654f2018-07-17 12:24:38 -0700101
Jooyung Han8e3b72c2021-05-22 02:54:37 +0900102 Options PlusImportDir(const std::string& import_dir) const {
103 Options copy(*this);
104 copy.import_dirs_.insert(import_dir);
105 return copy;
106 }
107
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900108 static Options From(const string& cmdline);
109
110 static Options From(const vector<string>& args);
111
Steven Moreland6cee3482018-07-18 14:39:58 -0700112 // Contain no references to unstructured data types (such as a parcelable that is
113 // implemented in Java). These interfaces aren't inherently stable but they have the
114 // capacity to be stabilized.
115 bool IsStructured() const { return structured_; }
116
Steven Morelanda57d0a62019-07-30 09:41:14 -0700117 Stability GetStability() const { return stability_; }
118
Jiyong Park223274e2021-09-24 14:14:12 +0900119 uint32_t GetMinSdkVersion() const { return min_sdk_version_; }
120
Jiyong Park74595c12018-07-23 15:22:50 +0900121 Language TargetLanguage() const { return language_; }
Steven Morelandc26d8142018-09-17 14:25:33 -0700122 bool IsCppOutput() const { return language_ == Language::CPP || language_ == Language::NDK; }
Steven Morelandda0654f2018-07-17 12:24:38 -0700123
Jiyong Park74595c12018-07-23 15:22:50 +0900124 Task GetTask() const { return task_; }
Steven Morelandda0654f2018-07-17 12:24:38 -0700125
Jooyung Hanb8a97772021-01-19 01:27:38 +0900126 CheckApiLevel GetCheckApiLevel() const { return check_api_level_; }
127
Jiyong Park8c380532018-08-30 14:55:26 +0900128 const set<string>& ImportDirs() const { return import_dirs_; }
Jiyong Park3c35e392018-08-30 13:10:30 +0900129
Jiyong Park74595c12018-07-23 15:22:50 +0900130 const vector<string>& PreprocessedFiles() const { return preprocessed_files_; }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800131
Jiyong Park74595c12018-07-23 15:22:50 +0900132 string DependencyFile() const {
Jiyong Park74595c12018-07-23 15:22:50 +0900133 return dependency_file_;
134 }
Christopher Wiley4427d862015-09-14 11:07:39 -0700135
Jiyong Park74595c12018-07-23 15:22:50 +0900136 bool AutoDepFile() const { return auto_dep_file_; }
Christopher Wileyef4132c2015-11-05 15:47:40 -0800137
Steven Moreland46931082022-02-04 18:24:18 +0000138 bool GenRpc() const { return gen_rpc_; }
139
Jiyong Park74595c12018-07-23 15:22:50 +0900140 bool GenTraces() const { return gen_traces_; }
Steven Morelandda0654f2018-07-17 12:24:38 -0700141
Jiyong Park74595c12018-07-23 15:22:50 +0900142 bool GenTransactionNames() const { return gen_transaction_names_; }
Christopher Wiley4427d862015-09-14 11:07:39 -0700143
Jiyong Park74595c12018-07-23 15:22:50 +0900144 bool DependencyFileNinja() const { return dependency_file_ninja_; }
145
146 const vector<string>& InputFiles() const { return input_files_; }
147
148 // Path to the output file. This is used only when there is only one
149 // output file for the invocation. When there are multiple outputs
150 // (e.g. compile multiple AIDL files), output files are created under
151 // OutputDir().
152 const string& OutputFile() const { return output_file_; }
153
154 // Path to the directory where output file(s) will be generated under.
155 const string& OutputDir() const { return output_dir_; }
156
157 // Path to the directory where header file(s) will be generated under.
158 // Only used when TargetLanguage() == Language::CPP
159 const string& OutputHeaderDir() const { return output_header_dir_; }
160
161 bool FailOnParcelable() const { return fail_on_parcelable_; }
162
Jiyong Park309668e2018-07-28 16:55:44 +0900163 int Version() const { return version_; }
164
Paul Trautrimb77048c2020-01-21 16:39:32 +0900165 string Hash() const { return hash_; }
166
Jiyong Parkce50e262018-10-29 09:54:20 +0900167 bool GenLog() const { return gen_log_; }
168
Jooyung Han252657e2021-02-27 02:51:39 +0900169 bool DumpNoLicense() const { return dump_no_license_; }
170
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900171 bool Ok() const { return error_message_.stream_.str().empty(); }
Jiyong Park74595c12018-07-23 15:22:50 +0900172
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900173 string GetErrorMessage() const { return error_message_.stream_.str(); }
Jiyong Park74595c12018-07-23 15:22:50 +0900174
175 string GetUsage() const;
Andreas Gampee9c816e2018-03-14 09:05:48 -0700176
Andrei Onea8714b022019-02-01 18:55:54 +0000177 bool GenApiMapping() const { return task_ == Task::DUMP_MAPPINGS; }
178
Jooyung Han808a2a02020-12-28 16:46:54 +0900179 DiagnosticMapping GetDiagnosticMapping() const { return warning_options_.GetDiagnosticMapping(); }
Jooyung Han888c5bc2020-12-22 17:28:47 +0900180
Steven Morelandda0654f2018-07-17 12:24:38 -0700181 // The following are for testability, but cannot be influenced on the command line.
Andreas Gampee9c816e2018-03-14 09:05:48 -0700182 // Threshold of interface methods to enable outlining of onTransact cases.
183 size_t onTransact_outline_threshold_{275u};
184 // Number of cases to _not_ outline, if outlining is enabled.
185 size_t onTransact_non_outline_count_{275u};
186
Christopher Wiley4427d862015-09-14 11:07:39 -0700187 private:
Jiyong Park74595c12018-07-23 15:22:50 +0900188 Options() = default;
Christopher Wiley4427d862015-09-14 11:07:39 -0700189
Jiyong Park74595c12018-07-23 15:22:50 +0900190 const string myname_;
191 Language language_ = Language::UNSPECIFIED;
192 Task task_ = Task::COMPILE;
Jooyung Hanb8a97772021-01-19 01:27:38 +0900193 CheckApiLevel check_api_level_ = CheckApiLevel::COMPATIBLE;
Jiyong Park8c380532018-08-30 14:55:26 +0900194 set<string> import_dirs_;
Jiyong Park74595c12018-07-23 15:22:50 +0900195 vector<string> preprocessed_files_;
196 string dependency_file_;
Steven Moreland46931082022-02-04 18:24:18 +0000197 bool gen_rpc_ = false;
Jiyong Park74595c12018-07-23 15:22:50 +0900198 bool gen_traces_ = false;
199 bool gen_transaction_names_ = false;
200 bool dependency_file_ninja_ = false;
Steven Morelanda57d0a62019-07-30 09:41:14 -0700201 bool structured_ = false;
202 Stability stability_ = Stability::UNSPECIFIED;
Jiyong Park223274e2021-09-24 14:14:12 +0900203 uint32_t min_sdk_version_ = 0; // invalid version
Jiyong Park74595c12018-07-23 15:22:50 +0900204 string output_dir_;
205 string output_header_dir_;
206 bool fail_on_parcelable_ = false;
207 bool auto_dep_file_ = false;
208 vector<string> input_files_;
209 string output_file_;
Jiyong Park309668e2018-07-28 16:55:44 +0900210 int version_ = 0;
Paul Trautrimb77048c2020-01-21 16:39:32 +0900211 string hash_ = "";
Jiyong Parkce50e262018-10-29 09:54:20 +0900212 bool gen_log_ = false;
Jooyung Han252657e2021-02-27 02:51:39 +0900213 bool dump_no_license_ = false;
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900214 ErrorMessage error_message_;
Jooyung Han888c5bc2020-12-22 17:28:47 +0900215 WarningOptions warning_options_;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800216};
217
Jooyung Han9435e9a2021-01-06 10:16:31 +0900218std::string to_string(Options::Language language);
Jooyung Han74406c52021-11-06 06:17:36 +0900219android::base::Result<uint32_t> MinSdkVersionFromString(const std::string& str);
Jooyung Han9435e9a2021-01-06 10:16:31 +0900220
Christopher Wiley4427d862015-09-14 11:07:39 -0700221} // namespace aidl
Steven Morelandf4c64df2019-07-29 19:54:04 -0700222} // namespace android