variables:
  BUILDTYPE: Release

default:
  image: git.do.srb2.org:5050/stjr/srb2ci/srb2ci:stable

  cache:
    - key: ccache-$CI_PROJECT_PATH_SLUG-$CI_JOB_NAME_SLUG
      fallback_keys:
        - cache-$CI_PROJECT_PATH_SLUG-$CI_DEFAULT_BRANCH
        - cache-$CI_PROJECT_PATH_SLUG-default
      paths:
        - ccache
        #- ccache_statslog

    - key: apt-$CI_JOB_IMAGE
      paths:
        - apt-cache
      unprotect: true

  before_script:
    - - |
         # debconf
         echo -e "\e[0Ksection_start:`date +%s`:debconf[collapsed=true]\r\e[0KSetup debconf's environment"
      - export DEBIAN_FRONTEND="noninteractive"
      - export DEBIAN_PRIORITY="low"
      - export DEBCONF_NONINTERACTIVE_SEEN="true"
      - |
          # debconf
          echo -e "\e[0Ksection_end:`date +%s`:debconf\r\e[0K"

    - - |
          # dpkg_aa
          echo -e "\e[0Ksection_start:`date +%s`:dpkg_aa[collapsed=true]\r\e[0KAdding architectures to dpkg"
      - dpkg --add-architecture i386
      - dpkg --add-architecture amd64
      - dpkg --add-architecture arm64
      - |
          # dpkg_aa
          echo -e "\e[0Ksection_end:`date +%s`:dpkg_aa\r\e[0K"

    - - |
          # apt_conf
          echo -e "\e[0Ksection_start:`date +%s`:apt_conf[collapsed=true]\r\e[0KSetting up APT conf"
      - export APT_CACHE_DIR=`pwd`/apt-cache
      - mkdir --parents --verbose $APT_CACHE_DIR/partial/
      - touch /etc/apt/apt.conf.d/99build
      - |
          # apt.conf
          echo Adding options to apt.conf':'
      - |
          # APT::Install-Recommends
          echo APT::Install-Recommends "false"\;       | tee --append /etc/apt/apt.conf.d/99build
      - |
          # quit
          echo quiet "1"\;                             | tee --append /etc/apt/apt.conf.d/99build
      - |
          # APT::Get::Assume-Yes
          echo APT::Get::Assume-Yes "true"\;           | tee --append /etc/apt/apt.conf.d/99build
      - |
          # Dir::Cache::Archives
          echo Dir::Cache::Archives "$APT_CACHE_DIR"\; | tee --append /etc/apt/apt.conf.d/99build
      - |
          # apt_conf
          echo -e "\e[0Ksection_end:`date +%s`:apt_conf\r\e[0K"

    - - |
          # apt_update
          echo -e "\e[0Ksection_start:`date +%s`:apt_update[collapsed=true]\r\e[0KUpdating APT listing"
      - apt-get update
      - |
          # apt_update
          echo -e "\e[0Ksection_end:`date +%s`:apt_update\r\e[0K"

    - - |
          # apt_pre
          echo -e "\e[0Ksection_start:`date +%s`:apt_pre[collapsed=true]\r\e[0KInstalling pre packages"
      - apt-get install apt-utils
      - |
          # apt_pre
          echo -e "\e[0Ksection_end:`date +%s`:apt_pre\r\e[0K"

    - - |
          # apt_upgrade
          echo -e "\e[0Ksection_start:`date +%s`:apt_upgrade[collapsed=true]\r\e[0KUpdating existing packages"
      - apt-get upgrade
      - |
          # apt_update
          echo -e "\e[0Ksection_end:`date +%s`:apt_upgrade\r\e[0K"

    - - |
          # apt_common
          echo -e "\e[0Ksection_start:`date +%s`:apt_common[collapsed=true]\r\e[0KInstalling common packages"
      - apt-get install make git ccache
      - |
          # apt_common
          echo -e "\e[0Ksection_end:`date +%s`:apt_common\r\e[0K"

    - - |
          # ccache_config
          echo -e "\e[0Ksection_start:`date +%s`:ccache_config[collapsed=true]\r\e[0KSetting up ccache config"
      - mkdir --parents --verbose ~/.ccache/
      - touch ~/.ccache/ccache.conf
      - |
          # cache.conf
          echo Adding ccache configution option
      - |
          # base_dir
          echo base_dir = $PWD                  | tee --append ~/.ccache/ccache.conf
      - |
          # cache_dir
          echo cache_dir = $PWD/ccache          | tee --append ~/.ccache/ccache.conf
      - |
          # compiler_check
          echo compiler_check = content         | tee --append ~/.ccache/ccache.conf
      - |
          # stats_log
          echo #stats_log = $PWD/ccache_statslog | tee --append ~/.ccache/ccache.conf
      - |
          # max_size
          echo max_size = 50M                   | tee --append ~/.ccache/ccache.conf
      - |
          # ccache_config
          echo -e "\e[0Ksection_end:`date +%s`:ccache_config\r\e[0K"

    - - |
          # cache_reset
          echo -e "\e[0Ksection_start:`date +%s`:ccache_reset[collapsed=true]\r\e[0KResetting ccache statistics"
      - ccache --zero-stats
      - ccache --show-stats
      - |
          # ccache_reset
          echo -e "\e[0Ksection_end:`date +%s`:ccache_reset\r\e[0K"

  artifacts:
    paths:
      - "Build/"
    exclude:
      - "Build/Source/Native/*.d"
      - "Build/Source.Native/*.o"
      - "Build/Source.Native/OpenGL/*.d"
      - "Build/Source.Native/OpenGL/*.o"
      - "Build/Source.Native/OpenGL/gl_load/*.d"
      - "Build/Source.Native/OpenGL/gl_load/*.o"
      - "Build/Source.Native/VPO/*.d"
      - "Build/Source.Native/VPO/*.o"
      - "Build/libBuilderNative.so"
    expose_as: "Base"
    name: "$CI_PROJECT_PATH_SLUG-$CI_COMMIT_REF_SLUG-$CI_COMMIT_SHORT_SHA-$CI_JOB_NAME_SLUG"

  after_script:
    - - |
           # apt_clean
           echo -e "\e[0Ksection_start:`date +%s`:apt_clean[collapsed=true]\r\e[0KCleaning of unneeded APT packages"
      - apt-get autoclean
      - |
          # apt_clean
          echo -e "\e[0Ksection_end:`date +%s`:apt_clean\r\e[0K"

    - - |
          # ccache_stats
          echo -e "\e[0Ksection_start:`date +%s`:ccache_stats[collapsed=true]\r\e[0Kccache statistics:"
      - ccache --show-stats --verbose || ccache --show-stats
      - ccache --show-log-stats --verbose || ccache --show-log-stats || true
      - |
          # ccahe_stats
          echo -e "\e[0Ksection_end:`date +%s`:ccache_stats\r\e[0K"

stages:
  - build

Release:
  stage: build

  variables:
    CC: ccache clang
    CXX: ccache clang

  script:
   - - |
          # apt_toolchain
          echo -e "\e[0Ksection_start:`date +%s`:apt_toolchain[collapsed=true]\r\e[0KInstalling toolchain packages"
     - apt-get install clang msbuild
     - |
          # apt_toolchain
          echo -e "\e[0Ksection_end:`date +%s`:apt_toolchain\r\e[0K"

   - - |
          # apt_development
          echo -e "\e[0Ksection_start:`date +%s`:apt_development[collapsed=true]\r\e[0KInstalling development packages"
     - apt-get install mesa-common-dev
     - |
          # apt_development
          echo -e "\e[0Ksection_end:`date +%s`:apt_development\r\e[0K"

   - - |
          # make
          echo -e "\e[0Ksection_start:`date +%s`:make[collapsed=false]\r\e[0KCompiling UZB"
     - make --keep-going --silent || make --keep-going
     - |
          # make
          echo -e "\e[0Ksection_end:`date +%s`:make\r\e[0K"

i386:
  stage: build

  variables:
    CC: ccache i686-linux-gnu-gcc
    CXX: ccache i686-linux-gnu-g++
    CPPFLAGS: -msse2

  artifacts:
    paths:
      - "Build/libBuilderNative.so"
    expose_as: "Linux i386"
    name: "$CI_PROJECT_PATH_SLUG-$CI_COMMIT_REF_SLUG-$CI_COMMIT_SHORT_SHA-$CI_JOB_NAME_SLUG"

  script:
   - - |
          # apt_toolchain
          echo -e "\e[0Ksection_start:`date +%s`:apt_toolchain[collapsed=true]\r\e[0KInstalling toolchain packages"
     - apt-get install g++-i686-linux-gnu || apt-get install g++
     - |
          # apt_toolchain
          echo -e "\e[0Ksection_end:`date +%s`:apt_toolchain\r\e[0K"

   - - |
          # apt_development
          echo -e "\e[0Ksection_start:`date +%s`:apt_development[collapsed=true]\r\e[0KInstalling development packages"
     - apt-get install mesa-common-dev:i386
     - |
          # apt_development
          echo -e "\e[0Ksection_end:`date +%s`:apt_development\r\e[0K"

   - - |
          # make
          echo -e "\e[0Ksection_start:`date +%s`:make[collapsed=false]\r\e[0KCompiling Native"
     - make --keep-going Build/libBuilderNative.so || make --keep-going Build/libBuilderNative.so
     - |
          # make
          echo -e "\e[0Ksection_end:`date +%s`:make\r\e[0K"

amd64:
  stage: build

  variables:
    CC: ccache x86_64-linux-gnu-gcc
    CXX: ccache x86_64-linux-gnu-gcc

  artifacts:
    paths:
      - "Build/libBuilderNative.so"
    expose_as: "Linux amd64"
    name: "$CI_PROJECT_PATH_SLUG-$CI_COMMIT_REF_SLUG-$CI_COMMIT_SHORT_SHA-$CI_JOB_NAME_SLUG"

  script:
   - - |
          # apt_toolchain
          echo -e "\e[0Ksection_start:`date +%s`:apt_toolchain[collapsed=true]\r\e[0KInstalling toolchain packages"
     - apt-get install g++-x86-64-linux-gnu || apt-get install g++
     - |
          # apt_toolchain
          echo -e "\e[0Ksection_end:`date +%s`:apt_toolchain\r\e[0K"

   - - |
          # apt_development
          echo -e "\e[0Ksection_start:`date +%s`:apt_development[collapsed=true]\r\e[0KInstalling development packages"
     - apt-get install mesa-common-dev:amd64
     - |
          # apt_development
          echo -e "\e[0Ksection_end:`date +%s`:apt_development\r\e[0K"

   - - |
          # make
          echo -e "\e[0Ksection_start:`date +%s`:make[collapsed=false]\r\e[0KCompiling Native"
     - make --keep-going Build/libBuilderNative.so || make --keep-going Build/libBuilderNative.so
     - |
          # make
          echo -e "\e[0Ksection_end:`date +%s`:make\r\e[0K"

arm64:
  stage: build

  variables:
    CC: ccache aarch64-linux-gnu-gcc
    CXX: ccache aarch64-linux-gnu-g++

  artifacts:
    paths:
      - "Build/libBuilderNative.so"
    expose_as: "Linux arm64"
    name: "$CI_PROJECT_PATH_SLUG-$CI_COMMIT_REF_SLUG-$CI_COMMIT_SHORT_SHA-$CI_JOB_NAME_SLUG"

  script:
   - - |
          # apt_toolchain
          echo -e "\e[0Ksection_start:`date +%s`:apt_toolchain[collapsed=true]\r\e[0KInstalling toolchain packages"
     - apt-get install g++-aarch64-linux-gnu || apt-get install g++
     - |
          # apt_toolchain
          echo -e "\e[0Ksection_end:`date +%s`:apt_toolchain\r\e[0K"

   - - |
          # apt_development
          echo -e "\e[0Ksection_start:`date +%s`:apt_development[collapsed=true]\r\e[0KInstalling development packages"
     - apt-get install mesa-common-dev:arm64
     - |
          # apt_development
          echo -e "\e[0Ksection_end:`date +%s`:apt_development\r\e[0K"

   - - |
          # make
          echo -e "\e[0Ksection_start:`date +%s`:make[collapsed=false]\r\e[0KCompiling Native"
     - make --keep-going Build/libBuilderNative.so || make --keep-going Build/libBuilderNative.so
     - |
          # make
          echo -e "\e[0Ksection_end:`date +%s`:make\r\e[0K"

win32:
  stage: build

  variables:
    CC: ccache i686-w64-mingw32-gcc-posix
    CXX: ccache i686-w64-mingw32-g++-posix

  artifacts:
    paths:
      - "Build/BuilderNative.dll"
    expose_as: "Windows x86"
    name: "$CI_PROJECT_PATH_SLUG-$CI_COMMIT_REF_SLUG-$CI_COMMIT_SHORT_SHA-$CI_JOB_NAME_SLUG"

  script:
   - - |
          # apt_toolchain
          echo -e "\e[0Ksection_start:`date +%s`:apt_toolchain[collapsed=true]\r\e[0KInstalling toolchain packages"
     - apt-get install g++-mingw-w64-i686
     - |
          # apt_toolchain
          echo -e "\e[0Ksection_end:`date +%s`:apt_toolchain\r\e[0K"

   - - |
          # make
          echo -e "\e[0Ksection_start:`date +%s`:make[collapsed=false]\r\e[0KCompiling Native"
     - make --keep-going Build/BuilderNative.dll MINGW=1 || make --keep-going Build/BuilderNative.dll MINGW=1
     - |
          # make
          echo -e "\e[0Ksection_end:`date +%s`:make\r\e[0K"

win64:
  stage: build

  variables:
    CC: ccache x86_64-w64-mingw32-gcc-posix
    CXX: ccache x86_64-w64-mingw32-g++-posix

  artifacts:
    paths:
      - "Build/BuilderNative.dll"
    expose_as: "Windows amd64"
    name: "$CI_PROJECT_PATH_SLUG-$CI_COMMIT_REF_SLUG-$CI_COMMIT_SHORT_SHA-$CI_JOB_NAME_SLUG"

  script:
   - - |
          # apt_toolchain
          echo -e "\e[0Ksection_start:`date +%s`:apt_toolchain[collapsed=true]\r\e[0KInstalling toolchain packages"
     - apt-get install g++-mingw-w64-x86-64
     - |
          # apt_toolchain
          echo -e "\e[0Ksection_end:`date +%s`:apt_toolchain\r\e[0K"

   - - |
          # make
          echo -e "\e[0Ksection_start:`date +%s`:make[collapsed=false]\r\e[0KCompiling Native"
     - make --keep-going Build/BuilderNative.dll MINGW=1 || make --keep-going Build/BuilderNative.dll MINGW=1
     - |
          # make
          echo -e "\e[0Ksection_end:`date +%s`:make\r\e[0K"

Debug:
  extends:
    - Release

  allow_failure: true

  variables:
    BUILDTYPE: Debug
