# "make depend" script for PHP

- Thread: https://externals.io/message/30135
- Original archive: https://news-web.php.net/php.internals/30135
- Started by Wietse Venema on 2007-06-10T18:29:28+00:00
- Messages: 1
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #30135 — Wietse Venema — 2007-06-10T18:29:28+00:00

URL: https://externals.io/message/30135#30135

This 30-line script was originally written in Perl, but it didn't
seem proper etiquette to post non-PHP code on this list :-) 

So I morphed it into PHP, admittedly with a Perl accent.

If I overlooked an already existing way to minimize PHP build time,
without the risk of using out-of-date .lo files, then you can ignore
this post.

	Wietse

<?php

# Usage: php depend.php
#
# The PHP 5.2.3 top-level Makefile provides very few include file
# dependencies. This results in an instantaneous "Build complete"
# message after changing a critical file such as Zend/zend.h.
#
# This script reads the PHP top-level Makefile, and emits on stdout
# a copy of that same Makefile, with source/include file dependencies
# appended at the end. Typically you'd redirect output to an alternate
# Makefile, and then build PHP by running "make -f altmakefile ..."
#
# On my 2GHz laptop the script runs for about 13 seconds; that is
# less than a tenth of the time needed for "make clean; make all",
# so it pays off relatively quickly.
#
# How this script works: it scans the PHP top-level Makefile for
# libtool commands (there's one for each .lo file). It then transforms
# those libtool commands into "$(CC) -M" commands that will produce
# the desired source/include file dependencies, and pipes those
# commands into a "make -f -" command. This script uses "make" because
# the "$(CC) -M" commands contain macros that are defined in the PHP
# top-level Makefile.
#
# The rest is a matter of fixing up the output. "$(CC) -M" assumes
# that we want source/include file dependencies for .o files in the
# current directory, but we need rules for .lo files in subdirectories
# instead.
#
# The script also massages the "$(CC) -M" output into portable form,
# by removing the system-dependent /usr/include dependencies and the
# site-dependent location of the build directory; this part is not
# strictly necessary and can easily be removed.
#
# Paranoia: we try to make sure that we miss no LIBTOOL command,
# otherwise we would miss dependencies for the corresponding .lo
# file,  and the build would use out-of-date .lo files.
#

# First copy the top-level Makefile to stdout, followed by a separator.
#
system("cat Makefile");
print "\n# BEGIN ADDITIONAL SOURCE/INCLUDE FILE DEPENDENCIES\n\n";

# Pipe Makefile-on-stdin rules into a "make" command.
#
$cwd = `getcwd()`;
$make_portable = 
    "| sed -e 's;/usr/include/[^ ]* ;;g' -e '/^  *\\\\/d' -e 's;$cwd/;;g'";
($MAKE = popen("cat Makefile - | make -f - __depend__ $make_portable", "w")) || 
    die("Cannot execute \"make\"\n");

fprintf($MAKE, "\n__depend__:\n");

($MAKEFILE = fopen("Makefile", "r")) || die("Cannot open \"Makefile\"\n");
while (($line = fgets($MAKEFILE)) != FALSE) {
    if (preg_match("/^\s+\S\(LIBTOOL\)\s+--mode=compile\s+(\S\(CC\))\s+(.+)\s+-c\s+(.+)\s+-o\s+(\S+)/", $line, $s)) {
	fprintf($MAKE, "\t@$s[1] -M $s[2] $s[3] | sed 's;^[^ ]*: ;$s[4]: ;'\n");
	continue;
    }
    if (preg_match("/^\s+\S\(LIBTOOL\)\s+--mode=/", $line)) {
	continue;
    }
    if (preg_match("/^\s+\S\(LIBTOOL\)/", $line)) {
	die("Unrecognized LIBTOOL command line in Makefile: $line\n");
    }
}
fclose($MAKEFILE);

# Wait until the "make" commands complete.
#
$status = pclose($MAKE);
exit($status);
?
