Check In tool source code based on Build tool project revision r1655.
[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
174 Line = DEFINE_STR + ' ' + Name + ' ' * (ValueStartPtr - len(DEFINE_STR + Name)) + DecToHexStr(Token, 4)\r
175 else:\r
176 Line = COMMENT_DEFINE_STR + ' ' + Name + ' ' * (ValueStartPtr - len(DEFINE_STR + Name)) + DecToHexStr(Token, 4) + COMMENT_NOT_REFERENCED\r
177 Str = WriteLine(Str, Line)\r
178\r
179 Str = WriteLine(Str, '')\r
180 Str = WriteLine(Str, 'extern unsigned char ' + BaseName + 'Strings[];')\r
181 return Str\r
182\r
183## Create a complete .h file\r
184#\r
185# Create a complet .h file with file header and file content\r
186#\r
187# @param BaseName: The basename of strings\r
188# @param UniObjectClass: A UniObjectClass instance\r
189#\r
190# @retval Str: A string of complete .h file\r
191#\r
192def CreateHFile(BaseName, UniObjectClass):\r
193 HFile = WriteLine('', CreateHFileContent(BaseName, UniObjectClass))\r
194\r
195 return HFile\r
196\r
197## Create header of .c file\r
198#\r
199# Create a header of .c file\r
200#\r
201# @retval Str: A string for .c file header\r
202#\r
203def CreateCFileHeader():\r
204 Str = ''\r
205 for Item in H_C_FILE_HEADER:\r
206 Str = WriteLine(Str, Item)\r
207\r
208 return Str\r
209\r
210## Create a formatted string all items in an array\r
211#\r
212# Use ',' to join each item in an array, and break an new line when reaching the width (default is 16)\r
213#\r
214# @param Array: The array need to be formatted\r
215# @param Width: The line length, the default value is set to 16\r
216#\r
217# @retval ArrayItem: A string for all formatted array items\r
218#\r
219def CreateArrayItem(Array, Width = 16):\r
220 MaxLength = Width\r
221 Index = 0\r
222 Line = ' '\r
223 ArrayItem = ''\r
224\r
225 for Item in Array:\r
226 if Index < MaxLength:\r
227 Line = Line + Item + ', '\r
228 Index = Index + 1\r
229 else:\r
230 ArrayItem = WriteLine(ArrayItem, Line)\r
231 Line = ' ' + Item + ', '\r
232 Index = 1\r
233 ArrayItem = Write(ArrayItem, Line.rstrip())\r
234\r
235 return ArrayItem\r
236\r
237## CreateCFileStringValue\r
238#\r
239# Create a line with string value\r
240#\r
241# @param Value: Value of the string\r
242#\r
243# @retval Str: A formatted string with string value\r
244#\r
245\r
246def CreateCFileStringValue(Value):\r
247 Value = [StringBlockType] + Value\r
248 Str = WriteLine('', CreateArrayItem(Value))\r
249\r
250 return Str\r
251\r
252\r
253## Create content of .c file\r
254#\r
255# Create content of .c file\r
256#\r
257# @param BaseName: The basename of strings\r
258# @param UniObjectClass: A UniObjectClass instance\r
259#\r
260# @retval Str: A string of .c file content\r
261#\r
262def CreateCFileContent(BaseName, UniObjectClass, IsCompatibleMode):\r
263 #\r
264 # Init array length\r
265 #\r
266 TotalLength = EFI_HII_ARRAY_SIZE_LENGTH\r
267 Str = ''\r
268 Offset = 0\r
269\r
270 #\r
271 # Create lines for each language's strings\r
272 #\r
273 for IndexI in range(len(UniObjectClass.LanguageDef)):\r
274 Language = UniObjectClass.LanguageDef[IndexI][0]\r
275 LangPrintName = UniObjectClass.LanguageDef[IndexI][1]\r
276\r
277 StrStringValue = ''\r
278 ArrayLength = 0\r
279 NumberOfUseOhterLangDef = 0\r
280 Index = 0\r
281 for IndexJ in range(1, len(UniObjectClass.OrderedStringList[UniObjectClass.LanguageDef[IndexI][0]])):\r
282 Item = UniObjectClass.FindByToken(IndexJ, Language)\r
283 Name = Item.StringName\r
284 Value = Item.StringValueByteList\r
285 Referenced = Item.Referenced\r
286 Token = Item.Token\r
287 Length = Item.Length\r
288 UseOtherLangDef = Item.UseOtherLangDef\r
289\r
290 if UseOtherLangDef != '' and Referenced:\r
291 NumberOfUseOhterLangDef = NumberOfUseOhterLangDef + 1\r
292 Index = Index + 1\r
293 else:\r
294 if NumberOfUseOhterLangDef > 0:\r
295 StrStringValue = WriteLine(StrStringValue, CreateArrayItem([StringSkipType] + DecToHexList(NumberOfUseOhterLangDef, 4)))\r
296 NumberOfUseOhterLangDef = 0\r
297 ArrayLength = ArrayLength + 3\r
298 if Referenced and Item.Token > 0:\r
299 Index = Index + 1\r
300 StrStringValue = WriteLine(StrStringValue, "// %s: %s:%s" % (DecToHexStr(Index, 4), Name, DecToHexStr(Token, 4)))\r
301 StrStringValue = Write(StrStringValue, CreateCFileStringValue(Value))\r
302 Offset = Offset + Length\r
303 ArrayLength = ArrayLength + Item.Length + 1 # 1 is for the length of string type\r
304\r
305 #\r
306 # EFI_HII_PACKAGE_HEADER\r
307 #\r
308 Offset = EFI_HII_STRING_PACKAGE_HDR_LENGTH + len(Language) + 1\r
309 ArrayLength = Offset + ArrayLength + 1\r
310\r
311 #\r
312 # Create PACKAGE HEADER\r
313 #\r
314 Str = WriteLine(Str, '// PACKAGE HEADER\n')\r
315 TotalLength = TotalLength + ArrayLength\r
316\r
317 List = DecToHexList(ArrayLength, 6) + \\r
318 [StringPackageType] + \\r
319 DecToHexList(Offset) + \\r
320 DecToHexList(Offset) + \\r
321 DecToHexList(EFI_HII_LANGUAGE_WINDOW, EFI_HII_LANGUAGE_WINDOW_LENGTH * 2) * EFI_HII_LANGUAGE_WINDOW_NUMBER + \\r
322 DecToHexList(EFI_STRING_ID, 4) + \\r
323 AscToHexList(Language) + \\r
324 DecToHexList(0, 2)\r
325 Str = WriteLine(Str, CreateArrayItem(List, 16) + '\n')\r
326\r
327 #\r
328 # Create PACKAGE DATA\r
329 #\r
330 Str = WriteLine(Str, '// PACKAGE DATA\n')\r
331 Str = Write(Str, StrStringValue)\r
332\r
333 #\r
334 # Add an EFI_HII_SIBT_END at last\r
335 #\r
336 Str = WriteLine(Str, ' ' + EFI_HII_SIBT_END + ",")\r
337\r
338 #\r
339 # Create line for string variable name\r
340 # "unsigned char $(BaseName)Strings[] = {"\r
341 #\r
342 AllStr = WriteLine('', CHAR_ARRAY_DEFIN + ' ' + BaseName + COMMON_FILE_NAME + '[] = {\n' )\r
343\r
344 #\r
345 # Create FRAMEWORK_EFI_HII_PACK_HEADER in compatible mode\r
346 #\r
347 if IsCompatibleMode:\r
348 AllStr = WriteLine(AllStr, '// FRAMEWORK PACKAGE HEADER Length')\r
349 AllStr = WriteLine(AllStr, CreateArrayItem(DecToHexList(TotalLength + 2)) + '\n')\r
350 AllStr = WriteLine(AllStr, '// FRAMEWORK PACKAGE HEADER Type')\r
351 AllStr = WriteLine(AllStr, CreateArrayItem(DecToHexList(2, 4)) + '\n')\r
352\r
353 #\r
354 # Create whole array length in UEFI mode\r
355 #\r
356 if not IsCompatibleMode:\r
357 AllStr = WriteLine(AllStr, '// STRGATHER_OUTPUT_HEADER')\r
358 AllStr = WriteLine(AllStr, CreateArrayItem(DecToHexList(TotalLength)) + '\n')\r
359\r
360 #\r
361 # Join package data\r
362 #\r
363 AllStr = Write(AllStr, Str)\r
364\r
365 return AllStr\r
366\r
367## Create end of .c file\r
368#\r
369# Create end of .c file\r
370#\r
371# @retval Str: A string of .h file end\r
372#\r
373def CreateCFileEnd():\r
374 Str = Write('', '};')\r
375 return Str\r
376\r
377## Create a .c file\r
378#\r
379# Create a complete .c file\r
380#\r
381# @param BaseName: The basename of strings\r
382# @param UniObjectClass: A UniObjectClass instance\r
383#\r
384# @retval CFile: A string of complete .c file\r
385#\r
386def CreateCFile(BaseName, UniObjectClass, IsCompatibleMode):\r
387 CFile = ''\r
388 #CFile = WriteLine(CFile, CreateCFileHeader())\r
389 CFile = WriteLine(CFile, CreateCFileContent(BaseName, UniObjectClass, IsCompatibleMode))\r
390 CFile = WriteLine(CFile, CreateCFileEnd())\r
391 return CFile\r
392\r
393## GetFileList\r
394#\r
395# Get a list for all files\r
396#\r
397# @param IncludeList: A list of all path to be searched\r
398# @param SkipList: A list of all types of file could be skipped\r
399#\r
400# @retval FileList: A list of all files found\r
401#\r
402def GetFileList(SourceFileList, IncludeList, SkipList):\r
403 if IncludeList == None:\r
404 EdkLogger.error("UnicodeStringGather", AUTOGEN_ERROR, "Include path for unicode file is not defined")\r
405\r
406 FileList = []\r
407 if SkipList == None:\r
408 SkipList = []\r
409\r
410 for File in SourceFileList:\r
411 for Dir in IncludeList:\r
412 if not os.path.exists(Dir):\r
413 continue\r
414 File = os.path.join(Dir, File.Path)\r
415 #\r
416 # Ignore Dir\r
417 #\r
418 if os.path.isfile(File) != True:\r
419 continue\r
420 #\r
421 # Ignore file listed in skip list\r
422 #\r
423 IsSkip = False\r
424 for Skip in SkipList:\r
425 if os.path.splitext(File)[1].upper() == Skip.upper():\r
426 EdkLogger.verbose("Skipped %s for string token uses search" % File)\r
427 IsSkip = True\r
428 break\r
429\r
430 if not IsSkip:\r
431 FileList.append(File)\r
432\r
433 break\r
434\r
435 return FileList\r
436\r
437## SearchString\r
438#\r
439# Search whether all string defined in UniObjectClass are referenced\r
440# All string used should be set to Referenced\r
441#\r
442# @param UniObjectClass: Input UniObjectClass\r
443# @param FileList: Search path list\r
444#\r
445# @retval UniObjectClass: UniObjectClass after searched\r
446#\r
447def SearchString(UniObjectClass, FileList):\r
448 if FileList == []:\r
449 return UniObjectClass\r
450\r
451 for File in FileList:\r
452 if os.path.isfile(File):\r
453 Lines = open(File, 'r')\r
454 for Line in Lines:\r
455 StringTokenList = STRING_TOKEN.findall(Line)\r
456 for StrName in StringTokenList:\r
457 EdkLogger.debug(EdkLogger.DEBUG_5, "Found string identifier: " + StrName)\r
458 UniObjectClass.SetStringReferenced(StrName)\r
459\r
460 UniObjectClass.ReToken()\r
461\r
462 return UniObjectClass\r
463\r
464## GetStringFiles\r
465#\r
466# This function is used for UEFI2.1 spec\r
467#\r
468#\r
469def GetStringFiles(UniFilList, SourceFileList, IncludeList, SkipList, BaseName, IsCompatibleMode = False, ShellMode = False):\r
470 Status = True\r
471 ErrorMessage = ''\r
472\r
473 if len(UniFilList) > 0:\r
474 if ShellMode:\r
475 #\r
476 # support ISO 639-2 codes in .UNI files of EDK Shell\r
477 #\r
478 Uni = UniFileClassObject(UniFilList, True)\r
479 else:\r
480 Uni = UniFileClassObject(UniFilList, IsCompatibleMode)\r
481 else:\r
482 EdkLogger.error("UnicodeStringGather", AUTOGEN_ERROR, 'No unicode files given')\r
483\r
484 FileList = GetFileList(SourceFileList, IncludeList, SkipList)\r
485\r
486 Uni = SearchString(Uni, FileList)\r
487\r
488 HFile = CreateHFile(BaseName, Uni)\r
489 CFile = CreateCFile(BaseName, Uni, IsCompatibleMode)\r
490\r
491 return HFile, CFile\r
492\r
493#\r
494# Write an item\r
495#\r
496def Write(Target, Item):\r
497 return Target + Item\r
498\r
499#\r
500# Write an item with a break line\r
501#\r
502def WriteLine(Target, Item):\r
503 return Target + Item + '\n'\r
504\r
505# This acts like the main() function for the script, unless it is 'import'ed into another\r
506# script.\r
507if __name__ == '__main__':\r
508 EdkLogger.info('start')\r
509\r
510 UniFileList = [\r
511 r'C:\\Edk\\Strings2.uni',\r
512 r'C:\\Edk\\Strings.uni'\r
513 ]\r
514\r
515 SrcFileList = []\r
516 for Root, Dirs, Files in os.walk('C:\\Edk'):\r
517 for File in Files:\r
518 SrcFileList.append(File)\r
519\r
520 IncludeList = [\r
521 r'C:\\Edk'\r
522 ]\r
523\r
524 SkipList = ['.inf', '.uni']\r
525 BaseName = 'DriverSample'\r
526 (h, c) = GetStringFiles(UniFileList, SrcFileList, IncludeList, SkipList, BaseName, True)\r
527 hfile = open('unistring.h', 'w')\r
528 cfile = open('unistring.c', 'w')\r
529 hfile.write(h)\r
530 cfile.write(c)\r
531\r
532 EdkLogger.info('end')\r