Skip to content

Commit

Permalink
chore: prebuilt
Browse files Browse the repository at this point in the history
  • Loading branch information
li-yechao committed May 28, 2023
1 parent efdc7de commit 054e827
Show file tree
Hide file tree
Showing 8 changed files with 452 additions and 66 deletions.
50 changes: 0 additions & 50 deletions .github/workflows/build.yml

This file was deleted.

158 changes: 158 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
name: CI
on:
workflow_dispatch:
pull_request:
push:
branches:
- main
tags:
- '*'
env:
FORCE_COLOR: 1
concurrency:
group: ${{ github.head_ref || github.run_id }}
cancel-in-progress: true
jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os:
- macos-latest
- ubuntu-20.04
- windows-latest
host:
- x64
target:
- x64
node:
- 16
- 18
- 20
include:
- os: macos
node: 16
host: arm64
target: arm64
name: ${{ matrix.os }} (node=${{ matrix.node }}, host=${{ matrix.host }}, target=${{ matrix.target }})
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node }}
architecture: ${{ matrix.host }}

- name: Add yarn (self-hosted)
if: matrix.os == 'macos-m1'
run: npm install -g yarn

- name: Add msbuild to PATH
uses: microsoft/setup-msbuild@v1.3
if: contains(matrix.os, 'windows')
with:
msbuild-architecture: ${{ matrix.target }}

- name: Install dependencies
run: yarn install --ignore-scripts

- name: Add env vars
shell: bash
run: |
echo "V=1" >> $GITHUB_ENV
if [ "${{ matrix.target }}" = "x86" ]; then
echo "TARGET=ia32" >> $GITHUB_ENV
else
echo "TARGET=${{ matrix.target }}" >> $GITHUB_ENV
fi
- name: Configure build
run: yarn node-pre-gyp configure --target_arch=${{ env.TARGET }}

- name: Build binaries
run: yarn node-pre-gyp build --target_arch=${{ env.TARGET }}

- name: Print binary info
if: contains(matrix.os, 'ubuntu')
run: |
ldd lib/binding/*/addon.node
echo "---"
nm lib/binding/*/addon.node | grep "GLIBC_" | c++filt || true
echo "---"
file lib/binding/napi-v*/*
- name: Run tests
run: yarn test

- name: Package prebuilt binaries
run: yarn node-pre-gyp package --target_arch=${{ env.TARGET }}

- name: Upload binaries to commit artifacts
uses: actions/upload-artifact@v3
if: matrix.node == 16
with:
name: prebuilt-binaries
path: build/stage/*/*
retention-days: 7

- name: Upload binaries to GitHub Release
run: yarn node-pre-gyp-github publish
if: matrix.node == 16 && startsWith(github.ref, 'refs/tags/')
env:
NODE_PRE_GYP_GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
build-qemu:
runs-on: ubuntu-latest
if: github.event_name == 'workflow_dispatch' || startsWith(github.ref, 'refs/tags/')
strategy:
fail-fast: false
matrix:
node:
- 16
target:
- linux/arm64
variant:
- bullseye
- alpine3.15
include:
# musl x64 builds
- target: linux/amd64
variant: alpine3.15
node: 16
name: ${{ matrix.variant }} (node=${{ matrix.node }}, target=${{ matrix.target }})
steps:
- uses: actions/checkout@v3

- name: Set up QEMU
uses: docker/setup-qemu-action@v2

- name: Setup Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Build binaries and test
run: |
docker buildx build \
--file ./tools/BinaryBuilder.Dockerfile \
--load \
--tag builder \
--platform ${{ matrix.target }} \
--no-cache \
--build-arg VARIANT=${{ matrix.variant }} \
--build-arg NODE_VERSION=${{ matrix.node }} \
.
CONTAINER_ID=$(docker create -it builder)
docker cp $CONTAINER_ID:/usr/src/build/build/ ./build
- name: Upload binaries to commit artifacts
uses: actions/upload-artifact@v3
if: matrix.node == 16
with:
name: prebuilt-binaries
path: build/stage/*/*
retention-days: 7

- name: Upload binaries to GitHub Release
run: yarn install --ignore-scripts && yarn node-pre-gyp-github publish
if: matrix.node == 16 && startsWith(github.ref, 'refs/tags/')
env:
NODE_PRE_GYP_GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@
/node_modules
/doc
.DS_Store

build-tmp-*
lib/binding
13 changes: 12 additions & 1 deletion binding.gyp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"targets": [
{
"target_name": "addon",
"target_name": "<(module_name)",
"sources": [ "src/addon.cc" ],
"include_dirs": ["<!(node -p \"require('node-addon-api').include_dir\")"],
"defines": [ "NAPI_DISABLE_CPP_EXCEPTIONS" ],
Expand All @@ -26,6 +26,17 @@
}
}]
]
},
{
"target_name": "action_after_build",
"type": "none",
"dependencies": [ "<(module_name)" ],
"copies": [
{
"files": [ "<(PRODUCT_DIR)/<(module_name).node" ],
"destination": "<(module_path)"
}
]
}
]
}
7 changes: 5 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@
* limitations under the License.
*/

const addon = require('bindings')('addon');
module.exports = addon;
const binary = require('@mapbox/node-pre-gyp');
const path = require('path');
const binding_path = binary.find(path.resolve(path.join(__dirname,'../package.json')));
const binding = require(binding_path);
module.exports = exports = binding;
24 changes: 19 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,27 +1,40 @@
{
"name": "hnswlib-node",
"name": "@blocklet/hnswlib-node",
"version": "1.4.2",
"description": "Node.js bindings for Hnswlib",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"repository": "https://github.com/yoshoku/hnswlib-node",
"homepage": "https://github.com/yoshoku/hnswlib-node",
"repository": "https://github.com/blocklet/hnswlib-node",
"homepage": "https://github.com/blocklet/hnswlib-node",
"author": "Atsushi Tatsuma",
"license": "Apache-2.0",
"keywords": [
"hnswlib",
"approximate nearest neighbor search",
"machine learning"
],
"binary": {
"module_name": "addon",
"module_path": "./lib/binding/napi-v{napi_build_version}-{platform}-{libc}-{arch}",
"host": "https://github.com/blocklet/hnswlib-node/releases/download/",
"remote_path": "v{version}",
"package_name": "napi-v{napi_build_version}-{platform}-{libc}-{arch}.tar.gz",
"napi_versions": [
3,
6
]
},
"gypfile": true,
"scripts": {
"prepare": "husky install",
"build": "node-gyp build",
"rebuild": "node-gyp rebuild",
"build": "node-pre-gyp build",
"rebuild": "node-pre-gyp rebuild",
"install": "node-pre-gyp install --fallback-to-build",
"test": "jest",
"doc": "typedoc"
},
"dependencies": {
"@mapbox/node-pre-gyp": "^1.0.10",
"bindings": "^1.5.0",
"node-addon-api": "^6.0.0"
},
Expand All @@ -37,6 +50,7 @@
"jest": "^29.3.1",
"jest-extended": "^3.2.4",
"node-gyp": "^9.3.0",
"node-pre-gyp-github": "^1.4.4",
"typedoc": "^0.24.1",
"typescript": "^5.0.4",
"yarn-release": "^1.10.3"
Expand Down
28 changes: 28 additions & 0 deletions tools/BinaryBuilder.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
ARG NODE_VERSION=16
ARG VARIANT=bullseye

FROM node:$NODE_VERSION-$VARIANT

ARG VARIANT

RUN if case $VARIANT in "alpine"*) true;; *) false;; esac; then apk add build-base python3 --update-cache ; fi

WORKDIR /usr/src/build

COPY . .
RUN npm install --ignore-scripts

# Workaround for https://github.com/mapbox/node-pre-gyp/issues/644
RUN cd node_modules/\@mapbox/node-pre-gyp \
&& npm install fs-extra@10.0.1 \
&& sed -i -e s/\'fs/\'fs-extra/ -e s/fs\.renameSync/fs.moveSync/ ./lib/util/napi.js

RUN npx node-pre-gyp configure
RUN npx node-pre-gyp build

RUN if case $VARIANT in "alpine"*) false;; *) true;; esac; then ldd lib/binding/*/addon.node; nm lib/binding/*/addon.node | grep \"GLIBC_\" | c++filt || true ; fi

RUN npm run test
RUN npx node-pre-gyp package

CMD ["sh"]

0 comments on commit 054e827

Please sign in to comment.