diff --git a/mesonbuild/compilers.py b/mesonbuild/compilers.py index 03ef9f9ff..eeb4185cb 100644 --- a/mesonbuild/compilers.py +++ b/mesonbuild/compilers.py @@ -293,6 +293,9 @@ class Compiler(): def get_library_dirs(self): return [] + def has_argument(self, arg): + raise EnvironmentException('Language {} does not support has_arg.'.format(self.language)) + class CCompiler(Compiler): def __init__(self, exelist, version, is_cross, exe_wrapper=None): super().__init__(exelist, version) @@ -506,6 +509,8 @@ int main () {{ {1}; }}''' return p def compiles(self, code, extra_args = []): + if isinstance(extra_args, str): + extra_args = [extra_args] suflen = len(self.default_suffix) (fd, srcname) = tempfile.mkstemp(suffix='.'+self.default_suffix) os.close(fd) @@ -819,6 +824,9 @@ void bar() { def thread_link_flags(self): return ['-pthread'] + def has_argument(self, arg): + return self.compiles('int i;\n', extra_args=arg) + class CPPCompiler(CCompiler): def __init__(self, exelist, version, is_cross, exe_wrap): CCompiler.__init__(self, exelist, version, is_cross, exe_wrap) @@ -1427,6 +1435,27 @@ class VisualStudioCCompiler(CCompiler): # msvc does not have a concept of system header dirs. return ['-I' + path] + # Visual Studio is special. It ignores arguments it does not + # understand and you can't tell it to error out on those. + # http://stackoverflow.com/questions/15259720/how-can-i-make-the-microsoft-c-compiler-treat-unknown-flags-as-errors-rather-t + def has_argument(self, arg): + warning_text = b'9002' + code = 'int i;\n' + (fd, srcname) = tempfile.mkstemp(suffix='.'+self.default_suffix) + os.close(fd) + ofile = open(srcname, 'w') + ofile.write(code) + ofile.close() + commands = self.exelist + [arg] + self.get_compile_only_args() + [srcname] + mlog.debug('Running VS compile:') + mlog.debug('Command line: ', ' '.join(commands)) + mlog.debug('Code:\n', code) + p = subprocess.Popen(commands, cwd=os.path.split(srcname)[0], stdout=subprocess.PIPE, stderr=subprocess.PIPE) + (stde, stdo) = p.communicate() + if p.returncode != 0: + raise MesonException('Compiling test app failed.') + return not(warning_text in stde or warning_text in stdo) + class VisualStudioCPPCompiler(VisualStudioCCompiler): def __init__(self, exelist, version, is_cross, exe_wrap): VisualStudioCCompiler.__init__(self, exelist, version, is_cross, exe_wrap) diff --git a/mesonbuild/interpreter.py b/mesonbuild/interpreter.py index 47ed61c1b..e6b740691 100644 --- a/mesonbuild/interpreter.py +++ b/mesonbuild/interpreter.py @@ -583,6 +583,8 @@ class CompilerHolder(InterpreterObject): 'version' : self.version_method, 'cmd_array' : self.cmd_array_method, 'find_library': self.find_library_method, + 'has_argument' : self.has_argument_method, + 'first_supported_argument' : self.first_supported_argument_method, }) def version_method(self, args, kwargs): @@ -789,6 +791,26 @@ class CompilerHolder(InterpreterObject): lib = dependencies.ExternalLibrary(libname, linkargs) return ExternalLibraryHolder(lib) + def has_argument_method(self, args, kwargs): + args = mesonlib.stringlistify(args) + if len(args) != 1: + raise InterpreterException('Has_arg takes exactly one argument.') + 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 diff --git a/test cases/common/112 has arg/meson.build b/test cases/common/112 has arg/meson.build new file mode 100644 index 000000000..64041079f --- /dev/null +++ b/test cases/common/112 has arg/meson.build @@ -0,0 +1,35 @@ +project('has arg', 'c', 'cpp') + +cc = meson.get_compiler('c') +cpp = meson.get_compiler('cpp') + +if cc.get_id() == 'msvc' + is_arg = '/O2' + useless = '/DFOO' +else + is_arg = '-O2' + useless = '-DFOO' +endif + +isnt_arg = '-fiambroken' + +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.') + +assert(cpp.has_argument(is_arg), 'Arg that should have worked does not work.') +assert(not cpp.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.') + +l1 = cpp.first_supported_argument([isnt_arg, is_arg, isnt_arg, useless]) +l2 = cpp.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.')