blob: e7669c57f5517abfb85b4a9f4fb246102b8494db [file] [log] [blame]
#!/usr/bin/env python
# Copyright (c) 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Fixup GCC-generated dependency files.
Modify GCC generated dependency files so they are more suitable for including
in a GNU Makefile. Without the fixups, deleting or renaming headers can cause
the build to be broken.
See for more details of the problem.
import argparse
import os
import sys
TAG_LINE = '# Updated by\n'
class Error(Exception):
def ParseLine(line, new_target):
"""Parse one line of a GCC-generated deps file.
Each line contains an optional target and then a list
of space separated dependencies. Spaces within filenames
are escaped with a backslash.
filenames = []
if new_target and ':' in line:
line = line.split(':', 1)[1]
line = line.strip()
line = line.rstrip('\\')
while True:
# Find the next non-escaped space
line = line.strip()
pos = line.find(' ')
while pos > 0 and line[pos-1] == '\\':
pos = line.find(' ', pos+1)
if pos == -1:
line = line[pos+1:]
return filenames
def FixupDepFile(filename, output_filename=None):
if not os.path.exists(filename):
raise Error('File not found: %s' % filename)
if output_filename is None:
output_filename = filename
outlines = [TAG_LINE]
deps = []
new_target = True
with open(filename) as infile:
for line in infile:
if line == TAG_LINE:
raise Error('Already processed: %s' % filename)
deps += ParseLine(line, new_target)
new_target = line.endswith('\\')
# For every depenency found output a dummy target with no rules
for dep in deps:
outlines.append('%s:\n' % dep)
with open(output_filename, 'w') as outfile:
for line in outlines:
def main(argv):
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('-o', '--output', help='Output filename (defaults to '
'input name with .deps extension')
parser.add_argument('-c', '--clean', action='store_true',
help='Remove input file after writing output')
options = parser.parse_args(argv)
output_filename = options.output
if not output_filename:
output_filename = os.path.splitext(options.dep_file)[0] + '.deps'
FixupDepFile(options.dep_file, output_filename)
if options.clean and options.dep_file != output_filename:
return 0
if __name__ == '__main__':
except Error as e:
sys.stderr.write('%s: %s\n' % (os.path.basename(__file__), e))