Source for file Builder.php
Documentation is available at Builder.php
* PEAR_Builder for building PHP extensions (PECL packages)
* @author Stig Bakken <ssb@php.net>
* @author Greg Beaver <cellog@php.net>
* @copyright 1997-2009 The Authors
* @license http://opensource.org/licenses/bsd-license.php New BSD License
* @version CVS: $Id: Builder.php 313024 2011-07-06 19:51:24Z dufuz $
* @link http://pear.php.net/package/PEAR
* @since File available since Release 0.1
* TODO: log output parameters in PECL command line
* TODO: msdev path in configuration
* Needed for extending PEAR_Builder
require_once 'PEAR/Common.php';
require_once 'PEAR/PackageFile.php';
* Class to handle building (compiling) extensions.
* @author Stig Bakken <ssb@php.net>
* @author Greg Beaver <cellog@php.net>
* @copyright 1997-2009 The Authors
* @license http://opensource.org/licenses/bsd-license.php New BSD License
* @version Release: 1.9.4
* @link http://pear.php.net/package/PEAR
* @since Class available since PHP 4.0.2
* @see http://pear.php.net/manual/en/core.ppm.pear-builder.php
* @var string Used for reporting when it is not possible to pass function
* via extra parameter, e.g. log, msdevCallback
* PEAR_Builder constructor.
* @param object $ui user interface object (instance of PEAR_Frontend_*)
* Build an extension from source on windows.
function _build_win32 ($descfile, $callback = null )
$descfile = $pkg->getPackageFile ();
return $this->raiseError(" could not chdir to $dir" );
// packages that were in a .tar have the packagefile in this directory
$vdir = $pkg->getPackage () . '-' . $pkg->getVersion ();
$this->log(2 , " building in $dir" );
$dsp = $pkg->getPackage (). '.dsp';
return $this->raiseError(" The DSP $dsp does not exist." );
// XXX TODO: make release build type configurable
$command = 'msdev '. $dsp. ' /MAKE "'. $pkg->getPackage (). ' - Release"';
$err = $this->_runCommand ($command, array (&$this, 'msdevCallback'));
// figure out the build platform and type
if (preg_match('/.*?'. $pkg->getPackage (). '\s-\s(\w+)\s(.*?)-+/i',$this->_firstline,$matches)) {
$buildtype = $matches[2 ];
if (preg_match('/(.*)?\s-\s(\d+).*?(\d+)/', $this->_lastline, $matches)) {
// there were errors in the build
return $this->raiseError("There were errors during compilation.");
return $this->raiseError("Did not understand the completion status returned from msdev.exe.");
// msdev doesn't tell us the output directory :/
// open the dsp, find /out and use that directory
// this regex depends on the build platform and type having been
// correctly identified above.
$regex = '/.*?!IF\s+"\$\(CFG\)"\s+==\s+("'.
$pkg->getPackage (). '\s-\s'.
if ($dsptext && preg_match($regex, $dsptext, $matches)) {
// what we get back is a relative path to the output file itself.
return $this->raiseError(" Could not retrieve output information from $dsp." );
// realpath returns false if the file doesn't exist
if ($outfile && copy($outfile, " $dir/$out" )) {
$this->_firstline = $data;
$this->_lastline = $data;
function _harvestInstDir ($dest_prefix, $dirname, &$built_files)
while (($ent = readdir($d)) !== false ) {
$full = $dirname . DIRECTORY_SEPARATOR . $ent;
if (!$this->_harvestInstDir (
$dest_prefix . DIRECTORY_SEPARATOR . $ent,
$dest = $dest_prefix . DIRECTORY_SEPARATOR . $ent;
* Build an extension from source. Runs "phpize" in the source
* directory, but compiles in a temporary directory
* (TMPDIR/pear-build-USER/PACKAGE-VERSION).
* @param string|PEAR_PackageFile_v*$descfile path to XML package description file, or
* a PEAR_PackageFile object
* @param mixed $callback callback function used to report output,
* see PEAR_Builder::_runCommand for details
* @return array an array of associative arrays with built files,
* array( array( 'file' => '/path/to/ext.so',
* 'zend_mod_api' => YYYYMMDD,
* 'zend_ext_api' => YYYYMMDD ),
* @see PEAR_Builder::_runCommand
function build($descfile, $callback = null )
if (preg_match('/(\\/|\\\\|^)([^\\/\\\\]+)?php(.+)?$/',
$this->config->get ('php_bin'), $matches)) {
if (isset ($matches[2 ]) && strlen($matches[2 ]) &&
$this->log(0 , 'WARNING: php_bin ' . $this->config->get ('php_bin') .
' appears to have a prefix ' . $matches[2 ] . ', but' .
' config variable php_prefix does not match');
if (isset ($matches[3 ]) && strlen($matches[3 ]) &&
$this->log(0 , 'WARNING: php_bin ' . $this->config->get ('php_bin') .
' appears to have a suffix ' . $matches[3 ] . ', but' .
' config variable php_suffix does not match');
return $this->_build_win32 ($descfile, $callback);
return $this->raiseError("building extensions not supported on this platform");
$descfile = $pkg->getPackageFile ();
if (is_a($pkg, 'PEAR_PackageFile_v1')) {
$dir = $pkg->_config ->get ('temp_dir') . '/' . $pkg->getName ();
// automatically delete at session end
// Find config. outside of normal path - e.g. config.m4
foreach (array_keys($pkg->getInstallationFileList ()) as $item) {
$dir .= DIRECTORY_SEPARATOR . dirname($item);
return $this->raiseError(" could not chdir to $dir" );
$vdir = $pkg->getPackage () . '-' . $pkg->getVersion ();
$this->log(2 , " building in $dir" );
$err = $this->_runCommand ($this->config->get ('php_prefix')
$this->config->get ('php_suffix'),
array (&$this, 'phpizeCallback'));
// {{{ start of interactive part
$configure_command = " $dir/configure";
$configure_options = $pkg->getConfigureOptions ();
if ($configure_options) {
foreach ($configure_options as $o) {
list ($r) = $this->ui->userDialog ('build',
if (substr($o['name'], 0 , 5 ) == 'with-' &&
($r == 'yes' || $r == 'autodetect')) {
$configure_command .= " --$o[name]";
$configure_command .= " --$o[name]=". trim($r);
// }}} end of interactive part
// FIXME make configurable
$tmpdir = $this->config->get ('temp_dir');
$build_basedir = System::mktemp(' -t "' . $tmpdir . '" -d "pear-build-' . $user . '"');
$build_dir = " $build_basedir/$vdir";
$inst_dir = " $build_basedir/install-$vdir";
$this->log(1 , " building in $build_dir" );
return $this->raiseError(" could not create build dir: $build_dir" );
return $this->raiseError(" could not create temporary install dir: $inst_dir" );
" $make_command INSTALL_ROOT=\"$inst_dir\" install" ,
" find \"$inst_dir\" | xargs ls -dils"
return $this->raiseError(" could not chdir to $build_dir" );
putenv('PHP_PEAR_VERSION=1.9.4');
foreach ($to_run as $cmd) {
$err = $this->_runCommand ($cmd, $callback);
return $this->raiseError("no `modules' directory found");
$this->config->get ('php_suffix') . " --prefix");
$this->_harvestInstDir ($prefix, $inst_dir . DIRECTORY_SEPARATOR . $prefix, $built_files);
* Message callback function used when running the "phpize"
* program. Extracts the API numbers used. Ignores other message
* types than "cmdoutput".
* @param string $what the type of message
* @param mixed $data the message
if ($what != 'cmdoutput') {
if (preg_match('/You should update your .aclocal.m4/', $data)) {
if (preg_match('/^\s+(\S[^:]+):\s+(\d{8})/', $data, $matches)) {
$apino = (int) $matches[2 ];
if (isset ($this->$member)) {
//$msg = sprintf("%-22s : %d", $matches[1], $apino);
* Run an external command, using a message callback to report
* output. The command will be run through popen and output is
* reported for every line with a "cmdoutput" message with the
* line string, including newlines, as payload.
* @param string $command the command to run
* @param mixed $callback (optional) function to use as message
* @return bool whether the command was successful (exit code 0
* means success, any other means failure)
function _runCommand ($command, $callback = null )
$this->log(1 , " running: $command" );
$pp = popen(" $command 2>&1" , "r");
return $this->raiseError(" failed to run `$command'" );
if ($callback && $callback[0 ]->debug == 1 ) {
$olddbg = $callback[0 ]->debug;
while ($line = fgets($pp, 1024 )) {
if ($callback && isset ($olddbg)) {
$callback[0 ]->debug = $olddbg;
function log($level, $msg)
if ($this->debug >= $level) {
Documentation generated on Wed, 06 Jul 2011 23:30:26 +0000 by phpDocumentor 1.4.3. PEAR Logo Copyright © PHP Group 2004.
|