author Mike Hommey <>
Tue, 20 Aug 2013 09:14:45 +0900
changeset 143172 57f4731f0a866c3cf434c5b4d021f554d7586fd1
parent 128232 260999a5d63b383d04c070410417828db69196d6
child 143981 927e48de12e14f6637b980fd480636504f17eeab
permissions -rw-r--r--
Bug 906240 - Adapt dependency output to pymake deficiencies ; also add source file to the list of dependencies. r=gps

# 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

import os, os.path
import subprocess
import sys

CL_INCLUDES_PREFIX = os.environ.get("CL_INCLUDES_PREFIX", "Note: including file:")

def InvokeClWithDependencyGeneration(cmdline):
    target = ""
    # Figure out what the target is
    for arg in cmdline:
        if arg.startswith("-Fo"):
            target = arg[3:]

    if target == None:
        print >>sys.stderr, "No target set" and sys.exit(1)

    # Assume the source file is the last argument
    source = cmdline[-1]
    assert not source.startswith('-')

    # The deps target lives here
    depstarget = os.path.basename(target) + ".pp"

    cmdline += ['-showIncludes']
    cl = subprocess.Popen(cmdline, stdout=subprocess.PIPE)

    deps = set([os.path.normcase(source).replace(os.sep, '/')])
    for line in cl.stdout:
        # cl -showIncludes prefixes every header with "Note: including file:"
        # and an indentation corresponding to the depth (which we don't need)
        if line.startswith(CL_INCLUDES_PREFIX):
            dep = line[len(CL_INCLUDES_PREFIX):].strip()
            # We can't handle pathes with spaces properly in, but
            # we can assume that anything in a path with spaces is a system
            # header and throw it away.
            if ' ' not in dep:
                deps.add(os.path.normcase(dep).replace(os.sep, '/'))
            sys.stdout.write(line) # Make sure we preserve the relevant output
                                   # from cl

    ret = cl.wait()
    if ret != 0 or target == "":

    depsdir = os.path.normpath(os.path.join(os.curdir, ".deps"))
    depstarget = os.path.join(depsdir, depstarget)
    if not os.path.isdir(depsdir):
        except OSError:
            pass # This suppresses the error we get when the dir exists, at the
                 # cost of masking failure to create the directory.  We'll just
                 # die on the next line though, so it's not that much of a loss.

    with open(depstarget, "w") as f:
        f.write("%s: %s" % (target, source))
        for dep in sorted(deps):
            f.write(" \\\n%s" % dep)
        for dep in sorted(deps):
            f.write("%s:\n" % dep)

if __name__ == "__main__":