@napi-rs/cli 3.0.0-alpha.95 → 3.0.0-alpha.97
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cli.js +107 -58
- package/dist/index.cjs +107 -58
- package/dist/index.js +107 -58
- package/dist/utils/target.d.ts +1 -1
- package/package.json +2 -2
- package/src/api/build.ts +140 -76
- package/src/api/templates/js-binding.ts +29 -19
- package/src/utils/target.ts +1 -1
package/dist/utils/target.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@napi-rs/cli",
|
|
3
|
-
"version": "3.0.0-alpha.
|
|
3
|
+
"version": "3.0.0-alpha.97",
|
|
4
4
|
"description": "Cli tools for napi-rs",
|
|
5
5
|
"author": "LongYinan <lynweklm@gmail.com>",
|
|
6
6
|
"homepage": "https://github.com/napi-rs/napi-rs",
|
|
@@ -132,5 +132,5 @@
|
|
|
132
132
|
"e2e/**/*.spec.ts"
|
|
133
133
|
]
|
|
134
134
|
},
|
|
135
|
-
"gitHead": "
|
|
135
|
+
"gitHead": "402679859f57e7f13a98144f3d2e529c13d2d5a4"
|
|
136
136
|
}
|
package/src/api/build.ts
CHANGED
|
@@ -514,86 +514,15 @@ class Builder {
|
|
|
514
514
|
}
|
|
515
515
|
|
|
516
516
|
if (this.target.platform === 'android') {
|
|
517
|
-
|
|
518
|
-
if (!ANDROID_NDK_LATEST_HOME) {
|
|
519
|
-
debug.warn(
|
|
520
|
-
`${colors.red(
|
|
521
|
-
'ANDROID_NDK_LATEST_HOME',
|
|
522
|
-
)} environment variable is missing`,
|
|
523
|
-
)
|
|
524
|
-
}
|
|
525
|
-
|
|
526
|
-
const targetArch = this.target.arch === 'arm' ? 'armv7a' : 'aarch64'
|
|
527
|
-
const targetPlatform =
|
|
528
|
-
this.target.arch === 'arm' ? 'androideabi24' : 'android24'
|
|
529
|
-
const hostPlatform =
|
|
530
|
-
process.platform === 'darwin'
|
|
531
|
-
? 'darwin'
|
|
532
|
-
: process.platform === 'win32'
|
|
533
|
-
? 'windows'
|
|
534
|
-
: 'linux'
|
|
535
|
-
Object.assign(this.envs, {
|
|
536
|
-
CARGO_TARGET_AARCH64_LINUX_ANDROID_LINKER: `${ANDROID_NDK_LATEST_HOME}/toolchains/llvm/prebuilt/${hostPlatform}-x86_64/bin/${targetArch}-linux-android24-clang`,
|
|
537
|
-
CARGO_TARGET_ARMV7_LINUX_ANDROIDEABI_LINKER: `${ANDROID_NDK_LATEST_HOME}/toolchains/llvm/prebuilt/${hostPlatform}-x86_64/bin/${targetArch}-linux-androideabi24-clang`,
|
|
538
|
-
TARGET_CC: `${ANDROID_NDK_LATEST_HOME}/toolchains/llvm/prebuilt/${hostPlatform}-x86_64/bin/${targetArch}-linux-${targetPlatform}-clang`,
|
|
539
|
-
TARGET_CXX: `${ANDROID_NDK_LATEST_HOME}/toolchains/llvm/prebuilt/${hostPlatform}-x86_64/bin/${targetArch}-linux-${targetPlatform}-clang++`,
|
|
540
|
-
TARGET_AR: `${ANDROID_NDK_LATEST_HOME}/toolchains/llvm/prebuilt/${hostPlatform}-x86_64/bin/llvm-ar`,
|
|
541
|
-
TARGET_RANLIB: `${ANDROID_NDK_LATEST_HOME}/toolchains/llvm/prebuilt/${hostPlatform}-x86_64/bin/llvm-ranlib`,
|
|
542
|
-
ANDROID_NDK: ANDROID_NDK_LATEST_HOME,
|
|
543
|
-
PATH: `${ANDROID_NDK_LATEST_HOME}/toolchains/llvm/prebuilt/${hostPlatform}-x86_64/bin:${process.env.PATH}`,
|
|
544
|
-
})
|
|
517
|
+
this.setAndroidEnv()
|
|
545
518
|
}
|
|
546
|
-
// END LINKER
|
|
547
519
|
|
|
548
520
|
if (this.target.platform === 'wasi') {
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
'..',
|
|
552
|
-
'lib',
|
|
553
|
-
'wasm32-wasi-threads',
|
|
554
|
-
)
|
|
555
|
-
this.envs.EMNAPI_LINK_DIR = emnapi
|
|
556
|
-
this.envs.SETJMP_LINK_DIR = setjmpLib
|
|
557
|
-
const { WASI_SDK_PATH } = process.env
|
|
521
|
+
this.setWasiEnv()
|
|
522
|
+
}
|
|
558
523
|
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
WASI_SDK_PATH,
|
|
562
|
-
'bin',
|
|
563
|
-
'wasm-ld',
|
|
564
|
-
)
|
|
565
|
-
this.envs.CARGO_TARGET_WASM32_WASIP1_LINKER = join(
|
|
566
|
-
WASI_SDK_PATH,
|
|
567
|
-
'bin',
|
|
568
|
-
'wasm-ld',
|
|
569
|
-
)
|
|
570
|
-
this.envs.CARGO_TARGET_WASM32_WASIP1_THREADS_LINKER = join(
|
|
571
|
-
WASI_SDK_PATH,
|
|
572
|
-
'bin',
|
|
573
|
-
'wasm-ld',
|
|
574
|
-
)
|
|
575
|
-
this.envs.CARGO_TARGET_WASM32_WASIP2_LINKER = join(
|
|
576
|
-
WASI_SDK_PATH,
|
|
577
|
-
'bin',
|
|
578
|
-
'wasm-ld',
|
|
579
|
-
)
|
|
580
|
-
this.setEnvIfNotExists('CC', join(WASI_SDK_PATH, 'bin', 'clang'))
|
|
581
|
-
this.setEnvIfNotExists('CXX', join(WASI_SDK_PATH, 'bin', 'clang++'))
|
|
582
|
-
this.setEnvIfNotExists('AR', join(WASI_SDK_PATH, 'bin', 'ar'))
|
|
583
|
-
this.setEnvIfNotExists('RANLIB', join(WASI_SDK_PATH, 'bin', 'ranlib'))
|
|
584
|
-
this.setEnvIfNotExists(
|
|
585
|
-
'CFLAGS',
|
|
586
|
-
`--target=wasm32-wasi-threads --sysroot=${WASI_SDK_PATH}/share/wasi-sysroot -pthread -mllvm -wasm-enable-sjlj -I${setjmpInclude}`,
|
|
587
|
-
)
|
|
588
|
-
this.setEnvIfNotExists(
|
|
589
|
-
'CXXFLAGS',
|
|
590
|
-
`--target=wasm32-wasi-threads --sysroot=${WASI_SDK_PATH}/share/wasi-sysroot -pthread -mllvm -wasm-enable-sjlj -I${setjmpInclude}`,
|
|
591
|
-
)
|
|
592
|
-
this.setEnvIfNotExists(
|
|
593
|
-
`LDFLAGS`,
|
|
594
|
-
`-fuse-ld=${WASI_SDK_PATH}/bin/wasm-ld --target=wasm32-wasi-threads`,
|
|
595
|
-
)
|
|
596
|
-
}
|
|
524
|
+
if (this.target.abi === 'ohos') {
|
|
525
|
+
this.setOpenHarmonyEnv()
|
|
597
526
|
}
|
|
598
527
|
|
|
599
528
|
debug('Set envs: ')
|
|
@@ -618,6 +547,141 @@ class Builder {
|
|
|
618
547
|
})
|
|
619
548
|
}
|
|
620
549
|
|
|
550
|
+
private setAndroidEnv() {
|
|
551
|
+
const { ANDROID_NDK_LATEST_HOME } = process.env
|
|
552
|
+
if (!ANDROID_NDK_LATEST_HOME) {
|
|
553
|
+
debug.warn(
|
|
554
|
+
`${colors.red(
|
|
555
|
+
'ANDROID_NDK_LATEST_HOME',
|
|
556
|
+
)} environment variable is missing`,
|
|
557
|
+
)
|
|
558
|
+
}
|
|
559
|
+
|
|
560
|
+
// skip cross compile setup if host is android
|
|
561
|
+
if (process.platform === 'android') {
|
|
562
|
+
return
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
const targetArch = this.target.arch === 'arm' ? 'armv7a' : 'aarch64'
|
|
566
|
+
const targetPlatform =
|
|
567
|
+
this.target.arch === 'arm' ? 'androideabi24' : 'android24'
|
|
568
|
+
const hostPlatform =
|
|
569
|
+
process.platform === 'darwin'
|
|
570
|
+
? 'darwin'
|
|
571
|
+
: process.platform === 'win32'
|
|
572
|
+
? 'windows'
|
|
573
|
+
: 'linux'
|
|
574
|
+
Object.assign(this.envs, {
|
|
575
|
+
CARGO_TARGET_AARCH64_LINUX_ANDROID_LINKER: `${ANDROID_NDK_LATEST_HOME}/toolchains/llvm/prebuilt/${hostPlatform}-x86_64/bin/${targetArch}-linux-android24-clang`,
|
|
576
|
+
CARGO_TARGET_ARMV7_LINUX_ANDROIDEABI_LINKER: `${ANDROID_NDK_LATEST_HOME}/toolchains/llvm/prebuilt/${hostPlatform}-x86_64/bin/${targetArch}-linux-androideabi24-clang`,
|
|
577
|
+
TARGET_CC: `${ANDROID_NDK_LATEST_HOME}/toolchains/llvm/prebuilt/${hostPlatform}-x86_64/bin/${targetArch}-linux-${targetPlatform}-clang`,
|
|
578
|
+
TARGET_CXX: `${ANDROID_NDK_LATEST_HOME}/toolchains/llvm/prebuilt/${hostPlatform}-x86_64/bin/${targetArch}-linux-${targetPlatform}-clang++`,
|
|
579
|
+
TARGET_AR: `${ANDROID_NDK_LATEST_HOME}/toolchains/llvm/prebuilt/${hostPlatform}-x86_64/bin/llvm-ar`,
|
|
580
|
+
TARGET_RANLIB: `${ANDROID_NDK_LATEST_HOME}/toolchains/llvm/prebuilt/${hostPlatform}-x86_64/bin/llvm-ranlib`,
|
|
581
|
+
ANDROID_NDK: ANDROID_NDK_LATEST_HOME,
|
|
582
|
+
PATH: `${ANDROID_NDK_LATEST_HOME}/toolchains/llvm/prebuilt/${hostPlatform}-x86_64/bin${process.platform === 'win32' ? ';' : ':'}${process.env.PATH}`,
|
|
583
|
+
})
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
private setWasiEnv() {
|
|
587
|
+
const emnapi = join(
|
|
588
|
+
require.resolve('emnapi'),
|
|
589
|
+
'..',
|
|
590
|
+
'lib',
|
|
591
|
+
'wasm32-wasi-threads',
|
|
592
|
+
)
|
|
593
|
+
this.envs.EMNAPI_LINK_DIR = emnapi
|
|
594
|
+
this.envs.SETJMP_LINK_DIR = setjmpLib
|
|
595
|
+
const { WASI_SDK_PATH } = process.env
|
|
596
|
+
|
|
597
|
+
if (WASI_SDK_PATH && existsSync(WASI_SDK_PATH)) {
|
|
598
|
+
this.envs.CARGO_TARGET_WASM32_WASI_PREVIEW1_THREADS_LINKER = join(
|
|
599
|
+
WASI_SDK_PATH,
|
|
600
|
+
'bin',
|
|
601
|
+
'wasm-ld',
|
|
602
|
+
)
|
|
603
|
+
this.envs.CARGO_TARGET_WASM32_WASIP1_LINKER = join(
|
|
604
|
+
WASI_SDK_PATH,
|
|
605
|
+
'bin',
|
|
606
|
+
'wasm-ld',
|
|
607
|
+
)
|
|
608
|
+
this.envs.CARGO_TARGET_WASM32_WASIP1_THREADS_LINKER = join(
|
|
609
|
+
WASI_SDK_PATH,
|
|
610
|
+
'bin',
|
|
611
|
+
'wasm-ld',
|
|
612
|
+
)
|
|
613
|
+
this.envs.CARGO_TARGET_WASM32_WASIP2_LINKER = join(
|
|
614
|
+
WASI_SDK_PATH,
|
|
615
|
+
'bin',
|
|
616
|
+
'wasm-ld',
|
|
617
|
+
)
|
|
618
|
+
this.setEnvIfNotExists('TARGET_CC', join(WASI_SDK_PATH, 'bin', 'clang'))
|
|
619
|
+
this.setEnvIfNotExists(
|
|
620
|
+
'TARGET_CXX',
|
|
621
|
+
join(WASI_SDK_PATH, 'bin', 'clang++'),
|
|
622
|
+
)
|
|
623
|
+
this.setEnvIfNotExists('TARGET_AR', join(WASI_SDK_PATH, 'bin', 'ar'))
|
|
624
|
+
this.setEnvIfNotExists(
|
|
625
|
+
'TARGET_RANLIB',
|
|
626
|
+
join(WASI_SDK_PATH, 'bin', 'ranlib'),
|
|
627
|
+
)
|
|
628
|
+
this.setEnvIfNotExists(
|
|
629
|
+
'TARGET_CFLAGS',
|
|
630
|
+
`--target=wasm32-wasi-threads --sysroot=${WASI_SDK_PATH}/share/wasi-sysroot -pthread -mllvm -wasm-enable-sjlj -I${setjmpInclude}`,
|
|
631
|
+
)
|
|
632
|
+
this.setEnvIfNotExists(
|
|
633
|
+
'TARGET_CXXFLAGS',
|
|
634
|
+
`--target=wasm32-wasi-threads --sysroot=${WASI_SDK_PATH}/share/wasi-sysroot -pthread -mllvm -wasm-enable-sjlj -I${setjmpInclude}`,
|
|
635
|
+
)
|
|
636
|
+
this.setEnvIfNotExists(
|
|
637
|
+
`TARGET_LDFLAGS`,
|
|
638
|
+
`-fuse-ld=${WASI_SDK_PATH}/bin/wasm-ld --target=wasm32-wasi-threads`,
|
|
639
|
+
)
|
|
640
|
+
}
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
private setOpenHarmonyEnv() {
|
|
644
|
+
const { OHOS_SDK_PATH, OHOS_SDK_NATIVE } = process.env
|
|
645
|
+
const ndkPath = OHOS_SDK_PATH
|
|
646
|
+
? `${OHOS_SDK_NATIVE}/native`
|
|
647
|
+
: OHOS_SDK_NATIVE
|
|
648
|
+
// @ts-expect-error
|
|
649
|
+
if (!ndkPath && process.platform !== 'openharmony') {
|
|
650
|
+
debug.warn(
|
|
651
|
+
`${colors.red('OHOS_SDK_PATH')} or ${colors.red('OHOS_SDK_NATIVE')} environment variable is missing`,
|
|
652
|
+
)
|
|
653
|
+
return
|
|
654
|
+
}
|
|
655
|
+
const linkerName = `CARGO_TARGET_${this.target.triple.toUpperCase().replace(/-/g, '_')}_LINKER`
|
|
656
|
+
const ranPath = `${ndkPath}/llvm/bin/llvm-ranlib`
|
|
657
|
+
const arPath = `${ndkPath}/llvm/bin/llvm-ar`
|
|
658
|
+
const ccPath = `${ndkPath}/llvm/bin/${this.target.triple}-clang`
|
|
659
|
+
const cxxPath = `${ndkPath}/llvm/bin/${this.target.triple}-clang++`
|
|
660
|
+
const asPath = `${ndkPath}/llvm/bin/llvm-as`
|
|
661
|
+
const ldPath = `${ndkPath}/llvm/bin/ld.lld`
|
|
662
|
+
const stripPath = `${ndkPath}/llvm/bin/llvm-strip`
|
|
663
|
+
const objDumpPath = `${ndkPath}/llvm/bin/llvm-objdump`
|
|
664
|
+
const objCopyPath = `${ndkPath}/llvm/bin/llvm-objcopy`
|
|
665
|
+
const nmPath = `${ndkPath}/llvm/bin/llvm-nm`
|
|
666
|
+
const binPath = `${ndkPath}/llvm/bin`
|
|
667
|
+
const libPath = `${ndkPath}/llvm/lib`
|
|
668
|
+
|
|
669
|
+
this.setEnvIfNotExists('LIBCLANG_PATH', libPath)
|
|
670
|
+
this.setEnvIfNotExists('DEP_ATOMIC', 'clang_rt.builtins')
|
|
671
|
+
this.setEnvIfNotExists(linkerName, ccPath)
|
|
672
|
+
this.setEnvIfNotExists('TARGET_CC', ccPath)
|
|
673
|
+
this.setEnvIfNotExists('TARGET_CXX', cxxPath)
|
|
674
|
+
this.setEnvIfNotExists('TARGET_AR', arPath)
|
|
675
|
+
this.setEnvIfNotExists('TARGET_RANLIB', ranPath)
|
|
676
|
+
this.setEnvIfNotExists('TARGET_AS', asPath)
|
|
677
|
+
this.setEnvIfNotExists('TARGET_LD', ldPath)
|
|
678
|
+
this.setEnvIfNotExists('TARGET_STRIP', stripPath)
|
|
679
|
+
this.setEnvIfNotExists('TARGET_OBJDUMP', objDumpPath)
|
|
680
|
+
this.setEnvIfNotExists('TARGET_OBJCOPY', objCopyPath)
|
|
681
|
+
this.setEnvIfNotExists('TARGET_NM', nmPath)
|
|
682
|
+
this.envs.PATH = `${binPath}${process.platform === 'win32' ? ';' : ':'}${process.env.PATH}`
|
|
683
|
+
}
|
|
684
|
+
|
|
621
685
|
private setFeatures() {
|
|
622
686
|
const args = []
|
|
623
687
|
if (this.options.allFeatures && this.options.noDefaultFeatures) {
|
|
@@ -38,17 +38,19 @@ const bindingHeader = `// prettier-ignore
|
|
|
38
38
|
`
|
|
39
39
|
|
|
40
40
|
function createCommonBinding(localName: string, pkgName: string): string {
|
|
41
|
-
function requireTuple(tuple: string) {
|
|
41
|
+
function requireTuple(tuple: string, identSize = 8) {
|
|
42
|
+
const identLow = ' '.repeat(identSize - 2)
|
|
43
|
+
const ident = ' '.repeat(identSize)
|
|
42
44
|
return `try {
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
45
|
+
${ident}return require('./${localName}.${tuple}.node')
|
|
46
|
+
${identLow}} catch (e) {
|
|
47
|
+
${ident}loadErrors.push(e)
|
|
48
|
+
${identLow}}
|
|
49
|
+
${identLow}try {
|
|
50
|
+
${ident}return require('${pkgName}-${tuple}')
|
|
51
|
+
${identLow}} catch (e) {
|
|
52
|
+
${ident}loadErrors.push(e)
|
|
53
|
+
${identLow}}
|
|
52
54
|
`
|
|
53
55
|
}
|
|
54
56
|
|
|
@@ -70,6 +72,8 @@ const isMusl = () => {
|
|
|
70
72
|
return musl
|
|
71
73
|
}
|
|
72
74
|
|
|
75
|
+
const isOpenHarmony = process.platform === 'openharmony'
|
|
76
|
+
|
|
73
77
|
const isFileMusl = (f) => f.includes('libc.musl-') || f.includes('ld-musl-')
|
|
74
78
|
|
|
75
79
|
const isMuslFromFilesystem = () => {
|
|
@@ -135,7 +139,7 @@ function requireNative() {
|
|
|
135
139
|
loadErrors.push(new Error(\`Unsupported architecture on Windows: \${process.arch}\`))
|
|
136
140
|
}
|
|
137
141
|
} else if (process.platform === 'darwin') {
|
|
138
|
-
${requireTuple('darwin-universal')}
|
|
142
|
+
${requireTuple('darwin-universal', 6)}
|
|
139
143
|
if (process.arch === 'x64') {
|
|
140
144
|
${requireTuple('darwin-x64')}
|
|
141
145
|
} else if (process.arch === 'arm64') {
|
|
@@ -154,27 +158,33 @@ function requireNative() {
|
|
|
154
158
|
} else if (process.platform === 'linux') {
|
|
155
159
|
if (process.arch === 'x64') {
|
|
156
160
|
if (isMusl()) {
|
|
157
|
-
${requireTuple('linux-x64-musl')}
|
|
161
|
+
${requireTuple('linux-x64-musl', 10)}
|
|
162
|
+
} else if (isOpenHarmony) {
|
|
163
|
+
${requireTuple('linux-x64-ohos', 10)}
|
|
158
164
|
} else {
|
|
159
|
-
${requireTuple('linux-x64-gnu')}
|
|
165
|
+
${requireTuple('linux-x64-gnu', 10)}
|
|
160
166
|
}
|
|
161
167
|
} else if (process.arch === 'arm64') {
|
|
162
168
|
if (isMusl()) {
|
|
163
|
-
${requireTuple('linux-arm64-musl')}
|
|
169
|
+
${requireTuple('linux-arm64-musl', 10)}
|
|
170
|
+
} else if (isOpenHarmony) {
|
|
171
|
+
${requireTuple('linux-arm64-ohos', 10)}
|
|
164
172
|
} else {
|
|
165
|
-
${requireTuple('linux-arm64-gnu')}
|
|
173
|
+
${requireTuple('linux-arm64-gnu', 10)}
|
|
166
174
|
}
|
|
167
175
|
} else if (process.arch === 'arm') {
|
|
168
176
|
if (isMusl()) {
|
|
169
|
-
${requireTuple('linux-arm-musleabihf')}
|
|
177
|
+
${requireTuple('linux-arm-musleabihf', 10)}
|
|
178
|
+
} else if (isOpenHarmony) {
|
|
179
|
+
${requireTuple('linux-arm-ohos', 10)}
|
|
170
180
|
} else {
|
|
171
|
-
${requireTuple('linux-arm-gnueabihf')}
|
|
181
|
+
${requireTuple('linux-arm-gnueabihf', 10)}
|
|
172
182
|
}
|
|
173
183
|
} else if (process.arch === 'riscv64') {
|
|
174
184
|
if (isMusl()) {
|
|
175
|
-
${requireTuple('linux-riscv64-musl')}
|
|
185
|
+
${requireTuple('linux-riscv64-musl', 10)}
|
|
176
186
|
} else {
|
|
177
|
-
${requireTuple('linux-riscv64-gnu')}
|
|
187
|
+
${requireTuple('linux-riscv64-gnu', 10)}
|
|
178
188
|
}
|
|
179
189
|
} else if (process.arch === 'ppc64') {
|
|
180
190
|
${requireTuple('linux-ppc64-gnu')}
|
package/src/utils/target.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { execSync } from 'node:child_process'
|
|
2
2
|
|
|
3
|
-
export type Platform = NodeJS.Platform | 'wasm' | 'wasi'
|
|
3
|
+
export type Platform = NodeJS.Platform | 'wasm' | 'wasi' | 'ohos'
|
|
4
4
|
|
|
5
5
|
export const UNIVERSAL_TARGETS = {
|
|
6
6
|
'universal-apple-darwin': ['aarch64-apple-darwin', 'x86_64-apple-darwin'],
|