forked from Water-Melon/Melon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configure
executable file
·535 lines (488 loc) · 18.8 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
#!/bin/bash
#
# Copyright (C) Niklaus F.Schen.
#
#Global Variables
# test system type
sysname=`uname -s`
# paths
realpath=""
nullpath=""
confpath=""
tmpfilepath=""
pidpath=""
logpath=""
melang_script_path=""
melang_script_path=""
melang_dylib_path=""
# wasm flag
wasm=0
# debug
debug=0
# optimization level
olevel='-O3'
# selected source files
select_files='all'
# disabled macros
disabled_macros=""
# llvm
llvm_flag=""
# event
event_flag="-DMLN_SELECT"
# sendfile
sendfile_flag=""
# writev
writev_flag=""
# unix98
unix98_flag=""
# mmap
mmap_flag=""
#Functions
set_melang_default_paths() {
if ! case $sysname in MINGW*) false;; esac; then
melang_script_path=$HOME/lib/melang
melang_dylib_path=$HOME/lib/melang_dynamic
else
melang_script_path=/usr/local/lib/melang
melang_dylib_path=/usr/local/lib/melang_dynamic
fi
}
set_c_compilers() {
if ! case $sysname in MINGW*) false;; esac; then
install_path=$HOME/libmelon
cc="gcc"
tcc="gcc"
else
install_path=`echo "/usr/local/melon"`
cc="cc"
tcc="cc"
fi
echo -e "#include <stdio.h>\nint main(void) {printf(\"1\");return 0;}" > .xcode.c
$tcc -o .xcode .xcode.c 2>/dev/null 1>&2
if [ $? -ne 0 ]; then
tcc="$tcc -isysroot `xcrun --show-sdk-path`"
cc="$cc -isysroot `xcrun --show-sdk-path`"
fi
rm -fr .xcode .xcode.c
}
send_install_request() {
curl http://register.melang.org/ -H "Referer: Melon Installation" 2> /dev/null 1>&2
}
before_get_params() {
send_install_request
set_melang_default_paths
set_c_compilers
}
get_params() {
for param in $@
do
if [ $param == "--help" ]; then
echo -e "\nMelon platform."
echo "Copyright (C) Niklaus F.Schen."
echo "Options:"
echo -e "\t--prefix=INSTALL_PATH"
echo -e "\t--melang-prefix=MELANG_SCRIPT_PATH"
echo -e "\t--melang-dylib-prefix=MELANG_DYLIB_PATH"
echo -e "\t--cc=C compiler"
echo -e "\t--enable-wasm"
echo -e "\t--debug"
echo -e "\t--olevel=O|O1|O2|O3"
echo -e "\t--select=[all|module1,module2,...]"
echo -e "\t--disable-macro=[macro1,macro2,...]"
exit 0
fi
param_prefix=`echo $param|cut -d '=' -f 1`
param_suffix=`echo $param|cut -d '=' -f 2`
if [ $param_prefix == "--prefix" ]; then
install_path=$param_suffix
elif [ $param_prefix == "--melang-prefix" ]; then
melang_script_path=$param_suffix
elif [ $param_prefix == "--melang-dylib-prefix" ]; then
melang_dylib_path=$param_suffix
elif [ $param_prefix == "--cc" ]; then
cc=$param_suffix
elif [ $param_prefix == "--enable-wasm" ]; then
wasm=1
elif [ $param_prefix == "--debug" ]; then
debug=1
elif [ $param_prefix == "--select" ]; then
select_files=$param_suffix
elif [ $param_prefix == "--disable-macro" ]; then
disabled_macros=$param_suffix
elif [ $param_prefix == "--olevel" ]; then
if [ -z $param_suffix ]; then
olevel=''
else
olevel='-'$param_suffix
fi
fi
done
}
update_llvm_flag() {
if [ $wasm -eq 1 ]; then
echo -e "Webassembly\t\t[enable]"
$cc --help|grep LLVM > /dev/null 2>&1
if [ $? -ne 0 ]; then
llvm_flag="--llvm-lto 1"
fi
fi
}
relocate_install_path() {
if [[ "$install_path" == ~* ]]; then
install_path=$HOME/${install_path:1}
fi
if [[ "$melang_script_path" == ~* ]]; then
melang_script_path=$HOME/${melang_script_path:1}
fi
if [[ "$melang_dylib_path" == ~* ]]; then
melang_dylib_path=$HOME/${melang_dylib_path:1}
fi
}
update_debug_flag() {
if [ $debug -ne 0 ]; then
if [ $wasm -eq 1 ]; then
debug='-g -D__DEBUG__'
else
debug='-ggdb -D__DEBUG__'
fi
else
debug=''
fi
}
get_disabled_macros() {
macros=()
old_ifs=$IFS
IFS=','
for m in $disabled_macros
do
macros+=$m"_flag "
done
IFS=$old_ifs
disabled_macros=$macros
}
detect_operating_system_event_support() {
output="event\t\t\t[SELECT]"
if [[ ! "${disabled_macros[@]}" =~ "event_flag" ]]; then
echo "#include<stdio.h>
#include<sys/types.h>
#include<sys/event.h>
#include<sys/time.h>
int main(void){kqueue();return 0;}" > ev_test.c
$cc -o ev_test ev_test.c 2>/dev/null
if [ "$?" == "0" ]; then
event_flag="-DMLN_KQUEUE"
output="event\t\t\t[KQUEUE]"
fi
echo "#include<stdio.h>
#include<sys/epoll.h>
int main(void){epoll_create(10);return 0;}" > ev_test.c
$cc -o ev_test ev_test.c 2>/dev/null
if [ "$?" == "0" ]; then
event_flag="-DMLN_EPOLL"
output="event\t\t\t[EPOLL]"
fi
fi
rm -f ev_test ev_test.c
echo -e $output
}
detect_operating_system_sendfile_support() {
output="sendfile\t\t[NOT support]"
if [[ ! "${disabled_macros[@]}" =~ "sendfile_flag" ]]; then
echo "#include <sys/sendfile.h>
int main(void){sendfile(1,0,0,1);return 0;}" > sendfile_test.c
$cc -o sendfile_test sendfile_test.c 2>/dev/null
if [ "$?" == "0" ]; then
sendfile_flag="-DMLN_SENDFILE"
output="sendfile\t\t[support]"
fi
rm -f sendfile_test sendfile_test.c
fi
echo -e $output
}
detect_operating_system_writev_support() {
output="writev\t\t\t[NOT support]"
if [[ ! "${disabled_macros[@]}" =~ "writev_flag" ]]; then
echo -e "#include <stdio.h>\n#include <sys/uio.h>" > writev_test.c
echo "int main(void){writev(0,NULL,0);return 0;}" >> writev_test.c
$cc -o writev_test writev_test.c 2>/dev/null
if [ "$?" == "0" ]; then
writev_flag="-DMLN_WRITEV"
output="writev\t\t\t[support]"
fi
rm -f writev_test writev_test.c
fi
echo -e $output
}
detect_operating_system_unix98_support() {
output="__USE_UNIX98\t\t[not support]"
if [[ ! "${disabled_macros[@]}" =~ "unix98_flag" ]]; then
echo -e "#ifndef __USE_UNIX98\n#define __USE_UNIX98\n#endif\n#include <pthread.h>\n" > unix98_test.c
echo "int main(void){pthread_setconcurrency(0);return 0;}" >> unix98_test.c
$cc -o unix98_test unix98_test.c -lpthread 2>/dev/null
if [ "$?" == "0" ]; then
output="__USE_UNIX98\t\t[support]"
unix98_flag="-DMLN_USE_UNIX98"
fi
rm -fr unix98_test unix98_test.c
fi
echo -e $output
}
detect_operating_system_mmap_support() {
output="mmap\t\t\t[NOT support]"
if [[ ! "${disabled_macros[@]}" =~ "mmap_flag" ]]; then
echo -e "#include <stdio.h>\n#include <sys/mman.h>" >> mmap_test.c
echo "int main(void){mmap(NULL, 0, PROT_READ, MAP_PRIVATE, 0, 0);return 0;}" >> mmap_test.c
$cc -o mmap_test mmap_test.c 2>/dev/null
if [ "$?" == "0" ]; then
mmap_flag="-DMLN_MMAP"
output="mmap\t\t\t[support]"
fi
rm -f mmap_test mmap_test.c
fi
echo -e $output
}
detect_operating_system_support() {
if [ $wasm -eq 0 ]; then
get_disabled_macros
detect_operating_system_event_support
detect_operating_system_sendfile_support
detect_operating_system_writev_support
detect_operating_system_unix98_support
detect_operating_system_mmap_support
fi
}
update_paths() {
echo -e "Installation Path \t[$install_path]"
echo -e "Melang script Path \t[$melang_script_path]"
echo -e "Melang dylib Path \t[$melang_dylib_path]"
echo -e "#include <stdio.h>\nint main(int argc, char *argv[]) {printf(\"%s\", argv[1]);return 0;}" > .path_generator.c
$tcc -o path_generator .path_generator.c
realpath=`./path_generator $install_path`
nullpath=`./path_generator /dev/null`
confpath=`./path_generator $realpath/conf/melon.conf`
tmpfilepath=`./path_generator $realpath/tmp`
pidpath=`./path_generator $realpath/logs/melon.pid`
logpath=`./path_generator $realpath/logs/melon.log`
melang_script_path=`./path_generator $melang_script_path`
melang_script_path=`./path_generator $melang_script_path`
melang_dylib_path=`./path_generator $melang_dylib_path`
rm -f path_generator .path_generator.c
}
generate_path_module() {
echo -e "\n/*\n * Copyright (C) Niklaus F.Schen.\n */" > include/mln_path.h
echo -e "#ifndef __MLN_PATH_H\n#define __MLN_PATH_H" >> include/mln_path.h
echo -e "\ntypedef enum {\n m_p_install,\n m_p_conf,\n m_p_tmpfile,\n m_p_pid,\n m_p_log,\n m_p_null,\n m_p_melang_lib,\n m_p_melang_dylib\n} mln_path_type_t;" >> include/mln_path.h
echo -e "\ntypedef char *(*mln_path_hook_t)(void);" >> include/mln_path.h
echo -e "\nextern char *mln_path(void);" >> include/mln_path.h
echo -e "\nextern char *mln_path_null(void);" >> include/mln_path.h
echo -e "\nextern char *mln_path_melang_lib(void);" >> include/mln_path.h
echo -e "\nextern char *mln_path_melang_dylib(void);" >> include/mln_path.h
echo -e "\nextern char *mln_path_conf(void);" >> include/mln_path.h
echo -e "\nextern char *mln_path_tmpfile(void);" >> include/mln_path.h
echo -e "\nextern char *mln_path_pid(void);" >> include/mln_path.h
echo -e "\nextern char *mln_path_log(void);" >> include/mln_path.h
echo -e "\nextern void mln_path_hook_set(mln_path_type_t type, mln_path_hook_t hook);" >> include/mln_path.h
echo -e "\n#endif\n" >> include/mln_path.h
echo -e "\n/*\n * Copyright (C) Niklaus F.Schen.\n */" > src/mln_path.c
echo "#include <stdio.h>" >> src/mln_path.c
echo "#include \"mln_path.h\"" >> src/mln_path.c
echo -e "\nstatic char install_path[] = \"$realpath\";" >> src/mln_path.c
echo -e "\nstatic char conf_path[] = \"$confpath\";" >> src/mln_path.c
echo -e "\nstatic char tmpfile_path[] = \"$tmpfilepath\";" >> src/mln_path.c
echo -e "\nstatic char pid_path[] = \"$pidpath\";" >> src/mln_path.c
echo -e "\nstatic char log_path[] = \"$logpath\";" >> src/mln_path.c
echo -e "\nstatic char null_path[] = \"$nullpath\";" >> src/mln_path.c
echo -e "\nstatic char melang_lib_path[] = \"$melang_script_path\";" >> src/mln_path.c
echo -e "\nstatic char melang_dylib_path[] = \"$melang_dylib_path\";" >> src/mln_path.c
echo -e "\nstatic mln_path_hook_t _install_path = NULL;" >> src/mln_path.c
echo -e "\nstatic mln_path_hook_t _null_path = NULL;" >> src/mln_path.c
echo -e "\nstatic mln_path_hook_t _melang_lib_path = NULL;" >> src/mln_path.c
echo -e "\nstatic mln_path_hook_t _melang_dylib_path = NULL;" >> src/mln_path.c
echo -e "\nstatic mln_path_hook_t _conf_path = NULL;" >> src/mln_path.c
echo -e "\nstatic mln_path_hook_t _tmpfile_path = NULL;" >> src/mln_path.c
echo -e "\nstatic mln_path_hook_t _pid_path = NULL;" >> src/mln_path.c
echo -e "\nstatic mln_path_hook_t _log_path = NULL;" >> src/mln_path.c
echo -e "\nvoid mln_path_hook_set(mln_path_type_t type, mln_path_hook_t hook)\n{" >> src/mln_path.c
echo -e " switch (type) {\n" >> src/mln_path.c
echo -e " case m_p_install:\n _install_path = hook;\n break;" >> src/mln_path.c
echo -e " case m_p_conf:\n _conf_path = hook;\n break;" >> src/mln_path.c
echo -e " case m_p_tmpfile:\n _tmpfile_path = hook;\n break;" >> src/mln_path.c
echo -e " case m_p_pid:\n _pid_path = hook;\n break;" >> src/mln_path.c
echo -e " case m_p_log:\n _log_path = hook;\n break;" >> src/mln_path.c
echo -e " case m_p_null:\n _null_path = hook;\n break;" >> src/mln_path.c
echo -e " case m_p_melang_lib:\n _melang_lib_path = hook;\n break;" >> src/mln_path.c
echo -e " case m_p_melang_dylib:\n _melang_dylib_path = hook;\n break;" >> src/mln_path.c
echo -e " default:\n break;" >> src/mln_path.c
echo -e " }" >> src/mln_path.c
echo -e "}" >> src/mln_path.c
echo -e "\nchar *mln_path(void)\n{" >> src/mln_path.c
echo " return _install_path == NULL? install_path: _install_path();" >> src/mln_path.c
echo "}" >> src/mln_path.c
echo -e "\nchar *mln_path_conf(void)\n{" >> src/mln_path.c
echo " return _conf_path == NULL? conf_path: _conf_path();" >> src/mln_path.c
echo "}" >> src/mln_path.c
echo -e "\nchar *mln_path_tmpfile(void)\n{" >> src/mln_path.c
echo " return _tmpfile_path == NULL? tmpfile_path: _tmpfile_path();" >> src/mln_path.c
echo "}" >> src/mln_path.c
echo -e "\nchar *mln_path_pid(void)\n{" >> src/mln_path.c
echo " return _pid_path == NULL? pid_path: _pid_path();" >> src/mln_path.c
echo "}" >> src/mln_path.c
echo -e "\nchar *mln_path_log(void)\n{" >> src/mln_path.c
echo " return _log_path == NULL? log_path: _log_path();" >> src/mln_path.c
echo "}" >> src/mln_path.c
echo -e "\nchar *mln_path_null(void)\n{" >> src/mln_path.c
echo " return _null_path == NULL? null_path: _null_path();" >> src/mln_path.c
echo "}" >> src/mln_path.c
echo -e "\nchar *mln_path_melang_lib(void)\n{" >> src/mln_path.c
echo " return _melang_lib_path == NULL? melang_lib_path: _melang_lib_path();" >> src/mln_path.c
echo "}" >> src/mln_path.c
echo -e "\nchar *mln_path_melang_dylib(void)\n{" >> src/mln_path.c
echo " return _melang_dylib_path == NULL? melang_dylib_path: _melang_dylib_path();" >> src/mln_path.c
echo "}" >> src/mln_path.c
}
# build makefile content
get_source_files() {
files=()
if [ $1 = 'all' ]; then
for f in `find src -name "*.c"`
do
files+=$f" "
done
else
old_ifs=$IFS
IFS=','
for f in $select_files
do
IFS=$old_ifs
fname='mln_'$f'.c'
fname=`find src -name $fname`
if [ -z $fname ]; then
echo "[$f] not found"
exit 1
fi
files+=$fname" "
for header in `cpp -MM -MG $fname 2> /dev/null`
do
suffix=`echo $header | cut -d '.' -f 2`
if [ $suffix == 'h' ]; then
source_file=`echo $header | cut -d '.' -f 1`'.c'
source_file=`find src -name $source_file`
if [ ! -z $source_file ]; then
if [[ ! "${files[@]}" =~ $source_file ]]; then
files+=$source_file" "
fi
fi
fi
done
done
IFS=$old_ifs
fi
select_files=$files
}
generate_Makefile() {
echo "# " > Makefile
echo "# Copyright (C) Niklaus F.Schen." >> Makefile
echo "# " >> Makefile
if [ $wasm -eq 1 ]; then
echo -e "CC\t\t= emcc" >> Makefile
else
echo -e "CC\t\t= $cc" >> Makefile
fi
if [ $wasm -eq 1 ]; then
echo -e "FLAGS\t\t= -Iinclude -c $debug $olevel $llvm_flag -s -mmutable-globals -mnontrapping-fptoint -msign-ext -Wemcc" >> Makefile
else
echo -e "FLAGS\t\t= -Iinclude -c -Wall $debug -Werror $olevel -fPIC $event_flag $sendfile_flag $writev_flag $unix98_flag $mmap_flag" >> Makefile
fi
if ! case $sysname in MINGW*) false;; esac; then
if [ $wasm -eq 0 ]; then
echo -e "MELONSO\t\t= libmelon.dll" >> Makefile
fi
echo -e "MELONA\t\t= libmelon.lib" >> Makefile
else
if [ $wasm -eq 0 ]; then
echo -e "MELONSO\t\t= libmelon.so" >> Makefile
fi
echo -e "MELONA\t\t= libmelon.a" >> Makefile
fi
echo -e "OBJS\t\t= \\" >> Makefile
for fname in ${select_files[@]}
do
fname=`basename $fname`
objname=`echo $fname | cut -d '.' -f 1`".o"
echo -n " objs/"$objname >> Makefile
echo " \\" >> Makefile
done
echo "" >> Makefile
echo -e ".PHONY :\tcompile install clean" >> Makefile
if [ $wasm -eq 1 ]; then
echo "compile: MKDIR \$(OBJS) \$(MELONA)" >> Makefile
else
echo "compile: MKDIR \$(OBJS) \$(MELONSO) \$(MELONA)" >> Makefile
fi
echo "clean:" >> Makefile
echo -e "\trm -fr objs lib Makefile" >> Makefile
echo "MKDIR :" >> Makefile
echo -e "\ttest -d objs || mkdir objs" >> Makefile
echo -e "\ttest -d lib || mkdir lib" >> Makefile
echo "\$(MELONA) : \$(OBJS)" >> Makefile
if [ $wasm -eq 1 ]; then
echo -e "\temar rcs lib/\$(MELONA) \$(OBJS)" >> Makefile
else
echo -e "\tar -r lib/\$(MELONA) \$(OBJS)" >> Makefile
fi
if [ $wasm -eq 0 ]; then
echo "\$(MELONSO) : \$(OBJS)" >> Makefile
if [ $sysname = 'Linux' ]; then
echo -e "\t\$(CC) -o lib/\$(MELONSO) \$(OBJS) $debug -Wall -lpthread -Llib/ -ldl -shared -fPIC" >> Makefile
elif ! case $sysname in MINGW*) false;; esac; then
echo -e "\t\$(CC) -o lib/\$(MELONSO) \$(OBJS) $debug -Wall -lpthread -lWs2_32 -Llib/ -shared -fPIC" >> Makefile
else
echo -e "\t\$(CC) -o lib/\$(MELONSO) \$(OBJS) $debug -Wall -lpthread -Llib/ -lc -shared -fPIC" >> Makefile
fi
fi
echo "install:" >> Makefile
echo -e "\ttest -d $melang_script_path || mkdir -p $melang_script_path" >> Makefile
echo -e "\ttest -d $install_path || mkdir -p $install_path" >> Makefile
echo -e "\tcp -fr lib $install_path" >> Makefile
echo -e "\tcp -fr include $install_path" >> Makefile
echo -e "\ttest -d $install_path/conf || cp -fr conf $install_path" >> Makefile
echo -e "\ttest -d $melang_script_path/trace || cp -fr trace $melang_script_path" >> Makefile
for fname in ${select_files[@]}
do
objname=`basename $fname | cut -d '.' -f 1`".o"
echo -n "objs/$objname :" >> Makefile
for header in `cpp -MM -MG $fname 2> /dev/null`
do
suffix=`echo $header | cut -d '.' -f 2`
if [ $suffix = 'c' ]; then
echo -n $header >> Makefile
echo -n " " >> Makefile
continue
fi
if [ $suffix != 'h' ]; then
continue
fi
test -e include/$header && echo -n "include/$header " >> Makefile
done
echo "" >> Makefile
echo -e "\t\$(CC) \$(FLAGS) -o \$@ $fname" >> Makefile
done
}
generate_conf_file() {
sed -e "s#{{ROOT}}#${realpath}#g" conf/melon.conf.template > conf/melon.conf
}
after_get_params() {
update_llvm_flag
relocate_install_path
update_debug_flag
detect_operating_system_support
update_paths
generate_path_module
get_source_files $select_files
generate_Makefile
generate_conf_file
}
before_get_params
get_params $@
after_get_params
echo "Configure done!"