aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDaniel Stone <daniels@collabora.com>2017-04-12 16:00:48 +0100
committerDaniel Stone <daniels@collabora.com>2017-08-31 20:24:20 +0100
commite1ffae0580259343be7665c6b2f014fe71b8c05c (patch)
tree24462ab941900841b2d1e320e2c02fbef0dd992e
parent7fb1973caca249e284ba6bec7e7a7b2439f9721f (diff)
downloadmeson-e1ffae0580259343be7665c6b2f014fe71b8c05c.zip
meson-e1ffae0580259343be7665c6b2f014fe71b8c05c.tar.gz
meson-e1ffae0580259343be7665c6b2f014fe71b8c05c.tar.bz2
Add Compiler.get_supported_arguments()
Add a helper for the common pattern of: args_to_use = [] foreach arg : candidate_args if cc.has_argument(arg) args_to_use += arg endif endforeach Replaced with: args_to_use = cc.get_supported_arguments(candidate_args)
-rw-r--r--docs/markdown/Reference-manual.md4
-rw-r--r--docs/markdown/snippets/get-supported-arguments.md23
-rw-r--r--mesonbuild/compilers/compilers.py7
-rw-r--r--mesonbuild/interpreter.py17
-rw-r--r--test cases/common/112 has arg/meson.build3
5 files changed, 54 insertions, 0 deletions
diff --git a/docs/markdown/Reference-manual.md b/docs/markdown/Reference-manual.md
index 91f7edd..e388878 100644
--- a/docs/markdown/Reference-manual.md
+++ b/docs/markdown/Reference-manual.md
@@ -1286,6 +1286,10 @@ the following methods:
- `get_id()` returns a string identifying the compiler. For example,
`gcc`, `msvc`, [and more](Compiler-properties.md#compiler-id).
+- `get_supported_arguments(list_of_string)` returns an array
+ containing only the arguments supported by the compiler, as if
+ `has_argument` were called on them individually.
+
- `compiles(code)` returns true if the code fragment given in the
positional argument compiles, you can specify external dependencies
to use with `dependencies` keyword argument, `code` can be either a
diff --git a/docs/markdown/snippets/get-supported-arguments.md b/docs/markdown/snippets/get-supported-arguments.md
new file mode 100644
index 0000000..c0cc9bf
--- /dev/null
+++ b/docs/markdown/snippets/get-supported-arguments.md
@@ -0,0 +1,23 @@
+# Easier handling of supported compiler arguments
+
+A common pattern for handling multiple desired compiler arguments, was to
+test their presence and add them to an array one-by-one, e.g.:
+
+ warning_flags_maybe = [
+ '-Wsomething',
+ '-Wanother-thing',
+ '-Wno-the-other-thing',
+ ]
+ warning_flags = []
+ foreach flag : warning_flags_maybe
+ if cc.has_argument(flag)
+ warning_flags += flag
+ endif
+ endforeach
+ cc.add_project_argument(warning_flags)
+
+A helper has been added for the foreach/has_argument pattern, so you can
+now simply do:
+
+ warning_flags = [ ... ]
+ flags = cc.get_supported_flags(warning_flags)
diff --git a/mesonbuild/compilers/compilers.py b/mesonbuild/compilers/compilers.py
index 5077a6e..c431194 100644
--- a/mesonbuild/compilers/compilers.py
+++ b/mesonbuild/compilers/compilers.py
@@ -712,6 +712,13 @@ class Compiler:
'Language {} does not support has_multi_arguments.'.format(
self.get_display_language()))
+ def get_supported_arguments(self, args, env):
+ supported_args = []
+ for arg in args:
+ if self.has_argument(arg, env):
+ supported_args.append(arg)
+ return supported_args
+
def get_cross_extra_flags(self, environment, link):
extra_flags = []
if self.is_cross and environment:
diff --git a/mesonbuild/interpreter.py b/mesonbuild/interpreter.py
index 313f430..72df3c7 100644
--- a/mesonbuild/interpreter.py
+++ b/mesonbuild/interpreter.py
@@ -652,6 +652,7 @@ class CompilerHolder(InterpreterObject):
'find_library': self.find_library_method,
'has_argument': self.has_argument_method,
'has_multi_arguments': self.has_multi_arguments_method,
+ 'get_supported_arguments': self.get_supported_arguments_method,
'first_supported_argument': self.first_supported_argument_method,
'unittest_args': self.unittest_args_method,
'symbols_have_underscore_prefix': self.symbols_have_underscore_prefix_method,
@@ -1013,6 +1014,22 @@ class CompilerHolder(InterpreterObject):
h)
return result
+ def get_supported_arguments_method(self, args, kwargs):
+ args = mesonlib.stringlistify(args)
+ result = self.compiler.get_supported_arguments(args, self.environment)
+ if len(result) == len(args):
+ h = mlog.green('YES')
+ elif len(result) > 0:
+ h = mlog.yellow('SOME')
+ else:
+ h = mlog.red('NO')
+ mlog.log(
+ 'Compiler for {} supports arguments {}:'.format(
+ self.compiler.get_display_language(), ' '.join(args)),
+ h)
+ return result
+
+
def first_supported_argument_method(self, args, kwargs):
for i in mesonlib.stringlistify(args):
if self.compiler.has_argument(i, self.environment):
diff --git a/test cases/common/112 has arg/meson.build b/test cases/common/112 has arg/meson.build
index a89b59e..27290a1 100644
--- a/test cases/common/112 has arg/meson.build
+++ b/test cases/common/112 has arg/meson.build
@@ -19,6 +19,9 @@ 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.')
+assert(cc.get_supported_arguments([is_arg, isnt_arg, useless]) == [is_arg, useless], 'Arg filtering returned different result.')
+assert(cpp.get_supported_arguments([is_arg, isnt_arg, useless]) == [is_arg, useless], 'Arg filtering returned different result.')
+
# 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)