#------------------------------------------------------------------------------
# Copyright 1996-2016 United States Government as represented by the
# Administrator of the National Aeronautics and Space Administration.
# All Rights Reserved.
#------------------------------------------------------------------------------
#
#  NSSDC/CDF						CDF library makefile.
#
#  Version 1.5b, 3-Apr-97, Hughes STX.
#
#  Modification history:
#
#   V1.0  15-Dec-93, J Love	Original version.
#   V1.0a 23-Feb-94, J Love	Added `CC='.
#   V1.0b  6-Apr-94, J Love	Finished `CC='.
#   V1.1  14-Nov-94, J Love	CDF V2.5.
#   V1.2  30-Dec-94, J Love	Corrected dependencies.
#   V1.2a 19-Jan-95, J Love	IRIX 6.x.
#   V1.3  16-Mar-95, J Love	Multi-compiler support, etc.
#   V1.4   3-Apr-95, J Love	POSIX.
#   V1.4a  4-May-95, J Love	Added `cdfmem.c'.
#   V1.5  13-Jun-96, J Love	CDF V2.6.
#   V1.5a 21-Feb-97, J Love	Removed RICE.
#   V1.5b  3-Apr-97, J Love	Removed mysterious `-M' option.
#   V2.0  20-Feb-16, M Liu	Mac OS to build both 32 and 64-bit boundle and
#                               copy libraries into $HOME/lib for Mac.
#
#------------------------------------------------------------------------------
#
# Notes:
#
# `make' on DECstations complains (prints an error message) if an if-then-else
# construct evaluates to FALSE but there is no `false' part.  To prevent this,
# the `NULL' command is executed as the `false' part.
#
#------------------------------------------------------------------------------

.SILENT:

SHELL=/bin/sh
NULL=true

SHARED=
PIC=
CCx=
LDx=
COPTIONS=
SYSLIBS=
SHAREDEXT=
LDOPTIONS=
AROPTIONS=
RANLIB=
VERSION=
MAKE=make
MAKEFILE=
SRCDIR=
BLDDIR=

INCLUDEcdf=../include
ZlibDIR=zlib

CC=$(CCx)
LD=$(LDx)
#AR=gcc-ar
AR=ar
#RANLIBcmd=gcc-ranlib
RANLIBcmd=ranlib
MACos=Darwin

CFLAGS=$(COPTIONS) $(PIC) -I$(INCLUDEcdf) -I$(ZlibDIR)
CFLAGS2=-DZ_PREFIX
LDFLAGS=$(LDOPTIONS)
ARFLAGS=$(AROPTIONS)

ZLIB_OBJs := $(wildcard $(BLDDIR)/$(ZlibDIR)/*.o)

OBJs= $(BLDDIR)/cdflib.o $(BLDDIR)/cdfcre.o $(BLDDIR)/cdfope.o \
      $(BLDDIR)/cdfclo.o $(BLDDIR)/cdfdel.o $(BLDDIR)/cdfsel.o \
      $(BLDDIR)/cdfcon.o $(BLDDIR)/cdfget.o $(BLDDIR)/cdfmisc2.o \
      $(BLDDIR)/cdfread.o $(BLDDIR)/cdfwrite.o $(BLDDIR)/cdfhyper.o \
      $(BLDDIR)/epochuf.o $(BLDDIR)/dirutils.o $(BLDDIR)/cdfput1.o \
      $(BLDDIR)/cdfput2.o $(BLDDIR)/cdfcsi.o $(BLDDIR)/cdffsi.o \
      $(BLDDIR)/cdffii.o $(BLDDIR)/cdftext.o $(BLDDIR)/cdfed.o \
      $(BLDDIR)/cdfstr.o $(BLDDIR)/cdfvalid.o $(BLDDIR)/cdfmem.o \
      $(BLDDIR)/vstream.o $(BLDDIR)/epochu.o $(BLDDIR)/cdfmisc3.o \
      $(BLDDIR)/cdfhuff.o $(BLDDIR)/cdfrle.o $(BLDDIR)/cdfmisc1.o \
      $(BLDDIR)/cdfalloc.o $(BLDDIR)/cdfgzip.o $(BLDDIR)/cdfalloc64.o \
      $(BLDDIR)/cdfdel64.o $(BLDDIR)/cdfmisc2_64.o $(BLDDIR)/cdfread64.o \
      $(BLDDIR)/cdfclo64.o $(BLDDIR)/cdfget64.o $(BLDDIR)/cdfmisc3_64.o \
      $(BLDDIR)/cdfsel64.o $(BLDDIR)/cdfcon64.o $(BLDDIR)/cdfput1_64.o \
      $(BLDDIR)/cdfwrite64.o $(BLDDIR)/cdfcre64.o $(BLDDIR)/cdfmisc1_64.o \
      $(BLDDIR)/cdfput2_64.o $(BLDDIR)/vstream64.o $(BLDDIR)/cdfhyper64.o \
      $(BLDDIR)/cdfrle64.o $(BLDDIR)/cdfhuff64.o $(BLDDIR)/cdfgzip64.o \
      $(BLDDIR)/cdffsi2.o $(BLDDIR)/cdffsi3.o $(BLDDIR)/cdfcks.o \
      $(BLDDIR)/cdfmd5.o $(BLDDIR)/cdfsav.o $(BLDDIR)/cdfvalidator.o \
      $(BLDDIR)/cdfvalidator64.o $(BLDDIR)/cdftt2000.o \
      $(BLDDIR)/cdftt2000f.o $(ZLIB_OBJs)

CDFh= $(INCLUDEcdf)/cdf.h
CDFREVh= $(INCLUDEcdf)/cdfrev.h
CONFIGh= $(INCLUDEcdf)/cdfconfig.h
CDFDISTh= $(INCLUDEcdf)/cdfdist.h $(CDFh) $(CONFIGh)
CDFLIBh= $(INCLUDEcdf)/cdflib.h $(INCLUDEcdf)/cdflib64.h $(CDFDISTh)
CDFGZIPh=$(ZlibDIR)/zconf.h $(ZlibDIR)/zlib.h
CDFMD5h= $(CDFLIBh) $(INCLUDEcdf)/cdfmd5.h

#------------------------------------------------------------------------------
# Compile/link.
#------------------------------------------------------------------------------

all: $(BLDDIR)/libcdf.a shared.$(SHARED)

$(BLDDIR)/libcdf.a: $(OBJs)
	@if [ -f $@ ] ; then rm $@ ; else $(NULL) ; fi
	@echo $(AR) $(ARFLAGS) $@ $(OBJs)
	      $(AR) $(ARFLAGS) $@ $(OBJs)
	@$(MAKE) MAKE=$(MAKE) -f $(MAKEFILE) ranlib.$(RANLIB)

ranlib.yes:
	@echo $(RANLIBcmd) $(BLDDIR)/libcdf.a
	$(RANLIBcmd) $(BLDDIR)/libcdf.a

shared.yes: $(BLDDIR)/libcdf.$(SHAREDEXT)

$(BLDDIR)/libcdf.so \
$(BLDDIR)/libcdf.dylib \
$(BLDDIR)/libcdf.dll \
$(BLDDIR)/libcdf.sl: $(OBJs)
	@if [ -f $@ ] ; then rm $@ ; else $(NULL) ; fi
	@echo $(LD) $(LDFLAGS) -o $@ $(OBJs) $(SYSLIBS)
	      $(LD) $(LDFLAGS) -o $@ $(OBJs) $(SYSLIBS)

$(BLDDIR)/libcdf.o: $(OBJs)
	@echo $(CC) $(LDFLAGS) -o $@ $(OBJs) $(SYSLIBS)
	      $(CC) $(LDFLAGS) -o $@ $(OBJs) $(SYSLIBS)

$(BLDDIR)/%.o: %.c
	@if [ "$<" = "cdfgzip.c" ] || [ "$<" = "cdfgzip64.c" ] ; then \
	 echo $(CC) $(CFLAGS) $(CFLAGS2) -c $< -o $@ ; \
	      $(CC) $(CFLAGS) $(CFLAGS2) -c $< -o $@ ; \
	else \
	 echo $(CC) $(CFLAGS) -c $< -o $@ ; \
	      $(CC) $(CFLAGS) -c $< -o $@ ; \
	fi

$(BLDDIR)/cdflib.o:	cdflib.c $(CDFLIBh)
$(BLDDIR)/cdfcre.o:	cdfcre.c $(CDFLIBh) $(CDFREVh)
$(BLDDIR)/cdfope.o:	cdfope.c $(CDFLIBh) $(CDFREVh)
$(BLDDIR)/cdfdel.o:	cdfdel.c $(CDFLIBh)
$(BLDDIR)/cdfclo.o:	cdfclo.c $(CDFLIBh)
$(BLDDIR)/cdfget.o:	cdfget.c $(CDFLIBh) $(CDFREVh)
$(BLDDIR)/cdfput1.o:	cdfput1.c $(CDFLIBh)
$(BLDDIR)/cdfput2.o:	cdfput2.c $(CDFLIBh)
$(BLDDIR)/cdfsel.o:	cdfsel.c $(CDFLIBh)
$(BLDDIR)/cdfcon.o:	cdfcon.c $(CDFLIBh)
$(BLDDIR)/cdfwrite.o:	cdfwrite.c $(CDFLIBh)
$(BLDDIR)/cdfread.o:	cdfread.c $(CDFLIBh)
$(BLDDIR)/cdfmisc1.o:	cdfmisc1.c $(CDFLIBh) $(CDFREVh)
$(BLDDIR)/cdfmisc2.o:	cdfmisc2.c $(CDFLIBh)
$(BLDDIR)/cdfmisc3.o:	cdfmisc3.c $(CDFLIBh)
$(BLDDIR)/cdfalloc.o:	cdfalloc.c $(CDFLIBh) $(CDFREVh)
$(BLDDIR)/cdftext.o:	cdftext.c $(CDFLIBh)
$(BLDDIR)/cdfcsi.o:	cdfcsi.c $(CDFLIBh)
$(BLDDIR)/cdffsi.o: 	cdffsi.c $(CDFLIBh)
$(BLDDIR)/cdffsi2.o:	cdffsi2.c $(CDFLIBh)
$(BLDDIR)/cdffsi3.o:	cdffsi3.c $(CDFLIBh)
$(BLDDIR)/cdffii.o: 	cdffii.c $(CDFLIBh)
$(BLDDIR)/cdfstr.o:	cdfstr.c $(CDFLIBh)
$(BLDDIR)/cdfed.o:  	cdfed.c $(CDFLIBh)
$(BLDDIR)/cdfvalid.o:  	cdfvalid.c $(CDFLIBh)
$(BLDDIR)/cdfhyper.o:	cdfhyper.c $(CDFLIBh)
$(BLDDIR)/cdfhyper64.o:	cdfhyper64.c $(CDFLIBh)
$(BLDDIR)/cdfmem.o:	cdfmem.c $(CDFLIBh)
$(BLDDIR)/cdfhuff.o:	cdfhuff.c $(CDFLIBh)
$(BLDDIR)/cdfrle.o:	cdfrle.c $(CDFLIBh)
$(BLDDIR)/cdfgzip.o:	cdfgzip.c $(CDFLIBh) $(CDFGZIPh)
$(BLDDIR)/epochu.o:	epochu.c $(CDFLIBh)
$(BLDDIR)/epochuf.o:	epochuf.c $(CDFLIBh)
$(BLDDIR)/dirutils.o:	dirutils.c $(CDFLIBh)
$(BLDDIR)/vstream.o:	vstream.c $(CDFLIBh)
$(BLDDIR)/cdfalloc64.o:	cdfalloc64.c $(CDFLIBh) $(CDFREVh)
$(BLDDIR)/cdfclo64.o:	cdfclo64.c $(CDFLIBh)
$(BLDDIR)/cdfcon64.o:	cdfcon64.c $(CDFLIBh)
$(BLDDIR)/cdfcre64.o:	cdfcre64.c $(CDFLIBh) $(CDFREVh)
$(BLDDIR)/cdfdel64.o:	cdfdel64.c $(CDFLIBh)
$(BLDDIR)/cdfget64.o:	cdfget64.c $(CDFLIBh) $(CDFREVh)
$(BLDDIR)/cdfput1_64.o:	cdfput1_64.c $(CDFLIBh)
$(BLDDIR)/cdfput2_64.o:	cdfput2_64.c $(CDFLIBh)
$(BLDDIR)/cdfmisc1_64.o:	cdfmisc1_64.c $(CDFLIBh) $(CDFREVh)
$(BLDDIR)/cdfmisc2_64.o:	cdfmisc2_64.c $(CDFLIBh)
$(BLDDIR)/cdfmisc3_64.o:	cdfmisc3_64.c $(CDFLIBh)
$(BLDDIR)/cdfread64.o:	cdfread64.c $(CDFLIBh)
$(BLDDIR)/cdfsel64.o:	cdfsel64.c $(CDFLIBh)
$(BLDDIR)/cdfwrite64.o:	cdfwrite64.c $(CDFLIBh)
$(BLDDIR)/vstream64.o:	vstream64.c $(CDFLIBh)
$(BLDDIR)/cdfhuff64.o:	cdfhuff64.c $(CDFLIBh)
$(BLDDIR)/cdfrle64.o:	cdfrle64.c $(CDFLIBh)
$(BLDDIR)/cdfgzip64.o:	cdfgzip64.c $(CDFLIBh) $(CDFGZIPh)
$(BLDDIR)/cdfcks.o:	cdfcks.c $(CDFLIBh) $(CDFMD5h)
$(BLDDIR)/cdfmd5.o:	cdfmd5.c $(CDFLIBh) $(CDFMD5h)
$(BLDDIR)/cdfsav.o:	cdfsav.c $(CDFLIBh)
$(BLDDIR)/cdfvalidator.o:	cdfvalidator.c $(CDFLIBh)
$(BLDDIR)/cdfvalidator64.o:	cdfvalidator64.c $(CDFLIBh)
$(BLDDIR)/cdftt2000.o:	cdftt2000.c $(CDFLIBh)
$(BLDDIR)/cdftt2000f.o:	cdftt2000f.c $(CDFLIBh)

ranlib.no:
shared.no:

#------------------------------------------------------------------------------
# Clean/purge.
#------------------------------------------------------------------------------

clean:
	@-rm -f $(BLDDIR)/*.o
	@-rm -f $(BLDDIR)/core
	@-rm -f $(BLDDIR)/libcdf.a
	@-rm -f $(BLDDIR)/libcdf.*

purge:
	@-rm -f $(BLDDIR)/*~
	@-rm -f $(BLDDIR)/*#

