使用python批量编译Qt工程的脚本

使用python脚本编写而成,主要用于windows平台(msvc编译器)。

部署环境

  设置脚本内的build_list变量的值即可。

  • qmake.exe路径;
  • jom.exe路径;
  • 编译环境变量;
  • 编译项目。

源码

import os
import datetime
import subprocess

build_list = [
    {
        'qmake': r'C:\Qt\Qt5.12.2\5.12.2\msvc2017_64\bin\qmake.exe',
        'qmake_params': r'"CONFIG+=qtquickcompiler"',
        'jom': r'C:\Qt\Qt5.12.2\Tools\QtCreator\bin\jom.exe',
        'env_bat': r'C:\"Program Files (x86)"\"Microsoft Visual Studio"\2017\Community\VC\Auxiliary\Build\vcvarsall.bat amd64',
        'project': r'C:\Users\Document\Example.pro'
    }
]

class Builder:
    def __init__(self, project, qmake, qmake_params, jom, env_bat):
        self.project = project
        self.qmake = qmake
        self.qmake_params = qmake_params
        self.jom = jom
        self.env_bat = env_bat

        (self.qmake_path, self.qmake_name) = os.path.split(qmake)
        (self.jom_path, self.jom_name) = os.path.split(jom)
        (self.project_path, self.project_name) = os.path.split(project)

        self.qt_version = self.qmake_path.split('\\')[-3]
        self.compiler_version = self.qmake_path.split('\\')[-2]

    def execute_qmake_cmd_line(self):
        create_qmake_cmd_line = self.qmake + ' ' + self.project + ' -spec win32-msvc ' + self.qmake_params
        return True if execute(create_qmake_cmd_line) else False

    def execute_make_cmd_line(self):
        cmd_line = '{0} -j8'.format(self.jom)
        return True if execute(cmd_line) else False

    def execute_make_clean_cmd_line(self):
        if os.path.exists('Makefile') | os.path.exists('makefile'):
            return True if execute('{0} clean'.format(self.jom)) else False

        return True

    def init_env(self):
        tmp = os.popen("{0} && {1}".format(self.env_bat, 'set'))
        env_str = tmp.read()
        tmp.close()

        env_list = str(env_str).split('\n')

        for each in env_list:
            if '=' in each:
                key = each.split('=')[0].lower()
                value = each.split('=')[1]
                os.environ[key] = value

        return True

    def build(self):
        os.chdir(self.project_path)
        print('Starting build [{0}]'.format(os.path.splitext(self.project_name)[0]))

        isOk = True
        status = self.init_env()
        isOk = isOk and status
        print('Init env:   ', 'Success' if status else 'Failed')

        status = self.execute_make_clean_cmd_line()
        isOk = isOk and status
        print('Make clean: ', 'Success' if status else 'Failed')

        status = self.execute_qmake_cmd_line()
        isOk = isOk and status
        print('QMake:      ', 'Success' if status else 'Failed')

        status = self.execute_make_cmd_line()
        isOk = isOk and status
        print('Make:       ', 'Success' if status else 'Failed')

        print('End build   ', 'Success' if isOk else 'Failed')
        return isOk

def execute(cmd):
    result = subprocess.run(cmd, encoding='gbk',shell=True, capture_output=True)
    if result.returncode != 0:
        print(result.stdout, result.stderr)

    return True if result.returncode == 0 else False

def update_project_code(project_path, branch):
    print('Update ', project_path)
    os.chdir(project_path)
    return execute('git pull origin ' + branch)


def update_code(build_list):
    print('Updating...')
    for each in build_list:
        project = '' if each.get('project') == None else each.get('project')
        path = os.path.split(project)[0]
        if not update_project_code(path, 'master'):
            print('Updated failed')
            exit(-1)

    print('Updated success')

def build(build_list):
    for each in build_list:
        qmake = each.get('qmake')
        qmake_params = '' if each.get('qmake_params') == None else each.get('qmake_params')
        jom = each.get('jom')
        env_bat = each.get('env_bat')
        project = each.get('project')

        builder = Builder(project, qmake, qmake_params, jom, env_bat)
        if (builder.build() == False):
            print('[{0}] Deploy failed!!!'.format(datetime.datetime.now()))
            exit(-1)


if __name__ == '__main__':
    print('[{0}] Start deploy...'.format(datetime.datetime.now()))
    # update_code(build_list)
    build(build_list)
    print('[{0}] Deploy success!!!'.format(datetime.datetime.now()))

你可能感兴趣的:(qt,python)