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