]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/Python/Eot/InfParserLite.py
BaseTools/UPT:merge UPT Tool use Python2 and Python3
[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
47f15da1 4# Copyright (c) 2008 - 2018, Intel Corporation. All rights reserved.<BR>\r
40d841f6 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
72443dd2 17from __future__ import print_function\r
64429fbd 18from __future__ import absolute_import\r
44957929 19\r
1be2ed90 20import Common.LongFilePathOs as os\r
52302d4d
LG
21import Common.EdkLogger as EdkLogger\r
22from Common.DataType import *\r
23from CommonDataClass.DataClass import *\r
44957929 24from Eot.Identification import Identification\r
5a57246e 25from Common.StringUtils import *\r
47f15da1
HC
26from Eot.Parser import *\r
27from Eot import Database\r
28from Eot import EotGlobalData\r
52302d4d
LG
29\r
30## EdkInfParser() class\r
31#\r
32# This class defined basic INF object which is used by inheriting\r
33#\r
34# @param object: Inherited from object class\r
35#\r
36class EdkInfParser(object):\r
37 ## The constructor\r
38 #\r
39 # @param self: The object pointer\r
40 # @param Filename: INF file name\r
41 # @param Database: Eot database\r
42 # @param SourceFileList: A list for all source file belonging this INF file\r
43 # @param SourceOverridePath: Override path for source file\r
44 # @param Edk_Source: Envirnoment variable EDK_SOURCE\r
45 # @param Efi_Source: Envirnoment variable EFI_SOURCE\r
46 #\r
47 def __init__(self, Filename = None, Database = None, SourceFileList = None, SourceOverridePath = None, Edk_Source = None, Efi_Source = None):\r
48 self.Identification = Identification()\r
49 self.Sources = []\r
50 self.Macros = {}\r
51\r
52 self.Cur = Database.Cur\r
53 self.TblFile = Database.TblFile\r
54 self.TblInf = Database.TblInf\r
55 self.FileID = -1\r
56 self.SourceOverridePath = SourceOverridePath\r
57\r
58 # Load Inf file if filename is not None\r
4231a819 59 if Filename is not None:\r
52302d4d
LG
60 self.LoadInfFile(Filename)\r
61\r
62 if SourceFileList:\r
63 for Item in SourceFileList:\r
64 self.TblInf.Insert(MODEL_EFI_SOURCE_FILE, Item, '', '', '', '', 'COMMON', -1, self.FileID, -1, -1, -1, -1, 0)\r
65\r
66\r
67 ## LoadInffile() method\r
68 #\r
69 # Load INF file and insert a record in database\r
70 #\r
71 # @param self: The object pointer\r
72 # @param Filename: Input value for filename of Inf file\r
73 #\r
74 def LoadInfFile(self, Filename = None):\r
75 # Insert a record for file\r
76 Filename = NormPath(Filename)\r
77 self.Identification.FileFullPath = Filename\r
78 (self.Identification.FileRelativePath, self.Identification.FileName) = os.path.split(Filename)\r
79\r
80 self.FileID = self.TblFile.InsertFile(Filename, MODEL_FILE_INF)\r
81\r
82 self.ParseInf(PreProcess(Filename, False), self.Identification.FileRelativePath, Filename)\r
83\r
84 ## ParserSource() method\r
85 #\r
86 # Parse Source section and insert records in database\r
87 #\r
88 # @param self: The object pointer\r
89 # @param CurrentSection: current section name\r
90 # @param SectionItemList: the item belonging current section\r
91 # @param ArchList: A list for arch for this section\r
92 # @param ThirdList: A list for third item for this section\r
93 #\r
94 def ParserSource(self, CurrentSection, SectionItemList, ArchList, ThirdList):\r
95 for Index in range(0, len(ArchList)):\r
96 Arch = ArchList[Index]\r
97 Third = ThirdList[Index]\r
98 if Arch == '':\r
99 Arch = TAB_ARCH_COMMON\r
100\r
101 for Item in SectionItemList:\r
102 if CurrentSection.upper() == 'defines'.upper():\r
103 (Name, Value) = AddToSelfMacro(self.Macros, Item[0])\r
104 self.TblInf.Insert(MODEL_META_DATA_HEADER, Name, Value, Third, '', '', Arch, -1, self.FileID, Item[1], -1, Item[1], -1, 0)\r
105\r
106 ## ParseInf() method\r
107 #\r
108 # Parse INF file and get sections information\r
109 #\r
110 # @param self: The object pointer\r
111 # @param Lines: contents of INF file\r
112 # @param FileRelativePath: relative path of the file\r
113 # @param Filename: file name of INF file\r
114 #\r
115 def ParseInf(self, Lines = [], FileRelativePath = '', Filename = ''):\r
116 IfDefList, SectionItemList, CurrentSection, ArchList, ThirdList, IncludeFiles = \\r
117 [], [], TAB_UNKNOWN, [], [], []\r
118 LineNo = 0\r
119\r
120 for Line in Lines:\r
121 LineNo = LineNo + 1\r
122 if Line == '':\r
123 continue\r
124 if Line.startswith(TAB_SECTION_START) and Line.endswith(TAB_SECTION_END):\r
125 self.ParserSource(CurrentSection, SectionItemList, ArchList, ThirdList)\r
126\r
127 # Parse the new section\r
128 SectionItemList = []\r
129 ArchList = []\r
130 ThirdList = []\r
131 # Parse section name\r
132 CurrentSection = ''\r
133 LineList = GetSplitValueList(Line[len(TAB_SECTION_START):len(Line) - len(TAB_SECTION_END)], TAB_COMMA_SPLIT)\r
134 for Item in LineList:\r
135 ItemList = GetSplitValueList(Item, TAB_SPLIT)\r
136 if CurrentSection == '':\r
137 CurrentSection = ItemList[0]\r
138 else:\r
139 if CurrentSection != ItemList[0]:\r
140 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
141 ItemList.append('')\r
142 ItemList.append('')\r
143 if len(ItemList) > 5:\r
144 RaiseParserError(Line, CurrentSection, Filename, '', LineNo)\r
145 else:\r
146 ArchList.append(ItemList[1].upper())\r
147 ThirdList.append(ItemList[2])\r
148\r
149 continue\r
150\r
151 # Add a section item\r
152 SectionItemList.append([Line, LineNo])\r
153 # End of parse\r
154\r
155 self.ParserSource(CurrentSection, SectionItemList, ArchList, ThirdList)\r
156 #End of For\r
157\r
47f15da1 158\r