name: CPP
# It runs default OSes for each PR, push event or a new tag,
# checks basic builds with various compilers and executes all sets of tests.

on:
  push:
  pull_request:
  release:
    types:
      - created

env:
  REPO:           libpmemobj-cpp
  GITHUB_REPO:    pmem/libpmemobj-cpp
  CONTAINER_REG:  ghcr.io/pmem/libpmemobj-cpp
  HOST_WORKDIR:   ${{ github.workspace }}
  TEST_TIMEOUT:   600
  IMG_VER:        latest

jobs:
  linux:
    name: Linux
    runs-on: ubuntu-latest
    env:
      # use org's Private Access Token to log in to GitHub Container Registry
      CONTAINER_REG_USER:   ${{ secrets.GH_CR_USER }}
      CONTAINER_REG_PASS:   ${{ secrets.GH_CR_PAT }}
      FORCE_IMAGE_ACTION:   ${{ secrets.FORCE_IMAGE_ACTION }}
      WORKDIR:         utils/docker
    strategy:
      matrix:
        CONFIG: ["TYPE=debug OS=fedora OS_VER=32 PUSH_IMAGE=1",
                 "TYPE=debug OS=ubuntu OS_VER=20.04 PUSH_IMAGE=1 CHECK_CPP_STYLE=1",
                 "TYPE=debug OS=ubuntu OS_VER=20.04 COVERAGE=1",
                 "TYPE=release OS=fedora OS_VER=32",
                 "TYPE=release OS=ubuntu OS_VER=20.04",
                 "TYPE=valgrind OS=ubuntu OS_VER=20.04",
                 "TYPE=memcheck_drd OS=ubuntu OS_VER=20.04",
                 "TYPE=package OS=fedora OS_VER=32",
                 "TYPE=package OS=ubuntu OS_VER=20.04"]
    steps:
      - name: Get release version
        if: github.event_name == 'release'
        id: get_release_version
        run: echo ::set-output name=VERSION::$(echo ${{ github.event.release.tag_name }} | grep -E "^[0-9]+.[0-9]+(-rc[0-9]+)?$" | cut -f 1,2 -d . | cut -f 1 -d -)

      - name: Set image version and force image action for release
        if: github.event_name == 'release' && steps.get_release_version.outputs.VERSION != ''
        run: |
          echo "IMG_VER=${{ steps.get_release_version.outputs.VERSION }}" >> $GITHUB_ENV
          echo "FORCE_IMAGE_ACTION=rebuild" >> $GITHUB_ENV

      - name: Set image version and force image action for stable branch
        if: startsWith(github.ref, 'refs/heads/stable-')
        run: |
          echo "IMG_VER=$(echo ${GITHUB_REF#refs/heads/} | cut -d - -f 2)" >> $GITHUB_ENV
          echo "FORCE_IMAGE_ACTION=rebuild" >> $GITHUB_ENV

      - name: Clone the git repo
        uses: actions/checkout@v2
        with:
          fetch-depth: 0

      # "pull" or "rebuild" can be passed to a secret FORCE_IMAGE_ACTION to override default action
      - name: Pull the image or rebuild and push it
        run: cd $WORKDIR && ${{ matrix.CONFIG }} ./pull-or-rebuild-image.sh $FORCE_IMAGE_ACTION

      - name: Run the build
        run: cd $WORKDIR && ${{ matrix.CONFIG }} ./build.sh
  doc:
    name: build and publish docs
    runs-on: ubuntu-latest
    needs: linux
    env:
      DOC_UPDATE_GITHUB_TOKEN:   ${{ secrets.DOC_UPDATE_GITHUB_TOKEN }}
      DOC_UPDATE_BOT_NAME:       ${{ secrets.DOC_UPDATE_BOT_NAME }}
      DOC_REPO_OWNER:            ${{ secrets.DOC_REPO_OWNER }}
      WORKDIR:        utils/docker
    if:
      github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/heads/stable-')
    strategy:
      matrix:
        CONFIG: ["TYPE=doc OS=fedora OS_VER=32"]
    steps:
      - name: Clone the git repo
        uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - name: Pull the image
        run: cd $WORKDIR && ${{ matrix.CONFIG }} ./pull-or-rebuild-image.sh pull

      - name: Run the build
        run: cd $WORKDIR && ${{ matrix.CONFIG }} ./build.sh

  windows:
    name: Windows
    runs-on: windows-latest

    env:
      platform: x64
      VCPKG_DEFAULT_TRIPLET: x64-windows
      GENERATOR: "Visual Studio 16 2019"
      ARCH:      "x64"
      PMDK_VERSION: "1.9"
      CMAKE_TOOLCHAIN_FILE: "C:\\vcpkg\\scripts\\buildsystems\\vcpkg.cmake"
      CMAKE_INSTALL_PREFIX: "C:\\install\\libpmemobj-cpp"
      MSBUILD:  "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Enterprise\\MSBuild\\Current\\Bin"
    strategy:
      matrix:
        BUILD_TYPE:   [Debug, Release]
        CXX_STANDARD: [14]
        include:
          - BUILD_TYPE:   RelWithDebInfo
            CXX_STANDARD: 17

    steps:
      - name: Update PATH
        run: echo "${env:MSBUILD}" | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append

      - name: Cache vcpkg packages
        uses: actions/cache@v1
        id: cache-vcpkg-packages
        with:
          path: C:/vcpkg/packages
          key:  vcpkg-packages-45e5a67

      - name: Cache vcpkg installed
        uses: actions/cache@v1
        id: cache-vcpkg-installed
        with:
          path: C:/vcpkg/installed
          key:  vcpkg-installed-45e5a67

      - name: Clone the git repo
        uses: actions/checkout@v2
        with:
          fetch-depth: 0

      - name: Install PMDK
        run: |
          vcpkg install pmdk:x64-windows
          vcpkg install sfml:x64-windows
          vcpkg install tbb:x64-windows
          vcpkg update
          vcpkg upgrade pmdk:x64-windows --no-dry-run

      - name: vcpkg integrate install
        run: vcpkg integrate install

      - name: Configure
        run: cmake . -Bbuild
                    -G "${env:GENERATOR}"
                    -A "${env:ARCH}"
                    -DCMAKE_BUILD_TYPE="${{ matrix.BUILD_TYPE }}"
                    -DCMAKE_TOOLCHAIN_FILE="${env:CMAKE_TOOLCHAIN_FILE}"
                    -DCMAKE_INSTALL_PREFIX="${env:CMAKE_INSTALL_PREFIX}"
                    -DTESTS_USE_FORCED_PMEM=ON
                    -DTESTS_USE_VALGRIND=OFF
                    -DTESTS_TBB=ON
                    -DDEVELOPER_MODE=ON
                    -DCXX_STANDARD="${{ matrix.CXX_STANDARD }}"

      - name: Build
        run: msbuild build/ALL_BUILD.vcxproj /property:Configuration=${{ matrix.BUILD_TYPE }} /verbosity:minimal /m

      - name: Tests
        working-directory: build
        run: ctest -C ${{ matrix.BUILD_TYPE }} --output-on-failure --timeout "${env:TEST_TIMEOUT}"

      - name: Install
        working-directory: build
        run: msbuild INSTALL.vcxproj /verbosity:minimal /m

      - name: Examples
        working-directory: examples/map_cli
        run: |
          cmake . -G "${env:GENERATOR}" -A "${env:ARCH}" -DCMAKE_TOOLCHAIN_FILE="${env:CMAKE_TOOLCHAIN_FILE}" -DCMAKE_PREFIX_PATH="${env:CMAKE_INSTALL_PREFIX}"
          msbuild ALL_BUILD.vcxproj /verbosity:minimal /m
