name: Build and Test Device Detection C++

pr:
- master
- develop

trigger: none

stages:

- stage: Data
  jobs:
  - job: Get_Enterprise_DataFile
    pool:
      name: Hosted Ubuntu 1604

    steps:
    
    - script: |
        curl "https://distributor.51degrees.com/api/v2/download?LicenseKeys=$(licensekey)&Type=HashV41&Download=True&Product=V4Enterprise" --output Enterprise-HashV41.hash.gz
      displayName: 'Download Latest Data File'

    - task: ExtractFiles@1
      displayName: 'Extract Data File'
      inputs:
        archiveFilePatterns: 'Enterprise-HashV41.hash.gz'
        cleanDestinationFolder: false

    - script: |
          mv $(Build.SourcesDirectory)/Enterprise-HashV41.hash  $(Build.ArtifactStagingDirectory)/51Degrees-EnterpriseV4.1.hash
      displayName: 'Move Data File'

    - task: PublishBuildArtifacts@1
      inputs:
        PathtoPublish: '$(Build.ArtifactStagingDirectory)'
        ArtifactName: 'enterpriseFile'
        publishLocation: 'Container'


- stage: Windows
  dependsOn: [Data]
  pool:
    name: Hosted VS2017
    demands:
    - msbuild
    - vstest

  jobs:
 
  - job: Build_VisualStudio  
      
    strategy:
      maxParallel: 1
      matrix:
        debug x86 multi threads:
          BuildConfiguration: Debug
          BuildPlatform: x86
          BuildDirectory: Win32/Debug
        release x86 multi threads:
          BuildConfiguration: Release 
          BuildPlatform: x86
          BuildDirectory: Win32/Release
        debug x64 multi threads:
          BuildConfiguration: Debug
          BuildPlatform: x64
          BuildDirectory: x64/Debug
        release x64 multi threads:
          BuildConfiguration: Release
          BuildPlatform: x64 
          BuildDirectory: x64/Release
        ebug x86 single thread:
          BuildConfiguration: Debug-Single
          BuildPlatform: x86
          BuildDirectory: Win32/Debug
        release x86 single thread:
          BuildConfiguration: Release-Single
          BuildPlatform: x86
          BuildDirectory: Win32/Release
        debug x64 single thread:
          BuildConfiguration: Debug-Single
          BuildPlatform: x64
          BuildDirectory: x64/Debug
        release x64 single thread:
          BuildConfiguration: Release-Single
          BuildPlatform: x64 
          BuildDirectory: x64/Release

    timeoutInMinutes: 240

    steps:

    - checkout: self
      submodules: recursive
      lfs: true
      persistCredentials: true

    - powershell: |
        git lfs install
        ls
        git -c http.https://51degrees.visualstudio.com.extraheader="AUTHORIZATION: bearer $(System.AccessToken)" lfs fetch
        git -c http.https://51degrees.visualstudio.com.extraheader="AUTHORIZATION: bearer $(System.AccessToken)" lfs checkout
        ls
      workingDirectory: 'device-detection-data'
      displayName: 'Fetch LFS Data Files'

    - task: DownloadBuildArtifacts@0
      displayName: 'Download Enterprise Data File'
      inputs:
        buildType: 'current'
        downloadType: 'single'
        artifactName: 'enterpriseFile'
        downloadPath: '$(System.ArtifactsDirectory)'

    - script: |
          mv $(System.ArtifactsDirectory)\enterpriseFile\51Degrees-EnterpriseV4.1.hash  $(Build.SourcesDirectory)\device-detection-data\51Degrees-EnterpriseV4.1.hash
      displayName: 'Move Data File'

    - task: NuGetCommand@2
      inputs:
        command: 'restore'
        restoreSolution: 'VisualStudio/DeviceDetection.sln'
        feedsToUse: 'select'
        vstsFeed: 'd2431f86-c1e6-4d8b-8d27-311cf3614847'

    - task: MSBuild@1
      displayName: 'Build Visual Studio Solution'
      inputs:
        solution: 'VisualStudio/DeviceDetection.sln'
        msbuildArchitecture: x64
        platform: '$(BuildPlatform)'
        configuration: '$(BuildConfiguration)'
        clean: true

    - bash: './FiftyOne.DeviceDetection.Hash.Tests.exe --gtest_filter="-*TestOfflineProcessing*" --gtest_catch_exceptions=1 --gtest_break_on_failure=0 --gtest_output=xml:testoutput.xml'
      workingDirectory: VisualStudio/FiftyOne.DeviceDetection.Hash.Tests/$(BuildDirectory)
      failOnStderr: true
      displayName: 'Run Hash Tests'

    - task: PublishTestResults@2
      condition: true
      inputs:
        testResultsFormat: 'JUnit'
        testRunTitle: 'Visual Studio $(BuildPlatform) $(BuildConfiguration)'
        testResultsFiles: 'VisualStudio\FiftyOne.DeviceDetection.Hash.Tests\$(BuildDirectory)\testoutput.xml'

  - job: Build_And_Test_CMake

    timeoutInMinutes: 240

    strategy:
      maxParallel: 1
      matrix:
        debug x86 multi threads:
          BuildConfiguration: Debug
          BuildPlatform: Win32
          BuildArguments: -DNoThreading=OFF
        release x86 multi threads:
          BuildConfiguration: Release 
          BuildPlatform: Win32
          BuildArguments: -DNoThreading=OFF
        debug x64 multi threads:
          BuildConfiguration: Debug
          BuildPlatform: x64
          BuildArguments: -DNoThreading=OFF
        release x64 multi threads:
          BuildConfiguration: Release  
          BuildPlatform: x64
          BuildArguments: -DNoThreading=OFF
        debug x86 single thread:
          BuildConfiguration: Debug
          BuildPlatform: Win32
          BuildArguments: -DNoThreading=ON
        release x86 single thread:
          BuildConfiguration: Release 
          BuildPlatform: Win32
          BuildArguments: -DNoThreading=ON
        debug x64 single thread:
          BuildConfiguration: Debug
          BuildPlatform: x64
          BuildArguments: -DNoThreading=ON
        release x64 single thread:
          BuildConfiguration: Release  
          BuildPlatform: x64
          BuildArguments: -DNoThreading=ON

    steps:

    - checkout: self
      submodules: recursive
      lfs: true
      persistCredentials: true

    - powershell: |
        git lfs install
        ls
        git -c http.https://51degrees.visualstudio.com.extraheader="AUTHORIZATION: bearer $(System.AccessToken)" lfs fetch
        git -c http.https://51degrees.visualstudio.com.extraheader="AUTHORIZATION: bearer $(System.AccessToken)" lfs checkout
        ls
      workingDirectory: 'device-detection-data'
      displayName: 'Fetch LFS Data Files'

    - task: Bash@3
      displayName: 'Create Build Directory'
      inputs:
        targetType: 'inline'
        script: 'mkdir build'

    - task: DownloadBuildArtifacts@0
      displayName: 'Download Enterprise Data File'
      inputs:
        buildType: 'current'
        downloadType: 'single'
        artifactName: 'enterpriseFile'
        downloadPath: '$(System.ArtifactsDirectory)'

    - script: |
          mv $(System.ArtifactsDirectory)\enterpriseFile\51Degrees-EnterpriseV4.1.hash  $(Build.SourcesDirectory)\device-detection-data\51Degrees-EnterpriseV4.1.hash
      displayName: 'Move Data File'

    - task: CMake@1
      displayName: 'CMake Configure'
      inputs:
        workingDirectory: build
        cmakeArgs: '.. -A $(BuildPlatform) $(BuildArguments)'

    - task: CMake@1
      displayName: 'CMake Build'
      inputs:
        workingDirectory: build
        cmakeArgs: '--build . --config $(BuildConfiguration)'

    - bash: 'ctest -R ".*Hash.*" -E ".*TestOfflineProcessing.*" -T test --no-compress-output'
      workingDirectory: build
      failOnStderr: true
      displayName: 'Run Tests'

    - task: PublishTestResults@2
      condition: true
      inputs:
        testResultsFormat: 'CTest'
        testRunTitle: 'Windows CTest $(BuildPlatform) $(BuildConfiguration) $(BuildArguments)'
        testResultsFiles: '**/Test.xml'


- stage: Linux
  dependsOn: [Data]
  jobs:
  - job: Build_And_Test

    timeoutInMinutes: 240

    pool:
      name: Hosted Ubuntu 1604
      
    strategy:
      matrix:
        debug x86 multi threads:
          BuildConfiguration: Debug
          BuildPlatform: x86
          BuildArguments: -D32bit=ON -DNoThreading=OFF
        release x86 multi threads:
          BuildConfiguration: Release 
          BuildPlatform: x86
          BuildArguments: -D32bit=ON -DNoThreading=OFF
        debug x64 multi threads:
          BuildConfiguration: Debug
          BuildPlatform: x64
          BuildArguments: -DNoThreading=OFF
        release x64 multi threads:
          BuildConfiguration: Release 
          BuildPlatform: x64
          BuildArguments: -DNoThreading=OFF
        debug x86 single thread:
          BuildConfiguration: Debug
          BuildPlatform: x86
          BuildArguments: -D32bit=ON -DNoThreading=ON
        release x86 single thread:
          BuildConfiguration: Release  
          BuildPlatform: x86
          BuildArguments: -D32bit=ON -DNoThreading=ON
        debug x64 single thread:
          BuildConfiguration: Debug
          BuildPlatform: x64
          BuildArguments: -DNoThreading=ON
        release x64 single thread:
          BuildConfiguration: Release  
          BuildPlatform: x64
          BuildArguments: -DNoThreading=ON

    steps:

    - checkout: self
      submodules: recursive
      lfs: true

    - script: |
        ulimit -c unlimited
        echo "cat /proc/sys/kernel/core_pattern"
        cat /proc/sys/kernel/core_pattern
        echo "cat /proc/sys/kernel/core_uses_pid"
        cat /proc/sys/kernel/core_uses_pid
        echo "sudo systemctl enable apport.service"
        sudo systemctl enable apport.service
        echo "sysctl -w kernel.core_pattern=core.%p"
        sudo sysctl -w kernel.core_pattern="core.%p"
        cat /proc/sys/kernel/core_pattern
        sudo apt-get -y install \
          gcc-multilib \
          g++-multilib        
      displayName: 'Install Required Packages'
      
    - script: |
        wget http://ftp.br.debian.org/debian/pool/main/d/dmalloc/libdmalloc-dev_5.5.2-6_i386.deb
        sudo dpkg --add-architecture i386
        sudo apt-get -y update
        sudo dpkg -i libdmalloc-dev_5.5.2-6_i386.deb
        sudo sudo apt-get -y install -f
      displayName: 'Install Required 32bit Packages'
      condition: and(succeeded(), eq(variables['BuildPlatform'], 'x86'))
      
    - script: |
        sudo apt-get -y install libdmalloc-dev
      displayName: 'Install Required 64bit Packages'
      condition: and(succeeded(), eq(variables['BuildPlatform'], 'x64'))

    - task: DownloadBuildArtifacts@0
      displayName: 'Download Enterprise Data File'
      inputs:
        buildType: 'current'
        downloadType: 'single'
        artifactName: 'enterpriseFile'
        downloadPath: '$(System.ArtifactsDirectory)'

    - script: |
          mv $(System.ArtifactsDirectory)/enterpriseFile/51Degrees-EnterpriseV4.1.hash  $(Build.SourcesDirectory)/device-detection-data/51Degrees-EnterpriseV4.1.hash
      displayName: 'Move Data File'

    - task: Bash@3
      displayName: 'Create Build Directory'
      inputs:
        targetType: 'inline'
        script: 'mkdir build'

    - task: CMake@1
      displayName: 'CMake Configure'
      inputs:
        workingDirectory: build
        cmakeArgs: '.. -DCMAKE_BUILD_TYPE=$(BuildConfiguration) $(BuildArguments)'

    - task: CMake@1
      displayName: 'CMake Build'
      inputs:
        workingDirectory: build
        cmakeArgs: '--build .'

    - bash: 'ctest -E ".*TestOfflineProcessing.*" -T test --no-compress-output'
      workingDirectory: build
      failOnStderr: true
      displayName: 'Run Tests'
      
    - task: CopyFiles@2
      displayName: 'Copy Files to: $(build.artifactstagingdirectory)'
      inputs:
        SourceFolder: '$(build.sourcesdirectory)'
        Contents: '**'
        TargetFolder: '$(build.artifactstagingdirectory)'
        CleanTargetFolder: true
      condition: failed()
      
    - task: PublishBuildArtifacts@1
      inputs:
        pathtoPublish: '$(Build.ArtifactStagingDirectory)'
        artifactName: 'drop'
      condition: failed()

    - task: PublishTestResults@2
      condition: true
      inputs:
        testResultsFormat: 'CTest'
        testRunTitle: 'Linux CTest $(BuildConfiguration) $(BuildArguments)'
        testResultsFiles: '**/Test.xml'


- stage: MacOS
  dependsOn: [Data]
  jobs:
  - job: Build_And_Test

    timeoutInMinutes: 240

    pool:
      vmImage: macOS-10.14
      
    strategy:
      matrix:
        debug x64 multi threads:
          BuildConfiguration: Debug
          BuildPlatform: x64
          BuildArguments: -DNoThreading=OFF
        release x64 multi threads:
          BuildConfiguration: Release 
          BuildPlatform: x64
          BuildArguments: -DNoThreading=OFF
        debug x64 single thread:
          BuildConfiguration: Debug
          BuildPlatform: x64
          BuildArguments: -DNoThreading=ON
        release x64 single thread:
          BuildConfiguration: Release  
          BuildPlatform: x64
          BuildArguments: -DNoThreading=ON

    steps:

    - checkout: self
      submodules: recursive
      lfs: true
      
    - script: |
        brew search dmalloc
        brew install dmalloc
      displayName: 'Install Required Packages'

    - task: DownloadBuildArtifacts@0
      displayName: 'Download Enterprise Data File'
      inputs:
        buildType: 'current'
        downloadType: 'single'
        artifactName: 'enterpriseFile'
        downloadPath: '$(System.ArtifactsDirectory)'

    - script: |
          mv $(System.ArtifactsDirectory)/enterpriseFile/51Degrees-EnterpriseV4.1.hash  $(Build.SourcesDirectory)/device-detection-data/51Degrees-EnterpriseV4.1.hash
      displayName: 'Move Data File'

    - task: Bash@3
      displayName: 'Create Build Directory'
      inputs:
        targetType: 'inline'
        script: 'mkdir build'

    - task: CMake@1
      displayName: 'CMake Configure'
      inputs:
        workingDirectory: build
        cmakeArgs: '.. -DCMAKE_BUILD_TYPE=$(BuildConfiguration) $(BuildArguments)'

    - task: CMake@1
      displayName: 'CMake Build'
      inputs:
        workingDirectory: build
        cmakeArgs: '--build .'

    - bash: 'ctest -E ".*TestOfflineProcessing.*" -T test --no-compress-output'
      workingDirectory: build
      failOnStderr: true
      displayName: 'Run Tests'

    - task: PublishTestResults@2
      condition: true
      inputs:
        testResultsFormat: 'CTest'
        testRunTitle: 'Linux CTest $(BuildConfiguration) $(BuildArguments)'
        testResultsFiles: '**/Test.xml'