version: 2

shared:
  restore_cache: &restore_cache
    restore_cache:
      keys:
        - ccache-{{ arch }}-{{ .Environment.CIRCLE_JOB }}-{{ .Branch }}
        - ccache-{{ arch }}-{{ .Environment.CIRCLE_JOB }}
        - ccache-{{ arch }}
  save_cache: &save_cache
    save_cache:
      key: ccache-{{ arch }}-{{ .Environment.CIRCLE_JOB }}-{{ .Branch }}-{{ .BuildNum }}
      paths:
        - ~/.ccache
  restore_homebrew_cache: &restore_homebrew_cache
    restore_cache:
      keys:
        - homebrew-{{ arch }}-{{ .Environment.CIRCLE_JOB }}-{{ .Branch }}
        - homebrew-{{ arch }}-{{ .Environment.CIRCLE_JOB }}
        - homebrew-{{ arch }}
  save_homebrew_cache: &save_homebrew_cache
    save_cache:
      key: homebrew-{{ arch }}-{{ .Environment.CIRCLE_JOB }}-{{ .Branch }}-{{ .BuildNum }}
      paths:
        - ~/Library/Caches/Homebrew
        - /usr/local/Homebrew

  ccache_stats: &ccache_stats
    run:
      name: Print ccache statistics
      command: |
        echo "[ccache config]"
        ccache -p
        echo "[ccache stats]"
        ccache -s

  # Commmon initializing commands
  init: &init
    run:
      name: Initialize
      command: |
        mkdir -p /tmp/artifacts
        # Workaround for failing submodule fetching
        git config --global --unset url."ssh://git@github.com".insteadOf || true
        if [[ -n "${CIRCLE_PR_NUMBER}" ]]
        then
            echo "Fetching out merged pull request"
            git fetch -u origin refs/pull/${CIRCLE_PR_NUMBER}/merge:pr/merge
            git checkout pr/merge
        else
            echo "Not a pull request"
        fi

  # Commmon environment variables
  common_environment: &common_environment
    QT5: True
    CMAKE_OPTS: -DUSE_WERROR=ON -DCMAKE_BUILD_TYPE=RelWithDebInfo -DUSE_CCACHE=ON
    CCACHE_MAXSIZE: 500M
    CCACHE_LOGFILE: /tmp/artifacts/ccache.log
    MAKEFLAGS: -j6

jobs:
  mingw32:
    environment:
      <<: *common_environment
    docker:
      - image: lmmsci/linux.mingw32:18.04
    steps:
      - checkout
      - *init
      - *restore_cache
      - run:
          name: Building
          command: |
            mkdir build && cd build
            ../cmake/build_win32.sh
            make lmms
            make
      - run:
          name: Build tests
          command: cd build && make tests
      - run:
          name: Build installer
          command: |
            cd build
            make package
            cp ./lmms-*.exe /tmp/artifacts/
      - store_artifacts:
          path: /tmp/artifacts/
          destination: /
      - *ccache_stats
      - *save_cache
  mingw64:
    environment:
      <<: *common_environment
    docker:
      - image: lmmsci/linux.mingw64:18.04
    steps:
      - checkout
      - *init
      - *restore_cache
      - run:
          name: Building
          command: |
            mkdir build && cd build
            ../cmake/build_win64.sh
            make
      - run:
          name: Build tests
          command: cd build && make tests
      - run:
          name: Build installer
          command: |
            cd build
            make package
            cp ./lmms-*.exe /tmp/artifacts/
      - store_artifacts:
          path: /tmp/artifacts/
          destination: /
      - *ccache_stats
      - *save_cache
  linux.gcc:
    docker:
      - image: lmmsci/linux.gcc:16.04
    environment:
      <<: *common_environment
    steps:
      - checkout
      - *init
      - *restore_cache
      - run:
          name: Configure
          command: |
            source /opt/qt5*/bin/qt5*-env.sh || true
            mkdir build && cd build
            cmake .. $CMAKE_OPTS -DCMAKE_INSTALL_PREFIX=./install
      - run:
          name: Build
          command: cd build && make
      - run:
          name: Build tests
          command: cd build && make tests
      - run:
          name: Run tests
          command: build/tests/tests
      - *ccache_stats
      - run:
          name: Build AppImage
          command: |
            cd build
            make install
            make appimage || (cat appimage.log && false)
            cp ./lmms-*.AppImage /tmp/artifacts/
      - store_artifacts:
          path: /tmp/artifacts/
          destination: /
      - store_artifacts:
          path: build/appimage.log
          destination: /
      - *save_cache
  shellcheck:
    docker:
      - image: koalaman/shellcheck-alpine:v0.4.6
    steps:
      - checkout
      - run:
          name: Shellcheck
          command: shellcheck $(find "./cmake/" -type f -name '*.sh' -o -name "*.sh.in")
  macos:
    environment:
      <<: *common_environment
    macos:
      xcode: "9.3.1"
    steps:
      - checkout
      - *init
      - *restore_homebrew_cache
      - *restore_cache
      - run:
          name: Install Homebrew dependencies
          command: |
            # unlink Homebrew's python 2 to prevent an node-gyp error
            brew unlink python@2 || true
            brew update && brew install ccache fftw cmake pkg-config libogg libvorbis lame libsndfile libsamplerate jack sdl libgig libsoundio stk fluid-synth portaudio fltk qt5 carla
      - run:
          name: Install nodejs dependencies
          command: npm install -g appdmg
      - run:
          name: Building
          command: |
            mkdir build && cd build
            cmake .. -DCMAKE_INSTALL_PREFIX="../target" -DCMAKE_PREFIX_PATH="$(brew --prefix qt5)" $CMAKE_OPTS -DUSE_WERROR=OFF
            make
      - run:
          name: Build tests
          command: cd build && make tests
      - run:
          name: Run tests
          command: build/tests/tests
      - run:
          name: Build DMG
          command: |
            cd build
            make install
            make dmg
            cp ./lmms-*.dmg /tmp/artifacts/
      - store_artifacts:
          path: /tmp/artifacts/
          destination: /
      - *save_cache
      - *save_homebrew_cache


workflows:
  version: 2
  build-and-test:
    jobs:
      - macos
      - mingw32
      - mingw64
      - linux.gcc
      - shellcheck
