media-libs/portaudio: Version bump
[gentoo.git] / eclass / php-ext-source-r3.eclass
1 # Copyright 1999-2016 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 # $Id$
4
5 # @ECLASS: php-ext-source-r3.eclass
6 # @MAINTAINER:
7 # Gentoo PHP team <php-bugs@gentoo.org>
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_unpack src_prepare src_configure src_compile src_install src_test
16
17 case ${EAPI} in
18         6) ;;
19         *)
20                 die "${ECLASS} is not compatible with EAPI=${EAPI}"
21 esac
22
23 # @ECLASS-VARIABLE: PHP_EXT_NAME
24 # @REQUIRED
25 # @DESCRIPTION:
26 # The extension name. This must be set, otherwise the eclass dies.
27 # Only automagically set by php-ext-pecl-r3.eclass, so unless your ebuild
28 # inherits that eclass, you must set this manually before inherit.
29 [[ -z "${PHP_EXT_NAME}" ]] && \
30         die "no extension name specified for the php-ext-source-r3 eclass"
31
32 # @ECLASS-VARIABLE: PHP_EXT_INI
33 # @DESCRIPTION:
34 # Controls whether or not to add a line to php.ini for the extension.
35 # Defaults to "yes" and should not be changed in most cases.
36 [[ -z "${PHP_EXT_INI}" ]] && PHP_EXT_INI="yes"
37
38 # @ECLASS-VARIABLE: PHP_EXT_ZENDEXT
39 # @DESCRIPTION:
40 # Controls whether the extension is a ZendEngine extension or not.
41 # Defaults to "no". If you don't know what this is, you don't need it.
42 [[ -z "${PHP_EXT_ZENDEXT}" ]] && PHP_EXT_ZENDEXT="no"
43
44 # @ECLASS-VARIABLE: USE_PHP
45 # @REQUIRED
46 # @DESCRIPTION:
47 # Lists the PHP slots compatible the extension is compatible with.
48 # Example:
49 # @CODE
50 # USE_PHP="php5-6 php7-0"
51 # @CODE
52 [[ -z "${USE_PHP}" ]] && \
53         die "USE_PHP is not set for the php-ext-source-r3 eclass"
54
55 # @ECLASS-VARIABLE: PHP_EXT_OPTIONAL_USE
56 # @DEFAULT_UNSET
57 # @DESCRIPTION:
58 # If set, all of the dependencies added by this eclass will be
59 # conditional on USE=${PHP_EXT_OPTIONAL_USE}. This is needed when
60 # ebuilds have to inherit this eclass unconditionally, but only
61 # actually use it when (for example) the user has USE=php.
62
63 # @ECLASS-VARIABLE: PHP_EXT_S
64 # @DESCRIPTION:
65 # The relative location of the temporary build directory for the PHP
66 # extension within the source package. This is useful for packages that
67 # bundle the PHP extension. Defaults to ${S}.
68 [[ -z "${PHP_EXT_S}" ]] && PHP_EXT_S="${S}"
69
70 # @ECLASS-VARIABLE: PHP_EXT_SAPIS
71 # @DESCRIPTION:
72 # A list of SAPIs for which we will install this extension. Formerly
73 # called PHPSAPILIST. The default includes every SAPI currently used in
74 # the tree.
75 [[ -z "${PHP_EXT_SAPIS}" ]] && PHP_EXT_SAPIS="apache2 cli cgi fpm embed phpdbg"
76
77
78 # Make sure at least one target is installed. First, start a USE
79 # conditional like "php?", but only when PHP_EXT_OPTIONAL_USE is
80 # non-null. The option group "|| (..." is always started here.
81 REQUIRED_USE="${PHP_EXT_OPTIONAL_USE}${PHP_EXT_OPTIONAL_USE:+? ( }|| ( "
82 for _php_target in ${USE_PHP}; do
83         # Now loop through each USE_PHP target and add the corresponding
84         # dev-lang/php slot to PHPDEPEND.
85         IUSE+=" php_targets_${_php_target}"
86         REQUIRED_USE+="php_targets_${_php_target} "
87         _php_slot=${_php_target/php}
88         _php_slot=${_php_slot/-/.}
89         PHPDEPEND+=" php_targets_${_php_target}? ( dev-lang/php:${_php_slot} )"
90 done
91
92 # Don't pollute the environment with our loop variables.
93 unset _php_slot _php_target
94
95 # Finally, end the optional group that we started before the loop. Close
96 # the USE-conditional if PHP_EXT_OPTIONAL_USE is non-null.
97 REQUIRED_USE+=") ${PHP_EXT_OPTIONAL_USE:+ )}"
98
99 RDEPEND="${RDEPEND}
100         ${PHP_EXT_OPTIONAL_USE}${PHP_EXT_OPTIONAL_USE:+? ( }
101         ${PHPDEPEND}
102         ${PHP_EXT_OPTIONAL_USE:+ )}"
103
104 DEPEND="${DEPEND}
105         sys-devel/m4
106         sys-devel/libtool
107         ${PHP_EXT_OPTIONAL_USE}${PHP_EXT_OPTIONAL_USE:+? ( }
108         ${PHPDEPEND}
109         ${PHP_EXT_OPTIONAL_USE:+ )}
110 "
111
112 # @ECLASS-VARIABLE: PHP_EXT_SKIP_PHPIZE
113 # @DEFAULT_UNSET
114 # @DESCRIPTION:
115 # By default, we run "phpize" in php-ext-source-r3_src_unpack(). Set
116 # PHP_EXT_SKIP_PHPIZE="yes" in your ebuild if you do not want to run
117 # phpize (and the autoreconf that becomes necessary afterwards).
118
119 # @FUNCTION: php-ext-source-r3_src_unpack
120 # @DESCRIPTION:
121 # Runs the default src_unpack and then makes a copy for each PHP slot.
122 php-ext-source-r3_src_unpack() {
123         default
124
125         local slot orig_s="${PHP_EXT_S}"
126         for slot in $(php_get_slots); do
127                 cp --recursive --preserve "${orig_s}" "${WORKDIR}/${slot}" || \
128                         die "failed to copy sources from ${orig_s} to ${WORKDIR}/${slot}"
129         done
130 }
131
132
133 # @FUNCTION: php-ext-source-r3_src_prepare
134 # @DESCRIPTION:
135 # For each PHP slot, we initialize the environment, run the default
136 # src_prepare() for PATCHES/eapply_user support, and then call
137 # php-ext-source-r3_phpize.
138 php-ext-source-r3_src_prepare() {
139         for slot in $(php_get_slots); do
140                 php_init_slot_env "${slot}"
141                 default
142                 php-ext-source-r3_phpize
143         done
144 }
145
146 # @FUNCTION: php-ext-source-r3_phpize
147 # @DESCRIPTION:
148 # Subject to PHP_EXT_SKIP_PHPIZE, this function runs phpize and
149 # autoreconf in a manner that avoids warnings.
150 php-ext-source-r3_phpize() {
151         if [[ "${PHP_EXT_SKIP_PHPIZE}" != 'yes' ]] ; then
152                 # Create configure out of config.m4. We use autotools_run_tool
153                 # to avoid some warnings about WANT_AUTOCONF and
154                 # WANT_AUTOMAKE (see bugs #329071 and #549268).
155                 autotools_run_tool "${PHPIZE}"
156
157                 # Force libtoolize to run and regenerate autotools files (bug
158                 # #220519).
159                 rm aclocal.m4 || die "failed to remove aclocal.m4"
160                 eautoreconf
161         fi
162 }
163
164
165 # @ECLASS-VARIABLE: PHP_EXT_ECONF_ARGS
166 # @DEFAULT_UNSET
167 # @DESCRIPTION:
168 # Set this in the ebuild to pass additional configure options to
169 # econf. Formerly called my_conf. Either a string or an array of
170 # --flag=value parameters is supported.
171
172 # @FUNCTION: php-ext-source-r3_src_configure
173 # @DESCRIPTION:
174 # Takes care of standard configure for PHP extensions (modules).
175 php-ext-source-r3_src_configure() {
176         # net-snmp creates these, bug #385403.
177         addpredict /usr/share/snmp/mibs/.index
178         addpredict /var/lib/net-snmp/mib_indexes
179
180         # Support either a string or an array for PHP_EXT_ECONF_ARGS.
181         local econf_args
182         if [[ $(declare -p PHP_EXT_ECONF_ARGS) == "declare -a"* ]]; then
183                 econf_args=( "${PHP_EXT_ECONF_ARGS[@]}" )
184         else
185                 econf_args=( ${PHP_EXT_ECONF_ARGS} )
186         fi
187
188         local slot
189         for slot in $(php_get_slots); do
190                 php_init_slot_env "${slot}"
191                 econf --with-php-config="${PHPCONFIG}" "${econf_args[@]}"
192         done
193 }
194
195 # @FUNCTION: php-ext-source-r3_src_compile
196 # @DESCRIPTION:
197 # Compile a standard standalone PHP extension.
198 php-ext-source-r3_src_compile() {
199         # net-snmp creates these, bug #324739.
200         addpredict /usr/share/snmp/mibs/.index
201         addpredict /var/lib/net-snmp/mib_indexes
202
203         # shm extension creates a semaphore file, bug #173574.
204         addpredict /session_mm_cli0.sem
205         local slot
206         for slot in $(php_get_slots); do
207                 php_init_slot_env "${slot}"
208                 emake
209         done
210 }
211
212 # @FUNCTION: php-ext-source-r3_src_install
213 # @DESCRIPTION:
214 # Install a standard standalone PHP extension. Uses einstalldocs()
215 # to support the DOCS variable/array.
216 php-ext-source-r3_src_install() {
217         local slot
218         for slot in $(php_get_slots); do
219                 php_init_slot_env "${slot}"
220
221                 # Strip $EPREFIX from $EXT_DIR before calling doexe (which
222                 # handles EPREFIX itself). Shared libs are +x by convention,
223                 # although nothing seems to depend on that.
224                 exeinto "${EXT_DIR#$EPREFIX}"
225                 doexe "modules/${PHP_EXT_NAME}.so"
226
227                 INSTALL_ROOT="${D}" emake install-headers
228         done
229         einstalldocs
230         php-ext-source-r3_createinifiles
231 }
232
233 # @FUNCTION: php-ext-source-r3_src_test
234 # @DESCRIPTION:
235 # Run tests delivered with the standalone PHP extension. Phpize will have generated
236 # a run-tests.php file to be executed by `make test`. We only need to
237 # force the test suite to run in non-interactive mode.
238 php-ext-source-r3_src_test() {
239         local slot
240         for slot in $(php_get_slots); do
241                 php_init_slot_env "${slot}"
242                 NO_INTERACTION="yes" emake test
243         done
244 }
245
246 # @FUNCTION: php_get_slots
247 # @DESCRIPTION:
248 # Get a list of PHP slots contained in both the ebuild's USE_PHP and the
249 # user's PHP_TARGETS.
250 php_get_slots() {
251         local s=""
252         local slot
253         for slot in ${USE_PHP}; do
254                 use php_targets_${slot} && s+=" ${slot/-/.}"
255         done
256         echo $s
257 }
258
259 # @FUNCTION: php_init_slot_env
260 # @USAGE: <slot>
261 # @DESCRIPTION:
262 # Takes a slot name, and initializes some global variables to values
263 # corresponding to that slot. For example, it sets the path to the "php"
264 # and "phpize" binaries, which will differ for each slot. This function
265 # is intended to be called while looping through a list of slots
266 # obtained from php_get_slots().
267 #
268 # Calling this function will change the working directory to the
269 # temporary build directory for the given slot.
270 php_init_slot_env() {
271         local libdir=$(get_libdir)
272         local slot="${1}"
273
274         PHPPREFIX="${EPREFIX}/usr/${libdir}/${slot}"
275         PHPIZE="${PHPPREFIX}/bin/phpize"
276         PHPCONFIG="${PHPPREFIX}/bin/php-config"
277         PHPCLI="${PHPPREFIX}/bin/php"
278         PHPCGI="${PHPPREFIX}/bin/php-cgi"
279         PHP_PKG="$(best_version =dev-lang/php-${1:3}*)"
280
281         EXT_DIR="$(${PHPCONFIG} --extension-dir 2>/dev/null)"
282         PHP_CURRENTSLOT=${1:3}
283
284         PHP_EXT_S="${WORKDIR}/${slot}"
285         cd "${PHP_EXT_S}" || die "failed to change directory to ${PHP_EXT_S}"
286 }
287
288 # @FUNCTION: php_slot_ini_files
289 # @USAGE: <slot>
290 # @INTERNAL
291 # @DESCRIPTION:
292 # Output a list of relative paths to INI files for the given
293 # slot. Usually there will be one INI file per SAPI.
294 php_slot_ini_files() {
295         local slot_ini_files=""
296         local x
297         for x in ${PHP_EXT_SAPIS} ; do
298                 if [[ -f "${EPREFIX}/etc/php/${x}-${1}/php.ini" ]] ; then
299                         slot_ini_files+=" etc/php/${x}-${1}/ext/${PHP_EXT_NAME}.ini"
300                 fi
301         done
302
303         echo "${slot_ini_files}"
304 }
305
306 # @FUNCTION: php-ext-source-r3_createinifiles
307 # @DESCRIPTION:
308 # Builds INI files for every enabled slot and SAPI.
309 php-ext-source-r3_createinifiles() {
310         local slot
311         for slot in $(php_get_slots); do
312                 php_init_slot_env "${slot}"
313
314                 local file
315                 for file in $(php_slot_ini_files "${slot}") ; do
316                         if [[ "${PHP_EXT_INI}" = "yes" ]] ; then
317                                 # Add the needed lines to the <ext>.ini files
318                                 php-ext-source-r3_addextension "${PHP_EXT_NAME}.so" "${file}"
319                         fi
320
321                         if [[ -n "${PHP_EXT_INIFILE}" ]] ; then
322                                 cat "${FILESDIR}/${PHP_EXT_INIFILE}" >> "${ED}/${file}" \
323                                         || die "failed to append to ${ED}/${file}"
324
325                                 einfo "Added contents of ${FILESDIR}/${PHP_EXT_INIFILE}" \
326                                           "to ${file}"
327                         fi
328                         inidir="${file/${PHP_EXT_NAME}.ini/}"
329                         inidir="${inidir/ext/ext-active}"
330                         dodir "/${inidir}"
331                         dosym "/${file}" "/${file/ext/ext-active}"
332                 done
333         done
334
335         # A location where PHP code for this extension can be stored,
336         # independent of the PHP or extension versions. This will be part of
337         # PHP's include_path, configured in php.ini. For example, pecl-apcu
338         # installs an "apc.php" file which you are supposed to load with
339         #
340         #   require('apcu/apc.php');
341         #
342         PHP_EXT_SHARED_DIR="${EPREFIX}/usr/share/php/${PHP_EXT_NAME}"
343 }
344
345 # @FUNCTION: php-ext-source-r3_addextension
346 # @USAGE: <extension-path> <ini-file>
347 # @INTERNAL
348 # @DESCRIPTION:
349 # Add a line to an INI file that will enable the given extension. The
350 # first parameter is the path to the extension (.so) file, and the
351 # second parameter is the name of the INI file in which it should be
352 # loaded. This function determines the setting name (either
353 # "extension=..." or "zend_extension=...") and then calls
354 # php-ext-source-r3_addtoinifile to do the actual work.
355 php-ext-source-r3_addextension() {
356         local ext_type="extension"
357         local ext_file="${1}"
358
359         if [[ "${PHP_EXT_ZENDEXT}" = "yes" ]] ; then
360                 ext_type="zend_extension"
361                 ext_file="${EXT_DIR}/${1}" # Zend extensions need the path...
362         fi
363
364         php-ext-source-r3_addtoinifile "${2}" "${ext_type}" "${ext_file}"
365 }
366
367 # @FUNCTION: php-ext-source-r3_addtoinifile
368 # @USAGE: <relative-ini-path> <setting-or-section-name> [setting-value]
369 # @INTERNAL
370 # @DESCRIPTION:
371 # Add a setting=value to one INI file. The first argument is the
372 # relative path to the INI file. The second argument is the setting
373 # name, and the third argument is its value.
374 #
375 # You can also pass "[Section]" as the second parameter, to create a new
376 # section in the INI file. In that case, the third parameter (which
377 # would otherwise be the value of the setting) is ignored.
378 php-ext-source-r3_addtoinifile() {
379         local inifile="${WORKDIR}/${1}"
380         local inidir="${inifile%/*}"
381
382         mkdir -p "${inidir}" || die "failed to create INI directory ${inidir}"
383
384         # Are we adding the name of a section? Assume not by default.
385         local my_added="${2}=${3}"
386         if [[ ${2:0:1} == "[" ]] ; then
387                 # Ok, it's a section name.
388                 my_added="${2}"
389         fi
390         echo "${my_added}" >> "${inifile}" || die "failed to append to ${inifile}"
391         einfo "Added '${my_added}' to /${1}"
392
393         insinto "/${1%/*}"
394         doins "${inifile}"
395 }
396
397 # @FUNCTION: php-ext-source-r3_addtoinifiles
398 # @USAGE: <setting-or-section-name> [setting-value] [message]
399 # @DESCRIPTION:
400 # Add settings to every php.ini file installed by this extension.
401 # You can also add new [Section]s -- see the example below.
402 #
403 # @CODE
404 # Add some settings for the extension:
405 #
406 # php-ext-source-r3_addtoinifiles "zend_optimizer.optimization_level" "15"
407 # php-ext-source-r3_addtoinifiles "zend_optimizer.enable_loader" "0"
408 # php-ext-source-r3_addtoinifiles "zend_optimizer.disable_licensing" "0"
409 #
410 # Adding values to a section in php.ini file installed by the extension:
411 #
412 # php-ext-source-r3_addtoinifiles "[Debugger]"
413 # php-ext-source-r3_addtoinifiles "debugger.enabled" "on"
414 # php-ext-source-r3_addtoinifiles "debugger.profiler_enabled" "on"
415 # @CODE
416 php-ext-source-r3_addtoinifiles() {
417         local slot
418         for slot in $(php_get_slots); do
419                 for file in $(php_slot_ini_files "${slot}") ; do
420                         php-ext-source-r3_addtoinifile "${file}" "${1}" "${2}"
421                 done
422         done
423 }