ARM 上的 Windows
¥Windows on ARM
如果你的应用在 Electron 6.0.8 或更高版本上运行,你现在可以在 Arm 上为 Windows 10 构建它。这大大提高了性能,但需要重新编译应用中使用的任何原生模块。它还可能需要对你的构建和打包脚本进行一些小修改。
¥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
目录为空。¥Make sure that your app's
node_modules
directory is empty. -
使用命令提示符,像平常一样运行
set npm_config_arch=arm64
,然后再运行npm install
/yarn install
。¥Using a Command Prompt, run
set npm_config_arch=arm64
before runningnpm install
/yarn install
as usual. -
如果你已安装 Electron 作为开发依赖,npm 会下载并解压 arm64 版本。然后,你可以正常打包和分发你的应用。
¥If you have Electron installed as a development dependency, npm will download and unpack the arm64 version. You can then package and distribute your app as normal.
一般考虑因素
¥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
文件是否可用于 Arm 上的 Windows。
¥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 或更高版本)的 Windows on Arm 设备。确保将应用复制到目标设备 - 从网络位置加载应用资源时,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 编译原生模块所需的更改。
¥Node.js v12.9.0 or later is recommended. If updating to a new version of Node is undesirable, you can instead update npm's copy of node-gyp manually to version 5.0.2 or later, which contains the required changes to compile native modules for Arm.
视觉工作室 2017
¥Visual Studio 2017
交叉编译原生模块需要 Visual Studio 2017(任何版本)。你可以通过微软的 Visual Studio Dev Essentials 计划 下载 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
文件,但 VS 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:
-
将 VS 2017 快捷方式的 x64_x86 交叉工具命令提示符复制到方便的位置(例如,通过在开始菜单中找到它,右键单击,选择“打开文件位置”,复制并粘贴)到方便的地方。
¥Duplicate the x64_x86 Cross Tools Command Prompt for VS 2017 shortcut (e.g. by locating it in the start menu, right clicking, selecting Open File Location, copying and pasting) to somewhere convenient.
-
右键单击新快捷方式并选择属性。
¥Right click the new shortcut and choose Properties.
-
将目标字段更改为末尾读取
vcvarsamd64_arm64.bat
而不是vcvarsamd64_x86.bat
。¥Change the Target field to read
vcvarsamd64_arm64.bat
at the end instead ofvcvarsamd64_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'
如果你想直接在 Windows on Arm 设备上开发应用,请替换 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.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
¥Download the arm64
node.lib
from https://electron.nodejs.cn/headers/v6.0.9/win-arm64/node.lib -
将其移至
%APPDATA%\..\Local\node-gyp\Cache\6.0.9\arm64\node.lib
¥Move it to
%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
以暂停它)。¥Launch your app
.exe
on the target device via the Command Prompt (passing--inspect-brk
to pause it before any native modules are loaded). -
在你的开发计算机上启动 Visual Studio 2017。
¥Launch Visual Studio 2017 on your development machine.
-
通过选择“调试”>“附加到进程...”连接到目标设备并输入设备的 IP 地址和 Visual Studio 远程调试器工具显示的端口号。
¥Connect to the target device by selecting Debug > Attach to Process... and enter the device's IP address and the port number displayed by the Visual Studio Remote Debugger tool.
-
单击刷新并选择 适当的 Electron 工艺来附加。
¥Click Refresh and select the appropriate Electron process to attach.
-
你可能需要确保应用中原生模块的所有符号均已正确加载。要配置它,请转到“调试”>“选项...”在 Visual Studio 2017 中,然后在“调试”>“符号”下添加包含
.pdb
符号的文件夹。¥You may need to make sure that any symbols for native modules in your app are loaded correctly. To configure this, head to Debug > Options... in Visual Studio 2017, and add the folders containing your
.pdb
symbols under Debugging > Symbols. -
连接后,设置适当的断点并使用 Chrome 的 Node 的远程工具 恢复 JavaScript 执行。
¥Once attached, set any appropriate breakpoints and resume JavaScript execution using Chrome's remote tools for Node.
获得额外帮助
¥Getting additional help
如果你在使用本文档时遇到问题,或者你的应用在针对 x86 编译但不适用于 arm64 时可以工作,请在标题中加上 提出问题 和 "Arm 上的 Windows"。
¥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.