Builder.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  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. // | Authors: Stig Sæther Bakken <ssb@php.net> |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Builder.php,v 1.16.2.3 2005/02/17 17:55:01 cellog Exp $
  20. require_once 'PEAR/Common.php';
  21. /**
  22. * Class to handle building (compiling) extensions.
  23. *
  24. * @author Stig Sæther Bakken <ssb@php.net>
  25. */
  26. class PEAR_Builder extends PEAR_Common
  27. {
  28. // {{{ properties
  29. var $php_api_version = 0;
  30. var $zend_module_api_no = 0;
  31. var $zend_extension_api_no = 0;
  32. var $extensions_built = array();
  33. var $current_callback = null;
  34. // used for msdev builds
  35. var $_lastline = null;
  36. var $_firstline = null;
  37. // }}}
  38. // {{{ constructor
  39. /**
  40. * PEAR_Builder constructor.
  41. *
  42. * @param object $ui user interface object (instance of PEAR_Frontend_*)
  43. *
  44. * @access public
  45. */
  46. function PEAR_Builder(&$ui)
  47. {
  48. parent::PEAR_Common();
  49. $this->setFrontendObject($ui);
  50. }
  51. // }}}
  52. // {{{ _build_win32()
  53. /**
  54. * Build an extension from source on windows.
  55. * requires msdev
  56. */
  57. function _build_win32($descfile, $callback = null)
  58. {
  59. if (PEAR::isError($info = $this->infoFromDescriptionFile($descfile))) {
  60. return $info;
  61. }
  62. $dir = dirname($descfile);
  63. $old_cwd = getcwd();
  64. if (!@chdir($dir)) {
  65. return $this->raiseError("could not chdir to $dir");
  66. }
  67. $this->log(2, "building in $dir");
  68. $dsp = $info['package'].'.dsp';
  69. if (!@is_file("$dir/$dsp")) {
  70. return $this->raiseError("The DSP $dsp does not exist.");
  71. }
  72. // XXX TODO: make release build type configurable
  73. $command = 'msdev '.$dsp.' /MAKE "'.$info['package']. ' - Release"';
  74. $this->current_callback = $callback;
  75. $err = $this->_runCommand($command, array(&$this, 'msdevCallback'));
  76. if (PEAR::isError($err)) {
  77. return $err;
  78. }
  79. // figure out the build platform and type
  80. $platform = 'Win32';
  81. $buildtype = 'Release';
  82. if (preg_match('/.*?'.$info['package'].'\s-\s(\w+)\s(.*?)-+/i',$this->_firstline,$matches)) {
  83. $platform = $matches[1];
  84. $buildtype = $matches[2];
  85. }
  86. if (preg_match('/(.*)?\s-\s(\d+).*?(\d+)/',$this->_lastline,$matches)) {
  87. if ($matches[2]) {
  88. // there were errors in the build
  89. return $this->raiseError("There were errors during compilation.");
  90. }
  91. $out = $matches[1];
  92. } else {
  93. return $this->raiseError("Did not understand the completion status returned from msdev.exe.");
  94. }
  95. // msdev doesn't tell us the output directory :/
  96. // open the dsp, find /out and use that directory
  97. $dsptext = join(file($dsp),'');
  98. // this regex depends on the build platform and type having been
  99. // correctly identified above.
  100. $regex ='/.*?!IF\s+"\$\(CFG\)"\s+==\s+("'.
  101. $info['package'].'\s-\s'.
  102. $platform.'\s'.
  103. $buildtype.'").*?'.
  104. '\/out:"(.*?)"/is';
  105. if ($dsptext && preg_match($regex,$dsptext,$matches)) {
  106. // what we get back is a relative path to the output file itself.
  107. $outfile = realpath($matches[2]);
  108. } else {
  109. return $this->raiseError("Could not retrieve output information from $dsp.");
  110. }
  111. if (@copy($outfile, "$dir/$out")) {
  112. $outfile = "$dir/$out";
  113. }
  114. $built_files[] = array(
  115. 'file' => "$outfile",
  116. 'php_api' => $this->php_api_version,
  117. 'zend_mod_api' => $this->zend_module_api_no,
  118. 'zend_ext_api' => $this->zend_extension_api_no,
  119. );
  120. return $built_files;
  121. }
  122. // }}}
  123. // {{{ msdevCallback()
  124. function msdevCallback($what, $data)
  125. {
  126. if (!$this->_firstline)
  127. $this->_firstline = $data;
  128. $this->_lastline = $data;
  129. }
  130. // }}}
  131. // {{{ _harventInstDir
  132. /**
  133. * @param string
  134. * @param string
  135. * @param array
  136. * @access private
  137. */
  138. function _harvestInstDir($dest_prefix, $dirname, &$built_files)
  139. {
  140. $d = opendir($dirname);
  141. if (!$d)
  142. return false;
  143. $ret = true;
  144. while (($ent = readdir($d)) !== false) {
  145. if ($ent{0} == '.')
  146. continue;
  147. $full = $dirname . DIRECTORY_SEPARATOR . $ent;
  148. if (is_dir($full)) {
  149. if (!$this->_harvestInstDir(
  150. $dest_prefix . DIRECTORY_SEPARATOR . $ent,
  151. $full, $built_files)) {
  152. $ret = false;
  153. break;
  154. }
  155. } else {
  156. $dest = $dest_prefix . DIRECTORY_SEPARATOR . $ent;
  157. $built_files[] = array(
  158. 'file' => $full,
  159. 'dest' => $dest,
  160. 'php_api' => $this->php_api_version,
  161. 'zend_mod_api' => $this->zend_module_api_no,
  162. 'zend_ext_api' => $this->zend_extension_api_no,
  163. );
  164. }
  165. }
  166. closedir($d);
  167. return $ret;
  168. }
  169. // }}}
  170. // {{{ build()
  171. /**
  172. * Build an extension from source. Runs "phpize" in the source
  173. * directory, but compiles in a temporary directory
  174. * (/var/tmp/pear-build-USER/PACKAGE-VERSION).
  175. *
  176. * @param string $descfile path to XML package description file
  177. *
  178. * @param mixed $callback callback function used to report output,
  179. * see PEAR_Builder::_runCommand for details
  180. *
  181. * @return array an array of associative arrays with built files,
  182. * format:
  183. * array( array( 'file' => '/path/to/ext.so',
  184. * 'php_api' => YYYYMMDD,
  185. * 'zend_mod_api' => YYYYMMDD,
  186. * 'zend_ext_api' => YYYYMMDD ),
  187. * ... )
  188. *
  189. * @access public
  190. *
  191. * @see PEAR_Builder::_runCommand
  192. * @see PEAR_Common::infoFromDescriptionFile
  193. */
  194. function build($descfile, $callback = null)
  195. {
  196. if (PEAR_OS == "Windows") {
  197. return $this->_build_win32($descfile,$callback);
  198. }
  199. if (PEAR_OS != 'Unix') {
  200. return $this->raiseError("building extensions not supported on this platform");
  201. }
  202. if (PEAR::isError($info = $this->infoFromDescriptionFile($descfile))) {
  203. return $info;
  204. }
  205. $dir = dirname($descfile);
  206. $old_cwd = getcwd();
  207. if (!@chdir($dir)) {
  208. return $this->raiseError("could not chdir to $dir");
  209. }
  210. $vdir = "$info[package]-$info[version]";
  211. if (is_dir($vdir)) {
  212. chdir($vdir);
  213. }
  214. $dir = getcwd();
  215. $this->log(2, "building in $dir");
  216. $this->current_callback = $callback;
  217. putenv('PATH=' . $this->config->get('bin_dir') . ':' . getenv('PATH'));
  218. $err = $this->_runCommand("phpize", array(&$this, 'phpizeCallback'));
  219. if (PEAR::isError($err)) {
  220. return $err;
  221. }
  222. if (!$err) {
  223. return $this->raiseError("`phpize' failed");
  224. }
  225. // {{{ start of interactive part
  226. $configure_command = "$dir/configure";
  227. if (isset($info['configure_options'])) {
  228. foreach ($info['configure_options'] as $o) {
  229. list($r) = $this->ui->userDialog('build',
  230. array($o['prompt']),
  231. array('text'),
  232. array(@$o['default']));
  233. if (substr($o['name'], 0, 5) == 'with-' &&
  234. ($r == 'yes' || $r == 'autodetect')) {
  235. $configure_command .= " --$o[name]";
  236. } else {
  237. $configure_command .= " --$o[name]=".trim($r);
  238. }
  239. }
  240. }
  241. // }}} end of interactive part
  242. // FIXME make configurable
  243. if(!$user=getenv('USER')){
  244. $user='defaultuser';
  245. }
  246. $build_basedir = "/var/tmp/pear-build-$user";
  247. $build_dir = "$build_basedir/$info[package]-$info[version]";
  248. $inst_dir = "$build_basedir/install-$info[package]-$info[version]";
  249. $this->log(1, "building in $build_dir");
  250. if (is_dir($build_dir)) {
  251. System::rm('-rf', $build_dir);
  252. }
  253. if (!System::mkDir(array('-p', $build_dir))) {
  254. return $this->raiseError("could not create build dir: $build_dir");
  255. }
  256. $this->addTempFile($build_dir);
  257. if (!System::mkDir(array('-p', $inst_dir))) {
  258. return $this->raiseError("could not create temporary install dir: $inst_dir");
  259. }
  260. $this->addTempFile($inst_dir);
  261. if (getenv('MAKE')) {
  262. $make_command = getenv('MAKE');
  263. } else {
  264. $make_command = 'make';
  265. }
  266. $to_run = array(
  267. $configure_command,
  268. $make_command,
  269. "$make_command INSTALL_ROOT=\"$inst_dir\" install",
  270. "find \"$inst_dir\" -ls"
  271. );
  272. if (!@chdir($build_dir)) {
  273. return $this->raiseError("could not chdir to $build_dir");
  274. }
  275. putenv('PHP_PEAR_VERSION=@PEAR-VER@');
  276. foreach ($to_run as $cmd) {
  277. $err = $this->_runCommand($cmd, $callback);
  278. if (PEAR::isError($err)) {
  279. chdir($old_cwd);
  280. return $err;
  281. }
  282. if (!$err) {
  283. chdir($old_cwd);
  284. return $this->raiseError("`$cmd' failed");
  285. }
  286. }
  287. if (!($dp = opendir("modules"))) {
  288. chdir($old_cwd);
  289. return $this->raiseError("no `modules' directory found");
  290. }
  291. $built_files = array();
  292. $prefix = exec("php-config --prefix");
  293. $this->_harvestInstDir($prefix, $inst_dir . DIRECTORY_SEPARATOR . $prefix, $built_files);
  294. chdir($old_cwd);
  295. return $built_files;
  296. }
  297. // }}}
  298. // {{{ phpizeCallback()
  299. /**
  300. * Message callback function used when running the "phpize"
  301. * program. Extracts the API numbers used. Ignores other message
  302. * types than "cmdoutput".
  303. *
  304. * @param string $what the type of message
  305. * @param mixed $data the message
  306. *
  307. * @return void
  308. *
  309. * @access public
  310. */
  311. function phpizeCallback($what, $data)
  312. {
  313. if ($what != 'cmdoutput') {
  314. return;
  315. }
  316. $this->log(1, rtrim($data));
  317. if (preg_match('/You should update your .aclocal.m4/', $data)) {
  318. return;
  319. }
  320. $matches = array();
  321. if (preg_match('/^\s+(\S[^:]+):\s+(\d{8})/', $data, $matches)) {
  322. $member = preg_replace('/[^a-z]/', '_', strtolower($matches[1]));
  323. $apino = (int)$matches[2];
  324. if (isset($this->$member)) {
  325. $this->$member = $apino;
  326. //$msg = sprintf("%-22s : %d", $matches[1], $apino);
  327. //$this->log(1, $msg);
  328. }
  329. }
  330. }
  331. // }}}
  332. // {{{ _runCommand()
  333. /**
  334. * Run an external command, using a message callback to report
  335. * output. The command will be run through popen and output is
  336. * reported for every line with a "cmdoutput" message with the
  337. * line string, including newlines, as payload.
  338. *
  339. * @param string $command the command to run
  340. *
  341. * @param mixed $callback (optional) function to use as message
  342. * callback
  343. *
  344. * @return bool whether the command was successful (exit code 0
  345. * means success, any other means failure)
  346. *
  347. * @access private
  348. */
  349. function _runCommand($command, $callback = null)
  350. {
  351. $this->log(1, "running: $command");
  352. $pp = @popen("$command 2>&1", "r");
  353. if (!$pp) {
  354. return $this->raiseError("failed to run `$command'");
  355. }
  356. if ($callback && $callback[0]->debug == 1) {
  357. $olddbg = $callback[0]->debug;
  358. $callback[0]->debug = 2;
  359. }
  360. while ($line = fgets($pp, 1024)) {
  361. if ($callback) {
  362. call_user_func($callback, 'cmdoutput', $line);
  363. } else {
  364. $this->log(2, rtrim($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. // {{{ log()
  375. function log($level, $msg)
  376. {
  377. if ($this->current_callback) {
  378. if ($this->debug >= $level) {
  379. call_user_func($this->current_callback, 'output', $msg);
  380. }
  381. return;
  382. }
  383. return PEAR_Common::log($level, $msg);
  384. }
  385. // }}}
  386. }
  387. ?>