# - CMAKE Config file for the MADNESS package
#
# Valid COMPONENTS: world tensor misc tensor linalg tinyxml muparser mra chem
# If MADNESS_FIND_COMPONENTS only contains world, will load the subset of targets
# necessary for MADworld
#
# The following variables are defined:
#  MADNESS_FOUND                - System has the MADNESS package
#  MADNESS_INCLUDE_DIRS         - The MADNESS include directory
#  MADNESS_LIBRARIES            - The MADNESS libraries and their dependencies
#  MADNESS_<COMPONENT>_FOUND    - System has the specified MADNESS COMPONENT
#  MADNESS_<COMPONENT>_LIBRARY  - The MADNESS COMPONENT library
#  MADNESS_F77_INTEGER_SIZE     - The default F77 integer size used for BLAS calls 
#
# Additionally MADNESS exports the following variables from its dependent packages:
# = LAPACK =
#    LAPACK_LIBRARIES           Libraries providing LAPACK and its dependents
# = MPI =
#    MPIEXEC                    Executable for running MPI programs
#    MPIEXEC_NUMPROC_FLAG       Flag to pass to MPIEXEC before giving
#                               it the number of processors to run on
#    MPIEXEC_PREFLAGS           Flags to pass to MPIEXEC directly
#                               before the executable to run.
#    MPIEXEC_POSTFLAGS          Flags to pass to MPIEXEC after other flags

# Set package version
set(MADNESS_VERSION "@MADNESS_VERSION@")

@PACKAGE_INIT@

# ensure that CMAKE_SYSTEM_NAME matches what was used to compile MADNESS
if (NOT CMAKE_SYSTEM_NAME STREQUAL @CMAKE_SYSTEM_NAME@)
  set (ERROR_MESSAGE "CMAKE_SYSTEM_NAME=${CMAKE_SYSTEM_NAME}, does not match CMAKE_SYSTEM_NAME=@CMAKE_SYSTEM_NAME@ used to compile MADNESS")
  set(MADNESS_CMAKE_TOOLCHAIN_FILE "@CMAKE_TOOLCHAIN_FILE@")
  if (MADNESS_CMAKE_TOOLCHAIN_FILE)
    set (ERROR_MESSAGE "${ERROR_MESSAGE}; make sure to use toolchain @CMAKE_TOOLCHAIN_FILE@ to compile your code!")
    message (FATAL_ERROR "${ERROR_MESSAGE}")
  endif(MADNESS_CMAKE_TOOLCHAIN_FILE)
endif()

# determine if only looking for MADworld ... this is a common scenario that loads a subset of targets
set(MADNESS_FIND_WORLD_ONLY FALSE)
list(LENGTH MADNESS_FIND_COMPONENTS MADNESS_FIND_NUM_COMPONENTS)
list(FIND MADNESS_FIND_COMPONENTS world MADNESS_FIND_COMPONENTS_world_idx)
if (MADNESS_FIND_NUM_COMPONENTS EQUAL 1 AND MADNESS_FIND_COMPONENTS_world_idx GREATER -1)
  set(MADNESS_FIND_WORLD_ONLY TRUE)
endif()

# Include library IMPORT targets
if(NOT TARGET madness AND NOT MADNESS_FIND_WORLD_ONLY)
  include("${CMAKE_CURRENT_LIST_DIR}/madness-targets.cmake")
  set(MADNESS_TOP_TARGET_LIBRARY madness)
  set(MADNESS_TARGET_COMPONENT madness)
endif()
if(NOT TARGET MADworld AND MADNESS_FIND_WORLD_ONLY)
  include("${CMAKE_CURRENT_LIST_DIR}/madworld-targets.cmake")
  set(MADNESS_TOP_TARGET_LIBRARY MADworld)
  set(MADNESS_TARGET_COMPONENT world)
endif()

# Set the madness libraries target
set(MADNESS_LIBRARIES ${MADNESS_TOP_TARGET_LIBRARY})

set(MADNESS_SOURCE_DIR "@MADNESS_SOURCE_DIR@")
set(MADNESS_BINARY_DIR "@MADNESS_BINARY_DIR@")

# Set Elemental variables
set(MADNESS_HAS_ELEMENTAL @MADNESS_HAS_ELEMENTAL@)
set(MADNESS_HAS_ELEMENTAL_EMBEDDED @MADNESS_HAS_ELEMENTAL_EMBEDDED@)

set(MADNESS_BUILD_INCLUDE_DIRS "${MADNESS_SOURCE_DIR}/src" "${MADNESS_BINARY_DIR}/src")
set(MADNESS_INSTALL_INCLUDE_DIRS "@PACKAGE_MADNESS_INSTALL_INCLUDEDIR@"
      "@PACKAGE_MADNESS_INSTALL_INCLUDEDIR@/madness/external")

if (MADNESS_HAS_ELEMENTAL)
  if(MADNESS_HAS_ELEMENTAL_EMBEDDED)
    set_and_check(Elemental_SOURCE_DIR "@Elemental_SOURCE_DIR@")
    set_and_check(Elemental_BINARY_DIR "@Elemental_BINARY_DIR@")
    list(APPEND MADNESS_BUILD_INCLUDE_DIRS
        "${Elemental_SOURCE_DIR}/include" "${Elemental_BINARY_DIR}/include"
        "${Elemental_SOURCE_DIR}/external/pmrrr/include"
        "${Elemental_BINARY_DIR}/external/pmrrr/include")
  else(MADNESS_HAS_ELEMENTAL_EMBEDDED)
    set_and_check(Elemental_SOURCE_DIR "@ELEMENTAL_SOURCE_DIR@")
    set_and_check(Elemental_BINARY_DIR "@ELEMENTAL_BINARY_DIR@")
    # elemental package and component name depends on version
    set(ELEMENTAL_PACKAGE_NAME @ELEMENTAL_PACKAGE_NAME@)
    # where to look for elemental cmake config file
    set(${ELEMENTAL_PACKAGE_NAME}_DIR "${Elemental_BINARY_DIR}")
    find_package(${ELEMENTAL_PACKAGE_NAME} REQUIRED
                 COMPONENTS REQUIRED ${ELEMENTAL_PACKAGE_NAME})
    list(APPEND MADNESS_BUILD_INCLUDE_DIRS
        "${Elemental_SOURCE_DIR}/include" "${Elemental_BINARY_DIR}/include"
        "${Elemental_SOURCE_DIR}/external/pmrrr/include"
        "${Elemental_BINARY_DIR}/external/pmrrr/include"
        "${Elemental_SOURCE_DIR}/external/pmrrr/suite_sparse"
        "${Elemental_BINARY_DIR}/external/pmrrr/suite_sparse"
        )
  endif(MADNESS_HAS_ELEMENTAL_EMBEDDED)
endif(MADNESS_HAS_ELEMENTAL)

# define MADNESS_INCLUDE_DIRS according to where we are compiling: MADNESS build tree or outside
# external packages should use MADNESS_BUILD_INCLUDE_DIRS and MADNESS_INSTALL_INCLUDE_DIRS directly
if(CMAKE_CURRENT_LIST_DIR EQUAL MADNESS_BINARY_DIR)
  set(MADNESS_INCLUDE_DIRS "${MADNESS_BUILD_INCLUDE_DIRS}")
else()
  set(MADNESS_INCLUDE_DIRS "${MADNESS_INSTALL_INCLUDE_DIRS}")
endif()

# Set component variables
foreach(_comp ${MADNESS_FIND_COMPONENTS})
  if(TARGET MAD${_comp})
    set(MADNESS_${_comp}_FOUND TRUE)
    set(MADNESS_${_comp}_LIBRARY MAD${_comp})
  else()
    set(MADNESS_${_comp}_FOUND FALSE)
    set(MADNESS_${_comp}_LIBRARY "MADNESS_${_comp}_LIBRARY-NOTFOUND")
  endif()
endforeach()

# Set the Fortran 77 integer size (4 or 8)
set(MADNESS_F77_INTEGER_SIZE @FORTRAN_INTEGER_SIZE@)

check_required_components(${MADNESS_TARGET_COMPONENT})

set(MADNESS_FOUND TRUE)

########### LAPACK ############
set(LAPACK_LIBRARIES "@LAPACK_LIBRARIES@")

########### MPI ############
set(MPIEXEC "@MPIEXEC@")
set(MPIEXEC_NUMPROC_FLAG "@MPIEXEC_NUMPROC_FLAG@")
set(MPIEXEC_PREFLAGS "@MPIEXEC_PREFLAGS@")
set(MPIEXEC_POSTFLAGS "@MPIEXEC_POSTFLAGS@")
