Subversion Repositories Applications.gtt

Rev

Rev 94 | Details | Compare with Previous | Last modification | View Log | RSS feed

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