1 /*
2  * Copyright (c) 2024 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 OHOS_IDL_OPTION_H
17 #define OHOS_IDL_OPTION_H
18 
19 #include <map>
20 #include <string>
21 #include <unordered_map>
22 #include <set>
23 
24 #include "util/common.h"
25 
26 namespace OHOS {
27 namespace Idl {
28 class Options {
29 public:
30     using PkgPathMap = std::unordered_map<std::string, std::string>;
31 
32     static Options &GetInstance();
33 
34     Options(const Options &other) = delete;
35     Options operator=(const Options &other) = delete;
36 
37     bool Parse(int argc, char *argv[]);
38 
39     ~Options() = default;
40 
41     struct Attribute {
AttributeAttribute42         Attribute() : doHitrace(false), doLog(false) {}
43         std::string hitraceTag;
44         std::string logTag;
45         std::string domainId;
46         bool doHitrace;
47         bool doLog;
48     };
49 
DoShowUsage()50     inline bool DoShowUsage() const
51     {
52         return doShowUsage;
53     }
54 
DoShowVersion()55     inline bool DoShowVersion() const
56     {
57         return doShowVersion;
58     }
59 
DoCompile()60     inline bool DoCompile() const
61     {
62         return doCompile;
63     }
64 
DoDumpAST()65     inline bool DoDumpAST() const
66     {
67         return doDumpAST;
68     }
69 
DoDumpMetadata()70     inline bool DoDumpMetadata() const
71     {
72         return doDumpMetadata;
73     }
74 
DoSaveMetadata()75     inline bool DoSaveMetadata() const
76     {
77         return doSaveMetadata;
78     }
79 
DoGetHashKey()80     inline bool DoGetHashKey() const
81     {
82         return doHashKey;
83     }
84 
DoGenerateCode()85     inline bool DoGenerateCode() const
86     {
87         return doGenerateCode;
88     }
89 
DoGenerateKernelCode()90     inline bool DoGenerateKernelCode() const
91     {
92         return genMode == GenMode::KERNEL;
93     }
94 
DoPassthrough()95     inline bool DoPassthrough() const
96     {
97         return genMode == GenMode::PASSTHROUGH;
98     }
99 
GetSourceFiles()100     inline std::set<std::string> GetSourceFiles() const
101     {
102         return sourceFiles;
103     }
104 
GetMetadataFile()105     inline std::string GetMetadataFile() const
106     {
107         return metadataFile;
108     }
109 
GetPackagePathMap()110     inline PkgPathMap GetPackagePathMap() const
111     {
112         return packagePathMap;
113     }
114 
GetPackage()115     inline std::string GetPackage() const
116     {
117         return idlPackage;
118     }
119 
GetGenerationDirectory()120     inline std::string GetGenerationDirectory() const
121     {
122         return genDir;
123     }
124 
GetOutPutFile()125     inline std::string GetOutPutFile() const
126     {
127         return outPutFile;
128     }
129 
130     bool HasErrors() const;
131 
132     void ShowErrors() const;
133 
134     bool HasWarning() const;
135 
136     void ShowWarning() const;
137 
138     void ShowVersion() const;
139 
140     void ShowUsage() const;
141 
142     bool DoLogOn() const;
143 
144     bool DoLegalLog() const;
145 
146     std::string GetRootPackage(const std::string &package) const;
147 
148     std::string GetRootPath(const std::string &package) const;
149 
150     std::string GetSubPackage(const std::string &package) const;
151 
152     std::string GetPackagePath(const std::string &package) const;
153 
154     std::string GetImportFilePath(const std::string &import) const;
155 
GetSystemLevel()156     inline SystemLevel GetSystemLevel() const
157     {
158         return systemLevel;
159     }
160 
GetGenMode()161     inline GenMode GetGenMode() const
162     {
163         return genMode;
164     }
165 
GetLanguage()166     inline Language GetLanguage() const
167     {
168         return genLanguage;
169     }
170 
DoHitraceState()171     inline bool DoHitraceState() const
172     {
173         return doHitrace;
174     }
175 
DoSearchKeywords()176     inline bool DoSearchKeywords() const
177     {
178         return doKeywords;
179     }
180 
GetGenerateHitraceTag()181     inline std::string GetGenerateHitraceTag() const
182     {
183         return hitraceTag;
184     }
185 
GetDomainId()186     inline std::string GetDomainId() const
187     {
188         return domainId;
189     }
190 
GetLogTag()191     inline std::string GetLogTag() const
192     {
193         return logTag;
194     }
195 
GetAttribute()196     inline Attribute GetAttribute() const
197     {
198         return attribute;
199     }
200 
GetInterfaceType()201     inline InterfaceType GetInterfaceType() const
202     {
203         return interfaceType;
204     }
205 private:
Options()206     Options()
207         : program(),
208         systemLevel(SystemLevel::INIT),
209         genMode(GenMode::INIT),
210         genLanguage(Language::CPP),
211         idlPackage(),
212         sourceFiles(),
213         genDir(),
214         packagePathMap(),
215         outPutFile(),
216         attribute(Attribute()),
217         doShowUsage(false),
218         doShowVersion(false),
219         doCompile(false),
220         doDumpAST(false),
221         doDumpMetadata(false),
222         doHashKey(false),
223         doGenerateCode(false),
224         doOutDir(false),
225         doKeywords(false),
226         doSaveMetadata(false),
227         doHitrace(false),
228         interfaceType(InterfaceType::SA)
229     {
230     }
231 
232     void SetLongOption(char op);
233 
234     bool SetSystemLevel(const std::string &system);
235 
236     bool SetGenerateMode(const std::string &mode);
237 
238     bool SetLanguage(const std::string &language);
239 
240     void SetPackage(const std::string &package);
241 
242     bool AddPackagePath(const std::string &packagePath);
243 
244     void AddSources(const std::string &sourceFile);
245 
246     void AddSourcesByDir(const std::string &dir);
247 
248     void SetOutDir(const std::string &dir);
249 
250     void SetCodePart(const std::string &part);
251 
252     bool DoLegalParam(const std::string &tag);
253 
254     bool SetHiTrace(const std::string &tag);
255 
256     bool SetLogDomainId(const std::string &id);
257 
258     bool SetLogTag(const std::string &id);
259 
260     bool SetInterfaceType(const std::string &type);
261 
262     bool DoSupportHdiType();
263 
264     bool DoSupportSmType();
265 
266     void SetSmDefaultOption();
267 
268     bool CheckSmOptions();
269 
270     bool DoSupportSaType();
271 
272     bool CheckHdiOptions();
273 
274     void SetHdiDefaultOption();
275 
276     bool CheckSaOptions();
277 
278     bool SetMetadataFile(const std::string &metadataFile);
279 
280     bool CheckOptions();
281 
282     bool ParseSingle(int option, std::string optVal);
283 
284     bool ParseOptionWithValue(int option, std::string optVal);
285 
286     static const char *optSupportArgs;
287 
288     static constexpr int VERSION_MAJOR  = 1;
289     static constexpr int VERSION_MINOR = 0;
290 
291     std::string program;
292     SystemLevel systemLevel;
293     GenMode genMode;
294     Language genLanguage;
295     std::string idlPackage;
296     std::string sourceDir; // '-D <directory>'
297     std::set<std::string> sourceFiles; // '-c <*.idl>'
298     std::string genDir; // '-d <directory>'
299     PkgPathMap packagePathMap;
300     std::string outPutFile;
301     std::string hitraceTag;
302     std::string domainId;
303     std::string logTag;
304     std::string metadataFile;
305     Attribute attribute;
306     std::string illegalOptions;
307 
308     bool doShowUsage;
309     bool doShowVersion; // '-v'
310     bool doCompile; // '-c'
311     bool doDumpAST;
312     bool doDumpMetadata;
313     bool doHashKey;
314     bool doGenerateCode;
315     bool doOutDir; // '-d'
316     bool doKeywords; // "-t" "-log-.."
317     bool doSaveMetadata;
318     bool doHitrace;
319     InterfaceType interfaceType;
320 };
321 } // namespace Idl
322 } // namespace OHOS
323 
324 #endif // OHOS_HDIL_OPTION_H