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