|
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 os |
|
24 import re |
|
25 |
|
26 class NoTargetException(Exception): |
|
27 pass |
|
28 |
|
29 def depcrunch(file,extensions,assume): |
|
30 target_pattern = r"^\s*(\S+):\s+" |
|
31 target_re = re.compile(target_pattern) |
|
32 extension_pattern = "[ \t]([^\/\\ \t]+\.(" + "|".join(["("+ t + ")" for t in extensions]) + "))\\b" |
|
33 extension_re = re.compile(extension_pattern) |
|
34 |
|
35 target = None |
|
36 |
|
37 deps = [] |
|
38 |
|
39 for l in file.xreadlines(): |
|
40 l = l.replace("\\","/").rstrip("\n\r") |
|
41 |
|
42 if not target: |
|
43 t = target_re.match(l) |
|
44 if t: |
|
45 target = t.groups()[0] |
|
46 |
|
47 m = extension_re.match(l) |
|
48 if m: |
|
49 deps.append(m.groups()[0]) |
|
50 |
|
51 if not target: |
|
52 raise NoTargetException() |
|
53 |
|
54 if len(deps) > 0: |
|
55 print "%s: \\" % target |
|
56 for d in deps[:-1]: |
|
57 print " %s \\" % (assume + "/" + d) |
|
58 print " %s " % (assume + "/" + deps[-1]) |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 ## Command Line Interface #################################################### |
|
64 |
|
65 parser = OptionParser(prog = "depcrunch", |
|
66 usage = "%prog [-h | options] [<depfile>]") |
|
67 |
|
68 parser.add_option("-e", "--extensions", |
|
69 action="store", dest="extensions", type='string', help="comma separated list of file extensions of missing files to keep in the crunched dep file.") |
|
70 |
|
71 parser.add_option("-a", "--assume", |
|
72 action="store", dest="assume", type='string', help="when cpp reports missing dependencies, assume that they are in this directory") |
|
73 |
|
74 (options, args) = parser.parse_args() |
|
75 |
|
76 |
|
77 if not options.extensions: |
|
78 parser.error("you must specify a comma-separated list of file extensions with the -t option.") |
|
79 sys.exit(1) |
|
80 |
|
81 if not options.assume: |
|
82 parser.error("you must specify an 'assumed directory' for correcting missing dependencies with the -a option.") |
|
83 sys.exit(1) |
|
84 |
|
85 depfilename="stdin" |
|
86 if len(args) > 0: |
|
87 depfilename=args[0] |
|
88 file = open(depfilename,"r") |
|
89 else: |
|
90 file = sys.stdin |
|
91 try: |
|
92 depcrunch(file,options.extensions.split(","), options.assume) |
|
93 except NoTargetException,e: |
|
94 sys.stderr.write("Target name not found in dependency file"); |
|
95 sys.exit(2) |
|
96 |
|
97 |
|
98 if file != sys.stdin: |
|
99 file.close() |
|
100 |
|
101 sys.exit(0) |