idlize

Форк
0
/
runPerfOnV8.mjs 
158 строк · 5.5 Кб
1
/*
2
 * Copyright (c) 2024 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
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"
20

21
let isFull = true
22
let isArm64 = true
23

24
for (let i = 2; i < argv.length; ++i) {
25
    switch (argv[i]) {
26
        case "subset":
27
            isFull = false
28
            break;
29
        case "full":
30
            isFull = true
31
            break;
32
        case "arm64":
33
            isArm64 = true
34
            break;
35
        case "arm32":
36
        case "arm":
37
            isArm64 = false
38
            break;
39
        default:
40
            break;
41
    }
42
}
43

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'
47

48
const libName = `NativeBridgeNapi.node`
49
const native = `native`
50
const ohosV8 = `ohos-v8`
51
const thirdToolsDir = `3rdtools`
52
let nodeBin = `node`
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/`
62

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' })
69
    }
70
}
71

72
function mountRW() {
73
    execSync(`hdc shell mount -o rw,remount /`, {stdio: 'inherit'})
74
}
75

76
function makeDir(targetDir, dir) {
77
    execSync(`hdc shell mkdir -p ${targetDir}/${dir}`, {stdio: 'inherit'})
78
    return `${targetDir}/${dir}`
79
}
80

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'})
85
}
86

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)
92
        }
93
    });
94
}
95

96
function pushAppJsDeps2Device() {
97
    let deviceNodeModulesDir = makeDir(deviceAppDir, 'node_modules')
98
    let deviceKoalauiModulesDir = makeDir(deviceNodeModulesDir, '@koalaui')
99
    pushDirectory(koalauiModulesDir, deviceKoalauiModulesDir)
100
}
101

102
function pushAppLibDeps2Device() {
103
    // push node
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' })
108

109
    // push libc++_shared.so to /system/lib64/platformsdk/ avoid runtime errors because of wrong version libc++
110
    let cppSharedLibPath = `${llvmDir}/lib/${target}/`
111
    let cppLibName = `libc++_shared.so`
112
    hdcFileSend(path.join(cppSharedLibPath, cppLibName), `${deviceLibDir}/${cppLibName}`)
113

114
    // push lib
115
    hdcFileSend(path.join(native, libName), deviceLibDir + '/' + libName)
116
}
117

118
function pushApp2Device() {
119
    pushDirectory(sourceDir, deviceAppDir)
120
}
121

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) => {
131
        if (error) {
132
        console.error(`write ${nativeModuleFileCopy} error :`, error)
133
        return
134
        }
135
        console.log(`write ${nativeModuleFileCopy} successfully`);
136
    });
137
    hdcFileSend(nativeModuleFileCopy, nativeModuleDevicePath)
138
}
139

140
function runPerfOnOhosV8(nodePath, appDir) {
141
    console.log(`hdc shell  ${nodePath} ${appDir}`)
142
    execSync(`hdc shell  ${nodePath} ${appDir}`, {stdio: 'inherit'})
143
}
144

145
if (!fs.existsSync(sourceDir)) {
146
    console.log(`${sourceDir} is empty. please build the codes first.`)
147
    process.exit()
148
}
149

150
downloadOhosNode()
151
mountRW()
152
pushAppJsDeps2Device()
153
pushAppLibDeps2Device()
154
pushApp2Device()
155
resolveBridgePath4NativeModule()
156

157
let nodePath = `${deviceLibDir}/${nodeBin}`
158
runPerfOnOhosV8(nodePath, deviceAppDir)

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.