#
# 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: Header file exporting Function Like Makefile (FLM)
#              The file destinations are relative to EPOCROOT
#

# Find Java build dir of component $(NAME) from $(TO_BLDINF). 
# (TO_BLDINF refers to the directory having initial bld.inf, i.e.
#  - combined build root, which contains directory $(NAME)
#  - $(NAME)/group (9.2 S60)
#  - $(NAME)/group/sf (9.2 SF)
#  - $(NAME) (10.1 SF/S60) 
#  therefore the $(NAME)-directory is found from finding first directory having
#  subsystem.mk, going first down to $(NAME) and then up to max. three directory levels)

JAVABUILDDIR:= $(dir $(firstword $(wildcard $(TO_BLDINF)/$(NAME)/subsystem.mk $(TO_BLDINF)/subsystem.mk $(TO_BLDINF)/../subsystem.mk $(TO_BLDINF)/../../subsystem.mk)))

# General Java makefile command
JAVACMD:= make -C $(JAVABUILDDIR) -f subsystem.mk EPOCROOT=$(EPOCROOT)/ \
               PLATFORM=$(PLATFORM) CFG=$(CFG) BLD_INF_JAVA=1

# General build command, targetting armv5 urel files
JAVABUILDCMD:= make -C $(JAVABUILDDIR) -f subsystem.mk EPOCROOT=$(EPOCROOT)/ \
               PLATFORM=armv5 CFG=urel BLD_INF_JAVA=1 java finalactions

# Marker file to prevent multiple Java compilations of the
# same platform/cfg, when building e.g. device variants.
NATIVEJAVAMARKER := $(EPOCBLD)/nativejavamarker_$(NAME)_$(PLATFORM)_$(CFG).done

# Java build artifact file source and target
JAVABUILD_ARTIFACT_SRC := $(EPOCROOT)/epoc32/release/armv5/urel
JAVABUILD_ARTIFACT_DST := $(EPOCROOT)/epoc32/release/$(PLATFORM)/$(CFG)

define compilenativejava

# This is the general Java compilation, done once for the component
ifeq ($(GUARD_nativejavatarget_$(NAME)),)
GUARD_nativejavatarget_$(NAME):=1
.PHONY:: nativejavatarget_$(NAME)
nativejavatarget_$(NAME):
	$(call startrule,compilenativejava,,javasources) \
	$(JAVABUILDCMD) \
	$(call endrule,compilenativejava)

# If component depends on some other Java compilation, do the dependant
# first (if on same build sequence)
ifneq ($(DEPENDS),)
.PHONY:: nativejavatarget_$(DEPENDS)
nativejavatarget_$(NAME): nativejavatarget_$(DEPENDS)
nativejavatarget_$(DEPENDS): ignored_dependency
endif
endif 

# Phony ignored dependency, just so that we have something to use when needed
ifeq ($(GUARD_ignored_dependency),)
GUARD_ignored_dependency:=1
.PHONY:: ignored_dependency
ignored_dependency:
endif

# Actual resource target, one for each platform/cfg variant
ifeq ($(GUARD_$(call sanitise,$(NATIVEJAVAMARKER))),)
GUARD_$(call sanitise,$(NATIVEJAVAMARKER)):=1

RESOURCE:: $(NATIVEJAVAMARKER)

# Copy the build artifacts to target
$(NATIVEJAVAMARKER): nativejavatarget_$(NAME)
ifeq ($(filter armv5_urel ARMV5_urel ARMV5_UREL,$(PLATFORM)_$(CFG)),)
	$(call makepath,$(JAVABUILD_ARTIFACT_DST)/z/resource/java/jvm/lib/jrt)
	$(call makepath,$(JAVABUILD_ARTIFACT_DST)/z/resource/java/security)
	$(call startrule,copynativejavaresults) \
	$(foreach f,java_impl_cdc.jar java_impl_cldc.jar java_platform_api.jar java_public_api.jar java_signature_test.jar, $(GNUCP) -v $(JAVABUILD_ARTIFACT_SRC)/$f $(JAVABUILD_ARTIFACT_DST) &&) \
	$(GNUCP) -Rv $(JAVABUILD_ARTIFACT_SRC)/z/resource/java/jvm/lib/jrt $(JAVABUILD_ARTIFACT_DST)/z/resource/java/jvm/lib && \
	$(GNUCP) -Rv $(JAVABUILD_ARTIFACT_SRC)/z/resource/java/security $(JAVABUILD_ARTIFACT_DST)/z/resource/java \
	$(call endrule,copynativejavaresults)
endif
endif

# Use normal clean for all variants
CLEAN::
	$(call startrule,cleannativejava,,javasources) \
	$(JAVACMD) clean_java \
	$(call endrule,cleannativejava)
endef

$(eval $(compilenativejava))

RELEASABLES:= $(shell $(JAVACMD) -s javareleasables)
$(eval $(call whatmacro,$(RELEASABLES),WHATRESOURCES))

$(eval $(call GenerateStandardCleanTarget,$(NATIVEJAVAMARKER)))
