tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

GenerateServoCSSPropList.py (4215B)


      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 http://mozilla.org/MPL/2.0/.
      4 
      5 import buildconfig
      6 import mozpack.path as mozpath
      7 import os
      8 import runpy
      9 import subprocess
     10 import string
     11 import sys
     12 
     13 SERVO_BASE = mozpath.join(buildconfig.topsrcdir, "servo")
     14 SERVO_PROP_BASE = mozpath.join(SERVO_BASE, "components", "style", "properties")
     15 
     16 
     17 def generate_data(output, template):
     18    output.write("# THIS IS AN AUTOGENERATED FILE.  DO NOT EDIT\n\n")
     19    output.write(
     20        subprocess.check_output(
     21            [
     22                sys.executable,
     23                mozpath.join(SERVO_PROP_BASE, "build.py"),
     24                "gecko",
     25                "geckolib",
     26                template,
     27            ],
     28            universal_newlines=True,
     29        )
     30    )
     31 
     32    # Add all relevant files into the dependencies of the generated file.
     33    DEP_EXTS = [".py", ".rs"]
     34    deps = set()
     35    for path, dirs, files in os.walk(SERVO_PROP_BASE):
     36        for file in files:
     37            if os.path.splitext(file)[1] in DEP_EXTS:
     38                deps.add(mozpath.join(path, file))
     39    return deps
     40 
     41 
     42 def generate_header(output, data):
     43    data = runpy.run_path(data)["data"]
     44 
     45    output.write(
     46        """/* THIS IS AN AUTOGENERATED FILE.  DO NOT EDIT */
     47 
     48 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     49 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
     50 /* This Source Code Form is subject to the terms of the Mozilla Public
     51 * License, v. 2.0. If a copy of the MPL was not distributed with this
     52 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
     53 
     54 #define CSS_PROP_DOMPROP_PREFIXED(name_) \\
     55  CSS_PROP_PUBLIC_OR_PRIVATE(Moz ## name_, name_)
     56 
     57 #ifndef CSS_PROP_LONGHAND
     58 #define CSS_PROP_LONGHAND(name_, id_, method_, flags_, pref_) /* nothing */
     59 #define DEFINED_CSS_PROP_LONGHAND
     60 #endif
     61 
     62 #ifndef CSS_PROP_SHORTHAND
     63 #define CSS_PROP_SHORTHAND(name_, id_, method_, flags_, pref_) /* nothing */
     64 #define DEFINED_CSS_PROP_SHORTHAND
     65 #endif
     66 
     67 #ifndef CSS_PROP_ALIAS
     68 #define CSS_PROP_ALIAS(name_, aliasid_, id_, method_, flags_, pref_) /* nothing */
     69 #define DEFINED_CSS_PROP_ALIAS
     70 #endif
     71 
     72 """
     73    )
     74 
     75    # Some flags are only used for code generation, so we don't need to
     76    # expose them to runtime.
     77    COMPILE_TIME_FLAGS = {"ExposedOnGetCS"}
     78 
     79    MACRO_NAMES = {
     80        "longhand": "CSS_PROP_LONGHAND",
     81        "shorthand": "CSS_PROP_SHORTHAND",
     82        "alias": "CSS_PROP_ALIAS",
     83    }
     84    for prop in data.values():
     85        is_internal = "Internal" in prop.flags
     86        flags = " | ".join(
     87            "CSSPropFlags::{}".format(flag)
     88            for flag in prop.flags
     89            if flag not in COMPILE_TIME_FLAGS
     90        )
     91        if not flags:
     92            flags = "CSSPropFlags(0)"
     93        pref = '"' + prop.pref + '"'
     94        method = prop.method
     95        if prop.type() == "alias":
     96            params = [prop.name, prop.alias_id, prop.prop_id, method, flags, pref]
     97        else:
     98            if method == "CssFloat":
     99                method = "CSS_PROP_PUBLIC_OR_PRIVATE(CssFloat, Float)"
    100            elif method.startswith("Moz"):
    101                method = "CSS_PROP_DOMPROP_PREFIXED({})".format(method[3:])
    102            params = [prop.name, prop.id, method, flags, pref]
    103        excludes = []
    104        if is_internal:
    105            excludes.append("CSS_PROP_LIST_EXCLUDE_INTERNAL")
    106        if "Style" not in prop.rules:
    107            excludes.append("CSS_PROP_LIST_EXCLUDE_NOT_IN_STYLE")
    108 
    109        if excludes:
    110            output.write(
    111                "#if {}\n".format(
    112                    " || ".join("!defined " + exclude for exclude in excludes)
    113                )
    114            )
    115        output.write("{}({})\n".format(MACRO_NAMES[prop.type()], ", ".join(params)))
    116        if excludes:
    117            output.write("#endif\n")
    118 
    119    output.write(
    120        """
    121 #ifdef DEFINED_CSS_PROP_ALIAS
    122 #undef CSS_PROP_ALIAS
    123 #undef DEFINED_CSS_PROP_ALIAS
    124 #endif
    125 
    126 #ifdef DEFINED_CSS_PROP_SHORTHAND
    127 #undef CSS_PROP_SHORTHAND
    128 #undef DEFINED_CSS_PROP_SHORTHAND
    129 #endif
    130 
    131 #ifdef DEFINED_CSS_PROP_LONGHAND
    132 #undef CSS_PROP_LONGHAND
    133 #undef DEFINED_CSS_PROP_LONGHAND
    134 #endif
    135 
    136 #undef CSS_PROP_DOMPROP_PREFIXED
    137 """
    138    )