summaryrefslogtreecommitdiffstats
path: root/build/rust/mozbuild/generate_buildconfig.py
blob: 09c32c37fd92636269275aea4d6653918350e86a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.

import string
import textwrap

import buildconfig


def generate_bool(name):
    value = buildconfig.substs.get(name)
    return f"pub const {name}: bool = {'true' if value else 'false'};\n"


def escape_rust_string(value):
    """escape the string into a Rust literal"""
    # This could be more generous, but we're only escaping paths with it.
    unescaped = string.ascii_letters + string.digits + "/$+-_~ "
    result = ""
    for ch in str(value):
        if ch in unescaped:
            result += ch
        elif ch == "\r":
            result += "\\r"
        elif ch == "\n":
            result += "\\n"
        elif ch == "\\":
            result += "\\\\"
        elif ch == '"':
            result += '\\"'
        else:
            result += "\\u{%x}" % ord(ch)
    return '"%s"' % result


def generate_string(buildvar, output):
    buildconfig_var = buildconfig.substs.get(buildvar)
    if buildconfig_var is not None:
        output.write(
            f"pub const {buildvar}: &str = {escape_rust_string(buildconfig_var)};\n"
        )


def generate(output):
    # Write out a macro which can be used within `include!`-like methods to
    # reference the topobjdir.
    output.write(
        textwrap.dedent(
            f"""
            /// Macro used to name a path in the objdir for use with macros like `include!`
            #[macro_export]
            macro_rules! objdir_path {{
                ($path:literal) => {{
                    concat!({escape_rust_string(buildconfig.topobjdir + "/")}, $path)
                }}
            }}

            /// Macro used to name a path in the srcdir for use with macros like `include!`
            #[macro_export]
            macro_rules! srcdir_path {{
                ($path:literal) => {{
                    concat!({escape_rust_string(buildconfig.topsrcdir + "/")}, $path)
                }}
            }}

            /// The objdir path for use in build scripts
            pub const TOPOBJDIR: &str = {escape_rust_string(buildconfig.topobjdir)};
            /// The srcdir path for use in build scripts
            pub const TOPSRCDIR: &str = {escape_rust_string(buildconfig.topsrcdir)};

            """
        )
    )

    windows_rs_dir = buildconfig.substs.get("MOZ_WINDOWS_RS_DIR")
    if windows_rs_dir:
        output.write(
            textwrap.dedent(
                f"""
                /// Macro used to name a path in the srcdir for use with macros like `include!`
                #[macro_export]
                macro_rules! windows_rs_path {{
                    ($path:literal) => {{
                        concat!({escape_rust_string(windows_rs_dir + "/")}, $path)
                    }}
                }}

                /// The path to the windows-rs crate, for use in build scripts
                pub const WINDOWS_RS_DIR: &str = {escape_rust_string(windows_rs_dir)};

                """
            )
        )

    # Write out some useful strings from the buildconfig.
    generate_string("MOZ_MACBUNDLE_ID", output)
    generate_string("MOZ_APP_NAME", output)

    # Finally, write out some useful booleans from the buildconfig.
    output.write(generate_bool("MOZ_FOLD_LIBS"))
    output.write(generate_bool("NIGHTLY_BUILD"))
    output.write(generate_bool("RELEASE_OR_BETA"))
    output.write(generate_bool("EARLY_BETA_OR_EARLIER"))
    output.write(generate_bool("MOZ_DEV_EDITION"))
    output.write(generate_bool("MOZ_ESR"))
    output.write(generate_bool("MOZ_DIAGNOSTIC_ASSERT_ENABLED"))