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