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