PageRenderTime 56ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/php/PEAR/Builder.php

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