Skip to content

Commit 90d0dba

Browse files
authoredMar 13, 2025··
cmake: add custom FindCBLAS FindLAPACKE (#5376)
This implementation makes use of pkg-config to find the CBLAS and LAPACKE libraries. It depends on CBLAS_PREFER_PKGCONFIG:BOOL=ON, which currently is the default (and practically the only option). - The CBLAS_PKGCONFIG may be set to the package name for finding CBLAS, e.g., -DCBLAS_PKGCONFIG=openblas. - The LAPACKE_PKGCONFIG may be set to the package name for finding CBLAS, e.g., -DLAPACKE_PKGCONFIG=openblas. By default the following pkg-config modules are searched for (in given order): - CBLAS_PKGCONFIG: cblas, blas-netlib, openblas and blas-atlas - LAPACKE_PKGCONFIG: lapacke and openblas
1 parent 6a92bea commit 90d0dba

8 files changed

+253
-2290
lines changed
 

‎cmake/find_scripts/FindBLAS.cmake

-1,426
This file was deleted.

‎cmake/find_scripts/FindCBLAS.cmake

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#[[
2+
COPYRIGHT: (c) 2025 Nicklas Larsson and the GRASS Development Team
3+
SPDX-License-Identifier: GPL-2.0-or-later
4+
]]
5+
6+
#[=======================================================================[.rst:
7+
FindCBLAS
8+
---------
9+
10+
Find CBLAS library, the C Interface to Basic Linear Algebra Subprograms (BLAS)
11+
12+
.. _`BLAS linear-algebra interface`: https://netlib.org/blas/
13+
14+
Input Variables
15+
^^^^^^^^^^^^^^^
16+
17+
The following variables may be set to influence this module's behavior:
18+
19+
``CBLAS_PREFER_PKGCONFIG``
20+
21+
If set, ``pkg-config`` will be used to search for a CBLAS library
22+
and if one is found that is used.
23+
Note: this is currently the only method.
24+
25+
``CBLAS_PKGCONFIG``
26+
27+
If set, the ``pkg-config`` method will look for this module name. If not
28+
set ``cblas``, ``blas-netlib``, ``openblas`` and ``blas-atlas`` will be
29+
looked for (in that order).
30+
31+
Imported targets
32+
^^^^^^^^^^^^^^^^
33+
34+
This module defines the following :prop_tgt:`IMPORTED` targets:
35+
36+
``CBLAS::CBLAS``
37+
38+
The libraries to use for CBLAS, if found.
39+
40+
Result Variables
41+
^^^^^^^^^^^^^^^^
42+
43+
This module defines the following variables:
44+
45+
``CBLAS_FOUND``
46+
library implementing the CBLAS interface is found
47+
``CBLAS_LINKER_FLAGS``
48+
uncached list of required linker flags (excluding ``-l`` and ``-L``).
49+
``CBLAS_LIBRARIES``
50+
list of libraries (using full path name) to link against to use CBLAS
51+
``CBLAS_INCLUDE_DIRS``
52+
path to the CBLAS include directory.
53+
``CBLAS_VERSION``
54+
version of the module providing CBLAS
55+
56+
#]=======================================================================]
57+
58+
set(_default_pkgs cblas blas-netlib openblas blas-atlas)
59+
60+
macro(_search_cblas_pkgs)
61+
foreach(_pkg ${_default_pkgs})
62+
pkg_check_modules(PKGC_CBLAS QUIET ${_pkg})
63+
if(PKGC_CBLAS_FOUND)
64+
set(CBLAS_PKGCONFIG ${_pkg})
65+
endif()
66+
endforeach()
67+
endmacro()
68+
69+
if(CBLAS_PREFER_PKGCONFIG)
70+
find_package(PkgConfig QUIET)
71+
if(PKG_CONFIG_FOUND)
72+
if(NOT CBLAS_PKGCONFIG)
73+
_search_cblas_pkgs()
74+
endif()
75+
pkg_check_modules(PKGC_CBLAS QUIET ${CBLAS_PKGCONFIG})
76+
if(PKGC_CBLAS_FOUND)
77+
set(CBLAS_FOUND ${PKGC_CBLAS_FOUND})
78+
set(CBLAS_LIBRARIES "${PKGC_CBLAS_LINK_LIBRARIES}")
79+
set(CBLAS_INCLUDEDIR "${PKGC_CBLAS_INCLUDEDIR}")
80+
set(CBLAS_INCLUDE_DIRS ${PKGC_CBLAS_INCLUDEDIR}
81+
${PKGC_CBLAS_INCLUDE_DIRS})
82+
set(CBLAS_VERSION "${PKGC_CBLAS_VERSION}")
83+
set(CBLAS_LINKER_FLAGS "${PKGC_CBLAS_LDFLAGS}")
84+
85+
list(REMOVE_DUPLICATES CBLAS_INCLUDE_DIRS)
86+
list(FILTER CBLAS_LINKER_FLAGS EXCLUDE REGEX "^-L\.*|^-l\.*")
87+
endif()
88+
endif()
89+
endif()
90+
91+
unset(_default_pkgs)
92+
93+
include(CheckSymbolExists)
94+
set(CMAKE_REQUIRED_LIBRARIES ${CBLAS_LIBRARIES})
95+
set(CMAKE_REQUIRED_INCLUDES ${CBLAS_INCLUDEDIR})
96+
set(CMAKE_REQUIRED_QUIET ${CBLAS_FIND_QUIETLY})
97+
check_symbol_exists(cblas_dgemm "cblas.h" HAVE_CBLAS_DGEMM)
98+
unset(CMAKE_REQUIRED_LIBRARIES)
99+
unset(CMAKE_REQUIRED_INCLUDES)
100+
unset(CMAKE_REQUIRED_QUIET)
101+
102+
include(FindPackageHandleStandardArgs)
103+
find_package_handle_standard_args(
104+
CBLAS
105+
REQUIRED_VARS CBLAS_FOUND CBLAS_LIBRARIES CBLAS_INCLUDEDIR HAVE_CBLAS_DGEMM
106+
VERSION_VAR CBLAS_VERSION)
107+
mark_as_advanced(CBLAS_LIBRARIES CBLAS_INCLUDEDIR)
108+
109+
if(CBLAS_FOUND AND NOT TARGET CBLAS::CBLAS)
110+
add_library(CBLAS::CBLAS INTERFACE IMPORTED)
111+
set_target_properties(
112+
CBLAS::CBLAS PROPERTIES INTERFACE_LINK_LIBRARIES "${CBLAS_LIBRARIES}"
113+
INTERFACE_INCLUDE_DIRECTORIES "${CBLAS_INCLUDEDIR}")
114+
if(CBLAS_LINKER_FLAGS)
115+
set_target_properties(CBLAS::CBLAS PROPERTIES INTERFACE_LINK_OPTIONS
116+
"${CBLAS_LINKER_FLAGS}")
117+
endif()
118+
endif()

‎cmake/find_scripts/FindLAPACK.cmake

-817
This file was deleted.

‎cmake/find_scripts/FindLAPACKE.cmake

+119
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
#[[
2+
COPYRIGHT: (c) 2025 Nicklas Larsson and the GRASS Development Team
3+
SPDX-License-Identifier: GPL-2.0-or-later
4+
]]
5+
6+
#[=======================================================================[.rst:
7+
FindLAPACKE
8+
-----------
9+
10+
Find LAPACKE library, the C Interface to LAPACK.
11+
12+
.. _`The LAPACKE C Interface to LAPACK`: https://www.netlib.org/lapack/lapacke.html
13+
14+
Input Variables
15+
^^^^^^^^^^^^^^^
16+
17+
The following variables may be set to influence this module's behavior:
18+
19+
``CBLAS_PREFER_PKGCONFIG``
20+
21+
If set ``pkg-config`` will be used to search for a CBLAS library
22+
and if one is found that is used.
23+
Note: this is currently the only method.
24+
25+
``LAPACKE_PKGCONFIG``
26+
27+
If set, the ``pkg-config`` method will look for this module name. If not
28+
set ``lapacke``and ``openblas`` will be looked for (in that order).
29+
30+
Imported targets
31+
^^^^^^^^^^^^^^^^
32+
33+
This module defines the following :prop_tgt:`IMPORTED` targets:
34+
35+
``LAPACKE::LAPACKE``
36+
37+
The libraries to use for LAPACKE, if found.
38+
39+
Result Variables
40+
^^^^^^^^^^^^^^^^
41+
42+
This module defines the following variables:
43+
44+
``LAPACKE_FOUND``
45+
library implementing the LAPACKE interface is found
46+
``LAPACKE_LINKER_FLAGS``
47+
uncached list of required linker flags (excluding ``-l`` and ``-L``).
48+
``LAPACKE_LIBRARIES``
49+
list of libraries (using full path name) to link against to use LAPACKE
50+
``LAPACKE_INCLUDE_DIRS``
51+
path to the CBLAS include directory.
52+
``LAPACKE_VERSION``
53+
version of the module providing LAPACKE
54+
55+
#]=======================================================================]
56+
57+
set(_default_pkgs lapacke openblas)
58+
59+
macro(_search_lapacke_pkgs)
60+
foreach(_pkg ${_default_pkgs})
61+
pkg_check_modules(PKGC_LAPACKE QUIET ${_pkg})
62+
if(PKGC_LAPACKE_FOUND)
63+
set(LAPACKE_PKGCONFIG ${_pkg})
64+
endif()
65+
endforeach()
66+
endmacro()
67+
68+
if(CBLAS_PREFER_PKGCONFIG)
69+
find_package(PkgConfig QUIET)
70+
if(PKG_CONFIG_FOUND)
71+
if(NOT LAPACKE_PKGCONFIG)
72+
_search_lapacke_pkgs()
73+
endif()
74+
pkg_check_modules(PKGC_LAPACKE QUIET ${LAPACKE_PKGCONFIG})
75+
if(PKGC_LAPACKE_FOUND)
76+
set(LAPACKE_FOUND ${PKGC_LAPACKE_FOUND})
77+
set(LAPACKE_LIBRARIES "${PKGC_LAPACKE_LINK_LIBRARIES}")
78+
set(LAPACKE_INCLUDEDIR "${PKGC_LAPACKE_INCLUDEDIR}")
79+
set(LAPACKE_INCLUDE_DIRS ${PKGC_LAPACKE_INCLUDEDIR}
80+
${PKGC_LAPACKE_INCLUDE_DIRS})
81+
set(LAPACKE_VERSION ${PKGC_LAPACKE_VERSION})
82+
set(LAPACKE_LINKER_FLAGS "${PKGC_LAPACKE_LDFLAGS}")
83+
84+
list(REMOVE_DUPLICATES LAPACKE_INCLUDE_DIRS)
85+
list(FILTER LAPACKE_LINKER_FLAGS EXCLUDE REGEX "^-L\.*|^-l\.*")
86+
endif()
87+
endif()
88+
endif()
89+
90+
unset(_default_pkgs)
91+
92+
include(CheckSymbolExists)
93+
set(CMAKE_REQUIRED_LIBRARIES ${LAPACKE_LIBRARIES})
94+
set(CMAKE_REQUIRED_INCLUDES ${LAPACKE_INCLUDEDIR})
95+
set(CMAKE_REQUIRED_QUIET ${LAPACKE_FIND_QUIETLY})
96+
check_symbol_exists(LAPACKE_dgesv "lapacke.h" HAVE_LAPACKE_DGESV)
97+
unset(CMAKE_REQUIRED_LIBRARIES)
98+
unset(CMAKE_REQUIRED_INCLUDES)
99+
unset(CMAKE_REQUIRED_QUIET)
100+
101+
include(FindPackageHandleStandardArgs)
102+
find_package_handle_standard_args(
103+
LAPACKE
104+
REQUIRED_VARS LAPACKE_FOUND LAPACKE_LIBRARIES LAPACKE_INCLUDEDIR
105+
HAVE_LAPACKE_DGESV
106+
VERSION_VAR LAPACKE_VERSION)
107+
mark_as_advanced(LAPACKE_LIBRARIES LAPACKE_INCLUDEDIR)
108+
109+
if(LAPACKE_FOUND AND NOT TARGET LAPACKE::LAPACKE)
110+
add_library(LAPACKE::LAPACKE INTERFACE IMPORTED)
111+
set_target_properties(
112+
LAPACKE::LAPACKE
113+
PROPERTIES INTERFACE_LINK_LIBRARIES "${LAPACKE_LIBRARIES}"
114+
INTERFACE_INCLUDE_DIRECTORIES "${LAPACKE_INCLUDEDIR}")
115+
if(LAPACKE_LINKER_FLAGS)
116+
set_target_properties(LAPACKE::LAPACKE PROPERTIES INTERFACE_LINK_OPTIONS
117+
"${LAPACKE_LINKER_FLAGS}")
118+
endif()
119+
endif()

‎cmake/modules/CheckDependentLibraries.cmake

+8-38
Original file line numberDiff line numberDiff line change
@@ -161,46 +161,18 @@ if(WITH_FFTW)
161161
endif()
162162
endif()
163163

164-
if(WIN32)
165-
set(BLA_PREFER_PKGCONFIG ON)
166-
set(BLA_PKGCONFIG_BLAS "openblas")
167-
set(BLA_PKGCONFIG_LAPACK "openblas")
168-
else()
169-
set(BLA_PKGCONFIG_BLAS "blas-netlib")
170-
set(BLA_PKGCONFIG_LAPACK "lapacke")
171-
endif()
172-
173164
if(WITH_CBLAS)
174-
# find_package(CBLAS CONFIG REQUIRED)
175-
pkg_check_modules(CBLAS QUIET ${BLA_PKGCONFIG_BLAS})
176-
if (CBLAS_FOUND)
177-
add_library(CBLAS INTERFACE IMPORTED GLOBAL)
178-
set_property(TARGET CBLAS PROPERTY INTERFACE_LINK_LIBRARIES
179-
${CBLAS_LIBRARIES})
180-
set_property(TARGET CBLAS PROPERTY INTERFACE_LINK_DIRECTORIES
181-
${CBLAS_LIBRARY_DIRS})
182-
set_property(TARGET CBLAS PROPERTY INTERFACE_INCLUDE_DIRECTORIES
183-
${CBLAS_INCLUDEDIR})
165+
if(NOT CBLAS_PREFER_PKGCONFIG)
166+
set(CBLAS_PREFER_PKGCONFIG ON)
184167
endif()
185-
include(FindPackageHandleStandardArgs)
186-
find_package_handle_standard_args(CBLAS REQUIRED_VARS CBLAS_LIBRARIES
187-
CBLAS_INCLUDEDIR)
168+
find_package(CBLAS REQUIRED)
188169
endif()
189170

190171
if(WITH_LAPACKE)
191-
# find_package(LAPACKE CONFIG REQUIRED)
192-
pkg_check_modules(LAPACKE QUIET ${BLA_PKGCONFIG_LAPACK})
193-
if(LAPACKE_FOUND)
194-
add_library(LAPACKE INTERFACE IMPORTED GLOBAL)
195-
set_property(TARGET LAPACKE PROPERTY INTERFACE_LINK_LIBRARIES
196-
${LAPACKE_LIBRARIES})
197-
set_property(TARGET LAPACKE PROPERTY INTERFACE_LINK_DIRECTORIES
198-
${LAPACKE_LIBRARY_DIRS})
199-
set_property(TARGET LAPACKE PROPERTY INTERFACE_INCLUDE_DIRECTORIES
200-
${LAPACKE_INCLUDEDIR})
172+
if(NOT WITH_CBLAS)
173+
message(FATAL_ERROR "LAPACKE support requires CBLAS")
201174
endif()
202-
find_package_handle_standard_args(LAPACKE REQUIRED_VARS LAPACKE_LIBRARIES
203-
LAPACKE_INCLUDEDIR)
175+
find_package(LAPACKE REQUIRED)
204176
endif()
205177

206178
if(WITH_OPENMP)
@@ -292,10 +264,8 @@ check_target(Cairo::Cairo HAVE_CAIRO_H)
292264
# set(CMAKE_REQUIRED_INCLUDES "${FFTW_INCLUDE_DIR}") no target ATLAS in
293265
# thirdpary/CMakeLists.txt
294266
check_target(ATLAS HAVE_LIBATLAS)
295-
check_target(CBLAS HAVE_LIBBLAS)
296-
check_target(CBLAS HAVE_CBLAS_H)
297-
check_target(LAPACKE HAVE_LIBLAPACK)
298-
check_target(LAPACKE HAVE_CLAPACK_H)
267+
check_target(CBLAS::CBLAS HAVE_LIBBLAS)
268+
check_target(LAPACKE::LAPACKE HAVE_LIBLAPACK)
299269
check_target(TIFF::TIFF HAVE_TIFFIO_H)
300270
check_target(NETCDF HAVE_NETCDF)
301271
check_target(GEOS::geos_c HAVE_GEOS)

‎cmake/modules/Configure.cmake

+3-4
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ check_include_file(sys/types.h HAVE_SYS_TYPES_H)
2222
check_include_file(sys/utsname.h HAVE_SYS_UTSNAME_H)
2323
check_include_file(g2c.h HAVE_G2C_H)
2424
check_include_file(f2c.h HAVE_F2C_H)
25+
check_include_file(cblas.h HAVE_CBLAS_H)
26+
check_include_file(cblas-atlas.h HAVE_CBLAS_ATLAS_H)
27+
2528

2629
if(MSVC)
2730
set(HAVE_PTHREAD_H 0)
@@ -46,10 +49,6 @@ endif()
4649
#
4750
# check_target(PROJ HAVE_PROJ_H)
4851
#
49-
# check_target(BLAS HAVE_LIBBLAS) check_target(BLAS HAVE_CBLAS_H)
50-
#
51-
# check_target(LAPACK HAVE_LIBLAPACK) check_target(LAPACK HAVE_CLAPACK_H)
52-
#
5352
# check_target(FREETYPE HAVE_FT2BUILD_H) check_target(POSTGRES HAVE_POSTGRES)
5453
# check_target(ODBC HAVE_SQL_H)
5554
#

‎include/config.h.cmake.in

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@
2020
/* Define to 1 if you have the <cairo.h> header file. */
2121
#cmakedefine HAVE_CAIRO_H ${HAVE_CAIRO_H}
2222

23+
/* Define to 1 if you have the <cblas-atlas.h> header file. */
24+
#cmakedefine HAVE_CBLAS_ATLAS_H ${HAVE_CBLAS_ATLAS_H}
25+
2326
/* Define to 1 if you have the <cblas.h> header file. */
2427
#cmakedefine HAVE_CBLAS_H ${HAVE_CBLAS_H}
2528

26-
/* Define to 1 if you have the <clapack.h> header file. */
27-
#cmakedefine HAVE_CLAPACK_H ${HAVE_CLAPACK_H}
28-
2929
/* Define to 1 if you have the <CL/cl.h> header file. */
3030
#cmakedefine HAVE_CL_CL_H ${HAVE_CL_CL_H}
3131

‎lib/CMakeLists.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ build_library_in_subdir(
5858
DEFS
5959
"${use_math_DEFS}"
6060
OPTIONAL_DEPENDS
61+
CBLAS::CBLAS
6162
FFTW
62-
LAPACKE
63-
CBLAS
63+
LAPACKE::LAPACKE
6464
OPENMP)
6565

6666
build_library_in_subdir(linkm)

0 commit comments

Comments
 (0)
Please sign in to comment.