server/3rdparty/PEAR/Builder.php

489 lines
16 KiB
PHP
Raw Normal View History

2010-03-10 12:03:40 +00:00
<?php
2012-05-11 15:57:55 +00:00
/**
* PEAR_Builder for building PHP extensions (PECL packages)
*
* PHP versions 4 and 5
*
* @category pear
* @package PEAR
* @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
*/
2010-03-10 12:03:40 +00:00
2012-05-11 15:57:55 +00:00
/**
* Needed for extending PEAR_Builder
*/
2010-03-10 12:03:40 +00:00
require_once 'PEAR/Common.php';
2012-05-11 15:57:55 +00:00
require_once 'PEAR/PackageFile.php';
2010-03-10 12:03:40 +00:00
/**
* Class to handle building (compiling) extensions.
*
2012-05-11 15:57:55 +00:00
* @category pear
* @package PEAR
* @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
2010-03-10 12:03:40 +00:00
*/
class PEAR_Builder extends PEAR_Common
{
var $php_api_version = 0;
var $zend_module_api_no = 0;
var $zend_extension_api_no = 0;
var $extensions_built = array();
2012-05-11 15:57:55 +00:00
/**
* @var string Used for reporting when it is not possible to pass function
* via extra parameter, e.g. log, msdevCallback
*/
2010-03-10 12:03:40 +00:00
var $current_callback = null;
// used for msdev builds
var $_lastline = null;
var $_firstline = null;
/**
* PEAR_Builder constructor.
*
* @param object $ui user interface object (instance of PEAR_Frontend_*)
*
* @access public
*/
function PEAR_Builder(&$ui)
{
parent::PEAR_Common();
$this->setFrontendObject($ui);
}
/**
* Build an extension from source on windows.
* requires msdev
*/
function _build_win32($descfile, $callback = null)
{
2012-05-11 15:57:55 +00:00
if (is_object($descfile)) {
$pkg = $descfile;
$descfile = $pkg->getPackageFile();
} else {
$pf = &new PEAR_PackageFile($this->config, $this->debug);
$pkg = &$pf->fromPackageFile($descfile, PEAR_VALIDATE_NORMAL);
if (PEAR::isError($pkg)) {
return $pkg;
}
2010-03-10 12:03:40 +00:00
}
$dir = dirname($descfile);
$old_cwd = getcwd();
2012-05-11 15:57:55 +00:00
if (!file_exists($dir) || !is_dir($dir) || !chdir($dir)) {
2010-03-10 12:03:40 +00:00
return $this->raiseError("could not chdir to $dir");
}
2012-05-11 15:57:55 +00:00
// packages that were in a .tar have the packagefile in this directory
$vdir = $pkg->getPackage() . '-' . $pkg->getVersion();
if (file_exists($dir) && is_dir($vdir)) {
if (!chdir($vdir)) {
return $this->raiseError("could not chdir to " . realpath($vdir));
}
$dir = getcwd();
}
2010-03-10 12:03:40 +00:00
$this->log(2, "building in $dir");
2012-05-11 15:57:55 +00:00
$dsp = $pkg->getPackage().'.dsp';
if (!file_exists("$dir/$dsp")) {
2010-03-10 12:03:40 +00:00
return $this->raiseError("The DSP $dsp does not exist.");
}
// XXX TODO: make release build type configurable
2012-05-11 15:57:55 +00:00
$command = 'msdev '.$dsp.' /MAKE "'.$pkg->getPackage(). ' - Release"';
2010-03-10 12:03:40 +00:00
$err = $this->_runCommand($command, array(&$this, 'msdevCallback'));
if (PEAR::isError($err)) {
return $err;
}
// figure out the build platform and type
$platform = 'Win32';
$buildtype = 'Release';
2012-05-11 15:57:55 +00:00
if (preg_match('/.*?'.$pkg->getPackage().'\s-\s(\w+)\s(.*?)-+/i',$this->_firstline,$matches)) {
2010-03-10 12:03:40 +00:00
$platform = $matches[1];
$buildtype = $matches[2];
}
2012-05-11 15:57:55 +00:00
if (preg_match('/(.*)?\s-\s(\d+).*?(\d+)/', $this->_lastline, $matches)) {
2010-03-10 12:03:40 +00:00
if ($matches[2]) {
// there were errors in the build
return $this->raiseError("There were errors during compilation.");
}
$out = $matches[1];
} else {
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
$dsptext = join(file($dsp),'');
// this regex depends on the build platform and type having been
// correctly identified above.
$regex ='/.*?!IF\s+"\$\(CFG\)"\s+==\s+("'.
2012-05-11 15:57:55 +00:00
$pkg->getPackage().'\s-\s'.
2010-03-10 12:03:40 +00:00
$platform.'\s'.
$buildtype.'").*?'.
'\/out:"(.*?)"/is';
2012-05-11 15:57:55 +00:00
if ($dsptext && preg_match($regex, $dsptext, $matches)) {
2010-03-10 12:03:40 +00:00
// what we get back is a relative path to the output file itself.
$outfile = realpath($matches[2]);
} else {
return $this->raiseError("Could not retrieve output information from $dsp.");
}
2012-05-11 15:57:55 +00:00
// realpath returns false if the file doesn't exist
if ($outfile && copy($outfile, "$dir/$out")) {
2010-03-10 12:03:40 +00:00
$outfile = "$dir/$out";
}
$built_files[] = array(
'file' => "$outfile",
'php_api' => $this->php_api_version,
'zend_mod_api' => $this->zend_module_api_no,
'zend_ext_api' => $this->zend_extension_api_no,
);
return $built_files;
}
// }}}
// {{{ msdevCallback()
function msdevCallback($what, $data)
{
if (!$this->_firstline)
$this->_firstline = $data;
$this->_lastline = $data;
2012-05-11 15:57:55 +00:00
call_user_func($this->current_callback, $what, $data);
2010-03-10 12:03:40 +00:00
}
/**
* @param string
* @param string
* @param array
* @access private
*/
function _harvestInstDir($dest_prefix, $dirname, &$built_files)
{
$d = opendir($dirname);
if (!$d)
return false;
$ret = true;
while (($ent = readdir($d)) !== false) {
if ($ent{0} == '.')
continue;
$full = $dirname . DIRECTORY_SEPARATOR . $ent;
if (is_dir($full)) {
if (!$this->_harvestInstDir(
$dest_prefix . DIRECTORY_SEPARATOR . $ent,
$full, $built_files)) {
$ret = false;
break;
}
} else {
$dest = $dest_prefix . DIRECTORY_SEPARATOR . $ent;
$built_files[] = array(
'file' => $full,
'dest' => $dest,
'php_api' => $this->php_api_version,
'zend_mod_api' => $this->zend_module_api_no,
'zend_ext_api' => $this->zend_extension_api_no,
);
}
}
closedir($d);
return $ret;
}
/**
* Build an extension from source. Runs "phpize" in the source
* directory, but compiles in a temporary directory
2012-05-11 15:57:55 +00:00
* (TMPDIR/pear-build-USER/PACKAGE-VERSION).
2010-03-10 12:03:40 +00:00
*
2012-05-11 15:57:55 +00:00
* @param string|PEAR_PackageFile_v* $descfile path to XML package description file, or
* a PEAR_PackageFile object
2010-03-10 12:03:40 +00:00
*
* @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,
* format:
* array( array( 'file' => '/path/to/ext.so',
* 'php_api' => YYYYMMDD,
* 'zend_mod_api' => YYYYMMDD,
* 'zend_ext_api' => YYYYMMDD ),
* ... )
*
* @access public
*
* @see PEAR_Builder::_runCommand
*/
function build($descfile, $callback = null)
{
2012-05-11 15:57:55 +00:00
if (preg_match('/(\\/|\\\\|^)([^\\/\\\\]+)?php(.+)?$/',
$this->config->get('php_bin'), $matches)) {
if (isset($matches[2]) && strlen($matches[2]) &&
trim($matches[2]) != trim($this->config->get('php_prefix'))) {
$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]) &&
trim($matches[3]) != trim($this->config->get('php_suffix'))) {
$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');
}
}
$this->current_callback = $callback;
2010-03-10 12:03:40 +00:00
if (PEAR_OS == "Windows") {
2012-05-11 15:57:55 +00:00
return $this->_build_win32($descfile, $callback);
2010-03-10 12:03:40 +00:00
}
2012-05-11 15:57:55 +00:00
2010-03-10 12:03:40 +00:00
if (PEAR_OS != 'Unix') {
return $this->raiseError("building extensions not supported on this platform");
}
2012-05-11 15:57:55 +00:00
if (is_object($descfile)) {
$pkg = $descfile;
$descfile = $pkg->getPackageFile();
if (is_a($pkg, 'PEAR_PackageFile_v1')) {
$dir = dirname($descfile);
} else {
$dir = $pkg->_config->get('temp_dir') . '/' . $pkg->getName();
// automatically delete at session end
$this->addTempFile($dir);
}
} else {
$pf = &new PEAR_PackageFile($this->config);
$pkg = &$pf->fromPackageFile($descfile, PEAR_VALIDATE_NORMAL);
if (PEAR::isError($pkg)) {
return $pkg;
}
$dir = dirname($descfile);
2010-03-10 12:03:40 +00:00
}
2012-05-11 15:57:55 +00:00
// Find config. outside of normal path - e.g. config.m4
foreach (array_keys($pkg->getInstallationFileList()) as $item) {
if (stristr(basename($item), 'config.m4') && dirname($item) != '.') {
$dir .= DIRECTORY_SEPARATOR . dirname($item);
break;
}
}
2010-03-10 12:03:40 +00:00
$old_cwd = getcwd();
2012-05-11 15:57:55 +00:00
if (!file_exists($dir) || !is_dir($dir) || !chdir($dir)) {
2010-03-10 12:03:40 +00:00
return $this->raiseError("could not chdir to $dir");
}
2012-05-11 15:57:55 +00:00
$vdir = $pkg->getPackage() . '-' . $pkg->getVersion();
2010-03-10 12:03:40 +00:00
if (is_dir($vdir)) {
chdir($vdir);
}
2012-05-11 15:57:55 +00:00
2010-03-10 12:03:40 +00:00
$dir = getcwd();
$this->log(2, "building in $dir");
putenv('PATH=' . $this->config->get('bin_dir') . ':' . getenv('PATH'));
2012-05-11 15:57:55 +00:00
$err = $this->_runCommand($this->config->get('php_prefix')
. "phpize" .
$this->config->get('php_suffix'),
array(&$this, 'phpizeCallback'));
2010-03-10 12:03:40 +00:00
if (PEAR::isError($err)) {
return $err;
}
2012-05-11 15:57:55 +00:00
2010-03-10 12:03:40 +00:00
if (!$err) {
return $this->raiseError("`phpize' failed");
}
// {{{ start of interactive part
$configure_command = "$dir/configure";
2012-05-11 15:57:55 +00:00
$configure_options = $pkg->getConfigureOptions();
if ($configure_options) {
foreach ($configure_options as $o) {
$default = array_key_exists('default', $o) ? $o['default'] : null;
2010-03-10 12:03:40 +00:00
list($r) = $this->ui->userDialog('build',
array($o['prompt']),
array('text'),
2012-05-11 15:57:55 +00:00
array($default));
2010-03-10 12:03:40 +00:00
if (substr($o['name'], 0, 5) == 'with-' &&
($r == 'yes' || $r == 'autodetect')) {
$configure_command .= " --$o[name]";
} else {
$configure_command .= " --$o[name]=".trim($r);
}
}
}
// }}} end of interactive part
// FIXME make configurable
2012-05-11 15:57:55 +00:00
if (!$user=getenv('USER')) {
2010-03-10 12:03:40 +00:00
$user='defaultuser';
}
2012-05-11 15:57:55 +00:00
$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";
2010-03-10 12:03:40 +00:00
$this->log(1, "building in $build_dir");
if (is_dir($build_dir)) {
2012-05-11 15:57:55 +00:00
System::rm(array('-rf', $build_dir));
2010-03-10 12:03:40 +00:00
}
2012-05-11 15:57:55 +00:00
2010-03-10 12:03:40 +00:00
if (!System::mkDir(array('-p', $build_dir))) {
return $this->raiseError("could not create build dir: $build_dir");
}
2012-05-11 15:57:55 +00:00
2010-03-10 12:03:40 +00:00
$this->addTempFile($build_dir);
if (!System::mkDir(array('-p', $inst_dir))) {
return $this->raiseError("could not create temporary install dir: $inst_dir");
}
$this->addTempFile($inst_dir);
2012-05-11 15:57:55 +00:00
$make_command = getenv('MAKE') ? getenv('MAKE') : 'make';
2010-03-10 12:03:40 +00:00
$to_run = array(
$configure_command,
$make_command,
"$make_command INSTALL_ROOT=\"$inst_dir\" install",
2012-05-11 15:57:55 +00:00
"find \"$inst_dir\" | xargs ls -dils"
2010-03-10 12:03:40 +00:00
);
2012-05-11 15:57:55 +00:00
if (!file_exists($build_dir) || !is_dir($build_dir) || !chdir($build_dir)) {
2010-03-10 12:03:40 +00:00
return $this->raiseError("could not chdir to $build_dir");
}
2012-05-11 15:57:55 +00:00
putenv('PHP_PEAR_VERSION=1.9.4');
2010-03-10 12:03:40 +00:00
foreach ($to_run as $cmd) {
$err = $this->_runCommand($cmd, $callback);
if (PEAR::isError($err)) {
chdir($old_cwd);
return $err;
}
if (!$err) {
chdir($old_cwd);
return $this->raiseError("`$cmd' failed");
}
}
if (!($dp = opendir("modules"))) {
chdir($old_cwd);
return $this->raiseError("no `modules' directory found");
}
$built_files = array();
2012-05-11 15:57:55 +00:00
$prefix = exec($this->config->get('php_prefix')
. "php-config" .
$this->config->get('php_suffix') . " --prefix");
2010-03-10 12:03:40 +00:00
$this->_harvestInstDir($prefix, $inst_dir . DIRECTORY_SEPARATOR . $prefix, $built_files);
chdir($old_cwd);
return $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
*
* @return void
*
* @access public
*/
function phpizeCallback($what, $data)
{
if ($what != 'cmdoutput') {
return;
}
$this->log(1, rtrim($data));
if (preg_match('/You should update your .aclocal.m4/', $data)) {
return;
}
$matches = array();
if (preg_match('/^\s+(\S[^:]+):\s+(\d{8})/', $data, $matches)) {
$member = preg_replace('/[^a-z]/', '_', strtolower($matches[1]));
$apino = (int)$matches[2];
if (isset($this->$member)) {
$this->$member = $apino;
//$msg = sprintf("%-22s : %d", $matches[1], $apino);
//$this->log(1, $msg);
}
}
}
/**
* 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
* callback
*
* @return bool whether the command was successful (exit code 0
* means success, any other means failure)
*
* @access private
*/
function _runCommand($command, $callback = null)
{
$this->log(1, "running: $command");
2012-05-11 15:57:55 +00:00
$pp = popen("$command 2>&1", "r");
2010-03-10 12:03:40 +00:00
if (!$pp) {
return $this->raiseError("failed to run `$command'");
}
if ($callback && $callback[0]->debug == 1) {
$olddbg = $callback[0]->debug;
$callback[0]->debug = 2;
}
while ($line = fgets($pp, 1024)) {
if ($callback) {
call_user_func($callback, 'cmdoutput', $line);
} else {
$this->log(2, rtrim($line));
}
}
if ($callback && isset($olddbg)) {
$callback[0]->debug = $olddbg;
}
2012-05-11 15:57:55 +00:00
$exitcode = is_resource($pp) ? pclose($pp) : -1;
2010-03-10 12:03:40 +00:00
return ($exitcode == 0);
}
function log($level, $msg)
{
if ($this->current_callback) {
if ($this->debug >= $level) {
call_user_func($this->current_callback, 'output', $msg);
}
return;
}
return PEAR_Common::log($level, $msg);
}
2012-05-11 15:57:55 +00:00
}