Add a has_arg method to compiler to check whether it supports a given argument.

pull/585/head
Jussi Pakkanen 9 years ago
parent 50609054ec
commit d8d989d9b8
  1. 8
      mesonbuild/compilers.py
  2. 7
      mesonbuild/interpreter.py
  3. 14
      test cases/common/112 has arg/meson.build

@ -293,6 +293,9 @@ class Compiler():
def get_library_dirs(self): def get_library_dirs(self):
return [] return []
def has_arg(self, arg):
raise EnvironmentException('Language {} does not support has_arg.'.format(self.language))
class CCompiler(Compiler): class CCompiler(Compiler):
def __init__(self, exelist, version, is_cross, exe_wrapper=None): def __init__(self, exelist, version, is_cross, exe_wrapper=None):
super().__init__(exelist, version) super().__init__(exelist, version)
@ -506,6 +509,8 @@ int main () {{ {1}; }}'''
return p return p
def compiles(self, code, extra_args = []): def compiles(self, code, extra_args = []):
if isinstance(extra_args, str):
extra_args = [extra_args]
suflen = len(self.default_suffix) suflen = len(self.default_suffix)
(fd, srcname) = tempfile.mkstemp(suffix='.'+self.default_suffix) (fd, srcname) = tempfile.mkstemp(suffix='.'+self.default_suffix)
os.close(fd) os.close(fd)
@ -819,6 +824,9 @@ void bar() {
def thread_link_flags(self): def thread_link_flags(self):
return ['-pthread'] return ['-pthread']
def has_arg(self, arg):
return self.compiles('int i;\n', extra_args=arg)
class CPPCompiler(CCompiler): class CPPCompiler(CCompiler):
def __init__(self, exelist, version, is_cross, exe_wrap): def __init__(self, exelist, version, is_cross, exe_wrap):
CCompiler.__init__(self, exelist, version, is_cross, exe_wrap) CCompiler.__init__(self, exelist, version, is_cross, exe_wrap)

@ -583,6 +583,7 @@ class CompilerHolder(InterpreterObject):
'version' : self.version_method, 'version' : self.version_method,
'cmd_array' : self.cmd_array_method, 'cmd_array' : self.cmd_array_method,
'find_library': self.find_library_method, 'find_library': self.find_library_method,
'has_arg' : self.has_arg_method,
}) })
def version_method(self, args, kwargs): def version_method(self, args, kwargs):
@ -789,6 +790,12 @@ class CompilerHolder(InterpreterObject):
lib = dependencies.ExternalLibrary(libname, linkargs) lib = dependencies.ExternalLibrary(libname, linkargs)
return ExternalLibraryHolder(lib) return ExternalLibraryHolder(lib)
def has_arg_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])
class ModuleState: class ModuleState:
pass pass

@ -0,0 +1,14 @@
project('has arg', 'c')
cc = meson.get_compiler('c')
if cc.get_id() == 'msvc'
is_arg = '/O2'
else
is_arg = '-O2'
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.')
Loading…
Cancel
Save