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
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
11 #This file is used to parse a strings file and create or add to a string database file.
\r
15 from UniClassObject import *
\r
16 from BuildToolError import *
\r
21 DEFINE_STR = '#define'
\r
22 COMMENT_DEFINE_STR = COMMENT + DEFINE_STR
\r
23 NOT_REFERENCED = 'not referenced'
\r
24 COMMENT_NOT_REFERENCED = ' ' + COMMENT + NOT_REFERENCED
\r
25 CHAR_ARRAY_DEFIN = 'unsigned char'
\r
26 COMMON_FILE_NAME = 'Strings'
\r
30 STRING_TOKEN = 'STRING_TOKEN'
\r
32 LENGTH_EFI_HII_STRING_PACK_HEADER = 22
\r
33 LENGTH_STRING_OFFSET = 4
\r
35 H_C_FILE_HEADER = ['//', \
\r
36 '// DO NOT EDIT -- auto-generated file', \
\r
38 '// This file is generated by the string gather utility', \
\r
40 LANGUAGE_NAME_STRING_NAME = '$LANGUAGE_NAME'
\r
41 PRINTABLE_LANGUAGE_NAME_STRING_NAME = '$PRINTABLE_LANGUAGE_NAME'
\r
43 def DecToHexStr(Dec, Digit):
\r
44 return eval("'0x%0" + str(Digit) + "X' % int(Dec)")
\r
46 def DecToHexList(Dec):
\r
47 Hex = "%08X" % int(Dec)
\r
48 List = [HexHeader + Hex[6:8], HexHeader + Hex[4:6], HexHeader + Hex[2:4], HexHeader + Hex[0:2]]
\r
51 def CreateHFileHeader(BaseName):
\r
53 for Item in H_C_FILE_HEADER:
\r
54 Str = WriteLine(Str, Item)
\r
55 Str = WriteLine(Str, '#ifndef _' + BaseName.upper() + '_STRINGS_DEFINE_H_')
\r
56 Str = WriteLine(Str, '#define _' + BaseName.upper() + '_STRINGS_DEFINE_H_')
\r
59 def CreateHFileContent(BaseName, UniObjectClass):
\r
62 Line = COMMENT_DEFINE_STR + ' ' + LANGUAGE_NAME_STRING_NAME + ' ' * (ValueStartPtr - len(DEFINE_STR + LANGUAGE_NAME_STRING_NAME)) + DecToHexStr(0, 4) + COMMENT_NOT_REFERENCED
\r
63 Str = WriteLine(Str, Line)
\r
64 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
65 Str = WriteLine(Str, Line)
\r
66 for Index in range(2, len(UniObjectClass.OrderedStringList[UniObjectClass.LanguageDef[0][0]])):
\r
67 StringItem = UniObjectClass.OrderedStringList[UniObjectClass.LanguageDef[0][0]][Index]
\r
68 Name = StringItem.StringName
\r
69 Token = StringItem.Token
\r
70 Referenced = StringItem.Referenced
\r
73 if Referenced == True:
\r
74 Line = DEFINE_STR + ' ' + Name + ' ' * (ValueStartPtr - len(DEFINE_STR + Name)) + DecToHexStr(Token, 4)
\r
76 Line = COMMENT_DEFINE_STR + ' ' + Name + ' ' * (ValueStartPtr - len(DEFINE_STR + Name)) + DecToHexStr(Token, 4) + COMMENT_NOT_REFERENCED
\r
77 Str = WriteLine(Str, Line)
\r
79 Str = WriteLine(Str, '')
\r
80 Str = WriteLine(Str, 'extern unsigned char ' + BaseName + 'Strings[];')
\r
83 def CreateHFile(BaseName, UniObjectClass):
\r
84 #HFile = WriteLine('', CreateHFileHeader(BaseName))
\r
85 HFile = WriteLine('', CreateHFileContent(BaseName, UniObjectClass))
\r
86 #HFile = WriteLine(HFile, '#endif')
\r
89 def CreateCFileHeader():
\r
91 for Item in H_C_FILE_HEADER:
\r
92 Str = WriteLine(Str, Item)
\r
96 def CreateArrayItem(Value, Width = 16):
\r
103 if Index < MaxLength:
\r
104 Line = Line + Item + ', '
\r
107 ArrayItem = WriteLine(ArrayItem, Line)
\r
108 Line = ' ' + Item + ', '
\r
110 ArrayItem = Write(ArrayItem, Line.rstrip())
\r
114 def CreateCFileStringDef(Name, Offset, Token, UseOtherLangDef = ''):
\r
116 if UseOtherLangDef != '':
\r
117 Comment = ' - not defined for this language -- using secondary language ' + UniToStr(UseOtherLangDef) + ' definition'
\r
118 Str = Write('', CreateArrayItem(DecToHexList(Offset)) + ' ' + COMMENT + 'offset to string ' + Name + ' (' + DecToHexStr(Token, 4) + ')' + Comment)
\r
122 def CreateCFileStringValue(Name, Language, Value, Offset):
\r
123 Str = WriteLine(' ', COMMENT + STRING + ' ' + Name + ' ' + OFFSET + ' ' + DecToHexStr(Offset, 8))
\r
124 Str = WriteLine(Str, CreateArrayItem(Value))
\r
128 def CreateCFileContent(BaseName, UniObjectClass):
\r
131 Str = WriteLine(Str, CHAR_ARRAY_DEFIN + ' ' + BaseName + COMMON_FILE_NAME + '[] = {\n' )
\r
132 for IndexI in range(len(UniObjectClass.LanguageDef)):
\r
133 Language = UniObjectClass.LanguageDef[IndexI][0]
\r
134 LangPrintName = UniObjectClass.LanguageDef[IndexI][1]
\r
138 for Item in UniObjectClass.OrderedStringList[Language]:
\r
139 if Item.Referenced:
\r
140 Length = Length + Item.Length
\r
143 Str = WriteLine(Str, '//******************************************************************************')
\r
144 Str = WriteLine(Str, COMMENT + 'Start of string definitions for ' + Language)
\r
147 # EFI_HII_STRING_PACK_HEADER
\r
149 Offset = LENGTH_EFI_HII_STRING_PACK_HEADER + LENGTH_STRING_OFFSET * Count
\r
150 Length = Offset + Length
\r
151 List = DecToHexList(Length) + DecToHexList(2)[0:2] + DecToHexList(Offset) + DecToHexList(Offset + len(UniToHexList(Language)) + 2) + DecToHexList(Count) + DecToHexList(0)
\r
152 Str = WriteLine(Str, CreateArrayItem(List, 8))
\r
154 Str = WriteLine(Str, ' // offset 0x16')
\r
156 StrStringValue = ''
\r
157 for Item in UniObjectClass.OrderedStringList[Language]:
\r
158 Name = Item.StringName
\r
159 Value = Item.StringValueByteList
\r
160 Referenced = Item.Referenced
\r
162 Length = Item.Length
\r
163 UseOtherLangDef = Item.UseOtherLangDef
\r
166 StrStringDef = WriteLine(StrStringDef, CreateCFileStringDef(Name, Offset, Token, UseOtherLangDef))
\r
167 StrStringValue = Write(StrStringValue, CreateCFileStringValue(Name, Language, Value, Offset))
\r
168 Offset = Offset + Length
\r
170 StrStringDef = WriteLine(StrStringDef, ' ' + COMMENT + Name + ' ' + NOT_REFERENCED)
\r
172 Str = WriteLine(Str, StrStringDef)
\r
173 Str = WriteLine(Str, StrStringValue)
\r
176 def CreateCFileEnd():
\r
177 Str = WriteLine(' ', '// strings terminator pack')
\r
178 Str = WriteLine(Str, ' ' + '0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, ')
\r
179 Str = WriteLine(Str, ' ' + '0x00, 0x00, 0x00, 0x00, 0x00, 0x00, ')
\r
180 Str = Write(Str, '};')
\r
183 def CreateCFile(BaseName, UniObjectClass):
\r
184 #CFile = WriteLine('', CreateCFileHeader())
\r
185 CFile = WriteLine('', CreateCFileContent(BaseName, UniObjectClass))
\r
186 CFile = WriteLine(CFile, CreateCFileEnd())
\r
189 def GetFileList(IncludeList, SkipList):
\r
190 if IncludeList == None:
\r
191 raise AutoGenError("Include path for unicode file is not defined")
\r
194 if SkipList == None:
\r
197 for Dir in IncludeList:
\r
198 for File in os.listdir(Dir):
\r
199 File = os.path.join(Dir, os.path.normcase(File))
\r
203 if os.path.isfile(File) != True:
\r
206 # Ignore file listed in skip list
\r
209 for Skip in SkipList:
\r
210 if os.path.splitext(File)[1].upper() == Skip.upper():
\r
215 FileList.append(File)
\r
219 def SearchString(UniObjectClass, FileList):
\r
221 return UniObjectClass
\r
223 for File in FileList:
\r
224 if os.path.isfile(File):
\r
225 Lines = open(File, 'r')
\r
227 if Line.find(STRING_TOKEN) > 0:
\r
228 Line = Line[Line.find(STRING_TOKEN) : ]
\r
229 StringList = Line.split(STRING_TOKEN)
\r
230 for Line in StringList:
\r
231 Line = Line.strip()
\r
232 StrName = Line[Line.find('(') + len('(') : Line.find(')')].strip()
\r
233 UniObjectClass.SetStringReferenced(StrName)
\r
235 UniObjectClass.ReToken()
\r
237 return UniObjectClass
\r
239 def GetStringFiles(UniFilList, IncludeList, SkipList, BaseName):
\r
243 if len(UniFilList) > 0:
\r
244 Uni = UniFileClassObject(UniFilList)
\r
246 raise AutoGenError('No unicode files given')
\r
248 FileList = GetFileList(IncludeList, SkipList)
\r
250 Uni = SearchString(Uni, FileList)
\r
252 HFile = CreateHFile(BaseName, Uni)
\r
253 CFile = CreateCFile(BaseName, Uni)
\r
255 return HFile, CFile
\r
257 def Write(Target, Item):
\r
258 return Target + Item
\r
260 def WriteLine(Target, Item):
\r
261 return Target + Item + '\n'
\r
263 # This acts like the main() function for the script, unless it is 'import'ed into another
\r
265 if __name__ == '__main__':
\r
266 EdkLogger.info('start')
\r
268 UniFileList = ['C:\\Tiano\\Edk\\Sample\\Universal\\UserInterface\\SetupBrowser\\Dxe\\DriverSample\\inventorystrings.uni', 'C:\\Tiano\\Edk\\Sample\\Universal\\UserInterface\\SetupBrowser\\Dxe\\DriverSample\\VfrStrings.uni']
\r
269 IncludeList = ['C:\\Tiano\\Edk\\Sample\\Universal\\UserInterface\\SetupBrowser\\Dxe\\DriverSample']
\r
270 # UniFileList = (['C:\\Tiano\\Edk\\Sample\\Platform\\Generic\\Dxe\\PlatformBds\\DeviceMngr\\DeviceManagerStrings.uni'])
\r
271 # UniFileList.append('C:\\Tiano\\Edk\\Sample\\Platform\\Generic\\Dxe\\PlatformBds\\BootMngr\\BootManagerStrings.uni')
\r
272 # UniFileList.append('C:\\Tiano\\Edk\\Sample\\Platform\\Generic\\Dxe\\PlatformBds\\Strings.uni')
\r
273 # UniFileList.append('C:\\Tiano\\Edk\\Sample\\Platform\\Generic\\Dxe\\PlatformBds\\FrontPageStrings.uni')
\r
275 # IncludeList = ['C:\\Tiano\\Edk\\Sample\\Platform\\Generic\\']
\r
276 # IncludeList.append('C:\\Tiano\\Edk\\Sample\\Platform\\Generic\\Dxe\\PlatformBds\\BootMngr\\')
\r
277 # IncludeList.append('C:\\Tiano\\Edk\\Sample\\Platform\\Generic\\Dxe\\')
\r
278 # IncludeList.append('C:\\Tiano\\Edk\\Sample\\Platform\\Generic\\Dxe\\PlatformBds\\')
\r
279 # IncludeList.append('C:\\Tiano\\Edk\\Sample\\Platform\\Generic\\Dxe\\PlatformBds\\DeviceMngr\\')
\r
282 SkipList = ['.inf', '.uni']
\r
283 BaseName = 'SetupBrowser'
\r
284 (h, c) = GetStringFiles(UniFileList, IncludeList, SkipList, BaseName)
\r
285 hfile = open('C:\string.h', 'w')
\r
286 cfile = open('C:\string.c', 'w')
\r
290 EdkLogger.info('end')
\r