#
#
#
# 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. 
#
######################################################################
#
# Makefile for building the MID Profile. It should not contain any
# targets for VM-specific tools.
#
# Most frequently used targets:
#
#    all   -- build optimized and debug versions of the core JDK.
#    midp  -- build the midp executable
#
######################################################################

# Cancels implicit rules
#
include $(MIDP_DIR)/build/common/makefiles/CancelImplicits.gmk

# Tool targets
#
include $(MIDP_DIR)/build/common/makefiles/Tools.gmk

# Build system
# We make the output directory as one of the vpaths 
# since generated code will go there.
#
vpath % $(MIDP_OUTPUT_DIR)

# ================================================================
# Pre-Java-Dependency-Search for classes.zip
#
# Lists of Java files to be compiled.  This rule must occur before any
# other rule for classes.zip so that the message is emitted and the
# list files are removed first.  Evaulation of subsequent dependencies
# will have the side effect of populating the list files.
# 
$(MIDP_OUTPUT_DIR)/classes.zip::
	@echo " ... searching updated .java files" $(LOG)
	$(remove_filelist)
	$(remove_genfilelist)

# Additional configuration settings, by each subsystem
#
include $(MIDP_DIR)/build/common/makefiles/SubsystemDefs.gmk

# I3 tests.
#
# Run through the .class file list and apply pattern matching and
# substitution to generate the list of i3 test classes.  A class file
# is considered to be a test case if its name begins with 'Test'
# and isn't named 'TestCase'.  Note that we are using the class file list
# constructed from the java filenames, so we don't have to worry about
# inner classes.  If we were to search for classes, we'd have to filter
# out classes whose names contain '$'.
#
ifeq ($(USE_I3_TEST), true)

I3_TEMPNUM := $(shell echo $$$$)
I3_TEMP1 = /tmp/midpi3testa$(I3_TEMPNUM)
I3_TEMP2 = /tmp/midpi3testb$(I3_TEMPNUM)

I3_TEST_REPOSITORY_DIR=$(GENERATED_DIR)/classes/com/sun/midp/i3test
I3_TEST_REPOSITORY_FILE=Repository.java
I3_TEST_REPOSITORY=$(I3_TEST_REPOSITORY_DIR)/$(I3_TEST_REPOSITORY_FILE)

$(MIDP_OUTPUT_DIR)/classes.zip:: $(JTWI_I3TEST_JAVA_FILES)
	@echo $? >> $(REBUILD_JAVA_LIST)
	@echo " ... generating I3 test repository " $(LOG)
	@mkdir -p $(I3_TEST_REPOSITORY_DIR)
	@echo $(JTWI_I3TEST_JAVA_FILES) | \
	    tr ' ' '\012' | \
	    grep 'i3test/.*/Test' | \
	    sed -e 's,/i3test/,/+++/,' -e 's,.*/+++/,,' | \
	    sed -e 's,\.java$$,,' -e 's,/,.,g' \
	    > $(I3_TEMP1)
	@( \
	echo '// This file is generated by "MIDP.gmk".  Do not edit.' ; \
	echo 'package com.sun.midp.i3test;' ; \
	echo 'class Repository {' ; \
	echo '    static String testNames[] = {' ; \
	sed -e 's,^,        ",' \
	    -e 's/$$/",/' $(I3_TEMP1) ; \
	echo '    };' ; \
	echo '}' \
	) > $(I3_TEMP2)
	@if [ -f $(I3_TEST_REPOSITORY) ] && \
	    cmp -s $(I3_TEMP2) $(I3_TEST_REPOSITORY) ; then \
	    echo " ... no changes" ; \
	else \
	    echo " ... found" `wc -l < $(I3_TEMP1)` "test cases" $(LOG) ; \
	    cp $(I3_TEMP2) $(I3_TEST_REPOSITORY) ; \
	fi
	@rm -f $(I3_TEMP1) $(I3_TEMP2)

$(MIDP_OUTPUT_DIR)/classes.zip:: $(I3_TEST_REPOSITORY)
	    @echo $(I3_TEST_REPOSITORY) >> $(GENERATED_JAVA_LIST)

endif

# ================================================================
# Post-Java-Dependency-Search for classes.zip
# Creates classes.zip
#
$(MIDP_OUTPUT_DIR)/classes.zip::$(GENERATED_JAVA_FILES) \
				$(PREVERIFY_CMD) \
                                $(CONFIGURATOR_JAR_FILE) \
                                $(UNCOMPRESSED_VM_CLASSES) \
                                FORCE
	@if [ '!' -d $(CLASSBINDIR) ]; then \
		mkdir -p $(CLASSBINDIR); \
	 fi
	$(compilefiles)
	$(remove_filelist)
	$(remove_genfilelist)

try_build_classes:: $(MIDP_OUTPUT_DIR)/classes.zip

build_classes:
	$(A)$(MAKE) try_build_classes

# Main rule
#
ROM_DEPS += $(BINDIR) $(OBJ_DIR) jpp build_classes

ifeq ($(TARGET_VM), cldc_vm)
ROM_DEPS += $(MIDP_OUTPUT_DIR)/ROMImage.cpp
else
# cdc_dummies is defined in cdc_vm.gmk
ROM_DEPS += cdc_dummies $(ROMSTRUCTS_H)
endif

rom: $(ROM_DEPS)

ifneq ($(USE_JSR_234), true)
ifneq ($(USE_JSR_135), true)
ifneq ($(USE_MIDP_ABB), true)
MMAPI_TARGET = mmapi
endif
endif
endif

MIDP_RESOURCES += ams_resources
MIDP_DEPS += $(ROM_DEPS) $(MMAPI_TARGET) $(OBJ_FILES) $(KDP_CMD) \
             all_tools not_yet_implemented


not_yet_implemented::

# If you're using a multi-CPU host, or if your MIDP_OUTPUT_DIR points
# to a network filed system, use something like "make parallel NUM_JOBS=3".
# That will speed up your build process.
#
# Note that the "rom" target cannot be built in parallel (because the way
# we generate the list of Java source file dependencies). Therefore, the
# build will break if you just do a "make all -j 3".

ifndef NUM_JOBS
NUM_JOBS = 2
endif

parallel_all:: $(VERIFY_BUILD_ENV) $(DISPLAY_BUILD_ENV) rom
	@echo "Building in parallel mode with $(NUM_JOBS) jobs"
	@$(MAKE) -j $(NUM_JOBS) IN_PARALLEL_BUILD=true
	@echo "Finished building in parallel mode with $(NUM_JOBS) jobs"

# If use static properties flag is false, we just merely 
# copy the config files with properties for dynamic usage.
#
ifeq ($(USE_STATIC_PROPERTIES), false)
MIDP_DEPS += $(LIBDIR)/internal.config $(LIBDIR)/system.config

$(LIBDIR)/internal.config $(LIBDIR)/system.config: $(LIBDIR) $(SUBSYSTEM_CONFIGURATION_CONFIG_FILES)
	if [ -f $@ ];then \
		rm -f $@; \
	fi $(LOG)
	-(cp $(GENERATED_DIR)/$(subst $(MIDP_OUTPUT_DIR),,$@) $@) $(LOG)
	-(chmod +w $@) $(LOG)
endif

# All object files
#
# Note: we need explicit rules for all generated C source files because they are
# not detected by vpath.
$(OBJ_DIR)/%.o: %.c
	@echo " ... $@" $(LOG)
	$(A)$(CC) $(CFLAGS) -c $(CC_OUTPUT)$@ `$(call fixcygpath, $<)` $(LOG)

$(OBJ_DIR)/ROMImage.o: $(MIDP_OUTPUT_DIR)/ROMImage.cpp
	@echo " ... $@" $(LOG)
	$(A)$(CPP) $(CPPFLAGS) -c $(CC_OUTPUT)$@ `$(call fixcygpath, $<)` $(LOG)

$(OBJ_DIR)/nativeFunctionTable.o: $(MIDP_OUTPUT_DIR)/nativeFunctionTable.cpp
	@echo " ... $@" $(LOG)
	$(A)$(CPP) $(CPPFLAGS) -c $(CC_OUTPUT)$@ `$(call fixcygpath, $<)` $(LOG)

$(OBJ_DIR)/%.o: %.cpp
	@echo " ... $@" $(LOG)
	$(A)$(CPP) $(CPPFLAGS) -c $(CC_OUTPUT)$@ `$(call fixcygpath, $<)` $(LOG)

$(MIDP_OUTPUT_DIR) $(OBJ_DIR) $(CLASSBINDIR) $(BINDIR) $(LIBDIR) $(VERIFYDIR) $(STORAGEDIR) $(CLASSBINDIR)/icons $(CLASSBINDIR)/audio:
	@echo " ... creating directories $@ " $(LOG)
	$(A)mkdir -p $@ $(LOG)

clean::
	@if [ "$(MIDP_OUTPUT_DIR)" = "/" -o -d "$(MIDP_OUTPUT_DIR)/SCCS" ]; then \
	    echo "ERROR: Invalid MIDP_OUTPUT_DIR $(MIDP_OUTPUT_DIR)"; \
	    exit 1; \
	 else \
	    echo; \
	    echo " ... deleting directory $(MIDP_OUTPUT_DIR)"; \
	    sleep 3; \
	    rm -rf "$(MIDP_OUTPUT_DIR)"; \
	 fi $(LOG)

midp: $(MIDP_DEPS) $(MIDP_RESOURCES)


# Phonies to avoid accidents.
#
.PHONY: all midp clean run_configurator ams_resources rom FORCE
