# Copyright (c) 2009-2010 Nokia Corporation and/or its subsidiary(-ies).
# All rights reserved.
# This component and the accompanying materials are made available
# under the terms of "Eclipse Public License v1.0"
# which accompanies this distribution, and is available
# at the URL "http://www.eclipse.org/legal/epl-v10.html".
#
# Initial Contributors:
# Nokia Corporation - initial contribution.
#
# Contributors:
#
# Description:
#

ifeq ($($(NAME)_$(PLATFORM_PATH)_bootstrap_flm),)
$(NAME)_$(PLATFORM_PATH)_bootstrap_flm := 1 

E32PATH := $(EXTENSION_ROOT)/$(E32PATH)
SOURCES := $(foreach S,$(SOURCES),$(addprefix $(EXTENSION_ROOT)/,$(S)))
INCLUDES2:=$(addprefix $(EXTENSION_ROOT)/,$(INCLUDES))
EXTRA_INC_PATH := $(foreach S,$(EXTRA_INC_PATH),$(addprefix $(EXTENSION_ROOT)/,$(S)))
GENINCLUDES_HEADERS := $(foreach H,$(GENINCLUDES_HEADERS),$(addprefix $(EXTENSION_ROOT)/,$(H)))

ifndef LINKBASE
LINKBASE := 0x00000000
endif

UNIQ:=$(E32PATH)$(PLATFORM_PATH)$(NAME)$(MEMMODEL)$(SOURCES)$(ASM_MACROS)
UNIQ:=$(word 1,$(shell echo $(UNIQ) | $(GNUMD5SUM)))

# Make the output build directory name unique, starting with NAME of the binary being built
EPOCBLDABS := $(EPOCBLD)/$(NAME)_$(UNIQ)

EPOCINC := $(EPOCROOT)/epoc32/include
EPOCKERNINC := $(EPOCINC)/kernel
EPOCCPUINC := $(EPOCKERNINC)/$(CPU)
EPOCTRG := $(EPOCROOT)/epoc32/release/$(PLATFORM_PATH)
TRG := $(EPOCTRG)/$(NAME).bin
TEMPTRG := $(EPOCBLDABS)/$(NAME).bin
ASMINCPATH := 
ASM_MACROS :=
CLEANTARGETS :=

CLEANTARGETS := $(CLEANTARGETS) $(TRG) $(TEMPTRG) $(join $(basename $(TRG)),.sym)

ifneq ($(EXTRA_INC_PATH),)
ASMINCPATH := $(EXTRA_INC_PATH)
endif

ASMINCPATH := . $(EPOCBLDABS) $(ASMINCPATH) $(EXTENSION_ROOT) $(EPOCCPUINC) $(EXTRA_EPOC32_INC_PATH) $(E32PATH)/eka/include/kernel/$(CPU)

ifeq ($(MEMMODEL),)
$(error MEMMODEL parameter not specified)
endif

# Convert MEMMODEL parameter to lower case
MEMMODEL := $(shell echo $(MEMMODEL) | tr A-Z a-z)

ifeq ($(MEMMODEL),direct)
CFG_MM := CFG_MMDirect
HEADERS_MM :=
endif
ifeq ($(MEMMODEL),flexible)
CFG_MM := CFG_MMFlexible
HEADERS_MM := $(E32PATH)/eka/include/memmodel/epoc/flexible/$(CPU)/mmboot.h
endif
ifeq ($(MEMMODEL),moving)
CFG_MM := CFG_MMMoving
HEADERS_MM := $(E32PATH)/eka/include/memmodel/epoc/moving/$(CPU)/mmboot.h
endif
ifeq ($(MEMMODEL),multiple)
CFG_MM := CFG_MMMultiple
HEADERS_MM := $(E32PATH)/eka/include/memmodel/epoc/multiple/$(CPU)/mmboot.h
endif
ifndef CFG_MM
$(error '$(MEMMODEL)' memory model unknown)
endif

ASM_MACROS := $(ASM_MACROS) $(CFG_MM)
ifneq ($(SMP),)
ASM_MACROS := $(ASM_MACROS) SMP
endif

ASMINCPATHCMD := $(foreach dir,$(ASMINCPATH),$(join -I ,$(dir)))
ASM_MACROS := $(ASM_MACROS) USE_CXSF
      
INCEXT := inc
ASM_MACRO_CMD := $(foreach macro,$(ASM_MACROS),--predefine "$(macro) SETL {TRUE}")
AFLAGS := -g --keep $(ASM_MACRO_CMD) $(ASMINCPATHCMD)
LFLAGS := --ro-base $(LINKBASE) --entry $(LINKBASE) --map
SYMOPT := --symdefs

define bootstrap_asm
$(1) : $(2) : $(3)
	$(call startrule,bootstrap_rvct_asm) \
	$(ASM) $(AFLAGS) -o $$@ --LIST $$(join $$(basename $$@),.lst) $$< \
	$(call endrule,bootstrap_rvct_asm)
endef

define bootstrap_link
$(EPOCBLDABS)/$(NAME).in : $(LINKOBJECTS) $(LINKFILE) | $(EPOCBLDABS)
	$(call startrule,bootstrap_rvct_link) \
	$(LD) $(LFLAGS) $(SYMOPT) $$(join $$(basename $$@),.sym) -o $$@ $$(filter %.o,$$^); \
	$(GNUCP) $$@ $$(join $$(basename $(TRG)),.sym) \
	$(call endrule,bootstrap_rvct_link)	
endef

define bootstrap_strip
$(TRG) : $(EPOCBLDABS)/$(NAME).in
	$(call startrule,bootstrap_strip) \
	$(FROMELF) --bin --output $$@ $$< \
	$(call endrule,bootstrap_rvct_strip)
endef

define bootstrap_h2inc
# How to translate the .h files to .inc
$(1) : $(2)
	$(call startrule,bootstrap_h2inc) \
	$(PERL) $(EPOCROOT)/epoc32/tools/h2inc.pl $$< $$@ ARMASM \
	$(call endrule, bootsrap_h2inc)
endef

# Joins two lists with a 1:1 mapping, separated by a ->
# $(call bootstrap_joinlists,a b c,d e f) returns a->d b->e c->f
define bootstrap_joinlists
$(join $(1),$(addprefix ->,$(2)))
endef

# Path for generic source files
BASESRCPATH := $(E32PATH)/eka/kernel/$(CPU)

# Generic source files
BASESOURCES := $(foreach S,$(BASESOURCES_NAMES),$(addprefix $(BASESRCPATH)/,$(S)))

HEADERS:= $(E32PATH)/eka/include/kernel/kernboot.h $(E32PATH)/eka/include/kernel/arm/bootdefs.h $(E32PATH)/eka/include/e32rom.h $(GENINCLUDES_HEADERS) $(HEADERS_MM)

# Generated include files
BOOTSTRAP_GENINCLUDES := $(foreach f,$(HEADERS),$(basename $(notdir $(f))).$(INCEXT))

# Non-generated generic include files
ifeq ($(BASEINCLUDES),)
BASEINCLUDES := $(E32PATH)/eka/include/kernel/$(CPU)/bootcpu.inc $(E32PATH)/eka/include/kernel/$(CPU)/bootmacro.inc
endif
INCLUDES2 := $(foreach f,$(INCLUDES2),$(basename $(f)).$(INCEXT))

# Generic object files
FULLBASEOBJECTS := $(foreach src, $(BASESOURCES_NAMES), $(addprefix $(EPOCBLDABS)/,$(basename $(src)).o))

# Platform specific object files
FULLOBJECTS := $(foreach src, $(SOURCES), $(addprefix $(EPOCBLDABS)/,$(basename $(notdir $(src))).o))

LINKOBJECTS := $(FULLBASEOBJECTS) $(FULLOBJECTS)

# Generated include files with paths
FULLGENINCLUDES := $(addprefix $(EPOCBLDABS)/,$(BOOTSTRAP_GENINCLUDES))

CLEANTARGETS := $(CLEANTARGETS) $(FULLBASEOBJECTS) $(FULLOBJECTS) $(LINKOBJECTS) $(FULLGENINCLUDES) 

CLEANTARGETS := $(CLEANTARGETS) $(EPOCBLDABS)/$(NAME).in $(join $(basename $(EPOCBLDABS)/$(NAME).in),.sym)

JOINED_INC := $(call bootstrap_joinlists,$(FULLGENINCLUDES),$(HEADERS))
$(foreach J,$(JOINED_INC),$(eval $(call bootstrap_h2inc,$(word 1,$(subst ->, ,$(J))),$(word 2,$(subst ->, ,$(J))) | $(EPOCBLDABS))))

# How to strip linked object to binary
$(eval $(call bootstrap_strip,$(TRG),$(EPOCBLDABS)/$(NAME).in))

LISTFILE := $(foreach f,$(FULLBASEOBJECTS),$(join $(basename $(f)),.lst)) $(foreach f,$(FULLOBJECTS),$(join $(basename $(f)),.lst))
CLEANTARGETS := $(CLEANTARGETS) $(LISTFILE)

JOINED_BASEOBJECTS := $(call bootstrap_joinlists,$(FULLBASEOBJECTS),$(BASESOURCES))

$(foreach J,$(JOINED_BASEOBJECTS),$(eval $(call bootstrap_asm,$(word 1,$(subst ->, ,$(J))),$(EPOCBLDABS)/%.o,$(word 2,$(subst ->, ,$(J))) $(BASEINCLUDES) $(FULLGENINCLUDES) $(INCLUDES2) | $(EPOCBLDABS))))

JOINED_OBJECTS := $(call bootstrap_joinlists,$(FULLOBJECTS),$(SOURCES))

$(foreach J,$(JOINED_OBJECTS),$(eval $(call bootstrap_asm,$(word 1,$(subst ->, ,$(J))),$(EPOCBLDABS)/%.o,$(word 2,$(subst ->, ,$(J))) $(BASEINCLUDES) $(FULLGENINCLUDES) $(INCLUDES2) |$(EPOCBLDABS))))

# How to link the object files
$(eval $(bootstrap_link))

# Hook into global targets
TARGET :: $(TRG)

#############################################

# --what to show releasables
$(eval $(call whatmacro,$(TRG),USERFLM))
# create directory
CREATABLEPATHS := $(EPOCBLDABS) $(EPOCTRG)
$(call makepath,$(CREATABLEPATHS))
# clean up
$(eval $(call GenerateStandardCleanTarget,$(CLEANTARGETS),$(BUILDLOC)))

endif
