1if (GGML_STATIC)
  2    set(BLA_STATIC ON)
  3endif()
  4#if (CMAKE_VERSION VERSION_GREATER_EQUAL 3.22)
  5#    set(BLA_SIZEOF_INTEGER 8)
  6#endif()
  7
  8set(BLA_VENDOR ${GGML_BLAS_VENDOR})
  9find_package(BLAS)
 10
 11if (BLAS_FOUND)
 12    message(STATUS "BLAS found, Libraries: ${BLAS_LIBRARIES}")
 13
 14    ggml_add_backend_library(ggml-blas
 15                             ggml-blas.cpp
 16                            )
 17
 18    if (${GGML_BLAS_VENDOR} MATCHES "Apple")
 19        add_compile_definitions(ACCELERATE_NEW_LAPACK)
 20        add_compile_definitions(ACCELERATE_LAPACK_ILP64)
 21        add_compile_definitions(GGML_BLAS_USE_ACCELERATE)
 22    elseif ("${BLAS_INCLUDE_DIRS}" STREQUAL "")
 23        # BLAS_INCLUDE_DIRS is missing in FindBLAS.cmake.
 24        # see https://gitlab.kitware.com/cmake/cmake/-/issues/20268
 25        find_package(PkgConfig REQUIRED)
 26        if (${GGML_BLAS_VENDOR} MATCHES "Generic")
 27            pkg_check_modules(DepBLAS blas)
 28        elseif (${GGML_BLAS_VENDOR} MATCHES "OpenBLAS")
 29            # As of openblas v0.3.22, the 64-bit is named openblas64.pc
 30            pkg_check_modules(DepBLAS openblas64)
 31            if (NOT DepBLAS_FOUND)
 32                pkg_check_modules(DepBLAS openblas)
 33            endif()
 34        elseif (${GGML_BLAS_VENDOR} MATCHES "FLAME")
 35            pkg_check_modules(DepBLAS blis)
 36        elseif (${GGML_BLAS_VENDOR} MATCHES "ATLAS")
 37            pkg_check_modules(DepBLAS blas-atlas)
 38        elseif (${GGML_BLAS_VENDOR} MATCHES "FlexiBLAS")
 39            pkg_check_modules(DepBLAS flexiblas_api)
 40        elseif (${GGML_BLAS_VENDOR} MATCHES "Intel")
 41            # all Intel* libraries share the same include path
 42            pkg_check_modules(DepBLAS mkl-sdl)
 43        elseif (${GGML_BLAS_VENDOR} MATCHES "NVHPC")
 44            # this doesn't provide pkg-config
 45            # suggest to assign BLAS_INCLUDE_DIRS on your own
 46            if ("${NVHPC_VERSION}" STREQUAL "")
 47                message(WARNING "Better to set NVHPC_VERSION")
 48            else()
 49                set(DepBLAS_FOUND ON)
 50                set(DepBLAS_INCLUDE_DIRS "/opt/nvidia/hpc_sdk/${CMAKE_SYSTEM_NAME}_${CMAKE_SYSTEM_PROCESSOR}/${NVHPC_VERSION}/math_libs/include")
 51            endif()
 52        endif()
 53        if (DepBLAS_FOUND)
 54            set(BLAS_INCLUDE_DIRS ${DepBLAS_INCLUDE_DIRS})
 55        else()
 56            message(WARNING "BLAS_INCLUDE_DIRS neither been provided nor been automatically"
 57            " detected by pkgconfig, trying to find cblas.h from possible paths...")
 58            find_path(BLAS_INCLUDE_DIRS
 59                NAMES cblas.h
 60                HINTS
 61                    /usr/include
 62                    /usr/local/include
 63                    /usr/include/openblas
 64                    /opt/homebrew/opt/openblas/include
 65                    /usr/local/opt/openblas/include
 66                    /usr/include/x86_64-linux-gnu/openblas/include
 67            )
 68        endif()
 69    endif()
 70
 71    message(STATUS "BLAS found, Includes: ${BLAS_INCLUDE_DIRS}")
 72
 73    target_compile_options(ggml-blas PRIVATE ${BLAS_LINKER_FLAGS})
 74
 75    if ("${GGML_BLAS_VENDOR}" STREQUAL "")
 76        message(WARNING "GGML_BLAS_VENDOR is not set; some methods may not link properly.")
 77    endif()
 78
 79    if ("${GGML_BLAS_VENDOR}" MATCHES "Intel" OR ("${BLAS_INCLUDE_DIRS}" MATCHES "mkl" AND "${GGML_BLAS_VENDOR}" MATCHES "Generic"))
 80        add_compile_definitions(GGML_BLAS_USE_MKL)
 81    endif()
 82
 83    if ("${GGML_BLAS_VENDOR}" MATCHES "OpenBLAS")
 84        add_compile_definitions(GGML_BLAS_USE_OPENBLAS)
 85    endif()
 86
 87    if ("${GGML_BLAS_VENDOR}" MATCHES "FLAME" OR "${GGML_BLAS_VENDOR}" MATCHES "AOCL" OR "${GGML_BLAS_VENDOR}" MATCHES "AOCL_mt")
 88        add_compile_definitions(GGML_BLAS_USE_BLIS)
 89    endif()
 90
 91    if ("${GGML_BLAS_VENDOR}" MATCHES "NVPL")
 92        add_compile_definitions(GGML_BLAS_USE_NVPL)
 93    endif()
 94
 95    target_link_libraries     (ggml-blas PRIVATE ${BLAS_LIBRARIES})
 96    target_include_directories(ggml-blas SYSTEM PRIVATE ${BLAS_INCLUDE_DIRS})
 97else()
 98    message(FATAL_ERROR "BLAS not found, please refer to "
 99                        "https://cmake.org/cmake/help/latest/module/FindBLAS.html#blas-lapack-vendors"
100                        " to set correct GGML_BLAS_VENDOR")
101endif()