# $Project: polymake $$Id: java.make,v 1.3 2004/11/25 13:07:47 gawrilow Exp $  -*- makefile -*-
#

.PHONY: all do_all clean install 

### provide 'all' be a default target
all : do_all ;

PackageName := $(notdir $(CURDIR))

ifndef JavaBuildDir
  $(error no java configured. reconfigure using "make configure" in $(ProjectTop))
endif

### compiler options depending on the chosen debugging features
JavacSwitches:=
JAVAC ?= javac
JAR ?= jar

### locate the sources
ifndef SourceDir
  SourceDir := $(wildcard $(ProjectTop)/*/$(PackageName)/java)
endif
ifndef SourceDir
  $(error SourceDir undefined, cannot locate source files)
endif

-include $(SourceDir)/Makefile.inc
-include $(SourceDir)/.export

Classes := $(patsubst %, %.class, $(FileOrder))
JarFile := $(JavaBuildDir)/$(PackageName).jar

### put classpath together
Jars := $(JavaViewTop)/jars/javaview.jar $(JavaBuildDir) \
	$(patsubst %,$(JavaBuildDir)/%.jar, $(UseModules)) 
override CLASSPATH += $(Jars)

empty:=
space:= $(empty) $(empty)
CP := $(subst $(space),:,$(CLASSPATH))

### determine the libraries to be built and/or linked with 
ifdef UseModules
  define _MakeModule
	$(MAKE) --print-directory -C $(JavaBuildDir)/$(PackageBase)/$(1) $(2)

  endef
  define _MakeModules
	$(foreach m,$(UseModules),$(call _MakeModule,$m,$(1)))
  endef
endif

ifndef _MakeModules
  _MakeModules := : ;
endif

define _create_jar
$(UMASK) $(JAR) cvf $(JarFile) \
	$(patsubst %, -C $(JavaBuildDir) '%', \
	$(addprefix $(PackageBase)/$(PackageName)/, $(wildcard *.class) $(OtherFiles)))

endef

ifdef Classes
  do_all:
	+@$(call _MakeModules,all)
	@$(MAKE) --no-print-directory $(Classes)
	@$(MAKE) --no-print-directory $(JarFile)

  $(JarFile) : $(Classes) $(addprefix $(SourceDir)/, $(OtherFiles))
	$(foreach file,$(OtherFiles), \
	  cp $(SourceDir)/$(file) $(JavaBuildDir)/$(PackageBase)/$(PackageName)/$(file))
	$(_create_jar)
else
  do_all : ;@:
endif # Classes

define _java_compile
	$(UMASK) $(JAVAC) -O $(JavacSwitches) -classpath $(CP) -d $(JavaBuildDir) $<

endef


%.class : %.java
	$(_java_compile)

vpath %.java $(SourceDir)

### delete all non-sources
clean:
	rm -f *.class $(JarFile) $(OtherFiles)
	@+$(call _MakeModules,clean)

InstallJavaDir := $(InstallTop)/jars

install : all
	$(PERL) $(INSTALL_PL) -d -m $(DirMask) -U $(InstallJavaDir)
	$(PERL) $(INSTALL_PL) -m 444 $(JarFile) $(InstallJavaDir)

# if nothing to do:
install% : ;

