3  * PEAR_Builder for building PHP extensions (PECL packages)
 
   9  * @author     Stig Bakken <ssb@php.net>
 
  10  * @author     Greg Beaver <cellog@php.net>
 
  11  * @copyright  1997-2009 The Authors
 
  12  * @license    http://opensource.org/licenses/bsd-license.php New BSD License
 
  13  * @link       http://pear.php.net/package/PEAR
 
  14  * @since      File available since Release 0.1
 
  16  * TODO: log output parameters in PECL command line
 
  17  * TODO: msdev path in configuration
 
  21  * Needed for extending PEAR_Builder
 
  23 require_once 'PEAR/Common.php';
 
  24 require_once 'PEAR/PackageFile.php';
 
  25 require_once 'System.php';
 
  28  * Class to handle building (compiling) extensions.
 
  32  * @author     Stig Bakken <ssb@php.net>
 
  33  * @author     Greg Beaver <cellog@php.net>
 
  34  * @copyright  1997-2009 The Authors
 
  35  * @license    http://opensource.org/licenses/bsd-license.php New BSD License
 
  36  * @version    Release: 1.10.1
 
  37  * @link       http://pear.php.net/package/PEAR
 
  38  * @since      Class available since PHP 4.0.2
 
  39  * @see        http://pear.php.net/manual/en/core.ppm.pear-builder.php
 
  41 class PEAR_Builder extends PEAR_Common
 
  43     var $php_api_version = 0;
 
  44     var $zend_module_api_no = 0;
 
  45     var $zend_extension_api_no = 0;
 
  47     var $extensions_built = array();
 
  50      * @var string Used for reporting when it is not possible to pass function
 
  51      *             via extra parameter, e.g. log, msdevCallback
 
  53     var $current_callback = null;
 
  55     // used for msdev builds
 
  56     var $_lastline = null;
 
  57     var $_firstline = null;
 
  60      * PEAR_Builder constructor.
 
  62      * @param object $ui user interface object (instance of PEAR_Frontend_*)
 
  66     function __construct(&$ui)
 
  68         parent::__construct();
 
  69         $this->setFrontendObject($ui);
 
  73      * Build an extension from source on windows.
 
  76     function _build_win32($descfile, $callback = null)
 
  78         if (is_object($descfile)) {
 
  80             $descfile = $pkg->getPackageFile();
 
  82             $pf = new PEAR_PackageFile($this->config, $this->debug);
 
  83             $pkg = &$pf->fromPackageFile($descfile, PEAR_VALIDATE_NORMAL);
 
  84             if (PEAR::isError($pkg)) {
 
  88         $dir = dirname($descfile);
 
  91         if (!file_exists($dir) || !is_dir($dir) || !chdir($dir)) {
 
  92             return $this->raiseError("could not chdir to $dir");
 
  95         // packages that were in a .tar have the packagefile in this directory
 
  96         $vdir = $pkg->getPackage() . '-' . $pkg->getVersion();
 
  97         if (file_exists($dir) && is_dir($vdir)) {
 
  99                 return $this->raiseError("could not chdir to " . realpath($vdir));
 
 105         $this->log(2, "building in $dir");
 
 107         $dsp = $pkg->getPackage().'.dsp';
 
 108         if (!file_exists("$dir/$dsp")) {
 
 109             return $this->raiseError("The DSP $dsp does not exist.");
 
 111         // XXX TODO: make release build type configurable
 
 112         $command = 'msdev '.$dsp.' /MAKE "'.$pkg->getPackage(). ' - Release"';
 
 114         $err = $this->_runCommand($command, array(&$this, 'msdevCallback'));
 
 115         if (PEAR::isError($err)) {
 
 119         // figure out the build platform and type
 
 121         $buildtype = 'Release';
 
 122         if (preg_match('/.*?'.$pkg->getPackage().'\s-\s(\w+)\s(.*?)-+/i',$this->_firstline,$matches)) {
 
 123             $platform = $matches[1];
 
 124             $buildtype = $matches[2];
 
 127         if (preg_match('/(.*)?\s-\s(\d+).*?(\d+)/', $this->_lastline, $matches)) {
 
 129                 // there were errors in the build
 
 130                 return $this->raiseError("There were errors during compilation.");
 
 134             return $this->raiseError("Did not understand the completion status returned from msdev.exe.");
 
 137         // msdev doesn't tell us the output directory :/
 
 138         // open the dsp, find /out and use that directory
 
 139         $dsptext = join(file($dsp),'');
 
 141         // this regex depends on the build platform and type having been
 
 142         // correctly identified above.
 
 143         $regex ='/.*?!IF\s+"\$\(CFG\)"\s+==\s+("'.
 
 144                     $pkg->getPackage().'\s-\s'.
 
 149         if ($dsptext && preg_match($regex, $dsptext, $matches)) {
 
 150             // what we get back is a relative path to the output file itself.
 
 151             $outfile = realpath($matches[2]);
 
 153             return $this->raiseError("Could not retrieve output information from $dsp.");
 
 155         // realpath returns false if the file doesn't exist
 
 156         if ($outfile && copy($outfile, "$dir/$out")) {
 
 157             $outfile = "$dir/$out";
 
 160         $built_files[] = array(
 
 161             'file' => "$outfile",
 
 162             'php_api' => $this->php_api_version,
 
 163             'zend_mod_api' => $this->zend_module_api_no,
 
 164             'zend_ext_api' => $this->zend_extension_api_no,
 
 171     // {{{ msdevCallback()
 
 172     function msdevCallback($what, $data)
 
 174         if (!$this->_firstline)
 
 175             $this->_firstline = $data;
 
 176         $this->_lastline = $data;
 
 177         call_user_func($this->current_callback, $what, $data);
 
 186     function _harvestInstDir($dest_prefix, $dirname, &$built_files)
 
 188         $d = opendir($dirname);
 
 193         while (($ent = readdir($d)) !== false) {
 
 197             $full = $dirname . DIRECTORY_SEPARATOR . $ent;
 
 199                 if (!$this->_harvestInstDir(
 
 200                         $dest_prefix . DIRECTORY_SEPARATOR . $ent,
 
 201                         $full, $built_files)) {
 
 206                 $dest = $dest_prefix . DIRECTORY_SEPARATOR . $ent;
 
 207                 $built_files[] = array(
 
 210                         'php_api' => $this->php_api_version,
 
 211                         'zend_mod_api' => $this->zend_module_api_no,
 
 212                         'zend_ext_api' => $this->zend_extension_api_no,
 
 221      * Build an extension from source.  Runs "phpize" in the source
 
 222      * directory, but compiles in a temporary directory
 
 223      * (TMPDIR/pear-build-USER/PACKAGE-VERSION).
 
 225      * @param string|PEAR_PackageFile_v* $descfile path to XML package description file, or
 
 226      *               a PEAR_PackageFile object
 
 228      * @param mixed $callback callback function used to report output,
 
 229      * see PEAR_Builder::_runCommand for details
 
 231      * @return array an array of associative arrays with built files,
 
 233      * array( array( 'file' => '/path/to/ext.so',
 
 234      *               'php_api' => YYYYMMDD,
 
 235      *               'zend_mod_api' => YYYYMMDD,
 
 236      *               'zend_ext_api' => YYYYMMDD ),
 
 241      * @see PEAR_Builder::_runCommand
 
 243     function build($descfile, $callback = null)
 
 245         if (preg_match('/(\\/|\\\\|^)([^\\/\\\\]+)?php([^\\/\\\\]+)?$/',
 
 246                        $this->config->get('php_bin'), $matches)) {
 
 247             if (isset($matches[2]) && strlen($matches[2]) &&
 
 248                 trim($matches[2]) != trim($this->config->get('php_prefix'))) {
 
 249                 $this->log(0, 'WARNING: php_bin ' . $this->config->get('php_bin') .
 
 250                            ' appears to have a prefix ' . $matches[2] . ', but' .
 
 251                            ' config variable php_prefix does not match');
 
 254             if (isset($matches[3]) && strlen($matches[3]) &&
 
 255                 trim($matches[3]) != trim($this->config->get('php_suffix'))) {
 
 256                 $this->log(0, 'WARNING: php_bin ' . $this->config->get('php_bin') .
 
 257                            ' appears to have a suffix ' . $matches[3] . ', but' .
 
 258                            ' config variable php_suffix does not match');
 
 262         $this->current_callback = $callback;
 
 263         if (PEAR_OS == "Windows") {
 
 264             return $this->_build_win32($descfile, $callback);
 
 267         if (PEAR_OS != 'Unix') {
 
 268             return $this->raiseError("building extensions not supported on this platform");
 
 271         if (is_object($descfile)) {
 
 273             $descfile = $pkg->getPackageFile();
 
 274             if (is_a($pkg, 'PEAR_PackageFile_v1')) {
 
 275                 $dir = dirname($descfile);
 
 277                 $dir = $pkg->_config->get('temp_dir') . '/' . $pkg->getName();
 
 278                 // automatically delete at session end
 
 279                 $this->addTempFile($dir);
 
 282             $pf = new PEAR_PackageFile($this->config);
 
 283             $pkg = &$pf->fromPackageFile($descfile, PEAR_VALIDATE_NORMAL);
 
 284             if (PEAR::isError($pkg)) {
 
 287             $dir = dirname($descfile);
 
 290         // Find config. outside of normal path - e.g. config.m4
 
 291         foreach (array_keys($pkg->getInstallationFileList()) as $item) {
 
 292           if (stristr(basename($item), 'config.m4') && dirname($item) != '.') {
 
 293             $dir .= DIRECTORY_SEPARATOR . dirname($item);
 
 299         if (!file_exists($dir) || !is_dir($dir) || !chdir($dir)) {
 
 300             return $this->raiseError("could not chdir to $dir");
 
 303         $vdir = $pkg->getPackage() . '-' . $pkg->getVersion();
 
 309         $this->log(2, "building in $dir");
 
 310         putenv('PATH=' . $this->config->get('bin_dir') . ':' . getenv('PATH'));
 
 311         $err = $this->_runCommand($this->config->get('php_prefix')
 
 313                                 $this->config->get('php_suffix'),
 
 314                                 array(&$this, 'phpizeCallback'));
 
 315         if (PEAR::isError($err)) {
 
 320             return $this->raiseError("`phpize' failed");
 
 323         // {{{ start of interactive part
 
 324         $configure_command = "$dir/configure";
 
 326         $phpConfigName = $this->config->get('php_prefix')
 
 328             . $this->config->get('php_suffix');
 
 329         $phpConfigPath = System::which($phpConfigName);
 
 330         if ($phpConfigPath !== false) {
 
 331             $configure_command .= ' --with-php-config='
 
 335         $configure_options = $pkg->getConfigureOptions();
 
 336         if ($configure_options) {
 
 337             foreach ($configure_options as $o) {
 
 338                 $default = array_key_exists('default', $o) ? $o['default'] : null;
 
 339                 list($r) = $this->ui->userDialog('build',
 
 343                 if (substr($o['name'], 0, 5) == 'with-' &&
 
 344                     ($r == 'yes' || $r == 'autodetect')) {
 
 345                     $configure_command .= " --$o[name]";
 
 347                     $configure_command .= " --$o[name]=".trim($r);
 
 351         // }}} end of interactive part
 
 353         // FIXME make configurable
 
 354         if (!$user=getenv('USER')) {
 
 358         $tmpdir = $this->config->get('temp_dir');
 
 359         $build_basedir = System::mktemp(' -t "' . $tmpdir . '" -d "pear-build-' . $user . '"');
 
 360         $build_dir = "$build_basedir/$vdir";
 
 361         $inst_dir = "$build_basedir/install-$vdir";
 
 362         $this->log(1, "building in $build_dir");
 
 363         if (is_dir($build_dir)) {
 
 364             System::rm(array('-rf', $build_dir));
 
 367         if (!System::mkDir(array('-p', $build_dir))) {
 
 368             return $this->raiseError("could not create build dir: $build_dir");
 
 371         $this->addTempFile($build_dir);
 
 372         if (!System::mkDir(array('-p', $inst_dir))) {
 
 373             return $this->raiseError("could not create temporary install dir: $inst_dir");
 
 375         $this->addTempFile($inst_dir);
 
 377         $make_command = getenv('MAKE') ? getenv('MAKE') : 'make';
 
 382             "$make_command INSTALL_ROOT=\"$inst_dir\" install",
 
 383             "find \"$inst_dir\" | xargs ls -dils"
 
 385         if (!file_exists($build_dir) || !is_dir($build_dir) || !chdir($build_dir)) {
 
 386             return $this->raiseError("could not chdir to $build_dir");
 
 388         putenv('PHP_PEAR_VERSION=1.10.1');
 
 389         foreach ($to_run as $cmd) {
 
 390             $err = $this->_runCommand($cmd, $callback);
 
 391             if (PEAR::isError($err)) {
 
 397                 return $this->raiseError("`$cmd' failed");
 
 400         if (!($dp = opendir("modules"))) {
 
 402             return $this->raiseError("no `modules' directory found");
 
 404         $built_files = array();
 
 405         $prefix = exec($this->config->get('php_prefix')
 
 407                        $this->config->get('php_suffix') . " --prefix");
 
 408         $this->_harvestInstDir($prefix, $inst_dir . DIRECTORY_SEPARATOR . $prefix, $built_files);
 
 414      * Message callback function used when running the "phpize"
 
 415      * program.  Extracts the API numbers used.  Ignores other message
 
 416      * types than "cmdoutput".
 
 418      * @param string $what the type of message
 
 419      * @param mixed $data the message
 
 425     function phpizeCallback($what, $data)
 
 427         if ($what != 'cmdoutput') {
 
 430         $this->log(1, rtrim($data));
 
 431         if (preg_match('/You should update your .aclocal.m4/', $data)) {
 
 435         if (preg_match('/^\s+(\S[^:]+):\s+(\d{8})/', $data, $matches)) {
 
 436             $member = preg_replace('/[^a-z]/', '_', strtolower($matches[1]));
 
 437             $apino = (int)$matches[2];
 
 438             if (isset($this->$member)) {
 
 439                 $this->$member = $apino;
 
 440                 //$msg = sprintf("%-22s : %d", $matches[1], $apino);
 
 441                 //$this->log(1, $msg);
 
 447      * Run an external command, using a message callback to report
 
 448      * output.  The command will be run through popen and output is
 
 449      * reported for every line with a "cmdoutput" message with the
 
 450      * line string, including newlines, as payload.
 
 452      * @param string $command the command to run
 
 454      * @param mixed $callback (optional) function to use as message
 
 457      * @return bool whether the command was successful (exit code 0
 
 458      * means success, any other means failure)
 
 462     function _runCommand($command, $callback = null)
 
 464         $this->log(1, "running: $command");
 
 465         $pp = popen("$command 2>&1", "r");
 
 467             return $this->raiseError("failed to run `$command'");
 
 469         if ($callback && $callback[0]->debug == 1) {
 
 470             $olddbg = $callback[0]->debug;
 
 471             $callback[0]->debug = 2;
 
 474         while ($line = fgets($pp, 1024)) {
 
 476                 call_user_func($callback, 'cmdoutput', $line);
 
 478                 $this->log(2, rtrim($line));
 
 481         if ($callback && isset($olddbg)) {
 
 482             $callback[0]->debug = $olddbg;
 
 485         $exitcode = is_resource($pp) ? pclose($pp) : -1;
 
 486         return ($exitcode == 0);
 
 489     function log($level, $msg, $append_crlf = true)
 
 491         if ($this->current_callback) {
 
 492             if ($this->debug >= $level) {
 
 493                 call_user_func($this->current_callback, 'output', $msg);
 
 497         return parent::log($level, $msg, $append_crlf);