downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

Our source is open

The syntax highlighted source is automatically generated by PHP from the plaintext script. If you're interested in what's behind the several functions we used, you can always take a look at the source of the following files:

Of course, if you want to see the source of this page, we have it available. You can also browse the SVN repository for this website on svn.php.net.

Source of: /manual/en/internals2.buildsys.configunix.php

<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$PARENTS = array();
include_once
dirname(__FILE__) ."/toc/internals2.buildsys.inc";
$setup = array (
 
'home' =>
  array (
   
0 => 'index.php',
   
1 => 'PHP Manual',
  ),
 
'head' =>
  array (
   
0 => 'UTF-8',
   
1 => 'en',
  ),
 
'this' =>
  array (
   
0 => 'internals2.buildsys.configunix.php',
   
1 => 'Talking to the UNIX build system: config.m4',
  ),
 
'up' =>
  array (
   
0 => 'internals2.buildsys.php',
   
1 => 'The PHP 5 build system',
  ),
 
'prev' =>
  array (
   
0 => 'internals2.buildsys.skeleton.php',
   
1 => 'The ext_skel script',
  ),
 
'next' =>
  array (
   
0 => 'internals2.buildsys.configwin.php',
   
1 => 'Talking to the Windows build system: config.w32',
  ),
);
$setup["toc"] = $TOC;
$setup["parents"] = $PARENTS;
manual_setup($setup);

manual_header();
?>
<div id="internals2.buildsys.configunix" class="sect1">
  <h2 class="title">Talking to the UNIX build system: config.m4</h2>
  <p class="para">
   The <var class="filename">config.m4</var> file for an extension tells the UNIX
   build system what <var class="filename">configure</var> options your extension
   supports, what external libraries and includes you require, and what source
   files are to be compiled as part of it. A reference to all the commonly used
   autoconf macros, both PHP-specific and those built into autoconf, is given
   in the <a href="internals2.apiref.php" class="xref">Zend Engine 2 API reference</a> section.
  </p>
 
  <div class="tip"><b class="tip">Tip</b>
   <p class="para">
    When developing a PHP extension, it is <em class="emphasis">strongly</em>
    recommended that <strong class="command">autoconf</strong> version 2.13 be installed,
    despite the newer releases which are available. Version 2.13 is recognized
    as a common denominator of <strong class="command">autoconf</strong> availability,
    usability, and user base. Using later versions will sometimes produce
    cosmetic differences from the expected output of
    <strong class="command">configure</strong>.
   </p>
  </div>
 
  <div class="example">
   <p><b>Example #1 An example config.m4 file</b></p>
   <div class="example-contents programlisting"><br />
dnl $Id$<br />
dnl config.m4 for extension example<br />
<div class="autoconfcode"><pre class="autoconfcode">PHP_ARG_WITH(example, for example support,
[  --with-example[=FILE]       Include example support. File is the optional path to example-config])
PHP_ARG_ENABLE(example-debug, whether to enable debugging support in example,
[  --enable-example-debug        example: Enable debugging support in example], no, no)
PHP_ARG_WITH(example-extra, for extra libraries for example,
[  --with-example-extra=DIR      example: Location of extra libraries for example], no, no)

dnl Check whether the extension is enabled at all
if test &quot;$PHP_EXAMPLE&quot; != &quot;no&quot;; then
 
  dnl Check for example-config. First try any path that was given to us, then look in $PATH
  AC_MSG_CHECKING([for example-config])
  EXAMPLE_CONFIG=&quot;example-config&quot;
  if test &quot;$PHP_EXAMPLE&quot; != &quot;yes&quot;; then
    EXAMPLE_PATH=$PHP_EXAMPLE
  else
    EXAMPLE_PATH=`$php_shtool path $EXAMPLE_CONFIG`
  fi
 
  dnl If a usable example-config was found, use it
  if test -f &quot;$EXAMPLE_PATH&quot; &amp;&amp; test -x &quot;$EXAMPLE_PATH&quot; &amp;&amp; $EXAMPLE_PATH --version &gt; /dev/null 2&gt;&amp;1; then
    AC_MSG_RESULT([$EXAMPLE_PATH])
    EXAMPLE_LIB_NAME=`$EXAMPLE_PATH --libname`
    EXAMPLE_INCDIRS=`$EXAMPLE_PATH --incdirs`
    EXAMPLE_LIBS=`$EXAMPLE_PATH --libs`
   
    dnl Check that the library works properly
    PHP_CHECK_LIBRARY($EXAMPLE_LIB_NAME, example_critical_function,
    [
      dnl Add the necessary include dirs
      PHP_EVAL_INCLINE($EXAMPLE_INCDIRS)
      dnl Add the necessary libraries and library dirs
      PHP_EVAL_LIBLINE($EXAMPLE_LIBS, EXAMPLE_SHARED_LIBADD)
    ],[
      dnl Bail out
      AC_MSG_ERROR([example library not found. Check config.log for more information.])
    ],[$EXAMPLE_LIBS]
    )
  else
    dnl No usable example-config, bail 
    AC_MSG_RESULT([not found])
    AC_MSG_ERROR([Please check your example installation.])
  fi
 
  dnl Check whether to enable debugging
  if test &quot;$PHP_EXAMPLE_DEBUG&quot; != &quot;no&quot;; then
    dnl Yes, so set the C macro
    AC_DEFINE(USE_EXAMPLE_DEBUG,1,[Include debugging support in example])
  fi
 
  dnl Check for the extra support
  if test &quot;$PHP_EXAMPLE_EXTRA&quot; != &quot;no&quot;; then
    if test &quot;$PHP_EXAMPLE_EXTRA&quot; == &quot;yes&quot;; then
      AC_MSG_ERROR([You must specify a path when using --with-example-extra])
    fi
   
    PHP_CHECK_LIBRARY(example-extra, example_critical_extra_function,
    [
      dnl Add the neccessary paths
      PHP_ADD_INCLUDE($PHP_EXAMPLE_EXTRA/include)
      PHP_ADD_LIBRARY_WITH_PATH(example-extra, $PHP_EXAMPLE_EXTRA/lib, EXAMPLE_SHARED_LIBADD)
      AC_DEFINE(HAVE_EXAMPLEEXTRALIB,1,[Whether example-extra support is present and requested])
      EXAMPLE_SOURCES=&quot;$EXAMPLE_SOURCES example_extra.c&quot;
    ],[
      AC_MSG_ERROR([example-extra lib not found. See config.log for more information.])
    ],[-L$PHP_EXAMPLE_EXTRA/lib]
    )
  fi
 
  dnl Finally, tell the build system about the extension and what files are needed
  PHP_NEW_EXTENSION(example, example.c $EXAMPLE_SOURCES, $ext_shared)
  PHP_SUBST(EXAMPLE_SHARED_LIBADD)
fi</pre>
</div>
   </div>

  </div>
 
  <div id="internals2.buildsys.configunix.autoconf" class="sect2">
   <h3 class="title">A short introduction to autoconf syntax</h3>
   <p class="para">
    <var class="filename">config.m4</var> files are written using the GNU
    <strong class="command">autoconf</strong> syntax. It can be described in a nutshell as
    shell scripting augmented by a powerful macro language. Comments are
    delimited by the string <i>dnl</i>, and strings are quoted
    using left and right brackets (e.g. <i>[</i> and
    <i>]</i>). Quoting of strings can be nested as many times as
    needed. A full reference to the syntax can be found in the
    <strong class="command">autoconf</strong> manual at
    <a href="http://www.gnu.org/software/autoconf/manual/" class="link external">&raquo; http://www.gnu.org/software/autoconf/manual/</a>.
   </p>
  </div>
 
  <div id="internals2.buildsys.configunix.php-arg" class="sect2">
   <h3 class="title">PHP_ARG_*: Giving users the option</h3>
   <p class="para">
    The very first thing seen in the example <var class="filename">config.m4</var>
    above, aside from a couple of comments, are three lines using
    <b>PHP_ARG_WITH()</b> and <b>PHP_ARG_ENABLE()</b>.
    These provide <strong class="command">configure</strong> with the options and help text
    seen when running <strong class="command">./configure --help</strong>. As the names
    suggest, the difference between the two is whether they create a
    <span class="option">--with-*</span> option or an
    <span class="option">--enable-*</span> option. Every extension should
    provide at least one or the other with the extension name, so that users
    can choose whether or not to build the extension into PHP. By convention,
    <b>PHP_ARG_WITH()</b> is used for an option which takes a
    parameter, such as the location of a library or program required by an
    extension, while <b>PHP_ARG_ENABLE()</b> is used for an option
    which represents a simple flag.
   </p>
   <div class="example">
    <p><b>Example #2 Sample configure output</b></p>
    <div class="example-contents screen">
<div class="cdata"><pre>
$ ./configure --help
...
  --with-example[=FILE]       Include example support. FILE is the optional path to example-config
  --enable-example-debug        example: Enable debugging support in example
  --with-example-extra=DIR      example: Location of extra libraries for example
...

$ ./configure --with-example=/some/library/path/example-config --disable-example-debug --with-example-extra=/another/library/path
...
checking for example support... yes
checking whether to enable debugging support in example... no
checking for extra libraries for example... /another/library/path
...
</pre></div>
    </div>
   </div>
   <blockquote><p><b class="note">Note</b>:
   
     Regardless of the order in which options are specified on the command line
     when <strong class="command">configure</strong> is called, the checks will be run in the
     order they are specified in <var class="filename">config.m4</var>.
    <br />
   </p></blockquote>
  </div>
 
  <div id="internals2.buildsys.configunix.processing" class="sect2">
   <h3 class="title">Processing the user&#039;s choices</h3>
   <p class="para">
    Now that <var class="filename">config.m4</var> can provide the user with some
    choices of what to do, it&#039;s time to act upon those choices. In the example
    above, the obvious default for all three options, if any of them are
    unspecified, is &quot;no&quot;. As a matter of convention, it is best to
    use this as the default for the option which enables the extension, as it
    will be overridden by <strong class="command">phpize</strong> for extensions built
    separately, and should not clutter the extension space by default when
    being built into PHP. The code to process the three options is by far the
    most complicated.
   </p>
  
   <div id="internals2.buildsys.configunix.processing.with-example" class="sect3">
    <h4 class="title">Handling the --with-example[=FILE] option</h4>
    <p class="para">
     The first check made of the
     <span class="option">--with-example[=FILE]</span> option is whether
     it was set at all. As this option controls the inclusion of the entire
     extension, if it was unspecified, given in the negative form
     (<span class="option">--without-example</span>), or given the value
     &quot;no&quot;, nothing else is done at all. In the example above, it is
     specified with the value
     <i>/some/library/path/example-config</i>, so the first test
     succeeds.
    </p>
   
    <p class="para">
     Next, the code calls <b>AC_MSG_CHECKING()</b>, an
     <strong class="command">autoconf</strong> macro which outputs a standard
     &quot;checking for something&quot; line, and checks whether the user gave
     an explicit path to the fictional <strong class="command">example-config</strong>. In
     this example, <i>PHP_EXAMPLE</i> got the value
     <i>/some/library/path/example-config</i>, which is now copied
     into the EXAMPLE_PATH variable. Had the user specified only
     <span class="option">--with-example</span>, the code would have
     executed <strong class="command">$php_shtool path $EXAMPLE_CONFIG</strong>, which would
     try to guess the location of <strong class="command">example-config</strong> using the
     user&#039;s current <i>PATH</i>. Either way, the next step is to
     check whether the chosen <i>EXAMPLE_PATH</i> is a regular
     file, is executable, and can be run successfully. If so,
     <b>AC_MSG_RESULT()</b> is called, which completes the output
     line started by <b>AC_MSG_CHECKING()</b>. Otherwise,
     <b>AC_MSG_ERROR()</b> is called, which prints the given
     message and halts <strong class="command">configure</strong> immediately.
    </p>
   
    <p class="para">
     The code now determines some site-specific configuration information by
     running <strong class="command">example-config</strong> several times. The next call is
     to <b>PHP_CHECK_LIBRARY()</b>, a macro provided by the PHP
     buildsystem as a wrapper around <strong class="command">autoconf</strong>&#039;s
     <b>AC_CHECK_LIB()</b>. <b>PHP_CHECK_LIBRARY()</b>
     attempts to compile, link, and run a program which calls the symbol
     specified by the second parameter in the library specified by the first,
     using the string given in the fifth as extra linker options. If the
     attempt succeeds, the script given in the third parameter is run. This
     script tells the PHP buildsystem to extract include paths, library paths,
     and library names from the raw option strings
     <strong class="command">example-config</strong> provided. If the attempt fails, the
     script in the fourth parameter is run instead. In this case,
     <b>AC_MSG_ERROR()</b> is called to stop processing.
    </p>
   </div>
  
   <div id="internals2.buildsys.configunix.processing.enable-example-debug" class="sect3">
    <h4 class="title">Handling the --enable-example-debug option</h4>
    <p class="para">
     Processing the <span class="option">--enable-example-debug</span> is
     much simpler. A simple check for its truth value is performed. If that
     check succeeds, <b>AC_DEFINE()</b> is called to make the C
     macro <i>USE_EXAMPLE_DEBUG</i> available to the source of the
     extension. The third parameter is a comment string for
     <var class="filename">config.h</var>; it is safe to leave this empty, and often is.
    </p>
   </div>
  
   <div id="internals2.buildsys.configunix.processing.with-example-extra" class="sect3">
    <h4 class="title">Handling the --with-example-extra=DIR option</h4>
    <p class="para">
     For the sake of this example, the fictional &quot;extra&quot;
     functionality requested by the
     <span class="option">--with-example-extra=DIR</span> option does not
     share the fictional <strong class="command">example-config</strong> program, nor does it
     have any default paths to search. Therefore, the user is required to
     provide the installation prefix of the necessary library. This setup is
     somewhat unlikely in a real-world extension, but is considered
     illustrative.
    </p>
   
    <p class="para">
     The code begins in a now-familiar way by checking the truth value of
     <i>PHP_EXAMPLE_EXTRA</i>. If a negative form was provided, no
     further processing is done; the user did not request extra functionality.
     If a positive form was provided without a parameter,
     <b>AC_MSG_ERROR()</b> is called to halt processing. The next
     step is another invocation of <b>PHP_CHECK_LIBRARY()</b>. This
     time, since there is no set of predefined compiler options provided,
     <b>PHP_ADD_INCLUDE()</b> and
     <b>PHP_ADD_LIBRARY_WITH_PATH()</b> are used to construct the
     necessary include paths, library paths, and library flags for the extra
     functionality. <b>AC_DEFINE()</b> is also called to indicate
     to the code that the extra functionality was both requested and available,
     and a variable is set to tell later code that there are extra source files
     to build. If the check fails, the familiar
     <b>AC_MSG_ERROR()</b> is called. A different way to handle the
     failure would have been to call <b>AC_MSG_WARNING()</b>
     instead, e.g.:
    </p>
    <div class="informalexample">
     <div class="example-contents programlisting">
<div class="autoconfcode"><pre class="autoconfcode">AC_MSG_WARNING([example-extra lib not found. example will be built without extra functionality.])</pre>
</div>
     </div>

    </div>
    <p class="para">
     In this case, <strong class="command">configure</strong> would print a warning message
     rather than an error, and continue processing. Which way such failures are
     handled is a design decision left to the extension developer.
    </p>
   </div>
  </div>
 
  <div id="internals2.buildsys.configunix.finishing" class="sect2">
   <h3 class="title">Telling the buildsystem what was decided</h3>
   <p class="para">
    With all the necessary includes and libraries specified, with all the
    options processed and macros defined, one more thing remains to be done:
    The build system must be told to build the extension itself, and which
    files are to be used for that. To do this, the
    <b>PHP_NEW_EXTENSION()</b> macro is called. The first parameter
    is the name of the extension, which is the same as the name of the
    directory containing it. The second parameter is the list of all source
    files which are part of the extension. See
    <b>PHP_ADD_BUILD_DIR()</b> for information about adding source
    files in subdirectories to the build process. The third parameter should
    always be <i>$ext_shared</i>, a value which was determined by
    <strong class="command">configure</strong> when <b>PHP_ARG_WITH()</b> was
    called for <span class="option">--with-example[=FILE]</span>. The
    fourth parameter specifies a &quot;SAPI class&quot;, and is only useful for
    extensions which require the CGI or CLI SAPIs specifically. It should be
    left empty in all other cases. The fifth parameter specifies a list of
    flags to be added to <i>CFLAGS</i> while building the
    extension; the sixth is a boolean value which, if &quot;yes&quot;, will
    force the entire extension to be built using <i>$CXX</i>
    instead of <i>$CC</i>. All parameters after the third are
    optional. Finally, <b>PHP_SUBST()</b> is called to enable
    shared builds of the extension. See <a href="internals2.faq.php" class="xref">Extension FAQs</a> for
    more information on disabling support for building an extension in shared
    mode.
   </p>
  </div>

  <div id="internals2.buildsys.configunix.counter" class="sect2">
   <h3 class="title">The counter extension&#039;s config.m4 file</h3>
   <p class="para">
    The counter extension previously documented has a much simpler
    <var class="filename">config.m4</var> file than that described above, as it doesn&#039;t
    make use of many buildsystem features. This is a preferred method of
    operation for any extension that doesn&#039;t use an external or bundled library.
   </p>
   <div class="example">
    <p><b>Example #3 counter&#039;s config.m4 file</b></p>
    <div class="example-contents programlisting">
<div class="autoconfcode"><pre class="autoconfcode">dnl</pre>
</div>$<div class="autoconfcode"><pre class="autoconfcode">Id$
dnl config.m4 for extension counter

PHP_ARG_ENABLE(counter, for counter support,
[  --enable-counter            Include counter support])

dnl Check whether the extension is enabled at all
if test &quot;$PHP_COUNTER&quot; != &quot;no&quot;; then
  dnl Finally, tell the build system about the extension and what files are needed
  PHP_NEW_EXTENSION(counter, counter.c counter_util.c, $ext_shared)
  PHP_SUBST(COUNTER_SHARED_LIBADD)
fi</pre>
</div>
    </div>

   </div>
  </div>

 </div><?php manual_footer(); ?>
 
show source | credits | sitemap | contact | advertising | mirror sites