1/* 2* Copyright (c) 2022 Shenzhen Kaihong Digital Industry Development 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//生成BUILD.gn 16//生成tool_utility.h,生成tool_utility.cpp 17const { replaceAll } = require("./tools/tool"); 18const { generateNamespace } = require("./generate/namespace"); 19const { writeFile } = require("./tools/FileRW"); 20const re = require("./tools/re"); 21const { generateGYP } = require("./extend/binding_gyp"); 22const { generateGN } = require("./extend/build_gn"); 23const { generateBase } = require("./extend/tool_utility"); 24const { NumberIncrease } = require("./tools/common"); 25var fs = require('fs'); 26 27let moduleCppTmplete = `\ 28#include <cstring> 29#include <string> 30#include <memory> 31#include <vector> 32#include <node_api.h> 33#include <any> 34#include "tool_utility.h" 35#include "[implName].h" 36 37#define NUMBER_JS_2_C(napi_v, type, dest) \\ 38 if (typeid(type) == typeid(int32_t)) { \\ 39 dest = pxt->SwapJs2CInt32(napi_v); \\ 40 } \\ 41 else if (typeid(type) == typeid(uint32_t)){\\ 42 dest = pxt->SwapJs2CUint32(napi_v); \\ 43 } \\ 44 else if (typeid(type) == typeid(int64_t)){ \\ 45 dest = pxt->SwapJs2CInt64(napi_v); \\ 46 } \\ 47 else if (typeid(type) == typeid(double_t)){\\ 48 dest = pxt->SwapJs2CDouble(napi_v); \\ 49 } 50 51#define NUMBER_JS_2_C_ENUM(napi_v, type, dest, enum_type) \\ 52 if (typeid(type) == typeid(int32_t)) { \\ 53 dest = static_cast<enum_type>(pxt->SwapJs2CInt32(napi_v)); \\ 54 } \\ 55 else if (typeid(type) == typeid(uint32_t)){\\ 56 dest = static_cast<enum_type>(pxt->SwapJs2CUint32(napi_v)); \\ 57 } \\ 58 else if (typeid(type) == typeid(int64_t)){ \\ 59 dest = static_cast<enum_type>(pxt->SwapJs2CInt64(napi_v)); \\ 60 } \\ 61 else if (typeid(type) == typeid(double_t)){\\ 62 dest = static_cast<enum_type>(pxt->SwapJs2CDouble(napi_v)); \\ 63 } 64 65#define BOOLEAN_JS_2_C(napi_v, type, dest){ \\ 66 dest = pxt->SwapC2JsBool(napi_v); \\ 67} 68 69#define C_DELETE(p) \\ 70 if (p) { \\ 71 delete p; \\ 72 } 73 74__attribute__((unused)) static napi_value number_c_to_js(XNapiTool *pxt, const std::type_info &n, void *num) 75{ 76 if (n == typeid(int32_t)) 77 return pxt->SwapC2JsInt32(*(int32_t *)num); 78 else if (n == typeid(uint32_t)) 79 return pxt->SwapC2JsUint32(*(uint32_t *)num); 80 else if (n == typeid(int64_t)) 81 return pxt->SwapC2JsInt64(*(int64_t *)num); 82 else if (n == typeid(double_t)) 83 return pxt->SwapC2JsDouble(*(double_t *)num); 84 return nullptr; 85} 86#define NUMBER_C_2_JS(pxt, n) \\ 87 number_c_to_js(pxt, typeid(n), &n) 88 89[body_replace] 90 91static napi_value init(napi_env env, napi_value exports) 92{ 93 std::shared_ptr<XNapiTool> pxt = std::make_shared<XNapiTool>(env, exports); 94 95 [init_replace] 96 97 return exports; 98} 99 100static napi_module g_[implName]_Module = { 101 .nm_version = 1, 102 .nm_flags = 0, 103 .nm_filename = nullptr, 104 .nm_register_func = init, 105 .nm_modname = "[modulename]", 106 .nm_priv = ((void *)0), 107 .reserved = {(void *)0}, 108}; 109 110extern "C" __attribute__((constructor)) void Register_[implName]_Module(void) 111{ 112 napi_module_register(&g_[implName]_Module); 113} 114` 115 116let implHTemplete = `\ 117#ifndef IMPL_[impl_name_upper]_H 118#define IMPL_[impl_name_upper]_H 119 120#include <string> 121#include <memory> 122#include <vector> 123#include <cmath> 124#include <map> 125#include <any> 126[importTs] 127 128[numberUsing] 129 130[implH_detail] 131 132#endif // IMPL_[impl_name_upper]_H 133` 134 135let implCppTemplete = `\ 136#include "[implName].h" 137 138[implCpp_detail] 139` 140 141function generateAll(structOfTs, destDir, moduleName, numberType) { 142 let ns0 = structOfTs.declareNamespace[0]; 143 let license = structOfTs.declareLicense[0]; 144 let result = generateNamespace(ns0.name, ns0.body) 145 let numberUsing = "" 146 var numbertype = "uint32_t"; 147 if(numberType != ""){ 148 numbertype = numberType; 149 } 150 for (let i = 1; i < NumberIncrease.get(); i++) { 151 numberUsing += "using NUMBER_TYPE_%d = ".format(i) + numbertype + ";\n" 152 } 153 let middleCpp = replaceAll(moduleCppTmplete, "[body_replace]", result.middleBody); 154 middleCpp = replaceAll(middleCpp, "[init_replace]", result.middleInit); 155 middleCpp = replaceAll(middleCpp, "[implName]", ns0.name); 156 middleCpp = replaceAll(middleCpp, "[modulename]", moduleName); 157 writeFile(re.pathJoin(destDir, "%s_middle.cpp".format(ns0.name)), 158 null != license ? (license + "\n" + middleCpp) : middleCpp) 159 160 let implH = replaceAll(implHTemplete, "[impl_name_upper]", ns0.name.toUpperCase()) 161 implH = implH.replaceAll("[numberUsing]", numberUsing); 162 implH = replaceAll(implH, "[implH_detail]", result.implH) 163 let imports = '' 164 for (let i = 0; i < structOfTs.imports.length; i++){ 165 imports += structOfTs.imports[i] 166 } 167 implH = replaceAll(implH, "[importTs]", imports) 168 writeFile(re.pathJoin(destDir, "%s.h".format(ns0.name)), null != license ? (license + "\n" + implH) : implH) 169 170 let implCpp = implCppTemplete.replaceAll("[implName]", ns0.name) 171 implCpp = implCpp.replaceAll("[implCpp_detail]", result.implCpp) 172 writeFile(re.pathJoin(destDir, "%s.cpp".format(ns0.name)), null != license ? (license + "\n" + implCpp) : implCpp) 173 174 let partName = moduleName.replace('.', '_') 175 generateGYP(destDir, ns0.name, license)//生成ubuntu下测试的编译脚本 176 generateGN(destDir, ns0.name, license, partName)//生成BUILD.gn for ohos 177 generateBase(destDir, license)//tool_utility.h/cpp 178} 179 180module.exports = { 181 generateAll 182} 183