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