3
// +----------------------------------------------------------------------+
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
// | Authors: Stig Sļæ½ther Bakken <ssb@php.net> |
17
// +----------------------------------------------------------------------+
19
// $Id: Builder.php,v 1.16.2.2 2005/03/28 16:46:06 cellog Exp $
21
require_once 'PEAR/Common.php';
24
* Class to handle building (compiling) extensions.
26
* @author Stig Sļæ½ther Bakken <ssb@php.net>
28
class PEAR_Builder extends PEAR_Common
32
var $php_api_version = 0;
33
var $zend_module_api_no = 0;
34
var $zend_extension_api_no = 0;
36
var $extensions_built = array();
38
var $current_callback = null;
40
// used for msdev builds
41
var $_lastline = null;
42
var $_firstline = null;
47
* PEAR_Builder constructor.
49
* @param object $ui user interface object (instance of PEAR_Frontend_*)
53
function PEAR_Builder(&$ui)
55
parent::PEAR_Common();
56
$this->setFrontendObject($ui);
64
* Build an extension from source on windows.
67
function _build_win32($descfile, $callback = null)
69
if (PEAR::isError($info = $this->infoFromDescriptionFile($descfile))) {
72
$dir = dirname($descfile);
76
return $this->raiseError("could not chdir to $dir");
78
$this->log(2, "building in $dir");
80
$dsp = $info['package'].'.dsp';
81
if (!@is_file("$dir/$dsp")) {
82
return $this->raiseError("The DSP $dsp does not exist.");
84
// XXX TODO: make release build type configurable
85
$command = 'msdev '.$dsp.' /MAKE "'.$info['package']. ' - Release"';
87
$this->current_callback = $callback;
88
$err = $this->_runCommand($command, array(&$this, 'msdevCallback'));
89
if (PEAR::isError($err)) {
93
// figure out the build platform and type
95
$buildtype = 'Release';
96
if (preg_match('/.*?'.$info['package'].'\s-\s(\w+)\s(.*?)-+/i',$this->_firstline,$matches)) {
97
$platform = $matches[1];
98
$buildtype = $matches[2];
101
if (preg_match('/(.*)?\s-\s(\d+).*?(\d+)/',$this->_lastline,$matches)) {
103
// there were errors in the build
104
return $this->raiseError("There were errors during compilation.");
108
return $this->raiseError("Did not understand the completion status returned from msdev.exe.");
111
// msdev doesn't tell us the output directory :/
112
// open the dsp, find /out and use that directory
113
$dsptext = join(file($dsp),'');
115
// this regex depends on the build platform and type having been
116
// correctly identified above.
117
$regex ='/.*?!IF\s+"\$\(CFG\)"\s+==\s+("'.
118
$info['package'].'\s-\s'.
123
if ($dsptext && preg_match($regex,$dsptext,$matches)) {
124
// what we get back is a relative path to the output file itself.
125
$outfile = realpath($matches[2]);
127
return $this->raiseError("Could not retrieve output information from $dsp.");
129
if (@copy($outfile, "$dir/$out")) {
130
$outfile = "$dir/$out";
133
$built_files[] = array(
134
'file' => "$outfile",
135
'php_api' => $this->php_api_version,
136
'zend_mod_api' => $this->zend_module_api_no,
137
'zend_ext_api' => $this->zend_extension_api_no,
144
// {{{ msdevCallback()
145
function msdevCallback($what, $data)
147
if (!$this->_firstline)
148
$this->_firstline = $data;
149
$this->_lastline = $data;
153
// {{{ _harventInstDir
160
function _harvestInstDir($dest_prefix, $dirname, &$built_files)
162
$d = opendir($dirname);
167
while (($ent = readdir($d)) !== false) {
171
$full = $dirname . DIRECTORY_SEPARATOR . $ent;
173
if (!$this->_harvestInstDir(
174
$dest_prefix . DIRECTORY_SEPARATOR . $ent,
175
$full, $built_files)) {
180
$dest = $dest_prefix . DIRECTORY_SEPARATOR . $ent;
181
$built_files[] = array(
184
'php_api' => $this->php_api_version,
185
'zend_mod_api' => $this->zend_module_api_no,
186
'zend_ext_api' => $this->zend_extension_api_no,
199
* Build an extension from source. Runs "phpize" in the source
200
* directory, but compiles in a temporary directory
201
* (/var/tmp/pear-build-USER/PACKAGE-VERSION).
203
* @param string $descfile path to XML package description file
205
* @param mixed $callback callback function used to report output,
206
* see PEAR_Builder::_runCommand for details
208
* @return array an array of associative arrays with built files,
210
* array( array( 'file' => '/path/to/ext.so',
211
* 'php_api' => YYYYMMDD,
212
* 'zend_mod_api' => YYYYMMDD,
213
* 'zend_ext_api' => YYYYMMDD ),
218
* @see PEAR_Builder::_runCommand
219
* @see PEAR_Common::infoFromDescriptionFile
221
function build($descfile, $callback = null)
223
if (PEAR_OS == "Windows") {
224
return $this->_build_win32($descfile,$callback);
226
if (PEAR_OS != 'Unix') {
227
return $this->raiseError("building extensions not supported on this platform");
229
if (PEAR::isError($info = $this->infoFromDescriptionFile($descfile))) {
232
$dir = dirname($descfile);
235
return $this->raiseError("could not chdir to $dir");
237
$vdir = "$info[package]-$info[version]";
242
$this->log(2, "building in $dir");
243
$this->current_callback = $callback;
244
putenv('PATH=' . $this->config->get('bin_dir') . ':' . getenv('PATH'));
245
$err = $this->_runCommand("phpize", array(&$this, 'phpizeCallback'));
246
if (PEAR::isError($err)) {
250
return $this->raiseError("`phpize' failed");
253
// {{{ start of interactive part
254
$configure_command = "$dir/configure";
255
if (isset($info['configure_options'])) {
256
foreach ($info['configure_options'] as $o) {
257
list($r) = $this->ui->userDialog('build',
260
array(@$o['default']));
261
if (substr($o['name'], 0, 5) == 'with-' &&
262
($r == 'yes' || $r == 'autodetect')) {
263
$configure_command .= " --$o[name]";
265
$configure_command .= " --$o[name]=".trim($r);
269
// }}} end of interactive part
271
// FIXME make configurable
272
if(!$user=getenv('USER')){
275
$build_basedir = "/var/tmp/pear-build-$user";
276
$build_dir = "$build_basedir/$info[package]-$info[version]";
277
$inst_dir = "$build_basedir/install-$info[package]-$info[version]";
278
$this->log(1, "building in $build_dir");
279
if (is_dir($build_dir)) {
280
System::rm(array('-rf', $build_dir));
282
if (!System::mkDir(array('-p', $build_dir))) {
283
return $this->raiseError("could not create build dir: $build_dir");
285
$this->addTempFile($build_dir);
286
if (!System::mkDir(array('-p', $inst_dir))) {
287
return $this->raiseError("could not create temporary install dir: $inst_dir");
289
$this->addTempFile($inst_dir);
291
if (getenv('MAKE')) {
292
$make_command = getenv('MAKE');
294
$make_command = 'make';
299
"$make_command INSTALL_ROOT=\"$inst_dir\" install",
300
"find \"$inst_dir\" -ls"
302
if (!@chdir($build_dir)) {
303
return $this->raiseError("could not chdir to $build_dir");
305
putenv('PHP_PEAR_VERSION=@PEAR-VER@');
306
foreach ($to_run as $cmd) {
307
$err = $this->_runCommand($cmd, $callback);
308
if (PEAR::isError($err)) {
314
return $this->raiseError("`$cmd' failed");
317
if (!($dp = opendir("modules"))) {
319
return $this->raiseError("no `modules' directory found");
321
$built_files = array();
322
$prefix = exec("php-config --prefix");
323
$this->_harvestInstDir($prefix, $inst_dir . DIRECTORY_SEPARATOR . $prefix, $built_files);
329
// {{{ phpizeCallback()
332
* Message callback function used when running the "phpize"
333
* program. Extracts the API numbers used. Ignores other message
334
* types than "cmdoutput".
336
* @param string $what the type of message
337
* @param mixed $data the message
343
function phpizeCallback($what, $data)
345
if ($what != 'cmdoutput') {
348
$this->log(1, rtrim($data));
349
if (preg_match('/You should update your .aclocal.m4/', $data)) {
353
if (preg_match('/^\s+(\S[^:]+):\s+(\d{8})/', $data, $matches)) {
354
$member = preg_replace('/[^a-z]/', '_', strtolower($matches[1]));
355
$apino = (int)$matches[2];
356
if (isset($this->$member)) {
357
$this->$member = $apino;
358
//$msg = sprintf("%-22s : %d", $matches[1], $apino);
359
//$this->log(1, $msg);
368
* Run an external command, using a message callback to report
369
* output. The command will be run through popen and output is
370
* reported for every line with a "cmdoutput" message with the
371
* line string, including newlines, as payload.
373
* @param string $command the command to run
375
* @param mixed $callback (optional) function to use as message
378
* @return bool whether the command was successful (exit code 0
379
* means success, any other means failure)
383
function _runCommand($command, $callback = null)
385
$this->log(1, "running: $command");
386
$pp = @popen("$command 2>&1", "r");
388
return $this->raiseError("failed to run `$command'");
390
if ($callback && $callback[0]->debug == 1) {
391
$olddbg = $callback[0]->debug;
392
$callback[0]->debug = 2;
395
while ($line = fgets($pp, 1024)) {
397
call_user_func($callback, 'cmdoutput', $line);
399
$this->log(2, rtrim($line));
402
if ($callback && isset($olddbg)) {
403
$callback[0]->debug = $olddbg;
405
$exitcode = @pclose($pp);
406
return ($exitcode == 0);
412
function log($level, $msg)
414
if ($this->current_callback) {
415
if ($this->debug >= $level) {
416
call_user_func($this->current_callback, 'output', $msg);
420
return PEAR_Common::log($level, $msg);