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