深入php内核四(自动构建系统)

PHP 提供了一套非常灵活的自动构建系统(automatic build system),它把所有的模块均放在 Ext 子目录下。每个模块除自身的源代码外,还都有一个用来配置该扩展的config.m4 文件(详情请参见http://www.gnu.org/software/m4/manual/m4.html )。

包括 .cvsignore 在内的所有文件都是由位于 Ext 目录下的 ext_skel 脚本自动生成的,它的参数就是你想创建模块的名称。这个脚本会创建一个与模块名相同的目录,里面包含了与该模块对应的一些的文件。

下面是操作步骤:

:~/cvs/php4/ext:> ./ext_skel –extname=my_module
Creating directory my_module
Creating basic files: config.m4 .cvsignore my_module.c php_my_module.h CREDITS EXPERIMENTAL tests/001.phpt my_module.php [done].

To use your new extension, you will have to execute the following steps:

1.  $ cd ..
2.  $ vi ext/my_module/config.m4
3.  $ ./buildconf
4.  $ ./configure –[with|enable]-my_module
5.  $ make
6.  $ ./php -f ext/my_module/my_module.php
7.  $ vi ext/my_module/my_module.c
8.  $ make

Repeat steps 3-6 until you are satisfied with ext/my_module/config.m4 and
step 6 confirms that your module is compiled into PHP. Then, start writing
code and repeat the last two steps as often as necessary.

这些指令就会生成前面所说的那些文件。为了能够在自动配置文件和构建程序中包含新增加的模块,你还需要再运行一次 buildconf 命令。这个命令会通过搜索 Ext 目录和查找所有 config.m4 文件来重新生成 configure 脚本。 默认情况下的的 config.m4 文件如例 3-1 所示,看起来可能会稍嫌复杂:

3.1 默认的 config.m4 文件

dnl $Id: build.xml,v 1.1 2005/08/21 16:27:06 goba Exp $
dnl config.m4 for extension my_module

dnl Comments in this file start with the string 'dnl'.
dnl Remove where necessary. This file will not work
dnl without editing.

dnl If your extension references something external, use with:

dnl PHP_ARG_WITH(my_module, for my_module support,
dnl Make sure that the comment is aligned:
dnl [  –with-my_module             Include my_module support])

dnl Otherwise use enable:

dnl PHP_ARG_ENABLE(my_module, whether to enable my_module support,
dnl Make sure that the comment is aligned:
dnl [  –enable-my_module           Enable my_module support])

if test "$PHP_MY_MODULE" != "no"; then
 dnl Write more examples of tests here…

 dnl # –with-my_module -> check with-path
 dnl SEARCH_PATH="/usr/local /usr"     # you might want to change this
 dnl SEARCH_FOR="/include/my_module.h"  # you most likely want to change this
 dnl if test -r $PHP_MY_MODULE/; then # path given as parameter
 dnl   MY_MODULE_DIR=$PHP_MY_MODULE
 dnl else # search default path list
 dnl   AC_MSG_CHECKING([for my_module files in default path])
 dnl   for i in $SEARCH_PATH ; do
 dnl     if test -r $i/$SEARCH_FOR; then
 dnl       MY_MODULE_DIR=$i
 dnl       AC_MSG_RESULT(found in $i)
 dnl     fi
 dnl   done
 dnl fi
 dnl
 dnl if test -z "$MY_MODULE_DIR"; then
 dnl   AC_MSG_RESULT([not found])
 dnl   AC_MSG_ERROR([Please reinstall the my_module distribution])
 dnl fi

 dnl # –with-my_module -> add include path
 dnl PHP_ADD_INCLUDE($MY_MODULE_DIR/include)

 dnl # –with-my_module -> chech for lib and symbol presence
 dnl LIBNAME=my_module # you may want to change this
 dnl LIBSYMBOL=my_module # you most likely want to change this

 dnl PHP_CHECK_LIBRARY($LIBNAME,$LIBSYMBOL,
 dnl [
 dnl   PHP_ADD_LIBRARY_WITH_PATH($LIBNAME, $MY_MODULE_DIR/lib, MY_MODULE_SHARED_LIBADD)
 dnl   AC_DEFINE(HAVE_MY_MODULELIB,1,[ ])
 dnl ],[
 dnl   AC_MSG_ERROR([wrong my_module lib version or lib not found])
 dnl ],[
 dnl   -L$MY_MODULE_DIR/lib -lm -ldl
 dnl ])
 dnl
 dnl PHP_SUBST(MY_MODULE_SHARED_LIBADD)

 PHP_NEW_EXTENSION(my_module, my_module.c, $ext_shared)
fi

如果你不太熟悉 M4 文件(现在毫无疑问是熟悉 M4 文件的大好时机),那么就可能会有点糊涂。但是别担心,其实非常简单。

注意:凡是带有 dnl 前缀的都是注释,注释是不被解析的。

config.m4 文件负责在配置时解析configure 的命令行选项。这就是说它将检查所需的外部文件并且要做一些类似配置与安装的任务。

默认的配置文件将会在 configure 脚本中产生两个配置指令:–with-my_module 和 –enable-my_module。当需要引用外部文件时使用第一个选项(就像用 –with-apache 指令来引用 Apache 的目录一样)。第二个选项可以让用户简单的决定是否要启用该扩展。不管你使用哪一个指令,你都应该注释掉另外一个。也就是说,如果你使用了–enable-my_module,那就应该去掉–with-my_module。反之亦然。

默认情况下,通过 ext_skel 创建的 config.m4 都能接受指令,并且会自动启用该扩展。启用该扩展是通过 PHP_EXTENSION 这个宏进行的。如果你要改变一下默认的情况,想让用户明确的使用 –enable-my_module 或 –with-my_module 指令来把扩展包含在 PHP 二进制文件当中,那么将 “if test "$PHP_MY_MODULE" != "no"”改为“if test "$PHP_MY_MODULE" == "yes"”即可。

if test "$PHP_MY_MODULE" == "yes"; then dnl
   Action.. PHP_EXTENSION(my_module, $ext_shared)
   fi 

这样就会导致在每次重新配置和编译 PHP 时都要求用户使用 –enable-my_module 指令。 

另外请注意在修改 config.m4 文件后需要重新运行 buildconf 命令。

你可能感兴趣的:(PHP)