name: Build on MacOS

on:
  workflow_call:
    inputs:
      version:
        type: string
        required: true
      matrix: # [ { name, runner, darwin_version }, ... ]
        type: string
        required: true
      release:
        type: boolean

jobs:
  build_on_macos:
    strategy:
      fail-fast: false
      matrix:
        include: ${{ fromJSON(inputs.matrix) }}
    name: ${{ matrix.name }}
    runs-on: ${{ matrix.runner }}
    env:
      BUILD_TESTS: ON
      BUILD_TYPE: Debug
    steps:
      - uses: actions/checkout@v3
        with:
          fetch-depth: 0
      - name: Ensure git safe directory
        run: |
          git config --global --add safe.directory $(pwd)
      - name: Setup build environment
        run: |
          eval $(/opt/homebrew/bin/brew shellenv)
          brew install llvm@16 ninja cmake wabt
      - name: Set environment variables for release
        if: ${{ inputs.release }}
        run: |
          echo "BUILD_TESTS=OFF" | tee -a $GITHUB_ENV
          echo "BUILD_TYPE=Release" | tee -a $GITHUB_ENV
      - name: Build WasmEdge
        run: |
          eval $(/opt/homebrew/bin/brew shellenv)
          export LLVM_DIR="$(brew --prefix)/opt/llvm@16/lib/cmake"
          export CC=clang
          export CXX=clang++
          rm -rf build output
          mkdir -p output
          cmake -Bbuild -GNinja -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DWASMEDGE_BUILD_TESTS=$BUILD_TESTS -DWASMEDGE_LINK_LLVM_STATIC=ON -DWASMEDGE_BUILD_PACKAGE="TGZ" -DCMAKE_INSTALL_PREFIX:PATH=$GITHUB_WORKSPACE/output .
          cmake --build build
      - name: Test WasmEdge
        run: |
          eval $(/opt/homebrew/bin/brew shellenv)
          export DYLD_LIBRARY_PATH="$(pwd)/build/lib/api:$DYLD_LIBRARY_PATH"
          cd build
          ctest
      - name: Create package tarball
        run: |
          eval $(/opt/homebrew/bin/brew shellenv)
          cmake --build build --target package
      - name: Test Standalone WasmEdge without LLVM
        run: |
          eval $(/opt/homebrew/bin/brew shellenv)
          export DYLD_LIBRARY_PATH="$(pwd)/build/lib/api:$DYLD_LIBRARY_PATH"
          echo "otool -L ./build/tools/wasmedge/wasmedge:"
          otool -L ./build/tools/wasmedge/wasmedge
          echo "otool -L ./build/tools/wasmedge/wasmedgec:"
          otool -L ./build/tools/wasmedge/wasmedgec
          echo "otool -L ./build/lib/api/libwasmedge.dylib:"
          otool -L ./build/lib/api/libwasmedge.dylib
          if otool -L ./build/lib/api/libwasmedge.dylib | grep libLLVM; then echo "Fail: libLLVM shared library linked" && exit 1; else echo "Pass: libLLVM shared library not linked"; fi;

          echo "Generate fibonacci.wasm from fibonacci.wat"
          wat2wasm examples/wasm/fibonacci.wat -o examples/wasm/fibonacci.wasm
          echo "./build/tools/wasmedge/wasmedgec examples/wasm/fibonacci.wasm fib_c.dylib"
          ./build/tools/wasmedge/wasmedgec examples/wasm/fibonacci.wasm fib_c.dylib
          echo "./build/tools/wasmedge/wasmedge --reactor fib_c.dylib fib 20"
          ./build/tools/wasmedge/wasmedge --reactor fib_c.dylib fib 20
          echo "./build/tools/wasmedge/wasmedge compile examples/wasm/fibonacci.wasm fib_compile.dylib"
          ./build/tools/wasmedge/wasmedge compile examples/wasm/fibonacci.wasm fib_compile.dylib
          echo "./build/tools/wasmedge/wasmedge --reactor fib_compile.dylib fib 20"
          ./build/tools/wasmedge/wasmedge --reactor fib_compile.dylib fib 20
      - name: Upload artifact
        if: ${{ !inputs.release }}
        uses: actions/upload-artifact@v3
        with:
          name: WasmEdge-${{ inputs.version }}-darwin_${{ matrix.darwin_version }}_${{ matrix.arch }}.tar.gz
          path: build/WasmEdge-${{ inputs.version }}-Darwin.tar.gz
      - name: Upload package tarball
        if: ${{ inputs.release }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        run: |
          eval $(/opt/homebrew/bin/brew shellenv)
          mv build/WasmEdge-${{ inputs.version }}-Darwin.tar.gz WasmEdge-${{ inputs.version }}-darwin_${{ matrix.arch }}.tar.gz
          gh release upload ${{ inputs.version }} WasmEdge-${{ inputs.version }}-darwin_${{ matrix.arch }}.tar.gz --clobber
