xref: /PHP-8.0/scripts/dev/genfiles (revision 3709e74b)
1#!/bin/sh
2#
3#  +----------------------------------------------------------------------+
4#  | Copyright (c) The PHP Group                                          |
5#  +----------------------------------------------------------------------+
6#  | This source file is subject to version 3.01 of the PHP license,      |
7#  | that is bundled with this package in the file LICENSE, and is        |
8#  | available through the world-wide-web at the following url:           |
9#  | https://php.net/license/3_01.txt                                     |
10#  | If you did not receive a copy of the PHP license and are unable to   |
11#  | obtain it through the world-wide-web, please send a note to          |
12#  | license@php.net so we can mail you a copy immediately.               |
13#  +----------------------------------------------------------------------+
14#  | Authors: Sascha Schumann <sascha@schumann.cx>                        |
15#  +----------------------------------------------------------------------+
16#
17# This script generates PHP lexer and parser files required to build PHP. The
18# generated files are ignored in the Git repository and packaged during the PHP
19# release process into the release installation archive download. This way the
20# bison and re2c dependencies are not required to build PHP when downloading
21# release archive.
22#
23# Usage: genfiles
24#
25# Environment:
26#   The following environment variables can override default generators paths.
27#
28#     YACC    Parser generator program, default bison
29#     RE2C    Lexer generator program, default re2c
30#     SED     Path to sed program, default sed
31#     MAKE    Path to make program, default make
32#
33#   For example:
34#     YACC=/path/to/bison ./genfiles
35
36YACC=${YACC:-bison}
37YACC="$YACC -l"
38RE2C=${RE2C:-re2c}
39RE2C_FLAGS="-i"
40SED=${SED:-sed}
41MAKE=${MAKE:-make}
42
43# Go to project root.
44cd $(CDPATH= cd -- "$(dirname -- "$0")/../../" && pwd -P)
45
46# Check required bison version from the configure.ac file.
47required_bison_version=$($SED -n 's/PHP_PROG_BISON(\[\([0-9\.]*\)\].*/\1/p' configure.ac)
48set -f; IFS='.'; set -- $required_bison_version; set +f; IFS=' '
49required_bison_num="$(expr ${1:-0} \* 10000 + ${2:-0} \* 100 + ${3:-0})"
50
51current_version=$($YACC --version 2> /dev/null | grep 'GNU Bison' | cut -d ' ' -f 4 | tr -d a-z)
52set -f; IFS='.'; set -- $current_version; set +f; IFS=' '
53current_bison_num="$(expr ${1:-0} \* 10000 + ${2:-0} \* 100 + ${3:-0})"
54
55if test -z "$current_version"; then
56  echo "genfiles: bison not found." >&2
57  echo "          You need bison version $required_bison_version or newer installed" >&2
58  echo "          to regenerate parser files." >&2
59  exit 1
60fi
61
62if test "$current_bison_num" -lt "$required_bison_num"; then
63  echo "genfiles: bison version $current_version found." >&2
64  echo "          You need bison version $required_bison_version or newer installed" >&2
65  echo "          to build parser files." >&2
66  exit 1
67else
68  echo "genfiles: bison version $current_version (ok)"
69fi
70
71# Check required re2c version from the configure.ac file.
72required_re2c_version=$($SED -n 's/PHP_PROG_RE2C(\[\(.*\)\])/\1/p' configure.ac)
73set -f; IFS='.'; set -- $required_re2c_version; set +f; IFS=' '
74required_re2c_num="$(expr ${1:-0} \* 10000 + ${2:-0} \* 100 + ${3:-0})"
75
76current_version="$($RE2C --version | cut -d ' ' -f 2  2>/dev/null)"
77set -f; IFS='.'; set -- $current_version; set +f; IFS=' '
78current_re2c_num="$(expr ${1:-0} \* 10000 + ${2:-0} \* 100 + ${3:-0})"
79
80if test -z "$current_version"; then
81  echo "genfiles: re2c not found." >&2
82  echo "          You need re2c version $required_re2c_version or newer installed" >&2
83  echo "          to regenerate lexer files." >&2
84  exit 1
85fi
86
87if test "$current_re2c_num" -lt "$required_re2c_num"; then
88  echo "genfiles: re2c version $current_version found." >&2
89  echo "          You need re2c version $required_re2c_version or newer installed" >&2
90  echo "          to build lexer files." >&2
91  exit 1
92else
93  echo "genfiles: re2c version $current_version (ok)"
94fi
95
96# Check if make exists.
97if ! test -x "$(command -v $MAKE)"; then
98  echo "genfiles: make not found. Please install make to generate files." >&2
99  exit 1
100fi
101
102echo "genfiles: Generating Zend parser and lexer files"
103$MAKE RE2C="$RE2C" RE2C_FLAGS="$RE2C_FLAGS" YACC="$YACC" SED="$SED" srcdir=Zend builddir=Zend top_srcdir=. \
104  -f Zend/Makefile.frag \
105  Zend/zend_language_parser.c \
106  Zend/zend_language_scanner.c \
107  Zend/zend_ini_parser.c \
108  Zend/zend_ini_scanner.c
109
110echo "genfiles: Generating phpdbg parser and lexer files"
111$MAKE RE2C="$RE2C" RE2C_FLAGS="$RE2C_FLAGS" YACC="$YACC" srcdir=sapi/phpdbg builddir=sapi/phpdbg top_srcdir=. \
112  -f sapi/phpdbg/Makefile.frag \
113  sapi/phpdbg/phpdbg_parser.c \
114  sapi/phpdbg/phpdbg_lexer.c
115
116echo "genfiles: Generating json extension parser and lexer files"
117$MAKE RE2C="$RE2C" RE2C_FLAGS="$RE2C_FLAGS" YACC="$YACC" srcdir=ext/json builddir=ext/json top_srcdir=. \
118  -f ext/json/Makefile.frag \
119  ext/json/json_parser.tab.c \
120  ext/json/json_scanner.c
121
122echo "genfiles: Generating PDO lexer file"
123$MAKE RE2C="$RE2C" RE2C_FLAGS="$RE2C_FLAGS" srcdir=ext/pdo builddir=ext/pdo top_srcdir=. \
124  -f ext/pdo/Makefile.frag \
125  ext/pdo/pdo_sql_parser.c
126
127echo "genfiles: Generating standard extension lexer files"
128$MAKE RE2C="$RE2C" RE2C_FLAGS="$RE2C_FLAGS" srcdir=ext/standard builddir=ext/standard top_srcdir=. \
129  -f ext/standard/Makefile.frag \
130  ext/standard/var_unserializer.c \
131  ext/standard/url_scanner_ex.c
132
133echo "genfiles: Generating phar extension lexer file"
134$MAKE RE2C="$RE2C" RE2C_FLAGS="$RE2C_FLAGS" srcdir=ext/phar builddir=ext/phar top_srcdir=. \
135  -f ext/phar/Makefile.frag \
136  ext/phar/phar_path_check.c
137