From 98ec22a2e5d82da7ee5bc63e4f2445df0e100e64 Mon Sep 17 00:00:00 2001 From: drhpc Date: Sun, 11 Apr 2021 16:52:03 +0200 Subject: BLD: introduce use of BLAS_LIBS and LAPACK_LIBS in distutils/system_info (#18737) This enables the behaviour of the build honouring the environment variables NPY_BLAS_LIBS, NPY_CBLAS_LIBS, and NPY_LAPACK_LIBS to select any standard-conforming implementation of the f77 interfaces. This is needed to sanely have automated builds provide BLAS libs in a way common to multiple packages (using some environment variable, BLAS_LIBS being canonical, but variants exist). Hacking a site.cfg is fine for a single user, but does not scale well. As example, pkgsrc, the NetBSD packages collection, offers differing builds of Netlib or OpenBLAS, the latter in variables openblas, openblas_openmp, and openblas_pthread. With this patch, differing builds can just use different variable values like NPY_BLAS_LIBS=-lblas NPY_CBLAS_LIBS=-lcblas \ NPY_LAPACK_LIBS='-lapack -lblas' \ python setup.py build NPY_BLAS_LIBS=-lopenblas_pthread NPY_CBLAS_LIBS= \ NPY_LAPACK_LIBS='-lopenblas_pthread' \ python setup.py build (NPY_LAPACK_LIBS contains reference to BLAS, too, for packages that only use LAPACK and would miss underlying BLAS otherwise for static linking.) --- numpy/distutils/system_info.py | 49 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) (limited to 'numpy/distutils/system_info.py') diff --git a/numpy/distutils/system_info.py b/numpy/distutils/system_info.py index ae6f83e12..9e192329f 100644 --- a/numpy/distutils/system_info.py +++ b/numpy/distutils/system_info.py @@ -114,6 +114,19 @@ Currently, the following classes are available, along with their section names: x11_info:x11 xft_info:xft +Note that blas_opt_info and lapack_opt_info honor the NPY_BLAS_ORDER +and NPY_LAPACK_ORDER environment variables to determine the order in which +specific BLAS and LAPACK libraries are searched for. + +This search (or autodetection) can be bypassed by defining the environment +variables NPY_BLAS_LIBS and NPY_LAPACK_LIBS, which should then contain the +exact linker flags to use (language will be set to F77). Building against +Netlib BLAS/LAPACK or stub files, in order to be able to switch BLAS and LAPACK +implementations at runtime. If using this to build NumPy itself, it is +recommended to also define NPY_CBLAS_LIBS (assuming your BLAS library has a +CBLAS interface) to enable CBLAS usage for matrix multiplication (unoptimized +otherwise). + Example: ---------- [DEFAULT] @@ -1847,6 +1860,16 @@ class lapack_opt_info(system_info): return True return False + def _calc_info_from_envvar(self): + info = {} + info['language'] = 'f77' + info['libraries'] = [] + info['include_dirs'] = [] + info['define_macros'] = [] + info['extra_link_args'] = os.environ['NPY_LAPACK_LIBS'].split() + self.set_info(**info) + return True + def _calc_info(self, name): return getattr(self, '_calc_info_{}'.format(name))() @@ -1857,6 +1880,12 @@ class lapack_opt_info(system_info): "LAPACK order has unacceptable " "values: {}".format(unknown_order)) + if 'NPY_LAPACK_LIBS' in os.environ: + # Bypass autodetection, set language to F77 and use env var linker + # flags directly + self._calc_info_from_envvar() + return + for lapack in lapack_order: if self._calc_info(lapack): return @@ -1979,6 +2008,20 @@ class blas_opt_info(system_info): self.set_info(**info) return True + def _calc_info_from_envvar(self): + info = {} + info['language'] = 'f77' + info['libraries'] = [] + info['include_dirs'] = [] + info['define_macros'] = [] + info['extra_link_args'] = os.environ['NPY_BLAS_LIBS'].split() + if 'NPY_CBLAS_LIBS' in os.environ: + info['define_macros'].append(('HAVE_CBLAS', None)) + info['extra_link_args'].extend( + os.environ['NPY_CBLAS_LIBS'].split()) + self.set_info(**info) + return True + def _calc_info(self, name): return getattr(self, '_calc_info_{}'.format(name))() @@ -1987,6 +2030,12 @@ class blas_opt_info(system_info): if len(unknown_order) > 0: raise ValueError("blas_opt_info user defined BLAS order has unacceptable values: {}".format(unknown_order)) + if 'NPY_BLAS_LIBS' in os.environ: + # Bypass autodetection, set language to F77 and use env var linker + # flags directly + self._calc_info_from_envvar() + return + for blas in blas_order: if self._calc_info(blas): return -- cgit v1.2.1