From 7faf4fc4cc3b2e9dc47c892a9acf9bcf7e0571ad Mon Sep 17 00:00:00 2001 From: LongYinan Date: Tue, 15 Nov 2022 21:55:56 +0800 Subject: [PATCH 01/15] feat(cli): auto choose the tooling for cross compiling --- cli/src/build.ts | 73 +++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 60 insertions(+), 13 deletions(-) diff --git a/cli/src/build.ts b/cli/src/build.ts index 2a99dc6d2f..c2a74d54ab 100644 --- a/cli/src/build.ts +++ b/cli/src/build.ts @@ -36,6 +36,8 @@ const ZIG_PLATFORM_TARGET_MAP = { 'aarch64-unknown-linux-musl': 'aarch64-linux-musl', } +const DEFAULT_GLIBC_TARGET = process.env.GLIBC_ABI_TARGET ?? '2.17' + function processZigLinkerArgs(platform: string, args: string[]) { if (platform.includes('apple')) { const newArgs = args.filter( @@ -158,7 +160,7 @@ export class BuildCommand extends Command { 'lto', )} and increase ${chalk.green( 'codegen-units', - )}. Enabled by default. See ${chalk.underline.blue( + )}. Disabled by default. See ${chalk.underline.blue( 'https://github.com/napi-rs/napi-rs/issues/297', )}`, }, @@ -256,7 +258,13 @@ export class BuildCommand extends Command { ] .filter((flag) => Boolean(flag)) .join(' ') - const cargo = process.env.CARGO ?? 'cargo' + const isCrossForWin = + triple.platform === 'win32' && process.platform !== 'win32' + const isCrossForLinux = + triple.platform === 'linux' && process.platform !== 'linux' + const isCrossForMacOS = + triple.platform === 'darwin' && process.platform !== 'darwin' + const cargo = process.env.CARGO ?? isCrossForWin ? 'cargo-xwin' : 'cargo' const cargoCommand = `${cargo} build ${externalFlags}` const intermediateTypeFile = join(tmpdir(), `type_def.${Date.now()}.tmp`) debug(`Run ${chalk.green(cargoCommand)}`) @@ -279,9 +287,13 @@ export class BuildCommand extends Command { additionalEnv['RUSTFLAGS'] = rustflags.join(' ') } - if (this.useZig) { + if (this.useZig || isCrossForLinux || isCrossForMacOS) { + const zigABIVersion = + this.zigABIVersion ?? (isCrossForLinux && triple.abi === 'gnu') + ? DEFAULT_GLIBC_TARGET + : null const zigTarget = `${ZIG_PLATFORM_TARGET_MAP[triple.raw]}${ - this.zigABIVersion ? `.${this.zigABIVersion}` : '' + zigABIVersion ? `.${zigABIVersion}` : '' }` if (!zigTarget) { throw new Error(`${triple.raw} can not be cross compiled by zig`) @@ -350,15 +362,50 @@ export class BuildCommand extends Command { additionalEnv[`CARGO_TARGET_${envTarget}_LINKER`] = linkerWrapperShell } - execSync(cargoCommand, { - env: { - ...process.env, - ...additionalEnv, - TYPE_DEF_TMP_PATH: intermediateTypeFile, - }, - stdio: 'inherit', - cwd, - }) + if (triple.platform === 'android') { + const { ANDROID_NDK_LATEST_HOME } = process.env + if (!ANDROID_NDK_LATEST_HOME) { + console.info( + `${chalk.yellow( + 'ANDROID_NDK_LATEST_HOME', + )} environment variable is missing`, + ) + } + const targetArch = triple.arch === 'arm' ? 'armv7a' : 'aarch64' + Object.assign(additionalEnv, { + CARGO_TARGET_AARCH64_LINUX_ANDROID_LINKER: `${ANDROID_NDK_LATEST_HOME}/toolchains/llvm/prebuilt/linux-x86_64/bin/${targetArch}-linux-android24-clang`, + CC: `${ANDROID_NDK_LATEST_HOME}/toolchains/llvm/prebuilt/linux-x86_64/bin/${targetArch}-linux-android24-clang`, + CXX: `${ANDROID_NDK_LATEST_HOME}/toolchains/llvm/prebuilt/linux-x86_64/bin/${targetArch}-linux-android24-clang++`, + AR: `${ANDROID_NDK_LATEST_HOME}/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-ar`, + PATH: `${ANDROID_NDK_LATEST_HOME}/toolchains/llvm/prebuilt/linux-x86_64/bin:${process.env.PATH}`, + }) + } + try { + execSync(cargoCommand, { + env: { + ...process.env, + ...additionalEnv, + TYPE_DEF_TMP_PATH: intermediateTypeFile, + }, + stdio: 'inherit', + cwd, + }) + } catch (e) { + if (cargo === 'cargo-xwin') { + console.warn( + `You are cross compiling ${chalk.underline( + triple.raw, + )} target on ${chalk.green(process.platform)} host`, + ) + } else if (isCrossForLinux || isCrossForMacOS) { + console.warn( + `You are cross compiling ${chalk.underline( + triple.raw, + )} on ${chalk.green(process.platform)} host`, + ) + } + throw e + } const { binaryName, packageName } = getNapiConfig(this.configFileName) let cargoArtifactName = this.cargoName if (!cargoArtifactName) { From 5e04e2e873efeefcc02eb4b81783082f236176e4 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Thu, 17 Nov 2022 21:55:13 +0800 Subject: [PATCH 02/15] chore: publish - @napi-rs/cli@2.13.0-alpha.0 --- cli/CHANGELOG.md | 6 ++++++ cli/package.json | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/cli/CHANGELOG.md b/cli/CHANGELOG.md index 7d367fb2f9..51bee33e90 100644 --- a/cli/CHANGELOG.md +++ b/cli/CHANGELOG.md @@ -3,6 +3,12 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +# [2.13.0-alpha.0](https://github.com/napi-rs/napi-rs/compare/@napi-rs/cli@2.12.1...@napi-rs/cli@2.13.0-alpha.0) (2022-11-17) + +### Features + +- **cli:** auto choose the tooling for cross compiling ([7faf4fc](https://github.com/napi-rs/napi-rs/commit/7faf4fc4cc3b2e9dc47c892a9acf9bcf7e0571ad)) + ## [2.12.1](https://github.com/napi-rs/napi-rs/compare/@napi-rs/cli@2.12.0...@napi-rs/cli@2.12.1) (2022-11-12) ### Bug Fixes diff --git a/cli/package.json b/cli/package.json index 8a8dc5d652..1d50efce23 100644 --- a/cli/package.json +++ b/cli/package.json @@ -1,6 +1,6 @@ { "name": "@napi-rs/cli", - "version": "2.12.1", + "version": "2.13.0-alpha.0", "description": "Cli tools for napi-rs", "keywords": [ "cli", From dfc791aee6482e1f9947a835ee0247a984af32e8 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Thu, 17 Nov 2022 23:59:53 +0800 Subject: [PATCH 03/15] Smater cross --- cli/src/build.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/cli/src/build.ts b/cli/src/build.ts index c2a74d54ab..0c24d6ec5b 100644 --- a/cli/src/build.ts +++ b/cli/src/build.ts @@ -258,17 +258,21 @@ export class BuildCommand extends Command { ] .filter((flag) => Boolean(flag)) .join(' ') + const additionalEnv = {} const isCrossForWin = triple.platform === 'win32' && process.platform !== 'win32' const isCrossForLinux = - triple.platform === 'linux' && process.platform !== 'linux' + triple.platform === 'linux' && + (process.platform !== 'linux' || triple.arch !== process.arch) const isCrossForMacOS = triple.platform === 'darwin' && process.platform !== 'darwin' const cargo = process.env.CARGO ?? isCrossForWin ? 'cargo-xwin' : 'cargo' + if (isCrossForWin && triple.arch === 'ia32') { + additionalEnv['XWIN_VARIANT'] = 'x86' + } const cargoCommand = `${cargo} build ${externalFlags}` const intermediateTypeFile = join(tmpdir(), `type_def.${Date.now()}.tmp`) debug(`Run ${chalk.green(cargoCommand)}`) - const additionalEnv = {} const rustflags = process.env.RUSTFLAGS ? process.env.RUSTFLAGS.split(' ') From 64cde4d4799e8ce47896696729be20dead38b783 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Fri, 18 Nov 2022 00:00:54 +0800 Subject: [PATCH 04/15] chore: publish - @napi-rs/cli@2.13.0-alpha.1 --- cli/CHANGELOG.md | 4 ++++ cli/package.json | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/cli/CHANGELOG.md b/cli/CHANGELOG.md index 51bee33e90..7e6ecfa1e8 100644 --- a/cli/CHANGELOG.md +++ b/cli/CHANGELOG.md @@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +# [2.13.0-alpha.1](https://github.com/napi-rs/napi-rs/compare/@napi-rs/cli@2.13.0-alpha.0...@napi-rs/cli@2.13.0-alpha.1) (2022-11-17) + +**Note:** Version bump only for package @napi-rs/cli + # [2.13.0-alpha.0](https://github.com/napi-rs/napi-rs/compare/@napi-rs/cli@2.12.1...@napi-rs/cli@2.13.0-alpha.0) (2022-11-17) ### Features diff --git a/cli/package.json b/cli/package.json index 1d50efce23..8afc60ea48 100644 --- a/cli/package.json +++ b/cli/package.json @@ -1,6 +1,6 @@ { "name": "@napi-rs/cli", - "version": "2.13.0-alpha.0", + "version": "2.13.0-alpha.1", "description": "Cli tools for napi-rs", "keywords": [ "cli", From 7b0f491c637f6666331215c7b401d9e658906c92 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Fri, 18 Nov 2022 00:18:39 +0800 Subject: [PATCH 05/15] Fix xwin --- cli/src/build.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/src/build.ts b/cli/src/build.ts index 0c24d6ec5b..9a1af5a82b 100644 --- a/cli/src/build.ts +++ b/cli/src/build.ts @@ -268,7 +268,7 @@ export class BuildCommand extends Command { triple.platform === 'darwin' && process.platform !== 'darwin' const cargo = process.env.CARGO ?? isCrossForWin ? 'cargo-xwin' : 'cargo' if (isCrossForWin && triple.arch === 'ia32') { - additionalEnv['XWIN_VARIANT'] = 'x86' + additionalEnv['XWIN_ARCH'] = 'x86' } const cargoCommand = `${cargo} build ${externalFlags}` const intermediateTypeFile = join(tmpdir(), `type_def.${Date.now()}.tmp`) From df5813bbe341056101906d896503f782a4850701 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Fri, 18 Nov 2022 00:19:13 +0800 Subject: [PATCH 06/15] chore: publish - @napi-rs/cli@2.13.0-alpha.2 --- cli/CHANGELOG.md | 4 ++++ cli/package.json | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/cli/CHANGELOG.md b/cli/CHANGELOG.md index 7e6ecfa1e8..da394809ae 100644 --- a/cli/CHANGELOG.md +++ b/cli/CHANGELOG.md @@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +# [2.13.0-alpha.2](https://github.com/napi-rs/napi-rs/compare/@napi-rs/cli@2.13.0-alpha.1...@napi-rs/cli@2.13.0-alpha.2) (2022-11-17) + +**Note:** Version bump only for package @napi-rs/cli + # [2.13.0-alpha.1](https://github.com/napi-rs/napi-rs/compare/@napi-rs/cli@2.13.0-alpha.0...@napi-rs/cli@2.13.0-alpha.1) (2022-11-17) **Note:** Version bump only for package @napi-rs/cli diff --git a/cli/package.json b/cli/package.json index 8afc60ea48..5131cf0411 100644 --- a/cli/package.json +++ b/cli/package.json @@ -1,6 +1,6 @@ { "name": "@napi-rs/cli", - "version": "2.13.0-alpha.1", + "version": "2.13.0-alpha.2", "description": "Cli tools for napi-rs", "keywords": [ "cli", From 3a05e7d38a8ed896657acabb41f567b61287328b Mon Sep 17 00:00:00 2001 From: LongYinan Date: Sun, 20 Nov 2022 11:01:17 +0800 Subject: [PATCH 07/15] Patch arm-features.h --- cli/src/arm-features.h.ts | 56 +++++++++++++++++++++++++++++++++++++++ cli/src/build.ts | 44 +++++++++++++++++++++++++++--- 2 files changed, 96 insertions(+), 4 deletions(-) create mode 100644 cli/src/arm-features.h.ts diff --git a/cli/src/arm-features.h.ts b/cli/src/arm-features.h.ts new file mode 100644 index 0000000000..2399f15a7f --- /dev/null +++ b/cli/src/arm-features.h.ts @@ -0,0 +1,56 @@ +export const ARM_FEATURES_H = `/* Macros to test for CPU features on ARM. Generic ARM version. +Copyright (C) 2012-2022 Free Software Foundation, Inc. +This file is part of the GNU C Library. +The GNU C Library is free software; you can redistribute it and/or +modify it under the terms of the GNU Lesser General Public +License as published by the Free Software Foundation; either +version 2.1 of the License, or (at your option) any later version. +The GNU C Library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +Lesser General Public License for more details. +You should have received a copy of the GNU Lesser General Public +License along with the GNU C Library. If not, see +. */ + +#ifndef _ARM_ARM_FEATURES_H +#define _ARM_ARM_FEATURES_H 1 + +/* An OS-specific arm-features.h file should define ARM_HAVE_VFP to +an appropriate expression for testing at runtime whether the VFP +hardware is present. We'll then redefine it to a constant if we +know at compile time that we can assume VFP. */ + +#ifndef __SOFTFP__ +/* The compiler is generating VFP instructions, so we're already +assuming the hardware exists. */ +# undef ARM_HAVE_VFP +# define ARM_HAVE_VFP 1 +#endif + +/* An OS-specific arm-features.h file may define ARM_ASSUME_NO_IWMMXT +to indicate at compile time that iWMMXt hardware is never present +at runtime (or that we never care about its state) and so need not +be checked for. */ + +/* A more-specific arm-features.h file may define ARM_ALWAYS_BX to indicate +that instructions using pc as a destination register must never be used, +so a "bx" (or "blx") instruction is always required. */ + +/* The log2 of the minimum alignment required for an address that +is the target of a computed branch (i.e. a "bx" instruction). +A more-specific arm-features.h file may define this to set a more +stringent requirement. +Using this only makes sense for code in ARM mode (where instructions +always have a fixed size of four bytes), or for Thumb-mode code that is +specifically aligning all the related branch targets to match (since +Thumb instructions might be either two or four bytes). */ +#ifndef ARM_BX_ALIGN_LOG2 +# define ARM_BX_ALIGN_LOG2 2 +#endif + +/* An OS-specific arm-features.h file may define ARM_NO_INDEX_REGISTER to +indicate that the two-register addressing modes must never be used. */ + +#endif /* arm-features.h */ +` diff --git a/cli/src/build.ts b/cli/src/build.ts index 9a1af5a82b..48684255d8 100644 --- a/cli/src/build.ts +++ b/cli/src/build.ts @@ -8,6 +8,7 @@ import { Command, Option } from 'clipanion' import envPaths from 'env-paths' import { groupBy } from 'lodash-es' +import { ARM_FEATURES_H } from './arm-features.h' import { getNapiConfig } from './consts' import { debugFactory } from './debug' import { createJsBinding } from './js-binding-template' @@ -34,10 +35,14 @@ const ZIG_PLATFORM_TARGET_MAP = { 'aarch64-apple-darwin': 'aarch64-macos', 'aarch64-unknown-linux-gnu': 'aarch64-linux-gnu', 'aarch64-unknown-linux-musl': 'aarch64-linux-musl', + 'armv7-unknown-linux-gnueabihf': 'arm-linux-gnueabihf', } const DEFAULT_GLIBC_TARGET = process.env.GLIBC_ABI_TARGET ?? '2.17' +const SHEBANG_NODE = process.platform === 'win32' ? '' : '#!/usr/bin/env node\n' +const SHEBANG_SH = process.platform === 'win32' ? '' : '#!/usr/bin/env bash\n' + function processZigLinkerArgs(platform: string, args: string[]) { if (platform.includes('apple')) { const newArgs = args.filter( @@ -319,30 +324,34 @@ export class BuildCommand extends Command { const linkerWrapper = join(paths.cache, `zig-cc-${triple.raw}.js`) mkdirSync(paths.cache, { recursive: true }) const forwardArgs = process.platform === 'win32' ? '%*' : '$@' + if (triple.arch === 'arm') { + await patchArmFeaturesHForArmTargets() + } await writeFileAsync( linkerWrapperShell, - `#!/bin/sh\nnode ${linkerWrapper} ${forwardArgs}`, + `${SHEBANG_SH}node ${linkerWrapper} ${forwardArgs}`, { mode: '777', }, ) await writeFileAsync( CCWrapperShell, - `#!/bin/sh\nzig cc -target ${zigTarget} ${forwardArgs}`, + `${SHEBANG_SH}zig cc -target ${zigTarget} ${forwardArgs}`, { mode: '777', }, ) await writeFileAsync( CXXWrapperShell, - `#!/bin/sh\nzig c++ -target ${zigTarget} ${forwardArgs}`, + `${SHEBANG_SH}zig c++ -target ${zigTarget} ${forwardArgs}`, { mode: '777', }, ) + await writeFileAsync( linkerWrapper, - `#!/usr/bin/env node\nconst{writeFileSync} = require('fs')\n${processZigLinkerArgs.toString()}\nconst {status} = require('child_process').spawnSync('zig', ['${ + `${SHEBANG_NODE}const{writeFileSync} = require('fs')\n${processZigLinkerArgs.toString()}\nconst {status} = require('child_process').spawnSync('zig', ['${ triple.platform === 'win32' ? 'c++' : 'cc' }', ...processZigLinkerArgs('${ triple.raw @@ -772,3 +781,30 @@ async function writeJsBinding( ) } } + +async function patchArmFeaturesHForArmTargets() { + let zigExePath: string + try { + const zigEnv = JSON.parse(execSync(`zig env`, { encoding: 'utf8' }).trim()) + zigExePath = zigEnv['zig_exe'] + } catch (e) { + throw new Error( + 'Cannot get zig env correctly, please ensure the zig is installed correctly on your system', + ) + } + try { + await writeFileAsync( + join(zigExePath, '../lib/libc/glibc/sysdeps/arm/arm-features.h'), + ARM_FEATURES_H, + { + mode: 0o644, + }, + ) + } catch (e) { + throw new Error( + `Cannot patch arm-features.h, error: ${ + (e as Error).message || e + }. See: https://github.com/ziglang/zig/issues/3287`, + ) + } +} From 03cd1bd71a7a70c4b9325fc4dc5280cd915d51ed Mon Sep 17 00:00:00 2001 From: LongYinan Date: Sun, 20 Nov 2022 11:53:11 +0800 Subject: [PATCH 08/15] chore: publish - @napi-rs/cli@2.13.0-alpha.3 --- cli/CHANGELOG.md | 4 ++++ cli/package.json | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/cli/CHANGELOG.md b/cli/CHANGELOG.md index da394809ae..379a4c7623 100644 --- a/cli/CHANGELOG.md +++ b/cli/CHANGELOG.md @@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +# [2.13.0-alpha.3](https://github.com/napi-rs/napi-rs/compare/@napi-rs/cli@2.13.0-alpha.2...@napi-rs/cli@2.13.0-alpha.3) (2022-11-20) + +**Note:** Version bump only for package @napi-rs/cli + # [2.13.0-alpha.2](https://github.com/napi-rs/napi-rs/compare/@napi-rs/cli@2.13.0-alpha.1...@napi-rs/cli@2.13.0-alpha.2) (2022-11-17) **Note:** Version bump only for package @napi-rs/cli diff --git a/cli/package.json b/cli/package.json index 5131cf0411..8a84b8f1a0 100644 --- a/cli/package.json +++ b/cli/package.json @@ -1,6 +1,6 @@ { "name": "@napi-rs/cli", - "version": "2.13.0-alpha.2", + "version": "2.13.0-alpha.3", "description": "Cli tools for napi-rs", "keywords": [ "cli", From 187bf03bc48be29714d74c768e097151edb82b3c Mon Sep 17 00:00:00 2001 From: LongYinan Date: Sun, 20 Nov 2022 16:33:32 +0800 Subject: [PATCH 09/15] Android armv7 fix --- cli/src/build.ts | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/cli/src/build.ts b/cli/src/build.ts index 48684255d8..61e0097914 100644 --- a/cli/src/build.ts +++ b/cli/src/build.ts @@ -41,7 +41,7 @@ const ZIG_PLATFORM_TARGET_MAP = { const DEFAULT_GLIBC_TARGET = process.env.GLIBC_ABI_TARGET ?? '2.17' const SHEBANG_NODE = process.platform === 'win32' ? '' : '#!/usr/bin/env node\n' -const SHEBANG_SH = process.platform === 'win32' ? '' : '#!/usr/bin/env bash\n' +const SHEBANG_SH = process.platform === 'win32' ? '' : '#!/usr/bin/env sh\n' function processZigLinkerArgs(platform: string, args: string[]) { if (platform.includes('apple')) { @@ -308,7 +308,7 @@ export class BuildCommand extends Command { throw new Error(`${triple.raw} can not be cross compiled by zig`) } const paths = envPaths('napi-rs') - const shellFileExt = process.platform === 'win32' ? 'bat' : 'sh' + const shellFileExt = process.platform === 'win32' ? 'cmd' : 'sh' const linkerWrapperShell = join( paths.cache, `zig-linker-${triple.raw}.${shellFileExt}`, @@ -323,13 +323,21 @@ export class BuildCommand extends Command { ) const linkerWrapper = join(paths.cache, `zig-cc-${triple.raw}.js`) mkdirSync(paths.cache, { recursive: true }) - const forwardArgs = process.platform === 'win32' ? '%*' : '$@' + const forwardArgs = process.platform === 'win32' ? '"%*"' : '$@' if (triple.arch === 'arm') { await patchArmFeaturesHForArmTargets() } await writeFileAsync( linkerWrapperShell, - `${SHEBANG_SH}node ${linkerWrapper} ${forwardArgs}`, + process.platform === 'win32' + ? `@IF EXIST "%~dp0\\node.exe" ( + "%~dp0\\node.exe" "${linkerWrapper}" %* +) ELSE ( + @SETLOCAL + @SET PATHEXT=%PATHEXT:;.JS;=;% + node "${linkerWrapper}" %* +)` + : `${SHEBANG_SH}node ${linkerWrapper} ${forwardArgs}`, { mode: '777', }, @@ -375,7 +383,7 @@ export class BuildCommand extends Command { additionalEnv[`CARGO_TARGET_${envTarget}_LINKER`] = linkerWrapperShell } - if (triple.platform === 'android') { + if (triple.platform.includes('android')) { const { ANDROID_NDK_LATEST_HOME } = process.env if (!ANDROID_NDK_LATEST_HOME) { console.info( From ddf80c05ffbbf370ca95a484470eb6fb46ab6911 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Sun, 20 Nov 2022 16:34:09 +0800 Subject: [PATCH 10/15] chore: publish - @napi-rs/cli@2.13.0-alpha.4 --- cli/CHANGELOG.md | 4 ++++ cli/package.json | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/cli/CHANGELOG.md b/cli/CHANGELOG.md index 379a4c7623..9b2803b267 100644 --- a/cli/CHANGELOG.md +++ b/cli/CHANGELOG.md @@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +# [2.13.0-alpha.4](https://github.com/napi-rs/napi-rs/compare/@napi-rs/cli@2.13.0-alpha.3...@napi-rs/cli@2.13.0-alpha.4) (2022-11-20) + +**Note:** Version bump only for package @napi-rs/cli + # [2.13.0-alpha.3](https://github.com/napi-rs/napi-rs/compare/@napi-rs/cli@2.13.0-alpha.2...@napi-rs/cli@2.13.0-alpha.3) (2022-11-20) **Note:** Version bump only for package @napi-rs/cli diff --git a/cli/package.json b/cli/package.json index 8a84b8f1a0..d32319c15a 100644 --- a/cli/package.json +++ b/cli/package.json @@ -1,6 +1,6 @@ { "name": "@napi-rs/cli", - "version": "2.13.0-alpha.3", + "version": "2.13.0-alpha.4", "description": "Cli tools for napi-rs", "keywords": [ "cli", From 54bbe60cea4a7b1eed1289ca0f217e6d9b2aaa9b Mon Sep 17 00:00:00 2001 From: LongYinan Date: Sun, 20 Nov 2022 17:56:29 +0800 Subject: [PATCH 11/15] Android armv7 fix --- cli/src/build.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/cli/src/build.ts b/cli/src/build.ts index 61e0097914..a0fe2ca307 100644 --- a/cli/src/build.ts +++ b/cli/src/build.ts @@ -382,8 +382,8 @@ export class BuildCommand extends Command { }) additionalEnv[`CARGO_TARGET_${envTarget}_LINKER`] = linkerWrapperShell } - - if (triple.platform.includes('android')) { + debug(`Platform: ${JSON.stringify(triple, null, 2)}`) + if (triple.platform === 'android') { const { ANDROID_NDK_LATEST_HOME } = process.env if (!ANDROID_NDK_LATEST_HOME) { console.info( @@ -393,10 +393,13 @@ export class BuildCommand extends Command { ) } const targetArch = triple.arch === 'arm' ? 'armv7a' : 'aarch64' + const targetPlatform = + triple.arch === 'arm' ? 'androideabi24' : 'android24' Object.assign(additionalEnv, { CARGO_TARGET_AARCH64_LINUX_ANDROID_LINKER: `${ANDROID_NDK_LATEST_HOME}/toolchains/llvm/prebuilt/linux-x86_64/bin/${targetArch}-linux-android24-clang`, - CC: `${ANDROID_NDK_LATEST_HOME}/toolchains/llvm/prebuilt/linux-x86_64/bin/${targetArch}-linux-android24-clang`, - CXX: `${ANDROID_NDK_LATEST_HOME}/toolchains/llvm/prebuilt/linux-x86_64/bin/${targetArch}-linux-android24-clang++`, + CARGO_TARGET_ARMV7_LINUX_ANDROIDEABI_LINKER: `${ANDROID_NDK_LATEST_HOME}/toolchains/llvm/prebuilt/linux-x86_64/bin/${targetArch}-linux-androideabi24-clang`, + CC: `${ANDROID_NDK_LATEST_HOME}/toolchains/llvm/prebuilt/linux-x86_64/bin/${targetArch}-linux-${targetPlatform}-clang`, + CXX: `${ANDROID_NDK_LATEST_HOME}/toolchains/llvm/prebuilt/linux-x86_64/bin/${targetArch}-linux-${targetPlatform}-clang++`, AR: `${ANDROID_NDK_LATEST_HOME}/toolchains/llvm/prebuilt/linux-x86_64/bin/llvm-ar`, PATH: `${ANDROID_NDK_LATEST_HOME}/toolchains/llvm/prebuilt/linux-x86_64/bin:${process.env.PATH}`, }) From c45f781231cf02ec2fe438b6777d3a7052c87a6c Mon Sep 17 00:00:00 2001 From: LongYinan Date: Sun, 20 Nov 2022 17:57:17 +0800 Subject: [PATCH 12/15] chore: publish - @napi-rs/cli@2.13.0-alpha.5 --- cli/CHANGELOG.md | 4 ++++ cli/package.json | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/cli/CHANGELOG.md b/cli/CHANGELOG.md index 9b2803b267..e334bd2b01 100644 --- a/cli/CHANGELOG.md +++ b/cli/CHANGELOG.md @@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +# [2.13.0-alpha.5](https://github.com/napi-rs/napi-rs/compare/@napi-rs/cli@2.13.0-alpha.4...@napi-rs/cli@2.13.0-alpha.5) (2022-11-20) + +**Note:** Version bump only for package @napi-rs/cli + # [2.13.0-alpha.4](https://github.com/napi-rs/napi-rs/compare/@napi-rs/cli@2.13.0-alpha.3...@napi-rs/cli@2.13.0-alpha.4) (2022-11-20) **Note:** Version bump only for package @napi-rs/cli diff --git a/cli/package.json b/cli/package.json index d32319c15a..5024cd0415 100644 --- a/cli/package.json +++ b/cli/package.json @@ -1,6 +1,6 @@ { "name": "@napi-rs/cli", - "version": "2.13.0-alpha.4", + "version": "2.13.0-alpha.5", "description": "Cli tools for napi-rs", "keywords": [ "cli", From a85bc3bac17ea50592bb640e61c9f4323eff024e Mon Sep 17 00:00:00 2001 From: LongYinan Date: Sun, 20 Nov 2022 20:08:07 +0800 Subject: [PATCH 13/15] use zig for musl --- cli/src/build.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/cli/src/build.ts b/cli/src/build.ts index a0fe2ca307..8eaad78b96 100644 --- a/cli/src/build.ts +++ b/cli/src/build.ts @@ -268,7 +268,15 @@ export class BuildCommand extends Command { triple.platform === 'win32' && process.platform !== 'win32' const isCrossForLinux = triple.platform === 'linux' && - (process.platform !== 'linux' || triple.arch !== process.arch) + (process.platform !== 'linux' || + triple.arch !== process.arch || + (function () { + const glibcVersionRuntime = + // @ts-expect-error + process.report?.getReport()?.header?.glibcVersionRuntime + const libc = glibcVersionRuntime ? 'gnu' : 'musl' + return triple.abi !== libc + })()) const isCrossForMacOS = triple.platform === 'darwin' && process.platform !== 'darwin' const cargo = process.env.CARGO ?? isCrossForWin ? 'cargo-xwin' : 'cargo' From 294a39b0cfe384aa434cb7fd60f56942f8223c71 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Sun, 20 Nov 2022 20:08:50 +0800 Subject: [PATCH 14/15] chore: publish - @napi-rs/cli@2.13.0-alpha.6 --- cli/CHANGELOG.md | 4 ++++ cli/package.json | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/cli/CHANGELOG.md b/cli/CHANGELOG.md index e334bd2b01..35acc11bd7 100644 --- a/cli/CHANGELOG.md +++ b/cli/CHANGELOG.md @@ -3,6 +3,10 @@ All notable changes to this project will be documented in this file. See [Conventional Commits](https://conventionalcommits.org) for commit guidelines. +# [2.13.0-alpha.6](https://github.com/napi-rs/napi-rs/compare/@napi-rs/cli@2.13.0-alpha.5...@napi-rs/cli@2.13.0-alpha.6) (2022-11-20) + +**Note:** Version bump only for package @napi-rs/cli + # [2.13.0-alpha.5](https://github.com/napi-rs/napi-rs/compare/@napi-rs/cli@2.13.0-alpha.4...@napi-rs/cli@2.13.0-alpha.5) (2022-11-20) **Note:** Version bump only for package @napi-rs/cli diff --git a/cli/package.json b/cli/package.json index 5024cd0415..948996d8b4 100644 --- a/cli/package.json +++ b/cli/package.json @@ -1,6 +1,6 @@ { "name": "@napi-rs/cli", - "version": "2.13.0-alpha.5", + "version": "2.13.0-alpha.6", "description": "Cli tools for napi-rs", "keywords": [ "cli", From 53371526df2a98174c17fcefaa7301019da939b9 Mon Sep 17 00:00:00 2001 From: LongYinan Date: Sun, 20 Nov 2022 21:33:35 +0800 Subject: [PATCH 15/15] fallback if no zig --- cli/src/build.ts | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/cli/src/build.ts b/cli/src/build.ts index 8eaad78b96..94c797716d 100644 --- a/cli/src/build.ts +++ b/cli/src/build.ts @@ -303,8 +303,27 @@ export class BuildCommand extends Command { if (rustflags.length > 0) { additionalEnv['RUSTFLAGS'] = rustflags.join(' ') } + let isZigExisted = false + if (isCrossForLinux || isCrossForMacOS) { + try { + execSync('zig version') + isZigExisted = true + } catch (e) { + if (this.useZig) { + throw new TypeError( + `Could not find ${chalk.green('zig')} on the PATH`, + ) + } else { + debug( + `Could not find ${chalk.green( + 'zig', + )} on the PATH, fallback to normal linker`, + ) + } + } + } - if (this.useZig || isCrossForLinux || isCrossForMacOS) { + if ((this.useZig || isCrossForLinux || isCrossForMacOS) && isZigExisted) { const zigABIVersion = this.zigABIVersion ?? (isCrossForLinux && triple.abi === 'gnu') ? DEFAULT_GLIBC_TARGET