# ****************************************************************************
# 
# Copyright 2009 Sandia Corporation. Under the terms of Contract
# DE-AC04-94AL85000 with Sandia Corporation, the U.S. Government
# retains certain rights in this software.
# 
# BSD Open Source License.
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 
#    * Redistributions of source code must retain the above copyright notice,
#      this list of conditions and the following disclaimer.
#    * Redistributions in binary form must reproduce the above copyright
#      notice, this list of conditions and the following disclaimer in the
#      documentation and/or other materials provided with the distribution.
#    * Neither the name of Sandia National Laboratories nor the names of its
#      contributors may be used to endorse or promote products derived from
#      this software without specific prior written permission.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# ****************************************************************************

#
#To change between building 64 and 32 bit binaries, specify on the command line a value for ARCH.
#Can be either "32bit" or "64bit".  Default is 64bit.  Example: "make ARCH=32bit"
#
CCSOURCES = cc/src/pglJavaUtil.cc cc/src/SlbmInterfaceJNI.cc
	
CCOBJECTS=$(CCSOURCES:.cc=.o)

INCLUDE=-Icc/include -I../SLBM/include -I../GeoTessCPP/include -I${JAVA_HOME}/include

LIB=-L../lib -lslbm -lgeotesscpp

ifeq ($(ARCH),32bit)
MACHINE=-m32
else
MACHINE=-m64
endif

# Determine OS
OS := $(shell uname -s)

# OS-specific compiler variables
ifeq ($(OS),SunOS)   
	OUTPUT=../lib/libslbmjni.so
	CCFLAGS = -D$(OS) $(MACHINE) -Kpic -xO5 -dalign -fns -fsimple=2 -ftrap=%none
	COMPILE = CC ${CCFLAGS} ${INCLUDE} -I${JAVA_HOME}/include/solaris -o $@ -c
	LINK = CC ${CCFLAGS} -G -Bdynamic -o $(OUTPUT) ${CCOBJECTS} ${LIB} -lm -lc -lCrun -lCstd -z defs
	VERSION= CC -V
endif
ifeq ($(OS),Linux)
	OUTPUT=../lib/libslbmjni.so
	CCFLAGS = -D$(OS) $(MACHINE) -fPIC -O3 -ffast-math -fno-trapping-math
	COMPILE = gcc ${CCFLAGS} ${INCLUDE} -I${JAVA_HOME}/include/linux  -o $@ -c
	LINK = gcc ${CCFLAGS} -shared -o $(OUTPUT)  ${CCOBJECTS} ${LIB} -lstdc++ -lm 
	VERSION = gcc --version
endif
ifeq ($(OS),Darwin)
	OUTPUT=../lib/libslbmjni.jnilib
	CCFLAGS= -D$(OS) $(MACHINE)
	COMPILE = g++ ${CCFLAGS} ${INCLUDE}  -o $@ -c
	LINK = g++ ${CCFLAGS} -shared -o $(OUTPUT)  ${CCOBJECTS} ${LIB} -lstdc++
	VERSION = g++ --version
endif


#-------------------------------------------------------------------------------
# targets
#-------------------------------------------------------------------------------


all : info clean slbmjni clean_objs javajar done
	


%.o: $($*:.o=.cc)
	${COMPILE} $*.cc

slbmjni : ${CCOBJECTS}
	@echo
	@echo "linking shared library"
	${LINK}
	@echo

clean: clean_objs
	@echo "removing" $(OUTPUT)
	@rm -f $(OUTPUT)
	@echo "removing ../lib/slbmjni.jar"
	@rm -f ../lib/slbmjni.jar
	@echo

clean_objs:
	@echo "removing object files"
	@rm -f cc/src/*.o
	@echo

# build the java jar file.
 javajar:
	@ echo "Building slbmjni.jar file"
	cd java/SlbmInterface/src; javac -classpath src/gov/sandia/gnem/slbmjni/ \
	gov/sandia/gnem/slbmjni/GreatCircleData.java \
	gov/sandia/gnem/slbmjni/GridProfile.java \
	gov/sandia/gnem/slbmjni/GridWeight.java \
	gov/sandia/gnem/slbmjni/Point.java \
	gov/sandia/gnem/slbmjni/QueryNeighborInfo.java \
	gov/sandia/gnem/slbmjni/QueryProfile.java \
	gov/sandia/gnem/slbmjni/SLBMException.java \
	gov/sandia/gnem/slbmjni/SlbmInterface.java \
	gov/sandia/gnem/slbmjni/ZhaoParameters.java;
	cd java/SlbmInterface/src; jar -cf ../../../../lib/slbmjni.jar \
	gov/sandia/gnem/slbmjni/GreatCircleData.class \
	gov/sandia/gnem/slbmjni/GridProfile.class \
	gov/sandia/gnem/slbmjni/GridWeight.class \
	gov/sandia/gnem/slbmjni/Point.class \
	gov/sandia/gnem/slbmjni/QueryNeighborInfo.class \
	gov/sandia/gnem/slbmjni/QueryProfile.class \
	gov/sandia/gnem/slbmjni/SLBMException.class \
	gov/sandia/gnem/slbmjni/SlbmInterface.class \
	gov/sandia/gnem/slbmjni/ZhaoParameters.class;
	rm -f *.class
	@ echo ""

info :
	@ echo
	@ echo "========================================="
	@ echo "Building SLBM_JNI on" $(OS) "with" $(MACHINE)
	@$(VERSION)
	@ echo ""

done :
	@echo "Done building SLBM_JNI"
	@echo ""
