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