1/* 2 * Copyright (c) 2021-2024 Huawei Device Co., Ltd. 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// Autogenerated file -- DO NOT EDIT! 16 17#ifndef PANDA_<%= Common::module.name.upcase %>_OPTIONS_GEN_H 18#define PANDA_<%= Common::module.name.upcase %>_OPTIONS_GEN_H 19 20#include "utils/pandargs.h" 21#include "platforms/target_defaults/default_target_options.h" 22#include "macros.h" 23 24#include <optional> 25#include <string> 26#include <unordered_set> 27#include <vector> 28 29namespace <%= Common::module.namespace %> { 30// NOLINTBEGIN(readability-identifier-naming) 31class PANDA_PUBLIC_API Options { 32public: 33 class Error { 34 public: 35 explicit Error(std::string msg) : msg_(std::move(msg)) {} 36 37 std::string GetMessage() { 38 return msg_; 39 } 40 private: 41 std::string msg_; 42 }; 43 44 explicit Options(const std::string &exe_path) : 45 exe_dir_(GetExeDir(exe_path)) 46% Common::options.each do |op| 47% next if op.sub_option? 48% next if op.deprecated? 49% if (op.default_value.is_a?(Hash)) 50 ,<%= op.field_name %>{"<%= op.name %>", ark::default_target_options::<%= op.getter_name %>({<% op.default_value.each_with_index do |(key, value), index| %> { "<%= key %>", <%= value.is_a?(Integer) ? value : "\"#{value}\"" %>}<%= "," unless index == op.default_value.size - 1 %> <% end %>}) , <%= op.full_description %>} 51% end 52% end 53 {} 54 55 // NOLINTBEGIN(readability-function-size) 56 void AddOptions(PandArgParser *parser) { 57 parser->Add(&version_); 58% Common::options.each do |op| 59% next if op.sub_option? 60% next if op.deprecated? 61 parser->Add(&<%= op.field_name %>); 62% end 63 } 64 // NOLINTEND(readability-function-size) 65 66 bool IsVersion() const { 67 return version_.GetValue(); 68 } 69 70 void SetVersion(bool value) { 71 version_.SetValue(value); 72 } 73 74 bool WasSetVersion() { 75 return version_.WasSet(); 76 } 77 78% Common::options.each do |op| 79% if !op.lang_specific? && !op.has_lang_suboptions? 80 <%= op.type %> <%= op.getter_name %>([[maybe_unused]] std::string_view lang = "") const { 81% if op.deprecated? 82 std::cerr << "WARNING: Option '<%= op.name %>' is deprecated and should not be used\n"; 83% end 84 return <%= op.field_name %>.GetValue(); 85 } 86 87 void <%= op.setter_name %>(<%= op.type %> value) { 88% if op.deprecated? 89 std::cerr << "WARNING: Option '<%= op.name %>' is deprecated and should not be used\n"; 90% end 91 <%= op.field_name %>.SetValue(<%= op.type == 'std::string' || op.type == 'arg_list_t' ? 'std::move(value)' : 'value' %>); 92 } 93 94 bool WasSet<%= op.camel_name %>([[maybe_unused]] std::string_view lang = "") const { 95% if op.deprecated? 96 std::cerr << "WARNING: Option '<%= op.name %>' is deprecated and should not be used\n"; 97% end 98 return <%= op.field_name %>.WasSet(); 99 } 100% end 101% if op.has_lang_suboptions? 102 <%= op.type %> <%= op.getter_name %>(std::string_view lang) const { 103% if op.deprecated? 104 std::cerr << "WARNING: Option '<%= op.name %>' is deprecated and should not be used\n"; 105% end 106% op.lang.each do |lang| 107 if (lang == "<%= "#{lang}" %>") { 108 return WasSet<%= op.lang_camel_name(lang) %>() ? <%= op.lang_field_name(lang) %>.GetValue() : <%= op.field_name %>.GetValue(); 109 } 110% end 111 return <%= op.field_name %>.GetValue(); 112 } 113 114 void <%= op.setter_name %>(<%= op.type %> value) { 115% if op.deprecated? 116 std::cerr << "WARNING: Option '<%= op.name %>' is deprecated and should not be used\n"; 117% end 118 <%= op.field_name %>.SetValue(<%= op.type == 'std::string' || op.type == 'arg_list_t' ? 'std::move(value)' : 'value' %>); 119 } 120 121 bool WasSet<%= op.camel_name %>(std::string_view lang) const { 122% if op.deprecated? 123 std::cerr << "WARNING: Option '<%= op.name %>' is deprecated and should not be used\n"; 124% end 125% op.lang.each do |lang| 126 if (lang == "<%= "#{lang}" %>") { 127 return <%= op.lang_field_name(lang) %>.WasSet(); 128 } 129% end 130 return <%= op.field_name %>.WasSet(); 131 } 132% end 133% end 134// NOLINTNEXTLINE(readability-function-size) 135 std::optional<Error> Validate() const { 136% Common::options.each do |op| 137% next unless defined? op.possible_values 138% is_string = op.type == 'std::string' || op.type == 'arg_list_t' 139% possible_values = op.possible_values.map { |e| is_string ? Common::to_raw(e) : e }.join(', ') 140 { 141 std::unordered_set<<%= is_string ? "std::string" : op.type %>> possible_values{<%= possible_values %>}; 142% if op.type != 'arg_list_t' 143 std::vector<<%= op.type %>> values{<%= op.field_name %>.GetValue()}; 144% else 145 const auto &values = <%= op.field_name %>.GetValue(); 146% end 147 for (const auto &value : values) { 148 if (possible_values.find(value) == possible_values.cend()) { 149 return Error("argument --<%= op.name %>: invalid value: '" + <%= is_string ? "value" : "std::to_string(value)" %> + \ 150 R"('. Possible values: <%= op.possible_values %>)"); 151 } 152 } 153 } 154% end 155 return {}; 156 } 157 158private: 159% Common::options.each do |op| 160% if op.lang_specific? 161 bool WasSet<%= op.camel_name %>() const { 162% if op.deprecated? 163 std::cerr << "WARNING: Option '<%= op.name %>' is deprecated and should not be used\n"; 164% end 165 return <%= op.field_name %>.WasSet(); 166 } 167% end 168% end 169 170 static std::string GetExeDir(const std::string &exe_path) { 171 auto pos = exe_path.find_last_of('/'); 172 return exe_path.substr(0, pos); 173 } 174 175% Common::options.each do |op| 176% next if (op.default_value.is_a?(Hash)) 177% next unless op.need_default_constant 178 static constexpr <%= op.type %> <%= op.default_constant_name %> = <%= op.default %>; 179 180% end 181 std::string exe_dir_; 182 PandArg<bool> version_{"version", false, R"(Ark version, file format version and minimum supported file format version)"}; 183% Common::options.each do |op| 184% if (op.default_value.is_a?(Hash)) 185 PandArg<<%= op.type %>> <%= op.field_name %>; 186% elsif defined? op.delimiter 187 PandArg<<%= op.type %>> <%= op.field_name %>{"<%= op.name %>", <%= op.default_value %>, <%= op.full_description %>, "<%= op.delimiter %>"}; 188% elsif defined? op.range 189% min, max = op.range.match('(\d+)\D*(\d+)').captures; 190% if op.default.to_i > max.to_i || op.default.to_i < min.to_i 191% abort "FAILED: Default value of argument " + op.name + " has out of range default parameter" 192% end 193 PandArg<<%= op.type %>> <%= op.field_name %>{"<%= op.name %>", <%= op.default_value %>, <%= op.full_description %>, <%= min.to_i %>, <%= max.to_i %>}; 194% elsif op.has_sub_options 195 PandArgCompound <%= op.field_name %>{"<%= op.name %>", <%= op.full_description %>, {<%= op.sub_options.map{|o| '&' + o.field_name}.join(', ') %>}}; 196% else 197 PandArg<<%= op.type %>> <%= op.field_name %>{"<%= op.name %>", <%= op.default_value %>, <%= op.full_description %>}; 198% end 199% end 200}; 201// NOLINTEND(readability-identifier-naming) 202} // namespace <%= Common::module.namespace %> 203 204#endif // PANDA_<%= Common::module.name.upcase %>_OPTIONS_GEN_H 205