SoFunction
Updated on 2025-03-10

Exploration on config.m4 in PHP

I'm looking at things related to php extensions recently. Although I have edited config.m4 many times back and forth, I have also seen it in the technical community.What is config.m4? What is the function?A question, but I still feel it is necessary to have an in-depth understanding.

Files with .m4 suffixes are generally treated as general macros. Let’s take a look at the official introduction:

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.

Simple and easy to understand translation: GNU M4 is an implementation method of traditional UNIX macro processors. It also has some built-in functions, including files, shells, operations, etc.
As a macro processor, the input is copied to the extended output, it is either built-in or user-defined and can accept parameters. In addition, this also has built-in functions, including naming files, running UNIX commands, executing integer operations, manipulating text in various ways, recursion, etc. M4 can be used as the front-end of the compiler or as its own macro processor.
One of the biggest users of GNU M4 is the GNU AutoCOF project.

At this point, I roughly understand that it is used as a macro processor. Then I think about what it does in the PHP extension. First, take a look at the code in bcmath in the php source code extension directory ext:

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,  \
libbcmath/src/ libbcmath/src/ libbcmath/src/ libbcmath/src/ libbcmath/src/ libbcmath/src/ libbcmath/src/ libbcmath/src/ \
libbcmath/src/ libbcmath/src/ libbcmath/src/ libbcmath/src/ libbcmath/src/ libbcmath/src/ \
libbcmath/src/ libbcmath/src/ libbcmath/src/ libbcmath/src/ libbcmath/src/ libbcmath/src/ libbcmath/src/ \
libbcmath/src/ libbcmath/src/,
 $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

【The meaning of dnl equivalent to line comments in m4 syntax】

Some books explain: config.m4 contains the instructions executed during configuration. For example, the above code obviously shows that I need external c source files such as libbcmath/src/, libbcmath/src/ and so on. PHP_NEW_EXTENSION() is a macro defined by PHP. The last $ext_shared parameter is used to declare that this extension is not a static module, but is loaded dynamically during php runtime.
It seems that I still don’t feel that it is too clear. I’m trying to describe it in a paragraph of vernacular.
The code in the config.m4 file will enter the configuration script, that is, configure. This contains the extension switch, the extension name, the required code, etc. What you want to do. Why play this way? Because PHP uses autoconf, automake, and libtool 3-piece set to build and expand. These 3 swordsmen are very powerful, but they are also a bit difficult. When the extension is part of the PHP source code, we can run the buildconf script in the top-level directory, which will scan the config.m4 file in each subdirectory, and then it will synthesize all the configuration files config.m4 into a configuration script containing all configuration switches. In this way, each extension can implement its own configuration checks, checking for any dependencies and system support it needs. Areas such ideas and processes, macro checking and configuration work, PHP chose to use the M4 script to configure it

The exploration of the config.4 file here has come to an end, and I seem to have understood a little~~-. -

In addition, some PHP macros are attached, and buildconf uses to process config.m4:

AC_MSG_CHECKING(message)
Output "checking" when executing the configure command” and other information.

AC_MSG_RESULT(value)
To obtain the execution result of AC_MSG_CHECKING, the value should generally be yes or no.

AC_MSG_ERROR(message)
When executing the configure command, output an error message and abort the execution of the script.

AC_DEFINE(name,value,description)
Add a line definition to php_config.h: #define name value // description (This is useful for conditional compilation of modules.)

AC_ADD_INCLUDE(path)
Add a compiler's include path, for example, if you need to add a search path to the header file for modules.

AC_ADD_LIBRARY_WITH_PATH(libraryname,librarypath)
Specifies the connection path to a library.

AC_ARG_WITH(modulename,description,unconditionaltest,conditionaltest)
This is a relatively powerful macro that adds the description of the module to the output of the "configure -help" command. PHP will check if there is any in the currently executed configure script – with-This option. If so, execute the unconditionaltest statement (such as –with-myext=yes, etc.), and the value of the option will be included in the $withval variable. Otherwise, execute the conditionaltest statement.

PHP_EXTENSION(modulename, [shared])
This is a macro that PHP must call when configuring your extension. You can provide a second parameter after the module name to indicate whether it is compiled into a dynamic shared module. This will result in a COMPILE_DL_ for your source code at compile timeDefinition of .

The following pages are found above to request information:
/support/knowledgecenter/zh/ssw_aix_72//
/wiki/M4_(%E7%A8%8B%E5%BC%8F%E8%AA%9E%E8%A8%80)
/timekeeperl/article/details/50738164
/orelly/webprog/php/ch14_04.htm

Summarize

This is the end of this article about config.m4 in PHP. For more related content in config.m4 in PHP, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!