PHP 中 config.m4 的探索

PHP 中 config.m4 的探索

最近在看php扩展相关的东西,虽然来来回回编辑了好屡次config.m4,而且也在技术社区看到了 config.m4是什么?什么做用? 类的问题,可是仍是以为有必要在深刻的了解下。php

.m4后缀的文件通常被当作 通用的宏处理,来看下官方的介绍:html

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.web

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

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

到这里大体了解到,它是做为一个宏处理器,而后再想一想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语法中至关于行注释的意思】ui

一些书籍中说明: 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脚原本配置.net

这里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

著做权归做者全部。商业转载请联系做者得到受权,非商业转载请注明出处。

相关文章
相关标签/搜索