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