summaryrefslogtreecommitdiff
path: root/Makefile.in
blob: 544233f0973d86b878b874e9d377790855b33814 (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
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
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
#
# %CopyrightBegin%
#
# Copyright Ericsson AB 1998-2012. All Rights Reserved.
#
# The contents of this file are subject to the Erlang Public License,
# Version 1.1, (the "License"); you may not use this file except in
# compliance with the License. You should have received a copy of the
# Erlang Public License along with this software. If not, it can be
# retrieved online at http://www.erlang.org/.
#
# Software distributed under the License is distributed on an "AS IS"
# basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
# the License for the specific language governing rights and limitations
# under the License.
#
# %CopyrightEnd%

# Toplevel makefile for building the Erlang system
#

.NOTPARALLEL:

# ----------------------------------------------------------------------

# And you'd think that this would be obvious... :-)
SHELL = /bin/sh

# The top directory in which Erlang is unpacked
ERL_TOP = @ERL_TOP@

# OTP release
OTP = OTP-@OTP_REL@

# erts (Erlang RunTime System) version
ERTS = erts-@ERTS_VSN@

# ----------------------------------------------------------------------

#
# The variables below control where Erlang is installed. They are
# configurable (unless otherwise stated). Some of them are best
# changed by giving special arguments to configure instead of changing
# them in this file. Note: If you change them in Makefile, instead of
# Makefile.in your changes will be lost the next time you run
# configure.
#

# prefix from configure, default is /usr/local (must be an absolute path)
prefix		= @prefix@
exec_prefix	= @exec_prefix@

# Locations where we should install according to configure. These location
# may be prefixed by $(DESTDIR) and/or $(EXTRA_PREFIX) (see below).
bindir		= @bindir@
libdir		= @libdir@

# Where Erlang/OTP is located
libdir_suffix	= /erlang
erlang_libdir	= $(libdir)$(libdir_suffix)
erlang_bindir	= $(erlang_libdir)/bin

#
# By default we install relative symbolic links for $(ERL_BASE_PUB_FILES)
# from $(bindir) to $(erlang_bindir) as long as they are both prefixed
# by $(exec_prefix) (and are otherwise reasonable). This behavior can be
# overridden by passing BINDIR_SYMLINKS=<HOW>, where <HOW> is either
# absolute or relative.
#

# $ make DESTDIR=<...> install
#
# DESTDIR can be set in case you want to install Erlang in a different
# location than where you have configured it to run. This can be
# useful, e.g. when installing on a server that stores the files with a
# different path than where the clients access them, when building
# rpms, or cross compiling, etc. DESTDIR will prefix the actual
# installation which will only be able to run once the DESTDIR prefix
# has disappeard, e.g. the part after DESTDIR has been packed and
# unpacked without DESTDIR. The name DESTDIR have been chosen since it
# is the GNU coding standard way of doing it.
#
# If INSTALL_PREFIX is set but not DESTDIR, DESTDIR will be set to
# INSTALL_PREFIX. INSTALL_PREFIX has been buggy for a long time. It was
# initially intended to have the same effect as DESTDIR. This effect was,
# however, lost even before it was first released :-( In all released OTP
# versions up to R13B03, INSTALL_PREFIX has behaved as EXTRA_PREFIX do
# today (see below).

ifeq ($(DESTDIR),)
ifneq ($(INSTALL_PREFIX),)
DESTDIR=$(INSTALL_PREFIX)
endif
else
ifneq ($(INSTALL_PREFIX),)
ifneq ($(DESTDIR),$(INSTALL_PREFIX))
$(error Both DESTDIR="$(DESTDIR)" and INSTALL_PREFIX="$(INSTALL_PREFIX)" have been set and have been set differently! Please, only set one of them)
endif
endif
endif

# $ make EXTRA_PREFIX=<...> install
#
# EXTRA_PREFIX behaves exactly as the buggy INSTALL_PREFIX behaved in
# pre R13B04 releases. It adds a prefix to all installation paths which
# will be used by the actuall installation. That is, the installation
# needs to be located at this location when run. This is useful if you
# want to try out the system, running test suites, etc, before doing the
# real install using the configuration you have set up using `configure'.
# A similar thing can be done by overriding `prefix' if only default
# installation directories are used. However, the installation can get
# sprawled out all over the place if the user use `--bindir', `--libdir',
# etc, and it is possible that `prefix' wont have any effect at all. That
# is, it is not at all the same thing as using EXTRA_PREFIX in the
# general case. It is also nice to be able to supply this feature if
# someone should have relied on the old buggy INSTALL_PREFIX.

# The directory in which user executables (ERL_BASE_PUB_FILES) are installed
BINDIR      = $(DESTDIR)$(EXTRA_PREFIX)$(bindir)

#
# Erlang base public files
#
ERL_BASE_PUB_FILES=erl erlc epmd run_erl to_erl dialyzer typer escript ct_run run_test

# ERLANG_INST_LIBDIR is the top directory where the Erlang installation
# will be located when running.
ERLANG_INST_LIBDIR=$(EXTRA_PREFIX)$(erlang_libdir)
ERLANG_INST_BINDIR= $(ERLANG_INST_LIBDIR)/bin

# ERLANG_LIBDIR is the top directory where the Erlang installation is copied
# during installation. If DESTDIR != "", it cannot be run from this location.
ERLANG_LIBDIR     = $(DESTDIR)$(ERLANG_INST_LIBDIR)

# ----------------------------------------------------------------------
# This functionality has been lost along the way... :(
# It could perhaps be nice to reintroduce some day; therefore,
# it is not removed just commented out.

## # The directory in which man pages for above executables are put
## ERL_MAN1DIR      = $(DESTDIR)$(EXTRA_PREFIX)@mandir@/man1
## ERL_MAN1EXT      = 1

## # The directory in which Erlang private man pages are put. In order
## # not to clutter up the man namespace these are by default put in the
## # Erlang private directory $(ERLANG_LIBDIR)/man (\@erl_mandir\@ is set
## # to $(erlang_libdir)/man). If you want to install the man pages
## # together with the rest give the argument "--disable-erlang-mandir"
## # when you run configure, which will set \@erl_mandir\@ to \@mandir\@.
## #   If you want a special suffix on the manpages set ERL_MANEXT to
## # this suffix, e.g. "erl"
## ERL_MANDIR       = $(DESTDIR)$(EXTRA_PREFIX)@erl_mandir@
## ERL_MANEXT       =

# ----------------------------------------------------------------------

# Must be GNU make!
MAKE		= @MAKE_PROG@

NATIVE_LIBS_ENABLED = @NATIVE_LIBS_ENABLED@

ifeq ($(NATIVE_LIBS_ENABLED),yes)
HIPE_BOOTSTRAP_EBIN = boot_ebin
else
HIPE_BOOTSTRAP_EBIN = ebin
endif

# This should be set to the target "arch-vendor-os"
TARGET	:= @TARGET@
include $(ERL_TOP)/make/target.mk
export TARGET

BOOTSTRAP_ONLY = @BOOTSTRAP_ONLY@

CROSS_COMPILING = @CROSS_COMPILING@
ifeq ($(CROSS_COMPILING),yes)
INSTALL_CROSS = -cross
TARGET_HOST=$(shell $(ERL_TOP)/erts/autoconf/config.guess)
else
ifneq ($(DESTDIR),)
INSTALL_CROSS = -cross
else
INSTALL_CROSS = 
endif
TARGET_HOST=
endif

# A BSD compatible install program
INSTALL         = @INSTALL@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
INSTALL_DATA    = @INSTALL_DATA@
MKSUBDIRS       = ${INSTALL} -d

# Program to create symbolic links
LN_S            = @LN_S@

# Ranlib program, if not needed set to e.g. ":"
RANLIB          = @RANLIB@

# ----------------------------------------------------------------------

# By default we require an Erlang/OTP of the same release as the one
# we cross compile.
ERL_XCOMP_FORCE_DIFFERENT_OTP = no

# ----------------------------------------------------------------------

#
# The directory where at least the primary bootstrap is placed under.
#
# We need to build to view private files in case we are in clearcase;
# therefore, we don't want BOOTSTRAP_TOP changed.
#
# PRIMARY_BOOTSTRAP_TOP would perhaps have been a better name...
#
override BOOTSTRAP_TOP = $(ERL_TOP)/bootstrap
# BOOTSTRAP_SRC_TOP is normally the same as BOOTSTRAP_TOP but
# it is allowed to be changed
BOOTSTRAP_SRC_TOP = $(BOOTSTRAP_TOP)

# Where to install the bootstrap directory.
#
# Typically one might want to set this to a fast local filesystem, or,
# the default, as ERL_TOP
BOOTSTRAP_ROOT = $(ERL_TOP)

# Directories which you need in the path if you wish to run the
# locally built system. (This can be put in front or back of the path
# depending on which system is preferred.)
LOCAL_PATH     = $(ERL_TOP)/erts/bin/$(TARGET):$(ERL_TOP)/erts/bin
ifeq ($(TARGET),win32)
BOOT_PREFIX=$(WIN32_WRAPPER_PATH):$(BOOTSTRAP_ROOT)/bootstrap/bin:
TEST_PATH_PREFIX=$(WIN32_WRAPPER_PATH):$(ERL_TOP)/bin/win32:
else
BOOT_PREFIX=$(BOOTSTRAP_ROOT)/bootstrap/bin:
TEST_PATH_PREFIX=$(ERL_TOP)/bin/$(TARGET_HOST):
endif

# ----------------------------------------------------------------------

# The following is currently only used for determining what to prevent
# usage of during strict install or release.
include $(ERL_TOP)/make/$(TARGET)/otp_ded.mk
CC	= @CC@
LD	= @LD@
CXX	= @CXX@

IBIN_DIR	= $(ERL_TOP)/ibin
#
# If $(OTP_STRICT_INSTALL) equals `yes' we prefix the PATH with $(IBIN_DIR)
# when doing `release' or `install'. This directory contains `erlc', `gcc',
# `ld' etc, that unconditionally will fail if used. This is used during the
# daily builds in order to pick up on things being erroneously built during
# the `release' and `install' phases.
#
INST_FORBID	= gcc g++ cc c++ cxx cl gcc.sh cc.sh ld ld.sh 
INST_FORBID	+= javac.sh javac guavac gcj jikes bock
INST_FORBID	+= $(notdir $(CC)) $(notdir $(LD)) $(notdir $(CXX))
INST_FORBID	+= $(notdir $(DED_CC)) $(notdir $(DED_LD))
INST_FORBID 	+= $(ERL_BASE_PUB_FILES)
IBIN_FILES	= $(addprefix $(IBIN_DIR)/,$(sort $(INST_FORBID))) # sort will
                                                                   # remove
                                                                   # duplicates

ifeq ($(OTP_STRICT_INSTALL),yes)

INST_PATH_PREFIX=$(IBIN_DIR):
INST_DEP	= strict_install
ifneq ($(CROSS_COMPILING),yes)
INST_DEP	+= strict_install_all_bootstraps
endif

else # --- Normal case, i.e., not strict install ---

#
# By default we allow build during install and release phase; therefore,
# make sure that the bootstrap system is available in the path.
#
INST_PATH_PREFIX=$(BOOT_PREFIX)
# If cross compiling `erlc', in path might have be used; therefore,
# avoid triggering a bootstrap build...
INST_DEP	=
ifneq ($(CROSS_COMPILING),yes)
INST_DEP	+= all_bootstraps
endif

endif # --- Normal case, i.e., not strict install ---

# ----------------------------------------------------------------------
# Fix up RELEASE_ROOT/TESTROOT havoc
ifeq ($(RELEASE_ROOT),)
ifneq ($(TESTROOT),)
RELEASE_ROOT = $(TESTROOT)
endif
endif


# ----------------------------------------------------------------------

# A default for the release_tests, not same target dir as release.
# More TESTROOT havoc...
ifeq ($(TESTSUITE_ROOT),)
ifneq ($(TESTROOT),)
TESTSUITE_ROOT = $(TESTROOT)
else
TESTSUITE_ROOT = $(ERL_TOP)/release/tests
endif
endif

#
# The steps to build a working system are:
#   * build an emulator
#   * setup the erl and erlc program in bootstrap/bin
#   * build additional compilers and copy them into bootstrap/lib
#   * use the bootstrap erl and erlc to build all the libs
#

.PHONY: all bootstrap all_bootstraps

ifneq ($(CROSS_COMPILING),yes)
# Not cross compiling

ifeq ($(BOOTSTRAP_ONLY),yes)
all: bootstrap
else
# The normal case; not cross compiling, and not bootstrap only build.
all: bootstrap libs local_setup
endif

else
# Cross compiling

all: cross_check_erl depend emulator libs start_scripts

endif

cross_check_erl:
	@PATH=$(BOOT_PREFIX)"$${PATH}" $(ERL_TOP)/make/cross_check_erl \
           -target $(TARGET) -otp $(OTP) -erl_top $(ERL_TOP) \
           -force $(ERL_XCOMP_FORCE_DIFFERENT_OTP)

is_cross_configured:
	@echo @CROSS_COMPILING@

target_configured:
	@echo @TARGET@

bootstrap: depend all_bootstraps



ifeq ($(OTP_STRICT_INSTALL),yes)

.PHONY: strict_install_all_bootstraps

strict_install_all_bootstraps:
	$(MAKE) BOOT_PREFIX=$(INST_PATH_PREFIX) OTP_STRICT_INSTALL=$(OTP_STRICT_INSTALL) all_bootstraps

endif

# With all bootstraps we mean all bootstrapping that is done when
# the system is delivered in open source, the primary
# bootstrap is not included, it requires a pre built emulator...
all_bootstraps: emulator \
     bootstrap_setup \
     secondary_bootstrap_build secondary_bootstrap_copy \
     tertiary_bootstrap_build tertiary_bootstrap_copy

#
# Use these targets when you want to use the erl and erlc
# binaries in your PATH instead of those created from the
# pre-compiled Erlang modules under bootstrap/.
#
noboot:
	$(MAKE) BOOT_PREFIX= emulator libs local_setup

noboot_install:
	$(MAKE) BOOT_PREFIX= install

.PHONY: release release_docs

release: $(INST_DEP)
ifeq ($(OTP_SMALL_BUILD),true)
	cd $(ERL_TOP)/lib && \
	  ERL_TOP=$(ERL_TOP) PATH=$(INST_PATH_PREFIX)"$${PATH}" \
	    $(MAKE) TESTROOT="$(RELEASE_ROOT)" release
else
	cd $(ERL_TOP)/lib && \
	  ERL_TOP=$(ERL_TOP) PATH=$(INST_PATH_PREFIX)"$${PATH}" \
	    $(MAKE) BUILD_ALL=1 TESTROOT="$(RELEASE_ROOT)" release
endif
	cd $(ERL_TOP)/erts && \
	  ERL_TOP=$(ERL_TOP) PATH=$(INST_PATH_PREFIX)"$${PATH}" \
	    $(MAKE) BUILD_ALL=1 TESTROOT="$(RELEASE_ROOT)" release

# ---------------------------------------------------------------
# Target only used when building commercial ERTS patches
# ---------------------------------------------------------------
release_docs docs: mod2app
ifeq ($(OTP_SMALL_BUILD),true)
	cd $(ERL_TOP)/lib && \
	  PATH=$(ERL_TOP)/bin:"$${PATH}" ERL_TOP=$(ERL_TOP) \
	  $(MAKE) TESTROOT="$(RELEASE_ROOT)" $@
else
	cd $(ERL_TOP)/lib && \
	  PATH=$(ERL_TOP)/bin:"$${PATH}" ERL_TOP=$(ERL_TOP) \
	  $(MAKE) BUILD_ALL=1 TESTROOT="$(RELEASE_ROOT)" $@
endif
	cd $(ERL_TOP)/erts && \
	  PATH=$(ERL_TOP)/bin:"$${PATH}" ERL_TOP=$(ERL_TOP) \
	  $(MAKE) BUILD_ALL=1 TESTROOT="$(RELEASE_ROOT)" $@
	cd $(ERL_TOP)/system/doc && \
	  PATH=$(ERL_TOP)/bin:"$${PATH}" \
	  ERL_TOP=$(ERL_TOP) $(MAKE) TESTROOT="$(RELEASE_ROOT)" $@

mod2app: 
	PATH=$(ERL_TOP)/bin:"$${PATH}" escript $(ERL_TOP)/lib/erl_docgen/priv/bin/xref_mod_app.escript -topdir $(ERL_TOP) -outfile $(ERL_TOP)/make/$(TARGET)/mod2app.xml

# ----------------------------------------------------------------------
ERLANG_EARS=$(BOOTSTRAP_ROOT)/bootstrap/erts
ELINK=$(BOOTSTRAP_ROOT)/bootstrap/erts/bin/elink
BOOT_BINDIR=$(BOOTSTRAP_ROOT)/bootstrap/erts/bin
BEAM_EVM=$(ERL_TOP)/bin/$(TARGET)/beam_evm
BOOTSTRAP_COMPILER  =  $(BOOTSTRAP_TOP)/primary_compiler

.PHONY: emulator libs kernel stdlib compiler hipe typer syntax_tools preloaded

emulator:
	cd erts && ERL_TOP=$(ERL_TOP) $(MAKE) NO_START_SCRIPTS=true $(TYPE) FLAVOR=$(FLAVOR)

libs:
ifeq ($(OTP_SMALL_BUILD),true)
	cd lib && \
	  ERL_TOP=$(ERL_TOP) PATH=$(BOOT_PREFIX)"$${PATH}" \
		$(MAKE) opt
else
	cd lib && \
	  ERL_TOP=$(ERL_TOP) PATH=$(BOOT_PREFIX)"$${PATH}" \
		$(MAKE) opt BUILD_ALL=true
endif
kernel:
	cd lib/kernel && \
	  ERL_TOP=$(ERL_TOP) PATH=$(BOOT_PREFIX)"$${PATH}" \
		$(MAKE) opt BUILD_ALL=true

stdlib:
	cd lib/stdlib && \
	  ERL_TOP=$(ERL_TOP) PATH=$(BOOT_PREFIX)"$${PATH}" \
		$(MAKE) opt BUILD_ALL=true

compiler:
	cd lib/compiler && \
	  ERL_TOP=$(ERL_TOP) PATH=$(BOOT_PREFIX)"$${PATH}" \
		$(MAKE) opt BUILD_ALL=true

hipe:
	cd lib/hipe && \
	  ERL_TOP=$(ERL_TOP) PATH=$(BOOT_PREFIX)"$${PATH}" \
		$(MAKE) opt BUILD_ALL=true

typer:
	cd lib/typer && \
	ERL_TOP=$(ERL_TOP) PATH=$(BOOT_PREFIX)"$${PATH}" \
	    $(MAKE) opt BUILD_ALL=true

syntax_tools:
	cd lib/syntax_tools && \
	ERL_TOP=$(ERL_TOP) PATH=$(BOOT_PREFIX)"$${PATH}" \
	    $(MAKE) opt BUILD_ALL=true

preloaded:
	cd erts/preloaded/src && \
	ERL_TOP=$(ERL_TOP) PATH=$(BOOT_PREFIX)"$${PATH}" \
		$(MAKE) opt BUILD_ALL=true	

dep depend:
	test X"$$ERTS_SKIP_DEPEND" = X"true" || (cd erts/emulator && ERL_TOP=$(ERL_TOP) $(MAKE) generate)
	test X"$$ERTS_SKIP_DEPEND" = X"true" || (cd erts/emulator && ERL_TOP=$(ERL_TOP) $(MAKE) depend)
	test X"$$ERTS_SKIP_DEPEND" = X"true" || (cd erts/lib_src && ERL_TOP=$(ERL_TOP) $(MAKE) depend)

# Creates "erl" and "erlc" in bootstrap/bin which uses the precompiled 
# libraries in the bootstrap directory

.PHONY: bootstrap_setup_target

# ----------------------------------------------------------------------
# Bootstraps... 
# ----------------------------------------------------------------------
ifeq ($(TARGET),win32)
bootstrap_setup: check_recreate_primary_bootstrap bootstrap_setup_target
	@rm -f $(BOOTSTRAP_ROOT)/bootstrap/bin/erl.exe \
		$(BOOTSTRAP_ROOT)/bootstrap/bin/erlc.exe \
		$(BOOTSTRAP_ROOT)/bootstrap/bin/escript.exe \
		$(BOOTSTRAP_ROOT)/bootstrap/bin/erl.ini \
		$(BOOTSTRAP_ROOT)/bootstrap/bin/beam.dll
	make_bootstrap_ini.sh $(BOOTSTRAP_ROOT)/bootstrap \
		$(ERL_TOP)/bin/$(TARGET)
	@cp $(ERL_TOP)/bin/$(TARGET)/erlc.exe \
		$(BOOTSTRAP_ROOT)/bootstrap/bin/erlc.exe
	@cp $(ERL_TOP)/bin/$(TARGET)/erl.exe \
		$(BOOTSTRAP_ROOT)/bootstrap/bin/erl.exe
	@cp $(ERL_TOP)/bin/$(TARGET)/escript.exe \
		$(BOOTSTRAP_ROOT)/bootstrap/bin/escript.exe
else
bootstrap_setup: check_recreate_primary_bootstrap bootstrap_setup_target $(BOOTSTRAP_ROOT)/bootstrap/bin/erl $(BOOTSTRAP_ROOT)/bootstrap/bin/erlc $(BOOTSTRAP_ROOT)/bootstrap/bin/escript

$(BOOTSTRAP_ROOT)/bootstrap/bin/erl: $(ERL_TOP)/erts/etc/unix/erl.src.src $(BOOTSTRAP_ROOT)/bootstrap/target
	@rm -f $(BOOTSTRAP_ROOT)/bootstrap/bin/erl 
	@sed	-e "s;%FINAL_ROOTDIR%;$(BOOTSTRAP_ROOT)/bootstrap;"   \
		-e "s;\$$ROOTDIR/erts-.*/bin;$(ERL_TOP)/bin/$(TARGET);"    \
		-e "s;EMU=.*;EMU=beam$(TYPEMARKER);" \
	        $(ERL_TOP)/erts/etc/unix/erl.src.src > \
			$(BOOTSTRAP_ROOT)/bootstrap/bin/erl
	@chmod 755 $(BOOTSTRAP_ROOT)/bootstrap/bin/erl

$(BOOTSTRAP_ROOT)/bootstrap/bin/erlc: $(ERL_TOP)/bin/$(TARGET)/erlc $(BOOTSTRAP_ROOT)/bootstrap/target
	@rm -f $(BOOTSTRAP_ROOT)/bootstrap/bin/erlc
	@cp $(ERL_TOP)/bin/$(TARGET)/erlc $(BOOTSTRAP_ROOT)/bootstrap/bin/erlc
	@chmod 755 $(BOOTSTRAP_ROOT)/bootstrap/bin/erlc

$(BOOTSTRAP_ROOT)/bootstrap/bin/escript: $(ERL_TOP)/bin/$(TARGET)/escript $(BOOTSTRAP_ROOT)/bootstrap/target
	@rm -f $(BOOTSTRAP_ROOT)/bootstrap/bin/escript
	@cp $(ERL_TOP)/bin/$(TARGET)/escript $(BOOTSTRAP_ROOT)/bootstrap/bin/escript
	@chmod 755 $(BOOTSTRAP_ROOT)/bootstrap/bin/escript
endif

bootstrap_setup_target:
	@{ test -r $(BOOTSTRAP_ROOT)/bootstrap/target && \
	   test $(TARGET) = `cat $(BOOTSTRAP_ROOT)/bootstrap/target`; } || \
	 echo $(TARGET) > $(BOOTSTRAP_ROOT)/bootstrap/target

secondary_bootstrap_build:
	cd lib && \
	  ERL_TOP=$(ERL_TOP) PATH=$(BOOT_PREFIX)"$${PATH}" \
		$(MAKE) opt SECONDARY_BOOTSTRAP=true

secondary_bootstrap_copy:
	if test ! -d $(BOOTSTRAP_ROOT)/bootstrap/lib/hipe ; then mkdir $(BOOTSTRAP_ROOT)/bootstrap/lib/hipe ; fi
	if test ! -d $(BOOTSTRAP_ROOT)/bootstrap/lib/hipe/ebin ; then mkdir $(BOOTSTRAP_ROOT)/bootstrap/lib/hipe/ebin ; fi
	for x in lib/hipe/$(HIPE_BOOTSTRAP_EBIN)/*.beam; do \
		BN=`basename $$x`; \
		TF=$(BOOTSTRAP_ROOT)/bootstrap/lib/hipe/ebin/$$BN; \
		test -f  $$TF && \
		test '!' -z "`find $$x -newer $$TF -print`" && \
			cp $$x $$TF; \
		test '!' -f $$TF && \
			cp $$x $$TF; \
		true; \
	done
	if test ! -d $(BOOTSTRAP_ROOT)/bootstrap/lib/parsetools ; then mkdir $(BOOTSTRAP_ROOT)/bootstrap/lib/parsetools ; fi
	if test ! -d $(BOOTSTRAP_ROOT)/bootstrap/lib/parsetools/ebin ; then mkdir $(BOOTSTRAP_ROOT)/bootstrap/lib/parsetools/ebin ; fi
	if test ! -d $(BOOTSTRAP_ROOT)/bootstrap/lib/parsetools/include ; then mkdir $(BOOTSTRAP_ROOT)/bootstrap/lib/parsetools/include ; fi
	if test ! -d $(BOOTSTRAP_ROOT)/bootstrap/lib/orber ; then mkdir $(BOOTSTRAP_ROOT)/bootstrap/lib/orber ; fi
	if test ! -d $(BOOTSTRAP_ROOT)/bootstrap/lib/orber/include ; then mkdir $(BOOTSTRAP_ROOT)/bootstrap/lib/orber/include ; fi
	for x in lib/parsetools/ebin/*.beam; do \
		BN=`basename $$x`; \
		TF=$(BOOTSTRAP_ROOT)/bootstrap/lib/parsetools/ebin/$$BN; \
		test -f  $$TF && \
		test '!' -z "`find $$x -newer $$TF -print`" && \
			cp $$x $$TF; \
		test '!' -f $$TF && \
			cp $$x $$TF; \
		true; \
	done
#	cp lib/parsetools/ebin/*.beam $(BOOTSTRAP_ROOT)/bootstrap/lib/parsetools/ebin
	for x in lib/parsetools/include/*.hrl; do \
		BN=`basename $$x`; \
		TF=$(BOOTSTRAP_ROOT)/bootstrap/lib/parsetools/include/$$BN; \
		test -f  $$TF && \
		test '!' -z "`find $$x -newer $$TF -print`" && \
			cp $$x $$TF; \
		test '!' -f $$TF && \
			cp $$x $$TF; \
		true; \
	done
#	cp -f lib/parsetools/include/*.hrl $(BOOTSTRAP_ROOT)/bootstrap/lib/parsetools/include
	if test ! -d $(BOOTSTRAP_ROOT)/bootstrap/lib/asn1 ; then mkdir $(BOOTSTRAP_ROOT)/bootstrap/lib/asn1 ; fi
	if test ! -d $(BOOTSTRAP_ROOT)/bootstrap/lib/asn1/ebin ; then mkdir $(BOOTSTRAP_ROOT)/bootstrap/lib/asn1/ebin ; fi
	if test ! -d $(BOOTSTRAP_ROOT)/bootstrap/lib/asn1/src ; then mkdir $(BOOTSTRAP_ROOT)/bootstrap/lib/asn1/src ; fi
	for x in lib/asn1/ebin/*.beam; do \
		BN=`basename $$x`; \
		TF=$(BOOTSTRAP_ROOT)/bootstrap/lib/asn1/ebin/$$BN; \
		test -f  $$TF && \
		test '!' -z "`find $$x -newer $$TF -print`" && \
			cp $$x $$TF; \
		test '!' -f $$TF && \
			cp $$x $$TF; \
		true; \
	done
#	cp lib/asn1/ebin/*.beam $(BOOTSTRAP_ROOT)/bootstrap/lib/asn1/ebin
	for x in lib/asn1/src/*.[eh]rl; do \
		BN=`basename $$x`; \
		TF=$(BOOTSTRAP_ROOT)/bootstrap/lib/asn1/src/$$BN; \
		test -f  $$TF && \
		test '!' -z "`find $$x -newer $$TF -print`" && \
			cp $$x $$TF; \
		test '!' -f $$TF && \
			cp $$x $$TF; \
		true; \
	done
#	cp -f lib/asn1/src/*.erl lib/asn1/src/*.hrl $(BOOTSTRAP_ROOT)/bootstrap/lib/asn1/src
	for x in lib/orber/include/*.hrl; do \
		BN=`basename $$x`; \
		TF=$(BOOTSTRAP_ROOT)/bootstrap/lib/orber/include/$$BN; \
		test -f  $$TF && \
		test '!' -z "`find $$x -newer $$TF -print`" && \
			cp $$x $$TF; \
		test '!' -f $$TF && \
			cp $$x $$TF; \
		true; \
	done
	if test ! -d $(BOOTSTRAP_ROOT)/bootstrap/lib/xmerl ; then mkdir $(BOOTSTRAP_ROOT)/bootstrap/lib/xmerl ; fi
	if test ! -d $(BOOTSTRAP_ROOT)/bootstrap/lib/xmerl/include ; then mkdir $(BOOTSTRAP_ROOT)/bootstrap/lib/xmerl/include ; fi
	for x in lib/xmerl/include/*.hrl; do \
		BN=`basename $$x`; \
		TF=$(BOOTSTRAP_ROOT)/bootstrap/lib/xmerl/include/$$BN; \
		test -f  $$TF && \
		test '!' -z "`find $$x -newer $$TF -print`" && \
			cp $$x $$TF; \
		test '!' -f $$TF && \
			cp $$x $$TF; \
		true; \
	done

tertiary_bootstrap_build:
	cd lib && \
	  ERL_TOP=$(ERL_TOP) PATH=$(BOOT_PREFIX)"$${PATH}" \
		$(MAKE) opt TERTIARY_BOOTSTRAP=true

tertiary_bootstrap_copy:
	if test ! -d $(BOOTSTRAP_ROOT)/bootstrap/lib/snmp ; then mkdir $(BOOTSTRAP_ROOT)/bootstrap/lib/snmp ; fi
	if test ! -d $(BOOTSTRAP_ROOT)/bootstrap/lib/snmp/ebin ; then mkdir $(BOOTSTRAP_ROOT)/bootstrap/lib/snmp/ebin ; fi
	if test ! -d $(BOOTSTRAP_ROOT)/bootstrap/lib/snmp/include ; then mkdir $(BOOTSTRAP_ROOT)/bootstrap/lib/snmp/include ; fi
	for x in lib/snmp/ebin/*.beam; do \
		BN=`basename $$x`; \
		TF=$(BOOTSTRAP_ROOT)/bootstrap/lib/snmp/ebin/$$BN; \
		test -f  $$TF && \
		test '!' -z "`find $$x -newer $$TF -print`" && \
			cp $$x $$TF; \
		test '!' -f $$TF && \
			cp $$x $$TF; \
		true; \
	done
#	cp lib/snmp/ebin/*.beam $(BOOTSTRAP_ROOT)/bootstrap/lib/snmp/ebin
	if test ! -d $(BOOTSTRAP_ROOT)/bootstrap/lib/sasl ; then mkdir $(BOOTSTRAP_ROOT)/bootstrap/lib/sasl ; fi
	if test ! -d $(BOOTSTRAP_ROOT)/bootstrap/lib/sasl/ebin ; then mkdir $(BOOTSTRAP_ROOT)/bootstrap/lib/sasl/ebin ; fi
	if test ! -d $(BOOTSTRAP_ROOT)/bootstrap/lib/sasl/include ; then mkdir $(BOOTSTRAP_ROOT)/bootstrap/lib/sasl/include ; fi
	if test ! -d $(BOOTSTRAP_ROOT)/bootstrap/lib/ic ; then mkdir $(BOOTSTRAP_ROOT)/bootstrap/lib/ic ; fi
	if test ! -d $(BOOTSTRAP_ROOT)/bootstrap/lib/ic/ebin ; then mkdir $(BOOTSTRAP_ROOT)/bootstrap/lib/ic/ebin ; fi
	if test ! -d $(BOOTSTRAP_ROOT)/bootstrap/lib/ic/include ; then mkdir $(BOOTSTRAP_ROOT)/bootstrap/lib/ic/include ; fi
	if test ! -d $(BOOTSTRAP_ROOT)/bootstrap/lib/wx ; then mkdir $(BOOTSTRAP_ROOT)/bootstrap/lib/wx ; fi
	if test ! -d $(BOOTSTRAP_ROOT)/bootstrap/lib/wx/ebin ; then mkdir $(BOOTSTRAP_ROOT)/bootstrap/lib/wx/ebin ; fi
	if test ! -d $(BOOTSTRAP_ROOT)/bootstrap/lib/wx/include ; then mkdir $(BOOTSTRAP_ROOT)/bootstrap/lib/wx/include ; fi
	if test ! -d $(BOOTSTRAP_ROOT)/bootstrap/lib/test_server ; then mkdir $(BOOTSTRAP_ROOT)/bootstrap/lib/test_server ; fi
	if test ! -d $(BOOTSTRAP_ROOT)/bootstrap/lib/test_server/include ; then mkdir $(BOOTSTRAP_ROOT)/bootstrap/lib/test_server/include ; fi
	if test ! -d $(BOOTSTRAP_ROOT)/bootstrap/lib/common_test ; then mkdir $(BOOTSTRAP_ROOT)/bootstrap/lib/common_test ; fi
	if test ! -d $(BOOTSTRAP_ROOT)/bootstrap/lib/common_test/include ; then mkdir $(BOOTSTRAP_ROOT)/bootstrap/lib/common_test/include ; fi
	for x in lib/ic/ebin/*.beam; do \
		BN=`basename $$x`; \
		TF=$(BOOTSTRAP_ROOT)/bootstrap/lib/ic/ebin/$$BN; \
		test -f  $$TF && \
		test '!' -z "`find $$x -newer $$TF -print`" && \
			cp $$x $$TF; \
		test '!' -f $$TF && \
			cp $$x $$TF; \
		true; \
	done
#	cp lib/ic/ebin/*.beam $(BOOTSTRAP_ROOT)/bootstrap/lib/ic/ebin
	for x in lib/ic/include/*.idl; do \
		BN=`basename $$x`; \
		TF=$(BOOTSTRAP_ROOT)/bootstrap/lib/ic/include/$$BN; \
		test -f  $$TF && \
		test '!' -z "`find $$x -newer $$TF -print`" && \
			cp $$x $$TF; \
		test '!' -f $$TF && \
			cp $$x $$TF; \
		true; \
	done
	for x in lib/ic/include/*.h; do \
		BN=`basename $$x`; \
		TF=$(BOOTSTRAP_ROOT)/bootstrap/lib/ic/include/$$BN; \
		test -f  $$TF && \
		test '!' -z "`find $$x -newer $$TF -print`" && \
			cp $$x $$TF; \
		test '!' -f $$TF && \
			cp $$x $$TF; \
		true; \
	done
#	cp -f lib/ic/include/*.idl lib/ic/include/*.h $(BOOTSTRAP_ROOT)/bootstrap/lib/ic/include
	for x in lib/sasl/ebin/*.beam; do \
		BN=`basename $$x`; \
		TF=$(BOOTSTRAP_ROOT)/bootstrap/lib/sasl/ebin/$$BN; \
		test -f  $$TF && \
		test '!' -z "`find $$x -newer $$TF -print`" && \
			cp $$x $$TF; \
		test '!' -f $$TF && \
			cp $$x $$TF; \
		true; \
	done
#	cp lib/sasl/ebin/*.beam $(BOOTSTRAP_ROOT)/bootstrap/lib/sasl/ebin
	if test ! -d $(BOOTSTRAP_ROOT)/bootstrap/lib/syntax_tools ; then mkdir $(BOOTSTRAP_ROOT)/bootstrap/lib/syntax_tools ; fi
	if test ! -d $(BOOTSTRAP_ROOT)/bootstrap/lib/syntax_tools/ebin ; then mkdir $(BOOTSTRAP_ROOT)/bootstrap/lib/syntax_tools/ebin ; fi
	for x in lib/syntax_tools/ebin/*.beam; do \
		BN=`basename $$x`; \
		TF=$(BOOTSTRAP_ROOT)/bootstrap/lib/syntax_tools/ebin/$$BN; \
		test -f  $$TF && \
		test '!' -z "`find $$x -newer $$TF -print`" && \
			cp $$x $$TF; \
		test '!' -f $$TF && \
			cp $$x $$TF; \
		true; \
	done
	for x in lib/wx/include/*.hrl; do \
		BN=`basename $$x`; \
		TF=$(BOOTSTRAP_ROOT)/bootstrap/lib/wx/include/$$BN; \
		test -f  $$TF && \
		test '!' -z "`find $$x -newer $$TF -print`" && \
			cp $$x $$TF; \
		test '!' -f $$TF && \
			cp $$x $$TF; \
		true; \
	done
#       copy wx_object to remove undef behaviour warnings
	for x in lib/wx/ebin/wx_object.beam; do \
		BN=`basename $$x`; \
		TF=$(BOOTSTRAP_ROOT)/bootstrap/lib/wx/ebin/$$BN; \
		test -f  $$TF && \
		test '!' -z "`find $$x -newer $$TF -print`" && \
			cp $$x $$TF; \
		test '!' -f $$TF && \
			cp $$x $$TF; \
		true; \
	done

#	copy test includes to be able to compile tests with bootstrap compiler
	for x in lib/test_server/include/*.hrl; do \
		BN=`basename $$x`; \
		TF=$(BOOTSTRAP_ROOT)/bootstrap/lib/test_server/include/$$BN; \
		test -f  $$TF && \
		test '!' -z "`find $$x -newer $$TF -print`" && \
			cp $$x $$TF; \
		test '!' -f $$TF && \
			cp $$x $$TF; \
		true; \
	done

	for x in lib/common_test/include/*.hrl; do \
		BN=`basename $$x`; \
		TF=$(BOOTSTRAP_ROOT)/bootstrap/lib/common_test/include/$$BN; \
		test -f  $$TF && \
		test '!' -z "`find $$x -newer $$TF -print`" && \
			cp $$x $$TF; \
		test '!' -f $$TF && \
			cp $$x $$TF; \
		true; \
	done
#	cp lib/syntax_tools/ebin/*.beam $(BOOTSTRAP_ROOT)/bootstrap/lib/syntax_tools/ebin

.PHONY: check_recreate_primary_bootstrap recreate_primary_bootstrap


#
# If the source is a prebuilt delivery, no $(ERL_TOP)/bootstrap/lib
# directory will exist. All applications part of the primary bootstrap
# are delivered prebuilt though. If it is a prebuilt delivery we need
# to recreate the primary bootstrap, from the prebuilt result.
#
# A prebuild delivery always contain a $(ERL_TOP)/prebuilt.files file.
# If no such file exists, we wont try to recreate the primary bootstrap,
# since it will just fail producing anything useful.
#

check_recreate_primary_bootstrap:
	@if test -f $(ERL_TOP)/prebuilt.files ; then \
	  if test ! -d $(ERL_TOP)/bootstrap/lib ; then \
	    $(ERL_TOP)/otp_build save_bootstrap ; \
	  fi ; \
	fi

#
# recreate_primary_bootstrap assumes that if $(ERL_TOP)/prebuilt.files
# exist, all build results needed already exist in the application specific
# directories of all applications part of the primary bootstrap.
#
recreate_primary_bootstrap:
	$(ERL_TOP)/otp_build save_bootstrap

# The first bootstrap build is rarely (never) used in open source, it's
# used to build the shipped bootstrap directory. The Open source bootstrap 
# stages start with secondary bootstrap.
#
# These are the ones used, the other ones (prefixed with old_) are for BC.

# These modules should stay in the kernel directory to make building
# of the emulator possible

.PHONY: primary_bootstrap						\
	primary_bootstrap_build						\
	primary_bootstrap_compiler					\
	primary_bootstrap_mkdirs					\
	primary_bootstrap_copy

primary_bootstrap:
	@echo "=== Building a bootstrap compiler in $(BOOTSTRAP_ROOT)/bootstrap"
	$(MAKE) BOOTSTRAP_ROOT=$(BOOTSTRAP_ROOT) \
		ERL_TOP=$(ERL_TOP) \
		bootstrap_clean
	cd $(ERL_TOP) && \
		$(MAKE) TESTROOT=$(BOOTSTRAP_TOP) \
		BOOTSTRAP_TOP=$(BOOTSTRAP_TOP) \
		primary_bootstrap_build
	cd $(ERL_TOP) && \
		$(MAKE) TESTROOT=$(BOOTSTRAP_TOP) \
		BOOTSTRAP_TOP=$(BOOTSTRAP_TOP) \
		primary_bootstrap_copy
	cd $(ERL_TOP)/erts/start_scripts && \
		$(MAKE) TESTROOT=$(BOOTSTRAP_TOP) \
		BOOTSTRAP_TOP=$(BOOTSTRAP_TOP) bootstrap_scripts
	test $(BOOTSTRAP_ROOT) = $(ERL_TOP) \
		|| $(ERL_TOP)/otp_build \
			copy_primary_bootstrap \
			$(BOOTSTRAP_TOP) \
			$(BOOTSTRAP_ROOT)

primary_bootstrap_build: primary_bootstrap_mkdirs primary_bootstrap_compiler \
  primary_bootstrap_stdlib
	cd lib && $(MAKE) ERLC_FLAGS='-pa $(BOOTSTRAP_COMPILER)/ebin' \
		BOOTSTRAP_TOP=$(BOOTSTRAP_TOP) \
		BOOTSTRAP=1 opt

primary_bootstrap_compiler: 
	cd lib/compiler && $(MAKE) \
		BOOTSTRAP_TOP=$(BOOTSTRAP_TOP) \
		BOOTSTRAP_COMPILER=$(BOOTSTRAP_COMPILER) \
		BOOTSTRAP=1 \
		opt

primary_bootstrap_stdlib: 
	cd lib/stdlib/src && $(MAKE) \
		BOOTSTRAP_COMPILER=$(BOOTSTRAP_COMPILER) \
		primary_bootstrap_compiler

primary_bootstrap_mkdirs:
	test -d $(BOOTSTRAP_COMPILER)/egen \
		|| mkdir -p $(BOOTSTRAP_COMPILER)/egen
	test -d $(BOOTSTRAP_COMPILER)/ebin \
		|| mkdir -p $(BOOTSTRAP_COMPILER)/ebin
	test -d $(BOOTSTRAP_TOP)/lib/kernel/egen \
		|| mkdir -p $(BOOTSTRAP_TOP)/lib/kernel/egen 
	test -d $(BOOTSTRAP_TOP)/lib/kernel/ebin \
		|| mkdir -p $(BOOTSTRAP_TOP)/lib/kernel/ebin 
	test -d $(BOOTSTRAP_TOP)/lib/kernel/include \
		|| mkdir -p $(BOOTSTRAP_TOP)/lib/kernel/include 
	test -d $(BOOTSTRAP_TOP)/lib/stdlib/egen \
		|| mkdir -p $(BOOTSTRAP_TOP)/lib/stdlib/egen 
	test -d $(BOOTSTRAP_TOP)/lib/stdlib/ebin \
		|| mkdir -p $(BOOTSTRAP_TOP)/lib/stdlib/ebin 
	test -d $(BOOTSTRAP_TOP)/lib/stdlib/include \
		|| mkdir -p $(BOOTSTRAP_TOP)/lib/stdlib/include 
	test -d $(BOOTSTRAP_TOP)/lib/compiler/egen \
		|| mkdir -p $(BOOTSTRAP_TOP)/lib/compiler/egen 
	test -d $(BOOTSTRAP_TOP)/lib/compiler/ebin \
		|| mkdir -p $(BOOTSTRAP_TOP)/lib/compiler/ebin 
	test -d $(BOOTSTRAP_TOP)/lib/orber/include \
		|| mkdir -p $(BOOTSTRAP_TOP)/lib/orber/include

primary_bootstrap_copy:
	cp -f lib/kernel/include/*.hrl $(BOOTSTRAP_TOP)/lib/kernel/include
	cp -f lib/stdlib/include/*.hrl $(BOOTSTRAP_TOP)/lib/stdlib/include
	cp -f lib/orber/include/* $(BOOTSTRAP_TOP)/lib/orber/include

# To remove modules left by the bootstrap building, but leave (restore)
# the modules in kernel which are needed for an emulator build
KERNEL_PRELOAD    = otp_ring0 init erl_prim_loader prim_inet prim_file zlib prim_zip erlang
KERNEL_PRELOAD_BEAMS=$(KERNEL_PRELOAD:%=$(BOOTSTRAP_TOP)/lib/kernel/ebin/%.beam)

start_scripts:
	@cd erts/start_scripts \
	     && ERL_TOP=$(ERL_TOP) PATH=$(BOOT_PREFIX)"$${PATH}" $(MAKE) script

# Creates "erl" and "erlc" scripts in bin/erl which uses the libraries in lib
local_setup:
	@rm -f bin/erl bin/erlc bin/cerl
	@cd erts && \
		ERL_TOP=$(ERL_TOP) PATH=$(BOOT_PREFIX)"$${PATH}" \
		$(MAKE) local_setup



# ----------------------------------------------------------------------
# Build tests
# ---------------------------------------------------------------------

TEST_DIRS := \
	lib/test_server \
	$(wildcard lib/*/test) \
	erts/test \
	erts/epmd/test \
	erts/emulator/test

.PHONY: tests release_tests $(TEST_DIRS)

tests release_tests: $(TEST_DIRS)

$(TEST_DIRS):
	if test -f $@/Makefile; then \
	    (cd $@; $(MAKE) TESTROOT="$(TESTSUITE_ROOT)" \
	    PATH=$(TEST_PATH_PREFIX)$(BOOT_PREFIX)"$${PATH}" release_tests) || exit $$?; \
	fi

#
# Install
#
# Order is important here, don't change it!
#
INST_DEP += install.dirs install.emulator install.libs install.Install install.bin

install: $(INST_DEP)

install-docs: 
	ERL_TOP=$(ERL_TOP) INSTALLROOT="$(ERLANG_LIBDIR)" PATH=$(BOOT_PREFIX)"$${PATH}" \
	$(MAKE) RELEASE_ROOT="$(ERLANG_LIBDIR)" release_docs


install.emulator:
	cd erts && \
	  ERL_TOP=$(ERL_TOP) PATH=$(INST_PATH_PREFIX)"$${PATH}" \
	  $(MAKE) TESTROOT="$(ERLANG_LIBDIR)" release

install.libs:
ifeq ($(OTP_SMALL_BUILD),true)
	cd lib && \
	  ERL_TOP=$(ERL_TOP) PATH=$(INST_PATH_PREFIX)"$${PATH}" \
	  $(MAKE) TESTROOT="$(ERLANG_LIBDIR)" release 
else
	cd lib && \
	  ERL_TOP=$(ERL_TOP) PATH=$(INST_PATH_PREFIX)"$${PATH}" \
	  $(MAKE) TESTROOT="$(ERLANG_LIBDIR)" BUILD_ALL=true release
endif

install.Install:
	(cd "$(ERLANG_LIBDIR)" \
	 && ./Install $(INSTALL_CROSS) -minimal "$(ERLANG_INST_LIBDIR)")

#
# Install erlang base public files
#

install.bin:
	@ DESTDIR="$(DESTDIR)" EXTRA_PREFIX="$(EXTRA_PREFIX)"		\
	  LN_S="$(LN_S)" BINDIR_SYMLINKS="$(BINDIR_SYMLINKS)"  		\
		$(ERL_TOP)/make/install_bin				\
			--bindir "$(bindir)"				\
			--erlang-bindir "$(erlang_bindir)"		\
			--exec-prefix "$(exec_prefix)"			\
			$(ERL_BASE_PUB_FILES)

#
# Directories needed before we can install
#
install.dirs:
	test -d "$(BINDIR)" || ${MKSUBDIRS} "$(BINDIR)"
	${MKSUBDIRS} "$(ERLANG_LIBDIR)"
	${MKSUBDIRS} "$(ERLANG_LIBDIR)/usr/lib"

.PHONY: strict_install

strict_install: $(IBIN_DIR) $(IBIN_FILES)

$(IBIN_FILES): $(ERL_TOP)/make/unexpected_use
	rm -f $@
	(cd $(dir $@) && $(LN_S) $(ERL_TOP)/make/unexpected_use $(notdir $@))

$(IBIN_DIR):
	$(MKSUBDIRS) $@

# ----------------------------------------------------------------------

.PHONY: clean eclean bootstrap_root_clean bootstrap_clean

#
# Clean targets
#

clean: check_recreate_primary_bootstrap
	rm -f *~ *.bak config.log config.status prebuilt.files ibin/*
	find . -type f -name SKIP -print | xargs $(RM)
	cd erts && ERL_TOP=$(ERL_TOP) $(MAKE) clean
	cd lib  && ERL_TOP=$(ERL_TOP) $(MAKE) clean BUILD_ALL=true

#
# Just wipe out emulator, not libraries
#

eclean:
	cd erts && ERL_TOP=$(ERL_TOP) $(MAKE) clean

#
# Clean up bootstrap
#

bootstrap_root_clean:
	rm -f $(BOOTSTRAP_ROOT)/bootstrap/lib/*/ebin/*.beam
	rm -f $(BOOTSTRAP_ROOT)/bootstrap/lib/*/include/*.hrl
	rm -f $(BOOTSTRAP_ROOT)/bootstrap/bin/*.*

# $(ERL_TOP)/bootstrap *should* equal $(BOOTSTRAP_TOP)
#
# We use $(ERL_TOP)/bootstrap instead of $(BOOTSTRAP_TOP) here as an
# extra safety precaution (we would really make a mess if
# $(BOOTSTRAP_TOP) for some reason should be empty).
bootstrap_clean:
	rm -f $(ERL_TOP)/bootstrap/lib/*/ebin/*.beam
	rm -f $(ERL_TOP)/bootstrap/lib/*/ebin/*.app
	rm -f $(ERL_TOP)/bootstrap/lib/*/egen/*
	rm -f $(ERL_TOP)/bootstrap/lib/*/include/*.hrl
	rm -f $(ERL_TOP)/bootstrap/primary_compiler/ebin/*
	rm -f $(ERL_TOP)/bootstrap/primary_compiler/egen/*
	rm -f $(ERL_TOP)/bootstrap/bin/*.*
	rm -f $(KERNEL_PRELOAD:%=$(ERL_TOP)/lib/kernel/ebin/%.beam)
	test $(BOOTSTRAP_ROOT) = $(ERL_TOP) \
		|| $(MAKE) BOOTSTRAP_ROOT=$(BOOTSTRAP_ROOT) bootstrap_root_clean

# ----------------------------------------------------------------------