php-src/scripts/dev/genfiles
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

136 lines
5.5 KiB
Bash
Executable file

#!/bin/sh
#
# +----------------------------------------------------------------------+
# | Copyright (c) The PHP Group |
# +----------------------------------------------------------------------+
# | This source file is subject to version 3.01 of the PHP license, |
# | that is bundled with this package in the file LICENSE, and is |
# | available through the world-wide-web at the following url: |
# | https://php.net/license/3_01.txt |
# | If you did not receive a copy of the PHP license and are unable to |
# | obtain it through the world-wide-web, please send a note to |
# | license@php.net so we can mail you a copy immediately. |
# +----------------------------------------------------------------------+
# | Authors: Sascha Schumann <sascha@schumann.cx> |
# +----------------------------------------------------------------------+
#
# This script generates PHP lexer and parser files required to build PHP. The
# generated files are ignored in the Git repository and packaged during the PHP
# release process into the release installation archive download. This way the
# bison and re2c dependencies are not required to build PHP when downloading
# release archive.
#
# Usage: genfiles
#
# Environment:
# The following environment variables can override default generators paths.
#
# YACC Parser generator program, default bison
# RE2C Lexer generator program, default re2c
# SED Path to sed program, default sed
# MAKE Path to make program, default make
#
# For example:
# YACC=/path/to/bison ./genfiles
YACC=${YACC:-bison}
YACC="$YACC -l"
RE2C=${RE2C:-re2c}
RE2C_FLAGS="-i"
SED=${SED:-sed}
MAKE=${MAKE:-make}
# Go to project root.
cd "$(CDPATH='' cd -- "$(dirname -- "$0")/../../" && pwd -P)" || exit
# Check required bison version from the configure.ac file.
required_bison_version=$($SED -n 's/PHP_PROG_BISON(\[\([0-9\.]*\)\].*/\1/p' configure.ac)
set -f; IFS='.'; set -- $required_bison_version; set +f; IFS=' '
required_bison_num="$(expr ${1:-0} \* 10000 + ${2:-0} \* 100 + ${3:-0})"
current_version=$($YACC --version 2> /dev/null | grep 'GNU Bison' | cut -d ' ' -f 4 | tr -d a-z)
set -f; IFS='.'; set -- $current_version; set +f; IFS=' '
current_bison_num="$(expr ${1:-0} \* 10000 + ${2:-0} \* 100 + ${3:-0})"
if test -z "$current_version"; then
echo "genfiles: bison not found." >&2
echo " You need bison version $required_bison_version or newer installed" >&2
echo " to regenerate parser files." >&2
exit 1
fi
if test "$current_bison_num" -lt "$required_bison_num"; then
echo "genfiles: bison version $current_version found." >&2
echo " You need bison version $required_bison_version or newer installed" >&2
echo " to build parser files." >&2
exit 1
else
echo "genfiles: bison version $current_version (ok)"
fi
# Check required re2c version from the configure.ac file.
required_re2c_version=$($SED -n 's/PHP_PROG_RE2C(\[\(.*\)\])/\1/p' configure.ac)
set -f; IFS='.'; set -- $required_re2c_version; set +f; IFS=' '
required_re2c_num="$(expr ${1:-0} \* 10000 + ${2:-0} \* 100 + ${3:-0})"
current_version="$($RE2C --version | cut -d ' ' -f 2 2>/dev/null)"
set -f; IFS='.'; set -- $current_version; set +f; IFS=' '
current_re2c_num="$(expr ${1:-0} \* 10000 + ${2:-0} \* 100 + ${3:-0})"
if test -z "$current_version"; then
echo "genfiles: re2c not found." >&2
echo " You need re2c version $required_re2c_version or newer installed" >&2
echo " to regenerate lexer files." >&2
exit 1
fi
if test "$current_re2c_num" -lt "$required_re2c_num"; then
echo "genfiles: re2c version $current_version found." >&2
echo " You need re2c version $required_re2c_version or newer installed" >&2
echo " to build lexer files." >&2
exit 1
else
echo "genfiles: re2c version $current_version (ok)"
fi
# Check if make exists.
if ! test -x "$(command -v $MAKE)"; then
echo "genfiles: make not found. Please install make to generate files." >&2
exit 1
fi
echo "genfiles: Generating Zend parser and lexer files"
$MAKE RE2C="$RE2C" RE2C_FLAGS="$RE2C_FLAGS" YACC="$YACC" SED="$SED" srcdir=Zend builddir=Zend top_srcdir=. \
-f Zend/Makefile.frag \
Zend/zend_language_parser.c \
Zend/zend_language_scanner.c \
Zend/zend_ini_parser.c \
Zend/zend_ini_scanner.c
echo "genfiles: Generating phpdbg parser and lexer files"
$MAKE RE2C="$RE2C" RE2C_FLAGS="$RE2C_FLAGS" YACC="$YACC" srcdir=sapi/phpdbg builddir=sapi/phpdbg top_srcdir=. \
-f sapi/phpdbg/Makefile.frag \
sapi/phpdbg/phpdbg_parser.c \
sapi/phpdbg/phpdbg_lexer.c
echo "genfiles: Generating json extension parser and lexer files"
$MAKE RE2C="$RE2C" RE2C_FLAGS="$RE2C_FLAGS" YACC="$YACC" srcdir=ext/json builddir=ext/json top_srcdir=. \
-f ext/json/Makefile.frag \
ext/json/json_parser.tab.c \
ext/json/json_scanner.c
echo "genfiles: Generating PDO lexer file"
$MAKE RE2C="$RE2C" RE2C_FLAGS="$RE2C_FLAGS" srcdir=ext/pdo builddir=ext/pdo top_srcdir=. \
-f ext/pdo/Makefile.frag \
ext/pdo/pdo_sql_parser.c
echo "genfiles: Generating standard extension lexer files"
$MAKE RE2C="$RE2C" RE2C_FLAGS="$RE2C_FLAGS" srcdir=ext/standard builddir=ext/standard top_srcdir=. \
-f ext/standard/Makefile.frag \
ext/standard/var_unserializer.c \
ext/standard/url_scanner_ex.c
echo "genfiles: Generating phar extension lexer file"
$MAKE RE2C="$RE2C" RE2C_FLAGS="$RE2C_FLAGS" srcdir=ext/phar builddir=ext/phar top_srcdir=. \
-f ext/phar/Makefile.frag \
ext/phar/phar_path_check.c