diff options
author | Jussi Pakkanen <jpakkane@gmail.com> | 2017-03-10 19:37:45 +0200 |
---|---|---|
committer | Jussi Pakkanen <jpakkane@gmail.com> | 2017-04-02 00:04:45 +0300 |
commit | e5a6283c4cf288fdfc9b43a92bf0ddd74dbf90f8 (patch) | |
tree | e3e320069b7665066336305759032b807b67a63c | |
parent | d2548e6e839b2058aae7f242db35d6836ccbeef7 (diff) | |
download | meson-e5a6283c4cf288fdfc9b43a92bf0ddd74dbf90f8.zip meson-e5a6283c4cf288fdfc9b43a92bf0ddd74dbf90f8.tar.gz meson-e5a6283c4cf288fdfc9b43a92bf0ddd74dbf90f8.tar.bz2 |
Add MVP implementation of overriding options.
-rw-r--r-- | mesonbuild/backend/ninjabackend.py | 8 | ||||
-rw-r--r-- | mesonbuild/build.py | 10 | ||||
-rw-r--r-- | mesonbuild/coredata.py | 45 | ||||
-rw-r--r-- | test cases/common/139 override options/four.c | 9 | ||||
-rw-r--r-- | test cases/common/139 override options/meson.build | 6 | ||||
-rw-r--r-- | test cases/common/139 override options/one.c | 3 | ||||
-rw-r--r-- | test cases/common/139 override options/three.c | 7 | ||||
-rw-r--r-- | test cases/common/139 override options/two.c | 6 |
8 files changed, 87 insertions, 7 deletions
diff --git a/mesonbuild/backend/ninjabackend.py b/mesonbuild/backend/ninjabackend.py index f29a7be..738d185 100644 --- a/mesonbuild/backend/ninjabackend.py +++ b/mesonbuild/backend/ninjabackend.py @@ -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: diff --git a/mesonbuild/build.py b/mesonbuild/build.py index 41e21e3..e46b65f 100644 --- a/mesonbuild/build.py +++ b/mesonbuild/build.py @@ -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 diff --git a/mesonbuild/coredata.py b/mesonbuild/coredata.py index 9562211..1d770e0 100644 --- a/mesonbuild/coredata.py +++ b/mesonbuild/coredata.py @@ -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: diff --git a/test cases/common/139 override options/four.c b/test cases/common/139 override options/four.c new file mode 100644 index 0000000..54f8491 --- /dev/null +++ b/test cases/common/139 override options/four.c @@ -0,0 +1,9 @@ +int func(); + +static int duplicate_func() { + return -4; +} + +int main(int argc, char **argv) { + return duplicate_func() + func(); +} diff --git a/test cases/common/139 override options/meson.build b/test cases/common/139 override options/meson.build new file mode 100644 index 0000000..2eb554b --- /dev/null +++ b/test cases/common/139 override options/meson.build @@ -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']) diff --git a/test cases/common/139 override options/one.c b/test cases/common/139 override options/one.c new file mode 100644 index 0000000..14fe9d6 --- /dev/null +++ b/test cases/common/139 override options/one.c @@ -0,0 +1,3 @@ +static int hidden_func() { + return 0; +} diff --git a/test cases/common/139 override options/three.c b/test cases/common/139 override options/three.c new file mode 100644 index 0000000..305a575 --- /dev/null +++ b/test cases/common/139 override options/three.c @@ -0,0 +1,7 @@ +static int duplicate_func() { + return 4; +} + +int func() { + return duplicate_func(); +} diff --git a/test cases/common/139 override options/two.c b/test cases/common/139 override options/two.c new file mode 100644 index 0000000..04b1d3f --- /dev/null +++ b/test cases/common/139 override options/two.c @@ -0,0 +1,6 @@ +/* + * Requires a Unity build. Otherwise hidden_func is not specified. + */ +int main(int argc, char **argv) { + return hidden_func(); +} |