xref: /php-src/scripts/dev/genfiles (revision d7d3a1c6)
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"
38YFLAGS="-Wall"
39RE2C=${RE2C:-re2c}
40RE2C_FLAGS="-i"
41SED=${SED:-sed}
42MAKE=${MAKE:-make}
43
44# Go to project root.
45cd $(CDPATH= cd -- "$(dirname -- "$0")/../../" && pwd -P)
46
47# Check required bison version from the configure.ac file.
48required_bison_version=$($SED -n 's/PHP_PROG_BISON(\[\([0-9\.]*\)\].*/\1/p' configure.ac)
49set -f; IFS='.'; set -- $required_bison_version; set +f; IFS=' '
50required_bison_num="$(expr ${1:-0} \* 10000 + ${2:-0} \* 100 + ${3:-0})"
51
52current_version=$($YACC --version 2> /dev/null | grep 'GNU Bison' | cut -d ' ' -f 4 | tr -d a-z)
53set -f; IFS='.'; set -- $current_version; set +f; IFS=' '
54current_bison_num="$(expr ${1:-0} \* 10000 + ${2:-0} \* 100 + ${3:-0})"
55
56if test -z "$current_version"; then
57  echo "genfiles: bison not found." >&2
58  echo "          You need bison version $required_bison_version or newer installed" >&2
59  echo "          to regenerate parser files." >&2
60  exit 1
61fi
62
63if test "$current_bison_num" -lt "$required_bison_num"; then
64  echo "genfiles: bison version $current_version found." >&2
65  echo "          You need bison version $required_bison_version or newer installed" >&2
66  echo "          to build parser files." >&2
67  exit 1
68else
69  echo "genfiles: bison version $current_version (ok)"
70fi
71
72# Check required re2c version from the configure.ac file.
73required_re2c_version=$($SED -n 's/PHP_PROG_RE2C(\[\(.*\)\])/\1/p' configure.ac)
74set -f; IFS='.'; set -- $required_re2c_version; set +f; IFS=' '
75required_re2c_num="$(expr ${1:-0} \* 10000 + ${2:-0} \* 100 + ${3:-0})"
76
77current_version="$($RE2C --version | cut -d ' ' -f 2  2>/dev/null)"
78set -f; IFS='.'; set -- $current_version; set +f; IFS=' '
79current_re2c_num="$(expr ${1:-0} \* 10000 + ${2:-0} \* 100 + ${3:-0})"
80
81if test -z "$current_version"; then
82  echo "genfiles: re2c not found." >&2
83  echo "          You need re2c version $required_re2c_version or newer installed" >&2
84  echo "          to regenerate lexer files." >&2
85  exit 1
86fi
87
88if test "$current_re2c_num" -lt "$required_re2c_num"; then
89  echo "genfiles: re2c version $current_version found." >&2
90  echo "          You need re2c version $required_re2c_version or newer installed" >&2
91  echo "          to build lexer files." >&2
92  exit 1
93else
94  echo "genfiles: re2c version $current_version (ok)"
95fi
96
97# Check if make exists.
98if ! test -x "$(command -v $MAKE)"; then
99  echo "genfiles: make not found. Please install make to generate files." >&2
100  exit 1
101fi
102
103echo "genfiles: Generating Zend parser and lexer files"
104$MAKE RE2C="$RE2C" RE2C_FLAGS="$RE2C_FLAGS" YACC="$YACC" YFLAGS="$YFLAGS" SED="$SED" srcdir=Zend builddir=Zend top_srcdir=. \
105  -f Zend/Makefile.frag \
106  Zend/zend_language_parser.c \
107  Zend/zend_language_scanner.c \
108  Zend/zend_ini_parser.c \
109  Zend/zend_ini_scanner.c
110
111echo "genfiles: Generating phpdbg parser and lexer files"
112$MAKE RE2C="$RE2C" RE2C_FLAGS="$RE2C_FLAGS" YACC="$YACC" YFLAGS="$YFLAGS" srcdir=sapi/phpdbg builddir=sapi/phpdbg top_srcdir=. \
113  -f sapi/phpdbg/Makefile.frag \
114  sapi/phpdbg/phpdbg_parser.c \
115  sapi/phpdbg/phpdbg_lexer.c
116
117echo "genfiles: Generating json extension parser and lexer files"
118$MAKE RE2C="$RE2C" RE2C_FLAGS="$RE2C_FLAGS" YACC="$YACC" YFLAGS="$YFLAGS" srcdir=ext/json builddir=ext/json top_srcdir=. \
119  -f ext/json/Makefile.frag \
120  ext/json/json_parser.tab.c \
121  ext/json/json_scanner.c
122
123echo "genfiles: Generating PDO lexer file"
124$MAKE RE2C="$RE2C" RE2C_FLAGS="$RE2C_FLAGS" srcdir=ext/pdo builddir=ext/pdo top_srcdir=. \
125  -f ext/pdo/Makefile.frag \
126  ext/pdo/pdo_sql_parser.c
127
128echo "genfiles: Generating standard extension lexer files"
129$MAKE RE2C="$RE2C" RE2C_FLAGS="$RE2C_FLAGS" srcdir=ext/standard builddir=ext/standard top_srcdir=. \
130  -f ext/standard/Makefile.frag \
131  ext/standard/var_unserializer.c \
132  ext/standard/url_scanner_ex.c
133
134echo "genfiles: Generating phar extension lexer file"
135$MAKE RE2C="$RE2C" RE2C_FLAGS="$RE2C_FLAGS" srcdir=ext/phar builddir=ext/phar top_srcdir=. \
136  -f ext/phar/Makefile.frag \
137  ext/phar/phar_path_check.c
138