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