Browse Source

linkers: fully type annotate and fix mypy issues

This makes GnuDynamicLinker not suitable for instantiation, so the tests
need to be changed to use the BFD class.
tags/0.56.0.rc1
Dylan Baker 1 month ago
parent
commit
3526b36a77
3 changed files with 27 additions and 8 deletions
  1. +24
    -6
      mesonbuild/linkers.py
  2. +1
    -0
      run_mypy.py
  3. +2
    -2
      run_unittests.py

+ 24
- 6
mesonbuild/linkers.py View File

@@ -22,6 +22,7 @@ from .envconfig import get_env_var

if T.TYPE_CHECKING:
from .coredata import OptionDictType
from .envconfig import MachineChoice
from .environment import Environment


@@ -516,6 +517,10 @@ class GnuLikeDynamicLinkerMixin:
other linkers like GNU-ld.
"""

if T.TYPE_CHECKING:
for_machine = MachineChoice.HOST
def _apply_prefix(self, arg: T.Union[str, T.List[str]]) -> T.List[str]: ...

_BUILDTYPE_ARGS = {
'plain': [],
'debug': [],
@@ -761,8 +766,10 @@ class LLVMDynamicLinker(GnuLikeDynamicLinkerMixin, PosixDynamicLinkerMixin, Dyna

id = 'ld.lld'

def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
def __init__(self, exelist: T.List[str],
for_machine: mesonlib.MachineChoice, prefix_arg: T.Union[str, T.List[str]],
always_args: T.List[str], *, version: str = 'unknown version'):
super().__init__(exelist, for_machine, prefix_arg, always_args, version=version)

# Some targets don't seem to support this argument (windows, wasm, ...)
_, _, e = mesonlib.Popen_safe(self.exelist + self._apply_prefix('--allow-shlib-undefined'))
@@ -1052,6 +1059,12 @@ NvidiaHPC_StaticLinker = PGIStaticLinker

class VisualStudioLikeLinkerMixin:

"""Mixin class for for dynamic linkers that act like Microsoft's link.exe."""

if T.TYPE_CHECKING:
for_machine = MachineChoice.HOST
def _apply_prefix(self, arg: T.Union[str, T.List[str]]) -> T.List[str]: ...

_BUILDTYPE_ARGS = {
'plain': [],
'debug': [],
@@ -1063,9 +1076,13 @@ class VisualStudioLikeLinkerMixin:
'custom': [],
} # type: T.Dict[str, T.List[str]]

def __init__(self, *args, direct: bool = True, machine: str = 'x86', **kwargs):
super().__init__(*args, **kwargs)
def __init__(self, exelist: T.List[str], for_machine: mesonlib.MachineChoice,
prefix_arg: T.Union[str, T.List[str]], always_args: T.List[str], *,
version: str = 'unknown version', direct: bool = True, machine: str = 'x86'):
# There's no way I can find to make mypy understand what's going on here
super().__init__(exelist, for_machine, prefix_arg, always_args, version=version) # type: ignore
self.machine = machine
self.direct = direct

def get_buildtype_args(self, buildtype: str) -> T.List[str]:
return mesonlib.listify([self._apply_prefix(a) for a in self._BUILDTYPE_ARGS[buildtype]])
@@ -1077,7 +1094,8 @@ class VisualStudioLikeLinkerMixin:
return self._apply_prefix(['/MACHINE:' + self.machine, '/OUT:' + outputname])

def get_always_args(self) -> T.List[str]:
return self._apply_prefix('/nologo') + super().get_always_args()
parent = super().get_always_args() # type: ignore
return self._apply_prefix('/nologo') + T.cast(T.List[str], parent)

def get_search_args(self, dirname: str) -> T.List[str]:
return self._apply_prefix('/LIBPATH:' + dirname)
@@ -1278,7 +1296,7 @@ class CudaLinker(PosixDynamicLinkerMixin, DynamicLinker):
id = 'nvlink'

@staticmethod
def parse_version():
def parse_version() -> str:
version_cmd = ['nvlink', '--version']
try:
_, out, _ = mesonlib.Popen_safe(version_cmd)


+ 1
- 0
run_mypy.py View File

@@ -21,6 +21,7 @@ modules = [
'mesonbuild/dependencies/mpi.py',
'mesonbuild/envconfig.py',
'mesonbuild/interpreterbase.py',
'mesonbuild/linkers.py',
'mesonbuild/mcompile.py',
'mesonbuild/mesonlib.py',
'mesonbuild/minit.py',


+ 2
- 2
run_unittests.py View File

@@ -465,7 +465,7 @@ class InternalTests(unittest.TestCase):

def test_compiler_args_class_gnuld(self):
## Test --start/end-group
linker = mesonbuild.linkers.GnuDynamicLinker([], MachineChoice.HOST, 'fake', '-Wl,', [])
linker = mesonbuild.linkers.GnuBFDDynamicLinker([], MachineChoice.HOST, '-Wl,', [])
gcc = mesonbuild.compilers.GnuCCompiler([], 'fake', False, MachineChoice.HOST, mock.Mock(), linker=linker)
## Ensure that the fake compiler is never called by overriding the relevant function
gcc.get_default_include_dirs = lambda: ['/usr/include', '/usr/share/include', '/usr/local/include']
@@ -493,7 +493,7 @@ class InternalTests(unittest.TestCase):

def test_compiler_args_remove_system(self):
## Test --start/end-group
linker = mesonbuild.linkers.GnuDynamicLinker([], MachineChoice.HOST, 'fake', '-Wl,', [])
linker = mesonbuild.linkers.GnuBFDDynamicLinker([], MachineChoice.HOST, '-Wl,', [])
gcc = mesonbuild.compilers.GnuCCompiler([], 'fake', False, MachineChoice.HOST, mock.Mock(), linker=linker)
## Ensure that the fake compiler is never called by overriding the relevant function
gcc.get_default_include_dirs = lambda: ['/usr/include', '/usr/share/include', '/usr/local/include']


Loading…
Cancel
Save