Skip to content

Commit 2f23460

Browse files
committed
Add shared SAPIs and enable position independent code globally
Changes: * Position independent code is for now unconditionally enabled globally * Apache PHP module output filename named to mod_php * Added phpdbg pkg-config file for its shared library/module for the sake of consistency with embed and php as a whole * Added shared embed SAPI For the time being, this simplifies building shared apache2handler, embed and phpdbg SAPIs that require the position independent code. While there is this rule of a thumb that position independent code enabled on executables slightly decreases performance, the usability benefits outweight this issue in this case. At least until a better solution can be integrated here. The conditional check for CMAKE_POSITION_INDEPENDENT_CODE is removed because now it seems to be completely redundant in any case. The upstream issue is different because there the PIC is forcefully disabled in configure.ac on some 32-bit machines and that's why it doesn't work.
1 parent 1552784 commit 2f23460

File tree

6 files changed

+196
-94
lines changed

6 files changed

+196
-94
lines changed

bin/check-cmake.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ function getProjectModules(): array
152152
'CheckLanguage' => ['check_language'],
153153
'CheckLibraryExists' => ['check_library_exists'],
154154
'CheckLinkerFlag' => ['check_linker_flag'],
155+
'CheckPIESupported' => ['check_pie_supported'],
155156
'CheckPrototypeDefinition' => ['check_prototype_definition'],
156157
'CheckSourceCompiles' => ['check_source_compiles'],
157158
'CheckSourceRuns' => ['check_source_runs'],

cmake/cmake/modules/PHP/PositionIndependentCode.cmake

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,31 @@
44
Check whether to enable the `POSITION_INDEPENDENT_CODE` or not for all targets.
55
The SHARED and MODULE targets have PIC enabled regardless of this option.
66
7+
TODO: This unconditionally enables position independent code globally, to be
8+
able to build shared apache2handler, embed, and phpdbg SAPIs. Probably could be
9+
fine tuned in the future better but it can exponentially complicate the build
10+
system code or the build usability.
11+
712
https://cmake.org/cmake/help/latest/variable/CMAKE_POSITION_INDEPENDENT_CODE.html
813
#]=============================================================================]
914

1015
include_guard(GLOBAL)
1116

12-
if(CMAKE_C_COMPILER_ID STREQUAL "Clang" AND CMAKE_SIZEOF_VOID_P EQUAL 4)
13-
# On 32-bit *nix (Linux and FreeBSD at least) when using Clang, the PIC is
14-
# required.
17+
block()
18+
include(CheckPIESupported)
19+
check_pie_supported(OUTPUT_VARIABLE output)
20+
if(NOT CMAKE_C_LINK_PIE_SUPPORTED)
21+
message(
22+
WARNING
23+
"Position independent executable (PIE) is not supported at link time: "
24+
"${output}.\n"
25+
"PIE link options will not be passed to linker."
26+
)
27+
endif()
28+
endblock()
29+
30+
if(NOT DEFINED CMAKE_POSITION_INDEPENDENT_CODE)
1531
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
16-
else()
17-
set(CMAKE_POSITION_INDEPENDENT_CODE OFF)
1832
endif()
1933

2034
message(

cmake/sapi/apache2handler/CMakeLists.txt

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ target_sources(
5858
set_target_properties(
5959
php_sapi_apache2handler
6060
PROPERTIES
61-
OUTPUT_NAME apache
61+
OUTPUT_NAME mod_${PHP_PROGRAM_PREFIX}php${PHP_PROGRAM_SUFFIX}
6262
)
6363

6464
target_compile_definitions(
@@ -132,5 +132,8 @@ endif()
132132

133133
install(
134134
TARGETS php_sapi_apache2handler
135-
DESTINATION ${CMAKE_INSTALL_LIBDIR}
135+
LIBRARY
136+
DESTINATION ${Apache_LIBEXECDIR}
137+
RUNTIME
138+
DESTINATION ${Apache_LIBEXECDIR}
136139
)

cmake/sapi/embed/CMakeLists.txt

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -34,38 +34,39 @@ endif()
3434
add_library(php_sapi_embed STATIC)
3535
add_library(PHP::sapi::embed ALIAS php_sapi_embed)
3636

37+
add_library(php_sapi_embed_shared SHARED)
38+
add_library(PHP::sapi::embed_shared ALIAS php_sapi_embed_shared)
39+
3740
target_sources(
3841
php_sapi_embed
39-
PRIVATE
40-
php_embed.c
4142
PUBLIC
4243
FILE_SET HEADERS
4344
FILES
4445
php_embed.h
4546
)
4647

47-
target_link_libraries(
48-
php_sapi_embed
49-
PRIVATE
50-
$<BUILD_INTERFACE:PHP::sapi>
51-
)
48+
foreach(target IN ITEMS php_sapi_embed php_sapi_embed_shared)
49+
target_sources(${target} PRIVATE php_embed.c)
5250

53-
target_compile_definitions(php_sapi_embed PRIVATE ZEND_ENABLE_STATIC_TSRMLS_CACHE)
51+
target_link_libraries(${target} PRIVATE $<BUILD_INTERFACE:PHP::sapi>)
5452

55-
set_target_properties(
56-
php_sapi_embed
57-
PROPERTIES
58-
OUTPUT_NAME libphp
59-
ENABLE_EXPORTS TRUE # TODO: Check if there's a better solution.
60-
PHP_CLI TRUE
61-
)
53+
target_compile_definitions(${target} PRIVATE ZEND_ENABLE_STATIC_TSRMLS_CACHE)
54+
55+
set_target_properties(
56+
${target}
57+
PROPERTIES
58+
OUTPUT_NAME libphp
59+
ENABLE_EXPORTS TRUE # TODO: Check if there's a better solution.
60+
PHP_CLI TRUE
61+
)
62+
endforeach()
6263

6364
# Configure pkg-config php-embed.pc metadata file.
6465
include(PHP/PkgConfig)
6566
php_pkgconfig_generate_pc(
6667
php-embed.pc.in
6768
php-embed.pc
68-
TARGET PHP::sapi::embed
69+
TARGET PHP::sapi::embed_shared
6970
VARIABLES
7071
prefix "$<INSTALL_PREFIX>"
7172
exec_prefix "$<INSTALL_PREFIX>"
@@ -83,7 +84,9 @@ php_pkgconfig_generate_pc(
8384
)
8485

8586
install(
86-
TARGETS php_sapi_embed
87+
TARGETS php_sapi_embed php_sapi_embed_shared
88+
ARCHIVE
89+
DESTINATION ${CMAKE_INSTALL_LIBDIR}
8790
RUNTIME
8891
DESTINATION ${CMAKE_INSTALL_LIBDIR}
8992
LIBRARY

cmake/sapi/phpdbg/CMakeLists.txt

Lines changed: 113 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -100,47 +100,68 @@ endif()
100100
# Executable and library.
101101
################################################################################
102102

103-
set(phpdbgSources
104-
phpdbg_bp.c
105-
phpdbg_break.c
106-
phpdbg_btree.c
107-
phpdbg_cmd.c
108-
phpdbg_frame.c
109-
phpdbg_help.c
110-
phpdbg_info.c
111-
phpdbg_io.c
112-
phpdbg_lexer.c
113-
phpdbg_list.c
114-
phpdbg_out.c
115-
phpdbg_parser.c
116-
phpdbg_print.c
117-
phpdbg.stub.php
118-
phpdbg_prompt.c
119-
phpdbg_set.c
120-
phpdbg_sigsafe.c
121-
phpdbg_utils.c
122-
phpdbg_watch.c
123-
$<$<PLATFORM_ID:Windows>:phpdbg_win.c>
124-
phpdbg.c
125-
)
126-
127-
add_executable(php_sapi_phpdbg ${phpdbgSources})
103+
add_executable(php_sapi_phpdbg)
128104
add_executable(PHP::sapi::phpdbg ALIAS php_sapi_phpdbg)
129105

130-
target_compile_definitions(
131-
php_sapi_phpdbg
132-
PRIVATE
133-
ZEND_ENABLE_STATIC_TSRMLS_CACHE
134-
$<$<PLATFORM_ID:Windows>:YY_NO_UNISTD_H>
135-
$<IF:$<BOOL:${PHP_SAPI_PHPDBG_DEBUG}>,PHPDBG_DEBUG=1,PHPDBG_DEBUG=0>
136-
)
106+
if(PHP_SAPI_PHPDBG_SHARED)
107+
add_library(php_sapi_phpdbg_shared SHARED)
108+
add_library(PHP::sapi::phpdbg_shared ALIAS php_sapi_phpdbg_shared)
109+
endif()
137110

138-
target_link_libraries(
139-
php_sapi_phpdbg
140-
PRIVATE
141-
$<BUILD_INTERFACE:PHP::sapi>
142-
$<$<PLATFORM_ID:Windows>:ws2_32;user32>
143-
)
111+
foreach(target IN ITEMS php_sapi_phpdbg php_sapi_phpdbg_shared)
112+
if(NOT TARGET ${target})
113+
continue()
114+
endif()
115+
116+
target_sources(
117+
${target}
118+
PRIVATE
119+
phpdbg_bp.c
120+
phpdbg_break.c
121+
phpdbg_btree.c
122+
phpdbg_cmd.c
123+
phpdbg_frame.c
124+
phpdbg_help.c
125+
phpdbg_info.c
126+
phpdbg_io.c
127+
phpdbg_lexer.c
128+
phpdbg_list.c
129+
phpdbg_out.c
130+
phpdbg_parser.c
131+
phpdbg_print.c
132+
phpdbg.stub.php
133+
phpdbg_prompt.c
134+
phpdbg_set.c
135+
phpdbg_sigsafe.c
136+
phpdbg_utils.c
137+
phpdbg_watch.c
138+
$<$<PLATFORM_ID:Windows>:phpdbg_win.c>
139+
phpdbg.c
140+
)
141+
142+
target_compile_definitions(
143+
${target}
144+
PRIVATE
145+
ZEND_ENABLE_STATIC_TSRMLS_CACHE
146+
$<$<PLATFORM_ID:Windows>:YY_NO_UNISTD_H>
147+
$<IF:$<BOOL:${PHP_SAPI_PHPDBG_DEBUG}>,PHPDBG_DEBUG=1,PHPDBG_DEBUG=0>
148+
)
149+
150+
target_link_libraries(
151+
${target}
152+
PRIVATE
153+
$<BUILD_INTERFACE:PHP::sapi>
154+
$<$<PLATFORM_ID:Windows>:ws2_32;user32>
155+
)
156+
157+
set_target_properties(
158+
${target}
159+
PROPERTIES
160+
OUTPUT_NAME ${PHP_PROGRAM_PREFIX}phpdbg${PHP_PROGRAM_SUFFIX}
161+
ENABLE_EXPORTS TRUE # TODO: Check if there's a better solution.
162+
PHP_CLI TRUE
163+
)
164+
endforeach()
144165

145166
target_link_options(
146167
php_sapi_phpdbg
@@ -152,10 +173,16 @@ set_target_properties(
152173
php_sapi_phpdbg
153174
PROPERTIES
154175
OUTPUT_NAME ${PHP_PROGRAM_PREFIX}phpdbg${PHP_PROGRAM_SUFFIX}
155-
ENABLE_EXPORTS TRUE # TODO: Check if there's a better solution.
156-
PHP_CLI TRUE
157176
)
158177

178+
if(TARGET php_sapi_phpdbg_shared)
179+
set_target_properties(
180+
php_sapi_phpdbg_shared
181+
PROPERTIES
182+
OUTPUT_NAME libphpdbg
183+
)
184+
endif()
185+
159186
################################################################################
160187
# Readline support.
161188
################################################################################
@@ -183,6 +210,9 @@ if(PHP_SAPI_PHPDBG_READLINE OR PHP_EXT_READLINE)
183210
)
184211

185212
target_link_libraries(php_sapi_phpdbg PRIVATE Readline::Readline)
213+
if(TARGET php_sapi_phpdbg_shared)
214+
target_link_libraries(php_sapi_phpdbg_shared PRIVATE Readline::Readline)
215+
endif()
186216

187217
set(HAVE_LIBREADLINE TRUE)
188218
else()
@@ -195,6 +225,9 @@ if(PHP_SAPI_PHPDBG_READLINE OR PHP_EXT_READLINE)
195225
)
196226

197227
target_link_libraries(php_sapi_phpdbg PRIVATE Editline::Editline)
228+
if(TARGET php_sapi_phpdbg_shared)
229+
target_link_libraries(php_sapi_phpdbg_shared PRIVATE Editline::Editline)
230+
endif()
198231

199232
set(HAVE_LIBEDIT TRUE)
200233
endif()
@@ -210,36 +243,6 @@ add_feature_info(
210243
"enhanced command-line accessibility"
211244
)
212245

213-
################################################################################
214-
# The phpdbg shared library.
215-
################################################################################
216-
217-
# TODO: Should readline support be also enabled like in the executable?
218-
# TODO: fix this better in the future (building with -fPIC etc).
219-
if(PHP_SAPI_PHPDBG_SHARED)
220-
add_library(php_sapi_phpdbg_shared SHARED)
221-
add_library(PHP::sapi::phpdbg_shared ALIAS php_sapi_phpdbg_shared)
222-
223-
set_target_properties(php_sapi_phpdbg_shared PROPERTIES OUTPUT_NAME phpdbg)
224-
225-
target_sources(php_sapi_phpdbg_shared PRIVATE ${phpdbgSources})
226-
227-
target_compile_definitions(
228-
php_sapi_phpdbg_shared
229-
PRIVATE
230-
ZEND_ENABLE_STATIC_TSRMLS_CACHE
231-
$<$<PLATFORM_ID:Windows>:YY_NO_UNISTD_H>
232-
$<IF:$<BOOL:${PHP_SAPI_PHPDBG_DEBUG}>,PHPDBG_DEBUG=1,PHPDBG_DEBUG=0>
233-
)
234-
235-
target_link_libraries(
236-
php_sapi_phpdbg_shared
237-
PRIVATE
238-
$<BUILD_INTERFACE:PHP::sapi>
239-
$<$<PLATFORM_ID:Windows>:ws2_32;user32>
240-
)
241-
endif()
242-
243246
################################################################################
244247
# Generate parser and lexer files.
245248
################################################################################
@@ -267,6 +270,9 @@ if(HAVE_UFFDIO_WRITEPROTECT_MODE_WP)
267270
find_package(Threads)
268271
if(Threads_FOUND)
269272
target_link_libraries(php_sapi_phpdbg PRIVATE Threads::Threads)
273+
if(TARGET php_sapi_phpdbg_shared)
274+
target_link_libraries(php_sapi_phpdbg_shared PRIVATE Threads::Threads)
275+
endif()
270276
else()
271277
message(WARNING "Threads not available.")
272278
endif()
@@ -313,4 +319,41 @@ php_install(CODE "
313319
)
314320
")
315321

322+
# Configure pkg-config phpdbg.pc metadata file and install shared phpdbg module.
323+
if(TARGET php_sapi_phpdbg_shared)
324+
include(PHP/PkgConfig)
325+
php_pkgconfig_generate_pc(
326+
phpdbg.pc.in
327+
phpdbg.pc
328+
TARGET PHP::sapi::phpdbg_shared
329+
VARIABLES
330+
prefix "$<INSTALL_PREFIX>"
331+
exec_prefix "$<INSTALL_PREFIX>"
332+
includedir "$<PATH:ABSOLUTE_PATH,NORMALIZE,${CMAKE_INSTALL_INCLUDEDIR},$<INSTALL_PREFIX>>"
333+
php_include_prefix "${PHP_INCLUDE_PREFIX}"
334+
libdir "$<PATH:ABSOLUTE_PATH,NORMALIZE,${CMAKE_INSTALL_LIBDIR},$<INSTALL_PREFIX>>"
335+
PHP_VERSION "${PHP_VERSION}"
336+
PHP_VERSION_ID "${PHP_VERSION_ID}"
337+
PHP_EXTENSION_DIR "$<PATH:ABSOLUTE_PATH,NORMALIZE,${PHP_EXTENSION_DIR},$<INSTALL_PREFIX>>"
338+
# TODO: Fix this for cmake --install ... --prefix
339+
PHP_CONFIG_FILE_SCAN_DIR "$<PATH:ABSOLUTE_PATH,NORMALIZE,${PHP_CONFIG_FILE_SCAN_DIR},$<INSTALL_PREFIX>>"
340+
PHP_CONFIG_FILE_PATH "$<PATH:ABSOLUTE_PATH,NORMALIZE,${PHP_CONFIG_FILE_PATH},$<INSTALL_PREFIX>>"
341+
PHP_DEBUG "$<IF:$<CONFIG:Debug,DebugAssertions>,yes,no>"
342+
PHP_THREAD_SAFETY "$<IF:$<BOOL:$<TARGET_PROPERTY:PHP::config,PHP_THREAD_SAFETY>>,yes,no>"
343+
)
344+
345+
install(
346+
FILES ${CMAKE_CURRENT_BINARY_DIR}/phpdbg.pc
347+
DESTINATION ${CMAKE_INSTALL_LIBDIR}/pkgconfig
348+
)
349+
350+
install(
351+
TARGETS php_sapi_phpdbg_shared
352+
LIBRARY
353+
DESTINATION ${CMAKE_INSTALL_LIBDIR}
354+
RUNTIME
355+
DESTINATION ${CMAKE_INSTALL_LIBDIR}
356+
)
357+
endif()
358+
316359
configure_file(cmake/config.h.in config.h)

0 commit comments

Comments
 (0)