HOTFIX: Adjust Windows build instructions repo
[foam-extend-3.2.git] / wmake / wmakeScheduler
blobd88240ebdf5d301f6b8a0d31f0e4e2136ff6c1a2
1 #!/bin/bash
2 #------------------------------------------------------------------------------
3 # ========= |
4 # \\ / F ield | foam-extend: Open Source CFD
5 # \\ / O peration | Version: 3.2
6 # \\ / A nd | Web: http://www.foam-extend.org
7 # \\/ M anipulation | For copyright notice see file Copyright
8 #------------------------------------------------------------------------------
9 # License
10 # This file is part of foam-extend.
12 # foam-extend is free software: you can redistribute it and/or modify it
13 # under the terms of the GNU General Public License as published by the
14 # Free Software Foundation, either version 3 of the License, or (at your
15 # option) any later version.
17 # foam-extend is distributed in the hope that it will be useful, but
18 # WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 # General Public License for more details.
22 # You should have received a copy of the GNU General Public License
23 # along with foam-extend. If not, see <http://www.gnu.org/licenses/>.
25 # Script
26 # wmakeScheduler
28 # Description
29 # Scheduler for network distributed compilations using wmake.
30 # - WM_HOSTS contains a list of hosts and number of concurrent processes
31 # eg,
32 # export WM_HOSTS="hostA:1 hostB:2 hostC:1"
33 # - WM_COLOURS contains a list of colours to cycle through
34 # export WM_COLOURS="black blue green cyan red magenta yellow"
36 # Sources the relevant cshrc/bashrc if not set.
38 # WM_PROJECT_DIR, WM_PROJECT and WM_PROJECT_VERSION will have been set
39 # before calling this routine.
40 # FOAM_INST_DIR may possibly have been set (to find installation)
42 # Usage
43 # wmakeScheduler COMMAND
44 # run 'COMMAND' on one of the slots listed in WM_HOSTS
46 # wmakeScheduler -count
47 # count the total number of slots available in WM_HOSTS
48 # eg, export WM_NCOMPPROCS=$(wmakeScheduler -count)
50 #------------------------------------------------------------------------------
51 lockDir=$HOME/.wmakeScheduler
53 # fallback - 1 core on current host
54 : ${WM_HOSTS:=$HOST:1}
56 # count the total number of slots available and exit
57 if [ "$1" = "-count" ]
58 then
59 expr $(
60 for slotGroup in $WM_HOSTS
62 n=${slotGroup##*:}
63 [ "$n" = "${slotGroup%%:*}" ] && n=1 # missing ':'
64 echo "+ ${n:-1}"
65 done
67 exit 0
70 # where to source WM_PROJECT settings in a remote shell
71 # This code tries to figure out which cshrc or bashrc to execute.
72 # !! Assumes remote computer running same shell and startup files
73 # in same location
75 sourceFoam=false # fallback command
76 case $SHELL in
77 */csh | */tcsh ) # [t]csh vs bash|ksh|sh
78 shellRc=cshrc
81 shellRc=bashrc
83 esac
85 # check ~/.$WM_PROJECT/$WM_PROJECT_VERSION/
86 # check ~/.$WM_PROJECT/
87 # check <installedProject>/etc/
88 if [ "$WM_PROJECT" ]
89 then
90 for i in \
91 $HOME/.$WM_PROJECT/$WM_PROJECT_VERSION \
92 $HOME/.$WM_PROJECT \
93 $WM_PROJECT_DIR/etc \
96 if [ -f "$i/$shellRc" ]
97 then
98 sourceFoam="$i/$shellRc"
99 break
101 done
104 # Construct test string for remote execution.
105 # Source WM_PROJECT settings if WM_PROJECT environment not set.
106 # attempt to preserve the installation directory 'FOAM_INST_DIR'
107 case $sourceFoam in
108 */bashrc)
109 if [ "$FOAM_INST_DIR" ]
110 then
111 sourceFoam='[ "$WM_PROJECT" ] || '"FOAM_INST_DIR=$FOAM_INST_DIR . $sourceFoam"
112 else
113 sourceFoam='[ "$WM_PROJECT" ] || '". $sourceFoam"
117 */cshrc)
118 # TODO: csh equivalent to bash code (preserving FOAM_INST_DIR)
119 sourceFoam='if ( ! $?WM_PROJECT ) source '"$sourceFoam"
121 esac
123 # quote double-quotes for remote command line
124 rcmd=$(echo $* | sed -e s/\"/\'\"\'/g)
125 ## the same, without forking (not ksh, maybe not /bin/sh either)
126 # rcmd=$(while [ "$#" -gt 0 ]; do echo "${1//\"/'\"'}"; shift; done)
129 # Convert WM_COLOURS into an array
130 declare colourList
131 nColours=0
132 for col in $WM_COLOURS
134 colourList[$nColours]=$col
135 ((nColours = $nColours + 1))
136 done
138 # Bashism: make pipe fail early.
139 # This ensures the return value of the command is returned and not of the
140 # colouring pipe etc.
141 set -o pipefail
145 # colour output by argument 1
147 colourPipe()
149 if [ "$1" ]
150 then
152 while read line
154 setterm -foreground $1
155 echo "$line"
156 done
157 setterm -foreground default
159 else
165 colourIndex=0
167 while :
169 for slotGroup in $WM_HOSTS
171 # split 'host:N', but catch 'host:' and 'host' too
172 host=${slotGroup%%:*}
173 n=${slotGroup##*:}
174 [ "$n" = "$host" ] && n=1 # missing ':'
175 : ${n:=1}
178 while [ "$i" -lt "$n" ]
180 lockFile="$lockDir/$host:$i"
181 if lockfile -r0 "$lockFile" 2>/dev/null
182 then
183 if [ "$nColours" -gt 0 ]
184 then
185 # Set colour
186 colour="${colourList[$colourIndex]}"
188 if [ "$host" = "$HOST" ]; then
189 eval $* 2>&1 | colourPipe "$colour"
190 else
191 ssh $host "$sourceFoam 2>/dev/null; cd $PWD && $rcmd" 2>&1 | colourPipe "$colour"
193 retval=$?
194 else
195 if [ "$host" = "$HOST" ]; then
196 eval $* 2>&1
197 else
198 ssh $host "$sourceFoam 2>/dev/null; cd $PWD && $rcmd" 2>&1
200 retval=$?
203 # Release lock
204 rm -f "$lockFile" 2>/dev/null
205 exit $retval
207 i=$(expr $i + 1)
209 # Cycle through colours. Note: outside lock clause!
210 colourIndex=$(expr $colourIndex + 1)
211 [ "$colourIndex" -lt "$nColours" ] || colourIndex=0
213 done
214 done
215 # Did not find any free slots. Rest a bit.
216 sleep 1
217 done
219 if [ "$nColours" -gt 0 ]
220 then
221 setterm -foreground default
224 #------------------------------------------------------------------------------