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