PageRenderTime 104ms CodeModel.GetById 57ms RepoModel.GetById 16ms app.codeStats 0ms

/win32/build/mkdist.php

https://github.com/andywuzh/php-src
PHP | 547 lines | 379 code | 86 blank | 82 comment | 82 complexity | 4f0493fa64e5b5fb072e4f9608e8f8e6 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. );
  198. foreach ($text_files as $src => $dest) {
  199. copy_text_file($src, $dist_dir . '/' . $dest);
  200. }
  201. /* general other files */
  202. $general_files = array(
  203. "php.gif" => "php.gif",
  204. "$GLOBALS[build_dir]\\deplister.exe" => "deplister.exe",
  205. );
  206. foreach ($general_files as $src => $dest) {
  207. copy($src, $dist_dir . '/' . $dest);
  208. }
  209. /* include a snapshot identifier */
  210. $branch = "HEAD"; // TODO - determine this from SVN branche name
  211. $fp = fopen("$dist_dir/snapshot.txt", "w");
  212. $now = date("r");
  213. $version = phpversion();
  214. fwrite($fp, <<<EOT
  215. This snapshot was automatically generated on
  216. $now
  217. Version: $version
  218. Branch: $branch
  219. Build: $build_dir
  220. EOT
  221. );
  222. /* list build-in extensions */
  223. $exts = get_loaded_extensions();
  224. fprintf($fp, "\r\nBuilt-in Extensions\r\n");
  225. fwrite($fp, "===========================\r\n");
  226. foreach ($exts as $ext) {
  227. fprintf($fp, "%s\r\n", $ext);
  228. }
  229. fwrite($fp, "\r\n\r\n");
  230. /* list dependencies */
  231. fprintf($fp, "Dependency information:\r\n");
  232. foreach ($per_module_deps as $modulename => $deps) {
  233. if (in_array($modulename, $pecl_targets))
  234. continue;
  235. fprintf($fp, "Module: %s\r\n", $modulename);
  236. fwrite($fp, "===========================\r\n");
  237. foreach ($deps as $dll) {
  238. fprintf($fp, "\t%s\r\n", basename($dll));
  239. }
  240. fwrite($fp, "\r\n");
  241. }
  242. fclose($fp);
  243. /* Now add those dependencies */
  244. foreach ($extra_dll_deps as $dll) {
  245. if (!file_exists($dll)) {
  246. /* try template dir */
  247. $tdll = $snapshot_template . "/dlls/" . basename($dll);
  248. if (!file_exists($tdll)) {
  249. $tdll = $php_build_dir . '/bin/' . basename($dll);
  250. if (!file_exists($tdll)) {
  251. echo "WARNING: distro depends on $dll, but could not find it on your system\n";
  252. continue;
  253. }
  254. }
  255. $dll = $tdll;
  256. }
  257. copy($dll, "$dist_dir/" . basename($dll));
  258. }
  259. /* TODO:
  260. add sanity check and test if all required DLLs are present, per version
  261. This version works at least for 3.6, 3.8 and 4.0 (5.3-vc6, 5.3-vc9 and HEAD).
  262. Add ADD_DLLS to add extra DLLs like dynamic dependencies for standard
  263. deps. For example, libenchant.dll loads libenchant_myspell.dll or
  264. libenchant_ispell.dll
  265. */
  266. $ICU_DLLS = $php_build_dir . '/bin/icu*.dll';
  267. foreach (glob($ICU_DLLS) as $filename) {
  268. copy($filename, "$dist_dir/" . basename($filename));
  269. }
  270. $ENCHANT_DLLS = array(
  271. 'glib-2.dll',
  272. 'gmodule-2.dll',
  273. 'libenchant_myspell.dll',
  274. 'libenchant_ispell.dll',
  275. );
  276. foreach ($ENCHANT_DLLS as $filename) {
  277. copy($php_build_dir . '/bin/' . $filename, "$dist_dir/" . basename($filename));
  278. }
  279. /* and those for pecl */
  280. foreach ($pecl_dll_deps as $dll) {
  281. if (in_array($dll, $extra_dll_deps)) {
  282. /* already in main distro */
  283. continue;
  284. }
  285. if (!file_exists($dll)) {
  286. /* try template dir */
  287. $tdll = $snapshot_template . "/dlls/" . basename($dll);
  288. if (!file_exists($tdll)) {
  289. echo "WARNING: distro depends on $dll, but could not find it on your system\n";
  290. continue;
  291. }
  292. $dll = $tdll;
  293. }
  294. copy($dll, "$pecl_dir/" . basename($dll));
  295. }
  296. function copy_dir($source, $dest)
  297. {
  298. if (!is_dir($dest)) {
  299. if (!mkdir($dest)) {
  300. return false;
  301. }
  302. }
  303. $d = opendir($source);
  304. while (($f = readdir($d)) !== false) {
  305. if ($f == '.' || $f == '..' || $f == '.svn') {
  306. continue;
  307. }
  308. $fs = $source . '/' . $f;
  309. $fd = $dest . '/' . $f;
  310. if (is_dir($fs)) {
  311. copy_dir($fs, $fd);
  312. } else {
  313. copy($fs, $fd);
  314. }
  315. }
  316. closedir($d);
  317. }
  318. function copy_test_dir($directory, $dest)
  319. {
  320. if(substr($directory,-1) == '/') {
  321. $directory = substr($directory,0,-1);
  322. }
  323. if ($directory == 'tests' || $directory == 'examples') {
  324. if (!is_dir($dest . '/tests')) {
  325. mkdir($dest . '/tests', 0775, true);
  326. }
  327. copy_dir($directory, $dest . '/tests/');
  328. return false;
  329. }
  330. if(!file_exists($directory) || !is_dir($directory)) {
  331. echo "failed... $directory\n";
  332. return FALSE;
  333. }
  334. $directory_list = opendir($directory);
  335. while (FALSE !== ($file = readdir($directory_list))) {
  336. $full_path = $directory . '/' . $file;
  337. if($file != '.' && $file != '..' && $file != '.svn' && is_dir($full_path)) {
  338. if ($file == 'tests' || $file == 'examples') {
  339. if (!is_dir($dest . '/' . $full_path)) {
  340. mkdir($dest . '/' . $full_path , 0775, true);
  341. }
  342. copy_dir($full_path, $dest . '/' . $full_path . '/');
  343. continue;
  344. } else {
  345. copy_test_dir($full_path, $dest);
  346. }
  347. }
  348. }
  349. closedir($directory_list);
  350. }
  351. function make_phar_dot_phar($dist_dir)
  352. {
  353. if (!extension_loaded('phar')) {
  354. return;
  355. }
  356. $path_to_phar = realpath(__DIR__ . '/../../ext/phar');
  357. echo "Generating pharcommand.phar\n";
  358. $phar = new Phar($dist_dir . '/pharcommand.phar', 0, 'pharcommand');
  359. foreach (new DirectoryIterator($path_to_phar . '/phar') as $file) {
  360. if ($file->isDir() || $file == 'phar.php') {
  361. continue;
  362. }
  363. echo 'adding ', $file, "\n";
  364. $phar[(string) $file] = file_get_contents($path_to_phar. '/phar/' . $file);
  365. }
  366. $phar->setSignatureAlgorithm(Phar::SHA1);
  367. $stub = file($path_to_phar . '/phar/phar.php');
  368. unset($stub[0]); // remove hashbang
  369. $phar->setStub(implode('', $stub));
  370. echo "Creating phar.phar.bat\n";
  371. file_put_contents($dist_dir . '/phar.phar.bat', "\"%~dp0php.exe\" \"%~dp0pharcommand.phar\" %*\r\n");
  372. }
  373. if (!is_dir($test_dir)) {
  374. mkdir($test_dir);
  375. }
  376. $dirs = array(
  377. 'ext',
  378. 'Sapi',
  379. 'Zend',
  380. 'tests'
  381. );
  382. foreach ($dirs as $dir) {
  383. copy_test_dir($dir, $test_dir);
  384. }
  385. copy('run-tests.php', $test_dir . '/run-test.php');
  386. /* change this next line to true to use good-old
  387. * hand-assembled go-pear-bundle from the snapshot template */
  388. $use_pear_template = true;
  389. if (!$use_pear_template) {
  390. /* Let's do a PEAR-less pear setup */
  391. mkdir("$dist_dir/PEAR");
  392. mkdir("$dist_dir/PEAR/go-pear-bundle");
  393. /* grab the bootstrap script */
  394. echo "Downloading go-pear\n";
  395. copy("http://pear.php.net/go-pear", "$dist_dir/PEAR/go-pear.php");
  396. /* import the package list -- sets $packages variable */
  397. include "pear/go-pear-list.php";
  398. /* download the packages into the destination */
  399. echo "Fetching packages\n";
  400. foreach ($packages as $name => $version) {
  401. $filename = "$name-$version.tgz";
  402. $destfilename = "$dist_dir/PEAR/go-pear-bundle/$filename";
  403. if (file_exists($destfilename))
  404. continue;
  405. $url = "http://pear.php.net/get/$filename";
  406. echo "Downloading $name from $url\n";
  407. flush();
  408. copy($url, $destfilename);
  409. }
  410. echo "Download complete. Extracting bootstrap files\n";
  411. /* Now, we want PEAR.php, Getopt.php (Console_Getopt) and Tar.php (Archive_Tar)
  412. * broken out of the tarballs */
  413. extract_file_from_tarball('PEAR', 'PEAR.php', "$dist_dir/PEAR/go-pear-bundle");
  414. extract_file_from_tarball('Archive_Tar', 'Archive/Tar.php', "$dist_dir/PEAR/go-pear-bundle");
  415. extract_file_from_tarball('Console_Getopt', 'Console/Getopt.php', "$dist_dir/PEAR/go-pear-bundle");
  416. }
  417. /* add extras from the template dir */
  418. if (file_exists($snapshot_template)) {
  419. $items = glob("$snapshot_template/*");
  420. print_r($items);
  421. foreach ($items as $item) {
  422. $bi = basename($item);
  423. if (is_dir($item)) {
  424. if ($bi == 'dlls' || $bi == 'symbols') {
  425. continue;
  426. } else if ($bi == 'PEAR') {
  427. if ($use_pear_template) {
  428. /* copy to top level */
  429. copy_dir($item, "$dist_dir/$bi");
  430. }
  431. } else {
  432. /* copy that dir into extras */
  433. copy_dir($item, "$dist_dir/extras/$bi");
  434. }
  435. } else {
  436. if ($bi == 'go-pear.bat') {
  437. /* copy to top level */
  438. copy($item, "$dist_dir/$bi");
  439. } else {
  440. /* copy to extras */
  441. copy($item, "$dist_dir/extras/$bi");
  442. }
  443. }
  444. }
  445. /* copy c++ runtime */
  446. $items = glob("$snapshot_template/dlls/*.CRT");
  447. foreach ($items as $item) {
  448. $bi = basename($item);
  449. if (is_dir($item)) {
  450. copy_dir($item, "$dist_dir/$bi");
  451. copy_dir($item, "$dist_dir/ext/$bi");
  452. }
  453. }
  454. } else {
  455. echo "WARNING: you don't have a snapshot template, your dist will not be complete\n";
  456. }
  457. make_phar_dot_phar($dist_dir);
  458. ?>