]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/Python/UPT/Parser/InfLibrarySectionParser.py
BaseTools: Replace BSD License with BSD+Patent License
[mirror_edk2.git] / BaseTools / Source / Python / UPT / Parser / InfLibrarySectionParser.py
CommitLineData
4234283c 1## @file\r
f7496d71 2# This file contained the parser for [Libraries] sections in INF file\r
4234283c 3#\r
f7496d71 4# Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>\r
4234283c 5#\r
2e351cbe 6# SPDX-License-Identifier: BSD-2-Clause-Patent\r
4234283c
LG
7#\r
8'''\r
9InfLibrarySectionParser\r
10'''\r
11##\r
12# Import Modules\r
13#\r
14\r
15import Logger.Log as Logger\r
16from Logger import StringTable as ST\r
17from Logger.ToolError import FORMAT_INVALID\r
18from Parser.InfParserMisc import InfExpandMacro\r
19from Library import DataType as DT\r
20from Library.Parsing import MacroParser\r
21from Library.Misc import GetSplitValueList\r
22from Object.Parser.InfCommonObject import InfLineCommentObject\r
23from Library import GlobalData\r
24from Parser.InfParserMisc import IsLibInstanceInfo\r
25from Parser.InfAsBuiltProcess import GetLibInstanceInfo\r
26from Parser.InfParserMisc import InfParserSectionRoot\r
27\r
28class InfLibrarySectionParser(InfParserSectionRoot):\r
29 ## InfLibraryParser\r
30 #\r
f7496d71 31 #\r
4234283c
LG
32 def InfLibraryParser(self, SectionString, InfSectionObject, FileName):\r
33 #\r
34 # For Common INF file\r
35 #\r
36 if not GlobalData.gIS_BINARY_INF:\r
37 #\r
f7496d71 38 # Macro defined in this section\r
4234283c
LG
39 #\r
40 SectionMacros = {}\r
421ccda3
HC
41 ValueList = []\r
42 LibraryList = []\r
43 LibStillCommentFalg = False\r
44 LibHeaderComments = []\r
45 LibLineComment = None\r
4234283c
LG
46 #\r
47 # Parse section content\r
48 #\r
49 for Line in SectionString:\r
50 LibLineContent = Line[0]\r
421ccda3
HC
51 LibLineNo = Line[1]\r
52\r
4234283c
LG
53 if LibLineContent.strip() == '':\r
54 continue\r
421ccda3 55\r
4234283c 56 #\r
f7496d71 57 # Found Header Comments\r
4234283c
LG
58 #\r
59 if LibLineContent.strip().startswith(DT.TAB_COMMENT_SPLIT):\r
60 #\r
61 # Last line is comments, and this line go on.\r
62 #\r
63 if LibStillCommentFalg:\r
64 LibHeaderComments.append(Line)\r
65 continue\r
66 #\r
f7496d71 67 # First time encounter comment\r
4234283c
LG
68 #\r
69 else:\r
70 #\r
71 # Clear original data\r
72 #\r
73 LibHeaderComments = []\r
74 LibHeaderComments.append(Line)\r
75 LibStillCommentFalg = True\r
76 continue\r
77 else:\r
78 LibStillCommentFalg = False\r
421ccda3 79\r
4234283c
LG
80 if len(LibHeaderComments) >= 1:\r
81 LibLineComment = InfLineCommentObject()\r
82 LineCommentContent = ''\r
83 for Item in LibHeaderComments:\r
84 LineCommentContent += Item[0] + DT.END_OF_LINE\r
85 LibLineComment.SetHeaderComments(LineCommentContent)\r
421ccda3 86\r
4234283c
LG
87 #\r
88 # Find Tail comment.\r
89 #\r
90 if LibLineContent.find(DT.TAB_COMMENT_SPLIT) > -1:\r
91 LibTailComments = LibLineContent[LibLineContent.find(DT.TAB_COMMENT_SPLIT):]\r
92 LibLineContent = LibLineContent[:LibLineContent.find(DT.TAB_COMMENT_SPLIT)]\r
4231a819 93 if LibLineComment is None:\r
4234283c 94 LibLineComment = InfLineCommentObject()\r
421ccda3
HC
95 LibLineComment.SetTailComments(LibTailComments)\r
96\r
4234283c
LG
97 #\r
98 # Find Macro\r
99 #\r
100 Name, Value = MacroParser((LibLineContent, LibLineNo),\r
101 FileName,\r
102 DT.MODEL_EFI_LIBRARY_CLASS,\r
103 self.FileLocalMacros)\r
4231a819 104 if Name is not None:\r
4234283c
LG
105 SectionMacros[Name] = Value\r
106 LibLineComment = None\r
421ccda3 107 LibHeaderComments = []\r
4234283c 108 continue\r
421ccda3 109\r
4234283c
LG
110 TokenList = GetSplitValueList(LibLineContent, DT.TAB_VALUE_SPLIT, 1)\r
111 ValueList[0:len(TokenList)] = TokenList\r
421ccda3 112\r
4234283c
LG
113 #\r
114 # Replace with Local section Macro and [Defines] section Macro.\r
f7496d71 115 #\r
421ccda3 116 ValueList = [InfExpandMacro(Value, (FileName, LibLineContent, LibLineNo),\r
4234283c
LG
117 self.FileLocalMacros, SectionMacros, True)\r
118 for Value in ValueList]\r
421ccda3
HC
119\r
120 LibraryList.append((ValueList, LibLineComment,\r
4234283c
LG
121 (LibLineContent, LibLineNo, FileName)))\r
122 ValueList = []\r
123 LibLineComment = None\r
124 LibTailComments = ''\r
125 LibHeaderComments = []\r
421ccda3 126\r
4234283c 127 continue\r
421ccda3 128\r
4234283c
LG
129 #\r
130 # Current section archs\r
f7496d71 131 #\r
4234283c
LG
132 KeyList = []\r
133 for Item in self.LastSectionHeaderContent:\r
134 if (Item[1], Item[2]) not in KeyList:\r
135 KeyList.append((Item[1], Item[2]))\r
421ccda3
HC
136\r
137 if not InfSectionObject.SetLibraryClasses(LibraryList, KeyList=KeyList):\r
138 Logger.Error('InfParser',\r
4234283c
LG
139 FORMAT_INVALID,\r
140 ST.ERR_INF_PARSER_MODULE_SECTION_TYPE_ERROR % ("[Library]"),\r
421ccda3 141 File=FileName,\r
4234283c
LG
142 Line=Item[3])\r
143 #\r
144 # For Binary INF\r
145 #\r
146 else:\r
147 self.InfAsBuiltLibraryParser(SectionString, InfSectionObject, FileName)\r
421ccda3 148\r
4234283c
LG
149 def InfAsBuiltLibraryParser(self, SectionString, InfSectionObject, FileName):\r
150 LibraryList = []\r
151 LibInsFlag = False\r
152 for Line in SectionString:\r
153 LineContent = Line[0]\r
421ccda3
HC
154 LineNo = Line[1]\r
155\r
4234283c
LG
156 if LineContent.strip() == '':\r
157 LibInsFlag = False\r
158 continue\r
421ccda3 159\r
4234283c 160 if not LineContent.strip().startswith("#"):\r
421ccda3 161 Logger.Error('InfParser',\r
4234283c 162 FORMAT_INVALID,\r
421ccda3
HC
163 ST.ERR_LIB_CONTATIN_ASBUILD_AND_COMMON,\r
164 File=FileName,\r
165 Line=LineNo,\r
4234283c 166 ExtraData=LineContent)\r
4afd3d04 167\r
4234283c
LG
168 if IsLibInstanceInfo(LineContent):\r
169 LibInsFlag = True\r
170 continue\r
4afd3d04 171\r
4234283c 172 if LibInsFlag:\r
4afd3d04 173 LibGuid, LibVer = GetLibInstanceInfo(LineContent, GlobalData.gWORKSPACE, LineNo, FileName)\r
4234283c
LG
174 #\r
175 # If the VERSION_STRING is missing from the INF file, tool should default to "0".\r
176 #\r
177 if LibVer == '':\r
178 LibVer = '0'\r
179 if LibGuid != '':\r
4afd3d04
LG
180 if (LibGuid, LibVer) not in LibraryList:\r
181 LibraryList.append((LibGuid, LibVer))\r
4afd3d04 182\r
4234283c
LG
183 #\r
184 # Current section archs\r
f7496d71 185 #\r
4234283c
LG
186 KeyList = []\r
187 Item = ['', '', '']\r
188 for Item in self.LastSectionHeaderContent:\r
189 if (Item[1], Item[2]) not in KeyList:\r
190 KeyList.append((Item[1], Item[2]))\r
421ccda3
HC
191\r
192 if not InfSectionObject.SetLibraryClasses(LibraryList, KeyList=KeyList):\r
193 Logger.Error('InfParser',\r
4234283c
LG
194 FORMAT_INVALID,\r
195 ST.ERR_INF_PARSER_MODULE_SECTION_TYPE_ERROR % ("[Library]"),\r
421ccda3
HC
196 File=FileName,\r
197 Line=Item[3])\r