Debian Codegraph @

mesonbuild/dependencies/scalapack.py

raw
1 # SPDX-License-Identifier: Apache-2.0
2 # Copyright 2013-2020 The Meson development team
3
4 from __future__ import annotations
5
6 from pathlib import Path
7 import os
8 import typing as T
9
10 from ..options import OptionKey
11 from .base import DependencyCandidate, DependencyException, DependencyMethods
12 from .cmake import CMakeDependency
13 from .detect import packages
14 from .pkgconfig import PkgConfigDependency
15 from .factory import factory_methods
16
17 if T.TYPE_CHECKING:
18 from ..environment import Environment
19 from .factory import DependencyGenerator
20 from .base import DependencyObjectKWs
21
22
23 @factory_methods({DependencyMethods.PKGCONFIG, DependencyMethods.CMAKE})
24 def scalapack_factory(env: 'Environment',
25 kwargs: DependencyObjectKWs,
26 methods: T.List[DependencyMethods]) -> T.List['DependencyGenerator']:
27 candidates: T.List['DependencyGenerator'] = []
28
29 if DependencyMethods.PKGCONFIG in methods:
30 static_opt = kwargs['static'] if kwargs.get('static') is not None else env.coredata.optstore.get_value_for(OptionKey('prefer_static'))
31 mkl = 'mkl-static-lp64-iomp' if static_opt else 'mkl-dynamic-lp64-iomp'
32 candidates.append(DependencyCandidate.from_dependency(
33 mkl, MKLPkgConfigDependency, (env, kwargs)))
34
35 for pkg in ['scalapack-openmpi', 'scalapack']:
36 candidates.append(DependencyCandidate.from_dependency(
37 pkg, PkgConfigDependency, (env, kwargs)))
38
39 if DependencyMethods.CMAKE in methods:
40 candidates.append(DependencyCandidate.from_dependency(
41 'Scalapack', CMakeDependency, (env, kwargs)))
42
43 return candidates
44
45 packages['scalapack'] = scalapack_factory
46
47
48 class MKLPkgConfigDependency(PkgConfigDependency):
49
50 """PkgConfigDependency for Intel MKL.
51
52 MKL's pkg-config is pretty much borked in every way. We need to apply a
53 bunch of fixups to make it work correctly.
54 """
55
56 def __init__(self, name: str, env: 'Environment', kwargs: DependencyObjectKWs):
57 _m = os.environ.get('MKLROOT')
58 self.__mklroot = Path(_m).resolve() if _m else None
59
60 # We need to call down into the normal super() method even if we don't
61 # find mklroot, otherwise we won't have all of the instance variables
62 # initialized that meson expects.
63 super().__init__(name, env, kwargs)
64
65 # Doesn't work with gcc on windows, but does on Linux
66 if env.machines[self.for_machine].is_windows() and self.clib_compiler.id == 'gcc':
67 self.is_found = False
68
69 # This can happen either because we're using GCC, we couldn't find the
70 # mklroot, or the pkg-config couldn't find it.
71 if not self.is_found:
72 return
73
74 assert self.version != '', 'This should not happen if we didn\'t return above'
75
76 if self.version == 'unknown':
77 # At least by 2020 the version is in the pkg-config, just not with
78 # the correct name
79 v = self.get_variable(pkgconfig='Version', default_value='')
80
81 if not v and self.__mklroot:
82 try:
83 v = (
84 self.__mklroot.as_posix()
85 .split('compilers_and_libraries_')[1]
86 .split('/', 1)[0]
87 )
88 except IndexError:
89 pass
90
91 if v:
92 assert isinstance(v, str)
93 self.version = v
94
95 def _set_libs(self) -> None:
96 if self.__mklroot is None:
97 raise DependencyException('MKLROOT not set')
98
99 super()._set_libs()
100
101 if self.env.machines[self.for_machine].is_windows():
102 suffix = '.lib'
103 elif self.static:
104 suffix = '.a'
105 else:
106 suffix = ''
107 libdir = self.__mklroot / 'lib/intel64'
108
109 if self.clib_compiler.id == 'gcc':
110 for i, a in enumerate(self.link_args):
111 # only replace in filename, not in directory names
112 dirname, basename = os.path.split(a)
113 if 'mkl_intel_lp64' in basename:
114 basename = basename.replace('intel', 'gf')
115 self.link_args[i] = '/' + os.path.join(dirname, basename)
116 # MKL pkg-config omits scalapack
117 # be sure "-L" and "-Wl" are first if present
118 i = 0
119 for j, a in enumerate(self.link_args):
120 if a.startswith(('-L', '-Wl')):
121 i = j + 1
122 elif j > 3:
123 break
124 if self.env.machines[self.for_machine].is_windows() or self.static:
125 self.link_args.insert(
126 i, str(libdir / ('mkl_scalapack_lp64' + suffix))
127 )
128 self.link_args.insert(
129 i + 1, str(libdir / ('mkl_blacs_intelmpi_lp64' + suffix))
130 )
131 else:
132 self.link_args.insert(i, '-lmkl_scalapack_lp64')
133 self.link_args.insert(i + 1, '-lmkl_blacs_intelmpi_lp64')
134
135 def _set_cargs(self) -> None:
136 if self.__mklroot is None:
137 raise DependencyException('MKLROOT not set')
138
139 allow_system = False
140 if self.language == 'fortran':
141 # gfortran doesn't appear to look in system paths for INCLUDE files,
142 # so don't allow pkg-config to suppress -I flags for system paths
143 allow_system = True
144 cflags = self.pkgconfig.cflags(self.name, allow_system, define_variable=(('prefix', self.__mklroot.as_posix()),))
145 self.compile_args = self._convert_mingw_paths(cflags)