PHP 中 config.m4 的探索

PHP 中 config.m4 的探索

最近在看php扩展相关的东西,虽然来来回回编辑了好多次config.m4,并且也在技术社区看到了 config.m4是什么?什么作用? 类的问题,但是还是觉得有必要在深入的了解下。

.m4后缀的文件一般被当做 通用的宏处理,来看下官方的介绍:

GNU M4 is an implementation of the traditional Unix macro processor. It is mostly SVR4 compatible although it has some extensions (for example, handling more than 9 positional parameters to macros). GNU M4 also has built-in functions for including files, running shell commands, doing arithmetic, etc.

GNU M4 is a macro processor in the sense that it copies its input to the output expanding macros as it goes. Macros are either builtin or user-defined and can take any number of arguments. Besides just doing macro expansion, m4 has builtin functions for including named files, running UNIX commands, doing integer arithmetic, manipulating text in various ways, recursion etc... m4 can be used either as a front-end to a compiler or as a macro processor in its own right.

One of the biggest users of GNU M4 is the GNU Autoconf project.

简单而通俗易懂的翻译下:GNU M4是传统UNIX宏处理器的一种实现方式,它还具有一些内置功能,包括文件,shell,运算等。
作为一个宏处理器,将输入复制到扩展的输出,它要么内置,要么用户定义,且可以接受参数。另外这个还有内置函数,包括命名文件、运行UNIX命令、执行整数运算、以各种方式操作文本、递归等。M4既可以作为编译器的前端使用,也可以作为自己的宏处理器使用。
GNU M4的最大用户之一是GNU AutoCOF项目。

到这里大致了解到,它是作为一个宏处理器,然后再想想PHP扩展里面用到它做了什么,先看看 php源码扩展目录ext中 bcmath 中的代码:

dnl
dnl $Id$
dnl

PHP_ARG_ENABLE(bcmath, whether to enable bc style precision math functions,
[  --enable-bcmath Enable bc style precision math functions])

if test "$PHP_BCMATH" != "no"; then
  PHP_NEW_EXTENSION(bcmath, bcmath.c \
libbcmath/src/add.c libbcmath/src/div.c libbcmath/src/init.c libbcmath/src/neg.c libbcmath/src/outofmem.c libbcmath/src/raisemod.c libbcmath/src/rt.c libbcmath/src/sub.c \
libbcmath/src/compare.c libbcmath/src/divmod.c libbcmath/src/int2num.c libbcmath/src/num2long.c libbcmath/src/output.c libbcmath/src/recmul.c \
libbcmath/src/sqrt.c libbcmath/src/zero.c libbcmath/src/debug.c libbcmath/src/doaddsub.c libbcmath/src/nearzero.c libbcmath/src/num2str.c libbcmath/src/raise.c \
libbcmath/src/rmzero.c libbcmath/src/str2num.c,
  $ext_shared,,-I@ext_srcdir@/libbcmath/src)
  PHP_ADD_BUILD_DIR($ext_builddir/libbcmath/src)
  AC_DEFINE(HAVE_BCMATH, 1, [Whether you have bcmath])
fi

【dnl 在m4语法中相当于行注释的意思】

一些书籍中说明: config.m4是包含了配置时所执行的指令,例如上面这段代码很显然表明了,我写这个bcmath扩展,需要libbcmath/src/add.c,libbcmath/src/div.c 等等这些外部c源文件。PHP_NEW_EXTENSION()则是PHP定义的一个宏,最后的$ext_shared参数用来声明这个扩展不是一个静态模块,而是在php运行时动态加载的。
好像 我感觉还是不算太清晰,我在用一段白话来试图描述下吧。
config.m4文件中的代码会进入配置脚本的,也就是 configure。这里面包含 扩展的开关,扩展的名称,所需要的代码等等你想做的事情。为什么这么玩呢,因为PHP是使用 autoconf, automake, and libtool 3件套来构建扩展的,这3剑客一起使用,威力很大,但是也有点难。当扩展是PHP源码中的一部分时,我们可以在顶级目录 运行buildconf脚本,它会扫描每个子目录中的config.m4文件,然后他会把所有的配置文件config.m4合成一个 包含所有配置开关的 配置脚本。 这样的话,每个扩展就可以自己实现自己的配置检查,检查其所需的任何依赖和系统支持。区域这些想法和过程,宏检查和配置等工作,PHP选择了使用通过的M4脚本来配置

这里config.4文件的探索告一段落了,好像明白了一些了~~-。-


另外附一些PHP的宏,buildconf 处理config.m4所用:

AC_MSG_CHECKING(message)
在执行 configure 命令时输出“checking ”等信息。

AC_MSG_RESULT(value)
取得 AC_MSG_CHECKING 的执行结果,一般情况下 value 应为 yes 或 no。

AC_MSG_ERROR(message)
在执行 configure 命令时输出一条错误消息 message 并中止脚本的执行。

AC_DEFINE(name,value,description)
向 php_config.h 添加一行定义:#define name value // description (这对模块的条件编译很有用。)

AC_ADD_INCLUDE(path)
添加一条编译器的包含路径,比如用于模块需要为头文件添加搜索路径。

AC_ADD_LIBRARY_WITH_PATH(libraryname,librarypath)
指定一个库的连接路径。

AC_ARG_WITH(modulename,description,unconditionaltest,conditionaltest)
这是一款比较强大的宏,用于将模块的描述 description 添加到“configure –help”命令的输出里面。PHP 会检查当前执行的 configure 脚本里面有没有–with- 这个选项。 如果有则执行 unconditionaltest 语句(比如 –with-myext=yes 等), 此时,选项的值会被包含在 $withval 变量里面。否则就执行 conditionaltest 语句。

PHP_EXTENSION(modulename, [shared])
这个是配置你的扩展时 PHP 必定调用的一个宏。你可以在模块名后面提供第二个参数,用来表明是否将其编译为动态共享模块。这会导致在编译时为你的源码提供一个 COMPILE_DL_ 的定义。

上述有查到以下页面索取资料:
https://www.ibm.com/support/knowledgecenter/zh/ssw_aix_72/com.ibm.aix.genprogc/m4macro.htm
https://zh.wikipedia.org/wiki/M4_(%E7%A8%8B%E5%BC%8F%E8%AA%9E%E8%A8%80)
https://blog.csdn.net/timekeeperl/article/details/50738164
https://docstore.mik.ua/orelly/webprog/php/ch14_04.htm

##########作者:fredGui
##########来源:http://www.cnblogs.com/guixiaoming/p/8927233.html *
##########
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。*

posted @ 2018-04-24 11:40  fredgui  阅读(3230)  评论(0编辑  收藏  举报