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