Skip to content

Commit

Permalink
ci(js): add prebuilding for different architectures
Browse files Browse the repository at this point in the history
  • Loading branch information
es3n1n committed Oct 4, 2024
1 parent a4824a4 commit 125ace8
Show file tree
Hide file tree
Showing 7 changed files with 284 additions and 36 deletions.
200 changes: 200 additions & 0 deletions .github/workflows/js.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
# Most of the code here is based on https://github.com/Julusian/node-jpeg-turbo/blob/main/.github/workflows/node.yaml
name: JS

on:
release:
types: [ released ]
workflow_dispatch:
pull_request:
push:
branches:
- master

env:
NAPI_VERSION: 7
BINDING_NAME: re-unplayplay-js
MACOSX_DEPLOYMENT_TARGET: "10.13"

jobs:
test_js:
if: '!cancelled()'
Expand All @@ -28,3 +36,195 @@ jobs:

- name: Test
run: npm run test

build_js:
# if: github.repository == 'es3n1n/re-unplayplay' && github.event_name == 'release'
name: Build ${{ matrix.docker-arch || matrix.arch }} on ${{ matrix.docker-image || matrix.container || matrix.os }} ${{ matrix.libc }}
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
include:
# windows
- os: windows-2022
arch: x64
is-native: true
- os: windows-2022
arch: ia32
is-native: false
- os: windows-2022
arch: arm64
is-native: false
# macos
- os: macos-latest
arch: arm64
is-native: false
- os: macos-latest
arch: x64
is-native: true
# linux
- os: ubuntu-latest
arch: x64
is-native: true
docker-arch: linux/amd64
docker-image: node:18-buster
# linux-libc
- os: ubuntu-latest
arch: arm64
is-native: false
docker-arch: linux/arm64
docker-image: node:18-buster
- os: ubuntu-latest
arch: arm
is-native: false
docker-arch: linux/arm/v7
docker-image: node:18-buster
# linux-musl
- os: ubuntu-latest
arch: x64
is-native: false
docker-arch: linux/amd64
docker-image: node:18-alpine
libc: musl

steps:
- uses: actions/checkout@v4

- name: Use Node.js 18.x
uses: actions/setup-node@v4
with:
node-version: 18.x

- name: Build
if: ${{ !matrix.docker-arch }}
shell: bash
run: |
npm install
if [ -n "${{ matrix.is-native }}" ]; then
npm run test
fi
npm run rebuild -- --arch=${{ matrix.arch }}
npm run pkg-prebuilds-copy -- \
${{ matrix.extra-copy-args }} \
--source $BINDING_NAME.node \
--name=$BINDING_NAME \
--napi_version=$NAPI_VERSION \
--arch=${{ matrix.arch }}
env:
CI: true
npm_config_build_from_source: true

- name: Set up QEMU
uses: docker/setup-qemu-action@v3
if: matrix.docker-arch

- name: Pre-fetch image
if: matrix.docker-arch
run: docker pull ${{ matrix.docker-image }} --platform=${{ matrix.docker-arch }}

- name: Build (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
run: |
if command -v apt-get &> /dev/null
then
echo "deb http://archive.debian.org/debian buster-backports main contrib non-free" | tee -a /etc/apt/sources.list.d/backports.list
apt update
apt install -yq --no-install-recommends cmake/buster-backports g++ gcc
elif command -v apk &> /dev/null
then
apk update
apk add cmake make g++ gcc
fi
cd /work
npm install
npm run test
npm run pkg-prebuilds-copy -- \
${{ matrix.extra-copy-args }} \
--source $BINDING_NAME.node \
--name=$BINDING_NAME \
--napi_version=$NAPI_VERSION \
--arch=${{ matrix.arch }} \
--libc=${{ matrix.libc }}
- name: Upload artifacts
uses: actions/upload-artifact@v4
with:
name: ${{ runner.os }}-${{ matrix.arch }}-${{ matrix.libc }}-prebuilds
path: prebuilds
retention-days: 1

bundle:
# if: github.repository == 'es3n1n/re-unplayplay' && github.event_name == 'release'
name: Bundle prebuilds
needs: build_js
runs-on: ubuntu-latest
steps:
- uses: actions/download-artifact@v4
with:
path: tmp

- name: Display structure of downloaded files
run: |
mkdir prebuilds
mv tmp/*/* prebuilds/
- name: Upload artifacts
uses: actions/upload-artifact@v4
with:
name: all-prebuilds
path: prebuilds
retention-days: 7

publish:
# if: github.repository == 'es3n1n/re-unplayplay' && github.event_name == 'release'
name: Publish to npm
needs: bundle
runs-on: ubuntu-latest

permissions:
contents: read
id-token: write

steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Use Node.js 20.x
uses: actions/setup-node@v4
with:
node-version: 20.x

- name: Prepare build
if: ${{ steps.do-publish.outputs.tag }}
run: |
npm install
env:
CI: true

- uses: actions/download-artifact@v4
with:
name: all-prebuilds
path: prebuilds

# - name: Publish to NPM
# if: ${{ steps.do-publish.outputs.tag }}
# run: |
# npm set "//registry.npmjs.org/:_authToken" "$NPM_AUTH_TOKEN"
#
# npm publish --access=public --tag ${{ steps.do-publish.outputs.tag }} --provenance
#
# NEW_VERSION=$(node -p "require('./package.json').version")
# echo "**Published:** $NEW_VERSION" >> $GITHUB_STEP_SUMMARY
# env:
# NPM_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
# CI: true
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ if(RE_UNPP_BUILD_JS) # re-unpp-build-js
add_library(node-api INTERFACE)

target_compile_definitions(node-api INTERFACE
NAPI_VERSION=3
NAPI_VERSION=7
)

target_include_directories(node-api INTERFACE
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: 're-unplayplay',
napi_versions: [7],
}
4 changes: 2 additions & 2 deletions cmake.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ install(TARGETS re-unplayplay-python LIBRARY DESTINATION .)"""
type = "interface"
include-directories = ["${CMAKE_JS_INC}", "${NODE_ADDON_API_INCLUDE}"]
condition = "re-unpp-build-js"
compile-definitions = ["NAPI_VERSION=3"]
compile-definitions = ["NAPI_VERSION=7"]
link-libraries = ["${CMAKE_JS_LIB}"]
cmake-before = 'include("cmake/cmakejs.cmake")'
# We can't use `sources` since cmkr will always create a source_group for the current dir
# CMKR ISSUE: We can't use `sources` since cmkr will always create a source_group for the current dir
cmake-after = "target_sources(node-api INTERFACE ${CMAKE_JS_SRC})"

[target.re-unplayplay-js]
Expand Down
35 changes: 33 additions & 2 deletions cmake/cmakejs.cmake
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# Authored by Graham Dianaty for Bitlogix Technologies. Based on code from Rene Hollander.
# Rewritten by @es3n1n in 2024
cmake_minimum_required(VERSION 3.12)

if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.28)
Expand Down Expand Up @@ -75,6 +73,8 @@ function(parse_cmakejs_output CMAKE_JS_OUTPUT)
string(REGEX MATCH "-DCMAKE_JS_LIB=[^']*" CMAKE_JS_LIB_MATCH "${CMAKE_JS_OUTPUT}")
string(REGEX MATCH "-DCMAKE_RUNTIME_OUTPUT_DIRECTORY=[^']*" CMAKE_RUNTIME_OUTPUT_DIRECTORY_MATCH "${CMAKE_JS_OUTPUT}")
string(REGEX MATCH "-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=[^']*" CMAKE_LIBRARY_OUTPUT_DIRECTORY_MATCH "${CMAKE_JS_OUTPUT}")
string(REGEX MATCH "-DCMAKE_JS_NODELIB_DEF=[^']*" CMAKE_JS_NODELIB_DEF_MATCH "${CMAKE_JS_OUTPUT}")
string(REGEX MATCH "-DCMAKE_JS_NODELIB_TARGET=[^']*" CMAKE_JS_NODELIB_TARGET_MATCH "${CMAKE_JS_OUTPUT}")

if(CMAKE_JS_INCLUDE_MATCH)
string(REPLACE "-DCMAKE_JS_INC=" "" CMAKE_JS_INCLUDE "${CMAKE_JS_INCLUDE_MATCH}")
Expand All @@ -99,10 +99,41 @@ function(parse_cmakejs_output CMAKE_JS_OUTPUT)
set(CMAKE_OUTPUT_DIRECTORY "" PARENT_SCOPE)
endif()

# Set CMAKE_RUNTIME_OUTPUT_DIRECTORY/CMAKE_LIBRARY_OUTPUT_DIRECTORY
if(DEFINED CMAKE_OUTPUT_DIRECTORY)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_OUTPUT_DIRECTORY}" PARENT_SCOPE)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_OUTPUT_DIRECTORY}" PARENT_SCOPE)
endif()

# Set CMAKE_JS_NODELIB_DEF
if(CMAKE_JS_NODELIB_DEF_MATCH)
string(REPLACE "-DCMAKE_JS_NODELIB_DEF=" "" CMAKE_JS_NODELIB_DEF "${CMAKE_JS_NODELIB_DEF_MATCH}")
string(REGEX REPLACE "[\r\n\"]" "" CMAKE_JS_NODELIB_DEF ${CMAKE_JS_NODELIB_DEF})
else()
set(CMAKE_JS_NODELIB_DEF "")
endif()

# Set CMAKE_JS_NODELIB_TARGET
if(CMAKE_JS_NODELIB_TARGET_MATCH)
string(REPLACE "-DCMAKE_JS_NODELIB_TARGET=" "" CMAKE_JS_NODELIB_TARGET "${CMAKE_JS_NODELIB_TARGET_MATCH}")
string(REGEX REPLACE "[\r\n\"]" "" CMAKE_JS_NODELIB_TARGET ${CMAKE_JS_NODELIB_TARGET})
else()
set(CMAKE_JS_NODELIB_TARGET "")
endif()

# On msvc we have to build node.lib
if(MSVC AND CMAKE_JS_NODELIB_TARGET AND CMAKE_JS_NODELIB_DEF AND NOT EXISTS "${CMAKE_JS_NODELIB_TARGET}")
execute_process(
COMMAND ${CMAKE_AR} /def:${CMAKE_JS_NODELIB_DEF} /out:${CMAKE_JS_NODELIB_TARGET} ${CMAKE_STATIC_LINKER_FLAGS}
RESULT_VARIABLE CMAKE_AR_RESULT
)

if(NOT CMAKE_AR_RESULT EQUAL 0)
message(FATAL_ERROR "[CMakeJS] Failed to generate node.lib at ${CMAKE_JS_NODELIB_TARGET}")
endif()

message(VERBOSE "[CMakeJS] Generated node.lib")
endif()
else()
message(FATAL_ERROR "[CMakeJS] Failed to parse CMake.js output: CMAKE_JS_INC not found")
endif()
Expand Down
Loading

0 comments on commit 125ace8

Please sign in to comment.