dependencies: switch the delayed-import mechanism for custom dependencies

Simply store the module it is expected to be found in. That module then
appends to the packages dict, which guarantees mypy can verify that
it's got the right type -- there is no casting needed.
pull/11907/head
Eli Schwartz 2 years ago
parent b1ddfabf8f
commit b8b2d87567
No known key found for this signature in database
GPG Key ID: CEB167EFB5722BD6
  1. 86
      mesonbuild/dependencies/__init__.py
  2. 2
      mesonbuild/dependencies/boost.py
  3. 2
      mesonbuild/dependencies/coarrays.py
  4. 3
      mesonbuild/dependencies/cuda.py
  5. 8
      mesonbuild/dependencies/detect.py
  6. 15
      mesonbuild/dependencies/dev.py
  7. 3
      mesonbuild/dependencies/hdf5.py
  8. 33
      mesonbuild/dependencies/misc.py
  9. 3
      mesonbuild/dependencies/mpi.py
  10. 3
      mesonbuild/dependencies/platform.py
  11. 5
      mesonbuild/dependencies/python.py
  12. 7
      mesonbuild/dependencies/qt.py
  13. 3
      mesonbuild/dependencies/scalapack.py
  14. 10
      mesonbuild/dependencies/ui.py

@ -192,62 +192,66 @@ this approach, and no new dependencies should do this.
# - An ExternalDependency subclass
# - A DependencyFactory object
# - A callable with a signature of (Environment, MachineChoice, Dict[str, Any]) -> List[Callable[[], ExternalDependency]]
#
# The internal "defaults" attribute contains a separate dictionary mapping
# for lazy imports. The values must be:
# - a string naming the submodule that should be imported from `mesonbuild.dependencies` to populate the dependency
packages.defaults.update({
# From dev:
'gtest': 'dev:gtest_factory',
'gmock': 'dev:gmock_factory',
'llvm': 'dev:llvm_factory',
'valgrind': 'dev:ValgrindDependency',
'zlib': 'dev:zlib_factory',
'jni': 'dev:JNISystemDependency',
'jdk': 'dev:JDKSystemDependency',
'gtest': 'dev',
'gmock': 'dev',
'llvm': 'dev',
'valgrind': 'dev',
'zlib': 'dev',
'jni': 'dev',
'jdk': 'dev',
'boost': 'boost:BoostDependency',
'cuda': 'cuda:CudaDependency',
'boost': 'boost',
'cuda': 'cuda',
# per-file
'coarray': 'coarrays:coarray_factory',
'hdf5': 'hdf5:hdf5_factory',
'mpi': 'mpi:mpi_factory',
'scalapack': 'scalapack:scalapack_factory',
'coarray': 'coarrays',
'hdf5': 'hdf5',
'mpi': 'mpi',
'scalapack': 'scalapack',
# From misc:
'blocks': 'misc:BlocksDependency',
'curses': 'misc:curses_factory',
'netcdf': 'misc:netcdf_factory',
'openmp': 'misc:OpenMPDependency',
'threads': 'misc:threads_factory',
'pcap': 'misc:pcap_factory',
'cups': 'misc:cups_factory',
'libwmf': 'misc:libwmf_factory',
'libgcrypt': 'misc:libgcrypt_factory',
'gpgme': 'misc:gpgme_factory',
'shaderc': 'misc:shaderc_factory',
'iconv': 'misc:iconv_factory',
'intl': 'misc:intl_factory',
'dl': 'misc:dl_factory',
'openssl': 'misc:openssl_factory',
'libcrypto': 'misc:libcrypto_factory',
'libssl': 'misc:libssl_factory',
'blocks': 'misc',
'curses': 'misc',
'netcdf': 'misc',
'openmp': 'misc',
'threads': 'misc',
'pcap': 'misc',
'cups': 'misc',
'libwmf': 'misc',
'libgcrypt': 'misc',
'gpgme': 'misc',
'shaderc': 'misc',
'iconv': 'misc',
'intl': 'misc',
'dl': 'misc',
'openssl': 'misc',
'libcrypto': 'misc',
'libssl': 'misc',
# From platform:
'appleframeworks': 'platform:AppleFrameworks',
'appleframeworks': 'platform',
# from python:
'python3': 'python:python_factory',
'pybind11': 'python:pybind11_factory',
'python3': 'python',
'pybind11': 'python',
# From ui:
'gl': 'ui:gl_factory',
'gnustep': 'ui:GnuStepDependency',
'sdl2': 'ui:sdl2_factory',
'wxwidgets': 'ui:WxDependency',
'vulkan': 'ui:vulkan_factory',
'gl': 'ui',
'gnustep': 'ui',
'sdl2': 'ui',
'wxwidgets': 'ui',
'vulkan': 'ui',
# from qt
'qt4': 'qt:qt4_factory',
'qt5': 'qt:qt5_factory',
'qt6': 'qt:qt6_factory',
'qt4': 'qt',
'qt5': 'qt',
'qt6': 'qt',
})
_packages_accept_language.update({
'hdf5',

@ -23,6 +23,7 @@ from .. import mlog
from .. import mesonlib
from .base import DependencyException, SystemDependency
from .detect import packages
from .pkgconfig import PkgConfigDependency
from .misc import threads_factory
@ -738,6 +739,7 @@ class BoostDependency(SystemDependency):
# BOOST_ALL_DYN_LINK should not be required with the known defines below
return ['-DBOOST_ALL_NO_LIB'] # Disable automatic linking
packages['boost'] = BoostDependency
# See https://www.boost.org/doc/libs/1_72_0/more/getting_started/unix-variants.html#library-naming
# See https://mesonbuild.com/Reference-tables.html#cpu-families

@ -18,6 +18,7 @@ import typing as T
from .base import DependencyMethods, detect_compiler, SystemDependency
from .cmake import CMakeDependency
from .detect import packages
from .pkgconfig import PkgConfigDependency
from .factory import factory_methods
@ -51,6 +52,7 @@ def coarray_factory(env: 'Environment',
candidates.append(functools.partial(CoarrayDependency, env, kwargs))
return candidates
packages['coarray'] = coarray_factory
class CoarrayDependency(SystemDependency):

@ -23,6 +23,7 @@ from .. import mesonlib
from .. import mlog
from ..environment import detect_cpu_family
from .base import DependencyException, SystemDependency
from .detect import packages
if T.TYPE_CHECKING:
@ -290,3 +291,5 @@ class CudaDependency(SystemDependency):
for lib in self.requested_modules:
args += self.lib_modules[lib]
return args
packages['cuda'] = CudaDependency

@ -35,12 +35,10 @@ class DependencyPackages(collections.UserDict):
def __missing__(self, key: str) -> PackageTypes:
if key in self.defaults:
modn, package = self.defaults[key].split(':', maxsplit=1)
mod = importlib.import_module(f'mesonbuild.dependencies.{modn}')
value = T.cast('PackageTypes', getattr(mod, package))
self.data[key] = value
modn = self.defaults[key]
importlib.import_module(f'mesonbuild.dependencies.{modn}')
return value
return self.data[key]
raise KeyError(key)
def __contains__(self, key: object) -> bool:

@ -34,6 +34,7 @@ from ..mesonlib import version_compare, version_compare_many, search_version, st
from .base import DependencyException, DependencyMethods, detect_compiler, strip_system_includedirs, strip_system_libdirs, SystemDependency, ExternalDependency, DependencyTypeName
from .cmake import CMakeDependency
from .configtool import ConfigToolDependency
from .detect import packages
from .factory import DependencyFactory
from .misc import threads_factory
from .pkgconfig import PkgConfigDependency
@ -507,6 +508,8 @@ class ValgrindDependency(PkgConfigDependency):
def get_link_args(self, language: T.Optional[str] = None, raw: bool = False) -> T.List[str]:
return []
packages['valgrind'] = ValgrindDependency
class ZlibSystemDependency(SystemDependency):
@ -671,6 +674,8 @@ class JNISystemDependency(SystemDependency):
return None
packages['jni'] = JNISystemDependency
class JDKSystemDependency(JNISystemDependency):
def __init__(self, environment: 'Environment', kwargs: JNISystemDependencyKW):
@ -683,29 +688,31 @@ class JDKSystemDependency(JNISystemDependency):
'Use the jni system dependency instead'
))
packages['jdk'] = JDKSystemDependency
llvm_factory = DependencyFactory(
packages['llvm'] = llvm_factory = DependencyFactory(
'LLVM',
[DependencyMethods.CMAKE, DependencyMethods.CONFIG_TOOL],
cmake_class=LLVMDependencyCMake,
configtool_class=LLVMDependencyConfigTool,
)
gtest_factory = DependencyFactory(
packages['gtest'] = gtest_factory = DependencyFactory(
'gtest',
[DependencyMethods.PKGCONFIG, DependencyMethods.SYSTEM],
pkgconfig_class=GTestDependencyPC,
system_class=GTestDependencySystem,
)
gmock_factory = DependencyFactory(
packages['gmock'] = gmock_factory = DependencyFactory(
'gmock',
[DependencyMethods.PKGCONFIG, DependencyMethods.SYSTEM],
pkgconfig_class=GMockDependencyPC,
system_class=GMockDependencySystem,
)
zlib_factory = DependencyFactory(
packages['zlib'] = zlib_factory = DependencyFactory(
'zlib',
[DependencyMethods.PKGCONFIG, DependencyMethods.CMAKE, DependencyMethods.SYSTEM],
cmake_name='ZLIB',

@ -25,6 +25,7 @@ from ..mesonlib import Popen_safe, OrderedSet, join_args
from ..programs import ExternalProgram
from .base import DependencyException, DependencyMethods
from .configtool import ConfigToolDependency
from .detect import packages
from .pkgconfig import PkgConfigDependency
from .factory import factory_methods
import typing as T
@ -178,3 +179,5 @@ def hdf5_factory(env: 'Environment', for_machine: 'MachineChoice',
candidates.append(functools.partial(HDF5ConfigToolDependency, 'hdf5', env, kwargs, language))
return candidates
packages['hdf5'] = hdf5_factory

@ -25,6 +25,7 @@ from .base import DependencyException, DependencyMethods
from .base import BuiltinDependency, SystemDependency
from .cmake import CMakeDependency
from .configtool import ConfigToolDependency
from .detect import packages
from .factory import DependencyFactory, factory_methods
from .pkgconfig import PkgConfigDependency
@ -57,6 +58,8 @@ def netcdf_factory(env: 'Environment',
return candidates
packages['netcdf'] = netcdf_factory
class DlBuiltinDependency(BuiltinDependency):
def __init__(self, name: str, env: 'Environment', kwargs: T.Dict[str, T.Any]):
@ -136,6 +139,8 @@ class OpenMPDependency(SystemDependency):
if not self.is_found:
mlog.log(mlog.yellow('WARNING:'), 'OpenMP found but omp.h missing.')
packages['openmp'] = OpenMPDependency
class ThreadDependency(SystemDependency):
def __init__(self, name: str, environment: 'Environment', kwargs: T.Dict[str, T.Any]) -> None:
@ -183,6 +188,8 @@ class BlocksDependency(SystemDependency):
self.is_found = True
packages['blocks'] = BlocksDependency
class PcapDependencyConfigTool(ConfigToolDependency):
@ -489,6 +496,7 @@ def curses_factory(env: 'Environment',
candidates.append(functools.partial(CursesSystemDependency, 'curses', env, kwargs))
return candidates
packages['curses'] = curses_factory
@factory_methods({DependencyMethods.PKGCONFIG, DependencyMethods.SYSTEM})
@ -524,83 +532,84 @@ def shaderc_factory(env: 'Environment',
candidates.append(functools.partial(ShadercDependency, env, kwargs))
return candidates
packages['shaderc'] = shaderc_factory
cups_factory = DependencyFactory(
packages['cups'] = cups_factory = DependencyFactory(
'cups',
[DependencyMethods.PKGCONFIG, DependencyMethods.CONFIG_TOOL, DependencyMethods.EXTRAFRAMEWORK, DependencyMethods.CMAKE],
configtool_class=CupsDependencyConfigTool,
cmake_name='Cups',
)
dl_factory = DependencyFactory(
packages['dl'] = dl_factory = DependencyFactory(
'dl',
[DependencyMethods.BUILTIN, DependencyMethods.SYSTEM],
builtin_class=DlBuiltinDependency,
system_class=DlSystemDependency,
)
gpgme_factory = DependencyFactory(
packages['gpgme'] = gpgme_factory = DependencyFactory(
'gpgme',
[DependencyMethods.PKGCONFIG, DependencyMethods.CONFIG_TOOL],
configtool_class=GpgmeDependencyConfigTool,
)
libgcrypt_factory = DependencyFactory(
packages['libgcrypt'] = libgcrypt_factory = DependencyFactory(
'libgcrypt',
[DependencyMethods.PKGCONFIG, DependencyMethods.CONFIG_TOOL],
configtool_class=LibGCryptDependencyConfigTool,
)
libwmf_factory = DependencyFactory(
packages['libwmf'] = libwmf_factory = DependencyFactory(
'libwmf',
[DependencyMethods.PKGCONFIG, DependencyMethods.CONFIG_TOOL],
configtool_class=LibWmfDependencyConfigTool,
)
pcap_factory = DependencyFactory(
packages['pcap'] = pcap_factory = DependencyFactory(
'pcap',
[DependencyMethods.PKGCONFIG, DependencyMethods.CONFIG_TOOL],
configtool_class=PcapDependencyConfigTool,
pkgconfig_name='libpcap',
)
threads_factory = DependencyFactory(
packages['threads'] = threads_factory = DependencyFactory(
'threads',
[DependencyMethods.SYSTEM, DependencyMethods.CMAKE],
cmake_name='Threads',
system_class=ThreadDependency,
)
iconv_factory = DependencyFactory(
packages['iconv'] = iconv_factory = DependencyFactory(
'iconv',
[DependencyMethods.BUILTIN, DependencyMethods.SYSTEM],
builtin_class=IconvBuiltinDependency,
system_class=IconvSystemDependency,
)
intl_factory = DependencyFactory(
packages['intl'] = intl_factory = DependencyFactory(
'intl',
[DependencyMethods.BUILTIN, DependencyMethods.SYSTEM],
builtin_class=IntlBuiltinDependency,
system_class=IntlSystemDependency,
)
openssl_factory = DependencyFactory(
packages['openssl'] = openssl_factory = DependencyFactory(
'openssl',
[DependencyMethods.PKGCONFIG, DependencyMethods.SYSTEM, DependencyMethods.CMAKE],
system_class=OpensslSystemDependency,
cmake_class=lambda name, env, kwargs: CMakeDependency('OpenSSL', env, dict(kwargs, modules=['OpenSSL::Crypto', 'OpenSSL::SSL'])),
)
libcrypto_factory = DependencyFactory(
packages['libcrypto'] = libcrypto_factory = DependencyFactory(
'libcrypto',
[DependencyMethods.PKGCONFIG, DependencyMethods.SYSTEM, DependencyMethods.CMAKE],
system_class=OpensslSystemDependency,
cmake_class=lambda name, env, kwargs: CMakeDependency('OpenSSL', env, dict(kwargs, modules=['OpenSSL::Crypto'])),
)
libssl_factory = DependencyFactory(
packages['libssl'] = libssl_factory = DependencyFactory(
'libssl',
[DependencyMethods.PKGCONFIG, DependencyMethods.SYSTEM, DependencyMethods.CMAKE],
system_class=OpensslSystemDependency,

@ -21,6 +21,7 @@ import re
from ..environment import detect_cpu_family
from .base import DependencyMethods, detect_compiler, SystemDependency
from .configtool import ConfigToolDependency
from .detect import packages
from .factory import factory_methods
from .pkgconfig import PkgConfigDependency
@ -100,6 +101,8 @@ def mpi_factory(env: 'Environment',
return candidates
packages['mpi'] = mpi_factory
class _MPIConfigToolDependency(ConfigToolDependency):

@ -17,6 +17,7 @@
from __future__ import annotations
from .base import DependencyTypeName, ExternalDependency, DependencyException
from .detect import packages
from ..mesonlib import MesonException
import typing as T
@ -58,3 +59,5 @@ class AppleFrameworks(ExternalDependency):
@staticmethod
def log_tried() -> str:
return 'framework'
packages['appleframeworks'] = AppleFrameworks

@ -20,6 +20,7 @@ import typing as T
from .. import mesonlib, mlog
from .base import process_method_kw, DependencyMethods, DependencyTypeName, ExternalDependency, SystemDependency
from .configtool import ConfigToolDependency
from .detect import packages
from .factory import DependencyFactory
from .framework import ExtraFrameworkDependency
from .pkgconfig import PkgConfigDependency
@ -407,7 +408,9 @@ def python_factory(env: 'Environment', for_machine: 'MachineChoice',
return candidates
pybind11_factory = DependencyFactory(
packages['python3'] = python_factory
packages['pybind11'] = pybind11_factory = DependencyFactory(
'pybind11',
[DependencyMethods.PKGCONFIG, DependencyMethods.CONFIG_TOOL, DependencyMethods.CMAKE],
configtool_class=Pybind11ConfigToolDependency,

@ -24,6 +24,7 @@ import typing as T
from .base import DependencyException, DependencyMethods
from .configtool import ConfigToolDependency
from .detect import packages
from .framework import ExtraFrameworkDependency
from .pkgconfig import PkgConfigDependency
from .factory import DependencyFactory
@ -467,21 +468,21 @@ class Qt6PkgConfigDependency(Qt6WinMainMixin, QtPkgConfigDependency):
return _qt_get_private_includes(mod_inc_dir, module, self.version)
qt4_factory = DependencyFactory(
packages['qt4'] = qt4_factory = DependencyFactory(
'qt4',
[DependencyMethods.PKGCONFIG, DependencyMethods.CONFIG_TOOL],
pkgconfig_class=Qt4PkgConfigDependency,
configtool_class=Qt4ConfigToolDependency,
)
qt5_factory = DependencyFactory(
packages['qt5'] = qt5_factory = DependencyFactory(
'qt5',
[DependencyMethods.PKGCONFIG, DependencyMethods.CONFIG_TOOL],
pkgconfig_class=Qt5PkgConfigDependency,
configtool_class=Qt5ConfigToolDependency,
)
qt6_factory = DependencyFactory(
packages['qt6'] = qt6_factory = DependencyFactory(
'qt6',
[DependencyMethods.PKGCONFIG, DependencyMethods.CONFIG_TOOL],
pkgconfig_class=Qt6PkgConfigDependency,

@ -22,6 +22,7 @@ from ..mesonlib import OptionKey
from .base import DependencyMethods
from .base import DependencyException
from .cmake import CMakeDependency
from .detect import packages
from .pkgconfig import PkgConfigDependency
from .factory import factory_methods
@ -52,6 +53,8 @@ def scalapack_factory(env: 'Environment', for_machine: 'MachineChoice',
return candidates
packages['scalapack'] = scalapack_factory
class MKLPkgConfigDependency(PkgConfigDependency):

@ -29,6 +29,7 @@ from ..environment import detect_cpu_family
from .base import DependencyException, DependencyMethods, DependencyTypeName, SystemDependency
from .configtool import ConfigToolDependency
from .detect import packages
from .factory import DependencyFactory
if T.TYPE_CHECKING:
@ -136,6 +137,8 @@ class GnuStepDependency(ConfigToolDependency):
version = '1'
return version
packages['gnustep'] = GnuStepDependency
class SDL2DependencyConfigTool(ConfigToolDependency):
@ -187,6 +190,7 @@ class WxDependency(ConfigToolDependency):
raise DependencyException('wxwidgets module argument is not a string')
return candidates
packages['wxwidgets'] = WxDependency
class VulkanDependencySystem(SystemDependency):
@ -244,20 +248,20 @@ class VulkanDependencySystem(SystemDependency):
self.link_args.append(lib)
return
gl_factory = DependencyFactory(
packages['gl'] = gl_factory = DependencyFactory(
'gl',
[DependencyMethods.PKGCONFIG, DependencyMethods.SYSTEM],
system_class=GLDependencySystem,
)
sdl2_factory = DependencyFactory(
packages['sdl2'] = sdl2_factory = DependencyFactory(
'sdl2',
[DependencyMethods.PKGCONFIG, DependencyMethods.CONFIG_TOOL, DependencyMethods.EXTRAFRAMEWORK, DependencyMethods.CMAKE],
configtool_class=SDL2DependencyConfigTool,
cmake_name='SDL2',
)
vulkan_factory = DependencyFactory(
packages['vulkan'] = vulkan_factory = DependencyFactory(
'vulkan',
[DependencyMethods.PKGCONFIG, DependencyMethods.SYSTEM],
system_class=VulkanDependencySystem,

Loading…
Cancel
Save