# Workflow for building Psychtoolbox mex files for Matlab/Octave on Ubuntu Linux 20.04-LTS.
# Currently builds all mex files except Datapixx, due to lack of SDK availability in VM.
#
# Note: - Free GitHub runners are only available for public / OSS repositories.
#       - The free Matlab runner is only available for Linux.
#
# Copyright (c) 2022-2023 by Mario Kleiner, licensed under MIT license.
#
name: CI

# Controls when the workflow will run
on:
  # Triggers the workflow on push or pull request events but only for the master branch
  #push:
  #  branches: [ master ]
  #pull_request:
  #  branches: [ master ]

  # Allows you to run this workflow manually from the Actions tab
  workflow_dispatch:

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # Run a job to build the majority of the mex files targetting Ubuntu 20.04-LTS and later, simply called "build"
  build:
    name: Run commands to build+upload Matlab+Octave mex files for 64-Bit Ubuntu Linux 20.04-LTS on Intel.

    # The type of runner that the job will run on: Fixed to Ubuntu 20.04-LTS
    runs-on: ubuntu-20.04

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - name: Check out Psychtoolbox repository to runner.
        uses: actions/checkout@v3

      - name: Add SR-Research Eyelink repositories.
        run:  sudo apt-key adv --fetch-keys https://apt.sr-research.com/SRResearch_key ; sudo add-apt-repository 'deb [arch=amd64] https://apt.sr-research.com SRResearch main'

      - name: Setup additional build and runtime dependencies of Psychtoolbox.
        run:  sudo apt update ; sudo apt install -y octave-psychtoolbox-3 liboctave-dev libfontconfig1-dev libopenhmd-dev libusb-1.0-0-dev xorg-dev libx11-xcb-dev libxcb*-dev libglu1-mesa-dev freeglut3-dev libgstreamer*-dev libasound2-dev portaudio19-dev libfreenect-dev libopenal-dev libapriltag-dev ; sudo apt remove -y libgamemode0; sudo apt install -y libdc1394-dev

      - name: Setup OculusVR SDK 0.5 for 64-Bit Linux.
        run: tar xvf PsychSourceGL/Cohorts/OculusSDK/ovr_sdk_linux_0.5.0.1.tar.xz; cd ovr_sdk_linux_0.5.0.1 ; sudo cp --recursive LibOVR/Include/* /usr/local/include/ ; sudo cp ../PsychSourceGL/Cohorts/OculusSDK/libOVR.a /usr/local/lib/

      - name: Setup OpenXR SDK and OpenXR loader for PsychOpenXRCore build, backported for 64-Bit Ubuntu Linux 20.04.
        run: sudo add-apt-repository ppa:monado-xr/monado; sudo apt-get update; sudo apt install -y libopenxr-dev;

      - name: Setup latest Vulkan SDK from LunarG for PsychVulkanCore build, as Ubuntu 20.04 SDK is too old for all our needs.
        run: wget -qO - https://packages.lunarg.com/lunarg-signing-key-pub.asc | sudo apt-key add - ; sudo wget -qO /etc/apt/sources.list.d/lunarg-vulkan-1.3.243-focal.list https://packages.lunarg.com/vulkan/1.3.243/lunarg-vulkan-1.3.243-focal.list ; sudo apt update ; sudo apt install -y vulkan-sdk ;

      - name: Install minimal SR-Research Eyelink SDK for building Eyelink mex files.
        run:  sudo apt install eyelinkcore

      - name: Set up latest MATLAB
        uses: matlab-actions/setup-matlab@v1

      - name: Build all eligible Psychtoolbox Matlab mex files.
        uses: matlab-actions/run-command@v1
        with:
          command: cd('Psychtoolbox'); pwd, addpath(genpath(pwd)); cd('../PsychSourceGL/Source'); disp(version); pwd, linuxmakeit64(-1, 1);

      - name: Build all eligible Psychtoolbox Octave mex files.
        run: octave --eval "warning('off', 'Octave:shadowed-function'); cd('Psychtoolbox'); pwd, addpath(genpath(pwd)); cd('../PsychSourceGL/Source'); disp(version); pwd, linuxmakeitoctave3(-1, 1); linuxmakeitoctave3(12, 0); linuxmakeitoctave3(4, 0); cd('../Cohorts/FTGLTextRenderer'); makeftglplugin;"

      - name: Archive built 64-Bit Matlab and Octave mex files for 64-Bit Intel
        uses: actions/upload-artifact@v3
        with:
          name: Linux64BitIntelMexFiles
          path: |
                Psychtoolbox/PsychBasic/Octave5LinuxFiles64/*.mex
                Psychtoolbox/PsychBasic/*.mexa64
                Psychtoolbox/PsychBasic/PsychPlugins/libptbdrawtext_ftgl64.so.1

      - name: Use xvfb for basic testing of Psychtoolbox under Octave. Rendering tests under llvmpipe, some basic queries.
        run: xvfb-run octave --eval "warning('off', 'Octave:shadowed-function'); cd('Psychtoolbox'); pwd, addpath(genpath(pwd)); addpath(genpath([pwd '/PsychBasic/Octave5LinuxFiles64'])); PsychDebugWindowConfiguration; Screen('Preference', 'ConserveVRAM', 4194304); [a,b,c] = GetKeyboardIndices, [d,e,f] = GetMouseIndices"


  # Run a job called "buildforubuntu22.04", only for some few mex files which are targetted at only easily running on Ubuntu 22.04 and later:
  buildforubuntu22-04:
    name: Run commands to build+upload some Matlab+Octave mex files for 64-Bit Ubuntu Linux 22.04-LTS on Intel.

    # The type of runner that the job will run on: Fixed to Ubuntu 22.04-LTS
    runs-on: ubuntu-22.04

    # Steps represent a sequence of tasks that will be executed as part of the job
    steps:
      # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
      - name: Check out Psychtoolbox repository to runner.
        uses: actions/checkout@v3

#      - name: Add SR-Research Eyelink repositories.
#        run:  sudo apt-key adv --fetch-keys https://apt.sr-research.com/SRResearch_key ; sudo add-apt-repository 'deb [arch=amd64] https://apt.sr-research.com SRResearch main'

      - name: Setup additional build and runtime dependencies of Psychtoolbox. Currently only libapriltag
        run:  sudo apt update ; sudo apt install -y octave-psychtoolbox-3 liboctave-dev freeglut3-dev libapriltag-dev

#      - name: Setup OculusVR SDK 0.5 for 64-Bit Linux.
#        run: tar xvf PsychSourceGL/Cohorts/OculusSDK/ovr_sdk_linux_0.5.0.1.tar.xz; cd ovr_sdk_linux_0.5.0.1 ; sudo cp --recursive LibOVR/Include/* /usr/local/include/ ; sudo cp ../PsychSourceGL/Cohorts/OculusSDK/libOVR.a /usr/local/lib/

#      - name: Setup OpenXR SDK and OpenXR loader for PsychOpenXRCore build, backported for 64-Bit Ubuntu Linux 20.04.
#        run: sudo add-apt-repository ppa:monado-xr/monado; sudo apt-get update; sudo apt install -y libopenxr-dev;

#      - name: Setup latest Vulkan SDK from LunarG for PsychVulkanCore build, as Ubuntu 20.04 SDK is too old for all our needs.
#        run: wget -qO - https://packages.lunarg.com/lunarg-signing-key-pub.asc | sudo apt-key add - ; sudo wget -qO /etc/apt/sources.list.d/lunarg-vulkan-1.3.243-focal.list https://packages.lunarg.com/vulkan/1.3.243/lunarg-vulkan-1.3.243-focal.list ; sudo apt update ; sudo apt install -y vulkan-sdk ;

#      - name: Install minimal SR-Research Eyelink SDK for building Eyelink mex files.
#        run:  sudo apt install eyelinkcore

      - name: Set up latest MATLAB
        uses: matlab-actions/setup-matlab@v1

      - name: Build all eligible Psychtoolbox Matlab mex files. Currently only PsychCV
        uses: matlab-actions/run-command@v1
        with:
          command: cd('Psychtoolbox'); pwd, addpath(genpath(pwd)); cd('../PsychSourceGL/Source'); pwd, linuxmakeit64(10, 1);

      - name: Build all eligible Psychtoolbox Octave mex files. Currently only PsychCV
        run: octave --eval "warning('off', 'Octave:shadowed-function'); cd('Psychtoolbox'); pwd, addpath(genpath(pwd)); cd('../PsychSourceGL/Source'); linuxmakeitoctave3(10, 1);"

      - name: Archive built 64-Bit Matlab and Octave mex files for 64-Bit Intel on Ubuntu 22.04
        uses: actions/upload-artifact@v3
        with:
          name: Linux64BitIntelMexFilesForUbuntu22.04
          path: |
                Psychtoolbox/PsychBasic/Octave5LinuxFiles64/*.mex
                Psychtoolbox/PsychBasic/*.mexa64
#                Psychtoolbox/PsychBasic/PsychPlugins/libptbdrawtext_ftgl64.so.1

#      - name: Use xvfb for basic testing of Psychtoolbox under Octave. Rendering tests under llvmpipe, some basic queries.
#        run: xvfb-run octave --eval "warning('off', 'Octave:shadowed-function'); cd('Psychtoolbox'); pwd, addpath(genpath(pwd)); addpath(genpath([pwd '/PsychBasic/Octave5LinuxFiles64'])); PsychDebugWindowConfiguration; Screen('Preference', 'ConserveVRAM', 4194304); [a,b,c] = GetKeyboardIndices, [d,e,f] = GetMouseIndices"
