Skip to content

Commit f200f67

Browse files
committed
Add JIT as a subdirectory
JIT directory can be nicely wrapped as a simple OBJECT library. In this case everything seems to propagate properly forward.
1 parent ddc368d commit f200f67

File tree

2 files changed

+173
-147
lines changed

2 files changed

+173
-147
lines changed

cmake/ext/opcache/CMakeLists.txt

Lines changed: 21 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,8 @@ include(CheckSymbolExists)
4343
include(CMakeDependentOption)
4444
include(FeatureSummary)
4545
include(PHP/CheckCompilerFlag)
46-
include(PHP/SearchLibraries)
4746

4847
option(EXT_OPCACHE "Enable the opcache extension" ON)
49-
5048
add_feature_info(
5149
"ext/opcache"
5250
EXT_OPCACHE
@@ -65,21 +63,15 @@ cmake_dependent_option(
6563
EXT_OPCACHE_JIT
6664
"Enable JIT"
6765
ON
68-
"EXT_OPCACHE"
66+
EXT_OPCACHE
6967
OFF
7068
)
7169

72-
add_feature_info(
73-
"ext/opcache JIT"
74-
EXT_OPCACHE_JIT
75-
"Opcache's JIT (Just-In-Time compiler)"
76-
)
77-
7870
cmake_dependent_option(
7971
EXT_OPCACHE_CAPSTONE
8072
"Support opcache JIT disassembly through Capstone"
8173
OFF
82-
"EXT_OPCACHE"
74+
EXT_OPCACHE
8375
OFF
8476
)
8577

@@ -132,15 +124,6 @@ set_target_properties(
132124
PHP_ZEND_EXTENSION TRUE
133125
)
134126

135-
php_check_compiler_flag(
136-
C
137-
-Wno-implicit-fallthrough
138-
_HAVE_WNO_IMPLICIT_FALLTHROUGH_C
139-
)
140-
if(_HAVE_WNO_IMPLICIT_FALLTHROUGH_C)
141-
target_compile_options(php_opcache PRIVATE -Wno-implicit-fallthrough)
142-
endif()
143-
144127
target_compile_definitions(php_opcache PRIVATE ZEND_ENABLE_STATIC_TSRMLS_CACHE=1)
145128

146129
if(EXT_OPCACHE_HUGE_CODE_PAGES)
@@ -151,142 +134,33 @@ endif()
151134
# JIT.
152135
################################################################################
153136

154-
# Check JIT requirements.
155137
if(EXT_OPCACHE_JIT)
156-
if(
157-
# *nix:
158-
NOT CMAKE_SYSTEM_PROCESSOR MATCHES "^(i[34567]86.*|x86.*|amd64|aarch64.*)$"
159-
# Windows:
160-
AND NOT CMAKE_SYSTEM_PROCESSOR MATCHES "^(X86|AMD64|ARM64)$"
161-
)
162-
message(
163-
WARNING
164-
"JIT is not supported by target architecture ${CMAKE_SYSTEM_PROCESSOR}"
165-
)
166-
set_property(CACHE EXT_OPCACHE_JIT PROPERTY VALUE 0)
167-
elseif(
168-
CMAKE_SYSTEM_NAME STREQUAL "Darwin"
169-
AND CMAKE_SYSTEM_PROCESSOR MATCHES "^aarch64.*$"
170-
AND PHP_THREAD_SAFETY
171-
)
172-
message(
173-
WARNING
174-
"JIT is not supported on Apple Silicon with thread safety enabled"
175-
)
176-
set_property(CACHE EXT_OPCACHE_JIT PROPERTY VALUE 0)
177-
endif()
138+
add_subdirectory(jit)
178139
endif()
179140

180-
if(EXT_OPCACHE_JIT)
181-
set(HAVE_JIT 1)
182-
183-
target_sources(
184-
php_opcache
185-
PRIVATE
186-
$<$<NOT:$<PLATFORM_ID:Windows>>:jit/zend_jit_gdb.c>
187-
jit/zend_jit_vm_helpers.c
188-
jit/zend_jit.c
189-
)
190-
191-
# The string.h header is always available with C89 standard. The bundled
192-
# ext/opcache/jit/libudis86 still includes it conditionally.
193-
target_compile_definitions(php_opcache PRIVATE HAVE_STRING_H=1)
194-
195-
# Check for Capstone.
196-
if(EXT_OPCACHE_CAPSTONE)
197-
find_package(Capstone 3.0.0)
198-
set_package_properties(
199-
Capstone
200-
PROPERTIES
201-
TYPE REQUIRED
202-
PURPOSE "Necessary to enable OPcache JIT disassembly through Capstone."
203-
)
204-
205-
target_link_libraries(php_opcache PRIVATE Capstone::Capstone)
206-
207-
set(HAVE_CAPSTONE 1)
208-
endif()
209-
210-
# Find out which ABI to use.
211-
if(CMAKE_SYSTEM_PROCESSOR MATCHES "^(x86_64|amd64|AMD64|ARM64)$")
212-
set(DASM_FLAGS -D X64=1)
213-
set(DASM_ARCH "x86")
214-
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(i[34567]86.*|x86.*|X86)$")
215-
set(DASM_ARCH "x86")
216-
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^aarch64.*")
217-
set(DASM_FLAGS -D ARM64=1)
218-
set(DASM_ARCH "arm64")
219-
endif()
141+
add_feature_info(
142+
"ext/opcache JIT"
143+
[[HAVE_JIT]]
144+
"Opcache's JIT (Just-In-Time compiler)"
145+
)
220146

221-
if(
222-
CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64"
223-
AND CMAKE_SYSTEM_NAME STREQUAL "Darwin"
224-
)
225-
list(APPEND DASM_FLAGS -D X64APPLE=1)
226-
endif()
147+
add_feature_info(
148+
"ext/opcache JIT Capstone"
149+
[[HAVE_CAPSTONE]]
150+
"opcache JIT disassembly supported through Capstone"
151+
)
227152

228-
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
229-
list(APPEND DASM_FLAGS -D WIN=1)
230-
endif()
153+
if(HAVE_JIT)
154+
target_link_libraries(php_opcache PRIVATE php_opcache_jit)
231155

232-
if(
233-
CMAKE_SYSTEM_PROCESSOR MATCHES "^(AMD64|ARM64)$"
234-
AND CMAKE_SYSTEM_NAME STREQUAL "Windows"
156+
php_check_compiler_flag(
157+
C
158+
-Wno-implicit-fallthrough
159+
_HAVE_WNO_IMPLICIT_FALLTHROUGH_C
235160
)
236-
list(APPEND DASM_FLAGS -D X64WIN=1)
161+
if(_HAVE_WNO_IMPLICIT_FALLTHROUGH_C)
162+
target_compile_options(php_opcache PUBLIC -Wno-implicit-fallthrough)
237163
endif()
238-
239-
if(PHP_THREAD_SAFETY)
240-
list(APPEND DASM_FLAGS -D ZTS=1)
241-
endif()
242-
243-
add_executable(
244-
php_opcache_jit_minilua
245-
jit/dynasm/minilua.c
246-
)
247-
set_target_properties(
248-
php_opcache_jit_minilua
249-
PROPERTIES
250-
OUTPUT_NAME minilua
251-
)
252-
253-
# Link math library as needed.
254-
php_search_libraries(
255-
floor
256-
_HAVE_FLOOR
257-
HEADERS math.h
258-
LIBRARIES m
259-
TARGET php_opcache_jit_minilua PRIVATE
260-
)
261-
262-
# Create jit directory in the current build directory if it doesn't exist yet.
263-
add_custom_command(
264-
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/jit
265-
COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/jit
266-
COMMENT "[ext/opcache] Creating ext/opcache/jit directory"
267-
)
268-
269-
# Generate Jit for architecture.
270-
add_custom_command(
271-
OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/jit/zend_jit_${DASM_ARCH}.c
272-
COMMAND
273-
php_opcache_jit_minilua ${CMAKE_CURRENT_LIST_DIR}/jit/dynasm/dynasm.lua
274-
${DASM_FLAGS}
275-
-o ${CMAKE_CURRENT_BINARY_DIR}/jit/zend_jit_${DASM_ARCH}.c
276-
${CMAKE_CURRENT_LIST_DIR}/jit/zend_jit_${DASM_ARCH}.dasc
277-
DEPENDS
278-
php_opcache_jit_minilua
279-
${CMAKE_CURRENT_BINARY_DIR}/jit
280-
COMMENT "[ext/opcache] Generating ext/opcache/jit/zend_jit_${DASM_ARCH}.c"
281-
)
282-
283-
add_custom_target(
284-
php_opcache_jit
285-
DEPENDS ${CMAKE_CURRENT_BINARY_DIR}/jit/zend_jit_${DASM_ARCH}.c
286-
COMMENT "[ext/opcache] Building JIT for architecture ${DASM_ARCH}"
287-
)
288-
289-
add_dependencies(php_opcache php_opcache_jit)
290164
endif()
291165

292166
################################################################################
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
################################################################################
2+
# Check if JIT is supported by the target architecture.
3+
################################################################################
4+
5+
if(
6+
# *nix:
7+
NOT CMAKE_SYSTEM_PROCESSOR MATCHES "^(i[34567]86.*|x86.*|amd64|aarch64.*)$"
8+
# Windows:
9+
AND NOT CMAKE_SYSTEM_PROCESSOR MATCHES "^(X86|AMD64|ARM64)$"
10+
)
11+
message(
12+
WARNING
13+
"JIT is not supported by target architecture ${CMAKE_SYSTEM_PROCESSOR}"
14+
)
15+
return()
16+
elseif(
17+
CMAKE_SYSTEM_NAME STREQUAL "Darwin"
18+
AND CMAKE_SYSTEM_PROCESSOR MATCHES "^aarch64.*$"
19+
AND PHP_THREAD_SAFETY
20+
)
21+
message(
22+
WARNING
23+
"JIT is not supported on Apple Silicon with thread safety enabled"
24+
)
25+
return()
26+
endif()
27+
28+
################################################################################
29+
# Find out which ABI to use.
30+
################################################################################
31+
32+
if(CMAKE_SYSTEM_PROCESSOR MATCHES "^(x86_64|amd64|AMD64|ARM64)$")
33+
set(DASM_FLAGS -D X64=1)
34+
set(DASM_ARCH "x86")
35+
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^(i[34567]86.*|x86.*|X86)$")
36+
set(DASM_ARCH "x86")
37+
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "^aarch64.*")
38+
set(DASM_FLAGS -D ARM64=1)
39+
set(DASM_ARCH "arm64")
40+
endif()
41+
42+
if(
43+
CMAKE_SYSTEM_PROCESSOR STREQUAL "x86_64"
44+
AND CMAKE_SYSTEM_NAME STREQUAL "Darwin"
45+
)
46+
list(APPEND DASM_FLAGS -D X64APPLE=1)
47+
endif()
48+
49+
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
50+
list(APPEND DASM_FLAGS -D WIN=1)
51+
endif()
52+
53+
if(
54+
CMAKE_SYSTEM_PROCESSOR MATCHES "^(AMD64|ARM64)$"
55+
AND CMAKE_SYSTEM_NAME STREQUAL "Windows"
56+
)
57+
list(APPEND DASM_FLAGS -D X64WIN=1)
58+
endif()
59+
60+
if(PHP_THREAD_SAFETY)
61+
list(APPEND DASM_FLAGS -D ZTS=1)
62+
endif()
63+
64+
################################################################################
65+
# Generate zend_jit_<arch>.c file.
66+
################################################################################
67+
68+
add_executable(php_opcache_jit_minilua dynasm/minilua.c)
69+
set_target_properties(
70+
php_opcache_jit_minilua
71+
PROPERTIES RUNTIME_OUTPUT_DIRECTORY CMakeFiles
72+
)
73+
74+
# Link math library as needed.
75+
include(PHP/SearchLibraries)
76+
php_search_libraries(
77+
floor
78+
_HAVE_FLOOR
79+
HEADERS math.h
80+
LIBRARIES m
81+
TARGET php_opcache_jit_minilua PRIVATE
82+
)
83+
84+
# Generate Jit for architecture.
85+
add_custom_command(
86+
OUTPUT zend_jit_${DASM_ARCH}.c
87+
COMMAND
88+
php_opcache_jit_minilua ${CMAKE_CURRENT_SOURCE_DIR}/dynasm/dynasm.lua
89+
${DASM_FLAGS}
90+
-o ${CMAKE_CURRENT_BINARY_DIR}/zend_jit_${DASM_ARCH}.c
91+
${CMAKE_CURRENT_SOURCE_DIR}/zend_jit_${DASM_ARCH}.dasc
92+
COMMENT "[ext/opcache/jit] Generating ext/opcache/jit/zend_jit_${DASM_ARCH}.c"
93+
)
94+
95+
################################################################################
96+
# Add library.
97+
################################################################################
98+
99+
add_library(php_opcache_jit OBJECT)
100+
101+
target_sources(
102+
php_opcache_jit
103+
PRIVATE
104+
$<$<NOT:$<PLATFORM_ID:Windows>>:zend_jit_gdb.c>
105+
zend_jit_vm_helpers.c
106+
zend_jit.c
107+
${CMAKE_CURRENT_BINARY_DIR}/zend_jit_${DASM_ARCH}.c
108+
)
109+
110+
# Mark generated file as "header" to not get compiled into object at this level.
111+
set_source_files_properties(
112+
${CMAKE_CURRENT_BINARY_DIR}/zend_jit_${DASM_ARCH}.c
113+
PROPERTIES HEADER_FILE_ONLY ON
114+
)
115+
116+
# The string.h header is always available by C89 standard. The bundled libudis86
117+
# still includes it conditionally.
118+
target_compile_definitions(php_opcache_jit PRIVATE HAVE_STRING_H=1)
119+
120+
target_link_libraries(php_opcache_jit PRIVATE PHP::configuration)
121+
122+
target_include_directories(
123+
php_opcache_jit
124+
PRIVATE
125+
${CMAKE_CURRENT_SOURCE_DIR}/..
126+
${CMAKE_CURRENT_BINARY_DIR}/..
127+
)
128+
129+
set_target_properties(php_opcache_jit PROPERTIES POSITION_INDEPENDENT_CODE ON)
130+
131+
################################################################################
132+
# Check for Capstone.
133+
################################################################################
134+
135+
if(EXT_OPCACHE_CAPSTONE)
136+
find_package(Capstone 3.0.0)
137+
include(FeatureSummary)
138+
set_package_properties(
139+
Capstone
140+
PROPERTIES
141+
TYPE REQUIRED
142+
PURPOSE "Necessary to enable OPcache JIT disassembly through Capstone."
143+
)
144+
145+
target_link_libraries(php_opcache_jit PRIVATE Capstone::Capstone)
146+
147+
set(HAVE_CAPSTONE 1)
148+
endif()
149+
150+
set(HAVE_JIT 1)
151+
152+
return(PROPAGATE HAVE_JIT HAVE_CAPSTONE)

0 commit comments

Comments
 (0)