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

name: compilation on macos

on:
  # will be triggered on PR events
  pull_request:
    types:
      - opened
      - synchronize
    paths:
      - ".github/workflows/build_llvm_libraries.yml"
      - ".github/workflows/compilation_on_macos.yml"
      - "build-scripts/**"
      - "core/**"
      - "!core/deps/**"
      - "product-mini/**"
      - "samples/**"
      - "!samples/workload/**"
      - "tests/wamr-test-suites/**"
      - "wamr-compiler/**"
      - "wamr-sdk/**"
  # will be triggered on push events
  push:
    branches:
      - main
      - "dev/**"
    paths:
      - ".github/workflows/build_llvm_libraries.yml"
      - ".github/workflows/compilation_on_macos.yml"
      - "build-scripts/**"
      - "core/**"
      - "!core/deps/**"
      - "product-mini/**"
      - "samples/**"
      - "!samples/workload/**"
      - "tests/wamr-test-suites/**"
      - "wamr-compiler/**"
      - "wamr-sdk/**"
  # allow to be triggered manually
  workflow_dispatch:

# Cancel any in-flight jobs for the same PR/branch so there's only one active
# at a time
concurrency:
  group: ${{ github.workflow }}-${{ github.ref }}
  cancel-in-progress: true

env:
  AOT_BUILD_OPTIONS: "-DWAMR_BUILD_AOT=1 -DWAMR_BUILD_FAST_INTERP=0 -DWAMR_BUILD_INTERP=0 -DWAMR_BUILD_JIT=0 -DWAMR_BUILD_LAZY_JIT=0"
  CLASSIC_INTERP_BUILD_OPTIONS: "-DWAMR_BUILD_AOT=0 -DWAMR_BUILD_FAST_INTERP=0 -DWAMR_BUILD_INTERP=1 -DWAMR_BUILD_JIT=0 -DWAMR_BUILD_LAZY_JIT=0"
  FAST_INTERP_BUILD_OPTIONS: "-DWAMR_BUILD_AOT=0 -DWAMR_BUILD_FAST_INTERP=1 -DWAMR_BUILD_INTERP=1 -DWAMR_BUILD_JIT=0 -DWAMR_BUILD_LAZY_JIT=0"
  LLVM_LAZY_JIT_BUILD_OPTIONS: "-DWAMR_BUILD_AOT=1 -DWAMR_BUILD_FAST_INTERP=0 -DWAMR_BUILD_INTERP=0 -DWAMR_BUILD_JIT=1 -DWAMR_BUILD_LAZY_JIT=1"
  LLVM_EAGER_JIT_BUILD_OPTIONS: "-DWAMR_BUILD_AOT=1 -DWAMR_BUILD_FAST_INTERP=0 -DWAMR_BUILD_INTERP=0 -DWAMR_BUILD_JIT=1 -DWAMR_BUILD_LAZY_JIT=0"

jobs:
  build_llvm_libraries_on_intel_macos:
    uses: ./.github/workflows/build_llvm_libraries.yml
    with:
      os: "macos-latest"
      arch: "X86"
  build_llvm_libraries_on_arm_macos:
    uses: ./.github/workflows/build_llvm_libraries.yml
    with:
      os: "macos-14"
      arch: "AArch64 ARM"

  build_wamrc:
    needs: [build_llvm_libraries_on_intel_macos]
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        include:
          - os: macos-latest
            llvm_cache_key: ${{ needs.build_llvm_libraries_on_intel_macos.outputs.cache_key }}
    steps:
      - name: checkout
        uses: actions/checkout@v4

      - name: Get LLVM libraries
        id: retrieve_llvm_libs
        uses: actions/cache@v4
        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: ${{ matrix.llvm_cache_key }}

      - name: Quit if cache miss
        if: steps.retrieve_llvm_libs.outputs.cache-hit != 'true'
        run: echo "::error::can not get prebuilt llvm libraries" && exit 1

      - name: Build wamrc
        run: |
          mkdir build && cd build
          cmake ..
          cmake --build . --config Release --parallel 4
        working-directory: wamr-compiler

  build_iwasm:
    needs: [build_llvm_libraries_on_intel_macos]
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        make_options_run_mode: [
            # Running mode
            $AOT_BUILD_OPTIONS,
            $CLASSIC_INTERP_BUILD_OPTIONS,
            $FAST_INTERP_BUILD_OPTIONS,
            $LLVM_LAZY_JIT_BUILD_OPTIONS,
            $LLVM_EAGER_JIT_BUILD_OPTIONS,
          ]
        make_options_feature: [
            # Features
            "-DWAMR_BUILD_CUSTOM_NAME_SECTION=1",
            # doesn't support
            #"-DWAMR_BUILD_DEBUG_AOT=1",
            "-DWAMR_BUILD_DEBUG_INTERP=1",
            "-DWAMR_BUILD_DUMP_CALL_STACK=1",
            "-DWAMR_BUILD_LIB_PTHREAD=1",
            "-DWAMR_BUILD_LIB_WASI_THREADS=1",
            "-DWAMR_BUILD_LOAD_CUSTOM_SECTION=1",
            "-DWAMR_BUILD_MINI_LOADER=1",
            "-DWAMR_BUILD_MEMORY_PROFILING=1",
            "-DWAMR_BUILD_MULTI_MODULE=1",
            "-DWAMR_BUILD_PERF_PROFILING=1",
            "-DWAMR_BUILD_REF_TYPES=1",
            "-DWAMR_BUILD_SIMD=1",
            "-DWAMR_BUILD_TAIL_CALL=1",
            "-DWAMR_DISABLE_HW_BOUND_CHECK=1",
          ]
        os: [macos-latest]
        platform: [darwin]
        exclude:
          # uncompatiable feature and platform
          # uncompatiable mode and feature
          # MULTI_MODULE only on INTERP mode and AOT mode
          - make_options_run_mode: $LLVM_LAZY_JIT_BUILD_OPTIONS
            make_options_feature: "-DWAMR_BUILD_MULTI_MODULE=1"
          - make_options_run_mode: $LLVM_EAGER_JIT_BUILD_OPTIONS
            make_options_feature: "-DWAMR_BUILD_MULTI_MODULE=1"
          # SIMD only on JIT/AOT mode
          - make_options_run_mode: $CLASSIC_INTERP_BUILD_OPTIONS
            make_options_feature: "-DWAMR_BUILD_SIMD=1"
          - make_options_run_mode: $FAST_INTERP_BUILD_OPTIONS
            make_options_feature: "-DWAMR_BUILD_SIMD=1"
          # DEBUG_INTERP only on CLASSIC INTERP mode
          - make_options_run_mode: $AOT_BUILD_OPTIONS
            make_options_feature: "-DWAMR_BUILD_DEBUG_INTERP=1"
          - make_options_run_mode: $LLVM_LAZY_JIT_BUILD_OPTIONS
            make_options_feature: "-DWAMR_BUILD_DEBUG_INTERP=1"
          - make_options_run_mode: $LLVM_EAGER_JIT_BUILD_OPTIONS
            make_options_feature: "-DWAMR_BUILD_DEBUG_INTERP=1"
          - make_options_run_mode: $FAST_INTERP_BUILD_OPTIONS
            make_options_feature: "-DWAMR_BUILD_DEBUG_INTERP=1"
          # DEBUG_AOT only on JIT/AOT mode
          - make_options_run_mode: $CLASSIC_INTERP_BUILD_OPTIONS
            make_options_feature: "-DWAMR_BUILD_DEBUG_AOT=1"
          - make_options_run_mode: $FAST_INTERP_BUILD_OPTIONS
            make_options_feature: "-DWAMR_BUILD_DEBUG_AOT=1"
          # TODO: DEBUG_AOT on JIT
          - make_options_run_mode: $LLVM_LAZY_JIT_BUILD_OPTIONS
            make_options_feature: "-DWAMR_BUILD_DEBUG_AOT=1"
          - make_options_run_mode: $LLVM_EAGER_JIT_BUILD_OPTIONS
            make_options_feature: "-DWAMR_BUILD_DEBUG_AOT=1"
          # MINI_LOADER only on INTERP mode
          - make_options_run_mode: $AOT_BUILD_OPTIONS
            make_options_feature: "-DWAMR_BUILD_MINI_LOADER=1"
          - make_options_run_mode: $LLVM_LAZY_JIT_BUILD_OPTIONS
            make_options_feature: "-DWAMR_BUILD_MINI_LOADER=1"
          - make_options_run_mode: $LLVM_EAGER_JIT_BUILD_OPTIONS
            make_options_feature: "-DWAMR_BUILD_MINI_LOADER=1"
        include:
          - os: macos-latest
            llvm_cache_key: ${{ needs.build_llvm_libraries_on_intel_macos.outputs.cache_key }}
    steps:
      - name: checkout
        uses: actions/checkout@v4

      # only download llvm cache when needed
      - name: Get LLVM libraries
        id: retrieve_llvm_libs
        if: endsWith(matrix.make_options_run_mode, '_JIT_BUILD_OPTIONS')
        uses: actions/cache@v4
        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: ${{ matrix.llvm_cache_key }}

      - name: Quit if cache miss
        if: endsWith(matrix.make_options_run_mode, '_JIT_BUILD_OPTIONS') && (steps.retrieve_llvm_libs.outputs.cache-hit != 'true')
        run: echo "::error::can not get prebuilt llvm libraries" && exit 1

      - name: Build iwasm
        run: |
          mkdir build && cd build
          cmake .. ${{ matrix.make_options_run_mode }} ${{ matrix.make_options_feature }}
          cmake --build . --config Release --parallel 4
        working-directory: product-mini/platforms/${{ matrix.platform }}

  build_samples_wasm_c_api:
    needs: [build_iwasm]
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        make_options: [
            # Running modes supported
            $CLASSIC_INTERP_BUILD_OPTIONS,
            $FAST_INTERP_BUILD_OPTIONS,
            # Running modes unsupported
            #$LLVM_LAZY_JIT_BUILD_OPTIONS,
            #$LLVM_EAGER_JIT_BUILD_OPTIONS,
            #$AOT_BUILD_OPTIONS,
          ]
        os: [macos-latest]
        wasi_sdk_release:
          [
            "https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-20/wasi-sdk-20.0-macos.tar.gz",
          ]
        wabt_release:
          [
            "https://github.com/WebAssembly/wabt/releases/download/1.0.31/wabt-1.0.31-macos-12.tar.gz",
          ]
    steps:
      - name: checkout
        uses: actions/checkout@v4

      - name: download and install wabt
        run: |
          cd /opt
          sudo wget ${{ matrix.wabt_release }}
          sudo tar -xzf wabt-1.0.31-*.tar.gz
          sudo mv wabt-1.0.31 wabt

      - name: Build Sample [wasm-c-api]
        run: |
          cmake -S . -B build ${{ matrix.make_options }}
          cmake --build build --config Release --parallel 4
          ctest --test-dir build
        working-directory: samples/wasm-c-api

  build_samples_others:
    needs: [build_iwasm, build_wamrc, build_llvm_libraries_on_intel_macos, build_llvm_libraries_on_arm_macos]
    runs-on: ${{ matrix.os }}
    strategy:
      matrix:
        os: [macos-latest, macos-14]
        wasi_sdk_release:
          [
            "https://github.com/WebAssembly/wasi-sdk/releases/download/wasi-sdk-20/wasi-sdk-20.0-macos.tar.gz",
          ]
        wabt_release:
          [
            "https://github.com/WebAssembly/wabt/releases/download/1.0.31/wabt-1.0.31-macos-12.tar.gz",
          ]
        include:
          - os: macos-latest
            llvm_cache_key: ${{ needs.build_llvm_libraries_on_intel_macos.outputs.cache_key }}
          - os: macos-14
            llvm_cache_key: ${{ needs.build_llvm_libraries_on_arm_macos.outputs.cache_key }}
    steps:
      - name: checkout
        uses: actions/checkout@v4

      - name: download and install wasi-sdk
        run: |
          cd /opt
          sudo wget ${{ matrix.wasi_sdk_release }}
          sudo tar -xzf wasi-sdk-*.tar.gz
          sudo mv wasi-sdk-20.0 wasi-sdk

      - name: download and install wabt
        run: |
          cd /opt
          sudo wget ${{ matrix.wabt_release }}
          sudo tar -xzf wabt-1.0.31-*.tar.gz
          sudo mv wabt-1.0.31 wabt

      - name: Build Sample [basic]
        run: |
          cd samples/basic
          ./build.sh
          ./run.sh

      - name: Build Sample [file]
        run: |
          cd samples/file
          mkdir build && cd build
          cmake ..
          cmake --build . --config Debug --parallel 4
          ./src/iwasm -f wasm-app/file.wasm -d .

      - name: Build Sample [multi-thread]
        run: |
          cd samples/multi-thread
          mkdir build && cd build
          cmake ..
          cmake --build . --config Debug --parallel 4
          ./iwasm wasm-apps/test.wasm

      - name: Build Sample [multi-module]
        run: |
          cd samples/multi-module
          mkdir build && cd build
          cmake ..
          cmake --build . --config Debug --parallel 4
          ./multi_module mC.wasm

      - name: Build Sample [spawn-thread]
        run: |
          cd samples/spawn-thread
          mkdir build && cd build
          cmake ..
          cmake --build . --config Debug --parallel 4
          ./spawn_thread

      - name: Build Sample [ref-types]
        run: |
          cd samples/ref-types
          mkdir build && cd build
          cmake ..
          cmake --build . --config Debug --parallel 4
          ./hello

      - name: Get LLVM libraries
        id: retrieve_llvm_libs
        uses: actions/cache@v4
        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: ${{ matrix.llvm_cache_key }}

      - name: Build wamrc
        run: |
          mkdir build && cd build
          cmake ..
          cmake --build . --config Release --parallel 4
        working-directory: wamr-compiler
        
      - name: Build Sample [wasi-threads]
        run: |
          cd samples/wasi-threads
          mkdir build && cd build
          cmake ..
          cmake --build . --config Debug --parallel 4
          ./iwasm wasm-apps/no_pthread.wasm

          ../../../wamr-compiler/build/wamrc --size-level=0 --enable-multi-thread -o wasm-apps/no_pthread.aot wasm-apps/no_pthread.wasm
          ./iwasm wasm-apps/no_pthread.aot

      - name: Build Sample [shared-module]
        run: |
          cd samples/shared-module
          ./build.sh
          ./run.sh

      - name: Build Sample [terminate]
        run: |
          cd samples/terminate
          ./build.sh
          ./run.sh
