tor-browser

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

generate_buildconfig.py (4750B)


      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 string
      6 import textwrap
      7 
      8 import buildconfig
      9 
     10 
     11 def generate_bool(name):
     12    value = buildconfig.substs.get(name)
     13    return f"pub const {name}: bool = {'true' if value else 'false'};\n"
     14 
     15 
     16 def generate_string_array(name):
     17    value = buildconfig.substs.get(name) or []
     18    return (
     19        f"pub const {name}: [&str; {len(value)}] = ["
     20        + ",".join(map(escape_rust_string, value))
     21        + "];\n"
     22    )
     23 
     24 
     25 def generate_string(name):
     26    value = buildconfig.substs.get(name) or ""
     27    return f"pub const {name}: &str = {escape_rust_string(value)};\n"
     28 
     29 
     30 def escape_rust_string(value):
     31    """escape the string into a Rust literal"""
     32    # This could be more generous, but we're only escaping paths with it.
     33    unescaped = string.ascii_letters + string.digits + "/$+-_~ "
     34    result = ""
     35    for ch in str(value):
     36        if ch in unescaped:
     37            result += ch
     38        elif ch == "\r":
     39            result += "\\r"
     40        elif ch == "\n":
     41            result += "\\n"
     42        elif ch == "\\":
     43            result += "\\\\"
     44        elif ch == '"':
     45            result += '\\"'
     46        else:
     47            result += "\\u{%x}" % ord(ch)
     48    return '"%s"' % result
     49 
     50 
     51 def generate(output):
     52    # Write out a macro which can be used within `include!`-like methods to
     53    # reference the topobjdir.
     54    output.write(
     55        textwrap.dedent(
     56            f"""
     57            /// Macro used to name a path in the objdir for use with macros like `include!`
     58            #[macro_export]
     59            macro_rules! objdir_path {{
     60                ($path:expr) => {{
     61                    concat!({escape_rust_string(buildconfig.topobjdir + "/")}, $path)
     62                }}
     63            }}
     64 
     65            /// Macro used to name a path in the srcdir for use with macros like `include!`
     66            #[macro_export]
     67            macro_rules! srcdir_path {{
     68                ($path:expr) => {{
     69                    concat!({escape_rust_string(buildconfig.topsrcdir + "/")}, $path)
     70                }}
     71            }}
     72 
     73            /// The objdir path for use in build scripts
     74            pub const TOPOBJDIR: &str = {escape_rust_string(buildconfig.topobjdir)};
     75            /// The srcdir path for use in build scripts
     76            pub const TOPSRCDIR: &str = {escape_rust_string(buildconfig.topsrcdir)};
     77 
     78            """
     79        )
     80    )
     81 
     82    windows_rs_dir = buildconfig.substs.get("MOZ_WINDOWS_RS_DIR")
     83    if windows_rs_dir:
     84        output.write(
     85            textwrap.dedent(
     86                f"""
     87                /// Macro used to name a path in the srcdir for use with macros like `include!`
     88                #[macro_export]
     89                macro_rules! windows_rs_path {{
     90                    ($path:literal) => {{
     91                        concat!({escape_rust_string(windows_rs_dir + "/")}, $path)
     92                    }}
     93                }}
     94 
     95                /// Macro used to re-export windows-rs's public items
     96                #[macro_export]
     97                macro_rules! windows_rs_lib {{
     98                    () => {{
     99                        #[path = {escape_rust_string(windows_rs_dir + "/src/lib.rs")}]
    100                        mod lib;
    101                        pub use lib::*;
    102                    }}
    103                }}
    104                """
    105            )
    106        )
    107 
    108    # Write out some useful strings from the buildconfig.
    109    output.write(generate_string("MOZ_MACBUNDLE_ID"))
    110    output.write(generate_string("MOZ_APP_BASENAME"))
    111    output.write(generate_string("MOZ_APP_NAME"))
    112    output.write(generate_string("MOZ_APP_VENDOR"))
    113    output.write(generate_string("MOZ_APP_VERSION"))
    114 
    115    # Write out some useful booleans from the buildconfig.
    116    output.write(generate_bool("MOZ_FOLD_LIBS"))
    117    output.write(generate_bool("NIGHTLY_BUILD"))
    118    output.write(generate_bool("RELEASE_OR_BETA"))
    119    output.write(generate_bool("EARLY_BETA_OR_EARLIER"))
    120    output.write(generate_bool("MOZ_DEV_EDITION"))
    121    output.write(generate_bool("MOZ_ESR"))
    122    output.write(generate_bool("MOZ_DIAGNOSTIC_ASSERT_ENABLED"))
    123    output.write(generate_bool("MOZ_CODE_COVERAGE"))
    124 
    125    # Used by toolkit/crashreporter/client
    126    output.write(generate_bool("MOZ_CRASHREPORTER_MOCK"))
    127    output.write(generate_string_array("BINDGEN_SYSTEM_FLAGS"))
    128    output.write(generate_string_array("MOZ_GTK3_CFLAGS"))
    129    output.write(generate_string_array("MOZ_GTK3_LIBS"))
    130    output.write(generate_string_array("NSPR_CFLAGS"))
    131    output.write(generate_string_array("NSS_CFLAGS"))
    132    output.write(generate_string_array("MOZ_PIXMAN_CFLAGS"))
    133    output.write(generate_string_array("MOZ_ICU_CFLAGS"))