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