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 #ifndef OHOS_IDL_MODULE_TEST_IDL_COMMON_H 17 #define OHOS_IDL_MODULE_TEST_IDL_COMMON_H 18 19 #include <gtest/gtest.h> 20 #include <string> 21 #define private public 22 #define protected public 23 #include "codegen/code_emitter.h" 24 #include "codegen/code_generator.h" 25 #include "codegen/rust_code_emitter.h" 26 #undef private 27 #undef protected 28 #include "metadata/metadata_builder.h" 29 #include "metadata/metadata_dumper.h" 30 #include "metadata/metadata_reader.h" 31 #include "metadata/metadata_serializer.h" 32 #include "parser/parser.h" 33 #include "util/file.h" 34 #include "util/logger.h" 35 #include "util/options.h" 36 #include "util/string.h" 37 #include "util/string_builder.h" 38 #include "idl_file.h" 39 #include "securec.h" 40 41 namespace OHOS { 42 namespace Idl { 43 namespace TestCommon { 44 const int ERR_FAIL = -1; 45 const int ERR_OK = 0; 46 47 class IdlCommon { 48 public: 49 IdlCommon() = default; 50 ~IdlCommon() = default; 51 Ready(int argc,char ** argv)52 int Ready(int argc, char** argv) 53 { 54 Options options(argc, argv); 55 std::shared_ptr<MetaComponent> metadata = nullptr; 56 57 if (options.DoCompile()) { 58 Parser parser(options); 59 if (!parser.Parse(options.GetSourceFile())) { 60 return ERR_FAIL; 61 } 62 63 MetadataBuilder builder(parser.GetModule()); 64 metadata = builder.Build(); 65 metadata_ = metadata; 66 if (metadata == nullptr) { 67 return ERR_FAIL; 68 } 69 } 70 71 if (options.DoDumpMetadata()) { 72 MetadataDumper dumper(metadata.get()); 73 dumper.Dump(""); 74 } 75 76 if (options.DoSaveMetadata()) { 77 File metadataFile(options.GetMetadataFile(), File::WRITE); 78 if (!metadataFile.IsValid()) { 79 return ERR_FAIL; 80 } 81 82 MetadataSerializer serializer(metadata.get()); 83 serializer.Serialize(); 84 uintptr_t data = serializer.GetData(); 85 int size = serializer.GetDataSize(); 86 87 metadataFile.WriteData(reinterpret_cast<void*>(data), size); 88 metadataFile.Flush(); 89 metadataFile.Close(); 90 } 91 92 if (options.DoGenerateCode()) { 93 if (metadata == nullptr) { 94 String metadataFile = options.GetMetadataFile(); 95 metadata = MetadataReader::ReadMetadataFromFile(metadataFile); 96 if (metadata == nullptr) { 97 return ERR_FAIL; 98 } 99 } 100 if (options.GetTargetLanguage().Equals("rust")) { 101 this->rustCodeGen_ = std::make_shared<RustCodeEmitter>(metadata.get()); 102 this->rustCodeGen_->SetDirectory(options.GetGenerationDirectory()); 103 } 104 } 105 return 0; 106 } 107 108 int PrepareIdlFile( 109 const std::string &fileName, const std::string &fileContent, const std::string &fileLicense = LICENSE_CONTENT) 110 { 111 String filePath = String::Format("%s/%s", "./", fileName.c_str()); 112 File file(filePath, File::WRITE); 113 if (!file.IsValid()) { 114 GTEST_LOG_(INFO) << "OPEN FILE FAIL"; 115 return ERR_FAIL; 116 } 117 118 StringBuilder stringBuilder; 119 stringBuilder.Append(fileLicense.c_str()); 120 stringBuilder.Append(fileContent.c_str()); 121 122 String data = stringBuilder.ToString(); 123 file.WriteData(data.string(), data.GetLength()); 124 file.Flush(); 125 file.Close(); 126 return ERR_OK; 127 } 128 129 std::shared_ptr<MetaComponent> metadata_ = nullptr; 130 std::shared_ptr<RustCodeEmitter> rustCodeGen_ = nullptr; 131 }; 132 133 class ParameterArgv { 134 public: ParameterArgv(const char ** args,const int argc)135 ParameterArgv(const char** args, const int argc) : argc_(argc) 136 { 137 argv_ = new char* [argc_] {nullptr}; 138 for (int i = 0; i < argc_; ++i) { 139 const int itemSize = strlen(args[i]); 140 argv_[i] = new char[itemSize + 1] {0}; 141 if (strcpy_s(argv_[i], itemSize + 1, args[i])) { 142 GTEST_LOG_(ERROR) << "strcpy_s error [!EOK]"; 143 } 144 } 145 }; ~ParameterArgv()146 ~ParameterArgv() 147 { 148 if (argv_ == nullptr) { 149 return; 150 } 151 152 for (int i = 0; i < argc_; ++i) { 153 if (argv_[i] == nullptr) { 154 continue; 155 } 156 delete[] argv_[i]; 157 argv_[i] = nullptr; 158 } 159 delete[] argv_; 160 argv_ = nullptr; 161 }; 162 GetArgv()163 char** GetArgv() 164 { 165 return argv_; 166 }; 167 168 char** argv_; 169 const int argc_; 170 }; 171 } 172 } 173 } 174 #endif // OHOS_IDL_MODULE_TEST_IDL_COMMON_H