通过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)

©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,544评论 6 501
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,430评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 162,764评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,193评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,216评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,182评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,063评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,917评论 0 274
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,329评论 1 310
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,543评论 2 332
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,722评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,425评论 5 343
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,019评论 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,671评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,825评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,729评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,614评论 2 353