-
Notifications
You must be signed in to change notification settings - Fork 1
/
configure
executable file
·228 lines (191 loc) · 3.95 KB
/
configure
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
#!/usr/bin/env sh
# simple and flexible configure script for people who don't like to waste time
# licensed to the public domain
set -e
mkf=config.mk
# these also apply to all libraries in external/
cflags='-std=c99'
cppflags=''
ext_cppflags='-DLUA_USE_POSIX -DLUA_USE_DLOPEN'
ldflags='-lm -Wl,-E'
# optional libraries to build with support for; a dynamically
# linked Lua 5.4 library may be supported here later
optlibs='readline'
withlibs=''
#
## utility functions
#
present () {
command -v "$1" 1>/dev/null 2>/dev/null
}
using () {
printf "using $1\n"
}
throw () {
>&2 printf "$(basename $0): $1\n"
exit 1
}
#
## list utilities
#
# Returns 0 if the given item is in the given list, otherwise returns 1.
inlist () {
local item="$1"
local list="$2"
for elem in $list; do
if [ "$elem" = "$item" ]; then
return 0
fi
done
return 1
}
# Removes the given item from the given list and returns the new list.
removefrom () {
local item="$1"
local list="$2"
local newlist=""
for elem in $list; do
if ! [ "$elem" = "$item" ]; then
newlist="$newlist $elem"
fi
done
printf '%s' "$newlist"
}
# Trims the given string of leading and trailing whitespace.
trim () {
printf '%s' "$1" | xargs
}
#
## flag generators
#
## C compiler
gen_CC () {
if ! [ -z "$cc" ]; then
using "$cc"
return 0
fi
if present clang; then
cc=clang
elif present gcc; then
cc=gcc
elif present cc; then
cc=cc
else
throw "C compiler not found, please acquire a copy of LLVM Clang or the GNU C Compiler"
fi
using "$cc"
}
## flags used in the compilation step
gen_CFLAGS () {
if [ -z "$debug" ]; then
cflags="$cflags -O3"
else
cflags="$cflags -O0 -g"
fi
if inlist readline "$withlibs"; then
cppflags="$cppflags -DLUA_USE_READLINE"
fi
for flag in $cflags $cppflags; do
using "$flag"
done
cflags="$(trim "$cflags")"
cppflags="$(trim "$cppflags")"
}
## flags used in the linking step
gen_LDFLAGS () {
if inlist readline "$withlibs"; then
ldflags="$ldflags $(pkg-config --libs readline)"
fi
if present ld.lld; then
ldflags="$ldflags -fuse-ld=lld"
elif present ld.gold; then
ldflags="$ldflags -fuse-ld=gold"
fi
[ -z "$debug" ] && ldflags="$ldflags -Wl,--gc-sections"
for flag in $ldflags; do
using "$flag"
done
ldflags="$(trim "$ldflags")"
}
## Lua objects
gen_LUAOBJS () {
touch config.mk
for obj in $(make -Cexternal/lua -s echo | grep -E '^BASE_O= ' | cut -d " " -f2-); do
luaobjs="$luaobjs external/lua/$obj"
done
luaobjs="$(trim "$luaobjs")"
}
#
## misc. functions
#
assertvalidlib () {
if ! inlist "$OPTARG" "$optlibs"; then
throw "invalid library '$OPTARG'; supported libraries are:
$optlibs"
fi
}
#
## command line interface
#
while getopts c:dhrw:W: ch; do
case "$ch" in
c) cc="$OPTARG" ;;
d) debug=1 ;;
r) unset debug ;;
h)
cat <<EOF
configure: create an optimised makefile for the current environment
options:
-c: force use of a particular compiler
-d: build in debug mode, with debug symbols enabled
-r: build in release mode with optimisation flags enabled (default)
-h: show this help message
-wname: build with support for the given library; see the list below
-Wname: build without support for the given library
If a -w or -W option is supplied on the command line, then the
argument is checked against the following list of libraries:
EOF
for lib in $optlibs; do
printf ' - %s\n' "$lib"
done
exit 0
;;
w)
assertvalidlib
if ! inlist "$OPTARG" "$withlibs"; then
withlibs="$withlibs $OPTARG"
fi
;;
W)
assertvalidlib
if inlist "$OPTARG" "$withlibs"; then
withlibs="$(removefrom "$OPTARG" "$withlibs")"
fi
;;
?) exit 1 ;;
:) exit 1 ;;
esac
done
#
## create the makefile
#
cd $(dirname $0)
gen_CC
gen_CFLAGS
gen_LDFLAGS
gen_LUAOBJS
printf '# generated by configure
_CC = %s
_CFLAGS = %s
_CPPFLAGS = %s
_LDFLAGS =%s
_EXT_CPPFLAGS = %s
_LUAOBJS = %s
' \
"$cc" \
"$cflags" \
"$cppflags"\
"$ldflags" \
"$ext_cppflags"\
"$luaobjs"\
>"$mkf"