# if("${CUDA_ARCH}" STREQUAL "")
#   include(utils)
#   detect_installed_gpus(LEGION_CUDA_ARCH)
#   message( STATUS "Detected CUDA_ARCH : ${LEGION_CUDA_ARCH}" )
# else()
#   set(LEGION_CUDA_ARCH ${CUDA_ARCH})
# endif()

set(LEGION_NAME legion)

find_package(MPI REQUIRED)

ExternalProject_get_property(${GASNET_NAME} INSTALL_DIR)
set(GASNET_INSTALL_DIR ${INSTALL_DIR})

# TODO default python version
message(STATUS "Building ${LEGION_NAME}")
message(STATUS "Max DIM: ${MAX_DIM}")
message(STATUS "Python Version: ${PYTHON_VERSION}")
ExternalProject_Add(${LEGION_NAME}
 DEPENDS ${GASNET_NAME}
 SOURCE_DIR ${PROJECT_SOURCE_DIR}/legion
 PREFIX ${LEGION_NAME}
 INSTALL_DIR ${LEGION_NAME}/install
 CONFIGURE_COMMAND ${CMAKE_COMMAND}
   -DCMAKE_BUILD_TYPE:STRING=${CMAKE_BUILD_TYPE}
   -DCMAKE_CXX_COMPILER:FILEPATH=${CMAKE_CXX_COMPILER}
   -DCMAKE_CXX_FLAGS:STRING=${CMAKE_CXX_FLAGS}
   -DCMAKE_C_COMPILER:FILEPATH=${CMAKE_C_COMPILER}
   -DCMAKE_C_FLAGS:STRING=${CMAKE_C_FLAGS}
   -DMPI_C_COMPILER:FILEPATH=${MPI_C_COMPILER}
   -DMPI_CXX_COMPILER:FILEPATH=${MPI_CXX_COMPILER}
   -DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
   -DCUDA_USE_STATIC_CUDA_RUNTIME=OFF
   -DGASNet_ROOT_DIR:PATH=${GASNET_INSTALL_DIR}
   -DGASNet_INCLUDE_DIR:PATH=${GASNET_INSTALL_DIR}/include
   -DGASNet_CONDUIT:STRING=${GASNet_CONDUIT}
   ${Legion_GASNet_OPTS}
   -DLegion_USE_GASNet=${ENABLE_GASNET}
   -DLegion_USE_CUDA=ON
   -DLegion_GPU_REDUCTIONS=OFF
   -DLegion_MAX_DIM=${MAX_DIM}
   -DLegion_BUILD_EXAMPLES=OFF
   -DLegion_BUILD_APPS=OFF
   -DLegion_CUDA_ARCH=${CUDA_ARCH}
   -DLegion_USE_Python=${LEGION_USE_PYTHON}
   -DLegion_Python_Version=${PYTHON_VERSION}
   <SOURCE_DIR>
)

ExternalProject_get_property(${LEGION_NAME} INSTALL_DIR)
install(DIRECTORY ${INSTALL_DIR}/ DESTINATION ${CMAKE_INSTALL_PREFIX} USE_SOURCE_PERMISSIONS)
