php-src/buildconf
Hans Krentel (hakre) c075546320
Fail early in *nix configuration build script
Adding two exit early safeguards in the *nix configuration build script:

1) Given the initial cd into the build tree fails (the project root),
   the `buildconf` script exits with non-zero status (failure).
2) Given the grep command does not exist or `configure.ac` AC_INIT [1]
   expectations are unmet, the buildconf script exits non-zero.

Additionally quoting the pathname to cd into and the empty CD_PATH
parameter for portability, also for systems that are using a
non-portable pathname [2] for the build tree.

The initial CD safeguard has been applied to the `buildconf` and
four more scripts:

- build/genif.sh
- scripts/dev/credits
- scripts/dev/genfiles
- scripts/dev/makedist

Rationale:

Cd-ing into the project root should always prematurely exit w/ FAILURE
as a required precondition for its invocation has not been met. This
should never go unnoticed as it always requires user intervention.

Similar and more specifically to the PHP build on *nix systems, the
grep command is required early to obtain the `php_extra_version` from
configure.ac.  Previously, if the grep command is missing (or failing
due to not matching the line with the AC_INIT macro [1]), the internal
dev parameter would always be zero (0) which can easily result in the
situation that the configure script is not being rebuilt. This is
cumbersome as the rebuild of a configure script is more likely required
with checked-out dev versions under change rather than an already
properly set-up build environment on a dedicated build or release
system. Missing the fact that either the grep utility is missing or
the expectation of having the AC_INIT macro in configure.ac is unmet
should never go unnoticed as it always requires user intervention.

[1]: https://www.gnu.org/software/autoconf/manual/autoconf-2.67/html_node/Initializing-configure.html
[2]: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_271

Closes GH-16717.
2024-11-09 14:03:04 +01:00

137 lines
3.9 KiB
Bash
Executable file

#!/bin/sh
#
# A wrapper around Autoconf that generates files to build PHP on *nix systems.
PHP_AUTOCONF=${PHP_AUTOCONF:-autoconf}
PHP_AUTOHEADER=${PHP_AUTOHEADER:-autoheader}
force=0
debug=0
# Go to project root.
cd "$(CDPATH='' cd -- "$(dirname -- "$0")" && pwd -P)" || exit
php_extra_version=$(grep '^AC_INIT(' configure.ac) || exit
case "$php_extra_version" in
*-dev*)
dev=1
;;
*)
dev=0
;;
esac
while test $# -gt 0; do
if test "$1" = "-h" || test "$1" = "--help"; then
cat << HELP
PHP buildconf
A wrapper around the autoconf and autoheader that generate files for building
PHP on *nix systems (configure and main/php_config.h.in). The configure script
is used to customize the PHP build based on the provided options and system. PHP
releases downloaded from PHP.net already include the configure script so
installing Autoconf and running buildconf is not needed. For the PHP sources
from the Git repository, buildconf is used for generating a new configure script
and required files.
SYNOPSIS:
buildconf [<options>]
OPTIONS:
-f, --force Regenerate configure files in PHP release packages.
--debug Display warnings emitted by Autoconf.
-h, --help Display this help.
ENVIRONMENT:
The following optional variables are supported:
PHP_AUTOCONF Overrides the path to autoconf tool.
PHP_AUTOCONF=/path/to/autoconf ./buildconf
PHP_AUTOHEADER Overrides the path to autoheader tool.
PHP_AUTOHEADER=/path/to/autoheader ./buildconf
HELP
exit 0
fi
if test "$1" = "-f" || test "$1" = "--force"; then
force=1
fi
if test "$1" = "--debug"; then
debug=1
fi
shift
done
if test "$dev" = "0" && test "$force" = "0"; then
if test -f "configure" && test -f "main/php_config.h.in"; then
echo "buildconf: The configure script is already built. All done."
echo " Run ./configure to proceed with customizing the PHP build."
exit 0
else
echo "buildconf: Configure files are missing." >&2
echo " Run ./buildconf --force to create a configure script." >&2
exit 1
fi
fi
echo "buildconf: Checking installation"
# Get minimum required autoconf version from the configure.ac file.
min_version=$(sed -n 's/AC_PREREQ(\[\(.*\)\])/\1/p' configure.ac)
# Check if autoconf exists.
ac_version=$($PHP_AUTOCONF --version 2>/dev/null|head -n 1|sed -e 's/^[^0-9]*//' -e 's/[a-z]* *$//')
if test -z "$ac_version"; then
echo "buildconf: autoconf not found." >&2
echo " You need autoconf version $min_version or newer installed" >&2
echo " to build PHP from Git." >&2
exit 1
fi
# Check autoconf version.
set -f; IFS='.'; set -- $ac_version; set +f; IFS=' '
ac_version_num="$(expr ${1} \* 10000 + ${2} \* 100)"
set -f; IFS='.'; set -- $min_version; set +f; IFS=' '
min_version_num="$(expr ${1} \* 10000 + ${2} \* 100)"
if test "$ac_version_num" -lt "$min_version_num"; then
echo "buildconf: autoconf version $ac_version found." >&2
echo " You need autoconf version $min_version or newer installed" >&2
echo " to build PHP from Git." >&2
exit 1
else
echo "buildconf: autoconf version $ac_version (ok)"
fi
if test "$force" = "1"; then
echo "buildconf: Forcing buildconf. The configure files will be regenerated."
fi
# Clean cache and explicitly remove all targets if present. Remove also
# aclocal.m4 if present. It is automatically included by autoconf but not used
# by the PHP build system since PHP 7.4.
echo "buildconf: Cleaning cache and configure files"
rm -rf \
aclocal.m4 \
autom4te.cache \
config.cache \
configure \
main/php_config.h.in
if test "$debug" = "1"; then
autoconf_flags="-f -Wall"
autoheader_flags="-Wall"
else
autoconf_flags="-f"
autoheader_flags=""
fi
echo "buildconf: Rebuilding configure"
$PHP_AUTOCONF $autoconf_flags
echo "buildconf: Rebuilding main/php_config.h.in"
$PHP_AUTOHEADER $autoheader_flags
echo "buildconf: Run ./configure to proceed with customizing the PHP build."