Skip to content

Commit

Permalink
Improve FindBISON module
Browse files Browse the repository at this point in the history
  • Loading branch information
petk committed Dec 29, 2024
1 parent e15979d commit 2be4310
Show file tree
Hide file tree
Showing 9 changed files with 403 additions and 77 deletions.
8 changes: 4 additions & 4 deletions cmake/Zend/cmake/GenerateGrammar.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ if(BISON_FOUND)
if(CMAKE_SCRIPT_MODE_FILE)
set(verbose "")
else()
set(verbose VERBOSE REPORT_FILE zend_ini_parser.output)
set(verbose VERBOSE)
endif()

bison(
Expand All @@ -29,7 +29,7 @@ if(BISON_FOUND)
if(CMAKE_SCRIPT_MODE_FILE)
set(verbose "")
else()
set(verbose VERBOSE REPORT_FILE zend_language_parser.output)
set(verbose VERBOSE)
endif()

bison(
Expand Down Expand Up @@ -94,12 +94,12 @@ if(BISON_FOUND)
else()
file(
GENERATE
OUTPUT CMakeFiles/PatchLanguageParser.cmake
OUTPUT CMakeFiles/Zend/PatchLanguageParser.cmake
CONTENT "${patch}"
)
add_custom_target(
zend_language_parser_patch
COMMAND ${CMAKE_COMMAND} -P CMakeFiles/PatchLanguageParser.cmake
COMMAND ${CMAKE_COMMAND} -P CMakeFiles/Zend/PatchLanguageParser.cmake
DEPENDS zend_language_parser
VERBATIM
)
Expand Down
171 changes: 115 additions & 56 deletions cmake/cmake/modules/FindBISON.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ in various scenarios.
* `DEPENDS <depends>...` - Optional list of dependent files to regenerate the
output file.
* `VERBOSE` - This adds the `--verbose` (`-v`) command-line option to
`bison` executable and will create extra output file
`<parser-output-filename>.output` containing verbose descriptions of the
grammar and parser. File will be created in the current binary directory.
* `REPORT_FILE <file>` - This adds the `--report-file=<file>` command-line
option to `bison` executable and will create verbose information report in the
specified `<file>`. This option must be used together with the `VERBOSE`
option. Relative file path is interpreted as being relative to the current
binary directory.
* `NO_DEFAULT_OPTIONS` - If specified, the `BISON_DEFAULT_OPTIONS` are not added
to the current `bison` invocation.
Expand Down Expand Up @@ -225,15 +236,113 @@ function(_bison_process_options options result)
return(PROPAGATE ${result})
endfunction()

# Process HEADER and HEADER_FILE options.
function(_bison_process_header_option)
if(NOT parsed_HEADER AND NOT parsed_HEADER_FILE)
return()
endif()

# Bison versions 3.8 and later introduced the --header=[FILE] (-H) option.
# For prior versions the --defines=[FILE] (-d) option can be used.
if(parsed_HEADER_FILE)
set(header ${parsed_HEADER_FILE})
if(NOT IS_ABSOLUTE "${header}")
set(header ${CMAKE_CURRENT_BINARY_DIR}/${header})
endif()

if(BISON_VERSION VERSION_LESS 3.8)
list(APPEND options --defines=${header})
else()
list(APPEND options --header=${header})
endif()
else()
if(BISON_VERSION VERSION_LESS 3.8)
list(APPEND options -d)
else()
list(APPEND options --header)
endif()

# Produce default header path generated by bison (see option --header).
cmake_path(GET output EXTENSION LAST_ONLY extension)
string(REPLACE "c" "h" extension "${extension}")
if(NOT extension)
set(extension ".h")
endif()
cmake_path(
REPLACE_EXTENSION
output
LAST_ONLY
"${extension}"
OUTPUT_VARIABLE header
)
# TODO: Add path if header is relative.
endif()

list(APPEND outputs ${header})

return(PROPAGATE outputs options)
endfunction()

# Process the VERBOSE and REPORT_FILE options.
function(_bison_process_verbose_option)
if(NOT parsed_VERBOSE)
return()
endif()

list(APPEND options --verbose)

if(NOT parsed_REPORT_FILE)
cmake_path(GET output FILENAME reportFile)
cmake_path(GET output EXTENSION extension)

# Bison treats output files <parser-output-filename>.tab.<last-extension>
# differently. It removes the '.tab' part of the extension and creates
# <parser-output-filename>.output file. Elsewhere, it replaces only the
# last extension with '.output'.
if(extension MATCHES "\\.tab\\.([^.]+)$")
string(
REGEX REPLACE
"\\.tab\\.${CMAKE_MATCH_1}$"
".output"
reportFile
"${reportFile}"
)
else()
cmake_path(REPLACE_EXTENSION reportFile LAST_ONLY "output")
endif()
else()
set(reportFile ${parsed_REPORT_FILE})
endif()

if(NOT IS_ABSOLUTE "${reportFile}")
set(reportFile ${CMAKE_CURRENT_BINARY_DIR}/${reportFile})
endif()

list(APPEND options --report-file=${reportFile})

return(PROPAGATE options)
endfunction()

macro(_bison_process)
if(parsed_UNPARSED_ARGUMENTS)
message(FATAL_ERROR "Bad arguments: ${parsed_UNPARSED_ARGUMENTS}")
message(FATAL_ERROR "Unrecognized arguments: ${parsed_UNPARSED_ARGUMENTS}")
endif()

if(parsed_KEYWORDS_MISSING_VALUES)
message(FATAL_ERROR "Missing values for: ${parsed_KEYWORDS_MISSING_VALUES}")
endif()

if(parsed_HEADER AND parsed_HEADER_FILE)
message(
FATAL_ERROR
"When 'HEADER_FILE' is specified, remove redundant 'HEADER' option."
)
endif()

if(parsed_REPORT_FILE AND NOT parsed_VERBOSE)
message(FATAL_ERROR "'REPORT_FILE' option requires also 'VERBOSE' option.")
endif()

set(input ${ARGV1})
if(NOT IS_ABSOLUTE "${input}")
set(input ${CMAKE_CURRENT_SOURCE_DIR}/${input})
Expand All @@ -249,58 +358,8 @@ macro(_bison_process)
set(outputs ${output})

_bison_process_options(parsed_OPTIONS options)

if(parsed_HEADER OR parsed_HEADER_FILE)
# Bison versions 3.8 and later introduced the --header=[FILE] (-H) option.
# For prior versions the --defines=[FILE] (-d) option can be used.
if(parsed_HEADER_FILE)
set(header ${parsed_HEADER_FILE})
if(NOT IS_ABSOLUTE "${header}")
set(header ${CMAKE_CURRENT_BINARY_DIR}/${header})
endif()
if(BISON_VERSION VERSION_LESS 3.8)
list(APPEND options --defines=${header})
else()
list(APPEND options --header=${header})
endif()
else()
if(BISON_VERSION VERSION_LESS 3.8)
list(APPEND options -d)
else()
list(APPEND options --header)
endif()

# Produce default header path generated by bison (see option --header)
cmake_path(GET output EXTENSION LAST_ONLY extension)
string(REPLACE "c" "h" extension "${extension}")
if(NOT extension)
set(extension "h")
endif()
cmake_path(
REPLACE_EXTENSION
output
LAST_ONLY
"${extension}"
OUTPUT_VARIABLE header
)
# TODO: Add path if header is relative.
endif()

list(APPEND outputs ${header})

if(parsed_VERBOSE)
list(APPEND options --verbose)
if(parsed_REPORT_FILE)
if(NOT IS_ABSOLUTE "${parsed_REPORT_FILE}")
set(
parsed_REPORT_FILE
${CMAKE_CURRENT_BINARY_DIR}/${parsed_REPORT_FILE}
)
endif()
list(APPEND options --report-file=${parsed_REPORT_FILE})
endif()
endif()
endif()
_bison_process_header_option()
_bison_process_verbose_option()

# Assemble commands for add_custom_command() and execute_process().
set(commands "")
Expand Down Expand Up @@ -356,7 +415,7 @@ function(bison)
PARSE_ARGV
3
parsed # prefix
"NO_DEFAULT_OPTIONS;CODEGEN;VERBOSE;HEADER" # options
"NO_DEFAULT_OPTIONS;CODEGEN;HEADER;VERBOSE" # options
"HEADER_FILE;WORKING_DIRECTORY;REPORT_FILE" # one-value keywords
"OPTIONS;DEPENDS" # multi-value keywords
)
Expand Down Expand Up @@ -484,12 +543,12 @@ block(PROPAGATE BISON_VERSION _bisonVersionValid)
endif()
endblock()

set(_bisonRequiredVars "")

################################################################################
# Download and build the package.
################################################################################

set(_bisonRequiredVars "")

if(
NOT CMAKE_SCRIPT_MODE_FILE
AND NOT BISON_DISABLE_DOWNLOAD
Expand Down
6 changes: 3 additions & 3 deletions cmake/cmake/modules/FindRE2C.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ endfunction()

macro(_re2c_process)
if(parsed_UNPARSED_ARGUMENTS)
message(FATAL_ERROR "Bad arguments: ${parsed_UNPARSED_ARGUMENTS}")
message(FATAL_ERROR "Unrecognized arguments: ${parsed_UNPARSED_ARGUMENTS}")
endif()

if(parsed_KEYWORDS_MISSING_VALUES)
Expand Down Expand Up @@ -470,12 +470,12 @@ block(PROPAGATE RE2C_VERSION _re2cVersionValid)
endif()
endblock()

set(_re2cRequiredVars "")

################################################################################
# Download and build the package.
################################################################################

set(_re2cRequiredVars "")

if(
NOT CMAKE_SCRIPT_MODE_FILE
AND NOT RE2C_DISABLE_DOWNLOAD
Expand Down
5 changes: 0 additions & 5 deletions cmake/cmake/scripts/GenerateGrammar.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@
# [-D BISON_EXECUTABLE=path/to/bison] \
# [-D RE2C_EXECUTABLE=path/to/re2c] \
# -P cmake/scripts/GenerateGrammar.cmake
#
# TODO: Should the Bison-generated report files (*.output) really be also
# created by this script (the `VERBOSE REPORT_FILE <file>` options)? PHP still
# packages these reports also in the archive release files?! Also, ext/json
# doesn't produce the *.output file.

cmake_minimum_required(VERSION 3.25...3.31)

Expand Down
2 changes: 1 addition & 1 deletion cmake/ext/json/cmake/GenerateGrammar.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ if(BISON_FOUND)
if(CMAKE_SCRIPT_MODE_FILE)
set(verbose "")
else()
set(verbose VERBOSE REPORT_FILE json_parser.output)
set(verbose VERBOSE) #REPORT_FILE json_parser.output)
endif()

bison(
Expand Down
2 changes: 1 addition & 1 deletion cmake/sapi/phpdbg/cmake/GenerateGrammar.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ if(BISON_FOUND)
if(CMAKE_SCRIPT_MODE_FILE)
set(verbose "")
else()
set(verbose VERBOSE REPORT_FILE phpdbg_parser.output)
set(verbose VERBOSE)
endif()

bison(
Expand Down
Loading

0 comments on commit 2be4310

Please sign in to comment.