Skip to content

Commit

Permalink
Add PHP_CLI target property
Browse files Browse the repository at this point in the history
This enables marking extensions and SAPIs as CLI-based. These are meant
to be used in a CLI environment.
  • Loading branch information
petk committed Dec 11, 2024
1 parent 14b8b4d commit fd09510
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 90 deletions.
6 changes: 6 additions & 0 deletions cmake/cmake/Bootstrap.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ Configure project after the project() call.

include_guard(GLOBAL)

define_property(
TARGET
PROPERTY PHP_CLI
BRIEF_DOCS "Whether the PHP SAPI or extension is CLI-based"
)

# Optionally enable CXX for extensions.
include(CheckLanguage)
check_language(CXX)
Expand Down
67 changes: 34 additions & 33 deletions cmake/ext/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,6 @@ Add subdirectories of PHP extensions.
* `php_extensions` (alias `PHP::extensions`) is an INTERFACE library with all
enabled extensions linked into for convenience.
## Custom CMake properties
* `PHP_ALL_EXTENSIONS`
Global property with a list of all PHP extensions in the ext directory.
* `PHP_ALWAYS_ENABLED_EXTENSIONS`
Global property with a list of always enabled PHP extensions which can be
considered part of the core PHP engine.
* `PHP_EXTENSIONS`
Global property with a list of all enabled PHP extensions for the current
configuration. Extensions are sorted by their dependencies (extensions added
with CMake command `add_dependencies()`).
#]=============================================================================]

include(PHP/Extensions)
Expand Down Expand Up @@ -127,23 +110,41 @@ foreach(extension IN LISTS extensions)
)

get_target_property(type php_${extension} TYPE)
get_target_property(isCli php_${extension} PHP_CLI)
if(NOT type MATCHES "^(MODULE|SHARED)_LIBRARY$")
target_link_libraries(
php_extensions
INTERFACE
# If extension is STATIC library link as whole archive, otherwise link
# normally:
$<IF:$<STREQUAL:$<TARGET_PROPERTY:PHP::${extension},TYPE>,STATIC_LIBRARY>,$<LINK_LIBRARY:WHOLE_ARCHIVE,PHP::${extension}>,PHP::${extension}>
)

target_sources(
php_extensions
INTERFACE
# If extension is OBJECT library:
$<$<STREQUAL:$<TARGET_PROPERTY:PHP::${extension},TYPE>,OBJECT_LIBRARY>:$<TARGET_OBJECTS:PHP::${extension}>>
# If extension and linked target (SAPI) are both STATIC libraries:
$<$<AND:$<STREQUAL:$<TARGET_PROPERTY:TYPE>,STATIC_LIBRARY>,$<STREQUAL:$<TARGET_PROPERTY:PHP::${extension},TYPE>,STATIC_LIBRARY>>:$<TARGET_OBJECTS:PHP::${extension}>>
)
# If extension is STATIC library link as whole archive, otherwise link
# normally. CLI-based extensions are linked only to CLI-based SAPIs.
if(NOT isCli)
target_link_libraries(
php_extensions
INTERFACE
$<IF:$<STREQUAL:$<TARGET_PROPERTY:PHP::${extension},TYPE>,STATIC_LIBRARY>,$<LINK_LIBRARY:WHOLE_ARCHIVE,PHP::${extension}>,PHP::${extension}>
)

target_sources(
php_extensions
INTERFACE
# If extension is OBJECT library:
$<$<STREQUAL:$<TARGET_PROPERTY:PHP::${extension},TYPE>,OBJECT_LIBRARY>:$<TARGET_OBJECTS:PHP::${extension}>>
# If extension and linked target (SAPI) are both STATIC libraries:
$<$<AND:$<STREQUAL:$<TARGET_PROPERTY:TYPE>,STATIC_LIBRARY>,$<STREQUAL:$<TARGET_PROPERTY:PHP::${extension},TYPE>,STATIC_LIBRARY>>:$<TARGET_OBJECTS:PHP::${extension}>>
)
else()
target_link_libraries(
php_extensions
INTERFACE
$<$<BOOL:$<TARGET_PROPERTY:PHP_CLI>>:$<IF:$<STREQUAL:$<TARGET_PROPERTY:PHP::${extension},TYPE>,STATIC_LIBRARY>,$<LINK_LIBRARY:WHOLE_ARCHIVE,PHP::${extension}>,PHP::${extension}>>
)

target_sources(
php_extensions
INTERFACE
# If extension is OBJECT library:
$<$<BOOL:$<TARGET_PROPERTY:PHP_CLI>>:$<$<STREQUAL:$<TARGET_PROPERTY:PHP::${extension},TYPE>,OBJECT_LIBRARY>:$<TARGET_OBJECTS:PHP::${extension}>>>
# If extension and linked target (SAPI) are both STATIC libraries:
$<$<BOOL:$<TARGET_PROPERTY:PHP_CLI>>:$<$<AND:$<STREQUAL:$<TARGET_PROPERTY:TYPE>,STATIC_LIBRARY>,$<STREQUAL:$<TARGET_PROPERTY:PHP::${extension},TYPE>,STATIC_LIBRARY>>:$<TARGET_OBJECTS:PHP::${extension}>>>
)
endif()
endif()

message(CHECK_PASS "enabled")
Expand Down
3 changes: 2 additions & 1 deletion cmake/ext/pcntl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
return()
endif()

# TODO: pcntl is a cli/cgi extension.
include(CheckSymbolExists)
include(CheckTypeSize)
include(CMakeDependentOption)
Expand Down Expand Up @@ -68,6 +67,8 @@ else()
add_library(php_pcntl)
endif()

set_target_properties(php_pcntl PROPERTIES PHP_CLI TRUE)

target_sources(
php_pcntl
PRIVATE
Expand Down
4 changes: 2 additions & 2 deletions cmake/ext/readline/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ Build extension as shared.
Use the GNU Readline library instead of Editline.
#]=============================================================================]

# TODO: ext/readline is cli extension only.

project(
PhpExtensionReadline
LANGUAGES C
Expand Down Expand Up @@ -79,6 +77,8 @@ else()
add_library(php_readline)
endif()

set_target_properties(php_readline PROPERTIES PHP_CLI TRUE)

target_sources(
php_readline
PRIVATE
Expand Down
14 changes: 9 additions & 5 deletions cmake/ext/skeleton/CMakeLists.txt.in
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,15 @@ else()
add_library(php_%EXTNAME%)
endif()

# Configure extension as CLI-based. Such extensions are only meant to be used
# and enabled on CLI-based PHP SAPIs, such as cgi, cli, phpdbg, embed, etc.
#set_target_properties(php_%EXTNAME% PROPERTIES PHP_CLI TRUE)

# Configure extension as Zend extension. Zend extensions are loaded with the
# 'zend_extension' INI directive and include additional advanced hooks. Use only
# when building advanced extensions, such as debuggers, profilers, caching, etc.
#set_target_properties(php_%EXTNAME% PROPERTIES PHP_ZEND_EXTENSION TRUE)

# Add library target sources.
target_sources(
php_%EXTNAME%
Expand All @@ -98,11 +107,6 @@ target_compile_definitions(
ZEND_ENABLE_STATIC_TSRMLS_CACHE
)

# Configure extension as Zend extension. Zend extensions are loaded with the
# 'zend_extension' INI directive and include additional advanced hooks. Use only
# when building advanced extensions, such as debuggers, profilers, caching, etc.
#set_target_properties(php_%EXTNAME% PROPERTIES PHP_ZEND_EXTENSION TRUE)

# Find PHP package on the system.
find_package(PHP REQUIRED)

Expand Down
2 changes: 1 addition & 1 deletion cmake/ext/standard/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -553,7 +553,7 @@ target_compile_definitions(
php_standard_functions_cli
PRIVATE
$<TARGET_PROPERTY:php_standard,COMPILE_DEFINITIONS>
ENABLE_CHROOT_FUNC
$<$<NOT:$<PLATFORM_ID:Windows>>:ENABLE_CHROOT_FUNC>
)
target_compile_definitions(
php_standard_functions
Expand Down
67 changes: 45 additions & 22 deletions cmake/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ target_sources(
$<TARGET_OBJECTS:php_main>

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

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

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

################################################################################
Expand Down Expand Up @@ -272,16 +272,16 @@ if(PHP_DMALLOC)
endif()

################################################################################
# Generate files and install headers.
# Create main/internal_functions*.c files with a list of static enabled PHP
# extensions based on the PHP SAPI type.
################################################################################

# Create main/internal_functions*.c files based on the enabled extensions.
function(_php_main_create_internal_functions)
# Create main/internal_functions* files.
set(EXT_INCLUDE_CODE "")
set(EXT_MODULE_PTRS "")
block()
set(includes "")
set(includesCli "")
set(pointers "")
set(pointersCli "")

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

get_target_property(isCli php_${extension} PHP_CLI)

file(GLOB_RECURSE headers ${PHP_SOURCE_DIR}/ext/${extension}/*.h)

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

if(NOT index EQUAL -1)
cmake_path(GET header FILENAME filename)
string(
APPEND
EXT_INCLUDE_CODE
"#include \"ext/${extension}/${filename}\"\n"
)
set(code "#include \"ext/${extension}/${filename}\"\n")
string(APPEND includesCli "${code}")
if(NOT isCli)
string(APPEND includes "${code}")
endif()
endif()
endforeach()

set(EXT_MODULE_PTRS "${EXT_MODULE_PTRS}\n\tphpext_${extension}_ptr,")
set(code "\n\tphpext_${extension}_ptr,")
set(pointersCli "${pointersCli}${code}")
if(NOT isCli)
set(pointers "${pointers}${code}")
endif()
endforeach()

message(STATUS "Creating main/internal_functions.c")
configure_file(main/internal_functions.c.in main/internal_functions.c)
cmake_path(
RELATIVE_PATH
CMAKE_CURRENT_BINARY_DIR
BASE_DIRECTORY ${CMAKE_BINARY_DIR}
OUTPUT_VARIABLE relativeDir
)

set(EXT_INCLUDE_CODE "${includes}")
set(EXT_MODULE_PTRS "${pointers}")
message(STATUS "Creating ${relativeDir}/internal_functions.c")
configure_file(internal_functions.c.in internal_functions.c)

message(STATUS "Creating main/internal_functions_cli.c")
configure_file(main/internal_functions.c.in main/internal_functions_cli.c)
endfunction()
set(EXT_INCLUDE_CODE "${includesCli}")
set(EXT_MODULE_PTRS "${pointersCli}")
message(STATUS "Creating ${relativeDir}/internal_functions_cli.c")
configure_file(internal_functions.c.in internal_functions_cli.c)
endblock()

################################################################################
# Generate configuration headers.
################################################################################

# Run at the end of the configuration.
cmake_language(DEFER DIRECTORY ${PHP_SOURCE_DIR} CALL _php_main_create_files)
function(_php_main_create_files)
_php_main_create_internal_functions()

##############################################################################
# Map CMake variable names to names in PHP configuration headers where needed.
##############################################################################
Expand Down Expand Up @@ -512,6 +531,10 @@ function(_php_main_create_files)
)
endfunction()

################################################################################
# Configure installation.
################################################################################

install(
TARGETS php_main
ARCHIVE EXCLUDE_FROM_ALL
Expand Down
22 changes: 0 additions & 22 deletions cmake/sapi/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,21 +1,5 @@
#[=============================================================================[
Add subdirectories of PHP SAPIs.
## Custom CMake properties
* `PHP_ALL_SAPIS`
Global property with a list of all PHP SAPIs in the sapi directory.
* `PHP_SAPIS`
Global property with a list of all enabled PHP SAPIs.
* `PHP_SAPI_CLI`
Target property that designates PHP SAPI as CLI-based. These SAPIs can utilize
CLI-based PHP extensions (for example, pcntl) and include
main/internal_functions_cli.c object instead of the main/internal_functions.c.
#]=============================================================================]

message(STATUS "")
Expand All @@ -36,12 +20,6 @@ define_property(
BRIEF_DOCS "A list of all enabled PHP SAPIs"
)

define_property(
TARGET
PROPERTY PHP_SAPI_CLI
BRIEF_DOCS "Whether the PHP SAPI is CLI-based to run in a CLI environment"
)

list(APPEND CMAKE_MESSAGE_CONTEXT "sapi")

# Traverse CMakeLists.txt files of PHP SAPIs.
Expand Down
2 changes: 1 addition & 1 deletion cmake/sapi/cgi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ set_target_properties(
OUTPUT_NAME ${PHP_PROGRAM_PREFIX}php-cgi${PHP_PROGRAM_SUFFIX}
# TODO: Check if there's a better solution here:
ENABLE_EXPORTS TRUE
PHP_SAPI_CLI TRUE
PHP_CLI TRUE
)

# BSD systems.
Expand Down
2 changes: 1 addition & 1 deletion cmake/sapi/cli/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ set_target_properties(
OUTPUT_NAME ${PHP_PROGRAM_PREFIX}php${PHP_PROGRAM_SUFFIX}
# TODO: Check if there's a better solution here:
ENABLE_EXPORTS TRUE
PHP_SAPI_CLI TRUE
PHP_CLI TRUE
)

if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
Expand Down
2 changes: 1 addition & 1 deletion cmake/sapi/embed/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ set_target_properties(
OUTPUT_NAME libphp
# TODO: Check if there's a better solution here:
ENABLE_EXPORTS TRUE
PHP_SAPI_CLI TRUE
PHP_CLI TRUE
)

# Configure pkg-config php-embed.pc metadata file.
Expand Down
2 changes: 1 addition & 1 deletion cmake/sapi/phpdbg/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ set_target_properties(
OUTPUT_NAME ${PHP_PROGRAM_PREFIX}phpdbg${PHP_PROGRAM_SUFFIX}
# TODO: Check if there's a better solution here:
ENABLE_EXPORTS TRUE
PHP_SAPI_CLI TRUE
PHP_CLI TRUE
)

################################################################################
Expand Down

0 comments on commit fd09510

Please sign in to comment.