• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  *
4  * HDF is dual licensed: you can use it either under the terms of
5  * the GPL, or the BSD license, at your option.
6  * See the LICENSE file in the root of this repository for complete details.
7  */
8 
9 #include "codegen/code_generator.h"
10 #include "parser/parser.h"
11 #include "preprocessor/preprocessor.h"
12 #include "util/file.h"
13 #include "util/logger.h"
14 #include "util/options.h"
15 
16 using namespace OHOS::HDI;
17 
GetHashKey()18 static bool GetHashKey()
19 {
20     for (const auto &sourceFile : Options::GetInstance().GetSourceFiles()) {
21         std::unique_ptr<File> idlFile = std::make_unique<File>(sourceFile, int(File::READ));
22         if (!idlFile->IsValid()) {
23             Logger::E("hdi-gen", "failed to open idl file");
24             return false;
25         }
26         printf("%s:%lu\n", idlFile->GetPath().c_str(), idlFile->GetHashKey());
27     }
28     return true;
29 }
main(int argc,char ** argv)30 int main(int argc, char **argv)
31 {
32     const Options &options = Options::GetInstance().Parse(argc, argv);
33     if (options.HasErrors()) {
34         options.ShowErrors();
35         return 0;
36     }
37     if (options.DoShowUsage()) {
38         options.ShowUsage();
39         return 0;
40     }
41     if (options.DoShowVersion()) {
42         options.ShowVersion();
43         return 0;
44     }
45     if (!options.DoCompile()) {
46         return 0;
47     }
48     if (options.DoGetHashKey()) {
49         return GetHashKey() ? 0 : -1;
50     }
51 
52     Preprocessor preprocessor;
53     std::vector<std::string> sourceFiles;
54     if (!preprocessor.Preprocess(sourceFiles)) {
55         Logger::E("MAIN", "failed to preprocess");
56         return -1;
57     }
58 
59     Parser parser;
60     if (!parser.Parse(sourceFiles)) {
61         Logger::E("MAIN", "failed to parse file");
62         return -1;
63     }
64 
65     if (options.DoDumpAST()) {
66         for (const auto &astPair : parser.GetAllAst()) {
67             printf("%s\n", astPair.second->Dump("").c_str());
68         }
69     }
70 
71     if (!options.DoGenerateCode()) {
72         return 0;
73     }
74     if (!CodeGenerator(parser.GetAllAst()).Generate()) {
75         Logger::E("hdi-gen", "failed to generate code");
76         return -1;
77     }
78     return 0;
79 }