dependency: define equality and hash operators for Dependency

When a dependency is copied and its name is changed, we still need a way
to say "this is the same dependency", which we now have. It is
important, however, when a partial dependency is created that dependency
does *not* match the original dependency, since they are not providing
the same data. This was always happening for InternalDependencies as an
implementation detail, but not for other kinds.
pull/13709/head
Dylan Baker 9 months ago committed by Eli Schwartz
parent 726d9c0b61
commit 6e98767c31
  1. 19
      mesonbuild/dependencies/base.py

@ -1,5 +1,6 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright 2013-2018 The Meson development team
# Copyright © 2024 Intel Corporation
# This file contains the detection logic for external dependencies.
# Custom logic for several other packages are in separate files.
@ -10,6 +11,7 @@ import os
import collections
import itertools
import typing as T
import uuid
from enum import Enum
from .. import mlog, mesonlib
@ -106,6 +108,9 @@ class Dependency(HoldableObject):
return kwargs['include_type']
def __init__(self, type_name: DependencyTypeName, kwargs: T.Dict[str, T.Any]) -> None:
# This allows two Dependencies to be compared even after being copied.
# The purpose is to allow the name to be changed, but still have a proper comparison
self._id = uuid.uuid4().int
self.name = f'dep{id(self)}'
self.version: T.Optional[str] = None
self.language: T.Optional[str] = None # None means C-like
@ -124,6 +129,14 @@ class Dependency(HoldableObject):
self.featurechecks: T.List['FeatureCheckBase'] = []
self.feature_since: T.Optional[T.Tuple[str, str]] = None
def __eq__(self, other: object) -> bool:
if not isinstance(other, Dependency):
return NotImplemented
return self._id == other._id
def __hash__(self) -> int:
return self._id
def __repr__(self) -> str:
return f'<{self.__class__.__name__} {self.name}: {self.is_found}>'
@ -402,6 +415,7 @@ class ExternalDependency(Dependency, HasNativeKwarg):
link_args: bool = False, links: bool = False,
includes: bool = False, sources: bool = False) -> Dependency:
new = copy.copy(self)
new._id = uuid.uuid4().int
if not compile_args:
new.compile_args = []
if not link_args:
@ -472,7 +486,9 @@ class NotFoundDependency(Dependency):
def get_partial_dependency(self, *, compile_args: bool = False,
link_args: bool = False, links: bool = False,
includes: bool = False, sources: bool = False) -> 'NotFoundDependency':
return copy.copy(self)
new = copy.copy(self)
new._id = uuid.uuid4().int
return new
class ExternalLibrary(ExternalDependency):
@ -512,6 +528,7 @@ class ExternalLibrary(ExternalDependency):
# External library only has link_args, so ignore the rest of the
# interface.
new = copy.copy(self)
new._id = uuid.uuid4().int
if not link_args:
new.link_args = []
return new

Loading…
Cancel
Save