Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cleanup PerMachine classes, especially nullability #14019

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 22 additions & 35 deletions mesonbuild/utils/universal.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,18 +502,18 @@ def get_prefix(self) -> str:
return MACHINE_PREFIXES[self.value]


@dataclasses.dataclass(eq=False, order=False)
class PerMachine(T.Generic[_T]):
def __init__(self, build: _T, host: _T) -> None:
self.build = build
self.host = host
build: _T
host: _T

def __getitem__(self, machine: MachineChoice) -> _T:
return [self.build, self.host][machine.value]

def __setitem__(self, machine: MachineChoice, val: _T) -> None:
setattr(self, machine.get_lower_case_name(), val)

def miss_defaulting(self) -> "PerMachineDefaultable[T.Optional[_T]]":
def miss_defaulting(self) -> PerMachineDefaultable[T.Optional[_T]]:
"""Unset definition duplicated from their previous to None

This is the inverse of ''default_missing''. By removing defaulted
Expand All @@ -531,20 +531,17 @@ def assign(self, build: _T, host: _T) -> None:
self.build = build
self.host = host

def __repr__(self) -> str:
return f'PerMachine({self.build!r}, {self.host!r})'


@dataclasses.dataclass(eq=False, order=False)
class PerThreeMachine(PerMachine[_T]):
"""Like `PerMachine` but includes `target` too.

It turns out just one thing do we need track the target machine. There's no
need to computer the `target` field so we don't bother overriding the
`__getitem__`/`__setitem__` methods.
"""
def __init__(self, build: _T, host: _T, target: _T) -> None:
super().__init__(build, host)
self.target = target

target: _T

def miss_defaulting(self) -> "PerThreeMachineDefaultable[T.Optional[_T]]":
"""Unset definition duplicated from their previous to None
Expand All @@ -566,29 +563,23 @@ def miss_defaulting(self) -> "PerThreeMachineDefaultable[T.Optional[_T]]":
def matches_build_machine(self, machine: MachineChoice) -> bool:
return self.build == self[machine]

def __repr__(self) -> str:
return f'PerThreeMachine({self.build!r}, {self.host!r}, {self.target!r})'


@dataclasses.dataclass(eq=False, order=False)
class PerMachineDefaultable(PerMachine[T.Optional[_T]]):
"""Extends `PerMachine` with the ability to default from `None`s.
"""
def __init__(self, build: T.Optional[_T] = None, host: T.Optional[_T] = None) -> None:
super().__init__(build, host)

def default_missing(self) -> "PerMachine[_T]":
build: T.Optional[_T] = None
host: T.Optional[_T] = None

def default_missing(self) -> PerMachine[_T]:
"""Default host to build

This allows just specifying nothing in the native case, and just host in the
cross non-compiler case.
"""
freeze = PerMachine(self.build, self.host)
if freeze.host is None:
freeze.host = freeze.build
return freeze

def __repr__(self) -> str:
return f'PerMachineDefaultable({self.build!r}, {self.host!r})'
assert self.build is not None, 'Cannot fill in missing when all fields are empty'
return PerMachine(self.build, self.host if self.host is not None else self.build)

@classmethod
def default(cls, is_cross: bool, build: _T, host: _T) -> PerMachine[_T]:
Expand All @@ -605,28 +596,24 @@ def default(cls, is_cross: bool, build: _T, host: _T) -> PerMachine[_T]:
return m.default_missing()


@dataclasses.dataclass(eq=False, order=False)
class PerThreeMachineDefaultable(PerMachineDefaultable[T.Optional[_T]], PerThreeMachine[T.Optional[_T]]):
"""Extends `PerThreeMachine` with the ability to default from `None`s.
"""
def __init__(self) -> None:
PerThreeMachine.__init__(self, None, None, None)

def default_missing(self) -> "PerThreeMachine[T.Optional[_T]]":
target: T.Optional[_T] = None

def default_missing(self) -> PerThreeMachine[_T]:
"""Default host to build and target to host.

This allows just specifying nothing in the native case, just host in the
cross non-compiler case, and just target in the native-built
cross-compiler case.
"""
freeze = PerThreeMachine(self.build, self.host, self.target)
if freeze.host is None:
freeze.host = freeze.build
if freeze.target is None:
freeze.target = freeze.host
return freeze

def __repr__(self) -> str:
return f'PerThreeMachineDefaultable({self.build!r}, {self.host!r}, {self.target!r})'
assert self.build is not None, 'Cannot default a PerMachine when all values are None'
host = self.host if self.host is not None else self.build
target = self.target if self.target is not None else host
return PerThreeMachine(self.build, host, target)


def is_sunos() -> bool:
Expand Down
Loading