SRC_DIR = $(SRC_TREE)/condor_$(SYSCALL_LIB)
CFLAGS = $(STD_C_FLAGS) -DCONDOR -g

TO_UPPER = $(PLATFORM_DIR)/condor_$(SYSCALL_LIB)/ToUpper

#define SourceFiles \
	CONDOR_sysnames.c CONDOR_syscalls.c \
	_mkckpt.c _updateckpt.c display.c ckpt.c restart.c syscall_mode.c \
	xfer_file.c finfo.c ckpt_main.c map_file.c C_getenv.c \
	stubs.c extern_path.c fake_getmnt.c nfs.c intern_path.c extern_name.c

#define UtilObjects \
	../condor_util_lib/blankline.o ../condor_util_lib/config.o \
	../condor_util_lib/do_connect.o ../condor_util_lib/dprintf.o \
	../condor_util_lib/dprintf_config.o ../condor_util_lib/except.o \
	../condor_util_lib/expr.o ../condor_util_lib/history.o \
	../condor_util_lib/job_queue.o ../condor_util_lib/ltrunc.o \
	../condor_util_lib/mkargv.o ../condor_util_lib/perror.o \
	../condor_util_lib/proc.o ../condor_util_lib/condor_config.o \
	../condor_util_lib/condor_errlst.o ../condor_util_lib/signames.o \
	../condor_util_lib/status.o ../condor_util_lib/strdup.o \
	../condor_util_lib/stricmp.o ../condor_util_lib/update_rusage.o

#define XdrObjects \
	../condor_xdr_lib/xdr_Init.o ../condor_xdr_lib/xdr_direntries.o \
	../condor_xdr_lib/xdr_expr.o ../condor_xdr_lib/xdr_fdset.o \
	../condor_xdr_lib/xdr_io.o ../condor_xdr_lib/xdr_itimerval.o \
	../condor_xdr_lib/xdr_mach_rec.o ../condor_xdr_lib/xdr_ports.o \
	../condor_xdr_lib/xdr_prio_rec.o ../condor_xdr_lib/xdr_proc.o \
	../condor_xdr_lib/xdr_ptr.o ../condor_xdr_lib/xdr_record.o \
	../condor_xdr_lib/xdr_rlimit.o ../condor_xdr_lib/xdr_rusage.o \
	../condor_xdr_lib/xdr_stat.o ../condor_xdr_lib/xdr_statfs.o \
	../condor_xdr_lib/xdr_status.o ../condor_xdr_lib/xdr_timeval.o \
	../condor_xdr_lib/xdr_timezone.o ../condor_xdr_lib/xdr_wait.o

#define ObjectFiles \
	UtilObjects XdrObjects CONDOR_sysnames.o CONDOR_syscalls.o \
	_mkckpt.o _updateckpt.o display.o ckpt.o restart.o syscall_mode.o \
	xfer_file.o finfo.o ckpt_main.rem.o map_file.o \
	extern_path.o fake_getmnt.o nfs.o intern_path.o \
	extern_name.o stubs.o condor_shr.o

all_target( libcondor.a libckpt.a condor_rt0.o syscall.shr.o syscall.exp libfort.a )
tags_target(SourceFiles)
depend_target(SourceFiles)
program_target($(TO_UPPER),ToUpper.o,$(NULL),release)
prev_library_target(UtilObjects,../condor_util_lib,$(SRC_DIR))
prev_library_target(XdrObjects,../condor_xdr_lib,$(SRC_DIR))

release::
	cp syscall.exp $(RELEASE_DIR)/lib
	cp C_getenv.o $(RELEASE_DIR)/lib

#
# Create a replacement for libc.a by copying it and replacing all
# the system calls with our own version.
#
libcondor.a:	ObjectFiles
	rm -f libcondor.a
	cp $(LIBC) libcondor.a
	chmod u+w libcondor.a
	AR_DELETE( libcondor.a, shr.o )
	AR_REPLACE( libcondor.a, ObjectFiles )
release:: libcondor.a
	cp libcondor.a  $(RELEASE_DIR)/lib
	RANLIB_TOUCH( $(RELEASE_DIR)/lib/libcondor.a )
clean::
	rm -f libcondor.a ObjectFiles

#
# Create a replacement for libc.a to be linked with programs for
# checkpointing, but not remote execution.  N.B. This is done differently
# than other libraries in that we start out with a copy of libcondor.a,
# and replace ckpt_main.rem.o with ckpt_main.loc.o, (checkpointing
# only, no remote system calls).  Don't use the default rule for this one!
#
libckpt.a: ../condor_$(SYSCALL_LIB)/libcondor.a ckpt_main.loc.o
	rm -f libckpt.a
	cp libcondor.a  libckpt.a
	chmod u+w libckpt.a
	AR_DELETE( libckpt.a, ckpt_main.rem.o )
	AR_REPLACE( libckpt.a, ckpt_main.loc.o )
release:: libckpt.a
	cp libckpt.a $(RELEASE_DIR)/lib
	RANLIB_TOUCH( $(RELEASE_DIR)/lib/libckpt.a )
clean::
	rm -f ckpt_main.rem.o libckpt.a

#
# Condor version of libc.a/shr.o.
# Take the original shr.o and strip out the import from /unix.
# This will force these symbols to be resolved from stubs.o which
# will satisfy them with calls to syscall().
#
condor_shr.o: $(LIBC)
	AR_EXTRACT( $(LIBC), shr.o )
	ld -o condor_shr.o \
		-bnoautoimp -r \
		shr.o
clean::
	rm -f shr.o

#
# Provide the syscall() routine.
# Imports: all system calls from /unix
# Exports: syscall() and CondorErrno
#
syscall.shr.o: syscall.o syscall.exp
	ld -o syscall.shr.o \
		-bM:SRE -T512 \
		-bE:syscall.exp \
		-bI:$(SYSCALLS_EXP) \
		syscall.o
release::
	rm -f $(RELEASE_DIR)/lib/syscall.shr.o
	cp syscall.shr.o $(RELEASE_DIR)/lib
clean::
	rm -f syscall.shr.o syscall.o

#
# Be sure every Condor program contains one object compiled with "-g".
# Otherwise there will not be a DEBUG section in the final executable,
# and the checkpointing code will fail!
#
# In this version we compile with CONDOR defined, and get a
# checkpointing plus remote system call version. 
#
ckpt_main.rem.o: ckpt_main.c
	$(CC) -c $(CFLAGS) -g ckpt_main.c
	mv ckpt_main.o ckpt_main.rem.o

#
# Be sure every Condor program contains one object compiled with "-g".
# Otherwise there will not be a DEBUG section in the final executable,
# and the checkpointing code will fail!
#
# In this version we compile without defining CONDOR, and get a
# checkpoint only, (no remote system calls) version.
#
ckpt_main.loc.o: ckpt_main.c
	$(CC) -c $(STD_C_FLAGS) -g ckpt_main.c
	mv ckpt_main.o ckpt_main.loc.o

#
# Replaces /lib/crt0.o in programs linked for execution with Condor
#
condor_rt0.o: $(CRT0) $(TO_UPPER)
	$(TO_UPPER) $(CRT0) condor_rt0.o main
release::
	cp condor_rt0.o $(RELEASE_DIR)/lib
clean::
	rm -f condor_rt0.o

#
# The aix fortran library libxlf.a defines a getenv() routine which takes 2
# arguments, the name of the environment variable, and buffer in which to
# put the associated value.
#
# The aix C library libc.a defines a getenv routine which takes 1 argument,
# the name of the environment variable, and it returns a pointer to the
# associated value.
#
# Condor programs need to be linked with the condor libraries, which will
# end up calling getenv(), and expecting the C version.  We therefore
# supply our own C version, so that we can get it linked in before the
# fortran version gets linked.
# 
# Of course this will break any fortran programs which call getenv and expect
# the fortran version!  If some AIXpert knows how to fix this, I would be
# very happy to learn about it. -- mike
#
library_target(libfort.a,C_getenv.o,release)
