Skip to content

Commit ba15f0b

Browse files
committed
Refactor RE2C and BISON find modules
Find modules are not meant to provide functions. This is an edge bad practice learned from some existing modules out there for convenience. Find modules should ideally only deal with finding packages and providing the imported targets. Ideally, how the package is used should be done in some wrapper module, which provides these functions. Here, the php_bison() and php_re2c(). Also, downloading is moved to these modules for now.
1 parent bfcbddd commit ba15f0b

File tree

13 files changed

+1755
-1693
lines changed

13 files changed

+1755
-1693
lines changed
Lines changed: 115 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -1,145 +1,129 @@
11
# Generate lexer and parser files.
22

3-
include(FeatureSummary)
4-
include(PHP/Package/BISON)
5-
include(PHP/Package/RE2C)
3+
if(CMAKE_SCRIPT_MODE_FILE STREQUAL CMAKE_CURRENT_LIST_FILE)
4+
message(FATAL_ERROR "This file should be used with include().")
5+
endif()
66

7-
if(
8-
NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/zend_ini_parser.c
9-
OR NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/zend_ini_parser.h
10-
OR NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/zend_language_parser.c
11-
OR NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/zend_language_parser.h
12-
)
13-
set_package_properties(BISON PROPERTIES TYPE REQUIRED)
7+
include(PHP/Bison)
8+
9+
if(CMAKE_SCRIPT_MODE_FILE)
10+
set(verbose "")
11+
else()
12+
set(verbose VERBOSE)
1413
endif()
1514

16-
if(BISON_FOUND)
17-
if(CMAKE_SCRIPT_MODE_FILE)
18-
set(verbose "")
19-
else()
20-
set(verbose VERBOSE)
21-
endif()
15+
php_bison(
16+
zend_ini_parser
17+
zend_ini_parser.y
18+
${CMAKE_CURRENT_SOURCE_DIR}/zend_ini_parser.c
19+
HEADER
20+
${verbose}
21+
CODEGEN
22+
)
2223

23-
bison(
24-
zend_ini_parser
25-
zend_ini_parser.y
26-
${CMAKE_CURRENT_SOURCE_DIR}/zend_ini_parser.c
27-
HEADER
28-
${verbose}
29-
CODEGEN
30-
)
24+
php_bison(
25+
zend_language_parser
26+
zend_language_parser.y
27+
${CMAKE_CURRENT_SOURCE_DIR}/zend_language_parser.c
28+
HEADER
29+
${verbose}
30+
CODEGEN
31+
)
3132

32-
bison(
33-
zend_language_parser
34-
zend_language_parser.y
35-
${CMAKE_CURRENT_SOURCE_DIR}/zend_language_parser.c
36-
HEADER
37-
${verbose}
38-
CODEGEN
39-
)
33+
# Tweak zendparse to be exported through ZEND_API. This has to be revisited
34+
# once bison supports foreign skeletons and that bison version is used. Read
35+
# https://git.savannah.gnu.org/cgit/bison.git/tree/data/README.md for more.
36+
block()
37+
string(
38+
CONCAT patch
39+
"cmake_path(SET SOURCE_DIR NORMALIZE ${CMAKE_CURRENT_SOURCE_DIR})\n"
40+
[[
41+
cmake_path(
42+
RELATIVE_PATH
43+
SOURCE_DIR
44+
BASE_DIRECTORY ${CMAKE_SOURCE_DIR}
45+
OUTPUT_VARIABLE relativeDir
46+
)
47+
file(READ "${SOURCE_DIR}/zend_language_parser.h" content)
48+
string(
49+
REPLACE
50+
"int zendparse"
51+
"ZEND_API int zendparse"
52+
content_2
53+
"${content}"
54+
)
55+
if(
56+
NOT content MATCHES "ZEND_API int zendparse"
57+
AND NOT content STREQUAL "${content_2}"
58+
)
59+
message(STATUS "Patching ${relativeDir}/zend_language_parser.h")
60+
file(WRITE "${SOURCE_DIR}/zend_language_parser.h" "${content_2}")
61+
endif()
4062

41-
# Tweak zendparse to be exported through ZEND_API. This has to be revisited
42-
# once bison supports foreign skeletons and that bison version is used. Read
43-
# https://git.savannah.gnu.org/cgit/bison.git/tree/data/README.md for more.
44-
block()
63+
file(READ "${SOURCE_DIR}/zend_language_parser.c" content)
4564
string(
46-
CONCAT patch
47-
"cmake_path(SET SOURCE_DIR NORMALIZE ${CMAKE_CURRENT_SOURCE_DIR})\n"
48-
[[
49-
cmake_path(
50-
RELATIVE_PATH
51-
SOURCE_DIR
52-
BASE_DIRECTORY ${CMAKE_SOURCE_DIR}
53-
OUTPUT_VARIABLE relativeDir
54-
)
55-
file(READ "${SOURCE_DIR}/zend_language_parser.h" content)
56-
string(
57-
REPLACE
58-
"int zendparse"
59-
"ZEND_API int zendparse"
60-
content_2
61-
"${content}"
62-
)
63-
if(
64-
NOT content MATCHES "ZEND_API int zendparse"
65-
AND NOT content STREQUAL "${content_2}"
66-
)
67-
message(STATUS "Patching ${relativeDir}/zend_language_parser.h")
68-
file(WRITE "${SOURCE_DIR}/zend_language_parser.h" "${content_2}")
69-
endif()
65+
REPLACE
66+
"int zendparse"
67+
"ZEND_API int zendparse"
68+
content_2
69+
"${content}"
70+
)
71+
if(
72+
NOT content MATCHES "ZEND_API int zendparse"
73+
AND NOT content STREQUAL "${content_2}"
74+
)
75+
message(STATUS "Patching ${relativeDir}/zend_language_parser.c")
76+
file(WRITE "${SOURCE_DIR}/zend_language_parser.c" "${content_2}")
77+
endif()
78+
]])
7079

71-
file(READ "${SOURCE_DIR}/zend_language_parser.c" content)
72-
string(
73-
REPLACE
74-
"int zendparse"
75-
"ZEND_API int zendparse"
76-
content_2
77-
"${content}"
78-
)
79-
if(
80-
NOT content MATCHES "ZEND_API int zendparse"
81-
AND NOT content STREQUAL "${content_2}"
82-
)
83-
message(STATUS "Patching ${relativeDir}/zend_language_parser.c")
84-
file(WRITE "${SOURCE_DIR}/zend_language_parser.c" "${content_2}")
85-
endif()
86-
]])
80+
# Run patch based on whether building or running inside a CMake script.
81+
if(CMAKE_SCRIPT_MODE_FILE)
82+
cmake_language(EVAL CODE "${patch}")
83+
else()
84+
file(
85+
GENERATE
86+
OUTPUT CMakeFiles/Zend/PatchLanguageParser.cmake
87+
CONTENT "${patch}"
88+
)
89+
add_custom_target(
90+
zend_language_parser_patch
91+
COMMAND ${CMAKE_COMMAND} -P CMakeFiles/Zend/PatchLanguageParser.cmake
92+
DEPENDS zend_language_parser
93+
VERBATIM
94+
)
95+
add_dependencies(zend zend_language_parser_patch)
96+
endif()
97+
endblock()
8798

88-
# Run patch based on whether building or running inside a CMake script.
89-
if(CMAKE_SCRIPT_MODE_FILE)
90-
cmake_language(EVAL CODE "${patch}")
91-
else()
92-
file(
93-
GENERATE
94-
OUTPUT CMakeFiles/Zend/PatchLanguageParser.cmake
95-
CONTENT "${patch}"
96-
)
97-
add_custom_target(
98-
zend_language_parser_patch
99-
COMMAND ${CMAKE_COMMAND} -P CMakeFiles/Zend/PatchLanguageParser.cmake
100-
DEPENDS zend_language_parser
101-
VERBATIM
102-
)
103-
add_dependencies(zend zend_language_parser_patch)
104-
endif()
105-
endblock()
106-
endif()
99+
include(PHP/Re2c)
107100

108-
if(
109-
NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/zend_ini_scanner.c
110-
OR NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/zend_ini_scanner_defs.h
111-
OR NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/zend_language_scanner.c
112-
OR NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/zend_language_scanner_defs.h
101+
php_re2c(
102+
zend_ini_scanner
103+
zend_ini_scanner.l
104+
${CMAKE_CURRENT_SOURCE_DIR}/zend_ini_scanner.c
105+
HEADER ${CMAKE_CURRENT_SOURCE_DIR}/zend_ini_scanner_defs.h
106+
APPEND
107+
OPTIONS
108+
--bit-vectors
109+
--case-inverted
110+
--conditions
111+
--debug-output
112+
--flex-syntax
113+
CODEGEN
113114
)
114-
set_package_properties(RE2C PROPERTIES TYPE REQUIRED)
115-
endif()
116-
117-
if(RE2C_FOUND)
118-
re2c(
119-
zend_ini_scanner
120-
zend_ini_scanner.l
121-
${CMAKE_CURRENT_SOURCE_DIR}/zend_ini_scanner.c
122-
HEADER ${CMAKE_CURRENT_SOURCE_DIR}/zend_ini_scanner_defs.h
123-
OPTIONS
124-
--case-inverted
125-
--conditions
126-
--bit-vectors
127-
--debug-output
128-
--flex-syntax
129-
CODEGEN
130-
)
131115

132-
re2c(
133-
zend_language_scanner
134-
zend_language_scanner.l
135-
${CMAKE_CURRENT_SOURCE_DIR}/zend_language_scanner.c
136-
HEADER ${CMAKE_CURRENT_SOURCE_DIR}/zend_language_scanner_defs.h
137-
OPTIONS
138-
--case-inverted
139-
--conditions
140-
--bit-vectors
141-
--debug-output
142-
--flex-syntax
143-
CODEGEN
144-
)
145-
endif()
116+
php_re2c(
117+
zend_language_scanner
118+
zend_language_scanner.l
119+
${CMAKE_CURRENT_SOURCE_DIR}/zend_language_scanner.c
120+
HEADER ${CMAKE_CURRENT_SOURCE_DIR}/zend_language_scanner_defs.h
121+
APPEND
122+
OPTIONS
123+
--bit-vectors
124+
--case-inverted
125+
--conditions
126+
--debug-output
127+
--flex-syntax
128+
CODEGEN
129+
)

0 commit comments

Comments
 (0)