PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/win32/build/mkdist.php

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