AMBuild

什么是AMBuild?

    AMBuild是构建软件项目和创建发布包的工具.它是针对C++项目的,当然也可以用于其它任何语言的项目,它主要针对解决大多数构建工具所解决不了的三个大问题:

    1.准确性:不需要清理构建.大部分构建工具都是要先清理之前的项目生成残留,然后再重新生成项目工程,这样是非常不必要,也很浪费时间的.而AMBuild总是会以最小的计算然后精确地重建项目,当然,任何一下生成失败都将会使整个构建工作终止.

    2.速度:大部分构建工具都会在生成之前遍历所有的依赖项,看是否有改变.而AMBuild只需要查看改变过的文件.

    3.灵活性:构建脚本是用Python编写的,所以很容易写,并且能够提供全面的编程控制.

准备工作:

    AMBuild需要使用Python,版本控制在2.6或以上,如果使用的是Python 3,那么则需要3.1或以上.当然,我个人是在Windows下的,这里我们可以不太在意此问题,因为等会为大家介绍一个合成工具,已经集合了Python的.我在Windows系统下的是使用名为MozillaBuild的,可以直接从这里下载:https://wiki.mozilla.org/MozillaBuild,我目前用的是最新版本1.10.0,建议下载安装到C盘,或是系统的安装盘.

    另外AMBuild已在以下运行平台中做过测试,运行正常:

Windows (7 x86 and x64, XP x86)
Linux (x86 and x64)
OS X (10.7-10.9)
FreeBSD (9.2-x64)
OpenBSD (5.4 amd64, i386)
Solaris (11, x64)
Cygwin (x64)

    测试过的编译器分别有:

Visual Studio 2010+
GCC 4+
Clang 3+
Emscripten 1.25+

    接着输入以下两行,命,这个工具就不贴链接了,自己搜一下就OK了,通过git下载https://github.com/alliedmodders/ambuild.现在所有需要的东西都有了.

    现在我们在目录C:\mozilla-build中打开start-shell-msvc2010.bat,这个批处理会先检测一下是否已经安装了编译器,如果顺利的话,你可以看到类似以下的内容:

"Mozilla tools directory: C:\mozilla-build\"
Visual C++ 10 directory: c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\
Visual C++ 11 directory: C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\
Windows SDK directory: C:\Program Files (x86)\Windows Kits\8.0\
Windows SDK version: 8.0
Setting environment for using Microsoft Visual Studio 2010 x86 tools.
Using the installed Windows 8.0 SDK
Mozilla build environment: MSVC version 10.

[email protected] ~
$

    接着输入以下两行,命令,主要是把git的路径告诉Mozila

echo "export PATH=\$PATH:/c/Program\ Files\ \(x86\)/Git/bin" >> ~/.profile
source ~/.profile

    紧接着我们要CD到AMBuild的主目录,比如我的AMBuild的目录是:

~/Desktop/Source Engine/alliedmodders/ambuild

    然后输入以下命令:

python setup.py install

    注意:如果提示python为无效的命令,那么就在C:\mozilla-build找python的子目录,再把这个python的目录填入系统环境中就可以了

    顺利的话,你会看到以下内容

running install

running build

running build_py

creating build

creating build\lib

creating build\lib\ambuild

copying ambuild\cache.py -> build\lib\ambuild

copying ambuild\command.py -> build\lib\ambuild

copying ambuild\cpp.py -> build\lib\ambuild

copying ambuild\job.py -> build\lib\ambuild

copying ambuild\osutil.py -> build\lib\ambuild

copying ambuild\runner.py -> build\lib\ambuild

copying ambuild\worker.py -> build\lib\ambuild

copying ambuild\__init__.py -> build\lib\ambuild

creating build\lib\ambuild2

copying ambuild2\builder.py -> build\lib\ambuild2

copying ambuild2\context.py -> build\lib\ambuild2

copying ambuild2\damage.py -> build\lib\ambuild2

copying ambuild2\database.py -> build\lib\ambuild2

copying ambuild2\graph.py -> build\lib\ambuild2

copying ambuild2\nodetypes.py -> build\lib\ambuild2

copying ambuild2\run.py -> build\lib\ambuild2

copying ambuild2\task.py -> build\lib\ambuild2

copying ambuild2\util.py -> build\lib\ambuild2

copying ambuild2\__init__.py -> build\lib\ambuild2

creating build\lib\ambuild2\frontend

copying ambuild2\frontend\paths.py -> build\lib\ambuild2\frontend

copying ambuild2\frontend\prep.py -> build\lib\ambuild2\frontend

copying ambuild2\frontend\version.py -> build\lib\ambuild2\frontend

copying ambuild2\frontend\__init__.py -> build\lib\ambuild2\frontend

creating build\lib\ambuild2\ipc

copying ambuild2\ipc\bsd.py -> build\lib\ambuild2\ipc

copying ambuild2\ipc\generic_poll.py -> build\lib\ambuild2\ipc

copying ambuild2\ipc\impl.py -> build\lib\ambuild2\ipc

copying ambuild2\ipc\linux.py -> build\lib\ambuild2\ipc

copying ambuild2\ipc\posix_proc.py -> build\lib\ambuild2\ipc

copying ambuild2\ipc\process.py -> build\lib\ambuild2\ipc

copying ambuild2\ipc\winapi.py -> build\lib\ambuild2\ipc

copying ambuild2\ipc\windows.py -> build\lib\ambuild2\ipc

copying ambuild2\ipc\__init__.py -> build\lib\ambuild2\ipc

creating build\lib\ambuild2\frontend\amb2

copying ambuild2\frontend\amb2\gen.py -> build\lib\ambuild2\frontend\amb2

copying ambuild2\frontend\amb2\__init__.py -> build\lib\ambuild2\frontend\amb2

creating build\lib\ambuild2\frontend\base

copying ambuild2\frontend\base\gen.py -> build\lib\ambuild2\frontend\base

copying ambuild2\frontend\base\__init__.py -> build\lib\ambuild2\frontend\base

creating build\lib\ambuild2\frontend\cpp

copying ambuild2\frontend\cpp\builders.py -> build\lib\ambuild2\frontend\cpp

copying ambuild2\frontend\cpp\compilers.py -> build\lib\ambuild2\frontend\cpp

copying ambuild2\frontend\cpp\detect.py -> build\lib\ambuild2\frontend\cpp

copying ambuild2\frontend\cpp\vendors.py -> build\lib\ambuild2\frontend\cpp

copying ambuild2\frontend\cpp\__init__.py -> build\lib\ambuild2\frontend\cpp

creating build\lib\ambuild2\frontend\vs

copying ambuild2\frontend\vs\cxx.py -> build\lib\ambuild2\frontend\vs

copying ambuild2\frontend\vs\export_vcxproj.py -> build\lib\ambuild2\frontend\vs

copying ambuild2\frontend\vs\gen.py -> build\lib\ambuild2\frontend\vs

copying ambuild2\frontend\vs\graph.py -> build\lib\ambuild2\frontend\vs

copying ambuild2\frontend\vs\nodes.py -> build\lib\ambuild2\frontend\vs

copying ambuild2\frontend\vs\xmlbuilder.py -> build\lib\ambuild2\frontend\vs

copying ambuild2\frontend\vs\__init__.py -> build\lib\ambuild2\frontend\vs

running build_scripts

creating build\scripts-2.7

copying and adjusting scripts\ambuild -> build\scripts-2.7

copying scripts\ambuild.bat -> build\scripts-2.7

running install_lib

running install_scripts

copying build\scripts-2.7\ambuild -> c:\mozilla-build\python\Scripts

running install_egg_info

Removing c:\mozilla-build\python\Lib\site-packages\AMBuild-2.0-py2.7.egg-info

Writing c:\mozilla-build\python\Lib\site-packages\AMBuild-2.0-py2.7.egg-info

    那么到此步我们的AMBuild已经安装完成了.可以在控制台中输入"ambuild"正常情况下会提示"Error: folder was not configured for AMBuild.",因为我们还没有创建AMBuild的构建工程.

如何使用AMBuild?

    我们依旧拿最为经典的Hello World来做示例,我们可以在桌面创建一下空的文件夹,命名为Demo的文件夹,然后CD到Demo这个目录下,并输入以下命令:

ambuild --new-project

    构建工程创建成功后,我们可以在Demo的目录下看到AMBuildScript与configure.py这两个文件,我们只需要打开AMBuildScript,内容如下:

# vim: set sts=2 ts=8 sw=2 tw=99 et ft=python:

builder.DetectCompilers()

builder.compiler.cflags += [

  ‘-Wall‘,

  ‘-Werror‘

]

program = builder.compiler.Program(‘sample‘)

program.sources += [

  ‘main.cpp‘,

]

builder.Add(program)

    暂时先把AMBuildScript改成这样:

# vim: set sts=2 ts=8 sw=2 tw=99 et ft=python:

builder.DetectCompilers()

if builder.target_platform == ‘linux‘:

  builder.compiler.cflags += [

    ‘-Wall‘,

    ‘-Werror‘

  ]

program = builder.compiler.Program(‘sample‘)

program.sources += [

  ‘main.cpp‘,

]

builder.Add(program)

    注:关于AMbuild 的API,今天暂时先不详细说明.但是我们可以从AMBuildScript看出来难度不是很大,只要把API记住了,就可以写出很好的编译脚本了^^

    修改完AMBuildScript后,别忘了在Demo目录下创建main.cpp文件,并在里面写入以下代码并保存

#include <stdio.h>

int main(int argc, char *argv[])

{

    printf("Hello World\n");

    getchar();

    return 0;

}

    接下来的工作还是在命令符操作中完成,有一点要记住的是,要使用AMBuild,命令行必需要从C:\mozilla-build目录下使用start-shell-msvc2010.bat或start-shell-msvc2012.bat等,我试过用其它的系统命令符,不可用,还有一个别打开x64的命令符.依旧把路径CD到我们的Demo目录下,依次执行

mkdir build

cd build

python ../configure.py

ambuild

    正常的情况如下

[email protected] ~/Desktop/demo

$ mkdir build

[email protected] ~/Desktop/demo

$ cd build

[email protected] ~/Desktop/demo/build

$ python ../configure.py

Checking CC compiler (vendor test msvc)... [‘cl‘, ‘test.c‘, ‘-o‘, ‘test.exe‘, ‘-nologo‘, ‘-showIncludes‘]

found msvc version 1600

Checking CXX compiler (vendor test msvc)... [‘cl‘, ‘test.cpp‘, ‘-o‘, ‘testp.exe‘, ‘-nologo‘, ‘-showIncludes‘]

found msvc version 1600

[email protected] ~/Desktop/demo/build

$ ambuild

[create] mkdir -p sample

Spawned taskmaster (pid: 6820)

Spawned worker (pid: 5876)

[5876] cl /Zi /showIncludes /nologo /c c:\Users\LittleKu\Desktop\demo\main.cpp /Fomain.obj

main.cpp

[5876] cl main.obj /link /nologo /OUT:sample.exe /nologo /DEBUG /PDB:"sample.pdb"

Build succeeded.

[email protected] ~/Desktop/demo/build

$

    到此,demo就已经创建成功了,去你的build目录下看下吧.

    本文章目前只限配置AMBuild与如果通过AMBuild生成目标文件.再次说明,AMbuild的API会在往后中加入解释,如有不便之处,敬请谅解哈^^

    email:[email protected]

    QQ:116268742

时间: 2024-10-13 23:24:58

AMBuild的相关文章