summaryrefslogtreecommitdiff
path: root/zephyr/zmake/zmake/zmake.py
blob: fb88dae7e9860d43fa95f5fe17377a83db608cb9 (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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
# Copyright 2020 The ChromiumOS Authors
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Module encapsulating Zmake wrapper object."""
import atexit
import difflib
import functools
import logging
import os
import pathlib
import re
import shutil
import subprocess
import tempfile
from typing import Dict, Optional, Set, Union

import zmake.build_config
import zmake.compare_builds
import zmake.generate_readme
import zmake.jobserver
import zmake.modules
import zmake.multiproc
import zmake.project
import zmake.util as util
import zmake.version

ninja_warnings = re.compile(r"^(\S*: )?warning:.*")
ninja_errors = re.compile(r"error:.*")


def ninja_stdout_log_level_override(line, current_log_level):
    """Update the log level for ninja builds if we hit an error.

    Ninja builds prints everything to stdout, but really we want to start
    logging things to CRITICAL

    Args:
        line: The line that is about to be logged.
        current_log_level: The active logging level that would be used for the
          line.
    """
    # pylint: disable=too-many-return-statements
    # Output lines from Zephyr that are not normally useful
    # Send any lines that start with these strings to INFO
    cmake_suppress = [
        "-- ",  # device tree messages
        "Loaded configuration",
        "Including boilerplate",
        "Parsing ",
        "No change to configuration",
        "No change to Kconfig header",
    ]

    # Herewith a long list of things which are really for debugging, not
    # development. Return logging.DEBUG for each of these.

    # ninja puts progress information on stdout
    if line.startswith("["):
        return logging.DEBUG
    # we don't care about entering directories since it happens every time
    if line.startswith("ninja: Entering directory"):
        return logging.DEBUG
    # we know the build stops from the compiler messages and ninja return code
    if line.startswith("ninja: build stopped"):
        return logging.DEBUG
    # someone prints a *** SUCCESS *** message which we don't need
    if line.startswith("***"):
        return logging.DEBUG
    # dopey ninja puts errors on stdout, so fix that. It does not look
    # likely that it will be fixed upstream:
    # https://github.com/ninja-build/ninja/issues/1537
    # Try to drop output about the device tree
    if any(line.startswith(x) for x in cmake_suppress):
        return logging.INFO
    # this message is a bit like make failing. We already got the error output.
    if line.startswith("FAILED: CMakeFiles"):
        return logging.INFO
    # if a particular file fails it shows the build line used, but that is not
    # useful except for debugging.
    if line.startswith("ccache"):
        return logging.DEBUG
    if ninja_warnings.match(line):
        return logging.WARNING
    if ninja_errors.match(line):
        return logging.ERROR
    # When we see "Memory region" go into INFO, and stay there as long as the
    # line starts with \S+:
    if line.startswith("Memory region"):
        return logging.INFO
    if current_log_level == logging.INFO and line.split()[0].endswith(":"):
        return current_log_level
    if current_log_level == logging.WARNING:
        return current_log_level
    return logging.ERROR


def cmake_log_level_override(line, default_log_level):
    """Update the log level for cmake output if we hit an error.

    Cmake prints some messages that are less than useful during
    development.

    Args:
        line: The line that is about to be logged.
        default_log_level: The default logging level that will be used for the
          line.
    """
    # Strange output from Zephyr that we normally ignore
    if line.startswith("Including boilerplate"):
        return logging.DEBUG
    if line.startswith("devicetree error:"):
        return logging.ERROR
    if ninja_warnings.match(line):
        return logging.WARNING
    if ninja_errors.match(line):
        return logging.ERROR
    return default_log_level


def get_process_failure_msg(proc):
    """Creates a suitable failure message if something exits badly

    Args:
        proc: subprocess.Popen object containing the thing that failed

    Returns:
        Failure message as a string:
    """
    return "Execution failed (return code={}): {}\n".format(
        proc.returncode, util.repr_command(proc.args)
    )


class Zmake:
    """Wrapper class encapsulating zmake's supported operations.

    The invocations of the constructor and the methods actually comes
    from the main function.  The command line arguments are translated
    such that dashes are replaced with underscores and applied as
    keyword arguments to the constructor and the method, and the
    subcommand invoked becomes the method run.

    As such, you won't find documentation for each method's parameters
    here, as it would be duplicate of the help strings from the
    command line.  Run "zmake --help" for full documentation of each
    parameter.

    Properties:
        executor: a zmake.multiproc.Executor object for submitting
            tasks to.
        _sequential: True to check the results of each build job sequentially,
            before launching more, False to just do this after all jobs complete
    """

    # pylint: disable=too-many-instance-attributes

    def __init__(
        self,
        checkout=None,
        jobserver: Optional[zmake.jobserver.JobClient] = None,
        jobs=0,
        goma=False,
        gomacc="/mnt/host/depot_tools/.cipd_bin/gomacc",
        modules_dir=None,
        zephyr_base=None,
    ):
        zmake.multiproc.LogWriter.reset()
        self.logger = logging.getLogger(self.__class__.__name__)
        self._checkout = checkout
        self.goma = goma
        self.gomacc = gomacc
        if zephyr_base:
            self.zephyr_base = zephyr_base
        else:
            self.zephyr_base = (
                self.checkout / "src" / "third_party" / "zephyr" / "main"
            )

        if modules_dir:
            self.module_paths = zmake.modules.locate_from_directory(modules_dir)
        else:
            self.module_paths = zmake.modules.locate_from_checkout(
                self.checkout
            )

        if jobserver:
            self.jobserver = jobserver
        else:
            self.jobserver = zmake.jobserver.GNUMakeJobServer(jobs=jobs)

        self.executor = zmake.multiproc.Executor()
        self._sequential = self.jobserver.is_sequential() and not goma
        self.failed_projects = []

    @property
    def checkout(self):
        """Returns the location of the cros checkout."""
        if not self._checkout:
            self._checkout = util.locate_cros_checkout()
        return self._checkout.resolve()

    def _resolve_projects(
        self,
        project_names,
        all_projects=False,
    ) -> Set[zmake.project.Project]:
        """Finds all projects for the specified command line flags.

        Returns a list of projects.
        """
        found_projects = zmake.project.find_projects(
            self.module_paths["ec"] / "zephyr"
        )
        if all_projects:
            projects = set(found_projects.values())
        else:
            projects = set()
            for project_name in project_names:
                try:
                    projects.add(found_projects[project_name])
                except KeyError as e:
                    raise KeyError(
                        "No project named {}".format(project_name)
                    ) from e
        return projects

    def configure(
        self,
        project_names,
        build_dir=None,
        toolchain=None,
        build_after_configure=False,
        clobber=False,
        bringup=False,
        coverage=False,
        allow_warnings=False,
        all_projects=False,
        extra_cflags=None,
        delete_intermediates=False,
        static_version=False,
        save_temps=False,
        wait_for_executor=True,
    ):
        """Locate and configure the specified projects."""
        # Resolve build_dir if needed.
        if not build_dir:
            build_dir = self.module_paths["ec"] / "build" / "zephyr"

        projects = self._resolve_projects(
            project_names,
            all_projects=all_projects,
        )
        for project in projects:
            project_build_dir = (
                pathlib.Path(build_dir) / project.config.project_name
            )
            self.executor.append(
                func=functools.partial(
                    self._configure,
                    project=project,
                    build_dir=project_build_dir,
                    toolchain=toolchain,
                    build_after_configure=build_after_configure,
                    clobber=clobber,
                    bringup=bringup,
                    coverage=coverage,
                    allow_warnings=allow_warnings,
                    extra_cflags=extra_cflags,
                    delete_intermediates=delete_intermediates,
                    static_version=static_version,
                    save_temps=save_temps,
                )
            )
            if self._sequential:
                result = self.executor.wait()
                if result:
                    return result
        non_test_projects = [p for p in projects if not p.config.is_test]
        if len(non_test_projects) > 1 and coverage and build_after_configure:
            result = self.executor.wait()
            if result:
                return result
            result = self._merge_lcov_files(
                projects=non_test_projects,
                build_dir=build_dir,
                output_file=build_dir / "all_builds.info",
            )
            if result:
                self.failed_projects.append(str(build_dir / "all_builds.info"))
                return result
        elif wait_for_executor:
            result = self.executor.wait()
            if result:
                return result

        return 0

    def build(
        self,
        project_names,
        build_dir=None,
        toolchain=None,
        clobber=False,
        bringup=False,
        coverage=False,
        allow_warnings=False,
        all_projects=False,
        extra_cflags=None,
        delete_intermediates=False,
        static_version=False,
        save_temps=False,
    ):
        """Locate and build the specified projects."""
        return self.configure(
            project_names,
            build_dir=build_dir,
            toolchain=toolchain,
            clobber=clobber,
            bringup=bringup,
            coverage=coverage,
            allow_warnings=allow_warnings,
            all_projects=all_projects,
            extra_cflags=extra_cflags,
            build_after_configure=True,
            delete_intermediates=delete_intermediates,
            static_version=static_version,
            save_temps=save_temps,
        )

    def compare_builds(
        self,
        ref1,
        ref2,
        project_names,
        toolchain=None,
        all_projects=False,
        extra_cflags=None,
        keep_temps=False,
    ):
        """Compare EC builds at two commits."""
        temp_dir = tempfile.mkdtemp(prefix="zcompare-")
        if not keep_temps:
            atexit.register(shutil.rmtree, temp_dir)
        else:
            self.logger.info("Temporary dir %s will be retained", temp_dir)

        projects = self._resolve_projects(
            project_names,
            all_projects=all_projects,
        )

        self.logger.info("Compare zephyr builds")

        cmp_builds = zmake.compare_builds.CompareBuilds(temp_dir, ref1, ref2)

        for checkout in cmp_builds.checkouts:
            self.logger.info(
                "Checkout %s: full hash %s", checkout.ref, checkout.full_ref
            )

        cmp_builds.do_checkouts(self.zephyr_base, self.module_paths)

        for checkout in cmp_builds.checkouts:
            # Now that the sources have been checked out, transform the
            # zephyr-base and module-paths to use the temporary directory
            # created by BuildInfo.
            for module_name in self.module_paths.keys():
                new_path = checkout.modules_dir / module_name
                transformed_module = {module_name: new_path}
                self.module_paths.update(transformed_module)

            self.zephyr_base = checkout.zephyr_dir

            self.logger.info("Building projects at %s", checkout.ref)
            result = self.configure(
                project_names,
                build_dir=None,
                toolchain=toolchain,
                clobber=False,
                bringup=False,
                coverage=False,
                allow_warnings=False,
                all_projects=all_projects,
                extra_cflags=extra_cflags,
                build_after_configure=True,
                delete_intermediates=False,
                static_version=True,
                save_temps=False,
                wait_for_executor=False,
            )
            if not result:
                result = self.executor.wait()
            if result:
                self.logger.error(
                    "compare-builds failed to build all projects at %s",
                    checkout.ref,
                )
                return result

        self.failed_projects = cmp_builds.check_binaries(projects)

        if len(self.failed_projects) == 0:
            self.logger.info("Zephyr compare builds successful:")
            for checkout in cmp_builds.checkouts:
                self.logger.info("   %s: %s", checkout.ref, checkout.full_ref)

        return len(self.failed_projects)

    def test(  # pylint: disable=unused-argument
        self,
        project_names,
    ):
        """Build and run tests for the specified projects.

        Using zmake to run tests is no longer supported. Use twister.
        """
        self.logger.error(
            "zmake test is deprecated. Use twister -T zephyr/test/<test_dir>."
        )

        return 0

    def testall(
        self,
    ):
        """Build and run tests for all projects.

        Using zmake to run tests is no longer supported. Use twister.
        """
        self.logger.error(
            "zmake testall is deprecated. To build all packages, use zmake build -a."
        )
        return self.test([])

    def _configure(
        self,
        project,
        build_dir: pathlib.Path,
        toolchain=None,
        build_after_configure=False,
        clobber=False,
        bringup=False,
        coverage=False,
        allow_warnings=False,
        extra_cflags=None,
        delete_intermediates=False,
        static_version=False,
        save_temps=False,
    ):
        """Set up a build directory to later be built by "zmake build"."""
        try:
            with self.jobserver.get_job():
                # Clobber build directory if requested.
                if clobber and build_dir.exists():
                    self.logger.info(
                        "Clearing build directory %s due to --clobber",
                        build_dir,
                    )
                    shutil.rmtree(build_dir)

                generated_include_dir = (build_dir / "include").resolve()
                base_config = zmake.build_config.BuildConfig(
                    cmake_defs={
                        "CMAKE_EXPORT_COMPILE_COMMANDS": "ON",
                        "DTS_ROOT": str(self.module_paths["ec"] / "zephyr"),
                        "SYSCALL_INCLUDE_DIRS": str(
                            self.module_paths["ec"]
                            / "zephyr"
                            / "include"
                            / "drivers"
                        ),
                        "USER_CACHE_DIR": str(
                            self.module_paths["ec"]
                            / "build"
                            / "zephyr"
                            / "user-cache"
                        ),
                        "ZEPHYR_BASE": str(self.zephyr_base),
                        "ZMAKE_INCLUDE_DIR": str(generated_include_dir),
                        "ZMAKE_PROJECT_NAME": project.config.project_name,
                        **(
                            {"EXTRA_EC_VERSION_FLAGS": "--static"}
                            if static_version
                            else {}
                        ),
                        **(
                            {"EXTRA_CFLAGS": "-save-temps=obj"}
                            if save_temps
                            else {}
                        ),
                    },
                )

                # Prune the module paths to just those required by the project.
                module_paths = project.prune_modules(self.module_paths)

                module_config = zmake.modules.setup_module_symlinks(
                    build_dir / "modules", module_paths
                )

                # Symlink the Zephyr base into the build directory so it can
                # be used in the build phase.
                util.update_symlink(self.zephyr_base, build_dir / "zephyr_base")

                dts_overlay_config = project.find_dts_overlays(module_paths)

                toolchain_support = project.get_toolchain(
                    module_paths, override=toolchain
                )
                toolchain_config = toolchain_support.get_build_config()

                if bringup:
                    base_config |= zmake.build_config.BuildConfig(
                        kconfig_defs={"CONFIG_PLATFORM_EC_BRINGUP": "y"}
                    )
                if coverage:
                    base_config |= zmake.build_config.BuildConfig(
                        kconfig_defs={"CONFIG_COVERAGE": "y"}
                    )
                if allow_warnings:
                    base_config |= zmake.build_config.BuildConfig(
                        cmake_defs={"ALLOW_WARNINGS": "ON"}
                    )
                if extra_cflags:
                    base_config |= zmake.build_config.BuildConfig(
                        cmake_defs={"EXTRA_CFLAGS": extra_cflags},
                    )
                if self.goma:
                    base_config |= zmake.build_config.BuildConfig(
                        cmake_defs={
                            "CMAKE_C_COMPILER_LAUNCHER": self.gomacc,
                            "CMAKE_CXX_COMPILER_LAUNCHER": self.gomacc,
                        },
                    )

                if not build_dir.exists():
                    build_dir.mkdir()
                if not generated_include_dir.exists():
                    generated_include_dir.mkdir()
                self.logger.info(
                    "Building %s in %s.", project.config.project_name, build_dir
                )
                # To reconstruct a Project object later, we need to know the
                # name and project directory.
                (build_dir / "project_name.txt").write_text(
                    project.config.project_name
                )
                util.update_symlink(
                    project.config.project_dir, build_dir / "project"
                )

                wait_funcs = []
                for build_name, build_config in project.iter_builds():
                    config: zmake.build_config.BuildConfig = (
                        base_config
                        | toolchain_config
                        | module_config
                        | dts_overlay_config
                        | build_config
                    )

                    wait_func = self.executor.append(
                        func=functools.partial(
                            self._configure_one_build,
                            config=config,
                            build_dir=build_dir,
                            build_name=build_name,
                            project=project,
                        )
                    )
                    wait_funcs.append(wait_func)
            # Outside the with...get_job above.
            for wait_func in wait_funcs:
                wait_func()

            if build_after_configure:
                self._build(
                    build_dir=build_dir,
                    project=project,
                    coverage=coverage,
                    static_version=static_version,
                    delete_intermediates=delete_intermediates,
                )
            return 0
        except Exception:
            self.failed_projects.append(project.config.project_name)
            raise

    def _configure_one_build(
        self,
        config,
        build_dir,
        build_name,
        project,
    ):
        """Run cmake and maybe ninja on one build dir."""
        with self.jobserver.get_job():
            config_json = config.as_json()
            config_json_file = build_dir / f"cfg-{build_name}.json"
            if config_json_file.is_file():
                if config_json_file.read_text() == config_json:
                    self.logger.info(
                        "Skip reconfiguring %s:%s due to previous cmake run of "
                        "equivalent configuration.  Run with --clobber if this "
                        "optimization is undesired.",
                        project.config.project_name,
                        build_name,
                    )
                    return 0
                config_json_file.unlink()

            output_dir = build_dir / "build-{}".format(build_name)
            if output_dir.exists():
                self.logger.info(
                    "Clobber %s due to configuration changes.",
                    output_dir,
                )
                shutil.rmtree(output_dir)

            self.logger.info(
                "Configuring %s:%s.",
                project.config.project_name,
                build_name,
            )

            kconfig_file = build_dir / "kconfig-{}.conf".format(build_name)
            proc = config.popen_cmake(
                self.jobserver,
                project.config.project_dir,
                output_dir,
                kconfig_file,
                stdin=subprocess.DEVNULL,
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE,
                encoding="utf-8",
                errors="replace",
            )
            job_id = "{}:{}".format(project.config.project_name, build_name)
            zmake.multiproc.LogWriter.log_output(
                self.logger,
                logging.DEBUG,
                proc.stdout,
                log_level_override_func=cmake_log_level_override,
                job_id=job_id,
            )
            zmake.multiproc.LogWriter.log_output(
                self.logger,
                logging.ERROR,
                proc.stderr,
                log_level_override_func=cmake_log_level_override,
                job_id=job_id,
            )
            if proc.wait():
                raise OSError(get_process_failure_msg(proc))
            config_json_file.write_text(config_json)
            return 0

    def _build(
        self,
        build_dir,
        project: zmake.project.Project,
        coverage=False,
        static_version=False,
        delete_intermediates=False,
    ):
        """Build a pre-configured build directory."""

        with self.jobserver.get_job():
            dirs: Dict[str, pathlib.Path] = {}

            build_dir = build_dir.resolve()

            # Compute the version string.
            version_string = zmake.version.get_version_string(
                project.config.project_name,
                build_dir / "zephyr_base",
                zmake.modules.locate_from_directory(build_dir / "modules"),
                static=static_version,
            )

            # The version header needs to generated during the build phase
            # instead of configure, as the tree may have changed since
            # configure was run.
            zmake.version.write_version_header(
                version_string,
                build_dir / "include" / "ec_version.h",
                "zmake",
                static=static_version,
            )

            gcov = "gcov.sh-not-found"
            wait_funcs = []
            for build_name, _ in project.iter_builds():
                dirs[build_name] = build_dir / "build-{}".format(build_name)
                gcov = dirs[build_name] / "gcov.sh"
                wait_func = self.executor.append(
                    func=functools.partial(
                        self._build_one_dir,
                        build_name=build_name,
                        dirs=dirs,
                        coverage=coverage,
                        project=project,
                    )
                )
                wait_funcs.append(wait_func)
        # Outside the with...get_job above.
        for wait_func in wait_funcs:
            wait_func()

        with self.jobserver.get_job():
            # Run the packer.
            packer_work_dir = build_dir / "packer"
            output_dir = build_dir / "output"
            for newdir in output_dir, packer_work_dir:
                if not newdir.exists():
                    newdir.mkdir()

            # For non-tests, they won't link with coverage, so don't pack the
            # firmware. Also generate a lcov file.
            if coverage and not project.config.is_test:
                self._run_lcov(
                    build_dir,
                    output_dir / "zephyr.info",
                    initial=True,
                    gcov=gcov,
                )
            else:
                for output_file, output_name in project.packer.pack_firmware(
                    packer_work_dir,
                    self.jobserver,
                    dirs,
                    version_string=version_string,
                ):
                    shutil.copy2(output_file, output_dir / output_name)
                    self.logger.debug("Output file '%s' created.", output_file)

            if delete_intermediates:
                outdir = build_dir / "output"
                for child in build_dir.iterdir():
                    if child != outdir:
                        logging.debug("Deleting %s", child)
                        if not child.is_symlink() and child.is_dir():
                            shutil.rmtree(child)
                        else:
                            child.unlink()
            return 0

    def _build_one_dir(self, build_name, dirs, coverage, project):
        """Builds one sub-dir of a configured project (build-ro, etc)."""

        with self.jobserver.get_job():
            cmd = ["/usr/bin/ninja", "-C", dirs[build_name].as_posix()]
            if self.goma:
                # Go nuts ninja, goma does the heavy lifting!
                cmd.append("-j1024")
            elif self._sequential:
                cmd.append("-j1")
            # Only tests will actually build with coverage enabled.
            if coverage and not project.config.is_test:
                cmd.append("all.libraries")
            self.logger.info(
                "Building %s:%s: %s",
                project.config.project_name,
                build_name,
                util.repr_command(cmd),
            )
            proc = self.jobserver.popen(
                cmd,
                stdout=subprocess.PIPE,
                stderr=subprocess.PIPE,
                encoding="utf-8",
                errors="replace",
                # TODO(b/239619222): Filter os.environ for ninja.
                env=os.environ,
            )
            job_id = "{}:{}".format(project.config.project_name, build_name)
            dirs[build_name].mkdir(parents=True, exist_ok=True)
            build_log = open(  # pylint:disable=consider-using-with
                dirs[build_name] / "build.log",
                "w",
            )
            out = zmake.multiproc.LogWriter.log_output(
                logger=self.logger,
                log_level=logging.INFO,
                file_descriptor=proc.stdout,
                log_level_override_func=ninja_stdout_log_level_override,
                job_id=job_id,
                tee_output=build_log,
            )
            err = zmake.multiproc.LogWriter.log_output(
                self.logger,
                logging.ERROR,
                proc.stderr,
                job_id=job_id,
            )

            if proc.wait():
                raise OSError(get_process_failure_msg(proc))

            # Let all output be produced before exiting
            out.wait()
            err.wait()
            return 0

    def _run_lcov(
        self,
        build_dir,
        lcov_file,
        initial=False,
        gcov: Union[os.PathLike, str] = "",
    ):
        gcov = os.path.abspath(gcov)
        if initial:
            self.logger.info("Running (initial) lcov on %s.", build_dir)
        else:
            self.logger.info("Running lcov on %s.", build_dir)
        cmd = [
            "/usr/bin/lcov",
            "--gcov-tool",
            gcov,
            "-q",
            "-o",
            "-",
            "-c",
            "-d",
            build_dir,
            "-t",
            build_dir.stem,
            "--rc",
            "lcov_branch_coverage=1",
        ]
        if initial:
            cmd += ["-i"]
        proc = self.jobserver.popen(
            cmd,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            encoding="utf-8",
            errors="replace",
        )
        zmake.multiproc.LogWriter.log_output(
            self.logger,
            logging.WARNING,
            proc.stderr,
            job_id="{}-lcov".format(build_dir),
        )

        with open(lcov_file, "w") as outfile:
            for line in proc.stdout:
                if line.startswith("SF:"):
                    path = line[3:].rstrip()
                    outfile.write("SF:%s\n" % os.path.realpath(path))
                else:
                    outfile.write(line)
        if proc.wait():
            raise OSError(get_process_failure_msg(proc))

        return 0

    def _merge_lcov_files(self, projects, build_dir, output_file):
        all_lcov_files = []
        for project in projects:
            project_build_dir = (
                pathlib.Path(build_dir) / project.config.project_name
            )
            all_lcov_files.append(project_build_dir / "output" / "zephyr.info")
        # Merge info files into a single lcov.info
        self.logger.info("Merging coverage data into %s.", output_file)
        cmd = [
            "/usr/bin/lcov",
            "-o",
            output_file,
            "--rc",
            "lcov_branch_coverage=1",
        ]
        for info in all_lcov_files:
            cmd += ["-a", info]
        proc = self.jobserver.popen(
            cmd,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE,
            encoding="utf-8",
            errors="replace",
        )
        zmake.multiproc.LogWriter.log_output(
            self.logger, logging.ERROR, proc.stderr, job_id="lcov"
        )
        zmake.multiproc.LogWriter.log_output(
            self.logger, logging.DEBUG, proc.stdout, job_id="lcov"
        )
        if proc.wait():
            raise OSError(get_process_failure_msg(proc))
        return 0

    def list_projects(self, fmt, search_dir):
        """List project names known to zmake on stdout.

        Args:
            fmt: The formatting string to print projects with.
            search_dir: Directory to start the search for
                BUILD.py files at.
        """
        if not search_dir:
            search_dir = self.module_paths["ec"] / "zephyr"

        for project in zmake.project.find_projects(search_dir).values():
            print(fmt.format(config=project.config), end="")

        return 0

    def generate_readme(self, output_file, diff=False):
        """Re-generate the auto-generated README file.

        Args:
            output_file: A pathlib.Path; to be written only if changed.
            diff: Instead of writing out, report the diff.
        """
        expected_contents = zmake.generate_readme.generate_readme()

        if output_file.is_file():
            current_contents = output_file.read_text()
            if expected_contents == current_contents:
                return 0
            if diff:
                self.logger.error(
                    "The auto-generated README.md differs from the expected contents:"
                )
                for line in difflib.unified_diff(
                    current_contents.splitlines(keepends=True),
                    expected_contents.splitlines(keepends=True),
                    str(output_file),
                ):
                    self.logger.error(line.rstrip())
                self.logger.error('Run "zmake generate-readme" to fix this.')
                return 1

        if diff:
            self.logger.error(
                'The README.md file does not exist.  Run "zmake generate-readme".'
            )
            return 1

        output_file.write_text(expected_contents)
        return 0