1 /** 2 * Copyright (c) 2021-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 ES2PANDA_AOT_OPTIONS_H 17 #define ES2PANDA_AOT_OPTIONS_H 18 19 #include <es2panda.h> 20 #include <macros.h> 21 #include <parser/program/program.h> 22 23 #include <exception> 24 #include <fstream> 25 #include <iostream> 26 #include <util/base64.h> 27 28 namespace panda { 29 class PandArgParser; 30 class PandaArg; 31 } // namespace panda 32 33 namespace panda::es2panda::aot { 34 enum class OptionFlags { 35 DEFAULT = 0, 36 PARSE_ONLY = 1 << 1, 37 SIZE_STAT = 1 << 2, 38 }; 39 40 inline std::underlying_type_t<OptionFlags> operator&(OptionFlags a, OptionFlags b) 41 { 42 using utype = std::underlying_type_t<OptionFlags>; 43 /* NOLINTNEXTLINE(hicpp-signed-bitwise) */ 44 return static_cast<utype>(static_cast<utype>(a) & static_cast<utype>(b)); 45 } 46 47 inline OptionFlags &operator|=(OptionFlags &a, OptionFlags b) 48 { 49 using utype = std::underlying_type_t<OptionFlags>; 50 /* NOLINTNEXTLINE(hicpp-signed-bitwise) */ 51 return a = static_cast<OptionFlags>(static_cast<utype>(a) | static_cast<utype>(b)); 52 } 53 54 class Options { 55 public: 56 Options(); 57 NO_COPY_SEMANTIC(Options); 58 NO_MOVE_SEMANTIC(Options); 59 ~Options(); 60 61 bool Parse(int argc, const char **argv); 62 CompilerOptions()63 const es2panda::CompilerOptions &CompilerOptions() const 64 { 65 return compilerOptions_; 66 } 67 CompilerOptions()68 es2panda::CompilerOptions &CompilerOptions() 69 { 70 return compilerOptions_; 71 } 72 ScriptKind()73 es2panda::parser::ScriptKind ScriptKind() const 74 { 75 return scriptKind_; 76 } 77 CompilerOutput()78 const std::string &CompilerOutput() const 79 { 80 return compilerOutput_; 81 } 82 SourceFile()83 const std::string &SourceFile() const 84 { 85 return sourceFile_; 86 } 87 RecordName()88 const std::string &RecordName() const 89 { 90 return recordName_; 91 } 92 ErrorMsg()93 const std::string &ErrorMsg() const 94 { 95 return errorMsg_; 96 } 97 OptLevel()98 int OptLevel() const 99 { 100 return optLevel_; 101 } 102 ParseOnly()103 bool ParseOnly() const 104 { 105 return (options_ & OptionFlags::PARSE_ONLY) != 0; 106 } 107 SizeStat()108 bool SizeStat() const 109 { 110 return (options_ & OptionFlags::SIZE_STAT) != 0; 111 } 112 113 std::string ExtractContentFromBase64Input(const std::string &inputBase64String); 114 compilerProtoOutput()115 const std::string &compilerProtoOutput() const 116 { 117 return compilerProtoOutput_; 118 } 119 NpmModuleEntryList()120 const std::string &NpmModuleEntryList() const 121 { 122 return npmModuleEntryList_; 123 } 124 OutputFiles()125 const std::unordered_map<std::string, std::string> &OutputFiles() const 126 { 127 return outputFiles_; 128 } 129 130 bool CollectInputFilesFromFileList(const std::string &input, const std::string &inputExtension); 131 bool CollectInputFilesFromFileDirectory(const std::string &input, const std::string &extension); 132 void ParseCacheFileOption(const std::string &cacheInput); 133 134 private: 135 es2panda::CompilerOptions compilerOptions_ {}; 136 es2panda::parser::ScriptKind scriptKind_ {es2panda::parser::ScriptKind::SCRIPT}; 137 OptionFlags options_ {OptionFlags::DEFAULT}; 138 panda::PandArgParser *argparser_; 139 std::string base64Input_; 140 std::string compilerOutput_; 141 std::string result_; 142 std::string sourceFile_; 143 std::string recordName_; 144 std::string errorMsg_; 145 std::string compilerProtoOutput_; 146 int optLevel_ {0}; 147 int functionThreadCount_ {0}; 148 int fileThreadCount_ {0}; 149 std::string npmModuleEntryList_; 150 std::vector<es2panda::SourceFile> sourceFiles_; 151 std::unordered_map<std::string, std::string> outputFiles_; 152 }; 153 } // namespace panda::es2panda::aot 154 155 #endif // AOT_OPTIONS_H 156