python/mozbuild/mach/commands/build.py
author Gregory Szorc <gps@mozilla.com>
Tue, 06 Nov 2012 16:57:41 -0800
changeset 117041 e0e1220d2c5a1086a4911be8d69e2937b842d116
parent 112939 d04782dc009156e114f27da3a468321a99edc47b
permissions -rw-r--r--
Bug 808336 - Part 1: Refactor mach command handler management; r=jhammel Previously we were tighly coupled with MozbuildObject. This was not in the spirit of mach being a generic tool. Now, instead of passing multiple arguments to __init__ of the class providing the mach command we either pass 0 or 1. The number of arguments is detected when processing the @CommandProvider decorator. The optional argument is a named tuple containing mach run-time state. Capturing of mach command provider information is now captured in a class (as opposed to an anoymous tuple). We also capture these in a rich data structure which is passed as part of the run-time data to the command provider class. This allows mach commands to inspect the mach environment. Mach decorators have been moved to mach.decorators. mach.base is reserved for generic mach data/container classes. Existing mach command classes derived from MozbuildObject have been replaced with either object or mozbuild.base.MachCommandBase. This latter class translates the mach context instance passed to __init__ into the constructor arguments for MozbuildObject.__init__. Support for registering function handlers with mach has been removed. All handlers must be inside classes.

# 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/.

from __future__ import unicode_literals

import logging
import os

from mach.decorators import (
    CommandProvider,
    Command,
)

from mozbuild.base import MachCommandBase


@CommandProvider
class Build(MachCommandBase):
    """Interface to build the tree."""

    @Command('build', help='Build the tree.')
    def build(self):
        # This code is only meant to be temporary until the more robust tree
        # building code in bug 780329 lands.
        from mozbuild.compilation.warnings import WarningsCollector
        from mozbuild.compilation.warnings import WarningsDatabase

        warnings_path = self._get_state_filename('warnings.json')
        warnings_database = WarningsDatabase()

        if os.path.exists(warnings_path):
            warnings_database.load_from_file(warnings_path)

        warnings_collector = WarningsCollector(database=warnings_database,
            objdir=self.topobjdir)

        def on_line(line):
            try:
                warning = warnings_collector.process_line(line)
                if warning:
                    self.log(logging.INFO, 'compiler_warning', warning,
                        'Warning: {flag} in {filename}: {message}')
            except:
                # This will get logged in the more robust implementation.
                pass

            self.log(logging.INFO, 'build_output', {'line': line}, '{line}')

        self._run_make(srcdir=True, filename='client.mk', line_handler=on_line,
            log=False, print_directory=False)

        self.log(logging.WARNING, 'warning_summary',
            {'count': len(warnings_collector.database)},
            '{count} compiler warnings present.')

        warnings_database.save_to_file(warnings_path)