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