]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/Python/Eot/InfParserLite.py
Sync BaseTool trunk (version r2599) into EDKII BaseTools.
[mirror_edk2.git] / BaseTools / Source / Python / Eot / InfParserLite.py
CommitLineData
52302d4d
LG
1## @file\r
2# This file is used to parse INF file of EDK project\r
3#\r
40d841f6
LG
4# Copyright (c) 2008 - 2010, Intel Corporation. All rights reserved.<BR>\r
5# This program and the accompanying materials\r
52302d4d
LG
6# are licensed and made available under the terms and conditions of the BSD License\r
7# which accompanies this distribution. The full text of the license may be found at\r
8# http://opensource.org/licenses/bsd-license.php\r
9#\r
10# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12#\r
13\r
14##\r
15# Import Modules\r
16#\r
17import os\r
18import Common.EdkLogger as EdkLogger\r
19from Common.DataType import *\r
20from CommonDataClass.DataClass import *\r
21from Common.Identification import *\r
22from Common.String import *\r
23from Parser import *\r
24import Database\r
25\r
26## EdkInfParser() class\r
27#\r
28# This class defined basic INF object which is used by inheriting\r
29#\r
30# @param object: Inherited from object class\r
31#\r
32class EdkInfParser(object):\r
33 ## The constructor\r
34 #\r
35 # @param self: The object pointer\r
36 # @param Filename: INF file name\r
37 # @param Database: Eot database\r
38 # @param SourceFileList: A list for all source file belonging this INF file\r
39 # @param SourceOverridePath: Override path for source file\r
40 # @param Edk_Source: Envirnoment variable EDK_SOURCE\r
41 # @param Efi_Source: Envirnoment variable EFI_SOURCE\r
42 #\r
43 def __init__(self, Filename = None, Database = None, SourceFileList = None, SourceOverridePath = None, Edk_Source = None, Efi_Source = None):\r
44 self.Identification = Identification()\r
45 self.Sources = []\r
46 self.Macros = {}\r
47\r
48 self.Cur = Database.Cur\r
49 self.TblFile = Database.TblFile\r
50 self.TblInf = Database.TblInf\r
51 self.FileID = -1\r
52 self.SourceOverridePath = SourceOverridePath\r
53\r
54 # Load Inf file if filename is not None\r
55 if Filename != None:\r
56 self.LoadInfFile(Filename)\r
57\r
58 if SourceFileList:\r
59 for Item in SourceFileList:\r
60 self.TblInf.Insert(MODEL_EFI_SOURCE_FILE, Item, '', '', '', '', 'COMMON', -1, self.FileID, -1, -1, -1, -1, 0)\r
61\r
62\r
63 ## LoadInffile() method\r
64 #\r
65 # Load INF file and insert a record in database\r
66 #\r
67 # @param self: The object pointer\r
68 # @param Filename: Input value for filename of Inf file\r
69 #\r
70 def LoadInfFile(self, Filename = None):\r
71 # Insert a record for file\r
72 Filename = NormPath(Filename)\r
73 self.Identification.FileFullPath = Filename\r
74 (self.Identification.FileRelativePath, self.Identification.FileName) = os.path.split(Filename)\r
75\r
76 self.FileID = self.TblFile.InsertFile(Filename, MODEL_FILE_INF)\r
77\r
78 self.ParseInf(PreProcess(Filename, False), self.Identification.FileRelativePath, Filename)\r
79\r
80 ## ParserSource() method\r
81 #\r
82 # Parse Source section and insert records in database\r
83 #\r
84 # @param self: The object pointer\r
85 # @param CurrentSection: current section name\r
86 # @param SectionItemList: the item belonging current section\r
87 # @param ArchList: A list for arch for this section\r
88 # @param ThirdList: A list for third item for this section\r
89 #\r
90 def ParserSource(self, CurrentSection, SectionItemList, ArchList, ThirdList):\r
91 for Index in range(0, len(ArchList)):\r
92 Arch = ArchList[Index]\r
93 Third = ThirdList[Index]\r
94 if Arch == '':\r
95 Arch = TAB_ARCH_COMMON\r
96\r
97 for Item in SectionItemList:\r
98 if CurrentSection.upper() == 'defines'.upper():\r
99 (Name, Value) = AddToSelfMacro(self.Macros, Item[0])\r
100 self.TblInf.Insert(MODEL_META_DATA_HEADER, Name, Value, Third, '', '', Arch, -1, self.FileID, Item[1], -1, Item[1], -1, 0)\r
101\r
102 ## ParseInf() method\r
103 #\r
104 # Parse INF file and get sections information\r
105 #\r
106 # @param self: The object pointer\r
107 # @param Lines: contents of INF file\r
108 # @param FileRelativePath: relative path of the file\r
109 # @param Filename: file name of INF file\r
110 #\r
111 def ParseInf(self, Lines = [], FileRelativePath = '', Filename = ''):\r
112 IfDefList, SectionItemList, CurrentSection, ArchList, ThirdList, IncludeFiles = \\r
113 [], [], TAB_UNKNOWN, [], [], []\r
114 LineNo = 0\r
115\r
116 for Line in Lines:\r
117 LineNo = LineNo + 1\r
118 if Line == '':\r
119 continue\r
120 if Line.startswith(TAB_SECTION_START) and Line.endswith(TAB_SECTION_END):\r
121 self.ParserSource(CurrentSection, SectionItemList, ArchList, ThirdList)\r
122\r
123 # Parse the new section\r
124 SectionItemList = []\r
125 ArchList = []\r
126 ThirdList = []\r
127 # Parse section name\r
128 CurrentSection = ''\r
129 LineList = GetSplitValueList(Line[len(TAB_SECTION_START):len(Line) - len(TAB_SECTION_END)], TAB_COMMA_SPLIT)\r
130 for Item in LineList:\r
131 ItemList = GetSplitValueList(Item, TAB_SPLIT)\r
132 if CurrentSection == '':\r
133 CurrentSection = ItemList[0]\r
134 else:\r
135 if CurrentSection != ItemList[0]:\r
136 EdkLogger.error("Parser", PARSER_ERROR, "Different section names '%s' and '%s' are found in one section definition, this is not allowed." % (CurrentSection, ItemList[0]), File=Filename, Line=LineNo)\r
137 ItemList.append('')\r
138 ItemList.append('')\r
139 if len(ItemList) > 5:\r
140 RaiseParserError(Line, CurrentSection, Filename, '', LineNo)\r
141 else:\r
142 ArchList.append(ItemList[1].upper())\r
143 ThirdList.append(ItemList[2])\r
144\r
145 continue\r
146\r
147 # Add a section item\r
148 SectionItemList.append([Line, LineNo])\r
149 # End of parse\r
150\r
151 self.ParserSource(CurrentSection, SectionItemList, ArchList, ThirdList)\r
152 #End of For\r
153\r
154##\r
155#\r
156# This acts like the main() function for the script, unless it is 'import'ed into another\r
157# script.\r
158#\r
159if __name__ == '__main__':\r
160 EdkLogger.Initialize()\r
161 EdkLogger.SetLevel(EdkLogger.QUIET)\r
162\r
163 Db = Database.Database('Inf.db')\r
164 Db.InitDatabase()\r
165 P = EdkInfParser(os.path.normpath("C:\Framework\Edk\Sample\Platform\Nt32\Dxe\PlatformBds\PlatformBds.inf"), Db, '', '')\r
166 for Inf in P.Sources:\r
167 print Inf\r
168 for Item in P.Macros:\r
169 print Item, P.Macros[Item]\r
170\r
171 Db.Close()