summaryrefslogtreecommitdiffstats
path: root/python/mozbuild/mozbuild/test/backend/test_build.py
blob: 3287ba5e573d71ede6e5a9ba8791d2d534647c52 (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# 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 os
import shutil
import sys
import unittest
from contextlib import contextmanager
from tempfile import mkdtemp

import buildconfig
import mozpack.path as mozpath
import six
from mozfile import which
from mozpack.files import FileFinder
from mozunit import main

from mozbuild.backend import get_backend_class
from mozbuild.backend.configenvironment import ConfigEnvironment
from mozbuild.backend.fastermake import FasterMakeBackend
from mozbuild.backend.recursivemake import RecursiveMakeBackend
from mozbuild.base import MozbuildObject
from mozbuild.frontend.emitter import TreeMetadataEmitter
from mozbuild.frontend.reader import BuildReader
from mozbuild.util import ensureParentDir


def make_path():
    try:
        return buildconfig.substs["GMAKE"]
    except KeyError:
        fetches_dir = os.environ.get("MOZ_FETCHES_DIR")
        extra_search_dirs = ()
        if fetches_dir:
            extra_search_dirs = (os.path.join(fetches_dir, "mozmake"),)
        # Fallback for when running the test without an objdir.
        for name in ("gmake", "make", "mozmake", "gnumake", "mingw32-make"):
            path = which(name, extra_search_dirs=extra_search_dirs)
            if path:
                return path


BASE_SUBSTS = [
    ("PYTHON", mozpath.normsep(sys.executable)),
    ("PYTHON3", mozpath.normsep(sys.executable)),
    ("MOZ_UI_LOCALE", "en-US"),
    ("GMAKE", make_path()),
]


class TestBuild(unittest.TestCase):
    def setUp(self):
        self._old_env = dict(os.environ)
        os.environ.pop("MOZCONFIG", None)
        os.environ.pop("MOZ_OBJDIR", None)
        os.environ.pop("MOZ_PGO", None)

    def tearDown(self):
        os.environ.clear()
        os.environ.update(self._old_env)

    @contextmanager
    def do_test_backend(self, *backends, **kwargs):
        # Create the objdir in the srcdir to ensure that they share
        # the same drive on Windows.
        topobjdir = mkdtemp(dir=buildconfig.topsrcdir)
        try:
            config = ConfigEnvironment(buildconfig.topsrcdir, topobjdir, **kwargs)
            reader = BuildReader(config)
            emitter = TreeMetadataEmitter(config)
            moz_build = mozpath.join(config.topsrcdir, "test.mozbuild")
            definitions = list(emitter.emit(reader.read_mozbuild(moz_build, config)))
            for backend in backends:
                backend(config).consume(definitions)

            yield config
        except Exception:
            raise
        finally:
            if not os.environ.get("MOZ_NO_CLEANUP"):
                shutil.rmtree(topobjdir)

    @contextmanager
    def line_handler(self):
        lines = []

        def handle_make_line(line):
            lines.append(line)

        try:
            yield handle_make_line
        except Exception:
            print("\n".join(lines))
            raise

        if os.environ.get("MOZ_VERBOSE_MAKE"):
            print("\n".join(lines))

    def test_recursive_make(self):
        substs = list(BASE_SUBSTS)
        with self.do_test_backend(RecursiveMakeBackend, substs=substs) as config:
            build = MozbuildObject(config.topsrcdir, None, None, config.topobjdir)
            build._config_environment = config
            overrides = [
                "install_manifest_depends=",
                "MOZ_JAR_MAKER_FILE_FORMAT=flat",
                "TEST_MOZBUILD=1",
            ]
            with self.line_handler() as handle_make_line:
                build._run_make(
                    directory=config.topobjdir,
                    target=overrides,
                    silent=False,
                    line_handler=handle_make_line,
                )

            self.validate(config)

    def test_faster_recursive_make(self):
        substs = list(BASE_SUBSTS) + [
            ("BUILD_BACKENDS", "FasterMake+RecursiveMake"),
        ]
        with self.do_test_backend(
            get_backend_class("FasterMake+RecursiveMake"), substs=substs
        ) as config:
            buildid = mozpath.join(config.topobjdir, "config", "buildid")
            ensureParentDir(buildid)
            with open(buildid, "w") as fh:
                fh.write("20100101012345\n")

            build = MozbuildObject(config.topsrcdir, None, None, config.topobjdir)
            build._config_environment = config
            overrides = [
                "install_manifest_depends=",
                "MOZ_JAR_MAKER_FILE_FORMAT=flat",
                "TEST_MOZBUILD=1",
            ]
            with self.line_handler() as handle_make_line:
                build._run_make(
                    directory=config.topobjdir,
                    target=overrides,
                    silent=False,
                    line_handler=handle_make_line,
                )

            self.validate(config)

    def test_faster_make(self):
        substs = list(BASE_SUBSTS) + [
            ("MOZ_BUILD_APP", "dummy_app"),
            ("MOZ_WIDGET_TOOLKIT", "dummy_widget"),
        ]
        with self.do_test_backend(
            RecursiveMakeBackend, FasterMakeBackend, substs=substs
        ) as config:
            buildid = mozpath.join(config.topobjdir, "config", "buildid")
            ensureParentDir(buildid)
            with open(buildid, "w") as fh:
                fh.write("20100101012345\n")

            build = MozbuildObject(config.topsrcdir, None, None, config.topobjdir)
            build._config_environment = config
            overrides = [
                "TEST_MOZBUILD=1",
            ]
            with self.line_handler() as handle_make_line:
                build._run_make(
                    directory=mozpath.join(config.topobjdir, "faster"),
                    target=overrides,
                    silent=False,
                    line_handler=handle_make_line,
                )

            self.validate(config)

    def validate(self, config):
        self.maxDiff = None
        test_path = mozpath.join(
            "$SRCDIR",
            "python",
            "mozbuild",
            "mozbuild",
            "test",
            "backend",
            "data",
            "build",
        )

        result = {
            p: six.ensure_text(f.open().read())
            for p, f in FileFinder(mozpath.join(config.topobjdir, "dist"))
        }
        self.assertTrue(len(result))
        self.assertEqual(
            result,
            {
                "bin/baz.ini": "baz.ini: FOO is foo\n",
                "bin/child/bar.ini": "bar.ini\n",
                "bin/child2/foo.css": "foo.css: FOO is foo\n",
                "bin/child2/qux.ini": "qux.ini: BAR is not defined\n",
                "bin/chrome.manifest": "manifest chrome/foo.manifest\n"
                "manifest components/components.manifest\n",
                "bin/chrome/foo.manifest": "content bar foo/child/\n"
                "content foo foo/\n"
                "override chrome://foo/bar.svg#hello "
                "chrome://bar/bar.svg#hello\n",
                "bin/chrome/foo/bar.js": "bar.js\n",
                "bin/chrome/foo/child/baz.jsm": '//@line 2 "%s/baz.jsm"\nbaz.jsm: FOO is foo\n'
                % (test_path),
                "bin/chrome/foo/child/hoge.js": '//@line 2 "%s/bar.js"\nbar.js: FOO is foo\n'
                % (test_path),
                "bin/chrome/foo/foo.css": "foo.css: FOO is foo\n",
                "bin/chrome/foo/foo.js": "foo.js\n",
                "bin/chrome/foo/qux.js": "bar.js\n",
                "bin/components/bar.js": '//@line 2 "%s/bar.js"\nbar.js: FOO is foo\n'
                % (test_path),
                "bin/components/components.manifest": "component {foo} foo.js\ncomponent {bar} bar.js\n",  # NOQA: E501
                "bin/components/foo.js": "foo.js\n",
                "bin/defaults/pref/prefs.js": "prefs.js\n",
                "bin/foo.ini": "foo.ini\n",
                "bin/modules/baz.jsm": '//@line 2 "%s/baz.jsm"\nbaz.jsm: FOO is foo\n'
                % (test_path),
                "bin/modules/child/bar.jsm": "bar.jsm\n",
                "bin/modules/child2/qux.jsm": '//@line 4 "%s/qux.jsm"\nqux.jsm: BAR is not defined\n'  # NOQA: E501
                % (test_path),
                "bin/modules/foo.jsm": "foo.jsm\n",
                "bin/res/resource": "resource\n",
                "bin/res/child/resource2": "resource2\n",
                "bin/app/baz.ini": "baz.ini: FOO is bar\n",
                "bin/app/child/bar.ini": "bar.ini\n",
                "bin/app/child2/qux.ini": "qux.ini: BAR is defined\n",
                "bin/app/chrome.manifest": "manifest chrome/foo.manifest\n"
                "manifest components/components.manifest\n",
                "bin/app/chrome/foo.manifest": "content bar foo/child/\n"
                "content foo foo/\n"
                "override chrome://foo/bar.svg#hello "
                "chrome://bar/bar.svg#hello\n",
                "bin/app/chrome/foo/bar.js": "bar.js\n",
                "bin/app/chrome/foo/child/baz.jsm": '//@line 2 "%s/baz.jsm"\nbaz.jsm: FOO is bar\n'
                % (test_path),
                "bin/app/chrome/foo/child/hoge.js": '//@line 2 "%s/bar.js"\nbar.js: FOO is bar\n'
                % (test_path),
                "bin/app/chrome/foo/foo.css": "foo.css: FOO is bar\n",
                "bin/app/chrome/foo/foo.js": "foo.js\n",
                "bin/app/chrome/foo/qux.js": "bar.js\n",
                "bin/app/components/bar.js": '//@line 2 "%s/bar.js"\nbar.js: FOO is bar\n'
                % (test_path),
                "bin/app/components/components.manifest": "component {foo} foo.js\ncomponent {bar} bar.js\n",  # NOQA: E501
                "bin/app/components/foo.js": "foo.js\n",
                "bin/app/defaults/preferences/prefs.js": "prefs.js\n",
                "bin/app/foo.css": "foo.css: FOO is bar\n",
                "bin/app/foo.ini": "foo.ini\n",
                "bin/app/modules/baz.jsm": '//@line 2 "%s/baz.jsm"\nbaz.jsm: FOO is bar\n'
                % (test_path),
                "bin/app/modules/child/bar.jsm": "bar.jsm\n",
                "bin/app/modules/child2/qux.jsm": '//@line 2 "%s/qux.jsm"\nqux.jsm: BAR is defined\n'  # NOQA: E501
                % (test_path),
                "bin/app/modules/foo.jsm": "foo.jsm\n",
            },
        )


if __name__ == "__main__":
    main()