summaryrefslogtreecommitdiff
path: root/scripts/prereq.sh
blob: 769487df76a2848b22bb15b910a4d6208e85232c (plain)
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
757
758
759
760
761
762
763
764
765
766
767
#!/bin/sh
# This file is part of the OpenADK project. OpenADK is copyrighted
# material, please see the LICENCE file in the top-level directory.

# resolve prerequisites for OpenADK build

topdir=$(pwd)
target="$@"
flags="$MAKEFLAGS"
out=0

mirror=http://distfiles.openadk.org
makever=4.1
bashver=4.3.30
dlverbose=0

# detect operating system
os=$(env uname)
osver=$(env uname -r)
printf " --->  $os $osver for build detected.\n"

# check if the filesystem is case sensitive
rm -f foo
echo >FOO
if [ -e foo ]; then
  printf "ERROR: OpenADK cannot be built in a case-insensitive file system."
  case $os in
    CYG*)
      printf "Building OpenADK on $os needs a small registry change."
      printf "http://cygwin.com/cygwin-ug-net/using-specialnames.html"
      ;;
    Darwin*)
      printf "Building OpenADK on $os needs a case-sensitive disk partition."
      printf "For Snow Leopard and above you can use diskutil to resize your existing disk."
      printf "Example: sudo diskutil resizeVolume disk0s2 90G 1 jhfsx adk 30G"
      printf "For older versions you might consider to use a disk image:"
      printf "hdiutil create -type SPARSE -fs 'Case-sensitive Journaled HFS+' -size 30g ~/openadk.dmg"
      ;;
  esac
  rm -f FOO
  exit 1
fi
rm -f FOO

# do we have a download tool?
tools="curl wget"
for tool in $tools; do
  printf " --->  checking if $tool is installed.. "
  if which $tool >/dev/null; then
    printf "found\n"
    case $tool in
      curl)
          FETCHCMD="$(which $tool) -L -k -f -\# -o "
        ;;
      wget)
          FETCHCMD="$(which $tool) --no-check-certificate -O "
        ;;
    esac
    break
  else
    printf "not found\n"
    continue
  fi
done
if [ -z "$FETCHCMD" ]; then
  printf "ERROR: no download tool found. Fatal error.\n"
  exit 1
fi

# do we have a checksum tool?
tools="sha256sum sha256 cksum shasum"
for tool in $tools; do
  printf " --->  checking if $tool is installed.. "
  if which $tool >/dev/null 2>/dev/null; then
    printf "found\n"
    # check if cksum is usable
    case $tool in
      sha256sum)
        SHA256=$(which $tool)
        ;;
      sha256)
        SHA256="$(which $tool) -q"
        ;;
      cksum)
        if cksum -q >/dev/null 2>/dev/null; then
          SHA256="$(which $tool) -q -a sha256"
        else
          continue
        fi
        ;;
      shasum)
        SHA256="$(which $tool) -a 256"
        ;;
    esac
    break
  else
    printf "not found\n"
    continue
  fi
done
if [ -z "$SHA256" ]; then
  printf "ERROR: no checksum tool found. Fatal error.\n"
  exit 1
fi

# create download dir
if [ ! -d $topdir/dl ]; then
  mkdir -p $topdir/dl
fi

# check for c compiler
compilerbins="cc gcc clang"
for compilerbin in $compilerbins; do
  printf " --->  checking if $compilerbin is installed.. "
  if which $compilerbin >/dev/null; then
    printf "found\n"
    CC=$compilerbin
    CCFOUND=1
    break
  else
    printf "not found\n"
    continue
  fi
done
if [ -z "$CCFOUND" ]; then
  printf "ERROR: no C compiler found. Fatal error.\n"
  exit 1
fi

# check for c++ compiler
compilerbins="c++ g++ clang++"
for compilerbin in $compilerbins; do
  printf " --->  checking if $compilerbin is installed.. "
  if which $compilerbin >/dev/null; then
    printf "found\n"
    CXX=$compilerbin
    CXXFOUND=1
    break
  else
    printf "not found\n"
    continue
  fi
done
if [ -z "$CXXFOUND" ]; then
  printf "ERROR: no C++ compiler found. Fatal error.\n"
  exit 1
fi

gnu_host_name=$(${CC} -dumpmachine)

# relocation of topdir?
olddir=$(grep "^ADK_TOPDIR" prereq.mk 2>/dev/null |cut -d '=' -f 2)
newdir=$(pwd)

if [ ! -z "$olddir" ]; then
  if [ "$olddir" != "$newdir" ]; then
    printf " --->  adk directory was relocated, fixing .."
    sed -i -e "s#$olddir#$newdir#g" $(find target_* -name \*.pc|xargs) 2>/dev/null
    sed -i -e "s#$olddir#$newdir#g" $(find host_${gnu_host_name} -type f|xargs) 2>/dev/null
    sed -i -e "s#$olddir#$newdir#g" $(find target_*/scripts -type f|xargs) 2>/dev/null
    sed -i -e "s#$olddir#$newdir#" target_*/etc/ipkg.conf 2>/dev/null
    sleep 2
    printf "done\n"
  fi
fi


case :$PATH: in
  (*:$topdir/host_${gnu_host_name}/bin:*) ;;
  (*) export PATH=$topdir/host_${gnu_host_name}/bin:$PATH ;;
esac

# check for GNU make
makebins="gmake make"
for makebin in $makebins; do
  printf " --->  checking if $makebin is installed.. "
  if which $makebin >/dev/null 2>/dev/null; then
    printf "found\n"
    printf " --->  checking if it is GNU make.. "
    $makebin --version 2>/dev/null| grep GNU >/dev/null
    if [ $? -eq 0 ]; then
      printf "yes\n"
      MAKE=$(which $makebin)
      break
    else
      # we need to build GNU make
      printf "no\n"
      printf " --->  compiling missing GNU make.. "
      cd dl
      $FETCHCMD make-${makever}.tar.gz $mirror/make-${makever}.tar.gz
      if [ $? -ne 0 ]; then
        printf "ERROR: failed to download make from $mirror\n"
        exit 1
      fi
      cd ..
      mkdir tmp
      cd tmp
      tar xzf ../dl/make-${makever}.tar.gz
      cd make-$makever
      ./configure --prefix=$topdir/host_$gnu_host_name/
      make
      make install
      cd ..
      cd ..
      rm -rf tmp
      MAKE=$topdir/host_$gnu_host_name/bin/make
      makebin=$topdir/host_$gnu_host_name/bin/make
      printf " done\n"
    fi
  else
    printf "not found\n"
    continue
  fi
done

# check for bash
printf " --->  checking if bash is installed.. "
if which bash >/dev/null; then
  printf "found\n"
  printf " --->  checking if it is bash 4.x.. "
  bash --version 2>/dev/null| grep -i "Version 4" >/dev/null
  if [ $? -eq 0 ]; then
    printf "yes\n"
  else
    # we need to build GNU bash 4.x
    printf "not found\n"
    printf " --->  compiling missing GNU bash.. "
    cd dl
    $FETCHCMD bash-${bashver}.tar.gz $mirror/bash-${bashver}.tar.gz
    if [ $? -ne 0 ]; then
      printf "ERROR: failed to download make from $mirror\n"
      exit 1
    fi
    cd ..
    mkdir tmp
    cd tmp
    tar xzf ../dl/bash-${bashver}.tar.gz
    cd bash-${bashver}
    ./configure --prefix=$topdir/host_$gnu_host_name/
    make
    make install
    cd ..
    cd ..
    rm -rf tmp
    printf " done\n"
  fi
fi

# skip the script if distclean / cleandir
if [ "$target" = "distclean" -o "$target" = "cleandir" ]; then
  touch prereq.mk
  $makebin ADK_TOPDIR=$topdir -s -f Makefile.adk $flags $target
  exit 0
fi

printf " --->  checking if strings is installed.. "
if ! which strings >/dev/null 2>&1; then
  echo You must install strings to continue.
  echo
  out=1
  printf "not found\n"
fi
printf "found\n"

printf " --->  checking if perl is installed.. "
if ! which perl >/dev/null 2>&1; then
  echo You must install perl to continue.
  echo
  out=1
  printf "not found\n"
fi
printf "found\n"

printf " --->  checking if gzip is installed.. "
if ! which gzip >/dev/null 2>&1; then
  echo You must install gzip to continue.
  echo
  out=1
  printf "not found\n"
fi
printf "found\n"

printf " --->  checking if git is installed.. "
if ! which git >/dev/null 2>&1; then
  echo You must install git to continue.
  echo
  out=1
  printf "not found\n"
fi
printf "found\n"


# creating prereq.mk
echo "ADK_TOPDIR:=$(readlink -nf . 2>/dev/null || pwd -P)" > $topdir/prereq.mk
echo "BASH:=$(which bash)" >> $topdir/prereq.mk
echo "SHELL:=$(which bash)" >> $topdir/prereq.mk
echo "GMAKE:=$MAKE" >> $topdir/prereq.mk
echo "MAKE:=$MAKE" >> $topdir/prereq.mk
echo "FETCHCMD:=$FETCHCMD" >> $topdir/prereq.mk
echo "SHA256:=$SHA256" >> $topdir/prereq.mk
echo "GNU_HOST_NAME:=${gnu_host_name}" >> $topdir/prereq.mk
echo "OS_FOR_BUILD:=${os}" >> $topdir/prereq.mk
echo "ARCH_FOR_BUILD:=$(${CC} -dumpmachine | sed \
    -e 's/x86_64-linux-gnux32/x32/' \
    -e s'/-.*//' \
    -e 's/sparc.*/sparc/' \
    -e 's/armeb.*/armeb/g' \
    -e 's/arm.*/arm/g' \
    -e 's/m68k.*/m68k/' \
    -e 's/sh[234]/sh/' \
    -e 's/mips-.*/mips/' \
    -e 's/mipsel-.*/mipsel/' \
    -e 's/i[3-9]86/x86/' \
    )" >>prereq.mk

if [ "$CC" = "clang" ]; then
  echo "HOST_CC:=${CC} -fbracket-depth=1024" >> $topdir/prereq.mk
else
  echo "HOST_CC:=${CC}" >> $topdir/prereq.mk
fi
if [ "$CXX" = "clang++" ]; then
  echo "HOST_CXX:=${CXX} -fbracket-depth=1024" >> $topdir/prereq.mk
else
  echo "HOST_CXX:=${CXX}" >> $topdir/prereq.mk
fi

echo "HOST_CFLAGS:=-O0 -g0" >> $topdir/prereq.mk
echo "HOST_CXXFLAGS:=-O0 -g0" >> $topdir/prereq.mk
echo 'LANGUAGE:=C' >> $topdir/prereq.mk
echo 'LC_ALL:=C' >> $topdir/prereq.mk
echo "_PATH:=$PATH" >> $topdir/prereq.mk
echo "PATH:=${topdir}/scripts:/usr/sbin:$PATH" >> $topdir/prereq.mk
echo "GIT:=$(which git 2>/dev/null)" >> $topdir/prereq.mk
if [ $dlverbose -eq 0 ]; then
  echo "GITOPTS:=--quiet" >> $topdir/prereq.mk
fi
echo "export ADK_TOPDIR GIT GITOPTS SHA256 BASH SHELL" >> $topdir/prereq.mk

# create temporary Makefile
cat >Makefile.tmp <<'EOF'
include ${ADK_TOPDIR}/prereq.mk
all: test

test: test.c
	@${HOST_CC} ${HOST_CFLAGS} -o $@ $^ ${LDADD}
EOF

# check if compiler works
cat >test.c <<-'EOF'
	#include <stdio.h>
	int
	main()
	{
		printf("YES");
		return (0);
	}
EOF

printf " --->  checking if compiler is working.. "
$MAKE --no-print-directory ADK_TOPDIR=$topdir -f Makefile.tmp >/dev/null 2>&1
X=$(./test 2>/dev/null)
if [ X$X != XYES ]; then
  echo Cannot compile a simple test programme.
  echo You must install a host make and C compiler.
  echo
  out=1
else
  printf "okay\n"
fi
rm test.c test 2>/dev/null

printf " --->  checking if zlib is installed.. "
# check for zlib
cat >test.c <<-'EOF'
	#include <stdio.h>
	#include <zlib.h>

	#ifndef STDIN_FILENO
	#define STDIN_FILENO 0
	#endif

	int
	main()
	{
		gzFile zstdin;
		char buf[1024];
		int i;

		zstdin = gzdopen(STDIN_FILENO, "rb");
		i = gzread(zstdin, buf, sizeof (buf));
		if ((i > 0) && (i < sizeof (buf)))
			buf[i] = '\0';
		buf[sizeof (buf) - 1] = '\0';
		printf("%s\n", buf);
		return (0);
	}
EOF

$MAKE --no-print-directory LDADD=-lz ADK_TOPDIR=$topdir -f Makefile.tmp >/dev/null 2>&1 
X=$(echo YES | gzip | ./test 2>/dev/null)
if [ X$X != XYES ]; then
  echo Cannot compile a libz test programm.
  echo You must install the zlib development package,
  echo usually called libz-dev, and the run-time library.
  echo
  out=1
else
  printf "found\n"
fi

rm test.c test 2>/dev/null
rm Makefile.tmp 2>/dev/null

# error out on any required prerequisite
if [ $out -ne 0 ]; then
  exit
fi

host_build_bc=0
if which bc >/dev/null 2>&1; then
  if ! echo quit|bc -q 2>/dev/null >/dev/null; then
    host_build_bc=1
  else 
    if bc -v 2>&1| grep -q BSD >/dev/null 2>&1; then
      host_build_bc=1
    fi 
  fi
else
  host_build_bc=1
fi

host_build_bison=0
if ! which bison >/dev/null 2>&1; then
  host_build_bison=1
fi

host_build_bzip2=0
if ! which bzip2 >/dev/null 2>&1; then
  host_build_bzip2=1
fi

host_build_file=0
if ! which file >/dev/null 2>&1; then
  host_build_file=1
fi

host_build_flex=0
if ! which flex >/dev/null 2>&1; then
  host_build_flex=1
fi

host_build_m4=0
if ! which m4 >/dev/null 2>&1; then
  host_build_m4=1
fi

host_build_mkimage=0
if ! which mkimage >/dev/null 2>&1; then
  host_build_mkimage=1
fi

host_build_mksh=0
if ! which mksh >/dev/null 2>&1; then
  host_build_mksh=1
fi

host_build_patch=0
if ! which patch >/dev/null 2>&1; then
  host_build_patch=1
fi

host_build_pkgconf=0
if ! which pkgconf >/dev/null 2>&1; then
  host_build_pkgconf=1
fi

host_build_tar=0
if which tar >/dev/null 2>&1; then
  if ! tar --version 2>/dev/null|grep GNU >/dev/null;then
    host_build_tar=1
  fi
else
  host_build_tar=1
fi

host_build_findutils=0
if which xargs >/dev/null 2>&1; then
  if ! xargs --version 2>/dev/null|grep GNU >/dev/null;then
    host_build_findutils=1
  fi
fi

if which find >/dev/null 2>&1; then
  if ! find --version 2>/dev/null|grep GNU >/dev/null;then
    host_build_findutils=1
  fi
fi

host_build_grep=0
if which grep >/dev/null 2>&1; then
  if ! grep --version 2>/dev/null|grep GNU >/dev/null;then
    host_build_grep=1
  fi
fi

host_build_gawk=0
if ! which gawk >/dev/null 2>&1; then
  host_build_gawk=1
fi

host_build_sed=0
if which sed >/dev/null 2>&1; then
  if ! sed --version 2>/dev/null|grep GNU >/dev/null;then
    host_build_sed=1
  fi
fi

host_build_xz=0
if ! which xz >/dev/null 2>&1; then
  host_build_xz=1
fi

# optional
host_build_cdrtools=0
if ! which mkisofs >/dev/null 2>&1; then
  host_build_cdrtools=1
fi

host_build_ccache=0
if ! which ccache >/dev/null 2>&1; then
  host_build_ccache=1
fi

host_build_genext2fs=0
if ! which genext2fs >/dev/null 2>&1; then
  host_build_genext2fs=1
fi

host_build_lzma=0
if ! which lzma >/dev/null 2>&1; then
  host_build_lzma=1
fi

host_build_lz4=0
if ! which lz4c >/dev/null 2>&1; then
  host_build_lz4=1
fi

host_build_lzop=0
if ! which lzop >/dev/null 2>&1; then
  host_build_lzop=1
fi

host_build_qemu=0
if ! which qemu-img >/dev/null 2>&1; then
  host_build_qemu=1
fi

host_build_coreutils=0
if which tr >/dev/null 2>&1; then
  if ! tr --version 2>/dev/null|grep GNU >/dev/null;then
    host_build_coreutils=1
  fi
fi

echo "config ADK_HOST_BUILD_TOOLS" > $topdir/target/config/Config.in.prereq
printf "\t%s\n" "bool" >> $topdir/target/config/Config.in.prereq
printf "\t%s\n" "default y" >> $topdir/target/config/Config.in.prereq
# always required
if [ $host_build_bc -eq 1 ]; then
  printf "\t%s\n" "select ADK_HOST_BUILD_BC" >> $topdir/target/config/Config.in.prereq
fi
if [ $host_build_bison -eq 1 ]; then
  printf "\t%s\n" "select ADK_HOST_BUILD_BISON" >> $topdir/target/config/Config.in.prereq
fi
if [ $host_build_bzip2 -eq 1 ]; then
  printf "\t%s\n" "select ADK_HOST_BUILD_BZIP2" >> $topdir/target/config/Config.in.prereq
fi
if [ $host_build_file -eq 1 ]; then
  printf "\t%s\n" "select ADK_HOST_BUILD_FILE" >> $topdir/target/config/Config.in.prereq
fi
if [ $host_build_flex -eq 1 ]; then
  printf "\t%s\n" "select ADK_HOST_BUILD_FLEX" >> $topdir/target/config/Config.in.prereq
fi
if [ $host_build_gawk -eq 1 ]; then
  printf "\t%s\n" "select ADK_HOST_BUILD_GAWK" >> $topdir/target/config/Config.in.prereq
fi
if [ $host_build_grep -eq 1 ]; then
  printf "\t%s\n" "select ADK_HOST_BUILD_GREP" >> $topdir/target/config/Config.in.prereq
fi
if [ $host_build_m4 -eq 1 ]; then
  printf "\t%s\n" "select ADK_HOST_BUILD_M4" >> $topdir/target/config/Config.in.prereq
fi
if [ $host_build_mkimage -eq 1 ]; then
  printf "\t%s\n" "select ADK_HOST_BUILD_U_BOOT" >> $topdir/target/config/Config.in.prereq
fi
if [ $host_build_mksh -eq 1 ]; then
  printf "\t%s\n" "select ADK_HOST_BUILD_MKSH" >> $topdir/target/config/Config.in.prereq
fi
if [ $host_build_patch -eq 1 ]; then 
  printf "\t%s\n" "select ADK_HOST_BUILD_PATCH" >> $topdir/target/config/Config.in.prereq
fi
if [ $host_build_pkgconf -eq 1 ]; then
  printf "\t%s\n" "select ADK_HOST_BUILD_PKGCONF" >> $topdir/target/config/Config.in.prereq
fi
if [ $host_build_findutils -eq 1 ]; then
  printf "\t%s\n" "select ADK_HOST_BUILD_FINDUTILS" >> $topdir/target/config/Config.in.prereq
fi
if [ $host_build_sed -eq 1 ]; then
  printf "\t%s\n" "select ADK_HOST_BUILD_SED" >> $topdir/target/config/Config.in.prereq
fi
if [ $host_build_tar -eq 1 ]; then
  printf "\t%s\n" "select ADK_HOST_BUILD_TAR" >> $topdir/target/config/Config.in.prereq
fi
if [ $host_build_xz -eq 1 ]; then
  printf "\t%s\n" "select ADK_HOST_BUILD_XZ" >> $topdir/target/config/Config.in.prereq
fi
# optional
if [ $host_build_ccache -eq 1 ]; then
  printf "\t%s\n" "select ADK_HOST_BUILD_CCACHE if ADK_HOST_NEED_CCACHE" >> $topdir/target/config/Config.in.prereq
fi
if [ $host_build_cdrtools -eq 1 ]; then
  printf "\t%s\n" "select ADK_HOST_BUILD_CDRTOOLS if ADK_HOST_NEED_CDRTOOLS" >> $topdir/target/config/Config.in.prereq
fi
if [ $host_build_genext2fs -eq 1 ]; then
  printf "\t%s\n" "select ADK_HOST_BUILD_GENEXT2FS if ADK_HOST_NEED_GENEXT2FS" >> $topdir/target/config/Config.in.prereq
fi
if [ $host_build_lzma -eq 1 ]; then
  printf "\t%s\n" "select ADK_HOST_BUILD_LZMA if ADK_HOST_NEED_LZMA" >> $topdir/target/config/Config.in.prereq
fi
if [ $host_build_lz4 -eq 1 ]; then
  printf "\t%s\n" "select ADK_HOST_BUILD_LZ4 if ADK_HOST_NEED_LZ4" >> $topdir/target/config/Config.in.prereq
fi
if [ $host_build_lzop -eq 1 ]; then
  printf "\t%s\n" "select ADK_HOST_BUILD_LZOP if ADK_HOST_NEED_LZOP" >> $topdir/target/config/Config.in.prereq
fi
if [ $host_build_qemu -eq 1 ]; then
  printf "\t%s\n" "select ADK_HOST_BUILD_QEMU if ADK_HOST_NEED_QEMU" >> $topdir/target/config/Config.in.prereq
fi
if [ $host_build_coreutils -eq 1 ]; then
  printf "\t%s\n" "select ADK_HOST_BUILD_COREUTILS if ADK_HOST_NEED_COREUTILS" >> $topdir/target/config/Config.in.prereq
fi

# create Host OS symbols
case $os in
  Linux)
    printf "\nconfig ADK_HOST_LINUX\n" >> $topdir/target/config/Config.in.prereq
    printf "\tbool\n" >> $topdir/target/config/Config.in.prereq
    printf "\tdefault y\n" >> $topdir/target/config/Config.in.prereq
    ;;
  Darwin)
    printf "\nconfig ADK_HOST_DARWIN\n" >> $topdir/target/config/Config.in.prereq
    printf "\tbool\n" >> $topdir/target/config/Config.in.prereq
    printf "\tdefault y\n" >> $topdir/target/config/Config.in.prereq
    ;;
  OpenBSD)
    printf "\nconfig ADK_HOST_OPENBSD\n" >> $topdir/target/config/Config.in.prereq
    printf "\tbool\n" >> $topdir/target/config/Config.in.prereq
    printf "\tdefault y\n" >> $topdir/target/config/Config.in.prereq
    ;;
  FreeBSD)
    printf "\nconfig ADK_HOST_FREEBSD\n" >> $topdir/target/config/Config.in.prereq
    printf "\tbool\n" >> $topdir/target/config/Config.in.prereq
    printf "\tdefault y\n" >> $topdir/target/config/Config.in.prereq
    ;;
  NetBSD)
    printf "\nconfig ADK_HOST_NETBSD\n" >> $topdir/target/config/Config.in.prereq
    printf "\tbool\n" >> $topdir/target/config/Config.in.prereq
    printf "\tdefault y\n" >> $topdir/target/config/Config.in.prereq
    ;;
  MirBSD)
    printf "\nconfig ADK_HOST_MIRBSD\n" >> $topdir/target/config/Config.in.prereq
    printf "\tbool\n" >> $topdir/target/config/Config.in.prereq
    printf "\tdefault y\n" >> $topdir/target/config/Config.in.prereq
    ;;
  Cygwin*)
    printf "\nconfig ADK_HOST_CYGWIN\n" >> $topdir/target/config/Config.in.prereq
    printf "\tbool\n" >> $topdir/target/config/Config.in.prereq
    printf "\tdefault y\n" >> $topdir/target/config/Config.in.prereq
    ;;
esac

if [ "$target" = "defconfig" ]; then
  $makebin ADK_TOPDIR=$topdir --no-print-directory -f Makefile.adk $flags $target
  exit 0
fi

if [ ! -f $topdir/.config ]; then
    # create a config if no exist
    touch .firstrun
    $makebin ADK_TOPDIR=$topdir --no-print-directory -f Makefile.adk menuconfig
else
  # scan host-tool prerequisites of certain packages before building.
  . $topdir/.config
  if [ -n "$ADK_PACKAGE_KODI" ]; then
    NEED_JAVA="$NEED_JAVA kodi"
  fi

  if [ -n "$ADK_PACKAGE_ICU4C" ]; then
    NEED_STATIC_LIBSTDCXX="$NEED_STATIC_LIBSTDCXX icu4c"
  fi

  if [ -n "$ADK_PACKAGE_XKEYBOARD_CONFIG" ]; then
    NEED_XKBCOMP="$NEED_XKBCOMP xkeyboard-config"
  fi

  if [ -n "$ADK_PACKAGE_FONT_BITSTREAM_100DPI" ]; then
    NEED_MKFONTDIR="$NEED_MKFONTDIR font-bitstream-100dpi"
  fi

  if [ -n "$ADK_PACKAGE_FONT_BITSTREAM_75DPI" ]; then
    NEED_MKFONTDIR="$NEED_MKFONTDIR font-bitstream-75dpi"
  fi

  if [ -n "$ADK_PACKAGE_FONT_ADOBE_100DPI" ]; then
    NEED_MKFONTDIR="$NEED_MKFONTDIR font-adobe-100dpi"
  fi

  if [ -n "$ADK_PACKAGE_FONT_ADOBE_75DPI" ]; then
    NEED_MKFONTDIR="$NEED_MKFONTDIR font-adobe-75dpi"
  fi

  if [ -n "$NEED_MKFONTDIR" ]; then
    if ! which mkfontdir >/dev/null 2>&1; then
      printf "You need mkfontdir to build $NEED_MKFONTDIR \n"
      out=1
    fi
  fi

  if [ -n "$NEED_XKBCOMP" ]; then
    if ! which xkbcomp >/dev/null 2>&1; then
      printf "You need xkbcomp to build $NEED_XKBCOMP \n"
      out=1
    fi
  fi

  if [ -n "$NEED_JAVA" ]; then
    if ! which java >/dev/null 2>&1; then
      printf "You need java to build $NEED_JAVA \n"
      out=1
    fi
  fi

  if [ -n "$NEED_STATIC_LIBSTDCXX" ]; then
cat >test.c <<-'EOF'
	#include <stdio.h>
	int
	main()
	{
		return (0);
	}
EOF
    if ! $CXX -static-libstdc++ -o test test.c 2>/dev/null ; then
      printf "You need static version of libstdc++ installed to build $NEED_STATIC_LIBSTDCXX \n"
      out=1
      rm test test.c 2>/dev/null
    fi
  fi

  # error out
  if [ $out -ne 0 ]; then
    exit $out
  fi

  # start build
  $makebin ADK_TOPDIR=$topdir --no-print-directory -f Makefile.adk $flags $target
fi