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