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