-
Notifications
You must be signed in to change notification settings - Fork 2
/
configure
executable file
·344 lines (274 loc) · 7.02 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
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#!/usr/bin/env bash
set -e
SCRIPT_DIR=$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &> /dev/null && pwd)
CONFIG_MK="$SCRIPT_DIR/config.mk"
PREFIX='/usr/local'
EXEC_PREFIX="$PREFIX"
BUILD_PREFIX="$SCRIPT_DIR/build"
SRC_DIR="$SCRIPT_DIR/src"
PKG_CONFIG_DIR=""
USE_SYSTEM_DYNAMIC_LINKER=0
function error() {
echo "$0: error: $1"
exit 1
}
function show_help() {
echo "\`configure\` configures this package to your system."
echo
echo "Usage: $0 [OPTION]... [VAR=VALUE]..."
echo
echo "To assign environment variables (e.g., CC, CFLAGS...), specify them as"
echo "VAR=VALUE. See below for descriptions of some of the useful variables."
echo
echo "Defaults for the options are specified in brackets."
echo
echo "Options:"
echo " -h, --help display this help text and exit"
echo " --prefix=PREFIX install files in PREFIX [$PREFIX]"
echo " --exec-prefix=EPREFIX install executables in PREFIX [$EXEC_PREFIX]"
echo " --build-prefix=BPREFIX put build files in BPREFIX [$BUILD_PREFIX]"
echo " --system-dynamic-linker use the system's dynamic linker [/usr/lib64/ld-linux-x86-64.so.2]"
echo
}
while [[ $# -gt 0 ]]; do
case $1 in
--prefix=*)
PREFIX=$(realpath ${1#*=})
;;
--build-prefix=*)
BUILD_PREFIX=$(realpath ${1#*=})
;;
--exec-prefix=*)
EXEC_PREFIX=$(realpath ${1#*=})
;;
--src-dir=*)
SRC_DIR=$(realpath ${1#*=})
;;
--prefix | --build-prefix | --src-dir | --exec-prefix)
error "missing argument to $1"
;;
--system-dynamic-linker)
USE_SYSTEM_DYNAMIC_LINKER=1
;;
-h | --help)
show_help
exit
;;
*=*)
export $1
;;
*)
error "unknown option -- '$1'"
;;
esac
shift
done
mkdir -p $BUILD_PREFIX
BIN_PREFIX="$BUILD_PREFIX/bin"
mkdir -p $BIN_PREFIX
function check_os() {
os=$OSTYPE
echo "checking host system type... $os"
if [[ $os != "linux-gnu"* ]]; then
error "host system does not match \`linux-gnu\`"
fi
}
check_os
#
# C Compiler checks ($CC)
#
function check_tool() {
return $(command -v "$1" > /dev/null 2>&1)
}
function verify_tool_found() {
echo -n "checking for $1... "
if check_tool $1; then
echo "found."
else
echo "not found."
error "\`$1\` could not be found. Be sure \`$1\` is in your PATH."
fi
}
echo -n "checking for C compiler... "
if [ -z "$CC" ]; then
compilers=("gcc" "clang" "cc")
for compiler in "${compilers[@]}"; do
if check_tool "$compiler"; then
export CC=$(which $compiler)
break
fi
done
if [ -z "$CC" ]; then
echo "not found"
error "no suitable C compiler found."
fi
fi
echo "$CC"
function test_compiler() {
echo -n "checking whether the C compiler works... "
test_file="$BUILD_PREFIX/test.c"
test_exec="$BUILD_PREFIX/test.out"
echo "int main() { return 0; }" > $test_file
if $CC -o $test_exec $test_file; then
echo "yes"
else
echo "no"
error "\`$CC\` was unable to compile a simple test file."
fi
}
test_compiler
#
# LD compiler checks ($LD)
#
echo -n "checking for linker... "
LD="$CC"
echo "$LD"
#
# Default compiler/linker flags
#
CSPC_DEFAULT_STD_PATH="${PREFIX}/share/cspydr/std"
CFLAGS="${CFLAGS} -fPIC -flto -Wall -Wextra -Wno-unused-parameter -DDEFAULT_STD_PATH=\"\\\"${CSPC_DEFAULT_STD_PATH}\\\"\""
LDFLAGS="${LDFLAGS} -lm"
#
# Check for pkg-config
#
echo -n "checking for pkg-config... "
if [ -z $PKG_CONFIG ]; then
if check_tool "pkg-config"; then
export PKG_CONFIG=$(which pkg-config)
else
echo "not found."
error "pkg-config not found. Makre sure \`pkg-config\` is in the PATH."
fi
fi
echo "$PKG_CONFIG"
echo -n "checking pkg-config directory... "
if test -z "$PKG_CONFIG_DIR"; then
PKG_CONFIG_PATHS=$($PKG_CONFIG --variable pc_path pkg-config)
PKG_CONFIG_DIR="${PKG_CONFIG_PATHS##*:}"
fi
echo "$PKG_CONFIG_DIR"
#
# bear (for compile_commands.json)
#
echo -n "checking for bear... "
if check_tool "bear"; then
echo "found."
BEAR=$(which bear)
else
echo "not found."
fi
#
# Git
#
echo -n "checking for git... "
if check_tool "git"; then
echo "found".
GIT=$(which git)
else
echo "not found".
fi
#
# Other tools
#
verify_tool_found "install"
INSTALL=$(which install)
verify_tool_found "find"
FIND=$(which find)
verify_tool_found "mkdir"
MKDIR=$(which mkdir)
verify_tool_found "rm"
RM=$(which rm)
verify_tool_found "sed"
SED=$(which sed)
#
# Other libraries
#
function check_library() {
echo -n "checking for $1... "
if pkg-config --exists "$1"; then
echo "found."
CFLAGS="${CFLAGS} $(pkg-config --cflags "$1")"
LDFLAGS="${LDFLAGS} $(pkg-config --libs "$1")"
else
echo "not found."
error "library $1 not found."
fi
}
check_library "libpkgconf"
check_library "json-c"
#
# Acutest
#
echo -n "checking for acutest... "
if [ ! -d "$SCRIPT_DIR/tests/lib/acutest" ]; then
echo "not found."
if [ ! -z "$GIT" ] && "$GIT" -C "$SCRIPT_DIR" rev-parse --is-inside-work-tree &>/dev/null; then
echo "downloading acutest as submodule..."
$(which git) submodule update --init --recursive
else
error "library acutest not found and couldn't be downloaded using git submodules."
fi
else
echo "found."
fi
#
# Dynamic linker for the compiler
#
if [ $USE_SYSTEM_DYNAMIC_LINKER -eq 1 ]; then
check_tool "patchelf"
CFLAGS="${CFLAGS} -DCSPYDR_DEFAULT_DYNAMIC_LINKER_PATH=\"\\\"$($(which patchelf) --print-interpreter $(which cp))\\\"\""
fi
#
# Generate verion numbers
#
CSPC_VERSION=$(<"$SCRIPT_DIR/version")
IFS='.' read -ra CSPC_VERSION_DIGITS <<< "$CSPC_VERSION"
CSPC_VERSION_X=${CSPC_VERSION_DIGITS[0]}
CSPC_VERSION_Y=${CSPC_VERSION_DIGITS[1]}
CSPC_VERSION_Z=${CSPC_VERSION_DIGITS[2]}
CSPC_GIT_HASH="nogit"
if [ ! -z "$GIT" ] && "$GIT" -C "$SCRIPT_DIR" rev-parse --is-inside-work-tree &>/dev/null; then
CSPC_GIT_HASH=$("$GIT" -C "$SCRIPT_DIR" rev-parse --short HEAD)
fi
#
# Write config file
#
if [ -e "$CONFIG_MK" ]; then
echo -n "removing existing $CONFIG_MK file... "
rm $CONFIG_MK
echo "done."
fi
touch $CONFIG_MK
echo -n "generating $CONFIG_MK file... "
cat > "$CONFIG_MK" <<EOF
# Auto-generated by \`configure\`
CC := $CC
LD := $LD
CFLAGS := $CFLAGS
LDFLAGS := $LDFLAGS
INSTALL := $INSTALL
FIND := $FIND
MKDIR := $MKDIR
RM := $RM
SED := $SED
BEAR := $BEAR
PREFIX := $PREFIX
EXEC_PREFIX := $EXEC_PREFIX
BUILD_PREFIX := $BUILD_PREFIX
BIN_PREFIX := $BIN_PREFIX
PKG_CONFIG_DIR := $PKG_CONFIG_DIR
SRC_DIR := $SRC_DIR
CSPC_DEFAULT_STD_PATH := $CSPC_DEFAULT_STD_PATH
VERSION := $CSPC_VERSION
VERSION_X := $CSPC_VERSION_X
VERSION_Y := $CSPC_VERSION_Y
VERSION_Z := $CSPC_VERSION_Z
GIT_HASH := $CSPC_GIT_HASH
EOF
echo "done."
echo
echo "generated configuration:"
cat $CONFIG_MK
echo
echo "done."