sbsv2/raptor/bin/depcrunch.py
changeset 18 de5b887c98f7
equal deleted inserted replaced
14:eb060913c963 18:de5b887c98f7
       
     1 #
       
     2 # Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 # All rights reserved.
       
     4 # This component and the accompanying materials are made available
       
     5 # under the terms of the License "Eclipse Public License v1.0"
       
     6 # which accompanies this distribution, and is available
       
     7 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 #
       
     9 # Initial Contributors:
       
    10 # Nokia Corporation - initial contribution.
       
    11 #
       
    12 # Contributors:
       
    13 #
       
    14 # Description: 
       
    15 # Minimise the dependencies in a C preprocessor dependency file to
       
    16 # those that CPP could not find.  Then add in an assumption about 
       
    17 # where to find them.  Output is assumed to be relevant to only one target
       
    18 # even if multiple dep files are analysed.
       
    19 #
       
    20 
       
    21 import sys
       
    22 from  optparse import OptionParser
       
    23 import re
       
    24 
       
    25 class NoTargetException(Exception):
       
    26 	pass
       
    27 
       
    28 def depcrunch(file,extensions,assume):
       
    29 	target_pattern = r"^\s*(\S+):\s+"
       
    30 	target_re = re.compile(target_pattern)
       
    31 	# Not the use of (?i) in the following expression.  re.I seems to cause re.findall
       
    32 	# to not actually find all files matching the extension whereas (?i) provides
       
    33 	# case insensitivity at the right point and it works.  Really don't understand this.
       
    34 	extension_pattern = r"\s([^/ \t]+\.((?i)" + "|".join([t for t in extensions]) + r"))\b"
       
    35 	extension_re = re.compile(extension_pattern)
       
    36 
       
    37 	target = None
       
    38 
       
    39 	deps = []
       
    40 
       
    41 	# Read through the dependencies.
       
    42 	for l in file:
       
    43 		l = l.replace("\\","/").rstrip("\n\r")
       
    44 
       
    45 		# Look out for the target name if 
       
    46 		# we have not found it yet
       
    47 		if not target:
       
    48 			t = target_re.match(l)
       
    49 			if t:
       
    50 				target = t.groups()[0]
       
    51 
       
    52 		# Look for prerequisites matching the 
       
    53 		# extensions.  There may be one or more on 
       
    54 		# the same line as the target name.
       
    55 		# Don't use re.I - somehow prevents 
       
    56 		# all but one match in a line which may have several files
       
    57 		m = extension_re.findall(l)
       
    58 		if m:
       
    59 			deps.extend([d[0] for d in m])
       
    60 
       
    61 	if not target:
       
    62 		raise NoTargetException()
       
    63 
       
    64 	if len(deps) > 0:
       
    65 		print "%s: \\" % target
       
    66 		for d in deps[:-1]:
       
    67 			print " %s \\" % (assume + "/" + d)
       
    68 		print " %s " % (assume + "/" + deps[-1])
       
    69 
       
    70 
       
    71 
       
    72 
       
    73 ## Command Line Interface ####################################################
       
    74 
       
    75 parser = OptionParser(prog = "depcrunch",
       
    76 	usage = "%prog [-h | options] [<depfile>]")
       
    77 
       
    78 parser.add_option("-e", "--extensions", 
       
    79 	 action="store", dest="extensions", type='string', help="comma separated list of file extensions of missing files to keep in the crunched dep file.") 
       
    80 
       
    81 parser.add_option("-a", "--assume", 
       
    82 	 action="store", dest="assume", type='string', help="when cpp reports missing dependencies, assume that they are in this directory") 
       
    83 
       
    84 (options, args) = parser.parse_args()
       
    85 
       
    86 
       
    87 if not options.extensions:
       
    88 	parser.error("you must specify a comma-separated list of file extensions with the -e option.")
       
    89 	sys.exit(1)
       
    90 
       
    91 if not options.assume:
       
    92 	parser.error("you must specify an 'assumed directory' for correcting missing dependencies with the -a option.")
       
    93 	sys.exit(1)
       
    94 
       
    95 depfilename="stdin"
       
    96 if len(args) > 0:
       
    97 	depfilename=args[0]
       
    98 	file = open(depfilename,"r")
       
    99 else:
       
   100 	file = sys.stdin
       
   101 try:
       
   102 	depcrunch(file,options.extensions.split(","), options.assume)
       
   103 except NoTargetException,e:
       
   104 	sys.stderr.write("Target name not found in dependency file\n");
       
   105 	sys.exit(2)
       
   106 	
       
   107 
       
   108 if file != sys.stdin:
       
   109 	file.close()
       
   110 
       
   111 sys.exit(0)