python configuration is still_通过Python配置关闭Release优化

通过自己之前转载的一篇文章,《VS设置增量编译》,在此基础上增加了Release版本如何关闭优化并自动生成pdb文件,最终实现思路也比较简单,就用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):

'''''判断某个节点是否包含所有传入参数属性

node: 节点

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):

'''''查找某个路径匹配的所有节点

tree: xml树

path: 节点路径'''

return tree.findall(path, namespace)

def get_node_by_keyvalue(nodelist, kv_map):

'''''根据属性及属性值定位符合的节点,返回节点

nodelist: 节点列表

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):

'''修改/增加 /删除 节点的属性及属性值

nodelist: 节点列表

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):

'''''改变/增加/删除一个节点的文本

nodelist:节点列表

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):

'''新造一个节点

tag:节点标签

property_map:属性及属性值map

content: 节点闭合标签里的文本内容

return 新节点'''

element = Element(tag, property_map)

element.text = content

return element

def add_child_node(nodelist, element):

'''''给一个节点添加子节点

nodelist: 节点列表

element: 子节点'''

for node in nodelist:

node.append(element)

def del_node_by_tagkeyvalue(nodelist, tag, kv_map):

'''''同过属性及属性值定位一个节点,并删除之

nodelist: 父节点列表

tag:子节点标签

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() # 找到父节点

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 是否生成调试信息

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 是否生成调试信息

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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值