stages:
  - slam_lib
  - ros_wrapping
  - paraview_wrapping

# Default parameters for all jobs
default:
  before_script: &default_before_script_anchor
    # To build SLAM, some dependencies are necessary.
    # If they are not installed system-wide or not visible by CMake, it is
    # necessary to indicate where to find them on each runner.
    # The following variables can be defined in the 'environment' field of the
    # [[runners]] section, defined in the 'config.toml' file.
    - |
      echo "Using following cmake arguments that are optionally defined by the runner:"
      echo "slam_cmake_option_Eigen_DIR=$slam_cmake_option_Eigen_DIR"
      echo "slam_cmake_option_Ceres_DIR=$slam_cmake_option_Ceres_DIR"
      echo "slam_cmake_option_nanoflann_DIR=$slam_cmake_option_nanoflann_DIR"
      echo "slam_cmake_option_Boost_DIR=$slam_cmake_option_Boost_DIR"
      echo "slam_cmake_option_PCL_DIR=$slam_cmake_option_PCL_DIR"
      echo "slam_cmake_option_g2o_DIR=$slam_cmake_option_g2o_DIR"
      echo "slam_cmake_option_ParaView_DIR=$slam_cmake_option_ParaView_DIR"
      echo "slam_cmake_option_Qt5_DIR=$slam_cmake_option_Qt5_DIR"

# Windows specific configuration
.windows_runner_config:
  before_script:
    - *default_before_script_anchor
    # To use the Microsoft C++ Toolset from the command line, some environment variables need to be set.
    # To help with this, Microsoft provides the vcvarsall.bat script that sets different environment vars.
    # (see https://docs.microsoft.com/en-us/cpp/build/building-on-the-command-line?view=vs-2019)
    # However, the script is made for the command shell and the job run in a powershell.
    # A powershell can run a command shell script by using the `cmd` executable, but this implies
    # that environment variables are only set in the child cmd.exe instance. The environment variables need to
    # be updated in the powershell too. This is why the function Invoke-CmdScript is needed as it invokes
    # the script and updates the powershell environment.
    # This is taken from https://stackoverflow.com/a/41399983
    - | # pipe allows to use multiline command, see https://gitlab.com/gitlab-org/gitlab-runner/issues/166
      function Invoke-CmdScript {
        param(
          [String] $scriptName
        )
        $cmdLine = """$scriptName"" $args & set"
        & $Env:SystemRoot\system32\cmd.exe /c $cmdLine |
          Select-String '^([^=]*)=(.*)$' |
          ForEach-Object {
            $varName = $_.Matches[0].Groups[1].Value
            $varValue = $_.Matches[0].Groups[2].Value
            Set-Item Env:$varName $varValue
        }
      }
    - Invoke-CmdScript "${vcvarsall_script_location}/vcvarsall.bat" $architecture
  variables:
    # Paraview only supports MSVC 14 (2015) and Ninja generator yet
    vcvarsall_script_location: 'C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC'
    architecture: x86_amd64
    cmake_option_GENERATOR: -GNinja

# ------------------------------------------------------------------------------
#   Build core LidarSlam lib
# ------------------------------------------------------------------------------

.slam_lib:
  stage: slam_lib
  needs: []
  variables:
    build_dir: $CI_PROJECT_DIR/../${CI_PROJECT_NAME}_build/build_slam_lib
  script:
    - cmake -E remove_directory $build_dir
    - cmake -E make_directory $build_dir
    - cd $build_dir
    - cmake
      $cmake_option_GENERATOR
      -DCMAKE_BUILD_TYPE=Release
      -DBUILD_SHARED_LIBS=ON
      $slam_cmake_option_Eigen_DIR
      $slam_cmake_option_Ceres_DIR
      $slam_cmake_option_nanoflann_DIR
      $slam_cmake_option_Boost_DIR
      $slam_cmake_option_PCL_DIR
      $slam_cmake_option_g2o_DIR
      $CI_PROJECT_DIR
    - cmake --build $build_dir -j 4

# Build core LidarSlam lib on Linux
linux_slam_lib:
  tags: [linux]
  extends: .slam_lib

# Build core LidarSlam lib on Windows
windows_slam_lib:
  tags: [windows]
  extends:
  - .windows_runner_config
  - .slam_lib

# ------------------------------------------------------------------------------
#   Build ROS wrapping
# ------------------------------------------------------------------------------

.ros_wrapping:
  stage: ros_wrapping
  needs: []
  variables:
    build_dir: $CI_PROJECT_DIR/../${CI_PROJECT_NAME}_build/build_ros_wrapping
  script:
    - cmake -E remove_directory $build_dir
    - cmake -E make_directory $build_dir/src
    - cmake -E create_symlink $CI_PROJECT_DIR $build_dir/src/slam
    - cd $build_dir
    - catkin_make
      -DCMAKE_BUILD_TYPE=Release
      $slam_cmake_option_Eigen_DIR
      $slam_cmake_option_Ceres_DIR
      $slam_cmake_option_nanoflann_DIR
      $slam_cmake_option_Boost_DIR
      $slam_cmake_option_PCL_DIR
      $slam_cmake_option_g2o_DIR
      -j 4

# Build ROS wrapping on Linux
linux_ros_wrapping:
  tags: [linux, ros]
  extends: .ros_wrapping

# ------------------------------------------------------------------------------
#   Build PV wrapping
# ------------------------------------------------------------------------------

.paraview_wrapping:
  stage: paraview_wrapping
  needs: []
  variables:
    build_dir: $CI_PROJECT_DIR/../${CI_PROJECT_NAME}_build/build_paraview_wrapping
  script:
    - cmake -E remove_directory $build_dir
    - cmake -E make_directory $build_dir
    - cd $build_dir
    - cmake
      $cmake_option_GENERATOR
      -DCMAKE_BUILD_TYPE=Release
      -DBUILD_SHARED_LIBS=OFF
      -DSLAM_PARAVIEW_PLUGIN=ON
      $slam_cmake_option_Eigen_DIR
      $slam_cmake_option_Ceres_DIR
      $slam_cmake_option_nanoflann_DIR
      $slam_cmake_option_Boost_DIR
      $slam_cmake_option_PCL_DIR
      $slam_cmake_option_g2o_DIR
      $slam_cmake_option_ParaView_DIR
      $slam_cmake_option_Qt5_DIR
      $CI_PROJECT_DIR
    - cmake --build $build_dir -j 4

# Build PV wrapping on Linux
linux_paraview_wrapping:
  tags: [linux, paraview]
  extends: .paraview_wrapping

# Build PV wrapping on Windows
windows_paraview_wrapping:
  tags: [windows, paraview]
  extends:
    - .windows_runner_config
    - .paraview_wrapping
