• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  *
4  * HDF is dual licensed: you can use it either under the terms of
5  * the GPL, or the BSD license, at your option.
6  * See the LICENSE file in the root of this repository for complete details.
7  */
8 
9 #include <cstring>
10 #include <getopt.h>
11 #include <iomanip>
12 #include <iostream>
13 
14 #include "file.h"
15 #include "logger.h"
16 #include "option.h"
17 
18 using namespace OHOS::Hardware;
19 static constexpr int HCS_COMPILER_VERSION_MAJOR = 00;
20 static constexpr int HCS_COMPILER_VERSION_MINOR = 7;
21 static constexpr int ARG_COUNT_MIN = 2;
22 static constexpr int SPACE_WIDTH = 12;
23 
Instance()24 Option &Option::Instance()
25 {
26     static Option option;
27     return option;
28 }
29 
30 static constexpr int OPTION_END = -1;
31 static constexpr const char *HCS_SUPPORT_ARGS = "o:ap:bditmvVhs";
32 
Parse(int argc,char ** argv)33 Option &Option::Parse(int argc, char **argv)
34 {
35     do {
36         if (argc < ARG_COUNT_MIN) {
37             showUsage_ = true;
38             break;
39         }
40 
41         if (!ParseOptions(argc, argv)) {
42             break;
43         }
44 
45         if (optind >= argc) {
46             Logger().Error() << "Miss input file name";
47             SetOptionError();
48             break;
49         }
50         SetSourceOption(argv[optind]);
51         return *this;
52     } while (false);
53 
54     return *this;
55 }
56 
ParseOptions(int argc,char ** argv)57 bool Option::ParseOptions(int argc, char **argv)
58 {
59     int32_t op = 0;
60     while (op != OPTION_END) {
61         op = getopt(argc, argv, HCS_SUPPORT_ARGS);
62         SetOptionData(op);
63         if (ShouldShowUsage() || ShouldShowVersion()) {
64             return false;
65         }
66     }
67 
68     return true;
69 }
70 
SetOptionData(char op)71 void Option::SetOptionData(char op)
72 {
73     switch (op) {
74         case 'o':
75             outputName_ = optarg;
76             break;
77         case 'a':
78             shouldAlign_ = true;
79             break;
80         case 'b':
81             shouldGenByteCodeConfig_ = true;
82             break;
83         case 't':
84             shouldGenTextConfig_ = true;
85             shouldGenByteCodeConfig_ = false;
86             shouldGenMacroConfig_ = false;
87             break;
88         case 'm':
89             shouldGenTextConfig_ = false;
90             shouldGenByteCodeConfig_ = false;
91             shouldGenMacroConfig_ = true;
92             break;
93         case 'p':
94             symbolNamePrefix_ = optarg;
95             break;
96         case 'i':
97             showGenHexDump_ = true;
98             break;
99         case 'V':
100             verboseLog_ = true;
101             break;
102         case 'd':
103             shouldDecompile_ = true;
104             break;
105         case 'v':
106             showVersion_ = true;
107             break;
108         case 'h': /* fall-through */
109             showUsage_ = true;
110             break;
111         case 's':
112             genStartCfg_ = true;
113             shouldGenByteCodeConfig_ = false;
114             break;
115         case '?':
116             SetOptionError();
117             break;
118         default:
119             break;
120     }
121 }
122 
ShowUsage()123 void Option::ShowUsage()
124 {
125     Logger() << "Usage: hc-gen [Options] [File]\n"
126              << "options:";
127     ShowOption("-o <file>", "output file name, default same as input");
128     ShowOption("-a", "hcb align with four bytes");
129     ShowOption("-b", "output binary output, default enable");
130     ShowOption("-t", "output config in C language source file style");
131     ShowOption("-m", "output config in macro file style");
132     ShowOption("-s", "output start config of host");
133     ShowOption("-i", "output binary hex dump in C language source file style");
134     ShowOption("-p <prefix>", "prefix of generated symbol name");
135     ShowOption("-d", "decompile hcb to hcs");
136     ShowOption("-V", "show verbose info");
137     ShowOption("-v", "show version");
138     ShowOption("-h", "show this help message");
139 }
140 
ShowOption(const::std::string & option,const::std::string & helpInfo)141 void Option::ShowOption(const ::std::string &option, const ::std::string &helpInfo)
142 {
143     Logger() << "  " << ::std::setw(SPACE_WIDTH) << ::std::left << option << "  " << helpInfo;
144 }
145 
ShouldShowUsage() const146 bool Option::ShouldShowUsage() const
147 {
148     return showUsage_;
149 }
150 
ShowVersion()151 void Option::ShowVersion()
152 {
153     Logger() << "Hcs compiler " << HCS_COMPILER_VERSION_MAJOR << "." << HCS_COMPILER_VERSION_MINOR;
154     Logger() << "Copyright (c) 2020-2021 Huawei Device Co., Ltd.";
155 }
156 
OptionError() const157 bool Option::OptionError() const
158 {
159     return optionError_;
160 }
161 
ShouldShowVersion() const162 bool Option::ShouldShowVersion() const
163 {
164     return showVersion_;
165 }
166 
ShouldAlign() const167 bool Option::ShouldAlign() const
168 {
169     return shouldAlign_;
170 }
171 
ShouldGenTextConfig() const172 bool Option::ShouldGenTextConfig() const
173 {
174     return shouldGenTextConfig_;
175 }
176 
ShouldGenMacroConfig() const177 bool Option::ShouldGenMacroConfig() const
178 {
179     return shouldGenMacroConfig_;
180 }
181 
ShouldGenBinaryConfig() const182 bool Option::ShouldGenBinaryConfig() const
183 {
184     return shouldGenByteCodeConfig_;
185 }
186 
ShouldGenHexDump() const187 bool Option::ShouldGenHexDump() const
188 {
189     return showGenHexDump_;
190 }
191 
ShouldDecompile() const192 bool Option::ShouldDecompile() const
193 {
194     return shouldDecompile_;
195 }
196 
ShouldGenStartConfig() const197 bool Option::ShouldGenStartConfig() const
198 {
199     return genStartCfg_;
200 }
201 
GetSymbolPrefix() const202 std::string Option::GetSymbolPrefix() const
203 {
204     return symbolNamePrefix_;
205 }
206 
VerboseLog() const207 bool Option::VerboseLog() const
208 {
209     return verboseLog_;
210 }
211 
SetOptionError(bool shouldShowUsage)212 void Option::SetOptionError(bool shouldShowUsage)
213 {
214     showUsage_ = shouldShowUsage;
215     optionError_ = true;
216 }
217 
GetSourceName() const218 std::string Option::GetSourceName() const
219 {
220     return sourceName_;
221 }
222 
GetSourceNameBase() const223 std::string Option::GetSourceNameBase() const
224 {
225     return sourceNameBase_;
226 }
227 
GetOutputName() const228 std::string Option::GetOutputName() const
229 {
230     return outputName_;
231 }
232 
SetSourceOption(const char * srcName)233 bool Option::SetSourceOption(const char *srcName)
234 {
235     std::string srcAbsPath(Util::File::AbsPath(srcName));
236     if (srcAbsPath.empty()) {
237         Logger().Error() << "invalid source file: " << srcName << ", " << strerror(errno);
238         SetOptionError(false);
239         return false;
240     }
241 
242     sourceName_ = srcAbsPath;
243     sourceNameBase_ = Util::File::FileNameBase(srcAbsPath);
244     return true;
245 }
246 
GetVersion(uint32_t & minor,uint32_t & major)247 void Option::GetVersion(uint32_t &minor, uint32_t &major)
248 {
249     minor = HCS_COMPILER_VERSION_MINOR;
250     major = HCS_COMPILER_VERSION_MAJOR;
251 }
252