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