@@ -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 )
169171endmacro ()
170172
171173################################################################################
@@ -557,84 +559,79 @@ function(_php_extensions_eval_options directories)
557559 endforeach ()
558560endfunction ()
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 ()
695724endfunction ()
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 ()
0 commit comments