[ Index ]
 

Code source de PHP PEAR 1.4.5

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/PEAR/ -> Builder.php (source)

   1  <?php
   2  /**
   3   * PEAR_Builder for building PHP extensions (PECL packages)
   4   *
   5   * PHP versions 4 and 5
   6   *
   7   * LICENSE: This source file is subject to version 3.0 of the PHP license
   8   * that is available through the world-wide-web at the following URI:
   9   * http://www.php.net/license/3_0.txt.  If you did not receive a copy of
  10   * the PHP License and are unable to obtain it through the web, please
  11   * send a note to license@php.net so we can mail you a copy immediately.
  12   *
  13   * @category   pear
  14   * @package    PEAR
  15   * @author     Stig Bakken <ssb@php.net>
  16   * @author     Greg Beaver <cellog@php.net>
  17   * @copyright  1997-2006 The PHP Group
  18   * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  19   * @version    CVS: $Id: Builder.php,v 1.31 2007/01/10 05:32:51 cellog Exp $
  20   * @link       http://pear.php.net/package/PEAR
  21   * @since      File available since Release 0.1
  22   * 
  23   * TODO: log output parameters in PECL command line
  24   * TODO: msdev path in configuration
  25   */
  26  
  27  /**
  28   * Needed for extending PEAR_Builder
  29   */
  30  require_once  'PEAR/Common.php';
  31  require_once  'PEAR/PackageFile.php';
  32  /**
  33   * Class to handle building (compiling) extensions.
  34   *
  35   * @category   pear
  36   * @package    PEAR
  37   * @author     Stig Bakken <ssb@php.net>
  38   * @author     Greg Beaver <cellog@php.net>
  39   * @copyright  1997-2006 The PHP Group
  40   * @license    http://www.php.net/license/3_0.txt  PHP License 3.0
  41   * @version    Release: 1.5.0
  42   * @link       http://pear.php.net/package/PEAR
  43   * @since      Class available since PHP 4.0.2
  44   * @see        http://pear.php.net/manual/en/core.ppm.pear-builder.php
  45   */
  46  class PEAR_Builder extends PEAR_Common
  47  {
  48      // {{{ properties
  49  
  50      var $php_api_version = 0;
  51      var $zend_module_api_no = 0;
  52      var $zend_extension_api_no = 0;
  53  
  54      var $extensions_built = array();
  55  
  56      /**
  57       * @var string Used for reporting when it is not possible to pass function
  58       *             via extra parameter, e.g. log, msdevCallback
  59       */
  60      var $current_callback = null;
  61  
  62      // used for msdev builds
  63      var $_lastline = null;
  64      var $_firstline = null;
  65      // }}}
  66      // {{{ constructor
  67  
  68      /**
  69       * PEAR_Builder constructor.
  70       *
  71       * @param object $ui user interface object (instance of PEAR_Frontend_*)
  72       *
  73       * @access public
  74       */
  75      function PEAR_Builder(&$ui)
  76      {
  77          parent::PEAR_Common();
  78          $this->setFrontendObject($ui);
  79      }
  80  
  81      // }}}
  82  
  83      // {{{ _build_win32()
  84  
  85      /**
  86       * Build an extension from source on windows.
  87       * requires msdev
  88       */
  89      function _build_win32($descfile, $callback = null)
  90      {
  91          if (is_object($descfile)) {
  92              $pkg = $descfile;
  93              $descfile = $pkg->getPackageFile();
  94          } else {
  95              $pf = &new PEAR_PackageFile($this->config, $this->debug);
  96              $pkg = &$pf->fromPackageFile($descfile, PEAR_VALIDATE_NORMAL);
  97              if (PEAR::isError($pkg)) {
  98                  return $pkg;
  99              }
 100          }
 101          $dir = dirname($descfile);
 102          $old_cwd = getcwd();
 103  
 104          if (!file_exists($dir) || !is_dir($dir) || !chdir($dir)) {
 105              return $this->raiseError("could not chdir to $dir");
 106          }
 107          // packages that were in a .tar have the packagefile in this directory
 108          $vdir = $pkg->getPackage() . '-' . $pkg->getVersion();
 109          if (file_exists($dir) && is_dir($vdir)) {
 110              if (chdir($vdir)) {
 111                  $dir = getcwd();
 112              } else {
 113                  return $this->raiseError("could not chdir to " . realpath($vdir));
 114              }
 115          }
 116  
 117          $this->log(2, "building in $dir");
 118  
 119          $dsp = $pkg->getPackage().'.dsp';
 120          if (!file_exists("$dir/$dsp")) {
 121              return $this->raiseError("The DSP $dsp does not exist.");
 122          }
 123          // XXX TODO: make release build type configurable
 124          $command = 'msdev '.$dsp.' /MAKE "'.$pkg->getPackage(). ' - Release"';
 125  
 126          $err = $this->_runCommand($command, array(&$this, 'msdevCallback'));
 127          if (PEAR::isError($err)) {
 128              return $err;
 129          }
 130  
 131          // figure out the build platform and type
 132          $platform = 'Win32';
 133          $buildtype = 'Release';
 134          if (preg_match('/.*?'.$pkg->getPackage().'\s-\s(\w+)\s(.*?)-+/i',$this->_firstline,$matches)) {
 135              $platform = $matches[1];
 136              $buildtype = $matches[2];
 137          }
 138  
 139          if (preg_match('/(.*)?\s-\s(\d+).*?(\d+)/',$this->_lastline,$matches)) {
 140              if ($matches[2]) {
 141                  // there were errors in the build
 142                  return $this->raiseError("There were errors during compilation.");
 143              }
 144              $out = $matches[1];
 145          } else {
 146              return $this->raiseError("Did not understand the completion status returned from msdev.exe.");
 147          }
 148  
 149          // msdev doesn't tell us the output directory :/
 150          // open the dsp, find /out and use that directory
 151          $dsptext = join(file($dsp),'');
 152  
 153          // this regex depends on the build platform and type having been
 154          // correctly identified above.
 155          $regex ='/.*?!IF\s+"\$\(CFG\)"\s+==\s+("'.
 156                      $pkg->getPackage().'\s-\s'.
 157                      $platform.'\s'.
 158                      $buildtype.'").*?'.
 159                      '\/out:"(.*?)"/is';
 160  
 161          if ($dsptext && preg_match($regex,$dsptext,$matches)) {
 162              // what we get back is a relative path to the output file itself.
 163              $outfile = realpath($matches[2]);
 164          } else {
 165              return $this->raiseError("Could not retrieve output information from $dsp.");
 166          }
 167          // realpath returns false if the file doesn't exist
 168          if ($outfile && copy($outfile, "$dir/$out")) {
 169              $outfile = "$dir/$out";
 170          }
 171  
 172          $built_files[] = array(
 173              'file' => "$outfile",
 174              'php_api' => $this->php_api_version,
 175              'zend_mod_api' => $this->zend_module_api_no,
 176              'zend_ext_api' => $this->zend_extension_api_no,
 177              );
 178  
 179          return $built_files;
 180      }
 181      // }}}
 182  
 183      // {{{ msdevCallback()
 184      function msdevCallback($what, $data)
 185      {
 186          if (!$this->_firstline)
 187              $this->_firstline = $data;
 188          $this->_lastline = $data;
 189          call_user_func($this->current_callback, $what, $data);
 190      }
 191      // }}}
 192  
 193      // {{{ _harventInstDir
 194      /**
 195       * @param string
 196       * @param string
 197       * @param array
 198       * @access private
 199       */
 200      function _harvestInstDir($dest_prefix, $dirname, &$built_files)
 201      {
 202          $d = opendir($dirname);
 203          if (!$d)
 204              return false;
 205  
 206          $ret = true;
 207          while (($ent = readdir($d)) !== false) {
 208              if ($ent{0} == '.')
 209                  continue;
 210  
 211              $full = $dirname . DIRECTORY_SEPARATOR . $ent;
 212              if (is_dir($full)) {
 213                  if (!$this->_harvestInstDir(
 214                          $dest_prefix . DIRECTORY_SEPARATOR . $ent,
 215                          $full, $built_files)) {
 216                      $ret = false;
 217                      break;
 218                  }
 219              } else {
 220                  $dest = $dest_prefix . DIRECTORY_SEPARATOR . $ent;
 221                  $built_files[] = array(
 222                          'file' => $full,
 223                          'dest' => $dest,
 224                          'php_api' => $this->php_api_version,
 225                          'zend_mod_api' => $this->zend_module_api_no,
 226                          'zend_ext_api' => $this->zend_extension_api_no,
 227                          );
 228              }
 229          }
 230          closedir($d);
 231          return $ret;
 232      }
 233  
 234      // }}}
 235  
 236      // {{{ build()
 237  
 238      /**
 239       * Build an extension from source.  Runs "phpize" in the source
 240       * directory, but compiles in a temporary directory
 241       * (/var/tmp/pear-build-USER/PACKAGE-VERSION).
 242       *
 243       * @param string|PEAR_PackageFile_v* $descfile path to XML package description file, or
 244       *               a PEAR_PackageFile object
 245       *
 246       * @param mixed $callback callback function used to report output,
 247       * see PEAR_Builder::_runCommand for details
 248       *
 249       * @return array an array of associative arrays with built files,
 250       * format:
 251       * array( array( 'file' => '/path/to/ext.so',
 252       *               'php_api' => YYYYMMDD,
 253       *               'zend_mod_api' => YYYYMMDD,
 254       *               'zend_ext_api' => YYYYMMDD ),
 255       *        ... )
 256       *
 257       * @access public
 258       *
 259       * @see PEAR_Builder::_runCommand
 260       */
 261      function build($descfile, $callback = null)
 262      {
 263          $this->current_callback = $callback;
 264          if (PEAR_OS == "Windows") {
 265              return $this->_build_win32($descfile,$callback);
 266          }
 267          if (PEAR_OS != 'Unix') {
 268              return $this->raiseError("building extensions not supported on this platform");
 269          }
 270          if (is_object($descfile)) {
 271              $pkg = $descfile;
 272              $descfile = $pkg->getPackageFile();
 273          } else {
 274              $pf = &new PEAR_PackageFile($this->config);
 275              $pkg = &$pf->fromPackageFile($descfile, PEAR_VALIDATE_NORMAL);
 276              if (PEAR::isError($pkg)) {
 277                  return $pkg;
 278              }
 279          }
 280          $dir = dirname($descfile);
 281          $old_cwd = getcwd();
 282          if (!file_exists($dir) || !is_dir($dir) || !chdir($dir)) {
 283              return $this->raiseError("could not chdir to $dir");
 284          }
 285          $vdir = $pkg->getPackage() . '-' . $pkg->getVersion();
 286          if (is_dir($vdir)) {
 287              chdir($vdir);
 288          }
 289          $dir = getcwd();
 290          $this->log(2, "building in $dir");
 291          putenv('PATH=' . $this->config->get('bin_dir') . ':' . getenv('PATH'));
 292          $err = $this->_runCommand("phpize", array(&$this, 'phpizeCallback'));
 293          if (PEAR::isError($err)) {
 294              return $err;
 295          }
 296          if (!$err) {
 297              return $this->raiseError("`phpize' failed");
 298          }
 299  
 300          // {{{ start of interactive part
 301          $configure_command = "$dir/configure";
 302          $configure_options = $pkg->getConfigureOptions();
 303          if ($configure_options) {
 304              foreach ($configure_options as $o) {
 305                  $default = array_key_exists('default', $o) ? $o['default'] : null;
 306                  list($r) = $this->ui->userDialog('build',
 307                                                   array($o['prompt']),
 308                                                   array('text'),
 309                                                   array($default));
 310                  if (substr($o['name'], 0, 5) == 'with-' &&
 311                      ($r == 'yes' || $r == 'autodetect')) {
 312                      $configure_command .= " --$o[name]";
 313                  } else {
 314                      $configure_command .= " --$o[name]=".trim($r);
 315                  }
 316              }
 317          }
 318          // }}} end of interactive part
 319  
 320          // FIXME make configurable
 321          if(!$user=getenv('USER')){
 322              $user='defaultuser';
 323          }
 324          $build_basedir = "/var/tmp/pear-build-$user";
 325          $build_dir = "$build_basedir/$vdir";
 326          $inst_dir = "$build_basedir/install-$vdir";
 327          $this->log(1, "building in $build_dir");
 328          if (is_dir($build_dir)) {
 329              System::rm(array('-rf', $build_dir));
 330          }
 331          if (!System::mkDir(array('-p', $build_dir))) {
 332              return $this->raiseError("could not create build dir: $build_dir");
 333          }
 334          $this->addTempFile($build_dir);
 335          if (!System::mkDir(array('-p', $inst_dir))) {
 336              return $this->raiseError("could not create temporary install dir: $inst_dir");
 337          }
 338          $this->addTempFile($inst_dir);
 339  
 340          if (getenv('MAKE')) {
 341              $make_command = getenv('MAKE');
 342          } else {
 343              $make_command = 'make';
 344          }
 345          $to_run = array(
 346              $configure_command,
 347              $make_command,
 348              "$make_command INSTALL_ROOT=\"$inst_dir\" install",
 349              "find \"$inst_dir\" -ls"
 350              );
 351          if (!file_exists($build_dir) || !is_dir($build_dir) || !chdir($build_dir)) {
 352              return $this->raiseError("could not chdir to $build_dir");
 353          }
 354          putenv('PHP_PEAR_VERSION=1.5.0');
 355          foreach ($to_run as $cmd) {
 356              $err = $this->_runCommand($cmd, $callback);
 357              if (PEAR::isError($err)) {
 358                  chdir($old_cwd);
 359                  return $err;
 360              }
 361              if (!$err) {
 362                  chdir($old_cwd);
 363                  return $this->raiseError("`$cmd' failed");
 364              }
 365          }
 366          if (!($dp = opendir("modules"))) {
 367              chdir($old_cwd);
 368              return $this->raiseError("no `modules' directory found");
 369          }
 370          $built_files = array();
 371          $prefix = exec("php-config --prefix");
 372          $this->_harvestInstDir($prefix, $inst_dir . DIRECTORY_SEPARATOR . $prefix, $built_files);
 373          chdir($old_cwd);
 374          return $built_files;
 375      }
 376  
 377      // }}}
 378      // {{{ phpizeCallback()
 379  
 380      /**
 381       * Message callback function used when running the "phpize"
 382       * program.  Extracts the API numbers used.  Ignores other message
 383       * types than "cmdoutput".
 384       *
 385       * @param string $what the type of message
 386       * @param mixed $data the message
 387       *
 388       * @return void
 389       *
 390       * @access public
 391       */
 392      function phpizeCallback($what, $data)
 393      {
 394          if ($what != 'cmdoutput') {
 395              return;
 396          }
 397          $this->log(1, rtrim($data));
 398          if (preg_match('/You should update your .aclocal.m4/', $data)) {
 399              return;
 400          }
 401          $matches = array();
 402          if (preg_match('/^\s+(\S[^:]+):\s+(\d{8})/', $data, $matches)) {
 403              $member = preg_replace('/[^a-z]/', '_', strtolower($matches[1]));
 404              $apino = (int)$matches[2];
 405              if (isset($this->$member)) {
 406                  $this->$member = $apino;
 407                  //$msg = sprintf("%-22s : %d", $matches[1], $apino);
 408                  //$this->log(1, $msg);
 409              }
 410          }
 411      }
 412  
 413      // }}}
 414      // {{{ _runCommand()
 415  
 416      /**
 417       * Run an external command, using a message callback to report
 418       * output.  The command will be run through popen and output is
 419       * reported for every line with a "cmdoutput" message with the
 420       * line string, including newlines, as payload.
 421       *
 422       * @param string $command the command to run
 423       *
 424       * @param mixed $callback (optional) function to use as message
 425       * callback
 426       *
 427       * @return bool whether the command was successful (exit code 0
 428       * means success, any other means failure)
 429       *
 430       * @access private
 431       */
 432      function _runCommand($command, $callback = null)
 433      {
 434          $this->log(1, "running: $command");
 435          $pp = popen("$command 2>&1", "r");
 436          if (!$pp) {
 437              return $this->raiseError("failed to run `$command'");
 438          }
 439          if ($callback && $callback[0]->debug == 1) {
 440              $olddbg = $callback[0]->debug;
 441              $callback[0]->debug = 2;
 442          }
 443  
 444          while ($line = fgets($pp, 1024)) {
 445              if ($callback) {
 446                  call_user_func($callback, 'cmdoutput', $line);
 447              } else {
 448                  $this->log(2, rtrim($line));
 449              }
 450          }
 451          if ($callback && isset($olddbg)) {
 452              $callback[0]->debug = $olddbg;
 453          }
 454          if (is_resource($pp)) {
 455              $exitcode = pclose($pp);
 456          } else {
 457              $exitcode = -1;
 458          }
 459          return ($exitcode == 0);
 460      }
 461  
 462      // }}}
 463      // {{{ log()
 464  
 465      function log($level, $msg)
 466      {
 467          if ($this->current_callback) {
 468              if ($this->debug >= $level) {
 469                  call_user_func($this->current_callback, 'output', $msg);
 470              }
 471              return;
 472          }
 473          return PEAR_Common::log($level, $msg);
 474      }
 475  
 476      // }}}
 477  }
 478  
 479  ?>


Généré le : Sun Feb 25 14:08:00 2007 par Balluche grâce à PHPXref 0.7