+## Regular expression for finding header file inclusions\r
+from AutoGen.GenMake import gIncludePattern\r
+\r
+## Find dependencies for one source file\r
+#\r
+# By searching recursively "#include" directive in file, find out all the\r
+# files needed by given source file. The dependecies will be only searched\r
+# in given search path list.\r
+#\r
+# @param SearchPathList The list of search path\r
+#\r
+# @retval list The list of files the given source file depends on\r
+#\r
+def GetDependencyList(FileStack,SearchPathList):\r
+ DepDb = dict()\r
+ DependencySet = set(FileStack)\r
+ while len(FileStack) > 0:\r
+ F = FileStack.pop()\r
+ FullPathDependList = []\r
+ CurrentFileDependencyList = []\r
+ if F in DepDb:\r
+ CurrentFileDependencyList = DepDb[F]\r
+ else:\r
+ try:\r
+ Fd = open(F, 'r')\r
+ FileContent = Fd.read()\r
+ except BaseException, X:\r
+ EdkLogger.error("build", FILE_OPEN_FAILURE, ExtraData=F + "\n\t" + str(X))\r
+ finally:\r
+ if "Fd" in dir(locals()):\r
+ Fd.close()\r
+\r
+ if len(FileContent) == 0:\r
+ continue\r
+\r
+ if FileContent[0] == 0xff or FileContent[0] == 0xfe:\r
+ FileContent = unicode(FileContent, "utf-16")\r
+ IncludedFileList = gIncludePattern.findall(FileContent)\r
+\r
+ for Inc in IncludedFileList:\r
+ Inc = Inc.strip()\r
+ Inc = os.path.normpath(Inc)\r
+ CurrentFileDependencyList.append(Inc)\r
+ DepDb[F] = CurrentFileDependencyList\r
+\r
+ CurrentFilePath = os.path.dirname(F)\r
+ PathList = [CurrentFilePath] + SearchPathList\r
+ for Inc in CurrentFileDependencyList:\r
+ for SearchPath in PathList:\r
+ FilePath = os.path.join(SearchPath, Inc)\r
+ if not os.path.exists(FilePath):\r
+ continue\r
+ if FilePath not in DependencySet:\r
+ FileStack.append(FilePath)\r
+ FullPathDependList.append(FilePath)\r
+ break\r
+ DependencySet.update(FullPathDependList)\r
+ DependencyList = list(DependencySet) # remove duplicate ones\r
+\r
+ return DependencyList\r
+\r