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.1 2004/10/19 04:27:53 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.     // {{{ build()
  153.  
  154.     /**
  155.      * Build an extension from source.  Runs "phpize" in the source
  156.      * directory, but compiles in a temporary directory
  157.      * (/var/tmp/pear-build-USER/PACKAGE-VERSION).
  158.      *
  159.      * @param string $descfile path to XML package description file
  160.      *
  161.      * @param mixed $callback callback function used to report output,
  162.      *  see PEAR_Builder::_runCommand for details
  163.      *
  164.      * @return array an array of associative arrays with built files,
  165.      *  format:
  166.      *  array( array( 'file' => '/path/to/ext.so',
  167.      *                'php_api' => YYYYMMDD,
  168.      *                'zend_mod_api' => YYYYMMDD,
  169.      *                'zend_ext_api' => YYYYMMDD ),
  170.      *         ... )
  171.      *
  172.      * @access public
  173.      *
  174.      * @see PEAR_Builder::_runCommand
  175.      * @see PEAR_Common::infoFromDescriptionFile
  176.      */
  177.     function build($descfile$callback = null)
  178.     {
  179.         if (PEAR_OS == "Windows"{
  180.             return $this->_build_win32($descfile,$callback);
  181.         }
  182.         if (PEAR_OS != 'Unix'{
  183.             return $this->raiseError("building extensions not supported on this platform");
  184.         }
  185.         if (PEAR::isError($info $this->infoFromDescriptionFile($descfile))) {
  186.             return $info;
  187.         }
  188.         $dir dirname($descfile);
  189.         $old_cwd getcwd();
  190.         if (!@chdir($dir)) {
  191.             return $this->raiseError("could not chdir to $dir");
  192.         }
  193.         $vdir = "$info[package]-$info[version]";
  194.         if (is_dir($vdir)) {
  195.             chdir($vdir);
  196.         }
  197.         $dir getcwd();
  198.         $this->log(2"building in $dir");
  199.         $this->current_callback $callback;
  200.         putenv('PATH=' $this->config->get('bin_dir'':' getenv('PATH'));
  201.         $err $this->_runCommand("phpize"array(&$this'phpizeCallback'));
  202.         if (PEAR::isError($err)) {
  203.             return $err;
  204.         }
  205.         if (!$err{
  206.             return $this->raiseError("`phpize' failed");
  207.         }
  208.  
  209.         // {{{ start of interactive part
  210.         $configure_command = "$dir/configure";
  211.         if (isset($info['configure_options'])) {
  212.             foreach ($info['configure_options'as $o{
  213.                 list($r$this->ui->userDialog('build',
  214.                                                  array($o['prompt']),
  215.                                                  array('text'),
  216.                                                  array(@$o['default']));
  217.                 if (substr($o['name']05== 'with-' &&
  218.                     ($r == 'yes' || $r == 'autodetect')) {
  219.                     $configure_command .= " --$o[name]";
  220.                 else {
  221.                     $configure_command .= " --$o[name]=".trim($r);
  222.                 }
  223.             }
  224.         }
  225.         // }}} end of interactive part
  226.  
  227.         // FIXME make configurable
  228.         if(!$user=getenv('USER')){
  229.             $user='defaultuser';
  230.         }
  231.         $build_basedir = "/var/tmp/pear-build-$user";
  232.         $build_dir = "$build_basedir/$info[package]-$info[version]";
  233.         $this->log(1"building in $build_dir");
  234.         if (is_dir($build_dir)) {
  235.             System::rm("-rf $build_dir");
  236.         }
  237.         if (!System::mkDir("-p $build_dir")) {
  238.             return $this->raiseError("could not create build dir: $build_dir");
  239.         }
  240.         $this->addTempFile($build_dir);
  241.         if (getenv('MAKE')) {
  242.             $make_command getenv('MAKE');
  243.         else {
  244.             $make_command 'make';
  245.         }
  246.         $to_run = array(
  247.             $configure_command,
  248.             $make_command,
  249.             );
  250.         if (!@chdir($build_dir)) {
  251.             return $this->raiseError("could not chdir to $build_dir");
  252.         }
  253.         foreach ($to_run as $cmd{
  254.             $err $this->_runCommand($cmd$callback);
  255.             if (PEAR::isError($err)) {
  256.                 chdir($old_cwd);
  257.                 return $err;
  258.             }
  259.             if (!$err{
  260.                 chdir($old_cwd);
  261.                 return $this->raiseError("`$cmd' failed");
  262.             }
  263.         }
  264.         if (!($dp opendir("modules"))) {
  265.             chdir($old_cwd);
  266.             return $this->raiseError("no `modules' directory found");
  267.         }
  268.         $built_files = array();
  269.         while ($ent readdir($dp)) {
  270.             if ($ent{0== '.' || substr($ent-3== '.la'{
  271.                 continue;
  272.             }
  273.             // harvest!
  274.             if (@copy("modules/$ent""$dir/$ent")) {
  275.                 $built_files[= array(
  276.                     'file' => "$dir/$ent",
  277.                     'php_api' => $this->php_api_version,
  278.                     'zend_mod_api' => $this->zend_module_api_no,
  279.                     'zend_ext_api' => $this->zend_extension_api_no,
  280.                     );
  281.  
  282.                 $this->log(1"$ent copied to $dir/$ent");
  283.             else {
  284.                 chdir($old_cwd);
  285.                 return $this->raiseError("failed copying $ent to $dir");
  286.             }
  287.         }
  288.         closedir($dp);
  289.         chdir($old_cwd);
  290.         return $built_files;
  291.     }
  292.  
  293.     // }}}
  294.     // {{{ phpizeCallback()
  295.  
  296.     /**
  297.      * Message callback function used when running the "phpize"
  298.      * program.  Extracts the API numbers used.  Ignores other message
  299.      * types than "cmdoutput".
  300.      *
  301.      * @param string $what the type of message
  302.      * @param mixed $data the message
  303.      *
  304.      * @return void 
  305.      *
  306.      * @access public
  307.      */
  308.     function phpizeCallback($what$data)
  309.     {
  310.         if ($what != 'cmdoutput'{
  311.             return;
  312.         }
  313.         $this->log(1rtrim($data));
  314.         if (preg_match('/You should update your .aclocal.m4/'$data)) {
  315.             return;
  316.         }
  317.         $matches = array();
  318.         if (preg_match('/^\s+(\S[^:]+):\s+(\d{8})/'$data$matches)) {
  319.             $member preg_replace('/[^a-z]/''_'strtolower($matches[1]));
  320.             $apino = (int)$matches[2];
  321.             if (isset($this->$member)) {
  322.                 $this->$member $apino;
  323.                 //$msg = sprintf("%-22s : %d", $matches[1], $apino);
  324.                 //$this->log(1, $msg);
  325.             }
  326.         }
  327.     }
  328.  
  329.     // }}}
  330.     // {{{ _runCommand()
  331.  
  332.     /**
  333.      * Run an external command, using a message callback to report
  334.      * output.  The command will be run through popen and output is
  335.      * reported for every line with a "cmdoutput" message with the
  336.      * line string, including newlines, as payload.
  337.      *
  338.      * @param string $command the command to run
  339.      *
  340.      * @param mixed $callback (optional) function to use as message
  341.      *  callback
  342.      *
  343.      * @return bool whether the command was successful (exit code 0
  344.      *  means success, any other means failure)
  345.      *
  346.      * @access private
  347.      */
  348.     function _runCommand($command$callback = null)
  349.     {
  350.         $this->log(1"running: $command");
  351.         $pp @popen("$command 2>&1""r");
  352.         if (!$pp{
  353.             return $this->raiseError("failed to run `$command'");
  354.         }
  355.         if ($callback && $callback[0]->debug == 1{
  356.             $olddbg $callback[0]->debug;
  357.             $callback[0]->debug = 2;
  358.         }
  359.  
  360.         while ($line fgets($pp1024)) {
  361.             if ($callback{
  362.                 call_user_func($callback'cmdoutput'$line);
  363.             else {
  364.                 $this->log(2rtrim($line));
  365.             }
  366.         }
  367.         if ($callback && isset($olddbg)) {
  368.             $callback[0]->debug = $olddbg;
  369.         }
  370.         $exitcode @pclose($pp);
  371.         return ($exitcode == 0);
  372.     }
  373.  
  374.     // }}}
  375.     // {{{ log()
  376.  
  377.     function log($level$msg)
  378.     {
  379.         if ($this->current_callback{
  380.             if ($this->debug >= $level{
  381.                 call_user_func($this->current_callback'output'$msg);
  382.             }
  383.             return;
  384.         }
  385.         return PEAR_Common::log($level$msg);
  386.     }
  387.  
  388.     // }}}
  389. }
  390.  
  391. ?>

Documentation generated on Mon, 11 Mar 2019 13:58:34 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.