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 "codegen/code_generator.h"
17 #include <cstdlib>
18 #include <unistd.h>
19 #include <sys/stat.h>
20 #include "codegen/rust_code_emitter.h"
21 #include "codegen/cpp_code_emitter.h"
22 #include "codegen/ts_code_emitter.h"
23 #include "util/logger.h"
24 
25 namespace OHOS {
26 namespace Idl {
27 const char* CodeGenerator::tag = "CodeGenerator";
CodeGenerator(MetaComponent * mc,const String & language,const String & dir,const Options::Attribute & att)28 CodeGenerator::CodeGenerator(
29     MetaComponent* mc, const String& language, const String& dir, const Options::Attribute& att)
30     : targetLanguage_(language),
31       targetDirectory_(dir),
32       metaComponent_(mc)
33 {
34     if (language.Equals("rust")) {
35         emitter_ = new RustCodeEmitter(metaComponent_);
36     } else if (language.Equals("cpp")) {
37         emitter_ = new CppCodeEmitter(metaComponent_);
38     } else if (language.Equals("ts")) {
39         emitter_ = new TsCodeEmitter(metaComponent_);
40     } else {
41         Logger::E(tag, "Unknown language: %s.", language.string());
42     }
43 
44     if (emitter_ != nullptr) {
45         emitter_->SetHitraceOn(att.doHitrace);
46         emitter_->SetLogOn(att.doLog);
47         emitter_->SetHitraceTag(att.hitraceTag);
48         emitter_->SetLogTag(att.logTag);
49         emitter_->SetDomainId(att.domainId);
50     }
51 }
52 
ResolveDirectory()53 bool CodeGenerator::ResolveDirectory()
54 {
55 #ifdef __MINGW32__
56     if (targetDirectory_.IndexOf(":\\") == -1) {
57         char* cmd = getcwd(nullptr, 0);
58         targetDirectory_ = String::Format("%s\\%s", cmd, targetDirectory_.string());
59         free(cmd);
60     }
61 #else
62     if (!targetDirectory_.StartsWith("/")) {
63         char* cwd = getcwd(nullptr, 0);
64         targetDirectory_ = String::Format("%s/%s", cwd, targetDirectory_.string());
65         free(cwd);
66     }
67 #endif
68 
69     if (!access(targetDirectory_.string(), R_OK | W_OK)) {
70         return true;
71     }
72 
73 #ifdef __MINGW32__
74     if (mkdir(targetDirectory_.string()) != 0) {
75 #else
76     if (mkdir(targetDirectory_.string(), S_IRWXU | S_IRWXG | S_IRWXO) != 0) {
77 #endif
78         Logger::E(tag, "Create \"%s\" directory failed.", targetDirectory_.string());
79         return false;
80     }
81 
82     return true;
83 }
84 
85 bool CodeGenerator::Generate()
86 {
87     if (!ResolveDirectory()) {
88         return false;
89     }
90 
91     emitter_->SetDirectory(targetDirectory_);
92 
93     emitter_->EmitInterface();
94     emitter_->EmitInterfaceProxy();
95     emitter_->EmitInterfaceStub();
96 
97     return true;
98 }
99 } // namespace Idl
100 } // namespace OHOS