name: Build with MSVC

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

on:
  push:
    branches:
      - '4.**'
      - '5.**'
      - 'trunk'
  pull_request:

# env:
  # Fully print commands executed by Make
  # MAKEFLAGS: V=1

defaults:
  run:
    shell: bash -eo pipefail -o igncr {0}

jobs:
  build:
    permissions: {}

    runs-on: windows-latest

    timeout-minutes: 60

    name: ${{ matrix.cc == 'cl' && 'MSVC' || 'clang-cl' }} ${{ matrix.x86_64 && '64 bits' || '32 bits' }}

    strategy:
      matrix:
        x86_64: [true, false]
        cc: [cl, clang-cl]
        exclude:
          - cc: clang-cl
            x86_64: false

    steps:

      - name: Fetch OCaml
        uses: actions/checkout@v4
        with:
          submodules: true

      - name: Restore Cygwin cache
        uses: actions/cache/restore@v4
        with:
          path: |
            C:\cygwin-packages
          key: cygwin-packages

      - name: Install Cygwin
        uses: cygwin/cygwin-install-action@v3
        with:
          packages: make,mingw64-x86_64-gcc-core
          install-dir: 'D:\cygwin'

      - name: Save Cygwin cache
        uses: actions/cache/save@v4
        with:
          path: |
            C:\cygwin-packages
          key: cygwin-packages

      - name: Set up MSVC
        uses: ilammy/msvc-dev-cmd@v1
        with:
          arch: ${{ matrix.x86_64 && 'x64' || 'x86' }}

      - name: Compute a key to cache configure results
        id: autoconf-cache-key
        env:
          HOST: ${{ matrix.x86_64 && 'x86_64-pc-windows' || 'i686-pc-windows' }}
        run: |
          echo "key=${{ env.HOST }}-${{ matrix.cc }}-${{ hashFiles('configure') }}" >> $GITHUB_OUTPUT

      - name: Restore Autoconf cache
        uses: actions/cache/restore@v4
        with:
          path: |
            config.cache
          key: ${{ steps.autoconf-cache-key.outputs.key }}

      - name: Configure tree
        env:
          HOST: ${{ matrix.x86_64 && 'x86_64-pc-windows' || 'i686-pc-windows' }}
        run: |
          eval $(tools/msvs-promote-path)
          if ! ./configure --cache-file=config.cache --host=$HOST \
               --prefix="$PROGRAMFILES/Бактріан🐫" \
               CC=${{ matrix.cc }}; then
            rm -rf config.cache
            failed=0
            ./configure --cache-file=config.cache --host=$HOST \
                --prefix="$PROGRAMFILES/Бактріан🐫" \
                CC=${{ matrix.cc }} \
              || failed=$?
            if ((failed)) ; then
              echo
              echo "::group::config.log content ($(wc -l config.log) lines)"
              cat config.log
              echo '::endgroup::'
              exit $failed
            fi
          fi

      - name: Save Autoconf cache
        uses: actions/cache/save@v4
        with:
          path: |
            config.cache
          key: ${{ steps.autoconf-cache-key.outputs.key }}

      - name: Build OCaml
        run: |
          eval $(tools/msvs-promote-path)
          make -j || failed=$?
          if ((failed)) ; then make -j1 V=1 ; exit $failed ; fi
          runtime/ocamlrun ocamlc -config

      - name: Assemble backend with mingw-w64 GASM and compare
        run: |
          x86_64-w64-mingw32-gcc -c -I./runtime  -I ./flexdll -D__USE_MINGW_ANSI_STDIO=0 -DUNICODE -D_UNICODE -DWINDOWS_UNICODE=1 -DCAMLDLLIMPORT= -DIN_CAML_RUNTIME -DNATIVE_CODE -DTARGET_amd64 -DMODEL_default -DSYS_mingw64 -o runtime/amd64.o runtime/amd64.S
          dumpbin /disasm:nobytes runtime/amd64nt.obj > runtime/amd64nt.dump
          awk -f tools/ci/actions/canonicalize-dumpbin.awk runtime/amd64nt.dump runtime/amd64nt.dump > runtime/amd64nt.canonical
          dumpbin /disasm:nobytes runtime/amd64.o > runtime/amd64.dump
          awk -f tools/ci/actions/canonicalize-dumpbin.awk runtime/amd64.dump runtime/amd64.dump > runtime/amd64.canonical
          git diff --no-index -- runtime/amd64*.canonical
          wc -l runtime/amd64*.dump runtime/amd64*.canonical
        # ^ The final wc is there to make sure that the canonical files are
        # reasonable cleaned-up versions of the raw dumpbins and not simply
        # empty
        if: matrix.x86_64

      - name: Run the test suite
        run: |
          eval $(tools/msvs-promote-path)
          make tests

      - name: Install the compiler
        run: make install
