您的位置:首页 > 其它

集成与构建指南(4)

2004-03-01 10:10 295 查看

1 构建基础知识

1.1 Ant构建脚本

自动化构建的核心是表达构件间依赖关系和构建步骤的脚本,Make工具使用makefile,而Ant使用了基于XML格式的缺省文件名为Build.Xml的配置文件。

1.1.1 Ant项目配置文件

Ant配置文件描述了一个构建项目(project),它由一些属性定义(property)和一个目标树(target tree)组成;目标代表了一个期望的构建结果(例如生成一个链接好的可执行文件),并表述了其依赖的其它目标,常见的构建目标有初始化(Init)、编译(Compile)、单元测试(Test)、安装(Install)、清除(Clean)等;每个目标包含了实现它而将要执行的任务(task),Ant支持的任务种类非常丰富,例如源码编译、文件拷贝、执行命令行操作等。Ant还提供一种非常重要的任务就是执行其它的项目配置文件,使得多个构建项目的批处理成为可能。

1.1.2 Ant配置文件示例

下面是一个Ant项目的配置文件示例:

<?xml version="1.0"?>
<project name="Improve-Install" default="InstallAll" basedir=".">
<description >
Build file for all Improve library project
</description>

<!-- task and type extension -->
<taskdef resource="cpptasks.tasks"/>
<typedef resource="cpptasks.types"/>

<taskdef resource="net/sf/antcontrib/antcontrib.properties"/>

<!-- Properties -->
<property environment="sys_env"/>
<property name="proj.root" location="../."/>
<target name="-Init">
<condition property="cc" value="${mswin32-compiler}">
<os family="windows"/>
</condition>
<condition property="cc" value="gcc">
<os family="unix"/>
</condition>
<echo message="${ant.project.name}: Init Complete"/>
</target>

<target name="Clean" depends="-Init"
description="Clean the temporal building space">
<!-- Delete all files in temporal build space -->
<delete includeEmptyDirs="true" >
<fileset dir="${tgt.bin}" includes="objs/*, objs_d/*" defaultexcludes="false"/>
</delete>
<delete includeEmptyDirs="true" >
<fileset dir="${tgt.lib}" includes="objs/*, objs_d" defaultexcludes="false"/>
</delete>
<echo message="${ant.project.name}: Clean buildspace Complete"/>
</target>

<target name="AutoBuild" depends="-Init, -Prep" description="AutoBuild all source to get bin out">
<mkdir dir="${tgt.lib}/objs_d"/>
<mkdir dir="${tgt.lib}/objs"/>
<if>
<equals arg1="${cc}" arg2="msvc"/>
<then>
<cc name="${cc}" objdir="${tgt.lib}/objs_d" outfile="${tgt.lib}/improve_debug" outtype="static"
exceptions="true" multithreaded="true" runtime="dynamic" debug="true">
<compiler extends="${cc}-compiler">
<includepath path="${tgt.interface}"/>
<includepath path="${getenv.BOOST_ROOT}/include"/>
<includepath path="${getenv.CPPUNIT_ROOT}/include"/>
<defineset >
<define name="WIN32"/>
<define name="_WINDOWS"/>
</defineset>
<compilerarg value="/W3"/>
<compilerarg value="/GR"/>
<compilerarg value="/O2"/>
</compiler>
<linker extends="${cc}-linker">
<linkerarg value="/machine:I386"/>
<!--fileset dir="${getenv.LIBS_ROOT}/lib" includes="3d_party.*"/-->
</linker>
<fileset dir="${tgt.src}">
<include name="**/*.cpp"/>
<include name="**/*.c"/>
<exclude name="exception_ex.*"/>
</fileset>
</cc>

<echo message="${ant.project.name}: AutoBuild Complete"/>
</target>

<target name="Test" depends="-Init, TestBuild" description="Perform acception testing">
<exec executable="test_main_debug.exe" dir="${tgt.bin}"
failonerror="true" vmlauncher="false" os="Windows 2000">
<env key="Path" path="${getenv.Path};${getenv.CPPUNIT_ROOT}/lib;${getenv.BOOST_ROOT}/lib"/>
</exec>
<echo message="test_main.exe Debug Version Testing Pass"/>
<exec executable="test_main.exe" dir="${tgt.bin}"
failonerror="true" vmlauncher="false" os="Windows 2000">
<env key="Path" path="${getenv.Path};${getenv.CPPUNIT_ROOT}/lib;${getenv.BOOST_ROOT}/lib"/>
</exec>
<echo message="test_main.exe Release Version Testing Pass"/>
<echo message="${ant.project.name}: Testing Complete"/>
</target>

<target name="InstallAll"
depends="CleanAll, AutoBuild, Install, Test"
description="Make a clean (re)install and test">
<echo message="${ant.project.name}: Install/Reinstall Complete"/>
</target>

</project>
在此Ant配置文件中定义了项目将引用的一些property,例如环境变量、项目的根目录路径、指定的编译器等;项目的缺省构建目标为InstallAll,它通过依赖定义,使得初始化(Init)、清除(CleanAll)、编译(AutoBuild)、安装(Install)、测试(Test)等目标依次执行;AutoBuild目标执行的cc任务,将对源码进行编译,并通过includepath设置了编译头文件的查找路径,通过linker段指定了需链接的库3d_party.lib。
本项目中各Ant配置文件的具体配置和使用,请参见Ant用户手册等参考资料,和阅读Ant配置文件本身内容。

1.1.3 Ant执行示例

Ant在命令行下执行的示例:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: