Skip to content

Commit 266eb78

Browse files
authored
Enable OBJECT and STATIC libraries for Zend and extensions (#20)
This is for testing purposes for now to see how it behaves and if this would work. This enables having extensions, as STATIC, SHARED, MODULE, and OBJECT libraries. And Zend can be also STATIC. STATIC libraries are linked with WHOLE_ARCHIVE feature (--whole-archive and other linker flags) but this is more like a non-portable feature depending on linker, so Zend OBJECT library is still a better way here with the current state of the php-src. Additionally, always enabled extensions are marked as OBJECT libraries to make it clearer. Fixes GH-3
1 parent 8df1759 commit 266eb78

File tree

10 files changed

+76
-51
lines changed

10 files changed

+76
-51
lines changed

cmake/ext/CMakeLists.txt

Lines changed: 57 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -78,57 +78,73 @@ add_library(PHP::extensions ALIAS php_extensions)
7878
# Add subdirectories of extensions.
7979
foreach(extension IN LISTS extensions)
8080
list(APPEND CMAKE_MESSAGE_CONTEXT "${extension}")
81+
8182
message(CHECK_START "Configuring extension ${extension}")
8283
list(APPEND CMAKE_MESSAGE_INDENT " ")
83-
8484
add_subdirectory("${extension}")
85-
8685
php_extensions_postconfigure("${extension}")
87-
8886
list(POP_BACK CMAKE_MESSAGE_INDENT)
89-
if(TARGET php_${extension})
90-
set_property(GLOBAL APPEND PROPERTY PHP_EXTENSIONS ${extension})
91-
92-
# Add extension's PUBLIC/INTERFACE compile properties to configuration.
93-
# Cleaner COMPILE_ONLY generator expression is available in CMake >= 3.27.
94-
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.27)
95-
target_link_libraries(
96-
php_configuration
97-
INTERFACE
98-
$<COMPILE_ONLY:PHP::${extension}>
99-
)
100-
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
105-
target_include_directories(
106-
php_configuration
107-
INTERFACE
108-
$<TARGET_PROPERTY:PHP::${extension},INTERFACE_INCLUDE_DIRECTORIES>
109-
)
110-
endif()
111-
112-
target_link_libraries(php_${extension} PRIVATE PHP::configuration)
113-
114-
# Add configuration compile options before the extension compile options.
115-
target_compile_options(
116-
php_${extension}
117-
BEFORE PRIVATE
118-
$<TARGET_PROPERTY:php_configuration,INTERFACE_COMPILE_OPTIONS>
119-
)
12087

121-
add_dependencies(php_${extension} Zend::Zend)
88+
if(NOT TARGET php_${extension})
89+
message(CHECK_FAIL "disabled")
90+
list(POP_BACK CMAKE_MESSAGE_CONTEXT)
91+
continue()
92+
endif()
12293

123-
get_target_property(type php_${extension} TYPE)
124-
if(NOT type MATCHES "^(MODULE|SHARED)_LIBRARY$")
125-
target_link_libraries(php_extensions INTERFACE PHP::${extension})
126-
endif()
94+
set_property(GLOBAL APPEND PROPERTY PHP_EXTENSIONS ${extension})
12795

128-
message(CHECK_PASS "enabled")
96+
add_dependencies(php_${extension} Zend::Zend)
97+
98+
# Add extension's PUBLIC/INTERFACE compile properties to configuration.
99+
# Cleaner COMPILE_ONLY generator expression is available in CMake >= 3.27.
100+
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.27)
101+
target_link_libraries(
102+
php_configuration
103+
INTERFACE
104+
$<COMPILE_ONLY:PHP::${extension}>
105+
)
129106
else()
130-
message(CHECK_FAIL "disabled")
107+
# TODO: Fix this better. Either require 3.27, or limit/adjust compile
108+
# properties propagated globally. Also, shared extensions shouldn't
109+
# propagate globally.
110+
# https://cmake.org/cmake/help/latest/manual/cmake-buildsystem.7.html#id36
111+
target_include_directories(
112+
php_configuration
113+
INTERFACE
114+
$<TARGET_PROPERTY:PHP::${extension},INTERFACE_INCLUDE_DIRECTORIES>
115+
)
116+
endif()
117+
118+
target_link_libraries(php_${extension} PRIVATE PHP::configuration)
119+
120+
# Add configuration compile options before the extension compile options.
121+
target_compile_options(
122+
php_${extension}
123+
BEFORE PRIVATE
124+
$<TARGET_PROPERTY:php_configuration,INTERFACE_COMPILE_OPTIONS>
125+
)
126+
127+
get_target_property(type php_${extension} TYPE)
128+
if(NOT type MATCHES "^(MODULE|SHARED)_LIBRARY$")
129+
target_link_libraries(
130+
php_extensions
131+
INTERFACE
132+
# If extension is STATIC library link as whole archive, otherwise link
133+
# normally:
134+
$<IF:$<STREQUAL:$<TARGET_PROPERTY:PHP::${extension},TYPE>,STATIC_LIBRARY>,$<LINK_LIBRARY:WHOLE_ARCHIVE,PHP::${extension}>,PHP::${extension}>
135+
)
136+
137+
target_sources(
138+
php_extensions
139+
INTERFACE
140+
# If extension is OBJECT library:
141+
$<$<STREQUAL:$<TARGET_PROPERTY:PHP::${extension},TYPE>,OBJECT_LIBRARY>:$<TARGET_OBJECTS:PHP::${extension}>>
142+
# If extension and linked target (SAPI) are both STATIC libraries:
143+
$<$<AND:$<STREQUAL:$<TARGET_PROPERTY:TYPE>,STATIC_LIBRARY>,$<STREQUAL:$<TARGET_PROPERTY:PHP::${extension},TYPE>,STATIC_LIBRARY>>:$<TARGET_OBJECTS:PHP::${extension}>>
144+
)
131145
endif()
146+
147+
message(CHECK_PASS "enabled")
132148
list(POP_BACK CMAKE_MESSAGE_CONTEXT)
133149
endforeach()
134150

cmake/ext/date/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ add_feature_info(
2525
# Check for headers needed by timelib.
2626
check_include_file(io.h HAVE_IO_H)
2727

28-
add_library(php_date STATIC)
28+
add_library(php_date OBJECT)
2929

3030
target_sources(
3131
php_date

cmake/ext/hash/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ if(EXT_HASH_MHASH)
4747
set(PHP_MHASH_BC TRUE)
4848
endif()
4949

50-
add_library(php_hash STATIC)
50+
add_library(php_hash OBJECT)
5151

5252
target_sources(
5353
php_hash

cmake/ext/json/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ add_feature_info(
2020
"JavaScript Object Notation"
2121
)
2222

23-
add_library(php_json STATIC)
23+
add_library(php_json OBJECT)
2424

2525
target_sources(
2626
php_json

cmake/ext/pcre/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ option(
6363

6464
mark_as_advanced(EXT_PCRE_EXTERNAL EXT_PCRE_JIT)
6565

66-
add_library(php_pcre STATIC)
66+
add_library(php_pcre OBJECT)
6767

6868
target_sources(
6969
php_pcre

cmake/ext/random/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ add_feature_info(
2222
"random number generators and functions related to randomness"
2323
)
2424

25-
add_library(php_random STATIC)
25+
add_library(php_random OBJECT)
2626

2727
target_sources(
2828
php_random

cmake/ext/reflection/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ add_feature_info(
2020
"reflection API to introspect PHP code"
2121
)
2222

23-
add_library(php_reflection STATIC)
23+
add_library(php_reflection OBJECT)
2424
target_sources(
2525
php_reflection
2626
PRIVATE

cmake/ext/spl/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ add_feature_info(
1919
"Standard PHP library (SPL)"
2020
)
2121

22-
add_library(php_spl STATIC)
22+
add_library(php_spl OBJECT)
2323

2424
target_sources(
2525
php_spl

cmake/ext/standard/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ add_feature_info(
6060
# Add library.
6161
################################################################################
6262

63-
add_library(php_standard STATIC)
63+
add_library(php_standard OBJECT)
6464

6565
target_sources(
6666
php_standard

cmake/main/CMakeLists.txt

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,8 @@ target_link_libraries(
163163
INTERFACE
164164
PHP::configuration
165165
php_main
166-
Zend::Zend
166+
# If Zend is STATIC library link as whole archive, otherwise link normally.
167+
$<IF:$<STREQUAL:$<TARGET_PROPERTY:Zend::Zend,TYPE>,STATIC_LIBRARY>,$<LINK_LIBRARY:WHOLE_ARCHIVE,Zend::Zend>,Zend::Zend>
167168
$<$<TARGET_EXISTS:PHP::windows>::PHP::windows>
168169
PHP::extensions
169170
)
@@ -176,9 +177,17 @@ target_sources(
176177
php
177178
INTERFACE
178179
$<TARGET_OBJECTS:php_main>
180+
179181
# Internal functions objects based on the SAPI type.
180182
$<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>
183+
184+
# If Zend is OBJECT library, add library objects as sources.
185+
$<$<STREQUAL:$<TARGET_PROPERTY:Zend::Zend,TYPE>,OBJECT_LIBRARY>:$<TARGET_OBJECTS:Zend::Zend>>
186+
187+
# If Zend is STATIC library, and linking to a STATIC library (SAPI), add
188+
# library objects as sources.
189+
$<$<AND:$<STREQUAL:$<TARGET_PROPERTY:TYPE>,STATIC_LIBRARY>,$<STREQUAL:$<TARGET_PROPERTY:Zend::Zend,TYPE>,STATIC_LIBRARY>>:$<TARGET_OBJECTS:Zend::Zend>>
190+
182191
$<$<TARGET_EXISTS:PHP::windows>:$<TARGET_OBJECTS:PHP::windows>>
183192
)
184193

0 commit comments

Comments
 (0)