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