From 51bb33566300d7163b522d7f9a825f417f0bec5e Mon Sep 17 00:00:00 2001 From: Nicklas Larsson Date: Thu, 2 Jul 2026 23:15:42 +0200 Subject: [PATCH 1/2] cmake: test CBLAS and LAPACKE packages for req. symbols --- cmake/find_scripts/FindCBLAS.cmake | 72 +++++++++++----- cmake/find_scripts/FindLAPACKE.cmake | 91 +++++++++++++-------- cmake/modules/CheckDependentLibraries.cmake | 3 + 3 files changed, 112 insertions(+), 54 deletions(-) diff --git a/cmake/find_scripts/FindCBLAS.cmake b/cmake/find_scripts/FindCBLAS.cmake index a99c879747b..691cd44d8ac 100644 --- a/cmake/find_scripts/FindCBLAS.cmake +++ b/cmake/find_scripts/FindCBLAS.cmake @@ -57,11 +57,55 @@ This module defines the following variables: set(_default_pkgs cblas blas-netlib openblas blas-atlas) +set(CBLAS_FOUND) +set(CBLAS_LIBRARIES) +set(CBLAS_INCLUDEDIR) +set(CBLAS_INCLUDE_DIRS) +set(CBLAS_VERSION) +set(CBLAS_LINKER_FLAGS) +set(HAVE_CBLAS_DGEMM) + +macro(_test_package) + pkg_check_modules(PKGC_CBLAS QUIET ${CBLAS_PKGCONFIG}) + if(PKGC_CBLAS_FOUND) + set(_cblas_found "${PKGC_CBLAS_FOUND}") + set(_cblas_libraries "${PKGC_CBLAS_LINK_LIBRARIES}") + set(_cblas_includedir "${PKGC_CBLAS_INCLUDEDIR}") + set(_cblas_include_dirs ${PKGC_CBLAS_INCLUDEDIR} + ${PKGC_CBLAS_INCLUDE_DIRS}) + set(_cblas_version "${PKGC_CBLAS_VERSION}") + set(_cblas_linker_flags "${PKGC_CBLAS_LDFLAGS}") + + list(REMOVE_DUPLICATES CBLAS_INCLUDE_DIRS) + list(FILTER _cblas_linker_flags EXCLUDE REGEX "^-L\.*|^-l\.*") + + include(CheckSymbolExists) + set(CMAKE_REQUIRED_LIBRARIES "${_cblas_libraries}") + set(CMAKE_REQUIRED_INCLUDES "${_cblas_includedir}") + set(CMAKE_REQUIRED_QUIET ON) + check_symbol_exists(cblas_dgemm "cblas.h" HAVE_CBLAS_DGEMM) + unset(CMAKE_REQUIRED_LIBRARIES) + unset(CMAKE_REQUIRED_INCLUDES) + unset(CMAKE_REQUIRED_QUIET) + + set(CBLAS_FOUND "${_cblas_found}") + set(CBLAS_LIBRARIES "${_cblas_libraries}") + set(CBLAS_INCLUDEDIR "${_cblas_includedir}") + set(CBLAS_INCLUDE_DIRS "${_cblas_include_dirs}") + set(CBLAS_VERSION "${_cblas_version}") + set(CBLAS_LINKER_FLAGS "${_cblas_linker_flags}") + endif() +endmacro() + macro(_search_cblas_pkgs) foreach(_pkg ${_default_pkgs}) pkg_check_modules(PKGC_CBLAS QUIET ${_pkg}) if(PKGC_CBLAS_FOUND) set(CBLAS_PKGCONFIG ${_pkg}) + _test_package() + if(HAVE_CBLAS_DGEMM) + break() + endif() endif() endforeach() endmacro() @@ -69,35 +113,19 @@ endmacro() if(CBLAS_PREFER_PKGCONFIG) find_package(PkgConfig QUIET) if(PKG_CONFIG_FOUND) - if(NOT CBLAS_PKGCONFIG) + if(CBLAS_PKGCONFIG) + _test_package() + else() _search_cblas_pkgs() endif() - pkg_check_modules(PKGC_CBLAS QUIET ${CBLAS_PKGCONFIG}) - if(PKGC_CBLAS_FOUND) - set(CBLAS_FOUND ${PKGC_CBLAS_FOUND}) - set(CBLAS_LIBRARIES "${PKGC_CBLAS_LINK_LIBRARIES}") - set(CBLAS_INCLUDEDIR "${PKGC_CBLAS_INCLUDEDIR}") - set(CBLAS_INCLUDE_DIRS ${PKGC_CBLAS_INCLUDEDIR} - ${PKGC_CBLAS_INCLUDE_DIRS}) - set(CBLAS_VERSION "${PKGC_CBLAS_VERSION}") - set(CBLAS_LINKER_FLAGS "${PKGC_CBLAS_LDFLAGS}") - - list(REMOVE_DUPLICATES CBLAS_INCLUDE_DIRS) - list(FILTER CBLAS_LINKER_FLAGS EXCLUDE REGEX "^-L\.*|^-l\.*") - endif() endif() endif() unset(_default_pkgs) -include(CheckSymbolExists) -set(CMAKE_REQUIRED_LIBRARIES ${CBLAS_LIBRARIES}) -set(CMAKE_REQUIRED_INCLUDES ${CBLAS_INCLUDEDIR}) -set(CMAKE_REQUIRED_QUIET ${CBLAS_FIND_QUIETLY}) -check_symbol_exists(cblas_dgemm "cblas.h" HAVE_CBLAS_DGEMM) -unset(CMAKE_REQUIRED_LIBRARIES) -unset(CMAKE_REQUIRED_INCLUDES) -unset(CMAKE_REQUIRED_QUIET) +if(NOT HAVE_CBLAS_DGEMM) + message(STATUS "Looking for cblas_dgemm - not found") +endif() include(FindPackageHandleStandardArgs) find_package_handle_standard_args( diff --git a/cmake/find_scripts/FindLAPACKE.cmake b/cmake/find_scripts/FindLAPACKE.cmake index 89593e7e728..46ea361a891 100644 --- a/cmake/find_scripts/FindLAPACKE.cmake +++ b/cmake/find_scripts/FindLAPACKE.cmake @@ -16,9 +16,9 @@ Input Variables The following variables may be set to influence this module's behavior: -``CBLAS_PREFER_PKGCONFIG`` +``LAPACKE_PREFER_PKGCONFIG`` - If set ``pkg-config`` will be used to search for a CBLAS library + If set ``pkg-config`` will be used to search for a LAPACKE library and if one is found that is used. Note: this is currently the only method. @@ -48,7 +48,7 @@ This module defines the following variables: ``LAPACKE_LIBRARIES`` list of libraries (using full path name) to link against to use LAPACKE ``LAPACKE_INCLUDE_DIRS`` - path to the CBLAS include directory. + path to the LAPACKE include directory. ``LAPACKE_VERSION`` version of the module providing LAPACKE @@ -56,53 +56,80 @@ This module defines the following variables: set(_default_pkgs lapacke openblas) +set(LAPACKE_FOUND) +set(LAPACKE_LIBRARIES) +set(LAPACKE_INCLUDEDIR) +set(LAPACKE_INCLUDE_DIRS) +set(LAPACKE_VERSION) +set(LAPACKE_LINKER_FLAGS) +set(HAVE_LAPACKE_DGESV) + +macro(_test_package) + pkg_check_modules(PKGC_LAPACKE QUIET ${LAPACKE_PKGCONFIG}) + if(PKGC_LAPACKE_FOUND) + set(_lapacke_found ${PKGC_LAPACKE_FOUND}) + set(_lapacke_libraries "${PKGC_LAPACKE_LINK_LIBRARIES}") + if(MSVC) + # MSVC needs msvc/lapacke.h to define custom complex types + set(_lapacke_includedir "${CMAKE_SOURCE_DIR}/msvc;${PKGC_LAPACKE_INCLUDEDIR}") + else() + set(_lapacke_includedir "${PKGC_LAPACKE_INCLUDEDIR}") + endif() + set(_lapacke_include_dirs ${PKGC_LAPACKE_INCLUDEDIR} + ${PKGC_LAPACKE_INCLUDE_DIRS}) + set(_lapacke_version ${PKGC_LAPACKE_VERSION}) + set(_lapacke_linker_flags "${PKGC_LAPACKE_LDFLAGS}") + + list(REMOVE_DUPLICATES _lapacke_include_dirs) + list(FILTER _lapacke_linker_flags EXCLUDE REGEX "^-L\.*|^-l\.*") + endif() + + include(CheckSymbolExists) + set(CMAKE_REQUIRED_LIBRARIES ${_lapacke_libraries}) + set(CMAKE_REQUIRED_INCLUDES ${_lapacke_includedir}) + set(CMAKE_REQUIRED_QUIET ON) + check_symbol_exists(LAPACKE_dgesv "lapacke.h" HAVE_LAPACKE_DGESV) + unset(CMAKE_REQUIRED_LIBRARIES) + unset(CMAKE_REQUIRED_INCLUDES) + unset(CMAKE_REQUIRED_QUIET) + + set(LAPACKE_FOUND ${_lapacke_found}) + set(LAPACKE_LIBRARIES ${_lapacke_libraries}) + set(LAPACKE_INCLUDEDIR ${_lapacke_includedir}) + set(LAPACKE_INCLUDE_DIRS ${_lapacke_include_dirs}) + set(LAPACKE_VERSION ${_lapacke_version}) + set(LAPACKE_LINKER_FLAGS ${_lapacke_linker_flags}) +endmacro() + macro(_search_lapacke_pkgs) foreach(_pkg ${_default_pkgs}) pkg_check_modules(PKGC_LAPACKE QUIET ${_pkg}) if(PKGC_LAPACKE_FOUND) set(LAPACKE_PKGCONFIG ${_pkg}) - break() + _test_package() + if(HAVE_LAPACKE_DGESV) + break() + endif() endif() endforeach() endmacro() -if(CBLAS_PREFER_PKGCONFIG) +if(LAPACKE_PREFER_PKGCONFIG) find_package(PkgConfig QUIET) if(PKG_CONFIG_FOUND) - if(NOT LAPACKE_PKGCONFIG) + if(LAPACKE_PKGCONFIG) + _test_package() + else() _search_lapacke_pkgs() endif() - pkg_check_modules(PKGC_LAPACKE QUIET ${LAPACKE_PKGCONFIG}) - if(PKGC_LAPACKE_FOUND) - set(LAPACKE_FOUND ${PKGC_LAPACKE_FOUND}) - set(LAPACKE_LIBRARIES "${PKGC_LAPACKE_LINK_LIBRARIES}") - if(MSVC) - # MSVC needs msvc/lapacke.h to define custom complex types - set(LAPACKE_INCLUDEDIR "${CMAKE_SOURCE_DIR}/msvc;${PKGC_LAPACKE_INCLUDEDIR}") - else() - set(LAPACKE_INCLUDEDIR "${PKGC_LAPACKE_INCLUDEDIR}") - endif() - set(LAPACKE_INCLUDE_DIRS ${PKGC_LAPACKE_INCLUDEDIR} - ${PKGC_LAPACKE_INCLUDE_DIRS}) - set(LAPACKE_VERSION ${PKGC_LAPACKE_VERSION}) - set(LAPACKE_LINKER_FLAGS "${PKGC_LAPACKE_LDFLAGS}") - - list(REMOVE_DUPLICATES LAPACKE_INCLUDE_DIRS) - list(FILTER LAPACKE_LINKER_FLAGS EXCLUDE REGEX "^-L\.*|^-l\.*") - endif() endif() endif() unset(_default_pkgs) -include(CheckSymbolExists) -set(CMAKE_REQUIRED_LIBRARIES ${LAPACKE_LIBRARIES}) -set(CMAKE_REQUIRED_INCLUDES ${LAPACKE_INCLUDEDIR}) -set(CMAKE_REQUIRED_QUIET ${LAPACKE_FIND_QUIETLY}) -check_symbol_exists(LAPACKE_dgesv "lapacke.h" HAVE_LAPACKE_DGESV) -unset(CMAKE_REQUIRED_LIBRARIES) -unset(CMAKE_REQUIRED_INCLUDES) -unset(CMAKE_REQUIRED_QUIET) +if(NOT HAVE_LAPACKE_DGESV) + message(STATUS "Looking for LAPACKE_dgesv - not found") +endif() include(FindPackageHandleStandardArgs) find_package_handle_standard_args( diff --git a/cmake/modules/CheckDependentLibraries.cmake b/cmake/modules/CheckDependentLibraries.cmake index 035db5d5d7b..9de0b65328d 100644 --- a/cmake/modules/CheckDependentLibraries.cmake +++ b/cmake/modules/CheckDependentLibraries.cmake @@ -224,6 +224,9 @@ if(WITH_LAPACKE) if(NOT WITH_CBLAS) message(FATAL_ERROR "LAPACKE support requires CBLAS") endif() + if(NOT LAPACKE_PREFER_PKGCONFIG) + set(LAPACKE_PREFER_PKGCONFIG ON) + endif() find_package(LAPACKE REQUIRED) endif() From fc184e4662b30abd337b3333a15005f7cb4e70e5 Mon Sep 17 00:00:00 2001 From: Nicklas Larsson Date: Thu, 2 Jul 2026 23:59:05 +0200 Subject: [PATCH 2/2] log chosen package --- cmake/find_scripts/FindCBLAS.cmake | 2 ++ cmake/find_scripts/FindLAPACKE.cmake | 2 ++ 2 files changed, 4 insertions(+) diff --git a/cmake/find_scripts/FindCBLAS.cmake b/cmake/find_scripts/FindCBLAS.cmake index 691cd44d8ac..e525006c19a 100644 --- a/cmake/find_scripts/FindCBLAS.cmake +++ b/cmake/find_scripts/FindCBLAS.cmake @@ -134,6 +134,8 @@ find_package_handle_standard_args( VERSION_VAR CBLAS_VERSION) mark_as_advanced(CBLAS_LIBRARIES CBLAS_INCLUDEDIR) +message(STATUS "Using CBLAS package: ${CBLAS_PKGCONFIG}") + if(CBLAS_FOUND AND NOT TARGET CBLAS::CBLAS) add_library(CBLAS::CBLAS INTERFACE IMPORTED) set_target_properties( diff --git a/cmake/find_scripts/FindLAPACKE.cmake b/cmake/find_scripts/FindLAPACKE.cmake index 46ea361a891..97bd897aef6 100644 --- a/cmake/find_scripts/FindLAPACKE.cmake +++ b/cmake/find_scripts/FindLAPACKE.cmake @@ -139,6 +139,8 @@ find_package_handle_standard_args( VERSION_VAR LAPACKE_VERSION) mark_as_advanced(LAPACKE_LIBRARIES LAPACKE_INCLUDEDIR) +message(STATUS "Using LAPACKE package: ${LAPACKE_PKGCONFIG}") + if(LAPACKE_FOUND AND NOT TARGET LAPACKE::LAPACKE) add_library(LAPACKE::LAPACKE INTERFACE IMPORTED) set_target_properties(