From 2be43107156a9d7ba40eaa5c3260c7a13326852d Mon Sep 17 00:00:00 2001 From: Peter Kokot Date: Sat, 28 Dec 2024 13:28:39 +0100 Subject: [PATCH] Improve FindBISON module --- cmake/Zend/cmake/GenerateGrammar.cmake | 8 +- cmake/cmake/modules/FindBISON.cmake | 171 ++++++++++++------ cmake/cmake/modules/FindRE2C.cmake | 6 +- cmake/cmake/scripts/GenerateGrammar.cmake | 5 - cmake/ext/json/cmake/GenerateGrammar.cmake | 2 +- cmake/sapi/phpdbg/cmake/GenerateGrammar.cmake | 2 +- patches/8.3/cmake.patch | 142 ++++++++++++++- patches/8.4/cmake.patch | 142 ++++++++++++++- patches/8.5/cmake.patch | 2 +- 9 files changed, 403 insertions(+), 77 deletions(-) diff --git a/cmake/Zend/cmake/GenerateGrammar.cmake b/cmake/Zend/cmake/GenerateGrammar.cmake index 5a26f142..a6fb16b9 100644 --- a/cmake/Zend/cmake/GenerateGrammar.cmake +++ b/cmake/Zend/cmake/GenerateGrammar.cmake @@ -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( @@ -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( @@ -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 ) diff --git a/cmake/cmake/modules/FindBISON.cmake b/cmake/cmake/modules/FindBISON.cmake index d8f77df5..67a93a94 100644 --- a/cmake/cmake/modules/FindBISON.cmake +++ b/cmake/cmake/modules/FindBISON.cmake @@ -79,6 +79,17 @@ in various scenarios. * `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 + `.output` containing verbose descriptions of the + grammar and parser. File will be created in the current binary directory. + +* `REPORT_FILE ` - This adds the `--report-file=` command-line + option to `bison` executable and will create verbose information report in the + specified ``. 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. @@ -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 .tab. + # differently. It removes the '.tab' part of the extension and creates + # .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}) @@ -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 "") @@ -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 ) @@ -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 diff --git a/cmake/cmake/modules/FindRE2C.cmake b/cmake/cmake/modules/FindRE2C.cmake index 007655a2..078dd6ff 100644 --- a/cmake/cmake/modules/FindRE2C.cmake +++ b/cmake/cmake/modules/FindRE2C.cmake @@ -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) @@ -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 diff --git a/cmake/cmake/scripts/GenerateGrammar.cmake b/cmake/cmake/scripts/GenerateGrammar.cmake index 41517ccd..6036e5f3 100755 --- a/cmake/cmake/scripts/GenerateGrammar.cmake +++ b/cmake/cmake/scripts/GenerateGrammar.cmake @@ -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 ` 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) diff --git a/cmake/ext/json/cmake/GenerateGrammar.cmake b/cmake/ext/json/cmake/GenerateGrammar.cmake index 4aef5c0e..e370159c 100644 --- a/cmake/ext/json/cmake/GenerateGrammar.cmake +++ b/cmake/ext/json/cmake/GenerateGrammar.cmake @@ -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( diff --git a/cmake/sapi/phpdbg/cmake/GenerateGrammar.cmake b/cmake/sapi/phpdbg/cmake/GenerateGrammar.cmake index ca85fb6a..92a023cd 100644 --- a/cmake/sapi/phpdbg/cmake/GenerateGrammar.cmake +++ b/cmake/sapi/phpdbg/cmake/GenerateGrammar.cmake @@ -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( diff --git a/patches/8.3/cmake.patch b/patches/8.3/cmake.patch index 22132cc7..162e434d 100644 --- a/patches/8.3/cmake.patch +++ b/patches/8.3/cmake.patch @@ -1,11 +1,147 @@ From: Peter Kokot Subject: Add CMake changes for PHP-8.3 branch -* CMake added to run-tests.php for info about failing tests and checks +- EditorConfig adjustments for code style support in editors and IDEs +- CMake build system files added to .gitignore +- CMake added to run-tests.php for info about failing tests and checks +- CMake-related modifications added to ext/skeleton template directory + and ext/ext_skel.php script --- - run-tests.php | 24 ++++++++++++++++++------ - 1 file changed, 18 insertions(+), 6 deletions(-) + .editorconfig | 2 +- + .gitignore | 50 ++++++++++++++++++++++++++++++++++++++ + ext/ext_skel.php | 2 ++ + ext/skeleton/.gitignore.in | 23 ++++++++++++++++++ + run-tests.php | 24 +++++++++++++----- + 5 files changed, 94 insertions(+), 7 deletions(-) +diff --git a/.editorconfig b/.editorconfig +index 7911bf8490..1d9b530947 100644 +--- a/.editorconfig ++++ b/.editorconfig +@@ -17,7 +17,7 @@ indent_style = tab + indent_size = 4 + indent_style = space + +-[*.{ac,m4,sh,yml}] ++[{CMakeLists.{txt,txt.in},*.{ac,cmake,cmake.in,json,m4,sh,yml}}] + indent_size = 2 + indent_style = space + +diff --git a/.gitignore b/.gitignore +index 449963153f..913ed3d96d 100644 +--- a/.gitignore ++++ b/.gitignore +@@ -153,6 +153,7 @@ php + # ------------------------------------------------------------------------------ + /ext/json/json_parser.tab.h + /ext/json/json_parser.tab.c ++/ext/json/json_parser.output + /sapi/phpdbg/phpdbg_parser.c + /sapi/phpdbg/phpdbg_parser.h + /sapi/phpdbg/phpdbg_parser.output +@@ -291,9 +292,58 @@ tmp-php.ini + /junit.out.xml + /.ccache/ + ++# ------------------------------------------------------------------------------ ++# CMake-based build system files ++# ------------------------------------------------------------------------------ ++cmake_install.cmake ++CMakeCache.txt ++CMakeFiles/ ++ ++# Generated by FetchContent ++/_deps/ ++ ++# Graphviz generated files ++/*.dependers ++/*.dot ++ ++# Generated by the Ninja build system ++/.ninja* ++/build.ninja ++ ++# Local user presets ++/CMakeUserPresets.json ++ ++# Generated when CMAKE_EXPORT_COMPILE_COMMANDS is enabled ++/compile-commands.json ++ ++# Generated by ctest ++/CTestTestfile.cmake ++/Testing/ ++ ++# CMake script profiling data output (--profiling-output ) ++/profile.json ++ ++# Generated by QT Creator ++CMakeLists.txt.user ++ ++# Generated by XCode ++CMakeScripts/ ++ ++# Generated by cmake --install ++/install_manifest.txt ++ ++# Generated by cmake when cross-compiling if missing cache variables are found ++/TryRunResults.cmake ++ ++# pkg-config .pc files ++/sapi/embed/php-embed.pc ++/scripts/php.pc ++ + # ------------------------------------------------------------------------------ + # Special cases to invert previous ignore patterns + # ------------------------------------------------------------------------------ ++!**/cmake/config.h.in ++!**/cmake/modules/ + !/ext/bcmath/libbcmath/src/config.h + !/ext/fileinfo/libmagic/config.h + !/ext/fileinfo/libmagic.patch +diff --git a/ext/ext_skel.php b/ext/ext_skel.php +index ae7a3a987c..a9e0dd1b51 100755 +--- a/ext/ext_skel.php ++++ b/ext/ext_skel.php +@@ -302,6 +302,8 @@ function copy_config_scripts() { + $files[] = 'config.w32'; + } + ++ $files[] = 'CMakeLists.txt'; ++ $files[] = 'cmake/config.h.in'; + $files[] = '.gitignore'; + + foreach($files as $config_script) { +diff --git a/ext/skeleton/.gitignore.in b/ext/skeleton/.gitignore.in +index ae434fef97..87545d9162 100644 +--- a/ext/skeleton/.gitignore.in ++++ b/ext/skeleton/.gitignore.in +@@ -39,3 +39,26 @@ tests/**/*.sh + tests/**/*.db + tests/**/*.mem + tmp-php.ini ++ ++# ------------------------------------------------------------------------------ ++# CMake-based build system files ++# ------------------------------------------------------------------------------ ++!**/cmake/config.h.in ++!**/cmake/modules/ ++/_deps/ ++/.ninja* ++/*.dependers ++/*.dot ++/build.ninja ++/CMakeUserPresets.json ++/compile-commands.json ++/CTestTestfile.cmake ++/install_manifest.txt ++/profile.json ++/Testing/ ++/TryRunResults.cmake ++cmake_install.cmake ++CMakeCache.txt ++CMakeFiles/ ++CMakeLists.txt.user ++CMakeScripts/ diff --git a/run-tests.php b/run-tests.php index d51ab99a20..9211cbb3e2 100755 --- a/run-tests.php diff --git a/patches/8.4/cmake.patch b/patches/8.4/cmake.patch index 6e3ff7a5..fe1d6d3f 100644 --- a/patches/8.4/cmake.patch +++ b/patches/8.4/cmake.patch @@ -1,11 +1,147 @@ From: Peter Kokot Subject: Add CMake changes for PHP-8.4 branch -* CMake added to run-tests.php for info about failing tests and checks +- EditorConfig adjustments for code style support in editors and IDEs +- CMake build system files added to .gitignore +- CMake added to run-tests.php for info about failing tests and checks +- CMake-related modifications added to ext/skeleton template directory + and ext/ext_skel.php script --- - run-tests.php | 24 ++++++++++++++++++------ - 1 file changed, 18 insertions(+), 6 deletions(-) + .editorconfig | 2 +- + .gitignore | 50 ++++++++++++++++++++++++++++++++++++++ + ext/ext_skel.php | 2 ++ + ext/skeleton/.gitignore.in | 23 ++++++++++++++++++ + run-tests.php | 24 +++++++++++++----- + 5 files changed, 94 insertions(+), 7 deletions(-) +diff --git a/.editorconfig b/.editorconfig +index 7911bf8490..1d9b530947 100644 +--- a/.editorconfig ++++ b/.editorconfig +@@ -17,7 +17,7 @@ indent_style = tab + indent_size = 4 + indent_style = space + +-[*.{ac,m4,sh,yml}] ++[{CMakeLists.{txt,txt.in},*.{ac,cmake,cmake.in,json,m4,sh,yml}}] + indent_size = 2 + indent_style = space + +diff --git a/.gitignore b/.gitignore +index 52dde98ed4..d698b5a19c 100644 +--- a/.gitignore ++++ b/.gitignore +@@ -152,6 +152,7 @@ php + # ------------------------------------------------------------------------------ + /ext/json/json_parser.tab.h + /ext/json/json_parser.tab.c ++/ext/json/json_parser.output + /sapi/phpdbg/phpdbg_parser.c + /sapi/phpdbg/phpdbg_parser.h + /sapi/phpdbg/phpdbg_parser.output +@@ -290,9 +291,58 @@ tmp-php.ini + /junit.out.xml + /.ccache/ + ++# ------------------------------------------------------------------------------ ++# CMake-based build system files ++# ------------------------------------------------------------------------------ ++cmake_install.cmake ++CMakeCache.txt ++CMakeFiles/ ++ ++# Generated by FetchContent ++/_deps/ ++ ++# Graphviz generated files ++/*.dependers ++/*.dot ++ ++# Generated by the Ninja build system ++/.ninja* ++/build.ninja ++ ++# Local user presets ++/CMakeUserPresets.json ++ ++# Generated when CMAKE_EXPORT_COMPILE_COMMANDS is enabled ++/compile-commands.json ++ ++# Generated by ctest ++/CTestTestfile.cmake ++/Testing/ ++ ++# CMake script profiling data output (--profiling-output ) ++/profile.json ++ ++# Generated by QT Creator ++CMakeLists.txt.user ++ ++# Generated by XCode ++CMakeScripts/ ++ ++# Generated by cmake --install ++/install_manifest.txt ++ ++# Generated by cmake when cross-compiling if missing cache variables are found ++/TryRunResults.cmake ++ ++# pkg-config .pc files ++/sapi/embed/php-embed.pc ++/scripts/php.pc ++ + # ------------------------------------------------------------------------------ + # Special cases to invert previous ignore patterns + # ------------------------------------------------------------------------------ ++!**/cmake/config.h.in ++!**/cmake/modules/ + !/ext/bcmath/libbcmath/src/config.h + !/ext/fileinfo/libmagic/config.h + !/ext/fileinfo/libmagic.patch +diff --git a/ext/ext_skel.php b/ext/ext_skel.php +index ae7a3a987c..a9e0dd1b51 100755 +--- a/ext/ext_skel.php ++++ b/ext/ext_skel.php +@@ -302,6 +302,8 @@ function copy_config_scripts() { + $files[] = 'config.w32'; + } + ++ $files[] = 'CMakeLists.txt'; ++ $files[] = 'cmake/config.h.in'; + $files[] = '.gitignore'; + + foreach($files as $config_script) { +diff --git a/ext/skeleton/.gitignore.in b/ext/skeleton/.gitignore.in +index e691bd3964..191fcd70eb 100644 +--- a/ext/skeleton/.gitignore.in ++++ b/ext/skeleton/.gitignore.in +@@ -45,3 +45,26 @@ tests/**/*.db + tests/**/*.mem + tmp-php.ini + *~ ++ ++# ------------------------------------------------------------------------------ ++# CMake-based build system files ++# ------------------------------------------------------------------------------ ++!**/cmake/config.h.in ++!**/cmake/modules/ ++/_deps/ ++/.ninja* ++/*.dependers ++/*.dot ++/build.ninja ++/CMakeUserPresets.json ++/compile-commands.json ++/CTestTestfile.cmake ++/install_manifest.txt ++/profile.json ++/Testing/ ++/TryRunResults.cmake ++cmake_install.cmake ++CMakeCache.txt ++CMakeFiles/ ++CMakeLists.txt.user ++CMakeScripts/ diff --git a/run-tests.php b/run-tests.php index 4654ead998..a36dbf0f24 100755 --- a/run-tests.php diff --git a/patches/8.5/cmake.patch b/patches/8.5/cmake.patch index 955b2e88..cc6c334b 100644 --- a/patches/8.5/cmake.patch +++ b/patches/8.5/cmake.patch @@ -143,7 +143,7 @@ index e691bd3964..191fcd70eb 100644 +CMakeLists.txt.user +CMakeScripts/ diff --git a/run-tests.php b/run-tests.php -index 9dffecca8d..902409f53b 100755 +index 0ff11c2c95..f64002f185 100755 --- a/run-tests.php +++ b/run-tests.php @@ -847,10 +847,12 @@ function write_information(array $user_tests, $phpdbg): void