多个依赖共同组件的Qt工程,如果共同依赖的组件有修改,则所有依赖该组建的Qt工程都要重新编译。
不论是使用命令行还是使用QtCreator逐个打开编译,都是一件繁琐的事情。
为简化此过程,使用python3脚本封装了 qmake pro_path -o compile_path 和 make --directory=compile_path 指令,结合指令路径下遍历查找 .pro 文件,实现了一条指令重新编译指定路径下所有 .pro 。
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Nov 3 10:01:48 2025
@author: farman
"""
import pathlib
import os
import sys
import tempfile
def get_files_recursive(path_to_search_in, file_ext):
files_found = []
p = pathlib.Path(path_to_search_in).absolute()
files = p.glob("*")
for file in files:
if file.is_dir():
#print("dir", file)
files_found += get_files_recursive(file, file_ext)
p = pathlib.Path(path_to_search_in).absolute()
files = p.glob(file_ext)
for file in files:
if file.is_file():
#print("file", file)
files_found.append(file)
return files_found
def compile_pro_linux(pro_file):
temp_dir = tempfile.gettempdir()
pro_path = str(pathlib.Path(pro_file).parent)
print("Compile : %s\n"%pro_path + '=' * (len(pro_path) + len("Compile : ")))
cmd = "cd %s && qmake . -o %s && make --directory=%s clean && make --directory=%s -j%s"%(pro_path, temp_dir, temp_dir, temp_dir, os.cpu_count())
print("Compile command:", cmd)
os.system(cmd)
return
def compile_pro_win(pro_file):
print("Windows is NOT supported yet.")
exit(0)
return
def compile_pro_macos(pro_file):
print("MacOS is NOT supported yet.")
exit(0)
return
def compile_pro_unkonwn(pro_file):
print("Unknown os, try as linux.")
compile_pro_linux(pro_file)
return
def compile_recursive(path_to_search_in):
file_ext = "*.pro"
files = get_files_recursive(path_to_search_in, file_ext)
count = 1
for file in files:
print("\n[ No.%s of %s ]"%(count, len(files)))
if sys.platform.startswith("win"):
compile_pro_win(file)
elif sys.platform.startswith("linux"):
compile_pro_linux(file)
elif sys.platform.startswith("darwin"):
compile_pro_macos(file)
else:
compile_pro_unkonwn(file)
count += 1
return
#------------------------------------------------------------------
if __name__ == "__main__":
if len(sys.argv) == 1:
path_to_search_in = "."
else:# len(sys.argv) == 2:
path_to_search_in = sys.argv[1]
compile_recursive(path_to_search_in)
使用方法:
保存上面的代码到本地 compile_all_qt_projects.py 文件;
python3 compile_all_qt_projects.py,则搜索脚本所在路径下所有的 .pro 并尝试编译;
python3 compile_all_qt_projects.py path_to_search_in,则搜索path_to_search_in路径下所有的 .pro 并尝试编译。
Enjoy!
3754

被折叠的 条评论
为什么被折叠?



