PEAR
[ class tree: PEAR ] [ index: PEAR ] [ all elements ]

Source for file Builder.php

Documentation is available at Builder.php

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

Documentation generated on Mon, 11 Mar 2019 14:23:54 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.