Make use of unholder

We have a lot of cases of code like:
```python
if hasattr(var, 'held_object'):
    var = var.held_object`
```

replace that with the unholder function.
pull/6736/head
Dylan Baker 5 years ago
parent 4d6ac91f95
commit 06b1a317d2
  1. 18
      mesonbuild/backend/backends.py
  2. 7
      mesonbuild/backend/ninjabackend.py
  3. 67
      mesonbuild/build.py
  4. 4
      mesonbuild/interpreter.py
  5. 8
      mesonbuild/modules/__init__.py
  6. 39
      mesonbuild/modules/gnome.py
  7. 6
      mesonbuild/modules/qt.py
  8. 6
      mesonbuild/modules/windows.py

@ -21,7 +21,7 @@ from .. import mlog
import json
import subprocess
from ..mesonlib import MachineChoice, MesonException, OrderedSet, OptionOverrideProxy
from ..mesonlib import classify_unity_sources
from ..mesonlib import classify_unity_sources, unholder
from ..mesonlib import File
from ..compilers import CompilerArgs, VisualStudioLikeCompiler
from ..interpreter import Interpreter
@ -748,9 +748,7 @@ class Backend:
else:
extra_paths = []
cmd_args = []
for a in t.cmd_args:
if hasattr(a, 'held_object'):
a = a.held_object
for a in unholder(t.cmd_args):
if isinstance(a, build.BuildTarget):
extra_paths += self.determine_windows_extra_paths(a, [])
if isinstance(a, mesonlib.File):
@ -868,14 +866,10 @@ class Backend:
# also be built by default. XXX: Sometime in the future these should be
# built only before running tests.
for t in self.build.get_tests():
exe = t.exe
if hasattr(exe, 'held_object'):
exe = exe.held_object
exe = unholder(t.exe)
if isinstance(exe, (build.CustomTarget, build.BuildTarget)):
result[exe.get_id()] = exe
for arg in t.cmd_args:
if hasattr(arg, 'held_object'):
arg = arg.held_object
for arg in unholder(t.cmd_args):
if not isinstance(arg, (build.CustomTarget, build.BuildTarget)):
continue
result[arg.get_id()] = arg
@ -915,9 +909,7 @@ class Backend:
Returns the path to them relative to the build root directory.
'''
srcs = []
for i in target.get_sources():
if hasattr(i, 'held_object'):
i = i.held_object
for i in unholder(target.get_sources()):
if isinstance(i, str):
fname = [os.path.join(self.build_to_src, target.subdir, i)]
elif isinstance(i, build.BuildTarget):

@ -32,7 +32,8 @@ from ..compilers import (Compiler, CompilerArgs, CCompiler, FortranCompiler,
PGICCompiler, VisualStudioLikeCompiler)
from ..linkers import ArLinker
from ..mesonlib import (
File, LibType, MachineChoice, MesonException, OrderedSet, PerMachine, ProgressBar, quote_arg
File, LibType, MachineChoice, MesonException, OrderedSet, PerMachine,
ProgressBar, quote_arg, unholder,
)
from ..mesonlib import get_compiler_for_source, has_path_sep
from .backends import CleanTrees
@ -648,9 +649,7 @@ int dummy;
self.generate_target(t)
def custom_target_generator_inputs(self, target):
for s in target.sources:
if hasattr(s, 'held_object'):
s = s.held_object
for s in unholder(target.sources):
if isinstance(s, build.GeneratedList):
self.generate_genlist_for_target(s, target)

@ -534,9 +534,7 @@ class BuildTarget(Target):
def process_objectlist(self, objects):
assert(isinstance(objects, list))
for s in objects:
if hasattr(s, 'held_object'):
s = s.held_object
for s in unholder(objects):
if isinstance(s, (str, File, ExtractedObjects)):
self.objects.append(s)
elif isinstance(s, (GeneratedList, CustomTarget)):
@ -552,10 +550,7 @@ class BuildTarget(Target):
def process_sourcelist(self, sources):
sources = listify(sources)
added_sources = {} # If the same source is defined multiple times, use it only once.
for s in sources:
# Holder unpacking. Ugly.
if hasattr(s, 'held_object'):
s = s.held_object
for s in unholder(sources):
if isinstance(s, File):
if s not in added_sources:
self.sources.append(s)
@ -632,9 +627,7 @@ class BuildTarget(Target):
# which is what we need.
if not is_object(s):
sources.append(s)
for d in self.external_deps:
if hasattr(d, 'held_object'):
d = d.held_object
for d in unholder(self.external_deps):
for s in d.sources:
if isinstance(s, (str, File)):
sources.append(s)
@ -696,10 +689,7 @@ class BuildTarget(Target):
link_depends.
"""
sources = listify(sources)
for s in sources:
if hasattr(s, 'held_object'):
s = s.held_object
for s in unholder(sources):
if isinstance(s, File):
self.link_depends.append(s)
elif isinstance(s, str):
@ -810,11 +800,7 @@ class BuildTarget(Target):
kwargs.get('modules', [])
self.need_install = kwargs.get('install', self.need_install)
llist = extract_as_list(kwargs, 'link_with')
for linktarget in llist:
# Sorry for this hack. Keyword targets are kept in holders
# in kwargs. Unpack here without looking at the exact type.
if hasattr(linktarget, "held_object"):
linktarget = linktarget.held_object
for linktarget in unholder(llist):
if isinstance(linktarget, dependencies.ExternalLibrary):
raise MesonException('''An external library was used in link_with keyword argument, which
is reserved for libraries built as part of this project. External
@ -1033,9 +1019,7 @@ This will become a hard error in a future Meson release.''')
def add_deps(self, deps):
deps = listify(deps)
for dep in deps:
if hasattr(dep, 'held_object'):
dep = dep.held_object
for dep in unholder(deps):
if isinstance(dep, dependencies.InternalDependency):
# Those parts that are internal.
self.process_sourcelist(dep.sources)
@ -1170,10 +1154,7 @@ You probably should put it in link_with instead.''')
def add_include_dirs(self, args, set_is_system: T.Optional[str] = None):
ids = []
for a in args:
# FIXME same hack, forcibly unpack from holder.
if hasattr(a, 'held_object'):
a = a.held_object
for a in unholder(args):
if not isinstance(a, IncludeDirs):
raise InvalidArguments('Include directory to be added is not an include directory object.')
ids.append(a)
@ -1313,9 +1294,7 @@ class Generator:
def __init__(self, args, kwargs):
if len(args) != 1:
raise InvalidArguments('Generator requires exactly one positional argument: the executable')
exe = args[0]
if hasattr(exe, 'held_object'):
exe = exe.held_object
exe = unholder(args[0])
if not isinstance(exe, (Executable, dependencies.ExternalProgram)):
raise InvalidArguments('First generator argument must be an executable.')
self.exe = exe
@ -1416,9 +1395,7 @@ class Generator:
class GeneratedList:
def __init__(self, generator, subdir, preserve_path_from=None, extra_args=None):
if hasattr(generator, 'held_object'):
generator = generator.held_object
self.generator = generator
self.generator = unholder(generator)
self.name = self.generator.exe
self.subdir = subdir
self.infilelist = []
@ -1428,10 +1405,10 @@ class GeneratedList:
self.depend_files = []
self.preserve_path_from = preserve_path_from
self.extra_args = extra_args if extra_args is not None else []
if isinstance(generator.exe, dependencies.ExternalProgram):
if not generator.exe.found():
if isinstance(self.generator.exe, dependencies.ExternalProgram):
if not self.generator.exe.found():
raise InvalidArguments('Tried to use not-found external program as generator')
path = generator.exe.get_path()
path = self.generator.exe.get_path()
if os.path.isabs(path):
# Can only add a dependency on an external program which we
# know the absolute path of
@ -1873,9 +1850,7 @@ class SharedLibrary(BuildTarget):
# Visual Studio module-definitions file
if 'vs_module_defs' in kwargs:
path = kwargs['vs_module_defs']
if hasattr(path, 'held_object'):
path = path.held_object
path = unholder(kwargs['vs_module_defs'])
if isinstance(path, str):
if os.path.isabs(path):
self.vs_module_defs = File.from_absolute_file(path)
@ -2027,9 +2002,7 @@ class CustomTarget(Target):
def get_target_dependencies(self):
deps = self.dependencies[:]
deps += self.extra_depends
for c in self.sources:
if hasattr(c, 'held_object'):
c = c.held_object
for c in unholder(self.sources):
if isinstance(c, (BuildTarget, CustomTarget)):
deps.append(c)
return deps
@ -2161,9 +2134,7 @@ class CustomTarget(Target):
if not isinstance(self.build_always_stale, bool):
raise InvalidArguments('Argument build_always_stale must be a boolean.')
extra_deps, depend_files = [extract_as_list(kwargs, c, pop=False) for c in ['depends', 'depend_files']]
for ed in extra_deps:
while hasattr(ed, 'held_object'):
ed = ed.held_object
for ed in unholder(extra_deps):
if not isinstance(ed, (CustomTarget, BuildTarget)):
raise InvalidArguments('Can only depend on toplevel targets: custom_target or build_target (executable or a library) got: {}({})'
.format(type(ed), ed))
@ -2198,9 +2169,7 @@ class CustomTarget(Target):
def get_generated_lists(self):
genlists = []
for c in self.sources:
if hasattr(c, 'held_object'):
c = c.held_object
for c in unholder(self.sources):
if isinstance(c, GeneratedList):
genlists.append(c)
return genlists
@ -2463,9 +2432,7 @@ def get_sources_string_names(sources, backend):
get all the output basenames.
'''
names = []
for s in sources:
if hasattr(s, 'held_object'):
s = s.held_object
for s in unholder(sources):
if isinstance(s, str):
names.append(s)
elif isinstance(s, (BuildTarget, CustomTarget, CustomTargetIndex, GeneratedList)):

@ -3087,9 +3087,7 @@ external dependencies (including libraries) must go to "dependencies".''')
def program_from_file_for(self, for_machine, prognames, silent):
bins = self.environment.binaries[for_machine]
for p in prognames:
if hasattr(p, 'held_object'):
p = p.held_object
for p in unholder(prognames):
if isinstance(p, mesonlib.File):
continue # Always points to a local (i.e. self generated) file.
if not isinstance(p, str):

@ -18,6 +18,7 @@
import os
from .. import build
from ..mesonlib import unholder
class ExtensionModule:
@ -38,12 +39,7 @@ def get_include_args(include_dirs, prefix='-I'):
return []
dirs_str = []
for incdirs in include_dirs:
if hasattr(incdirs, "held_object"):
dirs = incdirs.held_object
else:
dirs = incdirs
for dirs in unholder(include_dirs):
if isinstance(dirs, str):
dirs_str += ['%s%s' % (prefix, dirs)]
continue

@ -29,7 +29,8 @@ from . import get_include_args
from . import ExtensionModule
from . import ModuleReturnValue
from ..mesonlib import (
MachineChoice, MesonException, OrderedSet, Popen_safe, extract_as_list, join_args
MachineChoice, MesonException, OrderedSet, Popen_safe, extract_as_list,
join_args, unholder,
)
from ..dependencies import Dependency, PkgConfigDependency, InternalDependency
from ..interpreterbase import noKwargs, permittedKwargs, FeatureNew, FeatureNewKwargs
@ -238,9 +239,7 @@ class GnomeModule(ExtensionModule):
subdirs = []
for resfile in dep_files[:]:
resbasename = os.path.basename(resfile)
for dep in dependencies:
if hasattr(dep, 'held_object'):
dep = dep.held_object
for dep in unholder(dependencies):
if isinstance(dep, mesonlib.File):
if dep.fname != resbasename:
continue
@ -322,9 +321,7 @@ class GnomeModule(ExtensionModule):
if isinstance(dep, InternalDependency):
cflags.update(dep.get_compile_args())
cflags.update(get_include_args(dep.include_directories))
for lib in dep.libraries:
if hasattr(lib, 'held_object'):
lib = lib.held_object
for lib in unholder(dep.libraries):
if isinstance(lib, build.SharedLibrary):
internal_ldflags.update(self._get_link_args(state, lib, depends, include_rpath))
libdepflags = self._get_dependencies_flags(lib.get_external_deps(), state, depends, include_rpath,
@ -341,9 +338,7 @@ class GnomeModule(ExtensionModule):
external_ldflags.update(extdepflags[2])
external_ldflags_nodedup += extdepflags[3]
gi_includes.update(extdepflags[4])
for source in dep.sources:
if hasattr(source, 'held_object'):
source = source.held_object
for source in unholder(dep.sources):
if isinstance(source, GirTarget):
gi_includes.update([os.path.join(state.environment.get_build_dir(),
source.get_subdir())])
@ -457,9 +452,7 @@ class GnomeModule(ExtensionModule):
if 'includes' in kwargs:
includes = mesonlib.extract_as_list(kwargs, 'includes', pop = True)
for inc in includes:
if hasattr(inc, 'held_object'):
inc = inc.held_object
for inc in unholder(includes):
if isinstance(inc, str):
ret += ['--include=%s' % (inc, )]
elif isinstance(inc, GirTarget):
@ -616,9 +609,7 @@ class GnomeModule(ExtensionModule):
gir_filelist_filename = os.path.join(gir_filelist_dir, '%s_%s_gir_filelist' % (ns, nsversion))
with open(gir_filelist_filename, 'w', encoding='utf-8') as gir_filelist:
for s in libsources:
if hasattr(s, 'held_object'):
s = s.held_object
for s in unholder(libsources):
if isinstance(s, (build.CustomTarget, build.CustomTargetIndex)):
for custom_output in s.get_outputs():
gir_filelist.write(os.path.join(state.environment.get_build_dir(),
@ -671,15 +662,11 @@ class GnomeModule(ExtensionModule):
# dependencies and also find the include directories needed for the
# typelib generation custom target below.
typelib_includes = []
for dep in deps:
if hasattr(dep, 'held_object'):
dep = dep.held_object
for dep in unholder(deps):
# Add a dependency on each GirTarget listed in dependencies and add
# the directory where it will be generated to the typelib includes
if isinstance(dep, InternalDependency):
for source in dep.sources:
if hasattr(source, 'held_object'):
source = source.held_object
for source in unholder(dep.sources):
if isinstance(source, GirTarget) and source not in depends:
depends.append(source)
subdir = os.path.join(state.environment.get_build_dir(),
@ -1003,9 +990,7 @@ This will become a hard error in the future.''')
depends = []
content_files = []
for s in mesonlib.extract_as_list(kwargs, 'content_files'):
if hasattr(s, 'held_object'):
s = s.held_object
for s in unholder(mesonlib.extract_as_list(kwargs, 'content_files')):
if isinstance(s, (build.CustomTarget, build.CustomTargetIndex)):
depends.append(s)
for o in s.get_outputs():
@ -1601,9 +1586,7 @@ G_END_DECLS'''
vapi_includes = []
ret = []
remaining_args = []
for arg in arg_list:
if hasattr(arg, 'held_object'):
arg = arg.held_object
for arg in unholder(arg_list):
if isinstance(arg, InternalDependency):
targets = [t for t in arg.sources if isinstance(t, VapiTarget)]
for target in targets:

@ -15,7 +15,7 @@
import os
from .. import mlog
from .. import build
from ..mesonlib import MesonException, Popen_safe, extract_as_list, File
from ..mesonlib import MesonException, Popen_safe, extract_as_list, File, unholder
from ..dependencies import Dependency, Qt4Dependency, Qt5Dependency
import xml.etree.ElementTree as ET
from . import ModuleReturnValue, get_include_args, ExtensionModule
@ -171,9 +171,7 @@ class QtBaseModule(ExtensionModule):
sources.append(ui_output)
inc = get_include_args(include_dirs=include_directories)
compile_args = []
for dep in dependencies:
if hasattr(dep, 'held_object'):
dep = dep.held_object
for dep in unholder(dependencies):
if isinstance(dep, Dependency):
for arg in dep.get_compile_args():
if arg.startswith('-I') or arg.startswith('-D'):

@ -18,7 +18,7 @@ import re
from .. import mlog
from .. import mesonlib, build
from ..mesonlib import MachineChoice, MesonException, extract_as_list
from ..mesonlib import MachineChoice, MesonException, extract_as_list, unholder
from . import get_include_args
from . import ModuleReturnValue
from . import ExtensionModule
@ -116,9 +116,7 @@ class WindowsModule(ExtensionModule):
for subsrc in src:
add_target(subsrc)
return
if hasattr(src, 'held_object'):
src = src.held_object
src = unholder(src)
if isinstance(src, str):
name_format = 'file {!r}'

Loading…
Cancel
Save