xref: /PHP-7.4/win32/build/mkdist.php (revision 7ec3aa18)
18d3f8ca1SPeter Kokot<?php
23be2b8d7SWez Furlong/* piece together a windows binary distro */
33be2b8d7SWez Furlong
48121cb50SAnatol Belski$php_version = $argv[1];
58121cb50SAnatol Belski$build_dir = $argv[2];
68121cb50SAnatol Belski$php_build_dir = $argv[3];
78121cb50SAnatol Belski$phpdll = $argv[4];
88121cb50SAnatol Belski$sapi_targets = explode(" ", $argv[5]);
98121cb50SAnatol Belski$ext_targets = explode(" ", $argv[6]);
108121cb50SAnatol Belski$pecl_targets = explode(" ", $argv[7]);
118121cb50SAnatol Belski$snapshot_template = $argv[8];
123be2b8d7SWez Furlong
133be2b8d7SWez Furlong$is_debug = preg_match("/^debug/i", $build_dir);
143be2b8d7SWez Furlong
153be2b8d7SWez Furlongecho "Making dist for $build_dir\n";
163be2b8d7SWez Furlong
178121cb50SAnatol Belski$dist_dir = $build_dir . "/php-" . $php_version;
188121cb50SAnatol Belski$test_dir = $build_dir . "/php-test-pack-" . $php_version;
198121cb50SAnatol Belski$pecl_dir = $build_dir . "/pecl-" . $php_version;
202aa33945SWez Furlong
213be2b8d7SWez Furlong@mkdir($dist_dir);
223be2b8d7SWez Furlong@mkdir("$dist_dir/ext");
233be2b8d7SWez Furlong@mkdir("$dist_dir/dev");
243be2b8d7SWez Furlong@mkdir("$dist_dir/extras");
252aa33945SWez Furlong@mkdir($pecl_dir);
263be2b8d7SWez Furlong
273be2b8d7SWez Furlong/* figure out additional DLL's that are required */
283be2b8d7SWez Furlong$extra_dll_deps = array();
293be2b8d7SWez Furlong$per_module_deps = array();
302aa33945SWez Furlong$pecl_dll_deps = array();
313be2b8d7SWez Furlong
323be2b8d7SWez Furlongfunction get_depends($module)
333be2b8d7SWez Furlong{
3458b17906SNikita Popov    static $no_dist = array(
3558b17906SNikita Popov        /* windows system dlls that should not be bundled */
3658b17906SNikita Popov        'advapi32.dll', 'comdlg32.dll', 'crypt32.dll', 'gdi32.dll', 'kernel32.dll', 'ntdll.dll',
3758b17906SNikita Popov        'odbc32.dll', 'ole32.dll', 'oleaut32.dll', 'rpcrt4.dll',
3858b17906SNikita Popov        'shell32.dll', 'shlwapi.dll', 'user32.dll', 'ws2_32.dll', 'ws2help.dll',
3958b17906SNikita Popov        'comctl32.dll', 'winmm.dll', 'wsock32.dll', 'winspool.drv', 'msasn1.dll',
4058b17906SNikita Popov        'secur32.dll', 'netapi32.dll', 'dnsapi.dll', 'psapi.dll', 'normaliz.dll',
4158b17906SNikita Popov        'iphlpapi.dll', 'bcrypt.dll',
4258b17906SNikita Popov
4358b17906SNikita Popov        /* apache */
4458b17906SNikita Popov        'apachecore.dll',
4558b17906SNikita Popov
4658b17906SNikita Popov        /* apache 2 */
4758b17906SNikita Popov        'libhttpd.dll', 'libapr.dll', 'libaprutil.dll','libapr-1.dll', 'libaprutil-1.dll',
4858b17906SNikita Popov
4958b17906SNikita Popov        /* oracle */
5058b17906SNikita Popov        'oci.dll', 'ociw32.dll',
5158b17906SNikita Popov
5258b17906SNikita Popov        /* sybase */
5358b17906SNikita Popov        'libcs.dll', 'libct.dll',
5458b17906SNikita Popov
5558b17906SNikita Popov        /* firebird */
5658b17906SNikita Popov        'fbclient.dll',
5758b17906SNikita Popov
5858b17906SNikita Popov        /* visual C++; mscvrt.dll is present on everyones system,
5958b17906SNikita Popov         * but the debug version (msvcrtd.dll) and those from visual studio.net
6058b17906SNikita Popov         * (msvcrt7x.dll) are not */
6158b17906SNikita Popov        'msvcrt.dll',
6258b17906SNikita Popov        'msvcr90.dll',
6358b17906SNikita Popov        'wldap32.dll',
6458b17906SNikita Popov        'vcruntime140.dll',
6558b17906SNikita Popov        'msvcp140.dll',
6658b17906SNikita Popov        );
6758b17906SNikita Popov    static $no_dist_re = array(
6858b17906SNikita Popov        "api-ms-win-crt-.+\.dll",
6958b17906SNikita Popov    );
7058b17906SNikita Popov    global $build_dir, $extra_dll_deps, $ext_targets, $sapi_targets, $pecl_targets, $phpdll, $per_module_deps, $pecl_dll_deps;
7158b17906SNikita Popov
7258b17906SNikita Popov    $bd = strtolower(realpath($build_dir));
7358b17906SNikita Popov
7458b17906SNikita Popov    $is_pecl = in_array($module, $pecl_targets);
7558b17906SNikita Popov
7658b17906SNikita Popov    $cmd = "$GLOBALS[build_dir]\\deplister.exe \"$module\" \"$GLOBALS[build_dir]\"";
7758b17906SNikita Popov    $proc = proc_open($cmd,
7858b17906SNikita Popov            array(1 => array("pipe", "w")),
7958b17906SNikita Popov            $pipes);
8058b17906SNikita Popov
8158b17906SNikita Popov    $n = 0;
8258b17906SNikita Popov    while (($line = fgetcsv($pipes[1]))) {
8358b17906SNikita Popov        $n++;
8458b17906SNikita Popov
8558b17906SNikita Popov        $dep = strtolower($line[0]);
8658b17906SNikita Popov        $depbase = basename($dep);
8758b17906SNikita Popov        /* ignore stuff in our build dir, but only if it is
8858b17906SNikita Popov         * one of our targets */
8958b17906SNikita Popov        if (((in_array($depbase, $sapi_targets) ||
9058b17906SNikita Popov                in_array($depbase, $ext_targets) || in_array($depbase, $pecl_targets)) ||
9158b17906SNikita Popov                $depbase == $phpdll) && file_exists($GLOBALS['build_dir'] . "/$depbase")) {
9258b17906SNikita Popov            continue;
9358b17906SNikita Popov        }
9458b17906SNikita Popov        /* ignore some well-known system dlls */
9558b17906SNikita Popov        if (in_array(basename($dep), $no_dist)) {
9658b17906SNikita Popov            continue;
9758b17906SNikita Popov        } else {
9858b17906SNikita Popov            $skip = false;
9958b17906SNikita Popov            foreach ($no_dist_re as $re) {
10058b17906SNikita Popov                if (preg_match(",$re,", basename($dep)) > 0) {
10158b17906SNikita Popov                    $skip = true;
10258b17906SNikita Popov                    break;
10358b17906SNikita Popov                }
10458b17906SNikita Popov            }
10558b17906SNikita Popov            if ($skip) {
10658b17906SNikita Popov                continue;
10758b17906SNikita Popov            }
10858b17906SNikita Popov        }
10958b17906SNikita Popov
11058b17906SNikita Popov        if ($is_pecl) {
11158b17906SNikita Popov            if (!in_array($dep, $pecl_dll_deps)) {
11258b17906SNikita Popov                $pecl_dll_deps[] = $dep;
11358b17906SNikita Popov            }
11458b17906SNikita Popov        } else {
11558b17906SNikita Popov            if (!in_array($dep, $extra_dll_deps)) {
11658b17906SNikita Popov                $extra_dll_deps[] = $dep;
11758b17906SNikita Popov            }
11858b17906SNikita Popov        }
11958b17906SNikita Popov
12058b17906SNikita Popov        if (!isset($per_module_deps[basename($module)]) || !in_array($dep, $per_module_deps[basename($module)])) {
12158b17906SNikita Popov            $per_module_deps[basename($module)][] = $dep;
12258b17906SNikita Popov            //recursively check dll dependencies
12358b17906SNikita Popov            get_depends($dep);
12458b17906SNikita Popov        }
12558b17906SNikita Popov    }
12658b17906SNikita Popov    fclose($pipes[1]);
12758b17906SNikita Popov    proc_close($proc);
1283be2b8d7SWez Furlong//echo "Module $module [$n lines]\n";
1293be2b8d7SWez Furlong}
1303be2b8d7SWez Furlong
1313be2b8d7SWez Furlongfunction copy_file_list($source_dir, $dest_dir, $list)
1323be2b8d7SWez Furlong{
13358b17906SNikita Popov    global $is_debug, $dist_dir;
13458b17906SNikita Popov
13558b17906SNikita Popov    foreach ($list as $item) {
13658b17906SNikita Popov        if (empty($item)) {
13758b17906SNikita Popov            continue;
13858b17906SNikita Popov        } elseif (!is_file($source_dir . DIRECTORY_SEPARATOR . $item)) {
13958b17906SNikita Popov            echo "WARNING: $item not found\n";
14058b17906SNikita Popov            continue;
14158b17906SNikita Popov        }
14258b17906SNikita Popov
14358b17906SNikita Popov        echo "Copying $item from $source_dir to $dest_dir\n";
14458b17906SNikita Popov        copy($source_dir . DIRECTORY_SEPARATOR . $item, $dest_dir . DIRECTORY_SEPARATOR . $item);
14558b17906SNikita Popov        if ($is_debug) {
14658b17906SNikita Popov            $itemdb = preg_replace("/\.(exe|dll|lib)$/i", ".pdb", $item);
14758b17906SNikita Popov            if (file_exists("$source_dir/$itemdb")) {
14858b17906SNikita Popov                copy("$source_dir/$itemdb", "$dist_dir/dev/$itemdb");
14958b17906SNikita Popov            }
15058b17906SNikita Popov        }
15158b17906SNikita Popov        if (preg_match("/\.(exe|dll)$/i", $item)) {
15258b17906SNikita Popov            get_depends($source_dir . '/' . $item);
15358b17906SNikita Popov        }
15458b17906SNikita Popov    }
1553be2b8d7SWez Furlong}
1563be2b8d7SWez Furlong
1573be2b8d7SWez Furlongfunction copy_text_file($source, $dest)
1583be2b8d7SWez Furlong{
15958b17906SNikita Popov    $text = file_get_contents($source);
16058b17906SNikita Popov    $text = preg_replace("/(\r\n?)|\n/", "\r\n", $text);
16158b17906SNikita Popov    $fp = fopen($dest, "w");
16258b17906SNikita Popov    fwrite($fp, $text);
16358b17906SNikita Popov    fclose($fp);
1643be2b8d7SWez Furlong}
1653be2b8d7SWez Furlong
1665e8d0919SWez Furlong/* very light-weight function to extract a single named file from
1675e8d0919SWez Furlong * a gzipped tarball.  This makes assumptions about the files
1685e8d0919SWez Furlong * based on the PEAR info set in $packages. */
1695e8d0919SWez Furlongfunction extract_file_from_tarball($pkg, $filename, $dest_dir) /* {{{ */
1705e8d0919SWez Furlong{
17158b17906SNikita Popov    global $packages;
17258b17906SNikita Popov
17358b17906SNikita Popov    $name = $pkg . '-' . $packages[$pkg];
17458b17906SNikita Popov    $tarball = $dest_dir . "/" . $name . '.tgz';
17558b17906SNikita Popov    $filename = $name . '/' . $filename;
17658b17906SNikita Popov    $destfilename = $dest_dir . "/" . basename($filename);
17758b17906SNikita Popov
17858b17906SNikita Popov    $fp = gzopen($tarball, 'rb');
17958b17906SNikita Popov
18058b17906SNikita Popov    $done = false;
18158b17906SNikita Popov    do {
18258b17906SNikita Popov        /* read the header */
18358b17906SNikita Popov        $hdr_data = gzread($fp, 512);
18458b17906SNikita Popov        if (strlen($hdr_data) == 0)
18558b17906SNikita Popov            break;
18658b17906SNikita Popov        $checksum = 0;
18758b17906SNikita Popov        for ($i = 0; $i < 148; $i++)
188*7ec3aa18SChristoph M. Becker            $checksum += ord($hdr_data[$i]);
18958b17906SNikita Popov        for ($i = 148; $i < 156; $i++)
19058b17906SNikita Popov            $checksum += 32;
19158b17906SNikita Popov        for ($i = 156; $i < 512; $i++)
192*7ec3aa18SChristoph M. Becker            $checksum += ord($hdr_data[$i]);
19358b17906SNikita Popov
19458b17906SNikita Popov        $hdr = unpack("a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/a1typeflag/a100link/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor", $hdr_data);
19558b17906SNikita Popov
19658b17906SNikita Popov        $hdr['checksum'] = octdec(trim($hdr['checksum']));
19758b17906SNikita Popov
19858b17906SNikita Popov        if ($hdr['checksum'] != $checksum) {
19958b17906SNikita Popov            echo "Checksum for $tarball $hdr[filename] is invalid\n";
20058b17906SNikita Popov            print_r($hdr);
20158b17906SNikita Popov            return;
20258b17906SNikita Popov        }
20358b17906SNikita Popov
20458b17906SNikita Popov        $hdr['size'] = octdec(trim($hdr['size']));
20558b17906SNikita Popov        echo "File: $hdr[filename] $hdr[size]\n";
20658b17906SNikita Popov
20758b17906SNikita Popov        if ($filename == $hdr['filename']) {
20858b17906SNikita Popov            echo "Found the file we want\n";
20958b17906SNikita Popov            $dest = fopen($destfilename, 'wb');
21058b17906SNikita Popov            $x = stream_copy_to_stream($fp, $dest, $hdr['size']);
21158b17906SNikita Popov            fclose($dest);
21258b17906SNikita Popov            echo "Wrote $x bytes into $destfilename\n";
21358b17906SNikita Popov            break;
21458b17906SNikita Popov        }
21558b17906SNikita Popov
21658b17906SNikita Popov        /* skip body of the file */
21758b17906SNikita Popov        $size = 512 * ceil((int)$hdr['size'] / 512);
21858b17906SNikita Popov        echo "Skipping $size bytes\n";
21958b17906SNikita Popov        gzseek($fp, gztell($fp) + $size);
22058b17906SNikita Popov
22158b17906SNikita Popov    } while (!$done);
22237c329d7SPeter Kokot
2235e8d0919SWez Furlong} /* }}} */
2245e8d0919SWez Furlong
2255e8d0919SWez Furlong
2263be2b8d7SWez Furlong/* the core dll */
2273be2b8d7SWez Furlongcopy("$build_dir/php.exe", "$dist_dir/php.exe");
2284e9c3e93SDylan K. Taylor/* copy dll and its dependencies */
2294e9c3e93SDylan K. Taylorcopy_file_list($build_dir, "$dist_dir", [$phpdll]);
2303be2b8d7SWez Furlong
2313be2b8d7SWez Furlong/* and the .lib goes into dev */
2323be2b8d7SWez Furlong$phplib = str_replace(".dll", ".lib", $phpdll);
2333be2b8d7SWez Furlongcopy("$build_dir/$phplib", "$dist_dir/dev/$phplib");
2343be2b8d7SWez Furlong/* debug builds; copy the symbols too */
2353be2b8d7SWez Furlongif ($is_debug) {
23658b17906SNikita Popov    $phppdb = str_replace(".dll", ".pdb", $phpdll);
23758b17906SNikita Popov    copy("$build_dir/$phppdb", "$dist_dir/dev/$phppdb");
2383be2b8d7SWez Furlong}
2393be2b8d7SWez Furlong/* copy the sapi */
240f5e4590fSWez Furlongcopy_file_list($build_dir, "$dist_dir", $sapi_targets);
2413be2b8d7SWez Furlong
2423be2b8d7SWez Furlong/* copy the extensions */
2433be2b8d7SWez Furlongcopy_file_list($build_dir, "$dist_dir/ext", $ext_targets);
2443be2b8d7SWez Furlong
2452aa33945SWez Furlong/* pecl sapi and extensions */
24602fe66d1SKalle Sommer Nielsenif(sizeof($pecl_targets)) {
24758b17906SNikita Popov    copy_file_list($build_dir, $pecl_dir, $pecl_targets);
24802fe66d1SKalle Sommer Nielsen}
2492aa33945SWez Furlong
2503be2b8d7SWez Furlong/* populate reading material */
2513be2b8d7SWez Furlong$text_files = array(
25258b17906SNikita Popov    "LICENSE" => "license.txt",
25358b17906SNikita Popov    "NEWS" => "news.txt",
25458b17906SNikita Popov    "README.md" => "README.md",
25558b17906SNikita Popov    "README.REDIST.BINS" => "readme-redist-bins.txt",
25658b17906SNikita Popov    "php.ini-development" => "php.ini-development",
25758b17906SNikita Popov    "php.ini-production" => "php.ini-production"
2583be2b8d7SWez Furlong);
2593be2b8d7SWez Furlong
2603be2b8d7SWez Furlongforeach ($text_files as $src => $dest) {
26158b17906SNikita Popov    copy_text_file($src, $dist_dir . '/' . $dest);
2623be2b8d7SWez Furlong}
2633be2b8d7SWez Furlong
2643be2b8d7SWez Furlong/* general other files */
2653be2b8d7SWez Furlong$general_files = array(
26658b17906SNikita Popov    "$GLOBALS[build_dir]\\deplister.exe"	=>	"deplister.exe",
2673be2b8d7SWez Furlong);
2683be2b8d7SWez Furlong
2693be2b8d7SWez Furlongforeach ($general_files as $src => $dest) {
27058b17906SNikita Popov    copy($src, $dist_dir . '/' . $dest);
2713be2b8d7SWez Furlong}
2723be2b8d7SWez Furlong
2733be2b8d7SWez Furlong/* include a snapshot identifier */
274dffe3d8dSPierre Joye$branch = "HEAD"; // TODO - determine this from SVN branche name
2753be2b8d7SWez Furlong$fp = fopen("$dist_dir/snapshot.txt", "w");
2763be2b8d7SWez Furlong$now = date("r");
2773be2b8d7SWez Furlongfwrite($fp, <<<EOT
2783be2b8d7SWez FurlongThis snapshot was automatically generated on
2793be2b8d7SWez Furlong$now
2803be2b8d7SWez Furlong
2818121cb50SAnatol BelskiVersion: $php_version
2823be2b8d7SWez FurlongBranch: $branch
2833be2b8d7SWez FurlongBuild: $build_dir
2843be2b8d7SWez Furlong
2853be2b8d7SWez FurlongEOT
2863be2b8d7SWez Furlong);
2873be2b8d7SWez Furlong/* list build-in extensions */
2883be2b8d7SWez Furlong$exts = get_loaded_extensions();
289a3075773SWez Furlongfprintf($fp, "\r\nBuilt-in Extensions\r\n");
290a3075773SWez Furlongfwrite($fp, "===========================\r\n");
2913be2b8d7SWez Furlongforeach ($exts as $ext) {
29258b17906SNikita Popov    fprintf($fp, "%s\r\n", $ext);
2933be2b8d7SWez Furlong}
2943be2b8d7SWez Furlongfwrite($fp, "\r\n\r\n");
2953be2b8d7SWez Furlong
2963be2b8d7SWez Furlong/* list dependencies */
2973be2b8d7SWez Furlongfprintf($fp, "Dependency information:\r\n");
2983be2b8d7SWez Furlongforeach ($per_module_deps as $modulename => $deps) {
29958b17906SNikita Popov    if (in_array($modulename, $pecl_targets))
30058b17906SNikita Popov        continue;
30158b17906SNikita Popov
30258b17906SNikita Popov    fprintf($fp, "Module: %s\r\n", $modulename);
30358b17906SNikita Popov    fwrite($fp, "===========================\r\n");
30458b17906SNikita Popov    foreach ($deps as $dll) {
30558b17906SNikita Popov        fprintf($fp, "\t%s\r\n", basename($dll));
30658b17906SNikita Popov    }
30758b17906SNikita Popov    fwrite($fp, "\r\n");
3083be2b8d7SWez Furlong}
3093be2b8d7SWez Furlongfclose($fp);
3103be2b8d7SWez Furlong
3113be2b8d7SWez Furlong/* Now add those dependencies */
3123be2b8d7SWez Furlongforeach ($extra_dll_deps as $dll) {
31358b17906SNikita Popov    if (!file_exists($dll)) {
31458b17906SNikita Popov        /* try template dir */
31558b17906SNikita Popov        $tdll = $snapshot_template . "/dlls/" . basename($dll);
31658b17906SNikita Popov        if (!file_exists($tdll)) {
31758b17906SNikita Popov            $tdll = $php_build_dir . '/bin/' . basename($dll);
31858b17906SNikita Popov            if (!file_exists($tdll)) {
31958b17906SNikita Popov                echo "WARNING: distro depends on $dll, but could not find it on your system\n";
32058b17906SNikita Popov                continue;
32158b17906SNikita Popov            }
32258b17906SNikita Popov        }
32358b17906SNikita Popov        $dll = $tdll;
32458b17906SNikita Popov    }
32558b17906SNikita Popov    copy($dll, "$dist_dir/" . basename($dll));
3263be2b8d7SWez Furlong}
3276b2be7c7SPierre Joye
32841a3bf43SPierre Joye/* TODO:
32937c329d7SPeter Kokotadd sanity check and test if all required DLLs are present, per version
33041a3bf43SPierre JoyeThis version works at least for 3.6, 3.8 and 4.0 (5.3-vc6, 5.3-vc9 and HEAD).
33137c329d7SPeter KokotAdd ADD_DLLS to add extra DLLs like dynamic dependencies for standard
33297595a14SPierre Joyedeps. For example, libenchant.dll loads libenchant_myspell.dll or
33397595a14SPierre Joyelibenchant_ispell.dll
33441a3bf43SPierre Joye*/
33597595a14SPierre Joye$ENCHANT_DLLS = array(
33658b17906SNikita Popov    array('', 'glib-2.dll'),
33758b17906SNikita Popov    array('', 'gmodule-2.dll'),
33858b17906SNikita Popov    array('lib/enchant', 'libenchant_myspell.dll'),
33958b17906SNikita Popov    array('lib/enchant', 'libenchant_ispell.dll'),
34097595a14SPierre Joye);
3410f18a15eSAnatol Belskiforeach ($ENCHANT_DLLS as $dll) {
34258b17906SNikita Popov    $dest  = "$dist_dir/$dll[0]";
34358b17906SNikita Popov    $filename = $dll[1];
34458b17906SNikita Popov
34558b17906SNikita Popov    if (!file_exists("$dest") || !is_dir("$dest")) {
34658b17906SNikita Popov        if (!mkdir("$dest", 0777, true)) {
34758b17906SNikita Popov            echo "WARNING: couldn't create '$dest' for enchant plugins ";
34858b17906SNikita Popov        }
34958b17906SNikita Popov    }
35058b17906SNikita Popov
35158b17906SNikita Popov    if (!copy($php_build_dir . '/bin/' . $filename, "$dest/" . basename($filename))) {
35258b17906SNikita Popov            echo "WARNING: couldn't copy $filename into the dist dir";
35358b17906SNikita Popov    }
35497595a14SPierre Joye}
3556b2be7c7SPierre Joye
356404360f2SAnatol Belski$SASL_DLLS = $php_build_dir . "/bin/sasl2/sasl*.dll";
357404360f2SAnatol Belski$fls = glob($SASL_DLLS);
358404360f2SAnatol Belskiif (!empty($fls)) {
35958b17906SNikita Popov    $sasl_dest_dir = "$dist_dir/sasl2";
36058b17906SNikita Popov    if (!file_exists($sasl_dest_dir) || !is_dir($sasl_dest_dir)) {
36158b17906SNikita Popov        if (!mkdir("$sasl_dest_dir", 0777, true)) {
36258b17906SNikita Popov            echo "WARNING: couldn't create '$sasl_dest_dir' for SASL2 auth plugins ";
36358b17906SNikita Popov        }
36458b17906SNikita Popov    }
36558b17906SNikita Popov    foreach ($fls as $fl) {
36658b17906SNikita Popov        if (!copy($fl, "$sasl_dest_dir/" . basename($fl))) {
36758b17906SNikita Popov            echo "WARNING: couldn't copy $fl into the $sasl_dest_dir";
36858b17906SNikita Popov        }
36958b17906SNikita Popov    }
370404360f2SAnatol Belski}
371404360f2SAnatol Belski
3722aa33945SWez Furlong/* and those for pecl */
3732aa33945SWez Furlongforeach ($pecl_dll_deps as $dll) {
37458b17906SNikita Popov    if (in_array($dll, $extra_dll_deps)) {
37558b17906SNikita Popov        /* already in main distro */
37658b17906SNikita Popov        continue;
37758b17906SNikita Popov    }
37858b17906SNikita Popov    if (!file_exists($dll)) {
37958b17906SNikita Popov        /* try template dir */
38058b17906SNikita Popov        $tdll = $snapshot_template . "/dlls/" . basename($dll);
38158b17906SNikita Popov        if (!file_exists($tdll)) {
38258b17906SNikita Popov            echo "WARNING: distro depends on $dll, but could not find it on your system\n";
38358b17906SNikita Popov            continue;
38458b17906SNikita Popov        }
38558b17906SNikita Popov        $dll = $tdll;
38658b17906SNikita Popov    }
38758b17906SNikita Popov    copy($dll, "$pecl_dir/" . basename($dll));
3882aa33945SWez Furlong}
38997595a14SPierre Joye
3903be2b8d7SWez Furlongfunction copy_dir($source, $dest)
3913be2b8d7SWez Furlong{
39258b17906SNikita Popov    if (!is_dir($dest)) {
39358b17906SNikita Popov        if (!mkdir($dest)) {
39458b17906SNikita Popov            return false;
39558b17906SNikita Popov        }
39658b17906SNikita Popov    }
39758b17906SNikita Popov
39858b17906SNikita Popov    $d = opendir($source);
39958b17906SNikita Popov    while (($f = readdir($d)) !== false) {
40058b17906SNikita Popov        if ($f == '.' || $f == '..' || $f == '.svn') {
40158b17906SNikita Popov            continue;
40258b17906SNikita Popov        }
40358b17906SNikita Popov        $fs = $source . '/' . $f;
40458b17906SNikita Popov        $fd = $dest . '/' . $f;
40558b17906SNikita Popov        if (is_dir($fs)) {
40658b17906SNikita Popov            copy_dir($fs, $fd);
40758b17906SNikita Popov        } else {
40858b17906SNikita Popov            copy($fs, $fd);
40958b17906SNikita Popov        }
41058b17906SNikita Popov    }
41158b17906SNikita Popov    closedir($d);
4123be2b8d7SWez Furlong}
4133be2b8d7SWez Furlong
4147376ddb6SPierre Joye
4157376ddb6SPierre Joye
4167376ddb6SPierre Joyefunction copy_test_dir($directory, $dest)
4177376ddb6SPierre Joye{
41858b17906SNikita Popov    if(substr($directory,-1) == '/') {
41958b17906SNikita Popov        $directory = substr($directory,0,-1);
42058b17906SNikita Popov    }
42158b17906SNikita Popov
42258b17906SNikita Popov    if ($directory == 'tests' || $directory == 'examples') {
42358b17906SNikita Popov        if (!is_dir($dest . '/tests')) {
42458b17906SNikita Popov            mkdir($dest . '/tests', 0775, true);
42558b17906SNikita Popov        }
42658b17906SNikita Popov        copy_dir($directory, $dest . '/tests/');
42758b17906SNikita Popov
42858b17906SNikita Popov        return false;
42958b17906SNikita Popov    }
43058b17906SNikita Popov
43158b17906SNikita Popov    if(!file_exists($directory) || !is_dir($directory)) {
43258b17906SNikita Popov        echo "failed... $directory\n";
43358b17906SNikita Popov        return FALSE;
43458b17906SNikita Popov    }
43558b17906SNikita Popov
43658b17906SNikita Popov    $directory_list = opendir($directory);
43758b17906SNikita Popov
43858b17906SNikita Popov    while (FALSE !== ($file = readdir($directory_list))) {
43958b17906SNikita Popov        $full_path = $directory . '/' . $file;
44058b17906SNikita Popov        if($file != '.' && $file != '..' && $file != '.svn' && is_dir($full_path)) {
44158b17906SNikita Popov            if ($file == 'tests' || $file == 'examples') {
44258b17906SNikita Popov                if (!is_dir($dest . '/' . $full_path)) {
44358b17906SNikita Popov                    mkdir($dest . '/' . $full_path , 0775, true);
44458b17906SNikita Popov                }
44558b17906SNikita Popov                copy_dir($full_path, $dest . '/' . $full_path . '/');
44658b17906SNikita Popov                continue;
44758b17906SNikita Popov            } else {
44858b17906SNikita Popov                copy_test_dir($full_path, $dest);
44958b17906SNikita Popov            }
45058b17906SNikita Popov        }
45158b17906SNikita Popov    }
45258b17906SNikita Popov
45358b17906SNikita Popov    closedir($directory_list);
4547376ddb6SPierre Joye}
4557376ddb6SPierre Joye
456f7246e20SGreg Beaverfunction make_phar_dot_phar($dist_dir)
457f7246e20SGreg Beaver{
45858b17906SNikita Popov    if (!extension_loaded('phar')) {
45958b17906SNikita Popov        return;
46058b17906SNikita Popov    }
4613410350cSKalle Sommer Nielsen
46258b17906SNikita Popov    $path_to_phar = realpath(__DIR__ . '/../../ext/phar');
4633410350cSKalle Sommer Nielsen
46458b17906SNikita Popov    echo "Generating pharcommand.phar\n";
46558b17906SNikita Popov    $phar = new Phar($dist_dir . '/pharcommand.phar', 0, 'pharcommand');
4663410350cSKalle Sommer Nielsen
46758b17906SNikita Popov    foreach (new DirectoryIterator($path_to_phar . '/phar') as $file) {
46858b17906SNikita Popov        if ($file->isDir() || $file == 'phar.php') {
46958b17906SNikita Popov            continue;
47058b17906SNikita Popov        }
4713410350cSKalle Sommer Nielsen
47258b17906SNikita Popov        echo 'adding ', $file, "\n";
47358b17906SNikita Popov        $phar[(string) $file] = file_get_contents($path_to_phar.  '/phar/' . $file);
47458b17906SNikita Popov    }
4753410350cSKalle Sommer Nielsen
47658b17906SNikita Popov    $phar->setSignatureAlgorithm(Phar::SHA1);
47758b17906SNikita Popov    $stub = file($path_to_phar . '/phar/phar.php');
4783410350cSKalle Sommer Nielsen
47958b17906SNikita Popov    unset($stub[0]); // remove hashbang
48058b17906SNikita Popov    $phar->setStub(implode('', $stub));
4813410350cSKalle Sommer Nielsen
48258b17906SNikita Popov    echo "Creating phar.phar.bat\n";
48358b17906SNikita Popov    file_put_contents($dist_dir . '/phar.phar.bat', "\"%~dp0php.exe\" \"%~dp0pharcommand.phar\" %*\r\n");
484f7246e20SGreg Beaver}
485f7246e20SGreg Beaver
4867376ddb6SPierre Joyeif (!is_dir($test_dir)) {
48758b17906SNikita Popov    mkdir($test_dir);
4887376ddb6SPierre Joye}
4897376ddb6SPierre Joye
4907376ddb6SPierre Joye$dirs = array(
49158b17906SNikita Popov    'ext',
49258b17906SNikita Popov    'Sapi',
49358b17906SNikita Popov    'Zend',
49458b17906SNikita Popov    'tests'
4957376ddb6SPierre Joye);
4967376ddb6SPierre Joyeforeach ($dirs as $dir) {
49758b17906SNikita Popov    copy_test_dir($dir, $test_dir);
4987376ddb6SPierre Joye}
4994e03e705SPierre Joyecopy('run-tests.php', $test_dir . '/run-test.php');
50097595a14SPierre Joye
5015e8d0919SWez Furlong/* change this next line to true to use good-old
5025e8d0919SWez Furlong * hand-assembled go-pear-bundle from the snapshot template */
50335f051b5SEdin Kadribasic$use_pear_template = true;
5045e8d0919SWez Furlong
5055e8d0919SWez Furlongif (!$use_pear_template) {
50658b17906SNikita Popov    /* Let's do a PEAR-less pear setup */
50758b17906SNikita Popov    mkdir("$dist_dir/PEAR");
50858b17906SNikita Popov    mkdir("$dist_dir/PEAR/go-pear-bundle");
50958b17906SNikita Popov
51058b17906SNikita Popov    /* grab the bootstrap script */
51158b17906SNikita Popov    echo "Downloading go-pear\n";
51258b17906SNikita Popov    copy("https://pear.php.net/go-pear.phar", "$dist_dir/PEAR/go-pear.php");
51358b17906SNikita Popov
51458b17906SNikita Popov    /* import the package list -- sets $packages variable */
51558b17906SNikita Popov    include "pear/go-pear-list.php";
51658b17906SNikita Popov
51758b17906SNikita Popov    /* download the packages into the destination */
51858b17906SNikita Popov    echo "Fetching packages\n";
51958b17906SNikita Popov
52058b17906SNikita Popov    foreach ($packages as $name => $version) {
52158b17906SNikita Popov        $filename = "$name-$version.tgz";
52258b17906SNikita Popov        $destfilename = "$dist_dir/PEAR/go-pear-bundle/$filename";
52358b17906SNikita Popov        if (file_exists($destfilename))
52458b17906SNikita Popov            continue;
52558b17906SNikita Popov        $url = "http://pear.php.net/get/$filename";
52658b17906SNikita Popov        echo "Downloading $name from $url\n";
52758b17906SNikita Popov        flush();
52858b17906SNikita Popov        copy($url, $destfilename);
52958b17906SNikita Popov    }
53058b17906SNikita Popov
53158b17906SNikita Popov    echo "Download complete.  Extracting bootstrap files\n";
53258b17906SNikita Popov
53358b17906SNikita Popov    /* Now, we want PEAR.php, Getopt.php (Console_Getopt) and Tar.php (Archive_Tar)
53458b17906SNikita Popov     * broken out of the tarballs */
53558b17906SNikita Popov    extract_file_from_tarball('PEAR', 'PEAR.php', "$dist_dir/PEAR/go-pear-bundle");
53658b17906SNikita Popov    extract_file_from_tarball('Archive_Tar', 'Archive/Tar.php', "$dist_dir/PEAR/go-pear-bundle");
53758b17906SNikita Popov    extract_file_from_tarball('Console_Getopt', 'Console/Getopt.php', "$dist_dir/PEAR/go-pear-bundle");
5385e8d0919SWez Furlong}
53937c329d7SPeter Kokot
5403be2b8d7SWez Furlong/* add extras from the template dir */
5413be2b8d7SWez Furlongif (file_exists($snapshot_template)) {
54258b17906SNikita Popov    $items = glob("$snapshot_template/*");
54358b17906SNikita Popov    print_r($items);
54458b17906SNikita Popov
54558b17906SNikita Popov    foreach ($items as $item) {
54658b17906SNikita Popov        $bi = basename($item);
54758b17906SNikita Popov        if (is_dir($item)) {
54858b17906SNikita Popov            if ($bi == 'dlls' || $bi == 'symbols') {
54958b17906SNikita Popov                continue;
55058b17906SNikita Popov            } else if ($bi == 'PEAR') {
55158b17906SNikita Popov                if ($use_pear_template) {
55258b17906SNikita Popov                    /* copy to top level */
55358b17906SNikita Popov                    copy_dir($item, "$dist_dir/$bi");
55458b17906SNikita Popov                }
55558b17906SNikita Popov            } else {
55658b17906SNikita Popov                /* copy that dir into extras */
55758b17906SNikita Popov                copy_dir($item, "$dist_dir/extras/$bi");
55858b17906SNikita Popov            }
55958b17906SNikita Popov        } else {
56058b17906SNikita Popov            if ($bi == 'go-pear.bat') {
56158b17906SNikita Popov                /* copy to top level */
56258b17906SNikita Popov                copy($item, "$dist_dir/$bi");
56358b17906SNikita Popov            } else {
56458b17906SNikita Popov                /* copy to extras */
56558b17906SNikita Popov                copy($item, "$dist_dir/extras/$bi");
56658b17906SNikita Popov            }
56758b17906SNikita Popov        }
56858b17906SNikita Popov    }
56958b17906SNikita Popov
57058b17906SNikita Popov    /* copy c++ runtime */
57158b17906SNikita Popov    $items = glob("$snapshot_template/dlls/*.CRT");
57258b17906SNikita Popov
57358b17906SNikita Popov    foreach ($items as $item) {
57458b17906SNikita Popov        $bi = basename($item);
57558b17906SNikita Popov        if (is_dir($item)) {
57658b17906SNikita Popov            copy_dir($item, "$dist_dir/$bi");
57758b17906SNikita Popov            copy_dir($item, "$dist_dir/ext/$bi");
57858b17906SNikita Popov        }
57958b17906SNikita Popov    }
5803be2b8d7SWez Furlong} else {
58158b17906SNikita Popov    echo "WARNING: you don't have a snapshot template, your dist will not be complete\n";
5823be2b8d7SWez Furlong}
5833be2b8d7SWez Furlong
584f7246e20SGreg Beavermake_phar_dot_phar($dist_dir);
5853be2b8d7SWez Furlong?>