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 15 matching lines...) Expand all Loading... |
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 | 35 import license |
| 36 import template_expander |
36 | 37 |
37 | 38 |
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 %(method_declarations)s | |
49 private: | |
50 %(class_name)s() { } | |
51 | |
52 %(storage_declarations)s | |
53 }; | |
54 | |
55 } // namespace WebCore | |
56 | |
57 #endif // %(class_name)s_h | |
58 """ | |
59 | |
60 IMPLEMENTATION_TEMPLATE = """%(license)s | |
61 #include "config.h" | |
62 #include "%(class_name)s.h" | |
63 | |
64 namespace WebCore { | |
65 | |
66 %(storage_definitions)s | |
67 | |
68 } // namespace WebCore | |
69 """ | |
70 | |
71 class RuntimeFeatureWriter(in_generator.Writer): | 39 class RuntimeFeatureWriter(in_generator.Writer): |
72 class_name = "RuntimeEnabledFeatures" | 40 class_name = "RuntimeEnabledFeatures" |
73 defaults = { | 41 defaults = { |
74 'condition' : None, | 42 'condition' : None, |
75 'depends_on' : [], | 43 'depends_on' : [], |
76 'default': 'false', | 44 'default': 'false', |
77 'custom': False, | 45 'custom': False, |
78 } | 46 } |
79 | 47 |
80 def __init__(self, in_file_path): | 48 def __init__(self, in_file_path): |
81 super(RuntimeFeatureWriter, self).__init__(in_file_path) | 49 super(RuntimeFeatureWriter, self).__init__(in_file_path) |
82 self._all_features = self.in_file.name_dictionaries | 50 self._features = self.in_file.name_dictionaries |
83 # Make sure the resulting dictionaries have all the keys we expect. | 51 # Make sure the resulting dictionaries have all the keys we expect. |
84 for feature in self._all_features: | 52 for feature in self._features: |
85 feature['first_lowered_name'] = self._lower_first(feature['name']) | 53 feature['first_lowered_name'] = self._lower_first(feature['name']) |
86 # Most features just check their isFooEnabled bool | 54 # Most features just check their isFooEnabled bool |
87 # but some depend on more than one bool. | 55 # but some depend on more than one bool. |
88 enabled_condition = "is%sEnabled" % feature['name'] | 56 enabled_condition = "is%sEnabled" % feature['name'] |
89 for dependant_name in feature['depends_on']: | 57 for dependant_name in feature['depends_on']: |
90 enabled_condition += " && is%sEnabled" % dependant_name | 58 enabled_condition += " && is%sEnabled" % dependant_name |
91 feature['enabled_condition'] = enabled_condition | 59 feature['enabled_condition'] = enabled_condition |
92 self._non_custom_features = filter(lambda feature: not feature['custom']
, self._all_features) | |
93 | 60 |
94 def _lower_first(self, string): | 61 def _lower_first(self, string): |
95 lowered = string[0].lower() + string[1:] | 62 lowered = string[0].lower() + string[1:] |
96 lowered = lowered.replace("cSS", "css") | 63 lowered = lowered.replace("cSS", "css") |
97 lowered = lowered.replace("iME", "ime") | 64 lowered = lowered.replace("iME", "ime") |
98 return lowered | 65 return lowered |
99 | 66 |
100 def _method_declaration(self, feature): | |
101 if feature['custom']: | |
102 return " static bool %(first_lowered_name)sEnabled();\n" % featur
e | |
103 unconditional = """ static void set%(name)sEnabled(bool isEnabled) {
is%(name)sEnabled = isEnabled; } | |
104 static bool %(first_lowered_name)sEnabled() { return %(enabled_condition)s;
} | |
105 """ | |
106 conditional = "#if ENABLE(%(condition)s)\n" + unconditional + """#else | |
107 static void set%(name)sEnabled(bool) { } | |
108 static bool %(first_lowered_name)sEnabled() { return false; } | |
109 #endif | |
110 """ | |
111 template = conditional if feature['condition'] else unconditional | |
112 return template % feature | |
113 | |
114 def _storage_declarations(self, feature): | |
115 declaration = " static bool is%(name)sEnabled;" % feature | |
116 return self.wrap_with_condition(declaration, feature['condition']) | |
117 | |
118 def generate_header(self): | 67 def generate_header(self): |
119 return HEADER_TEMPLATE % { | 68 params = { |
120 'class_name' : self.class_name, | |
121 'license' : license.license_for_generated_cpp(), | 69 'license' : license.license_for_generated_cpp(), |
122 'method_declarations' : "\n".join(map(self._method_declaration, self
._all_features)), | 70 'features' : self._features |
123 'storage_declarations' : "\n".join(map(self._storage_declarations, s
elf._non_custom_features)), | |
124 } | 71 } |
125 | 72 return template_expander.applytemplate("../page/RuntimeEnabledFeatures.h
.tmpl", params) |
126 def _storage_definition(self, feature): | |
127 definition = "bool RuntimeEnabledFeatures::is%(name)sEnabled = %(default
)s;" % feature | |
128 return self.wrap_with_condition(definition, feature['condition']) | |
129 | 73 |
130 def generate_implementation(self): | 74 def generate_implementation(self): |
131 return IMPLEMENTATION_TEMPLATE % { | 75 params = { |
132 'class_name' : self.class_name, | |
133 'license' : license.license_for_generated_cpp(), | 76 'license' : license.license_for_generated_cpp(), |
134 'storage_definitions' : "\n".join(map(self._storage_definition, self
._non_custom_features)), | 77 'features' : self._features |
135 } | 78 } |
| 79 return template_expander.applytemplate("../page/RuntimeEnabledFeatures.c
pp.tmpl", params) |
136 | 80 |
137 | 81 |
138 if __name__ == "__main__": | 82 if __name__ == "__main__": |
139 in_generator.Maker(RuntimeFeatureWriter).main(sys.argv) | 83 in_generator.Maker(RuntimeFeatureWriter).main(sys.argv) |
OLD | NEW |