Skip to content

Commit b39ab2a

Browse files
committed
Automate COMPILE_DL_<EXTENSION> macros
To simplify extensions config.h templates this now loops through the php-src extensions configurations and sets the `COMPILE_DL_<EXTENSION>` preprocessor macros automatically. Additionally, some configuration is refactored and regex in the Version.cmake fixed a bit.
1 parent 58e0ac6 commit b39ab2a

File tree

84 files changed

+127
-341
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+127
-341
lines changed

cmake/cmake/Version.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ Variables:
1313

1414
include_guard(GLOBAL)
1515

16+
# Set the PHP_VERSION_* variables from configure.ac.
1617
block(PROPAGATE PHP_VERSION)
17-
# Set the PHP_VERSION_* variables from configure.ac.
1818
set(regex "^AC_INIT\\(\\[PHP\\],\\[([0-9]+\.[0-9]+\.[0-9]+)([^\]]*)")
1919
file(STRINGS ${CMAKE_CURRENT_LIST_DIR}/../configure.ac _ REGEX "${regex}")
2020

@@ -54,7 +54,7 @@ function(_php_post_project)
5454

5555
# Read PHP API version.
5656
set(regex "^[ \t]*#[ \t]*define[ \t]+PHP_API_VERSION[ \t]+([0-9]+)")
57-
file(STRINGS main/php.h _ REGEX "^${regex}")
57+
file(STRINGS main/php.h _ REGEX "${regex}")
5858

5959
cmake_policy(GET CMP0159 policy)
6060
if(CMAKE_VERSION VERSION_LESS 3.29 OR NOT policy STREQUAL NEW)

cmake/cmake/modules/PHP/Extensions.cmake

Lines changed: 120 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -143,29 +143,31 @@ macro(php_extensions_add directory)
143143
# Evaluate options of extensions.
144144
_php_extensions_eval_options("${directories}")
145145

146-
# Configure extensions and their dependencies.
147-
_php_extensions_configure("${directories}")
148-
149146
# Add subdirectories of extensions.
150147
foreach(dir ${directories})
151148
cmake_path(GET dir FILENAME extension)
149+
150+
# Preconfigure extension and its dependencies.
151+
_php_extensions_preconfigure("${extension}" "${dir}")
152+
152153
message(STATUS "Checking ${extension} extension")
153154
list(APPEND CMAKE_MESSAGE_CONTEXT "ext/${extension}")
154-
unset(extension)
155155

156156
add_subdirectory("${dir}")
157157

158158
list(POP_BACK CMAKE_MESSAGE_CONTEXT)
159159

160-
_php_extensions_post_configure("${dir}")
160+
_php_extensions_postconfigure("${extension}")
161161
endforeach()
162162

163+
unset(directories)
164+
unset(extension)
165+
163166
# Validate options of extensions and their dependencies.
164-
get_cmake_property(extensions PHP_EXTENSIONS)
165-
_php_extensions_validate("${extensions}")
167+
_php_extensions_validate()
166168

167-
unset(directories)
168-
unset(extensions)
169+
# Reconfigure all enabled extensions at the end of the configuration phase.
170+
cmake_language(DEFER CALL _php_extensions_configure_headers)
169171
endmacro()
170172

171173
################################################################################
@@ -557,84 +559,79 @@ function(_php_extensions_eval_options directories)
557559
endforeach()
558560
endfunction()
559561

560-
# Configure extensions according to their dependencies.
561-
function(_php_extensions_configure directories)
562-
foreach(dir ${directories})
563-
cmake_path(GET dir FILENAME extension)
564-
string(TOUPPER "${extension}" extension_upper)
565-
566-
if(NOT EXT_${extension_upper})
567-
continue()
568-
endif()
569-
570-
# Mark shared option variable as advanced.
571-
if(DEFINED EXT_${extension_upper}_SHARED)
572-
mark_as_advanced(EXT_${extension_upper}_SHARED)
573-
endif()
574-
575-
_php_extensions_get_dependencies("${dir}" dependencies)
576-
577-
# If extension is enabled and one of its dependencies is built as a shared
578-
# library, configure extension also as a shared library.
579-
foreach(dependency ${dependencies})
580-
string(TOUPPER "${dependency}" dependency_upper)
581-
582-
if(EXT_${dependency_upper}_SHARED AND NOT EXT_${extension_upper}_SHARED)
583-
message(
584-
WARNING
585-
"The '${extension}' extension must be built as a shared library due "
586-
"to its dependency on the '${dependency}' extension, which is "
587-
"configured as shared. The 'EXT_${extension_upper}_SHARED' option "
588-
"has been automatically set to 'ON'."
589-
)
590-
591-
set(
592-
EXT_${extension_upper}_SHARED
593-
ON
594-
CACHE BOOL
595-
"Build the ${extension} extension as a shared library"
596-
FORCE
597-
)
562+
# Configure extension according to its dependencies.
563+
function(_php_extensions_preconfigure extension dir)
564+
string(TOUPPER "${extension}" extension_upper)
598565

599-
break()
600-
endif()
601-
endforeach()
566+
if(NOT EXT_${extension_upper})
567+
return()
568+
endif()
602569

603-
get_cmake_property(always_enabled_extensions PHP_ALWAYS_ENABLED_EXTENSIONS)
570+
# Mark shared option variable as advanced.
571+
if(DEFINED EXT_${extension_upper}_SHARED)
572+
mark_as_advanced(EXT_${extension_upper}_SHARED)
573+
endif()
604574

605-
# If extension is enabled, enable also all its dependencies.
606-
foreach(dependency ${dependencies})
607-
string(TOUPPER "${dependency}" dependency_upper)
575+
_php_extensions_get_dependencies("${dir}" dependencies)
608576

609-
if(
610-
EXT_${dependency_upper}
611-
OR dependency IN_LIST always_enabled_extensions
612-
)
613-
continue()
614-
endif()
577+
# If extension is enabled and one of its dependencies is built as a shared
578+
# library, configure extension also as a shared library.
579+
foreach(dependency ${dependencies})
580+
string(TOUPPER "${dependency}" dependency_upper)
615581

582+
if(EXT_${dependency_upper}_SHARED AND NOT EXT_${extension_upper}_SHARED)
616583
message(
617584
WARNING
618-
"The '${extension}' extension requires the '${dependency}' extension. "
619-
"The 'EXT_${dependency_upper}' option has been automatically set to "
620-
"'ON'."
585+
"The '${extension}' extension must be built as a shared library due "
586+
"to its dependency on the '${dependency}' extension, which is "
587+
"configured as shared. The 'EXT_${extension_upper}_SHARED' option "
588+
"has been automatically set to 'ON'."
621589
)
622590

623591
set(
624-
EXT_${dependency_upper}
592+
EXT_${extension_upper}_SHARED
625593
ON
626594
CACHE BOOL
627-
"Enable the ${dependency} extension"
595+
"Build the ${extension} extension as a shared library"
628596
FORCE
629597
)
630-
endforeach()
598+
599+
break()
600+
endif()
631601
endforeach()
632-
endfunction()
633602

634-
# Configure extension after extension CMakeLists.txt is added.
635-
function(_php_extensions_post_configure directory)
636-
cmake_path(GET directory FILENAME extension)
603+
get_cmake_property(always_enabled_extensions PHP_ALWAYS_ENABLED_EXTENSIONS)
604+
605+
# If extension is enabled, enable also all its dependencies.
606+
foreach(dependency ${dependencies})
607+
string(TOUPPER "${dependency}" dependency_upper)
608+
609+
if(
610+
EXT_${dependency_upper}
611+
OR dependency IN_LIST always_enabled_extensions
612+
)
613+
continue()
614+
endif()
615+
616+
message(
617+
WARNING
618+
"The '${extension}' extension requires the '${dependency}' extension. "
619+
"The 'EXT_${dependency_upper}' option has been automatically set to "
620+
"'ON'."
621+
)
622+
623+
set(
624+
EXT_${dependency_upper}
625+
ON
626+
CACHE BOOL
627+
"Enable the ${dependency} extension"
628+
FORCE
629+
)
630+
endforeach()
631+
endfunction()
637632

633+
# Postconfigure extension right after it has been configured.
634+
function(_php_extensions_postconfigure extension)
638635
if(NOT TARGET php_${extension})
639636
return()
640637
endif()
@@ -663,39 +660,73 @@ function(_php_extensions_post_configure directory)
663660
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/${PHP_INCLUDE_PREFIX}/ext/${extension}
664661
)
665662

666-
# Check if extension is always enabled.
667-
get_cmake_property(extensions PHP_ALWAYS_ENABLED_EXTENSIONS)
668-
if(extension IN_LIST extensions)
663+
# Configure shared extension.
664+
get_target_property(type php_${extension} TYPE)
665+
if(NOT type MATCHES "^(MODULE|SHARED)_LIBRARY$")
669666
return()
670667
endif()
671668

672-
get_target_property(extension_type php_${extension} TYPE)
669+
target_compile_definitions(php_${extension} PRIVATE ZEND_COMPILE_DL_EXT=1)
673670

674-
if(NOT extension_type MATCHES "^(MODULE|SHARED)_LIBRARY$")
675-
return()
676-
endif()
671+
set_target_properties(
672+
php_${extension}
673+
PROPERTIES
674+
POSITION_INDEPENDENT_CODE TRUE
675+
)
677676

678-
# Set location where to put shared extensions.
677+
# Set build-phase location for shared extensions.
679678
get_target_property(location php_${extension} LIBRARY_OUTPUT_DIRECTORY)
680679
if(NOT location)
681-
set_property(TARGET php_${extension}
680+
set_property(
681+
TARGET php_${extension}
682682
PROPERTY LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/modules"
683683
)
684684
endif()
685+
endfunction()
685686

686-
# Define COMPILE_DL_<extension-name> symbol for php_config.h to indicate
687-
# extension is built as a shared library and add compile definitions.
688-
string(TOUPPER "COMPILE_DL_${extension}" symbol)
689-
set(
690-
${symbol} 1
691-
CACHE INTERNAL
692-
"Whether the PHP extension '${extension}' is built as a dynamic module."
693-
)
694-
target_compile_definitions(php_${extension} PRIVATE ZEND_COMPILE_DL_EXT=1)
687+
# Prepend COMPILE_DL_<EXTENSION> macros to extensions configuration headers and
688+
# define them for shared extensions.
689+
function(_php_extensions_configure_headers)
690+
get_cmake_property(extensions PHP_EXTENSIONS)
691+
foreach(extension ${extensions})
692+
if(NOT TARGET php_${extension})
693+
continue()
694+
endif()
695+
696+
string(TOUPPER "COMPILE_DL_${extension}" macro)
697+
698+
get_target_property(type php_${extension} TYPE)
699+
if(type MATCHES "^(MODULE|SHARED)_LIBRARY$")
700+
set(${macro} 1)
701+
endif()
702+
703+
# Prepare config.h template.
704+
string(
705+
JOIN
706+
""
707+
template
708+
"/* Define to 1 if the PHP extension '@extension@' is built as a dynamic "
709+
"module. */\n"
710+
"#cmakedefine ${macro} 1\n"
711+
)
712+
713+
get_target_property(binaryDir php_${extension} BINARY_DIR)
714+
set(current "")
715+
if(EXISTS ${binaryDir}/config.h)
716+
file(READ ${binaryDir}/config.h current)
717+
endif()
718+
719+
string(STRIP "${template}\n${current}" config)
720+
721+
# Finalize extension's config.h header file.
722+
file(CONFIGURE OUTPUT ${binaryDir}/config.h CONTENT "${config}\n")
723+
endforeach()
695724
endfunction()
696725

697726
# Validate extensions and their dependencies defined via add_dependencies().
698-
function(_php_extensions_validate extensions)
727+
function(_php_extensions_validate)
728+
get_cmake_property(extensions PHP_EXTENSIONS)
729+
699730
foreach(extension ${extensions})
700731
if(NOT TARGET php_${extension})
701732
continue()

cmake/ext/bcmath/config.cmake.h.in

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
1-
/* Define to 1 if the PHP extension 'bcmath' is built as a dynamic module. */
2-
#cmakedefine COMPILE_DL_BCMATH 1
3-
41
/* Define to 1 if the PHP extension 'bcmath' is available. */
52
#cmakedefine HAVE_BCMATH 1

cmake/ext/bz2/config.cmake.h.in

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
1-
/* Define to 1 if the PHP extension 'bz2' is built as a dynamic module. */
2-
#cmakedefine COMPILE_DL_BZ2 1
3-
41
/* Define to 1 if the PHP extension 'bz2' is available. */
52
#cmakedefine HAVE_BZ2 1
Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,2 @@
1-
/* Define to 1 if the PHP extension 'calendar' is built as a dynamic module.
2-
*/
3-
#cmakedefine COMPILE_DL_CALENDAR 1
4-
51
/* Define to 1 if the PHP extension 'calendar' is available. */
62
#cmakedefine HAVE_CALENDAR 1

cmake/ext/com_dotnet/config.cmake.h.in

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
/* Define to 1 if the PHP extension 'com_dotnet' is built as a dynamic module.
2-
*/
3-
#cmakedefine COMPILE_DL_COM_DOTNET 1
4-
51
/* Define to 1 if the PHP extension 'com_dotnet' is available. */
62
#cmakedefine HAVE_COM_DOTNET 1
73

cmake/ext/ctype/config.cmake.h.in

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,2 @@
1-
/* Define to 1 if the PHP extension 'ctype' is built as a dynamic module. */
2-
#cmakedefine COMPILE_DL_CTYPE 1
3-
41
/* Define to 1 if the PHP extension 'ctype' is available. */
52
#cmakedefine HAVE_CTYPE 1

cmake/ext/curl/config.cmake.h.in

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
/* Define to 1 if the PHP extension 'curl' is built as a dynamic module. */
2-
#cmakedefine COMPILE_DL_CURL 1
3-
41
/* Define to 1 if the PHP extension 'curl' is available. */
52
#cmakedefine HAVE_CURL 1
63

cmake/ext/date/config.cmake.h.in

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
/* Define to 1 if the PHP extension 'date' is built as a dynamic module. */
2-
#cmakedefine COMPILE_DL_DATE 1
3-
41
/* Define to 1 if you have the <io.h> header file. */
52
#cmakedefine HAVE_IO_H 1
63

cmake/ext/dba/config.cmake.h.in

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
/* The cdb handler header file. */
22
#cmakedefine CDB_INCLUDE_FILE "@CDB_INCLUDE_FILE@"
33

4-
/* Define to 1 if the PHP extension 'dba' is built as a dynamic module. */
5-
#cmakedefine COMPILE_DL_DBA 1
6-
74
/* The DB1 handler header file. */
85
#cmakedefine DB1_INCLUDE_FILE "@DB1_INCLUDE_FILE@"
96

0 commit comments

Comments
 (0)