]> git.proxmox.com Git - mirror_edk2.git/blame - BaseTools/Source/Python/AutoGen/StrGather.py
Sync basetools' source and binary files with r1707 of the basetools project.
[mirror_edk2.git] / BaseTools / Source / Python / AutoGen / StrGather.py
CommitLineData
30fdf114
LG
1# Copyright (c) 2007, Intel Corporation\r
2# All rights reserved. This program and the accompanying materials\r
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
21\r
22##\r
23# Static definitions\r
24#\r
25EFI_HII_SIBT_END = '0x00'\r
26EFI_HII_SIBT_STRING_SCSU = '0x10'\r
27EFI_HII_SIBT_STRING_SCSU_FONT = '0x11'\r
28EFI_HII_SIBT_STRINGS_SCSU = '0x12'\r
29EFI_HII_SIBT_STRINGS_SCSU_FONT = '0x13'\r
30EFI_HII_SIBT_STRING_UCS2 = '0x14'\r
31EFI_HII_SIBT_STRING_UCS2_FONT = '0x15'\r
32EFI_HII_SIBT_STRINGS_UCS2 = '0x16'\r
33EFI_HII_SIBT_STRINGS_UCS2_FONT = '0x17'\r
34EFI_HII_SIBT_DUPLICATE = '0x20'\r
35EFI_HII_SIBT_SKIP2 = '0x21'\r
36EFI_HII_SIBT_SKIP1 = '0x22'\r
37EFI_HII_SIBT_EXT1 = '0x30'\r
38EFI_HII_SIBT_EXT2 = '0x31'\r
39EFI_HII_SIBT_EXT4 = '0x32'\r
40EFI_HII_SIBT_FONT = '0x40'\r
41\r
42EFI_HII_PACKAGE_STRINGS = '0x04'\r
43EFI_HII_PACKAGE_FORM = '0x02'\r
44\r
45StringPackageType = EFI_HII_PACKAGE_STRINGS\r
46StringPackageForm = EFI_HII_PACKAGE_FORM\r
47StringBlockType = EFI_HII_SIBT_STRING_UCS2\r
48StringSkipType = EFI_HII_SIBT_SKIP2\r
49\r
50HexHeader = '0x'\r
51\r
52COMMENT = '// '\r
53DEFINE_STR = '#define'\r
54COMMENT_DEFINE_STR = COMMENT + DEFINE_STR\r
55NOT_REFERENCED = 'not referenced'\r
56COMMENT_NOT_REFERENCED = ' ' + COMMENT + NOT_REFERENCED\r
57CHAR_ARRAY_DEFIN = 'unsigned char'\r
58COMMON_FILE_NAME = 'Strings'\r
59OFFSET = 'offset'\r
60STRING = 'string'\r
61TO = 'to'\r
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
97 return eval("'0x%0" + str(Digit) + "X' % int(Dec)")\r
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
112 Hex = eval("'%0" + str(Digit) + "X' % int(Dec)" )\r
113 List = []\r
114 for Bit in range(Digit - 2, -1, -2):\r
115 List.append(HexHeader + Hex[Bit:Bit + 2])\r
116 return List\r
117\r
118## Convert a acsii string to a hex list\r
119#\r
120# Convert a acsii string to a formatted hex list\r
121# AscToHexList('en-US') is ['0x65', '0x6E', '0x2D', '0x55', '0x53']\r
122#\r
123# @param Ascii: The acsii string\r
124#\r
125# @retval: A list for formatted hex string\r
126#\r
127def AscToHexList(Ascii):\r
128 List = []\r
129 for Item in Ascii:\r
130 List.append('0x%2X' % ord(Item))\r
131\r
132 return List\r
133\r
134## Create header of .h file\r
135#\r
136# Create a header of .h file\r
137#\r
138# @param BaseName: The basename of strings\r
139#\r
140# @retval Str: A string for .h file header\r
141#\r
142def CreateHFileHeader(BaseName):\r
143 Str = ''\r
144 for Item in H_C_FILE_HEADER:\r
145 Str = WriteLine(Str, Item)\r
146 Str = WriteLine(Str, '#ifndef _' + BaseName.upper() + '_STRINGS_DEFINE_H_')\r
147 Str = WriteLine(Str, '#define _' + BaseName.upper() + '_STRINGS_DEFINE_H_')\r
148 return Str\r
149\r
150## Create content of .h file\r
151#\r
152# Create content of .h file\r
153#\r
154# @param BaseName: The basename of strings\r
155# @param UniObjectClass: A UniObjectClass instance\r
156#\r
157# @retval Str: A string of .h file content\r
158#\r
159def CreateHFileContent(BaseName, UniObjectClass):\r
160 Str = ''\r
161 ValueStartPtr = 60\r
162 Line = COMMENT_DEFINE_STR + ' ' + LANGUAGE_NAME_STRING_NAME + ' ' * (ValueStartPtr - len(DEFINE_STR + LANGUAGE_NAME_STRING_NAME)) + DecToHexStr(0, 4) + COMMENT_NOT_REFERENCED\r
163 Str = WriteLine(Str, Line)\r
164 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
165 Str = WriteLine(Str, Line)\r
166 for Index in range(2, len(UniObjectClass.OrderedStringList[UniObjectClass.LanguageDef[0][0]])):\r
167 StringItem = UniObjectClass.OrderedStringList[UniObjectClass.LanguageDef[0][0]][Index]\r
168 Name = StringItem.StringName\r
169 Token = StringItem.Token\r
170 Referenced = StringItem.Referenced\r
171 if Name != None:\r
172 Line = ''\r
173 if Referenced == True:\r
fd171542 174 if (ValueStartPtr - len(DEFINE_STR + Name)) <= 0:\r
175 Line = DEFINE_STR + ' ' + Name + ' ' + DecToHexStr(Token, 4)\r
176 else:\r
177 Line = DEFINE_STR + ' ' + Name + ' ' * (ValueStartPtr - len(DEFINE_STR + Name)) + DecToHexStr(Token, 4)\r
30fdf114 178 else:\r
fd171542 179 if (ValueStartPtr - len(DEFINE_STR + Name)) <= 0:\r
180 Line = COMMENT_DEFINE_STR + ' ' + Name + ' ' + DecToHexStr(Token, 4) + COMMENT_NOT_REFERENCED\r
181 else:\r
182 Line = COMMENT_DEFINE_STR + ' ' + Name + ' ' * (ValueStartPtr - len(DEFINE_STR + Name)) + DecToHexStr(Token, 4) + COMMENT_NOT_REFERENCED\r
30fdf114
LG
183 Str = WriteLine(Str, Line)\r
184\r
185 Str = WriteLine(Str, '')\r
186 Str = WriteLine(Str, 'extern unsigned char ' + BaseName + 'Strings[];')\r
187 return Str\r
188\r
189## Create a complete .h file\r
190#\r
191# Create a complet .h file with file header and file content\r
192#\r
193# @param BaseName: The basename of strings\r
194# @param UniObjectClass: A UniObjectClass instance\r
195#\r
196# @retval Str: A string of complete .h file\r
197#\r
198def CreateHFile(BaseName, UniObjectClass):\r
199 HFile = WriteLine('', CreateHFileContent(BaseName, UniObjectClass))\r
200\r
201 return HFile\r
202\r
203## Create header of .c file\r
204#\r
205# Create a header of .c file\r
206#\r
207# @retval Str: A string for .c file header\r
208#\r
209def CreateCFileHeader():\r
210 Str = ''\r
211 for Item in H_C_FILE_HEADER:\r
212 Str = WriteLine(Str, Item)\r
213\r
214 return Str\r
215\r
216## Create a formatted string all items in an array\r
217#\r
218# Use ',' to join each item in an array, and break an new line when reaching the width (default is 16)\r
219#\r
220# @param Array: The array need to be formatted\r
221# @param Width: The line length, the default value is set to 16\r
222#\r
223# @retval ArrayItem: A string for all formatted array items\r
224#\r
225def CreateArrayItem(Array, Width = 16):\r
226 MaxLength = Width\r
227 Index = 0\r
228 Line = ' '\r
229 ArrayItem = ''\r
230\r
231 for Item in Array:\r
232 if Index < MaxLength:\r
233 Line = Line + Item + ', '\r
234 Index = Index + 1\r
235 else:\r
236 ArrayItem = WriteLine(ArrayItem, Line)\r
237 Line = ' ' + Item + ', '\r
238 Index = 1\r
239 ArrayItem = Write(ArrayItem, Line.rstrip())\r
240\r
241 return ArrayItem\r
242\r
243## CreateCFileStringValue\r
244#\r
245# Create a line with string value\r
246#\r
247# @param Value: Value of the string\r
248#\r
249# @retval Str: A formatted string with string value\r
250#\r
251\r
252def CreateCFileStringValue(Value):\r
253 Value = [StringBlockType] + Value\r
254 Str = WriteLine('', CreateArrayItem(Value))\r
255\r
256 return Str\r
257\r
258\r
259## Create content of .c file\r
260#\r
261# Create content of .c file\r
262#\r
263# @param BaseName: The basename of strings\r
264# @param UniObjectClass: A UniObjectClass instance\r
265#\r
266# @retval Str: A string of .c file content\r
267#\r
268def CreateCFileContent(BaseName, UniObjectClass, IsCompatibleMode):\r
269 #\r
270 # Init array length\r
271 #\r
272 TotalLength = EFI_HII_ARRAY_SIZE_LENGTH\r
273 Str = ''\r
274 Offset = 0\r
275\r
276 #\r
277 # Create lines for each language's strings\r
278 #\r
279 for IndexI in range(len(UniObjectClass.LanguageDef)):\r
280 Language = UniObjectClass.LanguageDef[IndexI][0]\r
281 LangPrintName = UniObjectClass.LanguageDef[IndexI][1]\r
282\r
283 StrStringValue = ''\r
284 ArrayLength = 0\r
285 NumberOfUseOhterLangDef = 0\r
286 Index = 0\r
287 for IndexJ in range(1, len(UniObjectClass.OrderedStringList[UniObjectClass.LanguageDef[IndexI][0]])):\r
288 Item = UniObjectClass.FindByToken(IndexJ, Language)\r
289 Name = Item.StringName\r
290 Value = Item.StringValueByteList\r
291 Referenced = Item.Referenced\r
292 Token = Item.Token\r
293 Length = Item.Length\r
294 UseOtherLangDef = Item.UseOtherLangDef\r
295\r
296 if UseOtherLangDef != '' and Referenced:\r
297 NumberOfUseOhterLangDef = NumberOfUseOhterLangDef + 1\r
298 Index = Index + 1\r
299 else:\r
300 if NumberOfUseOhterLangDef > 0:\r
301 StrStringValue = WriteLine(StrStringValue, CreateArrayItem([StringSkipType] + DecToHexList(NumberOfUseOhterLangDef, 4)))\r
302 NumberOfUseOhterLangDef = 0\r
303 ArrayLength = ArrayLength + 3\r
304 if Referenced and Item.Token > 0:\r
305 Index = Index + 1\r
306 StrStringValue = WriteLine(StrStringValue, "// %s: %s:%s" % (DecToHexStr(Index, 4), Name, DecToHexStr(Token, 4)))\r
307 StrStringValue = Write(StrStringValue, CreateCFileStringValue(Value))\r
308 Offset = Offset + Length\r
309 ArrayLength = ArrayLength + Item.Length + 1 # 1 is for the length of string type\r
310\r
311 #\r
312 # EFI_HII_PACKAGE_HEADER\r
313 #\r
314 Offset = EFI_HII_STRING_PACKAGE_HDR_LENGTH + len(Language) + 1\r
315 ArrayLength = Offset + ArrayLength + 1\r
316\r
317 #\r
318 # Create PACKAGE HEADER\r
319 #\r
320 Str = WriteLine(Str, '// PACKAGE HEADER\n')\r
321 TotalLength = TotalLength + ArrayLength\r
322\r
323 List = DecToHexList(ArrayLength, 6) + \\r
324 [StringPackageType] + \\r
325 DecToHexList(Offset) + \\r
326 DecToHexList(Offset) + \\r
327 DecToHexList(EFI_HII_LANGUAGE_WINDOW, EFI_HII_LANGUAGE_WINDOW_LENGTH * 2) * EFI_HII_LANGUAGE_WINDOW_NUMBER + \\r
328 DecToHexList(EFI_STRING_ID, 4) + \\r
329 AscToHexList(Language) + \\r
330 DecToHexList(0, 2)\r
331 Str = WriteLine(Str, CreateArrayItem(List, 16) + '\n')\r
332\r
333 #\r
334 # Create PACKAGE DATA\r
335 #\r
336 Str = WriteLine(Str, '// PACKAGE DATA\n')\r
337 Str = Write(Str, StrStringValue)\r
338\r
339 #\r
340 # Add an EFI_HII_SIBT_END at last\r
341 #\r
342 Str = WriteLine(Str, ' ' + EFI_HII_SIBT_END + ",")\r
343\r
344 #\r
345 # Create line for string variable name\r
346 # "unsigned char $(BaseName)Strings[] = {"\r
347 #\r
348 AllStr = WriteLine('', CHAR_ARRAY_DEFIN + ' ' + BaseName + COMMON_FILE_NAME + '[] = {\n' )\r
349\r
350 #\r
351 # Create FRAMEWORK_EFI_HII_PACK_HEADER in compatible mode\r
352 #\r
353 if IsCompatibleMode:\r
354 AllStr = WriteLine(AllStr, '// FRAMEWORK PACKAGE HEADER Length')\r
355 AllStr = WriteLine(AllStr, CreateArrayItem(DecToHexList(TotalLength + 2)) + '\n')\r
356 AllStr = WriteLine(AllStr, '// FRAMEWORK PACKAGE HEADER Type')\r
357 AllStr = WriteLine(AllStr, CreateArrayItem(DecToHexList(2, 4)) + '\n')\r
358\r
359 #\r
360 # Create whole array length in UEFI mode\r
361 #\r
362 if not IsCompatibleMode:\r
363 AllStr = WriteLine(AllStr, '// STRGATHER_OUTPUT_HEADER')\r
364 AllStr = WriteLine(AllStr, CreateArrayItem(DecToHexList(TotalLength)) + '\n')\r
365\r
366 #\r
367 # Join package data\r
368 #\r
369 AllStr = Write(AllStr, Str)\r
370\r
371 return AllStr\r
372\r
373## Create end of .c file\r
374#\r
375# Create end of .c file\r
376#\r
377# @retval Str: A string of .h file end\r
378#\r
379def CreateCFileEnd():\r
380 Str = Write('', '};')\r
381 return Str\r
382\r
383## Create a .c file\r
384#\r
385# Create a complete .c file\r
386#\r
387# @param BaseName: The basename of strings\r
388# @param UniObjectClass: A UniObjectClass instance\r
389#\r
390# @retval CFile: A string of complete .c file\r
391#\r
392def CreateCFile(BaseName, UniObjectClass, IsCompatibleMode):\r
393 CFile = ''\r
394 #CFile = WriteLine(CFile, CreateCFileHeader())\r
395 CFile = WriteLine(CFile, CreateCFileContent(BaseName, UniObjectClass, IsCompatibleMode))\r
396 CFile = WriteLine(CFile, CreateCFileEnd())\r
397 return CFile\r
398\r
399## GetFileList\r
400#\r
401# Get a list for all files\r
402#\r
403# @param IncludeList: A list of all path to be searched\r
404# @param SkipList: A list of all types of file could be skipped\r
405#\r
406# @retval FileList: A list of all files found\r
407#\r
408def GetFileList(SourceFileList, IncludeList, SkipList):\r
409 if IncludeList == None:\r
410 EdkLogger.error("UnicodeStringGather", AUTOGEN_ERROR, "Include path for unicode file is not defined")\r
411\r
412 FileList = []\r
413 if SkipList == None:\r
414 SkipList = []\r
415\r
416 for File in SourceFileList:\r
417 for Dir in IncludeList:\r
418 if not os.path.exists(Dir):\r
419 continue\r
420 File = os.path.join(Dir, File.Path)\r
421 #\r
422 # Ignore Dir\r
423 #\r
424 if os.path.isfile(File) != True:\r
425 continue\r
426 #\r
427 # Ignore file listed in skip list\r
428 #\r
429 IsSkip = False\r
430 for Skip in SkipList:\r
431 if os.path.splitext(File)[1].upper() == Skip.upper():\r
432 EdkLogger.verbose("Skipped %s for string token uses search" % File)\r
433 IsSkip = True\r
434 break\r
435\r
436 if not IsSkip:\r
437 FileList.append(File)\r
438\r
439 break\r
440\r
441 return FileList\r
442\r
443## SearchString\r
444#\r
445# Search whether all string defined in UniObjectClass are referenced\r
446# All string used should be set to Referenced\r
447#\r
448# @param UniObjectClass: Input UniObjectClass\r
449# @param FileList: Search path list\r
450#\r
451# @retval UniObjectClass: UniObjectClass after searched\r
452#\r
453def SearchString(UniObjectClass, FileList):\r
454 if FileList == []:\r
455 return UniObjectClass\r
456\r
457 for File in FileList:\r
458 if os.path.isfile(File):\r
459 Lines = open(File, 'r')\r
460 for Line in Lines:\r
461 StringTokenList = STRING_TOKEN.findall(Line)\r
462 for StrName in StringTokenList:\r
463 EdkLogger.debug(EdkLogger.DEBUG_5, "Found string identifier: " + StrName)\r
464 UniObjectClass.SetStringReferenced(StrName)\r
465\r
466 UniObjectClass.ReToken()\r
467\r
468 return UniObjectClass\r
469\r
470## GetStringFiles\r
471#\r
472# This function is used for UEFI2.1 spec\r
473#\r
474#\r
475def GetStringFiles(UniFilList, SourceFileList, IncludeList, SkipList, BaseName, IsCompatibleMode = False, ShellMode = False):\r
476 Status = True\r
477 ErrorMessage = ''\r
478\r
479 if len(UniFilList) > 0:\r
480 if ShellMode:\r
481 #\r
482 # support ISO 639-2 codes in .UNI files of EDK Shell\r
483 #\r
484 Uni = UniFileClassObject(UniFilList, True)\r
485 else:\r
486 Uni = UniFileClassObject(UniFilList, IsCompatibleMode)\r
487 else:\r
488 EdkLogger.error("UnicodeStringGather", AUTOGEN_ERROR, 'No unicode files given')\r
489\r
490 FileList = GetFileList(SourceFileList, IncludeList, SkipList)\r
491\r
492 Uni = SearchString(Uni, FileList)\r
493\r
494 HFile = CreateHFile(BaseName, Uni)\r
495 CFile = CreateCFile(BaseName, Uni, IsCompatibleMode)\r
496\r
497 return HFile, CFile\r
498\r
499#\r
500# Write an item\r
501#\r
502def Write(Target, Item):\r
503 return Target + Item\r
504\r
505#\r
506# Write an item with a break line\r
507#\r
508def WriteLine(Target, Item):\r
509 return Target + Item + '\n'\r
510\r
511# This acts like the main() function for the script, unless it is 'import'ed into another\r
512# script.\r
513if __name__ == '__main__':\r
514 EdkLogger.info('start')\r
515\r
516 UniFileList = [\r
517 r'C:\\Edk\\Strings2.uni',\r
518 r'C:\\Edk\\Strings.uni'\r
519 ]\r
520\r
521 SrcFileList = []\r
522 for Root, Dirs, Files in os.walk('C:\\Edk'):\r
523 for File in Files:\r
524 SrcFileList.append(File)\r
525\r
526 IncludeList = [\r
527 r'C:\\Edk'\r
528 ]\r
529\r
530 SkipList = ['.inf', '.uni']\r
531 BaseName = 'DriverSample'\r
532 (h, c) = GetStringFiles(UniFileList, SrcFileList, IncludeList, SkipList, BaseName, True)\r
533 hfile = open('unistring.h', 'w')\r
534 cfile = open('unistring.c', 'w')\r
535 hfile.write(h)\r
536 cfile.write(c)\r
537\r
538 EdkLogger.info('end')\r