1 /* 2 * Copyright (c) 2022 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 16 #ifndef TS2PANDA_TS2ABC_TS2ABC_OPTIONS_H_ 17 #define TS2PANDA_TS2ABC_TS2ABC_OPTIONS_H_ 18 19 #include "utils/pandargs.h" 20 21 #include <optional> 22 #include <string> 23 #include <unordered_set> 24 #include <vector> 25 26 namespace panda::ts2abc { 27 class Options { 28 public: Options(const std::string & exePath)29 explicit Options(const std::string &exePath) : exe_dir_(GetExeDir(exePath)) {} 30 31 ~Options() = default; 32 AddOptions(PandArgParser * parser)33 void AddOptions(PandArgParser *parser) 34 { 35 parser->Add(&size_stat_arg_); 36 parser->Add(&help_arg_); 37 parser->Add(&opt_level_arg_); 38 parser->Add(&opt_log_level_arg_); 39 parser->Add(&bc_version_arg_); 40 parser->Add(&bc_min_version_arg_); 41 parser->Add(&compile_by_pipe_arg_); 42 parser->Add(&compile_npm_entries_); 43 parser->Add(&compiler_output_proto_); 44 parser->Add(&multi_programs_pipe_); 45 parser->EnableTail(); 46 parser->PushBackTail(&Tail_Arg1_arg_); 47 parser->PushBackTail(&Tail_Arg2_arg_); 48 } 49 GetSizeStatArg()50 bool GetSizeStatArg() const 51 { 52 return size_stat_arg_.GetValue(); 53 } 54 SetSizeStatArg(bool value)55 void SetSizeStatArg(bool value) 56 { 57 size_stat_arg_.SetValue(value); 58 } 59 WasSetSizeStatArg()60 bool WasSetSizeStatArg() const 61 { 62 return size_stat_arg_.WasSet(); 63 } 64 GetHelpArg()65 bool GetHelpArg() const 66 { 67 return help_arg_.GetValue(); 68 } 69 SetHelpArg(bool value)70 void SetHelpArg(bool value) 71 { 72 help_arg_.SetValue(value); 73 } 74 WasSetHelpArg()75 bool WasSetHelpArg() const 76 { 77 return help_arg_.WasSet(); 78 } 79 GetOptLevelArg()80 int GetOptLevelArg() const 81 { 82 return opt_level_arg_.GetValue(); 83 } 84 SetOptLevelArg(int value)85 void SetOptLevelArg(int value) 86 { 87 opt_level_arg_.SetValue(value); 88 } 89 WasSetOptLevelArg()90 bool WasSetOptLevelArg() const 91 { 92 return opt_level_arg_.WasSet(); 93 } 94 GetOptLogLevelArg()95 std::string GetOptLogLevelArg() const 96 { 97 return opt_log_level_arg_.GetValue(); 98 } 99 SetOptLogLevelArg(std::string value)100 void SetOptLogLevelArg(std::string value) 101 { 102 opt_log_level_arg_.SetValue(value); 103 } 104 WasSetOptLogLevelArg()105 bool WasSetOptLogLevelArg() const 106 { 107 return opt_log_level_arg_.WasSet(); 108 } 109 GetBcVersionArg()110 bool GetBcVersionArg() const 111 { 112 return bc_version_arg_.GetValue(); 113 } 114 SetSetBcVersionArg(bool value)115 void SetSetBcVersionArg(bool value) 116 { 117 bc_version_arg_.SetValue(value); 118 } 119 WasBcVersionArg()120 bool WasBcVersionArg() const 121 { 122 return bc_version_arg_.WasSet(); 123 } 124 GetBcMinVersionArg()125 bool GetBcMinVersionArg() const 126 { 127 return bc_min_version_arg_.GetValue(); 128 } 129 SetBcMinVersionArg(bool value)130 void SetBcMinVersionArg(bool value) 131 { 132 bc_min_version_arg_.SetValue(value); 133 } 134 WasSetBcMinVersionArg()135 bool WasSetBcMinVersionArg() const 136 { 137 return bc_min_version_arg_.WasSet(); 138 } 139 GetCompileByPipeArg()140 bool GetCompileByPipeArg() const 141 { 142 return compile_by_pipe_arg_.GetValue(); 143 } 144 SetCompileByPipeArg(bool value)145 void SetCompileByPipeArg(bool value) 146 { 147 compile_by_pipe_arg_.SetValue(value); 148 } 149 WasSetCompileByPipeArg()150 bool WasSetCompileByPipeArg() const 151 { 152 return compile_by_pipe_arg_.WasSet(); 153 } 154 GetCompileNpmEntries()155 bool GetCompileNpmEntries() const 156 { 157 return compile_npm_entries_.GetValue(); 158 } 159 SetCompileNpmEntries(bool value)160 void SetCompileNpmEntries(bool value) 161 { 162 compile_npm_entries_.SetValue(value); 163 } 164 WasSetCompileNpmEntries()165 bool WasSetCompileNpmEntries() const 166 { 167 return compile_npm_entries_.WasSet(); 168 } 169 IsOutputProto()170 bool IsOutputProto() const 171 { 172 return compiler_output_proto_.GetValue(); 173 } 174 SetOutputProto(bool value)175 void SetOutputProto(bool value) 176 { 177 compiler_output_proto_.SetValue(value); 178 } 179 WasSetOutputProto()180 bool WasSetOutputProto() const 181 { 182 return compiler_output_proto_.WasSet(); 183 } 184 IsMultiProgramsPipe()185 bool IsMultiProgramsPipe() const 186 { 187 return multi_programs_pipe_.GetValue(); 188 } 189 GetTailArg1()190 std::string GetTailArg1() const 191 { 192 return Tail_Arg1_arg_.GetValue(); 193 } 194 SetTailArg1(std::string value)195 void SetTailArg1(std::string value) 196 { 197 Tail_Arg1_arg_.SetValue(value); 198 } 199 WasSetTailArg1()200 bool WasSetTailArg1() const 201 { 202 return Tail_Arg1_arg_.WasSet(); 203 } 204 GetTailArg2()205 std::string GetTailArg2() const 206 { 207 return Tail_Arg2_arg_.GetValue(); 208 } 209 SetTailArg2(std::string value)210 void SetTailArg2(std::string value) 211 { 212 Tail_Arg2_arg_.SetValue(value); 213 } 214 WasSetTailArg2()215 bool WasSetTailArg2() const 216 { 217 return Tail_Arg2_arg_.WasSet(); 218 } 219 220 private: GetExeDir(const std::string & exePath)221 static std::string GetExeDir(const std::string &exePath) 222 { 223 auto pos = exePath.find_last_of('/'); 224 return exePath.substr(0, pos); 225 } 226 227 std::string exe_dir_; 228 panda::PandArg<bool> size_stat_arg_{ "size-stat", false, 229 R"(Print panda file size statistic)"}; 230 panda::PandArg<bool> help_arg_{ "help", false, 231 R"(Print this message and exit)"}; 232 panda::PandArg<int> opt_level_arg_{ "opt-level", 0, 233 R"("Optimization level. Possible values: [0, 1, 2]. Default: 0\n" 234 "0: no optimizations\n " 235 "1: basic bytecode optimizations, including valueNumber," 236 "lowering, constantResolver, regAccAllocator\n " 237 "2: (experimental optimizations): Sta/Lda Peephole, " 238 "Movi/Lda Peephole, Register Coalescing")"}; 239 panda::PandArg<std::string> opt_log_level_arg_{ "opt-log-level", "error", 240 R"(Optimization log level. Possible values: " 241 "['error', 'debug', 'info', 'fatal']. Default: 'error' )"}; 242 panda::PandArg<bool> bc_version_arg_{ "bc-version", false, 243 R"(Print ark bytecode version)"}; 244 panda::PandArg<bool> bc_min_version_arg_{ "bc-min-version", false, 245 R"(Print ark bytecode minimum supported version)"}; 246 panda::PandArg<bool> compile_by_pipe_arg_{ "compile-by-pipe", false, 247 R"(Compile a json file that is passed by pipe)"}; 248 panda::PandArg<bool> compile_npm_entries_{ "compile-npm-entries", false, 249 R"(Compile npm entries info into an abc file)"}; 250 panda::PandArg<bool> compiler_output_proto_{ "output-proto", false, 251 R"(Output protoBin file)"}; 252 panda::PandArg<bool> multi_programs_pipe_{ "multi-programs-pipe", false, 253 R"(Genrate programs by single pipe)"}; 254 panda::PandArg<std::string> Tail_Arg1_arg_{ "ARG_1", "", 255 R"(Path to input(json file) or path to output(ark bytecode)" 256 " when 'compile-by-pipe' enabled)"}; 257 panda::PandArg<std::string> Tail_Arg2_arg_{ "ARG_2", "", 258 R"(Path to output(ark bytecode) or ignore when 'compile-by-pipe' enabled)"}; 259 }; 260 } // namespace panda::ts2abc 261 262 #endif // TS2PANDA_TS2ABC_TS2ABC_OPTIONS_H_ 263