# ----------------------------------------------------
# Copyright (C) 1997, Ericsson Telecommunications
# Author: Lars Thorsen
# ----------------------------------------------------
CC_ROOT := $(shell pwd | sed 's/erts.*$$//')
AUTOCONF := $(CC_ROOT)erts/autoconf
TARGET := $(shell $(AUTOCONF)/config.guess)
include $(CC_ROOT)internal_tools/make/$(TARGET)/otp.mk
include $(CC_ROOT)erts/lib/compiler/vsn.mk
include $(CC_ROOT)erts/system/vsn.mk

ifeq ($(TYPE),debug)
TYPEMARKER = .debug
else
ifeq ($(TYPE),instr)
TYPEMARKER = .instr
else
TYPEMARKER =
endif
endif

debug opt docs clean:
	${MAKE} TYPE=$@ LTYPE=$@ common

instr:
	${MAKE} TYPE=$@ LTYPE=opt common

common: lib_and_emulator local_setup

lib_and_emulator:
	(cd lib; ${MAKE} $(LTYPE))
	(cd system; ${MAKE} $(TYPE))

local_setup:
ifeq ($(findstring win32,$(TARGET)),win32)
	(cd install_nt; ${MAKE})
else
	sed -e "s;%FINAL_ROOTDIR%;`pwd`;" -e "s;erts-.*/bin;bin/$(TARGET);" \
	 -e "s;EMU=.*;EMU=${EMULATOR}$(TYPEMARKER);" \
         system/etc.unix/erl.dist > bin/erl
	cp bin/$(TARGET)/erlc bin/erlc
	chmod 755 bin/erl bin/erlc
	cd system/start_scripts; ${MAKE} $(CC_ROOT)erts/bin/start.script
endif

release release_docs: clean_release
	(cd autoconf; $(MAKE))
	(cd lib; ${MAKE} $@)
	(cd system; ${MAKE} TYPE=instr $@)
	(cd system; ${MAKE} $@)
	(cd doc/src; ${MAKE} $@)

#
# release_src: Is the main target for building a "base" source release.
# 
# It builds all necessary components and puts them in various release
# directories. The components are:
# 
#  - autoconf files
#  - start scripts
#  - base librarise (stdlib, kernel, compiler, parsetools and emacs)
#
# To build a tar file from these you need the script "make_base.sh" which
# collects all the above mentioned components into one tar file.
#

# Files to release in autoconf directory:
AUTOCONF_SRC_FILES = \
    autoconf/configure.in autoconf/Makefile autoconf/configure.win32
AUTOCONF_PGM_FILES = \
    autoconf/configure autoconf/config.guess autoconf/config.sub \
    autoconf/install-sh
AUTOCONF_RELDIR = release/$(TARGET)/erts-$(VSN)/autoconf

release_src_autoconf:
	${INSTALL_DIR} ${AUTOCONF_RELDIR}
	${INSTALL_DATA} ${AUTOCONF_SRC_FILES} ${AUTOCONF_RELDIR}
	${INSTALL_PROGRAM} ${AUTOCONF_PGM_FILES} ${AUTOCONF_RELDIR}


ERTS_RELDIR=release/$(TARGET)/erts-$(VSN)

release_src: clean_release release_src_autoconf
	@# Yuck, clearmake is broken :-(
	for d in kernel stdlib compiler parsetools; do \
	  if test ! -f lib/$$d/doc/html/min_head.gif ; then \
	    cp ../internal_tools/sgml/pics/min_head.gif lib/$$d/doc/html ;\
	  fi \
	done
	${INSTALL_DIR} $(ERTS_RELDIR)/bin
	${INSTALL_DIR} $(ERTS_RELDIR)/doc/html
	${INSTALL_DATA} EPL1x0.txt Makefile $(ERTS_RELDIR)
	( cd system/start_scripts; \
	  ${MAKE} start_clean.boot start_sasl.boot \
	          $(CC_ROOT)erts/bin/start.script )
	${INSTALL_DATA} bin/start.script bin/start.boot \
	          system/start_scripts/start_clean.script \
	          system/start_scripts/start_clean.boot \
	          system/start_scripts/start_sasl.script \
	          system/start_scripts/start_sasl.boot $(ERTS_RELDIR)/bin
	( cd lib/kernel     ; ${MAKE} TARGET=free_source $@ )
	( cd lib/stdlib     ; ${MAKE} TARGET=free_source $@ )
	( cd lib/compiler   ; ${MAKE} TARGET=free_source $@ )
	( cd lib/parsetools ; ${MAKE} TARGET=free_source $@ )
	( cd lib/emacs      ; ${MAKE} TARGET=free_source $@ )
	( cd doc/src        ; ${MAKE} TARGET=free_source $@ )
	( cd system         ; ${MAKE} TARGET=free_source $@ -f Makefile.src )

clean_release:
	rm -rf release/$(TARGET)

beam_debug:
	${MAKE} EMULATOR=beam debug

#
# Adding a test suite: Update this list and create a target for the test suite below.
#
ifeq ($(findstring vxworks,$(TARGET)),vxworks)
TESTS = test_server                \
	os_mon_test                \
	emulator_test              \
	erl_interface_test         \
	kernel_test                \
	stdlib_test                \
	compiler_test              \
	sasl_test                  \
	system_test
else
TESTS =	test_server                \
	ig_test                    \
	inets_test                 \
	os_mon_test                \
	emulator_test              \
	system_test                \
	erl_interface_test         \
	kernel_test                \
	stdlib_test                \
	compiler_test              \
	sasl_test                  \
	tools_test                 \
	debugger_test              \
	pman_test                  \
	toolbar_test               \
	snmpea_test                \
	eva_test                   \
	asn1_test                  \
	ic_test                    \
	orber_test                 \
	jive_test                  \
	mnemosyne_test             \
	mesh_test                  \
	mnesia_test                \
	mnesia_session_test        \
	test_server_test
endif

tests release_tests: $(TESTS)

# Using double colon to force rebuild of target. (Mattias)

test_server::
	cd lib/test_server; $(MAKE) release_tests

ig_test::
	cd lib/ig/test; $(MAKE) release_tests

inets_test::
	cd lib/inets/test; $(MAKE) release_tests

os_mon_test::
	cd lib/os_mon/test; $(MAKE) release_tests

emulator_test::
	cd system/emulator/test; $(MAKE) release_tests

system_test::
	cd system/test; $(MAKE) release_tests

erl_interface_test::
	cd system/erl_interface/test; $(MAKE) release_tests

kernel_test::
	cd lib/kernel/test; $(MAKE) release_tests

stdlib_test::
	cd lib/stdlib/test; $(MAKE) release_tests

compiler_test::
	cd lib/compiler/test; $(MAKE) release_tests

sasl_test::
	cd lib/sasl/test; $(MAKE) release_tests

tools_test::
	cd lib/tools/test; $(MAKE) release_tests

debugger_test::
	cd lib/debugger/test; $(MAKE) release_tests

pman_test::
	cd lib/pman/test; $(MAKE) release_tests

toolbar_test::
	cd lib/toolbar/test; $(MAKE) release_tests

snmpea_test::
	cd lib/snmpea/test; $(MAKE) release_tests

eva_test::
	cd lib/eva/test; $(MAKE) release_tests

asn1_test::
	cd lib/asn1/test; $(MAKE) release_tests

ic_test::
	cd lib/ic/test; $(MAKE) release_tests

orber_test::
	cd lib/orber/test; $(MAKE) release_tests

jive_test::
	cd lib/jive/test; $(MAKE) release_tests

mnemosyne_test::
	cd lib/mnemosyne/test; $(MAKE) release_tests

mesh_test::
	cd lib/mesh/test; $(MAKE) release_tests

mnesia_test::
	cd lib/mnesia/test; $(MAKE) release_tests

mnesia_session_test::
	cd lib/mnesia_session/test; $(MAKE) release_tests

test_server_test::
	cd lib/test_server/test; $(MAKE) release_tests



# The Makefiles in these directories not updated with test targets.
#	cd lib/gs/test; $(MAKE) $@

#
# The following targets are used for bootstrapping a new compiler.
#
# To bootstrap BEAM (using JAM), invoke as:
#
# 	clearmake EMULATOR=beam TESTROOT=<directory> bootstrap
#
# When done, in <directory> there will be a subset of OTP containing
# all compilers needed to build the full system.
#

BOOT_ROOT=$(CC_ROOT)erts/release/$(TARGET)
BOOT_ERTS=$(BOOT_ROOT)/erts-$(VSN)
BOOTSTRAP_COMPILER=$(CC_ROOT)erts/bootstrap_compiler
HOST_ERLC:=$(shell which erlc)
BOOT_EMULATOR=jam

#
# The fully automatic target to the complete bootstrap process.
#
# Abbreviations: OC = Old Compiler, NC = New Compiler,
# 		 OE = Old Emulator, NE = New Emulator
#

bootstrap: bootstrap_nc_for_oe bootstrap_nc_for_ne \
	   bootstrap_ne bootstrap_scripts

#
# Builds the New Compiler for the Old Emulator and copies it to the
# the release area.
#

bootstrap_nc_for_oe:
	test -d $(BOOTSTRAP_COMPILER) || mkdir $(BOOTSTRAP_COMPILER)
	cd lib/compiler; $(MAKE) EMULATOR=$(BOOT_EMULATOR) \
	EBIN=$(BOOTSTRAP_COMPILER) ERLC="$(HOST_ERLC)" opt
	cd lib/snmpea/mc_dummy ; $(MAKE) EMULATOR=$(BOOT_EMULATOR) \
	ERLC="$(HOST_ERLC)" BOOTSTRAP=1 TYPE=opt opt
	cd lib/sasl/src ; $(MAKE) EMULATOR=$(BOOT_EMULATOR) \
	ERLC="$(HOST_ERLC)" BOOTSTRAP=1 TYPE=opt opt
	cd lib/mnesia/include ; $(MAKE) EMULATOR=$(BOOT_EMULATOR) \
	ERLC="$(HOST_ERLC)" BOOTSTRAP=1 TYPE=opt opt


#
# Builds the New Compiler (including stdlib and kernel) for the New
# Emulator, and copies everything to the release area.
#

bootstrap_nc_for_ne:
	cd lib; $(MAKE) ERLC_FLAGS='-pa $(BOOTSTRAP_COMPILER)' \
	 BOOTSTRAP=1 TYPE=release release

#
# Builds the New Emulator.
#

bootstrap_ne:
	cd system/emulator/zlib; $(MAKE) release
	cd system/emulator/$(EMULATOR); $(MAKE) release
	cd system/etc.unix; $(MAKE) release

bootstrap_scripts:
	cd system/start_scripts; $(MAKE) release \
	SASL_FLAGS= SCRIPT_FLAGS=+'{machine,$(EMULATOR)}'
