1 /*
2 * Copyright (c) 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 "codegen/code_generator.h"
17 #include "metadata/metadata_builder.h"
18 #include "metadata/metadata_dumper.h"
19 #include "metadata/metadata_reader.h"
20 #include "metadata/metadata_serializer.h"
21 #include "parser/parser.h"
22 #include "util/logger.h"
23 #include "util/options.h"
24
25 using namespace OHOS::Idl;
26
27 static const char* TAG = "idl";
28
DoOptionsCheck(Options & options)29 static int DoOptionsCheck(Options& options)
30 {
31 if (options.DoShowUsage()) {
32 options.ShowUsage();
33 return 0;
34 }
35
36 if (options.DoShowVersion()) {
37 options.ShowVersion();
38 return 0;
39 }
40
41 if (options.HasErrors()) {
42 options.ShowErrors();
43 return 0;
44 }
45
46 if (options.GetTargetLanguage().Equals("rust") ||
47 options.GetTargetLanguage().Equals("ts")) {
48 if (options.DoSearchKeywords()) {
49 options.ShowWarning();
50 }
51 }
52 return 1;
53 }
54
DoCompile(Options & options,std::shared_ptr<MetaComponent> & metadata)55 static int DoCompile(Options& options, std::shared_ptr<MetaComponent>& metadata)
56 {
57 if (options.DoCompile()) {
58 Parser parser(options);
59 if (!parser.Parse(options.GetSourceFile())) {
60 Logger::E(TAG, "Parsing .idl failed.");
61 return -1;
62 }
63
64 MetadataBuilder builder(parser.GetModule());
65 metadata = builder.Build();
66 if (metadata == nullptr) {
67 Logger::E(TAG, "Generate metadata failed.");
68 return -1;
69 }
70 }
71
72 if (options.DoDumpMetadata()) {
73 MetadataDumper dumper(metadata.get());
74 dumper.Dump("");
75 }
76
77 if (options.DoSaveMetadata()) {
78 File metadataFile(options.GetMetadataFile(), File::WRITE);
79 if (!metadataFile.IsValid()) {
80 Logger::E(TAG, "Create metadata file failed.");
81 return -1;
82 }
83
84 MetadataSerializer serializer(metadata.get());
85 serializer.Serialize();
86 uintptr_t data = serializer.GetData();
87 int size = serializer.GetDataSize();
88
89 metadataFile.WriteData(reinterpret_cast<void*>(data), size);
90 metadataFile.Flush();
91 metadataFile.Close();
92 }
93 return 0;
94 }
95
DoGenerage(const Options & options,std::shared_ptr<MetaComponent> & metadata)96 static int DoGenerage(const Options& options, std::shared_ptr<MetaComponent>& metadata)
97 {
98 if (options.DoGenerateCode()) {
99 if (metadata == nullptr) {
100 String metadataFile = options.GetMetadataFile();
101 metadata = MetadataReader::ReadMetadataFromFile(metadataFile);
102 if (metadata == nullptr) {
103 Logger::E(TAG, "Get metadata from \"%s\" failed.", metadataFile.string());
104 return -1;
105 }
106 }
107
108 CodeGenerator codeGen(metadata.get(), options.GetTargetLanguage(),
109 options.GetGenerationDirectory(), options.GetAttribute());
110 if (!codeGen.Generate()) {
111 Logger::E(TAG, "Generate \"%s\" codes failed.", options.GetTargetLanguage().string());
112 return -1;
113 }
114 }
115 return 0;
116 }
117
main(int argc,char ** argv)118 int main(int argc, char** argv)
119 {
120 Options options(argc, argv);
121 int checkOffRes = DoOptionsCheck(options);
122 if (!checkOffRes) {
123 return checkOffRes;
124 }
125
126 std::shared_ptr<MetaComponent> metadata;
127 int compileRes = DoCompile(options, metadata);
128 if (compileRes) {
129 return compileRes;
130 }
131
132 int generateRes = DoGenerage(options, metadata);
133 if (generateRes) {
134 return generateRes;
135 }
136
137 return 0;
138 }
139