#
# This is a CMake makefile.  You can find the cmake utility and
# information about it at http://www.cmake.org
#
#
# This cmake file tries to find an installed BLAS library.  It looks
# for an installed copy of the Intel MKL library first and then
# attempts to find some other BLAS library.
#
#  blas_found        - True if BLAS is available
#  blas_libraries    - link against these to use BLAS library 

# setting this makes CMake allow normal looking if else statements
SET(CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS true)

SET(blas_found 0)


if (UNIX)
    message(STATUS "Searching for a BLAS library")

    include(CheckTypeSize)
    check_type_size( "void*" SIZE_OF_VOID_PTR)

    if (SIZE_OF_VOID_PTR EQUAL 8)
        set( mkl_search_path
            /opt/intel/mkl/*/lib/em64t
            )
	else()
        set( mkl_search_path
            /opt/intel/mkl/*/lib/32
            )
    endif()

   include(CheckLibraryExists)

   # Search for the needed libraries from the MKL
   find_library(mkl_mkl mkl ${mkl_search_path})
   find_library(mkl_core mkl_core ${mkl_search_path})
   find_library(mkl_guide guide ${mkl_search_path})

   #MKL also needs pthreads so search for that as well
   find_library(mkl_pthread pthread ${mkl_search_path})

   mark_as_advanced( mkl_mkl mkl_core mkl_guide mkl_pthread)

   
   # if we found the MKL 
   if (mkl_mkl AND mkl_core AND mkl_guide AND mkl_pthread)
      set(blas_libraries ${mkl_mkl} ${mkl_core} ${mkl_guide} ${mkl_pthread})
      set(blas_found 1)
      message(STATUS "Found Intel MKL BLAS library")
   endif()

   
   # try to find some other BLAS libraries if we didn't find the MKL

   if (NOT blas_found)
      find_library(cblas_lib cblas)
      if (cblas_lib)
         set(blas_libraries ${cblas_lib})
         set(blas_found 1)
         message(STATUS "Found CBLAS library")
      endif()
      mark_as_advanced( cblas_lib)
   endif()

   
   if (NOT blas_found)
      find_library(atlas_lib atlas)
      if (atlas_lib)
         set(blas_libraries ${atlas_lib})
         set(blas_found 1)
         message(STATUS "Found ATLAS BLAS library")
      endif()
      mark_as_advanced( atlas_lib)
   endif()

   
   if (NOT blas_found)
      find_library(generic_blas blas)
      if (generic_blas)
         set(blas_libraries ${generic_blas})
         set(blas_found 1)
         message(STATUS "Found BLAS library")
      endif()
      mark_as_advanced( generic_blas)
   endif()

   if (NOT blas_found)
      message(STATUS "***** No BLAS library found *****")
   endif()

endif()



