Skip to content

Commit

Permalink
feat: use pkg-prebuilds (#507)
Browse files Browse the repository at this point in the history
* add prebuilds for arm64

* use prebuildify

* fix workflow

* fix workflow (again)

* try again

* remove accidentally committed prebuilds

* add support for hidraw variants

* fix hidraw only exists on linux

* add prebuilds and *.tgz to .gitignore

* replace readme badge

* rename `BINDING_NAME_RAW` to `BINDING_NAME_HIDRAW`

* re-enable tests

* use options from `binding-options.js`

* chore: update pkg-prebuilds

* chore: update github actions workflow

* run for prs

* update workflow

* fix alpine

* fix alpine

* fix alpine

* update readme

* trying pr 507

* Delete package.json.prebuildify

argh I am so bad at github today

* update github workflow

* update actions

* update publishing notes

* fix: macos python 3.11

---------

Co-authored-by: Hannes Rüger <hannesrueger@gmx.de>
Co-authored-by: Tod Kurt <todbot@users.noreply.github.com>
  • Loading branch information
3 people committed Nov 26, 2023
1 parent d279b46 commit 45a453e
Show file tree
Hide file tree
Showing 11 changed files with 212 additions and 142 deletions.
163 changes: 163 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
name: Prebuild

on:
push:
# tags:
# - v*

env:
NAPI_VERSION: 3
BINDING_NAME: HID
BINDING_NAME_HIDRAW: HID_hidraw

jobs:
build-and-test:
name: Build ${{ matrix.arch }} on ${{ matrix.os }} ${{ matrix.libc }}
runs-on: ${{ matrix.os }}

strategy:
fail-fast: false
matrix:
include:
# windows
- os: windows-2019
arch: x64
is-native: true
- os: windows-2019
arch: ia32
is-native: false
# macos
- os: macos-11
arch: arm64
is-native: false
- os: macos-11
arch: x64
is-native: true
# linux
- os: ubuntu-latest
arch: x64
is-native: true
docker-arch: linux/amd64
docker-image: node:14-buster
- os: ubuntu-latest
arch: arm64
is-native: false
docker-arch: linux/arm64
docker-image: node:14-buster
- os: ubuntu-latest
arch: arm
is-native: false
docker-arch: linux/arm/v7
docker-image: node:14-buster
# linux musl/alpine
- os: ubuntu-latest
arch: x64
is-native: false
docker-arch: linux/amd64
docker-image: node:14-alpine
libc: musl

steps:
- uses: actions/checkout@v4
- name: Checkout submodules
shell: bash
run: |
auth_header="$(git config --local --get http.https://github.com/.extraheader)"
git submodule sync --recursive
git -c "http.extraheader=$auth_header" -c protocol.version=2 submodule update --init --force --recursive --depth=1
- name: Macos Use Python 3.11
if: runner.os == 'macOS'
uses: actions/setup-python@v4
with:
python-version: 3.11 # ${{ matrix.python }}
env:
PYTHON_VERSION: 3.11 # ${{ matrix.python }} # Why do this?

- name: Install Linux dependencies
if: runner.os == 'Linux'
run: |
sudo apt-get update
sudo apt-get install -y libudev-dev libusb-1.0-0-dev
- name: Use Node.js 16.x
uses: actions/setup-node@v4
with:
node-version: 16.x

- name: rebuild
if: ${{ !matrix.docker-arch }}
shell: bash
run: |
npm i
if [ -n "${{ matrix.is-native }}" ]; then
npm test
fi
npm run rebuild --arch=${{ matrix.arch }}
npx pkg-prebuilds-copy --baseDir build/Release --source $BINDING_NAME.node --name=$BINDING_NAME --strip --napi_version=$NAPI_VERSION --arch=${{ matrix.arch }}
if [ -f "build/Release/$BINDING_NAME_HIDRAW.node" ]; then
npx pkg-prebuilds-copy --baseDir build/Release --source $BINDING_NAME_HIDRAW.node --name=$BINDING_NAME_HIDRAW --strip --napi_version=$NAPI_VERSION --arch=${{ matrix.arch }}
fi
env:
CI: true
npm_config_build_from_source: true

- name: Set up QEMU
uses: docker/setup-qemu-action@v3
if: matrix.docker-arch
- name: rebuild (in docker)
uses: addnab/docker-run-action@v3
if: matrix.docker-arch
with:
image: ${{ matrix.docker-image }}
# shell: bash
options: --platform=${{ matrix.docker-arch }} -v ${{ github.workspace }}:/work -e CI=1 -e npm_config_build_from_source=1 -e NAPI_VERSION -e BINDING_NAME -e BINDING_NAME_HIDRAW
run: |
if command -v apt-get &> /dev/null
then
apt-get update
apt-get install -y libudev-dev libusb-1.0-0-dev
elif command -v apk &> /dev/null
then
apk update
apk add python3 make g++ gcc libusb-dev linux-headers eudev-dev
fi
cd /work
npm i --unsafe-perm
npm run test
npx pkg-prebuilds-copy --baseDir build/Release --source $BINDING_NAME.node --name=$BINDING_NAME --strip --napi_version=$NAPI_VERSION --arch=${{ matrix.arch }} --libc=${{ matrix.libc }}
npx pkg-prebuilds-copy --baseDir build/Release --source $BINDING_NAME_HIDRAW.node --name=$BINDING_NAME_HIDRAW --strip --napi_version=$NAPI_VERSION --arch=${{ matrix.arch }} --libc=${{ matrix.libc }}
- name: Upload artifacts
uses: actions/upload-artifact@v3
with:
name: ${{ runner.os }}-${{ matrix.arch }}-${{ matrix.libc }}-prebuilds
path: prebuilds
retention-days: 1

bundle:
name: Bundle prebuilds
needs: build-and-test
runs-on: ubuntu-latest
steps:
- uses: actions/download-artifact@v3
with:
path: tmp

- name: Display structure of downloaded files
run: |
mkdir prebuilds
mv tmp/*/* prebuilds/
- name: Upload artifacts
uses: actions/upload-artifact@v3
with:
name: all-prebuilds
path: prebuilds
retention-days: 7
36 changes: 0 additions & 36 deletions .github/workflows/linux.yml

This file was deleted.

37 changes: 0 additions & 37 deletions .github/workflows/macos.yml

This file was deleted.

30 changes: 0 additions & 30 deletions .github/workflows/windows.yml

This file was deleted.

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ npm-debug.log
package-lock.json
Release/
prebuilds/
*.tgz
1 change: 0 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,3 @@ node_modules
npm-debug.log
package-lock.json
Release/
prebuilds
26 changes: 17 additions & 9 deletions Publishing.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,23 @@ How to publish node-hid

1. First, make sure version is bumped to new version. (if code change)

2. Then, on each of MacOS, Windows, Linux, do:
2. Push changes to Github, and wait for the prebuild workflow to complete

3. Run:
```
npm run clean # clean out directory
```

4. Download the `all-prebuilds` artifact, and extract as a `prebuilds` folder in the root of the repository

5. Run:
```
npm publish # update npmjs, be sure to have Authy app for OTP code
```

-----

If desired, manual testing can be down on each of MacOS, Windows, Linux, do:
```
git clone https://github.com/node-hid/node-hid.git
cd node-hid
Expand All @@ -13,12 +29,4 @@ npm run prepublishOnly # get the hidapi submodule
npm install --build-from-source # rebuilds the C code
npm run showdevices # simple test
node ./src/test-blink1.js # simple test
npm run prebuild # build all the versions
npm run prebuild-upload <GH_TOKEN> # upload all the versions using github token
```

3. And then on master dev box:
```
npm run clean # clean out directory
npm publish # update npmjs, be sure to have Authy app for OTP code
```
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
# node-hid - Access USB HID devices from Node.js #

[![npm](https://img.shields.io/npm/dm/node-hid.svg?maxAge=2592000)](http://npmjs.com/package/node-hid)
[![build macos](https://github.com/node-hid/node-hid/workflows/macos/badge.svg)](https://github.com/node-hid/node-hid/actions?query=workflow%3Amacos)
[![build windows](https://github.com/node-hid/node-hid/workflows/windows/badge.svg)](https://github.com/node-hid/node-hid/actions?query=workflow%3Awindows)
[![build linux](https://github.com/node-hid/node-hid/workflows/linux/badge.svg)](https://github.com/node-hid/node-hid/actions?query=workflow%3Alinux)
[![Prebuild](https://github.com/node-hid/node-hid/actions/workflows/build.yml/badge.svg)](https://github.com/node-hid/node-hid/actions/workflows/build.yml)


* [node-hid - Access USB HID devices from Node.js](#node-hid---access-usb-hid-devices-from-nodejs)
Expand Down Expand Up @@ -48,17 +46,18 @@ We strive to make `node-hid` cross-platform so there's a good chance any
combination not listed here will compile and work.

### Supported Platforms ###
- Windows x86 (32-bit) (¹)
- Windows x86 (32-bit)
- Windows x64 (64-bit)
- Mac OSX 10.9+
- Linux x64 (²)
- Linux x86 (¹)
- Linux ARM / Raspberry Pi (¹)
- Linux ARM / Raspberry Pi / Various SBC (²)
- Linux ARM64 / Various SBC (²)
- Linux MIPSel (¹)
- Linux PPC64 (¹)

¹ prebuilt-binaries not provided for these platforms
² prebuilt binary built on Ubuntu 18.04 x64
² prebuilt binary built on Debian 10 Buster

### Supported Node versions ###

Expand Down
4 changes: 4 additions & 0 deletions binding-options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
name: 'HID',
napi_versions: [3],
}
16 changes: 5 additions & 11 deletions nodehid.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,12 @@ function setDriverType(type) {
// lazy load the C++ binding
let binding = null;
function loadBinding() {
if( !binding ) {
if( process.platform === 'linux' ) {
// Linux defaults to hidraw
if( !driverType || driverType === 'hidraw' ) {
binding = require('bindings')('HID_hidraw.node');
} else {
binding = require('bindings')('HID.node');
}
}
else {
binding = require('bindings')('HID.node');
if (!binding) {
const options = require('./binding-options');
if (process.platform === "linux" && (!driverType || driverType === "hidraw")) {
options.name = 'HID_hidraw';
}
binding = require("pkg-prebuilds/bindings")(__dirname, options);
}
}

Expand Down

0 comments on commit 45a453e

Please sign in to comment.