From 4dff3f9fb3efb1f9c32484cbdc2f0668de782913 Mon Sep 17 00:00:00 2001 From: Jussi Pakkanen Date: Sun, 4 Oct 2015 02:18:26 +0300 Subject: Refactored option classes to mesonlib. --- optinterpreter.py | 85 ++++++++++++++++--------------------------------------- 1 file changed, 24 insertions(+), 61 deletions(-) (limited to 'optinterpreter.py') diff --git a/optinterpreter.py b/optinterpreter.py index 6906d31c9..e61707aad 100644 --- a/optinterpreter.py +++ b/optinterpreter.py @@ -1,4 +1,4 @@ -# Copyright 2013-2014 The Meson development team +# Copyright 2013-2015 The Meson development team # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. @@ -13,7 +13,7 @@ # limitations under the License. import mparser -import coredata +import coredata, mesonlib import os, re forbidden_option_names = coredata.builtin_options @@ -23,64 +23,27 @@ class OptionException(coredata.MesonException): optname_regex = re.compile('[^a-zA-Z0-9_-]') -class UserOption: - def __init__(self, name, kwargs): - super().__init__() - self.description = kwargs.get('description', '') - self.name = name - - def parse_string(self, valuestring): - return valuestring - -class UserStringOption(UserOption): - def __init__(self, name, kwargs): - super().__init__(name, kwargs) - self.set_value(kwargs.get('value', '')) - - def set_value(self, newvalue): - if not isinstance(newvalue, str): - raise OptionException('Value "%s" for string option "%s" is not a string.' % (str(newvalue), self.name)) - self.value = newvalue - -class UserBooleanOption(UserOption): - def __init__(self, name, kwargs): - super().__init__(name, kwargs) - self.set_value(kwargs.get('value', 'true')) - - def set_value(self, newvalue): - if not isinstance(newvalue, bool): - raise OptionException('Value "%s" for boolean option "%s" is not a boolean.' % (str(newvalue), self.name)) - self.value = newvalue - - def parse_string(self, valuestring): - if valuestring == 'false': - return False - if valuestring == 'true': - return True - raise OptionException('Value "%s" for boolean option "%s" is not a boolean.' % (valuestring, self.name)) - -class UserComboOption(UserOption): - def __init__(self, name, kwargs): - super().__init__(name, kwargs) - if 'choices' not in kwargs: - raise OptionException('Combo option missing "choices" keyword.') - self.choices = kwargs['choices'] - if not isinstance(self.choices, list): - raise OptionException('Combo choices must be an array.') - for i in self.choices: - if not isinstance(i, str): - raise OptionException('Combo choice elements must be strings.') - self.value = kwargs.get('value', self.choices[0]) - - def set_value(self, newvalue): - if newvalue not in self.choices: - optionsstring = ', '.join(['"%s"' % (item,) for item in self.choices]) - raise OptionException('Value "%s" for combo option "%s" is not one of the choices. Possible choices are: %s.' % (newvalue, self.name, optionsstring)) - self.value = newvalue - -option_types = {'string' : UserStringOption, - 'boolean' : UserBooleanOption, - 'combo' : UserComboOption, +def StringParser(name, description, kwargs): + return mesonlib.UserStringOption(name, description, + kwargs.get('value', '')) + +def BooleanParser(name, description, kwargs): + return mesonlib.UserBooleanOption(name, description, kwargs.get('value', True)) + +def ComboParser(name, description, kwargs): + if 'choices' not in kwargs: + raise OptionException('Combo option missing "choices" keyword.') + choices = kwargs['choices'] + if not isinstance(choices, list): + raise OptionException('Combo choices must be an array.') + for i in choices: + if not isinstance(i, str): + raise OptionException('Combo choice elements must be strings.') + return mesonlib.UserComboOption(name, description, choices, kwargs.get('value', choices[0])) + +option_types = {'string' : StringParser, + 'boolean' : BooleanParser, + 'combo' : ComboParser, } class OptionInterpreter: @@ -161,7 +124,7 @@ class OptionInterpreter: raise OptionException('Option name %s is reserved.' % opt_name) if self.subproject != '': opt_name = self.subproject + ':' + opt_name - opt = option_types[opt_type](opt_name, kwargs) + opt = option_types[opt_type](opt_name, kwargs.get('description', ''), kwargs) if opt.description == '': opt.description = opt_name if opt_name in self.cmd_line_options: -- cgit v1.2.3 From b197d9e2796874420e7ffc6f5dd66ac8b08bc56c Mon Sep 17 00:00:00 2001 From: Jussi Pakkanen Date: Sun, 4 Oct 2015 04:02:17 +0300 Subject: Can set compiler options with mesonconf. --- mesonconf.py | 36 ++++++++++++++++-------------------- mesonlib.py | 13 ++++++++++--- optinterpreter.py | 18 +++++++++++++++++- 3 files changed, 43 insertions(+), 24 deletions(-) (limited to 'optinterpreter.py') diff --git a/mesonconf.py b/mesonconf.py index 6922ea6ff..963585ad4 100755 --- a/mesonconf.py +++ b/mesonconf.py @@ -64,13 +64,6 @@ class Conf: f = '%s%s %s%s' % (name, namepad, descr, descrpad) print(f, value) - def tobool(self, thing): - if thing.lower() == 'true': - return True - if thing.lower() == 'false': - return False - raise ConfException('Value %s is not boolean (true or false).' % thing) - def set_options(self, options): for o in options: if '=' not in o: @@ -127,18 +120,10 @@ class Conf: self.coredata.localedir = v elif k in self.coredata.user_options: tgt = self.coredata.user_options[k] - if isinstance(tgt, mesonlib.UserBooleanOption): - tgt.set_value(self.tobool(v)) - elif isinstance(tgt, mesonlib.UserComboOption): - try: - tgt.set_value(v) - except coredata.MesonException: - raise ConfException('Value of %s must be one of %s.' % - (k, tgt.choices)) - elif isinstance(tgt, mesonlib.UserStringOption): - tgt.set_value(v) - else: - raise ConfException('Internal error, unknown option type.') + tgt.set_value(v) + elif k in self.coredata.compiler_options: + tgt = self.coredata.compiler_options[k] + tgt.set_value(v) elif k.endswith('linkargs'): lang = k[:-8] if not lang in self.coredata.external_link_args: @@ -181,6 +166,17 @@ class Conf: for (lang, args) in self.coredata.external_link_args.items(): print(lang + 'linkargs', str(args)) print('') + okeys = sorted(self.coredata.compiler_options.keys()) + if len(okeys) == 0: + print('No compiler options\n') + else: + print('Compiler options\n') + coarr = [] + for k in okeys: + o = self.coredata.compiler_options[k] + coarr.append([k, o.description, o.value]) + self.print_aligned(coarr) + print('') print('Directories\n') parr = [] parr.append(['prefix', 'Install prefix', self.coredata.prefix]) @@ -193,7 +189,7 @@ class Conf: self.print_aligned(parr) print('') if len(self.coredata.user_options) == 0: - print('This project does not have any options') + print('This project does not have user options') else: print('Project options\n') options = self.coredata.user_options diff --git a/mesonlib.py b/mesonlib.py index 0b200ac4e..0e31ef118 100644 --- a/mesonlib.py +++ b/mesonlib.py @@ -279,10 +279,17 @@ class UserBooleanOption(UserOption): super().__init__(name, description) self.set_value(value) + def tobool(self, thing): + if isinstance(thing, bool): + return thing + if thing.lower() == 'true': + return True + if thing.lower() == 'false': + return False + raise MesonException('Value %s is not boolean (true or false).' % thing) + def set_value(self, newvalue): - if not isinstance(newvalue, bool): - raise MesonException('Value "%s" for boolean option "%s" is not a boolean.' % (str(newvalue), self.name)) - self.value = newvalue + self.value = self.tobool(newvalue) def parse_string(self, valuestring): if valuestring == 'false': diff --git a/optinterpreter.py b/optinterpreter.py index e61707aad..d66aa1f32 100644 --- a/optinterpreter.py +++ b/optinterpreter.py @@ -17,6 +17,22 @@ import coredata, mesonlib import os, re forbidden_option_names = coredata.builtin_options +forbidden_prefixes = {'c_': True, + 'cpp_': True, + 'rust_': True, + 'fortran_': True, + 'objc_': True, + 'objcpp_': True, + 'vala_': True, + 'csharp_': True + } + +def is_invalid_name(name): + if name in forbidden_option_names: + return True + if name in forbidden_prefixes: + return True + return False class OptionException(coredata.MesonException): pass @@ -120,7 +136,7 @@ class OptionInterpreter: raise OptionException('Positional argument must be a string.') if optname_regex.search(opt_name) is not None: raise OptionException('Option names can only contain letters, numbers or dashes.') - if opt_name in forbidden_option_names: + if is_invalid_name(opt_name): raise OptionException('Option name %s is reserved.' % opt_name) if self.subproject != '': opt_name = self.subproject + ':' + opt_name -- cgit v1.2.3