#===========================================================================
#
# SHARE Makefile.include
#
# author: Scott Binegar 
#
# 10/08/01  44600  WMiller    Change sys shared lib to osys
# 11/08/01  44751  WMiller    Add shared lib opus_jvm
# 02/28/02  45317  WMiller    Shared library link mechanism change
# 02/28/02  45317  Binegar    Change location of FITS libs
# 11/15/02  46765  Sontag     Changes for Linux and tux and gcc3.2
# 01/31/03  47450  Sontag     Added lines for AIX
# 08/18/03  41512  Sontag     Hush dependency generation
# 09/04/03  49367  Sontag     Classpath jar files for J2Ssh
# 11/18/03  49783  Sontag     Changes for AIX
# 12/11/03  49769  jschultz   changes for new cfitsio library
# 03/04/04  50605  Sontag     Move CFITSIO setting to .defs files
# 07/20/04  49860  Sontag     Build joapi on Windows via MinGW
# 09/16/05  53337  Sontag     Put ACE_ROOT in LD_LIB._64 too
# 05/05/06  54226  Sontag     Fix to the dep-generating greps
# 09/28/06  52073  Sontag     Port GUI's to OSX, handle Cygwin better
# 01/29/08  57952  Sontag     Handle x86_osx as well as ppc_osx
# 08/31/11  xxxxx  MSwam      Tweaks for science1/2 env
#===========================================================================

#
# only find TOPDIR if it hasn't been passed down to us
#
ifndef TOPDIR
  TOPDIR:=$(shell pwd | sed -e 's?/src.*??')
endif

# include operating system type and related parameters
include $(TOPDIR)/platform.defs

#
# Common Makefile definitions 
#
SRCINC=$(TOPDIR)/inc
OBJDIR=$(TOPDIR)/obj/$(OS_SUB_DIR)
LIBDIR=$(TOPDIR)/lib/$(OS_SUB_DIR)
BINDIR=$(TOPDIR)/bin/$(OS_SUB_DIR)
CXXREPOS=$(OBJDIR)/cxx_repository/ 

#
# Search paths for gmake
#
vpath %.o $(OBJDIR)
vpath %.dylib $(LIBDIR)
vpath %.so $(LIBDIR)
vpath %.a $(LIBDIR)
vpath %.h $(SRCINC)
vpath %.i $(SRCINC) 
vpath %.d $(CURDIR)/dep
vpath %.hxx $(SRCINC)
vpath %.jar $(BINDIR)
vpath %.c ../libs
vpath %.cpp ../libs

# Include platform specific variable defs
include $(TOPDIR)/src/$(OS_SUB_DIR).defs

#===================================================================================
#
# The following section contains common rules for compiling source, linking, etc 
#
#===================================================================================

.DELETE_ON_ERROR:
# (from the GNUmake documentation:
# "If .DELETE_ON_ERROR is mentioned as a target anywhere in the makefile, then
#  make will delete the target of a rule if it has changed and its commands exit
#  with a nonzero exit status, just as it does when it receives a signal."
#

# These are needed in order for the dptbuild account to run tao_idl
# <<<<<<<<<<<
# Add ACE paths to LD_LIBRARY_PATH as necessary
LD_LIBRARY_PATH := $(ACE_ROOT)/ace:$(ACE_ROOT)/lib:$(LD_LIBRARY_PATH) 
LD_LIBRARY_PATH_64 := $(ACE_ROOT)/ace:$(ACE_ROOT)/lib:$(LD_LIBRARY_PATH_64)
# And, for the Mac
DYLD_LIBRARY_PATH := $(ACE_ROOT)/ace:$(ACE_ROOT)/lib:$(DYLD_LIBRARY_PATH) 
# <<<<<<<<<<<


# Define rules for generating dependency files
# Remove any system files (path begins with /usr)
# Remove any ACE_ROOT files
# sleep 1 is necessary to prevent "time skew" warnings
ifeq ($(OSNAME),AIX)
define gen-deps
	@echo "Building dependency file: $@"
	@[ -d dep ] || mkdir dep
	@set -e; $(CXX) -I$(SRCINC) $(DEPFLAGS) $(CXXFLAGS) $< > $@; \
	echo "    " >> $@; \
	echo "$@ : $<" >> $@; \
	[ -s $@ ] || rm -f $@
	@sleep 1
endef
else
# Skip gen-deps on OSX until we do more serious dev. there.  It slows the bld.
ifneq ($(OSNAME),Darwin)
define gen-deps
	@echo "Building THIS dependency file: $@"
	@[ -d dep ] || mkdir dep
	#@set -e; $(CXX) $(DEPFLAGS) $(CXXFLAGS) $< \
	@set -e; $(CXX) $(DEPFLAGS) $< \
	| grep -v '^ *\/usr\/' \
	| grep -v '^ *$(ACE_ROOT)' > $@; \
	echo "    " >> $@; \
	echo "$@ : $<" >> $@; \
	[ -s $@ ] || rm -f $@
	@sleep 1
endef
else
define gen-deps
	@echo "Building dependency file: $@ (skipped)"
endef
endif
endif

dep/%.d: %.c
	$(gen-deps)

dep/%.d: %.cpp
	$(gen-deps)

dep/%.d: %.cxx
	$(gen-deps)


# Define rule for compiling c files:
%.o: %.c
	@echo "Compiling: $<"
	@[ -d $(OBJDIR) ] || mkdir -p $(OBJDIR)
	$(CC) -c $(CCFLAGS) $< -o $(OBJDIR)/$@


# Define rule for compiling cpp files:
%.o: %.cpp
	@echo "Compiling: $<"
	@[ -d $(OBJDIR) ] || mkdir -p $(OBJDIR)
	$(CXX) -c $(CXXFLAGS) $< -o $(OBJDIR)/$@

# Define rule for compiling cxx files :
%.o: %.cxx
	@echo "Compiling: $<"
	@[ -d $(OBJDIR) ] || mkdir -p $(OBJDIR)
	$(CXX) -c $(CXXFLAGS) $< -o $(OBJDIR)/$@

# Define rule for compiling java files:
%.class : %.java
	$(JAVAC) -Xlint -classpath ".:$(SYBJAR):$(J2SSHJAR):$(JUNITJAR)" $<


# Create the list of objects and depenency files from the sources file list
objects := $(patsubst %.c,%.o, $(sources))
objects := $(patsubst %.cpp,%.o, $(objects))
objects := $(patsubst %.cxx,%.o, $(objects))

deps := $(objects:.o=.d)
deps := $(addprefix dep/, $(deps))

# Define rule for generating share object files
ifneq ($(shared_lib_name),)

  ifeq ($(OS_SUB_DIR),cygwin_mingw)
    tgt_file = $(LIBDIR)/$(shared_lib_name).dll
    SOFLAGS += -Wl,--out-implib,$(tgt_file).a
  else
    tgt_file = $(LIBDIR)/lib$(shared_lib_name).so
    ifeq ($(findstring _osx,$(OS_SUB_DIR)),_osx)
      ifeq ($(shared_lib_for_jni),)
        tgt_file = $(LIBDIR)/lib$(shared_lib_name).dylib
      else
        tgt_file = $(LIBDIR)/lib$(shared_lib_name).jnilib
      endif
    endif
  endif

$(tgt_file) : $(objects)
	@echo "Building: $@ "
	@[ -d $(LIBDIR) ] || mkdir -p $(LIBDIR)

  # The following hinky syntax means: if ((os==cyg) && !(lib-for-jni))
  ifeq ($(OS_SUB_DIR)$(shared_lib_for_jni),cygwin_mingw)
    # use this specialized form for all Cygwin/MinGW DLL's not used for JNI
	cd $(OBJDIR); rm -f $(shared_lib_name).def; dlltool --export-all --output-def $(shared_lib_name).def --dllname $(shared_lib_name).dll $(objects)
	cd $(OBJDIR); cat $(shared_lib_name).def | sed 's/;.*//g' > $(shared_lib_name).deffix; mv $(shared_lib_name).deffix $(shared_lib_name).def
	cd $(OBJDIR); $(CXX) $(SOFLAGS) $(shared_lib_name).def $(objects) -o $@ $(SOLIBS)
  else
    # use this simple form for all other shared libs (even Cygwin libs for JNI)
	cd $(OBJDIR); $(CXX) $(SOFLAGS) $(objects) -o $@ $(SOLIBS)
  endif
#
# Mac JNI lib specifics
#
  ifeq ($(OSNAME),Darwin)
    ifneq ($(shared_lib_for_jni),)
    # Eg. joapi is needed in jni form for PMG, and dylib form for consumer_test
	cd $(LIBDIR); ln -s lib$(shared_lib_name).jnilib lib$(shared_lib_name).dylib
    endif
  endif
#
# AIX lib specifics
#
  ifeq ($(OSNAME),AIX)
	@[ -d $(LIBDIR)/non_rtl ] || mkdir -p $(LIBDIR)/non_rtl
    # On AIX we take away read-other; see notes in AIX port PR
	chmod 750 $@
    ifneq ($(shared_lib_for_jni),)
    # AIX non-rtl libs:
    # On AIX we drop this shared lib into an archive with the same base name.
    # Note that these are NOT static libraries.  This is for non-rtl lib builds.
	rm -f $(LIBDIR)/non_rtl/lib$(shared_lib_name).a
	rm -f $(LIBDIR)/non_rtl/shr.o
	cp -p $(LIBDIR)/lib$(shared_lib_name).so $(LIBDIR)/non_rtl/shr.o
	cd $(LIBDIR)/non_rtl; ar crv ./lib$(shared_lib_name).a shr.o
	chmod 750 $(LIBDIR)/non_rtl/lib$(shared_lib_name).a
	rm -f $(LIBDIR)/non_rtl/shr.o
	sleep 1
    else
    # AIX rtl libs:
    # On AIX the non-rtl libs and apps need to see .a versions of all
    # libs they use, though they don't necessarily need them to be archives
    # so create symbolic links.
	rm -f $(LIBDIR)/non_rtl/lib$(shared_lib_name).a
	cd $(LIBDIR)/non_rtl; ln -s ../lib$(shared_lib_name).so lib$(shared_lib_name).a
    endif
  endif

$(shared_lib_name) : $(tgt_file)
	@;

endif


# Define rule for generating binary image files

ifneq ($(bin_name),)

tgt_file =  $(BINDIR)/$(bin_name)

$(BINDIR)/$(bin_name) : $(objects)
	@echo "Building: $(bin_name)"
	@[ -d $(BINDIR) ] || mkdir -p $(BINDIR)
	cd $(OBJDIR); $(CXX) $(LINKFLAGS) $(objects) -o $@ $(LINKLIBS)
  ifeq ($(OS_SUB_DIR),cygwin_mingw)
	@echo Skipping chmod ugo+x $(@).exe
  else
	@chmod ugo+x $(@)
  endif

# would use something like this, below for the "cd; g++" line to use Purify
# cd $(OBJDIR); purify $(CXX) $(LINKFLAGS) $(objects) -o $@ $(LINKLIBS)

$(bin_name) : $(BINDIR)/$(bin_name)
	@;

endif

#
# Define rule for generating archive object files
#
ifneq ($(lib_name),)

tgt_file =  $(LIBDIR)/lib$(lib_name).a

$(LIBDIR)/lib$(lib_name).a : $(objects)
	@echo "Building: $@ "
	@[ -d $(LIBDIR) ] || mkdir -p $(LIBDIR)
	cd $(OBJDIR); $(AR) -cru $@ $(objects)
  ifeq ($(OSNAME),AIX)
	@[ -d $(LIBDIR)/non_rtl ] || mkdir -p $(LIBDIR)/non_rtl
	rm -f $(LIBDIR)/non_rtl/lib$(lib_name).a
	cd $(LIBDIR)/non_rtl; ln -s ../lib$(lib_name).a lib$(lib_name).a
  endif

$(lib_name) : $(LIBDIR)/lib$(lib_name).a
	@;

endif


.PHONY : default
default :
	@echo "No target defined for $(OS_SUB_DIR)" 


# if local_clean is defined as a null string, do the generic clean
ifeq ($(local_clean),)
.PHONY : clean
clean :
	@echo "Cleaning up files for: $(bin_name) $(shared_lib_name)"
	@cd $(OBJDIR); rm -f $(objects)
	@rm -f $(deps)
	@rm -f $(tgt_file)
    ifeq ($(OSNAME),AIX)
	@rm -f $(LIBDIR)/non_rtl/lib$(shared_lib_name).a
    endif
    ifeq ($(OS_SUB_DIR),cygwin_mingw)
      ifneq ($(shared_lib_name),)
	@rm -f $(LIBDIR)/$(shared_lib_name).*
	@rm -f $(OBJDIR)/$(shared_lib_name).def
      else
	@rm -f $(BINDIR)/$(bin_name).exe
      endif
    endif
endif

.PHONY :clean_deps
clean_deps:
	rm -f $(deps)

.PHONY : printit
printit:
	echo $(SRCINC)
	echo $(OBJDIR)
	echo $(LIBDIR)
	echo $(BINDIR)
	
#
# Do not include the dependency files if $deps is not defined or 
# if this is a "clean".
#
ifdef deps
  ifneq ($(MAKECMDGOALS),clean) 
    -include $(deps)
  endif
endif

