-
Notifications
You must be signed in to change notification settings - Fork 0
/
configure
executable file
·584 lines (509 loc) · 12.7 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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
#!/bin/bash
shopt -s nocasematch
declare OS="$(uname)"
declare CWD="$(pwd)"
declare -i DEBUG=0
declare SED_REGEX_FLAG="-r"
## output dot width
declare -i DOTWIDTH=50
## Library name
declare LIBRARY_NAME="sleepfile"
## Library version
function semver() {
cat package.json | \
grep '"version":' | \
tr -d '"version":' | \
tr -d ',' | \
tr -d '[:space:]' | \
tr '.' '\t' | \
eval $(printf "awk '{print $%d}'" $1) | \
tr -d '[:space:]'
return $?
}
declare -i VERSION_MAJOR=$(semver 1)
declare -i VERSION_MINOR=$(semver 2)
declare -i VERSION_PATCH=$(semver 3)
declare -i VERSION_REVISION=1
## Script name
declare SELF="$(basename $0)"
## Project version
declare PROJECT_VERSION="$(./scripts/version)"
## Required system commands
declare -a COMMAND_DEPENDENCIES=(
ar cc ln rm pwd date
uname mkdir strip make
)
## Optional user commands
declare -a OPTIONAL_COMMAND_DEPENDENCIES=()
## Required system libraries
declare -a LIBRARY_DEPENDENCIES=( )
## Required system frameworks on macos
declare -a LIBRARY_FRAMEWORK_DEPENDENCIES=()
## Optional system libraries
declare -a OPTIONAL_LIBRARY_DEPENDENCIES=()
## Source dependency files
declare -a DEPS=(
deps/nanoresource/*.c
deps/ras/*.c
)
## Source files
declare -a SRC=(
$(ls *.c)
$(ls src/*.c)
)
## Required system headers
declare -a HEADER_DEPENDENCIES=(
'math.h' 'errno.h' 'stdio.h' 'stdlib.h' 'string.h'
)
case $OS in
linux)
HEADER_DEPENDENCIES+=()
LIBRARY_DEPENDENCIES+=('m')
;;
darwin)
HEADER_DEPENDENCIES+=('TargetConditionals.h')
LIBRARY_FRAMEWORK_DEPENDENCIES+=('Foundation')
SED_REGEX_FLAG="-E"
;;
esac
## Flags passed to this script
declare CONFIGURE_FLAGS=""
## Configurable environment variables
declare CC="${CC:-$(which cc)}"
declare PREFIX="${PREFIX:-/usr/local}"
declare INCLUDEDIR="${INCLUDEDIR:-$PREFIX/include}"
declare LIBDIR="${LIBDIR:-$PREFIX/lib}"
declare MANDIR="${MANDIR:-$PREFIX/man}"
declare BUILD="$CWD/build"
## Template variables map with the following stride:
## <key, value, description>
declare -a TEMPLATE_VARS=(
OS "Operating System"
SRC "List of source files"
DEPS "List of external source dependency files"
BUILD "The build directory where all compiled objects and libaries are output to"
PREFIX "Default installation prefix"
CFLAGS "User CFLAGS"
LDFLAGS "User LDFLAGS"
LIBRARY_NAME "Library name"
VERSION_MAJOR "The major version of the library"
VERSION_MINOR "The minor version of the library"
VERSION_PATCH "The patch version of the library"
VERSION_REVISION "The revision version of the library"
)
##
# White space trim helper
function trim {
echo -n "$@" | sed "$SED_REGEX_FLAG" 's/^\s*(\S+(\s+\S+)*)\s*$/\1/'
return $?
}
##
# Appends flag with surrounding spaces
# to the CFLAGS environment variable
function cflag {
CFLAGS+=" $(trim "$@")"
}
##
# Appends flag with surrounding spaces
# to the LDFLAGS environment variable
function ldflag {
LDFLAGS+=" $(trim "$@")"
}
##
# Appends source file to SRC array
function src {
SRC+=($@)
}
## Gets or sets a template var
function var {
let local i=0
local key=$1
local value=$2
for (( i = 0; i < ${#TEMPLATE_VARS[@]}; i += 2 )); do
local name=${TEMPLATE_VARS[$i]}
if [ $name = $key ]; then
if (( 2 == $# )); then
eval "$name=$value"
info "$name = $value"
else
echo ${!name}
fi
return 0
fi
done
return 1
}
##
# Outputs prefixed info to stdout
function info {
printf "%s: %s" $SELF "$@"
echo
return 0
}
##
# Oututs ok and returns a 0 status code
function ok {
info "ok" && true && return $?
}
##
# Outputs a message to stderr
# and exits with status 1 immediately
function fatal {
info "fatal: $@" >&2
exit 1
}
##
# Outputs a warning message to stderr
function warn {
info "warn: $@" >&2
return 0
}
##
# Outputs command help
function usage {
let local i=0
cat <<HELP
usage: $SELF [-hV]
options:
--help Print this message
--debug Compile with debug output enabled
--prefix=PREFIX Install prefix directory (default: ${PREFIX})"
--includedir=DIR Header directory (default: ${INCLUDEDIR})"
--libdir=DIR Library directory (default: ${LIBDIR})"
--mandir=DIR Man page directory (default: ${MANDIR})"
--target=NAME Target library name (default: ${LIBRARY_NAME})"
HELP
return 0
}
##
# Just print yes
function yes {
printf " yes"
return 0
}
##
# Just print no
function no {
printf " no"
return 0
}
##
# Print dots (...)
function dots {
let local count=${1:-20}
local i=0
for (( i = 0; i < count; ++i )); do
printf "."
done
return 0
}
function check_posix_memalign {
local function_name='posix_memalign'
local headers='stdlib.h assert.h'
local program="$(cat <<-SRC
int main() {
void *ptr = 0;
assert(posix_memalign(&ptr, 8, 32));
return 0;
}
SRC
)"
check_c_program "$function_name" "$headers" "$program"
return $?
}
function check_memalign {
local function_name='memalign'
local headers='stdlib.h malloc.h assert.h'
local program="$(cat <<-SRC
int main() {
void *ptr = memalign(8, 32);
assert(ptr);
return 0;
}
SRC
)"
check_c_program "$function_name" "$headers" "$program"
return $?
}
function check_c_program {
let local rc=0
local function_name="$1"
declare -a local headers=($2)
local program="$3"
printf "Checking for $function_name"
dots $(expr $DOTWIDTH - ${#function_name} + 1)
{
for header in "${headers[@]}"; do
echo "#include <$header>"
done
echo $program
} | ${CC} -o /dev/null -xc - > /dev/null 2>&1
if (( $? != 0 )); then
no; rc=1
else
yes; rc=0
fi
echo
return $rc
}
##
# Check for the existence of a library
# with human readable output
function check_library {
let local rc=0
local lib="$1"
if [ -z "$lib" ]; then return 1; fi
printf "Checking for lib$lib " && dots $(expr $DOTWIDTH - ${#lib} - 3)
echo 'int main (void) { return (0); }' | \
${CC} -o /dev/null -l${1} -xc - > /dev/null 2>&1
if (( $? != 0 )); then
no; rc=1
else
yes; rc=0
fi
echo
return $rc
}
##
# Check for the existence of a framework
# with human readable output
function check_framework {
let local rc=0
local framework="$1"
if [ -z "$framework" ]; then return 1; fi
printf "Checking for framework $framework " && dots $(expr $DOTWIDTH - ${#framework} - 10)
echo 'int main (void) { return (0); }' | \
${CC} -o /dev/null -framework $framework -xc - > /dev/null 2>&1
if (( $? != 0 )); then
no; rc=1
else
yes; rc=0
fi
echo
return $rc
}
##
# Check if a dependency command exists with
# human readable output
function check_command {
let local rc=0
local cmd="$1"
if [ -z $cmd ]; then return 1; fi
printf "Checking for $cmd " && dots $(expr $DOTWIDTH - ${#cmd} )
if [ -z $(which $cmd) ]; then
no; rc=1
else
yes; rc=0
fi
echo
return $rc
}
##
# Check for the existence of a header file
# with human readable output
function check_header {
let local rc=0
local header="$1"
if [ -z "$header" ]; then return 1; fi
printf "Checking for $header " && dots $(expr $DOTWIDTH - ${#header})
{
echo "#include <$header>"
echo "int main (void) { return (0); }"
} | ${CC} -o /dev/null -xc - > /dev/null 2>&1
if (( $? != 0 )); then
no; rc=1
else
yes; rc=0
fi
echo
return $rc
}
##
# Generates a output from a named template
function template {
local input="$1"
local output
local buffer
local i=0
if ! [[ "$input" =~ ".in" ]]; then
input+=".in"
fi
if test -f "$input"; then
output="${input/.in/}"
## clean up existing output file
if test -f "$output"; then
rm -f "$output";
fi
info "Generating file \`$output'"
cmd="$(echo "sed -e $(eval echo $( \
for (( i = 0; i < ${#TEMPLATE_VARS[@]}; i += 2 )); do \
local name="${TEMPLATE_VARS[$i]}"; \
local value; eval 'value=${'$name'[@]}'; \
local values="${value[@]}"; \
local escaped="${values//\//\\/}"; \
echo "\-e \'\"s/@$name@/$escaped/g\"\'"; \
done)) "$input" > "$output"")"
## exec
eval $cmd
fi
}
##
# Configures library build files
function configure {
let local i=0
info "$(uname -a)"
info "Configuring lib$LIBRARY_NAME-($PROJECT_VERSION) for $OS"
case "$OS" in
Linux|Unix|GNU|*BSD|Darwin)
;;
Win32) ;;
*) fatal "Unsupported operating system."
esac
## gcc compiler flags
cflag '-std=c99'
cflag '-I deps'
cflag '-I include'
cflag '-Wall'
cflag '-O2'
cflag '-fvisibility=hidden'
cflag '-fPIC'
## we add this to stop the mising headering file error: 'libuv(pthread-barrier.h)'
## see: https://github.com/libuv/libuv/issues/891#issuecomment-222443086
cflag '-D_POSIX_C_SOURCE=200112'
## gcc/ld linker flags
for lib in "${LIBRARY_DEPENDENCIES[@]}"; do
ldflag "-l $lib"
done
## gcc/ld framework (macOS) flags
for framework in "${LIBRARY_FRAMEWORK_DEPENDENCIES[@]}"; do
ldflag "-framework $framework"
done
if (( ${#COMMAND_DEPENDENCIES[@]} > 0 )); then
echo
info "Checking required command dependencies"
for (( i = 0; i < ${#COMMAND_DEPENDENCIES[@]}; i++ )); do
local cmd="${COMMAND_DEPENDENCIES[$i]}"
if ! check_command $cmd; then
fatal "Missing required command $cmd"
fi
done
fi
if (( ${#HEADER_DEPENDENCIES[@]} > 0 )); then
echo
info "Checking required header dependencies"
for (( i = 0; i < ${#HEADER_DEPENDENCIES[@]}; i++ )); do
local header="${HEADER_DEPENDENCIES[$i]}"
if ! check_header $header; then
fatal "Missing required header $header"
fi
done
fi
if (( ${#LIBRARY_DEPENDENCIES[@]} > 0 )); then
echo
info "Checking required library dependencies"
for (( i = 0; i < ${#LIBRARY_DEPENDENCIES[@]}; i++ )); do
local lib="${LIBRARY_DEPENDENCIES[$i]}"
if ! check_library $lib; then
fatal "Missing required library $lib"
fi
done
fi
if (( ${#LIBRARY_FRAMEWORK_DEPENDENCIES[@]} > 0 )); then
echo
info "Checking required framework dependencies"
for (( i = 0; i < ${#LIBRARY_FRAMEWORK_DEPENDENCIES[@]}; i++ )); do
local framework="${LIBRARY_FRAMEWORK_DEPENDENCIES[$i]}"
if ! check_framework $framework; then
fatal "Missing required framework $framework"
fi
done
fi
if (( ${#OPTIONAL_COMMAND_DEPENDENCIES[@]} > 0 )); then
echo
info "Checking optional command dependencies"
for (( i = 0; i < ${#OPTIONAL_COMMAND_DEPENDENCIES[@]}; i++ )); do
local cmd="${OPTIONAL_COMMAND_DEPENDENCIES[$i]}"
check_command $cmd
done
fi
if (( ${#OPTIONAL_LIBRARY_DEPENDENCIES[@]} > 0 )); then
echo
info "Checking optional library dependencies"
for (( i = 0; i < ${#OPTIONAL_LIBRARY_DEPENDENCIES[@]}; i++ )); do
local lib="${OPTIONAL_LIBRARY_DEPENDENCIES[$i]}"
if ! check_library $lib; then
warn "Missing optional library $lib"
fi
done
fi
echo
info "Checking for required C functions"
if check_posix_memalign; then
cflag '-D SLEEPFILE_HAVE_POSIX_MEMALIGN'
else
warn "Missing posix_memalign()"
fi
if check_memalign; then
cflag '-D SLEEPFILE_HAVE_MEMALIGN'
else
warn "Missing memalign()"
fi
info "Checking for deprecated system headers"
if check_header 'malloc.h'; then
cflag '-D SLEEPFILE_HAVE_MALLOC_H'
elif check_header 'malloc/malloc.h'; then
cflag '-D SLEEPFILE_HAVE_MALLOC_MALLOC_H'
fi
echo
info "CFLAGS: $CFLAGS"
info "LDFLAGS: $LDFLAGS"
info "Generating template files"
## TODO(werle) - Check nested *.in with "find . -name *.in" ?
for file in *.in; do
template $file
done
return 0
}
##
# Scripts entry
function main {
local arg value
for arg in $@; do
## parse opt value
case "$arg" in
*=false) value=0 ;;
*=true) value=1 ;;
*=?*) value="$(expr -- "$arg" : '[^=]*=\(.*\)')" ;;
*=) value= ;;
*) value=1 ;;
esac
case $arg in
-h|--help) usage && return 0;;
## Output directories
--includedir=?*) INCLUDEDIR="$value" ;;
--prefix=?*) PREFIX="$value" ;;
--libdir=?*) LIBDIR="$value" ;;
--mandir=?*) MANDIR="$value" ;;
--target=?*) LIBRARY_NAME="$value" ;;
## debug configuration
--debug|--debug=?*) DEBUG=$value ;;
esac
done
CONFIGURE_FLAGS+="--includedir=$INCLUDEDIR "
CONFIGURE_FLAGS+="--prefix=$PREFIX "
CONFIGURE_FLAGS+="--libdir=$LIBDIR "
CONFIGURE_FLAGS+="--mandir=$MANDIR "
CONFIGURE_FLAGS+="--target=$LIBRARY_NAME "
if (( $DEBUG )); then
CONFIGURE_FLAGS+="--debug=true"
cflag '-D SLEEPFILE_DEBUG'
cflag '-g'
else
CONFIGURE_FLAGS+="--debug=false"
fi
info "flags: $CONFIGURE_FLAGS"
configure
return $?
}
## init
(main $@) && exit $?