批量编译Qt工程

多个依赖共同组件的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!

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值