idlize

Форк
0
/
nativeBuild.mjs 
130 строк · 4.4 Кб
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 { execSync } from "node:child_process"
17
import { argv } from 'process'
18
import path from 'path'
19
import * as fs from "node:fs"
20
import os from "os";
21

22
const platform = os.platform()
23
const isWindows = (platform === 'win32')
24

25
function crossPathResolve(inPath) {
26
    if (isWindows) {
27
        return path.resolve(inPath).replace(/\\/g, '\\\\')
28
    } else {
29
        return path.resolve(inPath)
30
    }
31
}
32

33
const nativeDir = 'native'
34
let isFull = true
35
let isArm64 = true
36
let isV8 = false
37
let exeSuffix = isWindows ? '.exe' : ''
38

39
for (let i = 2; i < argv.length; ++i) {
40
    switch (argv[i]) {
41
        case "subset":
42
            isFull = false
43
            break
44
        case "full":
45
            isFull = true
46
            break
47
        case "arm64":
48
            isArm64 = true
49
            break
50
        case "arm32":
51
        case "arm":
52
            isArm64 = false
53
            break
54
        case "v8":
55
            isV8 = true
56
            break;
57
    }
58
}
59

60
console.log(`isArm64 = ${isArm64}`)
61
console.log(`isFull = ${isFull}`)
62

63
let arch = isArm64 ? `arm64` : `arm32`
64
let mode = isFull ? `peers` : `subset`
65
let crossFile = `cross-compilation-ohos-${arch}.txt`
66
let outDir = `build-${mode}-ohos-${arch}`
67
let ohosSdkRoot = process.env.OHOS_SDK ?? '../koala-ui/ohos-sdk/ohos-sdk'
68
let ohosSdkVersion = process.env.OHOS_SDK_VERSION ?? 'HarmonyOS-NEXT-DP1'
69

70
const sysrootDir = crossPathResolve(`${ohosSdkRoot}/${ohosSdkVersion}/base/native/sysroot`)
71
let target = `${isArm64 ? 'aarch64-linux-ohos' : 'arm-linux-ohos'}`
72
let builtInArgs = (
73
    isArm64 ? [`'--target=${target}'`] : [`'--target=${target}'`, "'-m32'", "'-march=armv7-a'"]
74
).join(',')
75

76
let crossFileContent = `
77
[binaries]
78
c = \'${crossPathResolve(`${ohosSdkRoot}/${ohosSdkVersion}/base/native/llvm/bin/clang${exeSuffix}`)}\'
79
ar = \'${crossPathResolve(`${ohosSdkRoot}/${ohosSdkVersion}/base/native/llvm/bin/llvm-ar${exeSuffix}`)}\'
80
cpp = \'${crossPathResolve(`${ohosSdkRoot}/${ohosSdkVersion}/base/native/llvm/bin/clang++${exeSuffix}`)}\'
81
strip = \'${crossPathResolve(`${ohosSdkRoot}/${ohosSdkVersion}/base/native/llvm/bin/llvm-strip${exeSuffix}`)}\'
82

83
[built-in options]
84
c_args = ['--sysroot=${sysrootDir}', ${builtInArgs}]
85
c_link_args = ['--sysroot=${sysrootDir}', ${builtInArgs}]
86
cpp_args = ['--sysroot=${sysrootDir}', ${builtInArgs}]
87
cpp_link_args = ['--sysroot=${sysrootDir}', ${builtInArgs}]
88

89
[host_machine]
90
system = 'ohos'
91
cpu_family = ${isArm64 ? "'aarch64'" : "'arm'"}
92
cpu = ${isArm64 ? "'aarch64'" : "'armv7a'"}
93
endian = 'little'
94
`
95

96
fs.writeFileSync(`${nativeDir}/${crossFile}`, crossFileContent, 'utf8', (error) => {
97
    if (error) {
98
        console.error(`Init ${crossFile} error : `, error)
99
        return
100
    }
101
    console.log(`Init ${crossFile} successfully`);
102
});
103

104
function resolveV8Deps() {
105
    const thirdToolsDir = `3rdtools`
106
    let nodeLibPath = `${sysrootDir}/usr/lib/${target}/libnode.so`
107
    const nodeLibSrc = `${thirdToolsDir}/${target}/libnode.so.108`
108
    if (!fs.existsSync(nodeLibSrc)) {
109
        let downloadCmd = `node ./download-3rdtools.mjs ${arch}`
110
        if (fs.existsSync(thirdToolsDir)) fs.rmdirSync(thirdToolsDir)
111
        execSync(downloadCmd, { cwd: './', stdio: 'inherit' })
112
    }
113
    if (!fs.existsSync(nodeLibPath)) {
114
        console.log(`copy ${nodeLibSrc} ${nodeLibPath}`)
115
        fs.copyFileSync(nodeLibSrc, nodeLibPath)
116
    }
117
}
118

119
let cleanCmd = `npx rimraf ${outDir}`
120
let configCmd = `meson setup -Dstrip=true ${isFull ? "" : '-Dsource_set="subset"'} ${isV8 ? '-Dis_ohos_v8=true' : ''} ${outDir} --cross-file ${crossFile}`
121
let compileCmd = `meson compile -C ${outDir}`
122
let installCmd = `meson install -C ${outDir}`
123

124
execSync(cleanCmd, { cwd: nativeDir, stdio: 'inherit' })
125
if (isV8) resolveV8Deps()
126
execSync(configCmd, { cwd: nativeDir, stdio: 'inherit' })
127
console.log(`${compileCmd}`)
128
execSync(compileCmd, { cwd: nativeDir, stdio: 'inherit' })
129
console.log(`${installCmd}`)
130
execSync(installCmd, { cwd: nativeDir, stdio: 'inherit' })

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

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

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

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