]>
git.proxmox.com Git - mirror_edk2.git/blob - Tools/Python/Calc-Deps.py
3 """Calculate the dependencies a given module has by looking through the source
4 code to see what guids and functions are referenced to see which Packages and
5 Library Classes need to be referenced. """
7 import os
, sys
, re
, getopt
, string
, glob
, xml
.dom
.minidom
, pprint
8 from XmlRoutines
import *
10 # Map each function name back to the lib class that declares it.
13 # Map each guid name to a package name.
16 def inWorkspace(rel_path
):
17 """Treat the given path as relative to the workspace."""
19 # Make sure the user has set the workspace variable:
21 return os
.path
.join(os
.environ
["WORKSPACE"], rel_path
)
23 print "Oops! You must set the WORKSPACE environment variable to run this script."
26 def getIdentifiers(infiles
):
28 """Build a set of all the identifiers in this file."""
30 # Start with an empty set.
33 for infile
in infiles
:
38 # Create some lexical categories that we will use to filter out
39 strings
=re
.compile('L?"[^"]*"')
40 chars
=re
.compile("'[^']*'")
41 hex=re
.compile("0[Xx][0-9a-fA-F]*")
42 keywords
= re
.compile('for|do|while|if|else|break|int|unsigned|switch|volatile|goto|case|char|long|struct|return|extern')
43 common
= re
.compile('VOID|UINTN|UINT32|UINT8|UINT64')
45 # Compile a Regular expression to grab all the identifers from the input.
46 identifier
= re
.compile('[_a-zA-Z][0-9_a-zA-Z]{3,}')
48 for line
in f
.readlines():
50 # Filter some lexical categories out.
51 # for filter in [strings, chars, hex, keywords, common]:
52 for filter in [strings
, chars
, hex]:
53 line
= re
.sub(filter, '', line
)
55 # Add all the identifiers that we found on this line.
56 ids
= ids
.union(set(identifier
.findall(line
)))
61 # Return the set of identifiers.
65 def search_classes(ids
):
67 """ Search the set of classes for functions."""
69 # Start with an empty set.
74 # If it is not a "hit" in the table add it to the set.
75 classes
.add(function_table
[id])
77 # If it is not a "hit" in the table, ignore it.
82 def search_cnames(ids
):
84 """Search all the Packages to see if this code uses a Guid from one of them.
85 Return a set of matching packages."""
91 # If it is not a "hit" in the table add it to the set.
92 packages
.add(cname_table
[id])
94 # If it is not a "hit" in the table, ignore it.
101 """Open the database and get all the spd files out."""
104 database
= xml
.dom
.minidom
.parse(inWorkspace("Tools/Conf/FrameworkDatabase.db"))
106 # Get a list of all the packages
107 for filename
in XmlList(database
, "/FrameworkDatabase/PackageList/Filename"):
108 spdFile
= XmlElementData(filename
)
110 # Now open the spd file and build the database of guids.
111 getCNames(inWorkspace(spdFile
))
112 getLibClasses(inWorkspace(spdFile
))
114 def getCNames(spdFile
):
116 """Extract all the C_Names from an spd file."""
118 # Begin to parse the XML of the .spd
119 spd
= xml
.dom
.minidom
.parse(spdFile
)
121 # Get the name of the package
122 packageName
= XmlElement(spd
, "PackageSurfaceArea/SpdHeader/PackageName")
125 for cname
in XmlList(spd
, "/PackageSurfaceArea/GuidDeclarations/Entry/C_Name") + \
126 XmlList(spd
, "/PackageSurfaceArea/PcdDeclarations/PcdEntry/C_Name") + \
127 XmlList(spd
, "/PackageSurfaceArea/PpiDeclarations/Entry/C_Name") + \
128 XmlList(spd
, "/PackageSurfaceArea/ProtocolDeclarations/Entry/C_Name"):
130 # Get the text of the <C_Name> tag.
131 cname_text
= XmlElementData(cname
)
133 # Map the <C_Name> to the <PackageName>. We will use this to lookup every
134 # identifier in the Input Code.
135 cname_table
[cname_text
] = packageName
139 def getLibClasses(spdFile
):
141 """Extract all the Lib Classes from an spd file."""
143 # Begin to parse the XML of the .spd
144 spd
= xml
.dom
.minidom
.parse(spdFile
)
146 # Get the guid of the package
147 packageGuid
= XmlElement(spd
, "/PackageSurfaceArea/SpdHeader/GuidValue")
149 for libClass
in XmlList(spd
, "/PackageSurfaceArea/LibraryClassDeclarations/LibraryClass"):
150 className
= XmlAttribute(libClass
, "Name")
151 headerfile
= XmlElementData(libClass
.getElementsByTagName("IncludeHeader")[0])
153 packageRoot
=os
.path
.dirname(spdFile
)
155 headerfile
= os
.path
.join(packageRoot
, headerfile
)
159 # This pattern can pick out function names if the EFI coding
160 # standard is followed. We could also use dumpbin on library
161 # instances to get a list of symbols.
162 functionPattern
= re
.compile("([_a-zA-Z][_a-zA-Z0-9]*) *\( *");
164 for line
in f
.readlines():
165 m
= functionPattern
.match(line
)
167 functionName
= m
.group(1)
169 function_table
[functionName
] = (className
, packageGuid
)
174 """Make a guid number out of a guid hex string."""
175 return long(strVal
.replace('-',''), 16)
177 # This acts like the main() function for the script, unless it is 'import'ed into another
179 if __name__
== '__main__':
181 # Create a pretty printer for dumping data structures in a readable form.
182 pp
= pprint
.PrettyPrinter(indent
=2)
184 # Process the command line args.
185 optlist
, args
= getopt
.getopt(sys
.argv
[1:], 'h', [ 'example-long-arg=', 'testing'])
187 """You should pass a file name as a paramter. It should be preprocessed text
188 of all the .c and .h files in your module, which is cat'ed together into one
191 # Scrape out all the things that look like identifiers.
192 ids
= getIdentifiers(args
)
194 # Read in the spds from the workspace to find the Guids.
198 print pp
.pprint(function_table
)
199 print pp
.pprint(cname_table
)
200 print "Classes = ", pp
.pprint(list(search_classes(ids
)))
201 print "C_Names = ", pp
.pprint(list(search_cnames(ids
)))