PageRenderTime 54ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/edk2/BaseTools/Source/Python/Common/TargetTxtClassObject.py

https://gitlab.com/envieidoc/Clover
Python | 190 lines | 148 code | 5 blank | 37 comment | 11 complexity | f601edf0e7613078100c84ce4c29094c MD5 | raw file
  1. ## @file
  2. # This file is used to define each component of Target.txt file
  3. #
  4. # Copyright (c) 2007 - 2014, Intel Corporation. All rights reserved.<BR>
  5. # This program and the accompanying materials
  6. # are licensed and made available under the terms and conditions of the BSD License
  7. # which accompanies this distribution. The full text of the license may be found at
  8. # http://opensource.org/licenses/bsd-license.php
  9. #
  10. # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
  12. #
  13. ##
  14. # Import Modules
  15. #
  16. import Common.LongFilePathOs as os
  17. import EdkLogger
  18. import DataType
  19. from BuildToolError import *
  20. import GlobalData
  21. from Common.LongFilePathSupport import OpenLongFilePath as open
  22. gDefaultTargetTxtFile = "target.txt"
  23. ## TargetTxtClassObject
  24. #
  25. # This class defined content used in file target.txt
  26. #
  27. # @param object: Inherited from object class
  28. # @param Filename: Input value for full path of target.txt
  29. #
  30. # @var TargetTxtDictionary: To store keys and values defined in target.txt
  31. #
  32. class TargetTxtClassObject(object):
  33. def __init__(self, Filename = None):
  34. self.TargetTxtDictionary = {
  35. DataType.TAB_TAT_DEFINES_ACTIVE_PLATFORM : '',
  36. DataType.TAB_TAT_DEFINES_ACTIVE_MODULE : '',
  37. DataType.TAB_TAT_DEFINES_TOOL_CHAIN_CONF : '',
  38. DataType.TAB_TAT_DEFINES_MAX_CONCURRENT_THREAD_NUMBER : '',
  39. DataType.TAB_TAT_DEFINES_TARGET : [],
  40. DataType.TAB_TAT_DEFINES_TOOL_CHAIN_TAG : [],
  41. DataType.TAB_TAT_DEFINES_TARGET_ARCH : [],
  42. DataType.TAB_TAT_DEFINES_BUILD_RULE_CONF : '',
  43. }
  44. self.ConfDirectoryPath = ""
  45. if Filename != None:
  46. self.LoadTargetTxtFile(Filename)
  47. ## LoadTargetTxtFile
  48. #
  49. # Load target.txt file and parse it, return a set structure to store keys and values
  50. #
  51. # @param Filename: Input value for full path of target.txt
  52. #
  53. # @retval set() A set structure to store keys and values
  54. # @retval 1 Error happenes in parsing
  55. #
  56. def LoadTargetTxtFile(self, Filename):
  57. if os.path.exists(Filename) and os.path.isfile(Filename):
  58. return self.ConvertTextFileToDict(Filename, '#', '=')
  59. else:
  60. EdkLogger.error("Target.txt Parser", FILE_NOT_FOUND, ExtraData=Filename)
  61. return 1
  62. ## ConvertTextFileToDict
  63. #
  64. # Convert a text file to a dictionary of (name:value) pairs.
  65. # The data is saved to self.TargetTxtDictionary
  66. #
  67. # @param FileName: Text filename
  68. # @param CommentCharacter: Comment char, be used to ignore comment content
  69. # @param KeySplitCharacter: Key split char, between key name and key value. Key1 = Value1, '=' is the key split char
  70. #
  71. # @retval 0 Convert successfully
  72. # @retval 1 Open file failed
  73. #
  74. def ConvertTextFileToDict(self, FileName, CommentCharacter, KeySplitCharacter):
  75. F = None
  76. try:
  77. F = open(FileName, 'r')
  78. self.ConfDirectoryPath = os.path.dirname(FileName)
  79. except:
  80. EdkLogger.error("build", FILE_OPEN_FAILURE, ExtraData=FileName)
  81. if F != None:
  82. F.close()
  83. for Line in F:
  84. Line = Line.strip()
  85. if Line.startswith(CommentCharacter) or Line == '':
  86. continue
  87. LineList = Line.split(KeySplitCharacter, 1)
  88. Key = LineList[0].strip()
  89. if len(LineList) == 2:
  90. Value = LineList[1].strip()
  91. else:
  92. Value = ""
  93. if Key in [DataType.TAB_TAT_DEFINES_ACTIVE_PLATFORM, DataType.TAB_TAT_DEFINES_TOOL_CHAIN_CONF, \
  94. DataType.TAB_TAT_DEFINES_ACTIVE_MODULE, DataType.TAB_TAT_DEFINES_BUILD_RULE_CONF]:
  95. self.TargetTxtDictionary[Key] = Value.replace('\\', '/')
  96. if Key == DataType.TAB_TAT_DEFINES_TOOL_CHAIN_CONF and self.TargetTxtDictionary[Key]:
  97. if self.TargetTxtDictionary[Key].startswith("Conf/"):
  98. Tools_Def = os.path.join(self.ConfDirectoryPath, self.TargetTxtDictionary[Key].strip())
  99. if not os.path.exists(Tools_Def) or not os.path.isfile(Tools_Def):
  100. # If Conf/Conf does not exist, try just the Conf/ directory
  101. Tools_Def = os.path.join(self.ConfDirectoryPath, self.TargetTxtDictionary[Key].replace("Conf/", "", 1).strip())
  102. else:
  103. # The File pointed to by TOOL_CHAIN_CONF is not in a Conf/ directory
  104. Tools_Def = os.path.join(self.ConfDirectoryPath, self.TargetTxtDictionary[Key].strip())
  105. self.TargetTxtDictionary[Key] = Tools_Def
  106. if Key == DataType.TAB_TAT_DEFINES_BUILD_RULE_CONF and self.TargetTxtDictionary[Key]:
  107. if self.TargetTxtDictionary[Key].startswith("Conf/"):
  108. Build_Rule = os.path.join(self.ConfDirectoryPath, self.TargetTxtDictionary[Key].strip())
  109. if not os.path.exists(Build_Rule) or not os.path.isfile(Build_Rule):
  110. # If Conf/Conf does not exist, try just the Conf/ directory
  111. Build_Rule = os.path.join(self.ConfDirectoryPath, self.TargetTxtDictionary[Key].replace("Conf/", "", 1).strip())
  112. else:
  113. # The File pointed to by BUILD_RULE_CONF is not in a Conf/ directory
  114. Build_Rule = os.path.join(self.ConfDirectoryPath, self.TargetTxtDictionary[Key].strip())
  115. self.TargetTxtDictionary[Key] = Build_Rule
  116. elif Key in [DataType.TAB_TAT_DEFINES_TARGET, DataType.TAB_TAT_DEFINES_TARGET_ARCH, \
  117. DataType.TAB_TAT_DEFINES_TOOL_CHAIN_TAG]:
  118. self.TargetTxtDictionary[Key] = Value.split()
  119. elif Key == DataType.TAB_TAT_DEFINES_MAX_CONCURRENT_THREAD_NUMBER:
  120. try:
  121. V = int(Value, 0)
  122. except:
  123. EdkLogger.error("build", FORMAT_INVALID, "Invalid number of [%s]: %s." % (Key, Value),
  124. File=FileName)
  125. self.TargetTxtDictionary[Key] = Value
  126. #elif Key not in GlobalData.gGlobalDefines:
  127. # GlobalData.gGlobalDefines[Key] = Value
  128. F.close()
  129. return 0
  130. ## Print the dictionary
  131. #
  132. # Print all items of dictionary one by one
  133. #
  134. # @param Dict: The dictionary to be printed
  135. #
  136. def printDict(Dict):
  137. if Dict != None:
  138. KeyList = Dict.keys()
  139. for Key in KeyList:
  140. if Dict[Key] != '':
  141. print Key + ' = ' + str(Dict[Key])
  142. ## Print the dictionary
  143. #
  144. # Print the items of dictionary which matched with input key
  145. #
  146. # @param list: The dictionary to be printed
  147. # @param key: The key of the item to be printed
  148. #
  149. def printList(Key, List):
  150. if type(List) == type([]):
  151. if len(List) > 0:
  152. if Key.find(TAB_SPLIT) != -1:
  153. print "\n" + Key
  154. for Item in List:
  155. print Item
  156. ## TargetTxtDict
  157. #
  158. # Load target.txt in input Conf dir
  159. #
  160. # @param ConfDir: Conf dir
  161. #
  162. # @retval Target An instance of TargetTxtClassObject() with loaded target.txt
  163. #
  164. def TargetTxtDict(ConfDir):
  165. Target = TargetTxtClassObject()
  166. Target.LoadTargetTxtFile(os.path.normpath(os.path.join(ConfDir, gDefaultTargetTxtFile)))
  167. return Target
  168. ##
  169. #
  170. # This acts like the main() function for the script, unless it is 'import'ed into another
  171. # script.
  172. #
  173. if __name__ == '__main__':
  174. pass
  175. Target = TargetTxtDict(os.getenv("WORKSPACE"))
  176. print Target.TargetTxtDictionary[DataType.TAB_TAT_DEFINES_MAX_CONCURRENT_THREAD_NUMBER]
  177. print Target.TargetTxtDictionary[DataType.TAB_TAT_DEFINES_TARGET]
  178. print Target.TargetTxtDictionary