#  Copyright (c) 1997-2007
#  Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
#  http://www.math.tu-berlin.de/polymake,  mailto:polymake@math.tu-berlin.de
#
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the
#  Free Software Foundation; either version 2, or (at your option) any
#  later version: http://www.gnu.org/licenses/gpl.txt.
#
#  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 for more details.
#-----------------------------------------------------------------------------
#  $Project: polymake $$Id: java.make 7546 2007-01-08 16:34:56Z gawrilow $

.PHONY: all all_modules all_own clean install

### provide 'all' be a default target
all : all_modules all_own

include $(ProjectTop)/support/utils.make
include $(BuildDir)/conf.make

ifndef JavaBuildDir
  $(error java compiler not configured. Please rerun "make configure" in $(ProjectTop))
endif

JavaBuildDir := $(ProjectTop)/$(JavaBuildDir)

### locate the sources
PackageName := $(notdir $(CURDIR))

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

-include $(SourceDir)/Makefile.inc

IgnoreSources := $(foreach f,$(call _expand_var,JavaIgnore),$(if $(findstring .,$f),$f,$f.java))
Sources := $(filter-out $(addprefix %/,$(IgnoreSources)) $(call _get_no_export,$(SourceDir),broken|test), \
                        $(wildcard $(SourceDir)/*.java))
Classes := $(patsubst %.java, %.class, $(notdir $(Sources)))
OtherFiles := $(filter-out $(call _expand_var,JavaIgnore),$(OtherFiles))

Jars := $(JavaBuildDir) $(patsubst %,$(JavaBuildDir)/%.jar, $(UseModules))
JarFile := $(JavaBuildDir)/$(PackageName).jar
	 
###
ifdef UseModules
  ifdef BeFirst
    define _MakeModule
	$(MAKE) --print-directory -C $(JavaBuildDir)/$(PackageBase)/$(1) BeFirst= $(2)

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

ifndef _MakeModules
  _MakeModules := : ;
endif


ifdef Sources
  all_own : $(JarFile)

  $(JarFile) : $(Sources) $(addprefix $(SourceDir)/, $(OtherFiles))
	$(JAVAC) $(JAVACFLAGS) -d $(JavaBuildDir) -classpath $(call _list2path,$(CLASSPATH) $(Jars) $(AddJars)) $(Sources)
	$(if $(OtherFiles),cp $(addprefix $(SourceDir)/, $(OtherFiles)) .)
	cd $(JavaBuildDir); $(JAR) cvf $(notdir $(JarFile)) \
		 $(addprefix $(PackageBase)/$(PackageName)/, $(patsubst %.class,%[.\$$]*,$(Classes)) $(OtherFiles))

else
  all_own : ;@:
endif

all_modules:
	+@$(call _MakeModules,all)

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

InstallJavaDir := $(InstallTop)/jars

install : all
	$(if $(BeFirst),$(PERL) $(INSTALL_PL) -d -m $(DirMask) -U $(InstallJavaDir))
	$(if $(Sources),$(PERL) $(INSTALL_PL) -m 444 $(JarFile) $(InstallJavaDir))
	+@$(call _MakeModules,install)


# Local Variables:
# mode: Makefile
# End:
