summaryrefslogtreecommitdiff
path: root/mesonbuild/coredata.py
diff options
context:
space:
mode:
authorJussi Pakkanen <jpakkane@gmail.com>2017-04-03 23:31:32 +0300
committerGitHub <noreply@github.com>2017-04-03 23:31:32 +0300
commit8b73d807924dde250f1c4340885ef46a22b82ba7 (patch)
treee9c2ba1b19e115858a18ce9d9f20f094d817b02c /mesonbuild/coredata.py
parent3070461ecccc582eed186fa215a4c4dd36823d71 (diff)
parent1b81b32afbe516f9230393cd5706de692531e10c (diff)
downloadmeson-8b73d807924dde250f1c4340885ef46a22b82ba7.tar.gz
Merge pull request #1457 from mesonbuild/overrides
Add MVP implementation of overriding options.
Diffstat (limited to 'mesonbuild/coredata.py')
-rw-r--r--mesonbuild/coredata.py47
1 files changed, 41 insertions, 6 deletions
diff --git a/mesonbuild/coredata.py b/mesonbuild/coredata.py
index 51eeafffe..67516e78f 100644
--- a/mesonbuild/coredata.py
+++ b/mesonbuild/coredata.py
@@ -17,6 +17,7 @@ from pathlib import PurePath
from collections import OrderedDict
from .mesonlib import MesonException, commonpath
from .mesonlib import default_libdir, default_libexecdir, default_prefix
+import ast
version = '0.40.0.dev1'
backendlist = ['ninja', 'vs', 'vs2010', 'vs2015', 'vs2017', 'xcode']
@@ -31,6 +32,12 @@ class UserOption:
def parse_string(self, valuestring):
return valuestring
+ # Check that the input is a valid value and return the
+ # "cleaned" or "native" version. For example the Boolean
+ # option could take the string "true" and return True.
+ def validate_value(self, value):
+ raise RuntimeError('Derived option class did not override validate_value.')
+
class UserStringOption(UserOption):
def __init__(self, name, description, value, choices=None):
super().__init__(name, description, choices)
@@ -44,6 +51,10 @@ class UserStringOption(UserOption):
self.validate(newvalue)
self.value = newvalue
+ def validate_value(self, value):
+ self.validate(value)
+ return value
+
class UserBooleanOption(UserOption):
def __init__(self, name, description, value):
super().__init__(name, description, [True, False])
@@ -71,6 +82,9 @@ class UserBooleanOption(UserOption):
def __bool__(self):
return self.value
+ def validate_value(self, value):
+ return self.tobool(value)
+
class UserComboOption(UserOption):
def __init__(self, name, description, choices, value):
super().__init__(name, description, choices)
@@ -87,22 +101,36 @@ class UserComboOption(UserOption):
raise MesonException('Value "%s" for combo option "%s" is not one of the choices. Possible choices are: %s.' % (newvalue, self.name, optionsstring))
self.value = newvalue
+ def validate_value(self, value):
+ if value not in self.choices:
+ raise MesonException('Value %s not one of accepted values.' % value)
+ return value
+
class UserStringArrayOption(UserOption):
def __init__(self, name, description, value, **kwargs):
super().__init__(name, description, kwargs.get('choices', []))
self.set_value(value)
- def set_value(self, newvalue):
- if isinstance(newvalue, str):
- if not newvalue.startswith('['):
- raise MesonException('Valuestring does not define an array: ' + newvalue)
- newvalue = eval(newvalue, {}, {}) # Yes, it is unsafe.
+ def validate(self, value):
+ if isinstance(value, str):
+ if not value.startswith('['):
+ raise MesonException('Valuestring does not define an array: ' + value)
+ newvalue = ast.literal_eval(value)
+ else:
+ newvalue = value
if not isinstance(newvalue, list):
raise MesonException('"{0}" should be a string array, but it is not'.format(str(newvalue)))
for i in newvalue:
if not isinstance(i, str):
raise MesonException('String array element "{0}" is not a string.'.format(str(newvalue)))
- self.value = newvalue
+ return newvalue
+
+ def set_value(self, newvalue):
+ self.value = self.validate(newvalue)
+
+ def validate_value(self, value):
+ self.validate(value)
+ return value
# This class contains all data that must persist over multiple
# invocations of Meson. It is roughly the same thing as
@@ -204,6 +232,13 @@ class CoreData:
raise RuntimeError('Tried to set unknown builtin option %s.' % optname)
self.builtins[optname].set_value(value)
+ def validate_option_value(self, option_name, override_value):
+ for opts in (self.builtins, self.base_options, self.compiler_options, self.user_options):
+ if option_name in opts:
+ opt = opts[option_name]
+ return opt.validate_value(override_value)
+ raise MesonException('Tried to validate unknown option %s.' % option_name)
+
def load(filename):
load_fail_msg = 'Coredata file {!r} is corrupted. Try with a fresh build tree.'.format(filename)
try: