# This cmake script will find Intel MKL or Apple Accelerate BLAS and LAPACK
# libraries, and enable the corresponding functionality in Eigen if found.
#
# After this script is run, the following CMake variables will be defined:
#  BLAS_FOUND - if an appropriate BLAS library is found.
#  BLAS_LIBRARIES - the libraries to be linked to your executable for BLAS
#                   functionality. Since this script automatically enables
#                   Eigen's BLAS support, your linker will error if you don't
#                   link this correctly.
#                   Empty string if nothing is found.
#  LAPACK_FOUND - if an appropriate LAPACK library is found.
#  LAPACK_LIBRARIES - the libraries to be linked to your executable for LAPACK
#                     functionality. Since this script automatically enables
#                     Eigen's LAPACK support, your linker will error if you
#                     don't link this correctly.
#                     Empty string if nothing is found.
#  BLA_VENDOR - the CMake BLAS vendor identifier for the BLAS/LAPACK found
#

cmake_minimum_required(VERSION 3.10)

# BLAS
set(BLAS_LIBRARIES "")
set(BLA_VENDOR Intel10_64lp)
find_package(BLAS QUIET)
if(BLAS_FOUND)
    find_package_message(cBLAS "Found BLAS: ${BLAS_LIBRARIES}" "${BLAS_LIBRARIES}")
    add_definitions(-DEIGEN_USE_BLAS)
else()
    set(BLA_VENDOR Intel10_64ilp)
    find_package(BLAS QUIET)
    if(BLAS_FOUND)
        find_package_message(cBLAS "Found BLAS: ${BLAS_LIBRARIES}" "${BLAS_LIBRARIES}")
        add_definitions(-DEIGEN_USE_BLAS)
    else()
        set(BLA_VENDOR Apple)
        find_package(BLAS QUIET)
        if(BLAS_FOUND)
            find_package_message(cBLAS "Found BLAS: ${BLAS_LIBRARIES}" "${BLAS_LIBRARIES}")
            add_definitions(-DEIGEN_USE_BLAS)
        else()
            set(BLA_VENDOR None)
            
            # WE DO NOT SUPPORT ARBITRARY BLAS ANYMORE
            unset(BLA_VENDOR)
            find_package(BLAS QUIET)
            if(BLAS_FOUND)
                find_package_message(cBLAS "Found BLAS: ${BLAS_LIBRARIES}" "${BLAS_LIBRARIES}")
                add_definitions(-DEIGEN_USE_BLAS)
            else()
                set(BLA_VENDOR None)
            endif()
        endif()
    endif()
endif()

# LAPACK
if(BLAS_FOUND)
    set(LAPACK_LIBRARIES "")
    find_package(LAPACK QUIET)

    if(${BLA_VENDOR} STREQUAL "Apple")
        set(LAPACKE_NEEDED TRUE)
    else()
        set(LAPACKE_NEEDED FALSE)
    endif()
    if(LAPACKE_NEEDED)
        find_package(LAPACKE QUIET)
    endif()

    if(LAPACK_FOUND AND (NOT LAPACKE_NEEDED))
        find_package_message(cLAPACK "Found LAPACK: ${LAPACK_LIBRARIES}" "${LAPACK_LIBRARIES}")
        add_definitions(-DEIGEN_USE_LAPACKE)
    endif()

    if(LAPACKE_NEEDED)
        if(LAPACK_FOUND AND LAPACKE_FOUND)
            set(LAPACK_LIBRARIES ${LAPACK_LIBRARIES} ${LAPACKE_LIBRARIES})
            find_package_message(cLAPACK "Found LAPACK and LAPACKE: ${LAPACK_LIBRARIES}" "${LAPACK_LIBRARIES}")
            add_definitions(-DEIGEN_USE_LAPACKE)
            #For some reason, the LAPACKE script can clear the BLAS_FOUND variable.
            set(BLAS_FOUND TRUE)
        else()
            set(LAPACK_LIBRARIES "")
            set(LAPACK_FOUND FALSE)
        endif()
    endif()
endif()

