###############################################################################
#___INFO__MARK_BEGIN__
##########################################################################
#
#  The Contents of this file are made available subject to the terms of
#  the Sun Industry Standards Source License Version 1.2
#
#  Sun Microsystems Inc., March, 2001
#
#
#  Sun Industry Standards Source License Version 1.2
#  =================================================
#  The contents of this file are subject to the Sun Industry Standards
#  Source License Version 1.2 (the "License"); You may not use this file
#  except in compliance with the License. You may obtain a copy of the
#  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
#
#  Software provided under this License is provided on an "AS IS" basis,
#  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
#  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
#  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
#  See the License for the specific provisions governing your rights and
#  obligations concerning the Software.
#
#  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
#
#  Copyright: 2001 by Sun Microsystems, Inc.
#
#  All Rights Reserved.
#
##########################################################################
#___INFO__MARK_END__

# Variable definitions for targets to build

JAPILIB_DIR   = $(SDIR)/libs/japi
CLASSES_DIR   = $(SDIR)/libs/jdrmaa

JAPILIB_10    = japi.o
JAPILIB_OBJS  = $(JAPILIB_10)

DRMAALIB         = libdrmaa$(SHAREDEXT)
DRMAALIB_V       = libdrmaa$(SHAREDEXT).1.0
DRMAALIB_10_NJ  = drmaa.o
DRMAALIB_10_ALL = $(DRMAALIB_10_NJ) com_sun_grid_drmaa_SessionImpl.o

# This computed macro is here to allow libdrmaa to be built with or without
# JNI files
DRMAALIB_10_OBJS = $(DRMAALIB_10_$(JNI_TARGET))
DRMAALIB_OBJS    = $(DRMAALIB_10_OBJS)
DRMAA_OBJS       = $(EVCLIB_OBJS) $(GDILIB_OBJS) \
                   $(SGEOBJLIB_OBJS) $(SGEOBJDLIB_OBJS) \
                   $(CULLLIB_OBJS) $(COMMLIB_OBJS) \
                   $(COMMLISTSLIB_OBJS) $(UTILIB_DRMAA_OBJS) 

JAPI_ADD_SRC = parse_job_cull.c parse_qsub.c read_defaults.c sge_options.c
JAPI_ADD_OBJS = $(JAPI_ADD_SRC:.c=.o)
JAPI_DEPEND_OBJS = $(JAPI_ADD_OBJS)

###############################################################################
# global rules: all, clean, depend, messages
###############################################################################
drmaa_all: drmaalib japilib drmaa_extras

drmaa_clean: japi_clean drmaa_extras_clean
	$(RM) $(DRMAALIB_OBJS) libdrmaa.a $(DRMAALIB) $(DRMAALIB_V) libdrmaa$(JAVA_SHAREDEXT)

drmaa_extras_clean: drmaa_howto_clean
	$(RM) test_drmaa test_drmaa.o example example.o test_drmaa_perf \
         test_drmaa_perf.o test_drmaa_sync \
         test_drmaa_sync.o test_drmaa_no_bin test_drmaa_no_bin.o \
         test_drmaa_issue1768 test_drmaa_issue1768.o \
         test_drmaa_issue1832 test_drmaa_issue1832.o \
         test_drmaa_issue1892 test_drmaa_issue1892.o \
         test_drmaa.1.0 test_drmaa.o \
         compile compile.o drmaa.lds

japilib: $(JAPILIB)
drmaalib: $(DRMAALIB)

drmaa_extras: drmaa_howto_all test_drmaa example test_drmaa_perf test_drmaa_sync \
              test_drmaa_no_bin test_drmaa_issue1768 test_drmaa_issue1832 \
              test_drmaa_issue1892 compile \

japi_clean:
	$(RM) $(JAPILIB_OBJS) libjapi.*.*$(LIBEXT) libjapi$(JAVA_SHAREDEXT)

drmaa_depend:
	$(DEPEND) -f$(JAPILIB_DIR)/japi_dependencies $(JAPILIB_DIR)/*.c

drmaa_messages:
	@echo Creating $@
	@for i in `ls $(JAPILIB_DIR)/*.c`; do \
	   $(CC) $(CFLAGS) -DEXTRACT_MESSAGES -E $$i | $(XGETTEXT) - ;\
	done

# used with GNU ld via DRMAALIB_10_OBJS
drmaa.lds:
	@ln -sf $(JAPILIB_DIR)/drmaa.lds

###############################################################################
# rules to build library
###############################################################################
libdrmaa$(SHAREDEXT).1.0:  $(JAPILIB_10) $(JAPI_DEPEND_OBJS) $(DRMAALIB_OBJS) $(DRMAA_OBJS) $(WINGRIDLIB_DEP) drmaa.lds
	$(SHAREDLD) $(SHARED_LFLAGS) $(SONAME_LFLAG_DRMAA10) -o libdrmaa$(SHAREDEXT).1.0 $(DRMAALIB_10_OBJS) $(DRMAA_OBJS) $(JAPILIB_10) $(JAPI_ADD_OBJS) \
      $(SECLIB) $(WINGRIDLIB) $(LIBS) $(LIBRARY_LIBS) $(DLLIB) \
   && ../scripts/nonreentrant.sh -a `../dist/util/arch` -w libdrmaa$(SHAREDEXT).1.0 && true

# Needed to compile howtos, examples, and unit tests
libdrmaa$(SHAREDEXT): $(DRMAALIB_V)
	rm -f libdrmaa$(SHAREDEXT) ; cp libdrmaa$(SHAREDEXT).1.0 libdrmaa$(SHAREDEXT)
	../scripts/link_jnilib.sh libdrmaa  $(SHAREDEXT) $(JAVA_SHAREDEXT)

###############################################################################
# include dependencies for object files generated by sge_depend
###############################################################################
include $(JAPILIB_DIR)/japi_dependencies

###############################################################################
# include examples from the howto
###############################################################################
include $(JAPILIB_DIR)/howto/Makefile

###############################################################################
# rules to build object codes
###############################################################################
drmaa.o: $(JAPILIB_DIR)/drmaa.c
	$(CC) $(CFLAGS) $(SHARED_CFLAGS) -c $(JAPILIB_DIR)/drmaa.c -o drmaa.o

japi.o: $(JAPILIB_DIR)/japi.c 
	$(CC) $(CFLAGS) $(SHARED_CFLAGS) -c $(JAPILIB_DIR)/japi.c -o japi.o

com_sun_grid_drmaa_SessionImpl.o: $(CLASSES_DIR)/com_sun_grid_drmaa_SessionImpl.c
	$(CC) $(CFLAGS) $(SHARED_CFLAGS) -c $(CLASSES_DIR)/com_sun_grid_drmaa_SessionImpl.c -o com_sun_grid_drmaa_SessionImpl.o

#---------------------------------------------------------------------

test_drmaa: test_drmaa.1.0
	rm -f test_drmaa ; cp test_drmaa.1.0 test_drmaa

test_drmaa.1.0: test_drmaa.o $(DRMAALIB) $(WINGRIDLIB_DEP)
	$(LD_WRAPPER) $(CC) -o $@ $(LFLAGS) test_drmaa.o libdrmaa$(SHAREDEXT).1.0 $(GDILIB) $(COMMLIB) $(COMMLISTSLIB) $(SGEOBJLIB) $(SGEOBJDLIB) $(CULLLIB) $(UTILIB) $(WINGRIDLIB) $(LIBS) -lssl -lcrypto -ldl

test_drmaa.o: $(JAPILIB_DIR)/test_drmaa.c
	$(CC) $(CFLAGS) -c $(JAPILIB_DIR)/test_drmaa.c -o test_drmaa.o

test_drmaa_sync: test_drmaa_sync.o $(DRMAALIB)
	$(LD_WRAPPER) $(CC) -o $@ $(LFLAGS) test_drmaa_sync.o -ldrmaa $(LIBS)

test_drmaa_sync.o: $(JAPILIB_DIR)/test_drmaa_sync.c
	$(CC) $(CFLAGS) -c $(JAPILIB_DIR)/test_drmaa_sync.c

test_drmaa_mcpu: test_drmaa_mcpu.o $(DRMAALIB)
	$(LD_WRAPPER) $(CC) -o $@ $(LFLAGS) test_drmaa_mcpu.o -ldrmaa

test_drmaa_mcpu.o: $(JAPILIB_DIR)/test_drmaa_mcpu.c
	$(CC) $(CFLAGS) -c $(JAPILIB_DIR)/test_drmaa_mcpu.c

test_drmaa_perf: test_drmaa_perf.o $(DRMAALIB)
	$(LD_WRAPPER) $(CC) -o $@ $(LFLAGS) test_drmaa_perf.o -ldrmaa $(LIBS)

test_drmaa_perf.o: $(JAPILIB_DIR)/test_drmaa_perf.c
	$(CC) $(CFLAGS) -c $(JAPILIB_DIR)/test_drmaa_perf.c

test_drmaa_no_bin: test_drmaa_no_bin.o $(DRMAALIB)
	$(LD_WRAPPER) $(CC) -o $@ $(LFLAGS) test_drmaa_no_bin.o -ldrmaa $(LIBS)

test_drmaa_no_bin.o: $(JAPILIB_DIR)/test_drmaa_no_bin.c
	$(CC) $(CFLAGS) -c $(JAPILIB_DIR)/test_drmaa_no_bin.c

test_drmaa_issue1768: test_drmaa_issue1768.o $(DRMAALIB)
	$(LD_WRAPPER) $(CC) -o $@ $(LFLAGS) test_drmaa_issue1768.o -ldrmaa $(LIBS)

test_drmaa_issue1768.o: $(JAPILIB_DIR)/test_drmaa_issue1768.c
	$(CC) $(CFLAGS) -c $(JAPILIB_DIR)/test_drmaa_issue1768.c

test_drmaa_issue1832: test_drmaa_issue1832.o $(DRMAALIB)
	$(LD_WRAPPER) $(CC) -o $@ $(LFLAGS) test_drmaa_issue1832.o -ldrmaa $(LIBS)

test_drmaa_issue1832.o: $(JAPILIB_DIR)/test_drmaa_issue1832.c
	$(CC) $(CFLAGS) -c $(JAPILIB_DIR)/test_drmaa_issue1832.c

test_drmaa_issue1892: test_drmaa_issue1892.o $(DRMAALIB)
	$(LD_WRAPPER) $(CC) -o $@ $(LFLAGS) test_drmaa_issue1892.o -ldrmaa $(LIBS)

test_drmaa_issue1892.o: $(JAPILIB_DIR)/test_drmaa_issue1892.c
	$(CC) $(CFLAGS) -c $(JAPILIB_DIR)/test_drmaa_issue1892.c

example: example.o $(DRMAALIB)
	$(LD_WRAPPER) $(CC) -o example $(LFLAGS) example.o -ldrmaa $(LIBS)

example.o: $(JAPILIB_DIR)/example.c
	$(CC) $(CFLAGS) -c $(JAPILIB_DIR)/example.c

compile: compile.o $(DRMAALIB)
	$(LD_WRAPPER) $(CC) -o compile $(LFLAGS) compile.o -ldrmaa $(LIBS)

compile.o: $(JAPILIB_DIR)/compile.c
	$(CC) $(CFLAGS) -c $(JAPILIB_DIR)/compile.c
