]> git.proxmox.com Git - mirror_edk2.git/blob - BaseTools/Source/Python/AutoGen/StrGather.py
Sync EDKII BaseTools to BaseTools project r2093.
[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-Za-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
172 #Group the referred STRING token together.
173 for Index in range(2, len(UniObjectClass.OrderedStringList[UniObjectClass.LanguageDef[0][0]])):
174 StringItem = UniObjectClass.OrderedStringList[UniObjectClass.LanguageDef[0][0]][Index]
175 Name = StringItem.StringName
176 Token = StringItem.Token
177 Referenced = StringItem.Referenced
178 if Name != None:
179 Line = ''
180 if Referenced == True:
181 if (ValueStartPtr - len(DEFINE_STR + Name)) <= 0:
182 Line = DEFINE_STR + ' ' + Name + ' ' + DecToHexStr(Token, 4)
183 else:
184 Line = DEFINE_STR + ' ' + Name + ' ' * (ValueStartPtr - len(DEFINE_STR + Name)) + DecToHexStr(Token, 4)
185 Str = WriteLine(Str, Line)
186
187 #Group the unused STRING token together.
188 for Index in range(2, len(UniObjectClass.OrderedStringList[UniObjectClass.LanguageDef[0][0]])):
189 StringItem = UniObjectClass.OrderedStringList[UniObjectClass.LanguageDef[0][0]][Index]
190 Name = StringItem.StringName
191 Token = StringItem.Token
192 Referenced = StringItem.Referenced
193 if Name != None:
194 Line = ''
195 if Referenced == False:
196 if (ValueStartPtr - len(DEFINE_STR + Name)) <= 0:
197 Line = COMMENT_DEFINE_STR + ' ' + Name + ' ' + DecToHexStr(Token, 4) + COMMENT_NOT_REFERENCED
198 else:
199 Line = COMMENT_DEFINE_STR + ' ' + Name + ' ' * (ValueStartPtr - len(DEFINE_STR + Name)) + DecToHexStr(Token, 4) + COMMENT_NOT_REFERENCED
200 Str = WriteLine(Str, Line)
201
202 Str = WriteLine(Str, '')
203 if IsCompatibleMode or UniGenCFlag:
204 Str = WriteLine(Str, 'extern unsigned char ' + BaseName + 'Strings[];')
205 return Str
206
207 ## Create a complete .h file
208 #
209 # Create a complet .h file with file header and file content
210 #
211 # @param BaseName: The basename of strings
212 # @param UniObjectClass A UniObjectClass instance
213 # @param IsCompatibleMode Compatible mode
214 # @param UniGenCFlag UniString is generated into AutoGen C file when it is set to True
215 #
216 # @retval Str: A string of complete .h file
217 #
218 def CreateHFile(BaseName, UniObjectClass, IsCompatibleMode, UniGenCFlag):
219 HFile = WriteLine('', CreateHFileContent(BaseName, UniObjectClass, IsCompatibleMode, UniGenCFlag))
220
221 return HFile
222
223 ## Create header of .c file
224 #
225 # Create a header of .c file
226 #
227 # @retval Str: A string for .c file header
228 #
229 def CreateCFileHeader():
230 Str = ''
231 for Item in H_C_FILE_HEADER:
232 Str = WriteLine(Str, Item)
233
234 return Str
235
236 ## Create a buffer to store all items in an array
237 #
238 # @param BinBuffer Buffer to contain Binary data.
239 # @param Array: The array need to be formatted
240 #
241 def CreateBinBuffer(BinBuffer, Array):
242 for Item in Array:
243 BinBuffer.write(pack("B", int(Item,16)))
244
245 ## Create a formatted string all items in an array
246 #
247 # Use ',' to join each item in an array, and break an new line when reaching the width (default is 16)
248 #
249 # @param Array: The array need to be formatted
250 # @param Width: The line length, the default value is set to 16
251 #
252 # @retval ArrayItem: A string for all formatted array items
253 #
254 def CreateArrayItem(Array, Width = 16):
255 MaxLength = Width
256 Index = 0
257 Line = ' '
258 ArrayItem = ''
259
260 for Item in Array:
261 if Index < MaxLength:
262 Line = Line + Item + ', '
263 Index = Index + 1
264 else:
265 ArrayItem = WriteLine(ArrayItem, Line)
266 Line = ' ' + Item + ', '
267 Index = 1
268 ArrayItem = Write(ArrayItem, Line.rstrip())
269
270 return ArrayItem
271
272 ## CreateCFileStringValue
273 #
274 # Create a line with string value
275 #
276 # @param Value: Value of the string
277 #
278 # @retval Str: A formatted string with string value
279 #
280
281 def CreateCFileStringValue(Value):
282 Value = [StringBlockType] + Value
283 Str = WriteLine('', CreateArrayItem(Value))
284
285 return Str
286
287 ## GetFilteredLanguage
288 #
289 # apply get best language rules to the UNI language code list
290 #
291 # @param UniLanguageList: language code definition list in *.UNI file
292 # @param LanguageFilterList: language code filter list of RFC4646 format in DSC file
293 #
294 # @retval UniLanguageListFiltered: the filtered language code
295 #
296 def GetFilteredLanguage(UniLanguageList, LanguageFilterList):
297 UniLanguageListFiltered = []
298 # if filter list is empty, then consider there is no filter
299 if LanguageFilterList == []:
300 UniLanguageListFiltered = UniLanguageList
301 return UniLanguageListFiltered
302 for Language in LanguageFilterList:
303 # first check for exact match
304 if Language in UniLanguageList:
305 if Language not in UniLanguageListFiltered:
306 UniLanguageListFiltered += [Language]
307 # find the first one with the same/equivalent primary tag
308 else:
309 if Language.find('-') != -1:
310 PrimaryTag = Language[0:Language.find('-')].lower()
311 else:
312 PrimaryTag = Language
313
314 if len(PrimaryTag) == 3:
315 PrimaryTag = LangConvTable.get(PrimaryTag)
316
317 for UniLanguage in UniLanguageList:
318 if UniLanguage.find('-') != -1:
319 UniLanguagePrimaryTag = UniLanguage[0:UniLanguage.find('-')].lower()
320 else:
321 UniLanguagePrimaryTag = UniLanguage
322
323 if len(UniLanguagePrimaryTag) == 3:
324 UniLanguagePrimaryTag = LangConvTable.get(UniLanguagePrimaryTag)
325
326 if PrimaryTag == UniLanguagePrimaryTag:
327 if UniLanguage not in UniLanguageListFiltered:
328 UniLanguageListFiltered += [UniLanguage]
329 break
330 else:
331 # Here is rule 3 for "get best language"
332 # If tag is not listed in the Unicode file, the default ("en") tag should be used for that language
333 # for better processing, find the one that best suit for it.
334 DefaultTag = 'en'
335 if DefaultTag not in UniLanguageListFiltered:
336 # check whether language code with primary code equivalent with DefaultTag already in the list, if so, use that
337 for UniLanguage in UniLanguageList:
338 if UniLanguage.startswith('en-') or UniLanguage.startswith('eng-'):
339 if UniLanguage not in UniLanguageListFiltered:
340 UniLanguageListFiltered += [UniLanguage]
341 break
342 else:
343 UniLanguageListFiltered += [DefaultTag]
344 return UniLanguageListFiltered
345
346
347 ## Create content of .c file
348 #
349 # Create content of .c file
350 #
351 # @param BaseName: The basename of strings
352 # @param UniObjectClass A UniObjectClass instance
353 # @param IsCompatibleMode Compatible mode
354 # @param UniBinBuffer UniBinBuffer to contain UniBinary data.
355 # @param FilterInfo Platform language filter information
356 #
357 # @retval Str: A string of .c file content
358 #
359 def CreateCFileContent(BaseName, UniObjectClass, IsCompatibleMode, UniBinBuffer, FilterInfo):
360 #
361 # Init array length
362 #
363 TotalLength = EFI_HII_ARRAY_SIZE_LENGTH
364 Str = ''
365 Offset = 0
366
367 EDK2Module = FilterInfo[0]
368 if EDK2Module:
369 LanguageFilterList = FilterInfo[1]
370 else:
371 # EDK module is using ISO639-2 format filter, convert to the RFC4646 format
372 LanguageFilterList = [LangConvTable.get(F.lower()) for F in FilterInfo[1]]
373
374 UniLanguageList = []
375 for IndexI in range(len(UniObjectClass.LanguageDef)):
376 UniLanguageList += [UniObjectClass.LanguageDef[IndexI][0]]
377
378 UniLanguageListFiltered = GetFilteredLanguage(UniLanguageList, LanguageFilterList)
379
380
381 #
382 # Create lines for each language's strings
383 #
384 for IndexI in range(len(UniObjectClass.LanguageDef)):
385 Language = UniObjectClass.LanguageDef[IndexI][0]
386 LangPrintName = UniObjectClass.LanguageDef[IndexI][1]
387 if Language not in UniLanguageListFiltered:
388 continue
389
390 StringBuffer = StringIO()
391 StrStringValue = ''
392 ArrayLength = 0
393 NumberOfUseOtherLangDef = 0
394 Index = 0
395 for IndexJ in range(1, len(UniObjectClass.OrderedStringList[UniObjectClass.LanguageDef[IndexI][0]])):
396 Item = UniObjectClass.FindByToken(IndexJ, Language)
397 Name = Item.StringName
398 Value = Item.StringValueByteList
399 Referenced = Item.Referenced
400 Token = Item.Token
401 Length = Item.Length
402 UseOtherLangDef = Item.UseOtherLangDef
403
404 if UseOtherLangDef != '' and Referenced:
405 NumberOfUseOtherLangDef = NumberOfUseOtherLangDef + 1
406 Index = Index + 1
407 else:
408 if NumberOfUseOtherLangDef > 0:
409 StrStringValue = WriteLine(StrStringValue, CreateArrayItem([StringSkipType] + DecToHexList(NumberOfUseOtherLangDef, 4)))
410 CreateBinBuffer (StringBuffer, ([StringSkipType] + DecToHexList(NumberOfUseOtherLangDef, 4)))
411 NumberOfUseOtherLangDef = 0
412 ArrayLength = ArrayLength + 3
413 if Referenced and Item.Token > 0:
414 Index = Index + 1
415 StrStringValue = WriteLine(StrStringValue, "// %s: %s:%s" % (DecToHexStr(Index, 4), Name, DecToHexStr(Token, 4)))
416 StrStringValue = Write(StrStringValue, CreateCFileStringValue(Value))
417 CreateBinBuffer (StringBuffer, [StringBlockType] + Value)
418 ArrayLength = ArrayLength + Item.Length + 1 # 1 is for the length of string type
419
420 #
421 # EFI_HII_PACKAGE_HEADER
422 #
423 Offset = EFI_HII_STRING_PACKAGE_HDR_LENGTH + len(Language) + 1
424 ArrayLength = Offset + ArrayLength + 1
425
426 #
427 # Create PACKAGE HEADER
428 #
429 Str = WriteLine(Str, '// PACKAGE HEADER\n')
430 TotalLength = TotalLength + ArrayLength
431
432 List = DecToHexList(ArrayLength, 6) + \
433 [StringPackageType] + \
434 DecToHexList(Offset) + \
435 DecToHexList(Offset) + \
436 DecToHexList(EFI_HII_LANGUAGE_WINDOW, EFI_HII_LANGUAGE_WINDOW_LENGTH * 2) * EFI_HII_LANGUAGE_WINDOW_NUMBER + \
437 DecToHexList(EFI_STRING_ID, 4) + \
438 AscToHexList(Language) + \
439 DecToHexList(0, 2)
440 Str = WriteLine(Str, CreateArrayItem(List, 16) + '\n')
441
442 #
443 # Create PACKAGE DATA
444 #
445 Str = WriteLine(Str, '// PACKAGE DATA\n')
446 Str = Write(Str, StrStringValue)
447
448 #
449 # Add an EFI_HII_SIBT_END at last
450 #
451 Str = WriteLine(Str, ' ' + EFI_HII_SIBT_END + ",")
452
453 #
454 # Create binary UNI string
455 #
456 if UniBinBuffer:
457 CreateBinBuffer (UniBinBuffer, List)
458 UniBinBuffer.write (StringBuffer.getvalue())
459 UniBinBuffer.write (pack("B", int(EFI_HII_SIBT_END,16)))
460 StringBuffer.close()
461
462 #
463 # Create line for string variable name
464 # "unsigned char $(BaseName)Strings[] = {"
465 #
466 AllStr = WriteLine('', CHAR_ARRAY_DEFIN + ' ' + BaseName + COMMON_FILE_NAME + '[] = {\n' )
467
468 if IsCompatibleMode:
469 #
470 # Create FRAMEWORK_EFI_HII_PACK_HEADER in compatible mode
471 #
472 AllStr = WriteLine(AllStr, '// FRAMEWORK PACKAGE HEADER Length')
473 AllStr = WriteLine(AllStr, CreateArrayItem(DecToHexList(TotalLength + 2)) + '\n')
474 AllStr = WriteLine(AllStr, '// FRAMEWORK PACKAGE HEADER Type')
475 AllStr = WriteLine(AllStr, CreateArrayItem(DecToHexList(2, 4)) + '\n')
476 else:
477 #
478 # Create whole array length in UEFI mode
479 #
480 AllStr = WriteLine(AllStr, '// STRGATHER_OUTPUT_HEADER')
481 AllStr = WriteLine(AllStr, CreateArrayItem(DecToHexList(TotalLength)) + '\n')
482
483 #
484 # Join package data
485 #
486 AllStr = Write(AllStr, Str)
487
488 return AllStr
489
490 ## Create end of .c file
491 #
492 # Create end of .c file
493 #
494 # @retval Str: A string of .h file end
495 #
496 def CreateCFileEnd():
497 Str = Write('', '};')
498 return Str
499
500 ## Create a .c file
501 #
502 # Create a complete .c file
503 #
504 # @param BaseName: The basename of strings
505 # @param UniObjectClass A UniObjectClass instance
506 # @param IsCompatibleMode Compatible Mode
507 # @param FilterInfo Platform language filter information
508 #
509 # @retval CFile: A string of complete .c file
510 #
511 def CreateCFile(BaseName, UniObjectClass, IsCompatibleMode, FilterInfo):
512 CFile = ''
513 #CFile = WriteLine(CFile, CreateCFileHeader())
514 CFile = WriteLine(CFile, CreateCFileContent(BaseName, UniObjectClass, IsCompatibleMode, None, FilterInfo))
515 CFile = WriteLine(CFile, CreateCFileEnd())
516 return CFile
517
518 ## GetFileList
519 #
520 # Get a list for all files
521 #
522 # @param IncludeList: A list of all path to be searched
523 # @param SkipList: A list of all types of file could be skipped
524 #
525 # @retval FileList: A list of all files found
526 #
527 def GetFileList(SourceFileList, IncludeList, SkipList):
528 if IncludeList == None:
529 EdkLogger.error("UnicodeStringGather", AUTOGEN_ERROR, "Include path for unicode file is not defined")
530
531 FileList = []
532 if SkipList == None:
533 SkipList = []
534
535 for File in SourceFileList:
536 for Dir in IncludeList:
537 if not os.path.exists(Dir):
538 continue
539 File = os.path.join(Dir, File.Path)
540 #
541 # Ignore Dir
542 #
543 if os.path.isfile(File) != True:
544 continue
545 #
546 # Ignore file listed in skip list
547 #
548 IsSkip = False
549 for Skip in SkipList:
550 if os.path.splitext(File)[1].upper() == Skip.upper():
551 EdkLogger.verbose("Skipped %s for string token uses search" % File)
552 IsSkip = True
553 break
554
555 if not IsSkip:
556 FileList.append(File)
557
558 break
559
560 return FileList
561
562 ## SearchString
563 #
564 # Search whether all string defined in UniObjectClass are referenced
565 # All string used should be set to Referenced
566 #
567 # @param UniObjectClass: Input UniObjectClass
568 # @param FileList: Search path list
569 # @param IsCompatibleMode Compatible Mode
570 #
571 # @retval UniObjectClass: UniObjectClass after searched
572 #
573 def SearchString(UniObjectClass, FileList, IsCompatibleMode):
574 if FileList == []:
575 return UniObjectClass
576
577 for File in FileList:
578 if os.path.isfile(File):
579 Lines = open(File, 'r')
580 for Line in Lines:
581 if not IsCompatibleMode:
582 StringTokenList = STRING_TOKEN.findall(Line)
583 else:
584 StringTokenList = COMPATIBLE_STRING_TOKEN.findall(Line)
585 for StrName in StringTokenList:
586 EdkLogger.debug(EdkLogger.DEBUG_5, "Found string identifier: " + StrName)
587 UniObjectClass.SetStringReferenced(StrName)
588
589 UniObjectClass.ReToken()
590
591 return UniObjectClass
592
593 ## GetStringFiles
594 #
595 # This function is used for UEFI2.1 spec
596 #
597 #
598 def GetStringFiles(UniFilList, SourceFileList, IncludeList, IncludePathList, SkipList, BaseName, IsCompatibleMode = False, ShellMode = False, UniGenCFlag = True, UniGenBinBuffer = None, FilterInfo = [True, []]):
599 Status = True
600 ErrorMessage = ''
601
602 if len(UniFilList) > 0:
603 if ShellMode:
604 #
605 # support ISO 639-2 codes in .UNI files of EDK Shell
606 #
607 Uni = UniFileClassObject(sorted (UniFilList), True, IncludePathList)
608 else:
609 Uni = UniFileClassObject(sorted (UniFilList), IsCompatibleMode, IncludePathList)
610 else:
611 EdkLogger.error("UnicodeStringGather", AUTOGEN_ERROR, 'No unicode files given')
612
613 FileList = GetFileList(SourceFileList, IncludeList, SkipList)
614
615 Uni = SearchString(Uni, sorted (FileList), IsCompatibleMode)
616
617 HFile = CreateHFile(BaseName, Uni, IsCompatibleMode, UniGenCFlag)
618 CFile = None
619 if IsCompatibleMode or UniGenCFlag:
620 CFile = CreateCFile(BaseName, Uni, IsCompatibleMode, FilterInfo)
621 if UniGenBinBuffer:
622 CreateCFileContent(BaseName, Uni, IsCompatibleMode, UniGenBinBuffer, FilterInfo)
623
624 return HFile, CFile
625
626 #
627 # Write an item
628 #
629 def Write(Target, Item):
630 return Target + Item
631
632 #
633 # Write an item with a break line
634 #
635 def WriteLine(Target, Item):
636 return Target + Item + '\n'
637
638 # This acts like the main() function for the script, unless it is 'import'ed into another
639 # script.
640 if __name__ == '__main__':
641 EdkLogger.info('start')
642
643 UniFileList = [
644 r'C:\\Edk\\Strings2.uni',
645 r'C:\\Edk\\Strings.uni'
646 ]
647
648 SrcFileList = []
649 for Root, Dirs, Files in os.walk('C:\\Edk'):
650 for File in Files:
651 SrcFileList.append(File)
652
653 IncludeList = [
654 r'C:\\Edk'
655 ]
656
657 SkipList = ['.inf', '.uni']
658 BaseName = 'DriverSample'
659 (h, c) = GetStringFiles(UniFileList, SrcFileList, IncludeList, SkipList, BaseName, True)
660 hfile = open('unistring.h', 'w')
661 cfile = open('unistring.c', 'w')
662 hfile.write(h)
663 cfile.write(c)
664
665 EdkLogger.info('end')