Skip to content

Commit

Permalink
Add customized FindBISON module
Browse files Browse the repository at this point in the history
  • Loading branch information
petk committed Dec 28, 2024
1 parent 96fa396 commit 62ff762
Show file tree
Hide file tree
Showing 11 changed files with 806 additions and 383 deletions.
198 changes: 110 additions & 88 deletions cmake/Zend/cmake/GenerateGrammar.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -10,83 +10,103 @@ if(
endif()
include(PHP/BISON)

php_bison(
zend_ini_parser
zend_ini_parser.y
${CMAKE_CURRENT_SOURCE_DIR}/zend_ini_parser.c
COMPILE_FLAGS "${PHP_BISON_DEFAULT_OPTIONS}"
VERBOSE REPORT_FILE zend_ini_parser.output
DEFINES_FILE ${CMAKE_CURRENT_SOURCE_DIR}/zend_ini_parser.h
)

php_bison(
zend_language_parser
zend_language_parser.y
${CMAKE_CURRENT_SOURCE_DIR}/zend_language_parser.c
COMPILE_FLAGS "${PHP_BISON_DEFAULT_OPTIONS}"
VERBOSE REPORT_FILE zend_language_parser.output
DEFINES_FILE ${CMAKE_CURRENT_SOURCE_DIR}/zend_language_parser.h
)

# Tweak zendparse to be exported through ZEND_API. This has to be revisited
# once bison supports foreign skeletons and that bison version is used. Read
# https://git.savannah.gnu.org/cgit/bison.git/tree/data/README.md for more.
block()
string(
CONCAT patch
"set(SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})\n"
[[
file(READ "${SOURCE_DIR}/zend_language_parser.h" content)
string(
REPLACE
"int zendparse"
"ZEND_API int zendparse"
content_2
"${content}"
)
if(
NOT content MATCHES "ZEND_API int zendparse"
AND NOT content STREQUAL "${content_2}"
)
message(STATUS "[Zend] Patching zend_language_parser.h")
file(WRITE "${SOURCE_DIR}/zend_language_parser.h" "${content_2}")
endif()
if(BISON_FOUND)
if(CMAKE_SCRIPT_MODE_FILE)
set(verbose "")
else()
set(verbose VERBOSE REPORT_FILE zend_ini_parser.output)
endif()

file(READ "${SOURCE_DIR}/zend_language_parser.c" content)
string(
REPLACE
"int zendparse"
"ZEND_API int zendparse"
content_2
"${content}"
)
if(
NOT content MATCHES "ZEND_API int zendparse"
AND NOT content STREQUAL "${content_2}"
)
message(STATUS "[Zend] Patching zend_language_parser.c")
file(WRITE "${SOURCE_DIR}/zend_language_parser.c" "${content_2}")
endif()
]])
bison(
zend_ini_parser
zend_ini_parser.y
${CMAKE_CURRENT_SOURCE_DIR}/zend_ini_parser.c
HEADER
#HEADER_FILE ${CMAKE_CURRENT_SOURCE_DIR}/zend_ini_parser.h
${verbose}
)

# Run patch based on whether building or running inside a CMake script.
if(CMAKE_SCRIPT_MODE_FILE)
cmake_language(EVAL CODE "${patch}")
set(verbose "")
else()
file(
GENERATE
OUTPUT CMakeFiles/PatchLanguageParser.cmake
CONTENT "${patch}"
)
add_custom_target(
zend_patch_language_parser
COMMAND ${CMAKE_COMMAND} -P CMakeFiles/PatchLanguageParser.cmake
DEPENDS ${BISON_zend_language_parser_OUTPUTS}
VERBATIM
)
add_dependencies(zend zend_patch_language_parser)
set(verbose VERBOSE REPORT_FILE zend_language_parser.output)
endif()
endblock()

bison(
zend_language_parser
zend_language_parser.y
${CMAKE_CURRENT_SOURCE_DIR}/zend_language_parser.c
HEADER
#HEADER_FILE ${CMAKE_CURRENT_SOURCE_DIR}/zend_language_parser.h
${verbose}
)

# Tweak zendparse to be exported through ZEND_API. This has to be revisited
# once bison supports foreign skeletons and that bison version is used. Read
# https://git.savannah.gnu.org/cgit/bison.git/tree/data/README.md for more.
block()
string(
CONCAT patch
"cmake_path(SET SOURCE_DIR NORMALIZE ${CMAKE_CURRENT_SOURCE_DIR})\n"
[[
cmake_path(
RELATIVE_PATH
SOURCE_DIR
BASE_DIRECTORY ${CMAKE_SOURCE_DIR}
OUTPUT_VARIABLE relativeDir
)
file(READ "${SOURCE_DIR}/zend_language_parser.h" content)
string(
REPLACE
"int zendparse"
"ZEND_API int zendparse"
content_2
"${content}"
)
if(
NOT content MATCHES "ZEND_API int zendparse"
AND NOT content STREQUAL "${content_2}"
)
message(STATUS "Patching ${relativeDir}/zend_language_parser.h")
file(WRITE "${SOURCE_DIR}/zend_language_parser.h" "${content_2}")
endif()

file(READ "${SOURCE_DIR}/zend_language_parser.c" content)
string(
REPLACE
"int zendparse"
"ZEND_API int zendparse"
content_2
"${content}"
)
if(
NOT content MATCHES "ZEND_API int zendparse"
AND NOT content STREQUAL "${content_2}"
)
message(STATUS "Patching ${relativeDir}/zend_language_parser.c")
file(WRITE "${SOURCE_DIR}/zend_language_parser.c" "${content_2}")
endif()
]])

# Run patch based on whether building or running inside a CMake script.
if(CMAKE_SCRIPT_MODE_FILE)
cmake_language(EVAL CODE "${patch}")
else()
file(
GENERATE
OUTPUT CMakeFiles/PatchLanguageParser.cmake
CONTENT "${patch}"
)
add_custom_target(
zend_language_parser_patch
COMMAND ${CMAKE_COMMAND} -P CMakeFiles/PatchLanguageParser.cmake
DEPENDS zend_language_parser
VERBATIM
)
add_dependencies(zend zend_language_parser_patch)
endif()
endblock()
endif()

if(
EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/zend_ini_scanner.c
Expand All @@ -98,20 +118,22 @@ if(
endif()
include(PHP/RE2C)

php_re2c(
zend_ini_scanner
zend_ini_scanner.l
${CMAKE_CURRENT_SOURCE_DIR}/zend_ini_scanner.c
HEADER ${CMAKE_CURRENT_SOURCE_DIR}/zend_ini_scanner_defs.h
OPTIONS --case-inverted -cbdF
CODEGEN
)
if(RE2C_FOUND)
re2c(
zend_ini_scanner
zend_ini_scanner.l
${CMAKE_CURRENT_SOURCE_DIR}/zend_ini_scanner.c
HEADER ${CMAKE_CURRENT_SOURCE_DIR}/zend_ini_scanner_defs.h
OPTIONS --case-inverted -cbdF
CODEGEN
)

php_re2c(
zend_language_scanner
zend_language_scanner.l
${CMAKE_CURRENT_SOURCE_DIR}/zend_language_scanner.c
HEADER ${CMAKE_CURRENT_SOURCE_DIR}/zend_language_scanner_defs.h
OPTIONS --case-inverted -cbdF
CODEGEN
)
re2c(
zend_language_scanner
zend_language_scanner.l
${CMAKE_CURRENT_SOURCE_DIR}/zend_language_scanner.c
HEADER ${CMAKE_CURRENT_SOURCE_DIR}/zend_language_scanner_defs.h
OPTIONS --case-inverted -cbdF
CODEGEN
)
endif()
Loading

0 comments on commit 62ff762

Please sign in to comment.