cmparseMSBuildXML.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. # This python script parses the spec files from MSBuild to create
  2. # mappings from compiler options to IDE XML specifications. For
  3. # more information see here:
  4. # http://blogs.msdn.com/vcblog/archive/2008/12/16/msbuild-task.aspx
  5. # cl.xml
  6. #
  7. # BoolProperty <Name>true|false</Name>
  8. # simple example:
  9. # <BoolProperty ReverseSwitch="Oy-" Name="OmitFramePointers"
  10. # Category="Optimization" Switch="Oy">
  11. # <BoolProperty.DisplayName> <BoolProperty.Description>
  12. # <CLCompile>
  13. # <OmitFramePointers>true</OmitFramePointers>
  14. # </ClCompile>
  15. #
  16. # argument means it might be this: /MP3
  17. # example with argument:
  18. # <BoolProperty Name="MultiProcessorCompilation" Category="General" Switch="MP">
  19. # <BoolProperty.DisplayName>
  20. # <sys:String>Multi-processor Compilation</sys:String>
  21. # </BoolProperty.DisplayName>
  22. # <BoolProperty.Description>
  23. # <sys:String>Multi-processor Compilation</sys:String>
  24. # </BoolProperty.Description>
  25. # <Argument Property="ProcessorNumber" IsRequired="false" />
  26. # </BoolProperty>
  27. # <CLCompile>
  28. # <MultiProcessorCompilation>true</MultiProcessorCompilation>
  29. # <ProcessorNumber>4</ProcessorNumber>
  30. # </ClCompile>
  31. # IntProperty
  32. # not used AFIT
  33. # <IntProperty Name="ProcessorNumber" Category="General" Visible="false">
  34. # per config options example
  35. # <EnableFiberSafeOptimizations Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">true</EnableFiberSafeOptimizations>
  36. #
  37. # EnumProperty
  38. # <EnumProperty Name="Optimization" Category="Optimization">
  39. # <EnumProperty.DisplayName>
  40. # <sys:String>Optimization</sys:String>
  41. # </EnumProperty.DisplayName>
  42. # <EnumProperty.Description>
  43. # <sys:String>Select option for code optimization; choose Custom to use specific optimization options. (/Od, /O1, /O2, /Ox)</sys:String>
  44. # </EnumProperty.Description>
  45. # <EnumValue Name="MaxSpeed" Switch="O2">
  46. # <EnumValue.DisplayName>
  47. # <sys:String>Maximize Speed</sys:String>
  48. # </EnumValue.DisplayName>
  49. # <EnumValue.Description>
  50. # <sys:String>Equivalent to /Og /Oi /Ot /Oy /Ob2 /Gs /GF /Gy</sys:String>
  51. # </EnumValue.Description>
  52. # </EnumValue>
  53. # <EnumValue Name="MinSpace" Switch="O1">
  54. # <EnumValue.DisplayName>
  55. # <sys:String>Minimize Size</sys:String>
  56. # </EnumValue.DisplayName>
  57. # <EnumValue.Description>
  58. # <sys:String>Equivalent to /Og /Os /Oy /Ob2 /Gs /GF /Gy</sys:String>
  59. # </EnumValue.Description>
  60. # </EnumValue>
  61. # example for O2 would be this:
  62. # <Optimization>MaxSpeed</Optimization>
  63. # example for O1 would be this:
  64. # <Optimization>MinSpace</Optimization>
  65. #
  66. # StringListProperty
  67. # <StringListProperty Name="PreprocessorDefinitions" Category="Preprocessor" Switch="D ">
  68. # <StringListProperty.DisplayName>
  69. # <sys:String>Preprocessor Definitions</sys:String>
  70. # </StringListProperty.DisplayName>
  71. # <StringListProperty.Description>
  72. # <sys:String>Defines a preprocessing symbols for your source file.</sys:String>
  73. # </StringListProperty.Description>
  74. # </StringListProperty>
  75. # <StringListProperty Subtype="folder" Name="AdditionalIncludeDirectories" Category="General" Switch="I">
  76. # <StringListProperty.DisplayName>
  77. # <sys:String>Additional Include Directories</sys:String>
  78. # </StringListProperty.DisplayName>
  79. # <StringListProperty.Description>
  80. # <sys:String>Specifies one or more directories to add to the include path; separate with semi-colons if more than one. (/I[path])</sys:String>
  81. # </StringListProperty.Description>
  82. # </StringListProperty>
  83. # StringProperty
  84. # Example add bill include:
  85. # <AdditionalIncludeDirectories>..\..\..\..\..\..\bill;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
  86. import sys
  87. from xml.dom.minidom import parse, parseString
  88. def getText(node):
  89. nodelist = node.childNodes
  90. rc = ""
  91. for child in nodelist:
  92. if child.nodeType == child.TEXT_NODE:
  93. rc = rc + child.data
  94. return rc
  95. def print_tree(document, spaces=""):
  96. for i in range(len(document.childNodes)):
  97. if document.childNodes[i].nodeType == document.childNodes[i].ELEMENT_NODE:
  98. print spaces+str(document.childNodes[i].nodeName )
  99. print_tree(document.childNodes[i],spaces+"----")
  100. pass
  101. ###########################################################################################
  102. #Data structure that stores a property of MSBuild
  103. class Property:
  104. #type = type of MSBuild property (ex. if the property is EnumProperty type should be "Enum")
  105. #attributeNames = a list of any attributes that this property could have (ex. if this was a EnumProperty it should be ["Name","Category"])
  106. #document = the dom file that's root node is the Property node (ex. if you were parsing a BoolProperty the root node should be something like <BoolProperty Name="RegisterOutput" Category="General" IncludeInCommandLine="false">
  107. def __init__(self,type,attributeNames,document=None):
  108. self.suffix_type = "Property"
  109. self.prefix_type = type
  110. self.attributeNames = attributeNames
  111. self.attributes = {}
  112. self.DisplayName = ""
  113. self.Description = ""
  114. self.argumentProperty = ""
  115. self.argumentIsRequired = ""
  116. self.values = []
  117. if document is not None:
  118. self.populate(document)
  119. pass
  120. #document = the dom file that's root node is the Property node (ex. if you were parsing a BoolProperty the root node should be something like <BoolProperty Name="RegisterOutput" Category="General" IncludeInCommandLine="false">
  121. #spaces = do not use
  122. def populate(self,document, spaces = ""):
  123. if document.nodeName == self.prefix_type+self.suffix_type:
  124. for i in self.attributeNames:
  125. self.attributes[i] = document.getAttribute(i)
  126. for i in range(len(document.childNodes)):
  127. child = document.childNodes[i]
  128. if child.nodeType == child.ELEMENT_NODE:
  129. if child.nodeName == self.prefix_type+self.suffix_type+".DisplayName":
  130. self.DisplayName = getText(child.childNodes[1])
  131. if child.nodeName == self.prefix_type+self.suffix_type+".Description":
  132. self.Description = getText(child.childNodes[1])
  133. if child.nodeName == "Argument":
  134. self.argumentProperty = child.getAttribute("Property")
  135. self.argumentIsRequired = child.getAttribute("IsRequired")
  136. if child.nodeName == self.prefix_type+"Value":
  137. va = Property(self.prefix_type,["Name","Switch"])
  138. va.suffix_type = "Value"
  139. va.populate(child)
  140. self.values.append(va)
  141. self.populate(child,spaces+"----")
  142. pass
  143. #toString function
  144. def __str__(self):
  145. toReturn = self.prefix_type+self.suffix_type+":"
  146. for i in self.attributeNames:
  147. toReturn += "\n "+i+": "+self.attributes[i]
  148. if self.argumentProperty != "":
  149. toReturn += "\n Argument:\n Property: "+self.argumentProperty+"\n IsRequired: "+self.argumentIsRequired
  150. for i in self.values:
  151. toReturn+="\n "+str(i).replace("\n","\n ")
  152. return toReturn
  153. ###########################################################################################
  154. ###########################################################################################
  155. #Class that populates itself from an MSBuild file and outputs it in CMake
  156. #format
  157. class MSBuildToCMake:
  158. #document = the entire MSBuild xml file
  159. def __init__(self,document=None):
  160. self.enumProperties = []
  161. self.stringProperties = []
  162. self.stringListProperties = []
  163. self.boolProperties = []
  164. self.intProperties = []
  165. if document!=None :
  166. self.populate(document)
  167. pass
  168. #document = the entire MSBuild xml file
  169. #spaces = don't use
  170. #To add a new property (if they exist) copy and paste this code and fill in appropriate places
  171. #
  172. #if child.nodeName == "<Name>Property":
  173. # self.<Name>Properties.append(Property("<Name>",[<List of attributes>],child))
  174. #
  175. #Replace <Name> with the name of the new property (ex. if property is StringProperty replace <Name> with String)
  176. #Replace <List of attributes> with a list of attributes in your property's root node
  177. #in the __init__ function add the line self.<Name>Properties = []
  178. #
  179. #That is all that is required to add new properties
  180. #
  181. def populate(self,document, spaces=""):
  182. for i in range(len(document.childNodes)):
  183. child = document.childNodes[i]
  184. if child.nodeType == child.ELEMENT_NODE:
  185. if child.nodeName == "EnumProperty":
  186. self.enumProperties.append(Property("Enum",["Name","Category"],child))
  187. if child.nodeName == "StringProperty":
  188. self.stringProperties.append(Property("String",["Name","Subtype","Separator","Category","Visible","IncludeInCommandLine","Switch","ReadOnly"],child))
  189. if child.nodeName == "StringListProperty":
  190. self.stringListProperties.append(Property("StringList",["Name","Category","Switch","Subtype"],child))
  191. if child.nodeName == "BoolProperty":
  192. self.boolProperties.append(Property("Bool",["ReverseSwitch","Name","Category","Switch","SwitchPrefix","IncludeInCommandLine"],child))
  193. if child.nodeName == "IntProperty":
  194. self.intProperties.append(Property("Int",["Name","Category","Visible"],child))
  195. self.populate(child,spaces+"----")
  196. pass
  197. #outputs information that CMake needs to know about MSBuild xml files
  198. def toCMake(self):
  199. toReturn = "static cmVS7FlagTable cmVS10CxxTable[] =\n{\n"
  200. toReturn += "\n //Enum Properties\n"
  201. for i in self.enumProperties:
  202. for j in i.values:
  203. toReturn+=" {\""+i.attributes["Name"]+"\", \""+j.attributes["Switch"]+"\", \""+j.DisplayName+"\", \""+j.attributes["Name"]+"\", 0},\n"
  204. toReturn += "\n"
  205. toReturn += "\n //Bool Properties\n"
  206. for i in self.boolProperties:
  207. if i.argumentProperty == "":
  208. if i.attributes["ReverseSwitch"] != "":
  209. toReturn += " {\""+i.attributes["Name"]+"\", \""+i.attributes["ReverseSwitch"]+"\", \"\", \"false\", 0},\n"
  210. if i.attributes["Switch"] != "":
  211. toReturn += " {\""+i.attributes["Name"]+"\", \""+i.attributes["Switch"]+"\", \"\", \"true\", 0},\n"
  212. toReturn += "\n //Bool Properties With Argument\n"
  213. for i in self.boolProperties:
  214. if i.argumentProperty != "":
  215. if i.attributes["ReverseSwitch"] != "":
  216. toReturn += " {\""+i.attributes["Name"]+"\", \""+i.attributes["ReverseSwitch"]+"\", \"\", \"false\", cmVS7FlagTable::Continue},\n"
  217. toReturn += " {\""+i.attributes["Name"]+"\", \""+i.attributes["ReverseSwitch"]+"\", \""+i.DisplayName+"\", \"\", cmVS7FlagTable::UserValueRequired},\n"
  218. if i.attributes["Switch"] != "":
  219. toReturn += " {\""+i.attributes["Name"]+"\", \""+i.attributes["Switch"]+"\", \"\", \"true\", cmVS7FlagTable::Continue},\n"
  220. toReturn += " {\""+i.argumentProperty+"\", \""+i.attributes["Switch"]+"\", \""+i.DisplayName+"\", \"\", cmVS7FlagTable::UserValueRequired},\n"
  221. toReturn += "\n //String List Properties\n"
  222. for i in self.stringListProperties:
  223. toReturn+=" {\""+i.attributes["Name"]+"\", \""+i.attributes["Switch"]+"\", \""+i.DisplayName+"\", \"\", cmVS7FlagTable::UserValue | cmVS7FlagTable::SemicolonAppendable},\n"
  224. toReturn += " {0,0,0,0,0}\n};"
  225. return toReturn
  226. pass
  227. #toString function
  228. def __str__(self):
  229. toReturn = ""
  230. allList = [self.enumProperties,self.stringProperties,self.stringListProperties,self.boolProperties,self.intProperties]
  231. for p in allList:
  232. for i in p:
  233. toReturn += "==================================================\n"+str(i).replace("\n","\n ")+"\n==================================================\n"
  234. return toReturn
  235. ###########################################################################################
  236. ###########################################################################################
  237. # main function
  238. def main(argv):
  239. xml_file = None
  240. help = """
  241. Please specify an input xml file with -x
  242. Exiting...
  243. Have a nice day :)"""
  244. for i in range(0,len(argv)):
  245. if argv[i] == "-x":
  246. xml_file = argv[i+1]
  247. if argv[i] == "-h":
  248. print help
  249. sys.exit(0)
  250. pass
  251. if xml_file == None:
  252. print help
  253. sys.exit(1)
  254. f = open(xml_file,"r")
  255. xml_str = f.read()
  256. xml_dom = parseString(xml_str)
  257. convertor = MSBuildToCMake(xml_dom)
  258. print convertor.toCMake()
  259. xml_dom.unlink()
  260. ###########################################################################################
  261. # main entry point
  262. if __name__ == "__main__":
  263. main(sys.argv)
  264. sys.exit(0)