16
import { execSync } from "node:child_process"
17
import { argv } from 'process'
18
import path from 'path'
19
import * as fs from "node:fs"
22
const platform = os.platform()
23
const isWindows = (platform === 'win32')
25
function crossPathResolve(inPath) {
27
return path.resolve(inPath).replace(/\\/g, '\\\\')
29
return path.resolve(inPath)
33
const nativeDir = 'native'
37
let exeSuffix = isWindows ? '.exe' : ''
39
for (let i = 2; i < argv.length; ++i) {
60
console.log(`isArm64 = ${isArm64}`)
61
console.log(`isFull = ${isFull}`)
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'
70
const sysrootDir = crossPathResolve(`${ohosSdkRoot}/${ohosSdkVersion}/base/native/sysroot`)
71
let target = `${isArm64 ? 'aarch64-linux-ohos' : 'arm-linux-ohos'}`
73
isArm64 ? [`'--target=${target}'`] : [`'--target=${target}'`, "'-m32'", "'-march=armv7-a'"]
76
let crossFileContent = `
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}`)}\'
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}]
91
cpu_family = ${isArm64 ? "'aarch64'" : "'arm'"}
92
cpu = ${isArm64 ? "'aarch64'" : "'armv7a'"}
96
fs.writeFileSync(`${nativeDir}/${crossFile}`, crossFileContent, 'utf8', (error) => {
98
console.error(`Init ${crossFile} error : `, error)
101
console.log(`Init ${crossFile} successfully`);
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' })
113
if (!fs.existsSync(nodeLibPath)) {
114
console.log(`copy ${nodeLibSrc} ${nodeLibPath}`)
115
fs.copyFileSync(nodeLibSrc, nodeLibPath)
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}`
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' })