1 # Copyright 1999-2019 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
4 # @ECLASS: php-ext-source-r3.eclass
6 # Gentoo PHP team <php-bugs@gentoo.org>
7 # @SUPPORTED_EAPIS: 6 7
8 # @BLURB: Compile and install standalone PHP extensions.
10 # A unified interface for compiling and installing standalone PHP
15 EXPORT_FUNCTIONS src_prepare src_configure src_compile src_install src_test
18 6) inherit eapi7-ver ;;
21 die "${ECLASS} is not compatible with EAPI=${EAPI}"
24 # @ECLASS-VARIABLE: PHP_EXT_NAME
27 # The extension name. This must be set, otherwise the eclass dies.
28 # Only automagically set by php-ext-pecl-r3.eclass, so unless your ebuild
29 # inherits that eclass, you must set this manually before inherit.
30 [[ -z "${PHP_EXT_NAME}" ]] && \
31 die "no extension name specified for the php-ext-source-r3 eclass"
33 # @ECLASS-VARIABLE: PHP_EXT_INI
35 # Controls whether or not to add a line to php.ini for the extension.
36 # Defaults to "yes" and should not be changed in most cases.
37 [[ -z "${PHP_EXT_INI}" ]] && PHP_EXT_INI="yes"
39 # @ECLASS-VARIABLE: PHP_EXT_ZENDEXT
41 # Controls whether the extension is a ZendEngine extension or not.
42 # Defaults to "no". If you don't know what this is, you don't need it.
43 [[ -z "${PHP_EXT_ZENDEXT}" ]] && PHP_EXT_ZENDEXT="no"
45 # @ECLASS-VARIABLE: USE_PHP
48 # Lists the PHP slots compatible the extension is compatible with.
51 # USE_PHP="php5-6 php7-0"
53 [[ -z "${USE_PHP}" ]] && \
54 die "USE_PHP is not set for the php-ext-source-r3 eclass"
56 # @ECLASS-VARIABLE: PHP_EXT_OPTIONAL_USE
59 # If set, all of the dependencies added by this eclass will be
60 # conditional on USE=${PHP_EXT_OPTIONAL_USE}. This is needed when
61 # ebuilds have to inherit this eclass unconditionally, but only
62 # actually use it when (for example) the user has USE=php.
64 # @ECLASS-VARIABLE: PHP_EXT_S
66 # The relative location of the temporary build directory for the PHP
67 # extension within the source package. This is useful for packages that
68 # bundle the PHP extension. Defaults to ${S}.
69 [[ -z "${PHP_EXT_S}" ]] && PHP_EXT_S="${S}"
71 # @ECLASS-VARIABLE: PHP_EXT_SAPIS
73 # A list of SAPIs for which we will install this extension. Formerly
74 # called PHPSAPILIST. The default includes every SAPI currently used in
76 [[ -z "${PHP_EXT_SAPIS}" ]] && PHP_EXT_SAPIS="apache2 cli cgi fpm embed phpdbg"
78 # @ECLASS-VARIABLE: PHP_INI_NAME
80 # An optional file name of the saved ini file minis the ini extension
81 # This allows ordering of extensions such that one is loaded before
82 # or after another. Defaults to the PHP_EXT_NAME.
83 # Example (produces 40-foo.ini file):
85 # PHP_INI_NAME="40-foo"
87 : ${PHP_INI_NAME:=${PHP_EXT_NAME}}
89 # @ECLASS-VARIABLE: PHP_EXT_NEEDED_USE
92 # A list of USE flags to append to each PHP target selected
93 # as a valid USE-dependency string. The value should be valid
94 # for all targets so USE defaults may be necessary.
97 # PHP_EXT_NEEDED_USE="mysql?,pdo,pcre(+)"
100 # The PHP dependencies will result in:
102 # php_targets_php7-0? ( dev-lang/php:7.0[mysql?,pdo,pcre(+)] )
106 # Make sure at least one target is installed. First, start a USE
107 # conditional like "php?", but only when PHP_EXT_OPTIONAL_USE is
108 # non-null. The option group "|| (..." is always started here.
109 REQUIRED_USE="${PHP_EXT_OPTIONAL_USE}${PHP_EXT_OPTIONAL_USE:+? ( }|| ( "
110 PHPDEPEND="${PHP_EXT_OPTIONAL_USE}${PHP_EXT_OPTIONAL_USE:+? ( } "
111 for _php_target in ${USE_PHP}; do
112 # Now loop through each USE_PHP target and add the corresponding
113 # dev-lang/php slot to PHPDEPEND.
114 IUSE+=" php_targets_${_php_target}"
115 REQUIRED_USE+="php_targets_${_php_target} "
116 _php_slot=${_php_target/php}
117 _php_slot=${_php_slot/-/.}
118 if [[ ${PHP_EXT_NEEDED_USE} ]] ; then
119 _php_slot+=[${PHP_EXT_NEEDED_USE}]
121 PHPDEPEND+=" php_targets_${_php_target}? ( dev-lang/php:${_php_slot} )"
124 # Don't pollute the environment with our loop variables.
125 unset _php_slot _php_target
127 # Finally, end the optional group that we started before the loop. Close
128 # the USE-conditional if PHP_EXT_OPTIONAL_USE is non-null.
129 REQUIRED_USE+=") ${PHP_EXT_OPTIONAL_USE:+ )}"
130 PHPDEPEND+=" ${PHP_EXT_OPTIONAL_USE:+ )}"
131 TOOLDEPS="sys-devel/m4 sys-devel/libtool"
133 RDEPEND="${PHPDEPEND}"
136 6) DEPEND="${TOOLDEPS} ${PHPDEPEND}" ;;
137 7) DEPEND="${PHPDEPEND}" ; BDEPEND="${TOOLDEPS} ${PHPDEPEND}" ;;
140 unset PHPDEPEND TOOLDEPS
142 # @ECLASS-VARIABLE: PHP_EXT_SKIP_PHPIZE
145 # By default, we run "phpize" in php-ext-source-r3_src_prepare(). Set
146 # PHP_EXT_SKIP_PHPIZE="yes" in your ebuild if you do not want to run
147 # phpize (and the autoreconf that becomes necessary afterwards).
149 # @ECLASS-VARIABLE: PHP_EXT_SKIP_PATCHES
152 # By default, we run default_src_prepare to PHP_EXT_S.
153 # Set PHP_EXT_SKIP_PATCHES="yes" in your ebuild if you
154 # want to apply patches yourself.
156 # @FUNCTION: php-ext-source-r3_src_prepare
158 # Runs the default src_prepare() for PATCHES/eapply_user() support (optional),
159 # and for each PHP slot, makes a copy of sources, initializes the environment,
160 # and calls php-ext-source-r3_phpize().
161 php-ext-source-r3_src_prepare() {
162 local slot orig_s="${PHP_EXT_S}"
163 if [[ "${PHP_EXT_SKIP_PATCHES}" != 'yes' ]] ; then
164 pushd "${orig_s}" > /dev/null || die
166 popd > /dev/null || die
168 for slot in $(php_get_slots); do
169 cp --recursive --preserve "${orig_s}" "${WORKDIR}/${slot}" || \
170 die "failed to copy sources from ${orig_s} to ${WORKDIR}/${slot}"
171 php_init_slot_env "${slot}"
172 php-ext-source-r3_phpize
176 # @FUNCTION: php-ext-source-r3_phpize
178 # Subject to PHP_EXT_SKIP_PHPIZE, this function runs phpize and
179 # autoreconf in a manner that avoids warnings.
180 php-ext-source-r3_phpize() {
181 if [[ "${PHP_EXT_SKIP_PHPIZE}" != 'yes' ]] ; then
182 # Create configure out of config.m4. We use autotools_run_tool
183 # to avoid some warnings about WANT_AUTOCONF and
184 # WANT_AUTOMAKE (see bugs #329071 and #549268).
185 autotools_run_tool "${PHPIZE}"
187 # PHP >=7.4 no longer works with eautoreconf
188 if ver_test $PHP_CURRENTSLOT -ge 7.4 ; then
189 rm -fr aclocal.m4 autom4te.cache config.cache \
190 configure main/php_config.h.in || die
194 # Force libtoolize to run and regenerate autotools files (bug
196 rm aclocal.m4 || die "failed to remove aclocal.m4"
203 # @ECLASS-VARIABLE: PHP_EXT_ECONF_ARGS
206 # Set this in the ebuild to pass additional configure options to
207 # econf. Formerly called my_conf. Either a string or an array of
208 # --flag=value parameters is supported.
210 # @FUNCTION: php-ext-source-r3_src_configure
212 # Takes care of standard configure for PHP extensions (modules).
213 php-ext-source-r3_src_configure() {
214 # net-snmp creates these, bug #385403.
215 addpredict /usr/share/snmp/mibs/.index
216 addpredict /var/lib/net-snmp/mib_indexes
218 # Support either a string or an array for PHP_EXT_ECONF_ARGS.
220 if [[ -n "${PHP_EXT_ECONF_ARGS}" && $(declare -p PHP_EXT_ECONF_ARGS) == "declare -a"* ]]; then
221 econf_args=( "${PHP_EXT_ECONF_ARGS[@]}" )
223 econf_args=( ${PHP_EXT_ECONF_ARGS} )
227 for slot in $(php_get_slots); do
228 php_init_slot_env "${slot}"
229 econf --with-php-config="${PHPCONFIG}" "${econf_args[@]}"
233 # @FUNCTION: php-ext-source-r3_src_compile
235 # Compile a standard standalone PHP extension.
236 php-ext-source-r3_src_compile() {
237 # net-snmp creates these, bug #324739.
238 addpredict /usr/share/snmp/mibs/.index
239 addpredict /var/lib/net-snmp/mib_indexes
241 # shm extension creates a semaphore file, bug #173574.
242 addpredict /session_mm_cli0.sem
244 for slot in $(php_get_slots); do
245 php_init_slot_env "${slot}"
250 # @FUNCTION: php-ext-source-r3_src_install
252 # Install a standard standalone PHP extension. Uses einstalldocs()
253 # to support the DOCS variable/array.
254 php-ext-source-r3_src_install() {
256 for slot in $(php_get_slots); do
257 php_init_slot_env "${slot}"
259 # Strip $EPREFIX from $EXT_DIR before calling doexe (which
260 # handles EPREFIX itself). Shared libs are +x by convention,
261 # although nothing seems to depend on that.
262 exeinto "${EXT_DIR#$EPREFIX}"
263 doexe "modules/${PHP_EXT_NAME}.so"
265 INSTALL_ROOT="${D}" emake install-headers
268 php-ext-source-r3_createinifiles
271 # @FUNCTION: php-ext-source-r3_src_test
273 # Run tests delivered with the standalone PHP extension. Phpize will have generated
274 # a run-tests.php file to be executed by `make test`. We only need to
275 # force the test suite to run in non-interactive mode.
276 php-ext-source-r3_src_test() {
278 for slot in $(php_get_slots); do
279 php_init_slot_env "${slot}"
280 NO_INTERACTION="yes" emake test
284 # @FUNCTION: php_get_slots
286 # Get a list of PHP slots contained in both the ebuild's USE_PHP and the
287 # user's PHP_TARGETS.
291 for slot in ${USE_PHP}; do
292 use php_targets_${slot} && s+=" ${slot/-/.}"
297 # @FUNCTION: php_init_slot_env
300 # Takes a slot name, and initializes some global variables to values
301 # corresponding to that slot. For example, it sets the path to the "php"
302 # and "phpize" binaries, which will differ for each slot. This function
303 # is intended to be called while looping through a list of slots
304 # obtained from php_get_slots().
306 # Calling this function will change the working directory to the
307 # temporary build directory for the given slot.
308 php_init_slot_env() {
309 local libdir=$(get_libdir)
312 PHPPREFIX="${EPREFIX}/usr/${libdir}/${slot}"
313 PHPIZE="${PHPPREFIX}/bin/phpize"
314 PHPCONFIG="${PHPPREFIX}/bin/php-config"
315 PHPCLI="${PHPPREFIX}/bin/php"
316 PHPCGI="${PHPPREFIX}/bin/php-cgi"
317 PHP_PKG="$(best_version =dev-lang/php-${1:3}*)"
319 EXT_DIR="$(${PHPCONFIG} --extension-dir 2>/dev/null)"
320 PHP_CURRENTSLOT=${1:3}
322 PHP_EXT_S="${WORKDIR}/${slot}"
323 cd "${PHP_EXT_S}" || die "failed to change directory to ${PHP_EXT_S}"
326 # @FUNCTION: php_slot_ini_files
330 # Output a list of relative paths to INI files for the given
331 # slot. Usually there will be one INI file per SAPI.
332 php_slot_ini_files() {
333 local slot_ini_files=""
335 for x in ${PHP_EXT_SAPIS} ; do
336 if [[ -f "${EPREFIX}/etc/php/${x}-${1}/php.ini" ]] ; then
337 slot_ini_files+=" etc/php/${x}-${1}/ext/${PHP_INI_NAME}.ini"
341 echo "${slot_ini_files}"
344 # @FUNCTION: php-ext-source-r3_createinifiles
346 # Builds INI files for every enabled slot and SAPI.
347 php-ext-source-r3_createinifiles() {
349 for slot in $(php_get_slots); do
350 php_init_slot_env "${slot}"
353 for file in $(php_slot_ini_files "${slot}") ; do
354 if [[ "${PHP_EXT_INI}" = "yes" ]] ; then
355 # Add the needed lines to the <ext>.ini files
356 php-ext-source-r3_addextension "${PHP_EXT_NAME}.so" "${file}"
359 if [[ -n "${PHP_EXT_INIFILE}" ]] ; then
360 cat "${FILESDIR}/${PHP_EXT_INIFILE}" >> "${ED}/${file}" \
361 || die "failed to append to ${ED}/${file}"
363 einfo "Added contents of ${FILESDIR}/${PHP_EXT_INIFILE}" \
366 inidir="${file/${PHP_INI_NAME}.ini/}"
367 inidir="${inidir/ext/ext-active}"
369 dosym "../ext/${PHP_INI_NAME}.ini" "/${file/ext/ext-active}"
373 # A location where PHP code for this extension can be stored,
374 # independent of the PHP or extension versions. This will be part of
375 # PHP's include_path, configured in php.ini. For example, pecl-apcu
376 # installs an "apc.php" file which you are supposed to load with
378 # require('apcu/apc.php');
380 PHP_EXT_SHARED_DIR="${EPREFIX}/usr/share/php/${PHP_EXT_NAME}"
383 # @FUNCTION: php-ext-source-r3_addextension
384 # @USAGE: <extension-path> <ini-file>
387 # Add a line to an INI file that will enable the given extension. The
388 # first parameter is the path to the extension (.so) file, and the
389 # second parameter is the name of the INI file in which it should be
390 # loaded. This function determines the setting name (either
391 # "extension=..." or "zend_extension=...") and then calls
392 # php-ext-source-r3_addtoinifile to do the actual work.
393 php-ext-source-r3_addextension() {
394 local ext_type="extension"
395 local ext_file="${1}"
397 if [[ "${PHP_EXT_ZENDEXT}" = "yes" ]] ; then
398 ext_type="zend_extension"
399 ext_file="${EXT_DIR}/${1}" # Zend extensions need the path...
402 php-ext-source-r3_addtoinifile "${2}" "${ext_type}" "${ext_file}"
405 # @FUNCTION: php-ext-source-r3_addtoinifile
406 # @USAGE: <relative-ini-path> <setting-or-section-name> [setting-value]
409 # Add a setting=value to one INI file. The first argument is the
410 # relative path to the INI file. The second argument is the setting
411 # name, and the third argument is its value.
413 # You can also pass "[Section]" as the second parameter, to create a new
414 # section in the INI file. In that case, the third parameter (which
415 # would otherwise be the value of the setting) is ignored.
416 php-ext-source-r3_addtoinifile() {
417 local inifile="${WORKDIR}/${1}"
418 local inidir="${inifile%/*}"
420 mkdir -p "${inidir}" || die "failed to create INI directory ${inidir}"
422 # Are we adding the name of a section? Assume not by default.
423 local my_added="${2}=${3}"
424 if [[ ${2:0:1} == "[" ]] ; then
425 # Ok, it's a section name.
428 echo "${my_added}" >> "${inifile}" || die "failed to append to ${inifile}"
429 einfo "Added '${my_added}' to /${1}"
435 # @FUNCTION: php-ext-source-r3_addtoinifiles
436 # @USAGE: <setting-or-section-name> [setting-value] [message]
438 # Add settings to every php.ini file installed by this extension.
439 # You can also add new [Section]s -- see the example below.
442 # Add some settings for the extension:
444 # php-ext-source-r3_addtoinifiles "zend_optimizer.optimization_level" "15"
445 # php-ext-source-r3_addtoinifiles "zend_optimizer.enable_loader" "0"
446 # php-ext-source-r3_addtoinifiles "zend_optimizer.disable_licensing" "0"
448 # Adding values to a section in php.ini file installed by the extension:
450 # php-ext-source-r3_addtoinifiles "[Debugger]"
451 # php-ext-source-r3_addtoinifiles "debugger.enabled" "on"
452 # php-ext-source-r3_addtoinifiles "debugger.profiler_enabled" "on"
454 php-ext-source-r3_addtoinifiles() {
456 for slot in $(php_get_slots); do
457 for file in $(php_slot_ini_files "${slot}") ; do
458 php-ext-source-r3_addtoinifile "${file}" "${1}" "${2}"