kde-plasma/breeze-gtk: x86 stable wrt bug #613144
[gentoo.git] / eclass / virtualx.eclass
1 # Copyright 1999-2015 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
3
4 # @ECLASS: virtualx.eclass
5 # @MAINTAINER:
6 # x11@gentoo.org
7 # @AUTHOR:
8 # Original author: Martin Schlemmer <azarah@gentoo.org>
9 # @BLURB: This eclass can be used for packages that needs a working X environment to build.
10
11 if [[ ! ${_VIRTUAL_X} ]]; then
12
13 case "${EAPI:-0}" in
14         0|1)
15                 die "virtualx.eclass: EAPI ${EAPI} is too old."
16                 ;;
17         2|3|4|5|6)
18                 ;;
19         *)
20                 die "virtualx.eclass: EAPI ${EAPI} is not supported yet."
21                 ;;
22 esac
23
24 [[ ${EAPI} == [2345] ]] && inherit eutils
25
26 # @ECLASS-VARIABLE: VIRTUALX_REQUIRED
27 # @DESCRIPTION:
28 # Variable specifying the dependency on xorg-server and xhost.
29 # Possible special values are "always" and "manual", which specify
30 # the dependency to be set unconditionaly or not at all.
31 # Any other value is taken as useflag desired to be in control of
32 # the dependency (eg. VIRTUALX_REQUIRED="kde" will add the dependency
33 # into "kde? ( )" and add kde into IUSE.
34 : ${VIRTUALX_REQUIRED:=test}
35
36 # @ECLASS-VARIABLE: VIRTUALX_DEPEND
37 # @DESCRIPTION:
38 # Dep string available for use outside of eclass, in case a more
39 # complicated dep is needed.
40 # You can specify the variable BEFORE inherit to add more dependencies.
41 VIRTUALX_DEPEND="${VIRTUALX_DEPEND}
42         !prefix? ( x11-base/xorg-server[xvfb] )
43         x11-apps/xhost
44 "
45
46 # @ECLASS-VARIABLE: VIRTUALX_COMMAND
47 # @DESCRIPTION:
48 # Command (or eclass function call) to be run in the X11 environment
49 # (within virtualmake function).
50 : ${VIRTUALX_COMMAND:="emake"}
51
52 case ${VIRTUALX_REQUIRED} in
53         manual)
54                 ;;
55         always)
56                 DEPEND="${VIRTUALX_DEPEND}"
57                 RDEPEND=""
58                 ;;
59         optional|tests)
60                 [[ ${EAPI} == [2345] ]] \
61                         || die 'Values "optional" and "tests" for VIRTUALX_REQUIRED are banned in EAPI > 5'
62                 # deprecated section YAY.
63                 eqawarn "VIRTUALX_REQUIRED=optional and VIRTUALX_REQUIRED=tests are deprecated."
64                 eqawarn "You can drop the variable definition completely from ebuild,"
65                 eqawarn "because it is default behaviour."
66
67                 if [[ -n ${VIRTUALX_USE} ]]; then
68                         # so they like to specify the useflag
69                         eqawarn "VIRTUALX_USE variable is deprecated."
70                         eqawarn "Please read eclass manpage to find out how to use VIRTUALX_REQUIRED"
71                         eqawarn "to achieve the same behaviour."
72                 fi
73
74                 [[ -z ${VIRTUALX_USE} ]] && VIRTUALX_USE="test"
75                 DEPEND="${VIRTUALX_USE}? ( ${VIRTUALX_DEPEND} )"
76                 RDEPEND=""
77                 IUSE="${VIRTUALX_USE}"
78                 ;;
79         *)
80                 DEPEND="${VIRTUALX_REQUIRED}? ( ${VIRTUALX_DEPEND} )"
81                 RDEPEND=""
82                 IUSE="${VIRTUALX_REQUIRED}"
83                 ;;
84 esac
85
86 # @FUNCTION: virtualmake
87 # @DESCRIPTION:
88 # Function which start new Xvfb session
89 # where the VIRTUALX_COMMAND variable content gets executed.
90 virtualmake() {
91         debug-print-function ${FUNCNAME} "$@"
92
93         [[ ${EAPI} == [2345] ]] \
94                 || die "${FUNCNAME} is unsupported in EAPI > 5, please use virtx"
95
96         # backcompat for maketype
97         if [[ -n ${maketype} ]]; then
98                 [[ ${EAPI} == [2345] ]] || die "maketype is banned in EAPI > 5"
99                 eqawarn "ebuild is exporting \$maketype=${maketype}"
100                 eqawarn "Ebuild should be migrated to use 'virtx command' instead."
101                 VIRTUALX_COMMAND=${maketype}
102         fi
103
104         virtx "${VIRTUALX_COMMAND}" "${@}"
105 }
106
107
108 # @FUNCTION: virtx
109 # @USAGE: <command> [command arguments]
110 # @DESCRIPTION:
111 # Start new Xvfb session and run commands in it.
112 #
113 # IMPORTANT: The command is run nonfatal !!!
114 #
115 # This means we are checking for the return code and raise an exception if it
116 # isn't 0. So you need to make sure that all commands return a proper
117 # code and not just die. All eclass function used should support nonfatal
118 # calls properly.
119 #
120 # The rational behind this is the tear down of the started Xfvb session. A
121 # straight die would leave a running session behind.
122 #
123 # Example:
124 #
125 # @CODE
126 # src_test() {
127 #       virtx default
128 # }
129 # @CODE
130 #
131 # @CODE
132 # python_test() {
133 #       virtx py.test --verbose
134 # }
135 # @CODE
136 #
137 # @CODE
138 # my_test() {
139 #   some_command
140 #   return $?
141 # }
142 #
143 # src_test() {
144 #   virtx my_test
145 # }
146 # @CODE
147 virtx() {
148         debug-print-function ${FUNCNAME} "$@"
149
150         [[ $# -lt 1 ]] && die "${FUNCNAME} needs at least one argument"
151
152         local i=0
153         local retval=0
154         local OLD_SANDBOX_ON="${SANDBOX_ON}"
155         local XVFB XHOST XDISPLAY
156         local xvfbargs="-screen 0 1280x1024x24 +extension RANDR"
157         XVFB=$(type -p Xvfb) || die
158         XHOST=$(type -p xhost) || die
159
160         debug-print "${FUNCNAME}: running Xvfb hack"
161         export XAUTHORITY=
162         # The following is derived from Mandrake's hack to allow
163         # compiling without the X display
164
165         einfo "Scanning for an open DISPLAY to start Xvfb ..."
166         # If we are in a chrooted environment, and there is already a
167         # X server started outside of the chroot, Xvfb will fail to start
168         # on the same display (most cases this is :0 ), so make sure
169         # Xvfb is started, else bump the display number
170         #
171         # Azarah - 5 May 2002
172         XDISPLAY=$(i=0; while [[ -f /tmp/.X${i}-lock ]] ; do ((i++));done; echo ${i})
173         debug-print "${FUNCNAME}: XDISPLAY=${XDISPLAY}"
174
175         # We really do not want SANDBOX enabled here
176         export SANDBOX_ON="0"
177
178         debug-print "${FUNCNAME}: ${XVFB} :${XDISPLAY} ${xvfbargs}"
179         ${XVFB} :${XDISPLAY} ${xvfbargs} &>/dev/null &
180         sleep 2
181
182         local start=${XDISPLAY}
183         while [[ ! -f /tmp/.X${XDISPLAY}-lock ]]; do
184                 # Stop trying after 15 tries
185                 if ((XDISPLAY - start > 15)) ; then
186                         eerror "'${XVFB} :${XDISPLAY} ${xvfbargs}' returns:"
187                         echo
188                         ${XVFB} :${XDISPLAY} ${xvfbargs}
189                         echo
190                         eerror "If possible, correct the above error and try your emerge again."
191                         die "Unable to start Xvfb"
192                 fi
193                         ((XDISPLAY++))
194                 debug-print "${FUNCNAME}: ${XVFB} :${XDISPLAY} ${xvfbargs}"
195                 ${XVFB} :${XDISPLAY} ${xvfbargs} &>/dev/null &
196                 sleep 2
197         done
198
199         # Now enable SANDBOX again if needed.
200         export SANDBOX_ON="${OLD_SANDBOX_ON}"
201
202         einfo "Starting Xvfb on \$DISPLAY=${XDISPLAY} ..."
203
204         export DISPLAY=:${XDISPLAY}
205         # Do not break on error, but setup $retval, as we need
206         # to kill Xvfb
207         debug-print "${FUNCNAME}: $@"
208         if has "${EAPI}" 2 3; then
209                 "$@"
210                 retval=$?
211         else
212                 nonfatal "$@"
213                 retval=$?
214         fi
215
216         # Now kill Xvfb
217         kill $(cat /tmp/.X${XDISPLAY}-lock)
218
219         # die if our command failed
220         [[ ${retval} -ne 0 ]] && die "Failed to run '$@'"
221
222         return 0 # always return 0, it can be altered by failed kill for Xvfb
223 }
224
225 # @FUNCTION: Xmake
226 # @DESCRIPTION:
227 # Same as "make", but set up the Xvfb hack if needed.
228 # Deprecated call.
229 Xmake() {
230         debug-print-function ${FUNCNAME} "$@"
231
232         [[ ${EAPI} == [2345] ]] \
233                 || die "${FUNCNAME} is unsupported in EAPI > 5, please use 'virtx emake -j1 ....'"
234
235         eqawarn "you should not execute make directly"
236         eqawarn "rather execute Xemake -j1 if you have issues with parallel make"
237         VIRTUALX_COMMAND="emake -j1" virtualmake "$@"
238 }
239
240 # @FUNCTION: Xemake
241 # @DESCRIPTION:
242 # Same as "emake", but set up the Xvfb hack if needed.
243 Xemake() {
244         debug-print-function ${FUNCNAME} "$@"
245
246         [[ ${EAPI} == [2345] ]] \
247                 || die "${FUNCNAME} is unsupported in EAPI > 5, please use 'virtx emake ....'"
248
249         VIRTUALX_COMMAND="emake" virtualmake "$@"
250 }
251
252 # @FUNCTION: Xeconf
253 # @DESCRIPTION:
254 # Same as "econf", but set up the Xvfb hack if needed.
255 Xeconf() {
256         debug-print-function ${FUNCNAME} "$@"
257
258         [[ ${EAPI} == [2345] ]] \
259                 || die "${FUNCNAME} is unsupported in EAPI > 5, please use 'virtx econf ....'"
260
261         VIRTUALX_COMMAND="econf" virtualmake "$@"
262 }
263
264 _VIRTUAL_X=1
265 fi