function(detect_compiler COMPILER REQUIRED_VERSION)
  set(VERSION_STRING ${REQUIRED_VERSION})
  set(COMPILER_STRING ${COMPILER})

  if(${COMPILER} STREQUAL "GNU")
    set(COMPILER_STRING "GCC")
  endif()

  if(${COMPILER} STREQUAL "Clang")
    option(USE_LIBCXX OFF)
    if(USE_LIBCXX)
      set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++" PARENT_SCOPE)
    endif()
  endif()

  if(MSVC)
    if(REQUIRED_VERSION EQUAL 2015)
      set(REQUIRED_VERSION 19)
    elseif(REQUIRED_VERSION EQUAL 2013)
      set(REQUIRED_VERSION 18)
    elseif(REQUIRED_VERSION EQUAL 2012)
      set(REQUIRED_VERSION 17)
    elseif(REQUIRED_VERSION EQUAL 2010)
      set(REQUIRED_VERSION 16)
    elseif(REQUIRED_VERSION EQUAL 2008)
      set(REQUIRED_VERSION 15)
    else()
      message(FATAL_ERROR "Unknown required MSVC version: ${VERSION_STRING}")
    endif()
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /D_CRT_SECURE_NO_WARNINGS /D_SCL_SECURE_NO_WARNINGS" PARENT_SCOPE)
  endif()

  if(CMAKE_CXX_COMPILER_VERSION VERSION_LESS ${REQUIRED_VERSION})
    message(FATAL_ERROR "${COMPILER_STRING} version need to be at leaset ${VERSION_STRING}")
  endif()

#  if(NOT MSVC AND NOT (${CMAKE_BUILD_TYPE} STREQUAL "Debug"))
  if(CMAKE_BUILD_TYPE)
    if(NOT (${CMAKE_BUILD_TYPE} STREQUAL "Debug"))
      set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3 -DNDEBUG" PARENT_SCOPE)
    endif()
  endif()
endfunction()

function(find_compiler)
  foreach(ver_idx RANGE 1 ${ARGC} 3)
    math(EXPR com_idx "${ver_idx}-1")
    math(EXPR arg_idx "${ver_idx}+1")

    list(GET ARGV ${com_idx} COMPILER)
    list(GET ARGV ${ver_idx} REQUIRED_VERSION)
    list(GET ARGV ${arg_idx} FLAGS)

    if(${CMAKE_CXX_COMPILER_ID} STREQUAL ${COMPILER})
      detect_compiler(${COMPILER} ${REQUIRED_VERSION})
      set(COMPILER_FOUND TRUE)
      set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${FLAGS}" PARENT_SCOPE)
    endif()
  endforeach()
  if(NOT COMPILER_FOUND)
    message(FATAL_ERROR "${CMAKE_CXX_COMPILER_ID} is not supported")
  endif()
endfunction()

function(add_default_lib_path LIBS_DIR PLATFORM)
  include_directories(${LIBS_DIR}/clany/include)

  set(BOOST_INCLUDEDIR   ${LIBS_DIR}/Boost/include         PARENT_SCOPE)
  set(BOOST_LIBRARYDIR   ${LIBS_DIR}/Boost/${PLATFORM}/lib PARENT_SCOPE)
  set(TBB_INSTALL_DIR    ${LIBS_DIR}/TBB                   PARENT_SCOPE)
  set(TBB_ARCH_PLATFORM  ${PLATFORM}                       PARENT_SCOPE)
  set(EIGEN3_INCLUDE_DIR ${LIBS_DIR}/Eigen                 PARENT_SCOPE)
  set(OpenCV_DIR         ${LIBS_DIR}/OpenCV                PARENT_SCOPE)
  set(FFTW_ROOT          ${LIBS_DIR}/FFTW                  PARENT_SCOPE)
  set(OPENSSL_ROOT_DIR   ${LIBS_DIR}/OpenSSL               PARENT_SCOPE)
endfunction()
