cmparseMSBuildXML.py 15 KB

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