name: Build openMSX
description: "Builds the openMSX code in the base folder, no matter how it got there"
inputs:
  os:
    required: true
    description: "The OS to build for"
  cpu:
    required: true
    description: "The CPU to build for"
  flavour:
    required: false
    description: "The flavour to build"
    default: opt

runs:
  using: "composite"
  steps:
    - name: Install Linux prerequisites
      if: ${{ inputs.os == 'linux' }}
      shell: bash
      run: sudo apt-get update && sudo apt-get install libgl-dev libglu1-mesa-dev libasound2-dev
    - name: Install extra Linux prerequisites for native/dynamic build (e.g. for unittesting)
      if: ${{ inputs.os == 'linux' && (inputs.flavour == 'unittest' || inputs.flavour == 'super-opt')}}
      shell: bash
      run: sudo apt-get install libsdl2-dev libpng-dev tcl-dev libglew-dev libsdl2-ttf-dev libvorbis-dev libtheora-dev libogg-dev libao-dev libfreetype6-dev
    - name: Install Windows prerequisites
      if: ${{ contains(inputs.os, 'mingw') }}
      shell: bash
      run: sudo apt-get install mingw-w64 gcc make tcl
    - name: Add NDK to PATH
      if: ${{ contains(inputs.os, 'android') }}
      shell: bash
      run: echo "$ANDROID_NDK_LATEST_HOME/toolchains/llvm/prebuilt/linux-x86_64/bin" >> $GITHUB_PATH
    - name: Configure MinGW to use Posix threads
      if: ${{ contains(inputs.os, 'mingw') }}
      shell: bash
      run: |
        sudo update-alternatives --set i686-w64-mingw32-gcc /usr/bin/i686-w64-mingw32-gcc-posix
        sudo update-alternatives --set i686-w64-mingw32-g++ /usr/bin/i686-w64-mingw32-g++-posix
        sudo update-alternatives --set x86_64-w64-mingw32-gcc /usr/bin/x86_64-w64-mingw32-gcc-posix
        sudo update-alternatives --set x86_64-w64-mingw32-g++ /usr/bin/x86_64-w64-mingw32-g++-posix
    - name: Add MSBuild to PATH for Windows VC build
      if: ${{ inputs.os == 'windows-vc' }}
      uses: microsoft/setup-msbuild@v2
    - name: Cache 3rdparty libs
      id: cache-3rdparty
      uses: actions/cache@v4
      with:
        path: |
          derived/3rdparty
          derived/*/3rdparty
        key: ${{ github.ref }}-${{ inputs.os }}-${{ inputs.cpu }}-${{ inputs.flavour }}-${{ hashFiles('build/packages.py') }}
    - name: Download 3rd party packages for Windows VC build
      if: ${{ inputs.os == 'windows-vc' && steps.cache-3rdparty.outputs.cache-hit != 'true' }}
      shell: pwsh
      run: python build/thirdparty_download.py windows
    - name: Get number of CPU cores
      uses: SimenB/github-actions-cpu-cores@v2
      id: cpu-cores
    - name: Make
      if: ${{ inputs.os != 'windows-vc' }}
      shell: bash
      run: |
        CXX_PART=""
        BUILD_TARGET="staticbindist"
        if [ "${{ inputs.flavour }}" = "unittest" ] || [ "${{ inputs.flavour }}" = "super-opt" ]; then
          BUILD_TARGET=""
        fi
        make \
          -j ${{ steps.cpu-cores.outputs.count }} \
          OPENMSX_TARGET_CPU=${{ inputs.cpu }} \
          OPENMSX_TARGET_OS=${{ inputs.os }} \
          OPENMSX_FLAVOUR=${{ inputs.flavour }} \
          $CXX_PART \
          $BUILD_TARGET
    - name: msbuild
      if: ${{ inputs.os == 'windows-vc' }}
      shell: pwsh
      run: |
        msbuild /m /p:Configuration=Release /p:Platform=x64 build\3rdparty\3rdparty.sln /m
        msbuild /m /p:Configuration=Release /p:Platform=x64 build\msvc\openmsx.sln /m
    - name: Create ZIP package of VC build
      if: ${{ inputs.os == 'windows-vc' }}
      shell: pwsh
      run: cmd /c "build\\package-windows\\package.cmd" x64 release NOCATAPULT ZIPONLY
    - name: Determine version and redistributable paths and names
      shell: bash
      id: openmsx
      run: |
        OPENMSX_VERSION=`python3 build/version.py`
        DERIVED_PATH=derived/${{ inputs.cpu }}-${{ inputs.os }}-${{ inputs.flavour }}-3rd
        if [ "${{ inputs.os }}" = "darwin" ]; then
          HAS_SINGLE_FILE_OUTPUT=true
          SINGLE_OUTPUT_FILE=openmsx-${OPENMSX_VERSION}-mac-${{ inputs.cpu }}-bin.dmg
        else
          HAS_SINGLE_FILE_OUTPUT=false
        fi
        echo "version=$OPENMSX_VERSION" >> $GITHUB_OUTPUT
        echo "derived_path=$DERIVED_PATH" >> $GITHUB_OUTPUT
        echo "single_output_file=$SINGLE_OUTPUT_FILE" >> $GITHUB_OUTPUT
        echo "target_file=openmsx-$OPENMSX_VERSION-${{ inputs.os }}-${{ inputs.cpu }}-bin" >> $GITHUB_OUTPUT
        echo "target_file_pdb=openmsx-$OPENMSX_VERSION-${{ inputs.os }}-${{ inputs.cpu }}-pdb" >> $GITHUB_OUTPUT
        echo "single_file=$HAS_SINGLE_FILE_OUTPUT" >> $GITHUB_OUTPUT
    - name: Run unittests
      if: ${{ inputs.flavour == 'unittest' }}
      shell: bash
      run: derived/${{ inputs.cpu }}-${{ inputs.os }}-${{ inputs.flavour }}/bin/openmsx
    - name: Rename output folder for upload
      if: ${{ inputs.os != 'windows-vc' && steps.openmsx.outputs.single_file == 'false' && inputs.flavour != 'unittest' && inputs.flavour != 'super-opt' }}
      shell: bash
      run: |
        cd ${{ steps.openmsx.outputs.derived_path }}/bindist
        mv install ../${{ steps.openmsx.outputs.target_file }}
    - name: Upload redistributable zip
      if: ${{ inputs.os != 'windows-vc' && steps.openmsx.outputs.single_file == 'false' && inputs.flavour != 'unittest' && inputs.flavour != 'super-opt' }}
      uses: actions/upload-artifact@v4
      with:
        name: ${{ steps.openmsx.outputs.target_file }}.zip
        path: ${{ steps.openmsx.outputs.derived_path }}/${{ steps.openmsx.outputs.target_file }}
    - name: Upload single redistributable ${{ steps.openmsx.outputs.single_output_file}}
      if: ${{ inputs.os != 'windows-vc' && steps.openmsx.outputs.single_file == 'true' && inputs.flavour != 'unittest' && inputs.flavour != 'super-opt' }}
      uses: actions/upload-artifact@v4
      with:
        name: ${{ steps.openmsx.outputs.single_output_file }}
        path: ${{ steps.openmsx.outputs.derived_path }}/${{ steps.openmsx.outputs.single_output_file }}
    - name: Upload probe logging zip
      if: ${{ inputs.os != 'windows-vc' && inputs.flavour != 'unittest' && inputs.flavour != 'super-opt' }}
      uses: actions/upload-artifact@v4
      with:
        name: ${{ steps.openmsx.outputs.target_file }}-proberesults.zip
        path: ${{ steps.openmsx.outputs.derived_path }}/config
    - name: Upload Windows VC redistributable
      if: ${{ inputs.os == 'windows-vc' && inputs.flavour != 'unittest' }}
      uses: actions/upload-artifact@v4
      with:
        name: ${{ steps.openmsx.outputs.target_file }}.zip
        path: derived\x64-VC-Release\package-windows\*-bin.zip
    - name: Upload Windows VC debug symbols
      if: ${{ inputs.os == 'windows-vc' && inputs.flavour != 'unittest' }}
      uses: actions/upload-artifact@v4
      with:
        name: ${{ steps.openmsx.outputs.target_file_pdb }}.zip
        path: derived\x64-VC-Release\package-windows\*-pdb.zip
