#
# Copyright (c) 2009 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: 
#

# Default platform
ifeq ($(OS),Windows_NT)
  # On Windows, build symbian
  export BUILDENV = symbian

else
  ifdef EPOCROOT
    # On linux, build symbian if EPOCROOT is defined
    export BUILDENV = symbian
  else
    export BUILDENV = linux
  endif
endif

# Determine if we're building jrt or jrtext
GEN_BUILD_FILES_DIR = $(if $(JRTEXT_SRC_ROOT),$(JRTEXT_SRC_ROOT),$(JAVA_SRC_ROOT))

# S60 release file (written if release is given explicitly on commandline)
RD_JAVA_S60_RELEASE_FILE = $(GEN_BUILD_FILES_DIR)/inc/java_s60_release.hrh

include $(JAVA_SRC_ROOT)/build/Makefile.project

ifeq ($(BUILDENV),symbian)
  export PLATFORM = winscw
  export JAVA_BIN_ROOT=$(EPOCROOT)winscw/c/java
  ifeq ($(JAVA_6_HOME), )
  $(error The environment variable JAVA_6_HOME is not defined)
  endif
  export JAVA_HOME=$(JAVA_6_HOME)
  HASH_CHAR=\#

  ifeq ($(notdir $(basename $(SHELL))),sh)
    # Cygwin & Linux definitions
    BATPREFIX =
    RMFILE = rm $(call PATHFIX, $(1))
    RMFILES = $(RMFILE)
    CATCOMMAND = cat $(call PATHFIX, $(1))
    PATHFIX = $(subst \,/,$(1))
    ifeq ($(OS),Windows_NT)
      ANT := sh ant
    else
      ANT := ant
    endif
    SBS = MAKELEVEL=&&MAKEFLAGS=&&MFLAGS=&&sbs
    CPPCMD = gcc -E -x assembler-with-cpp -P -undef -w -traditional-cpp
    IFNOTEXIST = if [ ! -f $(1) ]; then $(2); fi
    IFEXIST = if [ -f $(1) ]; then $(2); fi
    DEVNULL = /dev/null
    MKDIR = if [ ! -d $(1)]; then mkdir $(1); fi
    RMDIR = if [ -d $(1)]; then rmdir $(1); fi
    DELTREE = if [ -d $(1)]; then rm -fr $(1); fi
    PATHSEARCH = $(firstword $(wildcard $(addsuffix /$(1),$(subst :, ,$(PATH)))))

  else
    # CMD.exe definitions
    BATPREFIX = cmd /c call
    RMFILE = $(if $(wildcard $(1)),del $(call PATHFIX,$(wildcard $(1))),echo Nothing to delete from $(1) > $(DEVNULL))
    RMFILES = $(RMFILE)
    CATCOMMAND = type $(call PATHFIX, $(1))
    PATHFIX = $(subst /,\,$(1))
    ANT := $(BATPREFIX) ant.bat
    SBS = set MAKELEVEL=&&set MAKEFLAGS=&&set MFLAGS=&&sbs
    CPPCMD = $(EPOCROOT)epoc32/gcc/bin/cpp -lang-asm -P -undef
    IFNOTEXIST = if not exist $(1) $(2)
    IFEXIST = if exist $(1) $(2)
    DEVNULL = NUL
    MKDIR = if not exist $(1) mkdir  $(1)
    RMDIR = if exist $(1) rmdir  $(1)
    DELTREE = if exist $(1) rmdir /S/Q $(1)
    PATHSEARCH = $(firstword $(wildcard $(addsuffix /$(1),$(subst ;, ,$(PATH)))))

  endif

  ifndef QMAKE_DEFINED
    QMAKESPEC_JAVA=$(if $(USESBS),symbian-sbsv2,symbian-abld)

    ifndef QTDIR
      ifneq ($(wildcard $(EPOCROOT)epoc32/tools/qmake*),)
        # Use platform's qmake
	QMAKE := $(EPOCROOT)epoc32/tools/qmake -spec $(QMAKESPEC_JAVA)
      else
        # Use builtin qmake
	QTDIR_JAVA := $(JAVA_SRC_ROOT)/tools/qt_symbian
	QMAKE := $(QTDIR_JAVA)/qmake -spec $(QTDIR_JAVA)/mkspecs/symbian-abld
      endif
    else
      QMAKE := $(QTDIR)/bin/qmake -spec $(QTDIR)/mkspecs/$(QMAKESPEC_JAVA)
    endif
    QMAKE := $(call PATHFIX,$(QMAKE))
    QMAKE_DEFINED = 1
    export QMAKE
    export QMAKE_DEFINED
  endif

  # Prevent enclosing ant options from propagating to Java compilation
  export ANT_ARGS=
  export ANT_OPTS=
  BLDMAKE = perl -S bldmake.pl
  ABLDMAKE = set MAKELEVEL=&&set MAKEFLAGS=&&set MFLAGS=&&make -r
  CODESCANNER = codescanner -c $(JAVA_SRC_ROOT)/build/codescanner_high.xml  -o std $(1) | perl -ne "print if /error:/;"

  ifdef NODEP
  # Makmake does not make dependency checks with this flag
  export NO_DEPENDENCIES=-nd
  endif


else
  export PLATFORM = linux
  QMAKE = qmake
  ANT := ant
  HASH_CHAR=\\\#

  ifndef JAVA_BIN_ROOT
  export JAVA_BIN_ROOT=$(JAVA_SRC_ROOT)/bin
  endif

  ifndef LD_LIBRARY_PATH_SET
  export LD_LIBRARY_PATH_SET=1
  export LD_LIBRARY_PATH:=$(JAVA_BIN_ROOT)/lib:$(LD_LIBRARY_PATH)
  endif

  PATHFIX = $(subst \,/,$(1))
  RMFILE = rm $(call PATHFIX, $(1))
  CATCOMMAND = cat $(1)
  DEVNULL = /dev/null
endif

# Ensure that VARIANT, CFG and PLATFORM are in lower case
ifeq ($(VARIANT),DEBUG)
  override VARIANT := debug
endif
ifeq ($(VARIANT),RELEASE)
  override VARIANT := release
endif
ifeq ($(CFG),UDEB)
  override CFG := udeb
endif
ifeq ($(CFG),UREL)
  override CFG := urel
endif
ifeq ($(PLATFORM),WINSCW)
  override PLATFORM := winscw
endif
ifeq ($(PLATFORM),ARMV5)
  override PLATFORM := armv5
endif

# If CFG is defined, use it instead of VARIANT (with case insensitive match)
ifdef CFG
  ifeq ($(CFG),udeb)
      override VARIANT := debug
  else
    ifeq ($(CFG),urel)
      override VARIANT := release
    else
      $(error CFG value "$(CFG)" is not recognized)
    endif
  endif
endif


# Default to debug
ifndef VARIANT
  VARIANT := debug
endif

# Define CFG based on VARIANT
ifeq ($(VARIANT),debug)
  override CFG := udeb
else
  override CFG := urel
endif

export VARIANT

# Specify RVCT version explicitly by default
OVERRIDE_RVCT = 1
SBSCONFIG = $(PLATFORM)_$(CFG)
ifeq ($(OVERRIDE_RVCT),1)
  ifeq ($(PLATFORM),armv5)
    ifdef RD_JAVA_S60_RELEASE_10_1_ONWARDS
      SBSCONFIG = $(PLATFORM)_$(CFG).rvct4_0
      RVCTBIN = $(RVCT40BIN)
    else 
      SBSCONFIG = $(PLATFORM)_$(CFG).rvct2_2
      RVCTBIN = $(RVCT22BIN)
    endif
    export PATH:=$(RVCTBIN);$(subst $(RVCT40BIN),,$(subst $(RVCT22BIN),,$(PATH)))
  endif
endif

ANT_PROJECT_DEFINES = $(foreach d,$(PROJECT_DEFINES),-D$d=1)

COMPONENT_NAME ?= $(notdir $(CURDIR))

# Following targets should be defined by every level
.PHONY: all export java native clean cleanexport reallyclean test qmake clean_generated clean_java clean_native builddefinefile generatejavaversion projectdefinefile javareleasables commonjavareleasables clean_build_defines clean_javaversion clean_project_defines freeze exec

# Default target
default_target: all

# Silent / verbose building
ifndef JAVAVERBOSE
.SILENT:
ANT += -q
endif
ifeq ($(JAVAVERBOSE),1)
ANT += -q
endif
ifeq ($(JAVAVERBOSE),2)
endif
ifeq ($(JAVAVERBOSE),3)
ANT += -v
endif

PHASEINFO = @echo $(if $(1),$(1),$@)	$(CURDIR)

# Empty target, forcing target compilation
FORCE:

# Common ----------------------------------

# Export files need a build_defines file
BUILDDEFINEFILE := $(GEN_BUILD_FILES_DIR)/inc/build_defines.hrh
QBUILDDEFINEFILE := $(GEN_BUILD_FILES_DIR)/inc/build_defines.pri

# Project define file is needed by export and native phases of nonqt components
# Native builds require project_defines.hrh for nonqt-mmp-files
PROJECTDEFINEFILE = $(GEN_BUILD_FILES_DIR)/inc/project_defines.hrh

# Java version file
JAVAVERSIONFILE := $(GEN_BUILD_FILES_DIR)/inc/java.txt


ifndef FIRSTTIME_FLAG
export FIRSTTIME_FLAG = 1
FIRSTTIME = 1
endif

# Write file on first make evaluation
ifdef FIRSTTIME

# Echo the S60 release before export
ifeq ($(BUILDENV),symbian)
all init export configure qmake java native releasebuild test: echo_s60_release
echo_s60_release:
	@echo Building on top of S60 $(RD_JAVA_S60_RELEASE), PLATFORM=$(PLATFORM) VARIANT=$(VARIANT)
endif

include $(JAVA_SRC_ROOT)/build/makefile.javaversion

COMMA = ,
JAVA_VERSION_ = $(subst .,$(COMMA),$(JAVA_VERSION))

configure: create_inc_dir generateconffiles generatereleasefile

create_inc_dir:
	$(call MKDIR, $(call PATHFIX,$(GEN_BUILD_FILES_DIR)/inc))

generateconffiles:
	@echo Writing $(BUILDDEFINEFILE) ... \
	$(shell $(call CATCOMMAND,$(JAVA_SRC_ROOT)/build/templates/generated_header.txt) > $(BUILDDEFINEFILE)) \
	$(shell echo $(HASH_CHAR)define RD_JAVA_VERSION $(JAVA_VERSION_)>> $(BUILDDEFINEFILE)) \
	$(shell echo $(HASH_CHAR)define RD_JAVA_BIN_VERSION_NONQT $(shell python $(JAVA_SRC_ROOT)/build/buildutils/ver2binver.py mmp $(JAVA_VERSION))>> $(BUILDDEFINEFILE)) \
	$(foreach d, $(PROJECT_DEFINES), $(shell echo $(HASH_CHAR)define $d>> $(BUILDDEFINEFILE)))
	@echo Writing $(QBUILDDEFINEFILE) ... \
	$(shell $(call CATCOMMAND,$(JAVA_SRC_ROOT)/build/templates/generated_header_hash.txt) > $(QBUILDDEFINEFILE)) \
	$(shell echo RD_JAVA_BIN_VERSION = $(shell python $(JAVA_SRC_ROOT)/build/buildutils/ver2binver.py qmake $(JAVA_VERSION))>> $(QBUILDDEFINEFILE)) \
	$(shell echo PROJECT_DEFINES *= $(PROJECT_DEFINES) >> $(QBUILDDEFINEFILE))
	@echo Writing $(JAVAVERSIONFILE) \
	$(shell echo $(JAVA_VERSION)>$(JAVAVERSIONFILE))
ifeq ($(BUILDENV),symbian)
	@echo Writing $(PROJECTDEFINEFILE) ... \
	$(shell $(call CATCOMMAND,$(JAVA_SRC_ROOT)/build/templates/generated_header.txt) > $(PROJECTDEFINEFILE)) \
	$(foreach d, $(PROJECT_DEFINES), $(shell echo MACRO $d >> $(PROJECTDEFINEFILE)))
ifdef RD_JAVA_EPOCALLOWDLLDATA_FIX
	@echo EPOCALLOWDLLDATA >> $(PROJECTDEFINEFILE)
endif
endif

generatereleasefile:
	@echo Writing $(RD_JAVA_S60_RELEASE_FILE) ... \
	$(shell $(call CATCOMMAND,$(JAVA_SRC_ROOT)/build/templates/generated_header_hash.txt) > $(RD_JAVA_S60_RELEASE_FILE)) \
	$(shell echo RD_JAVA_S60_RELEASE=$(RD_JAVA_S60_RELEASE)>$(RD_JAVA_S60_RELEASE_FILE))

# Clean file on clean_generated
clean_configure: clean_conffiles

clean_conffiles:
	$(call RMFILES,$(BUILDDEFINEFILE) $(QBUILDDEFINEFILE) $(JAVAVERSIONFILE) \
	$(PROJECTDEFINEFILE) $(RD_JAVA_S60_RELEASE_FILE))

endif # FIRSTTIME


# Symbian ----------------------------------
ifeq ($(BUILDENV),symbian)

# Get build directory in symbian
# Remove drive letter
DIRECTORY = $(wordlist 2,2,$(subst :, ,$(CURDIR)))
# Set makefile directory
BUILDDIR  = /epoc32/build$(DIRECTORY)

VERBOSE   = VERBOSE=-s

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

endif
