cmparseMSBuildXML.py 14 KB

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