2 # This file contained the miscellaneous functions for INF parser
4 # Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>
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
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.
25 from Library
import DataType
as DT
28 from Library
.StringUtils
import gMACRO_PATTERN
29 from Library
.StringUtils
import ReplaceMacro
30 from Object
.Parser
.InfMisc
import ErrorInInf
31 from Logger
.StringTable
import ERR_MARCO_DEFINITION_MISS_ERROR
38 # Sections can exist in INF file
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
61 # TAB_NMAKE.upper() : MODEL_META_DATA_NMAKE
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.
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
75 def InfExpandMacro(Content
, LineInfo
, GlobalMacros
=None, SectionMacros
=None, Flag
=False):
76 if GlobalMacros
is None:
78 if SectionMacros
is None:
81 FileName
= LineInfo
[0]
82 LineContent
= LineInfo
[1]
85 # Don't expand macros in comments
86 if LineContent
.strip().startswith("#"):
89 NewLineInfo
= (FileName
, LineNo
, LineContent
)
92 # First, replace MARCOs with value defined in specific section
94 Content
= ReplaceMacro (Content
,
97 (LineContent
, LineNo
),
101 # Then replace MARCOs with value defined in [Defines] section
103 Content
= ReplaceMacro (Content
,
106 (LineContent
, LineNo
),
110 MacroUsed
= gMACRO_PATTERN
.findall(Content
)
112 # no macro found in String, stop replacing
114 if len(MacroUsed
) == 0:
117 for Macro
in MacroUsed
:
118 gQuotedMacro
= re
.compile(".*\".*\$\(%s\).*\".*"%(Macro))
119 if not gQuotedMacro
.match(Content
):
121 # Still have MACROs can't be expanded.
123 ErrorInInf (ERR_MARCO_DEFINITION_MISS_ERROR
,
124 LineInfo
=NewLineInfo
)
131 # Judge whether the INF file is Binary INF or Common INF
133 # @param FileLineList A list contain all INF file content.
135 def IsBinaryInf(FileLineList
):
139 ReIsSourcesSection
= re
.compile("^\s*\[Sources.*\]\s.*$", re
.IGNORECASE
)
140 ReIsBinarySection
= re
.compile("^\s*\[Binaries.*\]\s.*$", re
.IGNORECASE
)
141 BinarySectionFoundFlag
= False
143 for Line
in FileLineList
:
144 if ReIsSourcesSection
.match(Line
):
146 if ReIsBinarySection
.match(Line
):
147 BinarySectionFoundFlag
= True
149 if BinarySectionFoundFlag
:
157 # Judge whether the string contain the information of ## @LIB_INSTANCES.
163 def IsLibInstanceInfo(String
):
164 ReIsLibInstance
= re
.compile("^\s*##\s*@LIB_INSTANCES\s*$")
165 if ReIsLibInstance
.match(String
):
171 ## IsAsBuildOptionInfo
173 # Judge whether the string contain the information of ## @ASBUILD.
179 def IsAsBuildOptionInfo(String
):
180 ReIsAsBuildInstance
= re
.compile("^\s*##\s*@AsBuilt\s*$")
181 if ReIsAsBuildInstance
.match(String
):
187 class InfParserSectionRoot(object):
190 # Macros defined in [Define] section are file scope global
192 self
.FileLocalMacros
= {}
195 # Current Section Header content.
197 self
.SectionHeaderContent
= []
200 # Last time Section Header content.
202 self
.LastSectionHeaderContent
= []
206 self
.InfDefSection
= None
207 self
.InfBuildOptionSection
= None
208 self
.InfLibraryClassSection
= None
209 self
.InfPackageSection
= None
210 self
.InfPcdSection
= None
211 self
.InfSourcesSection
= None
212 self
.InfUserExtensionSection
= None
213 self
.InfProtocolSection
= None
214 self
.InfPpiSection
= None
215 self
.InfGuidSection
= None
216 self
.InfDepexSection
= None
217 self
.InfPeiDepexSection
= None
218 self
.InfDxeDepexSection
= None
219 self
.InfSmmDepexSection
= None
220 self
.InfBinariesSection
= None
221 self
.InfHeader
= None
222 self
.InfSpecialCommentSection
= None