Home | History | Annotate | Download | only in AutoGen
      1 ## @file

      2 # This file is used to parse a strings file and create or add to a string database 

      3 # file.

      4 #

      5 # Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.<BR>

      6 # This program and the accompanying materials

      7 # are licensed and made available under the terms and conditions of the BSD License

      8 # which accompanies this distribution.  The full text of the license may be found at

      9 # http://opensource.org/licenses/bsd-license.php

     10 #

     11 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,

     12 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

     13 
     14 ##

     15 # Import Modules

     16 #

     17 import re
     18 import Common.EdkLogger as EdkLogger
     19 from Common.BuildToolError import *
     20 from UniClassObject import *
     21 from StringIO import StringIO
     22 from struct import pack, unpack
     23 from Common.LongFilePathSupport import OpenLongFilePath as open
     24 
     25 ##

     26 # Static definitions

     27 #

     28 EFI_HII_SIBT_END = '0x00'
     29 EFI_HII_SIBT_STRING_SCSU = '0x10'
     30 EFI_HII_SIBT_STRING_SCSU_FONT = '0x11'
     31 EFI_HII_SIBT_STRINGS_SCSU = '0x12'
     32 EFI_HII_SIBT_STRINGS_SCSU_FONT = '0x13'
     33 EFI_HII_SIBT_STRING_UCS2 = '0x14'
     34 EFI_HII_SIBT_STRING_UCS2_FONT = '0x15'
     35 EFI_HII_SIBT_STRINGS_UCS2 = '0x16'
     36 EFI_HII_SIBT_STRINGS_UCS2_FONT = '0x17'
     37 EFI_HII_SIBT_DUPLICATE = '0x20'
     38 EFI_HII_SIBT_SKIP2 = '0x21'
     39 EFI_HII_SIBT_SKIP1 = '0x22'
     40 EFI_HII_SIBT_EXT1 = '0x30'
     41 EFI_HII_SIBT_EXT2 = '0x31'
     42 EFI_HII_SIBT_EXT4 = '0x32'
     43 EFI_HII_SIBT_FONT = '0x40'
     44 
     45 EFI_HII_PACKAGE_STRINGS = '0x04'
     46 EFI_HII_PACKAGE_FORM = '0x02'
     47 
     48 StringPackageType = EFI_HII_PACKAGE_STRINGS
     49 StringPackageForm = EFI_HII_PACKAGE_FORM
     50 StringBlockType = EFI_HII_SIBT_STRING_UCS2
     51 StringSkipType = EFI_HII_SIBT_SKIP2
     52 
     53 HexHeader = '0x'
     54 
     55 COMMENT = '// '
     56 DEFINE_STR = '#define'
     57 COMMENT_DEFINE_STR = COMMENT + DEFINE_STR
     58 NOT_REFERENCED = 'not referenced'
     59 COMMENT_NOT_REFERENCED = ' ' + COMMENT + NOT_REFERENCED
     60 CHAR_ARRAY_DEFIN = 'unsigned char'
     61 COMMON_FILE_NAME = 'Strings'
     62 OFFSET = 'offset'
     63 STRING = 'string'
     64 TO = 'to'
     65 STRING_TOKEN = re.compile('STRING_TOKEN *\(([A-Z0-9_]+) *\)', re.MULTILINE | re.UNICODE)
     66 COMPATIBLE_STRING_TOKEN = re.compile('STRING_TOKEN *\(([A-Z0-9_]+) *\)', re.MULTILINE | re.UNICODE)
     67 
     68 EFI_HII_ARRAY_SIZE_LENGTH = 4
     69 EFI_HII_PACKAGE_HEADER_LENGTH = 4
     70 EFI_HII_HDR_SIZE_LENGTH = 4
     71 EFI_HII_STRING_OFFSET_LENGTH = 4
     72 EFI_STRING_ID = 1
     73 EFI_STRING_ID_LENGTH = 2
     74 EFI_HII_LANGUAGE_WINDOW = 0
     75 EFI_HII_LANGUAGE_WINDOW_LENGTH = 2
     76 EFI_HII_LANGUAGE_WINDOW_NUMBER = 16
     77 EFI_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
     78 
     79 H_C_FILE_HEADER = ['//', \
     80                    '//  DO NOT EDIT -- auto-generated file', \
     81                    '//', \
     82                    '//  This file is generated by the StrGather utility', \
     83                    '//']
     84 LANGUAGE_NAME_STRING_NAME = '$LANGUAGE_NAME'
     85 PRINTABLE_LANGUAGE_NAME_STRING_NAME = '$PRINTABLE_LANGUAGE_NAME'
     86 
     87 ## Convert a dec number to a hex string

     88 #

     89 # Convert a dec number to a formatted hex string in length digit

     90 # The digit is set to default 8

     91 # The hex string starts with "0x"

     92 # DecToHexStr(1000) is '0x000003E8'

     93 # DecToHexStr(1000, 6) is '0x0003E8'

     94 #

     95 # @param Dec:    The number in dec format

     96 # @param Digit:  The needed digit of hex string

     97 #

     98 # @retval:       The formatted hex string

     99 #

    100 def DecToHexStr(Dec, Digit = 8):
    101     return eval("'0x%0" + str(Digit) + "X' % int(Dec)")
    102 
    103 ## Convert a dec number to a hex list

    104 #

    105 # Convert a dec number to a formatted hex list in size digit

    106 # The digit is set to default 8

    107 # DecToHexList(1000) is ['0xE8', '0x03', '0x00', '0x00']

    108 # DecToHexList(1000, 6) is ['0xE8', '0x03', '0x00']

    109 #

    110 # @param Dec:    The number in dec format

    111 # @param Digit:  The needed digit of hex list

    112 #

    113 # @retval:       A list for formatted hex string

    114 #

    115 def DecToHexList(Dec, Digit = 8):
    116     Hex = eval("'%0" + str(Digit) + "X' % int(Dec)")
    117     List = []
    118     for Bit in range(Digit - 2, -1, -2):
    119         List.append(HexHeader + Hex[Bit:Bit + 2])
    120     return List
    121 
    122 ## Convert a acsii string to a hex list

    123 #

    124 # Convert a acsii string to a formatted hex list

    125 # AscToHexList('en-US') is ['0x65', '0x6E', '0x2D', '0x55', '0x53']

    126 #

    127 # @param Ascii:  The acsii string

    128 #

    129 # @retval:       A list for formatted hex string

    130 #

    131 def AscToHexList(Ascii):
    132     List = []
    133     for Item in Ascii:
    134         List.append('0x%02X' % ord(Item))
    135 
    136     return List
    137 
    138 ## Create header of .h file

    139 #

    140 # Create a header of .h file

    141 #

    142 # @param BaseName: The basename of strings

    143 #

    144 # @retval Str:     A string for .h file header

    145 #

    146 def CreateHFileHeader(BaseName):
    147     Str = ''
    148     for Item in H_C_FILE_HEADER:
    149         Str = WriteLine(Str, Item)
    150     Str = WriteLine(Str, '#ifndef _' + BaseName.upper() + '_STRINGS_DEFINE_H_')
    151     Str = WriteLine(Str, '#define _' + BaseName.upper() + '_STRINGS_DEFINE_H_')
    152     return Str
    153 
    154 ## Create content of .h file

    155 #

    156 # Create content of .h file

    157 #

    158 # @param BaseName:        The basename of strings

    159 # @param UniObjectClass   A UniObjectClass instance

    160 # @param IsCompatibleMode Compatible mode

    161 # @param UniGenCFlag      UniString is generated into AutoGen C file when it is set to True

    162 #

    163 # @retval Str:           A string of .h file content

    164 #

    165 def CreateHFileContent(BaseName, UniObjectClass, IsCompatibleMode, UniGenCFlag):
    166     Str = ''
    167     ValueStartPtr = 60
    168     Line = COMMENT_DEFINE_STR + ' ' + LANGUAGE_NAME_STRING_NAME + ' ' * (ValueStartPtr - len(DEFINE_STR + LANGUAGE_NAME_STRING_NAME)) + DecToHexStr(0, 4) + COMMENT_NOT_REFERENCED
    169     Str = WriteLine(Str, Line)
    170     Line = COMMENT_DEFINE_STR + ' ' + PRINTABLE_LANGUAGE_NAME_STRING_NAME + ' ' * (ValueStartPtr - len(DEFINE_STR + PRINTABLE_LANGUAGE_NAME_STRING_NAME)) + DecToHexStr(1, 4) + COMMENT_NOT_REFERENCED
    171     Str = WriteLine(Str, Line)
    172     UnusedStr = ''
    173 
    174     #Group the referred/Unused STRING token together. 

    175     for Index in range(2, len(UniObjectClass.OrderedStringList[UniObjectClass.LanguageDef[0][0]])):
    176         StringItem = UniObjectClass.OrderedStringList[UniObjectClass.LanguageDef[0][0]][Index]
    177         Name = StringItem.StringName
    178         Token = StringItem.Token
    179         Referenced = StringItem.Referenced
    180         if Name != None:
    181             Line = ''
    182             if Referenced == True:
    183                 if (ValueStartPtr - len(DEFINE_STR + Name)) <= 0:
    184                     Line = DEFINE_STR + ' ' + Name + ' ' + DecToHexStr(Token, 4)
    185                 else:
    186                     Line = DEFINE_STR + ' ' + Name + ' ' * (ValueStartPtr - len(DEFINE_STR + Name)) + DecToHexStr(Token, 4)
    187                 Str = WriteLine(Str, Line)
    188             else:
    189                 if (ValueStartPtr - len(DEFINE_STR + Name)) <= 0:
    190                     Line = COMMENT_DEFINE_STR + ' ' + Name + ' ' + DecToHexStr(Token, 4) + COMMENT_NOT_REFERENCED
    191                 else:
    192                     Line = COMMENT_DEFINE_STR + ' ' + Name + ' ' * (ValueStartPtr - len(DEFINE_STR + Name)) + DecToHexStr(Token, 4) + COMMENT_NOT_REFERENCED
    193                 UnusedStr = WriteLine(UnusedStr, Line)
    194 
    195     Str = ''.join([Str, UnusedStr])
    196 
    197     Str = WriteLine(Str, '')
    198     if IsCompatibleMode or UniGenCFlag:
    199         Str = WriteLine(Str, 'extern unsigned char ' + BaseName + 'Strings[];')
    200     return Str
    201 
    202 ## Create a complete .h file

    203 #

    204 # Create a complet .h file with file header and file content

    205 #

    206 # @param BaseName:        The basename of strings

    207 # @param UniObjectClass   A UniObjectClass instance

    208 # @param IsCompatibleMode Compatible mode

    209 # @param UniGenCFlag      UniString is generated into AutoGen C file when it is set to True

    210 #

    211 # @retval Str:           A string of complete .h file

    212 #

    213 def CreateHFile(BaseName, UniObjectClass, IsCompatibleMode, UniGenCFlag):
    214     HFile = WriteLine('', CreateHFileContent(BaseName, UniObjectClass, IsCompatibleMode, UniGenCFlag))
    215 
    216     return HFile
    217 
    218 ## Create header of .c file

    219 #

    220 # Create a header of .c file

    221 #

    222 # @retval Str:     A string for .c file header

    223 #

    224 def CreateCFileHeader():
    225     Str = ''
    226     for Item in H_C_FILE_HEADER:
    227         Str = WriteLine(Str, Item)
    228 
    229     return Str
    230 
    231 ## Create a buffer to store all items in an array

    232 #

    233 # @param BinBuffer   Buffer to contain Binary data.

    234 # @param Array:      The array need to be formatted

    235 #

    236 def CreateBinBuffer(BinBuffer, Array):
    237     for Item in Array:
    238         BinBuffer.write(pack("B", int(Item, 16)))
    239 
    240 ## Create a formatted string all items in an array

    241 #

    242 # Use ',' to join each item in an array, and break an new line when reaching the width (default is 16)

    243 #

    244 # @param Array:      The array need to be formatted

    245 # @param Width:      The line length, the default value is set to 16

    246 #

    247 # @retval ArrayItem: A string for all formatted array items

    248 #

    249 def CreateArrayItem(Array, Width = 16):
    250     MaxLength = Width
    251     Index = 0
    252     Line = '  '
    253     ArrayItem = ''
    254 
    255     for Item in Array:
    256         if Index < MaxLength:
    257             Line = Line + Item + ',  '
    258             Index = Index + 1
    259         else:
    260             ArrayItem = WriteLine(ArrayItem, Line)
    261             Line = '  ' + Item + ',  '
    262             Index = 1
    263     ArrayItem = Write(ArrayItem, Line.rstrip())
    264 
    265     return ArrayItem
    266 
    267 ## CreateCFileStringValue

    268 #

    269 # Create a line with string value

    270 #

    271 # @param Value:  Value of the string

    272 #

    273 # @retval Str:   A formatted string with string value

    274 #

    275 
    276 def CreateCFileStringValue(Value):
    277     Value = [StringBlockType] + Value
    278     Str = WriteLine('', CreateArrayItem(Value))
    279 
    280     return Str
    281 
    282 ## GetFilteredLanguage

    283 #

    284 # apply get best language rules to the UNI language code list

    285 #

    286 # @param UniLanguageList:  language code definition list in *.UNI file

    287 # @param LanguageFilterList:  language code filter list of RFC4646 format in DSC file

    288 #

    289 # @retval UniLanguageListFiltered:   the filtered language code

    290 #

    291 def GetFilteredLanguage(UniLanguageList, LanguageFilterList):
    292     UniLanguageListFiltered = []
    293     # if filter list is empty, then consider there is no filter

    294     if LanguageFilterList == []:
    295         UniLanguageListFiltered = UniLanguageList
    296         return UniLanguageListFiltered
    297     for Language in LanguageFilterList:
    298         # first check for exact match

    299         if Language in UniLanguageList:
    300             if Language not in UniLanguageListFiltered:
    301                 UniLanguageListFiltered += [Language]
    302         # find the first one with the same/equivalent primary tag

    303         else:
    304             if Language.find('-') != -1:
    305                 PrimaryTag = Language[0:Language.find('-')].lower()
    306             else:
    307                 PrimaryTag = Language
    308             
    309             if len(PrimaryTag) == 3:
    310                 PrimaryTag = LangConvTable.get(PrimaryTag)
    311             
    312             for UniLanguage in UniLanguageList:
    313                 if UniLanguage.find('-') != -1:
    314                     UniLanguagePrimaryTag = UniLanguage[0:UniLanguage.find('-')].lower()
    315                 else:
    316                     UniLanguagePrimaryTag = UniLanguage
    317                 
    318                 if len(UniLanguagePrimaryTag) == 3:
    319                     UniLanguagePrimaryTag = LangConvTable.get(UniLanguagePrimaryTag)
    320 
    321                 if PrimaryTag == UniLanguagePrimaryTag:
    322                     if UniLanguage not in UniLanguageListFiltered:
    323                         UniLanguageListFiltered += [UniLanguage]
    324                     break
    325             else:
    326                 # Here is rule 3 for "get best language"

    327                 # If tag is not listed in the Unicode file, the default ("en") tag should be used for that language

    328                 # for better processing, find the one that best suit for it.

    329                 DefaultTag = 'en'
    330                 if DefaultTag not in UniLanguageListFiltered:
    331                     # check whether language code with primary code equivalent with DefaultTag already in the list, if so, use that

    332                     for UniLanguage in UniLanguageList:
    333                         if UniLanguage.startswith('en-') or UniLanguage.startswith('eng-'):
    334                             if UniLanguage not in UniLanguageListFiltered:
    335                                 UniLanguageListFiltered += [UniLanguage]
    336                             break
    337                     else:
    338                         UniLanguageListFiltered += [DefaultTag]
    339     return  UniLanguageListFiltered
    340 
    341 
    342 ## Create content of .c file

    343 #

    344 # Create content of .c file

    345 #

    346 # @param BaseName:        The basename of strings

    347 # @param UniObjectClass   A UniObjectClass instance

    348 # @param IsCompatibleMode Compatible mode

    349 # @param UniBinBuffer     UniBinBuffer to contain UniBinary data.

    350 # @param FilterInfo       Platform language filter information 

    351 #

    352 # @retval Str:           A string of .c file content

    353 #

    354 def CreateCFileContent(BaseName, UniObjectClass, IsCompatibleMode, UniBinBuffer, FilterInfo):
    355     #

    356     # Init array length

    357     #

    358     TotalLength = EFI_HII_ARRAY_SIZE_LENGTH
    359     Str = ''
    360     Offset = 0
    361 
    362     EDK2Module = FilterInfo[0]
    363     if EDK2Module:
    364         LanguageFilterList = FilterInfo[1]
    365     else:
    366         # EDK module is using ISO639-2 format filter, convert to the RFC4646 format

    367         LanguageFilterList = [LangConvTable.get(F.lower()) for F in FilterInfo[1]]
    368     
    369     UniLanguageList = []
    370     for IndexI in range(len(UniObjectClass.LanguageDef)):
    371         UniLanguageList += [UniObjectClass.LanguageDef[IndexI][0]]
    372 
    373     UniLanguageListFiltered = GetFilteredLanguage(UniLanguageList, LanguageFilterList)
    374  
    375         
    376     #

    377     # Create lines for each language's strings

    378     #

    379     for IndexI in range(len(UniObjectClass.LanguageDef)):
    380         Language = UniObjectClass.LanguageDef[IndexI][0]
    381         if Language not in UniLanguageListFiltered:
    382             continue
    383         
    384         StringBuffer = StringIO()
    385         StrStringValue = ''
    386         ArrayLength = 0
    387         NumberOfUseOtherLangDef = 0
    388         Index = 0
    389         for IndexJ in range(1, len(UniObjectClass.OrderedStringList[UniObjectClass.LanguageDef[IndexI][0]])):
    390             Item = UniObjectClass.OrderedStringListByToken[Language][IndexJ]
    391 
    392             Name = Item.StringName
    393             Value = Item.StringValueByteList
    394             Referenced = Item.Referenced
    395             Token = Item.Token
    396             UseOtherLangDef = Item.UseOtherLangDef
    397 
    398             if UseOtherLangDef != '' and Referenced:
    399                 NumberOfUseOtherLangDef = NumberOfUseOtherLangDef + 1
    400                 Index = Index + 1
    401             else:
    402                 if NumberOfUseOtherLangDef > 0:
    403                     StrStringValue = WriteLine(StrStringValue, CreateArrayItem([StringSkipType] + DecToHexList(NumberOfUseOtherLangDef, 4)))
    404                     CreateBinBuffer (StringBuffer, ([StringSkipType] + DecToHexList(NumberOfUseOtherLangDef, 4)))
    405                     NumberOfUseOtherLangDef = 0
    406                     ArrayLength = ArrayLength + 3
    407                 if Referenced and Item.Token > 0:
    408                     Index = Index + 1
    409                     StrStringValue = WriteLine(StrStringValue, "// %s: %s:%s" % (DecToHexStr(Index, 4), Name, DecToHexStr(Token, 4)))
    410                     StrStringValue = Write(StrStringValue, CreateCFileStringValue(Value))
    411                     CreateBinBuffer (StringBuffer, [StringBlockType] + Value)
    412                     ArrayLength = ArrayLength + Item.Length + 1 # 1 is for the length of string type

    413 
    414         #

    415         # EFI_HII_PACKAGE_HEADER

    416         #

    417         Offset = EFI_HII_STRING_PACKAGE_HDR_LENGTH + len(Language) + 1
    418         ArrayLength = Offset + ArrayLength + 1
    419 
    420         #

    421         # Create PACKAGE HEADER

    422         #

    423         Str = WriteLine(Str, '// PACKAGE HEADER\n')
    424         TotalLength = TotalLength + ArrayLength
    425 
    426         List = DecToHexList(ArrayLength, 6) + \
    427                [StringPackageType] + \
    428                DecToHexList(Offset) + \
    429                DecToHexList(Offset) + \
    430                DecToHexList(EFI_HII_LANGUAGE_WINDOW, EFI_HII_LANGUAGE_WINDOW_LENGTH * 2) * EFI_HII_LANGUAGE_WINDOW_NUMBER + \
    431                DecToHexList(EFI_STRING_ID, 4) + \
    432                AscToHexList(Language) + \
    433                DecToHexList(0, 2)
    434         Str = WriteLine(Str, CreateArrayItem(List, 16) + '\n')
    435 
    436         #

    437         # Create PACKAGE DATA

    438         #

    439         Str = WriteLine(Str, '// PACKAGE DATA\n')
    440         Str = Write(Str, StrStringValue)
    441 
    442         #

    443         # Add an EFI_HII_SIBT_END at last

    444         #

    445         Str = WriteLine(Str, '  ' + EFI_HII_SIBT_END + ",")
    446         
    447         #

    448         # Create binary UNI string

    449         #

    450         if UniBinBuffer:
    451             CreateBinBuffer (UniBinBuffer, List)
    452             UniBinBuffer.write (StringBuffer.getvalue())
    453             UniBinBuffer.write (pack("B", int(EFI_HII_SIBT_END, 16)))
    454         StringBuffer.close()
    455 
    456     #

    457     # Create line for string variable name

    458     # "unsigned char $(BaseName)Strings[] = {"

    459     #

    460     AllStr = WriteLine('', CHAR_ARRAY_DEFIN + ' ' + BaseName + COMMON_FILE_NAME + '[] = {\n')
    461 
    462     if IsCompatibleMode:
    463         #

    464         # Create FRAMEWORK_EFI_HII_PACK_HEADER in compatible mode

    465         #

    466         AllStr = WriteLine(AllStr, '// FRAMEWORK PACKAGE HEADER Length')
    467         AllStr = WriteLine(AllStr, CreateArrayItem(DecToHexList(TotalLength + 2)) + '\n')
    468         AllStr = WriteLine(AllStr, '// FRAMEWORK PACKAGE HEADER Type')
    469         AllStr = WriteLine(AllStr, CreateArrayItem(DecToHexList(2, 4)) + '\n')
    470     else:
    471         #

    472         # Create whole array length in UEFI mode

    473         #

    474         AllStr = WriteLine(AllStr, '// STRGATHER_OUTPUT_HEADER')
    475         AllStr = WriteLine(AllStr, CreateArrayItem(DecToHexList(TotalLength)) + '\n')
    476 
    477     #

    478     # Join package data

    479     #

    480     AllStr = Write(AllStr, Str)
    481 
    482     return AllStr
    483 
    484 ## Create end of .c file

    485 #

    486 # Create end of .c file

    487 #

    488 # @retval Str:           A string of .h file end

    489 #

    490 def CreateCFileEnd():
    491     Str = Write('', '};')
    492     return Str
    493 
    494 ## Create a .c file

    495 #

    496 # Create a complete .c file

    497 #

    498 # @param BaseName:        The basename of strings

    499 # @param UniObjectClass   A UniObjectClass instance

    500 # @param IsCompatibleMode Compatible Mode

    501 # @param FilterInfo       Platform language filter information 

    502 #

    503 # @retval CFile:          A string of complete .c file

    504 #

    505 def CreateCFile(BaseName, UniObjectClass, IsCompatibleMode, FilterInfo):
    506     CFile = ''
    507     #CFile = WriteLine(CFile, CreateCFileHeader())

    508     CFile = WriteLine(CFile, CreateCFileContent(BaseName, UniObjectClass, IsCompatibleMode, None, FilterInfo))
    509     CFile = WriteLine(CFile, CreateCFileEnd())
    510     return CFile
    511 
    512 ## GetFileList

    513 #

    514 # Get a list for all files

    515 #

    516 # @param IncludeList:  A list of all path to be searched

    517 # @param SkipList:     A list of all types of file could be skipped

    518 #

    519 # @retval FileList:    A list of all files found

    520 #

    521 def GetFileList(SourceFileList, IncludeList, SkipList):
    522     if IncludeList == None:
    523         EdkLogger.error("UnicodeStringGather", AUTOGEN_ERROR, "Include path for unicode file is not defined")
    524 
    525     FileList = []
    526     if SkipList == None:
    527         SkipList = []
    528 
    529     for File in SourceFileList:
    530         for Dir in IncludeList:
    531             if not os.path.exists(Dir):
    532                 continue
    533             File = os.path.join(Dir, File.Path)
    534             #

    535             # Ignore Dir

    536             #

    537             if os.path.isfile(File) != True:
    538                 continue
    539             #

    540             # Ignore file listed in skip list

    541             #

    542             IsSkip = False
    543             for Skip in SkipList:
    544                 if os.path.splitext(File)[1].upper() == Skip.upper():
    545                     EdkLogger.verbose("Skipped %s for string token uses search" % File)
    546                     IsSkip = True
    547                     break
    548 
    549             if not IsSkip:
    550                 FileList.append(File)
    551 
    552             break
    553 
    554     return FileList
    555 
    556 ## SearchString

    557 #

    558 # Search whether all string defined in UniObjectClass are referenced

    559 # All string used should be set to Referenced

    560 #

    561 # @param UniObjectClass:  Input UniObjectClass

    562 # @param FileList:        Search path list

    563 # @param IsCompatibleMode Compatible Mode

    564 #

    565 # @retval UniObjectClass: UniObjectClass after searched

    566 #

    567 def SearchString(UniObjectClass, FileList, IsCompatibleMode):
    568     if FileList == []:
    569         return UniObjectClass
    570 
    571     for File in FileList:
    572         if os.path.isfile(File):
    573             Lines = open(File, 'r')
    574             for Line in Lines:
    575                 if not IsCompatibleMode:
    576                     StringTokenList = STRING_TOKEN.findall(Line)
    577                 else:
    578                     StringTokenList = COMPATIBLE_STRING_TOKEN.findall(Line)
    579                 for StrName in StringTokenList:
    580                     EdkLogger.debug(EdkLogger.DEBUG_5, "Found string identifier: " + StrName)
    581                     UniObjectClass.SetStringReferenced(StrName)
    582 
    583     UniObjectClass.ReToken()
    584 
    585     return UniObjectClass
    586 
    587 ## GetStringFiles

    588 #

    589 # This function is used for UEFI2.1 spec

    590 #

    591 #

    592 def GetStringFiles(UniFilList, SourceFileList, IncludeList, IncludePathList, SkipList, BaseName, IsCompatibleMode = False, ShellMode = False, UniGenCFlag = True, UniGenBinBuffer = None, FilterInfo = [True, []]):  
    593     if len(UniFilList) > 0:
    594         if ShellMode:
    595             #

    596             # support ISO 639-2 codes in .UNI files of EDK Shell

    597             #

    598             Uni = UniFileClassObject(sorted (UniFilList), True, IncludePathList)
    599         else:
    600             Uni = UniFileClassObject(sorted (UniFilList), IsCompatibleMode, IncludePathList)
    601     else:
    602         EdkLogger.error("UnicodeStringGather", AUTOGEN_ERROR, 'No unicode files given')
    603 
    604     FileList = GetFileList(SourceFileList, IncludeList, SkipList)
    605 
    606     Uni = SearchString(Uni, sorted (FileList), IsCompatibleMode)
    607 
    608     HFile = CreateHFile(BaseName, Uni, IsCompatibleMode, UniGenCFlag)
    609     CFile = None
    610     if IsCompatibleMode or UniGenCFlag:
    611         CFile = CreateCFile(BaseName, Uni, IsCompatibleMode, FilterInfo)
    612     if UniGenBinBuffer:
    613         CreateCFileContent(BaseName, Uni, IsCompatibleMode, UniGenBinBuffer, FilterInfo)
    614 
    615     return HFile, CFile
    616 
    617 #

    618 # Write an item

    619 #

    620 def Write(Target, Item):
    621     return ''.join([Target, Item])
    622 
    623 #

    624 # Write an item with a break line

    625 #

    626 def WriteLine(Target, Item):
    627     return ''.join([Target, Item, '\n'])
    628 
    629 # This acts like the main() function for the script, unless it is 'import'ed into another

    630 # script.

    631 if __name__ == '__main__':
    632     EdkLogger.info('start')
    633 
    634     UniFileList = [
    635                    r'C:\\Edk\\Strings2.uni',
    636                    r'C:\\Edk\\Strings.uni'
    637     ]
    638 
    639     SrcFileList = []
    640     for Root, Dirs, Files in os.walk('C:\\Edk'):
    641         for File in Files:
    642             SrcFileList.append(File)
    643 
    644     IncludeList = [
    645                    r'C:\\Edk'
    646     ]
    647 
    648     SkipList = ['.inf', '.uni']
    649     BaseName = 'DriverSample'
    650     (h, c) = GetStringFiles(UniFileList, SrcFileList, IncludeList, SkipList, BaseName, True)
    651     hfile = open('unistring.h', 'w')
    652     cfile = open('unistring.c', 'w')
    653     hfile.write(h)
    654     cfile.write(c)
    655 
    656     EdkLogger.info('end')
    657