一键开关VS的release模式优化

  因为我们公司的项目规模非常大了,如果日常调试使用debug模式的话,每次调试启动都要非常长的时间,因此大多数人使用release关优化的方式来进行日常开发。但是因为持续集成的存在,上传的代码要求是开启优化的,这样服务器才能打出优化后的版本。

  因为上面说的这种情况,导致我们的成员进行调试的时候,经常会调试进开了优化的dll中,那么这次的调试就宣告无果了,因为开了优化之后调试模式下的变量信息都是错乱的,必须关掉调试,关闭那个dll的优化,重新编译重新下断点,非常耗时。

  因此,我写了下面这个python脚本,用以方便程序员一键开关release模式下的优化。这样就可以在调试前关闭所有dll的优化,在提交前再开启所有优化了。代码如下:

import lxml
import codecs
import sys
import os
from lxml import etree
from lxml.html import clean

def ProcessOneFile(filePath, bMaxSpeed):
    oContent = etree.parse(filePath)
    root = oContent.getroot()
    ns1 = {}
    ns1['tt'] = root.nsmap[None]
    optimizationElements = root.xpath('//tt:ItemDefinitionGroup[contains(@Condition,"Release")]/tt:ClCompile/tt:Optimization', namespaces=ns1)
    for element in optimizationElements:
        element.text = 'MaxSpeed' if bMaxSpeed else 'Disabled'
    oContent.write(filePath, encoding = "utf-8", pretty_print = True, method = "xml", xml_declaration=True)

def searchFile(curpath, endWith, bMaxSpeed):
    try:
        os.chdir(curpath)
    except:
        return
    fl = os.listdir(os.curdir)
    for nFile in fl:
        if os.path.isfile(nFile):
            if nFile.endswith(endWith):
                print(nFile)
                ProcessOneFile(nFile, bMaxSpeed)
                return
    for nFile in fl:
        if os.path.isdir(nFile):
            searchFile(nFile, endWith, bMaxSpeed)
            os.chdir(os.pardir)


if __name__ == '__main__':
    bMaxSpeed = True
    if len(sys.argv) > 1:
        bMaxSpeed = sys.argv[1] == "MaxSpeed"
    fatherPath = os.getcwd()
    searchFile(fatherPath, ".vcxproj", bMaxSpeed)

使用方式就是将这个脚本放到你想开关代码的目录里。然后这个目录和其子目录下的所有工程都会处理。为了速度我还将其编成了exe,并用如下两个bat命令行来使用。

ModifyMaxSpeed MaxSpeed
ModifyMaxSpeed Disabled
原文地址:https://www.cnblogs.com/winnersun/p/10591647.html