### Global defaults

env:  
  CCACHE_SIZE: "800M"
  CCACHE_DIR: "/tmp/ccache_dir"
  CCACHE_NOHASHDIR: "1"  # Debug info might contain a stale path if the build dir changes, but this is fine
  CCACHE_CPP2: "yes"

### Task templates

# https://cirrus-ci.org/guide/tips-and-tricks/#sharing-configuration-between-tasks
filter_template: &FILTER_TEMPLATE
  skip: "!changesInclude('.cirrus.yml', '**.{h,hpp,cpp,c,pro,pri,ts,ui,png,qbs,js}')"

regular_task_template: &REGULER_TASK_TEMPLATE
  << : *FILTER_TEMPLATE
  ccache_cache:
    folder: "/tmp/ccache_dir"

linux_qt6_task_template: &LINUX_QT6_TASK_TEMPLATE
  install_script:
    - bash -c "$PACKAGE_MANAGER_INSTALL -y qbs qt6-base-dev qt6-l10n-tools libqt6svg6-dev qt6-base-dev-tools qmake6 qbs libxerces-c-dev poppler-utils libgl1-mesa-dev"
  build_script:
    - uname -a
    - mkdir -pm 0700 $XDG_RUNTIME_DIR
    - Xvfb $DISPLAY -ac -screen 0 1600x1200x24+32 -nolisten tcp -nolisten unix &
    - sleep 1
    - ccache --set-config=sloppiness=pch_defines,time_macros
    - echo $PATH
    - which qmake6
    - which qbs
    - pwd
    - ls -l /usr/bin/ | grep -E "${COMPILER_REGEX}"
    - ${COMPILER} --version
    - qmake6 --version
    - qbs --version
    - qbs setup-toolchains /usr/bin/${COMPILER} ${COMPILER}
    - qbs setup-qt /usr/bin/qmake6 qt6
    - qbs config profiles.qt6.baseProfile ${COMPILER}
    - qbs-config defaultProfile qt6
    - qbs build -f valentina.qbs -d build --command-echo-mode command-line profile:qt6 config:release modules.buildconfig.enableCcache:${ENABLE_CCACHE}
    - qbs -p autotest-runner -d build profile:qt6 config:release
    - ccache -s
    
linux_qt5_qmake_task_template: &LINUX_QT5_QMAKE_TASK_TEMPLATE
  install_script:
    - bash -c "$PACKAGE_MANAGER_INSTALL qtbase5-dev libqt5svg5-dev qttools5-dev-tools libqt5xmlpatterns5-dev libqt5core5a libqt5gui5 libqt5printsupport5 libqt5svg5 libqt5widgets5 libqt5xml5 libqt5xmlpatterns5 poppler-utils"
  build_script:
    - uname -a
    - echo $PATH
    - which qmake
    - mkdir build
    - cd build
    - pwd
    - ls -l /usr/bin/ | grep -E "${COMPILER_REGEX}"
    - ${COMPILER} --version
    - qmake --version
    - qmake ../Valentina.pro -r -spec ${QMAKE_SPEC} CONFIG+=noDebugSymbols CONFIG+=checkWarnings
    - make -j$(nproc)
    - export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:"$CIRRUS_WORKING_DIR/build/src/libs/vpropertyexplorer/bin:$CIRRUS_WORKING_DIR/build/src/libs/qmuparser/bin"
    - xvfb-run -a make --silent check TESTARGS="-silent"
    - ccache -s

linux_qt5_qbs_task_template: &LINUX_QT5_QBS_TASK_TEMPLATE
  install_script:
    - bash -c "$PACKAGE_MANAGER_INSTALL qbs qtbase5-dev libqt5svg5-dev qttools5-dev-tools libqt5xmlpatterns5-dev libqt5core5a libqt5gui5 libqt5printsupport5 libqt5svg5 libqt5widgets5 libqt5xml5 libqt5xmlpatterns5 poppler-utils"
  build_script:
    - uname -a
    - mkdir -pm 0700 $XDG_RUNTIME_DIR
    - Xvfb $DISPLAY -ac -screen 0 1600x1200x24+32 -nolisten tcp -nolisten unix &
    - sleep 1
    - ccache --set-config=sloppiness=pch_defines,time_macros
    - echo $PATH
    - which qmake
    - which qbs
    - pwd
    - ls -l /usr/bin/ | grep -E "${COMPILER_REGEX}"
    - ${COMPILER} --version
    - qmake --version
    - qbs --version
    - qbs setup-toolchains /usr/bin/${COMPILER} ${COMPILER}
    - qbs setup-qt /usr/bin/qmake qt5
    - qbs-config defaultProfile qt5
    - qbs config profiles.qt5.baseProfile ${COMPILER}
    - qbs build -f valentina.qbs -d build --command-echo-mode command-line profile:qt5 config:release modules.buildconfig.enableCcache:${ENABLE_CCACHE}
    - qbs -p autotest-runner -d build profile:qt5 config:release
    - ccache -s

linux_task:
  << : *REGULER_TASK_TEMPLATE
  env:
    DEBIAN_FRONTEND: "noninteractive"
    PACKAGE_MANAGER_INSTALL: "apt-get -qq update && apt-get install -y"
    DISPLAY: ":90"
    XDG_RUNTIME_DIR: "/tmp/runtime-kdeci/"
  container:
    cpu: 4
  matrix:
    - container:
        image: dismine/clang-ubuntu:latest
        memory: 8G  # Set to 8GB to avoid OOM. https://cirrus-ci.org/guide/linux/#linux-container
      env:
        COMPILER: clang
        QMAKE_SPEC: "linux-clang"
        COMPILER_REGEX: clang
      matrix:
        - name: 'latest Clang [Qt5 QMake]'
          << :  *LINUX_QT5_QMAKE_TASK_TEMPLATE
          env:
            QT_SELECT: "qt5"
        - name: 'latest Clang [Qt5 QBS]'
          << : *LINUX_QT5_QBS_TASK_TEMPLATE
          env:
            QT_SELECT: "qt5"
            ENABLE_CCACHE: false
        - name: 'latest Clang [Qt6]'
          << : *LINUX_QT6_TASK_TEMPLATE
          env:
            QT_SELECT: "qt6"
            ENABLE_CCACHE: false
    - container:
        image: dismine/gcc-ubuntu:latest
        memory: 16G   # Set to 16GB to avoid OOM. https://cirrus-ci.org/guide/linux/#linux-container
      env:
        COMPILER: gcc
        QMAKE_SPEC: "linux-g++"
        GCC_COLORS: 'error=01;31:warning=01;35:note=01;36:caret=01;32:locus=01:quote=01'
        COMPILER_REGEX: "gcc|g\\+\\+"
      matrix:
        - name: 'latest GCC [Qt5 QMake]'
          << :  *LINUX_QT5_QMAKE_TASK_TEMPLATE
          env:
            QT_SELECT: "qt5"
        - name: 'latest GCC [Qt5 QBS]'
          << : *LINUX_QT5_QBS_TASK_TEMPLATE
          env:
            QT_SELECT: "qt5"
            ENABLE_CCACHE: true
        - name: 'latest GCC [Qt6]'
          << : *LINUX_QT6_TASK_TEMPLATE
          env:
            QT_SELECT: "qt6"
            ENABLE_CCACHE: true

macos_task_template: &MACOS_TASK_TEMPLATE
  << : *REGULER_TASK_TEMPLATE
  timeout_in: 120m
  env:
    ACCESS_TOKEN: ENCRYPTED[81e0b2381ffb628b73f5c94f834010e6631191e0ad03cdd0850d440fb2737a74b68131d842030f010c1bf73ab4cdc1ae]
    QTDIR: "/opt/homebrew/opt/qt6"
    PATH: $QTDIR/bin:${PATH}
    # ^  add user paths
    PIP_CACHE_DIR: ${HOME}/.cache/pip
    LDFLAGS: "-L$QTDIR/lib"
    CPPFLAGS: "-I$QTDIR/include"
    PKG_CONFIG_PATH: "/opt/homebrew/opt/qt6/lib/pkgconfig"
  local_homebrew_cache:
    folder: "$HOME/Library/Caches/Homebrew"
  global_conan_cache:
    folder: "$HOME/Library/Caches/Conan"
  local_conan_cache:
    folder: "~/.conan/data"
  pip_cache:
    folder: ${PIP_CACHE_DIR}
  install_script:
    - clang --version
    - find /Applications -maxdepth 1 -type d -name 'Xcode*.app'
    - sudo xcode-select -p
    - brew update > /dev/null
    - brew install --force qt6 coreutils ccache qbs cmake
    - echo $PATH
    - export PATH="${HOME}/.local/bin:`python3 -m site --user-base`/bin:$PATH"
    - echo $PATH
    - chmod -R 755 /opt/homebrew/opt/qt6/*
    - python3 --version
    - pip3 install --user --upgrade pip dropbox conan==1.59
    - ccache --set-config=sloppiness=pch_defines,time_macros 
    - conan --version
    - qmake --version
    - which qmake
    - qbs --version
  build_script:
    - echo $PATH
    - export PATH="${HOME}/.local/bin:`python3 -m site --user-base`/bin:$PATH"
    - echo $PATH
    - pwd
    - conan install . -s os=Macos -s os.version=${MACOS_DEPLOYMENT_TARGET} --build=missing
    - qbs setup-toolchains --detect
    - qbs config --list profiles
    - qbs setup-qt /opt/homebrew/opt/qt6/bin/qmake qt6
    - qbs-config defaultProfile qt6
    - qbs config profiles.qt6.baseProfile clang
    - qbs build -f valentina.qbs -d $CIRRUS_WORKING_DIR/build --command-echo-mode command-line config:release modules.buildconfig.enableUnitTests:false modules.buildconfig.enableMultiBundle:${MULTI_BUNDLE} qbs.installRoot:$CIRRUS_WORKING_DIR/build/install-root profile:qt6 project.enableConan:true project.minimumMacosVersion:${MACOS_DEPLOYMENT_TARGET} modules.buildconfig.enableCcache:${ENABLE_CCACHE}
    - ccache -s
  deploy_script:
    - pwd
    - $CIRRUS_WORKING_DIR/scripts/cirrus-deploy.sh
  before_cache_script:
    - brew cleanup

macos_task:
  macos_instance:
    image: ghcr.io/cirruslabs/macos-monterey-xcode:13.4.1
  << : *MACOS_TASK_TEMPLATE
  env:
    DEPLOY: "true"
    QT_VERSION: Qt6
    ARCH: arm64
    PLATFORM: "macOS_11+"
    MACOS_DEPLOYMENT_TARGET: 11.0
    ENABLE_CCACHE: false
  matrix:
    - name: 'macOS Monterey 12 [signle bundle, no tests]'
      env:
        MULTI_BUNDLE: false
    - name: 'macOS Monterey 12 [multi bundle, no tests]'
      env:
        MULTI_BUNDLE: true