Add MVP implementation of overriding options.

pull/1457/head
Jussi Pakkanen 8 years ago
parent d2548e6e83
commit e5a6283c4c
  1. 8
      mesonbuild/backend/ninjabackend.py
  2. 10
      mesonbuild/build.py
  3. 45
      mesonbuild/coredata.py
  4. 9
      test cases/common/139 override options/four.c
  5. 6
      test cases/common/139 override options/meson.build
  6. 3
      test cases/common/139 override options/one.c
  7. 7
      test cases/common/139 override options/three.c
  8. 6
      test cases/common/139 override options/two.c

@ -283,6 +283,12 @@ int dummy;
return False
return True
def get_option_for_target(self, option_name, target):
if option_name in target.option_overrides:
override = target.option_overrides[option_name]
return self.environment.coredata.validate_option_value(option_name, override)
return self.environment.coredata.get_builtin_option('unity')
def generate_target(self, target, outfile):
if isinstance(target, build.CustomTarget):
self.generate_custom_target(target, outfile)
@ -336,7 +342,7 @@ int dummy;
outname = self.get_target_filename(target)
obj_list = []
use_pch = self.environment.coredata.base_options.get('b_pch', False)
is_unity = self.environment.coredata.get_builtin_option('unity')
is_unity = self.get_option_for_target('unity', target)
if use_pch and target.has_pch():
pch_objects = self.generate_pch(target, outfile)
else:

@ -47,6 +47,7 @@ known_basic_kwargs = {'install': True,
'objects': True,
'native': True,
'build_by_default': True,
'override_options': True,
}
# These contain kwargs supported by both static and shared libraries. These are
@ -305,6 +306,7 @@ class BuildTarget(Target):
self.extra_args = {}
self.generated = []
self.extra_files = []
self.option_overrides = {}
# Sources can be:
# 1. Pre-existing source files in the source tree
# 2. Pre-existing sources generated by configure_file in the build tree
@ -670,6 +672,14 @@ class BuildTarget(Target):
self.pic = kwargs.get('pic', False)
if not isinstance(self.pic, bool):
raise InvalidArguments('Argument pic to static library {!r} must be boolean'.format(self.name))
overrides = stringlistify(kwargs.get('override_options', []))
for o in overrides:
if '=' not in o:
raise InvalidArguments('Overrides must be of form "key=value"')
k, v = o.split('=', 1)
k = k.strip()
v = v.strip()
self.option_overrides[k] = v
def get_filename(self):
return self.filename

@ -30,6 +30,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)
@ -43,6 +49,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])
@ -70,6 +80,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)
@ -86,22 +99,35 @@ 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)
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 = eval(value, {}, {}) # Yes, it is unsafe.
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
@ -203,6 +229,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:

@ -0,0 +1,9 @@
int func();
static int duplicate_func() {
return -4;
}
int main(int argc, char **argv) {
return duplicate_func() + func();
}

@ -0,0 +1,6 @@
project('option override', 'c',
default_options : 'unity=true')
executable('mustunity', 'one.c', 'two.c')
executable('notunity', 'three.c', 'four.c',
override_options : ['unity=false'])

@ -0,0 +1,3 @@
static int hidden_func() {
return 0;
}

@ -0,0 +1,7 @@
static int duplicate_func() {
return 4;
}
int func() {
return duplicate_func();
}

@ -0,0 +1,6 @@
/*
* Requires a Unity build. Otherwise hidden_func is not specified.
*/
int main(int argc, char **argv) {
return hidden_func();
}
Loading…
Cancel
Save