summaryrefslogtreecommitdiff
path: root/python/mozbuild/dumbmake/dumbmake.py
blob: 5457c8b0a0bc19fa9e257d9560772ef367dc7ea1 (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
# 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 absolute_import, unicode_literals

from collections import OrderedDict
from itertools import groupby
from operator import itemgetter
from os.path import dirname

WHITESPACE_CHARACTERS = ' \t'

def indentation(line):
    """Number of whitespace (tab and space) characters at start of |line|."""
    i = 0
    while i < len(line):
        if line[i] not in WHITESPACE_CHARACTERS:
            break
        i += 1
    return i

def dependency_map(lines):
    """Return a dictionary with keys that are targets and values that
    are ordered lists of targets that should also be built.

    This implementation is O(n^2), but lovely and simple!  We walk the
    targets in the list, and for each target we walk backwards
    collecting its dependencies.  To make the walking easier, we
    reverse the list so that we are always walking forwards.

    """
    pairs = [(indentation(line), line.strip()) for line in lines]
    pairs.reverse()

    deps = {}

    for i, (indent, target) in enumerate(pairs):
        if not deps.has_key(target):
            deps[target] = []

        for j in range(i+1, len(pairs)):
            ind, tar = pairs[j]
            if ind < indent:
                indent = ind
                if tar not in deps[target]:
                    deps[target].append(tar)

    return deps

def all_dependencies(*targets, **kwargs):
    """Return a list containing all the dependencies of |targets|.

    The relative order of targets is maintained if possible.

    """
    dm = kwargs.pop('dependency_map', None)
    if dm is None:
        dm = dependency_map(targets)

    all_targets = OrderedDict() # Used as an ordered set.

    for target in targets:
        if target in dm:
            for dependency in dm[target]:
                # Move element back in the ordered set.
                if dependency in all_targets:
                    del all_targets[dependency]
                all_targets[dependency] = True

    return all_targets.keys()

def get_components(path):
    """Take a path and return all the components of the path."""
    paths = [path]
    while True:
        parent = dirname(paths[-1])
        if parent == "":
            break
        paths.append(parent)

    paths.reverse()
    return paths

def add_extra_dependencies(target_pairs, dependency_map):
    """Take a list [(make_dir, make_target)] and expand (make_dir, None)
    entries with extra make dependencies from |dependency_map|.

    Returns an iterator of pairs (make_dir, make_target).

    """
    all_targets = OrderedDict() # Used as an ordered set.
    make_dirs = OrderedDict() # Used as an ordered set.

    for make_target, group in groupby(target_pairs, itemgetter(1)):
        # Return non-simple directory targets untouched.
        if make_target is not None:
            for pair in group:
                # Generate dependencies for all components of a path.
                # Given path a/b/c, examine a, a/b, and a/b/c in that order.
                paths = get_components(pair[1])
                # For each component of a path, find and add all dependencies
                # to the final target list.
                for target in paths:
                    if target not in all_targets:
                        yield pair[0], target
                        all_targets[target] = True
            continue

        # Add extra dumbmake dependencies to simple directory targets.
        for make_dir, _ in group:
            if make_dir not in make_dirs:
                yield make_dir, None
                make_dirs[make_dir] = True

    all_components = []
    for make_dir in make_dirs.iterkeys():
        all_components.extend(get_components(make_dir))

    for i in all_dependencies(*all_components, dependency_map=dependency_map):
        if i not in make_dirs:
            yield i, None