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
main(int argc,char ** argv)29 int main(int argc, char** argv)
30 {
31 Options options(argc, argv);
32
33 if (options.DoShowUsage()) {
34 options.ShowUsage();
35 return 0;
36 }
37
38 if (options.DoShowVersion()) {
39 options.ShowVersion();
40 return 0;
41 }
42
43 if (options.HasErrors()) {
44 options.ShowErrors();
45 return 0;
46 }
47
48 if (options.GetTargetLanguage().Equals("rust") ||
49 options.GetTargetLanguage().Equals("ts")) {
50 if (options.DoSearchKeywords()) {
51 options.ShowWarning();
52 }
53 }
54
55 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
94 if (options.DoGenerateCode()) {
95 if (metadata == nullptr) {
96 String metadataFile = options.GetMetadataFile();
97 metadata = MetadataReader::ReadMetadataFromFile(metadataFile);
98 if (metadata == nullptr) {
99 Logger::E(TAG, "Get metadata from \"%s\" failed.", metadataFile.string());
100 return -1;
101 }
102 }
103
104 CodeGenerator codeGen(metadata.get(), options.GetTargetLanguage(),
105 options.GetGenerationDirectory(), options.GetAttribute());
106 if (!codeGen.Generate()) {
107 Logger::E(TAG, "Generate \"%s\" codes failed.", options.GetTargetLanguage().string());
108 return -1;
109 }
110 }
111
112 return 0;
113 }
114