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