16
import * as fs from "node:fs"
17
import { execSync } from "node:child_process"
18
import { argv } from 'process'
19
import * as path from "node:path"
24
for (let i = 2; i < argv.length; ++i) {
44
let arch = isArm64 ? 'arm64' : 'arm'
45
let deviceLibDir = isArm64 ? '/system/lib64/platformsdk' : '/system/lib/platformsdk'
46
let target = isArm64 ? 'aarch64-linux-ohos' : 'arm-linux-ohos'
48
const libName = `NativeBridgeNapi.node`
49
const native = `native`
50
const ohosV8 = `ohos-v8`
51
const thirdToolsDir = `3rdtools`
53
let nodeLib = `libnode.so`
54
let nodeLib108 = `libnode.so.108`
55
let nodeDir = `${thirdToolsDir}/${target}`
56
let sourceDir = isFull ? `build/peers` : `build/subset`
57
let deviceAppDir = `/data/local/tmp/perf/`
58
let koalauiModulesDir = `peer_lib/ts/@koalaui`
59
let ohosSdkRoot = process.env.OHOS_SDK ?? '../koala-ui/ohos-sdk/ohos-sdk'
60
let ohosSdkVersion = process.env.OHOS_SDK_VERSION ?? 'HarmonyOS-NEXT-DP1'
61
let llvmDir = `${ohosSdkRoot}/${ohosSdkVersion}/base/native/llvm/`
63
function downloadOhosNode() {
64
const download3rdToolCmd = 'node download-3rdtools.mjs'
65
if (!fs.existsSync(nodeDir)) {
66
if (fs.existsSync(thirdToolsDir)) fs.rmdirSync(thirdToolsDir)
67
console.log(`run ${download3rdToolCmd} to get 3rdtools for node`)
68
execSync(download3rdToolCmd, { cwd: ohosV8, stdio: 'inherit' })
73
execSync(`hdc shell mount -o rw,remount /`, {stdio: 'inherit'})
76
function makeDir(targetDir, dir) {
77
execSync(`hdc shell mkdir -p ${targetDir}/${dir}`, {stdio: 'inherit'})
78
return `${targetDir}/${dir}`
81
function hdcFileSend(sourcePath, targetPath) {
82
sourcePath = path.join(sourcePath)
83
console.log(`hdc file send ${sourcePath} ${targetPath}`)
84
execSync(`hdc file send ${sourcePath} ${targetPath}`, {stdio: 'inherit'})
87
function pushDirectory(sourceDir, targetDir) {
88
fs.readdirSync(sourceDir).forEach(file => {
89
if (file !== '.gitignore') {
90
const sourceFile = path.join(sourceDir, file);
91
hdcFileSend(sourceFile, targetDir)
96
function pushAppJsDeps2Device() {
97
let deviceNodeModulesDir = makeDir(deviceAppDir, 'node_modules')
98
let deviceKoalauiModulesDir = makeDir(deviceNodeModulesDir, '@koalaui')
99
pushDirectory(koalauiModulesDir, deviceKoalauiModulesDir)
102
function pushAppLibDeps2Device() {
104
hdcFileSend(path.join(nodeDir, nodeBin), deviceLibDir + '/' + nodeBin)
105
execSync(`hdc shell chmod 777 ${deviceLibDir}/${nodeBin}`, { stdio: 'inherit' })
106
hdcFileSend(path.join(nodeDir, nodeLib108), deviceLibDir + '/' + nodeLib108)
107
execSync(`hdc shell ln -s ${deviceLibDir}/${nodeLib108} ${deviceLibDir}/${nodeLib}`, { stdio: 'inherit' })
110
let cppSharedLibPath = `${llvmDir}/lib/${target}/`
111
let cppLibName = `libc++_shared.so`
112
hdcFileSend(path.join(cppSharedLibPath, cppLibName), `${deviceLibDir}/${cppLibName}`)
115
hdcFileSend(path.join(native, libName), deviceLibDir + '/' + libName)
118
function pushApp2Device() {
119
pushDirectory(sourceDir, deviceAppDir)
122
function resolveBridgePath4NativeModule() {
123
let nativeModuleFile = path.resolve(`${sourceDir}/generated/subset/NativeModule.js`)
124
let nativeModuleDevicePath = `${deviceAppDir}/generated/subset/NativeModule.js`
125
let nativeModuleFileCopy = `${nativeModuleFile}.copy`
126
let content = fs.readFileSync(nativeModuleFile, 'utf8')
127
const requireStatement = 'require("../../../../native/NativeBridgeNapi")'
128
const requireNapiStatement = `require("${deviceLibDir}/NativeBridgeNapi")`
129
let expectedData = content.replace(requireStatement, requireNapiStatement)
130
fs.writeFileSync(nativeModuleFileCopy, expectedData, 'utf8', (error) => {
132
console.error(`write ${nativeModuleFileCopy} error :`, error)
135
console.log(`write ${nativeModuleFileCopy} successfully`);
137
hdcFileSend(nativeModuleFileCopy, nativeModuleDevicePath)
140
function runPerfOnOhosV8(nodePath, appDir) {
141
console.log(`hdc shell ${nodePath} ${appDir}`)
142
execSync(`hdc shell ${nodePath} ${appDir}`, {stdio: 'inherit'})
145
if (!fs.existsSync(sourceDir)) {
146
console.log(`${sourceDir} is empty. please build the codes first.`)
152
pushAppJsDeps2Device()
153
pushAppLibDeps2Device()
155
resolveBridgePath4NativeModule()
157
let nodePath = `${deviceLibDir}/${nodeBin}`
158
runPerfOnOhosV8(nodePath, deviceAppDir)