Bump version to 0.36.9
[cygport.git] / cygclass / xvfb.cygclass
blobf39dcbbac8ea84cb20ca84af975da573df53ffdc
1 ################################################################################
3 # xvfb.cygclass - for building packages which require a running X server
5 # Part of cygport - Cygwin packaging application
6 # Copyright (C) 2006-2020 Cygport authors
7 # Provided by the Cygwin project <https://cygwin.com/>
9 # cygport is free software: you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation, either version 3 of the License, or
12 # (at your option) any later version.
14 # cygport is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with cygport.  If not, see <https://www.gnu.org/licenses/>.
22 ################################################################################
24 #****h* Cygclasses/xvfb.cygclass
25 #  SYNOPSIS
26 #  inherit xvfb
27 #  DESCRIPTION
28 #  Some packages require a running X server while building; if one is already
29 #  running, that usually suffices, but checking that one is present, and
30 #  starting one if not, is complicated to do in an automated fashion.
32 #  This cygclass provides a function which handles checking for, and
33 #  if necessary, launching a temporary X session.
34 #****
36 #****C* xvfb.cygclass/xvfb_run
37 #  SYNOPSIS
38 #  xvfb_run COMMAND [ARGUMENTS...]
39 #  DESCRIPTION
40 #  Runs the given COMMAND (binary, script, or shell function), with
41 #  optional ARGUMENTS, which requires an X session to complete.
42 #  NOTE
43 #  If several consecutive commands require an X session, then they can be
44 #  wrapped in a single custom function which can then be passed to xvfb_run,
45 #  e.g.:
46 #    mycommand() { for f in *; do foo; bar; done ; }
47 #    src_compile() {
48 #      ....
49 #      xvfb_run mycommand
50 #      ....
51 #    }
52 #  REQUIRES
53 #  dbus, gamin, xmodmap, xorg-server
54 #****
55 xvfb_run() {
56         local n
57         local xvfb_display xvfb_pid dbus_pid gamin_pid
58         local xvfb_sock dbus_sock gamin_sock
60         check_prog_req Xorg xorg-server
61         check_prog_req xmodmap  # relatively safe dep, required by xinit
63         if defined DISPLAY && xmodmap -display $DISPLAY &> /dev/null
64         then
65                 xvfb_display=$DISPLAY
66         else
67                 for ((n=1; ; n++))
68                 do
69                         if [ ! -e /tmp/.X11-unix/X${n} -a ! -e /tmp/.X${n}-lock ]
70                         then
71                                 xvfb_display=:${n}
72                                 Xorg $xvfb_display -config cygport-xvfb.conf -nolisten tcp -noreset &> /dev/null &
73                                 xvfb_pid=$!
74                                 xvfb_sock="/tmp/.X${n}-lock /tmp/.X11-unix/X${n}"
75                                 if xmodmap -display $xvfb_display &> /dev/null
76                                 then
77                                         export DISPLAY=${xvfb_display}
78                                         break
79                                 else
80                                         # why not ?!?
81                                         /bin/kill $xvfb_pid &> /dev/null || true
82                                         /bin/kill -s KILL $xvfb_pid &> /dev/null || true
83                                         rm -f $xvfb_sock
84                                 fi
85                         fi
86                 done
87         fi
89         # avoid the most common fork failures
90         case ${CBUILD##*-} in
91         cygwin*)
92                 if [ x"$(ps | grep gam_server)" = x ]
93                 then
94                         export GAM_CLIENT_ID="cygport"
95                         /usr/libexec/gam_server --notimeout $GAM_CLIENT_ID &
96                         gamin_pid=$!
97                         gamin_sock="/tmp/fam-$(id -un)/fam-"$GAM_CLIENT_ID
98                 fi
99         esac
101         # what doesn't require a D-Bus session nowadays?
102         if ! defined DBUS_SESSION_BUS_ADDRESS
103         then
104                 eval `dbus-launch --sh-syntax`
105                 dbus_pid=$DBUS_SESSION_BUS_PID
106                 dbus_sock=$(echo $DBUS_SESSION_BUS_ADDRESS | sed -e 's/.*path=\(.*\),.*/\1/')
107         fi
109         # now run the task
110         "$@"
112         # cleanup
113         /bin/kill $xvfb_pid $dbus_pid $gamin_pid &> /dev/null || true
114         /bin/kill -s KILL $xvfb_pid $dbus_pid $gamin_pid &> /dev/null || true
115         rm -f $xvfb_sock $dbus_sock $gamin_sock
118 readonly -f xvfb_run