name: csound_builds

env:
  VCPKG_BINARY_SOURCES: "clear;nuget,GitHub,readwrite"
  CSOUND_VERSION: 6.17.0

on:
  push:
    branches:
      - develop
      - master
      - cs7
      - PluginBase-heap
  pull_request:
    branches:
      - develop
      - cs7

jobs:
  linux_build:
    name: Linux/Ubuntu build
    runs-on: ubuntu-latest
    steps:
      - name: Checkout Source Code
        uses: actions/checkout@v1
        with:
          fetch-depth: 1
      - name: Install Dependencies
        run: sudo apt-get update && sudo apt-get install cmake libsndfile1-dev libasound2-dev libjack-dev portaudio19-dev libportmidi-dev libpulse-dev swig liblua5.1-0-dev default-jdk libfltk1.1-dev libfluidsynth-dev liblo-dev fluid ladspa-sdk libpng-dev dssi-dev libstk0-dev libgmm++-dev bison flex libportsmf-dev libeigen3-dev libcunit1-dev
      - name: Configure build
        run: mkdir build && cd build && cmake ..
      - name: Build Csound
        run: cd build && make
      - name: Run tests
        run: cd build && make test csdtests

  macos_build:
    name: MacOS build
    runs-on: macos-latest
    steps:
      - name: Checkout Source Code
        uses: actions/checkout@v1
        with:
          fetch-depth: 1
      - name: Install Dependencies
        run: brew install cmake swig eigen asio libsndfile portaudio portmidi libsamplerate liblo jack openjdk hdf5 gettext fluid-synth fltk stk wiiuse
      - name: Configure build
        run: mkdir build && cd build && cmake ..
      - name: Build Csound
        run: cd build && make
      - name: Run tests
        run: cd build && make test csdtests

  emscripten_build:
    name: EMscripten build
    runs-on: ubuntu-latest
    steps:
      - name: run the Docker image
        run: docker run --name csound_test_run -i sjamble/csound_emscripten
      - name: copying build out of container
        run: docker cp csound_test_run:/csound/Emscripten/csound-web-6.17.0.zip .
      - uses: actions/upload-artifact@v2
        with:
          name: csound-web-6.17.0.zip
          path: .

  android_build:
    name: Android build
    runs-on: ubuntu-latest
    steps:
      - name: run the Docker image
        run: docker run --name csound_test_run -i sjamble/csound_android
      - name: copying build out of container
        run: docker cp csound_test_run:/csound/Android/csound-android-6.17.0.zip .
      - uses: actions/upload-artifact@v2
        with:
          name: csound-android-6.17.0.zip
          path: .

  windows_build:
    name: Windows build
    runs-on: windows-latest
    steps:
      - name: Checkout Source Code
        uses: actions/checkout@v1
        with:
          fetch-depth: 1
          submodules: true
      - name: Install system dependencies
        run: |
          choco install -y winflexbison
      - name: Bootstrap VCPKG
        run: .\vcpkg\bootstrap-vcpkg.bat
      - name: Setup NuGet Credentials
        shell: bash
        run: >
          `./vcpkg/vcpkg fetch nuget | tail -n 1`
          sources add
          -source "https://nuget.pkg.github.com/csound/index.json"
          -storepasswordincleartext
          -name "GitHub"
          -username "csound"
          -password "${{ secrets.GITHUB_TOKEN }}"
      - name: Configure build
        run: cmake -B build -S . -DUSE_VCPKG=1 -DCUSTOM_CMAKE="./platform/windows/Custom-vs.cmake"
      - name: Build Csound
        run: cmake --build build --config Release
      - name: Run tests
        run: cmake --build build --target csdtests
      - name: Acquire Csound manual
        shell: powershell
        run: |
          Invoke-WebRequest -Uri "https://github.com/csound/manual/releases/download/6.16.0/Csound6.16.0_manual_html.zip" -OutFile "./manual.zip"
          7z x manual.zip
      - name: Build installer
        shell: powershell
        run: |
          $Env:RedistVersion=Get-Content "C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Auxiliary\Build\Microsoft.VCRedistVersion.default.txt"
          $Env:VCREDIST_CRT_DIR="C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Redist\MSVC\${Env:RedistVersion}\x64\Microsoft.VC142.CRT"
          $Env:VCREDIST_CXXAMP_DIR="C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Redist\MSVC\${Env:RedistVersion}\x64\Microsoft.VC142.CXXAMP"
          $Env:VCREDIST_OPENMP_DIR="C:\Program Files (x86)\Microsoft Visual Studio\2019\Enterprise\VC\Redist\MSVC\${Env:RedistVersion}\x64\Microsoft.VC142.OpenMP"
          iscc /o. installer\\windows\\csound6_x64_github.iss
      - name: Upload installer
        uses: actions/upload-artifact@v2
        with:
          name: csound-${{env.CSOUND_VERSION}}.${{github.run_number}}-windows-x64-installer
          path: ./csound-windows_x86_64-*.exe
          if-no-files-found: error
      - name: Upload zip
        uses: actions/upload-artifact@v2
        with:
          name: csound-${{env.CSOUND_VERSION}}-${{github.run_number}}-windows-x64-binaries
          path: |
            ./build/Release/*.exe
            ./build/Release/*.dll
            ./build/Release/*.pyd
            ./build/Release/csound64.lib
            ./build/*.jar
            ./build/*.py
            ./include/
          if-no-files-found: error
