#-*-makefile-*-   ; force emacs to enter makefile-mode
# ----------------------------------------------------
# Template target for generating an OSE5 load module
#
# %CopyrightBegin%
#
# Copyright Ericsson AB 2013. 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%
#
# Author: Petre Pircalabu
# ----------------------------------------------------

# ----------------------------------------------------
# 	build-ose-load-module
#	Creates an OSE5 load module
#	params:
#		$(1) - The output target
#		$(2) - Objects
#		$(3) - Libraries
#		$(4) - LM configuration file
# ----------------------------------------------------

ifeq ($(findstring ose,$(TARGET)),ose)
LDR1FLAGS    = @erl_xcomp_ose_ldflags_pass1@
LDR2FLAGS    = @erl_xcomp_ose_ldflags_pass2@
OSEROOT	     = @erl_xcomp_ose_OSEROOT@
LCF	     = @erl_xcomp_ose_LM_LCF@
LMCONF	     = @erl_xcomp_ose_LM_CONF@
STRIP	     = @erl_xcomp_ose_STRIP@
LM_POST_LINK = @erl_xcomp_ose_LM_POST_LINK@
LM_SET_CONF  = @erl_xcomp_ose_LM_SET_CONF@
LM_ELF_SIZE  = @erl_xcomp_ose_LM_ELF_SIZE@
OSE_CONFD    = @erl_xcomp_ose_CONFD@
CRT0_LM      = @erl_xcomp_ose_CRT0_LM@
endif

define build-ose-load-module
	@echo " --- Linking $(1)"

	@echo " --- Linking $(1) (pass 1)"
	$(ld_verbose)$(PURIFY) $(LD) -o $(1)_unconfigured_ro -r \
	$(2) --start-group $(3) --end-group --cref --discard-none -M >  $(1)_1.map

	@echo " --- Linking $(1) (pass 2)"
	$(ld_verbose)$(PURIFY) $(LD) -o $(1)_unconfigured \
	$(1)_unconfigured_ro -T $(LCF) -n --emit-relocs -e crt0_lm --cref \
	--discard-none -M >  $(1)_2.map

	@echo " --- Inserting configuration"
	$(ld_verbose) $(LM_SET_CONF) $(1)_unconfigured < $(4)

	@echo " --- Striping $(1)"
#	$(ld_verbose) $(STRIP) $(1)_unconfigured

	@echo " --- Postlinking $(1)"
	$(ld_verbose) $(LM_POST_LINK) $(1)_unconfigured

	@echo " --- Sizing $(1)"
	$(ld_verbose) $(LM_ELF_SIZE) $(1)_unconfigured
	mv $(1)_unconfigured $(1)
endef