通過自己之前轉(zhuǎn)載的一篇文章甘有,《VS設(shè)置增量編譯》,在此基礎(chǔ)上增加了Release版本如何關(guān)閉優(yōu)化并自動(dòng)生成pdb文件葡缰,最終實(shí)現(xiàn)思路也比較簡(jiǎn)單亏掀,就用Python自帶的ElementTree庫讀寫vcxproj文件,廢話不多說泛释,直接上代碼:
import xml
from xml.etree.ElementTree import ElementTree, Element
xml.etree.ElementTree.register_namespace('', 'http://schemas.microsoft.com/developer/msbuild/2003')
def read_xml(in_path):
'''''讀取并解析xml文件
in_path: xml路徑
return: ElementTree'''
tree = ElementTree()
tree.parse(in_path)
return tree
def write_xml(tree, out_path):
'''''將xml文件寫出
tree: xml樹
out_path: 寫出路徑'''
tree.write(out_path, encoding="utf-8", xml_declaration=True)
def if_match(node, kv_map):
'''''判斷某個(gè)節(jié)點(diǎn)是否包含所有傳入?yún)?shù)屬性
node: 節(jié)點(diǎn)
kv_map: 屬性及屬性值組成的map'''
for key in kv_map:
if node.get(key) != kv_map.get(key):
return False
return True
# ----------------search -----------------
def find_nodes(tree, path, namespace=None):
'''''查找某個(gè)路徑匹配的所有節(jié)點(diǎn)
tree: xml樹
path: 節(jié)點(diǎn)路徑'''
return tree.findall(path, namespace)
def get_node_by_keyvalue(nodelist, kv_map):
'''''根據(jù)屬性及屬性值定位符合的節(jié)點(diǎn)滤愕,返回節(jié)點(diǎn)
nodelist: 節(jié)點(diǎn)列表
kv_map: 匹配屬性及屬性值map'''
result_nodes = []
for node in nodelist:
if if_match(node, kv_map):
result_nodes.append(node)
return result_nodes
# ---------------change ----------------------
def change_node_properties(nodelist, kv_map, is_delete=False):
'''修改/增加 /刪除 節(jié)點(diǎn)的屬性及屬性值
nodelist: 節(jié)點(diǎn)列表
kv_map:屬性及屬性值map'''
for node in nodelist:
for key in kv_map:
if is_delete:
if key in node.attrib:
del node.attrib[key]
else:
node.set(key, kv_map.get(key))
def change_node_text(nodelist, text, is_add=False, is_delete=False):
'''''改變/增加/刪除一個(gè)節(jié)點(diǎn)的文本
nodelist:節(jié)點(diǎn)列表
text : 更新后的文本'''
for node in nodelist:
if is_add:
node.text += text
elif is_delete:
node.text = ""
else:
node.text = text
def create_node(tag, property_map, content):
'''新造一個(gè)節(jié)點(diǎn)
tag:節(jié)點(diǎn)標(biāo)簽
property_map:屬性及屬性值map
content: 節(jié)點(diǎn)閉合標(biāo)簽里的文本內(nèi)容
return 新節(jié)點(diǎn)'''
element = Element(tag, property_map)
element.text = content
return element
def add_child_node(nodelist, element):
'''''給一個(gè)節(jié)點(diǎn)添加子節(jié)點(diǎn)
nodelist: 節(jié)點(diǎn)列表
element: 子節(jié)點(diǎn)'''
for node in nodelist:
node.append(element)
def del_node_by_tagkeyvalue(nodelist, tag, kv_map):
'''''同過屬性及屬性值定位一個(gè)節(jié)點(diǎn),并刪除之
nodelist: 父節(jié)點(diǎn)列表
tag:子節(jié)點(diǎn)標(biāo)簽
kv_map: 屬性及屬性值列表'''
for parent_node in nodelist:
children = parent_node.getchildren()
for child in children:
if child.tag == tag and if_match(child, kv_map):
parent_node.remove(child)
if __name__ == "__main__":
################ 1. 讀取xml文件 ##########
vcxproj_file = input("input vcxproj file full path:")
choice = input("1. change output path to ..\\bin\\win64_vc12_$(Configuration)\\ \n"
"2. release disable optimization and generate pdb file \n"
"3. restore release default configuriton \n")
tree = read_xml(vcxproj_file)
################ 2. 屬性修改 ###############
if '1' == choice:
root = tree.getroot() # 找到父節(jié)點(diǎn)
outdir_node = create_node("OutDir", {}, "..\\bin\\win64_vc12_$(Configuration)\\")
outdir_node.tail = '\n\t'
property_group = create_node("PropertyGroup", {}, "")
property_group.tail = '\n\t'
property_group.append(outdir_node)
root.append(property_group)
elif '2' == choice:
# release 啟用增量編譯
linkincremental_node = find_nodes(tree, "PropertyGroup/LinkIncremental"
, {"": "http://schemas.microsoft.com/developer/msbuild/2003"})
release_x64_link_node = get_node_by_keyvalue( \
linkincremental_node, \
{"Condition": "'$(Configuration)|$(Platform)'=='Release|x64'"})
change_node_text(release_x64_link_node, "true")
# release 是否生成調(diào)試信息
itemdefinitiongroup_node = find_nodes(tree, "ItemDefinitionGroup"
, {"": "http://schemas.microsoft.com/developer/msbuild/2003"})
release_x64_node = get_node_by_keyvalue( \
itemdefinitiongroup_node, \
{"Condition": "'$(Configuration)|$(Platform)'=='Release|x64'"})
clcompile_node = find_nodes(release_x64_node[0], "ClCompile"
, {"": "http://schemas.microsoft.com/developer/msbuild/2003"})
if clcompile_node is None:
print("can't find clcompile_release_x64_node")
exit(-1)
debuginformationformat_node = find_nodes(clcompile_node[0], \
"DebugInformationFormat", \
{"": "http://schemas.microsoft.com/developer/msbuild/2003"})
change_node_text(debuginformationformat_node, "ProgramDatabase")
optimization_node = find_nodes(clcompile_node[0], \
"Optimization", \
{"": "http://schemas.microsoft.com/developer/msbuild/2003"})
change_node_text(optimization_node, "Disabled")
debuginformationformat_node = find_nodes(clcompile_node[0], \
"DebugInformationFormat", \
{"": "http://schemas.microsoft.com/developer/msbuild/2003"})
minimalrebuild_node = create_node("MinimalRebuild", {}, "true")
minimalrebuild_node.tail ='\n\t'
clcompile_node[0].append(minimalrebuild_node)
link_node = find_nodes(release_x64_node[0], "Link"
, {"": "http://schemas.microsoft.com/developer/msbuild/2003"})
generatedebuginformation_node = find_nodes(link_node[0], \
"GenerateDebugInformation", \
{"": "http://schemas.microsoft.com/developer/msbuild/2003"})
change_node_text(generatedebuginformation_node, "true")
elif '3' == choice:
# release 啟用增量編譯
linkincremental_node = find_nodes(tree, "PropertyGroup/LinkIncremental"
, {"": "http://schemas.microsoft.com/developer/msbuild/2003"})
release_x64_link_node = get_node_by_keyvalue( \
linkincremental_node, \
{"Condition": "'$(Configuration)|$(Platform)'=='Release|x64'"})
change_node_text(release_x64_link_node, "false")
# release 是否生成調(diào)試信息
itemdefinitiongroup_node = find_nodes(tree, "ItemDefinitionGroup"
, {"": "http://schemas.microsoft.com/developer/msbuild/2003"})
release_x64_node = get_node_by_keyvalue( \
itemdefinitiongroup_node, \
{"Condition": "'$(Configuration)|$(Platform)'=='Release|x64'"})
clcompile_node = find_nodes(release_x64_node[0], "ClCompile"
, {"": "http://schemas.microsoft.com/developer/msbuild/2003"})
if clcompile_node is None:
print("can't find clcompile_release_x64_node")
exit(-1)
debuginformationformat_node = find_nodes(clcompile_node[0], \
"DebugInformationFormat", \
{"": "http://schemas.microsoft.com/developer/msbuild/2003"})
change_node_text(debuginformationformat_node, "None")
optimization_node = find_nodes(clcompile_node[0], \
"Optimization", \
{"": "http://schemas.microsoft.com/developer/msbuild/2003"})
change_node_text(optimization_node, "MaxSpeed")
minimalrebuild_node = find_nodes(clcompile_node[0], \
"MinimalRebuild", \
{"": "http://schemas.microsoft.com/developer/msbuild/2003"})
change_node_text(minimalrebuild_node, "False")
link_node = find_nodes(release_x64_node[0], "Link"
, {"": "http://schemas.microsoft.com/developer/msbuild/2003"})
generatedebuginformation_node = find_nodes(link_node[0], \
"GenerateDebugInformation", \
{"": "http://schemas.microsoft.com/developer/msbuild/2003"})
change_node_text(generatedebuginformation_node, "false")
write_xml(tree, vcxproj_file)