perl-functions.eclass: should 'just work' in EAPI=6
[gentoo.git] / eclass / gnome2-utils.eclass
1 # Copyright 1999-2015 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3 # $Id$
4
5 # @ECLASS: gnome2-utils.eclass
6 # @MAINTAINER:
7 # gnome@gentoo.org
8 # @BLURB: Auxiliary functions commonly used by Gnome packages.
9 # @DESCRIPTION:
10 # This eclass provides a set of auxiliary functions needed by most Gnome
11 # packages. It may be used by non-Gnome packages as needed for handling various
12 # Gnome stack related functions such as:
13 #  * Gtk+ icon cache management
14 #  * GSettings schemas management
15 #  * GConf schemas management
16 #  * scrollkeeper (old Gnome help system) management
17
18 inherit eutils multilib xdg-utils
19
20 case "${EAPI:-0}" in
21         0|1|2|3|4|5) ;;
22         *) die "EAPI=${EAPI} is not supported" ;;
23 esac
24
25 # @ECLASS-VARIABLE: GCONFTOOL_BIN
26 # @INTERNAL
27 # @DESCRIPTION:
28 # Path to gconftool-2
29 : ${GCONFTOOL_BIN:="/usr/bin/gconftool-2"}
30
31 # @ECLASS-VARIABLE: SCROLLKEEPER_DIR
32 # @INTERNAL
33 # @DESCRIPTION:
34 # Directory where scrollkeeper-update should do its work
35 : ${SCROLLKEEPER_DIR:="/var/lib/scrollkeeper"}
36
37 # @ECLASS-VARIABLE: SCROLLKEEPER_UPDATE_BIN
38 # @INTERNAL
39 # @DESCRIPTION:
40 # Path to scrollkeeper-update
41 : ${SCROLLKEEPER_UPDATE_BIN:="/usr/bin/scrollkeeper-update"}
42
43 # @ECLASS-VARIABLE: GTK_UPDATE_ICON_CACHE
44 # @INTERNAL
45 # @DESCRIPTION:
46 # Path to gtk-update-icon-cache
47 : ${GTK_UPDATE_ICON_CACHE:="/usr/bin/gtk-update-icon-cache"}
48
49 # @ECLASS-VARIABLE: GLIB_COMPILE_SCHEMAS
50 # @INTERNAL
51 # @DESCRIPTION:
52 # Path to glib-compile-schemas
53 : ${GLIB_COMPILE_SCHEMAS:="/usr/bin/glib-compile-schemas"}
54
55 # @ECLASS-VARIABLE: GNOME2_ECLASS_SCHEMAS
56 # @INTERNAL
57 # @DEFAULT_UNSET
58 # @DESCRIPTION:
59 # List of GConf schemas provided by the package
60
61 # @ECLASS-VARIABLE: GNOME2_ECLASS_ICONS
62 # @INTERNAL
63 # @DEFAULT_UNSET
64 # @DESCRIPTION:
65 # List of icons provided by the package
66
67 # @ECLASS-VARIABLE: GNOME2_ECLASS_SCROLLS
68 # @INTERNAL
69 # @DEFAULT_UNSET
70 # @DESCRIPTION:
71 # List of scrolls (documentation files) provided by the package
72
73 # @ECLASS-VARIABLE: GNOME2_ECLASS_GLIB_SCHEMAS
74 # @INTERNAL
75 # @DEFAULT_UNSET
76 # @DESCRIPTION:
77 # List of GSettings schemas provided by the package
78
79 # @ECLASS-VARIABLE: GNOME2_ECLASS_GDK_PIXBUF_LOADERS
80 # @INTERNAL
81 # @DEFAULT_UNSET
82 # @DESCRIPTION:
83 # List of gdk-pixbuf loaders provided by the package
84
85 DEPEND=">=sys-apps/sed-4"
86
87
88 # @FUNCTION: gnome2_environment_reset
89 # @DESCRIPTION:
90 # Reset various variables inherited from root's evironment to a reasonable
91 # default for ebuilds to help avoid access violations and test failures.
92 gnome2_environment_reset() {
93         xdg_environment_reset
94
95         # Respected by >=glib-2.30.1-r1
96         export G_HOME="${T}"
97
98         # GST_REGISTRY is to work around gst utilities trying to read/write /root
99         export GST_REGISTRY="${T}/registry.xml"
100 }
101
102 # @FUNCTION: gnome2_gconf_savelist
103 # @DESCRIPTION:
104 # Find the GConf schemas that are about to be installed and save their location
105 # in the GNOME2_ECLASS_SCHEMAS environment variable.
106 # This function should be called from pkg_preinst.
107 gnome2_gconf_savelist() {
108         has ${EAPI:-0} 0 1 2 && ! use prefix && ED="${D}"
109         pushd "${ED}" > /dev/null || die
110         export GNOME2_ECLASS_SCHEMAS=$(find 'etc/gconf/schemas/' -name '*.schemas' 2> /dev/null)
111         popd > /dev/null || die
112 }
113
114 # @FUNCTION: gnome2_gconf_install
115 # @DESCRIPTION:
116 # Applies any schema files installed by the current ebuild to Gconf's database
117 # using gconftool-2.
118 # This function should be called from pkg_postinst.
119 gnome2_gconf_install() {
120         has ${EAPI:-0} 0 1 2 && ! use prefix && EROOT="${ROOT}"
121         local updater="${EROOT}${GCONFTOOL_BIN}"
122
123         if [[ ! -x "${updater}" ]]; then
124                 debug-print "${updater} is not executable"
125                 return
126         fi
127
128         if [[ -z "${GNOME2_ECLASS_SCHEMAS}" ]]; then
129                 debug-print "No GNOME 2 GConf schemas found"
130                 return
131         fi
132
133         # We are ready to install the GCONF Scheme now
134         unset GCONF_DISABLE_MAKEFILE_SCHEMA_INSTALL
135         export GCONF_CONFIG_SOURCE="$("${updater}" --get-default-source | sed "s;:/;:${ROOT};")"
136
137         einfo "Installing GNOME 2 GConf schemas"
138
139         local F
140         for F in ${GNOME2_ECLASS_SCHEMAS}; do
141                 if [[ -e "${EROOT}${F}" ]]; then
142                         debug-print "Installing schema: ${F}"
143                         "${updater}" --makefile-install-rule "${EROOT}${F}" 1>/dev/null
144                 fi
145         done
146
147         # have gconf reload the new schemas
148         pids=$(pgrep -x gconfd-2)
149         if [[ $? == 0 ]] ; then
150                 ebegin "Reloading GConf schemas"
151                 kill -HUP ${pids}
152                 eend $?
153         fi
154 }
155
156 # @FUNCTION: gnome2_gconf_uninstall
157 # @DESCRIPTION:
158 # Removes schema files previously installed by the current ebuild from Gconf's
159 # database.
160 gnome2_gconf_uninstall() {
161         has ${EAPI:-0} 0 1 2 && ! use prefix && EROOT="${ROOT}"
162         local updater="${EROOT}${GCONFTOOL_BIN}"
163
164         if [[ ! -x "${updater}" ]]; then
165                 debug-print "${updater} is not executable"
166                 return
167         fi
168
169         if [[ -z "${GNOME2_ECLASS_SCHEMAS}" ]]; then
170                 debug-print "No GNOME 2 GConf schemas found"
171                 return
172         fi
173
174         unset GCONF_DISABLE_MAKEFILE_SCHEMA_INSTALL
175         export GCONF_CONFIG_SOURCE="$("${updater}" --get-default-source | sed "s;:/;:${ROOT};")"
176
177         einfo "Uninstalling GNOME 2 GConf schemas"
178
179         local F
180         for F in ${GNOME2_ECLASS_SCHEMAS}; do
181                 if [[ -e "${EROOT}${F}" ]]; then
182                         debug-print "Uninstalling gconf schema: ${F}"
183                         "${updater}" --makefile-uninstall-rule "${EROOT}${F}" 1>/dev/null
184                 fi
185         done
186
187         # have gconf reload the new schemas
188         pids=$(pgrep -x gconfd-2)
189         if [[ $? == 0 ]] ; then
190                 ebegin "Reloading GConf schemas"
191                 kill -HUP ${pids}
192                 eend $?
193         fi
194 }
195
196 # @FUNCTION: gnome2_icon_savelist
197 # @DESCRIPTION:
198 # Find the icons that are about to be installed and save their location
199 # in the GNOME2_ECLASS_ICONS environment variable.
200 # This function should be called from pkg_preinst.
201 gnome2_icon_savelist() {
202         has ${EAPI:-0} 0 1 2 && ! use prefix && ED="${D}"
203         pushd "${ED}" > /dev/null || die
204         export GNOME2_ECLASS_ICONS=$(find 'usr/share/icons' -maxdepth 1 -mindepth 1 -type d 2> /dev/null)
205         popd > /dev/null || die
206 }
207
208 # @FUNCTION: gnome2_icon_cache_update
209 # @DESCRIPTION:
210 # Updates Gtk+ icon cache files under /usr/share/icons if the current ebuild
211 # have installed anything under that location.
212 # This function should be called from pkg_postinst and pkg_postrm.
213 gnome2_icon_cache_update() {
214         has ${EAPI:-0} 0 1 2 && ! use prefix && EROOT="${ROOT}"
215         local updater="${EROOT}${GTK_UPDATE_ICON_CACHE}"
216
217         if [[ ! -x "${updater}" ]] ; then
218                 debug-print "${updater} is not executable"
219                 return
220         fi
221
222         if [[ -z "${GNOME2_ECLASS_ICONS}" ]]; then
223                 debug-print "No icon cache to update"
224                 return
225         fi
226
227         ebegin "Updating icons cache"
228
229         local retval=0
230         local fails=( )
231
232         for dir in ${GNOME2_ECLASS_ICONS}
233         do
234                 if [[ -f "${EROOT}${dir}/index.theme" ]] ; then
235                         local rv=0
236
237                         "${updater}" -qf "${EROOT}${dir}"
238                         rv=$?
239
240                         if [[ ! $rv -eq 0 ]] ; then
241                                 debug-print "Updating cache failed on ${EROOT}${dir}"
242
243                                 # Add to the list of failures
244                                 fails[$(( ${#fails[@]} + 1 ))]="${EROOT}${dir}"
245
246                                 retval=2
247                         fi
248                 elif [[ $(ls "${EROOT}${dir}") = "icon-theme.cache" ]]; then
249                         # Clear stale cache files after theme uninstallation
250                         rm "${EROOT}${dir}/icon-theme.cache"
251                 fi
252
253                 if [[ -z $(ls "${EROOT}${dir}") ]]; then
254                         # Clear empty theme directories after theme uninstallation
255                         rmdir "${EROOT}${dir}"
256                 fi
257         done
258
259         eend ${retval}
260
261         for f in "${fails[@]}" ; do
262                 eerror "Failed to update cache with icon $f"
263         done
264 }
265
266 # @FUNCTION: gnome2_omf_fix
267 # @DESCRIPTION:
268 # Workaround applied to Makefile rules in order to remove redundant
269 # calls to scrollkeeper-update and sandbox violations.
270 # This function should be called from src_prepare.
271 gnome2_omf_fix() {
272         local omf_makefiles filename
273
274         omf_makefiles="$@"
275
276         if [[ -f ${S}/omf.make ]] ; then
277                 omf_makefiles="${omf_makefiles} ${S}/omf.make"
278         fi
279
280         if [[ -f ${S}/gnome-doc-utils.make ]] ; then
281                 omf_makefiles="${omf_makefiles} ${S}/gnome-doc-utils.make"
282         fi
283
284         # testing fixing of all makefiles found
285         # The sort is important to ensure .am is listed before the respective .in for
286         # maintainer mode regeneration not kicking in due to .am being newer than .in
287         for filename in $(find "${S}" -name "Makefile.in" -o -name "Makefile.am" |sort) ; do
288                 omf_makefiles="${omf_makefiles} ${filename}"
289         done
290
291         ebegin "Fixing OMF Makefiles"
292
293         local retval=0
294         local fails=( )
295
296         for omf in ${omf_makefiles} ; do
297                 sed -i -e 's:scrollkeeper-update:true:' "${omf}"
298                 retval=$?
299
300                 if [[ $retval -ne 0 ]] ; then
301                         debug-print "updating of ${omf} failed"
302
303                         # Add to the list of failures
304                         fails[$(( ${#fails[@]} + 1 ))]=$omf
305
306                         retval=2
307                 fi
308         done
309
310         eend $retval
311
312         for f in "${fails[@]}" ; do
313                 eerror "Failed to update OMF Makefile $f"
314         done
315 }
316
317 # @FUNCTION: gnome2_scrollkeeper_savelist
318 # @DESCRIPTION:
319 # Find the scrolls that are about to be installed and save their location
320 # in the GNOME2_ECLASS_SCROLLS environment variable.
321 # This function should be called from pkg_preinst.
322 gnome2_scrollkeeper_savelist() {
323         has ${EAPI:-0} 0 1 2 && ! use prefix && ED="${D}"
324         pushd "${ED}" > /dev/null || die
325         export GNOME2_ECLASS_SCROLLS=$(find 'usr/share/omf' -type f -name "*.omf" 2> /dev/null)
326         popd > /dev/null || die
327 }
328
329 # @FUNCTION: gnome2_scrollkeeper_update
330 # @DESCRIPTION:
331 # Updates the global scrollkeeper database.
332 # This function should be called from pkg_postinst and pkg_postrm.
333 gnome2_scrollkeeper_update() {
334         has ${EAPI:-0} 0 1 2 && ! use prefix && EROOT="${ROOT}"
335         local updater="${EROOT}${SCROLLKEEPER_UPDATE_BIN}"
336
337         if [[ ! -x "${updater}" ]] ; then
338                 debug-print "${updater} is not executable"
339                 return
340         fi
341
342         if [[ -z "${GNOME2_ECLASS_SCROLLS}" ]]; then
343                 debug-print "No scroll cache to update"
344                 return
345         fi
346
347         ebegin "Updating scrollkeeper database ..."
348         "${updater}" -q -p "${EROOT}${SCROLLKEEPER_DIR}"
349         eend $?
350 }
351
352 # @FUNCTION: gnome2_schemas_savelist
353 # @DESCRIPTION:
354 # Find if there is any GSettings schema to install and save the list in
355 # GNOME2_ECLASS_GLIB_SCHEMAS variable.
356 # This function should be called from pkg_preinst.
357 gnome2_schemas_savelist() {
358         has ${EAPI:-0} 0 1 2 && ! use prefix && ED="${D}"
359         pushd "${ED}" > /dev/null || die
360         export GNOME2_ECLASS_GLIB_SCHEMAS=$(find 'usr/share/glib-2.0/schemas' -name '*.gschema.xml' 2>/dev/null)
361         popd > /dev/null || die
362 }
363
364 # @FUNCTION: gnome2_schemas_update
365 # @USAGE: gnome2_schemas_update
366 # @DESCRIPTION:
367 # Updates GSettings schemas if GNOME2_ECLASS_GLIB_SCHEMAS has some.
368 # This function should be called from pkg_postinst and pkg_postrm.
369 gnome2_schemas_update() {
370         has ${EAPI:-0} 0 1 2 && ! use prefix && EROOT="${ROOT}"
371         local updater="${EROOT}${GLIB_COMPILE_SCHEMAS}"
372
373         if [[ ! -x ${updater} ]]; then
374                 debug-print "${updater} is not executable"
375                 return
376         fi
377
378         if [[ -z ${GNOME2_ECLASS_GLIB_SCHEMAS} ]]; then
379                 debug-print "No GSettings schemas to update"
380                 return
381         fi
382
383         ebegin "Updating GSettings schemas"
384         ${updater} --allow-any-name "$@" "${EROOT%/}/usr/share/glib-2.0/schemas" &>/dev/null
385         eend $?
386 }
387
388 # @FUNCTION: gnome2_gdk_pixbuf_savelist
389 # @DESCRIPTION:
390 # Find if there is any gdk-pixbuf loader to install and save the list in
391 # GNOME2_ECLASS_GDK_PIXBUF_LOADERS variable.
392 # This function should be called from pkg_preinst.
393 gnome2_gdk_pixbuf_savelist() {
394         has ${EAPI:-0} 0 1 2 && ! use prefix && ED="${D}"
395         pushd "${ED}" > /dev/null || die
396         export GNOME2_ECLASS_GDK_PIXBUF_LOADERS=$(find usr/lib*/gdk-pixbuf-2.0 -type f 2>/dev/null)
397         popd > /dev/null || die
398 }
399
400 # @FUNCTION: gnome2_gdk_pixbuf_update
401 # @USAGE: gnome2_gdk_pixbuf_update
402 # @DESCRIPTION:
403 # Updates gdk-pixbuf loader cache if GNOME2_ECLASS_GDK_PIXBUF_LOADERS has some.
404 # This function should be called from pkg_postinst and pkg_postrm.
405 gnome2_gdk_pixbuf_update() {
406         has ${EAPI:-0} 0 1 2 && ! use prefix && EROOT="${ROOT}"
407         local updater="${EROOT}/usr/bin/${CHOST}-gdk-pixbuf-query-loaders"
408
409         if [[ ! -x ${updater} ]]; then
410                 updater="${EROOT}/usr/bin/gdk-pixbuf-query-loaders"
411         fi
412
413         if [[ ! -x ${updater} ]]; then
414                 debug-print "${updater} is not executable"
415                 return
416         fi
417
418         if [[ -z ${GNOME2_ECLASS_GDK_PIXBUF_LOADERS} ]]; then
419                 debug-print "gdk-pixbuf loader cache does not need an update"
420                 return
421         fi
422
423         ebegin "Updating gdk-pixbuf loader cache"
424         local tmp_file=$(emktemp)
425         ${updater} 1> "${tmp_file}" &&
426         chmod 0644 "${tmp_file}" &&
427         cp -f "${tmp_file}" "${EROOT}usr/$(get_libdir)/gdk-pixbuf-2.0/2.10.0/loaders.cache" &&
428         rm "${tmp_file}" # don't replace this with mv, required for SELinux support
429         eend $?
430 }
431
432 # @FUNCTION: gnome2_query_immodules_gtk2
433 # @USAGE: gnome2_query_immodules_gtk2
434 # @DESCRIPTION:
435 # Updates gtk2 immodules/gdk-pixbuf loaders listing.
436 gnome2_query_immodules_gtk2() {
437         local updater=${EPREFIX}/usr/bin/${CHOST}-gtk-query-immodules-2.0
438         [[ ! -x ${updater} ]] && updater=${EPREFIX}/usr/bin/gtk-query-immodules-2.0
439
440         ebegin "Updating gtk2 input method module cache"
441         GTK_IM_MODULE_FILE="${EROOT}usr/$(get_libdir)/gtk-2.0/2.10.0/immodules.cache" \
442                 "${updater}" --update-cache
443         eend $?
444 }
445
446 # @FUNCTION: gnome2_query_immodules_gtk3
447 # @USAGE: gnome2_query_immodules_gtk3
448 # @DESCRIPTION:
449 # Updates gtk3 immodules/gdk-pixbuf loaders listing.
450 gnome2_query_immodules_gtk3() {
451         local updater=${EPREFIX}/usr/bin/${CHOST}-gtk-query-immodules-3.0
452         [[ ! -x ${updater} ]] && updater=${EPREFIX}/usr/bin/gtk-query-immodules-3.0
453
454         ebegin "Updating gtk3 input method module cache"
455         GTK_IM_MODULE_FILE="${EROOT}usr/$(get_libdir)/gtk-3.0/3.0.0/immodules.cache" \
456                 "${updater}" --update-cache
457         eend $?
458 }
459
460 # @FUNCTION: gnome2_disable_deprecation_warning
461 # @DESCRIPTION:
462 # Disable deprecation warnings commonly found in glib based packages.
463 # Should be called from src_prepare.
464 gnome2_disable_deprecation_warning() {
465         local retval=0
466         local fails=( )
467         local makefile
468
469         ebegin "Disabling deprecation warnings"
470         # The sort is important to ensure .am is listed before the respective .in for
471         # maintainer mode regeneration not kicking in due to .am being newer than .in
472         while read makefile ; do
473                 if ! grep -qE "(DISABLE_DEPRECATED|GSEAL_ENABLE)" "${makefile}"; then
474                         continue
475                 fi
476
477                 LC_ALL=C sed -r -i \
478                         -e 's:-D[A-Z_]+_DISABLE_DEPRECATED:$(NULL):g' \
479                         -e 's:-DGSEAL_ENABLE+[A-Z_]:$(NULL):g' \
480                         -i "${makefile}"
481
482                 if [[ $? -ne 0 ]]; then
483                         # Add to the list of failures
484                         fails+=( "${makefile}" )
485                         retval=2
486                 fi
487         done < <(find "${S}" -name "Makefile.in" \
488                 -o -name "Makefile.am" -o -name "Makefile.decl" \
489                 | sort; [[ -f "${S}"/configure ]] && echo configure)
490 # TODO: sedding configure.ac can trigger maintainer mode; bug #439602
491 #               -o -name "configure.ac" -o -name "configure.in" \
492 #               | sort; echo configure)
493         eend ${retval}
494
495         for makefile in "${fails[@]}" ; do
496                 ewarn "Failed to disable deprecation warnings in ${makefile}"
497         done
498 }