diff options
Diffstat (limited to 'mesonbuild')
-rw-r--r-- | mesonbuild/compilers.py | 29 | ||||
-rw-r--r-- | mesonbuild/interpreter.py | 22 |
2 files changed, 51 insertions, 0 deletions
diff --git a/mesonbuild/compilers.py b/mesonbuild/compilers.py index 03ef9f9..eeb4185 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 47ed61c..e6b7406 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 |