Add an option to dependencies called 'method'. This can be used to

configure a detection method, for those types of dependencies that have
more than one means of detection.

The default detection methods are unchanged if 'method' is not
specified, and all dependencies support the method 'auto', which is the
same as not specifying a method.

The dependencies which do support multiple detection methods
additionally support other values, depending on the dependency.
pull/1588/head
Aaron Small 8 years ago
parent b89af8b6dd
commit 76c8491d77
  1. 254
      mesonbuild/dependencies.py
  2. 13
      mesonbuild/mlog.py
  3. 7
      mesonbuild/modules/qt4.py
  4. 7
      mesonbuild/modules/qt5.py
  5. 6
      run_unittests.py
  6. 14
      test cases/frameworks/4 qt/meson.build
  7. 1
      test cases/frameworks/4 qt/meson_options.txt

@ -34,10 +34,20 @@ class DependencyException(MesonException):
'''Exceptions raised while trying to find dependencies'''
class Dependency:
def __init__(self, type_name='unknown'):
def __init__(self, type_name, kwargs):
self.name = "null"
self.is_found = False
self.type_name = type_name
method = kwargs.get('method', 'auto')
# Set the detection method. If the method is set to auto, use any available method.
# If method is set to a specific string, allow only that detection method.
if method == "auto":
self.methods = self.get_methods()
elif method in self.get_methods():
self.methods = [method]
else:
raise MesonException('Unsupported detection method: {}, allowed methods are {}'.format(method, mlog.format_list(["auto"] + self.get_methods())))
def __repr__(self):
s = '<{0} {1}: {2}>'
@ -57,6 +67,9 @@ class Dependency:
As an example, gtest-all.cc when using GTest."""
return []
def get_methods(self):
return ['auto']
def get_name(self):
return self.name
@ -71,7 +84,7 @@ class Dependency:
class InternalDependency(Dependency):
def __init__(self, version, incdirs, compile_args, link_args, libraries, sources, ext_deps):
super().__init__('internal')
super().__init__('internal', {})
self.version = version
self.include_directories = incdirs
self.compile_args = compile_args
@ -95,7 +108,7 @@ class PkgConfigDependency(Dependency):
class_pkgbin = None
def __init__(self, name, environment, kwargs):
Dependency.__init__(self, 'pkgconfig')
Dependency.__init__(self, 'pkgconfig', kwargs)
self.is_libtool = False
self.required = kwargs.get('required', True)
self.static = kwargs.get('static', False)
@ -254,6 +267,9 @@ class PkgConfigDependency(Dependency):
def get_link_args(self):
return self.libs
def get_methods(self):
return ['pkgconfig']
def check_pkgconfig(self):
evar = 'PKG_CONFIG'
if evar in os.environ:
@ -322,7 +338,7 @@ class WxDependency(Dependency):
wx_found = None
def __init__(self, environment, kwargs):
Dependency.__init__(self, 'wx')
Dependency.__init__(self, 'wx', kwargs)
self.is_found = False
self.modversion = 'none'
if WxDependency.wx_found is None:
@ -542,7 +558,7 @@ class ExternalLibrary(Dependency):
# TODO: Add `lang` to the parent Dependency object so that dependencies can
# be expressed for languages other than C-like
def __init__(self, name, link_args=None, language=None, silent=False):
super().__init__('external')
super().__init__('external', {})
self.name = name
self.is_found = False
self.link_args = []
@ -582,7 +598,7 @@ class BoostDependency(Dependency):
name2lib = {'test': 'unit_test_framework'}
def __init__(self, environment, kwargs):
Dependency.__init__(self, 'boost')
Dependency.__init__(self, 'boost', kwargs)
self.name = 'boost'
self.environment = environment
self.libdir = ''
@ -805,7 +821,7 @@ class BoostDependency(Dependency):
class GTestDependency(Dependency):
def __init__(self, environment, kwargs):
Dependency.__init__(self, 'gtest')
Dependency.__init__(self, 'gtest', kwargs)
self.main = kwargs.get('main', False)
self.name = 'gtest'
self.libname = 'libgtest.so'
@ -882,7 +898,7 @@ class GTestDependency(Dependency):
class GMockDependency(Dependency):
def __init__(self, environment, kwargs):
Dependency.__init__(self, 'gmock')
Dependency.__init__(self, 'gmock', kwargs)
# GMock may be a library or just source.
# Work with both.
self.name = 'gmock'
@ -937,7 +953,7 @@ class GMockDependency(Dependency):
class QtBaseDependency(Dependency):
def __init__(self, name, env, kwargs):
Dependency.__init__(self, name)
Dependency.__init__(self, name, kwargs)
self.name = name
self.qtname = name.capitalize()
self.qtver = name[-1]
@ -964,26 +980,32 @@ class QtBaseDependency(Dependency):
type_text = 'cross' if env.is_cross_build() else 'native'
found_msg = '{} {} {{}} dependency (modules: {}) found:' \
''.format(self.qtname, type_text, ', '.join(mods))
from_text = '`pkg-config`'
from_text = 'pkg-config'
# Keep track of the detection methods used, for logging purposes.
methods = []
# Prefer pkg-config, then fallback to `qmake -query`
self._pkgconfig_detect(mods, env, kwargs)
if not self.is_found:
if 'pkgconfig' in self.methods:
self._pkgconfig_detect(mods, env, kwargs)
methods.append('pkgconfig')
if not self.is_found and 'qmake' in self.methods:
from_text = self._qmake_detect(mods, env, kwargs)
if not self.is_found:
# Reset compile args and link args
self.cargs = []
self.largs = []
from_text = '(checked pkg-config, qmake-{}, and qmake)' \
''.format(self.name)
self.version = 'none'
if self.required:
err_msg = '{} {} dependency not found {}' \
''.format(self.qtname, type_text, from_text)
raise DependencyException(err_msg)
if not self.silent:
mlog.log(found_msg.format(from_text), mlog.red('NO'))
return
from_text = '`{}`'.format(from_text)
methods.append('qmake-' + self.name)
methods.append('qmake')
if not self.is_found:
# Reset compile args and link args
self.cargs = []
self.largs = []
from_text = '(checked {})'.format(mlog.format_list(methods))
self.version = 'none'
if self.required:
err_msg = '{} {} dependency not found {}' \
''.format(self.qtname, type_text, from_text)
raise DependencyException(err_msg)
if not self.silent:
mlog.log(found_msg.format(from_text), mlog.red('NO'))
return
from_text = '`{}`'.format(from_text)
if not self.silent:
mlog.log(found_msg.format(from_text), mlog.green('YES'))
@ -1053,6 +1075,7 @@ class QtBaseDependency(Dependency):
return
self.version = re.search(self.qtver + '(\.\d+)+', stdo).group(0)
# Query library path, header path, and binary path
mlog.log("Found qmake:", mlog.bold(self.qmake.get_name()), '(%s)' % self.version)
stdo = Popen_safe(self.qmake.get_command() + ['-query'])[1]
qvars = {}
for line in stdo.split('\n'):
@ -1092,7 +1115,7 @@ class QtBaseDependency(Dependency):
libdir = qvars['QT_INSTALL_LIBS']
for m in modules:
fname = 'Qt' + m
fwdep = ExtraFrameworkDependency(fname, kwargs.get('required', True), libdir)
fwdep = ExtraFrameworkDependency(fname, kwargs.get('required', True), libdir, kwargs)
self.cargs.append('-F' + libdir)
if fwdep.found():
self.is_found = True
@ -1113,6 +1136,9 @@ class QtBaseDependency(Dependency):
def get_link_args(self):
return self.largs
def get_methods(self):
return ['pkgconfig', 'qmake']
def found(self):
return self.is_found
@ -1150,7 +1176,7 @@ class Qt4Dependency(QtBaseDependency):
class GnuStepDependency(Dependency):
def __init__(self, environment, kwargs):
Dependency.__init__(self, 'gnustep')
Dependency.__init__(self, 'gnustep', kwargs)
self.required = kwargs.get('required', True)
self.modules = kwargs.get('modules', [])
self.detect()
@ -1248,7 +1274,7 @@ why. As a hack filter out everything that is not a flag."""
class AppleFrameworks(Dependency):
def __init__(self, environment, kwargs):
Dependency.__init__(self, 'appleframeworks')
Dependency.__init__(self, 'appleframeworks', kwargs)
modules = kwargs.get('modules', [])
if isinstance(modules, str):
modules = [modules]
@ -1271,31 +1297,33 @@ class AppleFrameworks(Dependency):
class GLDependency(Dependency):
def __init__(self, environment, kwargs):
Dependency.__init__(self, 'gl')
Dependency.__init__(self, 'gl', kwargs)
self.is_found = False
self.cargs = []
self.linkargs = []
try:
pcdep = PkgConfigDependency('gl', environment, kwargs)
if pcdep.found():
self.type_name = 'pkgconfig'
if 'pkgconfig' in self.methods:
try:
pcdep = PkgConfigDependency('gl', environment, kwargs)
if pcdep.found():
self.type_name = 'pkgconfig'
self.is_found = True
self.cargs = pcdep.get_compile_args()
self.linkargs = pcdep.get_link_args()
self.version = pcdep.get_version()
return
except Exception:
pass
if 'system' in self.methods:
if mesonlib.is_osx():
self.is_found = True
self.cargs = pcdep.get_compile_args()
self.linkargs = pcdep.get_link_args()
self.version = pcdep.get_version()
self.linkargs = ['-framework', 'OpenGL']
self.version = '1' # FIXME
return
if mesonlib.is_windows():
self.is_found = True
self.linkargs = ['-lopengl32']
self.version = '1' # FIXME: unfixable?
return
except Exception:
pass
if mesonlib.is_osx():
self.is_found = True
self.linkargs = ['-framework', 'OpenGL']
self.version = '1' # FIXME
return
if mesonlib.is_windows():
self.is_found = True
self.linkargs = ['-lopengl32']
self.version = '1' # FIXME: unfixable?
return
def get_link_args(self):
return self.linkargs
@ -1303,48 +1331,57 @@ class GLDependency(Dependency):
def get_version(self):
return self.version
def get_methods(self):
if mesonlib.is_osx() or mesonlib.is_windows():
return ['pkgconfig', 'system']
else:
return ['pkgconfig']
# There are three different ways of depending on SDL2:
# sdl2-config, pkg-config and OSX framework
class SDL2Dependency(Dependency):
def __init__(self, environment, kwargs):
Dependency.__init__(self, 'sdl2')
Dependency.__init__(self, 'sdl2', kwargs)
self.is_found = False
self.cargs = []
self.linkargs = []
try:
pcdep = PkgConfigDependency('sdl2', environment, kwargs)
if pcdep.found():
self.type_name = 'pkgconfig'
self.is_found = True
self.cargs = pcdep.get_compile_args()
self.linkargs = pcdep.get_link_args()
self.version = pcdep.get_version()
return
except Exception as e:
mlog.debug('SDL 2 not found via pkgconfig. Trying next, error was:', str(e))
pass
sdlconf = shutil.which('sdl2-config')
if sdlconf:
stdo = Popen_safe(['sdl2-config', '--cflags'])[1]
self.cargs = stdo.strip().split()
stdo = Popen_safe(['sdl2-config', '--libs'])[1]
self.linkargs = stdo.strip().split()
stdo = Popen_safe(['sdl2-config', '--version'])[1]
self.version = stdo.strip()
self.is_found = True
mlog.log('Dependency', mlog.bold('sdl2'), 'found:', mlog.green('YES'),
self.version, '(%s)' % sdlconf)
return
mlog.debug('Could not find sdl2-config binary, trying next.')
if mesonlib.is_osx():
fwdep = ExtraFrameworkDependency('sdl2', kwargs.get('required', True))
if fwdep.found():
if 'pkgconfig' in self.methods:
try:
pcdep = PkgConfigDependency('sdl2', environment, kwargs)
if pcdep.found():
self.type_name = 'pkgconfig'
self.is_found = True
self.cargs = pcdep.get_compile_args()
self.linkargs = pcdep.get_link_args()
self.version = pcdep.get_version()
return
except Exception as e:
mlog.debug('SDL 2 not found via pkgconfig. Trying next, error was:', str(e))
pass
if 'sdlconfig' in self.methods:
sdlconf = shutil.which('sdl2-config')
if sdlconf:
stdo = Popen_safe(['sdl2-config', '--cflags'])[1]
self.cargs = stdo.strip().split()
stdo = Popen_safe(['sdl2-config', '--libs'])[1]
self.linkargs = stdo.strip().split()
stdo = Popen_safe(['sdl2-config', '--version'])[1]
self.version = stdo.strip()
self.is_found = True
self.cargs = fwdep.get_compile_args()
self.linkargs = fwdep.get_link_args()
self.version = '2' # FIXME
mlog.log('Dependency', mlog.bold('sdl2'), 'found:', mlog.green('YES'),
self.version, '(%s)' % sdlconf)
return
mlog.log('Dependency', mlog.bold('sdl2'), 'found:', mlog.red('NO'))
mlog.debug('Could not find sdl2-config binary, trying next.')
if 'extraframework' in self.methods:
if mesonlib.is_osx():
fwdep = ExtraFrameworkDependency('sdl2', kwargs.get('required', True), None, kwargs)
if fwdep.found():
self.is_found = True
self.cargs = fwdep.get_compile_args()
self.linkargs = fwdep.get_link_args()
self.version = '2' # FIXME
return
mlog.log('Dependency', mlog.bold('sdl2'), 'found:', mlog.red('NO'))
def get_compile_args(self):
return self.cargs
@ -1358,9 +1395,15 @@ class SDL2Dependency(Dependency):
def get_version(self):
return self.version
def get_methods(self):
if mesonlib.is_osx():
return ['pkgconfig', 'sdlconfig', 'extraframework']
else:
return ['pkgconfig', 'sdlconfig']
class ExtraFrameworkDependency(Dependency):
def __init__(self, name, required, path=None):
Dependency.__init__(self, 'extraframeworks')
def __init__(self, name, required, path, kwargs):
Dependency.__init__(self, 'extraframeworks', kwargs)
self.name = None
self.detect(name, path)
if self.found():
@ -1404,7 +1447,7 @@ class ExtraFrameworkDependency(Dependency):
class ThreadDependency(Dependency):
def __init__(self, environment, kwargs):
super().__init__('threads')
super().__init__('threads', {})
self.name = 'threads'
self.is_found = True
mlog.log('Dependency', mlog.bold(self.name), 'found:', mlog.green('YES'))
@ -1417,28 +1460,29 @@ class ThreadDependency(Dependency):
class Python3Dependency(Dependency):
def __init__(self, environment, kwargs):
super().__init__('python3')
super().__init__('python3', kwargs)
self.name = 'python3'
self.is_found = False
# We can only be sure that it is Python 3 at this point
self.version = '3'
try:
pkgdep = PkgConfigDependency('python3', environment, kwargs)
if pkgdep.found():
self.cargs = pkgdep.cargs
self.libs = pkgdep.libs
self.version = pkgdep.get_version()
self.is_found = True
return
except Exception:
pass
if 'pkgconfig' in self.methods:
try:
pkgdep = PkgConfigDependency('python3', environment, kwargs)
if pkgdep.found():
self.cargs = pkgdep.cargs
self.libs = pkgdep.libs
self.version = pkgdep.get_version()
self.is_found = True
return
except Exception:
pass
if not self.is_found:
if mesonlib.is_windows():
if mesonlib.is_windows() and 'sysconfig' in self.methods:
self._find_libpy3_windows(environment)
elif mesonlib.is_osx():
elif mesonlib.is_osx() and 'extraframework' in self.methods:
# In OSX the Python 3 framework does not have a version
# number in its name.
fw = ExtraFrameworkDependency('python', False)
fw = ExtraFrameworkDependency('python', False, None, kwargs)
if fw.found():
self.cargs = fw.get_compile_args()
self.libs = fw.get_link_args()
@ -1490,6 +1534,14 @@ class Python3Dependency(Dependency):
def get_link_args(self):
return self.libs
def get_methods(self):
if mesonlib.is_windows():
return ['pkgconfig', 'sysconfig']
elif mesonlib.is_osx():
return ['pkgconfig', 'extraframework']
else:
return ['pkgconfig']
def get_version(self):
return self.version
@ -1537,7 +1589,7 @@ def find_external_dependency(name, environment, kwargs):
except Exception as e:
pkg_exc = e
if mesonlib.is_osx():
fwdep = ExtraFrameworkDependency(name, required)
fwdep = ExtraFrameworkDependency(name, required, None, kwargs)
if required and not fwdep.found():
m = 'Dependency {!r} not found, tried Extra Frameworks ' \
'and Pkg-Config:\n\n' + str(pkg_exc)

@ -99,3 +99,16 @@ def log(*args, **kwargs):
def warning(*args, **kwargs):
log(yellow('WARNING:'), *args, **kwargs)
# Format a list for logging purposes as a string. It separates
# all but the last item with commas, and the last with 'and'.
def format_list(list):
l = len(list)
if l > 2:
return ' and '.join([', '.join(list[:-1]), list[-1]])
elif l == 2:
return ' and '.join(list)
elif l == 1:
return list[0]
else:
return ''

@ -24,14 +24,14 @@ from . import ModuleReturnValue
class Qt4Module(ExtensionModule):
tools_detected = False
def _detect_tools(self, env):
def _detect_tools(self, env, method):
if self.tools_detected:
return
mlog.log('Detecting Qt4 tools')
# FIXME: We currently require Qt4 to exist while importing the module.
# We should make it gracefully degrade and not create any targets if
# the import is marked as 'optional' (not implemented yet)
kwargs = {'required': 'true', 'modules': 'Core', 'silent': 'true'}
kwargs = {'required': 'true', 'modules': 'Core', 'silent': 'true', 'method': method}
qt4 = Qt4Dependency(env, kwargs)
# Get all tools and then make sure that they are the right version
self.moc, self.uic, self.rcc = qt4.compilers_detect()
@ -113,7 +113,8 @@ class Qt4Module(ExtensionModule):
if not isinstance(sources, list):
sources = [sources]
sources += args[1:]
self._detect_tools(state.environment)
method = kwargs.get('method', 'auto')
self._detect_tools(state.environment, method)
err_msg = "{0} sources specified and couldn't find {1}, " \
"please check your qt4 installation"
if len(moc_headers) + len(moc_sources) > 0 and not self.moc.found():

@ -24,14 +24,14 @@ from . import ModuleReturnValue
class Qt5Module(ExtensionModule):
tools_detected = False
def _detect_tools(self, env):
def _detect_tools(self, env, method):
if self.tools_detected:
return
mlog.log('Detecting Qt5 tools')
# FIXME: We currently require Qt5 to exist while importing the module.
# We should make it gracefully degrade and not create any targets if
# the import is marked as 'optional' (not implemented yet)
kwargs = {'required': 'true', 'modules': 'Core', 'silent': 'true'}
kwargs = {'required': 'true', 'modules': 'Core', 'silent': 'true', 'method': method}
qt5 = Qt5Dependency(env, kwargs)
# Get all tools and then make sure that they are the right version
self.moc, self.uic, self.rcc = qt5.compilers_detect()
@ -119,7 +119,8 @@ class Qt5Module(ExtensionModule):
if not isinstance(sources, list):
sources = [sources]
sources += args[1:]
self._detect_tools(state.environment)
method = kwargs.get('method', 'auto')
self._detect_tools(state.environment, method)
err_msg = "{0} sources specified and couldn't find {1}, " \
"please check your qt5 installation"
if len(moc_headers) + len(moc_sources) > 0 and not self.moc.found():

@ -1121,7 +1121,7 @@ class LinuxlikeTests(BasePlatformTests):
if qt4 != 0 or qt5 != 0:
raise unittest.SkipTest('Qt not found with pkg-config')
testdir = os.path.join(self.framework_test_dir, '4 qt')
self.init(testdir)
self.init(testdir, ['-Dmethod=pkgconfig'])
# Confirm that the dependency was found with qmake
msg = 'Qt4 native `pkg-config` dependency (modules: Core, Gui) found: YES\n'
msg2 = 'Qt5 native `pkg-config` dependency (modules: Core, Gui) found: YES\n'
@ -1144,10 +1144,8 @@ class LinuxlikeTests(BasePlatformTests):
if 'Qt version 5' not in output and 'qt5' not in output:
raise unittest.SkipTest('Qmake found, but it is not for Qt 5.')
# Disable pkg-config codepath and force searching with qmake/qmake-qt5
os.environ['PKG_CONFIG_LIBDIR'] = self.builddir
os.environ['PKG_CONFIG_PATH'] = self.builddir
testdir = os.path.join(self.framework_test_dir, '4 qt')
self.init(testdir)
self.init(testdir, ['-Dmethod=qmake'])
# Confirm that the dependency was found with qmake
msg = 'Qt5 native `qmake-qt5` dependency (modules: Core) found: YES\n'
msg2 = 'Qt5 native `qmake` dependency (modules: Core) found: YES\n'

@ -9,17 +9,17 @@ foreach qt : ['qt4', 'qt5']
qt_modules += qt5_modules
endif
# Test that invalid modules are indeed not found
fakeqtdep = dependency(qt, modules : ['DefinitelyNotFound'], required : false)
fakeqtdep = dependency(qt, modules : ['DefinitelyNotFound'], required : false, method : get_option('method'))
if fakeqtdep.found()
error('Invalid qt dep incorrectly found!')
endif
# Test that partially-invalid modules are indeed not found
fakeqtdep = dependency(qt, modules : ['Core', 'DefinitelyNotFound'], required : false)
fakeqtdep = dependency(qt, modules : ['Core', 'DefinitelyNotFound'], required : false, method : get_option('method'))
if fakeqtdep.found()
error('Invalid qt dep incorrectly found!')
endif
# If qt4 modules are found, test that. qt5 is required.
qtdep = dependency(qt, modules : qt_modules, required : qt == 'qt5')
qtdep = dependency(qt, modules : qt_modules, required : qt == 'qt5', method : get_option('method'))
if qtdep.found()
qtmodule = import(qt)
@ -30,10 +30,11 @@ foreach qt : ['qt4', 'qt5']
moc_headers : ['mainWindow.h'], # These need to be fed through the moc tool before use.
ui_files : 'mainWindow.ui', # XML files that need to be compiled with the uic tol.
qresources : ['stuff.qrc', 'stuff2.qrc'], # Resource file for rcc compiler.
method : get_option('method')
)
# Test that setting a unique name with a positional argument works
qtmodule.preprocess(qt + 'teststuff', qresources : ['stuff.qrc'])
qtmodule.preprocess(qt + 'teststuff', qresources : ['stuff.qrc'], method : get_option('method'))
qexe = executable(qt + 'app',
sources : ['main.cpp', 'mainWindow.cpp', # Sources that don't need preprocessing.
@ -43,7 +44,7 @@ foreach qt : ['qt4', 'qt5']
# We need a console test application because some test environments
# do not have an X server.
qtcore = dependency(qt, modules : 'Core')
qtcore = dependency(qt, modules : 'Core', method : get_option('method'))
qtcoreapp = executable(qt + 'core', 'q5core.cpp',
dependencies : qtcore)
@ -55,7 +56,8 @@ foreach qt : ['qt4', 'qt5']
# files from sources.
manpreprocessed = qtmodule.preprocess(
moc_sources : 'manualinclude.cpp',
moc_headers : 'manualinclude.h')
moc_headers : 'manualinclude.h',
method : get_option('method'))
qtmaninclude = executable(qt + 'maninclude',
sources : ['manualinclude.cpp', manpreprocessed],

@ -0,0 +1 @@
option('method', type : 'string', value : 'auto', description : 'The method to use to find Qt')
Loading…
Cancel
Save