#*************************************************************************
# Copyright (c) 2002 The University of Chicago, as Operator of Argonne
#     National Laboratory.
# Copyright (c) 2002 The Regents of the University of California, as
#     Operator of Los Alamos National Laboratory.
# SPDX-License-Identifier: EPICS
# EPICS Base is distributed subject to a Software License Agreement found
# in file LICENSE that is included with this distribution. 
#*************************************************************************


ifeq ($(BUILD_CLASS),HOST)

#-------------------------------------------------------
# java jdk1.1.5 definitions

INSTALL_JAVA = $(INSTALL_LOCATION)/javalib
UNINSTALL_DIRS += $(INSTALL_JAVA)

JAVA_INC = $(JAVA_DIR)/include
JAVA_BIN = $(JAVA_DIR)/bin
JAVA_INCLUDES += -I$(JAVA_INC) -I$(JAVA_INC)/$(word 1, $(subst -, ,$(T_A))) -I$(COMMON_DIR)

JAVACCMD = $(subst \,/,$(JAVA_BIN)/javac$(EXE) $(CLASSPATH) $(SOURCEPATH) $(JAVACFLAGS))
JAVAHCMD = $(subst \,/,$(JAVA_BIN)/javah$(EXE) -d $(COMMON_DIR) -force $(CLASSPATH) $(JAVAHFLAGS))
JARCMD   = $(subst \,/,$(JAVA_BIN)/jar$(EXE) $(JAR_OPTIONS) $@ $(JARINPUT) $(JARPACKAGES))

#-------------------------------------------------------
vpath %.java .. 
vpath %.jar .. $(COMMON_DIR)

empty:=
space:= $(empty) $(empty)
CLASSPATH = -classpath $(subst $(space),:,$(strip $(USR_PRECLASSPATH) $(INSTALL_JAVA) $(USR_CLASSPATH)))
SOURCEPATH = -sourcepath .:..:../..

#-------------------------------------------------------
# Java directory

ifdef JAVA
JAVA_DIRECTORY_TARGETS += $(INSTALL_JAVA)
endif

#-------------------------------------------------------
# Java native method C header files

JAVAHFLAGS += $(USR_JAVAHFLAGS)
JAVAINC_CLASSFILES += $(addprefix $(INSTALL_JAVA)/,$(subst _,/,$(subst .h,.class,$(JAVAINC))))
JAVAINC_CLASSNAMES += $(subst _,.,$(subst .h,,$(JAVAINC)))
COMMON_JAVAINC += $(addprefix $(COMMON_DIR)/,$(JAVAINC))

#-------------------------------------------------------
# Java class files

JAVACFLAGS += $(USR_JAVACFLAGS)
CLASSES += $(subst .java,.class,$(JAVA))
INSTALL_CLASSES = $(addprefix $(INSTALL_JAVA)/,$(CLASSES))

TESTCLASSES += $(subst .java,.class,$(notdir $(TESTJAVA)))
COMMON_TESTCLASSES += $(addprefix $(COMMON_DIR)/,$(TESTCLASSES))
DEPTESTJAVA += $(subst .class,.java,$(TESTCLASSES))

#-------------------------------------------------------
# Java jar file

INSTALL_JAR =$(addprefix $(INSTALL_JAVA)/,$(JAR))
JARMANIFEST += $(firstword $(MANIFEST) $(JAR_MANIFEST))
JARDEPFILES += $(addprefix $(INSTALL_JAVA)/,$(subst .java,.class,$(JAVA)) $(JAR_INPUT))
JARINPUT += $(foreach inp,$(JAR_INPUT),-C $(INSTALL_JAVA) $(subst .java,.class,$(inp)))
JARPACKAGES += $(foreach pkg,$(JAR_PACKAGES),-C $(INSTALL_JAVA) $(subst .,/,$(pkg)))
PACKAGEDIRS += $(foreach pkg,$(JAR_PACKAGES),$(addprefix $(INSTALL_JAVA)/,$(subst .,/,$(pkg))))

ifneq ($(JARMANIFEST),)
JAR_OPTIONS = cvmf $(JARMANIFEST)
else
JAR_OPTIONS = cvf
endif

#-------------------------------------------------------
# Java doc  definitions
ifdef JAVADOC
JAVADOCFLAGS += $(USR_JAVADOCFLAGS)
JAVADOCCMD = $(subst \,/,$(JAVA_BIN)/javadoc$(EXE) $(CLASSPATH) $(SOURCEPATH) $(JAVADOCFLAGS))
INSTALL_JAVADOC = $(addprefix $(INSTALL_HTML)/,$(JAVADOC))/index.html
endif

#-------------------------------------------------------
# Java rules

all:    install

install: buildInstall

buildInstall : build

rebuild: clean install

inc:  $(JAVA_DIRECTORY_TARGETS) $(INSTALL_CLASSES) $(COMMON_JAVAINC)

build: inc

build:  $(COMMON_TESTCLASSES)

buildInstall : $(INSTALL_JAR) $(INSTALL_JAVADOC)

#This clean works only from O.* dirs.
buildclean: 
	@$(RMDIR) $(INSTALL_CLASSES) $(PACKAGEDIRS)
	@$(RM) $(INSTALL_JAR) $(INSTALL_JAVADOC)
	@$(RM) $(COMMON_TESTCLASSES) $(COMMON_JAVAINC)

ifdef JAVA_DIRECTORY_TARGETS
$(JAVA_DIRECTORY_TARGETS):
	$(MKDIR) $@
endif

$(COMMON_JAVAINC):$(JAVAINC_CLASSFILES)
	$(ECHO) Creating header files $(COMMON_JAVAINC)
	@$(RM) $@
	$(JAVAHCMD) $(JAVAINC_CLASSNAMES)

$(COMMON_TESTCLASSES): $(addprefix ../,$(DEPTESTJAVA))
	$(ECHO) Creating test java class files $(COMMON_TESTCLASSES)
	@$(RM) $@
	$(JAVACCMD) -d $(COMMON_DIR) $^

$(INSTALL_CLASSES): $(addprefix ../,$(JAVA))
	$(ECHO) Creating java class files 
	@$(RM) $@
	$(JAVACCMD) -d $(INSTALL_JAVA) $^

$(INSTALL_JAVADOC): $(addprefix ../,$(JAVA))
	$(ECHO) Creating java doc files 
	@$(RM) $@
	$(JAVADOCCMD) -d $(addprefix $(INSTALL_HTML)/,$(JAVADOC)) $^

$(COMMON_DIR)/%.jar: $(JARDEPFILES)
	$(ECHO) Creating java jar file $@
	@$(RM) $@
	$(JARCMD)

$(INSTALL_JAVA)/%.jar: $(COMMON_DIR)/%.jar
	$(ECHO) "Installing java jar file $@"
	@$(INSTALL) -d -m  $(INSTALL_PERMISSIONS) $< $(@D)

$(INSTALL_JAVA)/%.jar: %.jar
	$(ECHO) "Installing java jar file $@"
	@$(INSTALL) -d -m  $(INSTALL_PERMISSIONS) $< $(@D)

$(addprefix $(INSTALL_JAVA)/, $(JARINPUT)): $(INSTALL_JAVA)/%: ../%
	$(ECHO) "Installing jar input file $@"
	@$(INSTALL) -d -m  $(INSTALL_PERMISSIONS) $< $(@D)

.PHONY: all install buildInstall rebuild clean build inc

.PRECIOUS: $(COMMON_JAVAINC)

endif
