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

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