sys-process/glances: revbump 3.1.4.1, add missing doc dep
[gentoo.git] / eclass / php-ext-source-r3.eclass
1 # Copyright 1999-2019 Gentoo Authors
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: php-ext-source-r3.eclass
5 # @MAINTAINER:
6 # Gentoo PHP team <php-bugs@gentoo.org>
7 # @SUPPORTED_EAPIS: 6 7
8 # @BLURB: Compile and install standalone PHP extensions.
9 # @DESCRIPTION:
10 # A unified interface for compiling and installing standalone PHP
11 # extensions.
12
13 inherit autotools
14
15 EXPORT_FUNCTIONS src_prepare src_configure src_compile src_install src_test
16
17 case ${EAPI:-0} in
18         6) inherit eapi7-ver ;;
19         7) ;;
20         *)
21                 die "${ECLASS} is not compatible with EAPI=${EAPI}"
22 esac
23
24 # @ECLASS-VARIABLE: PHP_EXT_NAME
25 # @REQUIRED
26 # @DESCRIPTION:
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"
32
33 # @ECLASS-VARIABLE: PHP_EXT_INI
34 # @DESCRIPTION:
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"
38
39 # @ECLASS-VARIABLE: PHP_EXT_ZENDEXT
40 # @DESCRIPTION:
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"
44
45 # @ECLASS-VARIABLE: USE_PHP
46 # @REQUIRED
47 # @DESCRIPTION:
48 # Lists the PHP slots compatible the extension is compatible with.
49 # Example:
50 # @CODE
51 # USE_PHP="php5-6 php7-0"
52 # @CODE
53 [[ -z "${USE_PHP}" ]] && \
54         die "USE_PHP is not set for the php-ext-source-r3 eclass"
55
56 # @ECLASS-VARIABLE: PHP_EXT_OPTIONAL_USE
57 # @DEFAULT_UNSET
58 # @DESCRIPTION:
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.
63
64 # @ECLASS-VARIABLE: PHP_EXT_S
65 # @DESCRIPTION:
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}"
70
71 # @ECLASS-VARIABLE: PHP_EXT_SAPIS
72 # @DESCRIPTION:
73 # A list of SAPIs for which we will install this extension. Formerly
74 # called PHPSAPILIST. The default includes every SAPI currently used in
75 # the tree.
76 [[ -z "${PHP_EXT_SAPIS}" ]] && PHP_EXT_SAPIS="apache2 cli cgi fpm embed phpdbg"
77
78 # @ECLASS-VARIABLE: PHP_INI_NAME
79 # @DESCRIPTION:
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):
84 # @CODE@
85 # PHP_INI_NAME="40-foo"
86 # @CODE@
87 : ${PHP_INI_NAME:=${PHP_EXT_NAME}}
88
89 # @ECLASS-VARIABLE: PHP_EXT_NEEDED_USE
90 # @DEFAULT_UNSET
91 # @DESCRIPTION:
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.
95 # Example:
96 # @CODE
97 # PHP_EXT_NEEDED_USE="mysql?,pdo,pcre(+)"
98 # @CODE
99 #
100 # The PHP dependencies will result in:
101 # @CODE
102 # php_targets_php7-0? ( dev-lang/php:7.0[mysql?,pdo,pcre(+)] )
103 # @CODE
104
105
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}]
120         fi
121         PHPDEPEND+=" php_targets_${_php_target}? ( dev-lang/php:${_php_slot} )"
122 done
123
124 # Don't pollute the environment with our loop variables.
125 unset _php_slot _php_target
126
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"
132
133 RDEPEND="${PHPDEPEND}"
134
135 case ${EAPI:-0} in
136         6) DEPEND="${TOOLDEPS} ${PHPDEPEND}" ;;
137         7) DEPEND="${PHPDEPEND}" ; BDEPEND="${TOOLDEPS} ${PHPDEPEND}" ;;
138 esac
139
140 unset PHPDEPEND TOOLDEPS
141
142 # @ECLASS-VARIABLE: PHP_EXT_SKIP_PHPIZE
143 # @DEFAULT_UNSET
144 # @DESCRIPTION:
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).
148
149 # @ECLASS-VARIABLE: PHP_EXT_SKIP_PATCHES
150 # @DEFAULT_UNSET
151 # @DESCRIPTION:
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.
155
156 # @FUNCTION: php-ext-source-r3_src_prepare
157 # @DESCRIPTION:
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
165                 default
166                 popd > /dev/null || die
167         fi
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
173         done
174 }
175
176 # @FUNCTION: php-ext-source-r3_phpize
177 # @DESCRIPTION:
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}"
186
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
191                         eautoconf --force
192                         eautoheader
193                 else
194                         # Force libtoolize to run and regenerate autotools files (bug
195                         # #220519).
196                         rm aclocal.m4 || die "failed to remove aclocal.m4"
197                         eautoreconf
198                 fi
199         fi
200 }
201
202
203 # @ECLASS-VARIABLE: PHP_EXT_ECONF_ARGS
204 # @DEFAULT_UNSET
205 # @DESCRIPTION:
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.
209
210 # @FUNCTION: php-ext-source-r3_src_configure
211 # @DESCRIPTION:
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
217
218         # Support either a string or an array for PHP_EXT_ECONF_ARGS.
219         local 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[@]}" )
222         else
223                 econf_args=( ${PHP_EXT_ECONF_ARGS} )
224         fi
225
226         local slot
227         for slot in $(php_get_slots); do
228                 php_init_slot_env "${slot}"
229                 econf --with-php-config="${PHPCONFIG}" "${econf_args[@]}"
230         done
231 }
232
233 # @FUNCTION: php-ext-source-r3_src_compile
234 # @DESCRIPTION:
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
240
241         # shm extension creates a semaphore file, bug #173574.
242         addpredict /session_mm_cli0.sem
243         local slot
244         for slot in $(php_get_slots); do
245                 php_init_slot_env "${slot}"
246                 emake
247         done
248 }
249
250 # @FUNCTION: php-ext-source-r3_src_install
251 # @DESCRIPTION:
252 # Install a standard standalone PHP extension. Uses einstalldocs()
253 # to support the DOCS variable/array.
254 php-ext-source-r3_src_install() {
255         local slot
256         for slot in $(php_get_slots); do
257                 php_init_slot_env "${slot}"
258
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"
264
265                 INSTALL_ROOT="${D}" emake install-headers
266         done
267         einstalldocs
268         php-ext-source-r3_createinifiles
269 }
270
271 # @FUNCTION: php-ext-source-r3_src_test
272 # @DESCRIPTION:
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() {
277         local slot
278         for slot in $(php_get_slots); do
279                 php_init_slot_env "${slot}"
280                 NO_INTERACTION="yes" emake test
281         done
282 }
283
284 # @FUNCTION: php_get_slots
285 # @DESCRIPTION:
286 # Get a list of PHP slots contained in both the ebuild's USE_PHP and the
287 # user's PHP_TARGETS.
288 php_get_slots() {
289         local s=""
290         local slot
291         for slot in ${USE_PHP}; do
292                 use php_targets_${slot} && s+=" ${slot/-/.}"
293         done
294         echo $s
295 }
296
297 # @FUNCTION: php_init_slot_env
298 # @USAGE: <slot>
299 # @DESCRIPTION:
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().
305 #
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)
310         local slot="${1}"
311
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}*)"
318
319         EXT_DIR="$(${PHPCONFIG} --extension-dir 2>/dev/null)"
320         PHP_CURRENTSLOT=${1:3}
321
322         PHP_EXT_S="${WORKDIR}/${slot}"
323         cd "${PHP_EXT_S}" || die "failed to change directory to ${PHP_EXT_S}"
324 }
325
326 # @FUNCTION: php_slot_ini_files
327 # @USAGE: <slot>
328 # @INTERNAL
329 # @DESCRIPTION:
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=""
334         local x
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"
338                 fi
339         done
340
341         echo "${slot_ini_files}"
342 }
343
344 # @FUNCTION: php-ext-source-r3_createinifiles
345 # @DESCRIPTION:
346 # Builds INI files for every enabled slot and SAPI.
347 php-ext-source-r3_createinifiles() {
348         local slot
349         for slot in $(php_get_slots); do
350                 php_init_slot_env "${slot}"
351
352                 local file
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}"
357                         fi
358
359                         if [[ -n "${PHP_EXT_INIFILE}" ]] ; then
360                                 cat "${FILESDIR}/${PHP_EXT_INIFILE}" >> "${ED}/${file}" \
361                                         || die "failed to append to ${ED}/${file}"
362
363                                 einfo "Added contents of ${FILESDIR}/${PHP_EXT_INIFILE}" \
364                                           "to ${file}"
365                         fi
366                         inidir="${file/${PHP_INI_NAME}.ini/}"
367                         inidir="${inidir/ext/ext-active}"
368                         dodir "/${inidir}"
369                         dosym "../ext/${PHP_INI_NAME}.ini" "/${file/ext/ext-active}"
370                 done
371         done
372
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
377         #
378         #   require('apcu/apc.php');
379         #
380         PHP_EXT_SHARED_DIR="${EPREFIX}/usr/share/php/${PHP_EXT_NAME}"
381 }
382
383 # @FUNCTION: php-ext-source-r3_addextension
384 # @USAGE: <extension-path> <ini-file>
385 # @INTERNAL
386 # @DESCRIPTION:
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}"
396
397         if [[ "${PHP_EXT_ZENDEXT}" = "yes" ]] ; then
398                 ext_type="zend_extension"
399                 ext_file="${EXT_DIR}/${1}" # Zend extensions need the path...
400         fi
401
402         php-ext-source-r3_addtoinifile "${2}" "${ext_type}" "${ext_file}"
403 }
404
405 # @FUNCTION: php-ext-source-r3_addtoinifile
406 # @USAGE: <relative-ini-path> <setting-or-section-name> [setting-value]
407 # @INTERNAL
408 # @DESCRIPTION:
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.
412 #
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%/*}"
419
420         mkdir -p "${inidir}" || die "failed to create INI directory ${inidir}"
421
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.
426                 my_added="${2}"
427         fi
428         echo "${my_added}" >> "${inifile}" || die "failed to append to ${inifile}"
429         einfo "Added '${my_added}' to /${1}"
430
431         insinto "/${1%/*}"
432         doins "${inifile}"
433 }
434
435 # @FUNCTION: php-ext-source-r3_addtoinifiles
436 # @USAGE: <setting-or-section-name> [setting-value] [message]
437 # @DESCRIPTION:
438 # Add settings to every php.ini file installed by this extension.
439 # You can also add new [Section]s -- see the example below.
440 #
441 # @CODE
442 # Add some settings for the extension:
443 #
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"
447 #
448 # Adding values to a section in php.ini file installed by the extension:
449 #
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"
453 # @CODE
454 php-ext-source-r3_addtoinifiles() {
455         local slot
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}"
459                 done
460         done
461 }