Bug 1883017: Handle resizable buffers in ArrayBufferObject::ensureNonInline. r=sfink
[gecko.git] / servo / components / style / properties / build.py
blob6c3ee0cf66a06eb55c1465c8aae26aef3a9d4aba
1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at https://mozilla.org/MPL/2.0/.
5 import json
6 import os.path
7 import re
8 import sys
10 BASE = os.path.dirname(__file__.replace("\\", "/"))
11 sys.path.insert(0, os.path.join(BASE, "Mako-1.1.2-py2.py3-none-any.whl"))
12 sys.path.insert(0, BASE) # For importing `data.py`
14 from mako import exceptions
15 from mako.lookup import TemplateLookup
16 from mako.template import Template
18 import data
20 RE_PYTHON_ADDR = re.compile(r"<.+? object at 0x[0-9a-fA-F]+>")
22 OUT_DIR = os.environ.get("OUT_DIR", "")
24 STYLE_STRUCT_LIST = [
25 "background",
26 "border",
27 "box",
28 "column",
29 "counters",
30 "effects",
31 "font",
32 "inherited_box",
33 "inherited_svg",
34 "inherited_table",
35 "inherited_text",
36 "inherited_ui",
37 "list",
38 "margin",
39 "outline",
40 "page",
41 "padding",
42 "position",
43 "svg",
44 "table",
45 "text",
46 "ui",
47 "xul",
51 def main():
52 usage = (
53 "Usage: %s [ servo-2013 | servo-2020 | gecko ] [ style-crate | geckolib <template> | html ]"
54 % sys.argv[0]
56 if len(sys.argv) < 3:
57 abort(usage)
58 engine = sys.argv[1]
59 output = sys.argv[2]
61 if engine not in ["servo-2013", "servo-2020", "gecko"] or output not in [
62 "style-crate",
63 "geckolib",
64 "html",
66 abort(usage)
68 properties = data.PropertiesData(engine=engine)
69 files = {}
70 for kind in ["longhands", "shorthands"]:
71 files[kind] = {}
72 for struct in STYLE_STRUCT_LIST:
73 file_name = os.path.join(BASE, kind, "{}.mako.rs".format(struct))
74 if kind == "shorthands" and not os.path.exists(file_name):
75 files[kind][struct] = ""
76 continue
77 files[kind][struct] = render(
78 file_name,
79 engine=engine,
80 data=properties,
82 properties_template = os.path.join(BASE, "properties.mako.rs")
83 files["properties"] = render(
84 properties_template,
85 engine=engine,
86 data=properties,
87 __file__=properties_template,
88 OUT_DIR=OUT_DIR,
90 if output == "style-crate":
91 write(OUT_DIR, "properties.rs", files["properties"])
92 for kind in ["longhands", "shorthands"]:
93 for struct in files[kind]:
94 write(
95 os.path.join(OUT_DIR, kind),
96 "{}.rs".format(struct),
97 files[kind][struct],
100 if engine == "gecko":
101 template = os.path.join(BASE, "gecko.mako.rs")
102 rust = render(template, data=properties)
103 write(OUT_DIR, "gecko_properties.rs", rust)
105 if engine in ["servo-2013", "servo-2020"]:
106 if engine == "servo-2013":
107 pref_attr = "servo_2013_pref"
108 if engine == "servo-2020":
109 pref_attr = "servo_2020_pref"
110 properties_dict = {
111 kind: {
112 p.name: {"pref": getattr(p, pref_attr)}
113 for prop in properties_list
114 if prop.enabled_in_content()
115 for p in [prop] + prop.alias
117 for kind, properties_list in [
118 ("longhands", properties.longhands),
119 ("shorthands", properties.shorthands),
122 as_html = render(
123 os.path.join(BASE, "properties.html.mako"), properties=properties_dict
125 as_json = json.dumps(properties_dict, indent=4, sort_keys=True)
126 doc_servo = os.path.join(BASE, "..", "..", "..", "target", "doc", "servo")
127 write(doc_servo, "css-properties.html", as_html)
128 write(doc_servo, "css-properties.json", as_json)
129 write(OUT_DIR, "css-properties.json", as_json)
130 elif output == "geckolib":
131 if len(sys.argv) < 4:
132 abort(usage)
133 template = sys.argv[3]
134 header = render(template, data=properties)
135 sys.stdout.write(header)
138 def abort(message):
139 print(message, file=sys.stderr)
140 sys.exit(1)
143 def render(filename, **context):
144 try:
145 lookup = TemplateLookup(
146 directories=[BASE], input_encoding="utf8", strict_undefined=True
148 template = Template(
149 open(filename, "rb").read(),
150 filename=filename,
151 input_encoding="utf8",
152 lookup=lookup,
153 strict_undefined=True,
155 # Uncomment to debug generated Python code:
156 # write("/tmp", "mako_%s.py" % os.path.basename(filename), template.code)
157 return template.render(**context)
158 except Exception:
159 # Uncomment to see a traceback in generated Python code:
160 # raise
161 abort(exceptions.text_error_template().render())
164 def write(directory, filename, content):
165 if not os.path.exists(directory):
166 os.makedirs(directory)
167 full_path = os.path.join(directory, filename)
168 open(full_path, "w", encoding="utf-8").write(content)
170 python_addr = RE_PYTHON_ADDR.search(content)
171 if python_addr:
172 abort('Found "{}" in {} ({})'.format(python_addr.group(0), filename, full_path))
175 if __name__ == "__main__":
176 main()