From d8d989d9b80fffe88372f6606d21cb7d5f57e2c2 Mon Sep 17 00:00:00 2001 From: Jussi Pakkanen Date: Thu, 9 Jun 2016 21:19:58 +0300 Subject: [PATCH 1/3] Add a has_arg method to compiler to check whether it supports a given argument. --- mesonbuild/compilers.py | 8 ++++++++ mesonbuild/interpreter.py | 7 +++++++ test cases/common/112 has arg/meson.build | 14 ++++++++++++++ 3 files changed, 29 insertions(+) create mode 100644 test cases/common/112 has arg/meson.build diff --git a/mesonbuild/compilers.py b/mesonbuild/compilers.py index 03ef9f9ff..325b61157 100644 --- a/mesonbuild/compilers.py +++ b/mesonbuild/compilers.py @@ -293,6 +293,9 @@ class Compiler(): def get_library_dirs(self): return [] + def has_arg(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_arg(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) diff --git a/mesonbuild/interpreter.py b/mesonbuild/interpreter.py index 47ed61c1b..fc5620f83 100644 --- a/mesonbuild/interpreter.py +++ b/mesonbuild/interpreter.py @@ -583,6 +583,7 @@ 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, }) def version_method(self, args, kwargs): @@ -789,6 +790,12 @@ class CompilerHolder(InterpreterObject): lib = dependencies.ExternalLibrary(libname, linkargs) 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: 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..75c78e46c --- /dev/null +++ b/test cases/common/112 has arg/meson.build @@ -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.') From beef7cb291d55b214e2bc291cbb47bf953275569 Mon Sep 17 00:00:00 2001 From: Jussi Pakkanen Date: Thu, 9 Jun 2016 21:36:58 +0300 Subject: [PATCH 2/3] Added functionality to pick the first supported argument from a list. Closes #583. --- mesonbuild/compilers.py | 4 ++-- mesonbuild/interpreter.py | 21 ++++++++++++++++++--- test cases/common/112 has arg/meson.build | 14 ++++++++++++-- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/mesonbuild/compilers.py b/mesonbuild/compilers.py index 325b61157..5b68eaa53 100644 --- a/mesonbuild/compilers.py +++ b/mesonbuild/compilers.py @@ -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): diff --git a/mesonbuild/interpreter.py b/mesonbuild/interpreter.py index fc5620f83..e6b740691 100644 --- a/mesonbuild/interpreter.py +++ b/mesonbuild/interpreter.py @@ -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 diff --git a/test cases/common/112 has arg/meson.build b/test cases/common/112 has arg/meson.build index 75c78e46c..8a1d0f764 100644 --- a/test cases/common/112 has arg/meson.build +++ b/test cases/common/112 has arg/meson.build @@ -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.') From d4adf0983bd5dcf4083e433146aaec8b51daaaa8 Mon Sep 17 00:00:00 2001 From: Jussi Pakkanen Date: Sat, 11 Jun 2016 13:27:04 +0300 Subject: [PATCH 3/3] Add special casing for VS which ignores unknown arguments. --- mesonbuild/compilers.py | 21 +++++++++++++++++++++ test cases/common/112 has arg/meson.build | 13 ++++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/mesonbuild/compilers.py b/mesonbuild/compilers.py index 5b68eaa53..eeb4185cb 100644 --- a/mesonbuild/compilers.py +++ b/mesonbuild/compilers.py @@ -1435,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/test cases/common/112 has arg/meson.build b/test cases/common/112 has arg/meson.build index 8a1d0f764..64041079f 100644 --- a/test cases/common/112 has arg/meson.build +++ b/test cases/common/112 has arg/meson.build @@ -1,6 +1,7 @@ -project('has arg', 'c') +project('has arg', 'c', 'cpp') cc = meson.get_compiler('c') +cpp = meson.get_compiler('cpp') if cc.get_id() == 'msvc' is_arg = '/O2' @@ -15,6 +16,9 @@ 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) @@ -22,3 +26,10 @@ 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.')