cmparseMSBuildXML.py 15 KB

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