name: release

concurrency:
  group: ${{ github.workflow }}-${{ github.head_ref || github.ref }}
  cancel-in-progress: true

on:
  workflow_dispatch:
    inputs:
      logLevel:
        description: "Log level"
        required: true
        default: "info"
  push:
    tags:
      - "[0-9]+.[0-9]+.[0-9]+*"

jobs:
  create_release:
    name: Create Release
    runs-on: ubuntu-latest
    outputs:
      version: ${{ steps.prep.outputs.version }}
      upload_url: ${{ steps.create_release.outputs.upload_url }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
      - name: Ensure git safe directory
        run: |
          git config --global --add safe.directory $(pwd)
      - name: Get version
        id: prep
        run: |
          echo "version=${GITHUB_REF#refs/*/}" >> $GITHUB_OUTPUT
      - name: Create Release
        id: create_release
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          gh release create ${{ steps.prep.outputs.version }} --draft  --notes-file .CurrentChangelog.md --prerelease --title "WasmEdge ${{ steps.prep.outputs.version }}" --verify-tag

  create_source_tarball:
    needs: create_release
    uses: ./.github/workflows/reusable-create-source-tarball.yml
    with:
      version: ${{ needs.create_release.outputs.version }}
      release: true
    secrets: inherit

  build_on_macos:
    needs: create_release
    uses: ./.github/workflows/reusable-build-on-macos.yml
    with:
      version: ${{ needs.create_release.outputs.version }}
      matrix:
        "[{'name':'MacOS 11 (x86_64)','runner':'macos-11','darwin_version':20,'arch':'x86_64'},
        {'name':'MacOS 13 (arm64)','runner':'mac-arm64','darwin_version':22,'arch':'arm64'}]"
      release: true
    secrets: inherit

  build_on_ubuntu_20_04:
    needs: create_release
    uses: ./.github/workflows/reusable-build-on-ubuntu.yml
    with:
      version: ${{ needs.create_release.outputs.version }}
      matrix: "[{'name':'ubuntu-20.04','compiler':'clang++','build_type':'Release','docker_tag':'ubuntu-20.04-build-clang'}]"
      release: true
    secrets: inherit

  build_on_manylinux2014:
    needs: create_release
    uses: ./.github/workflows/reusable-build-on-manylinux.yml
    with:
      version: ${{ needs.create_release.outputs.version }}
      matrix:
        "[{'name':'manylinux 2014 x86_64','runner':'ubuntu-latest','docker_tag':'manylinux2014_x86_64'},
        {'name':'manylinux 2014 aarch64','runner':'linux-arm64','docker_tag':'manylinux2014_aarch64'}]"
      release: true
    secrets: inherit

  build_on_debian_static:
    needs: create_release
    uses: ./.github/workflows/reusable-build-on-debian-static.yml
    with:
      version: ${{ needs.create_release.outputs.version }}
      release: true
    secrets: inherit

  build_on_alpine_static:
    needs: create_release
    uses: ./.github/workflows/reusable-build-on-alpine-static.yml
    with:
      version: ${{ needs.create_release.outputs.version }}
      release: true
    secrets: inherit

  build_on_windows:
    needs: create_release
    uses: ./.github/workflows/reusable-build-on-windows.yml
    with:
      version: ${{ needs.create_release.outputs.version }}
      release: true
    secrets: inherit

  build_on_windows_msvc:
    needs: create_release
    uses: ./.github/workflows/reusable-build-on-windows-msvc.yml
    with:
      version: ${{ needs.create_release.outputs.version }}
      release: true
    secrets: inherit

  build_on_android:
    needs: create_release
    uses: ./.github/workflows/reusable-build-on-android.yml
    with:
      version: ${{ needs.create_release.outputs.version }}
      release: true
    secrets: inherit

  build_and_upload_wasinn_ubuntu:
    name: Build and upload WASI-NN on Ubuntu 20.04
    runs-on: ubuntu-latest
    env:
      output_dir: build/plugins/wasi_nn
      build_options: -DWASMEDGE_PLUGIN_WASI_NN_BACKEND=PyTorch -DWASMEDGE_PLUGIN_WASI_NN_BACKEND=OpenVINO -DWASMEDGE_PLUGIN_WASI_NN_BACKEND=TensorFlowLite -DWASMEDGE_PLUGIN_WASI_NN_BACKEND=GGML
      tar_names: wasi_nn-pytorch wasi_nn-openvino wasi_nn-tensorflowlite wasi_nn-ggml
      output_bin: libwasmedgePluginWasiNN.so
      OPENVINO_VERSION: "2023.0.2"
      OPENVINO_YEAR: "2023"
      PYTORCH_VERSION: "1.8.2"
      PYTORCH_INSTALL_TO: "."
    needs: create_release
    container:
      image: wasmedge/wasmedge:ubuntu-20.04-build-clang
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - name: Grant the safe directory for git
        run: |
          git config --global --add safe.directory $(pwd)
      - name: Install dependencies
        shell: bash
        run: |
          apt update
          apt install -y unzip libopenblas-dev pkg-config
          bash utils/wasi-nn/install-openvino.sh
          bash utils/wasi-nn/install-pytorch.sh
      - name: Build WASI-NN plugin
        shell: bash
        run: |
          export Torch_DIR=$(pwd)/${PYTORCH_INSTALL_TO}/libtorch
          ldconfig
          plugin_array=(${tar_names})
          option_array=(${build_options})
          outtarget=${output_bin%.*}
          outtarget=${outtarget#lib}
          cmake -Bbuild -GNinja -DCMAKE_BUILD_TYPE=Release -DWASMEDGE_BUILD_AOT_RUNTIME=OFF -DWASMEDGE_BUILD_TOOLS=OFF
          for (( i=0; i<${#plugin_array[@]}; i++ ));
          do
            echo "Building ${plugin_array[$i]} backend:"
            cmake -Bbuild -GNinja ${option_array[$i]}
            cmake --build build --target ${outtarget}

            echo "Copying ${plugin_array[$i]} backend:"
            cp -f ${output_dir}/${output_bin} ${output_bin}
            tar -zcvf plugin_${plugin_array[$i]}.tar.gz ${output_bin}
          done
      - name: Install gh
        run: |
          type -p curl >/dev/null || (apt update && apt install curl -y)
          curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg \
          && chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg \
          && echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | tee /etc/apt/sources.list.d/github-cli.list > /dev/null \
          && apt update \
          && apt install gh -y
      - name: Upload wasi_nn-pytorch plugin tar.gz package
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          mv plugin_wasi_nn-pytorch.tar.gz WasmEdge-plugin-wasi_nn-pytorch-${{ needs.create_release.outputs.version }}-ubuntu20.04_x86_64.tar.gz
          gh release upload ${{ needs.create_release.outputs.version }} WasmEdge-plugin-wasi_nn-pytorch-${{ needs.create_release.outputs.version }}-ubuntu20.04_x86_64.tar.gz --clobber
      - name: Upload wasi_nn-openvino plugin tar.gz package
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          mv plugin_wasi_nn-openvino.tar.gz WasmEdge-plugin-wasi_nn-openvino-${{ needs.create_release.outputs.version }}-ubuntu20.04_x86_64.tar.gz
          gh release upload ${{ needs.create_release.outputs.version }} WasmEdge-plugin-wasi_nn-openvino-${{ needs.create_release.outputs.version }}-ubuntu20.04_x86_64.tar.gz --clobber
      - name: Upload wasi_nn-tensorflowlite plugin tar.gz package
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          mv plugin_wasi_nn-tensorflowlite.tar.gz WasmEdge-plugin-wasi_nn-tensorflowlite-${{ needs.create_release.outputs.version }}-ubuntu20.04_x86_64.tar.gz
          gh release upload ${{ needs.create_release.outputs.version }} WasmEdge-plugin-wasi_nn-tensorflowlite-${{ needs.create_release.outputs.version }}-ubuntu20.04_x86_64.tar.gz --clobber
      - name: Upload wasi_nn-ggml plugin tar.gz package
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          mv plugin_wasi_nn-ggml.tar.gz WasmEdge-plugin-wasi_nn-ggml-${{ needs.create_release.outputs.version }}-ubuntu20.04_x86_64.tar.gz
          gh release upload ${{ needs.create_release.outputs.version }} WasmEdge-plugin-wasi_nn-ggml-${{ needs.create_release.outputs.version }}-ubuntu20.04_x86_64.tar.gz --clobber

  build_and_upload_plugin_ubuntu:
    name: Build and upload plugins on Ubuntu 20.04
    runs-on: ubuntu-latest
    env:
      output_prefix: build/plugins
      build_options: -DWASMEDGE_PLUGIN_WASI_CRYPTO=ON -DWASMEDGE_PLUGIN_WASI_LOGGING=ON -DWASMEDGE_PLUGIN_RUSTLS=ON -DWASMEDGE_PLUGIN_PROCESS=ON -DWASMEDGE_PLUGIN_TENSORFLOW=ON -DWASMEDGE_PLUGIN_TENSORFLOWLITE=ON -DWASMEDGE_PLUGIN_IMAGE=ON -DWASMEDGE_PLUGIN_WASM_BPF=ON -DWASMEDGE_PLUGIN_OPENCVMINI=ON -DWASMEDGE_PLUGIN_ZLIB=ON
      tar_names: wasi_crypto wasi_logging wasmedge_rustls wasmedge_process wasmedge_tensorflow wasmedge_tensorflowlite wasmedge_image wasm_bpf wasmedge_opencvmini wasmedge_zlib
      output_bins: libwasmedgePluginWasiCrypto.so libwasmedgePluginWasiLogging.so libwasmedge_rustls.so libwasmedgePluginWasmEdgeProcess.so libwasmedgePluginWasmEdgeTensorflow.so libwasmedgePluginWasmEdgeTensorflowLite.so libwasmedgePluginWasmEdgeImage.so libwasmedgePluginWasmBpf.so libwasmedgePluginWasmEdgeOpenCVMini.so libwasmedgePluginWasmEdgeZlib.so
    needs: create_release
    container:
      image: wasmedge/wasmedge:ubuntu-build-clang-plugins-deps
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - name: Grant the safe directory for git
        run: |
          git config --global --add safe.directory $(pwd)
      - name: Install dependencies
        run: |
          apt update
          apt install -y libssl-dev cmake g++ wget unzip
          apt install -y libelf-dev zlib1g-dev pkg-config
          apt install -y cargo
      - name: Build plugins
        shell: bash
        run: |
          outbin_array=(${output_bins})
          cmake -Bbuild -GNinja -DCMAKE_BUILD_TYPE=Release -DWASMEDGE_BUILD_AOT_RUNTIME=OFF -DWASMEDGE_BUILD_TOOLS=OFF ${build_options}
          for (( i=0; i<${#outbin_array[@]}; i++ ));
          do
            echo "Building ${outbin_array[$i]} :"
            outtarget=${outbin_array[$i]}
            outtarget=${outtarget%.*}
            outtarget=${outtarget#lib}
            cmake --build build --target ${outtarget}
          done
      - name: Prepare the plugins tar.gz package
        shell: bash
        run: |
          plugin_array=(${tar_names})
          outbin_array=(${output_bins})
          for (( i=0; i<${#plugin_array[@]}; i++ ));
          do
            echo "Copying ${plugin_array[$i]} :"
            cp ${output_prefix}/${plugin_array[$i]}/${outbin_array[$i]} ${outbin_array[$i]}
            tar -zcvf plugin_${plugin_array[$i]}.tar.gz ${outbin_array[$i]}
          done
      - name: Install gh
        run: |
          type -p curl >/dev/null || (apt update && apt install curl -y)
          curl -fsSL https://cli.github.com/packages/githubcli-archive-keyring.gpg | dd of=/usr/share/keyrings/githubcli-archive-keyring.gpg \
          && chmod go+r /usr/share/keyrings/githubcli-archive-keyring.gpg \
          && echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/githubcli-archive-keyring.gpg] https://cli.github.com/packages stable main" | tee /etc/apt/sources.list.d/github-cli.list > /dev/null \
          && apt update \
          && apt install gh -y
      - name: Upload wasi_crypto plugin tar.gz package
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          mv plugin_wasi_crypto.tar.gz WasmEdge-plugin-wasi_crypto-${{ needs.create_release.outputs.version }}-ubuntu20.04_x86_64.tar.gz
          gh release upload ${{ needs.create_release.outputs.version }} WasmEdge-plugin-wasi_crypto-${{ needs.create_release.outputs.version }}-ubuntu20.04_x86_64.tar.gz --clobber
      - name: Upload wasi_logging plugin tar.gz package
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          mv plugin_wasi_logging.tar.gz WasmEdge-plugin-wasi_logging-${{ needs.create_release.outputs.version }}-ubuntu20.04_x86_64.tar.gz
          gh release upload ${{ needs.create_release.outputs.version }} WasmEdge-plugin-wasi_logging-${{ needs.create_release.outputs.version }}-ubuntu20.04_x86_64.tar.gz --clobber
      - name: Upload wasmedge_rustls plugin tar.gz package
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          mv plugin_wasmedge_rustls.tar.gz WasmEdge-plugin-wasmedge_rustls-${{ needs.create_release.outputs.version }}-ubuntu20.04_x86_64.tar.gz
          gh release upload ${{ needs.create_release.outputs.version }} WasmEdge-plugin-wasmedge_rustls-${{ needs.create_release.outputs.version }}-ubuntu20.04_x86_64.tar.gz --clobber
      - name: Upload wasmedge_process plugin tar.gz package
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          mv plugin_wasmedge_process.tar.gz WasmEdge-plugin-wasmedge_process-${{ needs.create_release.outputs.version }}-ubuntu20.04_x86_64.tar.gz
          gh release upload ${{ needs.create_release.outputs.version }} WasmEdge-plugin-wasmedge_process-${{ needs.create_release.outputs.version }}-ubuntu20.04_x86_64.tar.gz --clobber
      - name: Upload wasmedge_tensorflow plugin tar.gz package
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          mv plugin_wasmedge_tensorflow.tar.gz WasmEdge-plugin-wasmedge_tensorflow-${{ needs.create_release.outputs.version }}-ubuntu20.04_x86_64.tar.gz
          gh release upload ${{ needs.create_release.outputs.version }} WasmEdge-plugin-wasmedge_tensorflow-${{ needs.create_release.outputs.version }}-ubuntu20.04_x86_64.tar.gz --clobber
      - name: Upload wasmedge_tensorflowlite plugin tar.gz package
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          mv plugin_wasmedge_tensorflowlite.tar.gz WasmEdge-plugin-wasmedge_tensorflowlite-${{ needs.create_release.outputs.version }}-ubuntu20.04_x86_64.tar.gz
          gh release upload ${{ needs.create_release.outputs.version }} WasmEdge-plugin-wasmedge_tensorflowlite-${{ needs.create_release.outputs.version }}-ubuntu20.04_x86_64.tar.gz --clobber
      - name: Upload wasmedge_image plugin tar.gz package
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          mv plugin_wasmedge_image.tar.gz WasmEdge-plugin-wasmedge_image-${{ needs.create_release.outputs.version }}-ubuntu20.04_x86_64.tar.gz
          gh release upload ${{ needs.create_release.outputs.version }} WasmEdge-plugin-wasmedge_image-${{ needs.create_release.outputs.version }}-ubuntu20.04_x86_64.tar.gz --clobber
      - name: Upload wasm_bpf plugin tar.gz package
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          mv plugin_wasm_bpf.tar.gz WasmEdge-plugin-wasm_bpf-${{ needs.create_release.outputs.version }}-ubuntu20.04_x86_64.tar.gz
          gh release upload ${{ needs.create_release.outputs.version }} WasmEdge-plugin-wasm_bpf-${{ needs.create_release.outputs.version }}-ubuntu20.04_x86_64.tar.gz --clobber
      - name: Upload wasmedge_opencvmini plugin tar.gz package
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          mv plugin_wasmedge_opencvmini.tar.gz WasmEdge-plugin-wasmedge_opencvmini-${{ needs.create_release.outputs.version }}-ubuntu20.04_x86_64.tar.gz
          gh release upload ${{ needs.create_release.outputs.version }} WasmEdge-plugin-wasmedge_opencvmini-${{ needs.create_release.outputs.version }}-ubuntu20.04_x86_64.tar.gz --clobber
      - name: Upload wasmedge_zlib plugin tar.gz package
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          mv plugin_wasmedge_zlib.tar.gz WasmEdge-plugin-wasmedge_zlib-${{ needs.create_release.outputs.version }}-ubuntu20.04_x86_64.tar.gz
          gh release upload ${{ needs.create_release.outputs.version }} WasmEdge-plugin-wasmedge_zlib-${{ needs.create_release.outputs.version }}-ubuntu20.04_x86_64.tar.gz --clobber

  build_and_upload_wasinn_manylinux:
    strategy:
      matrix:
        include:
          - name: Plugins_x86_64
            host_runner: ubuntu-latest
            docker_tag: manylinux2014_x86_64
          - name: Plugins_aarch64
            host_runner: linux-arm64
            docker_tag: manylinux2014_aarch64
    name: Build and upload WASI-NN on ${{ matrix.docker_tag }}
    needs: create_release
    runs-on: ${{ matrix.host_runner }}
    env:
      output_dir: build/plugins/wasi_nn
      build_options_all_platforms: -DWASMEDGE_PLUGIN_WASI_NN_BACKEND=TensorFlowLite -DWASMEDGE_PLUGIN_WASI_NN_BACKEND=GGML
      build_options_manylinux2014_x86_64: -DWASMEDGE_PLUGIN_WASI_NN_BACKEND=PyTorch
      build_options_manylinux2014_aarch64:
      tar_names_all_platforms: wasi_nn-tensorflowlite wasi_nn-ggml
      tar_names_manylinux2014_x86_64: wasi_nn-pytorch
      tar_names_manylinux2014_aarch64:
      output_bin: libwasmedgePluginWasiNN.so
      PYTORCH_VERSION: "1.8.2"
      PYTORCH_INSTALL_TO: "."
    container:
      image: wasmedge/wasmedge:${{ matrix.docker_tag }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - name: Grant the safe directory for git
        run: |
          git config --global --add safe.directory $(pwd)
      - name: Install dependencies
        shell: bash
        run: |
          mkdir -p build
          bash ./utils/wasi-nn/install-pytorch.sh --disable-cxx11-abi
      - name: Build WASI-NN plugin
        shell: bash
        run: |
          export Torch_DIR=$(pwd)/${PYTORCH_INSTALL_TO}/libtorch
          plugin_array=(${tar_names_all_platforms} ${tar_names_${{ matrix.docker_tag }}})
          option_array=(${build_options_all_platforms} ${build_options_${{ matrix.docker_tag }}})
          testbin_array=(${test_bins_all_platforms} ${test_bins_${{ matrix.docker_tag }}})
          outtarget=${output_bin%.*}
          outtarget=${outtarget#lib}

          cmake -Bbuild -GNinja -DCMAKE_BUILD_TYPE=Release -DWASMEDGE_BUILD_AOT_RUNTIME=OFF -DWASMEDGE_BUILD_TOOLS=OFF -DWASMEDGE_PLUGIN_WASI_NN_GGML_LLAMA_BLAS=OFF
          for (( i=0; i<${#plugin_array[@]}; i++ ));
          do
            echo "Building ${plugin_array[$i]} backend:"
            cmake -Bbuild -GNinja ${option_array[$i]} -DWASMEDGE_PLUGIN_WASI_NN_GGML_LLAMA_BLAS=OFF
            cmake --build build --target ${outtarget}

            echo "Copying ${plugin_array[$i]} backend:"
            cp -f ${output_dir}/${output_bin} ${output_bin}
            tar -zcvf plugin_${plugin_array[$i]}.tar.gz ${output_bin}
          done
      - name: Install gh on manylinux
        run: |
          type -p yum-config-manager >/dev/null || sudo yum install yum-utils
          yum-config-manager --add-repo https://cli.github.com/packages/rpm/gh-cli.repo
          yum install -y gh
      - name: Upload WasmEdge wasi_nn-pytorch plugin tar.gz package
        if: contains(matrix.docker_tag, 'manylinux2014_x86_64')
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          mv plugin_wasi_nn-pytorch.tar.gz WasmEdge-plugin-wasi_nn-pytorch-${{ needs.create_release.outputs.version }}-${{ matrix.docker_tag }}.tar.gz
          gh release upload ${{ needs.create_release.outputs.version }} WasmEdge-plugin-wasi_nn-pytorch-${{ needs.create_release.outputs.version }}-${{ matrix.docker_tag }}.tar.gz --clobber
      - name: Upload WasmEdge wasi_nn-tensorflowlite plugin tar.gz package
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          mv plugin_wasi_nn-tensorflowlite.tar.gz WasmEdge-plugin-wasi_nn-tensorflowlite-${{ needs.create_release.outputs.version }}-${{ matrix.docker_tag }}.tar.gz
          gh release upload ${{ needs.create_release.outputs.version }} WasmEdge-plugin-wasi_nn-tensorflowlite-${{ needs.create_release.outputs.version }}-${{ matrix.docker_tag }}.tar.gz --clobber
      - name: Upload WasmEdge wasi_nn-ggml plugin tar.gz package
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          mv plugin_wasi_nn-ggml.tar.gz WasmEdge-plugin-wasi_nn-ggml-${{ needs.create_release.outputs.version }}-${{ matrix.docker_tag }}.tar.gz
          gh release upload ${{ needs.create_release.outputs.version }} WasmEdge-plugin-wasi_nn-ggml-${{ needs.create_release.outputs.version }}-${{ matrix.docker_tag }}.tar.gz --clobber

  build_and_upload_plugin_manylinux:
    strategy:
      matrix:
        include:
          - name: Plugins_x86_64
            host_runner: ubuntu-latest
            docker_tag: manylinux2014_x86_64-plugins-deps
          - name: Plugins_aarch64
            host_runner: linux-arm64
            docker_tag: manylinux2014_aarch64-plugins-deps
    name: Build and upload plugins on ${{ matrix.docker_tag }}
    runs-on: ${{ matrix.host_runner }}
    env:
      output_prefix: build/plugins
      build_options_all_platforms: -DWASMEDGE_PLUGIN_WASI_CRYPTO=ON -DWASMEDGE_PLUGIN_WASI_LOGGING=ON -DWASMEDGE_PLUGIN_PROCESS=ON -DWASMEDGE_PLUGIN_TENSORFLOW=ON -DWASMEDGE_PLUGIN_TENSORFLOWLITE=ON -DWASMEDGE_PLUGIN_IMAGE=ON -DWASMEDGE_PLUGIN_OPENCVMINI=ON -DWASMEDGE_PLUGIN_ZLIB=ON
      build_options_manylinux2014_x86_64: -DWASMEDGE_PLUGIN_WASM_BPF=ON -DWASMEDGE_PLUGIN_RUSTLS=ON -DWASMEDGE_PLUGIN_WASM_BPF_BUILD_LIBBPF_WITH_PKG_CONF=OFF
      build_options_manylinux2014_aarch64:
      tar_names_all_platforms: wasi_crypto wasi_logging wasmedge_process wasmedge_tensorflow wasmedge_tensorflowlite wasmedge_image wasmedge_opencvmini wasmedge_zlib
      tar_names_manylinux2014_x86_64: wasm_bpf wasmedge_rustls
      tar_names_manylinux2014_aarch64:
      output_bins_all_platforms: libwasmedgePluginWasiCrypto.so libwasmedgePluginWasiLogging.so libwasmedgePluginWasmEdgeProcess.so libwasmedgePluginWasmEdgeTensorflow.so libwasmedgePluginWasmEdgeTensorflowLite.so libwasmedgePluginWasmEdgeImage.so libwasmedgePluginWasmEdgeOpenCVMini.so libwasmedgePluginWasmEdgeZlib.so
      output_bins_manylinux2014_x86_64: libwasmedgePluginWasmBpf.so libwasmedge_rustls.so
      output_bins_manylinux2014_aarch64:
    needs: create_release
    container:
      image: wasmedge/wasmedge:${{ matrix.docker_tag }}
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - name: Grant the safe directory for git
        run: |
          git config --global --add safe.directory $(pwd)
      - name: Build and install dependencies
        run: |
          yum update -y
          yum install -y zlib-devel zlib-static cmake curl wget unzip
          curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | bash -s -- -y
          source "$HOME/.cargo/env"
          bash ./utils/wasi-crypto/build-openssl.sh
      - name: Build plugins
        shell: bash
        run: |
          source "$HOME/.cargo/env"
          outbin_array=(${output_bins_all_platforms} ${output_bins_${{ matrix.docker_tag }}})
          cmake -Bbuild -GNinja -DCMAKE_BUILD_TYPE=Release -DWASMEDGE_BUILD_AOT_RUNTIME=OFF -DWASMEDGE_BUILD_TOOLS=OFF -DWASMEDGE_BUILD_AOT_RUNTIME=OFF -DWASMEDGE_BUILD_TOOLS=OFF ${build_options_all_platforms} ${build_options_${{ matrix.docker_tag }}} -DOPENSSL_ROOT_DIR=$(pwd)/openssl-1.1.1n/openssl
          for (( i=0; i<${#outbin_array[@]}; i++ ));
          do
            echo "Building ${outbin_array[$i]} :"
            outtarget=${outbin_array[$i]}
            outtarget=${outtarget%.*}
            outtarget=${outtarget#lib}
            cmake --build build --target ${outtarget}
          done
      - name: Prepare the plugins tar.gz package
        shell: bash
        run: |
          plugin_array=(${tar_names_all_platforms} ${tar_names_${{ matrix.docker_tag }}})
          outbin_array=(${output_bins_all_platforms} ${output_bins_${{ matrix.docker_tag }}})
          for (( i=0; i<${#plugin_array[@]}; i++ ));
          do
            echo "Copying ${plugin_array[$i]} :"
            cp ${output_prefix}/${plugin_array[$i]}/${outbin_array[$i]} ${outbin_array[$i]}
            tar -zcvf plugin_${plugin_array[$i]}.tar.gz ${outbin_array[$i]}
          done
      - name: Install gh on manylinux
        run: |
          type -p yum-config-manager >/dev/null || sudo yum install yum-utils
          yum-config-manager --add-repo https://cli.github.com/packages/rpm/gh-cli.repo
          yum install -y gh
      - name: Upload wasi_crypto plugin tar.gz package
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          mv plugin_wasi_crypto.tar.gz WasmEdge-plugin-wasi_crypto-${{ needs.create_release.outputs.version }}-${{ matrix.docker_tag }}.tar.gz
          gh release upload ${{ needs.create_release.outputs.version }} WasmEdge-plugin-wasi_crypto-${{ needs.create_release.outputs.version }}-${{ matrix.docker_tag }}.tar.gz --clobber
      - name: Upload wasi_logging plugin tar.gz package
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          mv plugin_wasi_logging.tar.gz WasmEdge-plugin-wasi_logging-${{ needs.create_release.outputs.version }}-${{ matrix.docker_tag }}.tar.gz
          gh release upload ${{ needs.create_release.outputs.version }} WasmEdge-plugin-wasi_logging-${{ needs.create_release.outputs.version }}-${{ matrix.docker_tag }}.tar.gz --clobber
      - name: Upload wasmedge_rustls plugin tar.gz package
        if: contains(matrix.docker_tag, 'manylinux2014_x86_64')
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          mv plugin_wasmedge_rustls.tar.gz WasmEdge-plugin-wasmedge_rustls-${{ needs.create_release.outputs.version }}-${{ matrix.docker_tag }}.tar.gz
          gh release upload ${{ needs.create_release.outputs.version }} WasmEdge-plugin-wasmedge_rustls-${{ needs.create_release.outputs.version }}-${{ matrix.docker_tag }}.tar.gz --clobber
      - name: Upload wasmedge_process plugin tar.gz package
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          mv plugin_wasmedge_process.tar.gz WasmEdge-plugin-wasmedge_process-${{ needs.create_release.outputs.version }}-${{ matrix.docker_tag }}.tar.gz
          gh release upload ${{ needs.create_release.outputs.version }} WasmEdge-plugin-wasmedge_process-${{ needs.create_release.outputs.version }}-${{ matrix.docker_tag }}.tar.gz --clobber
      - name: Upload wasmedge_tensorflow plugin tar.gz package
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          mv plugin_wasmedge_tensorflow.tar.gz WasmEdge-plugin-wasmedge_tensorflow-${{ needs.create_release.outputs.version }}-${{ matrix.docker_tag }}.tar.gz
          gh release upload ${{ needs.create_release.outputs.version }} WasmEdge-plugin-wasmedge_tensorflow-${{ needs.create_release.outputs.version }}-${{ matrix.docker_tag }}.tar.gz --clobber
      - name: Upload wasmedge_tensorflowlite plugin tar.gz package
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          mv plugin_wasmedge_tensorflowlite.tar.gz WasmEdge-plugin-wasmedge_tensorflowlite-${{ needs.create_release.outputs.version }}-${{ matrix.docker_tag }}.tar.gz
          gh release upload ${{ needs.create_release.outputs.version }} WasmEdge-plugin-wasmedge_tensorflowlite-${{ needs.create_release.outputs.version }}-${{ matrix.docker_tag }}.tar.gz --clobber
      - name: Upload wasmedge_image plugin tar.gz package
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          mv plugin_wasmedge_image.tar.gz WasmEdge-plugin-wasmedge_image-${{ needs.create_release.outputs.version }}-${{ matrix.docker_tag }}.tar.gz
          gh release upload ${{ needs.create_release.outputs.version }} WasmEdge-plugin-wasmedge_image-${{ needs.create_release.outputs.version }}-${{ matrix.docker_tag }}.tar.gz --clobber
      - name: Upload wasm_bpf plugin tar.gz package
        if: contains(matrix.docker_tag, 'manylinux2014_x86_64')
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          mv plugin_wasm_bpf.tar.gz WasmEdge-plugin-wasm_bpf-${{ needs.create_release.outputs.version }}-${{ matrix.docker_tag }}.tar.gz
          gh release upload ${{ needs.create_release.outputs.version }} WasmEdge-plugin-wasm_bpf-${{ needs.create_release.outputs.version }}-${{ matrix.docker_tag }}.tar.gz --clobber
      - name: Upload wasmedge_opencvmini plugin tar.gz package
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          mv plugin_wasmedge_opencvmini.tar.gz WasmEdge-plugin-wasmedge_opencvmini-${{ needs.create_release.outputs.version }}-${{ matrix.docker_tag }}.tar.gz
          gh release upload ${{ needs.create_release.outputs.version }} WasmEdge-plugin-wasmedge_opencvmini-${{ needs.create_release.outputs.version }}-${{ matrix.docker_tag }}.tar.gz --clobber
      - name: Upload wasmedge_zlib plugin tar.gz package
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          mv plugin_wasmedge_zlib.tar.gz WasmEdge-plugin-wasmedge_zlib-${{ needs.create_release.outputs.version }}-${{ matrix.docker_tag }}.tar.gz
          gh release upload ${{ needs.create_release.outputs.version }} WasmEdge-plugin-wasmedge_zlib-${{ needs.create_release.outputs.version }}-${{ matrix.docker_tag }}.tar.gz --clobber

  build_and_upload_plugin_macos:
    strategy:
      matrix:
        include:
          - name: Plugins_MacOS_11
            system: MacOS 11
            host_runner: macos-11
            darwin_version: darwin_20
            arch: x86_64
          - name: Plugins_MacOS_arm64
            system: MacOS 13 (arm64)
            host_runner: mac-arm64
            darwin_version: darwin_22
            arch: arm64
    name: Build and upload plugins on ${{ matrix.system }}
    runs-on: ${{ matrix.host_runner }}
    env:
      output_prefix: build/plugins
      build_options: -DWASMEDGE_PLUGIN_WASI_CRYPTO=ON -DWASMEDGE_PLUGIN_WASI_LOGGING=ON -DWASMEDGE_PLUGIN_RUSTLS=ON -DWASMEDGE_PLUGIN_TENSORFLOW=ON -DWASMEDGE_PLUGIN_TENSORFLOWLITE=ON -DWASMEDGE_PLUGIN_IMAGE=ON -DWASMEDGE_PLUGIN_OPENCVMINI=ON -DWASMEDGE_PLUGIN_ZLIB=ON
      tar_names: wasi_crypto wasi_logging wasmedge_rustls wasmedge_tensorflow wasmedge_tensorflowlite wasmedge_image wasmedge_opencvmini wasmedge_zlib
      output_bins: libwasmedgePluginWasiCrypto.dylib libwasmedgePluginWasiLogging.dylib libwasmedge_rustls.dylib libwasmedgePluginWasmEdgeTensorflow.dylib libwasmedgePluginWasmEdgeTensorflowLite.dylib libwasmedgePluginWasmEdgeImage.dylib libwasmedgePluginWasmEdgeOpenCVMini.dylib libwasmedgePluginWasmEdgeZlib.dylib
    needs: create_release
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - name: Grant the safe directory for git
        run: |
          git config --global --add safe.directory $(pwd)
      - name: Install dependencies
        run: |
          eval $(/opt/homebrew/bin/brew shellenv)
          brew install llvm@16 ninja cmake openssl wabt opencv rust gh
      - name: Build plugins
        shell: bash
        run: |
          eval $(/opt/homebrew/bin/brew shellenv)
          outbin_array=(${output_bins})
          export LLVM_DIR="$(brew --prefix)/opt/llvm@16/lib/cmake"
          export CC=clang
          export CXX=clang++
          cmake -Bbuild -GNinja -DCMAKE_BUILD_TYPE=Release -DWASMEDGE_BUILD_AOT_RUNTIME=OFF -DWASMEDGE_BUILD_TOOLS=OFF -DOPENSSL_ROOT_DIR=$(brew --prefix)/opt/openssl ${build_options}
          for (( i=0; i<${#outbin_array[@]}; i++ ));
          do
            echo "Building ${outbin_array[$i]} :"
            outtarget=${outbin_array[$i]}
            outtarget=${outtarget%.*}
            outtarget=${outtarget#lib}
            cmake --build build --target ${outtarget}
          done
      - name: Prepare the WasmEdge plugins tar.gz package
        shell: bash
        run: |
          plugin_array=(${tar_names})
          outbin_array=(${output_bins})
          for (( i=0; i<${#plugin_array[@]}; i++ ));
          do
            echo "Copying ${plugin_array[$i]} :"
            cp ${output_prefix}/${plugin_array[$i]}/${outbin_array[$i]} ${outbin_array[$i]}
            tar -zcvf plugin_${plugin_array[$i]}.tar.gz ${outbin_array[$i]}
          done
      - name: Upload wasi_crypto plugin tar.gz package
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          eval $(/opt/homebrew/bin/brew shellenv)
          mv plugin_wasi_crypto.tar.gz WasmEdge-plugin-wasi_crypto-${{ needs.create_release.outputs.version }}-darwin_${{ matrix.arch }}.tar.gz
          gh release upload ${{ needs.create_release.outputs.version }} WasmEdge-plugin-wasi_crypto-${{ needs.create_release.outputs.version }}-darwin_${{ matrix.arch }}.tar.gz --clobber
      - name: Upload wasi_logging plugin tar.gz package
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          eval $(/opt/homebrew/bin/brew shellenv)
          mv plugin_wasi_logging.tar.gz WasmEdge-plugin-wasi_logging-${{ needs.create_release.outputs.version }}-darwin_${{ matrix.arch }}.tar.gz
          gh release upload ${{ needs.create_release.outputs.version }} WasmEdge-plugin-wasi_logging-${{ needs.create_release.outputs.version }}-darwin_${{ matrix.arch }}.tar.gz --clobber
      - name: Upload wasmedge_rustls plugin tar.gz package
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          eval $(/opt/homebrew/bin/brew shellenv)
          mv plugin_wasmedge_rustls.tar.gz WasmEdge-plugin-wasmedge_rustls-${{ needs.create_release.outputs.version }}-darwin_${{ matrix.arch }}.tar.gz
          gh release upload ${{ needs.create_release.outputs.version }} WasmEdge-plugin-wasmedge_rustls-${{ needs.create_release.outputs.version }}-darwin_${{ matrix.arch }}.tar.gz --clobber
      - name: Upload wasmedge_tensorflow plugin tar.gz package
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          eval $(/opt/homebrew/bin/brew shellenv)
          mv plugin_wasmedge_tensorflow.tar.gz WasmEdge-plugin-wasmedge_tensorflow-${{ needs.create_release.outputs.version }}-darwin_${{ matrix.arch }}.tar.gz
          gh release upload ${{ needs.create_release.outputs.version }} WasmEdge-plugin-wasmedge_tensorflow-${{ needs.create_release.outputs.version }}-darwin_${{ matrix.arch }}.tar.gz --clobber
      - name: Upload wasmedge_tensorflowlite plugin tar.gz package
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          eval $(/opt/homebrew/bin/brew shellenv)
          mv plugin_wasmedge_tensorflowlite.tar.gz WasmEdge-plugin-wasmedge_tensorflowlite-${{ needs.create_release.outputs.version }}-darwin_${{ matrix.arch }}.tar.gz
          gh release upload ${{ needs.create_release.outputs.version }} WasmEdge-plugin-wasmedge_tensorflowlite-${{ needs.create_release.outputs.version }}-darwin_${{ matrix.arch }}.tar.gz --clobber
      - name: Upload wasmedge_image plugin tar.gz package
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          eval $(/opt/homebrew/bin/brew shellenv)
          mv plugin_wasmedge_image.tar.gz WasmEdge-plugin-wasmedge_image-${{ needs.create_release.outputs.version }}-darwin_${{ matrix.arch }}.tar.gz
          gh release upload ${{ needs.create_release.outputs.version }} WasmEdge-plugin-wasmedge_image-${{ needs.create_release.outputs.version }}-darwin_${{ matrix.arch }}.tar.gz --clobber
      - name: Upload wasmedge_opencvmini plugin tar.gz package
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          eval $(/opt/homebrew/bin/brew shellenv)
          mv plugin_wasmedge_opencvmini.tar.gz WasmEdge-plugin-wasmedge_opencvmini-${{ needs.create_release.outputs.version }}-darwin_${{ matrix.arch }}.tar.gz
          gh release upload ${{ needs.create_release.outputs.version }} WasmEdge-plugin-wasmedge_opencvmini-${{ needs.create_release.outputs.version }}-darwin_${{ matrix.arch }}.tar.gz --clobber
      - name: Upload wasmedge_zlib plugin tar.gz package
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          eval $(/opt/homebrew/bin/brew shellenv)
          mv plugin_wasmedge_zlib.tar.gz WasmEdge-plugin-wasmedge_zlib-${{ needs.create_release.outputs.version }}-darwin_${{ matrix.arch }}.tar.gz
          gh release upload ${{ needs.create_release.outputs.version }} WasmEdge-plugin-wasmedge_zlib-${{ needs.create_release.outputs.version }}-darwin_${{ matrix.arch }}.tar.gz --clobber

  build_manylinux2014_runtime_only:
    name: Build runtime only on manylinux2014 platform
    needs: create_release
    runs-on: ubuntu-latest
    container:
      image: wasmedge/wasmedge:manylinux2014_x86_64
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
      - name: Grant the safe directory for git
        run: |
          git config --global --add safe.directory $(pwd)
      - name: Build runtime only manylinux2014 package
        run: |
          bash utils/docker/build-manylinux.sh -DWASMEDGE_BUILD_AOT_RUNTIME=OFF
      - name: Upload ${{ matrix.name }} tar.gz package to artifact
        uses: actions/upload-artifact@v3
        with:
          name: build_manylinux2014_runtime_only
          path: build/WasmEdge-${{ needs.create_release.outputs.version }}-Linux.tar.gz
      - name: Install gh on manylinux
        run: |
          type -p yum-config-manager >/dev/null || sudo yum install yum-utils
          yum-config-manager --add-repo https://cli.github.com/packages/rpm/gh-cli.repo
          yum install -y gh
      - name: Upload ${{ matrix.name }} tar.gz package
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          mv build/WasmEdge-${{ needs.create_release.outputs.version }}-Linux.tar.xz WasmEdge-runtime-only-${{ needs.create_release.outputs.version }}-manylinux2014_x86_64.tar.xz
          gh release upload ${{ needs.create_release.outputs.version }} WasmEdge-runtime-only-${{ needs.create_release.outputs.version }}-manylinux2014_x86_64.tar.xz --clobber

  build_docker_slim_images:
    strategy:
      matrix:
        include:
          - name: wasmedge/slim:${{ needs.create_release.outputs.version }}
            artifact_name: WasmEdge-${{ needs.create_release.outputs.version }}-manylinux2014_x86_64.tar.gz
            include_bin: "--include-bin /usr/local/bin/wasmedge --include-bin /usr/local/bin/wasmedgec"
          - name: wasmedge/slim-runtime:${{ needs.create_release.outputs.version }}
            artifact_name: build_manylinux2014_runtime_only
            include_bin: "--include-bin /usr/local/bin/wasmedge"
    name: Build DockerSlim Images
    needs:
      [create_release, build_on_manylinux2014, build_manylinux2014_runtime_only]
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v3
      - name: Prepare tarball
        uses: actions/download-artifact@v3
        with:
          name: ${{ matrix.artifact_name }}
          path: utils/docker
      - name: Grant the safe directory for git
        run: |
          git config --global --add safe.directory $(pwd)
      - name: Install requirements
        run: |
          curl -sL https://raw.githubusercontent.com/slimtoolkit/slim/master/scripts/install-slim.sh | sudo -E bash -
      - name: Prepare docker env
        id: docker_env
        run: |
          echo "docker_image=${{ matrix.name }}" >> $GITHUB_OUTPUT
          echo "created=$(date -u +'%Y-%m-%dT%H:%M:%SZ')" >> $GITHUB_OUTPUT
      - name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v3
      - name: Login to DockerHub
        uses: docker/login-action@v3
        with:
          username: ${{ secrets.DOCKER_USERNAME }}
          password: ${{ secrets.DOCKER_ACCESS_TOKEN }}
      - name: Run slim
        run: |
          slim build \
            --dockerfile Dockerfile.release \
            --dockerfile-context utils/docker \
            --tag ${{ steps.docker_env.outputs.docker_image }} \
            --http-probe-off \
            --include-bin /usr/lib/x86_64-linux-gnu/libnss_compat.so.2 \
            --include-bin /usr/lib/x86_64-linux-gnu/libnss_dns.so.2 \
            --include-bin /usr/lib/x86_64-linux-gnu/libnss_files.so.2 \
            --include-bin /usr/lib/x86_64-linux-gnu/libresolv.so.2 \
            --include-path /etc/services \
            ${{ matrix.include_bin }} \
            --cbo-build-arg VERSION="${{ needs.create_release.outputs.version }}" \
            --cbo-label org.opencontainers.image.title="${{ github.event.repository.name }}" \
            --cbo-label org.opencontainers.image.description="${{ github.event.repository.description }}" \
            --cbo-label org.opencontainers.image.url="${{ github.event.repository.html_url }}" \
            --cbo-label org.opencontainers.image.source="${{ github.event.repository.clone_url }}" \
            --cbo-label org.opencontainers.image.version="${{ needs.create_release.outputs.version }}" \
            --cbo-label org.opencontainers.image.created="${{ steps.docker_env.outputs.created }}" \
            --cbo-label org.opencontainers.image.revision="${{ github.sha }}" \
            --cbo-label org.opencontainers.image.licenses="${{ github.event.repository.license.spdx_id }}"
      - name: Push to DockerHub
        run: |
          docker push ${{ steps.docker_env.outputs.docker_image }}
