]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/AutoGen/StrGather.py
b84d9f90aaec59e003d65fa3adb3252622c85714
[mirror_edk2.git] / BaseTools / Source / Python / AutoGen / StrGather.py
1 # Copyright (c) 2007 - 2010, Intel Corporation. All rights reserved.<BR>
2 # This program and the accompanying materials
3 # are licensed and made available under the terms and conditions of the BSD License
4 # which accompanies this distribution. The full text of the license may be found at
5 # http://opensource.org/licenses/bsd-license.php
6 #
7 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
8 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
9
10 #
11 #This file is used to parse a strings file and create or add to a string database file.
12 #
13
14 ##
15 # Import Modules
16 #
17 import re
18 import Common.EdkLogger as EdkLogger
19 from Common.BuildToolError import *
20 from UniClassObject import *
21 from StringIO import StringIO
22 from struct import pack
23
24 ##
25 # Static definitions
26 #
27 EFI_HII_SIBT_END = '0x00'
28 EFI_HII_SIBT_STRING_SCSU = '0x10'
29 EFI_HII_SIBT_STRING_SCSU_FONT = '0x11'
30 EFI_HII_SIBT_STRINGS_SCSU = '0x12'
31 EFI_HII_SIBT_STRINGS_SCSU_FONT = '0x13'
32 EFI_HII_SIBT_STRING_UCS2 = '0x14'
33 EFI_HII_SIBT_STRING_UCS2_FONT = '0x15'
34 EFI_HII_SIBT_STRINGS_UCS2 = '0x16'
35 EFI_HII_SIBT_STRINGS_UCS2_FONT = '0x17'
36 EFI_HII_SIBT_DUPLICATE = '0x20'
37 EFI_HII_SIBT_SKIP2 = '0x21'
38 EFI_HII_SIBT_SKIP1 = '0x22'
39 EFI_HII_SIBT_EXT1 = '0x30'
40 EFI_HII_SIBT_EXT2 = '0x31'
41 EFI_HII_SIBT_EXT4 = '0x32'
42 EFI_HII_SIBT_FONT = '0x40'
43
44 EFI_HII_PACKAGE_STRINGS = '0x04'
45 EFI_HII_PACKAGE_FORM = '0x02'
46
47 StringPackageType = EFI_HII_PACKAGE_STRINGS
48 StringPackageForm = EFI_HII_PACKAGE_FORM
49 StringBlockType = EFI_HII_SIBT_STRING_UCS2
50 StringSkipType = EFI_HII_SIBT_SKIP2
51
52 HexHeader = '0x'
53
54 COMMENT = '// '
55 DEFINE_STR = '#define'
56 COMMENT_DEFINE_STR = COMMENT + DEFINE_STR
57 NOT_REFERENCED = 'not referenced'
58 COMMENT_NOT_REFERENCED = ' ' + COMMENT + NOT_REFERENCED
59 CHAR_ARRAY_DEFIN = 'unsigned char'
60 COMMON_FILE_NAME = 'Strings'
61 OFFSET = 'offset'
62 STRING = 'string'
63 TO = 'to'
64 STRING_TOKEN = re.compile('STRING_TOKEN *\(([A-Z0-9_]+) *\)', re.MULTILINE | re.UNICODE)
65 COMPATIBLE_STRING_TOKEN = re.compile('STRING_TOKEN *\(([A-Z0-9_]+) *\)', re.MULTILINE | re.UNICODE)
66
67 EFI_HII_ARRAY_SIZE_LENGTH = 4
68 EFI_HII_PACKAGE_HEADER_LENGTH = 4
69 EFI_HII_HDR_SIZE_LENGTH = 4
70 EFI_HII_STRING_OFFSET_LENGTH = 4
71 EFI_STRING_ID = 1
72 EFI_STRING_ID_LENGTH = 2
73 EFI_HII_LANGUAGE_WINDOW = 0
74 EFI_HII_LANGUAGE_WINDOW_LENGTH = 2
75 EFI_HII_LANGUAGE_WINDOW_NUMBER = 16
76 EFI_HII_STRING_PACKAGE_HDR_LENGTH = EFI_HII_PACKAGE_HEADER_LENGTH + EFI_HII_HDR_SIZE_LENGTH + EFI_HII_STRING_OFFSET_LENGTH + EFI_HII_LANGUAGE_WINDOW_LENGTH * EFI_HII_LANGUAGE_WINDOW_NUMBER + EFI_STRING_ID_LENGTH
77
78 H_C_FILE_HEADER = ['//', \
79 '// DO NOT EDIT -- auto-generated file', \
80 '//', \
81 '// This file is generated by the StrGather utility', \
82 '//']
83 LANGUAGE_NAME_STRING_NAME = '$LANGUAGE_NAME'
84 PRINTABLE_LANGUAGE_NAME_STRING_NAME = '$PRINTABLE_LANGUAGE_NAME'
85
86 ## Convert a dec number to a hex string
87 #
88 # Convert a dec number to a formatted hex string in length digit
89 # The digit is set to default 8
90 # The hex string starts with "0x"
91 # DecToHexStr(1000) is '0x000003E8'
92 # DecToHexStr(1000, 6) is '0x0003E8'
93 #
94 # @param Dec: The number in dec format
95 # @param Digit: The needed digit of hex string
96 #
97 # @retval: The formatted hex string
98 #
99 def DecToHexStr(Dec, Digit = 8):
100 return eval("'0x%0" + str(Digit) + "X' % int(Dec)")
101
102 ## Convert a dec number to a hex list
103 #
104 # Convert a dec number to a formatted hex list in size digit
105 # The digit is set to default 8
106 # DecToHexList(1000) is ['0xE8', '0x03', '0x00', '0x00']
107 # DecToHexList(1000, 6) is ['0xE8', '0x03', '0x00']
108 #
109 # @param Dec: The number in dec format
110 # @param Digit: The needed digit of hex list
111 #
112 # @retval: A list for formatted hex string
113 #
114 def DecToHexList(Dec, Digit = 8):
115 Hex = eval("'%0" + str(Digit) + "X' % int(Dec)" )
116 List = []
117 for Bit in range(Digit - 2, -1, -2):
118 List.append(HexHeader + Hex[Bit:Bit + 2])
119 return List
120
121 ## Convert a acsii string to a hex list
122 #
123 # Convert a acsii string to a formatted hex list
124 # AscToHexList('en-US') is ['0x65', '0x6E', '0x2D', '0x55', '0x53']
125 #
126 # @param Ascii: The acsii string
127 #
128 # @retval: A list for formatted hex string
129 #
130 def AscToHexList(Ascii):
131 List = []
132 for Item in Ascii:
133 List.append('0x%2X' % ord(Item))
134
135 return List
136
137 ## Create header of .h file
138 #
139 # Create a header of .h file
140 #
141 # @param BaseName: The basename of strings
142 #
143 # @retval Str: A string for .h file header
144 #
145 def CreateHFileHeader(BaseName):
146 Str = ''
147 for Item in H_C_FILE_HEADER:
148 Str = WriteLine(Str, Item)
149 Str = WriteLine(Str, '#ifndef _' + BaseName.upper() + '_STRINGS_DEFINE_H_')
150 Str = WriteLine(Str, '#define _' + BaseName.upper() + '_STRINGS_DEFINE_H_')
151 return Str
152
153 ## Create content of .h file
154 #
155 # Create content of .h file
156 #
157 # @param BaseName: The basename of strings
158 # @param UniObjectClass A UniObjectClass instance
159 # @param IsCompatibleMode Compatible mode
160 # @param UniGenCFlag UniString is generated into AutoGen C file when it is set to True
161 #
162 # @retval Str: A string of .h file content
163 #
164 def CreateHFileContent(BaseName, UniObjectClass, IsCompatibleMode, UniGenCFlag):
165 Str = ''
166 ValueStartPtr = 60
167 Line = COMMENT_DEFINE_STR + ' ' + LANGUAGE_NAME_STRING_NAME + ' ' * (ValueStartPtr - len(DEFINE_STR + LANGUAGE_NAME_STRING_NAME)) + DecToHexStr(0, 4) + COMMENT_NOT_REFERENCED
168 Str = WriteLine(Str, Line)
169 Line = COMMENT_DEFINE_STR + ' ' + PRINTABLE_LANGUAGE_NAME_STRING_NAME + ' ' * (ValueStartPtr - len(DEFINE_STR + PRINTABLE_LANGUAGE_NAME_STRING_NAME)) + DecToHexStr(1, 4) + COMMENT_NOT_REFERENCED
170 Str = WriteLine(Str, Line)
171 UnusedStr = ''
172
173 #Group the referred/Unused STRING token together.
174 for Index in range(2, len(UniObjectClass.OrderedStringList[UniObjectClass.LanguageDef[0][0]])):
175 StringItem = UniObjectClass.OrderedStringList[UniObjectClass.LanguageDef[0][0]][Index]
176 Name = StringItem.StringName
177 Token = StringItem.Token
178 Referenced = StringItem.Referenced
179 if Name != None:
180 Line = ''
181 if Referenced == True:
182 if (ValueStartPtr - len(DEFINE_STR + Name)) <= 0:
183 Line = DEFINE_STR + ' ' + Name + ' ' + DecToHexStr(Token, 4)
184 else:
185 Line = DEFINE_STR + ' ' + Name + ' ' * (ValueStartPtr - len(DEFINE_STR + Name)) + DecToHexStr(Token, 4)
186 Str = WriteLine(Str, Line)
187 else:
188 if (ValueStartPtr - len(DEFINE_STR + Name)) <= 0:
189 Line = COMMENT_DEFINE_STR + ' ' + Name + ' ' + DecToHexStr(Token, 4) + COMMENT_NOT_REFERENCED
190 else:
191 Line = COMMENT_DEFINE_STR + ' ' + Name + ' ' * (ValueStartPtr - len(DEFINE_STR + Name)) + DecToHexStr(Token, 4) + COMMENT_NOT_REFERENCED
192 UnusedStr = WriteLine(UnusedStr, Line)
193
194 Str = ''.join([Str,UnusedStr])
195
196 Str = WriteLine(Str, '')
197 if IsCompatibleMode or UniGenCFlag:
198 Str = WriteLine(Str, 'extern unsigned char ' + BaseName + 'Strings[];')
199 return Str
200
201 ## Create a complete .h file
202 #
203 # Create a complet .h file with file header and file content
204 #
205 # @param BaseName: The basename of strings
206 # @param UniObjectClass A UniObjectClass instance
207 # @param IsCompatibleMode Compatible mode
208 # @param UniGenCFlag UniString is generated into AutoGen C file when it is set to True
209 #
210 # @retval Str: A string of complete .h file
211 #
212 def CreateHFile(BaseName, UniObjectClass, IsCompatibleMode, UniGenCFlag):
213 HFile = WriteLine('', CreateHFileContent(BaseName, UniObjectClass, IsCompatibleMode, UniGenCFlag))
214
215 return HFile
216
217 ## Create header of .c file
218 #
219 # Create a header of .c file
220 #
221 # @retval Str: A string for .c file header
222 #
223 def CreateCFileHeader():
224 Str = ''
225 for Item in H_C_FILE_HEADER:
226 Str = WriteLine(Str, Item)
227
228 return Str
229
230 ## Create a buffer to store all items in an array
231 #
232 # @param BinBuffer Buffer to contain Binary data.
233 # @param Array: The array need to be formatted
234 #
235 def CreateBinBuffer(BinBuffer, Array):
236 for Item in Array:
237 BinBuffer.write(pack("B", int(Item,16)))
238
239 ## Create a formatted string all items in an array
240 #
241 # Use ',' to join each item in an array, and break an new line when reaching the width (default is 16)
242 #
243 # @param Array: The array need to be formatted
244 # @param Width: The line length, the default value is set to 16
245 #
246 # @retval ArrayItem: A string for all formatted array items
247 #
248 def CreateArrayItem(Array, Width = 16):
249 MaxLength = Width
250 Index = 0
251 Line = ' '
252 ArrayItem = ''
253
254 for Item in Array:
255 if Index < MaxLength:
256 Line = Line + Item + ', '
257 Index = Index + 1
258 else:
259 ArrayItem = WriteLine(ArrayItem, Line)
260 Line = ' ' + Item + ', '
261 Index = 1
262 ArrayItem = Write(ArrayItem, Line.rstrip())
263
264 return ArrayItem
265
266 ## CreateCFileStringValue
267 #
268 # Create a line with string value
269 #
270 # @param Value: Value of the string
271 #
272 # @retval Str: A formatted string with string value
273 #
274
275 def CreateCFileStringValue(Value):
276 Value = [StringBlockType] + Value
277 Str = WriteLine('', CreateArrayItem(Value))
278
279 return Str
280
281 ## GetFilteredLanguage
282 #
283 # apply get best language rules to the UNI language code list
284 #
285 # @param UniLanguageList: language code definition list in *.UNI file
286 # @param LanguageFilterList: language code filter list of RFC4646 format in DSC file
287 #
288 # @retval UniLanguageListFiltered: the filtered language code
289 #
290 def GetFilteredLanguage(UniLanguageList, LanguageFilterList):
291 UniLanguageListFiltered = []
292 # if filter list is empty, then consider there is no filter
293 if LanguageFilterList == []:
294 UniLanguageListFiltered = UniLanguageList
295 return UniLanguageListFiltered
296 for Language in LanguageFilterList:
297 # first check for exact match
298 if Language in UniLanguageList:
299 if Language not in UniLanguageListFiltered:
300 UniLanguageListFiltered += [Language]
301 # find the first one with the same/equivalent primary tag
302 else:
303 if Language.find('-') != -1:
304 PrimaryTag = Language[0:Language.find('-')].lower()
305 else:
306 PrimaryTag = Language
307
308 if len(PrimaryTag) == 3:
309 PrimaryTag = LangConvTable.get(PrimaryTag)
310
311 for UniLanguage in UniLanguageList:
312 if UniLanguage.find('-') != -1:
313 UniLanguagePrimaryTag = UniLanguage[0:UniLanguage.find('-')].lower()
314 else:
315 UniLanguagePrimaryTag = UniLanguage
316
317 if len(UniLanguagePrimaryTag) == 3:
318 UniLanguagePrimaryTag = LangConvTable.get(UniLanguagePrimaryTag)
319
320 if PrimaryTag == UniLanguagePrimaryTag:
321 if UniLanguage not in UniLanguageListFiltered:
322 UniLanguageListFiltered += [UniLanguage]
323 break
324 else:
325 # Here is rule 3 for "get best language"
326 # If tag is not listed in the Unicode file, the default ("en") tag should be used for that language
327 # for better processing, find the one that best suit for it.
328 DefaultTag = 'en'
329 if DefaultTag not in UniLanguageListFiltered:
330 # check whether language code with primary code equivalent with DefaultTag already in the list, if so, use that
331 for UniLanguage in UniLanguageList:
332 if UniLanguage.startswith('en-') or UniLanguage.startswith('eng-'):
333 if UniLanguage not in UniLanguageListFiltered:
334 UniLanguageListFiltered += [UniLanguage]
335 break
336 else:
337 UniLanguageListFiltered += [DefaultTag]
338 return UniLanguageListFiltered
339
340
341 ## Create content of .c file
342 #
343 # Create content of .c file
344 #
345 # @param BaseName: The basename of strings
346 # @param UniObjectClass A UniObjectClass instance
347 # @param IsCompatibleMode Compatible mode
348 # @param UniBinBuffer UniBinBuffer to contain UniBinary data.
349 # @param FilterInfo Platform language filter information
350 #
351 # @retval Str: A string of .c file content
352 #
353 def CreateCFileContent(BaseName, UniObjectClass, IsCompatibleMode, UniBinBuffer, FilterInfo):
354 #
355 # Init array length
356 #
357 TotalLength = EFI_HII_ARRAY_SIZE_LENGTH
358 Str = ''
359 Offset = 0
360
361 EDK2Module = FilterInfo[0]
362 if EDK2Module:
363 LanguageFilterList = FilterInfo[1]
364 else:
365 # EDK module is using ISO639-2 format filter, convert to the RFC4646 format
366 LanguageFilterList = [LangConvTable.get(F.lower()) for F in FilterInfo[1]]
367
368 UniLanguageList = []
369 for IndexI in range(len(UniObjectClass.LanguageDef)):
370 UniLanguageList += [UniObjectClass.LanguageDef[IndexI][0]]
371
372 UniLanguageListFiltered = GetFilteredLanguage(UniLanguageList, LanguageFilterList)
373
374
375 #
376 # Create lines for each language's strings
377 #
378 for IndexI in range(len(UniObjectClass.LanguageDef)):
379 Language = UniObjectClass.LanguageDef[IndexI][0]
380 if Language not in UniLanguageListFiltered:
381 continue
382
383 StringBuffer = StringIO()
384 StrStringValue = ''
385 ArrayLength = 0
386 NumberOfUseOtherLangDef = 0
387 Index = 0
388 for IndexJ in range(1, len(UniObjectClass.OrderedStringList[UniObjectClass.LanguageDef[IndexI][0]])):
389 Item = UniObjectClass.OrderedStringListByToken[Language][IndexJ]
390
391 Name = Item.StringName
392 Value = Item.StringValueByteList
393 Referenced = Item.Referenced
394 Token = Item.Token
395 UseOtherLangDef = Item.UseOtherLangDef
396
397 if UseOtherLangDef != '' and Referenced:
398 NumberOfUseOtherLangDef = NumberOfUseOtherLangDef + 1
399 Index = Index + 1
400 else:
401 if NumberOfUseOtherLangDef > 0:
402 StrStringValue = WriteLine(StrStringValue, CreateArrayItem([StringSkipType] + DecToHexList(NumberOfUseOtherLangDef, 4)))
403 CreateBinBuffer (StringBuffer, ([StringSkipType] + DecToHexList(NumberOfUseOtherLangDef, 4)))
404 NumberOfUseOtherLangDef = 0
405 ArrayLength = ArrayLength + 3
406 if Referenced and Item.Token > 0:
407 Index = Index + 1
408 StrStringValue = WriteLine(StrStringValue, "// %s: %s:%s" % (DecToHexStr(Index, 4), Name, DecToHexStr(Token, 4)))
409 StrStringValue = Write(StrStringValue, CreateCFileStringValue(Value))
410 CreateBinBuffer (StringBuffer, [StringBlockType] + Value)
411 ArrayLength = ArrayLength + Item.Length + 1 # 1 is for the length of string type
412
413 #
414 # EFI_HII_PACKAGE_HEADER
415 #
416 Offset = EFI_HII_STRING_PACKAGE_HDR_LENGTH + len(Language) + 1
417 ArrayLength = Offset + ArrayLength + 1
418
419 #
420 # Create PACKAGE HEADER
421 #
422 Str = WriteLine(Str, '// PACKAGE HEADER\n')
423 TotalLength = TotalLength + ArrayLength
424
425 List = DecToHexList(ArrayLength, 6) + \
426 [StringPackageType] + \
427 DecToHexList(Offset) + \
428 DecToHexList(Offset) + \
429 DecToHexList(EFI_HII_LANGUAGE_WINDOW, EFI_HII_LANGUAGE_WINDOW_LENGTH * 2) * EFI_HII_LANGUAGE_WINDOW_NUMBER + \
430 DecToHexList(EFI_STRING_ID, 4) + \
431 AscToHexList(Language) + \
432 DecToHexList(0, 2)
433 Str = WriteLine(Str, CreateArrayItem(List, 16) + '\n')
434
435 #
436 # Create PACKAGE DATA
437 #
438 Str = WriteLine(Str, '// PACKAGE DATA\n')
439 Str = Write(Str, StrStringValue)
440
441 #
442 # Add an EFI_HII_SIBT_END at last
443 #
444 Str = WriteLine(Str, ' ' + EFI_HII_SIBT_END + ",")
445
446 #
447 # Create binary UNI string
448 #
449 if UniBinBuffer:
450 CreateBinBuffer (UniBinBuffer, List)
451 UniBinBuffer.write (StringBuffer.getvalue())
452 UniBinBuffer.write (pack("B", int(EFI_HII_SIBT_END,16)))
453 StringBuffer.close()
454
455 #
456 # Create line for string variable name
457 # "unsigned char $(BaseName)Strings[] = {"
458 #
459 AllStr = WriteLine('', CHAR_ARRAY_DEFIN + ' ' + BaseName + COMMON_FILE_NAME + '[] = {\n' )
460
461 if IsCompatibleMode:
462 #
463 # Create FRAMEWORK_EFI_HII_PACK_HEADER in compatible mode
464 #
465 AllStr = WriteLine(AllStr, '// FRAMEWORK PACKAGE HEADER Length')
466 AllStr = WriteLine(AllStr, CreateArrayItem(DecToHexList(TotalLength + 2)) + '\n')
467 AllStr = WriteLine(AllStr, '// FRAMEWORK PACKAGE HEADER Type')
468 AllStr = WriteLine(AllStr, CreateArrayItem(DecToHexList(2, 4)) + '\n')
469 else:
470 #
471 # Create whole array length in UEFI mode
472 #
473 AllStr = WriteLine(AllStr, '// STRGATHER_OUTPUT_HEADER')
474 AllStr = WriteLine(AllStr, CreateArrayItem(DecToHexList(TotalLength)) + '\n')
475
476 #
477 # Join package data
478 #
479 AllStr = Write(AllStr, Str)
480
481 return AllStr
482
483 ## Create end of .c file
484 #
485 # Create end of .c file
486 #
487 # @retval Str: A string of .h file end
488 #
489 def CreateCFileEnd():
490 Str = Write('', '};')
491 return Str
492
493 ## Create a .c file
494 #
495 # Create a complete .c file
496 #
497 # @param BaseName: The basename of strings
498 # @param UniObjectClass A UniObjectClass instance
499 # @param IsCompatibleMode Compatible Mode
500 # @param FilterInfo Platform language filter information
501 #
502 # @retval CFile: A string of complete .c file
503 #
504 def CreateCFile(BaseName, UniObjectClass, IsCompatibleMode, FilterInfo):
505 CFile = ''
506 #CFile = WriteLine(CFile, CreateCFileHeader())
507 CFile = WriteLine(CFile, CreateCFileContent(BaseName, UniObjectClass, IsCompatibleMode, None, FilterInfo))
508 CFile = WriteLine(CFile, CreateCFileEnd())
509 return CFile
510
511 ## GetFileList
512 #
513 # Get a list for all files
514 #
515 # @param IncludeList: A list of all path to be searched
516 # @param SkipList: A list of all types of file could be skipped
517 #
518 # @retval FileList: A list of all files found
519 #
520 def GetFileList(SourceFileList, IncludeList, SkipList):
521 if IncludeList == None:
522 EdkLogger.error("UnicodeStringGather", AUTOGEN_ERROR, "Include path for unicode file is not defined")
523
524 FileList = []
525 if SkipList == None:
526 SkipList = []
527
528 for File in SourceFileList:
529 for Dir in IncludeList:
530 if not os.path.exists(Dir):
531 continue
532 File = os.path.join(Dir, File.Path)
533 #
534 # Ignore Dir
535 #
536 if os.path.isfile(File) != True:
537 continue
538 #
539 # Ignore file listed in skip list
540 #
541 IsSkip = False
542 for Skip in SkipList:
543 if os.path.splitext(File)[1].upper() == Skip.upper():
544 EdkLogger.verbose("Skipped %s for string token uses search" % File)
545 IsSkip = True
546 break
547
548 if not IsSkip:
549 FileList.append(File)
550
551 break
552
553 return FileList
554
555 ## SearchString
556 #
557 # Search whether all string defined in UniObjectClass are referenced
558 # All string used should be set to Referenced
559 #
560 # @param UniObjectClass: Input UniObjectClass
561 # @param FileList: Search path list
562 # @param IsCompatibleMode Compatible Mode
563 #
564 # @retval UniObjectClass: UniObjectClass after searched
565 #
566 def SearchString(UniObjectClass, FileList, IsCompatibleMode):
567 if FileList == []:
568 return UniObjectClass
569
570 for File in FileList:
571 if os.path.isfile(File):
572 Lines = open(File, 'r')
573 for Line in Lines:
574 if not IsCompatibleMode:
575 StringTokenList = STRING_TOKEN.findall(Line)
576 else:
577 StringTokenList = COMPATIBLE_STRING_TOKEN.findall(Line)
578 for StrName in StringTokenList:
579 EdkLogger.debug(EdkLogger.DEBUG_5, "Found string identifier: " + StrName)
580 UniObjectClass.SetStringReferenced(StrName)
581
582 UniObjectClass.ReToken()
583
584 return UniObjectClass
585
586 ## GetStringFiles
587 #
588 # This function is used for UEFI2.1 spec
589 #
590 #
591 def GetStringFiles(UniFilList, SourceFileList, IncludeList, IncludePathList, SkipList, BaseName, IsCompatibleMode = False, ShellMode = False, UniGenCFlag = True, UniGenBinBuffer = None, FilterInfo = [True, []]):
592 if len(UniFilList) > 0:
593 if ShellMode:
594 #
595 # support ISO 639-2 codes in .UNI files of EDK Shell
596 #
597 Uni = UniFileClassObject(sorted (UniFilList), True, IncludePathList)
598 else:
599 Uni = UniFileClassObject(sorted (UniFilList), IsCompatibleMode, IncludePathList)
600 else:
601 EdkLogger.error("UnicodeStringGather", AUTOGEN_ERROR, 'No unicode files given')
602
603 FileList = GetFileList(SourceFileList, IncludeList, SkipList)
604
605 Uni = SearchString(Uni, sorted (FileList), IsCompatibleMode)
606
607 HFile = CreateHFile(BaseName, Uni, IsCompatibleMode, UniGenCFlag)
608 CFile = None
609 if IsCompatibleMode or UniGenCFlag:
610 CFile = CreateCFile(BaseName, Uni, IsCompatibleMode, FilterInfo)
611 if UniGenBinBuffer:
612 CreateCFileContent(BaseName, Uni, IsCompatibleMode, UniGenBinBuffer, FilterInfo)
613
614 return HFile, CFile
615
616 #
617 # Write an item
618 #
619 def Write(Target, Item):
620 return ''.join([Target,Item])
621
622 #
623 # Write an item with a break line
624 #
625 def WriteLine(Target, Item):
626 return ''.join([Target,Item,'\n'])
627
628 # This acts like the main() function for the script, unless it is 'import'ed into another
629 # script.
630 if __name__ == '__main__':
631 EdkLogger.info('start')
632
633 UniFileList = [
634 r'C:\\Edk\\Strings2.uni',
635 r'C:\\Edk\\Strings.uni'
636 ]
637
638 SrcFileList = []
639 for Root, Dirs, Files in os.walk('C:\\Edk'):
640 for File in Files:
641 SrcFileList.append(File)
642
643 IncludeList = [
644 r'C:\\Edk'
645 ]
646
647 SkipList = ['.inf', '.uni']
648 BaseName = 'DriverSample'
649 (h, c) = GetStringFiles(UniFileList, SrcFileList, IncludeList, SkipList, BaseName, True)
650 hfile = open('unistring.h', 'w')
651 cfile = open('unistring.c', 'w')
652 hfile.write(h)
653 cfile.write(c)
654
655 EdkLogger.info('end')