自动化构建平台调用Qmake编译工程文件


本篇文章主要讲述,如何使用批处理调用Qmake编译工程,因此需要对Qmake有一定的了解,熟悉Qmake工作流程。
话不多说上干货

1、新建一个文本文件“QtCreator_Compile”,修改后缀“.bat”,将以下内容复制、粘贴

@echo on
@rem V1.0 lichao 2017.03.22

REM 编译环境的安装路径,eg:QT_PATH=C:\Qt\Qt5.6.0\5.6\mingw49_32, 请勿加双引号
set QT_PATH=C:\Qt\Qt5.6.0\5.6\mingw49_32

REM QTBuild的配置选项,默认可以选择Debug,Release
set QTBuild_CONFIG=Release

REM 项目的参数:项目默认的编译路径,编译的目标,eg: PROJECT_PATH="D:\MyProject\trunk\Code"
set PROJECT_PATH="D:\MyProject\trunk\Code"

REM TARGETS,如果只编译一个工程,请填写此处,eg: TARGETS=test1\Project1.cbproj,否则留空。 
REM 存在多个工程中间用";"分号区分
REM 如果存在依赖关系,需要将被依赖的先编译,即放在前面,工程路径配置,只能一行,不可以换行
set TARGETS=lib\quazip\quazip.pro

REM 呼叫AIP编译内核 ,对所设置的参数进行处理
ECHO 转移到本批处理所在目录
%~d0
cd %~p0
call QtCreator_CompileKernel.bat

这是第一个bat文件,提供可修改项,只需要修改路径即可

下面是真是实现编译过程的批处理

2、新建一个文本文件“QtCreator_CompileKernel”,修改后缀".bat",老规矩复制黏贴以下内容

@echo on
@echo AIP QtCreator_Compile
@rem lichao 2017.03.22

REM AIP的参数
set HELLO=AIP:-)
set RESULT_FILE=%~d0%~p0CompileResult.txt
set FINISH_FILE=%~d0%~p0CompileFinish.txt



@echo %HELLO%Result File Name [%RESULT_FILE%]
echo ACTION,PROJECT,RESULT > %RESULT_FILE%

set PRJ_EXT=.pro
set PRJ_I=1

REM =============检查qtenv2.bat是否存在===============
@ECHO %HELLO%[Finding QtCreator_Compile tools]...
if not exist "%QT_PATH%\bin\qtenv2.bat" goto :label_exist_fail


REM ==============运行qtenv2.bat=====================
@ECHO %HELLO%[Setting environment by running QtCreator tools——qtenv2.bat]...
call "%QT_PATH%\bin\qtenv2.bat"


REM ===============工程编译==============
@ECHO %HELLO%[编译工程]...
set BUILD_ERROR=0
for %%i in (%TARGETS%) do call :fun_build %%i

if %BUILD_ERROR% == 1 echo %HELLO%[编译工程]失败
if not %BUILD_ERROR% == 1 echo %HELLO%[编译工程]成功

REM 如果不想中止的话,注释掉这一行
REM if %BUILD_ERROR% == 1 goto :label_end
call :fun_space_one_line


rem =================结束================
goto :label_end
rem =====================================


rem ================fun_set_compile_path================
:fun_set_compile_path
echo %HELLO%设置默认项目路径...
echo Default Compile Path:%1, DRV:%~d1
cd %1
%~d1
echo %HELLO%完成设置默认项目路径的设置
call :fun_space_one_line 
goto :eof


rem ================fun_set_project_compile_path================
:fun_set_project_compile_path
echo %HELLO%设置编译路径环境...
echo Project Path:%~p1
cd %~p1
echo %HELLO%完成编译路径环境的设置
call :fun_space_one_line 
goto :eof


rem ===============fun_build==================
rem 用Msbuild编译的子函数
:fun_build
call :fun_set_compile_path %PRJ_PATH%
call :fun_set_project_compile_path %1
set prj=%~n1%PRJ_EXT%
echo %HELLO%Build task NO %PRJ_I%
echo %HELLO%Building project [%prj%]...
set /a PRJ_I=%PRJ_I%+1
echo on

rem 执行qmake,根据该目录下指定的pro文件,生成Makefile文件;Makefile.Debug;Makefile.Release
qmake

rem 清空上一次编译结果;如果不清楚可能会出现编译错误
mingw32-make clean

rem 多核编译,如果机器是多核的,这一步很有用,编译速度飞起
mingw32-make -j

rem 编译工程,可选择编译的类型
mingw32-make -f Makefile.Release

@if not %errorlevel% == 0 goto :label_build_fail
@call :label_build_ok
goto :eof

rem ================label_build_fail====================
:label_build_fail
@echo on
echo BUILD,%prj%,FAIL >> %RESULT_FILE%
echo %HELLO%BUILD FAIL! [%prj%]
set BUILD_ERROR=1
goto :eof

rem ================label_build_ok====================
:label_build_ok
@echo on
echo Build,%prj%,OK >> %RESULT_FILE%
echo %HELLO%BUILD OK! [%prj%]
goto :eof


REM ===============fun_space_one_line=================================
REM 分隔一次显示
:fun_space_one_line
echo on
@echo =============================================
@echo on 
goto :eof


rem ================label_exist_fail====================
:label_exist_fail
echo %HELLO%Cannot find "qmake.exe", please check the QT_PATH! 
echo Cannot build! >> %RESULT_FILE%
set BUILD_ERROR=1
goto :label_end
goto :eof

rem ===============label_end==========================================
:label_end
SET RESULT=OK 
if %BUILD_ERROR% == 1 set RESULT=FAIL
if %BUILD_ERROR% == 1 echo %HELLO%工程编译失败!
if %RESULT% == OK echo %HELLO%所有编译任务执行成功!
if %RESULT% == OK exit 0
echo %RESULT% > %FINISH_FILE%
echo BuildVersion: QtCreator5_6_0 >> %RESULT_FILE%
goto :eof


工程编译结果会被保存在同级目录下的CompileResult.txt中

posted @ 2017-03-29 17:42  还要怎样  阅读(649)  评论(1编辑  收藏  举报