1 # Copyright 1999-2018 Gentoo Foundation
2 # Distributed under the terms of the GNU General Public License v2
4 # @ECLASS: virtualx.eclass
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.
11 if [[ ! ${_VIRTUAL_X} ]]; then
15 die "virtualx.eclass: EAPI ${EAPI} is too old."
20 die "virtualx.eclass: EAPI ${EAPI} is not supported yet."
24 [[ ${EAPI} == [45] ]] && inherit eutils
26 # @ECLASS-VARIABLE: VIRTUALX_REQUIRED
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}
36 # @ECLASS-VARIABLE: VIRTUALX_DEPEND
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] )
46 # @ECLASS-VARIABLE: VIRTUALX_COMMAND
48 # Command (or eclass function call) to be run in the X11 environment
49 # (within virtualmake function).
50 : ${VIRTUALX_COMMAND:="emake"}
52 case ${VIRTUALX_REQUIRED} in
56 DEPEND="${VIRTUALX_DEPEND}"
60 [[ ${EAPI} == [45] ]] \
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."
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."
74 [[ -z ${VIRTUALX_USE} ]] && VIRTUALX_USE="test"
75 DEPEND="${VIRTUALX_USE}? ( ${VIRTUALX_DEPEND} )"
77 IUSE="${VIRTUALX_USE}"
80 DEPEND="${VIRTUALX_REQUIRED}? ( ${VIRTUALX_DEPEND} )"
82 IUSE="${VIRTUALX_REQUIRED}"
86 # @FUNCTION: virtualmake
88 # Function which start new Xvfb session
89 # where the VIRTUALX_COMMAND variable content gets executed.
91 debug-print-function ${FUNCNAME} "$@"
93 [[ ${EAPI} == [45] ]] \
94 || die "${FUNCNAME} is unsupported in EAPI > 5, please use virtx"
96 # backcompat for maketype
97 if [[ -n ${maketype} ]]; then
98 [[ ${EAPI} == [45] ]] || 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}
104 virtx "${VIRTUALX_COMMAND}" "${@}"
109 # @USAGE: <command> [command arguments]
111 # Start new Xvfb session and run commands in it.
113 # IMPORTANT: The command is run nonfatal !!!
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
120 # The rational behind this is the tear down of the started Xfvb session. A
121 # straight die would leave a running session behind.
133 # virtx py.test --verbose
148 debug-print-function ${FUNCNAME} "$@"
150 [[ $# -lt 1 ]] && die "${FUNCNAME} needs at least one argument"
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
160 debug-print "${FUNCNAME}: running Xvfb hack"
162 # The following is derived from Mandrake's hack to allow
163 # compiling without the X display
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
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}"
175 # We really do not want SANDBOX enabled here
176 export SANDBOX_ON="0"
178 debug-print "${FUNCNAME}: ${XVFB} :${XDISPLAY} ${xvfbargs}"
179 ${XVFB} :${XDISPLAY} ${xvfbargs} &>/dev/null &
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:"
188 ${XVFB} :${XDISPLAY} ${xvfbargs}
190 eerror "If possible, correct the above error and try your emerge again."
191 die "Unable to start Xvfb"
194 debug-print "${FUNCNAME}: ${XVFB} :${XDISPLAY} ${xvfbargs}"
195 ${XVFB} :${XDISPLAY} ${xvfbargs} &>/dev/null &
199 # Now enable SANDBOX again if needed.
200 export SANDBOX_ON="${OLD_SANDBOX_ON}"
202 einfo "Starting Xvfb on \$DISPLAY=${XDISPLAY} ..."
204 export DISPLAY=:${XDISPLAY}
205 # Do not break on error, but setup $retval, as we need
207 debug-print "${FUNCNAME}: $@"
212 kill $(cat /tmp/.X${XDISPLAY}-lock)
214 # die if our command failed
215 [[ ${retval} -ne 0 ]] && die "Failed to run '$@'"
217 return 0 # always return 0, it can be altered by failed kill for Xvfb
222 # Same as "make", but set up the Xvfb hack if needed.
225 debug-print-function ${FUNCNAME} "$@"
227 [[ ${EAPI} == [45] ]] \
228 || die "${FUNCNAME} is unsupported in EAPI > 5, please use 'virtx emake -j1 ....'"
230 eqawarn "you should not execute make directly"
231 eqawarn "rather execute Xemake -j1 if you have issues with parallel make"
232 VIRTUALX_COMMAND="emake -j1" virtualmake "$@"
237 # Same as "emake", but set up the Xvfb hack if needed.
239 debug-print-function ${FUNCNAME} "$@"
241 [[ ${EAPI} == [45] ]] \
242 || die "${FUNCNAME} is unsupported in EAPI > 5, please use 'virtx emake ....'"
244 VIRTUALX_COMMAND="emake" virtualmake "$@"
249 # Same as "econf", but set up the Xvfb hack if needed.
251 debug-print-function ${FUNCNAME} "$@"
253 [[ ${EAPI} == [45] ]] \
254 || die "${FUNCNAME} is unsupported in EAPI > 5, please use 'virtx econf ....'"
256 VIRTUALX_COMMAND="econf" virtualmake "$@"