Christopher Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 1 | /* |
Christopher Wiley | b1bbdf8 | 2016-04-21 11:43:45 -0700 | [diff] [blame] | 2 | * Copyright (C) 2015, The Android Open Source Project * |
Christopher Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 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 | |
Steven Moreland | 9fccf58 | 2018-08-27 20:36:27 -0700 | [diff] [blame] | 16 | #pragma once |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 17 | |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 18 | #include <set> |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 19 | #include <string> |
| 20 | #include <vector> |
| 21 | |
Jooyung Han | 888c5bc | 2020-12-22 17:28:47 +0900 | [diff] [blame] | 22 | #include "diagnostics.h" |
| 23 | |
Christopher Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 24 | namespace android { |
| 25 | namespace aidl { |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 26 | |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 27 | using std::set; |
Jiyong Park | 1d2df7d | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 28 | using std::string; |
| 29 | using std::vector; |
| 30 | |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 31 | // 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. |
| 36 | class ErrorMessage { |
| 37 | public: |
| 38 | ErrorMessage() = default; |
| 39 | ErrorMessage(const ErrorMessage&) {} |
| 40 | std::ostringstream stream_; |
Jiyong Park | e1d0268 | 2018-08-01 12:18:02 +0900 | [diff] [blame] | 41 | |
| 42 | template <typename T> |
| 43 | ErrorMessage& operator<<(T& t) { |
| 44 | stream_ << t; |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 45 | return *this; |
| 46 | } |
Jiyong Park | e1d0268 | 2018-08-01 12:18:02 +0900 | [diff] [blame] | 47 | |
| 48 | template <typename T> |
| 49 | ErrorMessage& operator<<(const T& t) { |
| 50 | stream_ << t; |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 51 | return *this; |
| 52 | } |
Jiyong Park | e1d0268 | 2018-08-01 12:18:02 +0900 | [diff] [blame] | 53 | |
| 54 | // for "<< endl" |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 55 | ErrorMessage& operator<<(std::ostream& (*f)(std::ostream&)) { |
| 56 | f(stream_); |
| 57 | return *this; |
| 58 | } |
| 59 | }; |
| 60 | |
Jooyung Han | 808a2a0 | 2020-12-28 16:46:54 +0900 | [diff] [blame] | 61 | // Handles warning-related options (e.g. -W, -w, ...) |
Jooyung Han | 888c5bc | 2020-12-22 17:28:47 +0900 | [diff] [blame] | 62 | class WarningOptions { |
| 63 | public: |
| 64 | std::vector<const char*> Parse(int argc, const char* const argv[], ErrorMessage& error_message); |
Jooyung Han | 808a2a0 | 2020-12-28 16:46:54 +0900 | [diff] [blame] | 65 | DiagnosticMapping GetDiagnosticMapping() const; |
Jooyung Han | 888c5bc | 2020-12-22 17:28:47 +0900 | [diff] [blame] | 66 | |
| 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 Han | 888c5bc | 2020-12-22 17:28:47 +0900 | [diff] [blame] | 74 | }; |
| 75 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 76 | class Options final { |
Steven Moreland | da0654f | 2018-07-17 12:24:38 -0700 | [diff] [blame] | 77 | public: |
Andrei Homescu | b62afd9 | 2020-05-11 19:24:59 -0700 | [diff] [blame] | 78 | enum class Language { UNSPECIFIED, JAVA, CPP, NDK, RUST }; |
Steven Moreland | da0654f | 2018-07-17 12:24:38 -0700 | [diff] [blame] | 79 | |
Andrei Onea | 8714b02 | 2019-02-01 18:55:54 +0000 | [diff] [blame] | 80 | enum class Task { UNSPECIFIED, COMPILE, PREPROCESS, DUMP_API, CHECK_API, DUMP_MAPPINGS }; |
Steven Moreland | da0654f | 2018-07-17 12:24:38 -0700 | [diff] [blame] | 81 | |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 82 | enum class Stability { UNSPECIFIED, VINTF }; |
| 83 | bool StabilityFromString(const std::string& stability, Stability* out_stability); |
| 84 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 85 | Options(int argc, const char* const argv[], Language default_lang = Language::UNSPECIFIED); |
Steven Moreland | da0654f | 2018-07-17 12:24:38 -0700 | [diff] [blame] | 86 | |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 87 | static Options From(const string& cmdline); |
| 88 | |
| 89 | static Options From(const vector<string>& args); |
| 90 | |
Steven Moreland | 6cee348 | 2018-07-18 14:39:58 -0700 | [diff] [blame] | 91 | // Contain no references to unstructured data types (such as a parcelable that is |
| 92 | // implemented in Java). These interfaces aren't inherently stable but they have the |
| 93 | // capacity to be stabilized. |
| 94 | bool IsStructured() const { return structured_; } |
| 95 | |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 96 | Stability GetStability() const { return stability_; } |
| 97 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 98 | Language TargetLanguage() const { return language_; } |
Steven Moreland | c26d814 | 2018-09-17 14:25:33 -0700 | [diff] [blame] | 99 | bool IsCppOutput() const { return language_ == Language::CPP || language_ == Language::NDK; } |
Steven Moreland | da0654f | 2018-07-17 12:24:38 -0700 | [diff] [blame] | 100 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 101 | Task GetTask() const { return task_; } |
Steven Moreland | da0654f | 2018-07-17 12:24:38 -0700 | [diff] [blame] | 102 | |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 103 | const set<string>& ImportDirs() const { return import_dirs_; } |
Jiyong Park | 3c35e39 | 2018-08-30 13:10:30 +0900 | [diff] [blame] | 104 | |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 105 | const set<string>& ImportFiles() const { return import_files_; } |
Steven Moreland | da0654f | 2018-07-17 12:24:38 -0700 | [diff] [blame] | 106 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 107 | const vector<string>& PreprocessedFiles() const { return preprocessed_files_; } |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 108 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 109 | string DependencyFile() const { |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 110 | return dependency_file_; |
| 111 | } |
Christopher Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 112 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 113 | bool AutoDepFile() const { return auto_dep_file_; } |
Christopher Wiley | ef4132c | 2015-11-05 15:47:40 -0800 | [diff] [blame] | 114 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 115 | bool GenTraces() const { return gen_traces_; } |
Steven Moreland | da0654f | 2018-07-17 12:24:38 -0700 | [diff] [blame] | 116 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 117 | bool GenTransactionNames() const { return gen_transaction_names_; } |
Christopher Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 118 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 119 | bool DependencyFileNinja() const { return dependency_file_ninja_; } |
| 120 | |
| 121 | const vector<string>& InputFiles() const { return input_files_; } |
| 122 | |
| 123 | // Path to the output file. This is used only when there is only one |
| 124 | // output file for the invocation. When there are multiple outputs |
| 125 | // (e.g. compile multiple AIDL files), output files are created under |
| 126 | // OutputDir(). |
| 127 | const string& OutputFile() const { return output_file_; } |
| 128 | |
| 129 | // Path to the directory where output file(s) will be generated under. |
| 130 | const string& OutputDir() const { return output_dir_; } |
| 131 | |
| 132 | // Path to the directory where header file(s) will be generated under. |
| 133 | // Only used when TargetLanguage() == Language::CPP |
| 134 | const string& OutputHeaderDir() const { return output_header_dir_; } |
| 135 | |
| 136 | bool FailOnParcelable() const { return fail_on_parcelable_; } |
| 137 | |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 138 | int Version() const { return version_; } |
| 139 | |
Paul Trautrim | b77048c | 2020-01-21 16:39:32 +0900 | [diff] [blame] | 140 | string Hash() const { return hash_; } |
| 141 | |
Jiyong Park | ce50e26 | 2018-10-29 09:54:20 +0900 | [diff] [blame] | 142 | bool GenLog() const { return gen_log_; } |
| 143 | |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 144 | bool Ok() const { return error_message_.stream_.str().empty(); } |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 145 | |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 146 | string GetErrorMessage() const { return error_message_.stream_.str(); } |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 147 | |
| 148 | string GetUsage() const; |
Andreas Gampe | e9c816e | 2018-03-14 09:05:48 -0700 | [diff] [blame] | 149 | |
Andrei Onea | 8714b02 | 2019-02-01 18:55:54 +0000 | [diff] [blame] | 150 | bool GenApiMapping() const { return task_ == Task::DUMP_MAPPINGS; } |
| 151 | |
Jooyung Han | 808a2a0 | 2020-12-28 16:46:54 +0900 | [diff] [blame] | 152 | DiagnosticMapping GetDiagnosticMapping() const { return warning_options_.GetDiagnosticMapping(); } |
Jooyung Han | 888c5bc | 2020-12-22 17:28:47 +0900 | [diff] [blame] | 153 | |
Steven Moreland | da0654f | 2018-07-17 12:24:38 -0700 | [diff] [blame] | 154 | // The following are for testability, but cannot be influenced on the command line. |
Andreas Gampe | e9c816e | 2018-03-14 09:05:48 -0700 | [diff] [blame] | 155 | // Threshold of interface methods to enable outlining of onTransact cases. |
| 156 | size_t onTransact_outline_threshold_{275u}; |
| 157 | // Number of cases to _not_ outline, if outlining is enabled. |
| 158 | size_t onTransact_non_outline_count_{275u}; |
| 159 | |
Christopher Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 160 | private: |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 161 | Options() = default; |
Christopher Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 162 | |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 163 | const string myname_; |
| 164 | Language language_ = Language::UNSPECIFIED; |
| 165 | Task task_ = Task::COMPILE; |
Jiyong Park | 8c38053 | 2018-08-30 14:55:26 +0900 | [diff] [blame] | 166 | set<string> import_dirs_; |
| 167 | set<string> import_files_; |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 168 | vector<string> preprocessed_files_; |
| 169 | string dependency_file_; |
| 170 | bool gen_traces_ = false; |
| 171 | bool gen_transaction_names_ = false; |
| 172 | bool dependency_file_ninja_ = false; |
Steven Moreland | a57d0a6 | 2019-07-30 09:41:14 -0700 | [diff] [blame] | 173 | bool structured_ = false; |
| 174 | Stability stability_ = Stability::UNSPECIFIED; |
Jiyong Park | 74595c1 | 2018-07-23 15:22:50 +0900 | [diff] [blame] | 175 | string output_dir_; |
| 176 | string output_header_dir_; |
| 177 | bool fail_on_parcelable_ = false; |
| 178 | bool auto_dep_file_ = false; |
| 179 | vector<string> input_files_; |
| 180 | string output_file_; |
Jiyong Park | 309668e | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 181 | int version_ = 0; |
Paul Trautrim | b77048c | 2020-01-21 16:39:32 +0900 | [diff] [blame] | 182 | string hash_ = ""; |
Jiyong Park | ce50e26 | 2018-10-29 09:54:20 +0900 | [diff] [blame] | 183 | bool gen_log_ = false; |
Jiyong Park | 6f77e0c | 2018-07-28 16:55:44 +0900 | [diff] [blame] | 184 | ErrorMessage error_message_; |
Jooyung Han | 888c5bc | 2020-12-22 17:28:47 +0900 | [diff] [blame] | 185 | WarningOptions warning_options_; |
Adam Lesinski | ffa1686 | 2014-01-23 18:17:42 -0800 | [diff] [blame] | 186 | }; |
| 187 | |
Jooyung Han | 9435e9a | 2021-01-06 10:16:31 +0900 | [diff] [blame^] | 188 | std::string to_string(Options::Language language); |
| 189 | |
Christopher Wiley | 4427d86 | 2015-09-14 11:07:39 -0700 | [diff] [blame] | 190 | } // namespace aidl |
Steven Moreland | f4c64df | 2019-07-29 19:54:04 -0700 | [diff] [blame] | 191 | } // namespace android |