#  Copyright (c) 1997-2009
#  Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Darmstadt, Germany)
#  http://www.polymake.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 9349 2009-10-01 15:54:28Z gawrilow $

.PHONY: all all-deps all-own all-native all-native-h clean install install-native

### provide 'all' be a default target
all : all-deps all-own all-native

include ${ProjectTop}/support/utils.make
include ${BuildDir}/conf.make

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

JavaBuildDir := ${ProjectTop}/java_build
SourceTop := ${ProjectTop}/java/src
ClassBuildDir := ${JavaBuildDir}/${PackageBase}/${PackageName}

ifndef SourceDir
  SourceDir := $(wildcard ${SourceTop}/${PackageBase}/${PackageName})
endif
ifndef SourceDir
  $(error SourceDir undefined, cannot locate source files)
endif

-include ${SourceDir}/Makefile.inc

Jars := ${JavaBuildDir} $(patsubst %,${JavaBuildDir}/%.jar, ${ImportApps})
JarFile := ${JavaBuildDir}/${PackageName}.jar

ifdef NativeClasses
  NativeLib := ${BuildDir}/lib/lib${PackageName}-JNI$(if ${Debug},-d).$(subst bundle,jnilib,${SO})
  NativeClassHeaders := $(addprefix ${ClassBuildDir}/,$(addsuffix .h, ${NativeClasses}))
  ifeq (${SO},bundle)
    # MacOS java binaries are usually multi-arch, the native library must match
    NativeArchFlags := $(addprefix -arch ,$(filter-out ppc%, $(shell lipo -info ${JAVA} | sed -n -e 's/Architectures .* are: //p')))
  endif
endif

###
ifdef JavaBuild
  WCSourceDir := ${ProjectTop}/apps/${PackageName}/java
  ifdef .svn
    ifeq ($(wildcard ${WCSourceDir}),)
      WCSourceDir := ${SourceDir}
    endif
  endif
  IgnoreSources := $(patsubst ${WCSourceDir}/%,${SourceDir}/%,$(call _get_no_export,${WCSourceDir},broken|test))

  _list_sources = $(filter-out ${IgnoreSources}, $(wildcard ${SourceDir}$(if $(2),/$(2))/*.$(1)))
  _2classes = $(patsubst %.java,%[.\$$]*class,$(patsubst ${SourceTop}/%,%,$(1)))
  Subdirs := $(notdir $(wildcard $(addprefix ${SourceDir}/,${JavaSubpackages})))

  Sources := $(call _list_sources,java)
  OtherFilesSrc := $(call _list_sources,txt) $(foreach d, ${Subdirs}, $(call _list_sources,txt,$d))
  OtherFiles := $(patsubst ${SourceDir}/%,%,${OtherFilesSrc})

  ifdef ImportApps
    ifdef BeFirst
      define _MakeDependence
	$(MAKE) --print-directory -C ${JavaBuildDir}/${PackageBase}/$(1) BeFirst= $(2)

      endef
      define _MakeDependences
	$(foreach m,${ImportApps},$(call _MakeDependence,$m,$(1)))
      endef
    endif
  endif
endif

ifndef _MakeDependences
  _MakeDependences := : ;
endif

ifdef JavaBuild
  all-own : ${JarFile}

  define _compile_java
	${JAVAC} ${JAVACFLAGS} -d ${JavaBuildDir} -classpath $(call _list2path,${CLASSPATH} ${Jars} $(1)) $(2)

  endef

  ${JarFile} : ${Sources} $(foreach d, ${Subdirs}, $(call _list_sources,java,$d)) ${OtherFilesSrc}
	$(call _compile_java,,${Sources})
	$(foreach d, ${Subdirs}, $(call _compile_java,${$dJars},$(call _list_sources,java,$d)))
	cd ${JavaBuildDir}; \
	${JAR} cvf $(notdir ${JarFile}) $(call _2classes,${Sources}); \
	$(foreach d, ${Subdirs}, ${JAR} uvf $(notdir ${JarFile}) $(call _2classes,$(call _list_sources,java,$d));)
ifdef OtherFiles
	for f in ${OtherFiles}; do cp -p ${SourceDir}/$$f $$f; done
	cd ${JavaBuildDir}; ${JAR} uvf $(notdir ${JarFile}) $(addprefix ${PackageBase}/${PackageName}/, ${OtherFiles})
endif

else
  all-own : ;@:
endif

all-deps:
	+@$(call _MakeDependences,all)

ifdef NativeClasses
  define _MakeNative
	$(MAKE) --print-directory -C ${BuildDir}/apps/${PackageName} $(1) SourceDir='$${ProjectTop}/apps/${PackageName}/java' \
		SharedModules="${NativeClasses}" OwnShared='$${BuildDir}/lib/$(notdir ${NativeLib})' BeFirst= \
		ExtraCFLAGS="-I${CURDIR} $(addprefix -I,${JNI_PATH} $(dir $(wildcard ${JNI_PATH}/*/jni_md.h)))" Libs= \
		CCLD='$${CC}' $(if ${NativeArchFlags},ARCHFLAGS='${NativeArchFlags}' ProcessDep=none)

  endef

  all-native : all-native-h
	@+$(call _MakeNative,all-lib)

  all-native-h : ${NativeClassHeaders}

  ${NativeClassHeaders} : ${ClassBuildDir}/%.h : ${SourceDir}/%.java
	cd ${JavaBuildDir}; \
	${JAVAH} -o ${PackageBase}/${PackageName}/$(notdir $@) $(if ${JavaBuild},,-classpath ${PackageName}.jar) \
		 $(subst /,.,${PackageBase}).${PackageName}.$*
	@touch $@

  install-native : all-native
	${PERL} ${INSTALL_PL} -m 444 ${NativeLib} ${InstallArch}/lib
else
  all-native install-native : ;@:
endif

### delete all non-sources
clean:
  ifdef JavaBuild
	rm -f *.class ${JarFile} ${OtherFiles}
  endif
  ifdef NativeClasses
	rm -f ${NativeClassHeaders} ${NativeLib} $(patsubst %,${BuildDir}/apps/${PackageName}/%.o,${NativeClasses})
  endif
	@+$(call _MakeDependences,clean)

InstallJavaDir := ${InstallTop}/jars

install : all
  ifdef BeFirst
	${PERL} ${INSTALL_PL} -d -m ${DirMask} -U ${InstallJavaDir}
  endif
	${PERL} ${INSTALL_PL} -m 444 ${JarFile} ${InstallJavaDir}
	+@$(call _MakeDependences,install)


# Local Variables:
# mode: Makefile
# End:
