name: "Build"
on:
  push:
  pull_request:

defaults:
  run:
    shell: bash

jobs:
  source-check:
    name: "source check"
    runs-on: ubuntu-latest

    steps:
    - name: "Checkout"
      uses: actions/checkout@v3

    - name: "Setup node.js"
      uses: actions/setup-node@v3
      with:
        node-version: "16"

    - name: "Check Enumerations"
      run: |
        cd tools
        node enumgen.js --verify

  build:
    strategy:
      fail-fast: false
      matrix:
        include:
          - { title: "linux-lib"      , os: "ubuntu-latest" , cc: "clang"   , arch: "x64", build_type: "Release", problem_matcher: "cpp" }
          - { title: "windows-lib"    , os: "windows-2022"  , cc: "vs2022"  , arch: "x86", build_type: "Debug"  , problem_matcher: "cpp" }

          - { title: "diag-asan"      , os: "ubuntu-latest" , cc: "clang"   , arch: "x64", build_type: "Release", defs: "ASMJIT_TEST=ON", diagnostics: "address" }
          - { title: "diag-ubsan"     , os: "ubuntu-latest" , cc: "clang"   , arch: "x64", build_type: "Release", defs: "ASMJIT_TEST=ON", diagnostics: "undefined" }
          - { title: "diag-valgrind"  , os: "ubuntu-latest" , cc: "clang"   , arch: "x64", build_type: "Release", defs: "ASMJIT_TEST=ON", diagnostics: "valgrind" }
          - { title: "diag-scan-build", os: "ubuntu-latest" , cc: "clang"   , arch: "x64", build_type: "Debug"  , diagnostics: "scan-build" }

          - { title: "no-deprecated"  , os: "ubuntu-latest" , cc: "clang"   , arch: "x64", build_type: "Release", defs: "ASMJIT_TEST=ON,ASMJIT_NO_DEPRECATED=1" }
          - { title: "no-intrinsics"  , os: "ubuntu-latest" , cc: "clang"   , arch: "x64", build_type: "Release", defs: "ASMJIT_TEST=ON,ASMJIT_NO_INTRINSICS=1" }
          - { title: "no-logging"     , os: "ubuntu-latest" , cc: "clang"   , arch: "x64", build_type: "Release", defs: "ASMJIT_TEST=ON,ASMJIT_NO_LOGGING=1" }
          - { title: "no-builder"     , os: "ubuntu-latest" , cc: "clang"   , arch: "x64", build_type: "Release", defs: "ASMJIT_TEST=ON,ASMJIT_NO_BUILDER=1" }
          - { title: "no-compiler"    , os: "ubuntu-latest" , cc: "clang"   , arch: "x64", build_type: "Release", defs: "ASMJIT_TEST=ON,ASMJIT_NO_COMPILER=1" }

          - { title: "linux"          , os: "ubuntu-20.04"  , cc: "gcc-7"   , arch: "x86", build_type: "Debug"  , defs: "ASMJIT_TEST=ON" }
          - { title: "linux"          , os: "ubuntu-20.04"  , cc: "gcc-7"   , arch: "x86", build_type: "Release", defs: "ASMJIT_TEST=ON" }
          - { title: "linux"          , os: "ubuntu-20.04"  , cc: "gcc-7"   , arch: "x64", build_type: "Debug"  , defs: "ASMJIT_TEST=ON" }
          - { title: "linux"          , os: "ubuntu-20.04"  , cc: "gcc-7"   , arch: "x64", build_type: "Release", defs: "ASMJIT_TEST=ON" }
          - { title: "linux"          , os: "ubuntu-20.04"  , cc: "gcc-8"   , arch: "x86", build_type: "Debug"  , defs: "ASMJIT_TEST=ON" }
          - { title: "linux"          , os: "ubuntu-20.04"  , cc: "gcc-8"   , arch: "x86", build_type: "Release", defs: "ASMJIT_TEST=ON" }
          - { title: "linux"          , os: "ubuntu-20.04"  , cc: "gcc-8"   , arch: "x64", build_type: "Debug"  , defs: "ASMJIT_TEST=ON" }
          - { title: "linux"          , os: "ubuntu-20.04"  , cc: "gcc-8"   , arch: "x64", build_type: "Release", defs: "ASMJIT_TEST=ON" }
          - { title: "linux"          , os: "ubuntu-22.04"  , cc: "gcc-9"   , arch: "x86", build_type: "Debug"  , defs: "ASMJIT_TEST=ON" }
          - { title: "linux"          , os: "ubuntu-22.04"  , cc: "gcc-9"   , arch: "x86", build_type: "Release", defs: "ASMJIT_TEST=ON" }
          - { title: "linux"          , os: "ubuntu-22.04"  , cc: "gcc-9"   , arch: "x64", build_type: "Debug"  , defs: "ASMJIT_TEST=ON" }
          - { title: "linux"          , os: "ubuntu-22.04"  , cc: "gcc-9"   , arch: "x64", build_type: "Release", defs: "ASMJIT_TEST=ON" }
          - { title: "linux"          , os: "ubuntu-22.04"  , cc: "gcc-10"  , arch: "x86", build_type: "Debug"  , defs: "ASMJIT_TEST=ON" }
          - { title: "linux"          , os: "ubuntu-22.04"  , cc: "gcc-10"  , arch: "x86", build_type: "Release", defs: "ASMJIT_TEST=ON" }
          - { title: "linux"          , os: "ubuntu-22.04"  , cc: "gcc-10"  , arch: "x64", build_type: "Debug"  , defs: "ASMJIT_TEST=ON" }
          - { title: "linux"          , os: "ubuntu-22.04"  , cc: "gcc-10"  , arch: "x64", build_type: "Release", defs: "ASMJIT_TEST=ON" }
          - { title: "linux"          , os: "ubuntu-22.04"  , cc: "gcc-11"  , arch: "x86", build_type: "Debug"  , defs: "ASMJIT_TEST=ON" }
          - { title: "linux"          , os: "ubuntu-22.04"  , cc: "gcc-11"  , arch: "x86", build_type: "Release", defs: "ASMJIT_TEST=ON" }
          - { title: "linux"          , os: "ubuntu-22.04"  , cc: "gcc-11"  , arch: "x64", build_type: "Debug"  , defs: "ASMJIT_TEST=ON" }
          - { title: "linux"          , os: "ubuntu-22.04"  , cc: "gcc-11"  , arch: "x64", build_type: "Release", defs: "ASMJIT_TEST=ON" }
          - { title: "linux"          , os: "ubuntu-22.04"  , cc: "gcc-12"  , arch: "x86", build_type: "Debug"  , defs: "ASMJIT_TEST=ON" }
          - { title: "linux"          , os: "ubuntu-22.04"  , cc: "gcc-12"  , arch: "x86", build_type: "Release", defs: "ASMJIT_TEST=ON" }
          - { title: "linux"          , os: "ubuntu-22.04"  , cc: "gcc-12"  , arch: "x64", build_type: "Debug"  , defs: "ASMJIT_TEST=ON" }
          - { title: "linux"          , os: "ubuntu-22.04"  , cc: "gcc-12"  , arch: "x64", build_type: "Release", defs: "ASMJIT_TEST=ON" }
          - { title: "linux"          , os: "ubuntu-20.04"  , cc: "clang-10", arch: "x86", build_type: "Debug"  , defs: "ASMJIT_TEST=ON" }
          - { title: "linux"          , os: "ubuntu-20.04"  , cc: "clang-10", arch: "x86", build_type: "Release", defs: "ASMJIT_TEST=ON" }
          - { title: "linux"          , os: "ubuntu-20.04"  , cc: "clang-10", arch: "x64", build_type: "Debug"  , defs: "ASMJIT_TEST=ON" }
          - { title: "linux"          , os: "ubuntu-20.04"  , cc: "clang-10", arch: "x64", build_type: "Release", defs: "ASMJIT_TEST=ON" }
          - { title: "linux"          , os: "ubuntu-22.04"  , cc: "clang-11", arch: "x86", build_type: "Debug"  , defs: "ASMJIT_TEST=ON" }
          - { title: "linux"          , os: "ubuntu-22.04"  , cc: "clang-11", arch: "x86", build_type: "Release", defs: "ASMJIT_TEST=ON" }
          - { title: "linux"          , os: "ubuntu-22.04"  , cc: "clang-11", arch: "x64", build_type: "Debug"  , defs: "ASMJIT_TEST=ON" }
          - { title: "linux"          , os: "ubuntu-22.04"  , cc: "clang-11", arch: "x64", build_type: "Release", defs: "ASMJIT_TEST=ON" }
          - { title: "linux"          , os: "ubuntu-22.04"  , cc: "clang-12", arch: "x86", build_type: "Debug"  , defs: "ASMJIT_TEST=ON" }
          - { title: "linux"          , os: "ubuntu-22.04"  , cc: "clang-12", arch: "x86", build_type: "Release", defs: "ASMJIT_TEST=ON" }
          - { title: "linux"          , os: "ubuntu-22.04"  , cc: "clang-12", arch: "x64", build_type: "Debug"  , defs: "ASMJIT_TEST=ON" }
          - { title: "linux"          , os: "ubuntu-22.04"  , cc: "clang-12", arch: "x64", build_type: "Release", defs: "ASMJIT_TEST=ON" }
          - { title: "linux"          , os: "ubuntu-22.04"  , cc: "clang-13", arch: "x86", build_type: "Debug"  , defs: "ASMJIT_TEST=ON" }
          - { title: "linux"          , os: "ubuntu-22.04"  , cc: "clang-13", arch: "x86", build_type: "Release", defs: "ASMJIT_TEST=ON" }
          - { title: "linux"          , os: "ubuntu-22.04"  , cc: "clang-13", arch: "x64", build_type: "Debug"  , defs: "ASMJIT_TEST=ON" }
          - { title: "linux"          , os: "ubuntu-22.04"  , cc: "clang-13", arch: "x64", build_type: "Release", defs: "ASMJIT_TEST=ON" }
          - { title: "linux"          , os: "ubuntu-22.04"  , cc: "clang-14", arch: "x86", build_type: "Debug"  , defs: "ASMJIT_TEST=ON" }
          - { title: "linux"          , os: "ubuntu-22.04"  , cc: "clang-14", arch: "x86", build_type: "Release", defs: "ASMJIT_TEST=ON" }
          - { title: "linux"          , os: "ubuntu-22.04"  , cc: "clang-14", arch: "x64", build_type: "Debug"  , defs: "ASMJIT_TEST=ON" }
          - { title: "linux"          , os: "ubuntu-22.04"  , cc: "clang-14", arch: "x64", build_type: "Release", defs: "ASMJIT_TEST=ON" }

          - { title: "macos"          , os: "macos-11.0"    , cc: "gcc-10"  , arch: "x64", build_type: "Debug"  , defs: "ASMJIT_TEST=ON" }
          - { title: "macos"          , os: "macos-11.0"    , cc: "gcc-10"  , arch: "x64", build_type: "Release", defs: "ASMJIT_TEST=ON" }
          - { title: "macos"          , os: "macos-11.0"    , cc: "clang"   , arch: "x64", build_type: "Debug"  , defs: "ASMJIT_TEST=ON" }
          - { title: "macos"          , os: "macos-11.0"    , cc: "clang"   , arch: "x64", build_type: "Release", defs: "ASMJIT_TEST=ON" }

          - { title: "windows"        , os: "windows-2019"  , cc: "vs2019"  , arch: "x86", build_type: "Debug"  , defs: "ASMJIT_TEST=ON" }
          - { title: "windows"        , os: "windows-2019"  , cc: "vs2019"  , arch: "x86", build_type: "Release", defs: "ASMJIT_TEST=ON" }
          - { title: "windows"        , os: "windows-2019"  , cc: "vs2019"  , arch: "x64", build_type: "Debug"  , defs: "ASMJIT_TEST=ON" }
          - { title: "windows"        , os: "windows-2019"  , cc: "vs2019"  , arch: "x64", build_type: "Release", defs: "ASMJIT_TEST=ON" }

          - { title: "windows"        , os: "windows-2022"  , cc: "vs2022"  , arch: "x86", build_type: "Debug"  , defs: "ASMJIT_TEST=ON" }
          - { title: "windows"        , os: "windows-2022"  , cc: "vs2022"  , arch: "x86", build_type: "Release", defs: "ASMJIT_TEST=ON" }
          - { title: "windows"        , os: "windows-2022"  , cc: "vs2022"  , arch: "x64", build_type: "Debug"  , defs: "ASMJIT_TEST=ON" }
          - { title: "windows"        , os: "windows-2022"  , cc: "vs2022"  , arch: "x64", build_type: "Release", defs: "ASMJIT_TEST=ON" }

    name: "${{matrix.title}} (${{matrix.cc}}, ${{matrix.arch}}, ${{matrix.build_type}})"
    runs-on: "${{matrix.os}}"

    steps:
      - name: "Checkout"
        uses: actions/checkout@v3
        with:
          path: "source"

      - name: "Checkout Build Actions"
        uses: actions/checkout@v3
        with:
          repository: build-actions/build-actions
          path: "build-actions"

      - name: "Python"
        uses: actions/setup-python@v4
        with:
          python-version: "3.x"

      - name: "Prepare"
        run: python build-actions/action.py
                    --step=prepare
                    --compiler=${{matrix.cc}}
                    --diagnostics=${{matrix.diagnostics}}
                    --architecture=${{matrix.arch}}

      - name: "Configure"
        run: python build-actions/action.py
                    --step=configure
                    --config=source/.github/workflows/build-config.json
                    --source-dir=source
                    --compiler=${{matrix.cc}}
                    --diagnostics=${{matrix.diagnostics}}
                    --architecture=${{matrix.arch}}
                    --build-type=${{matrix.build_type}}
                    --build-defs=${{matrix.defs}}
                    --problem-matcher=${{matrix.problem_matcher}}

      - name: "Build"
        run: python build-actions/action.py --step=build

      - name: "Test"
        run: python build-actions/action.py --step=test

  build-vm:
    strategy:
      fail-fast: false
      matrix:
        include:
          - { host: "macos-12"     , os: "freebsd", osver: "13.1", cc: "clang", arch: "x86-64", build_type: "Release", defs: "ASMJIT_TEST=ON" }
          - { host: "macos-12"     , os: "netbsd" , osver: "9.2" , cc: "clang", arch: "x86-64", build_type: "Release", defs: "ASMJIT_TEST=ON" }
          - { host: "macos-12"     , os: "openbsd", osver: "7.2" , cc: "clang", arch: "x86-64", build_type: "Release", defs: "ASMJIT_TEST=ON" }
          - { host: "ubuntu-latest", os: "openbsd", osver: "7.2" , cc: "clang", arch: "arm64" , build_type: "Release", defs: "ASMJIT_TEST=ON" }

    name: "${{matrix.os}}-${{matrix.osver}} (${{matrix.cc}}, ${{matrix.arch}}, ${{matrix.build_type}})"
    runs-on: ${{matrix.host}}

    steps:
      - name: "Checkout"
        uses: actions/checkout@v3
        with:
          path: "source"

      - name: "Checkout Build Actions"
        uses: actions/checkout@v3
        with:
          repository: build-actions/build-actions
          path: "build-actions"

      - name: Build & Test in VM
        uses: cross-platform-actions/action@master
        with:
          operating_system: ${{matrix.os}}
          architecture: ${{matrix.arch}}
          version: ${{matrix.osver}}
          shell: bash
          run: |
            set -e

            PATH="/usr/sbin:/usr/pkg/sbin:/usr/pkg/bin:$PATH:$(pwd)/build-actions"
            CI_NETBSD_USE_PKGIN=1

            export PATH
            export CI_NETBSD_USE_PKGIN

            sh ./build-actions/prepare-environment.sh
            python3 build-actions/action.py                       \
              --step=all                                          \
              --compiler=${{matrix.cc}}                           \
              --architecture=${{matrix.arch}}                     \
              --source-dir=source                                 \
              --config=source/.github/workflows/build-config.json \
              --build-type=${{matrix.build_type}}                 \
              --build-defs=${{matrix.defs}}