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