-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfigure
executable file
·503 lines (442 loc) · 13.6 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
#!/bin/sh
#
# Copyright (2019) Petr Ospalý <[email protected]>
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# globals
MAKEFILE=Makefile.custom
# control variables
CMD=$(basename "$0")
CUSTOM_CC= # leave empty
#
# functions
#
help()
{
cat <<EOF
NAME
${CMD} - configure script for baka tool
DESCRIPTION
This is nowhere close to the autotools configure script - baka has only
few requirements (C compiler and ncurses library) so this script is very
simple and naive.
If for some reasons the checking requirements part keeps failing but you
are certain that build will proceed fine and gcc/clang will find ncurses
where this script failed, then you can skip this part of the script with
the '--skip-checks'.
Optionally you can set the compiler before the checks are run with the
'--set-compiler' option. By doing this you will avoid the problem when
you have cc/gcc/clang compatible compiler but it is under unexpected
path or name. Setting up the PATH variable will also help. The CC
variable is also taken into account.
Basically this script only setup few variables for later use. It creates
${MAKEFILE} which is then used during the build and also by install
script.
It is not mandatory to run configure script at all if you are happy with
the defaults. The script will either way run interactively and ask you
few questions.
USAGE
${CMD} -h|--help
This help
${CMD} [--skip-checks] [--force-overwrite]
[--set-compiler <compiler> | --skip-compiler]
[--set-cflags <cflags> | --skip-cflags]
[--set-libs <libs> | --skip-libs]
[--set-includes <includes> | --skip-includes]
[--set-installdir <installation> | --skip-installdir]
[--set-configfile <config file> | --skip-configfile]
[--set-scriptfile <script file> | --skip-scriptfile]
It will run the configure script
--skip-checks: no avaibility check for compiler or ncurses
--force-overwrite: without asking rewrite the old ${MAKEFILE}
--set-compiler: set this compiler as default
--set-cflags: set these as cflags
--set-libs: set these as linked libraries
--set-includes: set these as header files locations
--set-installdir: set installation directory for baka tool
--set-configfile: set the path for the baka's config file
--set-scriptfile: set the path for the baka script
--skip-*: don't set variable and don't ask about it
(explicit use of defaults from Makefile.config)
If you wish to run this script completely uninteractively (in
scripts for example) then either provide value with '--set-*' option
or explicitly skip the questioning with the relevant '--skip-*' arg.
EOF
}
check_compiler()
{
# custom C compiler
if [ -n "$CC" ] ; then
if ! which "$CC" >/dev/null 2>&1 ; then
echo "ERROR: '${CC}' does not exist (maybe check the PATH)" 1>&2
echo " PATH=${PATH}" 1>&2
return 1
else
CUSTOM_CC="$CC"
return 0
fi
fi
# from Makefile.config
_cc=$(sed -n 's#[[:space:]]*CC[[:space:]]*=\(.*\)#\1#p' \
Makefile.config)
CUSTOM_CC=$(eval "echo ${_cc}")
if [ -n "$CUSTOM_CC" ] && which "$CUSTOM_CC" >/dev/null 2>&1 ; then
echo "INFO: '${CUSTOM_CC}' from Makefile.config is used"
return 0
else
CUSTOM_CC=
fi
# we guess one
for i in clang gcc cc ; do
if which ${i} >/dev/null 2>&1 ; then
CUSTOM_CC=${i}
echo "INFO: '${CUSTOM_CC}' was found"
return 0
fi
done
if [ -z "$CUSTOM_CC" ] ; then
echo "ERROR: No C compiler found" 1>&2
return 1
fi
# this should not happen
return 1
}
check_curses()
{
echo
set -x
{
cat <<EOF
#include <ncurses.h>
int main(void)
{
initscr();
box(stdscr, '*', '*');
addstr(" CURSES COMPILER TEST ");
refresh();
getch();
endwin();
return 0;
}
EOF
} | $CUSTOM_CC -x c - -std=c99 -lncurses -o /dev/null
set +x
echo
}
# arg: <prompt> [<reply to incorrect value>]
is_yes()
{
_prompt="$1"
_reply="${2:-Incorrect reply! Type either Y/yes or N/no...}"
_answer=
while echo ; do
printf "${_prompt} (Yes/No): "
read -r _answer
case "$_answer" in
Y|y|Yes|yes|YES)
return 0
;;
N|n|No|no|NO)
return 1
;;
*)
printf "${_reply}\n"
;;
esac
done
}
# arg: <prompt> [<default value>]
ask_value()
{
_prompt="$1"
_default="${2}"
_answer=
printf "${_prompt} (${_default:-default from Makefile.config}): "
read -r _answer
case "$_answer" in
'')
_VALUE="$_default"
;;
*)
_VALUE="$_answer"
;;
esac
echo
}
#
# main
#
# parse arguments
state=nil
arg_skipchecks=
arg_forceoverwrite=
arg_compiler=
arg_compiler_skip=
arg_cflags=
arg_cflags_skip=
arg_libs=
arg_libs_skip=
arg_includes=
arg_includes_skip=
arg_installdir=
arg_installdir_skip=
arg_configfile=
arg_configfile_skip=
arg_scriptfile=
arg_scriptfile_skip=
while [ -n "$1" ] ; do
case $state in
nil)
case "$1" in
-h|--help)
help
exit 0
;;
--skip-checks)
arg_skipchecks=yes
;;
--force-overwrite)
arg_forceoverwrite=yes
;;
--skip-compiler)
if [ -n "$arg_compiler" ] ; then
echo "ERROR: Argument mismatch: ${1}" 1>&2
exit 1
fi
arg_compiler_skip=yes
;;
--skip-cflags)
if [ -n "$arg_cflags" ] ; then
echo "ERROR: Argument mismatch: ${1}" 1>&2
exit 1
fi
arg_cflags_skip=yes
;;
--skip-libs)
if [ -n "$arg_libs" ] ; then
echo "ERROR: Argument mismatch: ${1}" 1>&2
exit 1
fi
arg_libs_skip=yes
;;
--skip-includes)
if [ -n "$arg_includes" ] ; then
echo "ERROR: Argument mismatch: ${1}" 1>&2
exit 1
fi
arg_includes_skip=yes
;;
--skip-installdir)
if [ -n "$arg_installdir" ] ; then
echo "ERROR: Argument mismatch: ${1}" 1>&2
exit 1
fi
arg_installdir_skip=yes
;;
--skip-configfile)
if [ -n "$arg_configfile" ] ; then
echo "ERROR: Argument mismatch: ${1}" 1>&2
exit 1
fi
arg_configfile_skip=yes
;;
--skip-scriptfile)
if [ -n "$arg_scriptfile" ] ; then
echo "ERROR: Argument mismatch: ${1}" 1>&2
exit 1
fi
arg_scriptfile_skip=yes
;;
--set-compiler)
if [ -n "$arg_compiler_skip" ] ; then
echo "ERROR: Argument mismatch: ${1}" 1>&2
exit 1
fi
state=compiler
;;
--set-cflags)
if [ -n "$arg_cflags_skip" ] ; then
echo "ERROR: Argument mismatch: ${1}" 1>&2
exit 1
fi
state=cflags
;;
--set-libs)
if [ -n "$arg_libs_skip" ] ; then
echo "ERROR: Argument mismatch: ${1}" 1>&2
exit 1
fi
state=libs
;;
--set-includes)
if [ -n "$arg_includes_skip" ] ; then
echo "ERROR: Argument mismatch: ${1}" 1>&2
exit 1
fi
state=includes
;;
--set-installdir)
if [ -n "$arg_installdir_skip" ] ; then
echo "ERROR: Argument mismatch: ${1}" 1>&2
exit 1
fi
state=installdir
;;
--set-configfile)
if [ -n "$arg_configfile_skip" ] ; then
echo "ERROR: Argument mismatch: ${1}" 1>&2
exit 1
fi
state=configfile
;;
--set-scriptfile)
if [ -n "$arg_scriptfile_skip" ] ; then
echo "ERROR: Argument mismatch: ${1}" 1>&2
exit 1
fi
state=scriptfile
;;
*)
echo "ERROR: Unknown argument: $1" 1>&2
exit 1
;;
esac
;;
compiler)
arg_compiler="$1"
state=nil
;;
cflags)
arg_cflags="$1"
state=nil
;;
libs)
arg_libs="$1"
state=nil
;;
includes)
arg_includes="$1"
state=nil
;;
installdir)
arg_installdir="$1"
state=nil
;;
configfile)
arg_configfile="$1"
state=nil
;;
scriptfile)
arg_scriptfile="$1"
state=nil
;;
esac
shift
done
if ! [ "$state" = nil ] ; then
echo "ERROR: Incomplete argument list" 1>&2
exit 1
fi
if [ -n "$arg_compiler" ] ; then
CC="$arg_compiler"
fi
# run checks
if [ -z "$arg_skipchecks" ] ; then
if ! check_compiler ; then
echo "ERROR: We cannot continue without a proper C compiler" 1>&2
exit 1
fi
if ! check_curses ; then
echo "$ERROR: We could not compile test source file" 1>&2
echo " Probably missing (n)curses library or the header file." 1>&2
echo " You can try to symlink curses to ncurses or vice versa." 1>&2
echo " Also maybe incompatible compiler options..." 1>&2
exit 1
else
echo "INFO: OK! Test source code successfully compiled!"
echo " We have (n)curses library and header installed."
fi
fi
# run questionnaire
cat <<EOF
******************************************************************************
This is the configure part of the script where the '${MAKEFILE}' will be
generated.
If you did not set or skipped all variables via parameters then couple of
questions will follow, each should have some default so if you are happy with
those then just keep hitting enter, otherwise set the proper values.
When you are done, you can run:
% make install
Or read the README.md for more guidance.
******************************************************************************
EOF
if [ -f "${MAKEFILE}" ] && [ -z "$arg_forceoverwrite" ] ; then
echo "This will rewrite the current '${MAKEFILE}' - do you wish to continue?"
if ! is_yes "Continue with configuration" ; then
echo "CONFIGURE: ABORTED"
exit 0
fi
fi
# set compiler (again)
if [ -n "$arg_compiler" ] ; then
CC="$arg_compiler"
elif [ -z "$arg_compiler_skip" ] ; then
ask_value "C compiler" "$CC"
CC="$_VALUE"
fi
# set compiler flags
if [ -n "$arg_cflags" ] ; then
CFLAGS="$arg_cflags"
elif [ -z "$arg_cflags_skip" ] ; then
ask_value "C compiler flags"
CFLAGS="$_VALUE"
fi
# set compiler flag for (n)curses library
if [ -n "$arg_libs" ] ; then
LIBS="$arg_libs"
elif [ -z "$arg_libs_skip" ] ; then
ask_value "Compiler flag(s) for (n)curses library and others"
LIBS="$_VALUE"
fi
# set compiler flag(s) for arbitrary header location(s)
if [ -n "$arg_includes" ] ; then
INCLUDES="$arg_includes"
elif [ -z "$arg_includes_skip" ] ; then
ask_value "Compiler flag(s) for arbitrary header location(s)"
INCLUDES="$_VALUE"
fi
# set baka paths
if [ -n "$arg_installdir" ] ; then
BAKA_HOME="$arg_installdir"
elif [ -z "$arg_installdir_skip" ] ; then
ask_value "Baka installation directory"
BAKA_HOME="$_VALUE"
fi
if [ -n "$arg_configfile" ] ; then
BAKA_CONFIG="$arg_configfile"
elif [ -z "$arg_configfile_skip" ] ; then
ask_value "Baka config file"
BAKA_CONFIG="$_VALUE"
fi
if [ -n "$arg_scriptfile" ] ; then
BAKA_SCRIPT="$arg_scriptfile"
elif [ -z "$arg_scriptfile_skip" ] ; then
ask_value "Baka execution script"
BAKA_SCRIPT="$_VALUE"
fi
# create customized makefile
for i in CC CFLAGS LIBS INCLUDES BAKA_HOME BAKA_CONFIG BAKA_SCRIPT ; do
ivalue=$(eval "echo \"\$${i}\"")
if [ -n "$ivalue" ] ; then
echo "${i} = ${ivalue}"
fi
done > "$MAKEFILE"
# we do not need empty file...
if ! [ -s "$MAKEFILE" ] ; then
rm -f "$MAKEFILE"
fi
echo "CONFIGURE: DONE"
exit 0