name: Build
on:
  push:
    branches:
      - '**'
    tags-ignore:
      - '*.*'
  pull_request:
    branches:
      - '**'
jobs:
  build_x86_64_legacy_nodejs:
    name: Build x86_64 (legacy Node.js)
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [macos-12, macos-13, ubuntu-20.04, ubuntu-22.04, windows-2019]
        node: [12, 13, 14, 15, 16, 17, 19]
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3

      - name: Install Node v${{ matrix.node }}
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node }}

      - name: Setup python
        uses: actions/setup-python@v4
        with:
          python-version: '3.8'

      - name: Install dependencies
        run: npm install --build-from-source

      - name: Package prebuild
        run: npm run build

  build_x86_64:
    name: Build x86_64
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [macos-12, macos-13, ubuntu-20.04, ubuntu-22.04, windows-2019, windows-2022]
        node: [18, 20, 21, 22]
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3

      - name: Install Node v${{ matrix.node }}
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node }}

      - name: Install dependencies
        run: npm install --build-from-source

      - name: Package prebuild
        run: npm run build

  build_musl_x86_64:
    name: Build x86_64 (musl)
    runs-on: ubuntu-latest
    container:
      image: node:${{ matrix.node }}-alpine
    strategy:
      fail-fast: false
      matrix:
        node: [12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]
    steps:
      - name: Setup env with Node v${{ matrix.node }}
        run: |
          apk add --update
          apk add --no-cache ca-certificates git curl build-base python3 g++ make

      - name: Checkout repository
        uses: actions/checkout@v3

      - name: Install dependencies
        run: npm install --unsafe-perm --build-from-source

      - name: Package prebuild
        run: npm run build

  build_aarch64_legacy_nodejs_linux:
    name: Prebuild aarch64 Linux (legacy Node.js)
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-20.04, ubuntu-22.04]
        node: [12, 13, 14, 15, 16, 17, 19]
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3

      - name: Install Node v${{ matrix.node }}
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node }}

      - name: Install dependencies
        run: npm install --build-from-source

      - name: Package prebuild
        run: |
          sudo apt update -y && sudo apt install -y gcc-aarch64-linux-gnu g++-aarch64-linux-gnu
          CC=aarch64-linux-gnu-gcc CXX=aarch64-linux-gnu-g++ npx node-pre-gyp --target_arch=arm64 configure build package

  build_aarch64_linux:
    name: Prebuild aarch64 Linux
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [ubuntu-20.04, ubuntu-22.04]
        node: [18, 20, 21, 22]
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3

      - name: Install Node v${{ matrix.node }}
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node }}

      - name: Install dependencies
        run: npm install --build-from-source

      - name: Package prebuild
        run: |
          sudo apt update -y && sudo apt install -y gcc-aarch64-linux-gnu g++-aarch64-linux-gnu
          CC=aarch64-linux-gnu-gcc CXX=aarch64-linux-gnu-g++ npx node-pre-gyp --target_arch=arm64 configure build package

  build_aarch64_legacy_nodejs_mac:
    name: Prebuild aarch64 macOS (legacy Node.js)
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [macos-14]
        node: [16, 17, 19]
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3

      - name: Install Node v${{ matrix.node }}
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node }}

      - name: Setup python
        uses: actions/setup-python@v4
        with:
          python-version: '3.8'

      - name: Install dependencies
        run: npm install --build-from-source

      - name: Package prebuild
        run: npm run build

  build_aarch64_mac:
    name: Prebuild aarch64 macOS
    runs-on: ${{ matrix.os }}
    strategy:
      fail-fast: false
      matrix:
        os: [macos-14]
        node: [18, 20, 21, 22]
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3

      - name: Install Node v${{ matrix.node }}
        uses: actions/setup-node@v3
        with:
          node-version: ${{ matrix.node }}

      - name: Install dependencies
        run: npm install --build-from-source

      - name: Package prebuild
        run: npm run build

  build_musl_aarch64:
    name: Prebuild aarch64 (musl)
    runs-on: ubuntu-latest
    container:
      image: node:${{ matrix.node }}-alpine
    strategy:
      fail-fast: false
      matrix:
        node: [12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]
    steps:
      - name: Setup env with Node v${{ matrix.node }}
        run: |
          apk add --update
          apk add --no-cache ca-certificates git curl build-base python3 g++ make

      - name: Checkout repository
        uses: actions/checkout@v3

      - name: Setup musl cross compiler
        run: |
          curl -OL https://musl.cc/aarch64-linux-musl-cross.tgz
          tar -xzvf aarch64-linux-musl-cross.tgz
          $(pwd)/aarch64-linux-musl-cross/bin/aarch64-linux-musl-gcc --version

      - name: Install dependencies
        run: npm install --unsafe-perm --build-from-source

      - name: Package prebuild
        run: |
          CC=$(pwd)/aarch64-linux-musl-cross/bin/aarch64-linux-musl-gcc \
          CXX=$(pwd)/aarch64-linux-musl-cross/bin/aarch64-linux-musl-g++ \
          npx node-pre-gyp --target_arch=arm64 configure build package
