Timeline: Fix locking behavior of Control Sequences.
[nondaw.git] / scripts / config-funcs
blob73426305c9e9b521f4eb074858268129858b59f2
1 #!/bin/sh
3 # Copyright (C) 2008 Jonathan Moore Liles #
4 # #
5 # This program is free software; you can redistribute it and/or modify it #
6 # under the terms of the GNU General Public License as published by the #
7 # Free Software Foundation; either version 2 of the License, or (at your #
8 # option) any later version. #
9 # #
10 # This program is distributed in the hope that it will be useful, but WITHOUT #
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
12 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for #
13 # more details. #
14 # #
15 # You should have received a copy of the GNU General Public License along #
16 # with This program; see the file COPYING. If not,write to the Free Software #
17 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #
20 . scripts/colors
22 #####
23 ## support functions for 'configure' scripts.
25 fatal ()
27 echo "$BOLD$RED$*$SGR0" > /dev/stderr
28 exit 255
31 UPDATE=no
32 HELP=no
34 split ()
36 while [ $# -gt 0 ]
38 echo $1
39 shift 1
40 done
43 if [ $# -gt 0 ]
44 then
45 case "$1" in
46 --update)
47 UPDATE=yes
48 shift 1
50 --help)
51 HELP=yes
52 shift 1
55 # fatal "This is not an autoconf script. Run it without any options and you will be prompted."
57 esac
59 if [ $# -gt 0 ]
60 then
61 echo "## options" > .config
63 split "$@" | sed '
64 s/--\(enable\|disable\)-\([^ =]\+\)/--\1-\U\2/g;
65 s/--enable-\([^ =]\+\)=\(.*\)/USE_\1=\2/g;
66 s/--enable-\([^ =]\+\)/USE_\1=yes/g;
67 s/--disable-\([^ =]\+\)/USE_\1=no/g;
68 s/--\([^ =]\+\)/\1/g;
69 ' | sed -n '/^[^ =]\+=./p' >> .config
70 UPDATE=yes;
74 if [ $HELP != yes ] && [ $UPDATE != yes ]
75 then
76 if ! ( [ -t 0 ] && [ -t 1 ] )
77 then
78 fatal "not a terminal!"
82 ask ()
84 local A D O
86 D="`eval echo \\$$2`"
87 D=${D:-$3}
89 if [ $HELP = yes ]
90 then
91 if [ "$3" = yes ] || [ "$3" = no ]
92 then
93 O=`echo -n "$2" | sed s/^USE_/--enable-/ | tr '[[:upper:]]' '[[:lower:]]'`
94 else
95 O=`echo -n "--$2" | tr '[[:upper:]]' '[[:lower:]]'`
98 printf " ${BOLD}${GREEN}%-15s${SGR0}\t%-40s (currently: ${BOLD}%s${SGR0})\n" "$O" "$1" "$D"
100 return
103 echo -n "$BLACK$BOLD::$SGR0 ${1}? [$BOLD${D}$SGR0] "
105 if [ $UPDATE = yes ]
106 then
107 A="$D"
108 echo
109 else
110 read A
111 A=${A:-$D}
114 if [ "$3" = yes ] || [ "$3" = no ]
115 then
116 case "$A" in
117 no | n | N) A=no ;;
118 yes | y | Y) A=yes ;;
119 * ) fatal "Invalid response. Must be 'yes' or 'no'" ;;
120 esac
123 append "${2}=${A:-$D}"
125 eval "${2}='${A:-$D}'"
128 ok ()
130 echo ' '`tput cuf 30`"$BOLD${GREEN}ok${SGR0} ${*:+${BOLD}${BLACK}($*)${SGR0}}"
133 failed ()
135 echo ' '`tput cuf 30`"$BOLD${RED}failed!${SGR0}" > /dev/stderr
136 rm -f .config
139 missing ()
141 echo ' '`tput cuf 30`"$BOLD${YELLOW}missing!${SGR0}" > /dev/stderr
144 using ()
146 [ "`eval echo \\$USE_$1`" = yes ]
148 return $?
151 upcase ()
153 echo "$*" | tr '[[:lower:]]' '[[:upper:]]'
156 extract_options ()
158 local line name value
160 if [ -f .config ]
161 then
163 while read line
165 [ "$line" = "## options" ] && break
166 done
168 while read line
170 if [ "$line" = "## libs" ]
171 then
172 break
173 else
174 name=${line%=*}
175 value=${line#*=}
176 eval "$name='$value'"
178 done
179 } < .config
183 begin ()
185 echo -n "Checking sanity..."
186 require_command tput tput > /dev/null
187 require_command pkg_config pkg-config > /dev/null
188 require_command sed sed > /dev/null
192 warn ()
194 echo " ${BOLD}${YELLOW}* ${SGR0}${BOLD}$*"
197 info ()
199 echo "${BOLD}${CYAN}--- ${SGR0}$*"
202 begin_options ()
204 # get the old values
205 extract_options
207 if [ $HELP = yes ]
208 then
209 echo
210 warn "This is a ${BOLD}non-configure${SGR0} script. Run without any arguments and you will be prompted"
211 warn "with configuration choices. Alternatively, you may use the following autoconf style"
212 warn "arguments for non-interactive configuration."
213 echo
214 echo " Available options:"
215 echo
216 else
217 echo > .config
218 append "# This file was automatically generated on `date`. Any changes may be lost!"
219 append "## options"
221 if [ $UPDATE = yes ]
222 then
223 info "Updating configuration"
224 else
225 info "Configuration required"
230 begin_tests ()
232 if [ $HELP = yes ]
233 then
234 echo
235 exit 0;
238 append "## libs"
239 extract_options
242 append ()
244 echo "$1" >> .config
247 end ()
249 info "Configuration complete"
250 touch .config
253 require_command ()
255 echo -n "Checking for ${BOLD}$1${SGR0}..."
257 local name;
259 if [ -x "$2" ]
260 then
261 name="$PWD/$2"
262 ok "$name"
263 elif [ -x "`which "$2"`" ]
264 then
265 name="`which "$2"`"
266 ok "$name"
267 else
268 failed
269 fatal "Command $1 not found."
272 append "$1=$name"
275 require_package ()
277 local name
279 echo -n "Checking for $BOLD$1$SGR0..."
280 if ! pkg-config --exists $3
281 then
282 failed
283 fatal "Required package $1 doesn't appear to be installed."
284 elif ! pkg-config --atleast-version $2 $3
285 then
286 failed
287 fatal "The installed version of $1 (`pkg-config --mod-version $3`) is too old."
290 name="`upcase \"$1\"`"
291 append "${name}_LIBS=`pkg-config --libs $3 | sed 's/,\\?--as-needed//g'`"
292 append "${name}_CFLAGS=-DHAVE_${1} `pkg-config --cflags $3`"
294 ok `pkg-config --modversion "$3"`
295 return 0
298 suggest_package ()
300 local name
302 echo -n "Checking for $BOLD$1$SGR0..."
303 if ! pkg-config --exists $3
304 then
305 missing
306 warn "Suggested package $1 doesn't appear to be installed. Some functionality may be missing from your build."
307 warn "Continuing without $1...\n"
308 return 1;
309 elif ! pkg-config --atleast-version $2 $3
310 then
311 missing
312 warn "The installed version of suggested package $1 (`pkg-config --mod-version $3`) is too old."
313 warn "Continuing without $1..."
314 return 1;
317 name="`upcase \"$1\"`"
318 append "${name}_LIBS=`pkg-config --libs $3 | sed 's/,\\?--as-needed//g'`"
319 append "${name}_CFLAGS=-DHAVE_${1} `pkg-config --cflags $3`"
321 ok `pkg-config --modversion "$3"`
322 return 0
325 _test_version ()
327 if [ $# = 6 ]
328 then
329 [ $1 -gt $4 ] && return 0
330 [ $1 -eq $4 ] && [ $2 -gt $5 ] && return 0
331 [ $1 -eq $4 ] && [ $2 -eq $5 ] && [ $3 -gt $6 ] && return 0
332 [ $1 -eq $4 ] && [ $2 -eq $5 ] && [ $3 -eq $6 ] && return 0
333 return 1
334 elif [ $# = 4 ]
335 then
336 [ $1 -gt $3 ] && return 0
337 [ $1 -eq $3 ] && [ $2 -eq $4 ] && return 0
338 return 1
342 # return true if #1 is greater than or equal to $2
343 test_version ()
345 local IFS
346 IFS='.'
348 _test_version $1 $2
351 version_of ()
353 echo `pkg-config --modversion $1`
356 hostname_resolvable ()
358 ping -c1 `hostname` >/dev/null 2>/dev/null
361 require_FLTK ()
363 local use
364 local appendflags
366 require_command ntk_config lib/ntk/ntk-config
368 echo -n "Checking for ${BOLD}FLTK${SGR0}..."
370 FLTK_VERSION=`lib/ntk/ntk-config --version`
372 if ! test_version $FLTK_VERSION $1
373 then
374 failed
375 fatal "The installed FLTK version ($FLTK_VERSION) is too old."
376 else
377 ok $FLTK_VERSION
380 use=
381 appendflags=
383 while [ $# -gt 1 ]
385 shift 1
387 [ "$1" = 'cairo' ] && appendflags="-DFLTK_HAVE_CAIRO=1 -DUSE_X11=1"
389 use="$use --use-$1"
390 done
392 append "FLTK_LIBS=`lib/ntk/ntk-config $use --libs | sed 's/,\\?--as-needed//g'`"
393 append "FLTK_LDFLAGS=`lib/ntk/ntk-config $use --ldflags | sed 's/,\\?--as-needed//g'`"
394 append "FLTK_CFLAGS=`lib/ntk/ntk-config $use --cflags` $appendflags"