name: Build emu variant (Windows)

on:
  workflow_call:
    inputs:
      emu-variant:
        description: |
          Which variant of the emu to build:
            regular: build the regular version of the emu
            exp: build the experimental version of the emu
            client: build the experimental steamclient version of the emu
            tools: build the tools only
        required: true
        type: string

      x32:
        description: |
          build architecture, unused when 'emu-variant' == 'tools'
            true: x32
            false: x64
        required: false
        type: boolean
        default: true
    
      debug:
        description: |
          build mode
            true: build in debug mode
            false: build in release mode
        required: true
        type: boolean

permissions:
  contents: write

env:
  DEPS_CACHE_KEY: emu-deps-win
  DEPS_CACHE_DIR: build/deps/win
  
  THIRD_PARTY_BASE_DIR: 'third-party'

jobs:
  build:
    runs-on: windows-2022
    if: ${{ !cancelled() }}

    steps:
### setup build vars
    - name: Setup build vars
      shell: cmd
      run: |
        echo env file = "%GITHUB_ENV%"
        echo workspace = "${{ github.workspace }}"
        
        set "arch="
        if /i "${{ inputs.x32 }}"=="true" (
          set "arch=32"
        ) else (
          set "arch=64"
        )
        >>"%GITHUB_ENV%" echo build_arch=%arch%

        set "build_switches="
        if "${{ inputs.emu-variant }}"=="regular" (
          set "build_switches=+lib-%arch%"
        ) else if "${{ inputs.emu-variant }}"=="exp" (
          set "build_switches=+ex-lib-%arch% +ex-client-%arch%"
        ) else if "${{ inputs.emu-variant }}"=="client" (
          set "build_switches=+exclient-%arch% +exclient-ldr-%arch% +exclient-extra-%arch% +lib-gameoverlay-%arch%"
        ) else if "${{ inputs.emu-variant }}"=="tools" (
          set "build_switches=+tool-itf +tool-lobby"
        ) else (
          1>&2 echo [X] invalid emu variant "${{ inputs.emu-variant }}"
          exit /b 1
        )
        >>"%GITHUB_ENV%" echo build_switches=%build_switches%

        set "build_mode="
        if /i "${{ inputs.debug }}"=="true" (
          set "build_mode=debug"
        ) else (
          set "build_mode=release"
        )
        >>"%GITHUB_ENV%" echo build_mode=%build_mode%

### on Windows Git will auto change line ending to CRLF, not preferable
    - name: Ensure LF line ending
      shell: cmd
      working-directory: ${{ github.workspace }}
      run: |
        git config --local core.autocrlf false
        git config --system core.autocrlf false
        git config --global core.autocrlf false
      
    - name: Checkout branch
      uses: actions/checkout@v4

### deps
    - name: Restore deps
      id: emu-deps-cache-step
      uses: actions/cache@v4
      with:
        key: ${{ env.DEPS_CACHE_KEY }}
        path: ${{ env.DEPS_CACHE_DIR }}

## extra helpers/tools, these are not built inside the deps build dir
    - name: Clone third-party build helpers (common/win)
      uses: actions/checkout@v4
      with:
        ref: 'third-party/common/win'
        path: "${{env.THIRD_PARTY_BASE_DIR}}/common/win"

    - name: Clone third-party deps (build/win)
      uses: actions/checkout@v4
      with:
        ref: 'third-party/build/win'
        path: "${{env.THIRD_PARTY_BASE_DIR}}/build/win"

### build target(s)
    - name: Build target(s)
      if: inputs.emu-variant != 'all'
      shell: cmd
      working-directory: ${{ github.workspace }}
      run: build_win.bat -j 3 -verbose ${{ env.build_mode }} clean +build_str ${{ github.sha }} ${{ env.build_switches }}

### upload artifact/package to github Actions (for targets)
    - name: Upload build package (for targets)
      uses: actions/upload-artifact@v4
      with:
        name: "emu-win-${{ inputs.emu-variant }}-${{ env.build_arch }}-${{ env.build_mode }}-${{ github.sha }}"
        path: "build/win/"
        if-no-files-found: 'error'
        compression-level: 9
        retention-days: 1
