PageRenderTime 68ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/win32/build/mkdist.php

https://github.com/brighub/php-src
PHP | 548 lines | 380 code | 86 blank | 82 comment | 82 complexity | 4fce2a1eb17173b62392e28f5bbb3d72 MD5 | raw file
Possible License(s): MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-3-Clause
  1. <?php # $Id$
  2. /* piece together a windows binary distro */
  3. $build_dir = $argv[1];
  4. $php_build_dir = $argv[2];
  5. $phpdll = $argv[3];
  6. $sapi_targets = explode(" ", $argv[4]);
  7. $ext_targets = explode(" ", $argv[5]);
  8. $pecl_targets = explode(" ", $argv[6]);
  9. $snapshot_template = $argv[7];
  10. $is_debug = preg_match("/^debug/i", $build_dir);
  11. echo "Making dist for $build_dir\n";
  12. $dist_dir = $build_dir . "/php-" . phpversion();
  13. $test_dir = $build_dir . "/php-test-pack-" . phpversion();
  14. $pecl_dir = $build_dir . "/pecl-" . phpversion();
  15. @mkdir($dist_dir);
  16. @mkdir("$dist_dir/ext");
  17. @mkdir("$dist_dir/dev");
  18. @mkdir("$dist_dir/extras");
  19. @mkdir($pecl_dir);
  20. /* figure out additional DLL's that are required */
  21. $extra_dll_deps = array();
  22. $per_module_deps = array();
  23. $pecl_dll_deps = array();
  24. function get_depends($module)
  25. {
  26. static $no_dist = array(
  27. /* windows system dlls that should not be bundled */
  28. 'advapi32.dll', 'comdlg32.dll', 'crypt32.dll', 'gdi32.dll', 'kernel32.dll', 'ntdll.dll',
  29. 'odbc32.dll', 'ole32.dll', 'oleaut32.dll', 'rpcrt4.dll',
  30. 'shell32.dll', 'shlwapi.dll', 'user32.dll', 'ws2_32.dll', 'ws2help.dll',
  31. 'comctl32.dll', 'winmm.dll', 'wsock32.dll', 'winspool.drv', 'msasn1.dll',
  32. 'secur32.dll', 'netapi32.dll',
  33. /* apache */
  34. 'apachecore.dll',
  35. /* apache 2 */
  36. 'libhttpd.dll', 'libapr.dll', 'libaprutil.dll','libapr-1.dll', 'libaprutil-1.dll',
  37. /* pi3web */
  38. 'piapi.dll', 'pi3api.dll',
  39. /* nsapi */
  40. 'ns-httpd30.dll', 'ns-httpd35.dll', 'ns-httpd36.dll', 'ns-httpd40.dll',
  41. /* oracle */
  42. 'oci.dll', 'ociw32.dll',
  43. /* sybase */
  44. 'libcs.dll', 'libct.dll',
  45. /* firebird */
  46. 'fbclient.dll',
  47. /* visual C++; mscvrt.dll is present on everyones system,
  48. * but the debug version (msvcrtd.dll) and those from visual studio.net
  49. * (msvcrt7x.dll) are not */
  50. 'msvcrt.dll',
  51. 'msvcr90.dll',
  52. 'wldap32.dll'
  53. );
  54. global $build_dir, $extra_dll_deps, $ext_targets, $sapi_targets, $pecl_targets, $phpdll, $per_module_deps, $pecl_dll_deps;
  55. $bd = strtolower(realpath($build_dir));
  56. $is_pecl = in_array($module, $pecl_targets);
  57. $cmd = "$GLOBALS[build_dir]\\deplister.exe \"$module\" \"$GLOBALS[build_dir]\"";
  58. $proc = proc_open($cmd,
  59. array(1 => array("pipe", "w")),
  60. $pipes);
  61. $n = 0;
  62. while (($line = fgetcsv($pipes[1]))) {
  63. $n++;
  64. $dep = strtolower($line[0]);
  65. $depbase = basename($dep);
  66. /* ignore stuff in our build dir, but only if it is
  67. * one of our targets */
  68. if (((in_array($depbase, $sapi_targets) ||
  69. in_array($depbase, $ext_targets) || in_array($depbase, $pecl_targets)) ||
  70. $depbase == $phpdll) && file_exists($GLOBALS['build_dir'] . "/$depbase")) {
  71. continue;
  72. }
  73. /* ignore some well-known system dlls */
  74. if (in_array(basename($dep), $no_dist)) {
  75. continue;
  76. }
  77. if ($is_pecl) {
  78. if (!in_array($dep, $pecl_dll_deps)) {
  79. $pecl_dll_deps[] = $dep;
  80. }
  81. } else {
  82. if (!in_array($dep, $extra_dll_deps)) {
  83. $extra_dll_deps[] = $dep;
  84. }
  85. }
  86. $per_module_deps[basename($module)][] = $dep;
  87. }
  88. fclose($pipes[1]);
  89. proc_close($proc);
  90. //echo "Module $module [$n lines]\n";
  91. }
  92. function copy_file_list($source_dir, $dest_dir, $list)
  93. {
  94. global $is_debug, $dist_dir;
  95. foreach ($list as $item) {
  96. if (empty($item)) {
  97. continue;
  98. } elseif (!is_file($source_dir . DIRECTORY_SEPARATOR . $item)) {
  99. echo "WARNING: $item not found\n";
  100. continue;
  101. }
  102. echo "Copying $item from $source_dir to $dest_dir\n";
  103. copy($source_dir . DIRECTORY_SEPARATOR . $item, $dest_dir . DIRECTORY_SEPARATOR . $item);
  104. if ($is_debug) {
  105. $itemdb = preg_replace("/\.(exe|dll|lib)$/i", ".pdb", $item);
  106. if (file_exists("$source_dir/$itemdb")) {
  107. copy("$source_dir/$itemdb", "$dist_dir/dev/$itemdb");
  108. }
  109. }
  110. if (preg_match("/\.(exe|dll)$/i", $item)) {
  111. get_depends($source_dir . '/' . $item);
  112. }
  113. }
  114. }
  115. function copy_text_file($source, $dest)
  116. {
  117. $text = file_get_contents($source);
  118. $text = preg_replace("/(\r\n?)|\n/", "\r\n", $text);
  119. $fp = fopen($dest, "w");
  120. fwrite($fp, $text);
  121. fclose($fp);
  122. }
  123. /* very light-weight function to extract a single named file from
  124. * a gzipped tarball. This makes assumptions about the files
  125. * based on the PEAR info set in $packages. */
  126. function extract_file_from_tarball($pkg, $filename, $dest_dir) /* {{{ */
  127. {
  128. global $packages;
  129. $name = $pkg . '-' . $packages[$pkg];
  130. $tarball = $dest_dir . "/" . $name . '.tgz';
  131. $filename = $name . '/' . $filename;
  132. $destfilename = $dest_dir . "/" . basename($filename);
  133. $fp = gzopen($tarball, 'rb');
  134. $done = false;
  135. do {
  136. /* read the header */
  137. $hdr_data = gzread($fp, 512);
  138. if (strlen($hdr_data) == 0)
  139. break;
  140. $checksum = 0;
  141. for ($i = 0; $i < 148; $i++)
  142. $checksum += ord($hdr_data{$i});
  143. for ($i = 148; $i < 156; $i++)
  144. $checksum += 32;
  145. for ($i = 156; $i < 512; $i++)
  146. $checksum += ord($hdr_data{$i});
  147. $hdr = unpack("a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/a1typeflag/a100link/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor", $hdr_data);
  148. $hdr['checksum'] = octdec(trim($hdr['checksum']));
  149. if ($hdr['checksum'] != $checksum) {
  150. echo "Checksum for $tarball $hdr[filename] is invalid\n";
  151. print_r($hdr);
  152. return;
  153. }
  154. $hdr['size'] = octdec(trim($hdr['size']));
  155. echo "File: $hdr[filename] $hdr[size]\n";
  156. if ($filename == $hdr['filename']) {
  157. echo "Found the file we want\n";
  158. $dest = fopen($destfilename, 'wb');
  159. $x = stream_copy_to_stream($fp, $dest, $hdr['size']);
  160. fclose($dest);
  161. echo "Wrote $x bytes into $destfilename\n";
  162. break;
  163. }
  164. /* skip body of the file */
  165. $size = 512 * ceil((int)$hdr['size'] / 512);
  166. echo "Skipping $size bytes\n";
  167. gzseek($fp, gztell($fp) + $size);
  168. } while (!$done);
  169. } /* }}} */
  170. /* the core dll */
  171. copy("$build_dir/php.exe", "$dist_dir/php.exe");
  172. copy("$build_dir/$phpdll", "$dist_dir/$phpdll");
  173. /* and the .lib goes into dev */
  174. $phplib = str_replace(".dll", ".lib", $phpdll);
  175. copy("$build_dir/$phplib", "$dist_dir/dev/$phplib");
  176. /* debug builds; copy the symbols too */
  177. if ($is_debug) {
  178. $phppdb = str_replace(".dll", ".pdb", $phpdll);
  179. copy("$build_dir/$phppdb", "$dist_dir/dev/$phppdb");
  180. }
  181. /* copy the sapi */
  182. copy_file_list($build_dir, "$dist_dir", $sapi_targets);
  183. /* copy the extensions */
  184. copy_file_list($build_dir, "$dist_dir/ext", $ext_targets);
  185. /* pecl sapi and extensions */
  186. if(sizeof($pecl_targets)) {
  187. copy_file_list($build_dir, $pecl_dir, $pecl_targets);
  188. }
  189. /* populate reading material */
  190. $text_files = array(
  191. "LICENSE" => "license.txt",
  192. "NEWS" => "news.txt",
  193. "README.REDIST.BINS" => "readme-redist-bins.txt",
  194. "php.ini-development" => "php.ini-development",
  195. "php.ini-production" => "php.ini-production",
  196. "win32/install.txt" => "install.txt",
  197. "win32/pws-php5cgi.reg" => "pws-php5cgi.reg",
  198. "win32/pws-php5isapi.reg" => "pws-php5isapi.reg",
  199. );
  200. foreach ($text_files as $src => $dest) {
  201. copy_text_file($src, $dist_dir . '/' . $dest);
  202. }
  203. /* general other files */
  204. $general_files = array(
  205. "php.gif" => "php.gif",
  206. );
  207. foreach ($general_files as $src => $dest) {
  208. copy($src, $dist_dir . '/' . $dest);
  209. }
  210. /* include a snapshot identifier */
  211. $branch = "HEAD"; // TODO - determine this from SVN branche name
  212. $fp = fopen("$dist_dir/snapshot.txt", "w");
  213. $now = date("r");
  214. $version = phpversion();
  215. fwrite($fp, <<<EOT
  216. This snapshot was automatically generated on
  217. $now
  218. Version: $version
  219. Branch: $branch
  220. Build: $build_dir
  221. EOT
  222. );
  223. /* list build-in extensions */
  224. $exts = get_loaded_extensions();
  225. fprintf($fp, "\r\nBuilt-in Extensions\r\n");
  226. fwrite($fp, "===========================\r\n");
  227. foreach ($exts as $ext) {
  228. fprintf($fp, "%s\r\n", $ext);
  229. }
  230. fwrite($fp, "\r\n\r\n");
  231. /* list dependencies */
  232. fprintf($fp, "Dependency information:\r\n");
  233. foreach ($per_module_deps as $modulename => $deps) {
  234. if (in_array($modulename, $pecl_targets))
  235. continue;
  236. fprintf($fp, "Module: %s\r\n", $modulename);
  237. fwrite($fp, "===========================\r\n");
  238. foreach ($deps as $dll) {
  239. fprintf($fp, "\t%s\r\n", basename($dll));
  240. }
  241. fwrite($fp, "\r\n");
  242. }
  243. fclose($fp);
  244. /* Now add those dependencies */
  245. foreach ($extra_dll_deps as $dll) {
  246. if (!file_exists($dll)) {
  247. /* try template dir */
  248. $tdll = $snapshot_template . "/dlls/" . basename($dll);
  249. if (!file_exists($tdll)) {
  250. $tdll = $php_build_dir . '/bin/' . basename($dll);
  251. if (!file_exists($tdll)) {
  252. echo "WARNING: distro depends on $dll, but could not find it on your system\n";
  253. continue;
  254. }
  255. }
  256. $dll = $tdll;
  257. }
  258. copy($dll, "$dist_dir/" . basename($dll));
  259. }
  260. /* TODO:
  261. add sanity check and test if all required DLLs are present, per version
  262. This version works at least for 3.6, 3.8 and 4.0 (5.3-vc6, 5.3-vc9 and HEAD).
  263. Add ADD_DLLS to add extra DLLs like dynamic dependencies for standard
  264. deps. For example, libenchant.dll loads libenchant_myspell.dll or
  265. libenchant_ispell.dll
  266. */
  267. $ICU_DLLS = $php_build_dir . '/bin/icu*.dll';
  268. foreach (glob($ICU_DLLS) as $filename) {
  269. copy($filename, "$dist_dir/" . basename($filename));
  270. }
  271. $ENCHANT_DLLS = array(
  272. 'glib-2.dll',
  273. 'gmodule-2.dll',
  274. 'libenchant_myspell.dll',
  275. 'libenchant_ispell.dll',
  276. );
  277. foreach ($ENCHANT_DLLS as $filename) {
  278. copy($php_build_dir . '/bin/' . $filename, "$dist_dir/" . basename($filename));
  279. }
  280. /* and those for pecl */
  281. foreach ($pecl_dll_deps as $dll) {
  282. if (in_array($dll, $extra_dll_deps)) {
  283. /* already in main distro */
  284. continue;
  285. }
  286. if (!file_exists($dll)) {
  287. /* try template dir */
  288. $tdll = $snapshot_template . "/dlls/" . basename($dll);
  289. if (!file_exists($tdll)) {
  290. echo "WARNING: distro depends on $dll, but could not find it on your system\n";
  291. continue;
  292. }
  293. $dll = $tdll;
  294. }
  295. copy($dll, "$pecl_dir/" . basename($dll));
  296. }
  297. function copy_dir($source, $dest)
  298. {
  299. if (!is_dir($dest)) {
  300. if (!mkdir($dest)) {
  301. return false;
  302. }
  303. }
  304. $d = opendir($source);
  305. while (($f = readdir($d)) !== false) {
  306. if ($f == '.' || $f == '..' || $f == '.svn') {
  307. continue;
  308. }
  309. $fs = $source . '/' . $f;
  310. $fd = $dest . '/' . $f;
  311. if (is_dir($fs)) {
  312. copy_dir($fs, $fd);
  313. } else {
  314. copy($fs, $fd);
  315. }
  316. }
  317. closedir($d);
  318. }
  319. function copy_test_dir($directory, $dest)
  320. {
  321. if(substr($directory,-1) == '/') {
  322. $directory = substr($directory,0,-1);
  323. }
  324. if ($directory == 'tests' || $directory == 'examples') {
  325. if (!is_dir($dest . '/tests')) {
  326. mkdir($dest . '/tests', 0775, true);
  327. }
  328. copy_dir($directory, $dest . '/tests/');
  329. return false;
  330. }
  331. if(!file_exists($directory) || !is_dir($directory)) {
  332. echo "failed... $directory\n";
  333. return FALSE;
  334. }
  335. $directory_list = opendir($directory);
  336. while (FALSE !== ($file = readdir($directory_list))) {
  337. $full_path = $directory . '/' . $file;
  338. if($file != '.' && $file != '..' && $file != '.svn' && is_dir($full_path)) {
  339. if ($file == 'tests' || $file == 'examples') {
  340. if (!is_dir($dest . '/' . $full_path)) {
  341. mkdir($dest . '/' . $full_path , 0775, true);
  342. }
  343. copy_dir($full_path, $dest . '/' . $full_path . '/');
  344. continue;
  345. } else {
  346. copy_test_dir($full_path, $dest);
  347. }
  348. }
  349. }
  350. closedir($directory_list);
  351. }
  352. function make_phar_dot_phar($dist_dir)
  353. {
  354. if (!extension_loaded('phar')) {
  355. return;
  356. }
  357. $path_to_phar = realpath(__DIR__ . '/../../ext/phar');
  358. echo "Generating pharcommand.phar\n";
  359. $phar = new Phar($dist_dir . '/pharcommand.phar', 0, 'pharcommand');
  360. foreach (new DirectoryIterator($path_to_phar . '/phar') as $file) {
  361. if ($file->isDir() || $file == 'phar.php') {
  362. continue;
  363. }
  364. echo 'adding ', $file, "\n";
  365. $phar[(string) $file] = file_get_contents($path_to_phar. '/phar/' . $file);
  366. }
  367. $phar->setSignatureAlgorithm(Phar::SHA1);
  368. $stub = file($path_to_phar . '/phar/phar.php');
  369. unset($stub[0]); // remove hashbang
  370. $phar->setStub(implode('', $stub));
  371. echo "Creating phar.phar.bat\n";
  372. file_put_contents($dist_dir . '/phar.phar.bat', "\"%~dp0php.exe\" \"%~dp0pharcommand.phar\" %*\r\n");
  373. }
  374. if (!is_dir($test_dir)) {
  375. mkdir($test_dir);
  376. }
  377. $dirs = array(
  378. 'ext',
  379. 'Sapi',
  380. 'Zend',
  381. 'tests'
  382. );
  383. foreach ($dirs as $dir) {
  384. copy_test_dir($dir, $test_dir);
  385. }
  386. copy('run-tests.php', $test_dir . '/run-test.php');
  387. /* change this next line to true to use good-old
  388. * hand-assembled go-pear-bundle from the snapshot template */
  389. $use_pear_template = true;
  390. if (!$use_pear_template) {
  391. /* Let's do a PEAR-less pear setup */
  392. mkdir("$dist_dir/PEAR");
  393. mkdir("$dist_dir/PEAR/go-pear-bundle");
  394. /* grab the bootstrap script */
  395. echo "Downloading go-pear\n";
  396. copy("http://pear.php.net/go-pear", "$dist_dir/PEAR/go-pear.php");
  397. /* import the package list -- sets $packages variable */
  398. include "pear/go-pear-list.php";
  399. /* download the packages into the destination */
  400. echo "Fetching packages\n";
  401. foreach ($packages as $name => $version) {
  402. $filename = "$name-$version.tgz";
  403. $destfilename = "$dist_dir/PEAR/go-pear-bundle/$filename";
  404. if (file_exists($destfilename))
  405. continue;
  406. $url = "http://pear.php.net/get/$filename";
  407. echo "Downloading $name from $url\n";
  408. flush();
  409. copy($url, $destfilename);
  410. }
  411. echo "Download complete. Extracting bootstrap files\n";
  412. /* Now, we want PEAR.php, Getopt.php (Console_Getopt) and Tar.php (Archive_Tar)
  413. * broken out of the tarballs */
  414. extract_file_from_tarball('PEAR', 'PEAR.php', "$dist_dir/PEAR/go-pear-bundle");
  415. extract_file_from_tarball('Archive_Tar', 'Archive/Tar.php', "$dist_dir/PEAR/go-pear-bundle");
  416. extract_file_from_tarball('Console_Getopt', 'Console/Getopt.php', "$dist_dir/PEAR/go-pear-bundle");
  417. }
  418. /* add extras from the template dir */
  419. if (file_exists($snapshot_template)) {
  420. $items = glob("$snapshot_template/*");
  421. print_r($items);
  422. foreach ($items as $item) {
  423. $bi = basename($item);
  424. if (is_dir($item)) {
  425. if ($bi == 'dlls' || $bi == 'symbols') {
  426. continue;
  427. } else if ($bi == 'PEAR') {
  428. if ($use_pear_template) {
  429. /* copy to top level */
  430. copy_dir($item, "$dist_dir/$bi");
  431. }
  432. } else {
  433. /* copy that dir into extras */
  434. copy_dir($item, "$dist_dir/extras/$bi");
  435. }
  436. } else {
  437. if ($bi == 'go-pear.bat') {
  438. /* copy to top level */
  439. copy($item, "$dist_dir/$bi");
  440. } else {
  441. /* copy to extras */
  442. copy($item, "$dist_dir/extras/$bi");
  443. }
  444. }
  445. }
  446. /* copy c++ runtime */
  447. $items = glob("$snapshot_template/dlls/*.CRT");
  448. foreach ($items as $item) {
  449. $bi = basename($item);
  450. if (is_dir($item)) {
  451. copy_dir($item, "$dist_dir/$bi");
  452. copy_dir($item, "$dist_dir/ext/$bi");
  453. }
  454. }
  455. } else {
  456. echo "WARNING: you don't have a snapshot template, your dist will not be complete\n";
  457. }
  458. make_phar_dot_phar($dist_dir);
  459. ?>