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