]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/Python/UPT/Parser/InfDefineSectionParser.py
BaseTools: Clean up source files
[mirror_edk2.git] / BaseTools / Source / Python / UPT / Parser / InfDefineSectionParser.py
CommitLineData
4234283c 1## @file\r
f7496d71 2# This file contained the parser for define sections in INF file\r
4234283c 3#\r
f7496d71 4# Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>\r
4234283c 5#\r
f7496d71
LG
6# This program and the accompanying materials are licensed and made available\r
7# under the terms and conditions of the BSD License which accompanies this\r
8# distribution. The full text of the license may be found at\r
4234283c
LG
9# http://opensource.org/licenses/bsd-license.php\r
10#\r
11# THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12# WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13#\r
14\r
15'''\r
16InfDefineSectionParser\r
17'''\r
18##\r
19# Import Modules\r
20#\r
21import re\r
22\r
23from Library import DataType as DT\r
24from Library import GlobalData\r
25from Library.Parsing import MacroParser\r
26from Library.Misc import GetSplitValueList\r
27from Library.ParserValidate import IsValidArch\r
28from Object.Parser.InfCommonObject import InfLineCommentObject\r
29from Object.Parser.InfDefineObject import InfDefMember\r
30from Parser.InfParserMisc import InfExpandMacro\r
31from Object.Parser.InfMisc import ErrorInInf\r
32from Logger import StringTable as ST\r
33from Parser.InfParserMisc import InfParserSectionRoot\r
34\r
35## __GetValidateArchList\r
f7496d71 36#\r
4234283c
LG
37#\r
38def GetValidateArchList(LineContent):\r
f7496d71 39\r
4234283c
LG
40 TempArch = ''\r
41 ArchList = []\r
42 ValidateAcrhPatten = re.compile(r"^\s*#\s*VALID_ARCHITECTURES\s*=\s*.*$", re.DOTALL)\r
f7496d71 43\r
4234283c
LG
44 if ValidateAcrhPatten.match(LineContent):\r
45 TempArch = GetSplitValueList(LineContent, DT.TAB_EQUAL_SPLIT, 1)[1]\r
f7496d71 46\r
4234283c 47 TempArch = GetSplitValueList(TempArch, '(', 1)[0]\r
f7496d71 48\r
4234283c
LG
49 ArchList = re.split('\s+', TempArch)\r
50 NewArchList = []\r
51 for Arch in ArchList:\r
52 if IsValidArch(Arch):\r
53 NewArchList.append(Arch)\r
f7496d71 54\r
4234283c 55 ArchList = NewArchList\r
f7496d71
LG
56\r
57 return ArchList\r
4234283c
LG
58\r
59class InfDefinSectionParser(InfParserSectionRoot):\r
60 def InfDefineParser(self, SectionString, InfSectionObject, FileName, SectionComment):\r
f7496d71 61\r
4234283c
LG
62 if SectionComment:\r
63 pass\r
64 #\r
65 # Parser Defines section content and fill self._ContentList dict.\r
66 #\r
67 StillCommentFalg = False\r
68 HeaderComments = []\r
69 SectionContent = ''\r
70 ArchList = []\r
71 _ContentList = []\r
72 _ValueList = []\r
73 #\r
74 # Add WORKSPACE to global Marco dict.\r
75 #\r
76 self.FileLocalMacros['WORKSPACE'] = GlobalData.gWORKSPACE\r
f7496d71 77\r
4234283c
LG
78 for Line in SectionString:\r
79 LineContent = Line[0]\r
80 LineNo = Line[1]\r
81 TailComments = ''\r
82 LineComment = None\r
f7496d71 83\r
4234283c
LG
84 LineInfo = ['', -1, '']\r
85 LineInfo[0] = FileName\r
86 LineInfo[1] = LineNo\r
87 LineInfo[2] = LineContent\r
f7496d71 88\r
4234283c
LG
89 if LineContent.strip() == '':\r
90 continue\r
91 #\r
92 # The first time encountered VALIDATE_ARCHITECHERS will be considered as support arch list.\r
93 #\r
94 if not ArchList:\r
95 ArchList = GetValidateArchList(LineContent)\r
96\r
97 #\r
98 # Parser Comment\r
99 #\r
100 if LineContent.strip().startswith(DT.TAB_COMMENT_SPLIT):\r
101 #\r
102 # Last line is comments, and this line go on.\r
103 #\r
104 if StillCommentFalg:\r
105 HeaderComments.append(Line)\r
106 SectionContent += LineContent + DT.END_OF_LINE\r
107 continue\r
108 #\r
f7496d71 109 # First time encounter comment\r
4234283c
LG
110 #\r
111 else:\r
112 #\r
113 # Clear original data\r
114 #\r
115 HeaderComments = []\r
116 HeaderComments.append(Line)\r
117 StillCommentFalg = True\r
118 SectionContent += LineContent + DT.END_OF_LINE\r
119 continue\r
120 else:\r
121 StillCommentFalg = False\r
f7496d71 122\r
4234283c
LG
123 if len(HeaderComments) >= 1:\r
124 LineComment = InfLineCommentObject()\r
125 LineCommentContent = ''\r
126 for Item in HeaderComments:\r
127 LineCommentContent += Item[0] + DT.END_OF_LINE\r
128 LineComment.SetHeaderComments(LineCommentContent)\r
f7496d71 129\r
4234283c
LG
130 #\r
131 # Find Tail comment.\r
132 #\r
133 if LineContent.find(DT.TAB_COMMENT_SPLIT) > -1:\r
134 TailComments = LineContent[LineContent.find(DT.TAB_COMMENT_SPLIT):]\r
135 LineContent = LineContent[:LineContent.find(DT.TAB_COMMENT_SPLIT)]\r
4231a819 136 if LineComment is None:\r
4234283c
LG
137 LineComment = InfLineCommentObject()\r
138 LineComment.SetTailComments(TailComments)\r
f7496d71 139\r
4234283c
LG
140 #\r
141 # Find Macro\r
142 #\r
f7496d71
LG
143 Name, Value = MacroParser((LineContent, LineNo),\r
144 FileName,\r
145 DT.MODEL_META_DATA_HEADER,\r
4234283c 146 self.FileLocalMacros)\r
4231a819 147 if Name is not None:\r
4234283c 148 self.FileLocalMacros[Name] = Value\r
f7496d71 149 continue\r
4234283c
LG
150\r
151 #\r
152 # Replace with [Defines] section Macro\r
153 #\r
f7496d71
LG
154 LineContent = InfExpandMacro(LineContent,\r
155 (FileName, LineContent, LineNo),\r
156 self.FileLocalMacros,\r
4234283c 157 None, True)\r
f7496d71 158\r
4234283c 159 SectionContent += LineContent + DT.END_OF_LINE\r
f7496d71 160\r
4234283c
LG
161 TokenList = GetSplitValueList(LineContent, DT.TAB_EQUAL_SPLIT, 1)\r
162 if len(TokenList) < 2:\r
163 ErrorInInf(ST.ERR_INF_PARSER_DEFINE_ITEM_NO_VALUE,\r
f7496d71 164 LineInfo=LineInfo)\r
4234283c
LG
165 _ValueList[0:len(TokenList)] = TokenList\r
166 if not _ValueList[0]:\r
167 ErrorInInf(ST.ERR_INF_PARSER_DEFINE_ITEM_NO_NAME,\r
168 LineInfo=LineInfo)\r
169 if not _ValueList[1]:\r
170 ErrorInInf(ST.ERR_INF_PARSER_DEFINE_ITEM_NO_VALUE,\r
f7496d71
LG
171 LineInfo=LineInfo)\r
172\r
173 Name, Value = _ValueList[0], _ValueList[1]\r
174\r
4234283c 175 InfDefMemberObj = InfDefMember(Name, Value)\r
4231a819 176 if (LineComment is not None):\r
4234283c
LG
177 InfDefMemberObj.Comments.SetHeaderComments(LineComment.GetHeaderComments())\r
178 InfDefMemberObj.Comments.SetTailComments(LineComment.GetTailComments())\r
f7496d71 179\r
4234283c
LG
180 InfDefMemberObj.CurrentLine.SetFileName(self.FullPath)\r
181 InfDefMemberObj.CurrentLine.SetLineString(LineContent)\r
182 InfDefMemberObj.CurrentLine.SetLineNo(LineNo)\r
f7496d71 183\r
4234283c
LG
184 _ContentList.append(InfDefMemberObj)\r
185 HeaderComments = []\r
186 TailComments = ''\r
f7496d71 187\r
4234283c
LG
188 #\r
189 # Current Define section archs\r
190 #\r
191 if not ArchList:\r
192 ArchList = ['COMMON']\r
f7496d71
LG
193\r
194 InfSectionObject.SetAllContent(SectionContent)\r
195\r
4234283c 196 InfSectionObject.SetDefines(_ContentList, Arch=ArchList)\r
f7496d71 197\r