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