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 "UniCheckAll":"UniCheckAll",
118 "UniCheckHelpInfo":"UniCheckHelpInfo",
119 "UniCheckPCDInfo":"UniCheckPCDInfo",
125 # This class is used to define all items in configuration file
127 # @param Filename: The name of configuration file, the default is config.ini
129 class Configuration(object):
130 def __init__(self
, Filename
):
131 self
.Filename
= Filename
135 ## Identify to if check all items
136 # 1 - Check all items and ignore all other detailed items
137 # 0 - Not check all items, the tool will go through all other detailed items to decide to check or not
141 ## Identify to if automatically correct mistakes
142 # 1 - Automatically correct
143 # 0 - Not automatically correct
144 # Only the following check points can be automatically corrected, others not listed below are not supported even it is 1
147 # GeneralCheckIndentation
149 # GeneralCheckCarriageReturn
154 # List customized Modifer here, split with ','
155 # Defaultly use the definition in class DataType
156 self
.ModifierSet
= MODIFIER_SET
159 self
.GeneralCheckAll
= 0
161 # Check whether NO Tab is used, replaced with spaces
162 self
.GeneralCheckNoTab
= 1
164 self
.GeneralCheckTabWidth
= 2
165 # Check whether the indentation is followed coding style
166 self
.GeneralCheckIndentation
= 1
167 # The width of indentation
168 self
.GeneralCheckIndentationWidth
= 2
169 # Check whether no line is exceeding defined widty
170 self
.GeneralCheckLine
= 1
171 # The width of a line
172 self
.GeneralCheckLineWidth
= 120
173 # Check whether no use of _asm in the source file
174 self
.GeneralCheckNo_Asm
= 1
175 # Check whether no use of "#progma" in source file except "#pragma pack(#)".
176 self
.GeneralCheckNoProgma
= 1
177 # Check whether there is a carriage return at the end of the file
178 self
.GeneralCheckCarriageReturn
= 1
179 # Check whether the file exists
180 self
.GeneralCheckFileExistence
= 1
181 # Check whether file has non ACSII char
182 self
.GeneralCheckNonAcsii
= 1
183 # Check whether UNI file is valid
184 self
.GeneralCheckUni
= 1
185 # Check Only use CRLF (Carriage Return Line Feed) line endings.
186 self
.GeneralCheckLineEnding
= 1
187 # Check if there is no trailing white space in one line.
188 self
.GeneralCheckTrailingWhiteSpaceLine
= 1
190 self
.CFunctionLayoutCheckNoDeprecated
= 1
193 self
.SpaceCheckAll
= 1
195 ## Predicate Expression Checking
196 self
.PredicateExpressionCheckAll
= 0
198 # Check whether Boolean values, variable type BOOLEAN not use explicit comparisons to TRUE or FALSE
199 self
.PredicateExpressionCheckBooleanValue
= 1
200 # Check whether Non-Boolean comparisons use a compare operator (==, !=, >, < >=, <=).
201 self
.PredicateExpressionCheckNonBooleanOperator
= 1
202 # Check whether a comparison of any pointer to zero must be done via the NULL type
203 self
.PredicateExpressionCheckComparisonNullType
= 1
206 self
.HeaderCheckAll
= 0
208 # Check whether File header exists
209 self
.HeaderCheckFile
= 1
210 # Check whether Function header exists
211 self
.HeaderCheckFunction
= 1
212 # Check whether Meta data File header Comment End with '##'
213 self
.HeaderCheckFileCommentEnd
= 1
214 # Check whether C File header Comment content start with two spaces
215 self
.HeaderCheckCFileCommentStartSpacesNum
= 1
216 # Check whether C File header Comment's each reference at list should begin with a bullet character '-'
217 self
.HeaderCheckCFileCommentReferenceFormat
= 1
218 # Check whether C File header Comment have the License immediately after the ""Copyright"" line
219 self
.HeaderCheckCFileCommentLicenseFormat
= 1
221 ## C Function Layout Checking
222 self
.CFunctionLayoutCheckAll
= 0
224 # Check whether return type exists and in the first line
225 self
.CFunctionLayoutCheckReturnType
= 1
226 # Check whether any optional functional modifiers exist and next to the return type
227 self
.CFunctionLayoutCheckOptionalFunctionalModifier
= 1
228 # Check whether the next line contains the function name, left justified, followed by the beginning of the parameter list
229 # Check whether the closing parenthesis is on its own line and also indented two spaces
230 self
.CFunctionLayoutCheckFunctionName
= 1
231 # Check whether the function prototypes in include files have the same form as function definitions
232 self
.CFunctionLayoutCheckFunctionPrototype
= 1
233 # Check whether the body of a function is contained by open and close braces that must be in the first column
234 self
.CFunctionLayoutCheckFunctionBody
= 1
235 # Check whether the data declarations is the first code in a module.
236 self
.CFunctionLayoutCheckDataDeclaration
= 1
237 # Check whether no initialization of a variable as part of its declaration
238 self
.CFunctionLayoutCheckNoInitOfVariable
= 1
239 # Check whether no use of STATIC for functions
240 self
.CFunctionLayoutCheckNoStatic
= 1
242 ## Include Files Checking
243 self
.IncludeFileCheckAll
= 0
245 #Check whether having include files with same name
246 self
.IncludeFileCheckSameName
= 1
247 # Check whether all include file contents is guarded by a #ifndef statement.
248 # the #ifndef must be the first line of code following the file header comment
249 # the #endif must appear on the last line in the file
250 self
.IncludeFileCheckIfndefStatement
= 1
251 # Check whether include files contain only public or only private data
252 # Check whether include files NOT contain code or define data variables
253 self
.IncludeFileCheckData
= 1
255 ## Declarations and Data Types Checking
256 self
.DeclarationDataTypeCheckAll
= 0
258 # Check whether no use of int, unsigned, char, void, static, long in any .c, .h or .asl files.
259 self
.DeclarationDataTypeCheckNoUseCType
= 1
260 # 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
261 self
.DeclarationDataTypeCheckInOutModifier
= 1
262 # Check whether the EFIAPI modifier should be used at the entry of drivers, events, and member functions of protocols
263 self
.DeclarationDataTypeCheckEFIAPIModifier
= 1
264 # Check whether Enumerated Type has a 'typedef' and the name is capital
265 self
.DeclarationDataTypeCheckEnumeratedType
= 1
266 # Check whether Structure Type has a 'typedef' and the name is capital
267 self
.DeclarationDataTypeCheckStructureDeclaration
= 1
268 # Check whether having same Structure
269 self
.DeclarationDataTypeCheckSameStructure
= 1
270 # Check whether Union Type has a 'typedef' and the name is capital
271 self
.DeclarationDataTypeCheckUnionType
= 1
273 ## Naming Conventions Checking
274 self
.NamingConventionCheckAll
= 0
276 # Check whether only capital letters are used for #define declarations
277 self
.NamingConventionCheckDefineStatement
= 1
278 # Check whether only capital letters are used for typedef declarations
279 self
.NamingConventionCheckTypedefStatement
= 1
280 # Check whether the #ifndef at the start of an include file uses both prefix and postfix underscore characters, '_'.
281 self
.NamingConventionCheckIfndefStatement
= 1
282 # Rule for path name, variable name and function name
283 # 1. First character should be upper case
284 # 2. Existing lower case in a word
285 # 3. No space existence
286 # Check whether the path name followed the rule
287 self
.NamingConventionCheckPathName
= 1
288 # Check whether the variable name followed the rule
289 self
.NamingConventionCheckVariableName
= 1
290 # Check whether the function name followed the rule
291 self
.NamingConventionCheckFunctionName
= 1
292 # Check whether NO use short variable name with single character
293 self
.NamingConventionCheckSingleCharacterVariable
= 1
296 self
.DoxygenCheckAll
= 0
298 # Check whether the file headers are followed Doxygen special documentation blocks in section 2.3.5
299 self
.DoxygenCheckFileHeader
= 1
300 # Check whether the function headers are followed Doxygen special documentation blocks in section 2.3.5
301 self
.DoxygenCheckFunctionHeader
= 1
302 # Check whether the first line of text in a comment block is a brief description of the element being documented.
303 # The brief description must end with a period.
304 self
.DoxygenCheckCommentDescription
= 1
305 # Check whether comment lines with '///< ... text ...' format, if it is used, it should be after the code section.
306 self
.DoxygenCheckCommentFormat
= 1
307 # Check whether only Doxygen commands allowed to mark the code are @bug and @todo.
308 self
.DoxygenCheckCommand
= 1
310 ## Meta-Data File Processing Checking
311 self
.MetaDataFileCheckAll
= 0
313 # Check whether each file defined in meta-data exists
314 self
.MetaDataFileCheckPathName
= 1
315 # Generate a list for all files defined in meta-data files
316 self
.MetaDataFileCheckGenerateFileList
= 1
317 # The path of log file
318 self
.MetaDataFileCheckPathOfGenerateFileList
= 'File.log'
319 # Check whether all Library Instances defined for a given module (or dependent library instance) match the module's type.
320 # Each Library Instance must specify the Supported Module Types in its INF file,
321 # and any module specifying the library instance must be one of the supported types.
322 self
.MetaDataFileCheckLibraryInstance
= 1
323 # Check whether a Library Instance has been defined for all dependent library classes
324 self
.MetaDataFileCheckLibraryInstanceDependent
= 1
325 # Check whether the Library Instances specified by the LibraryClasses sections are listed in order of dependencies
326 self
.MetaDataFileCheckLibraryInstanceOrder
= 1
327 # Check whether the unnecessary inclusion of library classes in the INF file
328 self
.MetaDataFileCheckLibraryNoUse
= 1
329 # Check the header file in Include\Library directory whether be defined in the package DEC file.
330 self
.MetaDataFileCheckLibraryDefinedInDec
= 1
331 # 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
332 self
.MetaDataFileCheckBinaryInfInFdf
= 1
333 # Not to report error and warning related OS include file such as "windows.h" and "stdio.h"
334 # Check whether a PCD is set in a DSC file or the FDF file, but not in both.
335 self
.MetaDataFileCheckPcdDuplicate
= 1
336 # Check whether PCD settings in the FDF file can only be related to flash.
337 self
.MetaDataFileCheckPcdFlash
= 1
338 # Check whether PCDs used in INF files but not specified in DSC or FDF files
339 self
.MetaDataFileCheckPcdNoUse
= 1
340 # Check whether having duplicate guids defined for Guid/Protocol/Ppi
341 self
.MetaDataFileCheckGuidDuplicate
= 1
342 # Check whether all files under module directory are described in INF files
343 self
.MetaDataFileCheckModuleFileNoUse
= 1
344 # Check whether the PCD is correctly used in C function via its type
345 self
.MetaDataFileCheckPcdType
= 1
346 # Check whether there are FILE_GUID duplication among different INF files
347 self
.MetaDataFileCheckModuleFileGuidDuplication
= 1
349 # Check Guid Format in INF files
350 self
.MetaDataFileCheckModuleFileGuidFormat
= 1
351 # Check Protocol Format in INF files
352 self
.MetaDataFileCheckModuleFileProtocolFormat
= 1
353 # Check Ppi Format in INF files
354 self
.MetaDataFileCheckModuleFilePpiFormat
= 1
355 # Check Pcd Format in INF files
356 self
.MetaDataFileCheckModuleFilePcdFormat
= 1
360 # Check INF or DEC file whether defined the localized information in the associated UNI file.
361 self
.UniCheckHelpInfo
= 1
362 # Check PCD whether defined the prompt, help in the DEC file and localized information in the associated UNI file.
363 self
.UniCheckPCDInfo
= 1
365 # Check SMM communication function parameter
366 self
.SmmCommParaCheckAll
= 0
367 # Check if the EFI_SMM_COMMUNICATION_PROTOCOL parameter buffer type is Reserved / ACPI NVS or UEFI RT code/data
368 self
.SmmCommParaCheckBufferType
= -1
371 # The check points in this section are reserved
373 # GotoStatementCheckAll = 0
375 self
.SpellingCheckAll
= 0
377 # The directory listed here will not be parsed, split with ','
378 self
.SkipDirList
= []
380 # The file listed here will not be parsed, split with ','
381 self
.SkipFileList
= []
383 # A list for binary file ext name
384 self
.BinaryExtList
= []
386 # A list for only scanned folders
387 self
.ScanOnlyDirList
= []
389 # A list for Copyright format
394 def ParseConfig(self
):
395 Filepath
= os
.path
.normpath(self
.Filename
)
396 if not os
.path
.isfile(Filepath
):
397 ErrorMsg
= "Can't find configuration file '%s'" % Filepath
398 EdkLogger
.error("Ecc", EdkLogger
.ECC_ERROR
, ErrorMsg
, File
= Filepath
)
401 for Line
in open(Filepath
, 'r'):
403 Line
= CleanString(Line
)
405 List
= GetSplitValueList(Line
, TAB_EQUAL_SPLIT
)
406 if List
[0] not in _ConfigFileToInternalTranslation
:
407 ErrorMsg
= "Invalid configuration option '%s' was found" % List
[0]
408 EdkLogger
.error("Ecc", EdkLogger
.ECC_ERROR
, ErrorMsg
, File
= Filepath
, Line
= LineNo
)
409 assert _ConfigFileToInternalTranslation
[List
[0]] in self
.__dict
__
410 if List
[0] == 'ModifierList':
411 List
[1] = GetSplitValueList(List
[1], TAB_COMMA_SPLIT
)
412 if List
[0] == 'MetaDataFileCheckPathOfGenerateFileList' and List
[1] == "":
414 if List
[0] == 'SkipDirList':
415 List
[1] = GetSplitValueList(List
[1], TAB_COMMA_SPLIT
)
416 if List
[0] == 'SkipFileList':
417 List
[1] = GetSplitValueList(List
[1], TAB_COMMA_SPLIT
)
418 if List
[0] == 'BinaryExtList':
419 List
[1] = GetSplitValueList(List
[1], TAB_COMMA_SPLIT
)
420 if List
[0] == 'Copyright':
421 List
[1] = GetSplitValueList(List
[1], TAB_COMMA_SPLIT
)
422 self
.__dict
__[_ConfigFileToInternalTranslation
[List
[0]]] = List
[1]
426 for Key
in self
.__dict
__.keys():
427 print(Key
, '=', self
.__dict
__[Key
])
430 # test that our dict and out class still match in contents.
432 if __name__
== '__main__':
433 myconfig
= Configuration("BaseTools\Source\Python\Ecc\config.ini")
434 for each
in myconfig
.__dict
__:
435 if each
== "Filename":
437 assert each
in _ConfigFileToInternalTranslation
.values()
438 for each
in _ConfigFileToInternalTranslation
.values():
439 assert each
in myconfig
.__dict
__