2020-10-15 21:16:52 +09:00
|
|
|
import { execSync } from 'child_process'
|
2022-01-04 20:26:22 +09:00
|
|
|
import { existsSync, mkdirSync } from 'fs'
|
2022-03-02 21:40:24 +09:00
|
|
|
import { tmpdir } from 'os'
|
2020-09-04 17:22:15 +09:00
|
|
|
import { join, parse, sep } from 'path'
|
2020-07-27 00:53:09 +09:00
|
|
|
|
2021-11-09 21:10:08 +09:00
|
|
|
import { Instance } from 'chalk'
|
2021-08-07 01:22:53 +09:00
|
|
|
import { Command, Option } from 'clipanion'
|
2021-12-31 12:20:55 +09:00
|
|
|
import envPaths from 'env-paths'
|
2021-11-23 20:00:31 +09:00
|
|
|
import { groupBy } from 'lodash-es'
|
2020-07-27 00:53:09 +09:00
|
|
|
import toml from 'toml'
|
|
|
|
|
2020-09-04 17:22:15 +09:00
|
|
|
import { getNapiConfig } from './consts'
|
|
|
|
import { debugFactory } from './debug'
|
2021-11-09 21:10:08 +09:00
|
|
|
import { createJsBinding } from './js-binding-template'
|
2022-01-04 20:26:22 +09:00
|
|
|
import { getDefaultTargetTriple, parseTriple } from './parse-triple'
|
2021-08-09 15:07:34 +09:00
|
|
|
import {
|
|
|
|
copyFileAsync,
|
|
|
|
mkdirAsync,
|
|
|
|
readFileAsync,
|
|
|
|
unlinkAsync,
|
2021-09-23 02:29:09 +09:00
|
|
|
writeFileAsync,
|
2021-08-09 15:07:34 +09:00
|
|
|
} from './utils'
|
2020-09-04 17:22:15 +09:00
|
|
|
|
|
|
|
const debug = debugFactory('build')
|
2021-11-09 21:10:08 +09:00
|
|
|
const chalk = new Instance({ level: 1 })
|
2020-07-27 00:53:09 +09:00
|
|
|
|
2022-01-04 20:26:22 +09:00
|
|
|
const ZIG_PLATFORM_TARGET_MAP = {
|
|
|
|
'x86_64-unknown-linux-musl': 'x86_64-linux-musl',
|
|
|
|
'x86_64-unknown-linux-gnu': 'x86_64-linux-gnu',
|
|
|
|
// Doesn't support Windows MSVC for now
|
|
|
|
// 'x86_64-pc-windows-gnu': 'x86_64-windows-gnu',
|
|
|
|
// https://github.com/ziglang/zig/issues/1759
|
|
|
|
// 'x86_64-unknown-freebsd': 'x86_64-freebsd',
|
|
|
|
'x86_64-apple-darwin': 'x86_64-macos-gnu',
|
|
|
|
'aarch64-apple-darwin': 'aarch64-macos-gnu',
|
|
|
|
'aarch64-unknown-linux-gnu': 'aarch64-linux-gnu',
|
|
|
|
'aarch64-unknown-linux-musl': 'aarch64-linux-musl',
|
|
|
|
}
|
|
|
|
|
|
|
|
function processZigLinkerArgs(platform: string, args: string[]) {
|
|
|
|
if (platform.includes('apple')) {
|
|
|
|
const newArgs = args.filter(
|
|
|
|
(arg) =>
|
|
|
|
!arg.startsWith('-Wl,-exported_symbols_list') &&
|
|
|
|
arg !== '-Wl,-dylib' &&
|
|
|
|
arg !== '-liconv',
|
|
|
|
)
|
|
|
|
newArgs.push('-Wl,"-undefined=dynamic_lookup"', '-dead_strip')
|
|
|
|
return newArgs
|
|
|
|
}
|
|
|
|
if (platform.includes('linux')) {
|
2022-01-17 15:05:52 +09:00
|
|
|
return args.map((arg) => {
|
|
|
|
if (arg === '-lgcc_s') {
|
|
|
|
return '-lunwind'
|
|
|
|
}
|
|
|
|
return arg
|
|
|
|
})
|
2022-01-04 20:26:22 +09:00
|
|
|
}
|
|
|
|
return args
|
|
|
|
}
|
|
|
|
|
2020-07-27 00:53:09 +09:00
|
|
|
export class BuildCommand extends Command {
|
|
|
|
static usage = Command.Usage({
|
2020-12-23 23:43:43 +09:00
|
|
|
description: 'Build and copy native module into specified dir',
|
2020-07-27 00:53:09 +09:00
|
|
|
})
|
|
|
|
|
2021-08-07 01:22:53 +09:00
|
|
|
static paths = [['build']]
|
2020-07-27 00:53:09 +09:00
|
|
|
|
2021-11-09 21:10:08 +09:00
|
|
|
appendPlatformToFilename = Option.Boolean(`--platform`, false, {
|
|
|
|
description: `Add platform triple to the .node file. ${chalk.green(
|
|
|
|
'[name].linux-x64-gnu.node',
|
|
|
|
)} for example`,
|
|
|
|
})
|
|
|
|
|
|
|
|
isRelease = Option.Boolean(`--release`, false, {
|
2021-12-23 18:47:43 +09:00
|
|
|
description: `Bypass to ${chalk.green('cargo build --release')}`,
|
2021-11-09 21:10:08 +09:00
|
|
|
})
|
2020-07-27 00:53:09 +09:00
|
|
|
|
2021-11-09 21:10:08 +09:00
|
|
|
configFileName?: string = Option.String('--config,-c', {
|
|
|
|
description: `napi config path, only JSON format accepted. Default to ${chalk.underline(
|
|
|
|
chalk.green('package.json'),
|
|
|
|
)}`,
|
|
|
|
})
|
2020-09-04 17:22:15 +09:00
|
|
|
|
2021-11-09 21:10:08 +09:00
|
|
|
cargoName?: string = Option.String('--cargo-name', {
|
|
|
|
description: `Override the ${chalk.green(
|
|
|
|
'name',
|
|
|
|
)} field in ${chalk.underline(chalk.yellowBright('Cargo.toml'))}`,
|
|
|
|
})
|
2020-09-05 02:20:30 +09:00
|
|
|
|
2021-11-09 21:10:08 +09:00
|
|
|
targetTripleDir = Option.String('--target', process.env.RUST_TARGET ?? '', {
|
2021-12-23 18:47:43 +09:00
|
|
|
description: `Bypass to ${chalk.green('cargo build --target')}`,
|
2021-11-09 21:10:08 +09:00
|
|
|
})
|
2020-10-15 17:14:11 +09:00
|
|
|
|
2021-11-09 21:10:08 +09:00
|
|
|
features?: string = Option.String('--features', {
|
2021-12-23 18:47:43 +09:00
|
|
|
description: `Bypass to ${chalk.green('cargo build --features')}`,
|
2021-11-09 21:10:08 +09:00
|
|
|
})
|
2020-10-15 21:16:52 +09:00
|
|
|
|
2021-11-09 21:10:08 +09:00
|
|
|
dts?: string = Option.String('--dts', 'index.d.ts', {
|
|
|
|
description: `The filename and path of ${chalk.green(
|
|
|
|
'.d.ts',
|
|
|
|
)} file, relative to cwd`,
|
|
|
|
})
|
2020-10-15 21:16:52 +09:00
|
|
|
|
2021-12-03 17:29:03 +09:00
|
|
|
project = Option.String('-p', {
|
|
|
|
description: `Bypass to ${chalk.green('cargo -p')}`,
|
|
|
|
})
|
|
|
|
|
2021-11-09 21:10:08 +09:00
|
|
|
cargoFlags = Option.String('--cargo-flags', '', {
|
2021-12-23 18:47:43 +09:00
|
|
|
description: `All the others flag passed to ${chalk.yellow('cargo build')}`,
|
2021-11-09 21:10:08 +09:00
|
|
|
})
|
2021-10-01 15:41:52 +09:00
|
|
|
|
2021-11-09 21:10:08 +09:00
|
|
|
jsBinding = Option.String('--js', 'index.js', {
|
|
|
|
description: `Path to the JS binding file, pass ${chalk.underline(
|
|
|
|
chalk.yellow('false'),
|
2021-12-23 18:47:43 +09:00
|
|
|
)} to disable it. Only affect if ${chalk.green('--target')} is specified.`,
|
2021-11-09 21:10:08 +09:00
|
|
|
})
|
2020-12-22 16:02:57 +09:00
|
|
|
|
2021-12-22 21:24:15 +09:00
|
|
|
jsPackageName = Option.String('--js-package-name', {
|
|
|
|
description: `Package name in generated js binding file, Only affect if ${chalk.green(
|
|
|
|
'--target',
|
|
|
|
)} specified and ${chalk.green('--js')} is not false.`,
|
|
|
|
required: false,
|
|
|
|
})
|
|
|
|
|
2021-11-09 21:10:08 +09:00
|
|
|
cargoCwd?: string = Option.String('--cargo-cwd', {
|
|
|
|
description: `The cwd of ${chalk.underline(
|
|
|
|
chalk.yellow('Cargo.toml'),
|
|
|
|
)} file`,
|
|
|
|
})
|
2021-08-07 01:22:53 +09:00
|
|
|
|
2021-11-16 14:45:33 +09:00
|
|
|
pipe?: string = Option.String('--pipe', {
|
|
|
|
description: `Pipe [${chalk.green(
|
|
|
|
'.js/.ts',
|
|
|
|
)}] files to this command, eg ${chalk.green('prettier -w')}`,
|
|
|
|
})
|
|
|
|
|
2021-11-19 15:58:21 +09:00
|
|
|
// https://github.com/napi-rs/napi-rs/issues/297
|
|
|
|
disableWindowsX32Optimize?: boolean = Option.Boolean(
|
|
|
|
'--disable-windows-x32-optimize',
|
|
|
|
true,
|
|
|
|
{
|
|
|
|
description: `Disable windows x32 ${chalk.green(
|
|
|
|
'lto',
|
|
|
|
)} and increase ${chalk.green(
|
|
|
|
'codegen-units',
|
|
|
|
)}. Enabled by default. See ${chalk.underline.blue(
|
|
|
|
'https://github.com/napi-rs/napi-rs/issues/297',
|
|
|
|
)}`,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
|
2021-08-07 01:22:53 +09:00
|
|
|
destDir = Option.String({
|
2020-09-04 17:22:15 +09:00
|
|
|
required: false,
|
|
|
|
})
|
2020-07-27 00:53:09 +09:00
|
|
|
|
2021-12-31 12:20:55 +09:00
|
|
|
useZig = Option.Boolean(`--zig`, false, {
|
2022-01-04 20:26:22 +09:00
|
|
|
description: `Use ${chalk.green('zig')} as linker ${chalk.yellowBright(
|
|
|
|
'(Experimental)',
|
|
|
|
)}`,
|
2021-12-31 12:20:55 +09:00
|
|
|
})
|
|
|
|
|
2022-01-13 15:26:29 +09:00
|
|
|
zigABIVersion = Option.String(`--zig-abi-suffix`, {
|
|
|
|
description: `The suffix of the ${chalk.green(
|
|
|
|
'zig --target',
|
|
|
|
)} ABI version. Eg. ${chalk.cyan(
|
|
|
|
'--target x86_64-unknown-linux-gnu',
|
|
|
|
)} ${chalk.green('--zig-abi-suffix=2.17')}`,
|
|
|
|
})
|
|
|
|
|
2022-01-13 18:00:04 +09:00
|
|
|
isStrip = Option.Boolean(`--strip`, false, {
|
|
|
|
description: `${chalk.green('Strip')} the library for minimum file size`,
|
|
|
|
})
|
|
|
|
|
2020-07-27 00:53:09 +09:00
|
|
|
async execute() {
|
2020-12-22 16:02:57 +09:00
|
|
|
const cwd = this.cargoCwd
|
|
|
|
? join(process.cwd(), this.cargoCwd)
|
|
|
|
: process.cwd()
|
2020-10-15 21:16:52 +09:00
|
|
|
const releaseFlag = this.isRelease ? `--release` : ''
|
2022-01-04 20:26:22 +09:00
|
|
|
|
2021-09-23 02:29:09 +09:00
|
|
|
const targetFlag = this.targetTripleDir
|
2020-10-15 21:16:52 +09:00
|
|
|
? `--target ${this.targetTripleDir}`
|
|
|
|
: ''
|
|
|
|
const featuresFlag = this.features ? `--features ${this.features}` : ''
|
|
|
|
const triple = this.targetTripleDir
|
|
|
|
? parseTriple(this.targetTripleDir)
|
|
|
|
: getDefaultTargetTriple(
|
|
|
|
execSync('rustup show active-toolchain', {
|
|
|
|
env: process.env,
|
|
|
|
}).toString('utf8'),
|
|
|
|
)
|
|
|
|
debug(`Current triple is: ${chalk.green(triple.raw)}`)
|
2021-12-03 17:29:03 +09:00
|
|
|
const pFlag = this.project ? `-p ${this.project}` : ''
|
2020-10-15 21:16:52 +09:00
|
|
|
const externalFlags = [
|
|
|
|
releaseFlag,
|
2021-09-23 02:29:09 +09:00
|
|
|
targetFlag,
|
2020-10-15 21:16:52 +09:00
|
|
|
featuresFlag,
|
2021-12-03 17:29:03 +09:00
|
|
|
pFlag,
|
2020-10-15 21:16:52 +09:00
|
|
|
this.cargoFlags,
|
|
|
|
]
|
|
|
|
.filter((flag) => Boolean(flag))
|
|
|
|
.join(' ')
|
|
|
|
const cargoCommand = `cargo build ${externalFlags}`
|
2022-03-02 21:40:24 +09:00
|
|
|
const intermediateTypeFile = join(tmpdir(), `type_def.${Date.now()}.tmp`)
|
2020-10-15 21:16:52 +09:00
|
|
|
debug(`Run ${chalk.green(cargoCommand)}`)
|
2021-11-19 15:58:21 +09:00
|
|
|
const additionalEnv = {}
|
|
|
|
if (
|
|
|
|
triple.arch === 'ia32' &&
|
|
|
|
triple.platform === 'win32' &&
|
|
|
|
triple.abi === 'msvc' &&
|
|
|
|
this.disableWindowsX32Optimize
|
|
|
|
) {
|
|
|
|
Object.assign(additionalEnv, {
|
2021-12-07 22:45:01 +09:00
|
|
|
CARGO_PROFILE_DEBUG_CODEGEN_UNITS: 256,
|
2021-11-19 15:58:21 +09:00
|
|
|
CARGO_PROFILE_RELEASE_CODEGEN_UNITS: 256,
|
|
|
|
CARGO_PROFILE_RELEASE_LTO: false,
|
|
|
|
})
|
|
|
|
}
|
2021-12-31 12:20:55 +09:00
|
|
|
|
2022-01-13 22:34:59 +09:00
|
|
|
const rustflags = process.env.RUSTFLAGS
|
|
|
|
? process.env.RUSTFLAGS.split(' ')
|
|
|
|
: []
|
2021-12-31 12:20:55 +09:00
|
|
|
if (triple.raw.includes('musl')) {
|
|
|
|
if (!rustflags.includes('target-feature=-crt-static')) {
|
2022-01-13 22:34:59 +09:00
|
|
|
rustflags.push('-C target-feature=-crt-static')
|
2021-12-31 12:20:55 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-13 18:00:04 +09:00
|
|
|
if (this.isStrip && !rustflags.includes('-C link-arg=-s')) {
|
2022-01-13 22:34:59 +09:00
|
|
|
rustflags.push('-C link-arg=-s')
|
2022-01-13 18:00:04 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
if (rustflags.length > 0) {
|
2022-01-13 22:34:59 +09:00
|
|
|
additionalEnv['RUSTFLAGS'] = rustflags.join(' ')
|
2022-01-13 18:00:04 +09:00
|
|
|
}
|
|
|
|
|
2022-01-04 20:26:22 +09:00
|
|
|
if (this.useZig) {
|
2022-01-13 15:26:29 +09:00
|
|
|
const zigTarget = `${ZIG_PLATFORM_TARGET_MAP[triple.raw]}${
|
|
|
|
this.zigABIVersion ? `.${this.zigABIVersion}` : ''
|
|
|
|
}`
|
2022-01-04 20:26:22 +09:00
|
|
|
if (!zigTarget) {
|
|
|
|
throw new Error(`${triple.raw} can not be cross compiled by zig`)
|
|
|
|
}
|
2021-12-31 12:20:55 +09:00
|
|
|
const paths = envPaths('napi-rs')
|
2022-01-04 20:26:22 +09:00
|
|
|
const linkerWrapperShell = join(
|
|
|
|
paths.cache,
|
|
|
|
`zig-cc-${triple.raw}.${process.platform === 'win32' ? 'bat' : 'sh'}`,
|
|
|
|
)
|
|
|
|
const linkerWrapper = join(paths.cache, `zig-cc-${triple.raw}.js`)
|
2021-12-31 12:20:55 +09:00
|
|
|
mkdirSync(paths.cache, { recursive: true })
|
2022-01-04 20:26:22 +09:00
|
|
|
const forwardArgs = process.platform === 'win32' ? '%*' : '$@'
|
|
|
|
await writeFileAsync(
|
|
|
|
linkerWrapperShell,
|
2022-01-18 14:41:01 +09:00
|
|
|
`#!/bin/sh\nnode ${linkerWrapper} ${forwardArgs}`,
|
2022-01-04 20:26:22 +09:00
|
|
|
{
|
|
|
|
mode: '777',
|
|
|
|
},
|
|
|
|
)
|
|
|
|
await writeFileAsync(
|
2021-12-31 12:20:55 +09:00
|
|
|
linkerWrapper,
|
2022-01-04 20:26:22 +09:00
|
|
|
`#!/usr/bin/env node\nconst{writeFileSync} = require('fs')\n${processZigLinkerArgs.toString()}\nconst {status} = require('child_process').spawnSync('zig', ['${
|
|
|
|
triple.platform === 'win32' ? 'c++' : 'cc'
|
|
|
|
}', ...processZigLinkerArgs('${
|
|
|
|
triple.raw
|
2022-01-13 19:21:20 +09:00
|
|
|
}', process.argv.slice(2)), '-target', '${zigTarget}'], { stdio: 'inherit', shell: true })\nwriteFileSync('${linkerWrapper.replaceAll(
|
2022-01-04 20:26:22 +09:00
|
|
|
'\\',
|
|
|
|
'/',
|
|
|
|
)}.args.log', process.argv.slice(2).join(' '))\n\nprocess.exit(status || 0)\n`,
|
|
|
|
{
|
|
|
|
mode: '777',
|
|
|
|
},
|
2021-12-31 12:20:55 +09:00
|
|
|
)
|
|
|
|
const envTarget = triple.raw.replaceAll('-', '_').toUpperCase()
|
|
|
|
Object.assign(additionalEnv, {
|
2022-01-04 20:26:22 +09:00
|
|
|
CC: `zig cc -target ${zigTarget}`,
|
|
|
|
CXX: `zig c++ -target ${zigTarget}`,
|
|
|
|
TARGET_CC: `zig cc -target ${zigTarget}`,
|
2021-12-31 12:20:55 +09:00
|
|
|
TARGET_CXX: `zig c++ -target ${zigTarget}`,
|
|
|
|
})
|
2022-01-04 20:26:22 +09:00
|
|
|
additionalEnv[`CARGO_TARGET_${envTarget}_LINKER`] = linkerWrapperShell
|
2021-12-31 12:20:55 +09:00
|
|
|
}
|
|
|
|
|
2020-10-15 21:16:52 +09:00
|
|
|
execSync(cargoCommand, {
|
2021-09-23 02:29:09 +09:00
|
|
|
env: {
|
|
|
|
...process.env,
|
2021-11-19 15:58:21 +09:00
|
|
|
...additionalEnv,
|
2021-09-23 02:29:09 +09:00
|
|
|
TYPE_DEF_TMP_PATH: intermediateTypeFile,
|
|
|
|
},
|
2020-10-15 21:16:52 +09:00
|
|
|
stdio: 'inherit',
|
2020-12-22 16:02:57 +09:00
|
|
|
cwd,
|
2020-10-15 21:16:52 +09:00
|
|
|
})
|
2021-11-09 21:10:08 +09:00
|
|
|
const { binaryName, packageName } = getNapiConfig(this.configFileName)
|
2020-09-05 02:20:30 +09:00
|
|
|
let dylibName = this.cargoName
|
|
|
|
if (!dylibName) {
|
|
|
|
let tomlContentString: string
|
|
|
|
let tomlContent: any
|
|
|
|
try {
|
|
|
|
debug('Start read toml')
|
|
|
|
tomlContentString = await readFileAsync(
|
2020-12-22 16:02:57 +09:00
|
|
|
join(cwd, 'Cargo.toml'),
|
2020-09-05 02:20:30 +09:00
|
|
|
'utf-8',
|
|
|
|
)
|
|
|
|
} catch {
|
2020-12-22 16:02:57 +09:00
|
|
|
throw new TypeError(`Could not find Cargo.toml in ${cwd}`)
|
2020-09-05 02:20:30 +09:00
|
|
|
}
|
2020-07-27 00:53:09 +09:00
|
|
|
|
2020-09-05 02:20:30 +09:00
|
|
|
try {
|
|
|
|
debug('Start parse toml')
|
|
|
|
tomlContent = toml.parse(tomlContentString)
|
|
|
|
} catch {
|
|
|
|
throw new TypeError('Could not parse the Cargo.toml')
|
|
|
|
}
|
2020-09-04 17:22:15 +09:00
|
|
|
|
2020-10-10 01:14:09 +09:00
|
|
|
if (tomlContent.package?.name) {
|
2020-09-05 02:20:30 +09:00
|
|
|
dylibName = tomlContent.package.name.replace(/-/g, '_')
|
|
|
|
} else {
|
|
|
|
throw new TypeError('No package.name field in Cargo.toml')
|
|
|
|
}
|
2020-12-22 16:14:29 +09:00
|
|
|
|
|
|
|
if (!tomlContent.lib?.['crate-type']?.includes?.('cdylib')) {
|
|
|
|
throw new TypeError(
|
2021-12-23 04:10:44 +09:00
|
|
|
`Missing ${chalk.green('crate-type = ["cdylib"]')} in ${chalk.green(
|
2020-12-22 16:14:29 +09:00
|
|
|
'[lib]',
|
|
|
|
)}`,
|
|
|
|
)
|
|
|
|
}
|
2020-07-27 00:53:09 +09:00
|
|
|
}
|
|
|
|
|
2020-09-04 17:22:15 +09:00
|
|
|
debug(`Dylib name: ${chalk.greenBright(dylibName)}`)
|
|
|
|
|
2020-12-09 16:49:05 +09:00
|
|
|
const platform = triple.platform
|
2020-07-27 00:53:09 +09:00
|
|
|
let libExt
|
2020-09-04 17:22:15 +09:00
|
|
|
|
|
|
|
debug(`Platform: ${chalk.greenBright(platform)}`)
|
2020-07-27 00:53:09 +09:00
|
|
|
|
|
|
|
// Platform based massaging for build commands
|
|
|
|
switch (platform) {
|
|
|
|
case 'darwin':
|
|
|
|
libExt = '.dylib'
|
2020-09-04 17:22:15 +09:00
|
|
|
dylibName = `lib${dylibName}`
|
2020-07-27 00:53:09 +09:00
|
|
|
break
|
|
|
|
case 'win32':
|
|
|
|
libExt = '.dll'
|
|
|
|
break
|
|
|
|
case 'linux':
|
2020-10-14 01:08:22 +09:00
|
|
|
case 'freebsd':
|
2020-10-15 21:16:52 +09:00
|
|
|
case 'openbsd':
|
2020-12-09 16:49:05 +09:00
|
|
|
case 'android':
|
2020-10-15 21:16:52 +09:00
|
|
|
case 'sunos':
|
2020-09-04 17:22:15 +09:00
|
|
|
dylibName = `lib${dylibName}`
|
2020-07-27 00:53:09 +09:00
|
|
|
libExt = '.so'
|
|
|
|
break
|
|
|
|
default:
|
2020-09-04 17:22:15 +09:00
|
|
|
throw new TypeError(
|
2020-07-27 00:53:09 +09:00
|
|
|
'Operating system not currently supported or recognized by the build script',
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-08-07 01:22:53 +09:00
|
|
|
const targetRootDir = await findUp(cwd)
|
|
|
|
|
|
|
|
if (!targetRootDir) {
|
|
|
|
throw new TypeError('No target dir found')
|
|
|
|
}
|
|
|
|
|
2020-10-15 17:14:11 +09:00
|
|
|
const targetDir = join(
|
|
|
|
this.targetTripleDir,
|
|
|
|
this.isRelease ? 'release' : 'debug',
|
|
|
|
)
|
2020-07-27 00:53:09 +09:00
|
|
|
|
2020-08-21 22:51:14 +09:00
|
|
|
const platformName = this.appendPlatformToFilename
|
2020-10-15 21:16:52 +09:00
|
|
|
? `.${triple.platformArchABI}`
|
2020-07-27 00:53:09 +09:00
|
|
|
: ''
|
|
|
|
|
2020-10-15 21:16:52 +09:00
|
|
|
debug(`Platform name: ${platformName || chalk.green('[Empty]')}`)
|
2021-08-07 01:22:53 +09:00
|
|
|
const distFileName = `${binaryName}${platformName}.node`
|
|
|
|
|
2021-08-09 15:07:34 +09:00
|
|
|
const distModulePath = join(this.destDir ?? '.', distFileName)
|
2020-07-27 00:53:09 +09:00
|
|
|
|
2020-09-04 17:22:15 +09:00
|
|
|
const parsedDist = parse(distModulePath)
|
2020-07-27 00:53:09 +09:00
|
|
|
|
2021-11-21 22:43:28 +09:00
|
|
|
if (parsedDist.dir && !existsSync(parsedDist.dir)) {
|
2021-08-09 15:07:34 +09:00
|
|
|
await mkdirAsync(parsedDist.dir, { recursive: true }).catch((e) => {
|
|
|
|
console.warn(
|
|
|
|
chalk.bgYellowBright(
|
|
|
|
`Create dir [${parsedDist.dir}] failed, reason: ${e.message}`,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
})
|
2020-07-27 00:53:09 +09:00
|
|
|
}
|
|
|
|
|
2021-08-07 01:22:53 +09:00
|
|
|
const sourcePath = join(
|
|
|
|
targetRootDir,
|
|
|
|
'target',
|
|
|
|
targetDir,
|
|
|
|
`${dylibName}${libExt}`,
|
|
|
|
)
|
2020-09-04 17:22:15 +09:00
|
|
|
|
2021-11-21 22:43:28 +09:00
|
|
|
if (existsSync(distModulePath)) {
|
2021-08-07 01:22:53 +09:00
|
|
|
debug(`remove old binary [${chalk.yellowBright(distModulePath)}]`)
|
2021-01-08 00:26:41 +09:00
|
|
|
await unlinkAsync(distModulePath)
|
|
|
|
}
|
2020-09-04 17:22:15 +09:00
|
|
|
|
|
|
|
debug(`Write binary content to [${chalk.yellowBright(distModulePath)}]`)
|
2021-01-08 00:26:41 +09:00
|
|
|
await copyFileAsync(sourcePath, distModulePath)
|
2021-09-23 02:29:09 +09:00
|
|
|
|
2021-11-16 14:45:33 +09:00
|
|
|
const dtsFilePath = join(
|
|
|
|
process.cwd(),
|
|
|
|
this.destDir ?? '.',
|
|
|
|
this.dts ?? 'index.d.ts',
|
|
|
|
)
|
|
|
|
|
2021-11-09 21:10:08 +09:00
|
|
|
const idents = await processIntermediateTypeFile(
|
2021-09-23 02:29:09 +09:00
|
|
|
intermediateTypeFile,
|
2021-11-16 14:45:33 +09:00
|
|
|
dtsFilePath,
|
2021-09-23 02:29:09 +09:00
|
|
|
)
|
2021-11-16 14:45:33 +09:00
|
|
|
if (this.pipe) {
|
|
|
|
const pipeCommand = `${this.pipe} ${dtsFilePath}`
|
|
|
|
console.info(`Run ${chalk.green(pipeCommand)}`)
|
|
|
|
try {
|
|
|
|
execSync(pipeCommand, { stdio: 'inherit', env: process.env })
|
|
|
|
} catch (e) {
|
|
|
|
console.warn(
|
|
|
|
chalk.bgYellowBright('Pipe the dts file to command failed'),
|
|
|
|
e,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const jsBindingFilePath =
|
2021-12-18 16:40:38 +09:00
|
|
|
this.jsBinding &&
|
|
|
|
this.jsBinding !== 'false' &&
|
|
|
|
this.appendPlatformToFilename
|
2021-11-09 21:10:08 +09:00
|
|
|
? join(process.cwd(), this.jsBinding)
|
2021-11-16 14:45:33 +09:00
|
|
|
: null
|
2021-12-22 21:24:15 +09:00
|
|
|
await writeJsBinding(
|
|
|
|
binaryName,
|
|
|
|
this.jsPackageName ?? packageName,
|
|
|
|
jsBindingFilePath,
|
|
|
|
idents,
|
|
|
|
)
|
2021-11-16 14:45:33 +09:00
|
|
|
if (this.pipe && jsBindingFilePath) {
|
|
|
|
const pipeCommand = `${this.pipe} ${jsBindingFilePath}`
|
|
|
|
console.info(`Run ${chalk.green(pipeCommand)}`)
|
|
|
|
try {
|
|
|
|
execSync(pipeCommand, { stdio: 'inherit', env: process.env })
|
|
|
|
} catch (e) {
|
|
|
|
console.warn(
|
|
|
|
chalk.bgYellowBright('Pipe the js binding file to command failed'),
|
|
|
|
e,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2020-07-27 00:53:09 +09:00
|
|
|
}
|
|
|
|
}
|
2020-09-04 17:22:15 +09:00
|
|
|
|
|
|
|
async function findUp(dir = process.cwd()): Promise<string | null> {
|
|
|
|
const dist = join(dir, 'target')
|
2021-11-21 22:43:28 +09:00
|
|
|
if (existsSync(dist)) {
|
2020-09-04 17:22:15 +09:00
|
|
|
return dir
|
|
|
|
}
|
|
|
|
const dirs = dir.split(sep)
|
|
|
|
if (dirs.length < 2) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
dirs.pop()
|
|
|
|
return findUp(dirs.join(sep))
|
|
|
|
}
|
2021-09-23 02:29:09 +09:00
|
|
|
|
|
|
|
interface TypeDef {
|
2021-09-28 01:01:19 +09:00
|
|
|
kind: 'fn' | 'struct' | 'impl' | 'enum' | 'interface'
|
2021-09-23 02:29:09 +09:00
|
|
|
name: string
|
2021-12-25 18:35:47 +09:00
|
|
|
original_name?: string
|
2021-09-23 02:29:09 +09:00
|
|
|
def: string
|
2021-11-23 20:00:31 +09:00
|
|
|
js_mod?: string
|
2021-11-29 13:54:45 +09:00
|
|
|
js_doc: string
|
2021-09-23 02:29:09 +09:00
|
|
|
}
|
|
|
|
|
2021-11-09 21:10:08 +09:00
|
|
|
async function processIntermediateTypeFile(
|
|
|
|
source: string,
|
|
|
|
target: string,
|
|
|
|
): Promise<string[]> {
|
|
|
|
const idents: string[] = []
|
2021-11-21 22:43:28 +09:00
|
|
|
if (!existsSync(source)) {
|
2021-09-23 02:29:09 +09:00
|
|
|
debug(`do not find tmp type file. skip type generation`)
|
2021-11-09 21:10:08 +09:00
|
|
|
return idents
|
2021-09-23 02:29:09 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
const tmpFile = await readFileAsync(source, 'utf8')
|
|
|
|
const lines = tmpFile
|
|
|
|
.split('\n')
|
|
|
|
.map((line) => line.trim())
|
|
|
|
.filter(Boolean)
|
2021-12-18 15:12:46 +09:00
|
|
|
|
|
|
|
if (!lines.length) {
|
|
|
|
return idents
|
|
|
|
}
|
|
|
|
|
2021-11-23 20:00:31 +09:00
|
|
|
const allDefs = lines.map((line) => JSON.parse(line) as TypeDef)
|
|
|
|
|
|
|
|
function convertDefs(defs: TypeDef[], nested = false): string {
|
2021-12-25 18:35:47 +09:00
|
|
|
const classes = new Map<
|
|
|
|
string,
|
|
|
|
{ def: string; js_doc: string; original_name?: string }
|
|
|
|
>()
|
2021-11-23 20:00:31 +09:00
|
|
|
const impls = new Map<string, string>()
|
|
|
|
let dts = ''
|
2021-11-29 13:54:45 +09:00
|
|
|
const nest = nested ? 2 : 0
|
|
|
|
|
2021-11-23 20:00:31 +09:00
|
|
|
defs.forEach((def) => {
|
|
|
|
switch (def.kind) {
|
|
|
|
case 'struct':
|
|
|
|
if (!nested) {
|
|
|
|
idents.push(def.name)
|
|
|
|
}
|
2021-12-25 18:35:47 +09:00
|
|
|
classes.set(def.name, {
|
|
|
|
original_name: def.original_name,
|
|
|
|
def: def.def,
|
|
|
|
js_doc: def.js_doc,
|
|
|
|
})
|
2021-11-23 20:00:31 +09:00
|
|
|
break
|
|
|
|
case 'impl':
|
2021-11-29 13:54:45 +09:00
|
|
|
impls.set(def.name, `${def.js_doc}${def.def}`)
|
2021-11-23 20:00:31 +09:00
|
|
|
break
|
|
|
|
case 'interface':
|
2021-11-29 13:54:45 +09:00
|
|
|
dts +=
|
|
|
|
indentLines(`${def.js_doc}export interface ${def.name} {`, nest) +
|
|
|
|
'\n'
|
|
|
|
dts += indentLines(def.def, nest + 2) + '\n'
|
|
|
|
dts += indentLines(`}`, nest) + '\n'
|
|
|
|
break
|
|
|
|
case 'enum':
|
2021-12-02 16:46:20 +09:00
|
|
|
if (!nested) {
|
|
|
|
idents.push(def.name)
|
|
|
|
}
|
2021-11-29 13:54:45 +09:00
|
|
|
dts +=
|
2021-12-09 19:48:50 +09:00
|
|
|
indentLines(`${def.js_doc}export const enum ${def.name} {`, nest) +
|
|
|
|
'\n'
|
2021-11-29 13:54:45 +09:00
|
|
|
dts += indentLines(def.def, nest + 2) + '\n'
|
|
|
|
dts += indentLines(`}`, nest) + '\n'
|
2021-11-23 20:00:31 +09:00
|
|
|
break
|
|
|
|
default:
|
|
|
|
if (!nested) {
|
|
|
|
idents.push(def.name)
|
|
|
|
}
|
2021-11-29 13:54:45 +09:00
|
|
|
dts += indentLines(`${def.js_doc}${def.def}`, nest) + '\n'
|
2021-11-23 20:00:31 +09:00
|
|
|
}
|
|
|
|
})
|
2021-09-23 02:29:09 +09:00
|
|
|
|
2021-12-25 18:35:47 +09:00
|
|
|
for (const [name, { js_doc, def, original_name }] of classes.entries()) {
|
2021-11-23 20:00:31 +09:00
|
|
|
const implDef = impls.get(name)
|
2021-09-23 02:29:09 +09:00
|
|
|
|
2021-12-25 18:35:47 +09:00
|
|
|
if (original_name && name !== original_name) {
|
|
|
|
dts += indentLines(`export type ${original_name} = ${name}\n`, nest)
|
|
|
|
}
|
|
|
|
|
2021-11-29 13:54:45 +09:00
|
|
|
dts += indentLines(`${js_doc}export class ${name} {`, nest)
|
|
|
|
|
|
|
|
if (def) {
|
|
|
|
dts += '\n' + indentLines(def, nest + 2)
|
|
|
|
}
|
2021-09-23 02:29:09 +09:00
|
|
|
|
2021-11-23 20:00:31 +09:00
|
|
|
if (implDef) {
|
2021-11-29 13:54:45 +09:00
|
|
|
dts += '\n' + indentLines(implDef, nest + 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (def || implDef) {
|
|
|
|
dts += '\n'
|
|
|
|
} else {
|
|
|
|
dts += ` `
|
2021-11-23 20:00:31 +09:00
|
|
|
}
|
2021-09-24 18:01:54 +09:00
|
|
|
|
2021-11-29 13:54:45 +09:00
|
|
|
dts += indentLines(`}`, nest) + '\n'
|
2021-09-24 18:01:54 +09:00
|
|
|
}
|
2021-11-29 13:54:45 +09:00
|
|
|
|
2021-11-23 20:00:31 +09:00
|
|
|
return dts
|
2021-09-23 02:29:09 +09:00
|
|
|
}
|
|
|
|
|
2021-11-23 20:00:31 +09:00
|
|
|
const topLevelDef = convertDefs(allDefs.filter((def) => !def.js_mod))
|
|
|
|
|
|
|
|
const namespaceDefs = Object.entries(
|
|
|
|
groupBy(
|
|
|
|
allDefs.filter((def) => def.js_mod),
|
|
|
|
'js_mod',
|
|
|
|
),
|
|
|
|
).reduce((acc, [mod, defs]) => {
|
|
|
|
idents.push(mod)
|
2021-11-29 13:54:45 +09:00
|
|
|
return acc + `export namespace ${mod} {\n${convertDefs(defs, true)}}\n`
|
2021-11-23 20:00:31 +09:00
|
|
|
}, '')
|
|
|
|
|
2022-02-10 19:37:04 +09:00
|
|
|
const dtsHeader = `/* tslint:disable */
|
|
|
|
/* eslint-disable */
|
|
|
|
|
|
|
|
/* auto-generated by NAPI-RS */\n
|
|
|
|
`
|
|
|
|
|
|
|
|
const externalDef =
|
|
|
|
topLevelDef.indexOf('ExternalObject<') > -1 ||
|
|
|
|
namespaceDefs.indexOf('ExternalObject<') > -1
|
|
|
|
? `export class ExternalObject<T> {
|
|
|
|
readonly '': {
|
|
|
|
readonly '': unique symbol
|
|
|
|
[K: symbol]: T
|
|
|
|
}
|
|
|
|
}\n`
|
|
|
|
: ''
|
|
|
|
|
2021-09-23 02:29:09 +09:00
|
|
|
await unlinkAsync(source)
|
2022-02-10 19:37:04 +09:00
|
|
|
await writeFileAsync(
|
|
|
|
target,
|
|
|
|
dtsHeader + externalDef + topLevelDef + namespaceDefs,
|
|
|
|
'utf8',
|
|
|
|
)
|
2021-11-09 21:10:08 +09:00
|
|
|
return idents
|
2021-09-23 02:29:09 +09:00
|
|
|
}
|
2021-09-28 01:01:19 +09:00
|
|
|
|
|
|
|
function indentLines(input: string, spaces: number) {
|
|
|
|
return input
|
|
|
|
.split('\n')
|
2021-11-29 13:54:45 +09:00
|
|
|
.map(
|
|
|
|
(line) =>
|
|
|
|
''.padEnd(spaces, ' ') +
|
|
|
|
(line.startsWith(' *') ? line.trimEnd() : line.trim()),
|
|
|
|
)
|
2021-09-28 01:01:19 +09:00
|
|
|
.join('\n')
|
|
|
|
}
|
2021-11-09 21:10:08 +09:00
|
|
|
|
|
|
|
async function writeJsBinding(
|
|
|
|
localName: string,
|
|
|
|
packageName: string,
|
|
|
|
distFileName: string | null,
|
|
|
|
idents: string[],
|
|
|
|
) {
|
2021-12-18 15:12:46 +09:00
|
|
|
if (distFileName && idents.length) {
|
2021-11-09 21:10:08 +09:00
|
|
|
const template = createJsBinding(localName, packageName)
|
|
|
|
const declareCodes = `const { ${idents.join(', ')} } = nativeBinding\n`
|
2021-11-29 13:54:45 +09:00
|
|
|
const exportsCode = idents.reduce(
|
|
|
|
(acc, cur) => `${acc}\nmodule.exports.${cur} = ${cur}`,
|
|
|
|
'',
|
|
|
|
)
|
2021-11-09 21:10:08 +09:00
|
|
|
await writeFileAsync(
|
|
|
|
distFileName,
|
2021-11-16 14:10:03 +09:00
|
|
|
template + declareCodes + exportsCode + '\n',
|
2021-11-09 21:10:08 +09:00
|
|
|
'utf8',
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|