Skip to content

Commit fd09510

Browse files
committed
Add PHP_CLI target property
This enables marking extensions and SAPIs as CLI-based. These are meant to be used in a CLI environment.
1 parent 14b8b4d commit fd09510

File tree

12 files changed

+103
-90
lines changed

12 files changed

+103
-90
lines changed

cmake/cmake/Bootstrap.cmake

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ Configure project after the project() call.
44

55
include_guard(GLOBAL)
66

7+
define_property(
8+
TARGET
9+
PROPERTY PHP_CLI
10+
BRIEF_DOCS "Whether the PHP SAPI or extension is CLI-based"
11+
)
12+
713
# Optionally enable CXX for extensions.
814
include(CheckLanguage)
915
check_language(CXX)

cmake/ext/CMakeLists.txt

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,6 @@ Add subdirectories of PHP extensions.
55
66
* `php_extensions` (alias `PHP::extensions`) is an INTERFACE library with all
77
enabled extensions linked into for convenience.
8-
9-
## Custom CMake properties
10-
11-
* `PHP_ALL_EXTENSIONS`
12-
13-
Global property with a list of all PHP extensions in the ext directory.
14-
15-
* `PHP_ALWAYS_ENABLED_EXTENSIONS`
16-
17-
Global property with a list of always enabled PHP extensions which can be
18-
considered part of the core PHP engine.
19-
20-
* `PHP_EXTENSIONS`
21-
22-
Global property with a list of all enabled PHP extensions for the current
23-
configuration. Extensions are sorted by their dependencies (extensions added
24-
with CMake command `add_dependencies()`).
258
#]=============================================================================]
269

2710
include(PHP/Extensions)
@@ -127,23 +110,41 @@ foreach(extension IN LISTS extensions)
127110
)
128111

129112
get_target_property(type php_${extension} TYPE)
113+
get_target_property(isCli php_${extension} PHP_CLI)
130114
if(NOT type MATCHES "^(MODULE|SHARED)_LIBRARY$")
131-
target_link_libraries(
132-
php_extensions
133-
INTERFACE
134-
# If extension is STATIC library link as whole archive, otherwise link
135-
# normally:
136-
$<IF:$<STREQUAL:$<TARGET_PROPERTY:PHP::${extension},TYPE>,STATIC_LIBRARY>,$<LINK_LIBRARY:WHOLE_ARCHIVE,PHP::${extension}>,PHP::${extension}>
137-
)
138-
139-
target_sources(
140-
php_extensions
141-
INTERFACE
142-
# If extension is OBJECT library:
143-
$<$<STREQUAL:$<TARGET_PROPERTY:PHP::${extension},TYPE>,OBJECT_LIBRARY>:$<TARGET_OBJECTS:PHP::${extension}>>
144-
# If extension and linked target (SAPI) are both STATIC libraries:
145-
$<$<AND:$<STREQUAL:$<TARGET_PROPERTY:TYPE>,STATIC_LIBRARY>,$<STREQUAL:$<TARGET_PROPERTY:PHP::${extension},TYPE>,STATIC_LIBRARY>>:$<TARGET_OBJECTS:PHP::${extension}>>
146-
)
115+
# If extension is STATIC library link as whole archive, otherwise link
116+
# normally. CLI-based extensions are linked only to CLI-based SAPIs.
117+
if(NOT isCli)
118+
target_link_libraries(
119+
php_extensions
120+
INTERFACE
121+
$<IF:$<STREQUAL:$<TARGET_PROPERTY:PHP::${extension},TYPE>,STATIC_LIBRARY>,$<LINK_LIBRARY:WHOLE_ARCHIVE,PHP::${extension}>,PHP::${extension}>
122+
)
123+
124+
target_sources(
125+
php_extensions
126+
INTERFACE
127+
# If extension is OBJECT library:
128+
$<$<STREQUAL:$<TARGET_PROPERTY:PHP::${extension},TYPE>,OBJECT_LIBRARY>:$<TARGET_OBJECTS:PHP::${extension}>>
129+
# If extension and linked target (SAPI) are both STATIC libraries:
130+
$<$<AND:$<STREQUAL:$<TARGET_PROPERTY:TYPE>,STATIC_LIBRARY>,$<STREQUAL:$<TARGET_PROPERTY:PHP::${extension},TYPE>,STATIC_LIBRARY>>:$<TARGET_OBJECTS:PHP::${extension}>>
131+
)
132+
else()
133+
target_link_libraries(
134+
php_extensions
135+
INTERFACE
136+
$<$<BOOL:$<TARGET_PROPERTY:PHP_CLI>>:$<IF:$<STREQUAL:$<TARGET_PROPERTY:PHP::${extension},TYPE>,STATIC_LIBRARY>,$<LINK_LIBRARY:WHOLE_ARCHIVE,PHP::${extension}>,PHP::${extension}>>
137+
)
138+
139+
target_sources(
140+
php_extensions
141+
INTERFACE
142+
# If extension is OBJECT library:
143+
$<$<BOOL:$<TARGET_PROPERTY:PHP_CLI>>:$<$<STREQUAL:$<TARGET_PROPERTY:PHP::${extension},TYPE>,OBJECT_LIBRARY>:$<TARGET_OBJECTS:PHP::${extension}>>>
144+
# If extension and linked target (SAPI) are both STATIC libraries:
145+
$<$<BOOL:$<TARGET_PROPERTY:PHP_CLI>>:$<$<AND:$<STREQUAL:$<TARGET_PROPERTY:TYPE>,STATIC_LIBRARY>,$<STREQUAL:$<TARGET_PROPERTY:PHP::${extension},TYPE>,STATIC_LIBRARY>>:$<TARGET_OBJECTS:PHP::${extension}>>>
146+
)
147+
endif()
147148
endif()
148149

149150
message(CHECK_PASS "enabled")

cmake/ext/pcntl/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
3535
return()
3636
endif()
3737

38-
# TODO: pcntl is a cli/cgi extension.
3938
include(CheckSymbolExists)
4039
include(CheckTypeSize)
4140
include(CMakeDependentOption)
@@ -68,6 +67,8 @@ else()
6867
add_library(php_pcntl)
6968
endif()
7069

70+
set_target_properties(php_pcntl PROPERTIES PHP_CLI TRUE)
71+
7172
target_sources(
7273
php_pcntl
7374
PRIVATE

cmake/ext/readline/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ Build extension as shared.
3030
Use the GNU Readline library instead of Editline.
3131
#]=============================================================================]
3232

33-
# TODO: ext/readline is cli extension only.
34-
3533
project(
3634
PhpExtensionReadline
3735
LANGUAGES C
@@ -79,6 +77,8 @@ else()
7977
add_library(php_readline)
8078
endif()
8179

80+
set_target_properties(php_readline PROPERTIES PHP_CLI TRUE)
81+
8282
target_sources(
8383
php_readline
8484
PRIVATE

cmake/ext/skeleton/CMakeLists.txt.in

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,15 @@ else()
7676
add_library(php_%EXTNAME%)
7777
endif()
7878

79+
# Configure extension as CLI-based. Such extensions are only meant to be used
80+
# and enabled on CLI-based PHP SAPIs, such as cgi, cli, phpdbg, embed, etc.
81+
#set_target_properties(php_%EXTNAME% PROPERTIES PHP_CLI TRUE)
82+
83+
# Configure extension as Zend extension. Zend extensions are loaded with the
84+
# 'zend_extension' INI directive and include additional advanced hooks. Use only
85+
# when building advanced extensions, such as debuggers, profilers, caching, etc.
86+
#set_target_properties(php_%EXTNAME% PROPERTIES PHP_ZEND_EXTENSION TRUE)
87+
7988
# Add library target sources.
8089
target_sources(
8190
php_%EXTNAME%
@@ -98,11 +107,6 @@ target_compile_definitions(
98107
ZEND_ENABLE_STATIC_TSRMLS_CACHE
99108
)
100109

101-
# Configure extension as Zend extension. Zend extensions are loaded with the
102-
# 'zend_extension' INI directive and include additional advanced hooks. Use only
103-
# when building advanced extensions, such as debuggers, profilers, caching, etc.
104-
#set_target_properties(php_%EXTNAME% PROPERTIES PHP_ZEND_EXTENSION TRUE)
105-
106110
# Find PHP package on the system.
107111
find_package(PHP REQUIRED)
108112

cmake/ext/standard/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,7 @@ target_compile_definitions(
553553
php_standard_functions_cli
554554
PRIVATE
555555
$<TARGET_PROPERTY:php_standard,COMPILE_DEFINITIONS>
556-
ENABLE_CHROOT_FUNC
556+
$<$<NOT:$<PLATFORM_ID:Windows>>:ENABLE_CHROOT_FUNC>
557557
)
558558
target_compile_definitions(
559559
php_standard_functions

cmake/main/CMakeLists.txt

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ target_sources(
179179
$<TARGET_OBJECTS:php_main>
180180

181181
# Internal functions objects based on the SAPI type.
182-
$<IF:$<BOOL:$<TARGET_PROPERTY:PHP_SAPI_CLI>>,$<TARGET_OBJECTS:php_main_internal_functions_cli>,$<TARGET_OBJECTS:php_main_internal_functions>>
182+
$<IF:$<BOOL:$<TARGET_PROPERTY:PHP_CLI>>,$<TARGET_OBJECTS:php_main_internal_functions_cli>,$<TARGET_OBJECTS:php_main_internal_functions>>
183183

184184
# If Zend is OBJECT library, add library objects as sources.
185185
$<$<STREQUAL:$<TARGET_PROPERTY:Zend::Zend,TYPE>,OBJECT_LIBRARY>:$<TARGET_OBJECTS:Zend::Zend>>
@@ -191,7 +191,7 @@ target_sources(
191191
$<$<TARGET_EXISTS:PHP::windows>:$<TARGET_OBJECTS:PHP::windows>>
192192

193193
# ext/standard functions objects based on the SAPI type.
194-
$<IF:$<BOOL:$<TARGET_PROPERTY:PHP_SAPI_CLI>>,$<TARGET_OBJECTS:php_standard_functions_cli>,$<TARGET_OBJECTS:php_standard_functions>>
194+
$<IF:$<BOOL:$<TARGET_PROPERTY:PHP_CLI>>,$<TARGET_OBJECTS:php_standard_functions_cli>,$<TARGET_OBJECTS:php_standard_functions>>
195195
)
196196

197197
################################################################################
@@ -272,16 +272,16 @@ if(PHP_DMALLOC)
272272
endif()
273273

274274
################################################################################
275-
# Generate files and install headers.
275+
# Create main/internal_functions*.c files with a list of static enabled PHP
276+
# extensions based on the PHP SAPI type.
276277
################################################################################
277278

278-
# Create main/internal_functions*.c files based on the enabled extensions.
279-
function(_php_main_create_internal_functions)
280-
# Create main/internal_functions* files.
281-
set(EXT_INCLUDE_CODE "")
282-
set(EXT_MODULE_PTRS "")
279+
block()
280+
set(includes "")
281+
set(includesCli "")
282+
set(pointers "")
283+
set(pointersCli "")
283284

284-
# Add artifacts of static enabled PHP extensions to symbol definitions.
285285
get_property(extensions GLOBAL PROPERTY PHP_EXTENSIONS)
286286
foreach(extension IN LISTS extensions)
287287
# Skip if extension is shared.
@@ -290,6 +290,8 @@ function(_php_main_create_internal_functions)
290290
continue()
291291
endif()
292292

293+
get_target_property(isCli php_${extension} PHP_CLI)
294+
293295
file(GLOB_RECURSE headers ${PHP_SOURCE_DIR}/ext/${extension}/*.h)
294296

295297
foreach(header IN LISTS headers)
@@ -298,29 +300,46 @@ function(_php_main_create_internal_functions)
298300

299301
if(NOT index EQUAL -1)
300302
cmake_path(GET header FILENAME filename)
301-
string(
302-
APPEND
303-
EXT_INCLUDE_CODE
304-
"#include \"ext/${extension}/${filename}\"\n"
305-
)
303+
set(code "#include \"ext/${extension}/${filename}\"\n")
304+
string(APPEND includesCli "${code}")
305+
if(NOT isCli)
306+
string(APPEND includes "${code}")
307+
endif()
306308
endif()
307309
endforeach()
308310

309-
set(EXT_MODULE_PTRS "${EXT_MODULE_PTRS}\n\tphpext_${extension}_ptr,")
311+
set(code "\n\tphpext_${extension}_ptr,")
312+
set(pointersCli "${pointersCli}${code}")
313+
if(NOT isCli)
314+
set(pointers "${pointers}${code}")
315+
endif()
310316
endforeach()
311317

312-
message(STATUS "Creating main/internal_functions.c")
313-
configure_file(main/internal_functions.c.in main/internal_functions.c)
318+
cmake_path(
319+
RELATIVE_PATH
320+
CMAKE_CURRENT_BINARY_DIR
321+
BASE_DIRECTORY ${CMAKE_BINARY_DIR}
322+
OUTPUT_VARIABLE relativeDir
323+
)
324+
325+
set(EXT_INCLUDE_CODE "${includes}")
326+
set(EXT_MODULE_PTRS "${pointers}")
327+
message(STATUS "Creating ${relativeDir}/internal_functions.c")
328+
configure_file(internal_functions.c.in internal_functions.c)
314329

315-
message(STATUS "Creating main/internal_functions_cli.c")
316-
configure_file(main/internal_functions.c.in main/internal_functions_cli.c)
317-
endfunction()
330+
set(EXT_INCLUDE_CODE "${includesCli}")
331+
set(EXT_MODULE_PTRS "${pointersCli}")
332+
message(STATUS "Creating ${relativeDir}/internal_functions_cli.c")
333+
configure_file(internal_functions.c.in internal_functions_cli.c)
334+
endblock()
335+
336+
################################################################################
337+
# Generate configuration headers.
338+
################################################################################
318339

319340
# Run at the end of the configuration.
320341
cmake_language(DEFER DIRECTORY ${PHP_SOURCE_DIR} CALL _php_main_create_files)
321342
function(_php_main_create_files)
322-
_php_main_create_internal_functions()
323-
324343
##############################################################################
325344
# Map CMake variable names to names in PHP configuration headers where needed.
326345
##############################################################################
@@ -512,6 +531,10 @@ function(_php_main_create_files)
512531
)
513532
endfunction()
514533

534+
################################################################################
535+
# Configure installation.
536+
################################################################################
537+
515538
install(
516539
TARGETS php_main
517540
ARCHIVE EXCLUDE_FROM_ALL

cmake/sapi/CMakeLists.txt

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,5 @@
11
#[=============================================================================[
22
Add subdirectories of PHP SAPIs.
3-
4-
## Custom CMake properties
5-
6-
* `PHP_ALL_SAPIS`
7-
8-
Global property with a list of all PHP SAPIs in the sapi directory.
9-
10-
* `PHP_SAPIS`
11-
12-
Global property with a list of all enabled PHP SAPIs.
13-
14-
* `PHP_SAPI_CLI`
15-
16-
Target property that designates PHP SAPI as CLI-based. These SAPIs can utilize
17-
CLI-based PHP extensions (for example, pcntl) and include
18-
main/internal_functions_cli.c object instead of the main/internal_functions.c.
193
#]=============================================================================]
204

215
message(STATUS "")
@@ -36,12 +20,6 @@ define_property(
3620
BRIEF_DOCS "A list of all enabled PHP SAPIs"
3721
)
3822

39-
define_property(
40-
TARGET
41-
PROPERTY PHP_SAPI_CLI
42-
BRIEF_DOCS "Whether the PHP SAPI is CLI-based to run in a CLI environment"
43-
)
44-
4523
list(APPEND CMAKE_MESSAGE_CONTEXT "sapi")
4624

4725
# Traverse CMakeLists.txt files of PHP SAPIs.

cmake/sapi/cgi/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ set_target_properties(
5757
OUTPUT_NAME ${PHP_PROGRAM_PREFIX}php-cgi${PHP_PROGRAM_SUFFIX}
5858
# TODO: Check if there's a better solution here:
5959
ENABLE_EXPORTS TRUE
60-
PHP_SAPI_CLI TRUE
60+
PHP_CLI TRUE
6161
)
6262

6363
# BSD systems.

cmake/sapi/cli/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ set_target_properties(
118118
OUTPUT_NAME ${PHP_PROGRAM_PREFIX}php${PHP_PROGRAM_SUFFIX}
119119
# TODO: Check if there's a better solution here:
120120
ENABLE_EXPORTS TRUE
121-
PHP_SAPI_CLI TRUE
121+
PHP_CLI TRUE
122122
)
123123

124124
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")

0 commit comments

Comments
 (0)