set (KOKKOS_INSTALLATION_NEEDED FALSE CACHE LOGICAL "")
if (NOT DEFINED E3SM_KOKKOS_PATH)
  # Build kokkos submodule if user did not specify KOKKOS_PATH.
  set (KOKKOS_INSTALLATION_NEEDED TRUE)
  set (KOKKOS_SRC ${CMAKE_SOURCE_DIR}/../../externals/kokkos)
  set (KOKKOS_BUILD_DIR ${CMAKE_BINARY_DIR}/kokkos/build)
  SET (KOKKOS_INCLUDE_DIR ${KOKKOS_SRC}/core/src ${KOKKOS_BUILD_DIR})
  SET (KOKKOS_LIBRARY_DIR ${KOKKOS_BUILD_DIR})
  message ("Kokkos will be built internally to E3SM, in ${KOKKOS_BUILD_DIR}")
else ()
  message ("The installation of kokkos in ${E3SM_KOKKOS_PATH} will be used.")
  SET (KOKKOS_INCLUDE_DIR ${KOKKOS_PATH}/include)
  SET (KOKKOS_LIBRARY_DIR ${KOKKOS_PATH}/lib)
endif ()

SET (KOKKOS_LIBRARIES "kokkos")
SET (KOKKOS_TPL_LIBRARIES "dl")

MESSAGE (STATUS "Kokkos installation found! Here are the details:")
MESSAGE ("    KOKKOS_INCLUDE_DIR: ${KOKKOS_INCLUDE_DIR}")
MESSAGE ("    KOKKOS_LIBRARY_DIR: ${KOKKOS_LIBRARY_DIR}")
MESSAGE ("    KOKKOS_LIBRARIES:   ${KOKKOS_LIBRARIES}")

macro(install_kokkos_if_needed)
  if (KOKKOS_INSTALLATION_NEEDED)
    if (ENABLE_OPENMP)
      set (KOKKOS_ENABLE_OPENMP TRUE)
    endif ()
    add_subdirectory (${KOKKOS_SRC} ${KOKKOS_LIBRARY_DIR})
  endif ()
endmacro()

macro(link_to_kokkos targetName)
  INCLUDE_DIRECTORIES("${KOKKOS_INCLUDE_DIR}")
  TARGET_LINK_LIBRARIES(${targetName} ${KOKKOS_TPL_LIBRARIES} ${KOKKOS_LIBRARIES} -L${KOKKOS_LIBRARY_DIR})
endmacro(link_to_kokkos)
