#
# Copyright(c) 2021 to 2022 ZettaScale Technology and others
#
# This program and the accompanying materials are made available under the
# terms of the Eclipse Public License v. 2.0 which is available at
# http://www.eclipse.org/legal/epl-2.0, or the Eclipse Distribution License
# v. 1.0 which is available at
# http://www.eclipse.org/org/documents/edl-v10.php.
#
# SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
#

#
# Template with basic build and test instructions to be included by pipelines.
#

steps:
  - task: UsePythonVersion@0
    inputs:
      versionSpec: '3.8'
    name: install_python
  # Set defaults from steps to share them among pipelines
  - bash: |
      [[ -n "${ARCH}" ]] || \
        echo "###vso[task.setvariable variable=arch;]x86_64"
      [[ -n "${BUILD_TYPE}" ]] || \
        echo "###vso[task.setvariable variable=build_type;]Debug"
    name: setup
  - bash: |
      echo "###vso[task.setvariable variable=pip_cache;]${HOME}/.cache/pip"
      echo "###vso[task.setvariable variable=PATH;]$(python3 -m site --user-base)/bin:${PATH}"
      echo "###vso[task.setvariable variable=build_tool_options;]-j 4"
      sudo apt install -y clang clang-tools clang-tidy
    condition: eq(variables['Agent.OS'], 'Linux')
    name: setup_linux
  - bash: |
      set -e -x
      sudo /usr/libexec/ApplicationFirewall/socketfilterfw --setglobalstate off
      echo "###vso[task.setvariable variable=pip_cache;]${HOME}/Library/Caches/pip"
      echo "###vso[task.setvariable variable=PATH;]$(python3 -m site --user-base)/bin:${PATH}"
      echo "###vso[task.setvariable variable=build_tool_options;]-j 4"
    condition: eq(variables['Agent.OS'], 'Darwin')
    name: setup_macos
  # Use PowerShell rather than Bash to ensure Windows-style paths
  - pwsh: |
      $python_bin = python -m site --user-base
      Write-Host "###vso[task.setvariable variable=pip_cache;]${env:LOCALAPPDATA}\\pip\\Cache"
      Write-Host "###vso[task.setvariable variable=PATH;]$python_bin\\bin;${env:PATH}"
      # Visual Studio is most likely used on Windows agents
      if (${env:GENERATOR} -match "2019" -and -not ${env:PLATFORM}) {
        # Make sure platform matches arch if not specified
        if (${env:ARCH} -match "arm*") {
          Write-Host "###vso[task.setvariable variable=platform;]ARM"
        } elseif (${env:ARCH} -eq "x86") {
          Write-Host "###vso[task.setvariable variable=platform;]Win32"
        } else {
          Write-Host "###vso[task.setvariable variable=platform;]x64"
        }
      }
      if (${env:GENERATOR} -match "Visual Studio") {
        Write-Host "###vso[task.setvariable variable=build_tool_options;]-nologo -verbosity:minimal -maxcpucount:4 -p:CL_MPCount=4"
      } else {
        Write-Host "###vso[task.setvariable variable=build_tool_options;]-j 4"
      }
    condition: eq(variables['Agent.OS'], 'Windows_NT')
    name: setup_windows
  - task: Cache@2
    inputs:
      key: pip | 2 | $(Agent.OS)
      path: $(pip_cache)
    name: cache_pip
  - bash: |
      set -e -x
      python -m pip install pip wheel setuptools --user --upgrade
      python -m pip install pre-commit --user --upgrade
      pre-commit run --all-files
    name: run_pre_commit
  - bash: |
      python -m pip install gcovr==5.0 --user --upgrade
    name: install_gcovr
  - bash: |
      set -e -x
      sudo apt install libacl1-dev libncurses5-dev pkg-config
      git clone --depth 1 \
                --branch "${ICEORYX_BRANCH:-release_2.0}" \
                "${ICEORYX_REPOSITORY:-https://github.com/eclipse-iceoryx/iceoryx.git}" \
                iceoryx
      mkdir iceoryx/build
      cd iceoryx/build
      [[ "${AGENT_OS}" == 'Darwin' ]] && BUILD_TOOL_OPTIONS="-j 4"
      cmake -DCMAKE_BUILD_TYPE=${BUILD_TYPE} \
            -DCMAKE_BUILD_SHARED_LIBS=on \
            -DCMAKE_INSTALL_PREFIX=install \
            ${GENERATOR:+-G} "${GENERATOR}" -A "${PLATFORM}" -T "${TOOLSET}" ../iceoryx_meta
      cmake --build . --config ${BUILD_TYPE} --target install -- ${BUILD_TOOL_OPTIONS}
    condition: eq(variables['iceoryx'], 'on')
    name: install_iceoryx
  - bash: |
      set -e -x
      mkdir build
      cd build
      prefix_path="${BUILD_SOURCESDIRECTORY}/iceoryx/build/install"
      if [[ "${AGENT_OS}" == 'Darwin' ]] ; then
        prefix_path="${prefix_path}:`brew --prefix openssl`"
        # Azure sometimes adds a spurious ' to BUILD_TOOL_OPTIONS
        BUILD_TOOL_OPTIONS="-j 4"
      fi
      cmake -DCMAKE_BUILD_TYPE=${BUILD_TYPE} \
            -DCMAKE_INSTALL_PREFIX=install \
            -DCMAKE_PREFIX_PATH="${prefix_path//:/;}" \
            -DANALYZER=${ANALYZER:-off} \
            -DSANITIZER=${SANITIZER:-none} \
            -DENABLE_SSL=${SSL:-on} \
            -DENABLE_SECURITY=${SECURITY:-on} \
            -DENABLE_LIFESPAN=${LIFESPAN:-on} \
            -DENABLE_DEADLINE_MISSED=${DEADLINE_MISSED:-on} \
            -DSKIP_DEADLINE_UPDATE={DEADLINE_UPDATE_SKIP:-off} \
            -DENABLE_TYPELIB=${TYPELIB:-on} \
            -DENABLE_TYPE_DISCOVERY=${TYPE_DISCOVERY:-on} \
            -DENABLE_TOPIC_DISCOVERY=${TOPIC_DISCOVERY:-on} \
            -DENABLE_COVERAGE=${COVERAGE:-off} \
            -DENABLE_SHM=${ICEORYX:-off} \
            -DBUILD_TESTING=${TESTING:-on} \
            -DBUILD_IDLC_XTESTS=${IDLC_XTESTS:-on} \
            -DBUILD_EXAMPLES=on \
            -DWERROR=on \
            ${GENERATOR:+-G} "${GENERATOR}" -A "${PLATFORM}" -T "${TOOLSET}" ..
      ${SCAN_BUILD} cmake --build . --config ${BUILD_TYPE} --target install -- ${BUILD_TOOL_OPTIONS}
      cmake --build . --config ${BUILD_TYPE} --target package -- ${BUILD_TOOL_OPTIONS}
    name: script
  - bash: |
      set -e -x
      cd build
      UBSAN_OPTIONS=print_stacktrace=1:halt_on_error=1 \
        ctest -j 4 --output-on-failure -T test -E '^CUnit_ddsrt_random_default_random$' -C ${BUILD_TYPE}
      [[ "${BUILD_TYPE}" != 'Release' ]] || [[ "${COVERAGE:-off}" == 'off' ]] || \
        ${SHELL} ../src/tools/ddsperf/sanity.bash
    condition: ne(variables['testing'], 'off')
    name: test
    displayName: Test
  - bash: |
      set -e -x
      cd build
      [[ "${AGENT_OS}" == 'Darwin' ]] && BUILD_TOOL_OPTIONS="-j 4"
      [[ "${SANITIZER:-none}" == 'none' ]] || \
        export CFLAGS="-fsanitize=${SANITIZER}" LDFLAGS="-fsanitize=${SANITIZER}"
      INSTALLPREFIX="$(pwd)/install"
      mkdir helloworld
      cd helloworld
      cmake -DCMAKE_PREFIX_PATH="${INSTALLPREFIX}" \
            -DCMAKE_BUILD_TYPE=${BUILD_TYPE} \
            ${GENERATOR:+-G} "${GENERATOR}" -A "${PLATFORM}" -T "${TOOLSET}" "${INSTALLPREFIX}/share/CycloneDDS/examples/helloworld"
      cmake --build . --config ${BUILD_TYPE} -- ${BUILD_TOOL_OPTIONS}
      ${SHELL} ../../scripts/sanity_helloworld.bash
    name: sanity_hello_world
    displayName: Sanity check Hello World
  - bash: |
      set -e -x
      cd build
      x=symbol_export_test
      d=bin
      [ -d bin/${BUILD_TYPE} ] && d=bin/${BUILD_TYPE}
      test -e $d/symbol_export_test
      [[ "${BUILD_TYPE}" == 'Release' ]] || [[ "${AGENT_OS}" == 'Windows_NT' ]] || \
        ${SHELL} ../scripts/check_symbol_export.bash $d/symbol_export_test
    condition: eq(variables['testing'], 'off')
    name: sanity_symbol_export
    displayName: Sanity check export symbols
  - bash: |
      set -e -x
      cd build
      [[ "${AGENT_OS}" == 'Darwin' ]] && BUILD_TOOL_OPTIONS="-j 4"
      cmake --build . --config ${BUILD_TYPE} --target gcov -- ${BUILD_TOOL_OPTIONS}
      gcovr --exclude '.*/tests/.*' --root "${BUILD_SOURCESDIRECTORY}" --json "$(System.DefaultWorkingDirectory)/coverage-$(Agent.JobName).json"
    name: generate_code_coverage
    displayName: Generate coverage artifact
    condition: and(succeededOrFailed(), eq(variables['coverage'], 'on'))
  - publish: $(System.DefaultWorkingDirectory)/coverage-$(Agent.JobName).json
    artifact: coverage-$(Agent.JobName)
    displayName: Publish coverage artifact
    condition: and(succeededOrFailed(), eq(variables['coverage'], 'on'))
  - task: PublishTestResults@2
    inputs:
      testRunTitle: $(Agent.JobName)
      testRunner: CTest
      testResultsFiles: '**/Test.xml'
      searchFolder: $(System.DefaultWorkingDirectory)/build/Testing
      platform: $(arch)
      configuration: $(build_type)
    name: publish_test_results
    displayName: Publish test results
    condition: and(succeededOrFailed(), or(ne(variables['testing'], 'off'), ne(variables['idlc_xtests'], 'off')))
