Skip to content

Commit 29e4227

Browse files
committed
Refactor bison and re2c usages
- This adds new BISON and RE2C utility modules on top of the find modules to have common configuration at one place. - Zend patching improved a bit - More common naming for lexers and parsers used in filenames: "grammar" - ...
1 parent f6a7a69 commit 29e4227

20 files changed

+953
-614
lines changed

cmake/Zend/CMakeLists.txt

Lines changed: 1 addition & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -526,103 +526,7 @@ endif()
526526
# Generate lexers and parsers.
527527
################################################################################
528528

529-
if(BISON_FOUND)
530-
bison_target(
531-
zend_ini_parser
532-
zend_ini_parser.y
533-
${CMAKE_CURRENT_SOURCE_DIR}/zend_ini_parser.c
534-
COMPILE_FLAGS "${PHP_DEFAULT_BISON_FLAGS}"
535-
VERBOSE REPORT_FILE zend_ini_parser.output
536-
DEFINES_FILE ${CMAKE_CURRENT_SOURCE_DIR}/zend_ini_parser.h
537-
)
538-
539-
bison_target(
540-
zend_language_parser
541-
zend_language_parser.y
542-
${CMAKE_CURRENT_SOURCE_DIR}/zend_language_parser.c
543-
COMPILE_FLAGS "${PHP_DEFAULT_BISON_FLAGS}"
544-
VERBOSE REPORT_FILE zend_language_parser.output
545-
DEFINES_FILE ${CMAKE_CURRENT_SOURCE_DIR}/zend_language_parser.h
546-
)
547-
548-
# Tweak zendparse to be exported through ZEND_API. This has to be revisited
549-
# once bison supports foreign skeletons and that bison version is used. Read
550-
# https://git.savannah.gnu.org/cgit/bison.git/tree/data/README.md for more.
551-
file(
552-
GENERATE
553-
OUTPUT CMakeFiles/PatchLanguageParser.cmake
554-
CONTENT [[
555-
file(READ "${SOURCE_DIR}/zend_language_parser.h" content)
556-
string(
557-
REPLACE
558-
"int zendparse"
559-
"ZEND_API int zendparse"
560-
content_2
561-
"${content}"
562-
)
563-
if(
564-
NOT content MATCHES "ZEND_API int zendparse"
565-
AND NOT content STREQUAL "${content_2}"
566-
)
567-
execute_process(
568-
COMMAND ${CMAKE_COMMAND} -E cmake_echo_color --blue --bold
569-
" [Zend] Patching Zend/zend_language_parser.h"
570-
)
571-
file(WRITE "${SOURCE_DIR}/zend_language_parser.h" "${content_2}")
572-
endif()
573-
574-
file(READ "${SOURCE_DIR}/zend_language_parser.c" content)
575-
string(
576-
REPLACE
577-
"int zendparse"
578-
"ZEND_API int zendparse"
579-
content_2
580-
"${content}"
581-
)
582-
if(
583-
NOT content MATCHES "ZEND_API int zendparse"
584-
AND NOT content STREQUAL "${content_2}"
585-
)
586-
execute_process(
587-
COMMAND ${CMAKE_COMMAND} -E cmake_echo_color --blue --bold
588-
" [Zend] Patching Zend/zend_language_parser.c"
589-
)
590-
file(WRITE "${SOURCE_DIR}/zend_language_parser.c" "${content_2}")
591-
endif()
592-
]]
593-
)
594-
595-
add_custom_target(
596-
zend_patch_language_parser
597-
COMMAND ${CMAKE_COMMAND}
598-
-D SOURCE_DIR=${CMAKE_CURRENT_SOURCE_DIR}
599-
-P "CMakeFiles/PatchLanguageParser.cmake"
600-
DEPENDS ${BISON_zend_language_parser_OUTPUTS}
601-
VERBATIM
602-
)
603-
604-
add_dependencies(zend zend_patch_language_parser)
605-
endif()
606-
607-
if(RE2C_FOUND)
608-
re2c_target(
609-
zend_language_scanner
610-
zend_language_scanner.l
611-
${CMAKE_CURRENT_SOURCE_DIR}/zend_language_scanner.c
612-
HEADER ${CMAKE_CURRENT_SOURCE_DIR}/zend_language_scanner_defs.h
613-
OPTIONS --case-inverted -cbdF
614-
CODEGEN
615-
)
616-
617-
re2c_target(
618-
zend_ini_scanner
619-
zend_ini_scanner.l
620-
${CMAKE_CURRENT_SOURCE_DIR}/zend_ini_scanner.c
621-
HEADER ${CMAKE_CURRENT_SOURCE_DIR}/zend_ini_scanner_defs.h
622-
OPTIONS --case-inverted -cbdF
623-
CODEGEN
624-
)
625-
endif()
529+
include(cmake/GenerateGrammar.cmake)
626530

627531
################################################################################
628532
# Configure fibers.
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
#[=============================================================================[
2+
Generate lexer and parser files for Zend Engine.
3+
#]=============================================================================]
4+
5+
if(
6+
EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/zend_ini_parser.c
7+
AND EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/zend_ini_parser.h
8+
AND EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/zend_language_parser.c
9+
AND EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/zend_language_parser.h
10+
)
11+
set(PHP_BISON_OPTIONAL TRUE)
12+
endif()
13+
include(PHP/BISON)
14+
15+
if(BISON_FOUND)
16+
php_bison(
17+
zend_ini_parser
18+
zend_ini_parser.y
19+
${CMAKE_CURRENT_SOURCE_DIR}/zend_ini_parser.c
20+
COMPILE_FLAGS "${PHP_BISON_DEFAULT_OPTIONS}"
21+
VERBOSE REPORT_FILE zend_ini_parser.output
22+
DEFINES_FILE ${CMAKE_CURRENT_SOURCE_DIR}/zend_ini_parser.h
23+
)
24+
25+
php_bison(
26+
zend_language_parser
27+
zend_language_parser.y
28+
${CMAKE_CURRENT_SOURCE_DIR}/zend_language_parser.c
29+
COMPILE_FLAGS "${PHP_BISON_DEFAULT_OPTIONS}"
30+
VERBOSE REPORT_FILE zend_language_parser.output
31+
DEFINES_FILE ${CMAKE_CURRENT_SOURCE_DIR}/zend_language_parser.h
32+
)
33+
34+
# Tweak zendparse to be exported through ZEND_API. This has to be revisited
35+
# once bison supports foreign skeletons and that bison version is used. Read
36+
# https://git.savannah.gnu.org/cgit/bison.git/tree/data/README.md for more.
37+
block()
38+
string(
39+
CONCAT patch
40+
"set(SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})\n"
41+
[[
42+
file(READ "${SOURCE_DIR}/zend_language_parser.h" content)
43+
string(
44+
REPLACE
45+
"int zendparse"
46+
"ZEND_API int zendparse"
47+
content_2
48+
"${content}"
49+
)
50+
if(
51+
NOT content MATCHES "ZEND_API int zendparse"
52+
AND NOT content STREQUAL "${content_2}"
53+
)
54+
message(STATUS "[Zend] Patching zend_language_parser.h")
55+
file(WRITE "${SOURCE_DIR}/zend_language_parser.h" "${content_2}")
56+
endif()
57+
58+
file(READ "${SOURCE_DIR}/zend_language_parser.c" content)
59+
string(
60+
REPLACE
61+
"int zendparse"
62+
"ZEND_API int zendparse"
63+
content_2
64+
"${content}"
65+
)
66+
if(
67+
NOT content MATCHES "ZEND_API int zendparse"
68+
AND NOT content STREQUAL "${content_2}"
69+
)
70+
message(STATUS "[Zend] Patching zend_language_parser.c")
71+
file(WRITE "${SOURCE_DIR}/zend_language_parser.c" "${content_2}")
72+
endif()
73+
]])
74+
75+
# Run patch based on whether building or running inside a CMake script.
76+
if(CMAKE_SCRIPT_MODE_FILE)
77+
cmake_language(EVAL CODE "${patch}")
78+
else()
79+
file(
80+
GENERATE
81+
OUTPUT CMakeFiles/PatchLanguageParser.cmake
82+
CONTENT "${patch}"
83+
)
84+
add_custom_target(
85+
zend_patch_language_parser
86+
COMMAND ${CMAKE_COMMAND} -P CMakeFiles/PatchLanguageParser.cmake
87+
DEPENDS ${BISON_zend_language_parser_OUTPUTS}
88+
VERBATIM
89+
)
90+
add_dependencies(zend zend_patch_language_parser)
91+
endif()
92+
endblock()
93+
endif()
94+
95+
if(
96+
EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/zend_ini_scanner.c
97+
AND EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/zend_ini_scanner_defs.h
98+
AND EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/zend_language_scanner.c
99+
AND EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/zend_language_scanner_defs.h
100+
)
101+
set(PHP_RE2C_OPTIONAL TRUE)
102+
endif()
103+
include(PHP/RE2C)
104+
105+
php_re2c(
106+
zend_ini_scanner
107+
zend_ini_scanner.l
108+
${CMAKE_CURRENT_SOURCE_DIR}/zend_ini_scanner.c
109+
HEADER ${CMAKE_CURRENT_SOURCE_DIR}/zend_ini_scanner_defs.h
110+
OPTIONS --case-inverted -cbdF
111+
CODEGEN
112+
)
113+
114+
php_re2c(
115+
zend_language_scanner
116+
zend_language_scanner.l
117+
${CMAKE_CURRENT_SOURCE_DIR}/zend_language_scanner.c
118+
HEADER ${CMAKE_CURRENT_SOURCE_DIR}/zend_language_scanner_defs.h
119+
OPTIONS --case-inverted -cbdF
120+
CODEGEN
121+
)

cmake/cmake/Configuration.cmake

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,6 @@ mark_as_advanced(PHP_PROGRAM_PREFIX)
9292
set(PHP_PROGRAM_SUFFIX "" CACHE STRING "Append suffix to the program names")
9393
mark_as_advanced(PHP_PROGRAM_SUFFIX)
9494

95-
option(PHP_RE2C_CGOTO "Enable computed goto GCC extension with re2c")
96-
mark_as_advanced(PHP_RE2C_CGOTO)
97-
9895
option(PHP_THREAD_SAFETY "Enable thread safety (ZTS)")
9996

10097
cmake_dependent_option(

cmake/cmake/Requirements.cmake

Lines changed: 0 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ include_guard(GLOBAL)
66

77
include(CheckSourceRuns)
88
include(CMakePushCheckState)
9-
include(FeatureSummary)
109

1110
################################################################################
1211
# Check whether some minimum supported compiler is used.
@@ -45,75 +44,6 @@ else()
4544
message(CHECK_PASS "ASCII")
4645
endif()
4746

48-
################################################################################
49-
# Check if bison and re2c are required.
50-
#
51-
# PHP tarball packaged and released at php.net already contains generated lexer
52-
# and parser files. In such cases these don't need to be generated again. When
53-
# building from a Git repository, bison and re2c are required to be installed so
54-
# files can be generated as part of the build process.
55-
################################################################################
56-
#
57-
# Check if bison is required.
58-
if(
59-
NOT EXISTS ${PHP_SOURCE_DIR}/Zend/zend_ini_parser.c
60-
OR NOT EXISTS ${PHP_SOURCE_DIR}/Zend/zend_ini_parser.h
61-
OR NOT EXISTS ${PHP_SOURCE_DIR}/Zend/zend_language_parser.c
62-
OR NOT EXISTS ${PHP_SOURCE_DIR}/Zend/zend_language_parser.h
63-
OR NOT EXISTS ${PHP_SOURCE_DIR}/ext/json/json_parser.tab.c
64-
OR NOT EXISTS ${PHP_SOURCE_DIR}/ext/json/json_parser.tab.h
65-
OR NOT EXISTS ${PHP_SOURCE_DIR}/sapi/phpdbg/phpdbg_parser.c
66-
OR NOT EXISTS ${PHP_SOURCE_DIR}/sapi/phpdbg/phpdbg_parser.h
67-
)
68-
find_package(BISON 3.0.0)
69-
set_package_properties(
70-
BISON
71-
PROPERTIES
72-
TYPE REQUIRED
73-
PURPOSE "Necessary to generate PHP parser files."
74-
)
75-
# Add Bison options based on the build type.
76-
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.32)
77-
# See: https://gitlab.kitware.com/cmake/cmake/-/merge_requests/9921
78-
set(PHP_DEFAULT_BISON_FLAGS "-Wall $<$<CONFIG:Release,MinSizeRel>:-l>")
79-
else()
80-
set(PHP_DEFAULT_BISON_FLAGS "$<IF:$<CONFIG:Release,MinSizeRel>,-lWall,-Wall>")
81-
endif()
82-
endif()
83-
84-
# Check if re2c is required.
85-
if(
86-
NOT EXISTS ${PHP_SOURCE_DIR}/Zend/zend_language_scanner.c
87-
OR NOT EXISTS ${PHP_SOURCE_DIR}/Zend/zend_language_scanner_defs.h
88-
OR NOT EXISTS ${PHP_SOURCE_DIR}/Zend/zend_ini_scanner.c
89-
OR NOT EXISTS ${PHP_SOURCE_DIR}/Zend/zend_ini_scanner_defs.h
90-
OR NOT EXISTS ${PHP_SOURCE_DIR}/ext/json/json_scanner.c
91-
OR NOT EXISTS ${PHP_SOURCE_DIR}/ext/json/php_json_scanner_defs.h
92-
OR NOT EXISTS ${PHP_SOURCE_DIR}/ext/pdo/pdo_sql_parser.c
93-
OR NOT EXISTS ${PHP_SOURCE_DIR}/ext/phar/phar_path_check.c
94-
OR NOT EXISTS ${PHP_SOURCE_DIR}/ext/standard/url_scanner_ex.c
95-
OR NOT EXISTS ${PHP_SOURCE_DIR}/ext/standard/var_unserializer.c
96-
OR NOT EXISTS ${PHP_SOURCE_DIR}/sapi/phpdbg/phpdbg_lexer.c
97-
)
98-
if(PHP_RE2C_CGOTO)
99-
set(RE2C_USE_COMPUTED_GOTOS TRUE)
100-
endif()
101-
102-
set(
103-
RE2C_DEFAULT_OPTIONS
104-
--no-generation-date # Suppress date output in the generated file.
105-
$<$<CONFIG:Release,MinSizeRel>:-i> # Do not output line directives.
106-
)
107-
108-
find_package(RE2C 1.0.3)
109-
set_package_properties(
110-
RE2C
111-
PROPERTIES
112-
TYPE REQUIRED
113-
PURPOSE "Necessary to generate PHP lexer files."
114-
)
115-
endif()
116-
11747
################################################################################
11848
# Find mailer.
11949
################################################################################

cmake/cmake/modules/FindBISON.cmake

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ See: https://cmake.org/cmake/help/latest/module/FindBISON.html
88
This module overrides the upstream CMake `FindBISON` module with few
99
customizations.
1010
11-
A new `bison_generate()` function is added to be able to use it in command-line
11+
A new `bison_execute()` function is added to be able to use it in command-line
1212
scripts.
1313
1414
```cmake
15-
bison_generate(
15+
bison_execute(
1616
<name>
1717
<input>
1818
<output>
19-
[COMPILE_OPTIONS <flags>]
19+
[COMPILE_FLAGS <string>]
2020
[DEFINES_FILE <file>]
2121
[VERBOSE [REPORT_FILE <file>]]
2222
)
@@ -44,14 +44,14 @@ if(NOT BISON_FOUND)
4444
return()
4545
endif()
4646

47-
function(bison_generate)
47+
function(bison_execute)
4848
cmake_parse_arguments(
4949
PARSE_ARGV
5050
3
5151
parsed # prefix
5252
"VERBOSE" # options
53-
"DEFINES_FILE;REPORT_FILE" # one-value keywords
54-
"COMPILE_OPTIONS" # multi-value keywords
53+
"DEFINES_FILE;REPORT_FILE;COMPILE_FLAGS" # one-value keywords
54+
"" # multi-value keywords
5555
)
5656

5757
if(parsed_UNPARSED_ARGUMENTS)
@@ -72,12 +72,33 @@ function(bison_generate)
7272
set(output ${CMAKE_CURRENT_BINARY_DIR}/${output})
7373
endif()
7474

75-
set(options ${parsed_OPTIONS})
75+
separate_arguments(options NATIVE_COMMAND "${parsed_COMPILE_FLAGS}")
76+
77+
if(parsed_DEFINES_FILE)
78+
list(APPEND options --defines=${parsed_DEFINES_FILE})
79+
endif()
80+
81+
if(parsed_VERBOSE)
82+
list(APPEND options --verbose)
83+
endif()
84+
85+
if(parsed_REPORT_FILE AND NOT IS_ABSOLUTE "${parsed_REPORT_FILE}")
86+
set(parsed_REPORT_FILE ${CMAKE_CURRENT_BINARY_DIR}/${parsed_REPORT_FILE})
87+
endif()
88+
89+
if(parsed_REPORT_FILE)
90+
list(APPEND options --report-file=${parsed_REPORT_FILE})
91+
endif()
92+
93+
set(
94+
commands
95+
COMMAND ${BISON_EXECUTABLE} ${options} --output ${output} ${input}
96+
)
7697

7798
message(
7899
STATUS
79100
"[BISON][${ARGV0}] Generating parser with bison ${BISON_VERSION}"
80101
)
81102

82-
execute_process(COMMAND ${BISON_EXECUTABLE} ${options} -o ${output} ${input})
103+
execute_process(${commands})
83104
endfunction()

0 commit comments

Comments
 (0)