name: CI

on:
  push:
    branches:
      - master
  pull_request:
    branches:
      - master

env:
  CARGO_TERM_COLOR: always

jobs:
  build-macos:
    runs-on: macos-12
    timeout-minutes: 20

    steps:
    - uses: actions/checkout@v3

    - name: Install latest Rust (macOS)
      uses: dtolnay/rust-toolchain@stable
      with:
          targets: aarch64-apple-darwin

    - name: Setup Rust dependency caching
      uses: Swatinem/rust-cache@v2

    - name: Build plugin
      run: cargo build -p octasine --verbose --features "vst2 clap"

    - name: Build plugin for aarch64
      run: |
        export SDKROOT=$(xcrun -sdk macosx12.3 --show-sdk-path)
        echo "SDKROOT=$SDKROOT"
        export MACOSX_DEPLOYMENT_TARGET=$(xcrun -sdk macosx12.3 --show-sdk-platform-version)
        echo "MACOSX_DEPLOYMENT_TARGET=$MACOSX_DEPLOYMENT_TARGET"
        cargo build -p octasine --target=aarch64-apple-darwin
      env:
        DEVELOPER_DIR: /Applications/Xcode_13.4.1.app/Contents/Developer
      shell: bash
      if: contains(matrix.os, 'macos')

  build-windows:
    runs-on: windows-latest
    timeout-minutes: 20

    steps:
    - uses: actions/checkout@v3

    - name: Install latest rust (windows/linux)
      uses: dtolnay/rust-toolchain@stable

    - name: Setup Rust dependency caching
      uses: Swatinem/rust-cache@v2

    - name: Build plugin
      run: cargo build -p octasine --verbose --features "vst2 clap"

  build-linux:
    runs-on: ubuntu-20.04
    timeout-minutes: 20

    steps:
    - uses: actions/checkout@v3

    - name: Install baseview dependencies (Linux)
      run: sudo apt update && sudo apt install libgl-dev libx11-xcb-dev libxcb1-dev libxcb-dri2-0-dev libxcb-icccm4-dev libxcursor-dev libxkbcommon-dev libxcb-shape0-dev libxcb-xfixes0-dev

    - name: Install latest Rust
      uses: dtolnay/rust-toolchain@stable
      with:
        targets: aarch64-unknown-linux-gnu

    - name: Setup Rust dependency caching
      uses: Swatinem/rust-cache@v2

    - name: Build plugin
      run: cargo build -p octasine --verbose --features "vst2 clap"
      # Disabled: needs more complex setup
      # run: |
      #   cargo build -p octasine --verbose
      #   cargo build -p octasine --verbose --target=aarch64-unknown-linux-gnu

  test-linux:
    runs-on: ubuntu-latest
    timeout-minutes: 20

    steps:
    - uses: actions/checkout@v3

    - name: Install baseview dependencies
      run: sudo apt update && sudo apt install libgl-dev libx11-xcb-dev libxcb1-dev libxcb-dri2-0-dev libxcb-icccm4-dev libxcursor-dev libxkbcommon-dev libxcb-shape0-dev libxcb-xfixes0-dev

    - name: Install latest Rust
      uses: dtolnay/rust-toolchain@stable

    - name: Install cargo-nextest
      uses: taiki-e/install-action@nextest

    - name: Setup Rust dependency caching
      uses: Swatinem/rust-cache@v2

    - name: Run tests
      run: cargo nextest run --workspace --verbose --features "vst2 clap"
      env:
        # Set target-cpu=skylake to enable avx-2 but not avx-512, since the
        # latter for some reason leads to SIGILL failures
        RUSTFLAGS: "-C target-cpu=skylake" 

  cargo-deny:
    runs-on: ubuntu-latest
    timeout-minutes: 20
    steps:
    - uses: actions/checkout@v3
    - uses: EmbarkStudios/cargo-deny-action@v1
      with:
        log-level: warn
        command: check licenses
        arguments: --all-features --workspace

  clap-validator:
    runs-on: ubuntu-latest
    timeout-minutes: 20

    steps:
    - uses: actions/checkout@v3

    - name: Install baseview dependencies
      run: sudo apt update && sudo apt install libgl-dev libx11-xcb-dev libxcb1-dev libxcb-dri2-0-dev libxcb-icccm4-dev libxcursor-dev libxkbcommon-dev libxcb-shape0-dev libxcb-xfixes0-dev

    - name: Install latest Rust
      uses: dtolnay/rust-toolchain@stable

    - name: Setup Rust dependency caching
      uses: Swatinem/rust-cache@v2

    - name: Install clap-validator
      run: cargo install --git https://github.com/free-audio/clap-validator.git

    - name: Build and bundle clap plugin
      run: cargo xtask bundle octasine --release --verbose --features "clap"
    
    - name: Validate clap plugin
      # Note: skip param-conversions test for now
      run: clap-validator validate --invert-filter --test-filter 'param-conversions' ./target/bundled/octasine.clap
