]>
Commit | Line | Data |
---|---|---|
4234283c | 1 | ## @file\r |
f7496d71 | 2 | # This file contained the miscellaneous functions for INF parser\r |
4234283c | 3 | #\r |
64285f15 | 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 | |
16 | InfParserMisc\r | |
17 | '''\r | |
18 | \r | |
19 | ##\r | |
20 | # Import Modules\r | |
21 | #\r | |
22 | import re\r | |
23 | \r | |
24 | \r | |
25 | from Library import DataType as DT\r | |
26 | \r | |
27 | \r | |
64285f15 YZ |
28 | from Library.StringUtils import gMACRO_PATTERN\r |
29 | from Library.StringUtils import ReplaceMacro\r | |
4234283c LG |
30 | from Object.Parser.InfMisc import ErrorInInf\r |
31 | from Logger.StringTable import ERR_MARCO_DEFINITION_MISS_ERROR\r | |
32 | \r | |
33 | #\r | |
34 | # Global variable\r | |
35 | #\r | |
36 | \r | |
37 | #\r | |
38 | # Sections can exist in INF file\r | |
39 | #\r | |
40 | gINF_SECTION_DEF = {\r | |
41 | DT.TAB_UNKNOWN.upper() : DT.MODEL_UNKNOWN,\r | |
42 | DT.TAB_HEADER.upper() : DT.MODEL_META_DATA_FILE_HEADER,\r | |
43 | DT.TAB_INF_DEFINES.upper() : DT.MODEL_META_DATA_DEFINE,\r | |
44 | DT.TAB_BUILD_OPTIONS.upper() : DT.MODEL_META_DATA_BUILD_OPTION,\r | |
45 | DT.TAB_LIBRARY_CLASSES.upper() : DT.MODEL_EFI_LIBRARY_CLASS,\r | |
46 | DT.TAB_PACKAGES.upper() : DT.MODEL_META_DATA_PACKAGE,\r | |
47 | DT.TAB_INF_FIXED_PCD.upper() : DT.MODEL_PCD_FIXED_AT_BUILD,\r | |
48 | DT.TAB_INF_PATCH_PCD.upper() : DT.MODEL_PCD_PATCHABLE_IN_MODULE,\r | |
49 | DT.TAB_INF_FEATURE_PCD.upper() : DT.MODEL_PCD_FEATURE_FLAG,\r | |
50 | DT.TAB_INF_PCD_EX.upper() : DT.MODEL_PCD_DYNAMIC_EX,\r | |
51 | DT.TAB_INF_PCD.upper() : DT.MODEL_PCD_DYNAMIC,\r | |
52 | DT.TAB_SOURCES.upper() : DT.MODEL_EFI_SOURCE_FILE,\r | |
53 | DT.TAB_GUIDS.upper() : DT.MODEL_EFI_GUID,\r | |
54 | DT.TAB_PROTOCOLS.upper() : DT.MODEL_EFI_PROTOCOL,\r | |
55 | DT.TAB_PPIS.upper() : DT.MODEL_EFI_PPI,\r | |
56 | DT.TAB_DEPEX.upper() : DT.MODEL_EFI_DEPEX,\r | |
57 | DT.TAB_BINARIES.upper() : DT.MODEL_EFI_BINARY_FILE,\r | |
58 | DT.TAB_USER_EXTENSIONS.upper() : DT.MODEL_META_DATA_USER_EXTENSION\r | |
59 | #\r | |
60 | # EDK1 section\r | |
61 | # TAB_NMAKE.upper() : MODEL_META_DATA_NMAKE\r | |
f7496d71 | 62 | #\r |
4234283c LG |
63 | }\r |
64 | \r | |
65 | ## InfExpandMacro\r | |
66 | #\r | |
f7496d71 | 67 | # Expand MACRO definition with MACROs defined in [Defines] section and specific section.\r |
4234283c LG |
68 | # The MACROs defined in specific section has high priority and will be expanded firstly.\r |
69 | #\r | |
70 | # @param LineInfo Contain information of FileName, LineContent, LineNo\r | |
71 | # @param GlobalMacros MACROs defined in INF [Defines] section\r | |
72 | # @param SectionMacros MACROs defined in INF specific section\r | |
f7496d71 | 73 | # @param Flag If the flag set to True, need to skip macros in a quoted string\r |
4234283c LG |
74 | #\r |
75 | def InfExpandMacro(Content, LineInfo, GlobalMacros=None, SectionMacros=None, Flag=False):\r | |
4231a819 | 76 | if GlobalMacros is None:\r |
4234283c | 77 | GlobalMacros = {}\r |
4231a819 | 78 | if SectionMacros is None:\r |
4234283c | 79 | SectionMacros = {}\r |
f7496d71 | 80 | \r |
4234283c LG |
81 | FileName = LineInfo[0]\r |
82 | LineContent = LineInfo[1]\r | |
83 | LineNo = LineInfo[2]\r | |
f7496d71 | 84 | \r |
421ccda3 HC |
85 | # Don't expand macros in comments\r |
86 | if LineContent.strip().startswith("#"):\r | |
87 | return Content\r | |
88 | \r | |
4234283c | 89 | NewLineInfo = (FileName, LineNo, LineContent)\r |
f7496d71 | 90 | \r |
4234283c LG |
91 | #\r |
92 | # First, replace MARCOs with value defined in specific section\r | |
93 | #\r | |
f7496d71 | 94 | Content = ReplaceMacro (Content,\r |
4234283c LG |
95 | SectionMacros,\r |
96 | False,\r | |
97 | (LineContent, LineNo),\r | |
98 | FileName,\r | |
99 | Flag)\r | |
100 | #\r | |
101 | # Then replace MARCOs with value defined in [Defines] section\r | |
102 | #\r | |
f7496d71 | 103 | Content = ReplaceMacro (Content,\r |
4234283c LG |
104 | GlobalMacros,\r |
105 | False,\r | |
106 | (LineContent, LineNo),\r | |
107 | FileName,\r | |
108 | Flag)\r | |
f7496d71 | 109 | \r |
4234283c LG |
110 | MacroUsed = gMACRO_PATTERN.findall(Content)\r |
111 | #\r | |
112 | # no macro found in String, stop replacing\r | |
113 | #\r | |
114 | if len(MacroUsed) == 0:\r | |
115 | return Content\r | |
116 | else:\r | |
117 | for Macro in MacroUsed:\r | |
118 | gQuotedMacro = re.compile(".*\".*\$\(%s\).*\".*"%(Macro))\r | |
119 | if not gQuotedMacro.match(Content):\r | |
120 | #\r | |
121 | # Still have MACROs can't be expanded.\r | |
122 | #\r | |
123 | ErrorInInf (ERR_MARCO_DEFINITION_MISS_ERROR,\r | |
124 | LineInfo=NewLineInfo)\r | |
f7496d71 | 125 | \r |
4234283c | 126 | return Content\r |
f7496d71 | 127 | \r |
4234283c LG |
128 | \r |
129 | ## IsBinaryInf\r | |
130 | #\r | |
131 | # Judge whether the INF file is Binary INF or Common INF\r | |
132 | #\r | |
133 | # @param FileLineList A list contain all INF file content.\r | |
134 | #\r | |
135 | def IsBinaryInf(FileLineList):\r | |
136 | if not FileLineList:\r | |
137 | return False\r | |
f7496d71 | 138 | \r |
4234283c LG |
139 | ReIsSourcesSection = re.compile("^\s*\[Sources.*\]\s.*$", re.IGNORECASE)\r |
140 | ReIsBinarySection = re.compile("^\s*\[Binaries.*\]\s.*$", re.IGNORECASE)\r | |
141 | BinarySectionFoundFlag = False\r | |
f7496d71 | 142 | \r |
4234283c LG |
143 | for Line in FileLineList:\r |
144 | if ReIsSourcesSection.match(Line):\r | |
145 | return False\r | |
146 | if ReIsBinarySection.match(Line):\r | |
147 | BinarySectionFoundFlag = True\r | |
f7496d71 | 148 | \r |
4234283c LG |
149 | if BinarySectionFoundFlag:\r |
150 | return True\r | |
f7496d71 | 151 | \r |
4234283c | 152 | return False\r |
f7496d71 LG |
153 | \r |
154 | \r | |
4234283c | 155 | ## IsLibInstanceInfo\r |
f7496d71 | 156 | #\r |
4234283c LG |
157 | # Judge whether the string contain the information of ## @LIB_INSTANCES.\r |
158 | #\r | |
159 | # @param String\r | |
160 | #\r | |
161 | # @return Flag\r | |
162 | #\r | |
163 | def IsLibInstanceInfo(String):\r | |
164 | ReIsLibInstance = re.compile("^\s*##\s*@LIB_INSTANCES\s*$")\r | |
165 | if ReIsLibInstance.match(String):\r | |
166 | return True\r | |
167 | else:\r | |
168 | return False\r | |
f7496d71 LG |
169 | \r |
170 | \r | |
4234283c | 171 | ## IsAsBuildOptionInfo\r |
f7496d71 | 172 | #\r |
4234283c LG |
173 | # Judge whether the string contain the information of ## @ASBUILD.\r |
174 | #\r | |
175 | # @param String\r | |
176 | #\r | |
177 | # @return Flag\r | |
178 | #\r | |
179 | def IsAsBuildOptionInfo(String):\r | |
180 | ReIsAsBuildInstance = re.compile("^\s*##\s*@AsBuilt\s*$")\r | |
181 | if ReIsAsBuildInstance.match(String):\r | |
182 | return True\r | |
183 | else:\r | |
f7496d71 LG |
184 | return False\r |
185 | \r | |
4234283c LG |
186 | \r |
187 | class InfParserSectionRoot(object):\r | |
188 | def __init__(self):\r | |
189 | #\r | |
190 | # Macros defined in [Define] section are file scope global\r | |
191 | #\r | |
192 | self.FileLocalMacros = {}\r | |
f7496d71 | 193 | \r |
4234283c | 194 | #\r |
f7496d71 | 195 | # Current Section Header content.\r |
4234283c LG |
196 | #\r |
197 | self.SectionHeaderContent = []\r | |
198 | \r | |
199 | #\r | |
f7496d71 | 200 | # Last time Section Header content.\r |
4234283c | 201 | #\r |
f7496d71 LG |
202 | self.LastSectionHeaderContent = []\r |
203 | \r | |
4234283c | 204 | self.FullPath = ''\r |
f7496d71 | 205 | \r |
4234283c LG |
206 | self.InfDefSection = None\r |
207 | self.InfBuildOptionSection = None\r | |
208 | self.InfLibraryClassSection = None\r | |
209 | self.InfPackageSection = None\r | |
210 | self.InfPcdSection = None\r | |
211 | self.InfSourcesSection = None\r | |
212 | self.InfUserExtensionSection = None\r | |
213 | self.InfProtocolSection = None\r | |
214 | self.InfPpiSection = None\r | |
215 | self.InfGuidSection = None\r | |
216 | self.InfDepexSection = None\r | |
217 | self.InfPeiDepexSection = None\r | |
218 | self.InfDxeDepexSection = None\r | |
219 | self.InfSmmDepexSection = None\r | |
220 | self.InfBinariesSection = None\r | |
221 | self.InfHeader = None\r | |
f7496d71 | 222 | self.InfSpecialCommentSection = None\r |