fast patches for napi/cli 3.0-alpha (#1553)

* fix(cli): use new napi config field

* fix(cli): avoid using node experimental feature to read self version

* fix(cli): correct linker environment

* fix(cli): missing wasi register env

* fix(cli): remove useless linker preset
This commit is contained in:
liuyi 2023-04-06 19:21:06 +08:00 committed by GitHub
parent 5daf558ead
commit c9f5ee14b7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 11 deletions

View file

@ -297,6 +297,10 @@ class Builder {
private setEnvs() {
// type definition intermediate file
this.envs.TYPE_DEF_TMP_PATH = this.getIntermediateTypeFile()
// WASI register intermediate file
this.envs.WASI_REGISTER_TMP_PATH = this.getIntermediateWasiRegisterFile()
// TODO:
// remove after napi-derive@v3 release
this.envs.CARGO_CFG_NAPI_RS_CLI_VERSION = CLI_VERSION
// RUSTFLAGS
@ -320,13 +324,18 @@ class Builder {
// END RUSTFLAGS
// LINKER
const linker = getTargetLinker(this.target.triple)
if (
linker &&
!process.env.RUSTC_LINKER &&
!process.env[`CARGET_TARGET_${targetToEnvVar(this.target.triple)}_LINKER`]
) {
this.envs.RUSTC_LINKER = linker
const linker = this.options.crossCompile
? void 0
: getTargetLinker(this.target.triple)
// TODO:
// directly set CARGO_TARGET_<target>_LINKER will cover .cargo/config.toml
// will detect by cargo config when it becomes stable
// see: https://github.com/rust-lang/cargo/issues/9301
const linkerEnv = `CARGO_TARGET_${targetToEnvVar(
this.target.triple,
)}_LINKER`
if (linker && !process.env[linkerEnv]) {
this.envs[linkerEnv] = linker
}
if (this.target.platform === 'android') {
@ -410,6 +419,17 @@ class Builder {
)
}
private getIntermediateWasiRegisterFile() {
return join(
tmpdir(),
`${this.crate.name}-${createHash('sha256')
.update(this.crate.manifest_path)
.update(CLI_VERSION)
.digest('hex')
.substring(0, 8)}.napi_wasi_register.tmp`,
)
}
private async postBuild() {
try {
debug(`Try to create output directory:`)

View file

@ -23,7 +23,7 @@ export const createPackageJson = ({
"node": "${engineRequirement}"
},
"napi": {
"name": "${binaryName}",
"binaryName": "${binaryName}",
"targets": [
${targets.map((t) => `"${t}"`).join(',\n ')}
]

View file

@ -5,7 +5,12 @@ import { promisify } from 'util'
import { debug } from './log.js'
const require = createRequire(import.meta.url)
const pkgJson = require('../../package.json')
// NOTE:
// import pkgJson from '@napi-rs/cli/package.json' assert { type: 'json' }
// is experimental feature now, avoid using it.
// see: https://nodejs.org/api/esm.html#import-assertions
// eslint-disable-next-line import/no-extraneous-dependencies
const pkgJson = require('@napi-rs/cli/package.json')
export const readFileAsync = promisify(readFile)
export const writeFileAsync = promisify(writeFile)

View file

@ -26,8 +26,6 @@ export const DEFAULT_TARGETS = [
] as const
export const TARGET_LINKER: Record<string, string> = {
'aarch64-unknown-linux-gnu': 'aarch64-linux-gnu-gcc',
'armv7-unknown-linux-gnueabihf': 'arm-linux-gnueabihf-gcc',
'aarch64-unknown-linux-musl': 'aarch64-linux-musl-gcc',
}