forked from ideosoft/astra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
configure.sh
executable file
·756 lines (624 loc) · 14.7 KB
/
configure.sh
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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
#!/bin/sh
usage()
{
cat <<EOF
Usage: $0 [OPTIONS]
--help
--bin=PATH - path to install binary file.
default value is /usr/bin/astra
--with-modules=PATH[:PATH] - list of modules (by default: *)
* - include all modules from ./modules dir.
For example, to append custom module, use:
--with-modules=*:path/to/custom/module
--with-libdvbcsa - build with libdvbcsa
--with-igmp-emulation - build with igmp emulated multicast renew
--cc=GCC - custom C compiler (cross-compile)
--static - build static binary
--arch=ARCH - CPU architecture type (by default: native)
--debug - build version for debug
--cflags="..." - custom compiler flags
--ldflags="..." - custom linker flags
EOF
exit 0
}
SRCDIR=`dirname $0`
MAKEFILE="Makefile"
CONFFILE="config.h"
APP="astra"
APP_C="gcc"
APP_STRIP="strip"
ARG_CC=0
ARG_BPATH="/usr/bin/$APP"
ARG_MODULES="*"
ARG_BUILD_STATIC=0
ARG_ARCH="native"
ARG_CFLAGS=""
ARG_LDFLAGS=""
ARG_LIBDVBCSA=0
ARG_IGMP_EMULATION=0
ARG_DEBUG=0
set_cc()
{
ARG_CC=1
APP_C="$1"
APP_STRIP=`echo $1 | sed 's/gcc$/strip/'`
}
while [ $# -ne 0 ] ; do
OPT="$1"
shift
case "$OPT" in
"--help")
usage
;;
"--bin="*)
ARG_BPATH=`echo $OPT | sed -e 's/^[a-z-]*=//'`
;;
"--with-modules="*)
ARG_MODULES=`echo $OPT | sed -e 's/^[a-z-]*=//'`
;;
"--with-libdvbcsa")
ARG_LIBDVBCSA=1
;;
"--with-igmp-emulation")
ARG_IGMP_EMULATION=1
;;
"--cc="*)
set_cc `echo $OPT | sed 's/^--cc=//'`
;;
"--static")
ARG_BUILD_STATIC=1
;;
"--build-static")
ARG_BUILD_STATIC=1
;;
"--arch="*)
ARG_ARCH=`echo $OPT | sed -e 's/^[a-z-]*=//'`
;;
"--cflags="*)
ARG_CFLAGS=`echo $OPT | sed -e 's/^[a-z-]*=//'`
;;
"--ldflags="*)
ARG_LDFLAGS=`echo $OPT | sed -e 's/^[a-z-]*=//'`
;;
"--debug")
ARG_DEBUG=1
;;
"CFLAGS="*)
ARG_CFLAGS=`echo $OPT | sed -e 's/^[A-Z]*=//'`
;;
"LDFLAGS="*)
ARG_LDFLAGS=`echo $OPT | sed -e 's/^[A-Z]*=//'`
;;
*)
echo "Unknown option: $OPT"
echo "For more information see: $0 --help"
exit 1
;;
esac
done
if ! which $APP_C >/dev/null ; then
echo "C Compiler is not found :$APP_C"
exit 1
fi
if test -f $MAKEFILE ; then
echo "Cleaning previous build..." >&2
make clean
echo >&2
fi
CFLAGS_DEBUG="-O2 -fomit-frame-pointer"
if [ $ARG_DEBUG -ne 0 ] ; then
CFLAGS_DEBUG="-g -O0"
APP_STRIP=":"
fi
CFLAGS="$CFLAGS_DEBUG -I$SRCDIR -Wall -Wextra -Wstrict-prototypes -pedantic -fno-builtin -std=iso9899:1999 -D_GNU_SOURCE"
if [ -n "$ARG_CFLAGS" ] ; then
CFLAGS="$CFLAGS $ARG_CFLAGS"
fi
MACHINE=`$APP_C -dumpmachine`
ARCH=`echo $MACHINE | sed "s|-.*\$||"`
echo $ARCH | grep -q "i[3-6]86\|x86_64"
ISx86=$?
cpucheck_c()
{
cat <<EOF
#include <stdio.h>
int main()
{
#if defined(__i386__) || defined(__x86_64__)
unsigned int eax, ebx, ecx, edx;
__asm__ __volatile__ ( "cpuid"
: "=a" (eax)
, "=b" (ebx)
, "=c" (ecx)
, "=d" (edx)
: "a" (1));
if(ecx & (0x00080000 /* 4.1 */ | 0x00100000 /* 4.2 */ )) printf("-msse -msse2 -msse4");
else if(ecx & 0x00000001) printf("-msse -msse2");
else if(edx & 0x04000000) printf("-msse -msse2");
else if(edx & 0x02000000) printf("-msse");
else if(edx & 0x00800000) printf("-mmmx");
#endif
return 0;
}
EOF
}
cpucheck()
{
CPUCHECK=".cpucheck"
cpucheck_c | $APP_C -Werror -O2 -fno-pic -o $CPUCHECK -x c - >/dev/null 2>&1
if [ $? -eq 0 ] ; then
./$CPUCHECK
rm $CPUCHECK
fi
}
if [ $ARG_CC -eq 0 ]; then
CPUFLAGS=`cpucheck`
if [ -n "$CPUFLAGS" ] ; then
$APP_C $CFLAGS $CPUFLAGS -E -x c /dev/null >/dev/null 2>&1
if [ $? -eq 0 ] ; then
CFLAGS="$CFLAGS $CPUFLAGS"
fi
fi
elif [ $ISx86 -eq 0 ]; then
$APP_C $CFLAGS -msse -E -x c /dev/null >/dev/null 2>&1
if [ $? -eq 0 ] ; then
CFLAGS="$CFLAGS -msse"
$APP_C $CFLAGS -msse2 -E -x c /dev/null >/dev/null 2>&1
if [ $? -eq 0 ] ; then
CFLAGS="$CFLAGS -msse2"
$APP_C $CFLAGS -msse4 -E -x c /dev/null >/dev/null 2>&1
if [ $? -eq 0 ] ; then
CFLAGS="$CFLAGS -msse4"
fi
fi
fi
fi
$APP_C $CFLAGS -march=$ARG_ARCH -E -x c /dev/null >/dev/null 2>&1
if [ $? -eq 0 ] ; then
CFLAGS="$CFLAGS -march=$ARG_ARCH"
else
echo "Error: gcc does not support -march=$ARG_ARCH" >&2
fi
case "$MACHINE" in
*"android"*)
OS="android"
CFLAGS="$CFLAGS -DWITH_EPOLL=1"
LDFLAGS="-ldl -lm"
;;
*"linux"*)
OS="linux"
CFLAGS="$CFLAGS -pthread -DWITH_EPOLL=1"
if $APP_C $CFLAGS -dM -E -xc /dev/null | grep -q "__i386__" ; then
CFLAGS="$CFLAGS -D_FILE_OFFSET_BITS=64"
fi
LDFLAGS="-ldl -lm -lpthread"
;;
*"freebsd"*)
OS="freebsd"
CFLAGS="$CFLAGS -pthread -DWITH_KQUEUE=1"
LDFLAGS="-lm -lpthread"
;;
*"darwin"*)
OS="darwin"
CFLAGS="$CFLAGS -pthread -Wno-deprecated-declarations -DWITH_KQUEUE=1"
LDFLAGS=""
;;
*"mingw"*)
OS="mingw"
CFLAGS="$CFLAGS -DWITH_SELECT=1"
APP="$APP.exe"
WS32=`$APP_C -print-file-name=libws2_32.a`
LDFLAGS="$WS32"
;;
*)
echo "Unknown machine type \"$MACHINE\""
exit 1
;;
esac
if [ $ARG_BUILD_STATIC -eq 1 ] ; then
LDFLAGS="$LDFLAGS -static"
fi
if [ -n "$ARG_LDFLAGS" ] ; then
LDFLAGS="$LDFLAGS $ARG_LDFLAGS"
fi
# libdvbcsa
LIBDVBCSA=0
libdvbcsa_test_c()
{
cat <<EOF
#include <stdio.h>
#include <dvbcsa/dvbcsa.h>
int main(void) {
struct dvbcsa_key_s *key = dvbcsa_key_alloc();
dvbcsa_key_free(key);
return 0;
}
EOF
}
check_libdvbcsa()
{
libdvbcsa_test_c | $APP_C -Werror $1 -c -o .link-test.o -x c - >/dev/null 2>&1
if [ $? -eq 0 ] ; then
$APP_C .link-test.o -o .link-test $2 >/dev/null 2>&1
if [ $? -eq 0 ] ; then
rm -f .link-test.o .link-test
return 0
else
rm -f .link-test.o
return 1
fi
else
return 1
fi
}
build_libdvbcsa()
{
$APP_C -dM -E -x c /dev/null | grep -q "__x86_64__"
if [ $? -eq 0 ] ; then
echo "Build libdvbcsa with UINT64"
$SRCDIR/contrib/libdvbcsa.sh UINT64 $APP_C
return $?
else
echo "Build libdvbcsa with UINT32"
$SRCDIR/contrib/libdvbcsa.sh UINT32 $APP_C
return $?
fi
}
check_libdvbcsa_all()
{
if check_libdvbcsa "$CFLAGS" "$LDFLAGS" ; then
LIBDVBCSA=1
return 0
fi
LIBDVBCSA_CFLAGS="-I$SRCDIR/contrib/build/libdvbcsa/src"
LIBDVBCSA_LDFLAGS="$SRCDIR/contrib/build/libdvbcsa/libdvbcsa.a"
if check_libdvbcsa "$LIBDVBCSA_CFLAGS" "$LIBDVBCSA_LDFLAGS" ; then
LIBDVBCSA=1
CFLAGS="$CFLAGS $LIBDVBCSA_CFLAGS"
LDFLAGS="$LDFLAGS $LIBDVBCSA_LDFLAGS"
return 0
fi
LIBDVBCSA_LDFLAGS="-ldvbcsa"
if check_libdvbcsa "" "$LIBDVBCSA_LDFLAGS" ; then
LIBDVBCSA=1
LDFLAGS="$LDFLAGS $LIBDVBCSA_LDFLAGS"
return 0
fi
if ! build_libdvbcsa ; then
return 1
fi
LIBDVBCSA_CFLAGS="-I$SRCDIR/contrib/build/libdvbcsa/src"
LIBDVBCSA_LDFLAGS="$SRCDIR/contrib/build/libdvbcsa/libdvbcsa.a"
if check_libdvbcsa "$LIBDVBCSA_CFLAGS" "$LIBDVBCSA_LDFLAGS" ; then
LIBDVBCSA=1
CFLAGS="$CFLAGS $LIBDVBCSA_CFLAGS"
LDFLAGS="$LDFLAGS $LIBDVBCSA_LDFLAGS"
return 0
fi
return 1
}
if [ $ARG_LIBDVBCSA -eq 1 ] ; then
check_libdvbcsa_all
fi
#
clock_gettime_test_c()
{
cat <<EOF
#include <time.h>
int main(void) {
struct timespec ts;
return clock_gettime(CLOCK_REALTIME, &ts);
}
EOF
}
check_clock_gettime()
{
clock_gettime_test_c | $APP_C -Werror $CFLAGS -c -o /dev/null -lrt -x c - >/dev/null 2>&1
}
if check_clock_gettime ; then
CFLAGS="$CFLAGS -DHAVE_CLOCK_GETTIME=1"
LDFLAGS="$LDFLAGS -lrt"
fi
sctp_h_test_c()
{
cat <<EOF
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/sctp.h>
int main(void) { return 0; }
EOF
}
check_sctp_h()
{
sctp_h_test_c | $APP_C -Werror $CFLAGS -o /dev/null -x c - >/dev/null 2>&1
}
if check_sctp_h ; then
CFLAGS="$CFLAGS -DHAVE_NETINET_SCTP_H=1"
fi
endian_h_test_c()
{
cat <<EOF
#include <endian.h>
#ifndef __BYTE_ORDER
#error "__BYTE_ORDER not defined"
#endif
int main(void) { return 0; }
EOF
}
check_endian_h()
{
endian_h_test_c | $APP_C -Werror $CFLAGS -c -o /dev/null -x c - >/dev/null 2>&1
}
if check_endian_h ; then
CFLAGS="$CFLAGS -DHAVE_ENDIAN_H=1"
fi
pread_test_c()
{
cat <<EOF
#include <unistd.h>
int main(void) { char b[256]; return pread(0, b, sizeof(b), 0); }
EOF
}
check_pread()
{
pread_test_c | $APP_C -Werror $CFLAGS -c -o /dev/null -x c - >/dev/null 2>&1
}
if check_pread ; then
CFLAGS="$CFLAGS -DHAVE_PREAD=1"
fi
strndup_test_c()
{
cat <<EOF
#include <string.h>
int main(void) { return (strndup("test", 2) != NULL) ? 0 : 1; }
EOF
}
check_strndup()
{
strndup_test_c | $APP_C -Werror $CFLAGS -c -o /dev/null -x c - >/dev/null 2>&1
}
if check_strndup ; then
CFLAGS="$CFLAGS -DHAVE_STRNDUP=1"
fi
strnlen_test_c()
{
cat <<EOF
#include <string.h>
int main(void) { return (strnlen("test", 2) == 4) ? 0 : 1; }
EOF
}
check_strnlen()
{
strnlen_test_c | $APP_C -Werror $CFLAGS -c -o /dev/null -x c - >/dev/null 2>&1
}
if check_strnlen ; then
CFLAGS="$CFLAGS -DHAVE_STRNLEN=1"
fi
# IGMP Emulation
if [ $ARG_IGMP_EMULATION -eq 1 ]; then
CFLAGS="$CFLAGS -DIGMP_EMULATION"
fi
# APP flags
APP_CFLAGS="$CFLAGS"
APP_LDFLAGS="$LDFLAGS"
# temporary file
TMP_MODULE_MK="/tmp"
if [ ! -d "/tmp" ] ; then
TMP_MODULE_MK="."
fi
TMP_MODULE_MK="$TMP_MODULE_MK/$APP_module.mk-$RANDOM"
touch $TMP_MODULE_MK 2>/dev/null
if [ $? -ne 0 ] ; then
echo "ERROR: failed to build tmp file ($TMP_MODULE_MK)"
exit 1
fi
rm -f $TMP_MODULE_MK
#
cat >&2 <<EOF
Compiler Flags:
TARGET: $MACHINE
CC: $APP_C
CFLAGS: $APP_CFLAGS
EOF
# makefile
rm -f $MAKEFILE
exec 5>$MAKEFILE
cat >&5 <<EOF
# generated by configure.sh
MAKEFLAGS = -rR --no-print-directory
APP = $APP
CC = $APP_C
CFLAGS = $APP_CFLAGS
OS = $OS
CORE_OBJS =
MODS_OBJS =
.PHONY: all clean distclean
all: \$(APP)
clean: \$(APP)-clean
@rm -f Makefile config.h
distclean: clean
EOF
echo "Check modules:" >&2
# main app
APP_SOURCE="$SRCDIR/main.c"
APP_OBJS=""
__check_main_app()
{
APP_OBJS="main.o"
$APP_C $APP_CFLAGS -MT $APP_OBJS -MM $APP_SOURCE 2>$TMP_MODULE_MK
if [ $? -ne 0 ] ; then
return 1
fi
cat <<EOF
@echo " CC: \$@"
@\$(CC) \$(CFLAGS) -o \$@ -c \$<
EOF
return 0
}
touch $CONFFILE
__check_main_app >&5
if [ $? -ne 0 ] ; then
echo " ERROR: $APP_SOURCE" >&2
if [ -f $TMP_MODULE_MK ] ; then
cat $TMP_MODULE_MK >&2
rm -f $TMP_MODULE_MK
fi
exec 5>&-
rm -f $MAKEFILE
exec 6>&-
rm -f $CONFFILE
exit 1
else
echo " OK: $APP_SOURCE"
fi
echo "" >&5
#
select_modules()
{
echo "$ARG_MODULES" | tr ':' '\n' | while read M ; do
if [ -z "$M" ] ; then
:
elif [ "$M" = "*" ] ; then
ls -d $SRCDIR/modules/* | while read M ; do
if [ -f "$M/module.mk" ] ; then
echo "$M"
fi
done
else
echo "$M" | sed 's/\/$//'
fi
done
}
APP_MODULES_LIST=`select_modules`
# modules checking
APP_MODULES_CONF=""
__check_module()
{
MODULE="$1"
OGROUP="$2"
SOURCES=""
MODULES=""
CFLAGS=""
LDFLAGS=""
ERROR=""
OBJECTS=""
. $MODULE/module.mk
if [ -n "$ERROR" ] ; then
echo "$MODULE: error: $ERROR" >$TMP_MODULE_MK
return 1
fi
if [ -n "$LDFLAGS" ] ; then
APP_LDFLAGS="$APP_LDFLAGS $LDFLAGS"
fi
if [ -z "$SOURCES" ] ; then
echo "$MODULE: SOURCES is not defined" >$TMP_MODULE_MK
return 1
fi
echo "${MODULE}_CFLAGS = $CFLAGS"
echo ""
for S in $SOURCES ; do
O=`echo $S | sed -e 's/.c$/.o/'`
OBJECTS="$OBJECTS $MODULE/$O"
$APP_C $APP_CFLAGS $CFLAGS -MT $MODULE/$O -MM $MODULE/$S 2>$TMP_MODULE_MK
if [ $? -ne 0 ] ; then
return 1
fi
cat <<EOF
@echo " CC: \$@"
@\$(CC) \$(CFLAGS) \$(${MODULE}_CFLAGS) -o \$@ -c \$<
EOF
done
cat <<EOF
${MODULE}_OBJECTS = $OBJECTS
${OGROUP} += \$(${MODULE}_OBJECTS)
EOF
if [ -n "MODULES" ] ; then
APP_MODULES_CONF="$APP_MODULES_CONF $MODULES"
fi
return 0
}
check_module()
{
MODULE="$1"
OGROUP="$2"
__check_module $MODULE $OGROUP >&5
if [ $? -eq 0 ] ; then
echo " OK: $MODULE" >&2
else
echo " SKIP: $MODULE" >&2
fi
if [ -f $TMP_MODULE_MK ] ; then
cat $TMP_MODULE_MK >&2
rm -f $TMP_MODULE_MK
fi
}
# CORE
check_module $SRCDIR/core "CORE_OBJS"
check_module $SRCDIR/lua "CORE_OBJS"
# MODULES
for M in $APP_MODULES_LIST ; do
check_module $M "MODS_OBJS"
done
# config.h
rm -f $CONFFILE
exec 6>$CONFFILE
cat >&6 <<EOF
/* generated by configure.sh */
#ifndef _CONFIG_H_
#define _CONFIG_H_
EOF
for M in $APP_MODULES_CONF ; do
echo "extern int luaopen_$M(lua_State *);" >&6
done
cat >&6 <<EOF
int (*astra_mods[])(lua_State *) =
{
EOF
for M in $APP_MODULES_CONF ; do
echo " luaopen_$M," >&6
done
cat >&6 <<EOF
NULL
};
#endif /* _CONFIG_H_ */
EOF
exec 6>&-
# MAKEFILE LINKER
VERSION_MAJOR=`sed -n 's/.*ASTRA_VERSION_MAJOR \([0-9]*\).*/\1/p' version.h`
VERSION_MINOR=`sed -n 's/.*ASTRA_VERSION_MINOR \([0-9]*\).*/\1/p' version.h`
VERSION_PATCH=`sed -n 's/.*ASTRA_VERSION_PATCH \([0-9]*\).*/\1/p' version.h`
VERSION="$VERSION_MAJOR.$VERSION_MINOR.$VERSION_PATCH"
cat >&2 <<EOF
Linker Flags:
VERSION: $VERSION
OUT: $APP
LDFLAGS: $APP_LDFLAGS
Install Path:
BINARY: $ARG_BPATH
EOF
cat >&5 <<EOF
LD = $APP_C
LDFLAGS = $APP_LDFLAGS
STRIP = $APP_STRIP
VERSION = $VERSION
BPATH = $ARG_BPATH
\$(APP): $APP_OBJS \$(CORE_OBJS) \$(MODS_OBJS)
@echo "BUILD: \$@"
@\$(LD) \$^ -o \$@ \$(LDFLAGS)
@\$(STRIP) \$@
install: \$(APP)
@echo "INSTALL: \$(BPATH)"
@rm -f \$(BPATH)
@cp \$(APP) \$(BPATH)
EOF
cat >&5 <<EOF
uninstall:
@echo "UNINSTALL: \$(APP)"
@rm -f \$(BPATH)
\$(APP)-clean:
@echo "CLEAN: \$(APP)"
@rm -f \$(APP) $APP_OBJS
@rm -f \$(MODS_OBJS)
@rm -f \$(CORE_OBJS)
EOF
exec 5>&-