Table of Contents
By far the simplest way to generate makefiles using Bakefile is to use so-called presets which are prepared skeletons of bakefiles that get you started quickly. Let's see how it works on an example of the famous Hello, world program written in C and implemented in the hello.c
file:
现在,用Bakefile生成makefile最简单的方法是,使用一些预先设定的框架 presets,它可以让你快速起步。让我们看看bakefile如何应用于这个著名的用C语言实现的Hello, world 例子,源文件是hello.c
:
#include <stdio.h> int main() { printf("Hello, world!\n"); return 0; }
The bakefile needed to compile it,hello.bkl
, looks like this:
Bakefile需要编译它,hello.bkl
, 如下:
<?xml version="1.0"?> <makefile> <include file="presets/simple.bkl"/> <exe id="hello" template="simple"> <sources>hello.c</sources> </exe> </makefile>
Presets are included using the include directive. The structure of the file name is always the same: presets/NAME-OF-PRESET.bkl
. In general, you can combine several presets, but in practice you must be careful when doing so. It's always a good idea to read the code for the preset before using it. The "simple" preset we include here defines a DEBUG
option and a template called simple
. Generated makefiles will allow the user to build all targets that are based on this template as either debug or release build.
使用include 命令来导入preset。文件名的结构总是如下: presets/NAME-OF-PRESET.bkl
。通常,你可以合并使用多个preset,但是实际操作的时候,你最好小心点来。导入之前最好先读读preset的代码。我们这里导入的 "simple" preset 定义了一个 DEBUG
option 和一个simple
template 。 生成的makefile文件可以让用户基于这个template 来生成所有的target的debug 或 release build。
Let's generate some makefiles now. The bakefile command is used to do it. For example:
Le现在我们来生成makefile。bakefile命令就是干这个的,例:
$
bakefile -f msvc hello.bkl
That's all. This will creates VC++ makefile makefile.vc
. Of course, you can change the name of output file if you don't like the default:
搞定。现在会生成VC++的makefile makefile.vc
。当然,如果你不喜欢默认的输出文件名,你可以换:
$
bakefile -f msvc -o makefile hello.bkl
Bakefile will also generate the Makefile.in
files used by Autoconf:
Bakefile 同时可以生成 Autoconf 使用的 Makefile.in
文件:
$
bakefile -f autoconf hello.bkl
These are templates for makefiles. Autoconf also requires a configure.ac
script (previously, configure.in
), but Bakefile will not generate this for you. This script checks for platform features necessary to build the program; see the autoconf manual for details.
These are templates for makefiles. Autoconf 还需要一个 configure.ac
脚本 (之前版本 configure.in
),但Bakefile不会帮你生成它。这个脚本检查系统平台是否满足build当前program,详见 autoconf 手册。
When producing autoconf format output, Bakefile will also generate a file called autoconf_inc.m4
which defines macros needed by the generated Makefile.in files. To use this, call the AC_BAKEFILE
macro within your configure.ac
script.
生成 autoconf format 输出的时候,Bakefile 同时会生成一个叫做 autoconf_inc.m4
的文件,它定义了生成的 Makefile.in 文件需要的宏。应用中,在 configure.ac
脚本里调用 AC_BAKEFILE
宏。
A minimal configure.ac
script for our example program would look like this:
我们的示例program的最精简 configure.ac
可能格式如下:
AC_PREREQ(2.53) AC_INIT(aclocal.m4) AC_CANONICAL_SYSTEM DEBUG=0 AC_BAKEFILE([m4_include(autoconf_inc.m4)]) AC_OUTPUT([Makefile])
Note the part that sets the DEBUG
variable. Any options declared in your bakefile must be set to some default value before calling AC_BAKEFILE
. The simple.bkl preset defines the DEBUG
option, so we have to give it a default value here.
注意设置 DEBUG
变量的位置。你的bakefile定义(?)的任何option在调用 AC_BAKEFILE
之前必须给定一个默认值。 "simple.bkl" preset 定义了 DEBUG
option,所以我们得给它个默认值。
While the above code will work, there's a better way to handle the debug option:
虽然上面的代码可以工作,这儿有个更好的方法来管理 debug option:
AC_PREREQ(2.53) AC_INIT(aclocal.m4) AC_CANONICAL_SYSTEM AC_ARG_ENABLE(debug, [ --enable-debug Enable debugging information], USE_DEBUG="$enableval", USE_DEBUG="no") if test $USE_DEBUG = yes ; then DEBUG=1 dnl Bakefile doesn't touch {C,CPP,CXX,LD}FLAGS in autoconf format, we dnl have to do it ourselves. This will work with many compilers dnl (but not all, proper configure script would check if the compiler dnl supports it): CFLAGS="$CFLAGS -g" else DEBUG=0 fi AC_BAKEFILE([m4_include(autoconf_inc.m4)]) AC_OUTPUT([Makefile])
You are ready to generate Autoconf's configure
script now:
你现在已经准备好生成 Autoconf 的 configure
脚本了:
$
bakefilize --copy && aclocal && autoconf
No comments:
Post a Comment