1 /*
2 * Copyright (c) 2022-2023 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 "options.h"
17
18 #include <cstdio>
19 #include <cstring>
20
21 #include "util/string_builder.h"
22
23 namespace OHOS {
24 namespace Idl {
Parse(int argc,char ** argv)25 void Options::Parse(int argc, char** argv)
26 {
27 StringBuilder errors;
28 program_ = argv[0];
29
30 int i = 1;
31 while (i < argc) {
32 String option(argv[i++]);
33 if (ParseSub(option, i, argv)) {
34 continue;
35 } else if (option.Equals("-t")) {
36 doKeywords_ = true;
37 if (targetLanguage_.Equals("cpp") && DoIllegalParameter(argv[i])) {
38 doLegalParameters_ = false;
39 return;
40 }
41 hitraceTag_ = argv[i++];
42 doHitrace_ = true;
43 } else if (option.Equals("-log-domainid")) {
44 doKeywords_ = true;
45 if (targetLanguage_.Equals("cpp") && DoIllegalParameter(argv[i])) {
46 doLegalParameters_ = false;
47 return;
48 }
49 domainId_ = argv[i++];
50 } else if (option.Equals("-log-tag")) {
51 doKeywords_ = true;
52 if (targetLanguage_.Equals("cpp") && DoIllegalParameter(argv[i])) {
53 doLegalParameters_ = false;
54 return;
55 }
56 logTag_ = argv[i++];
57 } else if (!option.StartsWith("-")) {
58 sourceFile_ = option;
59 } else {
60 errors.Append(option);
61 errors.Append(" ");
62 }
63 }
64
65 illegalOptions_ = errors.ToString();
66
67 attribute_.doHitrace = doHitrace_;
68 attribute_.hitraceTag = hitraceTag_;
69 attribute_.logTag = logTag_;
70 attribute_.domainId = domainId_;
71 attribute_.doLog = DoLogOn();
72 }
73
ParseSub(const String & option,int & i,char ** argv)74 bool Options::ParseSub(const String& option, int& i, char** argv)
75 {
76 if (option.Equals("--help")) {
77 doShowUsage_ = true;
78 return true;
79 } else if (option.Equals("--version")) {
80 doShowVersion_ = true;
81 return true;
82 } else if (option.Equals("-c")) {
83 doCompile_ = true;
84 return true;
85 } else if (option.Equals("-dump-ast")) {
86 doDumpAST_ = true;
87 return true;
88 } else if (option.Equals("-dump-metadata")) {
89 doDumpMetadata_ = true;
90 return true;
91 } else if (option.Equals("-s")) {
92 doSaveMetadata_ = true;
93 metadataFile_ = argv[i++];
94 return true;
95 } else if (option.Equals("-gen-rust")) {
96 doGenerateCode_ = true;
97 targetLanguage_ = "rust";
98 return true;
99 } else if (option.Equals("-gen-cpp")) {
100 doGenerateCode_ = true;
101 targetLanguage_ = "cpp";
102 return true;
103 } else if (option.Equals("-gen-ts")) {
104 doGenerateCode_ = true;
105 targetLanguage_ = "ts";
106 return true;
107 } else if (option.Equals("-d")) {
108 generationDirectory_ = argv[i++];
109 return true;
110 }
111 return false;
112 }
113
ShowErrors()114 void Options::ShowErrors()
115 {
116 if (!illegalOptions_.IsEmpty()) {
117 String options = illegalOptions_;
118 int index;
119 while ((index = options.IndexOf(' ')) != -1) {
120 printf("The Option \"%s\" is illegal.\n", options.Substring(0, index).string());
121 options = options.Substring(index + 1);
122 }
123 }
124 printf("Use \"--help\" to show usage.\n");
125 }
126
ShowVersion()127 void Options::ShowVersion()
128 {
129 printf("idl %d.%d\n"
130 "Copyright (c) Huawei Technologies Co., Ltd. 2022. All rights reserved.\n\n",
131 VERSION_MAJOR, VERSION_MINOR);
132 }
133
ShowWarning()134 void Options::ShowWarning()
135 {
136 printf("Warning : Executed successfully, but contains invalid commands !\n");
137 }
138
ShowUsage()139 void Options::ShowUsage()
140 {
141 printf("Compile a .idl file and generate metadata, or generate Rust/C++/Ts codes from metadata.\n"
142 "Usage: idl [options] file\n"
143 "Options:\n"
144 " --help Display command line options\n"
145 " --version Display toolchain version information\n"
146 " -dump-ast Display the AST of the compiled file\n"
147 " -dump-metadata Display the metadata generated from the compiled file\n"
148 " -c Compile the .idl file\n"
149 " -s <file> Place the metadata into <file>\n"
150 " -gen-rust Generate Rust codes\n"
151 " -gen-cpp Generate C++ codes\n"
152 " -gen-ts Generate Ts codes\n"
153 " -d <directory> Place generated codes into <directory>\n"
154 " -log-domainid <domainid> Place the service domain in <domainid>, Enable log(Pair with -log-tag)\n"
155 " -log-tag <tag> Place the subsystem name in <tag>, Enable log(Pair with -log-domainid)\n"
156 " -t <hitrace tag> Place the constant name from hitrace_meter.h file in <hitrace tag>\n");
157 }
158 }
159 }
160