Skip to content

Commit 2be4310

Browse files
committed
Improve FindBISON module
1 parent e15979d commit 2be4310

File tree

9 files changed

+403
-77
lines changed

9 files changed

+403
-77
lines changed

cmake/Zend/cmake/GenerateGrammar.cmake

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ if(BISON_FOUND)
1414
if(CMAKE_SCRIPT_MODE_FILE)
1515
set(verbose "")
1616
else()
17-
set(verbose VERBOSE REPORT_FILE zend_ini_parser.output)
17+
set(verbose VERBOSE)
1818
endif()
1919

2020
bison(
@@ -29,7 +29,7 @@ if(BISON_FOUND)
2929
if(CMAKE_SCRIPT_MODE_FILE)
3030
set(verbose "")
3131
else()
32-
set(verbose VERBOSE REPORT_FILE zend_language_parser.output)
32+
set(verbose VERBOSE)
3333
endif()
3434

3535
bison(
@@ -94,12 +94,12 @@ if(BISON_FOUND)
9494
else()
9595
file(
9696
GENERATE
97-
OUTPUT CMakeFiles/PatchLanguageParser.cmake
97+
OUTPUT CMakeFiles/Zend/PatchLanguageParser.cmake
9898
CONTENT "${patch}"
9999
)
100100
add_custom_target(
101101
zend_language_parser_patch
102-
COMMAND ${CMAKE_COMMAND} -P CMakeFiles/PatchLanguageParser.cmake
102+
COMMAND ${CMAKE_COMMAND} -P CMakeFiles/Zend/PatchLanguageParser.cmake
103103
DEPENDS zend_language_parser
104104
VERBATIM
105105
)

cmake/cmake/modules/FindBISON.cmake

Lines changed: 115 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,17 @@ in various scenarios.
7979
* `DEPENDS <depends>...` - Optional list of dependent files to regenerate the
8080
output file.
8181

82+
* `VERBOSE` - This adds the `--verbose` (`-v`) command-line option to
83+
`bison` executable and will create extra output file
84+
`<parser-output-filename>.output` containing verbose descriptions of the
85+
grammar and parser. File will be created in the current binary directory.
86+
87+
* `REPORT_FILE <file>` - This adds the `--report-file=<file>` command-line
88+
option to `bison` executable and will create verbose information report in the
89+
specified `<file>`. This option must be used together with the `VERBOSE`
90+
option. Relative file path is interpreted as being relative to the current
91+
binary directory.
92+
8293
* `NO_DEFAULT_OPTIONS` - If specified, the `BISON_DEFAULT_OPTIONS` are not added
8394
to the current `bison` invocation.
8495

@@ -225,15 +236,113 @@ function(_bison_process_options options result)
225236
return(PROPAGATE ${result})
226237
endfunction()
227238

239+
# Process HEADER and HEADER_FILE options.
240+
function(_bison_process_header_option)
241+
if(NOT parsed_HEADER AND NOT parsed_HEADER_FILE)
242+
return()
243+
endif()
244+
245+
# Bison versions 3.8 and later introduced the --header=[FILE] (-H) option.
246+
# For prior versions the --defines=[FILE] (-d) option can be used.
247+
if(parsed_HEADER_FILE)
248+
set(header ${parsed_HEADER_FILE})
249+
if(NOT IS_ABSOLUTE "${header}")
250+
set(header ${CMAKE_CURRENT_BINARY_DIR}/${header})
251+
endif()
252+
253+
if(BISON_VERSION VERSION_LESS 3.8)
254+
list(APPEND options --defines=${header})
255+
else()
256+
list(APPEND options --header=${header})
257+
endif()
258+
else()
259+
if(BISON_VERSION VERSION_LESS 3.8)
260+
list(APPEND options -d)
261+
else()
262+
list(APPEND options --header)
263+
endif()
264+
265+
# Produce default header path generated by bison (see option --header).
266+
cmake_path(GET output EXTENSION LAST_ONLY extension)
267+
string(REPLACE "c" "h" extension "${extension}")
268+
if(NOT extension)
269+
set(extension ".h")
270+
endif()
271+
cmake_path(
272+
REPLACE_EXTENSION
273+
output
274+
LAST_ONLY
275+
"${extension}"
276+
OUTPUT_VARIABLE header
277+
)
278+
# TODO: Add path if header is relative.
279+
endif()
280+
281+
list(APPEND outputs ${header})
282+
283+
return(PROPAGATE outputs options)
284+
endfunction()
285+
286+
# Process the VERBOSE and REPORT_FILE options.
287+
function(_bison_process_verbose_option)
288+
if(NOT parsed_VERBOSE)
289+
return()
290+
endif()
291+
292+
list(APPEND options --verbose)
293+
294+
if(NOT parsed_REPORT_FILE)
295+
cmake_path(GET output FILENAME reportFile)
296+
cmake_path(GET output EXTENSION extension)
297+
298+
# Bison treats output files <parser-output-filename>.tab.<last-extension>
299+
# differently. It removes the '.tab' part of the extension and creates
300+
# <parser-output-filename>.output file. Elsewhere, it replaces only the
301+
# last extension with '.output'.
302+
if(extension MATCHES "\\.tab\\.([^.]+)$")
303+
string(
304+
REGEX REPLACE
305+
"\\.tab\\.${CMAKE_MATCH_1}$"
306+
".output"
307+
reportFile
308+
"${reportFile}"
309+
)
310+
else()
311+
cmake_path(REPLACE_EXTENSION reportFile LAST_ONLY "output")
312+
endif()
313+
else()
314+
set(reportFile ${parsed_REPORT_FILE})
315+
endif()
316+
317+
if(NOT IS_ABSOLUTE "${reportFile}")
318+
set(reportFile ${CMAKE_CURRENT_BINARY_DIR}/${reportFile})
319+
endif()
320+
321+
list(APPEND options --report-file=${reportFile})
322+
323+
return(PROPAGATE options)
324+
endfunction()
325+
228326
macro(_bison_process)
229327
if(parsed_UNPARSED_ARGUMENTS)
230-
message(FATAL_ERROR "Bad arguments: ${parsed_UNPARSED_ARGUMENTS}")
328+
message(FATAL_ERROR "Unrecognized arguments: ${parsed_UNPARSED_ARGUMENTS}")
231329
endif()
232330

233331
if(parsed_KEYWORDS_MISSING_VALUES)
234332
message(FATAL_ERROR "Missing values for: ${parsed_KEYWORDS_MISSING_VALUES}")
235333
endif()
236334

335+
if(parsed_HEADER AND parsed_HEADER_FILE)
336+
message(
337+
FATAL_ERROR
338+
"When 'HEADER_FILE' is specified, remove redundant 'HEADER' option."
339+
)
340+
endif()
341+
342+
if(parsed_REPORT_FILE AND NOT parsed_VERBOSE)
343+
message(FATAL_ERROR "'REPORT_FILE' option requires also 'VERBOSE' option.")
344+
endif()
345+
237346
set(input ${ARGV1})
238347
if(NOT IS_ABSOLUTE "${input}")
239348
set(input ${CMAKE_CURRENT_SOURCE_DIR}/${input})
@@ -249,58 +358,8 @@ macro(_bison_process)
249358
set(outputs ${output})
250359

251360
_bison_process_options(parsed_OPTIONS options)
252-
253-
if(parsed_HEADER OR parsed_HEADER_FILE)
254-
# Bison versions 3.8 and later introduced the --header=[FILE] (-H) option.
255-
# For prior versions the --defines=[FILE] (-d) option can be used.
256-
if(parsed_HEADER_FILE)
257-
set(header ${parsed_HEADER_FILE})
258-
if(NOT IS_ABSOLUTE "${header}")
259-
set(header ${CMAKE_CURRENT_BINARY_DIR}/${header})
260-
endif()
261-
if(BISON_VERSION VERSION_LESS 3.8)
262-
list(APPEND options --defines=${header})
263-
else()
264-
list(APPEND options --header=${header})
265-
endif()
266-
else()
267-
if(BISON_VERSION VERSION_LESS 3.8)
268-
list(APPEND options -d)
269-
else()
270-
list(APPEND options --header)
271-
endif()
272-
273-
# Produce default header path generated by bison (see option --header)
274-
cmake_path(GET output EXTENSION LAST_ONLY extension)
275-
string(REPLACE "c" "h" extension "${extension}")
276-
if(NOT extension)
277-
set(extension "h")
278-
endif()
279-
cmake_path(
280-
REPLACE_EXTENSION
281-
output
282-
LAST_ONLY
283-
"${extension}"
284-
OUTPUT_VARIABLE header
285-
)
286-
# TODO: Add path if header is relative.
287-
endif()
288-
289-
list(APPEND outputs ${header})
290-
291-
if(parsed_VERBOSE)
292-
list(APPEND options --verbose)
293-
if(parsed_REPORT_FILE)
294-
if(NOT IS_ABSOLUTE "${parsed_REPORT_FILE}")
295-
set(
296-
parsed_REPORT_FILE
297-
${CMAKE_CURRENT_BINARY_DIR}/${parsed_REPORT_FILE}
298-
)
299-
endif()
300-
list(APPEND options --report-file=${parsed_REPORT_FILE})
301-
endif()
302-
endif()
303-
endif()
361+
_bison_process_header_option()
362+
_bison_process_verbose_option()
304363

305364
# Assemble commands for add_custom_command() and execute_process().
306365
set(commands "")
@@ -356,7 +415,7 @@ function(bison)
356415
PARSE_ARGV
357416
3
358417
parsed # prefix
359-
"NO_DEFAULT_OPTIONS;CODEGEN;VERBOSE;HEADER" # options
418+
"NO_DEFAULT_OPTIONS;CODEGEN;HEADER;VERBOSE" # options
360419
"HEADER_FILE;WORKING_DIRECTORY;REPORT_FILE" # one-value keywords
361420
"OPTIONS;DEPENDS" # multi-value keywords
362421
)
@@ -484,12 +543,12 @@ block(PROPAGATE BISON_VERSION _bisonVersionValid)
484543
endif()
485544
endblock()
486545

487-
set(_bisonRequiredVars "")
488-
489546
################################################################################
490547
# Download and build the package.
491548
################################################################################
492549

550+
set(_bisonRequiredVars "")
551+
493552
if(
494553
NOT CMAKE_SCRIPT_MODE_FILE
495554
AND NOT BISON_DISABLE_DOWNLOAD

cmake/cmake/modules/FindRE2C.cmake

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ endfunction()
247247

248248
macro(_re2c_process)
249249
if(parsed_UNPARSED_ARGUMENTS)
250-
message(FATAL_ERROR "Bad arguments: ${parsed_UNPARSED_ARGUMENTS}")
250+
message(FATAL_ERROR "Unrecognized arguments: ${parsed_UNPARSED_ARGUMENTS}")
251251
endif()
252252

253253
if(parsed_KEYWORDS_MISSING_VALUES)
@@ -470,12 +470,12 @@ block(PROPAGATE RE2C_VERSION _re2cVersionValid)
470470
endif()
471471
endblock()
472472

473-
set(_re2cRequiredVars "")
474-
475473
################################################################################
476474
# Download and build the package.
477475
################################################################################
478476

477+
set(_re2cRequiredVars "")
478+
479479
if(
480480
NOT CMAKE_SCRIPT_MODE_FILE
481481
AND NOT RE2C_DISABLE_DOWNLOAD

cmake/cmake/scripts/GenerateGrammar.cmake

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@
1313
# [-D BISON_EXECUTABLE=path/to/bison] \
1414
# [-D RE2C_EXECUTABLE=path/to/re2c] \
1515
# -P cmake/scripts/GenerateGrammar.cmake
16-
#
17-
# TODO: Should the Bison-generated report files (*.output) really be also
18-
# created by this script (the `VERBOSE REPORT_FILE <file>` options)? PHP still
19-
# packages these reports also in the archive release files?! Also, ext/json
20-
# doesn't produce the *.output file.
2116

2217
cmake_minimum_required(VERSION 3.25...3.31)
2318

cmake/ext/json/cmake/GenerateGrammar.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ if(BISON_FOUND)
1313
if(CMAKE_SCRIPT_MODE_FILE)
1414
set(verbose "")
1515
else()
16-
set(verbose VERBOSE REPORT_FILE json_parser.output)
16+
set(verbose VERBOSE) #REPORT_FILE json_parser.output)
1717
endif()
1818

1919
bison(

cmake/sapi/phpdbg/cmake/GenerateGrammar.cmake

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ if(BISON_FOUND)
1919
if(CMAKE_SCRIPT_MODE_FILE)
2020
set(verbose "")
2121
else()
22-
set(verbose VERBOSE REPORT_FILE phpdbg_parser.output)
22+
set(verbose VERBOSE)
2323
endif()
2424

2525
bison(

0 commit comments

Comments
 (0)