1 # Copyright 1999-2015 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
5 # @ECLASS: virtualx.eclass
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.
12 if [[ ! ${_VIRTUAL_X} ]]; then
16 die "virtualx.eclass: EAPI ${EAPI} is too old."
21 die "virtualx.eclass: EAPI ${EAPI} is not supported yet."
25 [[ ${EAPI} == [2345] ]] && inherit eutils
27 # @ECLASS-VARIABLE: VIRTUALX_REQUIRED
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}
37 # @ECLASS-VARIABLE: VIRTUALX_DEPEND
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] )
47 # @ECLASS-VARIABLE: VIRTUALX_COMMAND
49 # Command (or eclass function call) to be run in the X11 environment
50 # (within virtualmake function).
51 : ${VIRTUALX_COMMAND:="emake"}
53 case ${VIRTUALX_REQUIRED} in
57 DEPEND="${VIRTUALX_DEPEND}"
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."
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."
75 [[ -z ${VIRTUALX_USE} ]] && VIRTUALX_USE="test"
76 DEPEND="${VIRTUALX_USE}? ( ${VIRTUALX_DEPEND} )"
78 IUSE="${VIRTUALX_USE}"
81 DEPEND="${VIRTUALX_REQUIRED}? ( ${VIRTUALX_DEPEND} )"
83 IUSE="${VIRTUALX_REQUIRED}"
87 # @FUNCTION: virtualmake
89 # Function which start new Xvfb session
90 # where the VIRTUALX_COMMAND variable content gets executed.
92 debug-print-function ${FUNCNAME} "$@"
94 [[ ${EAPI} == [2345] ]] \
95 || die "${FUNCNAME} is unsupported in EAPI > 5, please use virtx"
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}
105 virtx "${VIRTUALX_COMMAND}" "${@}"
110 # @USAGE: <command> [command arguments]
112 # Start new Xvfb session and run commands in it.
114 # IMPORTANT: The command is run nonfatal !!!
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
121 # The rational behind this is the tear down of the started Xfvb session. A
122 # straight die would leave a running session behind.
134 # virtx py.test --verbose
149 debug-print-function ${FUNCNAME} "$@"
151 [[ $# -lt 1 ]] && die "${FUNCNAME} needs at least one argument"
155 local OLD_SANDBOX_ON="${SANDBOX_ON}"
156 local XVFB XHOST XDISPLAY
157 local xvfbargs="-screen 0 1280x1024x24 +extension RANDR"
158 XVFB=$(type -p Xvfb) || die
159 XHOST=$(type -p xhost) || die
161 debug-print "${FUNCNAME}: running Xvfb hack"
163 # The following is derived from Mandrake's hack to allow
164 # compiling without the X display
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
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}"
176 # We really do not want SANDBOX enabled here
177 export SANDBOX_ON="0"
179 debug-print "${FUNCNAME}: ${XVFB} :${XDISPLAY} ${xvfbargs}"
180 ${XVFB} :${XDISPLAY} ${xvfbargs} &>/dev/null &
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:"
189 ${XVFB} :${XDISPLAY} ${xvfbargs}
191 eerror "If possible, correct the above error and try your emerge again."
192 die "Unable to start Xvfb"
195 debug-print "${FUNCNAME}: ${XVFB} :${XDISPLAY} ${xvfbargs}"
196 ${XVFB} :${XDISPLAY} ${xvfbargs} &>/dev/null &
200 # Now enable SANDBOX again if needed.
201 export SANDBOX_ON="${OLD_SANDBOX_ON}"
203 einfo "Starting Xvfb on \$DISPLAY=${XDISPLAY} ..."
205 export DISPLAY=:${XDISPLAY}
206 # Do not break on error, but setup $retval, as we need
208 debug-print "${FUNCNAME}: $@"
209 if has "${EAPI}" 2 3; then
218 kill $(cat /tmp/.X${XDISPLAY}-lock)
220 # die if our command failed
221 [[ ${retval} -ne 0 ]] && die "Failed to run '$@'"
223 return 0 # always return 0, it can be altered by failed kill for Xvfb
228 # Same as "make", but set up the Xvfb hack if needed.
231 debug-print-function ${FUNCNAME} "$@"
233 [[ ${EAPI} == [2345] ]] \
234 || die "${FUNCNAME} is unsupported in EAPI > 5, please use 'virtx emake -j1 ....'"
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 "$@"
243 # Same as "emake", but set up the Xvfb hack if needed.
245 debug-print-function ${FUNCNAME} "$@"
247 [[ ${EAPI} == [2345] ]] \
248 || die "${FUNCNAME} is unsupported in EAPI > 5, please use 'virtx emake ....'"
250 VIRTUALX_COMMAND="emake" virtualmake "$@"
255 # Same as "econf", but set up the Xvfb hack if needed.
257 debug-print-function ${FUNCNAME} "$@"
259 [[ ${EAPI} == [2345] ]] \
260 || die "${FUNCNAME} is unsupported in EAPI > 5, please use 'virtx econf ....'"
262 VIRTUALX_COMMAND="econf" virtualmake "$@"