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