1 # Copyright 1999-2019 Gentoo Authors
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 # @SUPPORTED_EAPIS: 4 5 6 7
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} == [45] ]] && 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 if [[ ${EAPI:-0} != [0123456] ]]; then
58 BDEPEND="${VIRTUALX_DEPEND}"
60 DEPEND="${VIRTUALX_DEPEND}"
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."
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."
79 [[ -z ${VIRTUALX_USE} ]] && VIRTUALX_USE="test"
80 DEPEND="${VIRTUALX_USE}? ( ${VIRTUALX_DEPEND} )"
82 IUSE="${VIRTUALX_USE}"
85 if [[ ${EAPI:-0} != [0123456] ]]; then
86 BDEPEND="${VIRTUALX_REQUIRED}? ( ${VIRTUALX_DEPEND} )"
88 DEPEND="${VIRTUALX_REQUIRED}? ( ${VIRTUALX_DEPEND} )"
91 IUSE="${VIRTUALX_REQUIRED}"
92 [[ ${VIRTUALX_REQUIRED} == test ]] &&
93 RESTRICT+=" !test? ( test )"
97 # @FUNCTION: virtualmake
99 # Function which start new Xvfb session
100 # where the VIRTUALX_COMMAND variable content gets executed.
102 debug-print-function ${FUNCNAME} "$@"
104 [[ ${EAPI} == [45] ]] \
105 || die "${FUNCNAME} is unsupported in EAPI > 5, please use virtx"
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}
115 virtx "${VIRTUALX_COMMAND}" "${@}"
120 # @USAGE: <command> [command arguments]
122 # Start new Xvfb session and run commands in it.
124 # IMPORTANT: The command is run nonfatal !!!
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
131 # The rational behind this is the tear down of the started Xfvb session. A
132 # straight die would leave a running session behind.
144 # virtx py.test --verbose
159 debug-print-function ${FUNCNAME} "$@"
161 [[ $# -lt 1 ]] && die "${FUNCNAME} needs at least one argument"
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
171 debug-print "${FUNCNAME}: running Xvfb hack"
173 # The following is derived from Mandrake's hack to allow
174 # compiling without the X display
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
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}"
189 # We really do not want SANDBOX enabled here
190 export SANDBOX_ON="0"
192 debug-print "${FUNCNAME}: ${XVFB} :${XDISPLAY} ${xvfbargs}"
193 ${XVFB} :${XDISPLAY} ${xvfbargs} &>/dev/null &
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:"
202 ${XVFB} :${XDISPLAY} ${xvfbargs}
204 eerror "If possible, correct the above error and try your emerge again."
205 die "Unable to start Xvfb"
208 debug-print "${FUNCNAME}: ${XVFB} :${XDISPLAY} ${xvfbargs}"
209 ${XVFB} :${XDISPLAY} ${xvfbargs} &>/dev/null &
213 # Now enable SANDBOX again if needed.
214 export SANDBOX_ON="${OLD_SANDBOX_ON}"
216 einfo "Starting Xvfb on \$DISPLAY=${XDISPLAY} ..."
218 export DISPLAY=:${XDISPLAY}
219 # Do not break on error, but setup $retval, as we need
221 debug-print "${FUNCNAME}: $@"
226 kill $(cat /tmp/.X${XDISPLAY}-lock)
228 # die if our command failed
229 [[ ${retval} -ne 0 ]] && die "Failed to run '$@'"
231 return 0 # always return 0, it can be altered by failed kill for Xvfb
236 # Same as "make", 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 -j1 ....'"
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 "$@"
251 # Same as "emake", but set up the Xvfb hack if needed.
253 debug-print-function ${FUNCNAME} "$@"
255 [[ ${EAPI} == [45] ]] \
256 || die "${FUNCNAME} is unsupported in EAPI > 5, please use 'virtx emake ....'"
258 VIRTUALX_COMMAND="emake" virtualmake "$@"
263 # Same as "econf", but set up the Xvfb hack if needed.
265 debug-print-function ${FUNCNAME} "$@"
267 [[ ${EAPI} == [45] ]] \
268 || die "${FUNCNAME} is unsupported in EAPI > 5, please use 'virtx econf ....'"
270 VIRTUALX_COMMAND="econf" virtualmake "$@"