目录
一、python自动编译项目
1、环境变量
1) CMAKE
2)VS
2、CMAKE 生成VS项目
1) cmake 命令介绍
2)python 调用cmake 命令
3、devenv编译VS项目
1) devenv介绍
2)devenv命令行开关
3) 自动编译VS项目
一、python自动编译项目
1、环境变量
1) CMAKE
并且把CMake 相关路径添加到环境变量中,
如果只是编译VS项目,不CMAKE的话,可以忽略CMAKE配置
2)VS
也可以设置环境变量的方法,也可以在python中实现
def check_VS_installed():paths = os.getenv('PATH').split(';')hasVisualStudio = Falsefor path in paths:if re.match(r'.*Visual Studio.*', path):hasVisualStudio = Trueprint('FOUND: %s' % path)if not hasVisualStudio:for path in ('C:\\Program Files\\Microsoft Visual Studio\\2022\\Community\\Common7\\IDE', ):if os.path.isdir(path):paths.append(path)os.environ['PATH'] = ';'.join(paths)return Truereturn Falsereturn True
2、CMAKE 生成VS项目
1) cmake 命令介绍
CMake是一个跨平台的自动化构建系统,可以用简单的语句来描述所有平台的编译过程。
CMake在编译时可以产生标准的makefile文件,然后使用make命令进行编译。
以下是一些常用的CMake命令:
cmake -B
: 指定构建目录。
cmake -S
: 指定源代码目录。
cmake --build
: 构建项目。
cmake --install
: 安装项目。
-D
选项可以用来定义变量
cmake -DCMAKE_BUILD_TYPE=Debug
-G
选项可以用来指定生成器
cmake -G "Visual Studio 16 2019"
-A
选项可以用来指定平台
cmake -A Win32
根据上述命令,结合自己项目,命令行形式的cmake命令如下:
cmake -B C:\\xnwvideo\\xnwobs\\obs-vs -S C:\\xnwvideo\\xnwobs\\obs-studio -D QTDIR:PATH=C:\\Qt\\6.5.0\\msvc2019_64 -D DepsPath:PATH=C:\\xnwvideo\\windows-deps-2023-04-12-x64 -G "Visual Studio 17 2022" -A x64
2)python 调用cmake 命令
BuildDir = "C:\\build-video"
CMAKE = "C:\\Program Files\\CMake\\bin\\cmake.exe"
QTDIR = 'C:\\Qt\\6.5.0\\msvc2019_64'
DepsPath = 'D:\\Projects\\windows-deps-2023-04-12-x64'
CWD = os.getcwd() #当前工作路径
if not os.path.isdir(DepsPath):DepsPath = os.path.join(os.path.split(CWD)[0], 'windows-deps-2023-04-12-x64')def cmake_sln():paths = os.getenv('PATH').split(';')paths.append(os.path.split(CMAKE)[0])os.environ['PATH'] = ';'.join(paths)cmd = 'cmake -B' + os.path.join(CWD, 'obs-vs')cmd += ' -S' + os.path.join(CWD, 'obs-studio')cmd += ' -DQTDIR:PATH=' + QTDIRcmd += ' -DDepsPath:PATH=' + DepsPathcmd += ' -G "Visual Studio 17 2022" -A x64'print(cmd)os.system(cmd)
3、devenv编译VS项目
上一步,使用cmake编译成了 sln 项目工程
这一步,使用devenv 自动编译这个项目,生成exe
1) devenv介绍
devenv
是Visual Studio的可执行程序,我的安装在“C:\Program Files\Microsoft Visual Studio\2022\Community\Common7\IDE”下。也可能安装在C:\Program Files (x86)下。
执行devenv
命令可以启动带界面的VS编译器。
devenv
命令行可以使用多种开关来设置集成开发环境(IDE)的各个选项,以及从命令行生成、调试和部署项目。
如可以使用以下命令行来打开一个解决方案并生成它:
devenv /build Debug "C:\Users\username\Documents\Visual Studio 2010\Projects\WindowsApplication1\WindowsApplication1.sln"
2)devenv命令行开关
/build
: 生成指定的解决方案或项目。/clean
: 清除指定的解决方案或项目。/debugexe
: 在调试器的控制下加载可执行文件。/deploy
: 部署指定的解决方案或项目。/out
: 指定生成输出文件的目录。/project
: 指定要打开的项目文件。/rebuild
: 清除并重新生成指定的解决方案或项目。
3) 自动编译VS项目
devenv obs-vs/obs-studio.sln /Build "Release|x64"
python 实现自动编译
def match_rules(rules, line):import rejson = {'raw':line}if isinstance(rules, dict):rules = [rules]for idx, r in enumerate(rules):match = re.match(r['r'],line)if not match:continuel = match.groups()for i, f in enumerate(r['v']):if i >= len(l):breakjson[f] = l[i]return idx, jsonreturn -1, None
class DummyClass:pass
def call_cmdline(cmd, cb_output):with os.popen(cmd, 'r') as fd:while True:line = fd.readline()if not line:breakcb_output(line.strip())
def build(solution, project='', target='Release|x64', logfile='', clean=True):if clean:print('clean %s ...' % solution)cmd = 'devenv %s /Clean' % solutionif target:cmd += ' "%s"' % targetprint(cmd)os.system(cmd)print('build %s ...' % solution)cmd = 'devenv %s /Build' % solutionif target:cmd += ' "%s"' % targetif project:cmd += ' /project %s' % project#os.system(cmd)#returnprint(cmd)rules = [{'r':r'.*Build started:\s+Project:\s+(.*),\s+Configuration:\s+(.*)\s+.*','v':['project', 'config']},{'r':r'.*Build:\s+(\d+)\s+succeeded,\s+(\d+)\sfailed.*','v':['success', 'fail']}]data = DummyClass()data.idx = 0data.failed = 0data.projects = {}data.fd = open(logfile, 'w') if logfile else Nonedef cb(line):idx, matched = match_rules(rules, line)if idx == 0:project = matched['project']data.projects[project] = data.idxdata.idx += 1print('[%d] %s %s ...' % (data.idx, project, matched['config']))elif idx == 1:data.failed = int(matched['fail'])print('Success %s Failed %s' % (matched['success'], data.failed))if data.fd: data.fd.write('%s\n' % line)call_cmdline(cmd, cb)if data.fd: data.fd.close()return data.failed