# Copyright (C) 2019 Intel Corporation.  All rights reserved.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
name: build iwasm release

on:
  workflow_call:
    inputs:
      arch:
        description: arch of the release
        type: string
        required: false
        default: x86_64
      cwd:
        description: working directory
        type: string
        required: true
      llvm_cache_key:
        description: the cache key of llvm libraries
        type: string
        required: true
      runner:
        description: OS of compilation
        type: string
        required: true
      upload_url:
        description: upload binary assets to the URL of release
        type: string
        required: false
      ver_num:
        description: a semantic version number. it is required when `release` is true.
        type: string
        required: false

env:
  DEFAULT_BUILD_OPTIONS:
    "-DWAMR_BUILD_AOT=1 -DWAMR_BUILD_INTERP=1 -DWAMR_BUILD_FAST_JIT=1 -DWAMR_BUILD_JIT=1 \
    -DWAMR_BUILD_CUSTOM_NAME_SECTION=0 \
    -DWAMR_BUILD_DEBUG_INTERP=0 \
    -DWAMR_BUILD_DEBUG_AOT=0 \
    -DWAMR_BUILD_DUMP_CALL_STACK=0 \
    -DWAMR_BUILD_LIBC_UVWASI=0 \
    -DWAMR_BUILD_LIBC_EMCC=0 \
    -DWAMR_BUILD_LIB_RATS=0 \
    -DWAMR_BUILD_LOAD_CUSTOM_SECTION=0 \
    -DWAMR_BUILD_MEMORY_PROFILING=0 \
    -DWAMR_BUILD_MINI_LOADER=0 \
    -DWAMR_BUILD_MULTI_MODULE=0 \
    -DWAMR_BUILD_PERF_PROFILING=0 \
    -DWAMR_BUILD_SPEC_TEST=0 \
    -DWAMR_BUILD_BULK_MEMORY=1 \
    -DWAMR_BUILD_LIB_PTHREAD=1 \
    -DWAMR_BUILD_LIB_PTHREAD_SEMAPHORE=1 \
    -DWAMR_BUILD_LIB_WASI_THREADS=1 \
    -DWAMR_BUILD_LIBC_BUILTIN=1 \
    -DWAMR_BUILD_LIBC_WASI=1 \
    -DWAMR_BUILD_REF_TYPES=1 \
    -DWAMR_BUILD_SIMD=1 \
    -DWAMR_BUILD_SHARED_MEMORY=1 \
    -DWAMR_BUILD_TAIL_CALL=1 \
    -DWAMR_BUILD_THREAD_MGR=1"
  GC_EH_BUILD_OPTIONS:
    "-DWAMR_BUILD_AOT=1 -DWAMR_BUILD_INTERP=1 -DWAMR_BUILD_FAST_INTERP=0 -DWAMR_BUILD_FAST_JIT=0 -DWAMR_BUILD_JIT=0 \
    -DWAMR_BUILD_CUSTOM_NAME_SECTION=0 \
    -DWAMR_BUILD_DEBUG_INTERP=0 \
    -DWAMR_BUILD_DEBUG_AOT=0 \
    -DWAMR_BUILD_DUMP_CALL_STACK=0 \
    -DWAMR_BUILD_LIBC_UVWASI=0 \
    -DWAMR_BUILD_LIBC_EMCC=0 \
    -DWAMR_BUILD_LIB_RATS=0 \
    -DWAMR_BUILD_LOAD_CUSTOM_SECTION=0 \
    -DWAMR_BUILD_MEMORY_PROFILING=0 \
    -DWAMR_BUILD_MINI_LOADER=0 \
    -DWAMR_BUILD_MULTI_MODULE=0 \
    -DWAMR_BUILD_PERF_PROFILING=0 \
    -DWAMR_BUILD_SPEC_TEST=0 \
    -DWAMR_BUILD_BULK_MEMORY=1 \
    -DWAMR_BUILD_LIB_PTHREAD=1 \
    -DWAMR_BUILD_LIB_PTHREAD_SEMAPHORE=1 \
    -DWAMR_BUILD_LIB_WASI_THREADS=1 \
    -DWAMR_BUILD_LIBC_BUILTIN=1 \
    -DWAMR_BUILD_LIBC_WASI=1 \
    -DWAMR_BUILD_REF_TYPES=1 \
    -DWAMR_BUILD_SIMD=1 \
    -DWAMR_BUILD_SHARED_MEMORY=1 \
    -DWAMR_BUILD_TAIL_CALL=1 \
    -DWAMR_BUILD_THREAD_MGR=1 \
    -DWAMR_BUILD_EXCE_HANDLING=1 \
    -DWAMR_BUILD_GC=1"

permissions:
  contents: read

jobs:
  build:
    runs-on: ${{ inputs.runner }}
    strategy:
      matrix:
        include:
          - build_options: $DEFAULT_BUILD_OPTIONS
            suffix: ""
          - build_options: $GC_EH_BUILD_OPTIONS
            suffix: "-gc-eh"
    permissions:
      contents: write # for uploading release artifacts

    steps:
      - uses: actions/checkout@v6.0.1

      - name: get cached LLVM libraries
        id: retrieve_llvm_libs
        uses: actions/cache@v5
        with:
          path: |
            ./core/deps/llvm/build/bin
            ./core/deps/llvm/build/include
            ./core/deps/llvm/build/lib
            ./core/deps/llvm/build/libexec
            ./core/deps/llvm/build/share
          key: ${{ inputs.llvm_cache_key }}
          fail-on-cache-miss: true

      - name: generate iwasm binary release
        shell: bash
        run: |
          cmake -S . -B build ${{ matrix.build_options }}
          cmake --build build --config Release --parallel 4
        working-directory: ${{ inputs.cwd }}

      - name: smoke test on non-Windows
        if: ${{ !startsWith(inputs.runner, 'windows') }}
        shell: bash
        run: |
          if [[ ! -f build/iwasm ]]; then
            echo "iwasm binary is not found in the expected location."
            exit 1
          fi

          build/iwasm --version
        working-directory: ${{ inputs.cwd }}

      - name: smoke test on Windows
        if: ${{ startsWith(inputs.runner, 'windows') }}
        shell: bash
        run: |
          if [[ ! -f build/Release/iwasm ]]; then
            echo "iwasm binary is not found in the expected location."
            exit 1
          fi

          build/Release/iwasm --version
        working-directory: ${{ inputs.cwd }}

      - name: Compress the binary on Windows
        if: inputs.runner == 'windows-2022'
        run: |
          tar -czf iwasm${{ matrix.suffix }}-${{ inputs.ver_num }}-${{ inputs.runner }}.tar.gz iwasm.exe
          Compress-Archive -Path iwasm.exe -DestinationPath iwasm${{ matrix.suffix }}-${{ inputs.ver_num }}-${{ inputs.runner }}.zip
          mv iwasm${{ matrix.suffix }}-${{ inputs.ver_num }}-${{ inputs.runner }}.* ../
        working-directory: ${{ inputs.cwd }}/build/Release

      - name: compress the binary on non-Windows
        if: inputs.runner != 'windows-2022'
        run: |
          # Follow the symlink to the actual binary file
          tar --dereference -czf iwasm${{ matrix.suffix }}-${{ inputs.ver_num }}-${{ inputs.runner }}.tar.gz iwasm
          zip iwasm${{ matrix.suffix }}-${{ inputs.ver_num }}-${{ inputs.runner }}.zip iwasm
        working-directory: ${{ inputs.cwd }}/build

      - name: upload release tar.gz
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ inputs.upload_url }}
          asset_path: ${{ inputs.cwd }}/build/iwasm${{ matrix.suffix }}-${{ inputs.ver_num }}-${{ inputs.runner }}.tar.gz
          asset_name: iwasm${{ matrix.suffix }}-${{ inputs.ver_num }}-${{ inputs.arch }}-${{ inputs.runner }}.tar.gz
          asset_content_type: application/x-gzip

      - name: upload release zip
        uses: actions/upload-release-asset@v1
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        with:
          upload_url: ${{ inputs.upload_url }}
          asset_path: ${{ inputs.cwd }}/build/iwasm${{ matrix.suffix }}-${{ inputs.ver_num }}-${{ inputs.runner }}.zip
          asset_name: iwasm${{ matrix.suffix }}-${{ inputs.ver_num }}-${{ inputs.arch }}-${{ inputs.runner }}.zip
          asset_content_type: application/zip
