]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/Ecc/Configuration.py
c19a3990c74e113855d37747f2f4dc4bdc9efca7
[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 ## Space Checking
197 self.SpaceCheckAll = 1
198
199 ## Predicate Expression Checking
200 self.PredicateExpressionCheckAll = 0
201
202 # Check whether Boolean values, variable type BOOLEAN not use explicit comparisons to TRUE or FALSE
203 self.PredicateExpressionCheckBooleanValue = 1
204 # Check whether Non-Boolean comparisons use a compare operator (==, !=, >, < >=, <=).
205 self.PredicateExpressionCheckNonBooleanOperator = 1
206 # Check whether a comparison of any pointer to zero must be done via the NULL type
207 self.PredicateExpressionCheckComparisonNullType = 1
208
209 ## Headers Checking
210 self.HeaderCheckAll = 0
211
212 # Check whether File header exists
213 self.HeaderCheckFile = 1
214 # Check whether Function header exists
215 self.HeaderCheckFunction = 1
216 # Check whether Meta data File header Comment End with '##'
217 self.HeaderCheckFileCommentEnd = 1
218 # Check whether C File header Comment content start with two spaces
219 self.HeaderCheckCFileCommentStartSpacesNum = 1
220 # Check whether C File header Comment's each reference at list should begin with a bullet character '-'
221 self.HeaderCheckCFileCommentReferenceFormat = 1
222 # Check whether C File header Comment have the License immediately after the ""Copyright"" line
223 self.HeaderCheckCFileCommentLicenseFormat = 1
224
225 ## C Function Layout Checking
226 self.CFunctionLayoutCheckAll = 0
227
228 # Check whether return type exists and in the first line
229 self.CFunctionLayoutCheckReturnType = 1
230 # Check whether any optional functional modifiers exist and next to the return type
231 self.CFunctionLayoutCheckOptionalFunctionalModifier = 1
232 # Check whether the next line contains the function name, left justified, followed by the beginning of the parameter list
233 # Check whether the closing parenthesis is on its own line and also indented two spaces
234 self.CFunctionLayoutCheckFunctionName = 1
235 # Check whether the function prototypes in include files have the same form as function definitions
236 self.CFunctionLayoutCheckFunctionPrototype = 1
237 # Check whether the body of a function is contained by open and close braces that must be in the first column
238 self.CFunctionLayoutCheckFunctionBody = 1
239 # Check whether the data declarations is the first code in a module.
240 self.CFunctionLayoutCheckDataDeclaration = 1
241 # Check whether no initialization of a variable as part of its declaration
242 self.CFunctionLayoutCheckNoInitOfVariable = 1
243 # Check whether no use of STATIC for functions
244 self.CFunctionLayoutCheckNoStatic = 1
245
246 ## Include Files Checking
247 self.IncludeFileCheckAll = 0
248
249 #Check whether having include files with same name
250 self.IncludeFileCheckSameName = 1
251 # Check whether all include file contents is guarded by a #ifndef statement.
252 # the #ifndef must be the first line of code following the file header comment
253 # the #endif must appear on the last line in the file
254 self.IncludeFileCheckIfndefStatement = 1
255 # Check whether include files contain only public or only private data
256 # Check whether include files NOT contain code or define data variables
257 self.IncludeFileCheckData = 1
258
259 ## Declarations and Data Types Checking
260 self.DeclarationDataTypeCheckAll = 0
261
262 # Check whether no use of int, unsigned, char, void, static, long in any .c, .h or .asl files.
263 self.DeclarationDataTypeCheckNoUseCType = 1
264 # 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
265 self.DeclarationDataTypeCheckInOutModifier = 1
266 # Check whether the EFIAPI modifier should be used at the entry of drivers, events, and member functions of protocols
267 self.DeclarationDataTypeCheckEFIAPIModifier = 1
268 # Check whether Enumerated Type has a 'typedef' and the name is capital
269 self.DeclarationDataTypeCheckEnumeratedType = 1
270 # Check whether Structure Type has a 'typedef' and the name is capital
271 self.DeclarationDataTypeCheckStructureDeclaration = 1
272 # Check whether having same Structure
273 self.DeclarationDataTypeCheckSameStructure = 1
274 # Check whether Union Type has a 'typedef' and the name is capital
275 self.DeclarationDataTypeCheckUnionType = 1
276
277 ## Naming Conventions Checking
278 self.NamingConventionCheckAll = 0
279
280 # Check whether only capital letters are used for #define declarations
281 self.NamingConventionCheckDefineStatement = 1
282 # Check whether only capital letters are used for typedef declarations
283 self.NamingConventionCheckTypedefStatement = 1
284 # Check whether the #ifndef at the start of an include file uses both prefix and postfix underscore characters, '_'.
285 self.NamingConventionCheckIfndefStatement = 1
286 # Rule for path name, variable name and function name
287 # 1. First character should be upper case
288 # 2. Existing lower case in a word
289 # 3. No space existence
290 # Check whether the path name followed the rule
291 self.NamingConventionCheckPathName = 1
292 # Check whether the variable name followed the rule
293 self.NamingConventionCheckVariableName = 1
294 # Check whether the function name followed the rule
295 self.NamingConventionCheckFunctionName = 1
296 # Check whether NO use short variable name with single character
297 self.NamingConventionCheckSingleCharacterVariable = 1
298
299 ## Doxygen Checking
300 self.DoxygenCheckAll = 0
301
302 # Check whether the file headers are followed Doxygen special documentation blocks in section 2.3.5
303 self.DoxygenCheckFileHeader = 1
304 # Check whether the function headers are followed Doxygen special documentation blocks in section 2.3.5
305 self.DoxygenCheckFunctionHeader = 1
306 # Check whether the first line of text in a comment block is a brief description of the element being documented.
307 # The brief description must end with a period.
308 self.DoxygenCheckCommentDescription = 1
309 # Check whether comment lines with '///< ... text ...' format, if it is used, it should be after the code section.
310 self.DoxygenCheckCommentFormat = 1
311 # Check whether only Doxygen commands allowed to mark the code are @bug and @todo.
312 self.DoxygenCheckCommand = 1
313
314 ## Meta-Data File Processing Checking
315 self.MetaDataFileCheckAll = 0
316
317 # Check whether each file defined in meta-data exists
318 self.MetaDataFileCheckPathName = 1
319 # Generate a list for all files defined in meta-data files
320 self.MetaDataFileCheckGenerateFileList = 1
321 # The path of log file
322 self.MetaDataFileCheckPathOfGenerateFileList = 'File.log'
323 # Check whether all Library Instances defined for a given module (or dependent library instance) match the module's type.
324 # Each Library Instance must specify the Supported Module Types in its INF file,
325 # and any module specifying the library instance must be one of the supported types.
326 self.MetaDataFileCheckLibraryInstance = 1
327 # Check whether a Library Instance has been defined for all dependent library classes
328 self.MetaDataFileCheckLibraryInstanceDependent = 1
329 # Check whether the Library Instances specified by the LibraryClasses sections are listed in order of dependencies
330 self.MetaDataFileCheckLibraryInstanceOrder = 1
331 # Check whether the unnecessary inclusion of library classes in the INF file
332 self.MetaDataFileCheckLibraryNoUse = 1
333 # Check the header file in Include\Library directory whether be defined in the package DEC file.
334 self.MetaDataFileCheckLibraryDefinedInDec = 1
335 # 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
336 self.MetaDataFileCheckBinaryInfInFdf = 1
337 # Not to report error and warning related OS include file such as "windows.h" and "stdio.h"
338 # Check whether a PCD is set in a DSC file or the FDF file, but not in both.
339 self.MetaDataFileCheckPcdDuplicate = 1
340 # Check whether PCD settings in the FDF file can only be related to flash.
341 self.MetaDataFileCheckPcdFlash = 1
342 # Check whether PCDs used in INF files but not specified in DSC or FDF files
343 self.MetaDataFileCheckPcdNoUse = 1
344 # Check whether having duplicate guids defined for Guid/Protocol/Ppi
345 self.MetaDataFileCheckGuidDuplicate = 1
346 # Check whether all files under module directory are described in INF files
347 self.MetaDataFileCheckModuleFileNoUse = 1
348 # Check whether the PCD is correctly used in C function via its type
349 self.MetaDataFileCheckPcdType = 1
350 # Check whether there are FILE_GUID duplication among different INF files
351 self.MetaDataFileCheckModuleFileGuidDuplication = 1
352
353 # Check Guid Format in INF files
354 self.MetaDataFileCheckModuleFileGuidFormat = 1
355 # Check Protocol Format in INF files
356 self.MetaDataFileCheckModuleFileProtocolFormat = 1
357 # Check Ppi Format in INF files
358 self.MetaDataFileCheckModuleFilePpiFormat = 1
359 # Check Pcd Format in INF files
360 self.MetaDataFileCheckModuleFilePcdFormat = 1
361
362 # Check UNI file
363 self.UniCheckAll = 0
364 # Check INF or DEC file whether defined the localized information in the associated UNI file.
365 self.UniCheckHelpInfo = 1
366 # Check PCD whether defined the prompt, help in the DEC file and localized information in the associated UNI file.
367 self.UniCheckPCDInfo = 1
368
369 # Check SMM communication function parameter
370 self.SmmCommParaCheckAll = 0
371 # Check if the EFI_SMM_COMMUNICATION_PROTOCOL parameter buffer type is Reserved / ACPI NVS or UEFI RT code/data
372 self.SmmCommParaCheckBufferType = -1
373
374 #
375 # The check points in this section are reserved
376 #
377 # GotoStatementCheckAll = 0
378 #
379 self.SpellingCheckAll = 0
380
381 # The directory listed here will not be parsed, split with ','
382 self.SkipDirList = []
383
384 # The file listed here will not be parsed, split with ','
385 self.SkipFileList = []
386
387 # A list for binary file ext name
388 self.BinaryExtList = []
389
390 # A list for only scanned folders
391 self.ScanOnlyDirList = []
392
393 # A list for Copyright format
394 self.Copyright = []
395
396 self.ParseConfig()
397
398 def ParseConfig(self):
399 Filepath = os.path.normpath(self.Filename)
400 if not os.path.isfile(Filepath):
401 ErrorMsg = "Can't find configuration file '%s'" % Filepath
402 EdkLogger.error("Ecc", EdkLogger.ECC_ERROR, ErrorMsg, File = Filepath)
403
404 LineNo = 0
405 for Line in open(Filepath, 'r'):
406 LineNo = LineNo + 1
407 Line = CleanString(Line)
408 if Line != '':
409 List = GetSplitValueList(Line, TAB_EQUAL_SPLIT)
410 if List[0] not in _ConfigFileToInternalTranslation:
411 ErrorMsg = "Invalid configuration option '%s' was found" % List[0]
412 EdkLogger.error("Ecc", EdkLogger.ECC_ERROR, ErrorMsg, File = Filepath, Line = LineNo)
413 assert _ConfigFileToInternalTranslation[List[0]] in self.__dict__
414 if List[0] == 'ModifierList':
415 List[1] = GetSplitValueList(List[1], TAB_COMMA_SPLIT)
416 if List[0] == 'MetaDataFileCheckPathOfGenerateFileList' and List[1] == "":
417 continue
418 if List[0] == 'SkipDirList':
419 List[1] = GetSplitValueList(List[1], TAB_COMMA_SPLIT)
420 if List[0] == 'SkipFileList':
421 List[1] = GetSplitValueList(List[1], TAB_COMMA_SPLIT)
422 if List[0] == 'BinaryExtList':
423 List[1] = GetSplitValueList(List[1], TAB_COMMA_SPLIT)
424 if List[0] == 'Copyright':
425 List[1] = GetSplitValueList(List[1], TAB_COMMA_SPLIT)
426 self.__dict__[_ConfigFileToInternalTranslation[List[0]]] = List[1]
427
428 def ShowMe(self):
429 print(self.Filename)
430 for Key in self.__dict__.keys():
431 print(Key, '=', self.__dict__[Key])
432
433 #
434 # test that our dict and out class still match in contents.
435 #
436 if __name__ == '__main__':
437 myconfig = Configuration("BaseTools\Source\Python\Ecc\config.ini")
438 for each in myconfig.__dict__:
439 if each == "Filename":
440 continue
441 assert each in _ConfigFileToInternalTranslation.values()
442 for each in _ConfigFileToInternalTranslation.values():
443 assert each in myconfig.__dict__