2 # This file is used to define class Configuration
4 # Copyright (c) 2008 - 2018, Intel Corporation. All rights reserved.<BR>
5 # SPDX-License-Identifier: BSD-2-Clause-Patent
11 from __future__
import print_function
12 import Common
.LongFilePathOs
as os
13 import Common
.EdkLogger
as EdkLogger
14 from Common
.DataType
import *
15 from Common
.StringUtils
import *
16 from Common
.LongFilePathSupport
import OpenLongFilePath
as open
18 _ConfigFileToInternalTranslation
= {
20 "ModifierList":"ModifierSet",
23 # please keep this in correct alphabetical order.
24 "AutoCorrect":"AutoCorrect",
25 "BinaryExtList":"BinaryExtList",
26 "CFunctionLayoutCheckAll":"CFunctionLayoutCheckAll",
27 "CFunctionLayoutCheckDataDeclaration":"CFunctionLayoutCheckDataDeclaration",
28 "CFunctionLayoutCheckFunctionBody":"CFunctionLayoutCheckFunctionBody",
29 "CFunctionLayoutCheckFunctionName":"CFunctionLayoutCheckFunctionName",
30 "CFunctionLayoutCheckFunctionPrototype":"CFunctionLayoutCheckFunctionPrototype",
31 "CFunctionLayoutCheckNoInitOfVariable":"CFunctionLayoutCheckNoInitOfVariable",
32 "CFunctionLayoutCheckNoStatic":"CFunctionLayoutCheckNoStatic",
33 "CFunctionLayoutCheckOptionalFunctionalModifier":"CFunctionLayoutCheckOptionalFunctionalModifier",
34 "CFunctionLayoutCheckReturnType":"CFunctionLayoutCheckReturnType",
35 "CheckAll":"CheckAll",
36 "Copyright":"Copyright",
37 "DeclarationDataTypeCheckAll":"DeclarationDataTypeCheckAll",
38 "DeclarationDataTypeCheckEFIAPIModifier":"DeclarationDataTypeCheckEFIAPIModifier",
39 "DeclarationDataTypeCheckEnumeratedType":"DeclarationDataTypeCheckEnumeratedType",
40 "DeclarationDataTypeCheckInOutModifier":"DeclarationDataTypeCheckInOutModifier",
41 "DeclarationDataTypeCheckNoUseCType":"DeclarationDataTypeCheckNoUseCType",
42 "DeclarationDataTypeCheckSameStructure":"DeclarationDataTypeCheckSameStructure",
43 "DeclarationDataTypeCheckStructureDeclaration":"DeclarationDataTypeCheckStructureDeclaration",
44 "DeclarationDataTypeCheckUnionType":"DeclarationDataTypeCheckUnionType",
45 "DoxygenCheckAll":"DoxygenCheckAll",
46 "DoxygenCheckCommand":"DoxygenCheckCommand",
47 "DoxygenCheckCommentDescription":"DoxygenCheckCommentDescription",
48 "DoxygenCheckCommentFormat":"DoxygenCheckCommentFormat",
49 "DoxygenCheckFileHeader":"DoxygenCheckFileHeader",
50 "DoxygenCheckFunctionHeader":"DoxygenCheckFunctionHeader",
51 "GeneralCheckAll":"GeneralCheckAll",
52 "GeneralCheckCarriageReturn":"GeneralCheckCarriageReturn",
53 "GeneralCheckFileExistence":"GeneralCheckFileExistence",
54 "GeneralCheckIndentation":"GeneralCheckIndentation",
55 "GeneralCheckIndentationWidth":"GeneralCheckIndentationWidth",
56 "GeneralCheckLine":"GeneralCheckLine",
57 "GeneralCheckLineEnding":"GeneralCheckLineEnding",
58 "GeneralCheckLineWidth":"GeneralCheckLineWidth",
59 "GeneralCheckNoProgma":"GeneralCheckNoProgma",
60 "GeneralCheckNoTab":"GeneralCheckNoTab",
61 "GeneralCheckNo_Asm":"GeneralCheckNo_Asm",
62 "GeneralCheckNonAcsii":"GeneralCheckNonAcsii",
63 "GeneralCheckTabWidth":"GeneralCheckTabWidth",
64 "GeneralCheckTrailingWhiteSpaceLine":"GeneralCheckTrailingWhiteSpaceLine",
65 "GeneralCheckUni":"GeneralCheckUni",
66 "HeaderCheckAll":"HeaderCheckAll",
67 "HeaderCheckCFileCommentLicenseFormat":"HeaderCheckCFileCommentLicenseFormat",
68 "HeaderCheckCFileCommentReferenceFormat":"HeaderCheckCFileCommentReferenceFormat",
69 "HeaderCheckCFileCommentStartSpacesNum":"HeaderCheckCFileCommentStartSpacesNum",
70 "HeaderCheckFile":"HeaderCheckFile",
71 "HeaderCheckFileCommentEnd":"HeaderCheckFileCommentEnd",
72 "HeaderCheckFunction":"HeaderCheckFunction",
73 "IncludeFileCheckAll":"IncludeFileCheckAll",
74 "IncludeFileCheckData":"IncludeFileCheckData",
75 "IncludeFileCheckIfndefStatement":"IncludeFileCheckIfndefStatement",
76 "IncludeFileCheckSameName":"IncludeFileCheckSameName",
77 "MetaDataFileCheckAll":"MetaDataFileCheckAll",
78 "MetaDataFileCheckBinaryInfInFdf":"MetaDataFileCheckBinaryInfInFdf",
79 "MetaDataFileCheckGenerateFileList":"MetaDataFileCheckGenerateFileList",
80 "MetaDataFileCheckGuidDuplicate":"MetaDataFileCheckGuidDuplicate",
81 "MetaDataFileCheckLibraryDefinedInDec":"MetaDataFileCheckLibraryDefinedInDec",
82 "MetaDataFileCheckLibraryInstance":"MetaDataFileCheckLibraryInstance",
83 "MetaDataFileCheckLibraryInstanceDependent":"MetaDataFileCheckLibraryInstanceDependent",
84 "MetaDataFileCheckLibraryInstanceOrder":"MetaDataFileCheckLibraryInstanceOrder",
85 "MetaDataFileCheckLibraryNoUse":"MetaDataFileCheckLibraryNoUse",
86 "MetaDataFileCheckModuleFileGuidDuplication":"MetaDataFileCheckModuleFileGuidDuplication",
87 "MetaDataFileCheckModuleFileGuidFormat":"MetaDataFileCheckModuleFileGuidFormat",
88 "MetaDataFileCheckModuleFileNoUse":"MetaDataFileCheckModuleFileNoUse",
89 "MetaDataFileCheckModuleFilePcdFormat":"MetaDataFileCheckModuleFilePcdFormat",
90 "MetaDataFileCheckModuleFilePpiFormat":"MetaDataFileCheckModuleFilePpiFormat",
91 "MetaDataFileCheckModuleFileProtocolFormat":"MetaDataFileCheckModuleFileProtocolFormat",
92 "MetaDataFileCheckPathName":"MetaDataFileCheckPathName",
93 "MetaDataFileCheckPathOfGenerateFileList":"MetaDataFileCheckPathOfGenerateFileList",
94 "MetaDataFileCheckPcdDuplicate":"MetaDataFileCheckPcdDuplicate",
95 "MetaDataFileCheckPcdFlash":"MetaDataFileCheckPcdFlash",
96 "MetaDataFileCheckPcdNoUse":"MetaDataFileCheckPcdNoUse",
97 "MetaDataFileCheckPcdType":"MetaDataFileCheckPcdType",
98 "NamingConventionCheckAll":"NamingConventionCheckAll",
99 "NamingConventionCheckDefineStatement":"NamingConventionCheckDefineStatement",
100 "NamingConventionCheckFunctionName":"NamingConventionCheckFunctionName",
101 "NamingConventionCheckIfndefStatement":"NamingConventionCheckIfndefStatement",
102 "NamingConventionCheckPathName":"NamingConventionCheckPathName",
103 "NamingConventionCheckSingleCharacterVariable":"NamingConventionCheckSingleCharacterVariable",
104 "NamingConventionCheckTypedefStatement":"NamingConventionCheckTypedefStatement",
105 "NamingConventionCheckVariableName":"NamingConventionCheckVariableName",
106 "PredicateExpressionCheckAll":"PredicateExpressionCheckAll",
107 "PredicateExpressionCheckBooleanValue":"PredicateExpressionCheckBooleanValue",
108 "PredicateExpressionCheckComparisonNullType":"PredicateExpressionCheckComparisonNullType",
109 "PredicateExpressionCheckNonBooleanOperator":"PredicateExpressionCheckNonBooleanOperator",
110 "ScanOnlyDirList":"ScanOnlyDirList",
111 "SkipDirList":"SkipDirList",
112 "SkipFileList":"SkipFileList",
113 "SmmCommParaCheckAll":"SmmCommParaCheckAll",
114 "SmmCommParaCheckBufferType":"SmmCommParaCheckBufferType",
115 "SpaceCheckAll":"SpaceCheckAll",
116 "SpellingCheckAll":"SpellingCheckAll",
117 "TokenReleaceList":"TokenReleaceList",
118 "UniCheckAll":"UniCheckAll",
119 "UniCheckHelpInfo":"UniCheckHelpInfo",
120 "UniCheckPCDInfo":"UniCheckPCDInfo",
126 # This class is used to define all items in configuration file
128 # @param Filename: The name of configuration file, the default is config.ini
130 class Configuration(object):
131 def __init__(self
, Filename
):
132 self
.Filename
= Filename
136 ## Identify to if check all items
137 # 1 - Check all items and ignore all other detailed items
138 # 0 - Not check all items, the tool will go through all other detailed items to decide to check or not
142 ## Identify to if automatically correct mistakes
143 # 1 - Automatically correct
144 # 0 - Not automatically correct
145 # Only the following check points can be automatically corrected, others not listed below are not supported even it is 1
148 # GeneralCheckIndentation
150 # GeneralCheckCarriageReturn
155 # List customized Modifer here, split with ','
156 # Defaultly use the definition in class DataType
157 self
.ModifierSet
= MODIFIER_SET
160 self
.GeneralCheckAll
= 0
162 # Check whether NO Tab is used, replaced with spaces
163 self
.GeneralCheckNoTab
= 1
165 self
.GeneralCheckTabWidth
= 2
166 # Check whether the indentation is followed coding style
167 self
.GeneralCheckIndentation
= 1
168 # The width of indentation
169 self
.GeneralCheckIndentationWidth
= 2
170 # Check whether no line is exceeding defined widty
171 self
.GeneralCheckLine
= 1
172 # The width of a line
173 self
.GeneralCheckLineWidth
= 120
174 # Check whether no use of _asm in the source file
175 self
.GeneralCheckNo_Asm
= 1
176 # Check whether no use of "#progma" in source file except "#pragma pack(#)".
177 self
.GeneralCheckNoProgma
= 1
178 # Check whether there is a carriage return at the end of the file
179 self
.GeneralCheckCarriageReturn
= 1
180 # Check whether the file exists
181 self
.GeneralCheckFileExistence
= 1
182 # Check whether file has non ACSII char
183 self
.GeneralCheckNonAcsii
= 1
184 # Check whether UNI file is valid
185 self
.GeneralCheckUni
= 1
186 # Check Only use CRLF (Carriage Return Line Feed) line endings.
187 self
.GeneralCheckLineEnding
= 1
188 # Check if there is no trailing white space in one line.
189 self
.GeneralCheckTrailingWhiteSpaceLine
= 1
191 self
.CFunctionLayoutCheckNoDeprecated
= 1
194 self
.SpaceCheckAll
= 1
196 ## Predicate Expression Checking
197 self
.PredicateExpressionCheckAll
= 0
199 # Check whether Boolean values, variable type BOOLEAN not use explicit comparisons to TRUE or FALSE
200 self
.PredicateExpressionCheckBooleanValue
= 1
201 # Check whether Non-Boolean comparisons use a compare operator (==, !=, >, < >=, <=).
202 self
.PredicateExpressionCheckNonBooleanOperator
= 1
203 # Check whether a comparison of any pointer to zero must be done via the NULL type
204 self
.PredicateExpressionCheckComparisonNullType
= 1
207 self
.HeaderCheckAll
= 0
209 # Check whether File header exists
210 self
.HeaderCheckFile
= 1
211 # Check whether Function header exists
212 self
.HeaderCheckFunction
= 1
213 # Check whether Meta data File header Comment End with '##'
214 self
.HeaderCheckFileCommentEnd
= 1
215 # Check whether C File header Comment content start with two spaces
216 self
.HeaderCheckCFileCommentStartSpacesNum
= 1
217 # Check whether C File header Comment's each reference at list should begin with a bullet character '-'
218 self
.HeaderCheckCFileCommentReferenceFormat
= 1
219 # Check whether C File header Comment have the License immediately after the ""Copyright"" line
220 self
.HeaderCheckCFileCommentLicenseFormat
= 1
222 ## C Function Layout Checking
223 self
.CFunctionLayoutCheckAll
= 0
225 # Check whether return type exists and in the first line
226 self
.CFunctionLayoutCheckReturnType
= 1
227 # Check whether any optional functional modifiers exist and next to the return type
228 self
.CFunctionLayoutCheckOptionalFunctionalModifier
= 1
229 # Check whether the next line contains the function name, left justified, followed by the beginning of the parameter list
230 # Check whether the closing parenthesis is on its own line and also indented two spaces
231 self
.CFunctionLayoutCheckFunctionName
= 1
232 # Check whether the function prototypes in include files have the same form as function definitions
233 self
.CFunctionLayoutCheckFunctionPrototype
= 1
234 # Check whether the body of a function is contained by open and close braces that must be in the first column
235 self
.CFunctionLayoutCheckFunctionBody
= 1
236 # Check whether the data declarations is the first code in a module.
237 self
.CFunctionLayoutCheckDataDeclaration
= 1
238 # Check whether no initialization of a variable as part of its declaration
239 self
.CFunctionLayoutCheckNoInitOfVariable
= 1
240 # Check whether no use of STATIC for functions
241 self
.CFunctionLayoutCheckNoStatic
= 1
243 ## Include Files Checking
244 self
.IncludeFileCheckAll
= 0
246 #Check whether having include files with same name
247 self
.IncludeFileCheckSameName
= 1
248 # Check whether all include file contents is guarded by a #ifndef statement.
249 # the #ifndef must be the first line of code following the file header comment
250 # the #endif must appear on the last line in the file
251 self
.IncludeFileCheckIfndefStatement
= 1
252 # Check whether include files contain only public or only private data
253 # Check whether include files NOT contain code or define data variables
254 self
.IncludeFileCheckData
= 1
256 ## Declarations and Data Types Checking
257 self
.DeclarationDataTypeCheckAll
= 0
259 # Check whether no use of int, unsigned, char, void, long in any .c, .h or .asl files.
260 self
.DeclarationDataTypeCheckNoUseCType
= 1
261 # Check whether the modifiers IN, OUT, OPTIONAL, and UNALIGNED are used only to qualify arguments to a function and should not appear in a data type declaration
262 self
.DeclarationDataTypeCheckInOutModifier
= 1
263 # Check whether the EFIAPI modifier should be used at the entry of drivers, events, and member functions of protocols
264 self
.DeclarationDataTypeCheckEFIAPIModifier
= 1
265 # Check whether Enumerated Type has a 'typedef' and the name is capital
266 self
.DeclarationDataTypeCheckEnumeratedType
= 1
267 # Check whether Structure Type has a 'typedef' and the name is capital
268 self
.DeclarationDataTypeCheckStructureDeclaration
= 1
269 # Check whether having same Structure
270 self
.DeclarationDataTypeCheckSameStructure
= 1
271 # Check whether Union Type has a 'typedef' and the name is capital
272 self
.DeclarationDataTypeCheckUnionType
= 1
274 ## Naming Conventions Checking
275 self
.NamingConventionCheckAll
= 0
277 # Check whether only capital letters are used for #define declarations
278 self
.NamingConventionCheckDefineStatement
= 1
279 # Check whether only capital letters are used for typedef declarations
280 self
.NamingConventionCheckTypedefStatement
= 1
281 # Check whether the #ifndef at the start of an include file uses both prefix and postfix underscore characters, '_'.
282 self
.NamingConventionCheckIfndefStatement
= 1
283 # Rule for path name, variable name and function name
284 # 1. First character should be upper case
285 # 2. Existing lower case in a word
286 # 3. No space existence
287 # Check whether the path name followed the rule
288 self
.NamingConventionCheckPathName
= 1
289 # Check whether the variable name followed the rule
290 self
.NamingConventionCheckVariableName
= 1
291 # Check whether the function name followed the rule
292 self
.NamingConventionCheckFunctionName
= 1
293 # Check whether NO use short variable name with single character
294 self
.NamingConventionCheckSingleCharacterVariable
= 1
297 self
.DoxygenCheckAll
= 0
299 # Check whether the file headers are followed Doxygen special documentation blocks in section 2.3.5
300 self
.DoxygenCheckFileHeader
= 1
301 # Check whether the function headers are followed Doxygen special documentation blocks in section 2.3.5
302 self
.DoxygenCheckFunctionHeader
= 1
303 # Check whether the first line of text in a comment block is a brief description of the element being documented.
304 # The brief description must end with a period.
305 self
.DoxygenCheckCommentDescription
= 1
306 # Check whether comment lines with '///< ... text ...' format, if it is used, it should be after the code section.
307 self
.DoxygenCheckCommentFormat
= 1
308 # Check whether only Doxygen commands allowed to mark the code are @bug and @todo.
309 self
.DoxygenCheckCommand
= 1
311 ## Meta-Data File Processing Checking
312 self
.MetaDataFileCheckAll
= 0
314 # Check whether each file defined in meta-data exists
315 self
.MetaDataFileCheckPathName
= 1
316 # Generate a list for all files defined in meta-data files
317 self
.MetaDataFileCheckGenerateFileList
= 1
318 # The path of log file
319 self
.MetaDataFileCheckPathOfGenerateFileList
= 'File.log'
320 # Check whether all Library Instances defined for a given module (or dependent library instance) match the module's type.
321 # Each Library Instance must specify the Supported Module Types in its INF file,
322 # and any module specifying the library instance must be one of the supported types.
323 self
.MetaDataFileCheckLibraryInstance
= 1
324 # Check whether a Library Instance has been defined for all dependent library classes
325 self
.MetaDataFileCheckLibraryInstanceDependent
= 1
326 # Check whether the Library Instances specified by the LibraryClasses sections are listed in order of dependencies
327 self
.MetaDataFileCheckLibraryInstanceOrder
= 1
328 # Check whether the unnecessary inclusion of library classes in the INF file
329 self
.MetaDataFileCheckLibraryNoUse
= 1
330 # Check the header file in Include\Library directory whether be defined in the package DEC file.
331 self
.MetaDataFileCheckLibraryDefinedInDec
= 1
332 # Check whether an INF file is specified in the FDF file, but not in the DSC file, then the INF file must be for a Binary module only
333 self
.MetaDataFileCheckBinaryInfInFdf
= 1
334 # Not to report error and warning related OS include file such as "windows.h" and "stdio.h"
335 # Check whether a PCD is set in a DSC file or the FDF file, but not in both.
336 self
.MetaDataFileCheckPcdDuplicate
= 1
337 # Check whether PCD settings in the FDF file can only be related to flash.
338 self
.MetaDataFileCheckPcdFlash
= 1
339 # Check whether PCDs used in INF files but not specified in DSC or FDF files
340 self
.MetaDataFileCheckPcdNoUse
= 1
341 # Check whether having duplicate guids defined for Guid/Protocol/Ppi
342 self
.MetaDataFileCheckGuidDuplicate
= 1
343 # Check whether all files under module directory are described in INF files
344 self
.MetaDataFileCheckModuleFileNoUse
= 1
345 # Check whether the PCD is correctly used in C function via its type
346 self
.MetaDataFileCheckPcdType
= 1
347 # Check whether there are FILE_GUID duplication among different INF files
348 self
.MetaDataFileCheckModuleFileGuidDuplication
= 1
350 # Check Guid Format in INF files
351 self
.MetaDataFileCheckModuleFileGuidFormat
= 1
352 # Check Protocol Format in INF files
353 self
.MetaDataFileCheckModuleFileProtocolFormat
= 1
354 # Check Ppi Format in INF files
355 self
.MetaDataFileCheckModuleFilePpiFormat
= 1
356 # Check Pcd Format in INF files
357 self
.MetaDataFileCheckModuleFilePcdFormat
= 1
361 # Check INF or DEC file whether defined the localized information in the associated UNI file.
362 self
.UniCheckHelpInfo
= 1
363 # Check PCD whether defined the prompt, help in the DEC file and localized information in the associated UNI file.
364 self
.UniCheckPCDInfo
= 1
366 # Check SMM communication function parameter
367 self
.SmmCommParaCheckAll
= 0
368 # Check if the EFI_SMM_COMMUNICATION_PROTOCOL parameter buffer type is Reserved / ACPI NVS or UEFI RT code/data
369 self
.SmmCommParaCheckBufferType
= -1
372 # The check points in this section are reserved
374 # GotoStatementCheckAll = 0
376 self
.SpellingCheckAll
= 0
378 # The directory listed here will not be parsed, split with ','
379 self
.SkipDirList
= []
381 # The file listed here will not be parsed, split with ','
382 self
.SkipFileList
= []
384 # A list for binary file ext name
385 self
.BinaryExtList
= []
387 # A list for only scanned folders
388 self
.ScanOnlyDirList
= []
390 # A list for Copyright format
393 self
.TokenReleaceList
= []
397 def ParseConfig(self
):
398 Filepath
= os
.path
.normpath(self
.Filename
)
399 if not os
.path
.isfile(Filepath
):
400 ErrorMsg
= "Can't find configuration file '%s'" % Filepath
401 EdkLogger
.error("Ecc", EdkLogger
.ECC_ERROR
, ErrorMsg
, File
= Filepath
)
404 for Line
in open(Filepath
, 'r'):
406 Line
= CleanString(Line
)
408 List
= GetSplitValueList(Line
, TAB_EQUAL_SPLIT
)
409 if List
[0] not in _ConfigFileToInternalTranslation
:
410 ErrorMsg
= "Invalid configuration option '%s' was found" % List
[0]
411 EdkLogger
.error("Ecc", EdkLogger
.ECC_ERROR
, ErrorMsg
, File
= Filepath
, Line
= LineNo
)
412 assert _ConfigFileToInternalTranslation
[List
[0]] in self
.__dict
__
413 if List
[0] == 'ModifierList':
414 List
[1] = GetSplitValueList(List
[1], TAB_COMMA_SPLIT
)
415 if List
[0] == 'MetaDataFileCheckPathOfGenerateFileList' and List
[1] == "":
417 if List
[0] == 'SkipDirList':
418 List
[1] = GetSplitValueList(List
[1], TAB_COMMA_SPLIT
)
419 if List
[0] == 'SkipFileList':
420 List
[1] = GetSplitValueList(List
[1], TAB_COMMA_SPLIT
)
421 if List
[0] == 'BinaryExtList':
422 List
[1] = GetSplitValueList(List
[1], TAB_COMMA_SPLIT
)
423 if List
[0] == 'Copyright':
424 List
[1] = GetSplitValueList(List
[1], TAB_COMMA_SPLIT
)
425 if List
[0] == 'TokenReleaceList':
426 List
[1] = GetSplitValueList(List
[1], TAB_COMMA_SPLIT
)
427 self
.__dict
__[_ConfigFileToInternalTranslation
[List
[0]]] = List
[1]
431 for Key
in self
.__dict
__.keys():
432 print(Key
, '=', self
.__dict
__[Key
])
435 # test that our dict and out class still match in contents.
437 if __name__
== '__main__':
438 myconfig
= Configuration("BaseTools\Source\Python\Ecc\config.ini")
439 for each
in myconfig
.__dict
__:
440 if each
== "Filename":
442 assert each
in _ConfigFileToInternalTranslation
.values()
443 for each
in _ConfigFileToInternalTranslation
.values():
444 assert each
in myconfig
.__dict
__