stages:
  - build
  - upload
 
default:
  interruptible: true
  timeout: 4h

variables:
  AUTOBUILD_BUILD_ID: $CI_PIPELINE_ID
  AUTOBUILD_INSTALLABLE_CACHE: "$CI_PROJECT_DIR/.cache/autobuild"
  PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"

.win_build:
  stage: build
  tags:
    - autobuild
    - windows
  cache:
    key:
      files:
        - autobuild.xml
      prefix: ${CI_JOB_NAME}
    paths:
      - .cache/autobuild
      - .cache/pip
      - .venv/
    when: 'always'
  variables:
    USE_SENTRY: "TRUE"
  before_script:
    - virtualenv -p C:\Python39\python.exe .venv
    - .\.venv\Scripts\activate.ps1
    - pip3 install --upgrade autobuild -i https://ci-job-token:${CI_JOB_TOKEN}@git.alchemyviewer.org/api/v4/projects/54/packages/pypi/simple --extra-index-url https://pypi.org/simple
  script:
    - autobuild configure -c ReleaseOS -- -DUSE_FMODSTUDIO=ON -DUSE_NVAPI=ON -DUSE_LTO=ON -DDISABLE_FATAL_WARNINGS=ON -DREVISION_FROM_VCS=FALSE
    - autobuild build -c ReleaseOS --no-configure
    - autobuild graph -c ReleaseOS --graph-file alchemy-windows${AUTOBUILD_ADDRSIZE}-dependencies.svg 
    - |
        If ($env:USE_SENTRY -eq 'TRUE') 
        {
          $AlchemyPdbPath = Resolve-Path build-vc-*/newview/Release/alchemy-bin.pdb
          sentry-cli difutil bundle-sources $AlchemyPdbPath
        }
  artifacts:
    name: "$env:CI_COMMIT_REF_NAME-$env:CI_COMMIT_SHORT_SHA"
    expire_in: 1 week
    paths:
      - build-vc-*/packages/lib/release/alut*.dll
      - build-vc-*/packages/lib/release/epoxy-*.dll
      - build-vc-*/packages/lib/release/fmod*.dll
      - build-vc-*/packages/lib/release/glod*.dll
      - build-vc-*/packages/lib/release/libapr*.dll
      - build-vc-*/packages/lib/release/libexpat*.dll
      - build-vc-*/packages/lib/release/libhunspell*.dll
      - build-vc-*/packages/lib/release/libogg*.dll
      - build-vc-*/packages/lib/release/libvorbis*.dll
      - build-vc-*/packages/lib/release/OpenAL32*.dll
      - build-vc-*/packages/lib/release/openjp2*.dll
      - build-vc-*/packages/lib/release/sentry.dll
      - build-vc-*/packages/lib/release/xmlrpc-epi*.dll
      - build-vc-*/packages/lib/release/*.pdb
      - build-vc-*/newview/Release/build_data.json
      - build-vc-*/newview/Release/alchemy-bin.exe
      - build-vc-*/newview/Release/alchemy-bin.pdb
      - build-vc-*/newview/Release/alchemy-bin.src.zip
      - build-vc-*/newview/Release/Alchemy_*_Setup.exe
      - alchemy-*-dependencies.svg

.mac_build:
  stage: build
  tags:
    - mac
    - m1
    - bash
  cache:
    key:
      files:
        - autobuild.xml
      prefix: ${CI_JOB_NAME}
    paths:
      - .cache/autobuild
      - .cache/pip
      - .venv
    when: 'always'
  variables:
    USE_SENTRY: "TRUE"
  before_script:
    - virtualenv .venv -p python3
    - source .venv/bin/activate
    - pip3 install --upgrade autobuild dmgbuild -i https://ci-job-token:${CI_JOB_TOKEN}@git.alchemyviewer.org/api/v4/projects/54/packages/pypi/simple --extra-index-url https://pypi.org/simple
  script:
    - autobuild configure -c ReleaseOS -- -DUSE_FMODSTUDIO=ON -DUSE_NVAPI=ON -DUSE_LTO=ON -DDISABLE_FATAL_WARNINGS=ON -DREVISION_FROM_VCS=FALSE
    - autobuild build -c ReleaseOS --no-configure
    - autobuild graph -c ReleaseOS --graph-file alchemy-darwin${AUTOBUILD_ADDRSIZE}-dependencies.svg
    - | 
        if [ "$USE_SENTRY" = "TRUE" ] 
        then 
          sentry-cli difutil bundle-sources build-darwin-*/newview/Release/*.app.dSYM/Contents/Resources/DWARF/*
        fi
  artifacts:
    name: "$env:CI_COMMIT_REF_NAME-$env:CI_COMMIT_SHORT_SHA"
    expire_in: 1 week
    paths:
      - build-darwin-*/build_data.json
      - build-darwin-*/packages/lib/release/libalut*.dylib
      - build-darwin-*/packages/lib/release/libapr*.dylib
      - build-darwin-*/packages/lib/release/libepoxy*.dylib
      - build-darwin-*/packages/lib/release/libfmod*.dylib
      - build-darwin-*/packages/lib/release/libGLOD*.dylib
      - build-darwin-*/packages/lib/release/libhunspell*.dylib
      - build-darwin-*/packages/lib/release/libndofdev*.dylib
      - build-darwin-*/packages/lib/release/libogg*.dylib
      - build-darwin-*/packages/lib/release/libopenal*.dylib
      - build-darwin-*/packages/lib/release/libvorbis*.dylib
      - build-darwin-*/packages/lib/release/libxmlrpc*.dylib
      - build-darwin-*/packages/lib/release/Sentry.framework
      - build-darwin-*/packages/lib/release/*.dSYM
      - build-darwin-*/newview/Alchemy_*.dmg
      - build-darwin-*/newview/Release/*.app/Contents/MacOS/*
      - build-darwin-*/newview/Release/*.dSYM
      - build-darwin-*/newview/Release/*.src.zip
      - alchemy-*-dependencies.svg

.linux_build:
  stage: build
  image: r.alchemyviewer.org/alchemy/infrastructure/ubuntu-build-image:latest-llvm
  tags:
    - linux
    - docker
  cache:
    key:
      files:
        - autobuild.xml
      prefix: ${CI_JOB_NAME}
    paths:
      - .cache/autobuild
      - .cache/pip
      - .venv
    when: 'always'
  variables:
    USE_SENTRY: "TRUE"
  before_script:
    - virtualenv .venv -p python3
    - source .venv/bin/activate
    - pip3 install --upgrade autobuild -i https://ci-job-token:${CI_JOB_TOKEN}@git.alchemyviewer.org/api/v4/projects/54/packages/pypi/simple --extra-index-url https://pypi.org/simple
  script:
    - autobuild configure -c ReleaseOS -- -DUSE_FMODSTUDIO=ON -DUSE_NVAPI=ON -DUSE_LTO=ON -DDISABLE_FATAL_WARNINGS=ON -DREVISION_FROM_VCS=FALSE
    - autobuild build -c ReleaseOS --no-configure
    - autobuild graph -c ReleaseOS --graph-file alchemy-linuxs${AUTOBUILD_ADDRSIZE}-dependencies.svg
    - |
        if [ "$USE_SENTRY" = "TRUE" ] 
        then
          sentry-cli difutil bundle-sources build-linux-*/newview/alchemy-bin.debug
        fi
  artifacts:
    name: "$env:CI_COMMIT_REF_NAME-$env:CI_COMMIT_SHORT_SHA"
    expire_in: 1 week
    paths:
      - build-linux-*/build_data.json
      - build-linux-*/packages/lib/release/libalut.so*
      - build-linux-*/packages/lib/release/libapr-1.so*
      - build-linux-*/packages/lib/release/libaprutil-1.so*
      - build-linux-*/packages/lib/release/libepoxy.so*
      - build-linux-*/packages/lib/release/libexpat.so*
      - build-linux-*/packages/lib/release/libfmod.so*
      - build-linux-*/packages/lib/release/libGLOD.so*
      - build-linux-*/packages/lib/release/libjpeg.so*
      - build-linux-*/packages/lib/release/libogg.so*
      - build-linux-*/packages/lib/release/libopenal.so*
      - build-linux-*/packages/lib/release/libopenjp2.so*
      - build-linux-*/packages/lib/release/libSDL2*.so*
      - build-linux-*/packages/lib/release/libvorbis*.so*
      - build-linux-*/newview/Alchemy_*.tar.xz
      - build-linux-*/newview/alchemy-bin
      - build-linux-*/newview/alchemy-bin.debug
      - build-linux-*/newview/alchemy-bin.src.zip
      - alchemy-*-dependencies.svg


coverity_scan:
  stage: build
  image: r.alchemyviewer.org/alchemy/infrastructure/ubuntu-build-image:latest-llvm-coverity
  tags:
    - linux
    - docker
  rules:
    - if: '$CI_COMMIT_BRANCH == "coverity"'
  cache:
    key:
      files:
        - autobuild.xml
      prefix: ${CI_JOB_NAME}
    paths:
      - .cache/autobuild
      - .cache/pip
      - .venv
    when: 'always'
  variables:
    AUTOBUILD_ADDRSIZE: 64
    USE_SENTRY: "TRUE"
  before_script:
    - virtualenv .venv -p python3
    - source .venv/bin/activate
    - pip3 install --upgrade autobuild -i https://ci-job-token:${CI_JOB_TOKEN}@git.alchemyviewer.org/api/v4/projects/54/packages/pypi/simple --extra-index-url https://pypi.org/simple
  script:
    - cov-configure --template --comptype clangcc --compiler clang-12
    - cov-configure --template --comptype clangcxx --compiler clang++-12
    - autobuild configure -c Coverity -- -DLL_TESTS=OFF -DUSE_FMODSTUDIO=ON -DUSE_NVAPI=ON -DUSE_LTO=OFF -DDISABLE_FATAL_WARNINGS=ON -DREVISION_FROM_VCS=FALSE -DUSE_PRECOMPILED_HEADERS=OFF
    - autobuild build -c Coverity --no-configure
    - |
        pushd "build-linux-64"
          tar caf alchemyviewer.xz cov-int
          curl --header "JOB-TOKEN: $CI_JOB_TOKEN" --upload-file alchemyviewer.xz "${CI_API_V4_URL}/projects/165/packages/generic/coverity/0.0.$CI_PIPELINE_ID/alchemyviewer.xz"
        popd


.win32_build:
  extends: .win_build
  variables:
    AUTOBUILD_ADDRSIZE: 32
  cache:
    key:
      prefix: windows32

.win64_build:
  extends: .win_build
  variables:
    AUTOBUILD_ADDRSIZE: 64
  cache:
    key:
      prefix: windows64

.mac64_build:
  extends: .mac_build
  variables:
    AUTOBUILD_ADDRSIZE: 64
  cache:
    key:
      prefix: darwin64

.linux32_build:
  extends: .linux_build
  variables:
    AUTOBUILD_ADDRSIZE: 32
  cache:
    key:
      prefix: linux32

.linux64_build:
  extends: .linux_build
  variables:
    AUTOBUILD_ADDRSIZE: 64
  cache:
    key:
      prefix: linux64

.master_rules:
  rules:
    - if: $BUILD_CHANNEL || $CI_COMMIT_TAG
      when: never
    - if: '$CI_PIPELINE_SOURCE == "web"'
    - if: '$CI_PIPELINE_SOURCE == "schedule"'
    #- if: '$CI_COMMIT_BRANCH == "master" && $CI_PIPELINE_SOURCE == "push"'
    #  when: delayed
    #  start_in: '12 hours'
  variables:
    VIEWER_CHANNEL_TYPE: Test
    VIEWER_USE_CRASHPAD: "FALSE"

.project_rules:
  rules:
    - if: '$BUILD_CHANNEL == "Project" && ($CI_PIPELINE_SOURCE == "web" || $CI_PIPELINE_SOURCE == "schedule")'
    - if: '$CI_COMMIT_TAG =~ /.*-project/'
  variables:
    VIEWER_CHANNEL_TYPE: Project

.beta_rules:
  rules:
    - if: '$BUILD_CHANNEL == "Beta" && ($CI_PIPELINE_SOURCE == "web" || $CI_PIPELINE_SOURCE == "schedule")'
    - if: '$CI_COMMIT_TAG =~ /.*-beta/'
  variables:
    VIEWER_CHANNEL_TYPE: Beta

.release_rules:
  rules:
    - if: '$BUILD_CHANNEL == "Release" && ($CI_PIPELINE_SOURCE == "web" || $CI_PIPELINE_SOURCE == "schedule")'
    - if: '$CI_COMMIT_TAG =~ /.*-release/'
  variables:
    VIEWER_CHANNEL_TYPE: Release

build:master:linux64:
  extends: 
    - .linux64_build 
    - .master_rules

build:master:mac64:
  extends: 
    - .mac64_build
    - .master_rules

.build:master:windows32:
  extends: 
    - .win32_build
    - .master_rules

build:master:windows64:
  extends: 
    - .win64_build
    - .master_rules

build:project:linux64:
  extends: 
    - .linux64_build
    - .project_rules

build:project:mac64:
  extends: 
    - .mac64_build
    - .project_rules

.build:project:windows64:
  extends: 
    - .win32_build
    - .project_rules

build:project:windows64:
  extends: 
    - .win64_build
    - .project_rules

build:beta:linux64:
  extends: 
    - .linux64_build
    - .beta_rules

build:beta:mac64:
  extends: 
    - .mac64_build
    - .beta_rules

.build:beta:windows32:
  extends: 
    - .win32_build
    - .beta_rules

build:beta:windows64:
  extends: 
    - .win64_build
    - .beta_rules

build:release:linux64:
  extends: 
    - .linux64_build
    - .release_rules

build:release:mac64:
  extends: 
    - .mac64_build
    - .release_rules

.build:release:windows32:
  extends: 
    - .win32_build
    - .release_rules

build:release:windows64:
  extends: 
    - .win64_build
    - .release_rules

.upload_template:
  stage: upload
  tags:
    - powershell
    - m1
    - mac
  allow_failure: false
  variables:
    USE_SENTRY: "TRUE"
  script:
    - |
        $BuildData = Get-Content ./build-vc-64/newview/Release/build_data.json | ConvertFrom-Json
        $BuildChannelVersion = $BuildData."Channel" + ' ' + $BuildData."Version"
        $PkgName = $BuildData.Channel.Replace(" ", "_")
        $PkgVer = $BuildData.Version
        $UploadDestURL = "${CI_API_V4_URL}/projects/${CI_PROJECT_ID}/packages/generic/${PkgName}/${PkgVer}"

        $UploadParams = @{ UseBasicParsing = $true;
         Method = "PUT";
         Headers = @{
           "JOB-TOKEN" = "${CI_JOB_TOKEN}"; };
         Verbose = $true; };

        If ($env:USE_SENTRY -eq 'TRUE') 
        {
          sentry-cli releases new $BuildChannelVersion
          sentry-cli releases set-commits --auto $BuildChannelVersion
        }

        Push-Location ./build-linux-64/
          Push-Location ./newview/
            $FileNameLnx64 = Get-ChildItem -Path . -Name -Include Alchemy_*.tar.xz
            Invoke-WebRequest @UploadParams -InFile ./$FileNameLnx64 -Uri "${UploadDestURL}/${FileNameLnx64}"

            If ($env:USE_SENTRY -eq 'TRUE') 
            {
              sentry-cli upload-dif alchemy-bin alchemy-bin.debug alchemy-bin.src.zip
            }
          Pop-Location

          If ($env:USE_SENTRY -eq 'TRUE') 
          {
            sentry-cli upload-dif packages/
          }
        Pop-Location

        Push-Location ./build-darwin-64/
          Push-Location ./newview/
            $FileNameMac64 = Get-ChildItem -Path . -Name -Include Alchemy_*.dmg
            Invoke-WebRequest @UploadParams -InFile ./$FileNameMac64 -Uri "${UploadDestURL}/${FileNameMac64}"
          Pop-Location

          If ($env:USE_SENTRY -eq 'TRUE') 
          {
            sentry-cli upload-dif newview/Release/
            sentry-cli upload-dif packages/
          }
        Pop-Location

        Push-Location ./build-vc-64/
          Push-Location ./newview/Release/
            $FileNameWin64 = Get-ChildItem -Path . -Name -Include Alchemy_*_Setup.exe
            Invoke-WebRequest @UploadParams -InFile ./$FileNameWin64 -Uri "${UploadDestURL}/${FileNameWin64}"
          Pop-Location

          If ($env:USE_SENTRY -eq 'TRUE') 
          {
            $AlchemyBinPath = Resolve-Path newview/Release/alchemy-bin.*
            sentry-cli upload-dif $AlchemyBinPath
            sentry-cli upload-dif packages/
          }
        Pop-Location

        If ($env:USE_SENTRY -eq 'TRUE') 
        {
          sentry-cli releases finalize $BuildChannelVersion
        }

upload:project:
  extends:
    - .upload_template
  rules:
    - if: '$BUILD_CHANNEL == "Project" && ($CI_PIPELINE_SOURCE == "web" || $CI_PIPELINE_SOURCE == "schedule")'
      when: manual
    - if: '$CI_COMMIT_TAG =~ /.*-project/'
      when: manual
  environment:
    name: qa

upload:beta:
  extends:
    - .upload_template
  rules:
    - if: '$BUILD_CHANNEL == "Beta" && ($CI_PIPELINE_SOURCE == "web" || $CI_PIPELINE_SOURCE == "schedule")'
      when: manual
    - if: '$CI_COMMIT_TAG =~ /.*-beta/'
      when: manual
  environment:
    name: beta

upload:release:
  extends:
    - .upload_template
  rules:
    - if: '$BUILD_CHANNEL == "Release" && ($CI_PIPELINE_SOURCE == "web" || $CI_PIPELINE_SOURCE == "schedule")'
      when: manual
    - if: '$CI_COMMIT_TAG =~ /.*-release/'
      when: manual
  environment:
    name: release
