#*******************************************************************************
# E.S.O. - ALMA project
#
# "@(#) $Id: javaMakefile,v 1.81 2010/07/09 12:48:42 alopatin Exp $"
#
# who       when      what
# --------  --------  ----------------------------------------------
# mzampare  2002-11-12 VLTSW20020559 and VLTSW20020518 implemented
# mzampare  27/06/02  SHELL changed to ksh
# mzampare  26/06/02  vltMakeJava2Dependencies -> acsMakeJavaDependencies
# mzampare  20/06/02  created
#
#
#************************************************************************
#   NAME
#   javaMakefile - language specific Makefile
#
#   DESCRIPTION
#   The javaMakefile handles the targets to make, install and clean 
#   Java code (all files in the src directory of a software module 
#   with extension .java)
#
#   All the files .java are compiled calling the Java compiler. 
#   The resulting .class files are put in the ../object directory.
#   Afterwards, all the files .class existing in the ../object 
#   directory are combined in one or more  JAR archive files which are
#   created in the ../lib directory. 
#   
#   The "make install" will copy this jar file under the lib 
#   directory of the installation root (INTROOT, ACSROOT...)
#
#   The names of the jar files are determined by the user who will 
#   have to define in the module's Makefile the variable: 
#   JARFILES = <jjj1> <jjj2> .... (without extension .jar)
#   (name of the resulting jar files without extension)
#   plus corresponding directories in the form of
#   <jjj1>_DIRS = <dir11> <dir12> ... (space separated)
#   <jjj2>_DIRS = <dir21> <dir22> ...
# 
#   Additional non java files can be added specifying the directories they
#   are in, relative to the module src directory, in the following form:
#   <jjj>_EXTRAS = images
#
#    or
#
#   <jjj>_EXTRAS = sounds/rock/queen.au sounds/classic/Beethoven.au
#
#   Compiler options to the javac compiler can be passed by setting the
#   USER_JFLAGS and <jjj>_JFLAGS.
#
#   A single Jarfile may require to use the 'endorsed' behaviour, in which case
#   <jjj>_ENDORSED="on" can be used.
#
#   The above featuers can also be used for jarfiles listed in the tag
#   COMPONENTS_JARFILES, which is used to single out jarfiles which act as ACS
#   components and therefore follow a somewhat special treatment.
#   In particular, they are not installed straight under lib, but under
#   lib/ACScomponents
#
#   Notice   that   a jarfile may   be listed  in   either   JARFILES or 
#   COMPONENTS_JARFILES but not in both
#
#   The CLASSPATH variable is generated by a custom script. 
#   It takes the initial value of CLASSPATH from the shell and augments it 
#   with all jarfiles contained in <module>/lib, $INTROOT/lib and $ACSROOT/lib, 
#   in that sequence. 
#   If the script is called with the -components option, only the directories
#   containing ACS components jarfiles are returned.
#
#   Java byte compiled code goes under <module>/object, possibly 
#   under a directory structure reflecting the Java package 
#   structure. Jarfiles are created, as specified in the JARFILES 
#   variable and moved to the lib directory.
#
#   The DEBUG flag can be set, to make original Java files to be appended
#   in the jarfile.
#
#   make clean deletes all *.class files from object 
#
#   FILES
#   vltMakeJavaClasspath: Utility used by acsMakefile to dynamically 
#   generate the CLASSPATH for Java stuff, prior to compilation. 
#   acsMakeJavaDependencies: Utility used by acsMakefile to create 
#   the makefile to build a Java Jarfile
#
#
#   ENVIRONMENT
#
#   CAUTIONS
#
#   EXAMPLES
#
#   SEE ALSO
#   ACSROOT/include/acsMakefile
#             ACS wide definitions for UNIX or VxWorks
#             This file includes the javaMakefile
#
#   BUGS
#
#------------------------------------------------------------------------
#
SHELL=/bin/bash
ACS_CO=ACScomponents
######################################
# phony section
######################################
.PHONY : clean_java
.PHONY : install_java
.PHONY : do_java

######################################
# incrementing tasks
######################################
JARFILES_LIST = $(JARFILES) $(JARFILES_L)
C_JARFILES_LIST = $(COMPONENTS_JARFILES) $(COMPONENTS_JARFILES_L)

TMP_DEP_JAR := $(addprefix ../object/, $(addsuffix .djar, $(JARFILES_LIST) ))
TMP_DEP_C_JAR := $(addprefix ../object/, $(addsuffix .dcjar, $(C_JARFILES_LIST) ))
ifndef MAKE_VXWORKS
ifeq ($(call mustBuild,Java),true)
ifneq ($(strip $(JARFILES_LIST) $(COMPONENTS_JARFILES)),)
    CLEAN_TARGET := $(CLEAN_TARGET) clean_java
endif

ifneq ($(strip $(JARFILES) $(COMPONENTS_JARFILES)),)
    INSTALL_TARGET := $(INSTALL_TARGET) install_java
endif

ifneq ($(strip $(JARFILES_LIST) $(COMPONENTS_JARFILES)),)
    ALL_TARGET := $(ALL_TARGET) do_java
endif
endif # must build Java
endif
######################################
# targets specification
######################################

## install
##
install_java:
ifneq ($(strip $(COMPONENTS_JARFILES) $(JARFILES)),) 
	-@$(ECHO) "install_java: java_begin $(foreach member, $(JARFILES), $(LIB)/$(member).jar  ) $(foreach member, $(COMPONENTS_JARFILES), $(LIB)/$(ACS_CO)/$(member).jar  )" >>$(FILE)
	-@$(ECHO) "java_begin:" >>$(FILE)
	-@$(ECHO) "	-@echo \"\"; $(ECHO) \"......jarfiles:\"" >>$(FILE)
	-$(AT) for member in $(foreach name, $(JARFILES), $(name) ); \
		do \
		    $(ECHO) "$(LIB)/$${member}.jar: ../lib/$${member}.jar" >>$(FILE);\
		    $(ECHO) "	-\$$(AT)echo \"\t$${member}.jar\";\\" >>$(FILE);\
		    $(ECHO) "	cp ../lib/$${member}.jar $(LIB);\\" >>$(FILE);\
		    $(ECHO) "	chmod $(P644) $(LIB)/$${member}.jar" >>$(FILE);\
		done
	-@$(ECHO) "	-@echo \"\"; $(ECHO) \"......component jarfiles:\"" >>$(FILE)
	-$(AT) for member in $(foreach name, $(COMPONENTS_JARFILES), $(name) ); \
		do \
		    $(ECHO) "$(LIB)/$(ACS_CO)/$${member}.jar: ../lib/$(ACS_CO)/$${member}.jar" >>$(FILE);\
		    $(ECHO) "	-\$$(AT)echo \"\t$${member}.jar\";\\" >>$(FILE);\
		    $(ECHO) "	cp ../lib/$(ACS_CO)/$${member}.jar $(LIB)/$(ACS_CO)/$${member}.jar ;\\" >>$(FILE);\
		    $(ECHO) "	chmod $(P644) $(LIB)/$(ACS_CO)/$${member}.jar" >>$(FILE);\
		done
endif

##  clean
##
clean_java:
	-$(AT)$(RM) vltMake_clean_java  $(foreach member,  $(JARFILES_LIST), ../lib/$(member).jar ../object/$(member).djar ../object/$(member) ) $(foreach member,  $(COMPONENTS_JARFILES), ../lib/$(ACS_CO)/$(member).jar ../object/$(member).dcjar ../object/$(member) )../object/vltMakeIndexFiles.del $(foreach member, $(JDIR), ../object/$(member) )   $(OUTPUT)
#	-$(AT)find ../object -type d ! -name object ! -name CVS | $(XARGS) -i $(RM) {}
#	-$(AT)find ../object -type f ! -name .cvsignore | $(XARGS) -i $(RM) {}
	-@$(ECHO) " .\c"


## all
##
# - for Automatic Dependencies for Jarfiles
#
../object/%.djar: Makefile
	- @echo "== Dependencies: $@"
	$(AT)acsMakeJavaDependencies $(*F) djar lib "$($(*F)_DIRS)" "$($(*F)_EXTRAS)" "$(USER_JFLAGS)" "$($(*F)_JFLAGS)" "$($(*F)_ENDORSED)" "$(DEBUG)" >../object/$*.djar

../object/%.dcjar:
	- @echo "== Dependencies: $@"
	$(AT)acsMakeJavaDependencies $(*F) dcjar lib/$(ACS_CO) "$($(*F)_DIRS)" "$($(*F)_EXTRAS)" "$(USER_JFLAGS)" "$($(*F)_JFLAGS)" "$($(*F)_ENDORSED)" "$(DEBUG)" >../object/$*.dcjar

do_java:  do_names_check do_components_check $(TMP_DEP_JAR) $(TMP_DEP_C_JAR)
	$(AT) for member in $(foreach name, $(JARFILES_LIST), $(name) ); \
		do \
		make -f ../object/$${member}.djar ../lib/$${member}.jar ||  exit ;\
		done; for member in $(foreach name, $(C_JARFILES_LIST), $(name) ); \
		do \
		echo "== Making $${member} component jarfile ";\
		make -f ../object/$${member}.dcjar ../lib/$(ACS_CO)/$${member}.jar || exit ;\
		done
#
# this ugly thing should not stay in here.
# It conceptually does not belong here.
# But for the time being it ain't any better place to go.
#
do_names_check:
ifneq ($(strip $(JARFILES)),)
ifneq ($(strip $(IDL_FILES)),)
	$(AT) for member in $(foreach name, $(JARFILES), $(name) ); \
	                do \
			  for idl  in $(foreach name, $(IDL_FILES), $(name) ); do \
	               if [ "$${member}" == "$${idl}" ]; then \
                            echo "** Name	clash between IDL_FILES and JARFILES  ($${member})\n" ; NAME_CLASH ; \
         fi \
         done; \
        done; 
endif
endif

do_components_check:
	$(AT) for member in $(foreach name, $(COMPONENTS_JARFILES), $(name) ); \
		do \
		for jarfile in $(JARFILES); do \
			if [ "$${jarfile}" == "$${member}" ]; then \
			echo "== Component $${member} specification clashes with jarfile $${jarfile}"; \
			/bin/false ;\
			fi \
		done; \
		done; 

# modify vltMakeJavaClasspath

javadoc: 
	$(AT)echo "......Javadoc:"
	$(AT)mkdir -p ../doc/api/html
	$(AT)for member in $(foreach jarfile, $(JARFILES_LIST), $($(jarfile)_DIRS)) ;\
		do \
		javadoc $(JavadocOptions) -classpath `vltMakeJavaClasspath` -d ../doc/api/html `find $${member} -type f -name \*.java` > /dev/null 2>&1 ;\
		done

