Added functionality to pick the first supported argument from a list. Closes #583.
This commit is contained in:
parent
d8d989d9b8
commit
beef7cb291
|
@ -293,7 +293,7 @@ class Compiler():
|
|||
def get_library_dirs(self):
|
||||
return []
|
||||
|
||||
def has_arg(self, arg):
|
||||
def has_argument(self, arg):
|
||||
raise EnvironmentException('Language {} does not support has_arg.'.format(self.language))
|
||||
|
||||
class CCompiler(Compiler):
|
||||
|
@ -824,7 +824,7 @@ void bar() {
|
|||
def thread_link_flags(self):
|
||||
return ['-pthread']
|
||||
|
||||
def has_arg(self, arg):
|
||||
def has_argument(self, arg):
|
||||
return self.compiles('int i;\n', extra_args=arg)
|
||||
|
||||
class CPPCompiler(CCompiler):
|
||||
|
|
|
@ -583,7 +583,8 @@ class CompilerHolder(InterpreterObject):
|
|||
'version' : self.version_method,
|
||||
'cmd_array' : self.cmd_array_method,
|
||||
'find_library': self.find_library_method,
|
||||
'has_arg' : self.has_arg_method,
|
||||
'has_argument' : self.has_argument_method,
|
||||
'first_supported_argument' : self.first_supported_argument_method,
|
||||
})
|
||||
|
||||
def version_method(self, args, kwargs):
|
||||
|
@ -790,11 +791,25 @@ class CompilerHolder(InterpreterObject):
|
|||
lib = dependencies.ExternalLibrary(libname, linkargs)
|
||||
return ExternalLibraryHolder(lib)
|
||||
|
||||
def has_arg_method(self, args, kwargs):
|
||||
def has_argument_method(self, args, kwargs):
|
||||
args = mesonlib.stringlistify(args)
|
||||
if len(args) != 1:
|
||||
raise InterpreterException('Has_arg takes exactly one argument.')
|
||||
return self.compiler.has_arg(args[0])
|
||||
result = self.compiler.has_argument(args[0])
|
||||
if result:
|
||||
h = mlog.green('YES')
|
||||
else:
|
||||
h = mlog.red('NO')
|
||||
mlog.log('Compiler for {} supports argument {}:'.format(self.compiler.language, args[0]), h)
|
||||
return result
|
||||
|
||||
def first_supported_argument_method(self, args, kwargs):
|
||||
for i in mesonlib.stringlistify(args):
|
||||
if self.compiler.has_argument(i):
|
||||
mlog.log('First supported argument:', mlog.bold(i))
|
||||
return [i]
|
||||
mlog.log('First supported argument:', mlog.red('None'))
|
||||
return []
|
||||
|
||||
class ModuleState:
|
||||
pass
|
||||
|
|
|
@ -4,11 +4,21 @@ cc = meson.get_compiler('c')
|
|||
|
||||
if cc.get_id() == 'msvc'
|
||||
is_arg = '/O2'
|
||||
useless = '/DFOO'
|
||||
else
|
||||
is_arg = '-O2'
|
||||
useless = '-DFOO'
|
||||
endif
|
||||
|
||||
isnt_arg = '-fiambroken'
|
||||
|
||||
assert(cc.has_arg(is_arg), 'Arg that should have worked does not work.')
|
||||
assert(not cc.has_arg(isnt_arg), 'Arg that should be broken is not.')
|
||||
assert(cc.has_argument(is_arg), 'Arg that should have worked does not work.')
|
||||
assert(not cc.has_argument(isnt_arg), 'Arg that should be broken is not.')
|
||||
|
||||
# Have useless at the end to ensure that the search goes from front to back.
|
||||
l1 = cc.first_supported_argument([isnt_arg, is_arg, isnt_arg, useless])
|
||||
l2 = cc.first_supported_argument(isnt_arg, isnt_arg, isnt_arg)
|
||||
|
||||
assert(l1.length() == 1, 'First supported returned wrong result.')
|
||||
assert(l1.get(0) == is_arg, 'First supported returned wrong argument.')
|
||||
assert(l2.length() == 0, 'First supported did not return empty array.')
|
||||
|
|
Loading…
Reference in New Issue