#   
#
# Copyright  1990-2006 Sun Microsystems, Inc. All Rights Reserved.
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version
# 2 only, as published by the Free Software Foundation. 
# 
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License version 2 for more details (a copy is
# included at /legal/license.txt). 
# 
# You should have received a copy of the GNU General Public License
# version 2 along with this work; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
# 02110-1301 USA 
# 
# Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
# Clara, CA 95054 or visit www.sun.com if you need additional
# information or have any questions. 
#

ifeq ($(JAVACALL_DIR),)
$(error JAVACALL_DIR is not set.)
endif

include $(JAVACALL_DIR)/build/list.gmk

.PHONY: javacall_common create_output_dirs copy_include_files clean doxygen

javacall_common: create_output_dirs copy_include_files

PREFIX_true=1

#Form list of objects
VPATH+=$(foreach jsr,$(JSR_LIST), $(if $(PREFIX_$(USE_$(jsr))), $($(jsr)_JC_IMPL_PATH)/$($(jsr)_JC_DIR)))
PORTING_SOURCE+=$(foreach jsr,$(JSR_LIST), $(if $(PREFIX_$(USE_$(jsr))), $(notdir $(wildcard $($(jsr)_JC_IMPL_PATH)/$($(jsr)_JC_DIR)/*.c))))


#Form list of header files
JAVACALL_INCLUDE_DIR_SET_LOCAL=$(foreach jsr,$(JSR_LIST), $(if $(PREFIX_$(USE_$(jsr))), $($(jsr)_JC_DIR)))
JAVACALL_INCLUDE_DIR_SET=$(addprefix $(JAVACALL_DIR)/interface/,$(JAVACALL_INCLUDE_DIR_SET_LOCAL))
JAVACALL_INCLUDE_SOURCE_FILES_SET+=$(foreach dir,$(JAVACALL_INCLUDE_DIR_SET),$(wildcard $(dir)/*.h))


JAVACALL_INCLUDE_SOURCE_FILES_SET+= $(JAVACALL_PLATFORM_DEFS_DIR)/javacall_platform_defs.h
JAVACALL_INCLUDE_SOURCE_FILES_SET+= $(JAVACALL_DIR)/interface/javacall_defs.h

ifneq ($(UTILITIES)x, x)
VPATH+=$(JAVACALL_DIR)/implementation/utils
JAVACALL_INCLUDE_SOURCE_FILES_SET+= $(foreach util,$(UTILITIES),$(JAVACALL_DIR)/implementation/utils/inc/$(util).h)
SOURCE_OUTPUT_LIST += $(JAVACALL_DIR)/implementation/utils javacall/implementation
endif

# In case using of the javacall functions wrappers, set the USE_JAVACALL_WRAPPERS variable
ifeq ($(USE_JAVACALL_WRAPPERS),true)
UTILITIES+= wrappers
CFLAGS += -DUSE_JAVACALL_WRAPPERS
ifeq ($(UTILITIES)x, x)
VPATH+=$(JAVACALL_DIR)/implementation/utils
endif
endif

ifeq ($(USE_DEBUG),true)
BUILD=debug
endif

BUILD_EXT_debug=_g
BUILD_EXT=$(BUILD_EXT_$(BUILD))

ifeq ($(JAVACALL_OUTPUT_DIR),)
JAVACALL_OUTPUT_DIR:=output
endif

# The JAVACALL_OUTPUT_DIR has a fixed structure, it should contain:
#   - inc directory, which contains all the headers copied
#   - lib directory, which contains javacall(_g) library and other files
#     required for final linking
#   - obj(_g) directory (optional), which contains all intermediate obj files
#   - ext_lib directory (optional), which contains external libraries
JAVACALL_OUTPUT_INCLUDE_DIR:=$(JAVACALL_OUTPUT_DIR)/inc
JAVACALL_OUTPUT_OBJ_DIR:=$(JAVACALL_OUTPUT_DIR)/obj$(BUILD_EXT)
JAVACALL_OUTPUT_LIB_DIR:=$(JAVACALL_OUTPUT_DIR)/lib
JAVACALL_OUTPUT_EXT_LIB_DIR:=$(JAVACALL_OUTPUT_DIR)/ext_lib

JAVACALL_INCLUDE_TARGET_FILES_SET:=$(foreach file,$(JAVACALL_INCLUDE_SOURCE_FILES_SET),$(JAVACALL_OUTPUT_INCLUDE_DIR)/$(notdir $(file)))

$(JAVACALL_INCLUDE_TARGET_FILES_SET): $(JAVACALL_INCLUDE_SOURCE_FILES_SET)
	@echo Copying include files...
	@cp -f $^ $(JAVACALL_OUTPUT_INCLUDE_DIR)

$(JAVACALL_OUTPUT_DIR) $(JAVACALL_OUTPUT_INCLUDE_DIR) $(JAVACALL_OUTPUT_OBJ_DIR) $(JAVACALL_OUTPUT_LIB_DIR) $(JAVACALL_OUTPUT_EXT_LIB_DIR):
	@echo Creating $@...
	@mkdir -p $@

create_output_dirs: $(JAVACALL_OUTPUT_DIR) $(JAVACALL_OUTPUT_INCLUDE_DIR) $(JAVACALL_OUTPUT_LIB_DIR) $(JAVACALL_OUTPUT_OBJ_DIR) $(JAVACALL_OUTPUT_EXT_LIB_DIR)

copy_include_files: $(JAVACALL_INCLUDE_TARGET_FILES_SET)

clean:
	@rm -rf $(JAVACALL_OUTPUT_DIR)

include $(JAVACALL_DIR)/build/docs.gmk
doxygen: copy_include_files docs_doxy

# Verify source output directory is set
SOURCE_OUTPUT_DIR:
	@if [ -z "$($@)" ]; then \
	    echo "ERROR: $@ must be set"; \
	    exit -1; \
	fi; \
	mkdir -p "$($@)"

define make_source_bundle
		for path in $(1) ; do \
			if [ "$$FROM" != "" ]; then mkdir -p $(SOURCE_OUTPUT_DIR)/$$path ; \
				if [ -d "$$FROM/.svn" ]; then \
					svn export --force -q $$FROM $(SOURCE_OUTPUT_DIR)/$$path/`basename $$FROM` ; \
				else \
					 cp -rf $$FROM $(SOURCE_OUTPUT_DIR)/$$path ; \
				fi ; \
				FROM=; \
			else FROM=$$path ; \
			fi ;\
		done
endef

SOURCE_OUTPUT_LIST += $(foreach jsr,$(JSR_LIST), $(if $(PREFIX_$(USE_$(jsr))), $(JAVACALL_DIR)/interface/$($(jsr)_JC_DIR) javacall/interface))
SOURCE_OUTPUT_LIST += $(JAVACALL_DIR)/interface/javacall_defs.h javacall/interface
SOURCE_OUTPUT_LIST += $(JAVACALL_DIR)/build javacall

source_bundle:: SOURCE_OUTPUT_DIR
	@echo  ... javacall source bundle;
	@$(call make_source_bundle, $(SOURCE_OUTPUT_LIST))

