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