virtualx.eclass 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. # Copyright 1999-2015 Gentoo Foundation
  2. # Distributed under the terms of the GNU General Public License v2
  3. # @ECLASS: virtualx.eclass
  4. # @MAINTAINER:
  5. # x11@gentoo.org
  6. # @AUTHOR:
  7. # Original author: Martin Schlemmer <azarah@gentoo.org>
  8. # @BLURB: This eclass can be used for packages that needs a working X environment to build.
  9. if [[ ! ${_VIRTUAL_X} ]]; then
  10. case "${EAPI:-0}" in
  11. 0|1)
  12. die "virtualx.eclass: EAPI ${EAPI} is too old."
  13. ;;
  14. 2|3|4|5|6)
  15. ;;
  16. *)
  17. die "virtualx.eclass: EAPI ${EAPI} is not supported yet."
  18. ;;
  19. esac
  20. [[ ${EAPI} == [2345] ]] && inherit eutils
  21. # @ECLASS-VARIABLE: VIRTUALX_REQUIRED
  22. # @DESCRIPTION:
  23. # Variable specifying the dependency on xorg-server and xhost.
  24. # Possible special values are "always" and "manual", which specify
  25. # the dependency to be set unconditionaly or not at all.
  26. # Any other value is taken as useflag desired to be in control of
  27. # the dependency (eg. VIRTUALX_REQUIRED="kde" will add the dependency
  28. # into "kde? ( )" and add kde into IUSE.
  29. : ${VIRTUALX_REQUIRED:=test}
  30. # @ECLASS-VARIABLE: VIRTUALX_DEPEND
  31. # @DESCRIPTION:
  32. # Dep string available for use outside of eclass, in case a more
  33. # complicated dep is needed.
  34. # You can specify the variable BEFORE inherit to add more dependencies.
  35. VIRTUALX_DEPEND="${VIRTUALX_DEPEND}
  36. !prefix? ( x11-base/xorg-server[xvfb] )
  37. x11-apps/xhost
  38. "
  39. # @ECLASS-VARIABLE: VIRTUALX_COMMAND
  40. # @DESCRIPTION:
  41. # Command (or eclass function call) to be run in the X11 environment
  42. # (within virtualmake function).
  43. : ${VIRTUALX_COMMAND:="emake"}
  44. case ${VIRTUALX_REQUIRED} in
  45. manual)
  46. ;;
  47. always)
  48. DEPEND="${VIRTUALX_DEPEND}"
  49. RDEPEND=""
  50. ;;
  51. optional|tests)
  52. [[ ${EAPI} == [2345] ]] \
  53. || die 'Values "optional" and "tests" for VIRTUALX_REQUIRED are banned in EAPI > 5'
  54. # deprecated section YAY.
  55. eqawarn "VIRTUALX_REQUIRED=optional and VIRTUALX_REQUIRED=tests are deprecated."
  56. eqawarn "You can drop the variable definition completely from ebuild,"
  57. eqawarn "because it is default behaviour."
  58. if [[ -n ${VIRTUALX_USE} ]]; then
  59. # so they like to specify the useflag
  60. eqawarn "VIRTUALX_USE variable is deprecated."
  61. eqawarn "Please read eclass manpage to find out how to use VIRTUALX_REQUIRED"
  62. eqawarn "to achieve the same behaviour."
  63. fi
  64. [[ -z ${VIRTUALX_USE} ]] && VIRTUALX_USE="test"
  65. DEPEND="${VIRTUALX_USE}? ( ${VIRTUALX_DEPEND} )"
  66. RDEPEND=""
  67. IUSE="${VIRTUALX_USE}"
  68. ;;
  69. *)
  70. DEPEND="${VIRTUALX_REQUIRED}? ( ${VIRTUALX_DEPEND} )"
  71. RDEPEND=""
  72. IUSE="${VIRTUALX_REQUIRED}"
  73. ;;
  74. esac
  75. # @FUNCTION: virtualmake
  76. # @DESCRIPTION:
  77. # Function which start new Xvfb session
  78. # where the VIRTUALX_COMMAND variable content gets executed.
  79. virtualmake() {
  80. debug-print-function ${FUNCNAME} "$@"
  81. [[ ${EAPI} == [2345] ]] \
  82. || die "${FUNCNAME} is unsupported in EAPI > 5, please use virtx"
  83. # backcompat for maketype
  84. if [[ -n ${maketype} ]]; then
  85. [[ ${EAPI} == [2345] ]] || die "maketype is banned in EAPI > 5"
  86. eqawarn "ebuild is exporting \$maketype=${maketype}"
  87. eqawarn "Ebuild should be migrated to use 'virtx command' instead."
  88. VIRTUALX_COMMAND=${maketype}
  89. fi
  90. virtx "${VIRTUALX_COMMAND}" "${@}"
  91. }
  92. # @FUNCTION: virtx
  93. # @USAGE: <command> [command arguments]
  94. # @DESCRIPTION:
  95. # Start new Xvfb session and run commands in it.
  96. #
  97. # IMPORTANT: The command is run nonfatal !!!
  98. #
  99. # This means we are checking for the return code and raise an exception if it
  100. # isn't 0. So you need to make sure that all commands return a proper
  101. # code and not just die. All eclass function used should support nonfatal
  102. # calls properly.
  103. #
  104. # The rational behind this is the tear down of the started Xfvb session. A
  105. # straight die would leave a running session behind.
  106. #
  107. # Example:
  108. #
  109. # @CODE
  110. # src_test() {
  111. # virtx default
  112. # }
  113. # @CODE
  114. #
  115. # @CODE
  116. # python_test() {
  117. # virtx py.test --verbose
  118. # }
  119. # @CODE
  120. #
  121. # @CODE
  122. # my_test() {
  123. # some_command
  124. # return $?
  125. # }
  126. #
  127. # src_test() {
  128. # virtx my_test
  129. # }
  130. # @CODE
  131. virtx() {
  132. debug-print-function ${FUNCNAME} "$@"
  133. [[ $# -lt 1 ]] && die "${FUNCNAME} needs at least one argument"
  134. local i=0
  135. local retval=0
  136. local OLD_SANDBOX_ON="${SANDBOX_ON}"
  137. local XVFB XHOST XDISPLAY
  138. local xvfbargs="-screen 0 1280x1024x24 +extension RANDR"
  139. XVFB=$(type -p Xvfb) || die
  140. XHOST=$(type -p xhost) || die
  141. debug-print "${FUNCNAME}: running Xvfb hack"
  142. export XAUTHORITY=
  143. # The following is derived from Mandrake's hack to allow
  144. # compiling without the X display
  145. einfo "Scanning for an open DISPLAY to start Xvfb ..."
  146. # If we are in a chrooted environment, and there is already a
  147. # X server started outside of the chroot, Xvfb will fail to start
  148. # on the same display (most cases this is :0 ), so make sure
  149. # Xvfb is started, else bump the display number
  150. #
  151. # Azarah - 5 May 2002
  152. XDISPLAY=$(i=0; while [[ -f /tmp/.X${i}-lock ]] ; do ((i++));done; echo ${i})
  153. debug-print "${FUNCNAME}: XDISPLAY=${XDISPLAY}"
  154. # We really do not want SANDBOX enabled here
  155. export SANDBOX_ON="0"
  156. debug-print "${FUNCNAME}: ${XVFB} :${XDISPLAY} ${xvfbargs}"
  157. ${XVFB} :${XDISPLAY} ${xvfbargs} &>/dev/null &
  158. sleep 2
  159. local start=${XDISPLAY}
  160. while [[ ! -f /tmp/.X${XDISPLAY}-lock ]]; do
  161. # Stop trying after 15 tries
  162. if ((XDISPLAY - start > 15)) ; then
  163. eerror "'${XVFB} :${XDISPLAY} ${xvfbargs}' returns:"
  164. echo
  165. ${XVFB} :${XDISPLAY} ${xvfbargs}
  166. echo
  167. eerror "If possible, correct the above error and try your emerge again."
  168. die "Unable to start Xvfb"
  169. fi
  170. ((XDISPLAY++))
  171. debug-print "${FUNCNAME}: ${XVFB} :${XDISPLAY} ${xvfbargs}"
  172. ${XVFB} :${XDISPLAY} ${xvfbargs} &>/dev/null &
  173. sleep 2
  174. done
  175. # Now enable SANDBOX again if needed.
  176. export SANDBOX_ON="${OLD_SANDBOX_ON}"
  177. einfo "Starting Xvfb on \$DISPLAY=${XDISPLAY} ..."
  178. export DISPLAY=:${XDISPLAY}
  179. # Do not break on error, but setup $retval, as we need
  180. # to kill Xvfb
  181. debug-print "${FUNCNAME}: $@"
  182. if has "${EAPI}" 2 3; then
  183. "$@"
  184. retval=$?
  185. else
  186. nonfatal "$@"
  187. retval=$?
  188. fi
  189. # Now kill Xvfb
  190. kill $(cat /tmp/.X${XDISPLAY}-lock)
  191. # die if our command failed
  192. [[ ${retval} -ne 0 ]] && die "Failed to run '$@'"
  193. return 0 # always return 0, it can be altered by failed kill for Xvfb
  194. }
  195. # @FUNCTION: Xmake
  196. # @DESCRIPTION:
  197. # Same as "make", but set up the Xvfb hack if needed.
  198. # Deprecated call.
  199. Xmake() {
  200. debug-print-function ${FUNCNAME} "$@"
  201. [[ ${EAPI} == [2345] ]] \
  202. || die "${FUNCNAME} is unsupported in EAPI > 5, please use 'virtx emake -j1 ....'"
  203. eqawarn "you should not execute make directly"
  204. eqawarn "rather execute Xemake -j1 if you have issues with parallel make"
  205. VIRTUALX_COMMAND="emake -j1" virtualmake "$@"
  206. }
  207. # @FUNCTION: Xemake
  208. # @DESCRIPTION:
  209. # Same as "emake", but set up the Xvfb hack if needed.
  210. Xemake() {
  211. debug-print-function ${FUNCNAME} "$@"
  212. [[ ${EAPI} == [2345] ]] \
  213. || die "${FUNCNAME} is unsupported in EAPI > 5, please use 'virtx emake ....'"
  214. VIRTUALX_COMMAND="emake" virtualmake "$@"
  215. }
  216. # @FUNCTION: Xeconf
  217. # @DESCRIPTION:
  218. # Same as "econf", but set up the Xvfb hack if needed.
  219. Xeconf() {
  220. debug-print-function ${FUNCNAME} "$@"
  221. [[ ${EAPI} == [2345] ]] \
  222. || die "${FUNCNAME} is unsupported in EAPI > 5, please use 'virtx econf ....'"
  223. VIRTUALX_COMMAND="econf" virtualmake "$@"
  224. }
  225. _VIRTUAL_X=1
  226. fi