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

on:
  workflow_call:
    inputs:
      arch:
        description: arch of the release
        type: string
        required: false
        default: x86_64
      runner:
        description: OS of compilation
        type: string
        required: true
      upload_url:
        description: upload binary assets to the URL of release
        type: string
        required: true
      ver_num:
        description: a semantic version number
        type: string
        required: true
      wasi_sdk_url:
        description: download WASI_SDK from this URL
        type: string
        required: false
        default: "https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-20/wasi-sdk-20.0-linux.tar.gz"

jobs:
  try_reuse:
    uses: ./.github/workflows/reuse_latest_release_binaries.yml
    with:
      binary_name_stem: "wamr-lldb-${{ inputs.ver_num }}-${{ inputs.arch }}-${{ inputs.runner }}"
      last_commit: "ea63ba4bd010c2285623ad4acc0262a4d63bcfea"
      the_path: "./build-scripts/lldb_wasm.patch"
      upload_url: ${{ inputs.upload_url }}

  build:
    needs: try_reuse
    if: needs.try_reuse.outputs.result != 'hit'
    runs-on: ${{ inputs.runner }}

    env:
      PYTHON_VERSION: '3.10'
      PYTHON_UBUNTU_STANDALONE_BUILD: https://github.com/indygreg/python-build-standalone/releases/download/20230507/cpython-3.10.11+20230507-x86_64-unknown-linux-gnu-install_only.tar.gz
      PYTHON_MACOS_STANDALONE_BUILD: https://github.com/indygreg/python-build-standalone/releases/download/20230507/cpython-3.10.11+20230507-x86_64-apple-darwin-install_only.tar.gz
    steps:
      - uses: actions/checkout@v3

      - name: download and install wasi-sdk
        run: |
          cd /opt
          basename=$(basename ${{ inputs.wasi_sdk_url }})
          sudo wget --progress=dot:giga ${{ inputs.wasi_sdk_url }}
          sudo tar -xzf ${basename}
          sudo rm ${basename}
          sudo mv wasi-sdk-* wasi-sdk

      - name: Cache build
        id: lldb_build_cache
        uses: actions/cache@v3
        with:
          path: |
            ./core/deps/llvm-project/build/bin
            ./core/deps/llvm-project/build/include
            ./core/deps/llvm-project/build/lib
            ./core/deps/llvm-project/build/libexec
            ./core/deps/llvm-project/build/share
            ./core/deps/llvm-project/lldb/tools/
            ./core/deps/llvm-project/wamr-lldb/
          key: ${{inputs.arch}}-${{ inputs.runner }}-lldb_build

      - name: setup xcode macos
        if: steps.lldb_build_cache.outputs.cache-hit != 'true' && contains(inputs.runner, 'macos')
        uses: maxim-lobanov/setup-xcode@v1
        with:
          xcode-version: latest-stable

      # Remove xCode command line tools, to prevent duplicate symbol compilation failures
      - name: install utils macos
        if: steps.lldb_build_cache.outputs.cache-hit != 'true' && contains(inputs.runner, 'macos')
        run: |
          brew remove swig
          brew install swig@4.1 cmake ninja libedit
          brew link --overwrite swig@4.1
          sudo rm -rf /Library/Developer/CommandLineTools

      - name: install utils ubuntu
        if: steps.lldb_build_cache.outputs.cache-hit != 'true' && contains(inputs.runner, 'ubuntu')
        run: sudo apt update && sudo apt-get install -y lld ninja-build

      # `git clone` takes ~7m
      - name: download llvm
        if: steps.lldb_build_cache.outputs.cache-hit != 'true'
        run: |
          wget https://github.com/llvm/llvm-project/archive/1f27fe6128769f00197925c3b8f6abb9d0e5cd2e.zip
          unzip -q 1f27fe6128769f00197925c3b8f6abb9d0e5cd2e.zip
          mv llvm-project-1f27fe6128769f00197925c3b8f6abb9d0e5cd2e llvm-project
        working-directory: core/deps/

      - name: apply wamr patch
        if: steps.lldb_build_cache.outputs.cache-hit != 'true'
        run: |
          git init
          git config user.email "action@github.com"
          git config user.name "github action"
          git apply ../../../build-scripts/lldb_wasm.patch
        working-directory: core/deps/llvm-project

      - name: get stand-alone python ubuntu
        if: steps.lldb_build_cache.outputs.cache-hit != 'true' && contains(inputs.runner, 'ubuntu')
        run: |
          wget ${{ env.PYTHON_UBUNTU_STANDALONE_BUILD }} -O python.tar.gz
          tar -xvf python.tar.gz
        working-directory: core/deps

      - name: get stand-alone python macos
        if: steps.lldb_build_cache.outputs.cache-hit != 'true' && contains(inputs.runner, 'macos')
        run: |
          wget ${{ env.PYTHON_MACOS_STANDALONE_BUILD }} -O python.tar.gz
          tar -xvf python.tar.gz
        working-directory: core/deps

      - name: build lldb ubuntu
        if: steps.lldb_build_cache.outputs.cache-hit != 'true' && contains(inputs.runner, 'ubuntu')
        run: |
          echo "start to build lldb..."
          mkdir -p wamr-lldb
          cmake -S ./llvm -B build \
            -G Ninja \
            -DCMAKE_INSTALL_PREFIX=../wamr-lldb \
            -DCMAKE_BUILD_TYPE:STRING="Release" \
            -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
            -DLLVM_ENABLE_PROJECTS="clang;lldb" \
            -DLLVM_TARGETS_TO_BUILD:STRING="X86;WebAssembly" \
            -DLLVM_BUILD_BENCHMARKS:BOOL=OFF \
            -DLLVM_BUILD_DOCS:BOOL=OFF \
            -DLLVM_BUILD_EXAMPLES:BOOL=OFF \
            -DLLVM_BUILD_LLVM_DYLIB:BOOL=OFF \
            -DLLVM_BUILD_TESTS:BOOL=OFF \
            -DLLVM_INCLUDE_BENCHMARKS:BOOL=OFF \
            -DLLVM_INCLUDE_DOCS:BOOL=OFF \
            -DLLVM_INCLUDE_EXAMPLES:BOOL=OFF \
            -DLLVM_INCLUDE_TESTS:BOOL=OFF \
            -DLLVM_ENABLE_BINDINGS:BOOL=OFF \
            -DLLVM_ENABLE_LIBXML2:BOOL=ON \
            -DLLVM_ENABLE_LLD:BOOL=ON \
            -DLLDB_ENABLE_PYTHON:BOOL=ON \
            -DLLDB_EMBED_PYTHON_HOME=ON \
            -DLLDB_PYTHON_HOME=.. \
            -DLLDB_PYTHON_RELATIVE_PATH=lib/lldb-python \
            -DPython3_EXECUTABLE="$(pwd)/../python/bin/python${{ env.PYTHON_VERSION }}"
          cmake --build build --target lldb install --parallel $(nproc)
        working-directory: core/deps/llvm-project

      - name: validate lldb ubuntu
        if: steps.lldb_build_cache.outputs.cache-hit != 'true' && contains(inputs.runner, 'ubuntu')
        run: |
          echo "start to validate lldb..."
          mkdir -p wamr-debug
          cmake -S product-mini/platforms/linux -B wamr-debug -DWAMR_BUILD_DEBUG_INTERP=1
          cmake --build wamr-debug --parallel $(nproc)
          export LD_LIBRARY_PATH=$(pwd)/core/deps/python/lib:${LD_LIBRARY_PATH}
          python3 ci/validate_lldb.py --port 1239 --lldb core/deps/wamr-lldb/bin/lldb --wamr wamr-debug/iwasm --verbose
        working-directory: .

      - name: build lldb macos
        if: steps.lldb_build_cache.outputs.cache-hit != 'true' && contains(inputs.runner, 'macos')
        run: |
          echo "start to build lldb..."
          mkdir -p wamr-lldb
          cmake -S ./llvm -B build \
            -G Ninja \
            -DCMAKE_INSTALL_PREFIX=../wamr-lldb \
            -DCMAKE_BUILD_TYPE:STRING="Release" \
            -DCMAKE_EXPORT_COMPILE_COMMANDS=ON \
            -DLLVM_ENABLE_PROJECTS="clang;lldb" \
            -DLLVM_TARGETS_TO_BUILD:STRING="X86;WebAssembly" \
            -DLLVM_BUILD_BENCHMARKS:BOOL=OFF \
            -DLLVM_BUILD_DOCS:BOOL=OFF \
            -DLLVM_BUILD_EXAMPLES:BOOL=OFF \
            -DLLVM_BUILD_LLVM_DYLIB:BOOL=OFF \
            -DLLVM_BUILD_TESTS:BOOL=OFF \
            -DLLVM_INCLUDE_BENCHMARKS:BOOL=OFF \
            -DLLVM_INCLUDE_DOCS:BOOL=OFF \
            -DLLVM_INCLUDE_EXAMPLES:BOOL=OFF \
            -DLLVM_INCLUDE_TESTS:BOOL=OFF \
            -DLLVM_ENABLE_BINDINGS:BOOL=OFF \
            -DLLVM_ENABLE_LIBXML2:BOOL=ON \
            -DLLDB_BUILD_FRAMEWORK:BOOL=OFF \
            -DLLDB_ENABLE_PYTHON:BOOL=ON \
            -DLLDB_EMBED_PYTHON_HOME=ON \
            -DLLDB_PYTHON_HOME=.. \
            -DLLDB_PYTHON_RELATIVE_PATH=lib/lldb-python \
            -DPython3_EXECUTABLE="$(pwd)/../python/bin/python${{ env.PYTHON_VERSION }}"
          cmake --build build --target lldb install --parallel $(nproc)
        working-directory: core/deps/llvm-project

      - name: pack a distribution
        if: steps.lldb_build_cache.outputs.cache-hit != 'true'
        run: |
          mkdir -p wamr-lldb/bin
          mkdir -p wamr-lldb/lib
          cp build/bin/lldb* wamr-lldb/bin
          cp lldb/tools/lldb-vscode/package.json wamr-lldb
          cp -r lldb/tools/lldb-vscode/syntaxes/ wamr-lldb
        working-directory: core/deps/llvm-project

      - name: pack ubuntu specific libraries
        if: steps.lldb_build_cache.outputs.cache-hit != 'true' && contains(inputs.runner, 'ubuntu')
        run: |
          cp build/lib/liblldb*.so wamr-lldb/lib
          cp build/lib/liblldb*.so.* wamr-lldb/lib
          cp -R build/lib/lldb-python wamr-lldb/lib
          cp -R ../python/lib/python* wamr-lldb/lib
          cp ../python/lib/libpython${{ env.PYTHON_VERSION }}.so.1.0 wamr-lldb/lib
        working-directory: core/deps/llvm-project

      - name: pack macos specific libraries
        if: steps.lldb_build_cache.outputs.cache-hit != 'true' && contains(inputs.runner, 'macos')
        run: |
          cp build/lib/liblldb*.dylib wamr-lldb/lib
          cp -R build/lib/lldb-python wamr-lldb/lib
          cp -R ../python/lib/python* wamr-lldb/lib
          cp ../python/lib/libpython*.dylib wamr-lldb/lib
          install_name_tool -change /install/lib/libpython${{ env.PYTHON_VERSION }}.dylib @rpath/libpython${{ env.PYTHON_VERSION }}.dylib wamr-lldb/lib/liblldb.*.dylib
        # Patch path of python library -> https://github.com/indygreg/python-build-standalone/blob/85923ca3911784e6978b85d56e06e9ae75cb2dc4/docs/quirks.rst?plain=1#L412-L446
        working-directory: core/deps/llvm-project

      - name: compress the binary
        run: |
          tar czf wamr-lldb-${{ inputs.ver_num }}-${{ inputs.runner }}.tar.gz wamr-lldb
          zip -r wamr-lldb-${{ inputs.ver_num }}-${{ inputs.runner }}.zip wamr-lldb
        working-directory: core/deps/llvm-project

      - 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: core/deps/llvm-project/wamr-lldb-${{ inputs.ver_num }}-${{ inputs.runner }}.tar.gz
          asset_name: wamr-lldb-${{ 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: core/deps/llvm-project/wamr-lldb-${{ inputs.ver_num }}-${{ inputs.runner }}.zip
          asset_name: wamr-lldb-${{ inputs.ver_num }}-${{ inputs.arch }}-${{ inputs.runner }}.zip
          asset_content_type: application/zip
