Skip to content

Commit ddc368d

Browse files
authored
Fix OBJECT libraries usage (#16)
These fixes make using OBJECT libraries one step simpler and more intuitive according to the CMake recommendations at this state of the build system. Fixes: - All current OBJECT libraries synced and their objects added into the interface sources of `PHP::PHP` (`php`) target. Object do pass transitively, but PHP has case, there are static library SAPIs such as PHP embed, which is like a bucket of all "global" built objects and they need all these objects. - Windows OBJECT library improved according to current build system. - FindRE2C improved with VERBATIM and COMMAND_EXPAND_LISTS to hopefully make it behave the same across platforms. * TSRM configuration moved to Zend Engine and all sources are now added to the `Zend::Zend` (`zend`) OBJECT library.
1 parent 91934cb commit ddc368d

File tree

13 files changed

+169
-117
lines changed

13 files changed

+169
-117
lines changed

cmake/TSRM/CMakeLists.txt

Lines changed: 0 additions & 53 deletions
This file was deleted.

cmake/Zend/CMakeLists.txt

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#[=============================================================================[
22
Zend Engine.
33
4-
CMake target properties for the `Zend::Zend` (`zend`) target:
4+
CMake target properties for the `Zend::Zend` target:
55
66
* `VERSION`
77
@@ -298,16 +298,7 @@ target_sources(
298298
$<$<NOT:$<PLATFORM_ID:Windows>>:${CMAKE_CURRENT_BINARY_DIR}/zend_config.h>
299299
)
300300

301-
# Add TSRM.
302-
add_subdirectory(../TSRM ../TSRM)
303-
304-
target_link_libraries(
305-
zend
306-
PRIVATE
307-
PHP::configuration
308-
PUBLIC
309-
TSRM::TSRM
310-
)
301+
target_link_libraries(zend PRIVATE PHP::configuration)
311302

312303
target_include_directories(
313304
zend
@@ -352,18 +343,58 @@ block()
352343
)
353344
endblock()
354345

355-
# Add Zend PUBLIC/INTERFACE compile options to configuration.
346+
# Add Zend PUBLIC/INTERFACE compile properties to configuration.
356347
# Cleaner COMPILE_ONLY generator expression is available in CMake >= 3.27.
357348
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.27)
358-
target_link_libraries(php_configuration INTERFACE $<COMPILE_ONLY:Zend::Zend>)
349+
target_link_libraries(php_configuration INTERFACE $<COMPILE_ONLY:zend>)
359350
else()
360351
target_include_directories(
361352
php_configuration
362353
INTERFACE
363-
$<TARGET_PROPERTY:Zend::Zend,INTERFACE_INCLUDE_DIRECTORIES>
354+
$<TARGET_PROPERTY:zend,INTERFACE_INCLUDE_DIRECTORIES>
355+
)
356+
target_compile_definitions(
357+
php_configuration
358+
INTERFACE
359+
$<TARGET_PROPERTY:zend,INTERFACE_COMPILE_DEFINITIONS>
364360
)
365361
endif()
366362

363+
################################################################################
364+
# TSRM (Thread Safe Resource Manager) is a separate directory in php-src as it
365+
# was once a standalone project. Ideally, it should be moved into Zend Engine at
366+
# some point.
367+
################################################################################
368+
369+
target_sources(
370+
zend
371+
PRIVATE
372+
$<$<PLATFORM_ID:Windows>:${CMAKE_CURRENT_SOURCE_DIR}/../TSRM/tsrm_win32.c>
373+
${CMAKE_CURRENT_SOURCE_DIR}/../TSRM/TSRM.c
374+
PUBLIC
375+
FILE_SET tsrm
376+
TYPE HEADERS
377+
BASE_DIRS ${CMAKE_CURRENT_SOURCE_DIR}/../TSRM
378+
FILES
379+
$<$<PLATFORM_ID:Windows>:${CMAKE_CURRENT_SOURCE_DIR}/../TSRM/tsrm_win32.h>
380+
${CMAKE_CURRENT_SOURCE_DIR}/../TSRM/TSRM.h
381+
)
382+
383+
target_include_directories(
384+
zend
385+
INTERFACE
386+
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../TSRM>
387+
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}/${PHP_INCLUDE_PREFIX}/TSRM>
388+
)
389+
390+
target_compile_definitions(zend PUBLIC $<$<PLATFORM_ID:Windows>:TSRM_EXPORTS>)
391+
392+
install(
393+
TARGETS zend
394+
FILE_SET tsrm
395+
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PHP_INCLUDE_PREFIX}/TSRM
396+
)
397+
367398
################################################################################
368399
# Configuration checks.
369400
################################################################################

cmake/cmake/modules/FindRE2C.cmake

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,8 @@ function(re2c_target)
286286
${input}
287287
DEPENDS ${input} ${parsed_DEPENDS}
288288
COMMENT "[RE2C][${ARGV0}] Building lexer with re2c ${RE2C_VERSION}"
289+
VERBATIM
290+
COMMAND_EXPAND_LISTS
289291
)
290292

291293
add_custom_target(

cmake/ext/CMakeLists.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ foreach(extension IN LISTS extensions)
8989
if(TARGET php_${extension})
9090
set_property(GLOBAL APPEND PROPERTY PHP_EXTENSIONS ${extension})
9191

92-
# Add extension's PUBLIC/INTERFACE compile options to configuration.
92+
# Add extension's PUBLIC/INTERFACE compile properties to configuration.
9393
# Cleaner COMPILE_ONLY generator expression is available in CMake >= 3.27.
9494
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.27)
9595
target_link_libraries(
@@ -98,6 +98,10 @@ foreach(extension IN LISTS extensions)
9898
$<COMPILE_ONLY:PHP::${extension}>
9999
)
100100
else()
101+
# TODO: Fix this better. Either require 3.27, or limit/adjust compile
102+
# properties propagated globally. Also, shared extensions shouldn't
103+
# propagate globally.
104+
# https://cmake.org/cmake/help/latest/manual/cmake-buildsystem.7.html#id36
101105
target_include_directories(
102106
php_configuration
103107
INTERFACE
@@ -114,7 +118,7 @@ foreach(extension IN LISTS extensions)
114118
$<TARGET_PROPERTY:php_configuration,INTERFACE_COMPILE_OPTIONS>
115119
)
116120

117-
add_dependencies(php_${extension} zend)
121+
add_dependencies(php_${extension} Zend::Zend)
118122

119123
get_target_property(type php_${extension} TYPE)
120124
if(NOT type MATCHES "^(MODULE|SHARED)_LIBRARY$")

cmake/ext/pcntl/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ This extension provides support for process control support.
99
> This extension is available only on \*nix systems.
1010
1111
> [!IMPORTANT]
12-
> This extension should be used only with cgi, cli, embed, or phpdbg SAPI.
12+
> This extension should be used only with CLI-based PHP SAPIs.
1313
1414
## EXT_PCNTL
1515

cmake/ext/readline/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Configure the `readline` extension.
66
This extension provides interface for using Editline library.
77
88
> [!IMPORTANT]
9-
> This extension should be used only with cgi, cli, embed, or phpdbg SAPI.
9+
> This extension should be used only with CLI-based PHP SAPIs.
1010
1111
## EXT_READLINE
1212

cmake/main/CMakeLists.txt

Lines changed: 63 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,16 @@ PHP main binding.
99

1010
include(FeatureSummary)
1111

12-
add_library(php INTERFACE)
13-
add_library(PHP::PHP ALIAS php)
12+
################################################################################
13+
# Add library.
14+
################################################################################
1415

1516
add_library(php_main OBJECT)
16-
add_library(PHP::main ALIAS php_main)
1717

1818
# These contain a list of built-in extensions based on the SAPI types.
1919
add_library(php_main_internal_functions OBJECT internal_functions.c)
2020
add_library(php_main_internal_functions_cli OBJECT internal_functions_cli.c)
2121

22-
# Configuration library to transitively pass build options to php_main* targets.
23-
add_library(php_main_configuration INTERFACE)
24-
add_library(PHP::mainConfiguration ALIAS php_main_configuration)
25-
2622
target_sources(
2723
php_main
2824
PRIVATE
@@ -113,51 +109,83 @@ target_sources(
113109
)
114110

115111
target_compile_definitions(
116-
php_main_configuration
117-
INTERFACE
118-
ZEND_ENABLE_STATIC_TSRMLS_CACHE=1
112+
php_main
113+
PUBLIC
119114
$<$<PLATFORM_ID:Windows>:SAPI_EXPORTS>
120115
)
121116

122-
target_link_libraries(
123-
php_main_configuration
124-
INTERFACE
125-
PHP::configuration
126-
$<$<TARGET_EXISTS:PHP::win32>:PHP::win32 $<TARGET_OBJECTS:PHP::win32>>
127-
)
128-
129-
target_link_libraries(php_main PRIVATE PHP::mainConfiguration)
130-
target_link_libraries(php_main_internal_functions PRIVATE PHP::mainConfiguration)
131-
target_link_libraries(php_main_internal_functions_cli PRIVATE PHP::mainConfiguration)
132-
133117
target_include_directories(
134118
php_main
135119
INTERFACE
136120
${CMAKE_CURRENT_BINARY_DIR}
137121
${CMAKE_CURRENT_SOURCE_DIR}
138122
)
139123

140-
# Add main PUBLIC/INTERFACE include directories to configuration.
141-
target_include_directories(
142-
php_configuration
143-
INTERFACE
144-
$<TARGET_PROPERTY:PHP::main,INTERFACE_INCLUDE_DIRECTORIES>
124+
set_property(
125+
TARGET
126+
php_main
127+
php_main_internal_functions
128+
php_main_internal_functions_cli
129+
APPEND
130+
PROPERTY COMPILE_DEFINITIONS ZEND_ENABLE_STATIC_TSRMLS_CACHE=1
145131
)
146132

133+
# Add main PUBLIC/INTERFACE compile properties to configuration.
134+
# Cleaner COMPILE_ONLY generator expression is available in CMake >= 3.27.
135+
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.27)
136+
target_link_libraries(php_configuration INTERFACE $<COMPILE_ONLY:php_main>)
137+
else()
138+
target_include_directories(
139+
php_configuration
140+
INTERFACE
141+
$<TARGET_PROPERTY:php_main,INTERFACE_INCLUDE_DIRECTORIES>
142+
)
143+
target_compile_definitions(
144+
php_configuration
145+
INTERFACE
146+
$<TARGET_PROPERTY:php_main,INTERFACE_COMPILE_DEFINITIONS>
147+
)
148+
endif()
149+
150+
target_link_libraries(php_main PRIVATE PHP::configuration)
151+
target_link_libraries(php_main_internal_functions PRIVATE PHP::configuration)
152+
target_link_libraries(php_main_internal_functions_cli PRIVATE PHP::configuration)
153+
154+
################################################################################
155+
# Add PHP::PHP.
156+
################################################################################
157+
158+
add_library(php INTERFACE)
159+
add_library(PHP::PHP ALIAS php)
160+
147161
target_link_libraries(
148162
php
149163
INTERFACE
150164
PHP::configuration
151-
PHP::main
152-
$<TARGET_OBJECTS:PHP::main>
153-
# Pass transitively depending on the SAPI type.
154-
$<IF:$<IN_LIST:$<TARGET_PROPERTY:NAME>,php_cgi;php_cli;php_embed;php_phpdbg>,php_main_internal_functions_cli $<TARGET_OBJECTS:php_main_internal_functions_cli>,php_main_internal_functions $<TARGET_OBJECTS:php_main_internal_functions>>
165+
php_main
155166
Zend::Zend
156-
$<TARGET_OBJECTS:Zend::Zend>
167+
$<$<TARGET_EXISTS:PHP::windows>::PHP::windows>
157168
PHP::extensions
158169
)
159170

171+
# OBJECT libraries propagate only compile properties to static libraries without
172+
# objects as there is no "linking" involved on the compiler level. This is a
173+
# workaround using interface target sources to make PHP::PHP more intuitive to
174+
# work with SAPIs.
175+
target_sources(
176+
php
177+
INTERFACE
178+
$<TARGET_OBJECTS:php_main>
179+
# Internal functions objects based on the SAPI type.
180+
$<IF:$<BOOL:$<TARGET_PROPERTY:PHP_SAPI_CLI>>,$<TARGET_OBJECTS:php_main_internal_functions_cli>,$<TARGET_OBJECTS:php_main_internal_functions>>
181+
$<TARGET_OBJECTS:Zend::Zend>
182+
$<$<TARGET_EXISTS:PHP::windows>:$<TARGET_OBJECTS:PHP::windows>>
183+
)
184+
185+
################################################################################
160186
# Add DTrace.
187+
################################################################################
188+
161189
if(PHP_DTRACE)
162190
message(CHECK_START "Checking for DTrace support")
163191

@@ -196,7 +224,10 @@ add_feature_info(
196224
"performance analysis and troubleshooting"
197225
)
198226

227+
################################################################################
199228
# Add Dmalloc.
229+
################################################################################
230+
200231
if(PHP_DMALLOC)
201232
message(CHECK_START "Checking for Dmalloc support")
202233

@@ -211,7 +242,7 @@ if(PHP_DMALLOC)
211242
target_compile_definitions(
212243
php_configuration
213244
INTERFACE
214-
$<$<COMPILE_LANGUAGE:ASM,C,CXX>:MALLOC_FUNC_CHECK>
245+
$<$<COMPILE_LANGUAGE:ASM,C,CXX>:DMALLOC_FUNC_CHECK>
215246
)
216247

217248
target_link_libraries(php_main PRIVATE Dmalloc::Dmalloc)

cmake/sapi/CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,12 @@ Add subdirectories of PHP SAPIs.
1010
* `PHP_SAPIS`
1111
1212
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.
1319
#]=============================================================================]
1420

1521
message(STATUS "----------------------------")
@@ -28,6 +34,12 @@ define_property(
2834
BRIEF_DOCS "A list of all enabled PHP SAPIs"
2935
)
3036

37+
define_property(
38+
TARGET
39+
PROPERTY PHP_SAPI_CLI
40+
BRIEF_DOCS "Whether the PHP SAPI is CLI-based to run in a CLI environment"
41+
)
42+
3143
list(APPEND CMAKE_MESSAGE_CONTEXT "sapi")
3244

3345
# Traverse CMakeLists.txt files of PHP SAPIs.

cmake/sapi/cgi/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +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
6061
)
6162

6263
# BSD systems.

cmake/sapi/cli/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +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
121122
)
122123

123124
if(CMAKE_SYSTEM_NAME STREQUAL "Windows")

0 commit comments

Comments
 (0)