ant调用make实现Makefile编译

2022-05-07 10:12:01 浏览数 (1)

上一篇博文《cmake:shell(bat,sh) 脚本方式生成Makefile》中完成了cmake生成Makefile脚本的过程,也就是步骤1,本文谈谈步骤2和步骤3的实现:

步骤2:

为了让ant能执行make,还得用脚本实现

linux sh脚本实现,build.sh:

代码语言:javascript复制
#!/bin/sh

export BUILD_FOLDER="`cd "$(dirname "$0")"; pwd`.prj"
## 判断Makefile是否存在,如果不存在,则调用newprj.sh生成Makefile
if [ ! -r "$BUILD_FOLDER/Makefile" ]
then 
    ./newprj.sh
fi

cd $BUILD_FOLDER
# 执行clean 清除已经有编译生成的文件
make clean
# make编译
make

windows bat脚本实现,build.bat:

代码语言:javascript复制
ECHO OFF
SET BUILD_FOLDER=�%.prj
IF not exist "%BUILD_FOLDER%/Makefile" call newprj.bat
pushd "%BUILD_FOLDER%"
make clean
make
popd

上面两个不同的脚本实现的功能类似,区别只是在不同的平台上运行,实现make clean和make的动作。

步骤3:

有了步骤2中的build.sh/build.bat,就可以在ant脚本中调用它们实现项目编译了。

代码语言:javascript复制
<project name="codemgr" basedir="." default="release">
    <property environment="env" />
    <import file="${basedir}/../ant/build-common-param.xml" />
    <!--判断操作系统类型-->
    <condition property="is.windows">
        <or>
            <os family="windows" />
            <os family="winnt" />
            <os family="win9x" />
        </or>
    </condition>
    <condition property="is.linux">
        <and>
            <os family="unix" />
            <os name="Linux" />
        </and>
    </condition>
    <property name="lib.dir" value="${basedir}/../${ant.project.name}.prj/lib"/>
    <available file="${lib.dir}" type="dir" property="lib.dir.present"/>
    <target name="release.linux" description="release naitve(linux .so)" if="is.linux">
        <fail message="NOT FOUND: ${lib.dir}" unless="lib.dir.present"/>
        <!--调用tar命令进行打包-->
        <exec executable="tar" dir="${lib.dir}" osfamily="unix">
            <arg line="-cf ${service.facedbservice.native.dir}/linux_x64/native.tar ."/>
        </exec>
    </target>
    <target name="release.windows" description="release native(windows .dll)" if="is.windows">
        <fail message="NOT FOUND: ${lib.dir}" unless="lib.dir.present"/>
        <copy todir="${service.facedbservice.native.dir}/windows_x64">
            <fileset dir="${lib.dir}" />
        </copy>
    </target>
    <!--清除项目编译目录下的所有文件调用newprj.bat/sh生成新的Makefile-->
    <target name="clean">       
        <exec executable="./newprj.sh" dir="${basedir}" osfamily="unix"/>
        <exec executable="newprj.bat" dir="${basedir}" osfamily="windows"/>
    </target>
    <!--调用build.bat/sh进行项目编译-->
    <target name="build">       
        <exec executable="./build.sh" dir="${basedir}" osfamily="unix"/>
        <exec executable="build.bat" dir="${basedir}" osfamily="windows"/>
    </target>
    <!--根据操作系统类型的不同,将编译生成的目标文件复制到指定目录-->
    <target name="release" depends="build,release.linux,release.windows"/>
    <target name="all" depends="clean,build,release"/>
</project>

0 人点赞