| OLD | NEW |
| 1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
| 2 # Copyright (C) 2013 Google Inc. All rights reserved. | 2 # Copyright (C) 2013 Google Inc. All rights reserved. |
| 3 # | 3 # |
| 4 # Redistribution and use in source and binary forms, with or without | 4 # Redistribution and use in source and binary forms, with or without |
| 5 # modification, are permitted provided that the following conditions are | 5 # modification, are permitted provided that the following conditions are |
| 6 # met: | 6 # met: |
| 7 # | 7 # |
| 8 # * Redistributions of source code must retain the above copyright | 8 # * Redistributions of source code must retain the above copyright |
| 9 # notice, this list of conditions and the following disclaimer. | 9 # notice, this list of conditions and the following disclaimer. |
| 10 # * Redistributions in binary form must reproduce the above | 10 # * Redistributions in binary form must reproduce the above |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 28 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | 29 |
| 30 import os.path | 30 import os.path |
| 31 import sys | 31 import sys |
| 32 | 32 |
| 33 from in_file import InFile | 33 from in_file import InFile |
| 34 import in_generator | 34 import in_generator |
| 35 import license | 35 import license |
| 36 | 36 |
| 37 | 37 |
| 38 HEADER_TEMPLATE = """%(license)s | |
| 39 #ifndef %(class_name)s_h | |
| 40 #define %(class_name)s_h | |
| 41 | |
| 42 namespace WebCore { | |
| 43 | |
| 44 // A class that stores static enablers for all experimental features. | |
| 45 | |
| 46 class %(class_name)s { | |
| 47 public: | |
| 48 %(set_toggle_declarations)s | |
| 49 | |
| 50 %(accessor_declarations)s | |
| 51 private: | |
| 52 %(class_name)s() { } | |
| 53 | |
| 54 %(storage_declarations)s | |
| 55 }; | |
| 56 | |
| 57 } // namespace WebCore | |
| 58 | |
| 59 #endif // %(class_name)s_h | |
| 60 """ | |
| 61 | |
| 62 IMPLEMENTATION_TEMPLATE = """%(license)s | |
| 63 #include "config.h" | |
| 64 #include "%(class_name)s.h" | |
| 65 | |
| 66 namespace WebCore { | |
| 67 | |
| 68 %(set_toggle_definitions)s | |
| 69 %(storage_definitions)s | |
| 70 | |
| 71 } // namespace WebCore | |
| 72 """ | |
| 73 | |
| 74 class RuntimeFeatureWriter(in_generator.Writer): | 38 class RuntimeFeatureWriter(in_generator.Writer): |
| 75 class_name = 'RuntimeEnabledFeatures' | 39 class_name = 'RuntimeEnabledFeatures' |
| 76 | 40 |
| 77 # FIXME: valid_values and defaults should probably roll into one object. | 41 # FIXME: valid_values and defaults should probably roll into one object. |
| 78 valid_values = { | 42 valid_values = { |
| 79 'status': ['stable', 'experimental', 'test'], | 43 'status': ['stable', 'experimental', 'test'], |
| 80 } | 44 } |
| 81 defaults = { | 45 defaults = { |
| 82 'condition' : None, | 46 'condition' : None, |
| 83 'depends_on' : [], | 47 'depends_on' : [], |
| 84 'custom': False, | 48 'custom': False, |
| 85 'status': None, | 49 'status': None, |
| 86 } | 50 } |
| 87 | 51 |
| 88 def __init__(self, in_file_path): | 52 def __init__(self, in_file_path): |
| 89 super(RuntimeFeatureWriter, self).__init__(in_file_path) | 53 super(RuntimeFeatureWriter, self).__init__(in_file_path) |
| 90 self._all_features = self.in_file.name_dictionaries | 54 self._features = self.in_file.name_dictionaries |
| 91 # Make sure the resulting dictionaries have all the keys we expect. | 55 # Make sure the resulting dictionaries have all the keys we expect. |
| 92 for feature in self._all_features: | 56 for feature in self._features: |
| 93 feature['first_lowered_name'] = self._lower_first(feature['name']) | 57 feature['first_lowered_name'] = self._lower_first(feature['name']) |
| 94 # Most features just check their isFooEnabled bool | 58 # Most features just check their isFooEnabled bool |
| 95 # but some depend on more than one bool. | 59 # but some depend on more than one bool. |
| 96 enabled_condition = "is%sEnabled" % feature['name'] | 60 enabled_condition = "is%sEnabled" % feature['name'] |
| 97 for dependant_name in feature['depends_on']: | 61 for dependant_name in feature['depends_on']: |
| 98 enabled_condition += " && is%sEnabled" % dependant_name | 62 enabled_condition += " && is%sEnabled" % dependant_name |
| 99 feature['enabled_condition'] = enabled_condition | 63 feature['enabled_condition'] = enabled_condition |
| 100 self._non_custom_features = filter(lambda feature: not feature['custom']
, self._all_features) | 64 self._non_custom_features = filter(lambda feature: not feature['custom']
, self._features) |
| 101 | 65 |
| 102 def _lower_first(self, string): | 66 def _lower_first(self, string): |
| 103 lowered = string[0].lower() + string[1:] | 67 lowered = string[0].lower() + string[1:] |
| 104 lowered = lowered.replace("cSS", "css") | 68 lowered = lowered.replace("cSS", "css") |
| 105 lowered = lowered.replace("iME", "ime") | 69 lowered = lowered.replace("iME", "ime") |
| 106 return lowered | 70 return lowered |
| 107 | 71 |
| 108 def _feature_set_declaration(self, feature_set): | |
| 109 return " static void set%sFeaturesEnabled(bool);" % feature_set.capit
alize() | |
| 110 | |
| 111 def _feature_accessor_declaration(self, feature): | |
| 112 if feature['custom']: | |
| 113 return " static bool %(first_lowered_name)sEnabled();\n" % featur
e | |
| 114 unconditional = """ static void set%(name)sEnabled(bool isEnabled) {
is%(name)sEnabled = isEnabled; } | |
| 115 static bool %(first_lowered_name)sEnabled() { return %(enabled_condition)s;
} | |
| 116 """ | |
| 117 conditional = "#if ENABLE(%(condition)s)\n" + unconditional + """#else | |
| 118 static void set%(name)sEnabled(bool) { } | |
| 119 static bool %(first_lowered_name)sEnabled() { return false; } | |
| 120 #endif | |
| 121 """ | |
| 122 template = conditional if feature['condition'] else unconditional | |
| 123 return template % feature | |
| 124 | |
| 125 def _storage_declarations(self, feature): | |
| 126 declaration = " static bool is%(name)sEnabled;" % feature | |
| 127 return self.wrap_with_condition(declaration, feature['condition']) | |
| 128 | |
| 129 def generate_header(self): | |
| 130 return HEADER_TEMPLATE % { | |
| 131 'class_name' : self.class_name, | |
| 132 'license' : license.license_for_generated_cpp(), | |
| 133 'set_toggle_declarations' : "\n".join(map(self._feature_set_declarat
ion, self._feature_sets())), | |
| 134 'accessor_declarations' : "\n".join(map(self._feature_accessor_decla
ration, self._all_features)), | |
| 135 'storage_declarations' : "\n".join(map(self._storage_declarations, s
elf._non_custom_features)), | |
| 136 } | |
| 137 | |
| 138 def _feature_sets(self): | 72 def _feature_sets(self): |
| 139 # Another way to think of the status levels is as "sets of features" | 73 # Another way to think of the status levels is as "sets of features" |
| 140 # which is how we're referring to them in this generator. | 74 # which is how we're referring to them in this generator. |
| 141 return self.valid_values['status'] | 75 return self.valid_values['status'] |
| 142 | 76 |
| 143 def _feature_toggle(self, feature): | 77 def generate_header(self): |
| 144 return " set%(name)sEnabled(enable);" % feature | 78 return { |
| 145 | 79 'license': license.license_for_generated_cpp(), |
| 146 def _feature_set_definition(self, feature_set): | 80 'features': self._features, |
| 147 features_in_set = filter(lambda feature: feature['status'] == feature_se
t, self._all_features) | 81 'feature_sets': self._feature_sets(), |
| 148 template = """void %(class_name)s::set%(feature_set)sFeaturesEnabled(boo
l enable) | |
| 149 { | |
| 150 %(feature_toggles)s | |
| 151 } | |
| 152 """ | |
| 153 return template % { | |
| 154 'class_name': self.class_name, | |
| 155 'feature_set': feature_set.capitalize(), | |
| 156 'feature_toggles': "\n".join(map(self._feature_toggle, features_in_s
et)), | |
| 157 } | 82 } |
| 158 | 83 |
| 159 def _storage_definition(self, feature): | |
| 160 definition = "bool RuntimeEnabledFeatures::is%(name)sEnabled = false;" %
feature | |
| 161 return self.wrap_with_condition(definition, feature['condition']) | |
| 162 | |
| 163 def generate_implementation(self): | 84 def generate_implementation(self): |
| 164 return IMPLEMENTATION_TEMPLATE % { | 85 return { |
| 165 'class_name' : self.class_name, | 86 'license': license.license_for_generated_cpp(), |
| 166 'license' : license.license_for_generated_cpp(), | 87 'features': self._features, |
| 167 'set_toggle_definitions' : '\n'.join(map(self._feature_set_definitio
n, self._feature_sets())), | 88 'feature_sets': self._feature_sets(), |
| 168 'storage_definitions' : '\n'.join(map(self._storage_definition, self
._non_custom_features)), | |
| 169 } | 89 } |
| 170 | 90 |
| 171 | 91 |
| 172 if __name__ == "__main__": | 92 if __name__ == "__main__": |
| 173 in_generator.Maker(RuntimeFeatureWriter).main(sys.argv) | 93 in_generator.Maker(RuntimeFeatureWriter).main(sys.argv) |
| OLD | NEW |