1# Copyright (c) 2020 - 2024 David Guibert
2# Copyright (c) 2024 - 2025 Antoine Morvan
3# Copyright (c) 2024 - 2025 Niclas Schroeter
6# SPDX-License-Identifier: Apache-2.0
8set(OPENBLAS_BUILD_PARALLEL_LEVEL
10 CACHE STRING "Enable OpenBLAS parallel compilation")
11set_property(GLOBAL PROPERTY openblas_can_use_system TRUE)
12set_property(GLOBAL PROPERTY openblas_depends)
14set(openblas_cmake_args "" CACHE STRING "Custom CMake arguments for OpenBLAS")
15set(openblas_build_args "" CACHE STRING "Custom Make arguments for OpenBLAS")
18 if(USE_SYSTEM_blas OR USE_SYSTEM_openblas)
19 message(STATUS "blas/lapack: enabled (system installed)")
20 # set(BLA_VENDOR Intel10_64lp) set(BLA_F95 ON) #if ON tries to find the
21 # BLAS95/LAPACK95 interfaces
23 find_package(BLAS REQUIRED)
24 # BLAS_FOUND library implementing the BLAS interface is found
25 # BLAS_LINKER_FLAGS uncached list of required linker flags (excluding -l and
26 # -L). BLAS_LIBRARIES uncached list of libraries (using full path name) to
27 # link against to use BLAS (may be empty if compiler implicitly links BLAS)
28 # BLAS95_LIBRARIES uncached list of libraries (using full path name) to link
29 # against to use BLAS95 interface BLAS95_FOUND library implementing the
30 # BLAS95 interface is found
32 # https://cmake.org/cmake/help/latest/module/FindLAPACK.html
33 find_package(LAPACK REQUIRED)
34 # LAPACK_FOUND library implementing the LAPACK interface is found
35 # LAPACK_LINKER_FLAGS uncached list of required linker flags (excluding -l
36 # and -L). LAPACK_LIBRARIES uncached list of libraries (using full path
37 # name) to link against to use LAPACK LAPACK95_LIBRARIES uncached list of
38 # libraries (using full path name) to link against to use LAPACK95
39 # LAPACK95_FOUND library implementing the LAPACK95 interface is found
40 message(STATUS "BLAS: ${BLAS_FOUND}")
41 message(STATUS "LAPACK: ${LAPACK_FOUND}")
43 add_library(openblas UNKNOWN IMPORTED)
45 if(NOT TARGET openblas)
46 message(STATUS "OpenBLAS: enabled (internally built)")
48 if (CMAKE_Fortran_COMPILER_ID STREQUAL "LLVMFlang")
49 set(require_llvm_patch True)
52 ExternalProject_Data_Add(
56 PATCH_COMMAND $<IF:$<NOT:$<BOOL:${require_llvm_patch}>>,true,${Patch_EXECUTABLE} -p1 -i ${CMAKE_SOURCE_DIR}/projects/openblas_llvm.patch>
58 CMAKE_ARGS ${cmake_params}
59 -DCMAKE_INSTALL_LIBDIR:FILEPATH=lib
60 -DBUILD_SHARED_LIBS:BOOL=${BUILD_SHARED_LIBS}
61 -DCMAKE_INSTALL_PREFIX:FILEPATH=<INSTALL_DIR>
62 -DCMAKE_BUILD_TYPE:STRING=Release
63 -DBUILD_SHARED_LIBS:BOOL=ON
64 -DBUILD_STATIC_LIBS:BOOL=ON
67 -DCMAKE_POLICY_VERSION_MINIMUM=3.5
69 ${openblas_cmake_args}
70 BUILD_COMMAND ${CMAKE_MAKE_PROGRAM} -j${BUILD_PARALLEL_LEVEL} ${default_build_args} ${openblas_build_args}
71 INSTALL_COMMAND ${CMAKE_MAKE_PROGRAM} -j${BUILD_PARALLEL_LEVEL} PREFIX=<INSTALL_DIR> install
74 ExternalProject_Get_Property(openblas INSTALL_DIR)
75 set(OPENBLAS_DIR ${INSTALL_DIR})
77 set_if_not_present(ENV{CPATH} "$ENV{CPATH}" "${OPENBLAS_DIR}/include")
78 set_if_not_present(ENV{LIBRARY_PATH} "$ENV{LIBRARY_PATH}"
79 "${OPENBLAS_DIR}/lib")
80 set_if_not_present(ENV{LD_LIBRARY_PATH} "$ENV{LD_LIBRARY_PATH}"
81 "${OPENBLAS_DIR}/lib")
84 set(BLAS_LIBRARIES "${OPENBLAS_DIR}/lib/libopenblas.so")
86 set(LAPACK_LIBRARIES "${OPENBLAS_DIR}/lib/libopenblas.so")
88 endif(NOT TARGET openblas)
91 message(STATUS "BLAS_LIBRARIES: ${BLAS_LIBRARIES}")
92 message(STATUS "BLAS95_LIBRARIES: ${BLAS95_LIBRARIES}")
93 message(STATUS "LAPACK_LIBRARIES: ${LAPACK_LIBRARIES}")
94 message(STATUS "LAPACK95_LIBRARIES: ${LAPACK95_LIBRARIES}")
96 string(REPLACE ";" " " BLAS_LIBRARIES_LIBS "${BLAS_LIBRARIES}")
97 string(REPLACE ";" " " BLAS95_LIBRARIES_LIBS "${BLAS95_LIBRARIES}")
98 string(REPLACE ";" " " LAPACK_LIBRARIES_LIBS "${LAPACK_LIBRARIES}")
99 string(REPLACE ";" " " LAPACK95_LIBRARIES_LIBS "${LAPACK95_LIBRARIES}")
100 message(STATUS "BLAS_LIBRARIES_LIBS: ${BLAS_LIBRARIES_LIBS}")
101endif(openblas_enabled)