File: utils.sh

package info (click to toggle)
cmus 2.2.0-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,196 kB
  • ctags: 2,714
  • sloc: ansic: 24,078; sh: 1,024; makefile: 209; python: 26
file content (255 lines) | stat: -rw-r--r-- 3,996 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
#!/bin/sh
#
# Copyright 2005 Timo Hirvonen
#
# This file is licensed under the GPLv2.

# initialization {{{

LC_ALL=C
LANG=C

export LC_ALL LANG

if test "$CDPATH"
then
	echo "Exporting CDPATH is dangerous and unnecessary!"
	echo
fi
unset CDPATH

__cleanup()
{
	if test "$DEBUG_CONFIGURE" = y
	then
		echo
		echo "DEBUG_CONFIGURE=y, not removing temporary files"
		ls .tmp-[0-9]*-*
	else
		rm -f .tmp-[0-9]*-*
	fi
}

__abort()
{
	# can't use "die" because stderr is often redirected to /dev/null
	# (stdout could also be redirected but it's not so common)
	echo
	echo
	echo "Aborting. configure failed."
	# this executes __cleanup automatically
	exit 1
}

# clean temporary files on exit
trap '__cleanup' 0

# clean temporary files and die with error message if interrupted
trap '__abort' 1 2 3 13 15

# }}}

# config.mk variable names
makefile_variables=""

# cross compilation, prefix for CC, LD etc.
CROSS=

# argc function_name $# min [max]
argc()
{
	if test $# -lt 3 || test $# -gt 4
	then
		die "argc: expecting 3-4 arguments (got $*)"
	fi

	if test $# -eq 3
	then
		if test $2 -lt $3
		then
			die "$1: expecting at least $3 arguments"
		fi
	else
		if test $2 -lt $3 || test $2 -gt $4
		then
			die "$1: expecting $3-$4 arguments"
		fi
	fi
}

# print warning message (all parameters)
warn()
{
	echo "$@" >&2
}

# print error message (all parameters) and exit
die()
{
	warn "$@"
	exit 1
}

# usage: 'tmp_file .c'
# get filename for temporary file
tmp_file()
{
	if test -z "$__tmp_file_counter"
	then
		__tmp_file_counter=0
	fi

	while true
	do
		__tmp_filename=.tmp-${__tmp_file_counter}-${1}
		__tmp_file_counter=`expr $__tmp_file_counter + 1`
		test -f "$__tmp_filename" || break
	done
	echo "$__tmp_filename"
}

# get variable value
#
# @name:  name of the variable
get_var()
{
	eval echo \"\$${1}\"
}

# set variable by name
#
# @name:  name of the variable
# @value: value of the variable
set_var()
{
	eval $1='$2'
}

# set variable @name to @default IF NOT SET OR EMPTY
#
# @name:  name of the variable
# @value: value of the variable
var_default()
{
	test "`get_var $1`" || set_var $1 "$2"
}

# usage: echo $foo | to_upper
to_upper()
{
	# stupid solaris tr doesn't understand ranges
	tr abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ
}

# portable which command
path_find()
{
	case $1 in
	*/*)
		if test -x "$1"
		then
			echo "$1"
			return 0
		fi
		return 1
		;;
	esac

	_ifs="$IFS"
	IFS=:
	for __pf_i in $PATH
	do
		if test -x "$__pf_i/$1"
		then
			IFS="$_ifs"
			echo "$__pf_i/$1"
			return 0
		fi
	done
	IFS="$_ifs"
	return 1
}

show_usage()
{
	cat <<EOF
Usage ./configure [-f FILE] [OPTION=VALUE]...

  -f FILE         Read OPTION=VALUE list from FILE (sh script)
$USAGE
EOF
	exit 0
}

# @tmpfile: temporary file
# @file:    file to update
#
# replace @file with @tmpfile if their contents differ
update_file()
{
	if test -f "$2"
	then
		if cmp "$2" "$1" 2>/dev/null 1>&2
		then
			return 0
		fi
		echo "updating $2"
	else
		echo "creating $2"
	fi
	mv -f "$1" "$2"
}

# optionally used after try_compile
__try_link()
{
	__exe=`tmp_file prog`
	case $__check_lang in
	c)
		__cmd="$LD $LDFLAGS $@ -o $__exe $__obj"
		$LD $LDFLAGS "$@" -o $__exe $__obj 2>/dev/null
		;;
	cxx)
		__cmd="$CXXLD $CXXLDFLAGS $@ -o $__exe $__obj"
		$CXXLD $CXXLDFLAGS "$@" -o $__exe $__obj 2>/dev/null
		;;
	esac
	return $?
}

# optionally used after try_compile
__compile_failed()
{
	warn
	warn "Failed to compile simple program:"
	warn "---"
	cat $__src >&2
	warn "---"
	warn "Command: $__cmd"
	case $__check_lang in
	c)
		warn "Make sure your CC and CFLAGS are sane."
		;;
	cxx)
		warn "Make sure your CXX and CXXFLAGS are sane."
		;;
	esac
	exit 1
}

# optionally used after __try_link or try_link
__link_failed()
{
	warn
	warn "Failed to link simple program."
	warn "Command: $__cmd"
	case $__check_lang in
	c)
		warn "Make sure your LD (usually same as CC) and LDFLAGS are sane."
		;;
	cxx)
		warn "Make sure your CXXLD (usually same as CXX) and CXXLDFLAGS are sane."
		;;
	esac
	exit 1
}