#################################################
# BUILD
#################################################

.windows-vs:
  extends: .prepare
  stage: build
  rules:
    - if: $CI_PIPELINE_SOURCE == "merge_request_event" && $DOCKER_UPDATE == null && $SKIP_WINDOWS == null
    - if: $CI_PIPELINE_SOURCE == "schedule" && $DOCKER_UPDATE == null && $SKIP_WINDOWS == null
  variables:
    CMAKE_OPTIONS: -DENABLE_UNIT_TESTS=ON -DENABLE_G729=ON -DENABLE_PQCRYPTO=ON -DENABLE_GPL_THIRD_PARTIES=ON
    LINPHONESDK_PLATFORM: Desktop
    OUTPUT_ZIP_FOLDER: win64
    MINGW_TYPE: mingw64
    BUILD_TARGET: install
    CMAKE_C_COMPILER : cl.exe
    CMAKE_CXX_COMPILER : cl.exe
    CMAKE_RC_COMPILER : rc.exe

  script:
    - if ($MAKE_RELEASE_FILE_URL) { } else { $RELEASE_FILE = "" }
    - echo $env:Path
    - If ( Test-Path -Path "build-desktop" ) {Remove-Item -recurse -force -path "build-desktop" }
    - mkdir build-desktop
    - cd build-desktop
    - sccache --show-stats
    - sccache --zero-stats
    - Write-Output $CMAKE_ARCHITECTURE
    - Write-Output $SCHEDULE_CMAKE_OPTIONS
    - Write-Output $MAKEFILE_JOBS
    - Write-Output $CMAKE_C_COMPILER
    - Write-Output $CMAKE_CXX_COMPILER
    - Write-Output $CMAKE_RC_COMPILER
    - Write-Output $DEFAULT_CMAKE_OPTIONS
    - Write-Output $CMAKE_OPTIONS
    - Write-Output $CMAKE_ARCHITECTURE
    - Write-Output $SCHEDULE_CMAKE_OPTIONS
    - Write-Output $CMAKE_GENERATOR
    - Write-Output $LINPHONESDK_PLATFORM
    - Write-Output $MINGW_TYPE
    - Write-Output $CLI_OUTPUT_INFO_NINJA
    - Write-Output $Write-Output
    - Write-Output $NINJA_BUILD_PARALLEL_LEVEL
    - Write-Output $PARALLEL_OPTIONS
    - Write-Output $NINJA_OPTIMIZATION
    - Write-Output $NINJA_EXPLICIT_COMPILER_SET
    - Write-Output $DISPLAY_NINJA_LOG
    - Write-Output $SEARCH_NINJA_ERROR
    - Write-Output $DISPLAY_SEARCH_STATUS
    - Write-Output $SET_EXIT_CODE_BASED_ON_SEARCH_STATUS
    - Write-Output $SHOW_SEARCH_STATUS_SCRIPT
    - Write-Output $LAUNCH_SEARCH_STATUS_SCRIPT
    #We are forced to use Invoke-Expression to explain to powershell that we don't want it to touch to spaces in arguments
    #If we don't use it, '-A Win32' will be interpreted as "-A ' Win32'" thus making the build fail
    - echo $LastExitCode
    - Invoke-Expression "& cmake .. -G '$CMAKE_GENERATOR' -DLINPHONESDK_PLATFORM=$LINPHONESDK_PLATFORM -DENABLE_CSHARP_WRAPPER=YES -DCMAKE_BUILD_TYPE=$CI_BUILD_TYPE -DLINPHONEAPP_APPLICATION_NAME='$APPLICATION_NAME' -DLINPHONEAPP_EXECUTABLE_NAME='$EXECUTABLE_NAME' $NINJA_BUILD_PARALLEL_LEVEL $NINJA_EXPLICIT_COMPILER_SET $DEFAULT_CMAKE_OPTIONS $DEFAULT_WINDOWS_CMAKE_OPTIONS $CMAKE_OPTIONS $CMAKE_ARCHITECTURE $SCHEDULE_CMAKE_OPTIONS $RELEASE_FILE"
    - echo $LastExitCode
    - 'if (-not ($LastExitCode -eq 0)) {throw "Error: Configure failed"}'
    - $CLI_OUTPUT_INFO_NINJA
    #Warning : Ninja doesn't return an error code on Linker error.
    #Store outputs in a file log
    #Only in powershell 7 (Gitlab 14+)
    # - cmake --build . --target install --config RelWithDebInfo --parallel $MAKEFILE_JOBS | Select-String -NotMatch -Raw -Pattern "inclusion du fichier"
    - Write-Output $PARALLEL_OPTIONS
    - Write-Output $NINJA_OPTIMIZATION
    # /!\ By design, we must keep $NINJA_OPTIMIZATION as the last option of the line, because it contains an output redirect
    - echo $LastExitCode
    - Invoke-Expression "cmake --build . --target $BUILD_TARGET --config $CI_BUILD_TYPE $PARALLEL_OPTIONS  $NINJA_OPTIMIZATION"
    - if ($EXITS_ON_ERROR_MSBUILD) { Invoke-Expression "$EXITS_ON_ERROR_MSBUILD" } else { Write-Output "EXITS_ON_ERROR_MSBUILD is null" }
    - if ($DISPLAY_NINJA_LOG) { Invoke-Expression "$DISPLAY_NINJA_LOG" } else { Write-Output "DISPLAY_NINJA_LOG is null" }
    - if ($SEARCH_NINJA_ERROR) { Invoke-Expression "$SEARCH_NINJA_ERROR" } else { Write-Output "SEARCH_NINJA_ERROR is null" }
    - if ($DISPLAY_SEARCH_STATUS) { Invoke-Expression "$DISPLAY_SEARCH_STATUS" } else { Write-Output "DISPLAY_SEARCH_STATUS is null" }
    - if ($SET_EXIT_CODE_BASED_ON_SEARCH_STATUS) { Invoke-Expression "$SET_EXIT_CODE_BASED_ON_SEARCH_STATUS" } else { Write-Output "SET_EXIT_CODE_BASED_ON_SEARCH_STATUS is null" }
    - if ($SHOW_SEARCH_STATUS_SCRIPT) { Invoke-Expression "$SHOW_SEARCH_STATUS_SCRIPT" } else { Write-Output "SHOW_SEARCH_STATUS_SCRIPT is null" }
    - if ($LAUNCH_SEARCH_STATUS_SCRIPT) { Invoke-Expression "$LAUNCH_SEARCH_STATUS_SCRIPT" -ErrorAction stop } else { Write-Output "LAUNCH_SEARCH_STATUS_SCRIPT is null" }
    - sccache --show-stats

################
  artifacts:
    paths:
      - build-desktop\ninja_buildlog.txt
      - build-desktop\ninja_buildlog.txt
      - build-desktop\invertSearch.ps1
      - build-desktop\OUTPUT\*
      - build-desktop\external\linphone-sdk\openh264-prefix\src\openh264-stamp\*
    when: always
    expire_in: 1 week


.windows-vs2022:
  extends: .windows-vs
  tags: [ "windows-powershell-vs-17-2022" ]

.windows-codesigning:
  extends: .prepare
  tags: [ "windows-powershell-vs-17-2022-apps" ]

.windows-msbuild-variables:
  variables:
    PARALLEL_OPTIONS: "-- /maxcpucount /nodeReuse:true /p:TrackFileAccess=false"
    EXITS_ON_ERROR_MSBUILD: 'if (-not ($$LastExitCode -eq 0)) {throw "Error: Build failed"}'
    CMAKE_ARCHITECTURE : -A x64

.windows-ninja-variables:
  variables:
    CLI_OUTPUT_INFO_NINJA: Write-Output -NoEnumerate "Building with Ninja. See ninja_buildlog.txt to get details before completing the build."
    NINJA_BUILD_PARALLEL_LEVEL: -DCMAKE_BUILD_PARALLEL_LEVEL=$WINDOWS_PARALLEL_JOBS
    PARALLEL_OPTIONS: "--parallel $WINDOWS_PARALLEL_JOBS"
    NINJA_OPTIMIZATION: "| find /V \"inclusion du fichier\" > ninja_buildlog.txt; cmd /c \"exit /b 0\""
    NINJA_EXPLICIT_COMPILER_SET: -DCMAKE_C_COMPILER="$CMAKE_C_COMPILER" -DCMAKE_CXX_COMPILER="$CMAKE_CXX_COMPILER" -DCMAKE_RC_COMPILER="$CMAKE_RC_COMPILER"
    DISPLAY_NINJA_LOG: Get-Content ninja_buildlog.txt
    SEARCH_NINJA_ERROR: 'Write-Output "`$$isFound = (Select-String -Pattern `"build stopped: subcommand failed`" -SimpleMatch -Quiet -Path ninja_buildlog.txt)" > invertSearch.ps1'
    DISPLAY_SEARCH_STATUS: Write-Output 'echo $$isFound' >> invertSearch.ps1
    SET_EXIT_CODE_BASED_ON_SEARCH_STATUS: Write-Output -NoEnumerate "If (`$(echo `$$isFound) -eq `"True`") {throw `"There was an error in the build`"} else {exit 0}" >> invertSearch.ps1
    SHOW_SEARCH_STATUS_SCRIPT: Get-Content invertSearch.ps1
    LAUNCH_SEARCH_STATUS_SCRIPT: .\invertSearch.ps1
    CMAKE_GENERATOR: "Ninja"

.windows-msbuild-scheduled:
  extends: .windows-msbuild-variables
  rules:
    - if: $NIGHTLY_MASTER
    - if: $NIGHTLY_RELEASE
    - if: $ENABLE_WINDOWS_TESTS
  before_script:
    #cache disabled on scheduled builds since we dot not need the fastest build
    - Set-Variable -Name "CLCACHE_DISABLE" -Value 1

######################################################
# On each push
######################################################

.win64-mr-rules:
  rules:
    - if: ($CI_PIPELINE_SOURCE == "merge_request_event") && $DOCKER_UPDATE == null && $SKIP_WINDOWS == null

win64-msbuild-vs2022-windows:
  rules:
    - !reference [.rules-merge-request-manual, rules]
  extends:
    - .windows-vs2022
    - .windows-msbuild-variables
    - .win64-mr-rules

win64-ninja-vs2022-windows:
  extends:
    - .windows-vs2022
    - .windows-ninja-variables
    - .win64-mr-rules

######################################################
# NIGHTLY
######################################################

##      ON SCHEDULE     ##

.vs-scheduled-windows:
  rules:
    - !reference [.rules-merge-request-manual, rules]
    - if: $NIGHTLY_MASTER
    - if: $DEPLOY_PLUGINS
    - if: $DEPLOY_RUN_WINDOWS

win64-ninja-vs2022-scheduled-windows:
  extends:
    - win64-ninja-vs2022-windows
    - .vs-scheduled-windows

#################################################
#               PACKAGE
#################################################

#Remove . when packaging process is ready to use

.vs-win64-package:
  stage: package
  dependencies: []
  rules:
    - !reference [.rules-merge-request-manual, rules]
    - if: $NIGHTLY_MASTER
    - if: $NIGHTLY_RELEASE
    - if: $PACKAGE_WINDOWS
    - if: $DEPLOY_WINDOWS
  variables:
    CMAKE_OPTIONS: -DENABLE_APP_PACKAGING=YES -DENABLE_G729=ON -DENABLE_PQCRYPTO=ON -DENABLE_GPL_THIRD_PARTIES=ON
    RELEASE_FILE: -DLINPHONE_SDK_MAKE_RELEASE_FILE_URL=$MAKE_RELEASE_FILE_URL/$WINDOWS_PLATFORM/$APP_FOLDER

win64-ninja-vs2022-package-windows:
  variables:
    CMAKE_GENERATOR: "Ninja"
    CMAKE_ARCHITECTURE: ""
    PARALLEL_OPTIONS: ""
  extends:
    - .windows-vs2022
    - .windows-ninja-variables
    - .vs-win64-package

#################################################
#               SIGNING
#################################################

win64-codesigning:
  stage: signing
  extends:
    - .windows-codesigning
  needs:
    - win64-ninja-vs2022-package-windows
  rules:
    - !reference [.rules-merge-request-manual, rules]
    - if: $NIGHTLY_MASTER
    - if: $PACKAGE_WINDOWS
    - if: $DEPLOY_WINDOWS
  script:
    - cd build-desktop/OUTPUT/Packages/
    - ${WINDOWS_SIGN_TOOL} sign /fd SHA256 /t ${WINDOWS_SIGN_TIMESTAMP_URL} /sha1 ${WINDOWS_SIGN_HASH} *.exe

#################################################
#               DEPLOY
#################################################

.win64-upload:
  stage: deploy
  tags: [ "windows-powershell" ]
  rules:
    - if: $NIGHTLY_MASTER
    - if: $DEPLOY_WINDOWS

  script:
    - scp -pr build-desktop/OUTPUT/Packages/*.exe ${DEPLOY_SERVER}:${UPLOAD_ROOT_PATH}/${WINDOWS_PLATFORM}/${APP_FOLDER}
    - if ($MAKE_RELEASE_FILE_URL) { scp -pr build-desktop/OUTPUT/RELEASE ${DEPLOY_SERVER}:${UPLOAD_ROOT_PATH}/${WINDOWS_PLATFORM}/ }
    - if ($MAKE_RELEASE_FILE_URL) { scp -pr build-desktop/OUTPUT/RELEASE ${MAIN_DEPLOY_SERVER}:${UPLOAD_ROOT_PATH}/${WINDOWS_PLATFORM}/ }

win64-ninja-vs2022-upload:
  extends:
    - .win64-upload
  needs:
    - win64-codesigning

.win64-plugins-upload:
  stage: deploy
  tags: [ "windows" ]
  rules:
    - if: $DEPLOY_PLUGINS
  script:
    - scp "build-desktop/OUTPUT/plugins/app/*.dll" "%MAIN_DEPLOY_SERVER%:%WINDOWS_UPLOAD_DIRECTORY%/plugins"

win64-ninja-vs2022-plugins-upload:
  extends: .win64-plugins-upload
  needs:
    - win64-ninja-vs2022-scheduled-windows
