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 #include "assembly-type.h"
17 #include "assembly-program.h"
18 #include "assembly-emitter.h"
19 #include "json/json.h"
20 #include "ts2abc_options.h"
21 #include "ts2abc.h"
22
Preprocess(const panda::ts2abc::Options & options,const panda::PandArgParser & argParser,std::string & output,std::string & data,const std::string & usage)23 int Preprocess(const panda::ts2abc::Options &options, const panda::PandArgParser &argParser, std::string &output,
24 std::string &data, const std::string &usage)
25 {
26 std::string input;
27 if (!options.GetCompileByPipeArg()) {
28 input = options.GetTailArg1();
29 output = options.GetTailArg2();
30 if (input.empty() || output.empty()) {
31 std::cerr << "Incorrect args number" << std::endl;
32 std::cerr << "Usage example: ts2abc test.json test.abc"<< std::endl;
33 std::cerr << usage << std::endl;
34 std::cerr << argParser.GetHelpString();
35 return panda::ts2abc::RETURN_FAILED;
36 }
37
38 if (!panda::ts2abc::HandleJsonFile(input, data)) {
39 return panda::ts2abc::RETURN_FAILED;
40 }
41 } else {
42 output = options.GetTailArg1();
43 if (output.empty()) {
44 std::cerr << usage << std::endl;
45 std::cerr << argParser.GetHelpString();
46 return panda::ts2abc::RETURN_FAILED;
47 }
48 }
49 return panda::ts2abc::RETURN_SUCCESS;
50 }
51
HandleNpmEntries(const panda::ts2abc::Options & options,const panda::PandArgParser & argParser,const std::string & usage)52 bool HandleNpmEntries(const panda::ts2abc::Options &options, const panda::PandArgParser &argParser,
53 const std::string &usage)
54 {
55 std::string input = options.GetTailArg1();
56 std::string output = options.GetTailArg2();
57 if (options.GetCompileByPipeArg() || input.empty() || output.empty()) {
58 if (options.GetCompileByPipeArg()) {
59 std::cerr << "[compile-npm-entries] and [compile-by-pipe] can not be used simultaneously" << std::endl;
60 } else {
61 std::cerr << "Incorrect args number" << std::endl;
62 }
63 std::cerr << "Usage example: js2abc --compile-npm-entries npm_entries.txt npm_entries.abc"<< std::endl;
64 std::cerr << usage << std::endl;
65 std::cerr << argParser.GetHelpString();
66 return false;
67 }
68
69 if (!panda::ts2abc::CompileNpmEntries(input, output)) {
70 return false;
71 }
72
73 return true;
74 }
75
main(int argc,const char * argv[])76 int main(int argc, const char *argv[])
77 {
78 panda::PandArgParser argParser;
79 panda::Span<const char *> sp(argv, argc);
80 panda::ts2abc::Options options(sp[0]);
81 options.AddOptions(&argParser);
82
83 if (!argParser.Parse(argc, argv)) {
84 std::cerr << argParser.GetErrorString();
85 std::cerr << argParser.GetHelpString();
86 return panda::ts2abc::RETURN_FAILED;
87 }
88
89 std::string usage = "Usage: js2abc [OPTIONS]... [ARGS]...";
90 if (options.GetHelpArg()) {
91 std::cout << usage << std::endl;
92 std::cout << argParser.GetHelpString();
93 return panda::ts2abc::RETURN_SUCCESS;
94 }
95
96 if (options.GetBcVersionArg() || options.GetBcMinVersionArg()) {
97 std::string version = options.GetBcVersionArg() ? panda::panda_file::GetVersion(panda::panda_file::version) :
98 panda::panda_file::GetVersion(panda::panda_file::minVersion);
99 std::cout << version << std::endl;
100 return panda::ts2abc::RETURN_SUCCESS;
101 }
102
103 if (options.GetCompileNpmEntries()) {
104 if (!HandleNpmEntries(options, argParser, usage)) {
105 return panda::ts2abc::RETURN_FAILED;
106 }
107 return panda::ts2abc::RETURN_SUCCESS;
108 }
109
110
111 if ((options.GetOptLevelArg() < static_cast<int>(panda::ts2abc::OptLevel::O_LEVEL0)) ||
112 (options.GetOptLevelArg() > static_cast<int>(panda::ts2abc::OptLevel::O_LEVEL2))) {
113 std::cerr << "Incorrect optimization level value" << std::endl;
114 std::cerr << usage << std::endl;
115 std::cerr << argParser.GetHelpString();
116 return panda::ts2abc::RETURN_FAILED;
117 }
118
119 std::string optLogLevel(options.GetOptLogLevelArg());
120
121 if (options.IsMultiProgramsPipe()) {
122 if (!panda::ts2abc::GenerateProgramsFromPipe(options)) {
123 std::cerr << "call GenerateProgramsFromPipe fail" << std::endl;
124 return panda::ts2abc::RETURN_FAILED;
125 }
126 return panda::ts2abc::RETURN_SUCCESS;
127 }
128
129 std::string output;
130 std::string data = "";
131
132 if (Preprocess(options, argParser, output, data, usage) == panda::ts2abc::RETURN_FAILED) {
133 return panda::ts2abc::RETURN_FAILED;
134 }
135
136 if (!panda::ts2abc::GenerateProgram(data, output, options)) {
137 std::cerr << "call GenerateProgram fail" << std::endl;
138 return panda::ts2abc::RETURN_FAILED;
139 }
140
141 return panda::ts2abc::RETURN_SUCCESS;
142 }
143