Skip to content

Commit

Permalink
interpreter: when overriding a dependency make its name match
Browse files Browse the repository at this point in the history
Otherwise internal dependencies have auto-generated names that are not
human readable. Instead, use the name that the dependency overrides. For
example:

```meson
meson.override_dependency('zlib', declare_dependency())
dep_zlib = dependency('zlib')
assert(dep_zlib.name() == 'zlib')
```

Fixes: #12967
  • Loading branch information
dcbaker committed Mar 15, 2024
1 parent 6d713e4 commit b1340e9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
13 changes: 12 additions & 1 deletion mesonbuild/interpreter/mesonmain.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright 2012-2021 The Meson development team
# Copyright © 2021 Intel Corporation
# Copyright © 2021-2024 Intel Corporation
from __future__ import annotations

import copy
import os
import typing as T

Expand Down Expand Up @@ -347,6 +348,16 @@ def override_dependency_method(self, args: T.Tuple[str, dependencies.Dependency]
if not name:
raise InterpreterException('First argument must be a string and cannot be empty')

# Make a copy since we're going to mutate.
#
# dep = declare_dependency()
# meson.override_dependency('foo', dep)
# meson.override_dependency('foo-1.0', dep)
# dep = dependency('foo')
# dep.name() # == 'foo-1.0'
dep = copy.copy(dep)
dep.name = name

optkey = OptionKey('default_library', subproject=self.interpreter.subproject)
default_library = self.interpreter.coredata.get_option(optkey)
assert isinstance(default_library, str), 'for mypy'
Expand Down
18 changes: 17 additions & 1 deletion test cases/common/98 subproject subdir/meson.build
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# SPDX-License-Identifier: Apache-2.0
# Copyright 2016-2023 The Meson Developers
# Copyright © 2024 Intel Corporation

project('proj', 'c')
subproject('sub')
libSub = dependency('sub', fallback: ['sub', 'libSub'])
Expand All @@ -6,7 +10,19 @@ exe = executable('prog', 'prog.c', dependencies: libSub)
test('subproject subdir', exe)

# Verify the subproject has placed dependency override.
dependency('sub-1.0')
d = dependency('sub-1.0')

# verify that the name is the overridden name
assert(d.name() == 'sub-1.0', 'name was not properly set, should have been "sub-1.0", but was @0@'.format(d.name()))

# Verify that when a dependency object is used for two overrides, the correct
# name is used
meson.override_dependency('new-dep', d)
d2 = dependency('new-dep')
assert(d2.name() == 'new-dep', 'name was not properly set, should have been "new-dep", but was @0@'.format(d2.name()))

# And that the old dependency wasn't changed
assert(d.name() == 'sub-1.0', 'original dependency was mutated.')

# Verify we can now take 'sub' dependency without fallback, but only version 1.0.
dependency('sub')
Expand Down

0 comments on commit b1340e9

Please sign in to comment.