electron集成arm64架构的nodejs addon插件
随着macos M1芯片的发布,当前的electron PC应用要适配arm64架构(虽然低版本electron应用也可以在arm64架构上面运行(转译)但是这严重影响电脑性能)。
环境搭建
mac上面编译node需要安装xcode、node-gyp、python,node。可以根据下面链接查看到当前支持了M1架构的应用具体版本号。https://isapplesiliconready.com/zh
xcode-v12.2
node-v15.1.0
python-v3.9.1
electron-v11.1.0
查看当前本机安装的环境:
nodelee@bijiewangluodeMac-mini m1_addon % npm -v
7.0.8
nodelee@bijiewangluodeMac-mini m1_addon % node -v
v15.1.0
nodelee@bijiewangluodeMac-mini m1_addon % node-gyp -v
v7.1.2
环境测试
addon编写
强烈建议使用napi实现addon,nan api适配性很差,不同版本的electron需要重新编译,并且有些高版本的electron还不支持。
addon.cc
#include <string>
#include <assert.h>
#include <stdio.h>
#include <ctime>#define NAPI_EXPERIMENTAL
#include <node_api.h>#define NAPI_DESC(name, func) \napi_property_descriptor { name, 0, func, 0, 0, 0, napi_default, 0 }#define CHECK(expr) \{ \if ((expr == napi_ok) == 0) \{ \fprintf(stderr, "[Err] %s:%d: %s\n", __FILE__, __LINE__, #expr); \fflush(stderr); \abort(); \} \}napi_value get_value(napi_env env, napi_callback_info info)
{napi_value ret;CHECK(napi_create_int32(env, 1314, &ret));return ret;
}napi_value Init(napi_env env, napi_value exports)
{napi_property_descriptor desc;desc = NAPI_DESC("getValue", get_value);CHECK(napi_define_properties(env, exports, 1, &desc));return exports;
}
NAPI_MODULE(addon, Init)
binding.gyp
{'targets': [{"target_name": "addon", "sources": ["./addon.cc"] }]
}
index.js
var addon= require('./build/Release/addon.node');
console.log("addon get value is ", addon.getValue());
分别在intel和arm64 环境下编译
node-gyp rebuild --arch=arm64
node-gyp rebuild --arch=x64
查看intel x86上面编译的node架构
macdeMacBook-Pro:mac mac$ lipo -info addon_x64.node
Architectures in the fat file: addon_x64.node are: x86_64
查看arm64上面编译的node架构
nodelee@bijiewangluodeMac-mini Release % lipo -info addon_arm64.node
Non-fat file: addon_arm64.node is architecture: arm64
正常情况下两种架构的运行结果:
nodelee@bijiewangluodeMac-mini m1_addon % node index.js
addon get value is 1314
合并node
为了调用方便我们可以把不同架构的node合并成一个,加载的时候根据不同的架构加载不同的node。
lipo -create addon_arm64.node addon_x64.node -output addon.node
查看合并后的node架构
nodelee@bijiewangluodeMac-mini Release % lipo -info addon.node
Architectures in the fat file: addon.node are: x86_64 arm64
electron集成addon
electron高版本为了安全性打包后contextIsolation默认为true
mian.js
const {app,BrowserWindow,dialog,ipcMain
} = require("electron");
const path = require("path");// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.let win;
function createWindow() {// Create the browser window.win = new BrowserWindow({width: 800,height: 600,webPreferences: {nodeIntegration: false, // is default value after Electron v5contextIsolation: true, // protect against prototype pollution// enableRemoteModule: false, // turn off remotepreload: path.join(__dirname, "./preload.js") // use a preload script}});// Load appwin.loadFile(path.join(__dirname, "./index.html"));
}app.on("ready", createWindow);
index.html
<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head>
<script>console.log("value is ", window.api.getValue()); //
</script><body><h1>hello electron</h1>
</body></html>
package.json
{"name": "bjcast-demo","version": "1.0.18","description": "A minimal Electron application","main": "./main.js","scripts": {"dev": "electron .","build:mac": "electron-builder --mac --arm64"},"build": {"appId": "com.bjnet.demo","mac": {"target": {"target": "dir","arch": "arm64"}},},"author": "GitHub","license": "CC0-1.0","devDependencies": {"electron": "^11.1.0","electron-builder": "^22.9.1"}
}
preload.js
const {contextBridge,
} = require("electron");var addon = require('./addon.node');contextBridge.exposeInMainWorld("api", {getValue: () => {return addon.getValue();},
}
);
运行
npm run dev
打包
npm run build:mac
electron集成arm64架构的nodejs addon插件相关推荐
- 向vue中集成electron(使用electron-builder插件直接将electron集成到自己的项目)
文章目录 前言 一.创建一个vue项目 二.集成electron 三.运行项目 前言 一个完整的解决方案,用于打包和构建适用于 macOS.Windows 和 Linux 的可分发 Electron ...
- ARM64架构题目答案
1.A64指令集支持64位宽的数据和地址寻址,为什么指令的编码宽度只有32位 答:遵循简单源于规整的原则. 2.下面几条mov指令,哪些能成功,哪些会失败 mov x0, 0x1234 mov x0, ...
- docker buildx 构建arm64架构镜像
文章目录 buildx测试 前提条件 安装 构建镜像 推送http私库问题 docker容器里运行docker buildx 在x86架构下如何打包arm64架构的镜像,docker buildx 是 ...
- 安装您的Sbo Add-on插件
Sbo Add-on插件开发完成了,就需要注册到Sbo主程序中,从而实现随着Sbo主程序启动而启动,退出而退出,达到Add-on插件程序同Sbo主程序之间的一致运行. 1.运行AddOnRegData ...
- 【Linux 内核 内存管理】Linux 内核内存布局 ④ ( ARM64 架构体系内存分布 | 内核启动源码 start_kernel | 内存初始化 mm_init | mem_init )
文章目录 一.ARM64 架构体系内存分布 二.Linux 内核启动源码 start_kernel 三.内存初始化源码 mm_init 四.内存初始化源码 mem_init 一.ARM64 架构体系内 ...
- [Android]用架构师角度看插件化(2)-Replugin 唯一hook点
Replugin,为何我选择要研究这个的插件呢?很大的原因是因为它的介绍中说明,他只会有一个hook点. 一.Hook hook点是什么? 我们入门Android的时候,一定会看到过这个图,但是你确定 ...
- GMaps.js:让你快速集成 Google Maps 服务的 jQuery 插件
GMaps.js 功能 除了添加指定经纬度的标准地图之外,GMaps.js 还能定义地图放大的级别,添加标注,获取当前用户的地理位置(HTML5 geolocation),定义路线,绘制折线,并且实现 ...
- Springboot集成mybatis通用Mapper与分页插件PageHelper
Springboot集成mybatis通用Mapper与分页插件PageHelper 插件介绍 通用 Mapper 是一个可以实现任意 MyBatis 通用方法的框架,项目提供了常规的增删改查操作以及 ...
- 【intellij idea 高级用法之:集成JIRA、UML类图插件、集成SSH、集成FTP、Database管理 】
intellij idea 高级用法之:集成JIRA.UML类图插件.集成SSH.集成FTP.Database管理 - 菩提树下的杨过 - 博客园
最新文章
- 常见http状态码说明(转)
- Eclipse中SVN设置文件为ignore后重新添加至版本控制
- 关于分布式锁原理的一些学习与思考:redis分布式锁,zookeeper分布式锁
- js处理时间的那些事
- python base64编码_JS和Python实现AES算法
- javaweb项目搭建ehcache缓存系统
- Wordpress基础:精简头部wp_head
- 工作80:块级元素的间隙问题
- python获取磁盘剩余空间的方法
- 华为java开发面试难不难,秀出天际!
- hadoop之 hadoop日志存放路径
- 国内外低代码平台一览
- Arduino下LCD1602综合探究(下)——如何减少1602的连线,LiquidCrystal库,LiquidCrystal库中bug的解决方法
- QPSK、16QAM、64QAM信号的散点图、正交、同相分量波形图
- 使用Cloudflare API动态解析域名IP
- 大数据监测及预警系统平台怎么选择的方法参考
- java gef_GEF最简单的入门-helloword(1)
- 软件定义网络实验之SDN网络简单管理及开发
- 解决错误代码:0xc000007b系统蓝屏问题
- Linux下的Makefile编写与优化