~rdoering/ubuntu/karmic/erlang/fix-535090

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
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
#! /bin/sh

# Expected autoconf version
EXPECTED_AUTOCONF_VERSION=2.59

# clearmake command to use
clearmake=false

# Global configuration variables
#
# NOTE: lazy_configure depends on '.' allways being last directory
if [ -z "$ONLY_ERTS" ]; then
    AUTOCONF_SUBDIRS="lib lib/*"
fi
AUTOCONF_SUBDIRS="$AUTOCONF_SUBDIRS erts ."

# Utility functions
usage ()
{
    echo "Available options:"
    echo "setup [-a] [<configure parameters>] - does autoconf, configure and opt."
    echo "all [-a] <dir> - does autoconf, configure, primary, boot, release"
    echo "autoconf - (re)build the configure scripts"
    echo "configure [<configure parameters>] - does the actual configuration"
    echo "plain [-a] - build a small Erlang system without bootstrap; plain flavor"
    echo "opt [-a] - same as plain (above)"
    echo "smp [-a] - build a small Erlang system without bootstrap; smp flavor"
    echo "hybrid [-a] - build a small Erlang system without bootstrap; hybrid flavor"
    echo "nofrag [-a] - build a small Erlang system without bootstrap; nofrag flavor"
    echo "primary - creates the primary bootstrap, the one shipped"
    echo "boot [-a] - bootstraps and build small system (after primary)"
    echo "release <target_dir> - creates a small release to <target_dir>"
    echo "release [-a] <target_dir> - creates full release to <target_dir>"
    echo "tests <dir> - Build testsuites to <dir>"
    echo ""
    echo "remove_prebuilt_files - create a minimal source tree"
    echo "save_bootstrap - recreate primary bootstrap"
    echo ""
    echo "debuginfo_win32 <dir> - adds debug emulator and pdb files to <dir>"
    echo "installer_win32 <dir> - creates a windows installer from <dir>" 
    echo ""
    echo "Before trying to build on windows, consider the following option"
    echo "env_win32 - echo environment settings for win32 with visual C++, use with eval"
    echo "env_mingw32 - echo environment settings for win32 with MinGW, use with eval"
    echo "Before trying to build for vxworks, consider the following option"
    echo "env_vxworks <cpu>  - echo environment settings for vxworks, use with eval"
}

check_erltop ()
{
        ERLTOP_FORCED=false
	if [ "X$ERL_TOP" = "X" ]; then
		if [ -f ./otp_build -a -f ./erts/autoconf/config.guess ]; then
		    ERLTOP_FORCED=true
		    ERL_TOP=`/bin/pwd`
		else
		    echo "The environment variable ERL_TOP must be set." >&2
		    exit 1
		fi
	fi
}

target_contains ()
{
	Y=`echo $TARGET | sed "s,$1,,g"`
	[ X"$Y" != X"$TARGET" ]
	return $?
} 



# Execution of the different options

# Special static config flags for certain platforms are set here  
set_config_flags ()
{
	if target_contains win32; then
		CONFIG_FLAGS="--build=$BUILDSYS --host=win32 --target=win32" 
	fi
	if target_contains linux; then 
		CONFIG_FLAGS="--disable-hipe"
	fi
	if target_contains "univel-sysv4"; then
		CONFIG_FLAGS="--x-libraries=/usr/lib/X11"
	fi

	if target_contains free_source; then
		CONFIG_FLAGS="$CONFIG_FLAGS --host=$TARGET"
	fi
	CONFIG_FLAGS="$CONFIG_FLAGS $*"
	export CONFIG_FLAGS;
}
	
do_autoconf ()		
{
        if target_contains win32; then
	    # Select the correct autoconf on cygwin
	    save_want_autoconf_ver=$WANT_AUTOCONF_VER
	    WANT_AUTOCONF_VER=$EXPECTED_AUTOCONF_VERSION
	    export WANT_AUTOCONF_VER
	fi
	exp_ac_vsn=$EXPECTED_AUTOCONF_VERSION
	ac_vsn_blob=`autoconf --version`
	ac_vsn=`echo x$ac_vsn_blob | sed "s|[^0-9]*\([0-9][^ \t\n]*\).*|\1|"`
	case "$ac_vsn" in
	    $exp_ac_vsn)
		;;
	    *)
		echo "***************************************************" 1>&2
		echo "***************************************************" 1>&2
		echo "*** WARNING: System might fail to configure or"      1>&2
		echo "***          might be erroneously configured"        1>&2
		echo "***          since autoconf version $ac_vsn is used" 1>&2
		echo "***          instead of version $exp_ac_vsn!"        1>&2
		echo "***************************************************" 1>&2
		echo "***************************************************" 1>&2
		;;
	esac

	if [ -d erts/autom4te.cache ]; then
	    echo "Cleaning erts/autom4te.cache"
	    rm -f erts/autom4te.cache/*
	fi

	save_ot="$OVERRIDE_TARGET"
	save_t="$TARGET"
	if [ ! -z "$OVERRIDE_CONFIGURE" ]; then
	    echo "Autoconf disabled on target $TARGET, but is performed on host" >&2
	    OVERRIDE_TARGET=
	    export OVERRIDE_TARGET
	    # We still use erts configure for erl_interface and VxWorks
	    case "$TARGET" in
		*vxworks*)
		    AUTOCONF_SUBDIRS=`echo $AUTOCONF_SUBDIRS | \
					sed -e 's,lib/erl_interface,,' \
					    -e 's,lib/gs,,' \
					    -e 's,lib/megaco,,'`
		    ;;
		*ose*)
		    AUTOCONF_SUBDIRS=`echo $AUTOCONF_SUBDIRS | \
					sed -e 's,lib/erl_interface,,'`
		    ;;
	    esac
	    TARGET=`$ERL_TOP/erts/autoconf/config.guess`
	fi
	
	# Remove TSP if not on linux (in open source tsp does not exist...)
	# Code disabled, need to configure all to run release_docs on Solaris
#	case "$TARGET" in
#	    *linux*);;
#	    *)
#		AUTOCONF_SUBDIRS=`echo $AUTOCONF_SUBDIRS | sed 's,lib/tsp,,'`;;
#	esac
	for d in $AUTOCONF_SUBDIRS; do
		if [ -f $d/configure.in ]; then
			echo "=== running autoconf in $d"
			( cd $d && autoconf ) || exit 1
		fi
		if [ x$d = x"erts" ]; then
			echo "=== running autoheader in $d"
			( cd $d && autoheader configure.in > config.h.in ) \
			    || exit 1
		fi
	done
	OVERRIDE_TARGET="$save_ot"
	export OVERRIDE_TARGET
	TARGET="$save_t"
        if target_contains win32; then
	    WANT_AUTOCONF_VER=$save_want_autoconf_ver
	    export WANT_AUTOCONF_VER
	fi
}

mk_targetdir ()
{
    if [ ! -d $ERL_TOP/$TARGET ]; then
	echo "creating  $ERL_TOP/$TARGET"
	mkdir $ERL_TOP/$TARGET
    else
	echo "existing $ERL_TOP/$TARGET is used for cache"
    fi
}

do_configure ()
{
    setup_make
    mk_targetdir
    save_ot="$OVERRIDE_TARGET"
    save_t="$TARGET"
    if [ ! -z "$OVERRIDE_CONFIGURE" ]; then
	case $TARGET in
	    vxworks_*)
		( cd erts/autoconf && \
		  $ERL_TOP/erts/autoconf/configure.vxworks $TARGET )
		  echo "Configuring for build host too..." >&2
		  OVERRIDE_TARGET=
		  export OVERRIDE_TARGET
		  TARGET=`$ERL_TOP/erts/autoconf/config.guess`
		  mk_targetdir;;
	    ose_*)
		( cd erts/autoconf && \
		  $ERL_TOP/erts/autoconf/configure.ose $TARGET )
		  echo "Configuring for build host too..." >&2
		  OVERRIDE_TARGET=
		  export OVERRIDE_TARGET
		  TARGET=`$ERL_TOP/erts/autoconf/config.guess`
		  mk_targetdir;;
	    *)
		echo "Unexpected target when ordinary configure is" \
			"overridden" >&2
		echo 'check if $OVERRIDE_CONFIGURE and $OVERRIDE_TAGET' \
		        'environments are correct.' >&2
		exit 1;;
	esac
    fi
    set_config_flags $CONFIG_FLAGS "$@"
    if [ -z "$ONLY_ERTS" ]; then
	./configure --cache-file=/dev/null $CONFIG_FLAGS \
	    || exit 1
    else
	echo "erts/configure --cache-file=/dev/null $CONFIG_FLAGS"
	(cd $ERL_TOP/erts; ./configure --cache-file=/dev/null $CONFIG_FLAGS) || exit 1
    fi
    OVERRIDE_TARGET="$save_ot"
    export OVERRIDE_TARGET
    TARGET="$save_t"
}

do_lazy_configure ()
{
    setup_make
    case $TARGET in
	vxworks_*|ose_*)
	    echo "Not supported for target: $TARGET" >&2
	    exit 1
	    ;;
	*)
	    ;;
    esac
    set_config_flags $CONFIG_FLAGS "$@"
    for c_dir in $AUTOCONF_SUBDIRS; do
	if test -f $ERL_TOP/$c_dir/configure.in; then
	    dir=$ERL_TOP/$c_dir
	    echo ""
	    echo "=== Begin configuring $dir"
	    xc_dep= ;
	    xcs_dep= ;
	    test -d $dir/$TARGET || mkdir $dir/$TARGET
	    test -f $dir/aclocal.m4 && xc_dep="$xcs_dep $dir/aclocal.m4"
	    test -f $dir/acsite.m4 && xc_dep="$xcs_dep $dir/acsite.m4"
	    test x$c_dir = x"erts" && xcs_dep="$xcs_dep $dir/config.h.in"
	    $MAKE -f $ERL_TOP/make/lazy_configure.mk                         \
		MAKE="$MAKE" TARGET=$TARGET                                  \
		ERL_TOP=$ERL_TOP                                             \
		CONFIGURE_FLAGS="$CONFIG_FLAGS"                              \
		CONFIGURE_DIR=$dir                                           \
		EXTRA_CONFIGURE_DEPENDENCIES=$xc_dep                         \
		EXTRA_CONFIG_STATUS_DEPENDENCIES=$xcs_dep                    \
		EXPECTED_AUTOCONF_VERSION=$EXPECTED_AUTOCONF_VERSION         \
		lazy_configure
	    echo "=== Done configuring $dir"
	    echo ""
	fi
    done
}

do_lazy_configure_clean ()
{
    setup_make
    case $TARGET in
	vxworks_*|ose_*)
	    echo "Not supported for target: $TARGET" >&2
	    exit 1
	    ;;
	*)
	    ;;
    esac
    for c_dir in $AUTOCONF_SUBDIRS; do
	if test -f $ERL_TOP/$c_dir/configure.in; then
	    dir=$ERL_TOP/$c_dir
	    echo ""
	    echo "=== Begin cleaning configure in $dir"
	    xc_dep= ;
	    xcs_dep= ;
	    test -d $dir/$TARGET || mkdir $dir/$TARGET
	    test -f $dir/aclocal.m4 && xc_dep="$xcs_dep $dir/aclocal.m4"
	    test -f $dir/acsite.m4 && xc_dep="$xcs_dep $dir/acsite.m4"
	    test x$c_dir = x"erts" && xcs_dep="$xcs_dep $dir/config.h.in"
	    $MAKE -f $ERL_TOP/make/lazy_configure.mk                         \
		MAKE="$MAKE" TARGET=$TARGET                                  \
		ERL_TOP=$ERL_TOP                                             \
		CONFIGURE_DIR=$dir                                           \
		EXPECTED_AUTOCONF_VERSION=$EXPECTED_AUTOCONF_VERSION         \
		lazy_configure_clean
	    echo "=== Done cleaning configure in $dir"
	    echo ""
	fi
    done

}

do_lazy_configure_target_clean ()
{
    setup_make
    case $TARGET in
	vxworks_*|ose_*)
	    echo "Not supported for target: $TARGET" >&2
	    exit 1
	    ;;
	*)
	    ;;
    esac
    for c_dir in $AUTOCONF_SUBDIRS; do
	if test -f $ERL_TOP/$c_dir/configure.in; then
	    dir=$ERL_TOP/$c_dir
	    echo ""
	    echo "=== Begin target cleaning configure in $dir"
	    xc_dep= ;
	    xcs_dep= ;
	    test -d $dir/$TARGET || mkdir $dir/$TARGET
	    test -f $dir/aclocal.m4 && xc_dep="$xcs_dep $dir/aclocal.m4"
	    test -f $dir/acsite.m4 && xc_dep="$xcs_dep $dir/acsite.m4"
	    test x$c_dir = x"erts" && xcs_dep="$xcs_dep $dir/config.h.in"
	    $MAKE -f $ERL_TOP/make/lazy_configure.mk                         \
		MAKE="$MAKE" TARGET=$TARGET                                  \
		ERL_TOP=$ERL_TOP                                             \
		CONFIGURE_DIR=$dir                                           \
		EXPECTED_AUTOCONF_VERSION=$EXPECTED_AUTOCONF_VERSION         \
		lazy_configure_target_clean
	    echo "=== Done target cleaning configure in $dir"
	    echo ""
	fi
    done

}

echo_setenv ()
{
    case "$DAILY_BUILD_SCRIPT$SHELL" in
	true*)
	    echo "$1=$2";;
	*ash|*ksh|*/sh|*zsh|*ash)
	    echo "$1=\"$2\";export $1$3";;
	*csh)
	    echo "setenv $1 \"$2\"$3";;
    esac
}

echo_env_erltop ()
{
    if [ X"$ERL_TOP" = X"" -o "$ERLTOP_FORCED" = "true" ]; then
	if [ -f ./otp_build ]; then
	    # Seems to be current directory...
	    echo_setenv ERL_TOP `/bin/pwd` ';'
	else
	    echo "You need to either set ERL_TOP first or stand in the same" \ 
		"directory as this script resides in." >&2
	    exit 1
	fi
    fi
}

echo_envinfo ()
{
    case "$SHELL" in
	*csh)
	    return 0
	    ;;
	*)
	    ;;
    esac
    if [ X"$DAILY_BUILD_SCRIPT" = X"true" ]; then
	echo '# Output generated for daily build script only '\
	     '($DAILY_BUILD_SCRIPT=true)'
    else
	echo '# Please note:'
	echo '# The command you are running is supposed to be run'\
	     'using the shells'
	echo '# "eval" builtin, like in:'
	echo '# $ eval `./otp_build env_<something>`'
	echo '# If you see this comment, you probably haven'"'"'t done that.'
    fi
}

echo_env_vxworks ()
{
    if [ -z "$1" ]; then 
	echo "env_vxworks requires CPU architecture as parameter (ppc603, ppc860 etc)." >&2
	exit 1
    fi
    echo_env_erltop
    echo_setenv OVERRIDE_CONFIGURE true ';'
    echo_setenv OVERRIDE_TARGET vxworks_$1
    echo_envinfo
}

echo_env_ose ()
{
    if [ -z "$1" ]; then 
	echo "env_ose requires CPU architecture as parameter (ppc750, sfk etc)." >&2
	exit 1
    fi
    echo_env_erltop
    echo_setenv OVERRIDE_CONFIGURE true ';'
    echo_setenv OVERRIDE_TARGET ose_$1
    echo_envinfo
}

echo_env_win32 ()
{
    #echo_envinfo
    if [ X"$SHELL" = X"" ]; then
	echo "You need to export the shell variable first," \
		"for bourne-like shells, type:" >&2
	echo 'export SHELL' >&2
	echo "and for csh-like shells, type:" >&2
	echo 'setenv SHELL $SHELL' >&2
	echo " - then try again." >&2
	exit 1
    fi
    echo_env_erltop
    P2=`echo $PATH | \
	sed 's,",,g;s,:[cC]:,:/cygdrive/c,g;s,:[dD]:,:/cygdrive/d,g'`
    P3=""
    save_ifs=$IFS
    IFS=:
    for p in $P2; do
	if [ -d "$p" ]; then
	    C1="`(cygpath -d $p 2>/dev/null || cygpath -w $p)`" 2> /dev/null
	    C2=`cygpath "$C1" 2> /dev/null` 2> /dev/null
	else
	    C2=""
	fi
	if [ ! -z "$C2" ]; then
	    if [ -z "$P3" ];then 
		P3="$C2"
	    else 
		P3="$P3:$C2"
	    fi
	fi
    done
    IFS=$save_ifs

    echo_setenv OVERRIDE_TARGET win32 ';'
    echo_setenv CC cc.sh ';'
    echo_setenv CXX cc.sh ';'
    echo_setenv AR ar.sh ';'
    echo_setenv RANLIB true ';'
    echo_setenv PATH "$ERL_TOP/erts/etc/win32/cygwin_tools/vc:$ERL_TOP/erts/etc/win32/cygwin_tools:$P3"
    echo_envinfo
}

echo_env_mingw32 ()
{
    #echo_envinfo
    if [ X"$SHELL" = X"" ]; then
	echo "You need to export the shell variable first," \
		"for bourne-like shells, type:" >&2
	echo 'export SHELL' >&2
	echo "and for csh-like shells, type:" >&2
	echo 'setenv SHELL $SHELL' >&2
	echo " - then try again." >&2
	exit 1
    fi
    echo_env_erltop
    P2=`echo $PATH | \
	sed 's,",,g;s,:[cC]:,:/cygdrive/c,g;s,:[dD]:,:/cygdrive/d,g'`
    P3=""
    save_ifs=$IFS
    IFS=:
    for p in $P2; do
	if [ -d "$p" ]; then
	    C1="`(cygpath -d $p 2>/dev/null || cygpath -w $p)`" 2> /dev/null
	    C2=`cygpath "$C1" 2> /dev/null` 2> /dev/null
	else
	    C2=""
	fi
	if [ ! -z "$C2" ]; then
	    if [ -z "$P3" ];then 
		P3="$C2"
	    else 
		P3="$P3:$C2"
	    fi
	fi
    done
    found=false
    for p in $P3; do
	if [ -f "$p/mingw32-gcc.exe" ]; then
	    found=$p
	fi
    done
    found2=false
    for p in $P3; do
	if [ -f "$p/wmc.exe" ]; then
	    found2=$p
	fi
    done
    IFS=$save_ifs
    if [ X"$found" = X"false" ]; then
	echo "Could not find mingw32-gcc in PATH, build with mingw not possible!" >&2
	return
    fi

    if [ X"$found2" = X"false" ]; then
	echo "Could not find wmc.exe in PATH, part of wine for windows, " >&2
	echo "needed for message file compilation: http://wine.sourceforge.net!!" >&2
	return
    fi


    echo_setenv OVERRIDE_TARGET win32 ';'
    echo_setenv MINGW_EXE_PATH $found ';'
    echo_setenv WINE_EXE_PATH $found2 ';'
    echo_setenv CC cc.sh ';'
    echo_setenv CXX cc.sh ';'
    echo_setenv AR ar.sh ';'
    echo_setenv RANLIB true ';'
    echo_setenv PATH "$ERL_TOP/erts/etc/win32/cygwin_tools/mingw:$ERL_TOP/erts/etc/win32/cygwin_tools:$P3"
    echo_envinfo
}

lookup_prog_in_path ()
{
    PROG=$1
    save_ifs=$IFS
    IFS=:
    for p in $PATH; do
	# In cygwin the programs are not always executable and have .exe suffix...
	if [ "X$TARGET" = "Xwin32" ]; then
	    if [ -f $p/$PROG.exe ]; then
		echo $p/$PROG
		break;
	    fi
	else
	    if [ -x $p/$PROG ]; then
		echo $p/$PROG
		break;
	    fi
	fi
    done
    IFS=$save_ifs
}

setup_make ()
{
    if [ -z "$MAKE" ]; then
	case $TARGET in
	    win32)
		MAKE=make;;
	    *)
		if [ "X$CLEARCASE_MAKE_COMPAT" = "Xgnu" -a \
		     X"$CLEARCASE_ROOT" != X"" -a \
		    -n "`lookup_prog_in_path clearmake`"  ]; then
		    clearmake="clearmake -V"
		    MAKE=$clearmake
		else 
		    if [ -n "`lookup_prog_in_path gmake`" ]; then
			MAKE=gmake
		    else
			MAKE=make
		    fi
		fi;;
       esac
    fi
    export MAKE
}
    
do_noboot_lib_and_erts ()
{
    setup_make
    EMULATORS=emulator
    if [ "x$MAKE" != "x$clearmake" ]; then
	$MAKE MAKE="$MAKE" TARGET=$TARGET OTP_SMALL_BUILD=$OTP_SMALL_BUILD depend || exit 1;
    fi
    $MAKE MAKE="$MAKE" TARGET=$TARGET OTP_SMALL_BUILD=$OTP_SMALL_BUILD TYPE=$TYPE FLAVOR=$FLAVOR noboot || exit 1
}

do_primary_cross ()
{
    save_t="$TARGET"
    save_ot="$OVERRIDE_TARGET"
    OVERRIDE_TARGET=
    export OVERRIDE_TARGET
    TARGET=`$ERL_TOP/erts/autoconf/config.guess`
    $MAKE MAKE="$MAKE" BOOTSTRAP_ROOT=$BOOTSTRAP_ROOT TARGET=$TARGET primary_bootstrap || exit 1;
    $MAKE MAKE="$MAKE" BOOTSTRAP_ROOT=$BOOTSTRAP_ROOT TARGET=$TARGET all_bootstraps || exit 1
    OVERRIDE_TARGET="$save_ot"
    export OVERRIDE_TARGET
    TARGET="$save_t"
}

do_primary ()
{
    setup_make
    case $TARGET in
	vxworks_*)
	    do_primary_cross;;
	ose_*)
	    do_primary_cross;;
	*)
	    $MAKE MAKE="$MAKE" BOOTSTRAP_ROOT=$BOOTSTRAP_ROOT TARGET=$TARGET primary_bootstrap || exit 1;
    esac
}

do_boot_cross ()
{
    $MAKE MAKE="$MAKE" BOOTSTRAP_ROOT=$BOOTSTRAP_ROOT TARGET=$TARGET depend || exit 1;
    $MAKE MAKE="$MAKE" BOOTSTRAP_ROOT=$BOOTSTRAP_ROOT TARGET=$TARGET emulator || exit 1;
    $MAKE MAKE="$MAKE" BOOTSTRAP_ROOT=$BOOTSTRAP_ROOT TARGET=$TARGET libs || exit 1;
}

do_boot_emu_cross ()
{
    $MAKE MAKE="$MAKE" BOOTSTRAP_ROOT=$BOOTSTRAP_ROOT TARGET=$TARGET emulator || exit 1;
}

do_boot ()
{
    setup_make
    case $TARGET in
	vxworks_*)
	    do_boot_cross;;
        ose_*)
	    do_boot_cross;;
	*)
	    $MAKE MAKE="$MAKE" BOOTSTRAP_ROOT=$BOOTSTRAP_ROOT TARGET=$TARGET all || exit 1;;
    esac
}

do_boot_emu ()
{
    setup_make
    case $TARGET in
	vxworks_*)
	    do_boot_emu_cross;;
        ose_*)
	    do_boot_emu_cross;;
	*)
	    $MAKE MAKE="$MAKE" BOOTSTRAP_ROOT=$BOOTSTRAP_ROOT TARGET=$TARGET emulator || exit 1;;
    esac
}

do_release ()
{
    setup_make
    $MAKE MAKE="$MAKE" BOOTSTRAP_ROOT=$BOOTSTRAP_ROOT TARGET=$TARGET RELEASE_ROOT=$1 release || exit 1
}

do_tests ()
{
    setup_make
    if [ X"$1" = X"" ]; then
	$MAKE MAKE="$MAKE" TARGET=$TARGET release_tests || exit 1
    else
	$MAKE MAKE="$MAKE" TARGET=$TARGET TESTSUITE_ROOT=$1 release_tests || exit 1
    fi
}

do_debuginfo_win32 ()
{
    setup_make
    (cd erts/emulator && $MAKE MAKE="$MAKE" TARGET=$TARGET FLAVOR=smp debug &&\
	$MAKE MAKE="$MAKE" TARGET=$TARGET FLAVOR=plain debug) || exit 1
    if [ -z "$1" ]; then
	RELDIR=$ERL_TOP/release/$TARGET
    else
	RELDIR="$1"
    fi
    BINDIR=$ERL_TOP/bin/$TARGET
    EVSN=`cat erts/vsn.mk | sed 's,^VSN.*=[^0-9]*\([0-9].*\)$,@\1,g;s,^[^@].*,,g;s,^@,,g'`
    for f in beam.debug.dll beam.debug.smp.dll beam.pdb beam.smp.pdb erl.pdb werl.pdb erlexec.pdb; do
	if [ -f $BINDIR/$f ]; then
	    rm -f $RELDIR/erts-$EVSN/bin/$f
	    cp $BINDIR/$f $RELDIR/erts-$EVSN/bin/$f
	fi
    done
}

do_installer_win32 ()
{
    setup_make
    installer_dir=$ERL_TOP/erts/etc/win32/nsis
    (cd $installer_dir; $MAKE MAKE="$MAKE" TARGET=$TARGET TESTROOT=$1 release) || exit 1
}

do_copy_primary_bootstrap ()
{
    if [ "x$1" = "x" ]; then
	echo "Missing bootstrap source top" 1>&2
	exit 1
    fi
    if  [ ! -d $1 ]; then
	echo "Invalid bootstrap source top" 1>&2
	exit 1
    fi
    if [ "x$2" = "x" ]; then
	echo "Missing bootstrap root" 1>&2
	exit 1
    fi
    if  [ ! -d $2 ]; then
	echo "Invalid bootstrap root" 1>&2
	exit 1
    fi

    bootstrap=$2/bootstrap
    bootstrap_src_top=$1
    lib_src=$bootstrap_src_top/lib

    # kernel
    test -d $bootstrap/lib/kernel/ebin || mkdir -p  $bootstrap/lib/kernel/ebin
    test -d $bootstrap/lib/kernel/include || mkdir -p  $bootstrap/lib/kernel/include
    cp -f $lib_src/kernel/ebin/*.beam $bootstrap/lib/kernel/ebin
    cp -f $lib_src/kernel/ebin/*.app* $bootstrap/lib/kernel/ebin
    cp -f $lib_src/kernel/include/*.hrl $bootstrap/lib/kernel/include

    # stdlib
    test -d $bootstrap/lib/stdlib/ebin || mkdir -p  $bootstrap/lib/stdlib/ebin
    test -d $bootstrap/lib/stdlib/include || mkdir -p  $bootstrap/lib/stdlib/include
    cp -f $lib_src/stdlib/ebin/*.beam $bootstrap/lib/stdlib/ebin
    cp -f $lib_src/stdlib/ebin/*.app* $bootstrap/lib/stdlib/ebin
    cp -f $lib_src/stdlib/include/*.hrl $bootstrap/lib/stdlib/include

    # compiler
    test -d $bootstrap/lib/compiler/ebin || mkdir -p  $bootstrap/lib/compiler/ebin
    cp -f $lib_src/compiler/ebin/*.beam $bootstrap/lib/compiler/ebin
    cp -f $lib_src/compiler/ebin/*.app* $bootstrap/lib/compiler/ebin

    # orber include
    test -d $bootstrap/lib/orber/include || mkdir -p  $bootstrap/lib/orber/include
    cp -f $lib_src/orber/include/* $bootstrap/lib/orber/include

    # bootstrap bin
    if [ $bootstrap_src_top != $ERL_TOP ]; then
	test -d $bootstrap/bin || mkdir -p  $bootstrap/bin
	cp -f $bootstrap_src_top/bin/* $bootstrap/bin
    fi

}

do_save_bootstrap ()
{
    if [ ! -f $ERL_TOP/prebuilt.files ]; then
	echo "This is not a pre-built source distribution" 1>&2
	exit 1
    fi
    if  [ -d $ERL_TOP/bootstrap/lib ]; then
	echo "Bootstrap already exist" 1>&2
	exit 1
    fi

    do_copy_primary_bootstrap $ERL_TOP $ERL_TOP
}

do_remove_prebuilt_files ()
{
    do_save_bootstrap
    for file in $ERL_TOP/`cat $ERL_TOP/prebuilt.files` ; do
	rm -f $file
    done
}

# main

check_erltop

cd $ERL_TOP

# Unset ERL_FLAGS and ERL_<Release>_FLAGS to prevent, for instance,
# a value of "-hybrid" to run the hybrid emulator during bootstrap.
sys_vsn=`awk '/SYSTEM_VSN = / {print $3}' < erts/vsn.mk`
sys_erl_flags="ERL_${sys_vsn}_FLAGS"
unset ERL_FLAGS
unset ${sys_erl_flags}

# Target first guess, won't necessarily hold, may be changed for 
# certain parameters.
if [ X"$TARGET" = X"" ]; then
	TARGET=`$ERL_TOP/erts/autoconf/config.guess`
fi
BUILDSYS=$TARGET

case $TARGET in
    *-cygwin)
	if [ X"$BUILD_FOR_CYGWIN" = X"" ]; then
	    if [ X"$OVERRIDE_TARGET" = X"" -a X"$1" != X"env_win32" -a  X"$1" != X"env_mingw32" ];then
		echo "Building for windows, you should do the " \
		    "following first:" >&2
		echo 'eval `./otp_build env_win32`' >&2
		echo 'please note that there are backticks (``) in' \
		    'the command'
		exit 1
	    fi
	fi;;
	*)
	    ;;
esac

if [ ! -z "$OVERRIDE_TARGET" ]; then
    TARGET="$OVERRIDE_TARGET"
fi
if [ -z "$BOOTSTRAP_ROOT" ]; then
    BOOTSTRAP_ROOT="$ERL_TOP"
fi

if [ X"$1" = X"" ]; then 
	usage
	exit 1
fi

if [ X"$2" = X"-a" ]; then
    minus_a_flag=true
    OTP_SMALL_BUILD=
else
    OTP_SMALL_BUILD=true
    minus_a_flag=false
fi
export OTP_SMALL_BUILD

TYPE=
case "$1" in
	all)
		do_autoconf; 
		do_configure; 
		do_primary;
		do_boot;
		if [ $minus_a_flag = true ]; then 
		    shift
		fi;
		do_release "$2";;
	setup)
		shift;
		if [ $minus_a_flag = true ]; then 
		    shift
		fi;
		do_autoconf; 
		do_configure "$@";
		do_noboot_lib_and_erts;;
	lazy_setup)
		shift;
		if [ $minus_a_flag = true ]; then 
		    shift
		fi;
		do_lazy_configure "$@";
		do_noboot_lib_and_erts;;
	autoconf)
		do_autoconf;;
	configure)
		shift;
		do_configure "$@";;
	lazy_configure)
		shift;
		do_lazy_configure "$@";;
	lazy_configure_clean)
		shift;
		do_lazy_configure_clean;;
	lazy_configure_target_clean)
		shift;
		do_lazy_configure_target_clean;;
	opt)
		do_noboot_lib_and_erts;;
	plain|smp|hybrid|nofrag)
		if [ $minus_a_flag = false ]; then 
		    TYPE=opt
		fi;
		FLAVOR=$1
		if [ $FLAVOR = opt ]; then
		    FLAVOR=plain
		fi
		do_noboot_lib_and_erts;;
	primary)
		do_primary;;
	boot)
		do_boot;;
        emulator)
	        do_boot_emu;;
	release)
		if [ $minus_a_flag = true ]; then 
		    shift
		fi;
		do_release "$2";;
	tests)
		if [ $minus_a_flag = true ]; then 
		    shift
		fi;
		do_tests "$2";;
        remove_prebuilt_files)
		do_remove_prebuilt_files;;
        save_bootstrap)
		do_save_bootstrap;;
        copy_primary_bootstrap)
		do_copy_primary_bootstrap $2 $3;;
	installer_win32)
		if [ $minus_a_flag = true ]; then 
		    shift
		fi;
		do_installer_win32 "$2";;
	debuginfo_win32)
		if [ $minus_a_flag = true ]; then 
		    shift
		fi;
		do_debuginfo_win32 "$2";;
	env_win32)
		echo_env_win32;;
	env_mingw32)
		echo_env_mingw32;;
	env_vxworks)
		echo_env_vxworks "$2";;
	env_ose)
		echo_env_ose "$2";;
	*)
		usage;;
esac