cmparseMSBuildXML.py 15 KB

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