blob: 11a5e59db1254fbca0d3418e25bce4c8d0e92424 [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 Han888c5bc2020-12-22 17:28:47 +090022#include "diagnostics.h"
23
Christopher Wiley4427d862015-09-14 11:07:39 -070024namespace android {
25namespace aidl {
Adam Lesinskiffa16862014-01-23 18:17:42 -080026
Jiyong Park8c380532018-08-30 14:55:26 +090027using std::set;
Jiyong Park1d2df7d2018-07-23 15:22:50 +090028using std::string;
29using std::vector;
30
Jiyong Park6f77e0c2018-07-28 16:55:44 +090031// A simple wrapper around ostringstream. This is just to make Options class
32// copiable by the implicit copy constructor. If ostingstream is not wrapped,
33// the implcit copy constructor is not generated because ostringstream isn't
34// copiable. This class makes the field copiable by having a copy constructor
35// that does not copy the underlying stream.
36class ErrorMessage {
37 public:
38 ErrorMessage() = default;
39 ErrorMessage(const ErrorMessage&) {}
40 std::ostringstream stream_;
Jiyong Parke1d02682018-08-01 12:18:02 +090041
42 template <typename T>
43 ErrorMessage& operator<<(T& t) {
44 stream_ << t;
Jiyong Park6f77e0c2018-07-28 16:55:44 +090045 return *this;
46 }
Jiyong Parke1d02682018-08-01 12:18:02 +090047
48 template <typename T>
49 ErrorMessage& operator<<(const T& t) {
50 stream_ << t;
Jiyong Park6f77e0c2018-07-28 16:55:44 +090051 return *this;
52 }
Jiyong Parke1d02682018-08-01 12:18:02 +090053
54 // for "<< endl"
Jiyong Park6f77e0c2018-07-28 16:55:44 +090055 ErrorMessage& operator<<(std::ostream& (*f)(std::ostream&)) {
56 f(stream_);
57 return *this;
58 }
59};
60
Jooyung Han808a2a02020-12-28 16:46:54 +090061// Handles warning-related options (e.g. -W, -w, ...)
Jooyung Han888c5bc2020-12-22 17:28:47 +090062class WarningOptions {
63 public:
64 std::vector<const char*> Parse(int argc, const char* const argv[], ErrorMessage& error_message);
Jooyung Han808a2a02020-12-28 16:46:54 +090065 DiagnosticMapping GetDiagnosticMapping() const;
Jooyung Han888c5bc2020-12-22 17:28:47 +090066
67 private:
68 bool as_errors_ = false; // -Werror
69 bool enable_all_ = false; // -Weverything
70 bool disable_all_ = false; // -w
71 std::set<std::string> enabled_; // -Wfoo
72 std::set<std::string> disabled_; // -Wno-foo
73 std::set<std::string> no_errors_; // -Wno-error=foo
Jooyung Han888c5bc2020-12-22 17:28:47 +090074};
75
Jiyong Park74595c12018-07-23 15:22:50 +090076class Options final {
Steven Morelandda0654f2018-07-17 12:24:38 -070077 public:
Andrei Homescub62afd92020-05-11 19:24:59 -070078 enum class Language { UNSPECIFIED, JAVA, CPP, NDK, RUST };
Steven Morelandda0654f2018-07-17 12:24:38 -070079
Andrei Onea8714b022019-02-01 18:55:54 +000080 enum class Task { UNSPECIFIED, COMPILE, PREPROCESS, DUMP_API, CHECK_API, DUMP_MAPPINGS };
Steven Morelandda0654f2018-07-17 12:24:38 -070081
Jooyung Hanb8a97772021-01-19 01:27:38 +090082 enum class CheckApiLevel { COMPATIBLE, EQUAL };
83
Steven Morelanda57d0a62019-07-30 09:41:14 -070084 enum class Stability { UNSPECIFIED, VINTF };
85 bool StabilityFromString(const std::string& stability, Stability* out_stability);
86
Jiyong Park74595c12018-07-23 15:22:50 +090087 Options(int argc, const char* const argv[], Language default_lang = Language::UNSPECIFIED);
Steven Morelandda0654f2018-07-17 12:24:38 -070088
Jiyong Park6f77e0c2018-07-28 16:55:44 +090089 static Options From(const string& cmdline);
90
91 static Options From(const vector<string>& args);
92
Steven Moreland6cee3482018-07-18 14:39:58 -070093 // Contain no references to unstructured data types (such as a parcelable that is
94 // implemented in Java). These interfaces aren't inherently stable but they have the
95 // capacity to be stabilized.
96 bool IsStructured() const { return structured_; }
97
Steven Morelanda57d0a62019-07-30 09:41:14 -070098 Stability GetStability() const { return stability_; }
99
Jiyong Park74595c12018-07-23 15:22:50 +0900100 Language TargetLanguage() const { return language_; }
Steven Morelandc26d8142018-09-17 14:25:33 -0700101 bool IsCppOutput() const { return language_ == Language::CPP || language_ == Language::NDK; }
Steven Morelandda0654f2018-07-17 12:24:38 -0700102
Jiyong Park74595c12018-07-23 15:22:50 +0900103 Task GetTask() const { return task_; }
Steven Morelandda0654f2018-07-17 12:24:38 -0700104
Jooyung Hanb8a97772021-01-19 01:27:38 +0900105 CheckApiLevel GetCheckApiLevel() const { return check_api_level_; }
106
Jiyong Park8c380532018-08-30 14:55:26 +0900107 const set<string>& ImportDirs() const { return import_dirs_; }
Jiyong Park3c35e392018-08-30 13:10:30 +0900108
Jiyong Park8c380532018-08-30 14:55:26 +0900109 const set<string>& ImportFiles() const { return import_files_; }
Steven Morelandda0654f2018-07-17 12:24:38 -0700110
Jiyong Park74595c12018-07-23 15:22:50 +0900111 const vector<string>& PreprocessedFiles() const { return preprocessed_files_; }
Adam Lesinskiffa16862014-01-23 18:17:42 -0800112
Jiyong Park74595c12018-07-23 15:22:50 +0900113 string DependencyFile() const {
Jiyong Park74595c12018-07-23 15:22:50 +0900114 return dependency_file_;
115 }
Christopher Wiley4427d862015-09-14 11:07:39 -0700116
Jiyong Park74595c12018-07-23 15:22:50 +0900117 bool AutoDepFile() const { return auto_dep_file_; }
Christopher Wileyef4132c2015-11-05 15:47:40 -0800118
Jiyong Park74595c12018-07-23 15:22:50 +0900119 bool GenTraces() const { return gen_traces_; }
Steven Morelandda0654f2018-07-17 12:24:38 -0700120
Jiyong Park74595c12018-07-23 15:22:50 +0900121 bool GenTransactionNames() const { return gen_transaction_names_; }
Christopher Wiley4427d862015-09-14 11:07:39 -0700122
Jiyong Park74595c12018-07-23 15:22:50 +0900123 bool DependencyFileNinja() const { return dependency_file_ninja_; }
124
125 const vector<string>& InputFiles() const { return input_files_; }
126
127 // Path to the output file. This is used only when there is only one
128 // output file for the invocation. When there are multiple outputs
129 // (e.g. compile multiple AIDL files), output files are created under
130 // OutputDir().
131 const string& OutputFile() const { return output_file_; }
132
133 // Path to the directory where output file(s) will be generated under.
134 const string& OutputDir() const { return output_dir_; }
135
136 // Path to the directory where header file(s) will be generated under.
137 // Only used when TargetLanguage() == Language::CPP
138 const string& OutputHeaderDir() const { return output_header_dir_; }
139
140 bool FailOnParcelable() const { return fail_on_parcelable_; }
141
Jiyong Park309668e2018-07-28 16:55:44 +0900142 int Version() const { return version_; }
143
Paul Trautrimb77048c2020-01-21 16:39:32 +0900144 string Hash() const { return hash_; }
145
Jiyong Parkce50e262018-10-29 09:54:20 +0900146 bool GenLog() const { return gen_log_; }
147
Jooyung Han252657e2021-02-27 02:51:39 +0900148 bool DumpNoLicense() const { return dump_no_license_; }
149
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900150 bool Ok() const { return error_message_.stream_.str().empty(); }
Jiyong Park74595c12018-07-23 15:22:50 +0900151
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900152 string GetErrorMessage() const { return error_message_.stream_.str(); }
Jiyong Park74595c12018-07-23 15:22:50 +0900153
154 string GetUsage() const;
Andreas Gampee9c816e2018-03-14 09:05:48 -0700155
Andrei Onea8714b022019-02-01 18:55:54 +0000156 bool GenApiMapping() const { return task_ == Task::DUMP_MAPPINGS; }
157
Jooyung Han808a2a02020-12-28 16:46:54 +0900158 DiagnosticMapping GetDiagnosticMapping() const { return warning_options_.GetDiagnosticMapping(); }
Jooyung Han888c5bc2020-12-22 17:28:47 +0900159
Steven Morelandda0654f2018-07-17 12:24:38 -0700160 // The following are for testability, but cannot be influenced on the command line.
Andreas Gampee9c816e2018-03-14 09:05:48 -0700161 // Threshold of interface methods to enable outlining of onTransact cases.
162 size_t onTransact_outline_threshold_{275u};
163 // Number of cases to _not_ outline, if outlining is enabled.
164 size_t onTransact_non_outline_count_{275u};
165
Christopher Wiley4427d862015-09-14 11:07:39 -0700166 private:
Jiyong Park74595c12018-07-23 15:22:50 +0900167 Options() = default;
Christopher Wiley4427d862015-09-14 11:07:39 -0700168
Jiyong Park74595c12018-07-23 15:22:50 +0900169 const string myname_;
170 Language language_ = Language::UNSPECIFIED;
171 Task task_ = Task::COMPILE;
Jooyung Hanb8a97772021-01-19 01:27:38 +0900172 CheckApiLevel check_api_level_ = CheckApiLevel::COMPATIBLE;
Jiyong Park8c380532018-08-30 14:55:26 +0900173 set<string> import_dirs_;
174 set<string> import_files_;
Jiyong Park74595c12018-07-23 15:22:50 +0900175 vector<string> preprocessed_files_;
176 string dependency_file_;
177 bool gen_traces_ = false;
178 bool gen_transaction_names_ = false;
179 bool dependency_file_ninja_ = false;
Steven Morelanda57d0a62019-07-30 09:41:14 -0700180 bool structured_ = false;
181 Stability stability_ = Stability::UNSPECIFIED;
Jiyong Park74595c12018-07-23 15:22:50 +0900182 string output_dir_;
183 string output_header_dir_;
184 bool fail_on_parcelable_ = false;
185 bool auto_dep_file_ = false;
186 vector<string> input_files_;
187 string output_file_;
Jiyong Park309668e2018-07-28 16:55:44 +0900188 int version_ = 0;
Paul Trautrimb77048c2020-01-21 16:39:32 +0900189 string hash_ = "";
Jiyong Parkce50e262018-10-29 09:54:20 +0900190 bool gen_log_ = false;
Jooyung Han252657e2021-02-27 02:51:39 +0900191 bool dump_no_license_ = false;
Jiyong Park6f77e0c2018-07-28 16:55:44 +0900192 ErrorMessage error_message_;
Jooyung Han888c5bc2020-12-22 17:28:47 +0900193 WarningOptions warning_options_;
Adam Lesinskiffa16862014-01-23 18:17:42 -0800194};
195
Jooyung Han9435e9a2021-01-06 10:16:31 +0900196std::string to_string(Options::Language language);
197
Christopher Wiley4427d862015-09-14 11:07:39 -0700198} // namespace aidl
Steven Morelandf4c64df2019-07-29 19:54:04 -0700199} // namespace android