#------------------------------------------------------------------------------
# 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

SRCDIR=
BUILDDIR=
SHARED=
PIC=
CCx=
LDx=
COPTIONS=
COPTIONSld=
SYSLIBS=
SHAREDEXT=
LDOPTIONS=
AROPTIONS=
RANLIB=
RANLIBcmd=
VERSION=
MAKE=
ZLIB=
ARCH2=
AR=

INCLUDEcdf=$(SRCDIR)/../include
OUTZLIB=$(BUILDDIR)/$(ZLIB)
CC=$(CCx)
LD=$(LDx)

CFLAGS=$(COPTIONS) $(PIC) -I$(INCLUDEcdf)
ARFLAGS=$(AROPTIONS)

CFLAGS2=
ARCH_OBJs=
GENERIC_OBJs=
ifeq ($(ZLIB),zlib)
# original zlib version
  SRCZLIB=$(SRCDIR)/$(ZLIB)
  ZLIBs_OBJs := $(wildcard $(OUTZLIB)/*.o)
  ZLIBd_OBJs := $(wildcard $(OUTZLIB)/*.o)
  CFLAGS2=-DZ_PREFIX
else
# zlib-ng version
  SRCZLIB=$(BUILDDIR)/$(ZLIB)
  $(shell rm -f $(OUTZLIB)/gz*.o)
  $(shell rm -f $(OUTZLIB)/mini*.o)
  $(shell rm -f $(OUTZLIB)/make*.o)
  $(shell rm -f $(OUTZLIB)/examp*.o)
  $(shell rm -f $(OUTZLIB)/*test*.o)
  $(shell rm -f $(OUTZLIB)/libz.a)
  ZLIBs_OBJs := $(wildcard $(OUTZLIB)/*.o)
  GENERICs_OBJs := $(wildcard $(OUTZLIB)/arch/generic/*.o)
  ZLIBd_OBJs := $(wildcard $(OUTZLIB)/*.lo)
  GENERICd_OBJs := $(wildcard $(OUTZLIB)/arch/generic/*.lo)
endif 

CDFOBJs= cdflib.o cdfcre.o cdfope.o cdfclo.o cdfdel.o cdfsel.o \
         cdfcon.o cdfget.o cdfmisc2.o cdfread.o cdfwrite.o cdfhyper.o \
         epochuf.o dirutils.o cdfput1.o cdfput2.o cdfcsi.o cdffsi.o \
         cdffii.o cdftext.o cdfed.o cdfstr.o cdfvalid.o cdfmem.o \
         vstream.o epochu.o cdfmisc3.o cdfhuff.o cdfrle.o cdfmisc1.o \
         cdfalloc.o cdfgzip.o cdfalloc64.o cdfdel64.o cdfmisc2_64.o \
         cdfread64.o cdfclo64.o cdfget64.o cdfmisc3_64.o cdfsel64.o \
         cdfcon64.o cdfput1_64.o cdfwrite64.o cdfcre64.o cdfmisc1_64.o \
         cdfput2_64.o vstream64.o cdfhyper64.o cdfrle64.o cdfhuff64.o \
         cdfgzip64.o cdffsi2.o cdffsi3.o cdfcks.o cdfmd5.o cdfsav.o \
         cdfvalidator.o cdfvalidator64.o cdftt2000.o cdftt2000f.o
sALLOBJs= $(CDFOBJs) $(ZLIBs_OBJs) $(GENERICs_OBJs)
dALLOBJs= $(CDFOBJs) $(ZLIBd_OBJs) $(GENERICd_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=$(SRCZLIB)/zconf.h $(SRCZLIB)/zlib.h
CDFMD5h= $(CDFLIBh) $(INCLUDEcdf)/cdfmd5.h

CDFVersion=
OS=$(shell uname)
ifeq ($(OS),Darwin)
  CDFVersion=-compatibility_version 3.0.0 -current_version $(VERSION)
endif

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

all: libcdf.a shared.$(SHARED)

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

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

shared.yes: libcdf.$(SHAREDEXT)

libcdf.so \
libcdf.dylib \
libcdf.dll \
libcdf.sl: $(dALLOBJs)
	@if [ -f $@ ] ; then rm $@ ; else $(NULL) ; fi
	@echo $(LD) $(LDOPTIONS) $(COPTIONSld) $(CDFVersion) -o $@ $(dALLOBJs) $(SYSLIBS)
	      $(LD) $(LDOPTIONS) $(COPTIONSld) $(CDFVersion) -o $@ $(dALLOBJs) $(SYSLIBS)

libcdf.o: $(OBJs)
	@echo $(CC) $(LDOPTIONS) $(COPTIONSld) -o $@ $(OBJs) $(SYSLIBS)
	      $(CC) $(LDOPTIONS) $(COPTIONSld) -o $@ $(OBJs) $(SYSLIBS)

cdflib.o: $(SRCDIR)/cdflib.c $(CDFLIBh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdflib.c
	       $(CC) $(CFLAGS) -c $(SRCDIR)/cdflib.c

cdfcre.o: $(SRCDIR)/cdfcre.c $(CDFLIBh) $(CDFREVh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfcre.c
	       $(CC) $(CFLAGS) -c $(SRCDIR)/cdfcre.c

cdfope.o: $(SRCDIR)/cdfope.c $(CDFLIBh) $(CDFREVh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfope.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/cdfope.c

cdfdel.o: $(SRCDIR)/cdfdel.c $(CDFLIBh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfdel.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/cdfdel.c

cdfclo.o: $(SRCDIR)/cdfclo.c $(CDFLIBh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfclo.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/cdfclo.c

cdfget.o: $(SRCDIR)/cdfget.c $(CDFLIBh) $(CDFREVh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfget.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/cdfget.c

cdfput1.o: $(SRCDIR)/cdfput1.c $(CDFLIBh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfput1.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/cdfput1.c

cdfput2.o: $(SRCDIR)/cdfput2.c $(CDFLIBh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfput2.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/cdfput2.c

cdfsel.o: $(SRCDIR)/cdfsel.c $(CDFLIBh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfsel.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/cdfsel.c

cdfcon.o: $(SRCDIR)/cdfcon.c $(CDFLIBh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfcon.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/cdfcon.c

cdfwrite.o: $(SRCDIR)/cdfwrite.c $(CDFLIBh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfwrite.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/cdfwrite.c

cdfread.o: $(SRCDIR)/cdfread.c $(CDFLIBh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfread.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/cdfread.c

cdfmisc1.o: $(SRCDIR)/cdfmisc1.c $(CDFLIBh) $(CDFREVh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfmisc1.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/cdfmisc1.c

cdfmisc2.o: $(SRCDIR)/cdfmisc2.c $(CDFLIBh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfmisc2.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/cdfmisc2.c

cdfmisc3.o: $(SRCDIR)/cdfmisc3.c $(CDFLIBh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfmisc3.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/cdfmisc3.c

cdfalloc.o: $(SRCDIR)/cdfalloc.c $(CDFLIBh) $(CDFREVh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfalloc.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/cdfalloc.c

cdftext.o: $(SRCDIR)/cdftext.c $(CDFLIBh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdftext.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/cdftext.c

cdfcsi.o: $(SRCDIR)/cdfcsi.c $(CDFLIBh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfcsi.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/cdfcsi.c

cdffsi.o: $(SRCDIR)/cdffsi.c $(CDFLIBh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdffsi.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/cdffsi.c

cdffsi2.o: $(SRCDIR)/cdffsi2.c $(CDFLIBh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdffsi2.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/cdffsi2.c

cdffsi3.o: $(SRCDIR)/cdffsi3.c $(CDFLIBh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdffsi3.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/cdffsi3.c

cdffii.o: $(SRCDIR)/cdffii.c $(CDFLIBh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdffii.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/cdffii.c

cdfstr.o: $(SRCDIR)/cdfstr.c $(CDFLIBh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfstr.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/cdfstr.c

cdfed.o: $(SRCDIR)/cdfed.c $(CDFLIBh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfed.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/cdfed.c

cdfvalid.o: $(SRCDIR)/cdfvalid.c $(CDFLIBh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfvalid.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/cdfvalid.c

cdfhyper.o: $(SRCDIR)/cdfhyper.c $(CDFLIBh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfhyper.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/cdfhyper.c

cdfhyper64.o: $(SRCDIR)/cdfhyper64.c $(CDFLIBh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfhyper64.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/cdfhyper64.c

cdfmem.o: $(SRCDIR)/cdfmem.c $(CDFLIBh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfmem.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/cdfmem.c

cdfhuff.o: $(SRCDIR)/cdfhuff.c $(CDFLIBh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfhuff.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/cdfhuff.c

cdfrle.o: $(SRCDIR)/cdfrle.c $(CDFLIBh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfrle.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/cdfrle.c

cdfgzip.o: $(SRCDIR)/cdfgzip.c $(CDFLIBh) $(CDFGZIPh)
	  @echo $(CC) $(CFLAGS) -I $(SRCZLIB) $(CFLAGS2) -c $(SRCDIR)/cdfgzip.c
	        $(CC) $(CFLAGS) -I $(SRCZLIB) $(CFLAGS2) -c $(SRCDIR)/cdfgzip.c

epochu.o: $(SRCDIR)/epochu.c $(CDFLIBh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/epochu.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/epochu.c

epochuf.o: $(SRCDIR)/epochuf.c $(CDFLIBh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/epochuf.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/epochuf.c

dirutils.o: $(SRCDIR)/dirutils.c $(CDFLIBh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/dirutils.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/dirutils.c

vstream.o: $(SRCDIR)/vstream.c $(CDFLIBh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/vstream.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/vstream.c

cdfalloc64.o: $(SRCDIR)/cdfalloc64.c $(CDFLIBh) $(CDFREVh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfalloc64.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/cdfalloc64.c

cdfclo64.o: $(SRCDIR)/cdfclo64.c $(CDFLIBh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfclo64.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/cdfclo64.c

cdfcon64.o: $(SRCDIR)/cdfcon64.c $(CDFLIBh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfcon64.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/cdfcon64.c

cdfcre64.o: $(SRCDIR)/cdfcre64.c $(CDFLIBh) $(CDFREVh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfcre64.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/cdfcre64.c

cdfdel64.o: $(SRCDIR)/cdfdel64.c $(CDFLIBh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfdel64.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/cdfdel64.c

cdfget64.o: $(SRCDIR)/cdfget64.c $(CDFLIBh) $(CDFREVh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfget64.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/cdfget64.c

cdfput1_64.o: $(SRCDIR)/cdfput1_64.c $(CDFLIBh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfput1_64.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/cdfput1_64.c

cdfput2_64.o: $(SRCDIR)/cdfput2_64.c $(CDFLIBh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfput2_64.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/cdfput2_64.c

cdfmisc1_64.o: $(SRCDIR)/cdfmisc1_64.c $(CDFLIBh) $(CDFREVh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfmisc1_64.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/cdfmisc1_64.c

cdfmisc2_64.o: $(SRCDIR)/cdfmisc2_64.c $(CDFLIBh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfmisc2_64.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/cdfmisc2_64.c

cdfmisc3_64.o: $(SRCDIR)/cdfmisc3_64.c $(CDFLIBh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfmisc3_64.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/cdfmisc3_64.c

cdfread64.o: $(SRCDIR)/cdfread64.c $(CDFLIBh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfread64.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/cdfread64.c

cdfsel64.o: $(SRCDIR)/cdfsel64.c $(CDFLIBh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfsel64.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/cdfsel64.c

cdfwrite64.o: $(SRCDIR)/cdfwrite64.c $(CDFLIBh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfwrite64.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/cdfwrite64.c

vstream64.o: $(SRCDIR)/vstream64.c $(CDFLIBh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/vstream64.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/vstream64.c

cdfhuff64.o: $(SRCDIR)/cdfhuff64.c $(CDFLIBh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfhuff64.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/cdfhuff64.c

cdfrle64.o: $(SRCDIR)/cdfrle64.c $(CDFLIBh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfrle64.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/cdfrle64.c

cdfgzip64.o: $(SRCDIR)/cdfgzip64.c $(CDFLIBh) $(CDFGZIPh)
	  @echo $(CC) $(CFLAGS) -I $(SRCZLIB) $(CFLAGS2) -c $(SRCDIR)/cdfgzip64.c
	        $(CC) $(CFLAGS) -I $(SRCZLIB) $(CFLAGS2) -c $(SRCDIR)/cdfgzip64.c

cdfcks.o: $(SRCDIR)/cdfcks.c $(CDFLIBh) $(CDFMD5h)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfcks.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/cdfcks.c

cdfmd5.o: $(SRCDIR)/cdfmd5.c $(CDFLIBh) $(CDFMD5h)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfmd5.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/cdfmd5.c

cdfsav.o: $(SRCDIR)/cdfsav.c $(CDFLIBh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfsav.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/cdfsav.c

cdfvalidator.o:	$(SRCDIR)/cdfvalidator.c $(CDFLIBh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfvalidator.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/cdfvalidator.c

cdfvalidator64.o: $(SRCDIR)/cdfvalidator64.c $(CDFLIBh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdfvalidator64.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/cdfvalidator64.c

cdftt2000.o: $(SRCDIR)/cdftt2000.c $(CDFLIBh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdftt2000.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/cdftt2000.c

cdftt2000f.o: $(SRCDIR)/cdftt2000f.c $(CDFLIBh)
	  @echo $(CC) $(CFLAGS) -c $(SRCDIR)/cdftt2000f.c
	        $(CC) $(CFLAGS) -c $(SRCDIR)/cdftt2000f.c

ranlib.no:
shared.no:

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

clean:
	@-rm -f *.o
	@-rm -f core
	@-rm -f libcdf.*

purge:
	@-rm -f *~
	@-rm -f *#

