Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(557)

Side by Side Diff: Source/core/scripts/make_runtime_features.py

Issue 15095002: Use jinja2 for make_runtime_features and make_internal_runtime_flags (Closed) Base URL: https://chromium.googlesource.com/chromium/blink@master
Patch Set: Updated for Eric's suggestions Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 14 matching lines...) Expand all
25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
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
36 35
37 36
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): 37 class RuntimeFeatureWriter(in_generator.Writer):
75 class_name = 'RuntimeEnabledFeatures' 38 class_name = 'RuntimeEnabledFeatures'
76 39
77 # FIXME: valid_values and defaults should probably roll into one object. 40 # FIXME: valid_values and defaults should probably roll into one object.
78 valid_values = { 41 valid_values = {
79 'status': ['stable', 'experimental', 'test'], 42 'status': ['stable', 'experimental', 'test'],
80 } 43 }
81 defaults = { 44 defaults = {
82 'condition' : None, 45 'condition' : None,
83 'depends_on' : [], 46 'depends_on' : [],
84 'custom': False, 47 'custom': False,
85 'status': None, 48 'status': None,
86 } 49 }
87 50
88 def __init__(self, in_file_path): 51 def __init__(self, in_file_path):
89 super(RuntimeFeatureWriter, self).__init__(in_file_path) 52 super(RuntimeFeatureWriter, self).__init__(in_file_path)
90 self._all_features = self.in_file.name_dictionaries 53 self._features = self.in_file.name_dictionaries
91 # Make sure the resulting dictionaries have all the keys we expect. 54 # Make sure the resulting dictionaries have all the keys we expect.
92 for feature in self._all_features: 55 for feature in self._features:
93 feature['first_lowered_name'] = self._lower_first(feature['name']) 56 feature['first_lowered_name'] = self._lower_first(feature['name'])
94 # Most features just check their isFooEnabled bool 57 # Most features just check their isFooEnabled bool
95 # but some depend on more than one bool. 58 # but some depend on more than one bool.
96 enabled_condition = "is%sEnabled" % feature['name'] 59 enabled_condition = "is%sEnabled" % feature['name']
97 for dependant_name in feature['depends_on']: 60 for dependant_name in feature['depends_on']:
98 enabled_condition += " && is%sEnabled" % dependant_name 61 enabled_condition += " && is%sEnabled" % dependant_name
99 feature['enabled_condition'] = enabled_condition 62 feature['enabled_condition'] = enabled_condition
100 self._non_custom_features = filter(lambda feature: not feature['custom'] , self._all_features) 63 self._non_custom_features = filter(lambda feature: not feature['custom'] , self._features)
101 64
102 def _lower_first(self, string): 65 def _lower_first(self, string):
103 lowered = string[0].lower() + string[1:] 66 lowered = string[0].lower() + string[1:]
104 lowered = lowered.replace("cSS", "css") 67 lowered = lowered.replace("cSS", "css")
105 lowered = lowered.replace("iME", "ime") 68 lowered = lowered.replace("iME", "ime")
106 return lowered 69 return lowered
107 70
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): 71 def _feature_sets(self):
139 # Another way to think of the status levels is as "sets of features" 72 # 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. 73 # which is how we're referring to them in this generator.
141 return self.valid_values['status'] 74 return self.valid_values['status']
142 75
143 def _feature_toggle(self, feature): 76 def generate_header(self):
144 return " set%(name)sEnabled(enable);" % feature 77 return {
145 78 'features': self._features,
146 def _feature_set_definition(self, feature_set): 79 'feature_sets': self._feature_sets(),
147 features_in_set = filter(lambda feature: feature['status'] == feature_se t, self._all_features)
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 } 80 }
158 81
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): 82 def generate_implementation(self):
164 return IMPLEMENTATION_TEMPLATE % { 83 return {
165 'class_name' : self.class_name, 84 'features': self._features,
166 'license' : license.license_for_generated_cpp(), 85 'feature_sets': self._feature_sets(),
167 'set_toggle_definitions' : '\n'.join(map(self._feature_set_definitio n, self._feature_sets())),
168 'storage_definitions' : '\n'.join(map(self._storage_definition, self ._non_custom_features)),
169 } 86 }
170 87
171 88
172 if __name__ == "__main__": 89 if __name__ == "__main__":
173 in_generator.Maker(RuntimeFeatureWriter).main(sys.argv) 90 in_generator.Maker(RuntimeFeatureWriter).main(sys.argv)
OLDNEW
« no previous file with comments | « Source/core/scripts/make_internal_runtime_flags.py ('k') | Source/core/scripts/template_expander.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698