ARM 上的 Windows
🌐 Windows on ARM
如果你的应用运行在 Electron 6.0.8 或更高版本上,现在可以将其构建为适用于 Windows 10 on Arm。这将显著提升性能,但需要重新编译应用中使用的任何原生模块。它还可能需要对构建和打包脚本进行一些小的修复。
🌐 If your app runs with Electron 6.0.8 or later, you can now build it for Windows 10 on Arm. This considerably improves performance, but requires recompilation of any native modules used in your app. It may also require small fixups to your build and packaging scripts.
运行基本应用
🌐 Running a basic app
如果你的应用不使用任何原生模块,那么创建应用的 Arm 版本非常容易。
🌐 If your app doesn't use any native modules, then it's really easy to create an Arm version of your app.
- 确保你的应用的
node_modules目录是空的。 - 使用 命令提示符,在像往常一样运行
npm install/yarn install之前先运行set npm_config_arch=arm64。 - 如果你已经将 Electron 安装为开发依赖,npm 将下载并解压 arm64 版本。然后你可以像平常一样打包和分发你的应用。
一般考虑因素
🌐 General considerations
特定于架构的代码
🌐 Architecture-specific code
许多特定于 Windows 的代码包含 if...else 逻辑,用于在 x64 和 x86 架构之间进行选择。
🌐 Lots of Windows-specific code contains if... else logic that selects between either the x64 or x86 architectures.
if (process.arch === 'x64') {
// Do 64-bit thing...
} else {
// Do 32-bit thing...
}
如果你想针对 arm64 架构,像这样的逻辑通常会选择错误的架构,所以请仔细检查你的应用和构建脚本中是否有类似的条件。在自定义构建和打包脚本中,你应该始终检查环境中 npm_config_arch 的值,而不是依赖当前进程的架构。
🌐 If you want to target arm64, logic like this will typically select the wrong architecture, so carefully check your application and build scripts for conditions like this. In custom build and packaging scripts, you should always check the value of npm_config_arch in the environment, rather than relying on the current process arch.
原生模块
🌐 Native modules
如果你使用本地模块,必须确保它们针对 MSVC 编译器的 v142 版本进行编译(在 Visual Studio 2017 中提供)。你还必须检查本地模块提供或引用的任何预构建的 .dll 或 .lib 文件是否适用于 Windows on Arm。
🌐 If you use native modules, you must make sure that they compile against v142 of the MSVC compiler (provided in Visual Studio 2017). You must also check that any pre-built .dll or .lib files provided or referenced by the native module are available for Windows on Arm.
测试你的应用
🌐 Testing your app
要测试你的应用,请使用运行 Windows 10(版本 1903 或更高版本)的 Arm 架构 Windows 设备。确保将你的应用复制到目标设备上——当从网络位置加载应用资源时,Chromium 的沙盒将无法正确运行。
🌐 To test your app, use a Windows on Arm device running Windows 10 (version 1903 or later). Make sure that you copy your application over to the target device - Chromium's sandbox will not work correctly when loading your application assets from a network location.
开发先决条件
🌐 Development prerequisites
Node.js/node-gyp
建议使用 Node.js v12.9.0 或更高版本。 如果不想更新到新的 Node 版本,你也可以手动更新 npm 中的 node-gyp 至 5.0.2 或更高版本,该版本包含编译 Arm 原生模块所需的更改。
视觉工作室 2017
🌐 Visual Studio 2017
跨编译原生模块需要 Visual Studio 2017(任意版本)。你可以通过微软的 Visual Studio 开发者精选计划 下载 Visual Studio Community 2017。安装完成后,你可以通过在 命令提示符 中运行以下命令来添加特定于 Arm 的组件:
🌐 Visual Studio 2017 (any edition) is required for cross-compiling native modules. You can download Visual Studio Community 2017 via Microsoft's Visual Studio Dev Essentials program. After installation, you can add the Arm-specific components by running the following from a Command Prompt:
vs_installer.exe ^
--add Microsoft.VisualStudio.Workload.NativeDesktop ^
--add Microsoft.VisualStudio.Component.VC.ATLMFC ^
--add Microsoft.VisualStudio.Component.VC.Tools.ARM64 ^
--add Microsoft.VisualStudio.Component.VC.MFC.ARM64 ^
--includeRecommended
创建交叉编译命令提示符
🌐 Creating a cross-compilation command prompt
在环境中设置 npm_config_arch=arm64 会创建正确的 arm64 .obj 文件,但标准的 Visual Studio 2017 开发者命令提示符 将使用 x64 链接器。要解决此问题:
🌐 Setting npm_config_arch=arm64 in the environment creates the correct arm64 .obj files, but the standard Developer Command Prompt for VS 2017 will use the x64 linker. To fix this:
- 将 x64_x86 Cross Tools Command Prompt for VS 2017 快捷方式复制一份(例如,在开始菜单中找到它,右键点击,选择 打开文件位置,然后复制并粘贴)到一个方便的位置。
- 右键点击新快捷方式,然后选择“属性”。
- 将 Target 字段修改为在末尾显示
vcvarsamd64_arm64.bat,而不是vcvarsamd64_x86.bat。
如果成功完成,命令提示符应在启动时打印类似以下内容:
🌐 If done successfully, the command prompt should print something similar to this on startup:
**********************************************************************
** Visual Studio 2017 Developer Command Prompt v15.9.15
** Copyright (c) 2017 Microsoft Corporation
**********************************************************************
[vcvarsall.bat] Environment initialized for: 'x64_arm64'
如果你想直接在基于 Arm 的 Windows 设备上开发应用,请在 Target 中将 vcvarsx86_arm64.bat 替换,以便通过设备的 x86 模拟进行交叉编译。
🌐 If you want to develop your application directly on a Windows on Arm device, substitute vcvarsx86_arm64.bat in Target so that cross-compilation can happen with the device's x86 emulation.
链接到正确的 node.lib
🌐 Linking against the correct node.lib
默认情况下,node-gyp 会解压 Electron 的 node 头文件,并将 node.lib 的 x86 和 x64 版本下载到 %APPDATA%\..\Local\node-gyp\Cache,但不会下载 arm64 版本(对此的修复正在开发中)。要解决此问题,请执行以下操作:
🌐 By default, node-gyp unpacks Electron's node headers and downloads the x86 and x64 versions of node.lib into %APPDATA%\..\Local\node-gyp\Cache, but it does not download the arm64 version (a fix for this is in development.) To fix this:
- 从 https://electron.nodejs.cn/headers/v6.0.9/win-arm64/node.lib 下载 arm64
node.lib - 把它移到
%APPDATA%\..\Local\node-gyp\Cache\6.0.9\arm64\node.lib
将 6.0.9 替换为你正在使用的版本。
🌐 Substitute 6.0.9 for the version you're using.
交叉编译原生模块
🌐 Cross-compiling native modules
完成以上所有步骤后,打开你的交叉编译命令提示符并运行 set npm_config_arch=arm64。然后使用 npm install 正常构建你的项目。与交叉编译 x86 模块一样,如果本地模块之前是为其他架构编译的,你可能需要删除 node_modules 以强制重新编译本地模块。
🌐 After completing all of the above, open your cross-compilation command prompt and run set npm_config_arch=arm64. Then use npm install to build your project as normal. As with cross-compiling x86 modules, you may need to remove node_modules to force recompilation of native modules if they were previously compiled for another architecture.
调试原生模块
🌐 Debugging native modules
调试原生模块可以使用 Visual Studio 2017(在你的开发机器上运行)以及在目标设备上运行的对应 Visual Studio 远程调试器 来完成。调试步骤如下:
🌐 Debugging native modules can be done with Visual Studio 2017 (running on your development machine) and corresponding Visual Studio Remote Debugger running on the target device. To debug:
- 通过 命令提示符 在目标设备上启动你的应用
.exe(传递--inspect-brk以在加载任何本地模块之前暂停它)。 - 在你的开发计算机上启动 Visual Studio 2017。
- 通过选择 调试 > 附加到进程... 连接到目标设备,并输入 Visual Studio 远程调试器工具显示的设备 IP 地址和端口号。
- 点击 刷新 并选择要附加的 适当的 Electron 进程。
- 你可能需要确保你的应用中本地模块的符号已正确加载。要进行此配置,请在 Visual Studio 2017 中转到 调试 > 选项...,然后在 调试 > 符号 下添加包含你的
.pdb符号的文件夹。 - 一旦连接,设置任何适当的断点,然后使用 Chrome 的 Node 远程工具 继续执行 JavaScript。
获得额外帮助
🌐 Getting additional help
如果你在使用本文档时遇到问题,或者你的应用在为 x86 编译时可以运行,但在 arm64 上无法运行,请在标题中包含“Windows on Arm”并提交问题。
🌐 If you encounter a problem with this documentation, or if your app works when compiled for x86 but not for arm64, please file an issue with "Windows on Arm" in the title.