1 # Copyright 1999-2012 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 # @ECLASS-VARIABLE: VIRTUALX_REQUIRED
14 # Variable specifying the dependency on xorg-server and xhost.
15 # Possible special values are "always" and "manual", which specify
16 # the dependency to be set unconditionaly or not at all.
17 # Any other value is taken as useflag desired to be in control of
18 # the dependency (eg. VIRTUALX_REQUIRED="kde" will add the dependency
19 # into "kde? ( )" and add kde into IUSE.
20 : ${VIRTUALX_REQUIRED:=test}
22 # @ECLASS-VARIABLE: VIRTUALX_DEPEND
24 # Dep string available for use outside of eclass, in case a more
25 # complicated dep is needed.
26 # You can specify the variable BEFORE inherit to add more dependencies.
27 VIRTUALX_DEPEND="${VIRTUALX_DEPEND}
28 !prefix? ( x11-base/xorg-server[xvfb] )
32 # @ECLASS-VARIABLE: VIRTUALX_COMMAND
34 # Command (or eclass function call) to be run in the X11 environment
35 # (within virtualmake function).
36 : ${VIRTUALX_COMMAND:="emake"}
38 has "${EAPI:-0}" 0 1 && die "virtualx eclass require EAPI=2 or newer."
40 case ${VIRTUALX_REQUIRED} in
44 DEPEND="${VIRTUALX_DEPEND}"
48 # deprecated section YAY.
49 ewarn "QA: VIRTUALX_REQUIRED=optional and VIRTUALX_REQUIRED=tests are deprecated."
50 ewarn "QA: You can drop the variable definition completely from ebuild,"
51 ewarn "QA: because it is default behaviour."
53 if [[ -n ${VIRTUALX_USE} ]]; then
54 # so they like to specify the useflag
55 ewarn "QA: VIRTUALX_USE variable is deprecated."
56 ewarn "QA: Please read eclass manpage to find out how to use VIRTUALX_REQUIRED"
57 ewarn "QA: to achieve the same behaviour."
60 [[ -z ${VIRTUALX_USE} ]] && VIRTUALX_USE="test"
61 DEPEND="${VIRTUALX_USE}? ( ${VIRTUALX_DEPEND} )"
63 IUSE="${VIRTUALX_USE}"
66 DEPEND="${VIRTUALX_REQUIRED}? ( ${VIRTUALX_DEPEND} )"
68 IUSE="${VIRTUALX_REQUIRED}"
72 # @FUNCTION: virtualmake
74 # Function which start new Xvfb session
75 # where the VIRTUALX_COMMAND variable content gets executed.
77 debug-print-function ${FUNCNAME} "$@"
81 local OLD_SANDBOX_ON="${SANDBOX_ON}"
82 local XVFB=$(type -p Xvfb)
83 local XHOST=$(type -p xhost)
84 local xvfbargs="-screen 0 1280x1024x24"
86 # backcompat for maketype
87 if [[ -n ${maketype} ]]; then
88 ewarn "QA: ebuild is exporting \$maketype=${maketype}"
89 ewarn "QA: Ebuild should be migrated to use VIRTUALX_COMMAND=${maketype} instead."
90 ewarn "QA: Setting VIRTUALX_COMMAND to \$maketype conveniently for now."
91 VIRTUALX_COMMAND=${maketype}
94 debug-print "${FUNCNAME}: running Xvfb hack"
96 # The following is derived from Mandrake's hack to allow
97 # compiling without the X display
99 einfo "Scanning for an open DISPLAY to start Xvfb ..."
100 # If we are in a chrooted environment, and there is already a
101 # X server started outside of the chroot, Xvfb will fail to start
102 # on the same display (most cases this is :0 ), so make sure
103 # Xvfb is started, else bump the display number
105 # Azarah - 5 May 2002
106 XDISPLAY=$(i=0; while [[ -f /tmp/.X${i}-lock ]] ; do ((i++));done; echo ${i})
107 debug-print "${FUNCNAME}: XDISPLAY=${XDISPLAY}"
109 # We really do not want SANDBOX enabled here
110 export SANDBOX_ON="0"
112 debug-print "${FUNCNAME}: ${XVFB} :${XDISPLAY} ${xvfbargs}"
113 ${XVFB} :${XDISPLAY} ${xvfbargs} &>/dev/null &
116 local start=${XDISPLAY}
117 while [[ ! -f /tmp/.X${XDISPLAY}-lock ]]; do
118 # Stop trying after 15 tries
119 if ((XDISPLAY - start > 15)) ; then
120 eerror "'${XVFB} :${XDISPLAY} ${xvfbargs}' returns:"
122 ${XVFB} :${XDISPLAY} ${xvfbargs}
124 eerror "If possible, correct the above error and try your emerge again."
125 die "Unable to start Xvfb"
128 debug-print "${FUNCNAME}: ${XVFB} :${XDISPLAY} ${xvfbargs}"
129 ${XVFB} :${XDISPLAY} ${xvfbargs} &>/dev/null &
133 # Now enable SANDBOX again if needed.
134 export SANDBOX_ON="${OLD_SANDBOX_ON}"
136 einfo "Starting Xvfb on \$DISPLAY=${XDISPLAY} ..."
138 export DISPLAY=:${XDISPLAY}
139 # Do not break on error, but setup $retval, as we need
141 debug-print "${FUNCNAME}: ${VIRTUALX_COMMAND} \"$@\""
142 if has "${EAPI}" 2 3; then
143 ${VIRTUALX_COMMAND} "$@"
146 nonfatal ${VIRTUALX_COMMAND} "$@"
151 kill $(cat /tmp/.X${XDISPLAY}-lock)
153 # die if our command failed
154 [[ ${retval} -ne 0 ]] && die "${FUNCNAME}: the ${VIRTUALX_COMMAND} failed."
156 return 0 # always return 0, it can be altered by failed kill for Xvfb
161 # Same as "make", but set up the Xvfb hack if needed.
164 debug-print-function ${FUNCNAME} "$@"
166 ewarn "QA: you should not execute make directly"
167 ewarn "QA: rather execute Xemake -j1 if you have issues with parallel make"
168 VIRTUALX_COMMAND="emake -j1" virtualmake "$@"
173 # Same as "emake", but set up the Xvfb hack if needed.
175 debug-print-function ${FUNCNAME} "$@"
177 VIRTUALX_COMMAND="emake" virtualmake "$@"
182 # Same as "econf", but set up the Xvfb hack if needed.
184 debug-print-function ${FUNCNAME} "$@"
186 VIRTUALX_COMMAND="econf" virtualmake "$@"